diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index a2f4f621c94..5b9d2ca3661 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -2166,31 +2166,30 @@ static bool isInBoundsIndices(IndexTy const *Idxs, size_t NumIdx) { template static Constant *ConstantFoldGetElementPtrImpl(Constant *C, bool inBounds, - IndexTy const *Idxs, - unsigned NumIdx) { - if (NumIdx == 0) return C; + ArrayRef Idxs) { + if (Idxs.empty()) return C; Constant *Idx0 = cast(Idxs[0]); - if ((NumIdx == 1 && Idx0->isNullValue())) + if ((Idxs.size() == 1 && Idx0->isNullValue())) return C; if (isa(C)) { PointerType *Ptr = cast(C->getType()); - Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs, Idxs+NumIdx); + Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs.begin(), Idxs.end()); assert(Ty != 0 && "Invalid indices for GEP!"); return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace())); } if (C->isNullValue()) { bool isNull = true; - for (unsigned i = 0, e = NumIdx; i != e; ++i) + for (unsigned i = 0, e = Idxs.size(); i != e; ++i) if (!cast(Idxs[i])->isNullValue()) { isNull = false; break; } if (isNull) { PointerType *Ptr = cast(C->getType()); - Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs, - Idxs+NumIdx); + Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs.begin(), + Idxs.end()); assert(Ty != 0 && "Invalid indices for GEP!"); return ConstantPointerNull::get(PointerType::get(Ty, Ptr->getAddressSpace())); @@ -2210,7 +2209,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, if ((LastTy && LastTy->isArrayTy()) || Idx0->isNullValue()) { SmallVector NewIndices; - NewIndices.reserve(NumIdx + CE->getNumOperands()); + NewIndices.reserve(Idxs.size() + CE->getNumOperands()); for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) NewIndices.push_back(CE->getOperand(i)); @@ -2232,7 +2231,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, } NewIndices.push_back(Combined); - NewIndices.append(Idxs+1, Idxs+NumIdx); + NewIndices.append(Idxs.begin() + 1, Idxs.end()); return (inBounds && cast(CE)->isInBounds()) ? ConstantExpr::getInBoundsGetElementPtr(CE->getOperand(0), &NewIndices[0], @@ -2248,7 +2247,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, // i64 0, i64 0) // To: i32* getelementptr ([3 x i32]* %X, i64 0, i64 0) // - if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) { + if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) { if (PointerType *SPT = dyn_cast(CE->getOperand(0)->getType())) if (ArrayType *SAT = dyn_cast(SPT->getElementType())) @@ -2257,9 +2256,9 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, if (CAT->getElementType() == SAT->getElementType()) return inBounds ? ConstantExpr::getInBoundsGetElementPtr( - (Constant*)CE->getOperand(0), Idxs, NumIdx) : + (Constant*)CE->getOperand(0), Idxs.data(), Idxs.size()) : ConstantExpr::getGetElementPtr( - (Constant*)CE->getOperand(0), Idxs, NumIdx); + (Constant*)CE->getOperand(0), Idxs.data(), Idxs.size()); } } @@ -2270,7 +2269,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, SmallVector NewIdxs; Type *Ty = C->getType(); Type *Prev = 0; - for (unsigned i = 0; i != NumIdx; + for (unsigned i = 0, e = Idxs.size(); i != e; Prev = Ty, Ty = cast(Ty)->getTypeAtIndex(Idxs[i]), ++i) { if (ConstantInt *CI = dyn_cast(Idxs[i])) { if (ArrayType *ATy = dyn_cast(Ty)) @@ -2280,7 +2279,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, if (isa(Prev)) { // It's out of range, but we can factor it into the prior // dimension. - NewIdxs.resize(NumIdx); + NewIdxs.resize(Idxs.size()); ConstantInt *Factor = ConstantInt::get(CI->getType(), ATy->getNumElements()); NewIdxs[i] = ConstantExpr::getSRem(CI, Factor); @@ -2312,7 +2311,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, // If we did any factoring, start over with the adjusted indices. if (!NewIdxs.empty()) { - for (unsigned i = 0; i != NumIdx; ++i) + for (unsigned i = 0, e = Idxs.size(); i != e; ++i) if (!NewIdxs[i]) NewIdxs[i] = cast(Idxs[i]); return inBounds ? ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs.data(), @@ -2323,22 +2322,20 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, // If all indices are known integers and normalized, we can do a simple // check for the "inbounds" property. if (!Unknown && !inBounds && - isa(C) && isInBoundsIndices(Idxs, NumIdx)) - return ConstantExpr::getInBoundsGetElementPtr(C, Idxs, NumIdx); + isa(C) && isInBoundsIndices(Idxs.data(), Idxs.size())) + return ConstantExpr::getInBoundsGetElementPtr(C, Idxs.data(), Idxs.size()); return 0; } Constant *llvm::ConstantFoldGetElementPtr(Constant *C, bool inBounds, - Constant* const *Idxs, - unsigned NumIdx) { - return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs, NumIdx); + ArrayRef Idxs) { + return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs); } Constant *llvm::ConstantFoldGetElementPtr(Constant *C, bool inBounds, - Value* const *Idxs, - unsigned NumIdx) { - return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs, NumIdx); + ArrayRef Idxs) { + return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs); } diff --git a/lib/VMCore/ConstantFold.h b/lib/VMCore/ConstantFold.h index aa1af8f7cf5..e12f27a7cb1 100644 --- a/lib/VMCore/ConstantFold.h +++ b/lib/VMCore/ConstantFold.h @@ -48,9 +48,9 @@ namespace llvm { Constant *ConstantFoldCompareInstruction(unsigned short predicate, Constant *C1, Constant *C2); Constant *ConstantFoldGetElementPtr(Constant *C, bool inBounds, - Constant* const *Idxs, unsigned NumIdx); + ArrayRef Idxs); Constant *ConstantFoldGetElementPtr(Constant *C, bool inBounds, - Value* const *Idxs, unsigned NumIdx); + ArrayRef Idxs); } // End llvm namespace #endif diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index e9c049e1ce0..d790c33d6a6 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -1594,7 +1594,8 @@ Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2) { Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs, unsigned NumIdx, bool InBounds) { - if (Constant *FC = ConstantFoldGetElementPtr(C, InBounds, Idxs, NumIdx)) + if (Constant *FC = ConstantFoldGetElementPtr(C, InBounds, + makeArrayRef(Idxs, NumIdx))) return FC; // Fold a few common cases. // Get the result type of the getelementptr!