From d4458dd70705df720f7f9b69cb395bf9bf8d2786 Mon Sep 17 00:00:00 2001
From: Jay Foad
Date: Thu, 21 Jul 2011 14:31:17 +0000
Subject: [PATCH] Convert ConstantExpr::getGetElementPtr and
ConstantExpr::getInBoundsGetElementPtr to use ArrayRef.
llvm-svn: 135673
---
docs/ReleaseNotes.html | 2 ++
examples/BrainF/BrainF.cpp | 3 +-
include/llvm/Constants.h | 33 ++++++++++++-----
include/llvm/Support/ConstantFolder.h | 14 ++++----
include/llvm/Support/NoFolder.h | 5 ++-
include/llvm/Support/TargetFolder.h | 16 ++++-----
lib/Analysis/ConstantFolding.cpp | 7 ++--
lib/Analysis/InstructionSimplify.cpp | 4 +--
lib/Analysis/ScalarEvolutionExpander.cpp | 2 +-
lib/AsmParser/LLParser.cpp | 8 ++---
lib/Bitcode/Reader/BitcodeReader.cpp | 7 ++--
lib/Target/ARM/ARMGlobalMerge.cpp | 2 +-
lib/Transforms/IPO/GlobalOpt.cpp | 10 +++---
.../Instrumentation/ProfilingUtils.cpp | 5 ++-
lib/Transforms/Scalar/GVN.cpp | 4 +--
lib/Transforms/Scalar/SCCP.cpp | 4 +--
lib/VMCore/ConstantFold.cpp | 29 +++++++--------
lib/VMCore/Constants.cpp | 36 +++++++++----------
lib/VMCore/Core.cpp | 11 +++---
tools/bugpoint/Miscompilation.cpp | 3 +-
20 files changed, 99 insertions(+), 106 deletions(-)
diff --git a/docs/ReleaseNotes.html b/docs/ReleaseNotes.html
index 0eac384b622..5fc388e0da7 100644
--- a/docs/ReleaseNotes.html
+++ b/docs/ReleaseNotes.html
@@ -623,6 +623,8 @@ from the previous release.
ComputeLinearIndex
(in llvm/CodeGen/Analysis.h
)
ConstantArray::get
ConstantExpr::getExtractElement
+ConstantExpr::getGetElementPtr
+ConstantExpr::getInBoundsGetElementPtr
ConstantExpr::getIndices
ConstantExpr::getInsertElement
ConstantExpr::getWithOperands
diff --git a/examples/BrainF/BrainF.cpp b/examples/BrainF/BrainF.cpp
index cd9a1408715..df6687f2805 100644
--- a/examples/BrainF/BrainF.cpp
+++ b/examples/BrainF/BrainF.cpp
@@ -162,8 +162,7 @@ void BrainF::header(LLVMContext& C) {
};
Constant *msgptr = ConstantExpr::
- getGetElementPtr(aberrormsg, gep_params,
- array_lengthof(gep_params));
+ getGetElementPtr(aberrormsg, gep_params);
Value *puts_params[] = {
msgptr
diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h
index 1302a015345..20ed1344dbf 100644
--- a/include/llvm/Constants.h
+++ b/include/llvm/Constants.h
@@ -788,25 +788,40 @@ public:
/// all elements must be Constant's.
///
static Constant *getGetElementPtr(Constant *C,
- Constant *const *IdxList, unsigned NumIdx,
+ ArrayRef IdxList,
bool InBounds = false) {
- return getGetElementPtr(C, (Value**)IdxList, NumIdx, InBounds);
+ return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(),
+ IdxList.size()),
+ InBounds);
}
static Constant *getGetElementPtr(Constant *C,
- Value *const *IdxList, unsigned NumIdx,
+ Constant *Idx,
+ bool InBounds = false) {
+ // This form of the function only exists to avoid ambiguous overload
+ // warnings about whether to convert Idx to ArrayRef or
+ // ArrayRef.
+ return getGetElementPtr(C, cast(Idx), InBounds);
+ }
+ static Constant *getGetElementPtr(Constant *C,
+ ArrayRef IdxList,
bool InBounds = false);
/// Create an "inbounds" getelementptr. See the documentation for the
/// "inbounds" flag in LangRef.html for details.
static Constant *getInBoundsGetElementPtr(Constant *C,
- Constant *const *IdxList,
- unsigned NumIdx) {
- return getGetElementPtr(C, IdxList, NumIdx, true);
+ ArrayRef IdxList) {
+ return getGetElementPtr(C, IdxList, true);
}
static Constant *getInBoundsGetElementPtr(Constant *C,
- Value* const *IdxList,
- unsigned NumIdx) {
- return getGetElementPtr(C, IdxList, NumIdx, true);
+ Constant *Idx) {
+ // This form of the function only exists to avoid ambiguous overload
+ // warnings about whether to convert Idx to ArrayRef or
+ // ArrayRef.
+ return getGetElementPtr(C, Idx, true);
+ }
+ static Constant *getInBoundsGetElementPtr(Constant *C,
+ ArrayRef IdxList) {
+ return getGetElementPtr(C, IdxList, true);
}
static Constant *getExtractElement(Constant *Vec, Constant *Idx);
diff --git a/include/llvm/Support/ConstantFolder.h b/include/llvm/Support/ConstantFolder.h
index 771784b4f9c..93aa3436d27 100644
--- a/include/llvm/Support/ConstantFolder.h
+++ b/include/llvm/Support/ConstantFolder.h
@@ -120,34 +120,32 @@ public:
Constant *CreateGetElementPtr(Constant *C,
ArrayRef IdxList) const {
- return ConstantExpr::getGetElementPtr(C, IdxList.data(), IdxList.size());
+ return ConstantExpr::getGetElementPtr(C, IdxList);
}
Constant *CreateGetElementPtr(Constant *C, Constant *Idx) const {
// This form of the function only exists to avoid ambiguous overload
// warnings about whether to convert Idx to ArrayRef or
// ArrayRef.
- return ConstantExpr::getGetElementPtr(C, &Idx, 1);
+ return ConstantExpr::getGetElementPtr(C, Idx);
}
Constant *CreateGetElementPtr(Constant *C,
ArrayRef IdxList) const {
- return ConstantExpr::getGetElementPtr(C, IdxList.data(), IdxList.size());
+ return ConstantExpr::getGetElementPtr(C, IdxList);
}
Constant *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef IdxList) const {
- return ConstantExpr::getInBoundsGetElementPtr(C, IdxList.data(),
- IdxList.size());
+ return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
}
Constant *CreateInBoundsGetElementPtr(Constant *C, Constant *Idx) const {
// This form of the function only exists to avoid ambiguous overload
// warnings about whether to convert Idx to ArrayRef or
// ArrayRef.
- return ConstantExpr::getInBoundsGetElementPtr(C, &Idx, 1);
+ return ConstantExpr::getInBoundsGetElementPtr(C, Idx);
}
Constant *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef IdxList) const {
- return ConstantExpr::getInBoundsGetElementPtr(C, IdxList.data(),
- IdxList.size());
+ return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
}
//===--------------------------------------------------------------------===//
diff --git a/include/llvm/Support/NoFolder.h b/include/llvm/Support/NoFolder.h
index 7f14cd2bcd1..88e55a3d9bb 100644
--- a/include/llvm/Support/NoFolder.h
+++ b/include/llvm/Support/NoFolder.h
@@ -179,7 +179,7 @@ public:
Constant *CreateGetElementPtr(Constant *C,
ArrayRef IdxList) const {
- return ConstantExpr::getGetElementPtr(C, IdxList.data(), IdxList.size());
+ return ConstantExpr::getGetElementPtr(C, IdxList);
}
Instruction *CreateGetElementPtr(Constant *C,
ArrayRef IdxList) const {
@@ -188,8 +188,7 @@ public:
Constant *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef IdxList) const {
- return ConstantExpr::getInBoundsGetElementPtr(C, IdxList.data(),
- IdxList.size());
+ return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
}
Instruction *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef IdxList) const {
diff --git a/include/llvm/Support/TargetFolder.h b/include/llvm/Support/TargetFolder.h
index e6b501c88e6..c65faa66219 100644
--- a/include/llvm/Support/TargetFolder.h
+++ b/include/llvm/Support/TargetFolder.h
@@ -132,36 +132,32 @@ public:
Constant *CreateGetElementPtr(Constant *C,
ArrayRef IdxList) const {
- return Fold(ConstantExpr::getGetElementPtr(C, IdxList.data(),
- IdxList.size()));
+ return Fold(ConstantExpr::getGetElementPtr(C, IdxList));
}
Constant *CreateGetElementPtr(Constant *C, Constant *Idx) const {
// This form of the function only exists to avoid ambiguous overload
// warnings about whether to convert Idx to ArrayRef or
// ArrayRef.
- return Fold(ConstantExpr::getGetElementPtr(C, &Idx, 1));
+ return Fold(ConstantExpr::getGetElementPtr(C, Idx));
}
Constant *CreateGetElementPtr(Constant *C,
ArrayRef IdxList) const {
- return Fold(ConstantExpr::getGetElementPtr(C, IdxList.data(),
- IdxList.size()));
+ return Fold(ConstantExpr::getGetElementPtr(C, IdxList));
}
Constant *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef IdxList) const {
- return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList.data(),
- IdxList.size()));
+ return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList));
}
Constant *CreateInBoundsGetElementPtr(Constant *C, Constant *Idx) const {
// This form of the function only exists to avoid ambiguous overload
// warnings about whether to convert Idx to ArrayRef or
// ArrayRef.
- return Fold(ConstantExpr::getInBoundsGetElementPtr(C, &Idx, 1));
+ return Fold(ConstantExpr::getInBoundsGetElementPtr(C, Idx));
}
Constant *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef IdxList) const {
- return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList.data(),
- IdxList.size()));
+ return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList));
}
//===--------------------------------------------------------------------===//
diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp
index b60432be832..c9f738b6c3c 100644
--- a/lib/Analysis/ConstantFolding.cpp
+++ b/lib/Analysis/ConstantFolding.cpp
@@ -562,7 +562,7 @@ static Constant *CastGEPIndices(ArrayRef Ops,
if (!Any) return 0;
Constant *C =
- ConstantExpr::getGetElementPtr(Ops[0], &NewIdxs[0], NewIdxs.size());
+ ConstantExpr::getGetElementPtr(Ops[0], NewIdxs);
if (ConstantExpr *CE = dyn_cast(C))
if (Constant *Folded = ConstantFoldConstantExpression(CE, TD))
C = Folded;
@@ -702,7 +702,7 @@ static Constant *SymbolicallyEvaluateGEP(ArrayRef Ops,
// Create a GEP.
Constant *C =
- ConstantExpr::getGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size());
+ ConstantExpr::getGetElementPtr(Ptr, NewIdxs);
assert(cast(C->getType())->getElementType() == Ty &&
"Computed GetElementPtr has unexpected type!");
@@ -889,8 +889,7 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
if (Constant *C = SymbolicallyEvaluateGEP(Ops, DestTy, TD))
return C;
- return ConstantExpr::getGetElementPtr(Ops[0], Ops.data() + 1,
- Ops.size() - 1);
+ return ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1));
}
}
diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp
index 740351ceb8b..aa1546b8776 100644
--- a/lib/Analysis/InstructionSimplify.cpp
+++ b/lib/Analysis/InstructionSimplify.cpp
@@ -2254,9 +2254,7 @@ Value *llvm::SimplifyGEPInst(ArrayRef Ops,
if (!isa(Ops[i]))
return 0;
- return ConstantExpr::getGetElementPtr(cast(Ops[0]),
- (Constant *const*)Ops.data() + 1,
- Ops.size() - 1);
+ return ConstantExpr::getGetElementPtr(cast(Ops[0]), Ops.slice(1));
}
/// SimplifyPHINode - See if we can fold the given phi. If not, returns null.
diff --git a/lib/Analysis/ScalarEvolutionExpander.cpp b/lib/Analysis/ScalarEvolutionExpander.cpp
index 1904bdc5867..7ebf82a2aef 100644
--- a/lib/Analysis/ScalarEvolutionExpander.cpp
+++ b/lib/Analysis/ScalarEvolutionExpander.cpp
@@ -494,7 +494,7 @@ Value *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin,
// Fold a GEP with constant operands.
if (Constant *CLHS = dyn_cast(V))
if (Constant *CRHS = dyn_cast(Idx))
- return ConstantExpr::getGetElementPtr(CLHS, &CRHS, 1);
+ return ConstantExpr::getGetElementPtr(CLHS, CRHS);
// Do a quick scan to see if we have this GEP nearby. If so, reuse it.
unsigned ScanLimit = 6;
diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp
index c70731f018c..32993f73acb 100644
--- a/lib/AsmParser/LLParser.cpp
+++ b/lib/AsmParser/LLParser.cpp
@@ -2273,16 +2273,14 @@ bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
if (Elts.size() == 0 || !Elts[0]->getType()->isPointerTy())
return Error(ID.Loc, "getelementptr requires pointer operand");
+ ArrayRef Indices(Elts.begin() + 1, Elts.end());
if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(),
(Value**)(Elts.data() + 1),
Elts.size() - 1))
return Error(ID.Loc, "invalid indices for getelementptr");
ID.ConstantVal = InBounds ?
- ConstantExpr::getInBoundsGetElementPtr(Elts[0],
- Elts.data() + 1,
- Elts.size() - 1) :
- ConstantExpr::getGetElementPtr(Elts[0],
- Elts.data() + 1, Elts.size() - 1);
+ ConstantExpr::getInBoundsGetElementPtr(Elts[0], Indices) :
+ ConstantExpr::getGetElementPtr(Elts[0], Indices);
} else if (Opc == Instruction::Select) {
if (Elts.size() != 3)
return Error(ID.Loc, "expected three operands to select");
diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp
index 374a0418c95..910987a472d 100644
--- a/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1351,12 +1351,11 @@ bool BitcodeReader::ParseConstants() {
if (!ElTy) return Error("Invalid CE_GEP record");
Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
}
+ ArrayRef Indices(Elts.begin() + 1, Elts.end());
if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP)
- V = ConstantExpr::getInBoundsGetElementPtr(Elts[0], &Elts[1],
- Elts.size()-1);
+ V = ConstantExpr::getInBoundsGetElementPtr(Elts[0], Indices);
else
- V = ConstantExpr::getGetElementPtr(Elts[0], &Elts[1],
- Elts.size()-1);
+ V = ConstantExpr::getGetElementPtr(Elts[0], Indices);
break;
}
case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#]
diff --git a/lib/Target/ARM/ARMGlobalMerge.cpp b/lib/Target/ARM/ARMGlobalMerge.cpp
index e4b732ca1e4..5f863ea241c 100644
--- a/lib/Target/ARM/ARMGlobalMerge.cpp
+++ b/lib/Target/ARM/ARMGlobalMerge.cpp
@@ -150,7 +150,7 @@ bool ARMGlobalMerge::doMerge(SmallVectorImpl &Globals,
ConstantInt::get(Int32Ty, 0),
ConstantInt::get(Int32Ty, k-i)
};
- Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedGV, Idx, 2);
+ Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedGV, Idx);
Globals[k]->replaceAllUsesWith(GEP);
Globals[k]->eraseFromParent();
}
diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp
index 5c2b7e1a10b..2d971a1e192 100644
--- a/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/lib/Transforms/IPO/GlobalOpt.cpp
@@ -596,8 +596,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD) {
Idxs.push_back(NullInt);
for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i)
Idxs.push_back(CE->getOperand(i));
- NewPtr = ConstantExpr::getGetElementPtr(cast(NewPtr),
- &Idxs[0], Idxs.size());
+ NewPtr = ConstantExpr::getGetElementPtr(cast(NewPtr), Idxs);
} else {
GetElementPtrInst *GEPI = cast(GEP);
SmallVector Idxs;
@@ -753,8 +752,7 @@ static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) {
break;
if (Idxs.size() == GEPI->getNumOperands()-1)
Changed |= OptimizeAwayTrappingUsesOfValue(GEPI,
- ConstantExpr::getGetElementPtr(NewV, &Idxs[0],
- Idxs.size()));
+ ConstantExpr::getGetElementPtr(NewV, Idxs));
if (GEPI->use_empty()) {
Changed = true;
GEPI->eraseFromParent();
@@ -2410,8 +2408,8 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal,
i != e; ++i)
GEPOps.push_back(getVal(Values, *i));
InstResult = cast(GEP)->isInBounds() ?
- ConstantExpr::getInBoundsGetElementPtr(P, &GEPOps[0], GEPOps.size()) :
- ConstantExpr::getGetElementPtr(P, &GEPOps[0], GEPOps.size());
+ ConstantExpr::getInBoundsGetElementPtr(P, GEPOps) :
+ ConstantExpr::getGetElementPtr(P, GEPOps);
} else if (LoadInst *LI = dyn_cast(CurInst)) {
if (LI->isVolatile()) return false; // no volatile accesses.
InstResult = ComputeLoadResult(getVal(Values, LI->getOperand(0)),
diff --git a/lib/Transforms/Instrumentation/ProfilingUtils.cpp b/lib/Transforms/Instrumentation/ProfilingUtils.cpp
index 0ebab33f5cb..0263c75c752 100644
--- a/lib/Transforms/Instrumentation/ProfilingUtils.cpp
+++ b/lib/Transforms/Instrumentation/ProfilingUtils.cpp
@@ -51,8 +51,7 @@ void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
Constant::getNullValue(Type::getInt32Ty(Context)));
unsigned NumElements = 0;
if (Array) {
- Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0],
- GEPIndices.size());
+ Args[2] = ConstantExpr::getGetElementPtr(Array, GEPIndices);
NumElements =
cast(Array->getType()->getElementType())->getNumElements();
} else {
@@ -120,7 +119,7 @@ void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Indices[0] = Constant::getNullValue(Type::getInt32Ty(Context));
Indices[1] = ConstantInt::get(Type::getInt32Ty(Context), CounterNum);
Constant *ElementPtr =
- ConstantExpr::getGetElementPtr(CounterArray, &Indices[0], Indices.size());
+ ConstantExpr::getGetElementPtr(CounterArray, Indices);
// Load, increment and store the value back.
Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp
index b4d5667bfa0..81241693558 100644
--- a/lib/Transforms/Scalar/GVN.cpp
+++ b/lib/Transforms/Scalar/GVN.cpp
@@ -920,7 +920,7 @@ static int AnalyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
llvm::Type::getInt8PtrTy(Src->getContext()));
Constant *OffsetCst =
ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
- Src = ConstantExpr::getGetElementPtr(Src, &OffsetCst, 1);
+ Src = ConstantExpr::getGetElementPtr(Src, OffsetCst);
Src = ConstantExpr::getBitCast(Src, PointerType::getUnqual(LoadTy));
if (ConstantFoldLoadFromConstPtr(Src, &TD))
return Offset;
@@ -1081,7 +1081,7 @@ static Value *GetMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
llvm::Type::getInt8PtrTy(Src->getContext()));
Constant *OffsetCst =
ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
- Src = ConstantExpr::getGetElementPtr(Src, &OffsetCst, 1);
+ Src = ConstantExpr::getGetElementPtr(Src, OffsetCst);
Src = ConstantExpr::getBitCast(Src, PointerType::getUnqual(LoadTy));
return ConstantFoldLoadFromConstPtr(Src, &TD);
}
diff --git a/lib/Transforms/Scalar/SCCP.cpp b/lib/Transforms/Scalar/SCCP.cpp
index 9db2555e373..5d439971b44 100644
--- a/lib/Transforms/Scalar/SCCP.cpp
+++ b/lib/Transforms/Scalar/SCCP.cpp
@@ -1180,8 +1180,8 @@ void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
}
Constant *Ptr = Operands[0];
- markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, &Operands[0]+1,
- Operands.size()-1));
+ ArrayRef Indices(Operands.begin() + 1, Operands.end());
+ markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Indices));
}
void SCCPSolver::visitStoreInst(StoreInst &SI) {
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp
index 5b9d2ca3661..2a2faf6590d 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
-static bool isInBoundsIndices(IndexTy const *Idxs, size_t NumIdx) {
+static bool isInBoundsIndices(ArrayRef 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(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(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(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(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(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(C) && isInBoundsIndices(Idxs.data(), Idxs.size()))
- return ConstantExpr::getInBoundsGetElementPtr(C, Idxs.data(), Idxs.size());
+ isa(C) && isInBoundsIndices(Idxs))
+ return ConstantExpr::getInBoundsGetElementPtr(C, Idxs);
return 0;
}
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp
index d790c33d6a6..5147a0d111a 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(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(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 Ops, Type *Ty) const {
return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
case Instruction::GetElementPtr:
return cast(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 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(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 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(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(this)->isInBounds());
} else if (getOpcode() == Instruction::ExtractValue) {
Constant *Agg = getOperand(0);
diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp
index 35ba43a2632..e3b2cb400fc 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 IdxList(unwrap(ConstantIndices, NumIndices),
+ NumIndices);
return wrap(ConstantExpr::getGetElementPtr(unwrap(ConstantVal),
- unwrap(ConstantIndices,
- NumIndices),
- NumIndices));
+ IdxList));
}
LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
LLVMValueRef *ConstantIndices,
unsigned NumIndices) {
Constant* Val = unwrap(ConstantVal);
- Constant** Idxs = unwrap(ConstantIndices, NumIndices);
- return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
+ ArrayRef IdxList(unwrap(ConstantIndices, NumIndices),
+ NumIndices);
+ return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
}
LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
diff --git a/tools/bugpoint/Miscompilation.cpp b/tools/bugpoint/Miscompilation.cpp
index 4da444bca6c..f0ba24014cd 100644
--- a/tools/bugpoint/Miscompilation.cpp
+++ b/tools/bugpoint/Miscompilation.cpp
@@ -834,8 +834,7 @@ static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
// GetElementPtr *funcName, ulong 0, ulong 0
std::vector GEPargs(2,
Constant::getNullValue(Type::getInt32Ty(F->getContext())));
- Value *GEP =
- ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2);
+ Value *GEP = ConstantExpr::getGetElementPtr(funcName, GEPargs);
std::vector ResolverArgs;
ResolverArgs.push_back(GEP);