[PATCH] [DAGCombiner] Fix & simplify constant folding of sext/zext.

Simon Pilgrim llvm-dev at redking.me.uk
Mon Jun 22 14:27:14 PDT 2015


Thanks for finding this! I think the problems you're seeing revolve around the fact that integer build vectors scalar input operands can be of a wider type than the vector type - typically for legalization reasons. So your pcmpeqd regression is because the comparison result (v4i1) actually has scalar inputs of i32 that get implicitly truncated by BUILD_VECTOR.


================
Comment at: lib/CodeGen/SelectionDAG/DAGCombiner.cpp:5609
@@ -5610,4 +5608,3 @@
     SDLoc DL(Op);
-    ConstantSDNode *CurrentND = cast<ConstantSDNode>(Op);
-    const APInt &C = APInt(VTBits, CurrentND->getAPIntValue().getZExtValue());
+    const APInt &C = cast<ConstantSDNode>(Op)->getAPIntValue();
     if (Opcode == ISD::SIGN_EXTEND || Opcode == ISD::SIGN_EXTEND_VECTOR_INREG)
----------------
The operands of an integer build vector may be wider than the vector scalar type. You need to do something like 
const APInt C = cast<ConstantSDNode>(Op)->getAPIntValue().trunc(EVTBits); 

================
Comment at: lib/CodeGen/SelectionDAG/DAGCombiner.cpp:5611
@@ -5613,4 +5610,3 @@
     if (Opcode == ISD::SIGN_EXTEND || Opcode == ISD::SIGN_EXTEND_VECTOR_INREG)
-      Elts.push_back(DAG.getConstant(C.shl(ShAmt).ashr(ShAmt).getZExtValue(),
-                                     DL, SVT));
+      Elts.push_back(DAG.getConstant(C.sextOrTrunc(VTBits), DL, SVT));
     else
----------------
I think you need to just drop the 'getZExtValue()' and keep as a APInt:
Elts.push_back(DAG.getConstant(C.shl(ShAmt).ashr(ShAmt), DL, SVT);

================
Comment at: lib/CodeGen/SelectionDAG/DAGCombiner.cpp:5613
@@ -5616,4 +5612,3 @@
     else
-      Elts.push_back(DAG.getConstant(C.shl(ShAmt).lshr(ShAmt).getZExtValue(),
-                                     DL, SVT));
+      Elts.push_back(DAG.getConstant(C.zextOrTrunc(VTBits), DL, SVT));
   }
----------------
Elts.push_back(DAG.getConstant(C.shl(ShAmt).lshr(ShAmt), DL, SVT);

================
Comment at: test/CodeGen/X86/fold-vector-sext-crash2.ll:2
@@ +1,3 @@
+; RUN: llc < %s | FileCheck %s
+
+; DAGCombiner crashes during sext folding
----------------
Missing -mtriple args

http://reviews.llvm.org/D10607

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the llvm-commits mailing list