Skip to content

Commit e9b26b5

Browse files
topperctstellar
authored andcommitted
[RISCV] Add test case for miscompile caused by treating ANY_EXTEND of constants as SIGN_EXTEND.
The code that inserts AssertZExt based on predecessor information assumes constants are zero extended for phi incoming values this allows AssertZExt to be created in blocks consuming a Phi. SelectionDAG::getNode treats any_extend of i32 constants as sext for RISCV. The code that creates phi incoming values in the predecessors creates an any_extend for the constants which then gets treated as a sext by getNode. This makes the AssertZExt incorrect and can cause zexts to be incorrectly removed. This bug was introduced by D105918 Differential Revision: https://reviews.llvm.org/D122052 (cherry picked from commit 268371c)
1 parent a4681df commit e9b26b5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

llvm/test/CodeGen/RISCV/aext-to-sext.ll

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,36 @@ bb:
7575
bar:
7676
ret i32 %b
7777
}
78+
79+
; The code that inserts AssertZExt based on predecessor information assumes
80+
; constants are zero extended for phi incoming values so an AssertZExt is
81+
; created in 'merge' allowing the zext to be removed.
82+
; SelectionDAG::getNode treats any_extend of i32 constants as sext for RISCV.
83+
; The code that creates phi incoming values in the predecessors creates an
84+
; any_extend for the constants which then gets treated as a sext by getNode.
85+
; This means the zext was not safe to remove.
86+
define i64 @miscompile(i32 %c) {
87+
; RV64I-LABEL: miscompile:
88+
; RV64I: # %bb.0:
89+
; RV64I-NEXT: sext.w a1, a0
90+
; RV64I-NEXT: li a0, -1
91+
; RV64I-NEXT: beqz a1, .LBB2_2
92+
; RV64I-NEXT: # %bb.1: # %merge
93+
; RV64I-NEXT: ret
94+
; RV64I-NEXT: .LBB2_2: # %iffalse
95+
; RV64I-NEXT: li a0, -2
96+
; RV64I-NEXT: ret
97+
%a = icmp ne i32 %c, 0
98+
br i1 %a, label %iftrue, label %iffalse
99+
100+
iftrue:
101+
br label %merge
102+
103+
iffalse:
104+
br label %merge
105+
106+
merge:
107+
%b = phi i32 [-1, %iftrue], [-2, %iffalse]
108+
%d = zext i32 %b to i64
109+
ret i64 %d
110+
}

0 commit comments

Comments
 (0)