[llvm-commits] [llvm] r63128 - in /llvm/trunk: include/llvm/CodeGen/ValueTypes.h lib/CodeGen/SelectionDAG/DAGCombiner.cpp lib/CodeGen/SelectionDAG/LegalizeDAG.cpp lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Dan Gohman gohman at apple.com
Tue Jan 27 12:39:34 PST 2009


Author: djg
Date: Tue Jan 27 14:39:34 2009
New Revision: 63128

URL: http://llvm.org/viewvc/llvm-project?rev=63128&view=rev
Log:
Add an assertion to the form of SelectionDAG::getConstant that takes
a uint64_t to verify that the value is in range for the given type,
to help catch accidental overflow. Fix a few places that relied on
getConstant implicitly truncating the value.

Modified:
    llvm/trunk/include/llvm/CodeGen/ValueTypes.h
    llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
    llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
    llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Modified: llvm/trunk/include/llvm/CodeGen/ValueTypes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/ValueTypes.h?rev=63128&r1=63127&r2=63128&view=diff

==============================================================================
--- llvm/trunk/include/llvm/CodeGen/ValueTypes.h (original)
+++ llvm/trunk/include/llvm/CodeGen/ValueTypes.h Tue Jan 27 14:39:34 2009
@@ -444,8 +444,11 @@
     /// getIntegerVTBitMask - Return an integer with 1's every place there are
     /// bits in the specified integer value type. FIXME: Should return an apint.
     uint64_t getIntegerVTBitMask() const {
-      assert(isInteger() && !isVector() && "Only applies to int scalars!");
-      return ~uint64_t(0UL) >> (64-getSizeInBits());
+      assert(isInteger() && "Only applies to integers!");
+      MVT EltVT = isVector() ? getVectorElementType() : *this;
+      assert(EltVT.getSizeInBits() <= 64 &&
+             "getIntegerVTBitMask doesn't use APInt!");
+      return ~uint64_t(0UL) >> (64-EltVT.getSizeInBits());
     }
 
     /// getIntegerVTSignBit - Return an integer with a 1 in the position of the

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp?rev=63128&r1=63127&r2=63128&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp Tue Jan 27 14:39:34 2009
@@ -2434,11 +2434,12 @@
     if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
       MVT TruncVT = N1.getValueType();
       SDValue N100 = N1.getOperand(0).getOperand(0);
+      uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
+                        N101C->getZExtValue();
       return DAG.getNode(ISD::SHL, VT, N0,
                          DAG.getNode(ISD::AND, TruncVT,
                                      DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
-                                     DAG.getConstant(N101C->getZExtValue(),
-                                                     TruncVT)));
+                                     DAG.getConstant(TruncC, TruncVT)));
     }
   }
 
@@ -2561,11 +2562,12 @@
     if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
       MVT TruncVT = N1.getValueType();
       SDValue N100 = N1.getOperand(0).getOperand(0);
+      uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
+                        N101C->getZExtValue();
       return DAG.getNode(ISD::SRA, VT, N0,
                          DAG.getNode(ISD::AND, TruncVT,
                                      DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
-                                     DAG.getConstant(N101C->getZExtValue(),
-                                                     TruncVT)));
+                                     DAG.getConstant(TruncC, TruncVT)));
     }
   }
 
@@ -2678,11 +2680,12 @@
     if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
       MVT TruncVT = N1.getValueType();
       SDValue N100 = N1.getOperand(0).getOperand(0);
+      uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
+                        N101C->getZExtValue();
       return DAG.getNode(ISD::SRL, VT, N0,
                          DAG.getNode(ISD::AND, TruncVT,
                                      DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
-                                     DAG.getConstant(N101C->getZExtValue(),
-                                                     TruncVT)));
+                                     DAG.getConstant(TruncC, TruncVT)));
     }
   }
   

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp?rev=63128&r1=63127&r2=63128&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp Tue Jan 27 14:39:34 2009
@@ -6293,7 +6293,7 @@
     unsigned len = VT.getSizeInBits();
     for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
       //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
-      SDValue Tmp2 = DAG.getConstant(mask[i], VT);
+      SDValue Tmp2 = DAG.getConstant(VT.getIntegerVTBitMask() & mask[i], VT);
       SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
       Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
                        DAG.getNode(ISD::AND, VT,

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp?rev=63128&r1=63127&r2=63128&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp Tue Jan 27 14:39:34 2009
@@ -848,6 +848,9 @@
 
 SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
   MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
+  assert((EltVT.getSizeInBits() >= 64 ||
+         (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
+         "getConstant with a uint64_t value that doesn't fit in the type!");
   return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
 }
 





More information about the llvm-commits mailing list