From patchwork Wed Oct 18 10:03:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 154790 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp4678985vqb; Wed, 18 Oct 2023 03:03:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH1zZsKpPfPbJlvWnv6z8nQu064VxssXI4P5PaV6vaZRvgghOgW+5fDZbktG5D8mnR3PvxI X-Received: by 2002:a05:620a:25c8:b0:773:aa77:91d5 with SMTP id y8-20020a05620a25c800b00773aa7791d5mr5182073qko.33.1697623424304; Wed, 18 Oct 2023 03:03:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697623424; cv=pass; d=google.com; s=arc-20160816; b=c5ILtf4AnnHqTL8qPj3wf/zYnvQPgAa/KsWbE/vFFDtkCkYhawHo3gBhJuMAKoUnss gp3XlG17JhBqQP9K9zq8rKpRoCpVWrLVRkt+C1EMDN5L7shy9ZG9zuGwtypeHGCSGlv1 Bi4htUITsxJBOnARwbgmGesWlitl0Es/yUY4Nnwdgg55BhtJ5vlbPaEHeeXmbBhEY71y acWjnD+1l7Y+eQdPnRM/3ihPL/MZV2n1WYlb0XJzIroQ/Hu040FgrTM3HBeiiNrq7A4I 1hE3VuFkxPY3uRZQFpPbUSnll7qgFX27pIYjVmWkmEWcFKFVkbdEju3wuZHwc/RA8hvw xmWg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=4ZzQCqeDuN901vRPTguU4kIPt93vj7LRosnAVMJ0fHU=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=EEzPJ7HzjO/r59a1HI3J4nSoUSWeJqV530AHDRPopRxK1hLnDeOAlGm5jtPKDvK+nu qPCPo57A4sgJV3d4Q9YIiarUr2Ep2HeMRjQGOuEya7PNHrBd/q6TLvCtscHg2auxbTIE NHMJwbvYIH3Yc2iI30gVbsl0s8rnol9dBbPdL65pLO7M90kc2keORK+beO8MWFnmOBv2 o28zBFYQ5KpWsPHG+7OmVd1JmD+HRe3m8Z8KOxDlP9gVIQ6S3hHR+rD+2JcJenukUmk1 6FHHadGz0Lr2xjJMiYW03aCJcW+rMOqILe8A7zD2ZlvY+MLURK5XXhOPPkGImyPus36F 4waQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QKVSqDjn; 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id t9-20020a05622a01c900b004198ca326a2si2468896qtw.507.2023.10.18.03.03.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 03:03:44 -0700 (PDT) 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=@redhat.com header.s=mimecast20190719 header.b=QKVSqDjn; 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0B712385C6FA for ; Wed, 18 Oct 2023 10:03:44 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C773D3858C01 for ; Wed, 18 Oct 2023 10:03:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C773D3858C01 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C773D3858C01 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697623398; cv=none; b=d8+kDi9wRKE9knONXEDJvZLimaDcIQMNf0taOs2gOsmroioRm0btpERBV6+jS5jfSh/wkvTsaHpqHCETeECRiZkEpqGrYvYzpgMObNsqtMibiEqzVfLdazdVezdnmdUeZ6Ffm4BAVKUb6c5xS4bOs6XjMuausCYjpd0UVpvlMCk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697623398; c=relaxed/simple; bh=TXdGtNW6r2i97pCw1dbbn4/+ItNao7Y/GHc1TCOOrB4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=tKJjcelbAb1Jpu5gVpCqnhpyshZcAar2RF0ZLYH25LEyI40sAtr4fFJP3EITV5frLR7ipcffLN5hWceO6yhKAhe9hIC2Yqb25Hf3LH+ktAxiyVs/Uqnkq/nz081u5A0pAPkNQLsKF2h5LDwjmZPHTcAQOS7ycTFO9wS89n20410= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697623396; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=4ZzQCqeDuN901vRPTguU4kIPt93vj7LRosnAVMJ0fHU=; b=QKVSqDjnC2f+2uHIiyyab13TsohY0kher73OBXUjbAcfNWlE7WkGlX7dFawiqd0ee9rEQf qtwr6kTkLsynXzUOVitLH7J7ifGRoDs31n9RQSdm2Mw8mxjMxAeXkAyI74uMnB297uF9N7 xnAhWDzLE8xLxm4QYOXPjgOLOPJok4w= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-615-_6f0qqbJN-Gpz_XgnBd_Sg-1; Wed, 18 Oct 2023 06:03:14 -0400 X-MC-Unique: _6f0qqbJN-Gpz_XgnBd_Sg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6CC6D185A78E; Wed, 18 Oct 2023 10:03:13 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2FA6E492BEE; Wed, 18 Oct 2023 10:03:13 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 39IA3AvD2857025 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 18 Oct 2023 12:03:10 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 39IA39hm2857024; Wed, 18 Oct 2023 12:03:09 +0200 Date: Wed, 18 Oct 2023 12:03:09 +0200 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-ssa-math-opts: Fix up match_uaddc_usubc [PR111845] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780087179944362085 X-GMAIL-MSGID: 1780087179944362085 Hi! GCC ICEs on the first testcase. Successful match_uaddc_usubc ends up with some dead stmts which DCE will remove (hopefully) later all. The ICE is because one of the dead stmts refers to a freed SSA_NAME. The code already gsi_removes a couple of stmts in the /* Remove some statements which can't be kept in the IL because they use SSA_NAME whose setter is going to be removed too. */ section for the same reason (the reason for the freed SSA_NAMEs is that we don't really have a replacement for those cases - all we have after a match is combined overflow from the addition/subtraction of 2 operands + a [0, 1] carry in, but not the individual overflows from the former 2 additions), but for the last (most significant) limb case, where we try to match x = op1 + op2 + carry1 + carry2; or x = op1 - op2 - carry1 - carry2; we just gsi_replace the final stmt, but left around the 2 temporary stmts as dead; if we were unlucky enough that those referenced the carry flag that went away, it ICEs. So, the following patch remembers those temporary statements (rather than trying to rediscover them more expensively) and removes them before the final one is replaced. While working on it, I've noticed we didn't support all the reassociated possibilities of writing the addition of 4 operands or subtracting 3 operands from one, we supported e.g. x = ((op1 + op2) + op3) + op4; x = op1 + ((op2 + op3) + op4); but not x = (op1 + (op2 + op3)) + op4; x = op1 + (op2 + (op3 + op4)); Fixed by the change to inspect also rhs[2] when rhs[1] didn't yield what we were searching for (if non-NULL) - rhs[0] is inspected in the first loop and has different handling for the MINUS_EXPR case. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-10-18 Jakub Jelinek PR tree-optimization/111845 * tree-ssa-math-opts.cc (match_uaddc_usubc): Remember temporary statements for the 4 operand addition or subtraction of 3 operands from 1 operand cases and remove them when successful. Look for nested additions even from rhs[2], not just rhs[1]. * gcc.dg/pr111845.c: New test. * gcc.target/i386/pr111845.c: New test. Jakub --- gcc/tree-ssa-math-opts.cc.jj 2023-09-18 10:37:56.180963000 +0200 +++ gcc/tree-ssa-math-opts.cc 2023-10-17 14:46:39.430139602 +0200 @@ -4581,6 +4581,7 @@ match_uaddc_usubc (gimple_stmt_iterator if (!INTEGRAL_TYPE_P (type) || !TYPE_UNSIGNED (type)) return false; + auto_vec temp_stmts; if (code != BIT_IOR_EXPR && code != BIT_XOR_EXPR) { /* If overflow flag is ignored on the MSB limb, we can end up with @@ -4615,26 +4616,29 @@ match_uaddc_usubc (gimple_stmt_iterator rhs[0] = gimple_assign_rhs1 (g); tree &r = rhs[2] ? rhs[3] : rhs[2]; r = r2; + temp_stmts.quick_push (g); } else break; } - while (TREE_CODE (rhs[1]) == SSA_NAME && !rhs[3]) - { - gimple *g = SSA_NAME_DEF_STMT (rhs[1]); - if (has_single_use (rhs[1]) - && is_gimple_assign (g) - && gimple_assign_rhs_code (g) == PLUS_EXPR) - { - rhs[1] = gimple_assign_rhs1 (g); - if (rhs[2]) - rhs[3] = gimple_assign_rhs2 (g); - else - rhs[2] = gimple_assign_rhs2 (g); - } - else - break; - } + for (int i = 1; i <= 2; ++i) + while (rhs[i] && TREE_CODE (rhs[i]) == SSA_NAME && !rhs[3]) + { + gimple *g = SSA_NAME_DEF_STMT (rhs[i]); + if (has_single_use (rhs[i]) + && is_gimple_assign (g) + && gimple_assign_rhs_code (g) == PLUS_EXPR) + { + rhs[i] = gimple_assign_rhs1 (g); + if (rhs[2]) + rhs[3] = gimple_assign_rhs2 (g); + else + rhs[2] = gimple_assign_rhs2 (g); + temp_stmts.quick_push (g); + } + else + break; + } /* If there are just 3 addends or one minuend and two subtrahends, check for UADDC or USUBC being pattern recognized earlier. Say r = op1 + op2 + ovf1 + ovf2; where the (ovf1 + ovf2) part @@ -5039,7 +5043,17 @@ match_uaddc_usubc (gimple_stmt_iterator g = gimple_build_assign (ilhs, IMAGPART_EXPR, build1 (IMAGPART_EXPR, TREE_TYPE (ilhs), nlhs)); if (rhs[2]) - gsi_insert_before (gsi, g, GSI_SAME_STMT); + { + gsi_insert_before (gsi, g, GSI_SAME_STMT); + /* Remove some further statements which can't be kept in the IL because + they can use SSA_NAMEs whose setter is going to be removed too. */ + while (temp_stmts.length ()) + { + g = temp_stmts.pop (); + gsi2 = gsi_for_stmt (g); + gsi_remove (&gsi2, true); + } + } else gsi_replace (gsi, g, true); /* Remove some statements which can't be kept in the IL because they --- gcc/testsuite/gcc.dg/pr111845.c.jj 2023-10-17 16:08:48.561492321 +0200 +++ gcc/testsuite/gcc.dg/pr111845.c 2023-10-17 15:04:21.199150290 +0200 @@ -0,0 +1,16 @@ +/* PR tree-optimization/111845 */ +/* { dg-do compile } */ +/* { dg-options "-O2 --param tree-reassoc-width=2" } */ + +int a, b; +unsigned int c, d, e; + +void +foo (int x) +{ + b += d; + c += b < d; + b += e = a < x; + c += b; + c += b < e; +} --- gcc/testsuite/gcc.target/i386/pr111845.c.jj 2023-10-17 16:11:44.233010594 +0200 +++ gcc/testsuite/gcc.target/i386/pr111845.c 2023-10-17 16:11:27.944240706 +0200 @@ -0,0 +1,47 @@ +/* PR tree-optimization/111845 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -g -masm=att" } */ +/* { dg-final { scan-assembler-times "\tadcq\t" 8 { target lp64 } } } */ +/* { dg-final { scan-assembler-times "\tadcl\t" 8 { target ia32 } } } */ + +unsigned long l, m; + +__attribute__((noipa)) void +foo (unsigned long x, unsigned long y, unsigned long h, unsigned long i, int a, int b) +{ + unsigned long c, d; + unsigned long e = __builtin_add_overflow (x, y, &c); + unsigned long f = __builtin_add_overflow (c, a < b, &d); + m = ((h + i) + e) + f; + l = d; +} + +__attribute__((noipa)) void +bar (unsigned long x, unsigned long y, unsigned long h, unsigned long i, int a, int b) +{ + unsigned long c, d; + unsigned long e = __builtin_add_overflow (x, y, &c); + unsigned long f = __builtin_add_overflow (c, a < b, &d); + m = (h + (i + e)) + f; + l = d; +} + +__attribute__((noipa)) void +baz (unsigned long x, unsigned long y, unsigned long h, unsigned long i, int a, int b) +{ + unsigned long c, d; + unsigned long e = __builtin_add_overflow (x, y, &c); + unsigned long f = __builtin_add_overflow (c, a < b, &d); + m = h + (i + (e + f)); + l = d; +} + +__attribute__((noipa)) void +qux (unsigned long x, unsigned long y, unsigned long h, unsigned long i, int a, int b) +{ + unsigned long c, d; + unsigned long e = __builtin_add_overflow (x, y, &c); + unsigned long f = __builtin_add_overflow (c, a < b, &d); + m = h + ((i + e) + f); + l = d; +}