Simplify ConstantExpr::getInBoundsGetElementPtr and fix a possible crash, if
authorDaniel Dunbar <daniel@zuster.org>
Tue, 11 Aug 2009 18:28:09 +0000 (18:28 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Tue, 11 Aug 2009 18:28:09 +0000 (18:28 +0000)
constant folding eliminated the GEP instruction.
 - clang was hitting this on its test suite (for x86_64, at least).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78698 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/Constants.cpp

index 89578fb8972e40c7da28b508340cef430c8ad4e1..70d43576a7375b81f7b2a75b8e5fc3d5d3d0661c 100644 (file)
@@ -1442,14 +1442,11 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
 Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
                                                  Value* const *Idxs,
                                                  unsigned NumIdx) {
-  // Get the result type of the getelementptr!
-  const Type *Ty =
-    GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
-  assert(Ty && "GEP indices invalid!");
-  unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
-  Constant *Result = getGetElementPtrTy(PointerType::get(Ty, As), C,
-                                        Idxs, NumIdx);
-  cast<GEPOperator>(Result)->setIsInBounds(true);
+  Constant *Result = getGetElementPtr(C, Idxs, NumIdx);
+  // Set in bounds attribute, assuming constant folding didn't eliminate the
+  // GEP.
+  if (GEPOperator *GEP = dyn_cast<GEPOperator>(Result))
+    GEP->setIsInBounds(true);
   return Result;
 }