[llvm] [PowerPC] Peephole address calculation in TOC memops (PR #76488)

Sean Fertile via llvm-commits llvm-commits at lists.llvm.org
Tue May 14 08:06:13 PDT 2024


mandlebug wrote:

> > Something useful related to this came up in @stephenpeckham's presentation. Adding an offset to the large code model relocation expressions is not allowed, so I belive we couldn't transform something like
> > ```
> > addis r3, x[TD]@u(2)
> > addi  r3, x[TD]@l(r3)
> > ld.      r5, 2(r3)
> > ```
> > 
> > 
> >     
> >       
> >     
> > 
> >       
> >     
> > 
> >     
> >   
> > to
> > ```
> > addis r3, x[TD]@u(2)
> > ld  r5, x[TD]@l + 2)(r3)
> > ```
> > 
> > 
> >     
> >       
> >     
> > 
> >       
> >     
> > 
> >     
> >   
> > I though I'd comment here now so we won't forget to test this.
> 
> ```
> int a = 100;
> int main()
> {
>   return a;
> }
> ```
> 
> `1.c -S -mcmodel=large -mtocdata -m64`
> 
> ```
> .main:
> # %bb.0:                                # %entry
>         li 3, 0
>         stw 3, -12(1)
>         addis 3, a[TD]@u(2)
>         la 3, a[TD]@l(3)
>         lwz 3, 0(3)
>         extsw 3, 3
>         blr
> ```
> 
> After manually changing the assembly to:
> 
> ```
> .main:
> # %bb.0:                                # %entry
>         li 3, 0
>         stw 3, -12(1)
>         addis 3, a[TD]@u(2)
>         lwz 3, a[TD]@l(3)
>         extsw 3, 3
>         blr
> ```
> 
> It is still able to get good running result.
> 
> Do you mean other cases @mandlebug ?

Yeah - specifically when there is a non-zero offset it the memory operation we are forwarding the TOC-data symref to eg:
```
        addis 3, a[TD]@u(2)
        la    3, a[TD]@l(3)
        lbz   3, 1(3)
```

>From what I understand there is no support for parsing an add expression in the load/store instruction.

If we look at an example we do support on Linux:
```
%struct.S = type<{ i64, i64 , i64}>
@S = dso_local global %struct.S <{ i64 1, i64 2 , i64 3}>, align 16

define i64 @test_singleuse() nounwind {
entry:
  %0 = load i64, ptr getelementptr inbounds (%struct.S, ptr @S, i32 0, i32 2), align 8
  ret i64 %0
}
``` 
produces assembly of:
```
# %bb.0:                                # %entry
        addis 3, 2, S at toc@ha+16
        ld 3, S at toc@l+16(3)
        blr
```

But for AIX we couldn't do this as there is no equivalent of the `S at toc@l+16` expression that the system assembler can consume.

https://github.com/llvm/llvm-project/pull/76488


More information about the llvm-commits mailing list