[PATCH] D122053: [SelectionDAG][RISCV] Make RegsForValue::getCopyToRegs explicitly zero_extend constants.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 19 18:43:48 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG4eb59f017903: [SelectionDAG][RISCV] Make RegsForValue::getCopyToRegs explicitly zero_extend… (authored by craig.topper).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122053/new/

https://reviews.llvm.org/D122053

Files:
  llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
  llvm/test/CodeGen/RISCV/aext-to-sext.ll


Index: llvm/test/CodeGen/RISCV/aext-to-sext.ll
===================================================================
--- llvm/test/CodeGen/RISCV/aext-to-sext.ll
+++ llvm/test/CodeGen/RISCV/aext-to-sext.ll
@@ -80,19 +80,25 @@
 ; constants are zero extended for phi incoming values so an AssertZExt is
 ; created in 'merge' allowing the zext to be removed.
 ; 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 means the zext was not safe to remove.
+; This code used to miscompile because the code that creates phi incoming values
+; in the predecessors created any_extend for the constants which then gets
+; treated as a sext by getNode. This made the removal of the zext incorrect.
+; SelectionDAGBuilder now creates a zero_extend instead of an any_extend to
+; match the assumption.
+; FIXME: RISCV would prefer constant inputs to phis to be sign extended.
 define i64 @miscompile(i32 %c) {
 ; RV64I-LABEL: miscompile:
 ; RV64I:       # %bb.0:
-; RV64I-NEXT:    sext.w a1, a0
+; RV64I-NEXT:    sext.w a0, a0
+; RV64I-NEXT:    beqz a0, .LBB2_2
+; RV64I-NEXT:  # %bb.1:
 ; RV64I-NEXT:    li a0, -1
-; RV64I-NEXT:    beqz a1, .LBB2_2
-; RV64I-NEXT:  # %bb.1: # %merge
+; RV64I-NEXT:    srli a0, a0, 32
 ; RV64I-NEXT:    ret
 ; RV64I-NEXT:  .LBB2_2: # %iffalse
-; RV64I-NEXT:    li a0, -2
+; RV64I-NEXT:    li a0, 1
+; RV64I-NEXT:    slli a0, a0, 32
+; RV64I-NEXT:    addi a0, a0, -2
 ; RV64I-NEXT:    ret
   %a = icmp ne i32 %c, 0
   br i1 %a, label %iftrue, label %iffalse
Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -918,7 +918,10 @@
                                           CallConv.getValue(), RegVTs[Value])
                                     : RegVTs[Value];
 
-    if (ExtendKind == ISD::ANY_EXTEND && TLI.isZExtFree(Val, RegisterVT))
+    // We need to zero extend constants that are liveout to match assumptions
+    // in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
+    if (ExtendKind == ISD::ANY_EXTEND &&
+        (TLI.isZExtFree(Val, RegisterVT) || isa<ConstantSDNode>(Val)))
       ExtendKind = ISD::ZERO_EXTEND;
 
     getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122053.416752.patch
Type: text/x-patch
Size: 2508 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220320/f5b1f596/attachment.bin>


More information about the llvm-commits mailing list