Skip to content

Commit 5b9dd01

Browse files
topperctstellar
authored andcommitted
[SelectionDAG][RISCV] Make RegsForValue::getCopyToRegs explicitly zero_extend constants.
ComputePHILiveOutRegInfo assumes that constant incoming values to Phis will be zero extended if they aren't a legal type. To guarantee that we should zero_extend rather than any_extend constants. This fixes a bug for RISCV where any_extend of constants can be treated as a sign_extend. Differential Revision: https://reviews.llvm.org/D122053 (cherry picked from commit 4eb59f0)
1 parent e9b26b5 commit 5b9dd01

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,10 @@ void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG,
926926
CallConv.getValue(), RegVTs[Value])
927927
: RegVTs[Value];
928928

929-
if (ExtendKind == ISD::ANY_EXTEND && TLI.isZExtFree(Val, RegisterVT))
929+
// We need to zero extend constants that are liveout to match assumptions
930+
// in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
931+
if (ExtendKind == ISD::ANY_EXTEND &&
932+
(TLI.isZExtFree(Val, RegisterVT) || isa<ConstantSDNode>(Val)))
930933
ExtendKind = ISD::ZERO_EXTEND;
931934

932935
getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,25 @@ bar:
8080
; constants are zero extended for phi incoming values so an AssertZExt is
8181
; created in 'merge' allowing the zext to be removed.
8282
; 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.
83+
; This code used to miscompile because the code that creates phi incoming values
84+
; in the predecessors created any_extend for the constants which then gets
85+
; treated as a sext by getNode. This made the removal of the zext incorrect.
86+
; SelectionDAGBuilder now creates a zero_extend instead of an any_extend to
87+
; match the assumption.
88+
; FIXME: RISCV would prefer constant inputs to phis to be sign extended.
8689
define i64 @miscompile(i32 %c) {
8790
; RV64I-LABEL: miscompile:
8891
; RV64I: # %bb.0:
89-
; RV64I-NEXT: sext.w a1, a0
92+
; RV64I-NEXT: sext.w a0, a0
93+
; RV64I-NEXT: beqz a0, .LBB2_2
94+
; RV64I-NEXT: # %bb.1:
9095
; RV64I-NEXT: li a0, -1
91-
; RV64I-NEXT: beqz a1, .LBB2_2
92-
; RV64I-NEXT: # %bb.1: # %merge
96+
; RV64I-NEXT: srli a0, a0, 32
9397
; RV64I-NEXT: ret
9498
; RV64I-NEXT: .LBB2_2: # %iffalse
95-
; RV64I-NEXT: li a0, -2
99+
; RV64I-NEXT: li a0, 1
100+
; RV64I-NEXT: slli a0, a0, 32
101+
; RV64I-NEXT: addi a0, a0, -2
96102
; RV64I-NEXT: ret
97103
%a = icmp ne i32 %c, 0
98104
br i1 %a, label %iftrue, label %iffalse

0 commit comments

Comments
 (0)