diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 521f6b84a25..41078d97f1d 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -23,6 +23,7 @@ #include "llvm/Instructions.h" #include "llvm/DerivedTypes.h" #include "llvm/Function.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/ManagedStatic.h" @@ -216,7 +217,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, // the first element. If so, return the appropriate GEP instruction. if (const PointerType *PTy = dyn_cast(V->getType())) if (const PointerType *DPTy = dyn_cast(DestTy)) { - std::vector IdxList; + SmallVector IdxList; IdxList.push_back(Constant::getNullValue(Type::Int32Ty)); const Type *ElTy = PTy->getElementType(); while (ElTy != DPTy->getElementType()) { @@ -236,7 +237,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, if (ElTy == DPTy->getElementType()) return ConstantExpr::getGetElementPtr( - const_cast(V),IdxList); + const_cast(V), &IdxList[0], IdxList.size()); } // Handle casts from one packed constant to another. We know that the src @@ -1290,28 +1291,31 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, } Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, - const std::vector &IdxList) { - if (IdxList.size() == 0 || - (IdxList.size() == 1 && cast(IdxList[0])->isNullValue())) + Constant* const *Idxs, + unsigned NumIdx) { + if (NumIdx == 0 || + (NumIdx == 1 && Idxs[0]->isNullValue())) return const_cast(C); if (isa(C)) { - const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList, + const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), + (Value**)Idxs, NumIdx, true); assert(Ty != 0 && "Invalid indices for GEP!"); return UndefValue::get(PointerType::get(Ty)); } - Constant *Idx0 = cast(IdxList[0]); + Constant *Idx0 = Idxs[0]; if (C->isNullValue()) { bool isNull = true; - for (unsigned i = 0, e = IdxList.size(); i != e; ++i) - if (!cast(IdxList[i])->isNullValue()) { + for (unsigned i = 0, e = NumIdx; i != e; ++i) + if (!Idxs[i]->isNullValue()) { isNull = false; break; } if (isNull) { - const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList, + const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), + (Value**)Idxs, NumIdx, true); assert(Ty != 0 && "Invalid indices for GEP!"); return ConstantPointerNull::get(PointerType::get(Ty)); @@ -1330,8 +1334,8 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, LastTy = *I; if ((LastTy && isa(LastTy)) || Idx0->isNullValue()) { - std::vector NewIndices; - NewIndices.reserve(IdxList.size() + CE->getNumOperands()); + SmallVector NewIndices; + NewIndices.reserve(NumIdx + CE->getNumOperands()); for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) NewIndices.push_back(CE->getOperand(i)); @@ -1353,8 +1357,9 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, } NewIndices.push_back(Combined); - NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end()); - return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices); + NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx); + return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0], + NewIndices.size()); } } @@ -1363,7 +1368,7 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, // long 0, long 0) // To: int* getelementptr ([3 x int]* %X, long 0, long 0) // - if (CE->isCast() && IdxList.size() > 1 && Idx0->isNullValue()) + if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) if (const PointerType *SPT = dyn_cast(CE->getOperand(0)->getType())) if (const ArrayType *SAT = dyn_cast(SPT->getElementType())) @@ -1371,7 +1376,7 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, dyn_cast(cast(C->getType())->getElementType())) if (CAT->getElementType() == SAT->getElementType()) return ConstantExpr::getGetElementPtr( - (Constant*)CE->getOperand(0), IdxList); + (Constant*)CE->getOperand(0), Idxs, NumIdx); } return 0; } diff --git a/lib/VMCore/ConstantFold.h b/lib/VMCore/ConstantFold.h index 6d7327c01e7..e01fa770840 100644 --- a/lib/VMCore/ConstantFold.h +++ b/lib/VMCore/ConstantFold.h @@ -19,8 +19,6 @@ #ifndef CONSTANTFOLDING_H #define CONSTANTFOLDING_H -#include - namespace llvm { class Value; class Constant; @@ -49,7 +47,7 @@ namespace llvm { const Constant *C1, const Constant *C2); Constant *ConstantFoldGetElementPtr(const Constant *C, - const std::vector &IdxList); + Constant* const *Idxs, unsigned NumIdx); } // End llvm namespace #endif diff --git a/lib/VMCore/ConstantFolding.h b/lib/VMCore/ConstantFolding.h index 6d7327c01e7..e01fa770840 100644 --- a/lib/VMCore/ConstantFolding.h +++ b/lib/VMCore/ConstantFolding.h @@ -19,8 +19,6 @@ #ifndef CONSTANTFOLDING_H #define CONSTANTFOLDING_H -#include - namespace llvm { class Value; class Constant; @@ -49,7 +47,7 @@ namespace llvm { const Constant *C1, const Constant *C2); Constant *ConstantFoldGetElementPtr(const Constant *C, - const std::vector &IdxList); + Constant* const *Idxs, unsigned NumIdx); } // End llvm namespace #endif diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index c15ababf32b..cb296ad6ab9 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -1377,7 +1377,8 @@ namespace llvm { case Instruction::GetElementPtr: // Make everyone now use a constant of the new type... std::vector Idx(OldC->op_begin()+1, OldC->op_end()); - New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx); + New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), + &Idx[0], Idx.size()); break; } @@ -1744,45 +1745,41 @@ Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode, } Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, - const std::vector &IdxList) { - assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) && + Value* const *Idxs, + unsigned NumIdx) { + assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true) && "GEP indices invalid!"); - if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList)) + if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx)) return FC; // Fold a few common cases... assert(isa(C->getType()) && "Non-pointer type for constant GetElementPtr expression"); // Look up the constant in the table first to ensure uniqueness std::vector ArgVec; - ArgVec.reserve(IdxList.size()+1); + ArgVec.reserve(NumIdx+1); ArgVec.push_back(C); - for (unsigned i = 0, e = IdxList.size(); i != e; ++i) - ArgVec.push_back(cast(IdxList[i])); - const ExprMapKeyType Key(Instruction::GetElementPtr,ArgVec); + for (unsigned i = 0; i != NumIdx; ++i) + ArgVec.push_back(cast(Idxs[i])); + const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec); return ExprConstants->getOrCreate(ReqTy, Key); } -Constant *ConstantExpr::getGetElementPtr(Constant *C, - const std::vector &IdxList){ +Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs, + unsigned NumIdx) { // Get the result type of the getelementptr! - std::vector VIdxList(IdxList.begin(), IdxList.end()); - - const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList, - true); + const Type *Ty = + GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true); assert(Ty && "GEP indices invalid!"); - return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList); + return getGetElementPtrTy(PointerType::get(Ty), C, Idxs, NumIdx); } -Constant *ConstantExpr::getGetElementPtr(Constant *C, - const std::vector &IdxList) { - // Get the result type of the getelementptr! - const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList, - true); - assert(Ty && "GEP indices invalid!"); - return getGetElementPtrTy(PointerType::get(Ty), C, IdxList); +Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs, + unsigned NumIdx) { + return getGetElementPtr(C, (Value* const *)Idxs, NumIdx); } + Constant * ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) { assert(LHS->getType() == RHS->getType()); diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index f996ab88e53..ccd25de022f 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -765,12 +765,13 @@ GetElementPtrInst::~GetElementPtrInst() { // pointer type. // const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, - const std::vector &Idx, + Value* const *Idxs, + unsigned NumIdx, bool AllowCompositeLeaf) { if (!isa(Ptr)) return 0; // Type isn't a pointer type! // Handle the special case of the empty set index set... - if (Idx.empty()) + if (NumIdx == 0) if (AllowCompositeLeaf || cast(Ptr)->getElementType()->isFirstClassType()) return cast(Ptr)->getElementType(); @@ -779,12 +780,12 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, unsigned CurIdx = 0; while (const CompositeType *CT = dyn_cast(Ptr)) { - if (Idx.size() == CurIdx) { + if (NumIdx == CurIdx) { if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; return 0; // Can't load a whole structure or array!?!? } - Value *Index = Idx[CurIdx++]; + Value *Index = Idxs[CurIdx++]; if (isa(CT) && CurIdx != 1) return 0; // Can only index into pointer types at the first index! if (!CT->indexValid(Index)) return 0; @@ -798,7 +799,7 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Ptr = Ty; } } - return CurIdx == Idx.size() ? Ptr : 0; + return CurIdx == NumIdx ? Ptr : 0; } const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,