From patchwork Wed Nov 15 12:11:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 165322 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp2492190vqg; Wed, 15 Nov 2023 04:12:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IHFtmnE5Js85uuubmxKrk5wsw1GK8sMDjRRTQX0BTzC+WfTwxBBh8DEByyCJQNdETIKD1X/ X-Received: by 2002:a0c:f7c7:0:b0:671:e8d3:5a0f with SMTP id f7-20020a0cf7c7000000b00671e8d35a0fmr4794544qvo.64.1700050338907; Wed, 15 Nov 2023 04:12:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700050338; cv=pass; d=google.com; s=arc-20160816; b=hEUaNcuj6ZPIsexxYF6x2HIRbZFcP5O97rmuT5FR+/w7ERW2nddKo2BPQw+8t/XLs6 szbHheW89+lle4VyJeI7AuH25iU7BrCLN9KFhVRWkzdH92uGHcf55zDmaGkP4IzZhzgG I4RiGRR6xhAVNKYOOKwXlXr/9jRpZIuyq6G5c4tElmCLBllxaqDcy/HF71eXletDBW5c rETbOpaQxVqSAb9hxe6EoKAPOSK1D/XfgAuVn3d6KUdbrmSpaTEOIpjfh27TITVt9+Os JAqcK0QU9Q4n9aEgB81PsR9j5ZfPX4nwxOrBV4UcH1443AG1xznQHkdZ2uaaizS8dxVI pC7g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:errors-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:mime-version :user-agent:subject:to:from:date:dkim-signature:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=OGdOup5ObSbL5O+8+wb4a7kdzsU5je0AilmVuBo2f1c=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=djRVcinU9k9U+q2EnV0TMvVhaUNB0tWgs0X/Xjh0pXdsGsAX5gBFflqybw3iHvL0+D LBuCKw1PuZfbS5nP2qdeaPk9jY/U6G1p+/gZg+9xALyrL2GM5RDLXFXnOgr4fb7F+39l ilsAJKExDs/6BmXcoh0zbzR/aPto0M+lmc/9jGNoQARWI9VJ5BT7npHvKrBHjBQVfdri /lEXUuFqQDvgLLZeE8Y1LYjd5zNzxDU5Dl0bS5zkinf01w84w5hsfcoT03DWwzRMgqi+ XdfF1nKkPVufF3xAvyqIzzGSSzQlmHovShDUgThdyRkVWGO4N/Di4y7jeopEJ9OIHnKW /6ww== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b="Bq/yeNBY"; dkim=neutral (no key) header.i=@suse.de header.b=pytzLNZo; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x7-20020a05622a000700b0041cb4580d37si9120551qtw.359.2023.11.15.04.12.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 04:12:18 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b="Bq/yeNBY"; dkim=neutral (no key) header.i=@suse.de header.b=pytzLNZo; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 09387385B531 for ; Wed, 15 Nov 2023 12:12:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id C69D43857007 for ; Wed, 15 Nov 2023 12:11:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C69D43857007 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C69D43857007 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:2178:6::1c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700050313; cv=none; b=Wpq35CknB+ORZQCJJWEn5DRfbkK+fdeibjkTKZG+Eo7d0BMS6Il9GM0Yyfjzy/hWOjm4NGzmL135a4hGWmS2seICBYgYQAeHRp4f3ckzGpPC917sYTNxoUVnTqabr5egDYFuSV0mI1Qr1q0lTcfLpCU0tKYCOUwk23/OIAD3I7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700050313; c=relaxed/simple; bh=XzVN4qqUx8+dVMVdH0SODraD6Us3E2vCM3Z6kwYtETs=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:MIME-Version; b=X/JIr9I1Zhzfq/T3CtWV9jbngXu6dCMf3y5+TP5KYkkPjgpG/i/jEWvUo+uy5rjV1VGKNTYZMeN8n7zwwSNhU2n86JHESsjoiUW3Fu+tvqdTgNkFR7+WcRQALziDcu3o5H5UPq34dUXQJoSjcMpvB9UVN3GZEbbFGqWH5ibp5KQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 9E8F52199D for ; Wed, 15 Nov 2023 12:11:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1700050310; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=OGdOup5ObSbL5O+8+wb4a7kdzsU5je0AilmVuBo2f1c=; b=Bq/yeNBYOLqy6VLS81nZkUOBSMtQZZZvr0AvjdP9g8Q8ecM+KLusMHHKdSeoSed669maGW 74ZKvcSmOwWdNLnBL48ZFHAHBpAfzQEaaAZJw8UI6lfKY+3P2H9zSAjbiAjrO2v1uIPWBm Em114Sbr+G5INDVa+qkuWNZ6xLQnK1E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1700050310; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=OGdOup5ObSbL5O+8+wb4a7kdzsU5je0AilmVuBo2f1c=; b=pytzLNZor6u/bOVT3+O7BxdzaXdkeaMO/pxpf0iqAC3lBMv6vkxTeh+IrZEsPMHFHk9xjQ EbRAWemGxyw6EVAQ== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 7A9F62C4BF for ; Wed, 15 Nov 2023 12:11:50 +0000 (UTC) Date: Wed, 15 Nov 2023 12:11:50 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/112282 - wrong-code with ifcvt hoisting User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Level: X-Spamd-Bar: / Authentication-Results: smtp-out1.suse.de; dkim=none; dmarc=none; spf=softfail (smtp-out1.suse.de: 149.44.160.134 is neither permitted nor denied by domain of rguenther@suse.de) smtp.mailfrom=rguenther@suse.de X-Rspamd-Server: rspamd2 X-Spamd-Result: default: False [-0.01 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; RWL_MAILSPIKE_GOOD(0.00)[149.44.160.134:from]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; TO_DN_NONE(0.00)[]; R_SPF_SOFTFAIL(0.60)[~all:c]; RCPT_COUNT_ONE(0.00)[1]; MISSING_MID(2.50)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MX_GOOD(-0.01)[]; NEURAL_HAM_SHORT(-1.00)[-1.000]; DMARC_NA(0.20)[suse.de]; VIOLATED_DIRECT_SPF(3.50)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_NO_TLS_LAST(0.10)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.20)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: -0.01 X-Rspamd-Queue-Id: 9E8F52199D X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SCC_5_SHORT_WORD_LINES, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Message-Id: <20231115121218.09387385B531@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782631983939352165 X-GMAIL-MSGID: 1782631983939352165 The following avoids hoisting of invariants from conditionally executed parts of an if-converted loop. That now makes a difference since we perform bitfield lowering even when we do not actually if-convert the loop. if-conversion deals with resetting flow-sensitive info when necessary already. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/112282 * tree-if-conv.cc (ifcvt_hoist_invariants): Only hoist from the loop header. * gcc.dg/torture/pr112282.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr112282.c | 132 ++++++++++++++++++++++++ gcc/tree-if-conv.cc | 44 ++++---- 2 files changed, 153 insertions(+), 23 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr112282.c diff --git a/gcc/testsuite/gcc.dg/torture/pr112282.c b/gcc/testsuite/gcc.dg/torture/pr112282.c new file mode 100644 index 00000000000..23e0ed64b82 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr112282.c @@ -0,0 +1,132 @@ +/* { dg-do run } */ + +int printf(const char *, ...); +void __assert_fail(); +int a, g, h, i, v, w = 2, x, y, ab, ac, ad, ae, af, ag; +static int f, j, m, n, p, r, u, aa; +struct b { + int c : 20; + int d : 20; + int e : 10; +}; +static struct b l, o, q = {3, 3, 5}; +int s(int z) { + struct b ah; + int ai = 1, aj[7] = {1, 1, 1, 1, 1, 1, 1}; +ak: + for (u = -22; u < 2; ++u) { + struct b al[8] = {{2, 7, 9}, {8, 7, 1}, {2, 7, 9}, {8, 7, 1}, {2, 7, 9}, {8, 7, 1}, {2, 7, 9}}; + y = z = 0; + for (; z < 2; z++) { + int am[18], k; + ab = ac = 0; + for (; ac < 1; ac++) + for (k = 0; k < 9; k++) + am[k] = 0; + n = 0; + while (1) { + v = u < 0 || a; + h = z < ~u && 4 & q.c; + if ((aa <= l.c) > q.d && p) + return o.c; + if (w) + break; + return q.e; + } + a = j; + } + } + for (x = 0; x < 2; x++) { + struct b an = {1, 8, 4}; + int ao[28] = {5, 0, 0, 9, 0, 3, 0, 5, 0, 0, 9, 0, 3, 0, 5, 0, 0, 9, 0, 3, 0, 5, 0, 0, 9, 0, 3, 0}; + if (q.e) { + int ap = ai || l.c + q.c, aq = q.d, ar = p & f; + q.d = q.d || ar || ap; + p = 0; + if (!j && ai) + goto as; + if (q.d) { + printf("", l); + q.d = f >> j; + } + p = l.c = aq; + an = q; + } else { + int at[12][1] = {{9}, {9}, {5}, {9}, {9}, {5}, {9}, {9}, {5}, {9}, {9}, {5}}; + struct b au; + if (o.c) + aa = ah.e; + if (an.d) + ah.e = (j & (aa * m)) ^ au.d; + o.c = m + aa; + int av = o.c || 0, aw = ai || q.c & l.c, ax = n; + if (q.e < ai) + q = an; + if (r) + break; + ai = aw - av; + an.e = 0; + if (ai) { + an.e = l.c || 0; + f = q.c; + ah.e = l.c % q.d; + q.c = au.e; + if ((q.d && q.c) || ah.e) + __assert_fail(); + q.c = 0; + if (au.d > m || ah.e) + w = au.c | (n & ah.c); + as: + ae = af = ah.c; + int ay = au.d & q.e & au.c || o.c, az = 0 || o.c, ba = m & ah.d; + if (n) + au.c = au.e = (q.e || ah.d) ^ (o.c + (az / au.e)); + n = au.c || au.e; + if (ba) { + printf("", ax); + x = q.e | m; + continue; + } + m = ay; + n = printf("", au); + } + if (ah.d) + o.c = l.c & o.c & q.c; + if (q.d) + __assert_fail(); + printf("", an); + printf("", q); + printf("", au); + if (ah.e) + while (u++) { + struct b al[7] = {{7, 9, 8}, {7, 1, 2}, {7, 9, 8}, {7, 1, 2}, {7, 9, 8}, {7, 1, 2}, {7, 9, 0}}; + if (an.d) { + int d[8] = {0, 1, 0, 1, 0, 1, 0, 1}; + if (ad) + goto ak; + while (ag) + g = an.d = i = m; + f = j; + } + n++; + } + f = q.d; + } + if (l.c && m) { + int d[7] = {1, 0, 1, 0, 1, 0, 1}; + if (x) + h = an.d; + else + g = 0; + } + } + int bb = (q.d ^ ah.c) | aa | (q.e & q.c) | (f & ah.d); + if (bb) + return x; + return 0; +} +int main() { + j = 1; + s(0); + return 0; +} diff --git a/gcc/tree-if-conv.cc b/gcc/tree-if-conv.cc index 0190cf2369e..0bde281c246 100644 --- a/gcc/tree-if-conv.cc +++ b/gcc/tree-if-conv.cc @@ -3468,30 +3468,28 @@ ifcvt_can_hoist (class loop *loop, edge pe, gimple *stmt) static void ifcvt_hoist_invariants (class loop *loop, edge pe) { + /* Only hoist from the now unconditionally executed part of the loop. */ + basic_block bb = loop->header; gimple_stmt_iterator hoist_gsi = {}; - unsigned int num_blocks = loop->num_nodes; - basic_block *body = get_loop_body (loop); - for (unsigned int i = 0; i < num_blocks; ++i) - for (gimple_stmt_iterator gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);) - { - gimple *stmt = gsi_stmt (gsi); - if (ifcvt_can_hoist (loop, pe, stmt)) - { - /* Once we've hoisted one statement, insert other statements - after it. */ - gsi_remove (&gsi, false); - if (hoist_gsi.ptr) - gsi_insert_after (&hoist_gsi, stmt, GSI_NEW_STMT); - else - { - gsi_insert_on_edge_immediate (pe, stmt); - hoist_gsi = gsi_for_stmt (stmt); - } - continue; - } - gsi_next (&gsi); - } - free (body); + for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);) + { + gimple *stmt = gsi_stmt (gsi); + if (ifcvt_can_hoist (loop, pe, stmt)) + { + /* Once we've hoisted one statement, insert other statements + after it. */ + gsi_remove (&gsi, false); + if (hoist_gsi.ptr) + gsi_insert_after (&hoist_gsi, stmt, GSI_NEW_STMT); + else + { + gsi_insert_on_edge_immediate (pe, stmt); + hoist_gsi = gsi_for_stmt (stmt); + } + continue; + } + gsi_next (&gsi); + } } /* Returns the DECL_FIELD_BIT_OFFSET of the bitfield accesse in stmt iff its