diff --git a/lib/IR/ConstantFold.cpp b/lib/IR/ConstantFold.cpp index 46e391abb04..3219be11b68 100644 --- a/lib/IR/ConstantFold.cpp +++ b/lib/IR/ConstantFold.cpp @@ -1966,11 +1966,12 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, else if (VectorType *VTy = dyn_cast(LastTy)) NumElements = VTy->getNumElements(); - if (ConstantInt *CI = dyn_cast(Idx0)) { - int64_t Idx0Val = CI->getSExtValue(); - if (NumElements > 0 && Idx0Val >= 0 && - (uint64_t)Idx0Val < NumElements) - IsSequentialAccessInRange = true; + if (NumElements > 0) { + if (ConstantInt *CI = dyn_cast(Idx0)) { + int64_t Idx0Val = CI->getSExtValue(); + if (Idx0Val >= 0 && (uint64_t)Idx0Val < NumElements) + IsSequentialAccessInRange = true; + } } else if (PointerType *PTy = dyn_cast(LastTy)) // Only handle pointers to sized types, not pointers to functions. if (PTy->getElementType()->isSized())