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

Simon Pilgrim llvm-dev at redking.me.uk
Mon Jun 22 15:31:30 PDT 2015


================
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)
----------------
RKSimon wrote:
> 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); 
Sorry that should have been
const APInt &C = cast<ConstantSDNode>(Op)->getAPIntValue().zextOrTrunc(EVTBits);

The aim is to ensure that the constant starts out as the bitwidth of input vector's scalar size so that the sext/zext below can work correctly.

================
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
----------------
chfast wrote:
> RKSimon wrote:
> > 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);
> That's definitely safer change. But isn't it the same?
Sorry you're right - if you make the zextOrTrunc change above this should work.

http://reviews.llvm.org/D10607

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






More information about the llvm-commits mailing list