[llvm] [GlobalMerge] Aggressively merge constants to reduce TOC entries (PR #111756)
Zaara Syeda via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 10 07:22:48 PDT 2024
syzaara wrote:
> This seems like it will increase the number of loads from the TOC... and inserting more TOC loads so you can shrink the TOC doesn't seem like a good tradeoff.
The number of loads from TOC is not changed since we still need TOC loads for the constants before merging.
For example:
```
int callee(char *);
int str1() {
return callee("hello");
}
int str2() {
return callee("world\n");
}
```
The constant strings “hello” and “world\n” are placed in the TOC and without aggressive constant merging, the code we produce is:
```
.str1:
# %bb.0: # %entry
mflr 0
stwu 1, -64(1)
lwz 3, L..C0(2) # @.str
stw 0, 72(1)
bl .callee[PR]
nop
addi 1, 1, 64
lwz 0, 8(1)
mtlr 0
blr
.str2:
# %bb.0: # %entry
mflr 0
stwu 1, -64(1)
lwz 3, L..C1(2) # @.str.1
stw 0, 72(1)
bl .callee[PR]
nop
addi 1, 1, 64
lwz 0, 8(1)
mtlr 0
blr
```
We can observe the TOC loads:
```
lwz 3, L..C0(2) # @.str
lwz 3, L..C1(2) # @.str.1
.toc
L..C0:
.tc L...str[TC],L...str[RO]
L..C1:
.tc L...str.1[TC],L...str.1[RO]
.csect L...str[RO],2
.string "hello" # @.str
.csect L...str.1[RO],2
.byte 'w,'o,'r,'l,'d,0012,0000 # @.str.1
```
With aggressive merging of the constants, we can use one TOC entry, and keep the same number of TOC loads:
```
.str1:
# %bb.0: # %entry
mflr 0
stwu 1, -64(1)
lwz 3, L..C0(2) # @_MergedGlobals
stw 0, 72(1)
addi 3, 3, 8
bl .callee[PR]
nop
addi 1, 1, 64
lwz 0, 8(1)
mtlr 0
blr
.str2:
# %bb.0: # %entry
mflr 0
stwu 1, -64(1)
lwz 3, L..C0(2) # @_MergedGlobals
stw 0, 72(1)
addi 3, 3, 14
bl .callee[PR]
nop
addi 1, 1, 64
lwz 0, 8(1)
mtlr 0
blr
```
Now we see the TOC loads from the MergedGlobals:
`lwz 3, L..C0(2) # @_MergedGlobals`
```
.toc
L..C0:
.tc L.._MergedGlobals[TC],L.._MergedGlobals[RO]
.csect L.._MergedGlobals[RO],2
L...str: # @_MergedGlobals
.string "hello"
L...str.1:
.byte 'w,'o,'r,'l,'d,0012,0000
.extern .callee[PR]
```
https://github.com/llvm/llvm-project/pull/111756
More information about the llvm-commits
mailing list