Skip to content

Commit 71b5f57

Browse files
lcvon007DamonFool
authored andcommitted
[RISCV] Adjust first sp size to use c.addi16sp.
addi sp, sp, 512 may be used to recover the sp in the epilogue when stack size is larger than 2047(2^11 - 1), however, it can not be compressed using C extension, and addi sp, sp, 496 is able to be compressed, so try to use 496 as the ajust amount of the fisrt sp if function doesn't need extra instructions after adjust. Reviewed By: wangpc Differential Revision: https://reviews.llvm.org/D159431
1 parent 9ddcacc commit 71b5f57

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,8 @@ MachineBasicBlock::iterator RISCVFrameLowering::eliminateCallFramePseudoInstr(
12781278

12791279
// We would like to split the SP adjustment to reduce prologue/epilogue
12801280
// as following instructions. In this way, the offset of the callee saved
1281-
// register could fit in a single store.
1281+
// register could fit in a single store. Supposed that the first sp adjust
1282+
// amount is 2032.
12821283
// add sp,sp,-2032
12831284
// sw ra,2028(sp)
12841285
// sw s0,2024(sp)
@@ -1315,6 +1316,7 @@ RISCVFrameLowering::getFirstSPAdjustAmount(const MachineFunction &MF) const {
13151316
// offset that stack compression instructions accept when target supports
13161317
// compression instructions.
13171318
if (STI.hasStdExtCOrZca()) {
1319+
// The compression extensions may support the following instructions:
13181320
// riscv32: c.lwsp rd, offset[7:2] => 2^(6 + 2)
13191321
// c.swsp rs2, offset[7:2] => 2^(6 + 2)
13201322
// c.flwsp rd, offset[7:2] => 2^(6 + 2)
@@ -1330,10 +1332,22 @@ RISCVFrameLowering::getFirstSPAdjustAmount(const MachineFunction &MF) const {
13301332
// StackSize meets the condition (StackSize <= 2048 + RVCompressLen),
13311333
// case1: Amount is 2048 - StackAlign: use addi + addi to adjust sp.
13321334
// case2: Amount is RVCompressLen: use addi + addi to adjust sp.
1333-
if (StackSize <= 2047 + RVCompressLen ||
1334-
(StackSize > 2048 * 2 - StackAlign &&
1335-
StackSize <= 2047 * 2 + RVCompressLen) ||
1336-
StackSize > 2048 * 3 - StackAlign)
1335+
auto CanCompress = [&](uint64_t CompressLen) -> bool {
1336+
if (StackSize <= 2047 + CompressLen ||
1337+
(StackSize > 2048 * 2 - StackAlign &&
1338+
StackSize <= 2047 * 2 + CompressLen) ||
1339+
StackSize > 2048 * 3 - StackAlign)
1340+
return true;
1341+
1342+
return false;
1343+
};
1344+
// In the epilogue, addi sp, sp, 496 is used to recover the sp and it
1345+
// can be compressed(C.ADDI16SP, offset can be [-512, 496]), but
1346+
// addi sp, sp, 512 can not be compressed. So try to use 496 first.
1347+
const uint64_t ADDI16SPCompressLen = 496;
1348+
if (STI.is64Bit() && CanCompress(ADDI16SPCompressLen))
1349+
return ADDI16SPCompressLen;
1350+
if (CanCompress(RVCompressLen))
13371351
return RVCompressLen;
13381352
}
13391353
return 2048 - StackAlign;

llvm/test/CodeGen/RISCV/stack-inst-compress.mir

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,17 @@ body: |
9292
; CHECK-RV64-COM-LABEL: name: _Z15stack_size_2048v
9393
; CHECK-RV64-COM: liveins: $x1
9494
; CHECK-RV64-COM-NEXT: {{ $}}
95-
; CHECK-RV64-COM-NEXT: $x2 = frame-setup ADDI $x2, -512
96-
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION def_cfa_offset 512
97-
; CHECK-RV64-COM-NEXT: SD killed $x1, $x2, 504 :: (store (s64) into %stack.1)
95+
; CHECK-RV64-COM-NEXT: $x2 = frame-setup ADDI $x2, -496
96+
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION def_cfa_offset 496
97+
; CHECK-RV64-COM-NEXT: SD killed $x1, $x2, 488 :: (store (s64) into %stack.1)
9898
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION offset $x1, -8
99-
; CHECK-RV64-COM-NEXT: $x2 = frame-setup ADDI $x2, -1552
99+
; CHECK-RV64-COM-NEXT: $x2 = frame-setup ADDI $x2, -1568
100100
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION def_cfa_offset 2064
101101
; CHECK-RV64-COM-NEXT: renamable $x10 = ADDI $x2, 8
102102
; CHECK-RV64-COM-NEXT: PseudoCALL target-flags(riscv-call) @_Z6calleePi, csr_ilp32_lp64, implicit-def dead $x1, implicit killed $x10, implicit-def $x2
103-
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADDI $x2, 1552
104-
; CHECK-RV64-COM-NEXT: $x1 = LD $x2, 504 :: (load (s64) from %stack.1)
105-
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADDI $x2, 512
103+
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADDI $x2, 1568
104+
; CHECK-RV64-COM-NEXT: $x1 = LD $x2, 488 :: (load (s64) from %stack.1)
105+
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADDI $x2, 496
106106
; CHECK-RV64-COM-NEXT: PseudoRET
107107
ADJCALLSTACKDOWN 0, 0, implicit-def dead $x2, implicit $x2
108108
renamable $x10 = ADDI %stack.0, 0
@@ -183,19 +183,19 @@ body: |
183183
; CHECK-RV64-COM-LABEL: name: _Z15stack_size_4096v
184184
; CHECK-RV64-COM: liveins: $x1
185185
; CHECK-RV64-COM-NEXT: {{ $}}
186-
; CHECK-RV64-COM-NEXT: $x2 = frame-setup ADDI $x2, -512
187-
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION def_cfa_offset 512
188-
; CHECK-RV64-COM-NEXT: SD killed $x1, $x2, 504 :: (store (s64) into %stack.1)
186+
; CHECK-RV64-COM-NEXT: $x2 = frame-setup ADDI $x2, -496
187+
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION def_cfa_offset 496
188+
; CHECK-RV64-COM-NEXT: SD killed $x1, $x2, 488 :: (store (s64) into %stack.1)
189189
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION offset $x1, -8
190190
; CHECK-RV64-COM-NEXT: $x2 = frame-setup ADDI $x2, -2048
191-
; CHECK-RV64-COM-NEXT: $x2 = frame-setup ADDI killed $x2, -1552
191+
; CHECK-RV64-COM-NEXT: $x2 = frame-setup ADDI killed $x2, -1568
192192
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION def_cfa_offset 4112
193193
; CHECK-RV64-COM-NEXT: renamable $x10 = ADDI $x2, 8
194194
; CHECK-RV64-COM-NEXT: PseudoCALL target-flags(riscv-call) @_Z6calleePi, csr_ilp32_lp64, implicit-def dead $x1, implicit killed $x10, implicit-def $x2
195195
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADDI $x2, 2032
196-
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADDI killed $x2, 1568
197-
; CHECK-RV64-COM-NEXT: $x1 = LD $x2, 504 :: (load (s64) from %stack.1)
198-
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADDI $x2, 512
196+
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADDI killed $x2, 1584
197+
; CHECK-RV64-COM-NEXT: $x1 = LD $x2, 488 :: (load (s64) from %stack.1)
198+
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADDI $x2, 496
199199
; CHECK-RV64-COM-NEXT: PseudoRET
200200
ADJCALLSTACKDOWN 0, 0, implicit-def dead $x2, implicit $x2
201201
renamable $x10 = ADDI %stack.0, 0
@@ -282,21 +282,21 @@ body: |
282282
; CHECK-RV64-COM-LABEL: name: _Z15stack_size_8192v
283283
; CHECK-RV64-COM: liveins: $x1
284284
; CHECK-RV64-COM-NEXT: {{ $}}
285-
; CHECK-RV64-COM-NEXT: $x2 = frame-setup ADDI $x2, -512
286-
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION def_cfa_offset 512
287-
; CHECK-RV64-COM-NEXT: SD killed $x1, $x2, 504 :: (store (s64) into %stack.1)
285+
; CHECK-RV64-COM-NEXT: $x2 = frame-setup ADDI $x2, -496
286+
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION def_cfa_offset 496
287+
; CHECK-RV64-COM-NEXT: SD killed $x1, $x2, 488 :: (store (s64) into %stack.1)
288288
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION offset $x1, -8
289289
; CHECK-RV64-COM-NEXT: $x10 = frame-setup LUI 2
290-
; CHECK-RV64-COM-NEXT: $x10 = frame-setup ADDIW killed $x10, -496
290+
; CHECK-RV64-COM-NEXT: $x10 = frame-setup ADDIW killed $x10, -480
291291
; CHECK-RV64-COM-NEXT: $x2 = frame-setup SUB $x2, killed $x10
292292
; CHECK-RV64-COM-NEXT: frame-setup CFI_INSTRUCTION def_cfa_offset 8208
293293
; CHECK-RV64-COM-NEXT: renamable $x10 = ADDI $x2, 8
294294
; CHECK-RV64-COM-NEXT: PseudoCALL target-flags(riscv-call) @_Z6calleePi, csr_ilp32_lp64, implicit-def dead $x1, implicit killed $x10, implicit-def $x2
295295
; CHECK-RV64-COM-NEXT: $x10 = frame-destroy LUI 2
296-
; CHECK-RV64-COM-NEXT: $x10 = frame-destroy ADDIW killed $x10, -496
296+
; CHECK-RV64-COM-NEXT: $x10 = frame-destroy ADDIW killed $x10, -480
297297
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADD $x2, killed $x10
298-
; CHECK-RV64-COM-NEXT: $x1 = LD $x2, 504 :: (load (s64) from %stack.1)
299-
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADDI $x2, 512
298+
; CHECK-RV64-COM-NEXT: $x1 = LD $x2, 488 :: (load (s64) from %stack.1)
299+
; CHECK-RV64-COM-NEXT: $x2 = frame-destroy ADDI $x2, 496
300300
; CHECK-RV64-COM-NEXT: PseudoRET
301301
ADJCALLSTACKDOWN 0, 0, implicit-def dead $x2, implicit $x2
302302
renamable $x10 = ADDI %stack.0, 0

0 commit comments

Comments
 (0)