Skip to content

Commit 9ac5717

Browse files
topperctstellar
authored andcommitted
[RISCV] Restrict performANY_EXTENDCombine to prevent an infinite loop.
The sign_extend we insert here can get turned into a zero_extend if the sign bit is known zero. This can enable a setcc combine that shrinks compares with zero_extend. This reduces the use count of the zero_extend allowing other combines to turn it back into an any_extend. This restricts the combine to only cases where the result is used by a CopyToReg. This works for my original motivating case. I hope the CopyToReg use will prevent any converted extends from turning back into an any_extend. Reviewed By: luismarques Differential Revision: https://reviews.llvm.org/D106754 (cherry picked from commit 54588bc)
1 parent ddd8ca6 commit 9ac5717

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5814,6 +5814,13 @@ static SDValue performANY_EXTENDCombine(SDNode *N,
58145814
break;
58155815
}
58165816

5817+
// Only handle cases where the result is used by a CopyToReg that likely
5818+
// means the value is a liveout of the basic block. This helps prevent
5819+
// infinite combine loops like PR51206.
5820+
if (none_of(N->uses(),
5821+
[](SDNode *User) { return User->getOpcode() == ISD::CopyToReg; }))
5822+
return SDValue();
5823+
58175824
SmallVector<SDNode *, 4> SetCCs;
58185825
for (SDNode::use_iterator UI = Src.getNode()->use_begin(),
58195826
UE = Src.getNode()->use_end();

llvm/test/CodeGen/RISCV/pr51206.ll

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
;RUN: llc < %s -mtriple=riscv64-unknown-linux-gnu -mattr=+m | FileCheck %s
3+
4+
; This test used to cause an infinite loop.
5+
6+
@global = global i8 0, align 1
7+
@global.1 = global i32 0, align 4
8+
@global.2 = global i8 0, align 1
9+
@global.3 = global i32 0, align 4
10+
11+
define signext i32 @wobble() nounwind {
12+
; CHECK-LABEL: wobble:
13+
; CHECK: # %bb.0: # %bb
14+
; CHECK-NEXT: addi sp, sp, -16
15+
; CHECK-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
16+
; CHECK-NEXT: lui a0, %hi(global)
17+
; CHECK-NEXT: lbu a0, %lo(global)(a0)
18+
; CHECK-NEXT: lui a1, %hi(global.2)
19+
; CHECK-NEXT: lbu a1, %lo(global.2)(a1)
20+
; CHECK-NEXT: addi a0, a0, 1
21+
; CHECK-NEXT: lui a2, %hi(global.1)
22+
; CHECK-NEXT: sw a0, %lo(global.1)(a2)
23+
; CHECK-NEXT: mul a0, a0, a1
24+
; CHECK-NEXT: lui a1, 16
25+
; CHECK-NEXT: addiw a1, a1, -1
26+
; CHECK-NEXT: and a1, a0, a1
27+
; CHECK-NEXT: lui a2, 13
28+
; CHECK-NEXT: addiw a2, a2, -819
29+
; CHECK-NEXT: mul a1, a1, a2
30+
; CHECK-NEXT: srli a1, a1, 18
31+
; CHECK-NEXT: lui a2, %hi(global.3)
32+
; CHECK-NEXT: addi a3, zero, 5
33+
; CHECK-NEXT: sw a1, %lo(global.3)(a2)
34+
; CHECK-NEXT: bltu a0, a3, .LBB0_2
35+
; CHECK-NEXT: # %bb.1: # %bb10
36+
; CHECK-NEXT: call quux@plt
37+
; CHECK-NEXT: .LBB0_2: # %bb12
38+
; CHECK-NEXT: mv a0, zero
39+
; CHECK-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
40+
; CHECK-NEXT: addi sp, sp, 16
41+
; CHECK-NEXT: ret
42+
bb:
43+
%tmp = load i8, i8* @global, align 1
44+
%tmp1 = zext i8 %tmp to i32
45+
%tmp2 = add nuw nsw i32 %tmp1, 1
46+
store i32 %tmp2, i32* @global.1, align 4
47+
%tmp3 = load i8, i8* @global.2, align 1
48+
%tmp4 = zext i8 %tmp3 to i32
49+
%tmp5 = mul nuw nsw i32 %tmp2, %tmp4
50+
%tmp6 = trunc i32 %tmp5 to i16
51+
%tmp7 = udiv i16 %tmp6, 5
52+
%tmp8 = zext i16 %tmp7 to i32
53+
store i32 %tmp8, i32* @global.3, align 4
54+
%tmp9 = icmp ult i32 %tmp5, 5
55+
br i1 %tmp9, label %bb12, label %bb10
56+
57+
bb10: ; preds = %bb
58+
%tmp11 = tail call signext i32 bitcast (i32 (...)* @quux to i32 ()*)()
59+
br label %bb12
60+
61+
bb12: ; preds = %bb10, %bb
62+
ret i32 undef
63+
}
64+
65+
declare signext i32 @quux(...)

0 commit comments

Comments
 (0)