Re: [PATCH 3/4] rs6000: build constant via li/lis;rldicl/rldicr
Hi, David Edelsohn writes: > > On Wed, Jun 7, 2023 at 9:56 PM Jiufu Guo wrote: > > Hi, > > This patch checks if a constant is possible left/right cleaned on a rotated > value from a negative value of "li/lis". If so, we can build the constant > through "li/lis ; rldicl/rldicr". > > Bootstrap and regtest pass on ppc64{,le}. > Is this ok for trunk? > > BR, > Jeff (Jiufu) > > gcc/ChangeLog: > > * config/rs6000/rs6000.cc (can_be_built_by_li_lis_and_rldicl): New > function. > (can_be_built_by_li_lis_and_rldicr): New function. > (rs6000_emit_set_long_const): Call can_be_built_by_li_lis_and_rldicr > and > can_be_built_by_li_lis_and_rldicl. > > This is okay. See below. > > Thanks, David > > > > gcc/testsuite/ChangeLog: > > * gcc.target/powerpc/const-build.c: Add more tests. > --- > gcc/config/rs6000/rs6000.cc | 61 ++- > .../gcc.target/powerpc/const-build.c | 44 + > 2 files changed, 104 insertions(+), 1 deletion(-) > > diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc > index 03cd9d5e952..2a3fa733b45 100644 > --- a/gcc/config/rs6000/rs6000.cc > +++ b/gcc/config/rs6000/rs6000.cc > @@ -10332,6 +10332,61 @@ can_be_built_by_li_lis_and_rotldi (HOST_WIDE_INT c, > int *shift, > return false; > } > > +/* Check if value C can be built by 2 instructions: one is 'li or lis', > + another is rldicl. > + > + If so, *SHIFT is set to the shift operand of rldicl, and *MASK is set to > + the mask operand of rldicl, and return true. > + Return false otherwise. */ > + > +static bool > +can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift, > + HOST_WIDE_INT *mask) > +{ > + /* Leading zeros may be cleaned by rldicl with a mask. Change leading > zeros > + to ones and then recheck it. */ > + int lz = clz_hwi (c); > + HOST_WIDE_INT unmask_c > += c | (HOST_WIDE_INT_M1U << (HOST_BITS_PER_WIDE_INT - lz)); > + int n; > + if (can_be_rotated_to_negative_li (unmask_c, &n) > > using can_be_rotated_to_lowbits (~unmask_c, 15, &n) > > Maybe Segher would want the abstraction, but it seems more wasteful to > me. Thanks! I would update accordingly :) BR, Jeff (Jiufu) Guo > > + || can_be_rotated_to_negative_lis (unmask_c, &n)) > +{ > + *mask = HOST_WIDE_INT_M1U >> lz; > + *shift = n == 0 ? 0 : HOST_BITS_PER_WIDE_INT - n; > + return true; > +} > + > + return false; > +} > + > +/* Check if value C can be built by 2 instructions: one is 'li or lis', > + another is rldicr. > + > + If so, *SHIFT is set to the shift operand of rldicr, and *MASK is set to > + the mask operand of rldicr, and return true. > + Return false otherwise. */ > + > +static bool > +can_be_built_by_li_lis_and_rldicr (HOST_WIDE_INT c, int *shift, > + HOST_WIDE_INT *mask) > +{ > + /* Tailing zeros may be cleaned by rldicr with a mask. Change tailing > zeros > + to ones and then recheck it. */ > + int tz = ctz_hwi (c); > + HOST_WIDE_INT unmask_c = c | ((HOST_WIDE_INT_1U << tz) - 1); > + int n; > + if (can_be_rotated_to_negative_li (unmask_c, &n) > + || can_be_rotated_to_negative_lis (unmask_c, &n)) > +{ > + *mask = HOST_WIDE_INT_M1U << tz; > + *shift = 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. */ > @@ -10378,7 +10433,9 @@ rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT > c) > emit_move_insn (dest, gen_rtx_XOR (DImode, temp, > GEN_INT ((ud2 ^ 0x) << 16))); > } > - else if (can_be_built_by_li_lis_and_rotldi (c, &shift, &mask)) > + else if (can_be_built_by_li_lis_and_rotldi (c, &shift, &mask) > + || can_be_built_by_li_lis_and_rldicl (c, &shift, &mask) > + || can_be_built_by_li_lis_and_rldicr (c, &shift, &mask)) > { > temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode); > unsigned HOST_WIDE_INT imm = (c | ~mask); > @@ -10387,6 +10444,8 @@ rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT > c) > emit_move_insn (temp, GEN_INT (imm)); > if (shift != 0) > temp = gen_rtx_ROTATE (DImode, temp, GEN_INT (shift)); > + if (mask != HOST_WIDE_INT_M1) > + temp = gen_rtx_AND (DImode, temp, GEN_INT (mask)); > emit_move_insn (dest, temp); > } > else if (ud3 == 0 && ud4 == 0) > diff --git a/gcc/testsuite/gcc.target/powerpc/const-build.c > b/gcc/testsuite/gcc.target/powerpc/const-build.c > index c38a1dd91f2..8c209921d41 100644 > --- a/gcc/testsuite/gcc.target/powerpc/const-build.c > +++ b/gcc/testsuite/gcc.tar
Re: [PATCH 3/4] rs6000: build constant via li/lis;rldicl/rldicr
On Wed, Jun 7, 2023 at 9:56 PM Jiufu Guo wrote: > Hi, > > This patch checks if a constant is possible left/right cleaned on a rotated > value from a negative value of "li/lis". If so, we can build the constant > through "li/lis ; rldicl/rldicr". > > Bootstrap and regtest pass on ppc64{,le}. > Is this ok for trunk? > > BR, > Jeff (Jiufu) > > gcc/ChangeLog: > > * config/rs6000/rs6000.cc (can_be_built_by_li_lis_and_rldicl): New > function. > (can_be_built_by_li_lis_and_rldicr): New function. > (rs6000_emit_set_long_const): Call > can_be_built_by_li_lis_and_rldicr and > can_be_built_by_li_lis_and_rldicl. > This is okay. See below. Thanks, David > > gcc/testsuite/ChangeLog: > > * gcc.target/powerpc/const-build.c: Add more tests. > --- > gcc/config/rs6000/rs6000.cc | 61 ++- > .../gcc.target/powerpc/const-build.c | 44 + > 2 files changed, 104 insertions(+), 1 deletion(-) > > diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc > index 03cd9d5e952..2a3fa733b45 100644 > --- a/gcc/config/rs6000/rs6000.cc > +++ b/gcc/config/rs6000/rs6000.cc > @@ -10332,6 +10332,61 @@ can_be_built_by_li_lis_and_rotldi (HOST_WIDE_INT > c, int *shift, >return false; > } > > +/* Check if value C can be built by 2 instructions: one is 'li or lis', > + another is rldicl. > + > + If so, *SHIFT is set to the shift operand of rldicl, and *MASK is set > to > + the mask operand of rldicl, and return true. > + Return false otherwise. */ > + > +static bool > +can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift, > + HOST_WIDE_INT *mask) > +{ > + /* Leading zeros may be cleaned by rldicl with a mask. Change leading > zeros > + to ones and then recheck it. */ > + int lz = clz_hwi (c); > + HOST_WIDE_INT unmask_c > += c | (HOST_WIDE_INT_M1U << (HOST_BITS_PER_WIDE_INT - lz)); > + int n; > + if (can_be_rotated_to_negative_li (unmask_c, &n) > using can_be_rotated_to_lowbits (~unmask_c, 15, &n) Maybe Segher would want the abstraction, but it seems more wasteful to me. > + || can_be_rotated_to_negative_lis (unmask_c, &n)) > +{ > + *mask = HOST_WIDE_INT_M1U >> lz; > + *shift = n == 0 ? 0 : HOST_BITS_PER_WIDE_INT - n; > + return true; > +} > + > + return false; > +} > + > +/* Check if value C can be built by 2 instructions: one is 'li or lis', > + another is rldicr. > + > + If so, *SHIFT is set to the shift operand of rldicr, and *MASK is set > to > + the mask operand of rldicr, and return true. > + Return false otherwise. */ > + > +static bool > +can_be_built_by_li_lis_and_rldicr (HOST_WIDE_INT c, int *shift, > + HOST_WIDE_INT *mask) > +{ > + /* Tailing zeros may be cleaned by rldicr with a mask. Change tailing > zeros > + to ones and then recheck it. */ > + int tz = ctz_hwi (c); > + HOST_WIDE_INT unmask_c = c | ((HOST_WIDE_INT_1U << tz) - 1); > + int n; > + if (can_be_rotated_to_negative_li (unmask_c, &n) > + || can_be_rotated_to_negative_lis (unmask_c, &n)) > +{ > + *mask = HOST_WIDE_INT_M1U << tz; > + *shift = 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. */ > @@ -10378,7 +10433,9 @@ rs6000_emit_set_long_const (rtx dest, > HOST_WIDE_INT c) >emit_move_insn (dest, gen_rtx_XOR (DImode, temp, > GEN_INT ((ud2 ^ 0x) << 16))); > } > - else if (can_be_built_by_li_lis_and_rotldi (c, &shift, &mask)) > + else if (can_be_built_by_li_lis_and_rotldi (c, &shift, &mask) > + || can_be_built_by_li_lis_and_rldicl (c, &shift, &mask) > + || can_be_built_by_li_lis_and_rldicr (c, &shift, &mask)) > { >temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode); >unsigned HOST_WIDE_INT imm = (c | ~mask); > @@ -10387,6 +10444,8 @@ rs6000_emit_set_long_const (rtx dest, > HOST_WIDE_INT c) >emit_move_insn (temp, GEN_INT (imm)); >if (shift != 0) > temp = gen_rtx_ROTATE (DImode, temp, GEN_INT (shift)); > + if (mask != HOST_WIDE_INT_M1) > + temp = gen_rtx_AND (DImode, temp, GEN_INT (mask)); >emit_move_insn (dest, temp); > } >else if (ud3 == 0 && ud4 == 0) > diff --git a/gcc/testsuite/gcc.target/powerpc/const-build.c > b/gcc/testsuite/gcc.target/powerpc/const-build.c > index c38a1dd91f2..8c209921d41 100644 > --- a/gcc/testsuite/gcc.target/powerpc/const-build.c > +++ b/gcc/testsuite/gcc.target/powerpc/const-build.c > @@ -46,6 +46,42 @@ lis_rotldi_6 (void) >return 0x5318LL; > } > > +long long NOIPA > +li_rldicl_7 (void) > +{ > + return 0x3ffa1LL; > +} > + > +long long NOIPA > +li_rldicl_8 (void
[PATCH 3/4] rs6000: build constant via li/lis;rldicl/rldicr
Hi, This patch checks if a constant is possible left/right cleaned on a rotated value from a negative value of "li/lis". If so, we can build the constant through "li/lis ; rldicl/rldicr". Bootstrap and regtest pass on ppc64{,le}. Is this ok for trunk? BR, Jeff (Jiufu) gcc/ChangeLog: * config/rs6000/rs6000.cc (can_be_built_by_li_lis_and_rldicl): New function. (can_be_built_by_li_lis_and_rldicr): New function. (rs6000_emit_set_long_const): Call can_be_built_by_li_lis_and_rldicr and can_be_built_by_li_lis_and_rldicl. gcc/testsuite/ChangeLog: * gcc.target/powerpc/const-build.c: Add more tests. --- gcc/config/rs6000/rs6000.cc | 61 ++- .../gcc.target/powerpc/const-build.c | 44 + 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc index 03cd9d5e952..2a3fa733b45 100644 --- a/gcc/config/rs6000/rs6000.cc +++ b/gcc/config/rs6000/rs6000.cc @@ -10332,6 +10332,61 @@ can_be_built_by_li_lis_and_rotldi (HOST_WIDE_INT c, int *shift, return false; } +/* Check if value C can be built by 2 instructions: one is 'li or lis', + another is rldicl. + + If so, *SHIFT is set to the shift operand of rldicl, and *MASK is set to + the mask operand of rldicl, and return true. + Return false otherwise. */ + +static bool +can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift, + HOST_WIDE_INT *mask) +{ + /* Leading zeros may be cleaned by rldicl with a mask. Change leading zeros + to ones and then recheck it. */ + int lz = clz_hwi (c); + HOST_WIDE_INT unmask_c += c | (HOST_WIDE_INT_M1U << (HOST_BITS_PER_WIDE_INT - lz)); + int n; + if (can_be_rotated_to_negative_li (unmask_c, &n) + || can_be_rotated_to_negative_lis (unmask_c, &n)) +{ + *mask = HOST_WIDE_INT_M1U >> lz; + *shift = n == 0 ? 0 : HOST_BITS_PER_WIDE_INT - n; + return true; +} + + return false; +} + +/* Check if value C can be built by 2 instructions: one is 'li or lis', + another is rldicr. + + If so, *SHIFT is set to the shift operand of rldicr, and *MASK is set to + the mask operand of rldicr, and return true. + Return false otherwise. */ + +static bool +can_be_built_by_li_lis_and_rldicr (HOST_WIDE_INT c, int *shift, + HOST_WIDE_INT *mask) +{ + /* Tailing zeros may be cleaned by rldicr with a mask. Change tailing zeros + to ones and then recheck it. */ + int tz = ctz_hwi (c); + HOST_WIDE_INT unmask_c = c | ((HOST_WIDE_INT_1U << tz) - 1); + int n; + if (can_be_rotated_to_negative_li (unmask_c, &n) + || can_be_rotated_to_negative_lis (unmask_c, &n)) +{ + *mask = HOST_WIDE_INT_M1U << tz; + *shift = 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. */ @@ -10378,7 +10433,9 @@ rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c) emit_move_insn (dest, gen_rtx_XOR (DImode, temp, GEN_INT ((ud2 ^ 0x) << 16))); } - else if (can_be_built_by_li_lis_and_rotldi (c, &shift, &mask)) + else if (can_be_built_by_li_lis_and_rotldi (c, &shift, &mask) + || can_be_built_by_li_lis_and_rldicl (c, &shift, &mask) + || can_be_built_by_li_lis_and_rldicr (c, &shift, &mask)) { temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode); unsigned HOST_WIDE_INT imm = (c | ~mask); @@ -10387,6 +10444,8 @@ rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c) emit_move_insn (temp, GEN_INT (imm)); if (shift != 0) temp = gen_rtx_ROTATE (DImode, temp, GEN_INT (shift)); + if (mask != HOST_WIDE_INT_M1) + temp = gen_rtx_AND (DImode, temp, GEN_INT (mask)); emit_move_insn (dest, temp); } else if (ud3 == 0 && ud4 == 0) diff --git a/gcc/testsuite/gcc.target/powerpc/const-build.c b/gcc/testsuite/gcc.target/powerpc/const-build.c index c38a1dd91f2..8c209921d41 100644 --- a/gcc/testsuite/gcc.target/powerpc/const-build.c +++ b/gcc/testsuite/gcc.target/powerpc/const-build.c @@ -46,6 +46,42 @@ lis_rotldi_6 (void) return 0x5318LL; } +long long NOIPA +li_rldicl_7 (void) +{ + return 0x3ffa1LL; +} + +long long NOIPA +li_rldicl_8 (void) +{ + return 0xff8531LL; +} + +long long NOIPA +lis_rldicl_9 (void) +{ + return 0x00ff8531LL; +} + +long long NOIPA +li_rldicr_10 (void) +{ + return 0x8531fff0LL; +} + +long long NOIPA +li_rldicr_11 (void) +{ + return 0x21f0LL; +} + +long long NOIPA +lis_rldicr_12 (void) +{ + return 0x5310LL; +} + struct fun arr[] = { {li_rotldi_1, 0x75310LL}, {li_rotldi_2, 0x2164LL}, @@ -53,9 +89,17 @@ struct fun arr[] = {
[PATCH 3/4] rs6000: build constant via li/lis;rldicl/rldicr
Hi, This patch checks if a constant is possible left/right cleaned on a rotated value from a negative value of "li/lis". If so, we can build the constant through "li/lis ; rldicl/rldicr". Bootstrap and regtest pass on ppc64{,le}. Is this ok for trunk or next stage1? BR, Jeff (Jiufu) gcc/ChangeLog: * config/rs6000/rs6000.cc (can_be_built_by_li_lis_and_rldicl): New function. (can_be_built_by_li_lis_and_rldicr): New function. (rs6000_emit_set_long_const): Call can_be_built_by_li_lis_and_rldicr and can_be_built_by_li_lis_and_rldicl. gcc/testsuite/ChangeLog: * gcc.target/powerpc/const-build.c: Add more tests. --- gcc/config/rs6000/rs6000.cc | 57 ++- .../gcc.target/powerpc/const-build.c | 44 ++ 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc index dcbd5820a52..025abaa436e 100644 --- a/gcc/config/rs6000/rs6000.cc +++ b/gcc/config/rs6000/rs6000.cc @@ -10308,6 +10308,59 @@ can_be_built_by_li_lis_and_rotldi (HOST_WIDE_INT c, int *shift, return false; } +/* Check if value C can be built by 2 instructions: one is 'li or lis', + another is rldicl. + + If so, *SHIFT is set to the shift operand of rldicl, and *MASK is set to + the mask operand of rldicl, and return true. + Return false otherwise. */ +static bool +can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift, + HOST_WIDE_INT *mask) +{ + /* Leading zeros maybe cleaned by rldicl with mask. Change leading zeros + to ones and then recheck it. */ + int lz = clz_hwi (c); + HOST_WIDE_INT unmask_c += c | (HOST_WIDE_INT_M1U << (HOST_BITS_PER_WIDE_INT - lz)); + int n; + if (can_be_rotated_to_negative_li (unmask_c, &n) + || can_be_rotated_to_negative_lis (unmask_c, &n)) +{ + *mask = HOST_WIDE_INT_M1U >> lz; + *shift = n == 0 ? 0 : HOST_BITS_PER_WIDE_INT - n; + return true; +} + + return false; +} + +/* Check if value C can be built by 2 instructions: one is 'li or lis', + another is rldicr. + + If so, *SHIFT is set to the shift operand of rldicr, and *MASK is set to + the mask operand of rldicr, and return true. + Return false otherwise. */ +static bool +can_be_built_by_li_lis_and_rldicr (HOST_WIDE_INT c, int *shift, + HOST_WIDE_INT *mask) +{ + /* Tailing zeros maybe cleaned by rldicr with mask. Change tailing zeros + to ones and then recheck it. */ + int tz = ctz_hwi (c); + HOST_WIDE_INT unmask_c = c | ((HOST_WIDE_INT_1U << tz) - 1); + int n; + if (can_be_rotated_to_negative_li (unmask_c, &n) + || can_be_rotated_to_negative_lis (unmask_c, &n)) +{ + *mask = HOST_WIDE_INT_M1U << tz; + *shift = 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. */ @@ -10347,7 +10400,9 @@ rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c) emit_move_insn (dest, gen_rtx_XOR (DImode, temp, GEN_INT ((ud2 ^ 0x) << 16))); } - else if (can_be_built_by_li_lis_and_rotldi (c, &shift, &mask)) + else if (can_be_built_by_li_lis_and_rotldi (c, &shift, &mask) + || can_be_built_by_li_lis_and_rldicl (c, &shift, &mask) + || can_be_built_by_li_lis_and_rldicr (c, &shift, &mask)) { temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode); unsigned HOST_WIDE_INT imm = (c | ~mask); diff --git a/gcc/testsuite/gcc.target/powerpc/const-build.c b/gcc/testsuite/gcc.target/powerpc/const-build.c index c38a1dd91f2..8c209921d41 100644 --- a/gcc/testsuite/gcc.target/powerpc/const-build.c +++ b/gcc/testsuite/gcc.target/powerpc/const-build.c @@ -46,6 +46,42 @@ lis_rotldi_6 (void) return 0x5318LL; } +long long NOIPA +li_rldicl_7 (void) +{ + return 0x3ffa1LL; +} + +long long NOIPA +li_rldicl_8 (void) +{ + return 0xff8531LL; +} + +long long NOIPA +lis_rldicl_9 (void) +{ + return 0x00ff8531LL; +} + +long long NOIPA +li_rldicr_10 (void) +{ + return 0x8531fff0LL; +} + +long long NOIPA +li_rldicr_11 (void) +{ + return 0x21f0LL; +} + +long long NOIPA +lis_rldicr_12 (void) +{ + return 0x5310LL; +} + struct fun arr[] = { {li_rotldi_1, 0x75310LL}, {li_rotldi_2, 0x2164LL}, @@ -53,9 +89,17 @@ struct fun arr[] = { {li_rotldi_4, 0x2194LL}, {lis_rotldi_5, 0x8531LL}, {lis_rotldi_6, 0x5318LL}, + {li_rldicl_7, 0x3ffa1LL}, + {li_rldicl_8, 0xff8531LL}, + {lis_rldicl_9, 0x00ff8531LL}, + {li_rldicr_10, 0x8531fff0LL}, + {li_rldicr_11, 0x21f0LL}, + {lis_rldicr_12, 0x5310LL}, }; /* { dg-final