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 0E9263858D33 for ; Tue, 7 Nov 2023 14:34:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0E9263858D33 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 0E9263858D33 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=1699367700; cv=none; b=c1rxSCa9gBu+D49I8fnv9+Yq0FAswFyMg+jgw34N0/79HSftXbqgc3ih3SemNE7+vX1/fzllGY980yCkGH7/CwryQqyHM74EYrbRGFlM6VQkYQCWpsEaRTzp8W3+owgic+940K91P27l3SOuv8hLNgR21Kjzh81J51a78JEZf/g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699367700; c=relaxed/simple; bh=NdoB1jytsrUPYSnyt6b9uRdhFzlLSlF1rMkA9u+sJoA=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=lJ2SuU3DmY+pysH9LbW78wcEmZJEPR8QuOV+j5c1WLIPk35a0iY1lIAuYjjRjJGsVcNvK0m8e3uI154NDSQJl/Y/6X+67Ifi4dq8AQk0bAXKi2zmjvhPnzTefQeXfvvQ/XH7QaycEN0yXJgKplmSPt5dQ+jxwO2KXxJcO2eDzk0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-507cee17b00so7392797e87.2 for ; Tue, 07 Nov 2023 06:34:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699367696; x=1699972496; 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=qgMX50HMvFQakvIQcnGKczdLsSxDPLI2uppK0YaqkRA=; b=Sma5WF8gJ3UktqZAkIk7rNM/17wALNavstF0uOQO70isoLTs2ONBgev1yOzMMb0Zsu T2uwatPy1pXHrCcHM51iwKACEFy5wt3xT9B8lY0AQwZRyirJSgm+o2dHaUHP1E6hGm5A Nrp4gU2nHnrj8oomuC4aAf6OTXgNUSPguH3GmWZclTWbv9GX/jIMDAymT11nTnwjwR+B 4fFYABRrPlGgsdGHjA82hV54e7MwXfL4/nA6AQ2QSDX0qcyRUH9Eowd2Z6dcuEHI84ND IF+SCteDtE3TOpEwWvrQvKLSKYVXk4CdNslTDYALBD5uu0G2T5N+uiGr4hIH/LHB//Iw 0PPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699367696; x=1699972496; 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=qgMX50HMvFQakvIQcnGKczdLsSxDPLI2uppK0YaqkRA=; b=OT0JpV4NtUZfk6wigL3cs0cCABoeM+u77ZWnx1TwjDsZeKwSUHRj0uVvW7883hUT30 P8DaYT2FmXcASAqH8jKdYyGoI2UMqGSzr1qTkrIqORyqL7uZaKTUh8IEptRvfF+MkwJ0 lFB6ZFY5L1aMQyVyM0JpHG4bEx4QwTWggxr3fWi+R+MxdkaZyfTwbgyp19SC2tSCksC5 DRpZz46DClcVAN9j0YFb5YgFv1wB+yek4OhUtVT2z0tfJPOcHeGIIqjGi8gYWmHu6JcM FQAEiS7dP7EWgNUmwNyav+IneW/nYhFG3a/l61G4JGn4zogPEZlxZIaj4A4pPAPMGxpH onHw== X-Gm-Message-State: AOJu0Yy+aieX+AISyRNV/JuDUN5gYnm8m4ztp3MNwcd2BvszV/Qsfc8d KRuquAgP6kIv12SEZCqOIaalDHOQfJ6n5f/HScsWBbpO X-Google-Smtp-Source: AGHT+IGOJQZCzp9dv9kVmIQJOy63M8/4EQiZEgZVk2dkdVcIFnO4ytptoUMTYdMQ/b/fM3/kGr+KsUgv8OKxTUn3pso= X-Received: by 2002:a05:6512:3610:b0:4fe:af1:c3ae with SMTP id f16-20020a056512361000b004fe0af1c3aemr23197967lfs.15.1699367696211; Tue, 07 Nov 2023 06:34:56 -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: Tue, 7 Nov 2023 15:34:44 +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.3 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 2:03=E2=80=AFPM Hongtao Liu wro= te: > > 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 i= s > > > 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). 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? > > > > 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 wit= h > > > 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/testsui= te/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 "sc= cp" } } */ > > > + > > > +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= .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 (clas= s 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 (matc= h_op[1]))) > > > -- > > > 2.31.1 > > > > > > > -- > BR, > Hongtao