From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x112b.google.com (mail-yw1-x112b.google.com [IPv6:2607:f8b0:4864:20::112b]) by sourceware.org (Postfix) with ESMTPS id D0B0A3858D1E for ; Wed, 8 Nov 2023 01:18:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D0B0A3858D1E 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 D0B0A3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::112b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699406321; cv=none; b=aAldnZbFvAcRTaoKBlJhfI+Bc+32RcXoYhQIcbxT0jO4kHayKPeFjwslVU1tIem5CCpz3WjtvzuTNQL9PZbCcybO9uNFTc7kaRGIYs2eJOgoqitbKgjyWo9Y++fZHXhgEtRJXejF3ForcXgaLvJAg2+YQe3S2Gm1aHwB7A1xYQY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699406321; c=relaxed/simple; bh=rBTNXNdGv6l/xdA/DkHdzl1zykT+/ME8b7KiRlSBdT4=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=R/iRQOVi3pRyYBH3QndPc2OSqnFTD0AF8Hvwz0acoKL1ImdmSIbqjqiiXe7aUXwyOI/0G/d2iVJqKCksBLdrJ3KTtbdkosAdcqXoJZMZMtFFVnLKpnh6QkSCj3dEnQtlqt8EO06Kbo3e7V83mcyFMLBhcoiSMbb+uXQrS+vcnHQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-579de633419so76870607b3.3 for ; Tue, 07 Nov 2023 17:18:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699406319; x=1700011119; 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=cz+fwC12u6QF5cXdI82N9EWliwxPxwMpNKVl1Pj00xQ=; b=iUNnWOBcp+8Cm2qyR5YkZmIsJiIVW8bJ9jJAGOrkfmx0fxM3nJGckbAnPANgueVVcR RkOSXYpj5IdDndsgLPHC1hAz8H9pDWxlQMx8eQ2uxPc8FHgVKjxofmGXuvgX6xGCoNCN GA2cz+9VJdAOIzKb7ITYnzbBfPTTrNttpgjsfpz+WQQPzNYGh0/oNRDul0H+TCzOgRQA IP4l7apCKmBQnUZuzo2KaKf+GaNplcDCqDOrU/fvVJoMrSLCWcKS8MD2N45r9Z1cq7JG FEovAzmoM79Xhf0dZHs/MIeyMLaoWs5lDA4+RcsN/kE+loQQAv8VgZ53fByN0q+5KT4G ggfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699406319; x=1700011119; 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=cz+fwC12u6QF5cXdI82N9EWliwxPxwMpNKVl1Pj00xQ=; b=TWtSucQIOu0o5sc9nF43LRBzzwLm7U5TDFJQ7YX3fWf287dHU6qWl78HZTz54MXwmg HPWya31o5Kl8iho9PaCtX7YGjDBVMq8K9ejPn9LU3Rz+k+gXCKNaJ2AgEaPBLFNdQjov 9WreHj0Ow+jUf2562kKdm0oMiiNX9662iiycC5dNC3VVNxuxJRH/Nar9qkqzAeMwrBLX TiPL3fRfAUq/I6XKO3supksJp9SfjHcXMME6rBcCxbmd1j6WDYHCpGOD6TXePVro+VCF rhwp/WguoB7ufGLn9Y3pzXv0qGKSSHSW6je1xhCXfyyhWx9tbeDZoLn502hhOML2ItuW 855w== X-Gm-Message-State: AOJu0YxCqzCyJfx1XX/IPUrNAqFnTbDeu9U1TWZTVqgXxQTgP/SVZ67c 3xBYZ4nOVg/sgPZ/gSLKzqn8AR8sLtrdaLAIOYk= X-Google-Smtp-Source: AGHT+IEmc2t0A2GRwjXHCGWRWBFwhGd9p+YXTyEecL2z+O9rTFXZKzCqkfUhzfd72e0qVjB6nBg12qCtzz+Q6LOsscc= X-Received: by 2002:a0d:e042:0:b0:5a9:117d:ddbc with SMTP id j63-20020a0de042000000b005a9117dddbcmr387314ywe.15.1699406318749; Tue, 07 Nov 2023 17:18:38 -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: Wed, 8 Nov 2023 09:18:27 +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.9 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 10:34=E2=80=AFPM Richard Biener wrote: > > On Tue, Nov 7, 2023 at 2:03=E2=80=AFPM Hongtao Liu w= rote: > > > > 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 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 invarian= t > > > 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/o= p1 > 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. 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_o= p[1]))) 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 p= hidef) 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 w= ith > > > > 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/tests= uite/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-evoluti= on.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 (cl= ass 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 (ma= tch_op[1]))) > > > > -- > > > > 2.31.1 > > > > > > > > > > > > -- > > BR, > > Hongtao -- BR, Hongtao