From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id C79FE3858D1E for ; Fri, 10 Nov 2023 09:12:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C79FE3858D1E 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 C79FE3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::12a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699607576; cv=none; b=rOmr4Nt3aAKRpAEUM+dQ7qsSEjim9DbasR0QfnW02ltwwSPcX4LDQOeByf0clsXdQTqa21kPlm4QqZ+ckpH2P77b1cFiL/XvvPZBWRwUjqHVYWuIQHtBMQOA7XaKFpFwthFpNnnBmfjoyVk627StyC5ELImCTJY9e4hWKWyPbYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699607576; c=relaxed/simple; bh=9nShbA0IVrjCFWI26uNtLlVShhtc9pUI7n+JHTR9b5s=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=nRGSBuKE0r3zrdDEAlFgNo5gP4QOkquMfOlvcnNvqCQH6dGQ/QOIOTNN3UlLn31MElH/jnCT003fUzgzxbBxbqQs2VjlIqEA6iozij2lPVtSyEiyddNBy+2Ai+W+Rde+f7EZozbBx50ti1AbdxYbfTZM2DjUcCNC20//hO3Dhi8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-5079f3f3d7aso2479852e87.1 for ; Fri, 10 Nov 2023 01:12:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699607572; x=1700212372; 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=WuFkMn2rscccgBQWy057hGdsqcdTMuTbmldd0Xq0Te0=; b=I/rqE+YTofST3ot/CKIGYfNGeFaQIOUBKAPXuutdBg/5DhCIBvVHxJlutX0j8Un0gO Uo29AMFo5U644AdH6i8muwk/WgLw31KXR7USgYjnCnZS4Xz1NRMmB2/vhD4UIlbQ6iwY xJ4oVl+28w7Jm2pkSEkabsZmTLYI/ROP2L2nhWC01eqajkcBxEDu2jMbUq6oJdALOebl 9XAz0Yj+LMd2+ekob6IxPyjp3d6eqfGSMArrwD7xR6CDnx2CQnFr3hg8sXnGDfKSD9+t 5B4nGtM+N2E6ph1Ey23xPBrN2Do29sScBf2sVUhPhZ4XvOx5qrumMh/deRIHKacSlgEE qOyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699607572; x=1700212372; 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=WuFkMn2rscccgBQWy057hGdsqcdTMuTbmldd0Xq0Te0=; b=Ie87GcZaI3SxfK38bsNsNujjO6+tcrToYCqhSB7W+c5lVHrkq/kQqAmXfqAbfFLLty JCuUEWe1m5nWkEb/mdl4YZx1B6yF8T67hqoBBnoBeuJ1VhrgkOmpJ0oGTkZqQ5Enbq19 xx/k7WG01hYIf/IV6WsBSSodOxD3GCXVdw2Fb+eo4tKAlGMYLZ1hyMrmVQxnexQbriDj ZbIUDTor2wpFiYZ5yYZkeqfwhHm2qHl9c218ctFuQFB7fS/rCaQQKeWpKwbyam/3MUzi LrtpUxl+LizqjDnW98RAxBH6JvWl22i6Cu5I7L/YEbTf8kyTxWk7htGizRlcqWX2eaTy QPvw== X-Gm-Message-State: AOJu0YwZZl4LJhof4CvXXY3E/ZxijRqYwTKwAv3hBfxNLUKK6GHIayNa NwF4J7+AdtERjJ1zwB1xkR51L9vNzMSphovmmSEopzrWZoA= X-Google-Smtp-Source: AGHT+IGHLD+29GPODguYO9jvPfkQ5zvyVb2hThrRT1u+jwtG2NZsOS9sTfKINp3kkJxycXlhKTw4hYm5vn7TlhD/UnA= X-Received: by 2002:ac2:4945:0:b0:507:9ff7:2ed4 with SMTP id o5-20020ac24945000000b005079ff72ed4mr3287877lfi.43.1699607572011; Fri, 10 Nov 2023 01:12:52 -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: Fri, 10 Nov 2023 10:12:40 +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.4 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 9:22=E2=80=AFAM Hongtao Liu wro= te: > > On Wed, Nov 8, 2023 at 3:53=E2=80=AFPM Richard Biener > wrote: > > > > On Wed, Nov 8, 2023 at 2:18=E2=80=AFAM Hongtao Liu = wrote: > > > > > > 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 o= perand 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= that > > > > > > even with two SSA names we are only "lucky" when rhs1 is the in= variant > > > > > > 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 b= e > > > > > enough, if match_op[1] is a loop invariant.it must be false for t= he > > > > > 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_COD= E > > > (match_op[1]) =3D=3D INTEGER_CST), but then I realized that this coul= d > > > 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 handl= e > > > 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 (m= atch_op[1]))) > > > > but this only checks match_op[1] (an SSA name at this point) for being = defined > > 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 whole condition is like > > 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]))) > 3695 || gimple_bb (header_phi) !=3D loop->header ----- This would > be true if match_op[1] is SSA_NAME and expr_invariant_in_loop_p But it could be expr_invariant_in_loop_p (match_op[1]) and header_phi =3D dyn_cast (SSA_NAME_DEF_STMT (match_op[0])) all I say is that for two SSA names we could not match the condition (aka not fail) when we swap op0/op1. Not only when op1 is INTEGER_CST. > 3696 || gimple_phi_num_args (header_phi) !=3D 2) > > If expr_invariant_in_loop_p (loop, mach_op[1]) is true and it's an SSA_NA= ME > according to code in expr_invariant_in_loop_p, def_bb of gphi is > either NULL or not belong to this loop, either case will make will > make gimple_bb (header_phi) !=3D loop->header true. > > 1857 if (TREE_CODE (expr) =3D=3D SSA_NAME) > 1858 { > 1859 def_bb =3D gimple_bb (SSA_NAME_DEF_STMT (expr)); > 1860 if (def_bb > 1861 && flow_bb_inside_loop_p (loop, def_bb)) -- def_bb is > NULL or it doesn't belong to the loop > 1862 return false; > 1863 > 1864 return true; > 1865 } > 1866 > 1867 if (!EXPR_P (expr)) > > > > > The only canonicalization we have is that constant operands are put sec= ond so > > it would have been more natural to write the matching with the other op= erand > > order (but likely you'd have been unlucky for the existing testcases th= en). > > > > > 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 b= itop 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= /testsuite/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 replacemen= t} 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-e= volution.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_effe= ct (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_ST= MT (match_op[1]))) > > > > > > > -- > > > > > > > 2.31.1 > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > BR, > > > > > Hongtao > > > > > > > > > > > > -- > > > BR, > > > Hongtao > > > > -- > BR, > Hongtao