Hi Avinash,
On 18/11/26 2:37 pm, Avinash Jayakar wrote:
> The *largetocp10 patterns expand to two instructions (addis/addi or
> addis/la) but did not specify a length attribute, so they inherited the
> default length of four bytes.
>
> In large functions this made shorten_branches underestimate the distance
> to labels after such TOC address materializations. With -mtune=power10,
> which enables POWER10 fusion even on older CPUs, conditional branches
> that were actually outside the +/-32KB branch range could be emitted as
> short branches, causing assembler errors such as:
>
> Error: operand out of range (0x8138 is not between ...)
>
> Set the length of the four affected patterns to eight bytes.
>
> 2026-11-18 Avinash Jayakar <[email protected]>
> Assisted-by: LLM for testcase generation
>
> gcc/ChangeLog:
> PR target/128692
> * config/rs6000/rs6000.md (*largetocp10): Set length to 8.
> (*largetocp10_aix<mode>): Likewise.
> (*largetocp10_plus): Likewise.
> (*largetocp10_plus_aix<mode>): Likewise.
>
> gcc/testsuite/ChangeLog:
> PR target/128692
> * gcc.target/powerpc/pr128692.c: New test.
> ---
> gcc/config/rs6000/rs6000.md | 12 +++++---
> gcc/testsuite/gcc.target/powerpc/pr128692.c | 34 +++++++++++++++++++++
> 2 files changed, 42 insertions(+), 4 deletions(-)
> create mode 100644 gcc/testsuite/gcc.target/powerpc/pr128692.c
>
> diff --git a/gcc/config/rs6000/rs6000.md b/gcc/config/rs6000/rs6000.md
> index 85ba7e01113..1dbf95d881a 100644
> --- a/gcc/config/rs6000/rs6000.md
> +++ b/gcc/config/rs6000/rs6000.md
> @@ -11656,7 +11656,8 @@ (define_insn "*largetocp10"
> UNSPEC_TOCREL))]
> "TARGET_P10_FUSION
> && TARGET_ELF && TARGET_CMODEL != CMODEL_SMALL"
> - "addis %0,%2,%1@toc@ha\;addi %0,%0,%1@toc@l")
> + "addis %0,%2,%1@toc@ha\;addi %0,%0,%1@toc@l"
> + [(set_attr "length" "8")])
>
> (define_insn "*largetocp10_aix<mode>"
> [(set (match_operand:P 0 "gpc_reg_operand" "=b")
> @@ -11665,7 +11666,8 @@ (define_insn "*largetocp10_aix<mode>"
> UNSPEC_TOCREL))]
> "TARGET_P10_FUSION
> && TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL"
> - "addis %0,%1@u(%2)\;la %0,%2@l(%1)")
> + "addis %0,%1@u(%2)\;la %0,%2@l(%1)"
> + [(set_attr "length" "8")])
>
> (define_insn "*largetocp10_plus"
> [(set (match_operand:DI 0 "gpc_reg_operand" "=b")
> @@ -11676,7 +11678,8 @@ (define_insn "*largetocp10_plus"
> (match_operand:DI 3 "add_cint_operand" "n")))]
> "TARGET_P10_FUSION
> && TARGET_ELF && TARGET_CMODEL != CMODEL_SMALL"
> - "addis %0,%2,%1+%3@toc@ha\;addi %0,%0,%1+%3@toc@l")
> + "addis %0,%2,%1+%3@toc@ha\;addi %0,%0,%1+%3@toc@l"
> + [(set_attr "length" "8")])
>
> (define_insn "*largetocp10_plus_aix<mode>"
> [(set (match_operand:P 0 "gpc_reg_operand" "=b")
> @@ -11687,7 +11690,8 @@ (define_insn "*largetocp10_plus_aix<mode>"
> (match_operand:P 3 "add_cint_operand" "n")))]
> "TARGET_P10_FUSION
> && TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL"
> - "addis %0,%1+%3@u(%2);la %0,%2+%3@l(%1)")
> + "addis %0,%1+%3@u(%2);la %0,%2+%3@l(%1)"
The existing output template uses a plain ; between addis and la,
whereas the other multi-instruction templates use \;. can we make
this consistent?
> + [(set_attr "length" "8")])
One additional observation: These patterns expand into two instructions
(addis + addi or addis + la), but do not specify a cost attribute. Other
multi-instruction sequences, particularly the logical fusion patterns,
specify an explicit cost.
Could we also check whether an appropriate cost attribute should be added
here? It would be good to understand how the cost is determined and
it could help GCC make better optimization decisions.
Thanks,
Jeevitha.
>
>
> (define_insn_and_split "*tocref<mode>"
> diff --git a/gcc/testsuite/gcc.target/powerpc/pr128692.c
> b/gcc/testsuite/gcc.target/powerpc/pr128692.c
> new file mode 100644
> index 00000000000..51cf60f63e6
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/powerpc/pr128692.c
> @@ -0,0 +1,34 @@
> +/* { dg-do assemble { target { powerpc*-*-linux* && lp64 } } } */
> +/* { dg-options "-O2 -mcpu=power9 -mtune=power10 -fno-pic" } */
> +
> +/* PR target/128692. With -mtune=power10 the *largetocp10 patterns in
> + rs6000.md emit two instructions but used to have the default length of
> + 4. In a sufficiently large function this made shorten_branches
> + underestimate the distance to a label and emit an out-of-range short
> + branch. */
> +
> +#define ITEM_(n) extern volatile long v##n; if (c == n) goto far; s += v##n;
> +#define ITEM(n) ITEM_(n)
> +#define R1(M) M(__COUNTER__)
> +#define R2(M) R1(M) R1(M)
> +#define R4(M) R2(M) R2(M)
> +#define R8(M) R4(M) R4(M)
> +#define R16(M) R8(M) R8(M)
> +#define R32(M) R16(M) R16(M)
> +#define R64(M) R32(M) R32(M)
> +#define R128(M) R64(M) R64(M)
> +#define R256(M) R128(M) R128(M)
> +#define R512(M) R256(M) R256(M)
> +#define R1024(M) R512(M) R512(M)
> +
> +long f (long c)
> +{
> + long s = 0;
> + R1024(ITEM)
> + R128(ITEM)
> + R32(ITEM)
> + R16(ITEM)
> +far:
> + s += 1;
> + return s;
> +}