[ConstantFold] Don't skip the first gep index when folding geps
[oota-llvm.git] / lib / IR / ConstantFold.cpp
index 3f64c43e902dcfbe9c89e0958dd45a3cc573012e..2efc61242eac655f78c8ffa43b65e10359fa5c0b 100644 (file)
@@ -2165,9 +2165,9 @@ static Constant *ConstantFoldGetElementPtrImpl(Type *PointeeTy, Constant *C,
   // factored out into preceding dimensions.
   bool Unknown = false;
   SmallVector<Constant *, 8> NewIdxs;
-  Type *Ty = PointeeTy;
-  Type *Prev = C->getType();
-  for (unsigned i = 1, e = Idxs.size(); i != e;
+  Type *Ty = C->getType();
+  Type *Prev = nullptr;
+  for (unsigned i = 0, e = Idxs.size(); i != e;
        Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
     if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
       if (isa<ArrayType>(Ty) || isa<VectorType>(Ty))