From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id B35853858D38 for ; Wed, 8 Nov 2023 07:53:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B35853858D38 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B35853858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699430028; cv=none; b=Cyykl545gVb1sY8o/UU7CXEmSGpVh0KNavZ9EBMRkthpiN6PdMmy2i2xwsZi3UEAVHBkwgKVGOyJFAwnCIn37tp4F5iwZco+ETTvghd9ti92U2z7E+J3iCH3ldk+IRx2ixEQ5GBsqouIHPCxuLP7LyzWIhSGlbwB/2qF+hhX9Ts= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699430028; c=relaxed/simple; bh=cyTnmbO27pkemkUdw3mlJwy6OgRnp9z9NU2o3hPq03k=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=GE1gp8duLRvF/ry0rjtkCA1g+ZBieYg1Licoi6GXIyeb79/OFdsJjZE/SBb9HJqIG+uS5GFV0ZlVP2erY3de/pmZQlMPWQqYM7+BaNDrlcDlTkXmST3NMcpFXdqGEOsqN4xX68wvujHpKAl8q1SDS6IN20Mi8I4HFCDAy3nHiyw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-507975d34e8so9462414e87.1 for ; Tue, 07 Nov 2023 23:53:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699430025; x=1700034825; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=7pVg/WKWSFl5aTvyhfncW2gD8RsoItBTyQ2zuAhBZo4=; b=m1j8cc72oMn12rSzNgTmm1j4QsNeYZcM1NPNCaJrpKSG4m2bGrel8ZLq5UrsViCs1u 0b+yfzK17tWc0k5OXAqBZSAqvjX6gjOi5WIdOUIm+OE7iiM364yOYELqaz8YlnxtmOTx ckT9dyemsFkLr3RgTEpzd2s2TganUitiBvyjmkuT5QRVdySnmqu2AHU9plWzbs489nGB X8CzixMYOePNYTGKelhQN589xF6vTNnU2jD8C7O2g6X6sXO/hxUJVlCWDM5IjxrE9H7O 96lBJWww6MUHf436Pc7v0wLl03oBpTqg6wpqAjVMvcy2onA/SzGEGOFV2tw4J7h+0wzN 58aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699430025; x=1700034825; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7pVg/WKWSFl5aTvyhfncW2gD8RsoItBTyQ2zuAhBZo4=; b=RJXG+iGofvgefDHyPNKVC5SnJM1eljiwYOAIEBRt50110ZL0+XDjavIG2C/ZLkE2fj 6rdNW40Np3wKDlSzsw9Fs5ksGKpowJ2SNBu59MJ31pWOMxe4R0RcHwr9OlVHYhycLicT f8iselp3SEd78v0tVYb1OkK0d9yFAHVewkCUhx2eh5K+Pm6ZcpbzrWsQyIABunFJ5FN4 Q1UpKWfA8zTdARQNMA4Vu2BS9Sdclt1mooDAlAu7dstxTJiynni0Mj4EzKtCboHzlxc/ OT9NusxyDFjR7khnCxmGKlAalvVkA+BsSKXUDVcWyQnM83+mB//uB7dmkvUUCfMIY0si hbIA== X-Gm-Message-State: AOJu0YxQ9D41nVt4BhQbnxZZqu4PuNgnYSiEIPkFJUiPoSHN5na4Ap2S iIvkOCixHDEQJ5nP7/cFXItn5PR/G80oMhXj5SvysjhZee4= X-Google-Smtp-Source: AGHT+IFmGVf44KEWUMfjxTo0uuUHWuIh7aW1j4S6X6tOLU0nZcbt1xY1LTnklTEukYeyqf+7pI6AO4rofUG4wwD3jac= X-Received: by 2002:a05:6512:ba0:b0:4fb:9168:1fce with SMTP id b32-20020a0565120ba000b004fb91681fcemr782830lfv.59.1699430024405; Tue, 07 Nov 2023 23:53:44 -0800 (PST) MIME-Version: 1.0 References: <20231030104145.169132-1-hongtao.liu@intel.com> <20231107060539.443303-1-hongtao.liu@intel.com> In-Reply-To: From: Richard Biener Date: Wed, 8 Nov 2023 08:53:26 +0100 Message-ID: Subject: Re: [V2 PATCH] Handle bitop with INTEGER_CST in analyze_and_compute_bitop_with_inv_effect. To: Hongtao Liu Cc: liuhongt , gcc-patches@gcc.gnu.org, hjl.tools@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,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 List-Id: On Wed, Nov 8, 2023 at 2:18=E2=80=AFAM Hongtao Liu wro= te: > > On Tue, Nov 7, 2023 at 10:34=E2=80=AFPM Richard Biener > wrote: > > > > On Tue, Nov 7, 2023 at 2:03=E2=80=AFPM Hongtao Liu = wrote: > > > > > > On Tue, Nov 7, 2023 at 4:10=E2=80=AFPM Richard Biener > > > wrote: > > > > > > > > On Tue, Nov 7, 2023 at 7:08=E2=80=AFAM liuhongt wrote: > > > > > > > > > > analyze_and_compute_bitop_with_inv_effect assumes the first opera= nd is > > > > > loop invariant which is not the case when it's INTEGER_CST. > > > > > > > > > > Bootstrapped and regtseted on x86_64-pc-linux-gnu{-m32,}. > > > > > Ok for trunk? > > > > > > > > So this addresses a missed optimization, right? It seems to me tha= t > > > > even with two SSA names we are only "lucky" when rhs1 is the invari= ant > > > > one. So instead of swapping this way I'd do > > > Yes, it's a miss optimization. > > > And I think expr_invariant_in_loop_p (loop, match_op[1]) should be > > > enough, if match_op[1] is a loop invariant.it must be false for the > > > below conditions(there couldn't be any header_phi from its > > > definition). > > > > Yes, all I said is that when you now care for op1 being INTEGER_CST > > it could also be an invariant SSA name and thus only after swapping op0= /op1 > > we could have a successful match, no? > Sorry, the commit message is a little bit misleading. > At first, I just wanted to handle the INTEGER_CST case (with TREE_CODE > (match_op[1]) =3D=3D INTEGER_CST), but then I realized that this could > probably be extended to the normal SSA_NAME case as well, so I used > expr_invariant_in_loop_p, which should theoretically be able to handle > the SSA_NAME case as well. > > if (expr_invariant_in_loop_p (loop, match_op[1])) is true, w/o > swapping it must return NULL_TREE for below conditions. > if (expr_invariant_in_loop_p (loop, match_op[1])) is false, w/ > swapping it must return NULL_TREE too. > So it can cover the both cases you mentioned, no need for a loop to > iterate 2 match_ops for all conditions. Sorry if it appears we're going in circles ;) > 3692 if (TREE_CODE (match_op[1]) !=3D SSA_NAME > 3693 || !expr_invariant_in_loop_p (loop, match_op[0]) > 3694 || !(header_phi =3D dyn_cast (SSA_NAME_DEF_STMT (match= _op[1]))) but this only checks match_op[1] (an SSA name at this point) for being defi= ned by the header PHI. What if expr_invariant_in_loop_p (loop, mach_op[1]) and header_phi =3D dyn_cast (SSA_NAME_DEF_STMT (match_op[0])) which I think can happen when both ops are SSA name? The only canonicalization we have is that constant operands are put second = so it would have been more natural to write the matching with the other operan= d order (but likely you'd have been unlucky for the existing testcases then). > 3695 || gimple_bb (header_phi) !=3D loop->header > 3696 || gimple_phi_num_args (header_phi) !=3D 2) > 3697 return NULL_TREE; > 3698 > 3699 if (PHI_ARG_DEF_FROM_EDGE (header_phi, loop_latch_edge (loop)) !=3D= phidef) > 3700 return NULL_TREE; > > > > > > > > > > > > unsigned i; > > > > for (i =3D 0; i < 2; ++i) > > > > if (TREE_CODE (match_op[i]) =3D=3D SSA_NAME > > > > && ...) > > > > break; /* found! */ > > > > > > > > if (i =3D=3D 2) > > > > return NULL_TREE; > > > > if (i =3D=3D 0) > > > > std::swap (match_op[0], match_op[1]); > > > > > > > > to also handle a "swapped" pair of SSA names? > > > > > > > > > gcc/ChangeLog: > > > > > > > > > > PR tree-optimization/105735 > > > > > PR tree-optimization/111972 > > > > > * tree-scalar-evolution.cc > > > > > (analyze_and_compute_bitop_with_inv_effect): Handle bitop= with > > > > > INTEGER_CST. > > > > > > > > > > gcc/testsuite/ChangeLog: > > > > > > > > > > * gcc.target/i386/pr105735-3.c: New test. > > > > > --- > > > > > gcc/testsuite/gcc.target/i386/pr105735-3.c | 87 ++++++++++++++++= ++++++ > > > > > gcc/tree-scalar-evolution.cc | 3 + > > > > > 2 files changed, 90 insertions(+) > > > > > create mode 100644 gcc/testsuite/gcc.target/i386/pr105735-3.c > > > > > > > > > > diff --git a/gcc/testsuite/gcc.target/i386/pr105735-3.c b/gcc/tes= tsuite/gcc.target/i386/pr105735-3.c > > > > > new file mode 100644 > > > > > index 00000000000..9e268a1a997 > > > > > --- /dev/null > > > > > +++ b/gcc/testsuite/gcc.target/i386/pr105735-3.c > > > > > @@ -0,0 +1,87 @@ > > > > > +/* { dg-do compile } */ > > > > > +/* { dg-options "-O1 -fdump-tree-sccp-details" } */ > > > > > +/* { dg-final { scan-tree-dump-times {final value replacement} 8= "sccp" } } */ > > > > > + > > > > > +unsigned int > > > > > +__attribute__((noipa)) > > > > > +foo (unsigned int tmp) > > > > > +{ > > > > > + for (int bit =3D 0; bit < 64; bit++) > > > > > + tmp &=3D 11304; > > > > > + return tmp; > > > > > +} > > > > > + > > > > > +unsigned int > > > > > +__attribute__((noipa)) > > > > > +foo1 (unsigned int tmp) > > > > > +{ > > > > > + for (int bit =3D 63; bit >=3D 0; bit -=3D3) > > > > > + tmp &=3D 11304; > > > > > + return tmp; > > > > > +} > > > > > + > > > > > +unsigned int > > > > > +__attribute__((noipa)) > > > > > +foo2 (unsigned int tmp) > > > > > +{ > > > > > + for (int bit =3D 0; bit < 64; bit++) > > > > > + tmp |=3D 11304; > > > > > + return tmp; > > > > > +} > > > > > + > > > > > +unsigned int > > > > > +__attribute__((noipa)) > > > > > +foo3 (unsigned int tmp) > > > > > +{ > > > > > + for (int bit =3D 63; bit >=3D 0; bit -=3D3) > > > > > + tmp |=3D 11304; > > > > > + return tmp; > > > > > +} > > > > > + > > > > > +unsigned int > > > > > +__attribute__((noipa)) > > > > > +foo4 (unsigned int tmp) > > > > > +{ > > > > > + for (int bit =3D 0; bit < 64; bit++) > > > > > + tmp ^=3D 11304; > > > > > + return tmp; > > > > > +} > > > > > + > > > > > +unsigned int > > > > > +__attribute__((noipa)) > > > > > +foo5 (unsigned int tmp) > > > > > +{ > > > > > + for (int bit =3D 0; bit < 63; bit++) > > > > > + tmp ^=3D 11304; > > > > > + return tmp; > > > > > +} > > > > > + > > > > > +unsigned int > > > > > +__attribute__((noipa)) > > > > > +f (unsigned int tmp, int bit) > > > > > +{ > > > > > + unsigned int res =3D tmp; > > > > > + for (int i =3D 0; i < bit; i++) > > > > > + res &=3D 11304; > > > > > + return res; > > > > > +} > > > > > + > > > > > +unsigned int > > > > > +__attribute__((noipa)) > > > > > +f1 (unsigned int tmp, int bit) > > > > > +{ > > > > > + unsigned int res =3D tmp; > > > > > + for (int i =3D 0; i < bit; i++) > > > > > + res |=3D 11304; > > > > > + return res; > > > > > +} > > > > > + > > > > > +unsigned int > > > > > +__attribute__((noipa)) > > > > > +f2 (unsigned int tmp, int bit) > > > > > +{ > > > > > + unsigned int res =3D tmp; > > > > > + for (int i =3D 0; i < bit; i++) > > > > > + res ^=3D 11304; > > > > > + return res; > > > > > +} > > > > > diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolu= tion.cc > > > > > index 70b17c5bca1..f61277c32df 100644 > > > > > --- a/gcc/tree-scalar-evolution.cc > > > > > +++ b/gcc/tree-scalar-evolution.cc > > > > > @@ -3689,6 +3689,9 @@ analyze_and_compute_bitop_with_inv_effect (= class loop* loop, tree phidef, > > > > > match_op[0] =3D gimple_assign_rhs1 (def); > > > > > match_op[1] =3D gimple_assign_rhs2 (def); > > > > > > > > > > + if (expr_invariant_in_loop_p (loop, match_op[1])) > > > > > + std::swap (match_op[0], match_op[1]); > > > > > + > > > > > if (TREE_CODE (match_op[1]) !=3D SSA_NAME > > > > > || !expr_invariant_in_loop_p (loop, match_op[0]) > > > > > || !(header_phi =3D dyn_cast (SSA_NAME_DEF_STMT (= match_op[1]))) > > > > > -- > > > > > 2.31.1 > > > > > > > > > > > > > > > > > -- > > > BR, > > > Hongtao > > > > -- > BR, > Hongtao