Skip to content

Commit 23d27d5

Browse files
authored
[RISCV] Don't merge pseudo selects with stack adjustment instrs in between (#160105)
When we have sequence of select pseudo instructions with stack adjustment instructions in between, we shouldn't apply the optimization, proposed by link https://reviews.llvm.org/D59355. If optimization is applied, function won't be marked `adjustsStack` during Finalize ISel pass.
1 parent 8cb03a0 commit 23d27d5

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22274,6 +22274,7 @@ static MachineBasicBlock *emitSelectPseudo(MachineInstr &MI,
2227422274
// - They are debug instructions. Otherwise,
2227522275
// - They do not have side-effects, do not access memory and their inputs do
2227622276
// not depend on the results of the select pseudo-instructions.
22277+
// - They don't adjust stack.
2227722278
// The TrueV/FalseV operands of the selects cannot depend on the result of
2227822279
// previous selects in the sequence.
2227922280
// These conditions could be further relaxed. See the X86 target for a
@@ -22302,6 +22303,8 @@ static MachineBasicBlock *emitSelectPseudo(MachineInstr &MI,
2230222303
SelectDests.insert(MI.getOperand(0).getReg());
2230322304

2230422305
MachineInstr *LastSelectPseudo = &MI;
22306+
const RISCVInstrInfo &TII = *Subtarget.getInstrInfo();
22307+
2230522308
for (auto E = BB->end(), SequenceMBBI = MachineBasicBlock::iterator(MI);
2230622309
SequenceMBBI != E; ++SequenceMBBI) {
2230722310
if (SequenceMBBI->isDebugInstr())
@@ -22321,15 +22324,16 @@ static MachineBasicBlock *emitSelectPseudo(MachineInstr &MI,
2232122324
}
2232222325
if (SequenceMBBI->hasUnmodeledSideEffects() ||
2232322326
SequenceMBBI->mayLoadOrStore() ||
22324-
SequenceMBBI->usesCustomInsertionHook())
22327+
SequenceMBBI->usesCustomInsertionHook() ||
22328+
TII.isFrameInstr(*SequenceMBBI) ||
22329+
SequenceMBBI->isStackAligningInlineAsm())
2232522330
break;
2232622331
if (llvm::any_of(SequenceMBBI->operands(), [&](MachineOperand &MO) {
2232722332
return MO.isReg() && MO.isUse() && SelectDests.count(MO.getReg());
2232822333
}))
2232922334
break;
2233022335
}
2233122336

22332-
const RISCVInstrInfo &TII = *Subtarget.getInstrInfo();
2233322337
const BasicBlock *LLVM_BB = BB->getBasicBlock();
2233422338
DebugLoc DL = MI.getDebugLoc();
2233522339
MachineFunction::iterator I = ++BB->getIterator();
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
2+
; RUN: llc %s -mtriple riscv32 -verify-machineinstrs -o - | FileCheck %s
3+
4+
define i32 @test(i1 %arg_1, i32 %arg_2) {
5+
; CHECK-LABEL: test:
6+
; CHECK: # %bb.0: # %entry
7+
; CHECK-NEXT: addi sp, sp, -16
8+
; CHECK-NEXT: .cfi_def_cfa_offset 16
9+
; CHECK-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
10+
; CHECK-NEXT: sw s0, 8(sp) # 4-byte Folded Spill
11+
; CHECK-NEXT: sw s1, 4(sp) # 4-byte Folded Spill
12+
; CHECK-NEXT: .cfi_offset ra, -4
13+
; CHECK-NEXT: .cfi_offset s0, -8
14+
; CHECK-NEXT: .cfi_offset s1, -12
15+
; CHECK-NEXT: andi s1, a0, 1
16+
; CHECK-NEXT: mv a0, a1
17+
; CHECK-NEXT: mv s0, a1
18+
; CHECK-NEXT: bnez s1, .LBB0_2
19+
; CHECK-NEXT: # %bb.1: # %entry
20+
; CHECK-NEXT: li s0, 1
21+
; CHECK-NEXT: .LBB0_2: # %entry
22+
; CHECK-NEXT: li a1, 7
23+
; CHECK-NEXT: call __udivsi3
24+
; CHECK-NEXT: bnez s1, .LBB0_4
25+
; CHECK-NEXT: # %bb.3: # %entry
26+
; CHECK-NEXT: li a0, 3
27+
; CHECK-NEXT: .LBB0_4: # %entry
28+
; CHECK-NEXT: bnez s1, .LBB0_6
29+
; CHECK-NEXT: # %bb.5: # %entry
30+
; CHECK-NEXT: mv s0, a0
31+
; CHECK-NEXT: .LBB0_6: # %entry
32+
; CHECK-NEXT: li a0, 3
33+
; CHECK-NEXT: .LBB0_7: # %body
34+
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
35+
; CHECK-NEXT: addi s0, s0, 4
36+
; CHECK-NEXT: bltu a0, s0, .LBB0_7
37+
; CHECK-NEXT: # %bb.8: # %exit
38+
; CHECK-NEXT: mv a0, s0
39+
; CHECK-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
40+
; CHECK-NEXT: lw s0, 8(sp) # 4-byte Folded Reload
41+
; CHECK-NEXT: lw s1, 4(sp) # 4-byte Folded Reload
42+
; CHECK-NEXT: .cfi_restore ra
43+
; CHECK-NEXT: .cfi_restore s0
44+
; CHECK-NEXT: .cfi_restore s1
45+
; CHECK-NEXT: addi sp, sp, 16
46+
; CHECK-NEXT: .cfi_def_cfa_offset 0
47+
; CHECK-NEXT: ret
48+
entry:
49+
%sel_1 = select i1 %arg_1, i32 %arg_2, i32 1
50+
%div = udiv i32 %arg_2, 7
51+
%cond_1 = icmp ugt i32 %div, %sel_1
52+
%sel_2 = select i1 %arg_1, i32 %div, i32 3
53+
%sel = select i1 %arg_1, i32 %sel_1, i32 %sel_2
54+
br label %body
55+
56+
body:
57+
%res = phi i32 [ %sel, %entry ], [ %add_loop, %body ]
58+
%add_loop = add i32 4, %res
59+
%cond_2 = icmp ugt i32 %add_loop, 3
60+
br i1 %cond_2, label %body, label %exit
61+
62+
exit:
63+
ret i32 %add_loop
64+
}

0 commit comments

Comments
 (0)