From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x112d.google.com (mail-yw1-x112d.google.com [IPv6:2607:f8b0:4864:20::112d]) by sourceware.org (Postfix) with ESMTPS id 8AE0B38582A3 for ; Tue, 7 Nov 2023 13:03:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8AE0B38582A3 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 8AE0B38582A3 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::112d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699362224; cv=none; b=KND6wG55MIh6Ii/EouJlkMQD1cONhVg965mrer6pmL6JMFtTHYzZRglb33UouK70oT1VXuIQBZoX6ZaNmLMYp++cZq2ZZXIsmEMyRSnMOEfpJnD2IMszlSXNVa3s2y5FASEhe+ttZAIbnpXETXjjpm0z9L7A7kA7i06OLblp6/0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699362224; c=relaxed/simple; bh=wjIym6f2t2fQc5Ui8SP4mSco4Y3lX908MtoRby1Q5Mc=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=E/E9G4aQ7/q0liPrfOGxNMuZ+tzGXKkmA7rWXXkMV3VfGlmYk5bC1Qw0xeZ/hA8Kvctgm/4xKDu2nJTAKGA8rwhoTT2rhE6fxiWbmiiwTlSWzda+FjaNChroSfJx6lXDsvk630Of/jy8x3YPR4Gv6FGdbXNWIKlYFD5koYI2GrA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-5a7c95b8d14so61649307b3.3 for ; Tue, 07 Nov 2023 05:03:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699362214; x=1699967014; 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=qUSvJT1ajp54fDRM/8rNcey52NMoXreMDsHXoo1m9O0=; b=WoV9XPNYpGkAVdsO/fJZ9BQp+jiNmaV++pJpf4cQAkjj0km6/n0zxqgu3BsoD0W6MV 5fnu6zrVZj+IsR+AIWgxCq2qsls0YvpnW8FEKtkAtNnJkQ0EgO5TnwlwUhZdHt7kZlcI nLyoMWcIiPp1P5jH4SWMboKAP0jFpLhXVvpfr7jxw5f98qB/8Ae3HNFdMM+ySn3pSb5p UmdJ8kmr7myk1WADhhfTrNKNMx745oBbqW+TqIOd3lCchXucLX+K9Ch95W/JCW0ra/dp eQjpqfIc/UKVhdOYIrq3nmwtOD7+DkuStu7UgyauGIWtp0r6n3P+Lcv59e4QDSsAk7cu 9OlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699362214; x=1699967014; 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=qUSvJT1ajp54fDRM/8rNcey52NMoXreMDsHXoo1m9O0=; b=AAfczgF2kPRHdPzyag0n+dDcyl3/o1KQdu7wMvUPpEalcg9G+0DoKwBr1SnquTgxA3 Jc6JHmCkfQKyctNiPG6GuaCT/ZV2JuRr+LK90yCec1JQcn8zhtZm+WFI1FJFCKYk5mVq D3PyrJet3KUGvDl/KlGf7xUxlkI325JzPpX+b4zYuw2nR57F0QrY5aW4ithuiGjENcln uOCD3F8AFfldAMpAHvLXiXdvZVR6/Y8hSHwSr7U1I2xh7+8qXq7uz76HK2rqbgVWwvWP 4CwLYd6OLii0IYxF7HR1o+BNZ1gAKFKvo7o5vdoBJdXPvOfdazBviVzhW+pzOc5wvKuE ANaw== X-Gm-Message-State: AOJu0YztQajwG7LsPQrHYhxxtcH03H92rZFh/TtVupoxHI3SVP8YvYT3 sAA5cGW/QEjUAX9BovN8Aip15W/hE+frUwIxEm4= X-Google-Smtp-Source: AGHT+IH7+k1lkGDqtpDzqDXThd4SdEuhQONdUbWG9B6KlF+WRW8bCEN854OdCTmX3q9IBB3U5y92wE11l/AkVvyTujo= X-Received: by 2002:a0d:d94b:0:b0:5a7:ad67:2b67 with SMTP id b72-20020a0dd94b000000b005a7ad672b67mr9958578ywe.5.1699362213823; Tue, 07 Nov 2023 05:03:33 -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: Hongtao Liu Date: Tue, 7 Nov 2023 21:03:22 +0800 Message-ID: Subject: Re: [V2 PATCH] Handle bitop with INTEGER_CST in analyze_and_compute_bitop_with_inv_effect. To: Richard Biener 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.7 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 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 w= rote: > > > > analyze_and_compute_bitop_with_inv_effect assumes the first operand 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 invariant > 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). > > 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/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 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-evolution.c= c > > 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 > > --=20 BR, Hongtao