diff options
author | Jay Foad <jay.foad@gmail.com> | 2011-07-21 14:31:17 +0000 |
---|---|---|
committer | Jay Foad <jay.foad@gmail.com> | 2011-07-21 14:31:17 +0000 |
commit | dab3d29605a5c83db41b28176273ef55961120c1 (patch) | |
tree | 12b71353630e2902c1f2d6ea9dfcbb8d974030f0 /lib/VMCore | |
parent | 14732a1f42e9df9c4ca4f6403f67de27b563fcbb (diff) |
Convert ConstantExpr::getGetElementPtr and
ConstantExpr::getInBoundsGetElementPtr to use ArrayRef.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@135673 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 29 | ||||
-rw-r--r-- | lib/VMCore/Constants.cpp | 36 | ||||
-rw-r--r-- | lib/VMCore/Core.cpp | 11 |
3 files changed, 35 insertions, 41 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 5b9d2ca366..2a2faf6590 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -127,8 +127,7 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) { if (ElTy == DPTy->getElementType()) // This GEP is inbounds because all indices are zero. - return ConstantExpr::getInBoundsGetElementPtr(V, &IdxList[0], - IdxList.size()); + return ConstantExpr::getInBoundsGetElementPtr(V, IdxList); } // Handle casts from one vector constant to another. We know that the src @@ -2146,9 +2145,9 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, /// isInBoundsIndices - Test whether the given sequence of *normalized* indices /// is "inbounds". template<typename IndexTy> -static bool isInBoundsIndices(IndexTy const *Idxs, size_t NumIdx) { +static bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) { // No indices means nothing that could be out of bounds. - if (NumIdx == 0) return true; + if (Idxs.empty()) return true; // If the first index is zero, it's in bounds. if (cast<Constant>(Idxs[0])->isNullValue()) return true; @@ -2157,7 +2156,7 @@ static bool isInBoundsIndices(IndexTy const *Idxs, size_t NumIdx) { // by the one-past-the-end rule. if (!cast<ConstantInt>(Idxs[0])->isOne()) return false; - for (unsigned i = 1, e = NumIdx; i != e; ++i) + for (unsigned i = 1, e = Idxs.size(); i != e; ++i) if (!cast<Constant>(Idxs[i])->isNullValue()) return false; return true; @@ -2234,11 +2233,8 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, NewIndices.append(Idxs.begin() + 1, Idxs.end()); return (inBounds && cast<GEPOperator>(CE)->isInBounds()) ? ConstantExpr::getInBoundsGetElementPtr(CE->getOperand(0), - &NewIndices[0], - NewIndices.size()) : - ConstantExpr::getGetElementPtr(CE->getOperand(0), - &NewIndices[0], - NewIndices.size()); + NewIndices) : + ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices); } } @@ -2256,9 +2252,9 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, if (CAT->getElementType() == SAT->getElementType()) return inBounds ? ConstantExpr::getInBoundsGetElementPtr( - (Constant*)CE->getOperand(0), Idxs.data(), Idxs.size()) : + (Constant*)CE->getOperand(0), Idxs) : ConstantExpr::getGetElementPtr( - (Constant*)CE->getOperand(0), Idxs.data(), Idxs.size()); + (Constant*)CE->getOperand(0), Idxs); } } @@ -2314,16 +2310,15 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, for (unsigned i = 0, e = Idxs.size(); i != e; ++i) if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]); return inBounds ? - ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs.data(), - NewIdxs.size()) : - ConstantExpr::getGetElementPtr(C, NewIdxs.data(), NewIdxs.size()); + ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs) : + ConstantExpr::getGetElementPtr(C, NewIdxs); } // If all indices are known integers and normalized, we can do a simple // check for the "inbounds" property. if (!Unknown && !inBounds && - isa<GlobalVariable>(C) && isInBoundsIndices(Idxs.data(), Idxs.size())) - return ConstantExpr::getInBoundsGetElementPtr(C, Idxs.data(), Idxs.size()); + isa<GlobalVariable>(C) && isInBoundsIndices(Idxs)) + return ConstantExpr::getInBoundsGetElementPtr(C, Idxs); return 0; } diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index d790c33d6a..5147a0d111 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -840,12 +840,12 @@ ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { Ops[i-1] = getOperand(i); if (OpNo == 0) return cast<GEPOperator>(this)->isInBounds() ? - ConstantExpr::getInBoundsGetElementPtr(Op, &Ops[0], Ops.size()) : - ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size()); + ConstantExpr::getInBoundsGetElementPtr(Op, Ops) : + ConstantExpr::getGetElementPtr(Op, Ops); Ops[OpNo-1] = Op; return cast<GEPOperator>(this)->isInBounds() ? - ConstantExpr::getInBoundsGetElementPtr(getOperand(0), &Ops[0],Ops.size()): - ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size()); + ConstantExpr::getInBoundsGetElementPtr(getOperand(0), Ops) : + ConstantExpr::getGetElementPtr(getOperand(0), Ops); } default: assert(getNumOperands() == 2 && "Must be binary operator?"); @@ -892,8 +892,8 @@ getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const { return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); case Instruction::GetElementPtr: return cast<GEPOperator>(this)->isInBounds() ? - ConstantExpr::getInBoundsGetElementPtr(Ops[0], &Ops[1], Ops.size()-1) : - ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], Ops.size()-1); + ConstantExpr::getInBoundsGetElementPtr(Ops[0], Ops.slice(1)) : + ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1)); case Instruction::ICmp: case Instruction::FCmp: return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]); @@ -1518,7 +1518,7 @@ Constant *ConstantExpr::getSizeOf(Type* Ty) { // Note that a non-inbounds gep is used, as null isn't within any object. Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); Constant *GEP = getGetElementPtr( - Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1); + Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx); return getPtrToInt(GEP, Type::getInt64Ty(Ty->getContext())); } @@ -1532,7 +1532,7 @@ Constant *ConstantExpr::getAlignOf(Type* Ty) { Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0); Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); Constant *Indices[2] = { Zero, One }; - Constant *GEP = getGetElementPtr(NullPtr, Indices, 2); + Constant *GEP = getGetElementPtr(NullPtr, Indices); return getPtrToInt(GEP, Type::getInt64Ty(Ty->getContext())); } @@ -1550,7 +1550,7 @@ Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) { FieldNo }; Constant *GEP = getGetElementPtr( - Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx, 2); + Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx); return getPtrToInt(GEP, Type::getInt64Ty(Ty->getContext())); } @@ -1592,15 +1592,14 @@ Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2) { return pImpl->ExprConstants.getOrCreate(V1->getType(), Key); } -Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs, - unsigned NumIdx, bool InBounds) { - if (Constant *FC = ConstantFoldGetElementPtr(C, InBounds, - makeArrayRef(Idxs, NumIdx))) +Constant *ConstantExpr::getGetElementPtr(Constant *C, ArrayRef<Value *> Idxs, + bool InBounds) { + if (Constant *FC = ConstantFoldGetElementPtr(C, InBounds, Idxs)) return FC; // Fold a few common cases. // Get the result type of the getelementptr! - Type *Ty = - GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx); + Type *Ty = + GetElementPtrInst::getIndexedType(C->getType(), Idxs.begin(), Idxs.end()); assert(Ty && "GEP indices invalid!"); unsigned AS = cast<PointerType>(C->getType())->getAddressSpace(); Type *ReqTy = Ty->getPointerTo(AS); @@ -1609,9 +1608,9 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs, "Non-pointer type for constant GetElementPtr expression"); // Look up the constant in the table first to ensure uniqueness std::vector<Constant*> ArgVec; - ArgVec.reserve(NumIdx+1); + ArgVec.reserve(1 + Idxs.size()); ArgVec.push_back(C); - for (unsigned i = 0; i != NumIdx; ++i) + for (unsigned i = 0, e = Idxs.size(); i != e; ++i) ArgVec.push_back(cast<Constant>(Idxs[i])); const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0, InBounds ? GEPOperator::IsInBounds : 0); @@ -2092,8 +2091,7 @@ void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, if (Val == From) Val = To; Indices.push_back(Val); } - Replacement = ConstantExpr::getGetElementPtr(Pointer, - &Indices[0], Indices.size(), + Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices, cast<GEPOperator>(this)->isInBounds()); } else if (getOpcode() == Instruction::ExtractValue) { Constant *Agg = getOperand(0); diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index 35ba43a263..e3b2cb400f 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -792,18 +792,19 @@ LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, LLVMValueRef *ConstantIndices, unsigned NumIndices) { + ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), + NumIndices); return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal), - unwrap<Constant>(ConstantIndices, - NumIndices), - NumIndices)); + IdxList)); } LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal, LLVMValueRef *ConstantIndices, unsigned NumIndices) { Constant* Val = unwrap<Constant>(ConstantVal); - Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices); - return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices)); + ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), + NumIndices); + return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList)); } LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { |