From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id 87C173858D28; Sun, 11 Jun 2023 01:11:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 87C173858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-97454836448so447538666b.2; Sat, 10 Jun 2023 18:11:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686445888; x=1689037888; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=mYGkc6JRlYpU83/wEYBGe4OG2lg4lhQKN7sgHagYs9M=; b=BKPlP0SBV2iiwpE9bSwjV7Ha31Z8og3DOQ6umyz97FY9cCLXApmzCNVnL7J5HBMNE5 2Pyw3XpGcLcVGx7IpYZMiHjN/7+syt21mvHXNNT45ZBDGbGBwRJnCK3gmvFf2nJvHDKk XyKhpIRruV4zhSrvk8H2SZcEglvfqsY060g7PZOSaldphi3V67qQpBuJMyeFLO2CmDSA ZY/2RClRigOm964mNxspNsR9MdWRuWUtoaTJlcxn4XOLyUEbuXNoBujXm5o633oSyovm wAvPoxrleGPWMos00MnGn+x88zSzSFgQzLCGttcamQ310Xumm9fEF/8VfYvdwI/79MIK e8Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686445888; x=1689037888; h=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=mYGkc6JRlYpU83/wEYBGe4OG2lg4lhQKN7sgHagYs9M=; b=Dv7wAHrIDZ/zv/ePHvOxpiExcDRQDkeChwAG4/azCB8sHpSrS2PwD+spqRcxwJ8f9e jSqx7mjzVo+e+trDq0k22/bv5+06e0uO/gEkhZqdraR9lV0WhNfQwA586YL5jrnfQ3g+ YxDJbz192fae3xW6d5vQ/5uZm8Ba/8HospL56LvbPOWpIHttIM6AHny38aMCrAqZ8AhK rdb002cfnd+/7f7qcYVnLiql3lyZytEqxtfhFrvFisooZwvURbb6uw6EaTgFK/GWNv/Y QDtCO/j6pGX2MdAG9EeIG4ppC01aLXJDNnIJIAVki0S3GaJmktdCjpOEwbbyX68u3bLq vjEw== X-Gm-Message-State: AC+VfDwS6kUrGTqebf5+u9xz1CZ0vOlT6UUE2Di+LD2JCLrVgaHI6WSI 2We0HMubOYttGPVkAG9SyZmJY7VGibNXr4WTBz0= X-Google-Smtp-Source: ACHHUZ4A4sxR3iW6VM6tlU986W+NVj5mxwKLL251S+qRgWefecoZ0FZAVhSXYLCJj+kq5VaSBRBfdmjwEO45KntzTqk= X-Received: by 2002:a17:907:7f21:b0:97b:e0bb:8c2f with SMTP id qf33-20020a1709077f2100b0097be0bb8c2fmr4713629ejc.4.1686445888008; Sat, 10 Jun 2023 18:11:28 -0700 (PDT) MIME-Version: 1.0 References: <20230608015547.3432691-1-guojiufu@linux.ibm.com> <20230608015547.3432691-2-guojiufu@linux.ibm.com> In-Reply-To: <20230608015547.3432691-2-guojiufu@linux.ibm.com> From: David Edelsohn Date: Sat, 10 Jun 2023 21:11:16 -0400 Message-ID: Subject: Re: [PATCH 1/4] rs6000: build constant via li;rotldi To: Jiufu Guo Cc: gcc-patches@gcc.gnu.org, segher@kernel.crashing.org, linkw@gcc.gnu.org, bergner@linux.ibm.com Content-Type: multipart/alternative; boundary="00000000000016741305fdd048cf" 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,HTML_MESSAGE,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: --00000000000016741305fdd048cf Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jun 7, 2023 at 9:55=E2=80=AFPM Jiufu Guo w= rote: > Hi, > > This patch checks if a constant is possible to be rotated to/from a > positive > or negative value from "li". If so, we could use "li;rotldi" to build it. > > Bootstrap and regtest pass on ppc64{,le}. > Is this ok for trunk? > > BR, > Jeff (Jiufu) > > gcc/ChangeLog: > > * config/rs6000/rs6000.cc (can_be_rotated_to_positive_li): New > function. > (can_be_rotated_to_negative_li): New function. > (can_be_built_by_li_and_rotldi): New function. > (rs6000_emit_set_long_const): Call can_be_built_by_li_and_rotldi. > > gcc/testsuite/ChangeLog: > > * gcc.target/powerpc/const-build.c: New test. > --- > gcc/config/rs6000/rs6000.cc | 64 +++++++++++++++++-- > .../gcc.target/powerpc/const-build.c | 54 ++++++++++++++++ > 2 files changed, 112 insertions(+), 6 deletions(-) > create mode 100644 gcc/testsuite/gcc.target/powerpc/const-build.c > > diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc > index 42f49e4a56b..1dd0072350a 100644 > --- a/gcc/config/rs6000/rs6000.cc > +++ b/gcc/config/rs6000/rs6000.cc > @@ -10258,6 +10258,48 @@ rs6000_emit_set_const (rtx dest, rtx source) > return true; > } > > +/* Check if C can be rotated to a positive value which 'li' instruction > + is able to load. If so, set *ROT to the number by which C is rotated, > + and return true. Return false otherwise. */ > + > +static bool > +can_be_rotated_to_positive_li (HOST_WIDE_INT c, int *rot) > +{ > + /* 49 leading zeros and 15 low bits on the positive value > + generated by 'li' instruction. */ > + return can_be_rotated_to_lowbits (c, 15, rot); > +} > + > +/* Like can_be_rotated_to_positive_li, but check the negative value of > 'li'. */ > + > +static bool > +can_be_rotated_to_negative_li (HOST_WIDE_INT c, int *rot) > +{ > + return can_be_rotated_to_lowbits (~c, 15, rot); > +} > + > +/* Check if value C can be built by 2 instructions: one is 'li', another > is > + rotldi. > + > + If so, *SHIFT is set to the shift operand of rotldi(rldicl), and *MASK > + is set to -1, and return true. Return false otherwise. */ > + > I look at this feature and it's good, but I don't fully understand the benefit of this level of abstraction. Ideally all of the above functions would be inlined. They aren't reused. > +static bool > +can_be_built_by_li_and_rotldi (HOST_WIDE_INT c, int *shift, > + HOST_WIDE_INT *mask) > +{ > + int n; > + if (can_be_rotated_to_positive_li (c, &n) > + || can_be_rotated_to_negative_li (c, &n)) > Why not /* Check if C or ~C can be rotated to a positive or negative value which 'li' instruction is able to load. */ if (can_be_rotated_to_lowbits (c, 15, &n) || can_be_rotated_to_lowbits (~c, 15, &n)) ... This is a style of software engineering, but it seems overkill to me when the function is a single line that tail calls another function. Am I missing something? The rest of this patch looks good. Thanks, David > + { > + *mask =3D HOST_WIDE_INT_M1; > + *shift =3D HOST_BITS_PER_WIDE_INT - n; > + return true; > + } > + > + return false; > +} > + > /* Subroutine of rs6000_emit_set_const, handling PowerPC64 DImode. > Output insns to set DEST equal to the constant C as a series of > lis, ori and shl instructions. */ > @@ -10266,15 +10308,14 @@ static void > rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c) > { > rtx temp; > + int shift; > + HOST_WIDE_INT mask; > HOST_WIDE_INT ud1, ud2, ud3, ud4; > > ud1 =3D c & 0xffff; > - c =3D c >> 16; > - ud2 =3D c & 0xffff; > - c =3D c >> 16; > - ud3 =3D c & 0xffff; > - c =3D c >> 16; > - ud4 =3D c & 0xffff; > + ud2 =3D (c >> 16) & 0xffff; > + ud3 =3D (c >> 32) & 0xffff; > + ud4 =3D (c >> 48) & 0xffff; > > if ((ud4 =3D=3D 0xffff && ud3 =3D=3D 0xffff && ud2 =3D=3D 0xffff && (u= d1 & 0x8000)) > || (ud4 =3D=3D 0 && ud3 =3D=3D 0 && ud2 =3D=3D 0 && ! (ud1 & 0x800= 0))) > @@ -10305,6 +10346,17 @@ rs6000_emit_set_long_const (rtx dest, > HOST_WIDE_INT c) > emit_move_insn (dest, gen_rtx_XOR (DImode, temp, > GEN_INT ((ud2 ^ 0xffff) << 16))); > } > + else if (can_be_built_by_li_and_rotldi (c, &shift, &mask)) > + { > + temp =3D !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode); > + unsigned HOST_WIDE_INT imm =3D (c | ~mask); > + imm =3D (imm >> shift) | (imm << (HOST_BITS_PER_WIDE_INT - shift)); > + > + emit_move_insn (temp, GEN_INT (imm)); > + if (shift !=3D 0) > + temp =3D gen_rtx_ROTATE (DImode, temp, GEN_INT (shift)); > + emit_move_insn (dest, temp); > + } > else if (ud3 =3D=3D 0 && ud4 =3D=3D 0) > { > temp =3D !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode); > diff --git a/gcc/testsuite/gcc.target/powerpc/const-build.c > b/gcc/testsuite/gcc.target/powerpc/const-build.c > new file mode 100644 > index 00000000000..70f095f6bf2 > --- /dev/null > +++ b/gcc/testsuite/gcc.target/powerpc/const-build.c > @@ -0,0 +1,54 @@ > +/* { dg-do run } */ > +/* { dg-options "-O2 -save-temps" } */ > +/* { dg-require-effective-target has_arch_ppc64 } */ > + > +#define NOIPA __attribute__ ((noipa)) > + > +struct fun > +{ > + long long (*f) (void); > + long long val; > +}; > + > +long long NOIPA > +li_rotldi_1 (void) > +{ > + return 0x7531000000000LL; > +} > + > +long long NOIPA > +li_rotldi_2 (void) > +{ > + return 0x2100000000000064LL; > +} > + > +long long NOIPA > +li_rotldi_3 (void) > +{ > + return 0xffff8531ffffffffLL; > +} > + > +long long NOIPA > +li_rotldi_4 (void) > +{ > + return 0x21ffffffffffff94LL; > +} > + > +struct fun arr[] =3D { > + {li_rotldi_1, 0x7531000000000LL}, > + {li_rotldi_2, 0x2100000000000064LL}, > + {li_rotldi_3, 0xffff8531ffffffffLL}, > + {li_rotldi_4, 0x21ffffffffffff94LL}, > +}; > + > +/* { dg-final { scan-assembler-times {\mrotldi\M} 4 } } */ > + > +int > +main () > +{ > + for (int i =3D 0; i < sizeof (arr) / sizeof (arr[0]); i++) > + if ((*arr[i].f) () !=3D arr[i].val) > + __builtin_abort (); > + > + return 0; > +} > -- > 2.39.1 > > --00000000000016741305fdd048cf--