Convert GetElementPtrInst to use ArrayRef.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@135904 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jay Foad 2011-07-25 09:48:08 +00:00
parent b7fbcc9696
commit a9203109f4
25 changed files with 100 additions and 305 deletions

View File

@ -639,6 +639,9 @@ from the previous release.</p>
<li><code>FindInsertedValue</code> (in <code>llvm/Analysis/ValueTracking.h</code>)</li> <li><code>FindInsertedValue</code> (in <code>llvm/Analysis/ValueTracking.h</code>)</li>
<li><code>gep_type_begin</code> (in <code>llvm/Support/GetElementPtrTypeIterator.h</code>)</li> <li><code>gep_type_begin</code> (in <code>llvm/Support/GetElementPtrTypeIterator.h</code>)</li>
<li><code>gep_type_end</code> (in <code>llvm/Support/GetElementPtrTypeIterator.h</code>)</li> <li><code>gep_type_end</code> (in <code>llvm/Support/GetElementPtrTypeIterator.h</code>)</li>
<li><code>GetElementPtrInst::Create</code></li>
<li><code>GetElementPtrInst::CreateInBounds</code></li>
<li><code>GetElementPtrInst::getIndexedType</code></li>
<li><code>InsertValueInst::Create</code></li> <li><code>InsertValueInst::Create</code></li>
<li><code>InsertValueInst::getIndices</code></li> <li><code>InsertValueInst::getIndices</code></li>
<li><code>InvokeInst::Create</code></li> <li><code>InvokeInst::Create</code></li>

View File

@ -285,149 +285,51 @@ static inline Type *checkGEPType(Type *Ty) {
/// ///
class GetElementPtrInst : public Instruction { class GetElementPtrInst : public Instruction {
GetElementPtrInst(const GetElementPtrInst &GEPI); GetElementPtrInst(const GetElementPtrInst &GEPI);
void init(Value *Ptr, Value* const *Idx, unsigned NumIdx, void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
const Twine &NameStr);
void init(Value *Ptr, Value *Idx, const Twine &NameStr);
template<typename RandomAccessIterator>
void init(Value *Ptr,
RandomAccessIterator IdxBegin,
RandomAccessIterator IdxEnd,
const Twine &NameStr,
// This argument ensures that we have an iterator we can
// do arithmetic on in constant time
std::random_access_iterator_tag) {
unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
if (NumIdx > 0) {
// This requires that the iterator points to contiguous memory.
init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
// we have to build an array here
}
else {
init(Ptr, 0, NumIdx, NameStr);
}
}
/// getIndexedType - Returns the type of the element that would be loaded with
/// a load instruction with the specified parameters.
///
/// Null is returned if the indices are invalid for the specified
/// pointer type.
///
template<typename RandomAccessIterator>
static Type *getIndexedType(Type *Ptr,
RandomAccessIterator IdxBegin,
RandomAccessIterator IdxEnd,
// This argument ensures that we
// have an iterator we can do
// arithmetic on in constant time
std::random_access_iterator_tag) {
unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
if (NumIdx > 0)
// This requires that the iterator points to contiguous memory.
return getIndexedType(Ptr, &*IdxBegin, NumIdx);
else
return getIndexedType(Ptr, (Value *const*)0, NumIdx);
}
/// Constructors - Create a getelementptr instruction with a base pointer an /// Constructors - Create a getelementptr instruction with a base pointer an
/// list of indices. The first ctor can optionally insert before an existing /// list of indices. The first ctor can optionally insert before an existing
/// instruction, the second appends the new instruction to the specified /// instruction, the second appends the new instruction to the specified
/// BasicBlock. /// BasicBlock.
template<typename RandomAccessIterator> inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
inline GetElementPtrInst(Value *Ptr, RandomAccessIterator IdxBegin, unsigned Values, const Twine &NameStr,
RandomAccessIterator IdxEnd,
unsigned Values,
const Twine &NameStr,
Instruction *InsertBefore); Instruction *InsertBefore);
template<typename RandomAccessIterator> inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
inline GetElementPtrInst(Value *Ptr, unsigned Values, const Twine &NameStr,
RandomAccessIterator IdxBegin, BasicBlock *InsertAtEnd);
RandomAccessIterator IdxEnd,
unsigned Values,
const Twine &NameStr, BasicBlock *InsertAtEnd);
/// Constructors - These two constructors are convenience methods because one
/// and two index getelementptr instructions are so common.
GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
Instruction *InsertBefore = 0);
GetElementPtrInst(Value *Ptr, Value *Idx,
const Twine &NameStr, BasicBlock *InsertAtEnd);
protected: protected:
virtual GetElementPtrInst *clone_impl() const; virtual GetElementPtrInst *clone_impl() const;
public: public:
template<typename RandomAccessIterator> static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
static GetElementPtrInst *Create(Value *Ptr, RandomAccessIterator IdxBegin,
RandomAccessIterator IdxEnd,
const Twine &NameStr = "", const Twine &NameStr = "",
Instruction *InsertBefore = 0) { Instruction *InsertBefore = 0) {
typename std::iterator_traits<RandomAccessIterator>::difference_type unsigned Values = 1 + unsigned(IdxList.size());
Values = 1 + std::distance(IdxBegin, IdxEnd);
return new(Values) return new(Values)
GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore); GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
} }
template<typename RandomAccessIterator> static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
static GetElementPtrInst *Create(Value *Ptr,
RandomAccessIterator IdxBegin,
RandomAccessIterator IdxEnd,
const Twine &NameStr, const Twine &NameStr,
BasicBlock *InsertAtEnd) { BasicBlock *InsertAtEnd) {
typename std::iterator_traits<RandomAccessIterator>::difference_type unsigned Values = 1 + unsigned(IdxList.size());
Values = 1 + std::distance(IdxBegin, IdxEnd);
return new(Values) return new(Values)
GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd); GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
}
/// Constructors - These two creators are convenience methods because one
/// index getelementptr instructions are so common.
static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
const Twine &NameStr = "",
Instruction *InsertBefore = 0) {
return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
}
static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
const Twine &NameStr,
BasicBlock *InsertAtEnd) {
return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
} }
/// Create an "inbounds" getelementptr. See the documentation for the /// Create an "inbounds" getelementptr. See the documentation for the
/// "inbounds" flag in LangRef.html for details. /// "inbounds" flag in LangRef.html for details.
template<typename RandomAccessIterator>
static GetElementPtrInst *CreateInBounds(Value *Ptr, static GetElementPtrInst *CreateInBounds(Value *Ptr,
RandomAccessIterator IdxBegin, ArrayRef<Value *> IdxList,
RandomAccessIterator IdxEnd,
const Twine &NameStr = "", const Twine &NameStr = "",
Instruction *InsertBefore = 0) { Instruction *InsertBefore = 0) {
GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd, GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
NameStr, InsertBefore);
GEP->setIsInBounds(true); GEP->setIsInBounds(true);
return GEP; return GEP;
} }
template<typename RandomAccessIterator>
static GetElementPtrInst *CreateInBounds(Value *Ptr, static GetElementPtrInst *CreateInBounds(Value *Ptr,
RandomAccessIterator IdxBegin, ArrayRef<Value *> IdxList,
RandomAccessIterator IdxEnd,
const Twine &NameStr, const Twine &NameStr,
BasicBlock *InsertAtEnd) { BasicBlock *InsertAtEnd) {
GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd, GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
NameStr, InsertAtEnd);
GEP->setIsInBounds(true);
return GEP;
}
static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
const Twine &NameStr = "",
Instruction *InsertBefore = 0) {
GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
GEP->setIsInBounds(true);
return GEP;
}
static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
const Twine &NameStr,
BasicBlock *InsertAtEnd) {
GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
GEP->setIsInBounds(true); GEP->setIsInBounds(true);
return GEP; return GEP;
} }
@ -446,23 +348,9 @@ public:
/// Null is returned if the indices are invalid for the specified /// Null is returned if the indices are invalid for the specified
/// pointer type. /// pointer type.
/// ///
template<typename RandomAccessIterator> static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
static Type *getIndexedType(Type *Ptr, RandomAccessIterator IdxBegin, static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
RandomAccessIterator IdxEnd) { static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
return getIndexedType(Ptr, IdxBegin, IdxEnd,
typename std::iterator_traits<RandomAccessIterator>::
iterator_category());
}
// FIXME: Use ArrayRef
static Type *getIndexedType(Type *Ptr,
Value* const *Idx, unsigned NumIdx);
static Type *getIndexedType(Type *Ptr,
Constant* const *Idx, unsigned NumIdx);
static Type *getIndexedType(Type *Ptr,
uint64_t const *Idx, unsigned NumIdx);
static Type *getIndexedType(Type *Ptr, Value *Idx);
inline op_iterator idx_begin() { return op_begin()+1; } inline op_iterator idx_begin() { return op_begin()+1; }
inline const_op_iterator idx_begin() const { return op_begin()+1; } inline const_op_iterator idx_begin() const { return op_begin()+1; }
@ -530,43 +418,33 @@ struct OperandTraits<GetElementPtrInst> :
public VariadicOperandTraits<GetElementPtrInst, 1> { public VariadicOperandTraits<GetElementPtrInst, 1> {
}; };
template<typename RandomAccessIterator>
GetElementPtrInst::GetElementPtrInst(Value *Ptr, GetElementPtrInst::GetElementPtrInst(Value *Ptr,
RandomAccessIterator IdxBegin, ArrayRef<Value *> IdxList,
RandomAccessIterator IdxEnd,
unsigned Values, unsigned Values,
const Twine &NameStr, const Twine &NameStr,
Instruction *InsertBefore) Instruction *InsertBefore)
: Instruction(PointerType::get(checkGEPType( : Instruction(PointerType::get(checkGEPType(
getIndexedType(Ptr->getType(), getIndexedType(Ptr->getType(), IdxList)),
IdxBegin, IdxEnd)),
cast<PointerType>(Ptr->getType()) cast<PointerType>(Ptr->getType())
->getAddressSpace()), ->getAddressSpace()),
GetElementPtr, GetElementPtr,
OperandTraits<GetElementPtrInst>::op_end(this) - Values, OperandTraits<GetElementPtrInst>::op_end(this) - Values,
Values, InsertBefore) { Values, InsertBefore) {
init(Ptr, IdxBegin, IdxEnd, NameStr, init(Ptr, IdxList, NameStr);
typename std::iterator_traits<RandomAccessIterator>
::iterator_category());
} }
template<typename RandomAccessIterator>
GetElementPtrInst::GetElementPtrInst(Value *Ptr, GetElementPtrInst::GetElementPtrInst(Value *Ptr,
RandomAccessIterator IdxBegin, ArrayRef<Value *> IdxList,
RandomAccessIterator IdxEnd,
unsigned Values, unsigned Values,
const Twine &NameStr, const Twine &NameStr,
BasicBlock *InsertAtEnd) BasicBlock *InsertAtEnd)
: Instruction(PointerType::get(checkGEPType( : Instruction(PointerType::get(checkGEPType(
getIndexedType(Ptr->getType(), getIndexedType(Ptr->getType(), IdxList)),
IdxBegin, IdxEnd)),
cast<PointerType>(Ptr->getType()) cast<PointerType>(Ptr->getType())
->getAddressSpace()), ->getAddressSpace()),
GetElementPtr, GetElementPtr,
OperandTraits<GetElementPtrInst>::op_end(this) - Values, OperandTraits<GetElementPtrInst>::op_end(this) - Values,
Values, InsertAtEnd) { Values, InsertAtEnd) {
init(Ptr, IdxBegin, IdxEnd, NameStr, init(Ptr, IdxList, NameStr);
typename std::iterator_traits<RandomAccessIterator>
::iterator_category());
} }

View File

@ -773,9 +773,7 @@ public:
if (i == e) if (i == e)
return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name); return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
} }
return Insert(GetElementPtrInst::Create(Ptr, IdxList.begin(), return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
IdxList.end()),
Name);
} }
Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList, Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
const Twine &Name = "") { const Twine &Name = "") {
@ -788,9 +786,7 @@ public:
if (i == e) if (i == e)
return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name); return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
} }
return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList.begin(), return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
IdxList.end()),
Name);
} }
Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") { Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
if (Constant *PC = dyn_cast<Constant>(Ptr)) if (Constant *PC = dyn_cast<Constant>(Ptr))
@ -831,7 +827,7 @@ public:
if (Constant *PC = dyn_cast<Constant>(Ptr)) if (Constant *PC = dyn_cast<Constant>(Ptr))
return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name); return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name); return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
} }
Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
const Twine &Name = "") { const Twine &Name = "") {
@ -843,7 +839,7 @@ public:
if (Constant *PC = dyn_cast<Constant>(Ptr)) if (Constant *PC = dyn_cast<Constant>(Ptr))
return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name); return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name); return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
} }
Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") { Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
@ -872,7 +868,7 @@ public:
if (Constant *PC = dyn_cast<Constant>(Ptr)) if (Constant *PC = dyn_cast<Constant>(Ptr))
return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name); return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name); return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
} }
Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
const Twine &Name = "") { const Twine &Name = "") {
@ -884,7 +880,7 @@ public:
if (Constant *PC = dyn_cast<Constant>(Ptr)) if (Constant *PC = dyn_cast<Constant>(Ptr))
return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name); return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name); return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
} }
Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") { Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name); return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);

View File

@ -183,7 +183,7 @@ public:
} }
Instruction *CreateGetElementPtr(Constant *C, Instruction *CreateGetElementPtr(Constant *C,
ArrayRef<Value *> IdxList) const { ArrayRef<Value *> IdxList) const {
return GetElementPtrInst::Create(C, IdxList.begin(), IdxList.end()); return GetElementPtrInst::Create(C, IdxList);
} }
Constant *CreateInBoundsGetElementPtr(Constant *C, Constant *CreateInBoundsGetElementPtr(Constant *C,
@ -192,7 +192,7 @@ public:
} }
Instruction *CreateInBoundsGetElementPtr(Constant *C, Instruction *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef<Value *> IdxList) const { ArrayRef<Value *> IdxList) const {
return GetElementPtrInst::CreateInBounds(C, IdxList.begin(), IdxList.end()); return GetElementPtrInst::CreateInBounds(C, IdxList);
} }
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//

View File

@ -547,8 +547,7 @@ static Constant *CastGEPIndices(ArrayRef<Constant *> Ops,
for (unsigned i = 1, e = Ops.size(); i != e; ++i) { for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
if ((i == 1 || if ((i == 1 ||
!isa<StructType>(GetElementPtrInst::getIndexedType(Ops[0]->getType(), !isa<StructType>(GetElementPtrInst::getIndexedType(Ops[0]->getType(),
Ops.data() + 1, Ops.slice(1, i-1)))) &&
i-1))) &&
Ops[i]->getType() != IntPtrTy) { Ops[i]->getType() != IntPtrTy) {
Any = true; Any = true;
NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i], NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i],

View File

@ -2230,8 +2230,7 @@ Value *llvm::SimplifyGEPInst(ArrayRef<Value *> Ops,
if (isa<UndefValue>(Ops[0])) { if (isa<UndefValue>(Ops[0])) {
// Compute the (pointer) type returned by the GEP instruction. // Compute the (pointer) type returned by the GEP instruction.
Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, Ops.data() + 1, Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, Ops.slice(1));
Ops.size() - 1);
Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace()); Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace());
return UndefValue::get(GEPTy); return UndefValue::get(GEPTy);
} }

View File

@ -407,9 +407,10 @@ InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
} }
GetElementPtrInst *Result = GetElementPtrInst *Result =
GetElementPtrInst::Create(GEPOps[0], GEPOps.begin()+1, GEPOps.end(), GetElementPtrInst::Create(GEPOps[0],
InVal->getName()+".phi.trans.insert", makeArrayRef(GEPOps.begin() + 1, GEPOps.end()),
PredBB->getTerminator()); InVal->getName()+".phi.trans.insert",
PredBB->getTerminator());
Result->setIsInBounds(GEP->isInBounds()); Result->setIsInBounds(GEP->isInBounds());
NewInsts.push_back(Result); NewInsts.push_back(Result);
return Result; return Result;

View File

@ -2274,9 +2274,7 @@ bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
return Error(ID.Loc, "getelementptr requires pointer operand"); return Error(ID.Loc, "getelementptr requires pointer operand");
ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
(Value**)(Elts.data() + 1),
Elts.size() - 1))
return Error(ID.Loc, "invalid indices for getelementptr"); return Error(ID.Loc, "invalid indices for getelementptr");
ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices, ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
InBounds); InBounds);
@ -3660,10 +3658,9 @@ int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Indices.push_back(Val); Indices.push_back(Val);
} }
if (!GetElementPtrInst::getIndexedType(Ptr->getType(), if (!GetElementPtrInst::getIndexedType(Ptr->getType(), Indices))
Indices.begin(), Indices.end()))
return Error(Loc, "invalid getelementptr indices"); return Error(Loc, "invalid getelementptr indices");
Inst = GetElementPtrInst::Create(Ptr, Indices.begin(), Indices.end()); Inst = GetElementPtrInst::Create(Ptr, Indices);
if (InBounds) if (InBounds)
cast<GetElementPtrInst>(Inst)->setIsInBounds(true); cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
return AteExtraComma ? InstExtraComma : InstNormal; return AteExtraComma ? InstExtraComma : InstNormal;

View File

@ -2181,7 +2181,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
GEPIdx.push_back(Op); GEPIdx.push_back(Op);
} }
I = GetElementPtrInst::Create(BasePtr, GEPIdx.begin(), GEPIdx.end()); I = GetElementPtrInst::Create(BasePtr, GEPIdx);
InstructionList.push_back(I); InstructionList.push_back(I);
if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP) if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
cast<GetElementPtrInst>(I)->setIsInBounds(true); cast<GetElementPtrInst>(I)->setIsInBounds(true);

View File

@ -386,22 +386,20 @@ bool SjLjEHPass::insertSjLjEHSupport(Function &F) {
// We need to also keep around a reference to the call_site field // We need to also keep around a reference to the call_site field
Idxs[0] = Zero; Idxs[0] = Zero;
Idxs[1] = ConstantInt::get(Int32Ty, 1); Idxs[1] = ConstantInt::get(Int32Ty, 1);
CallSite = GetElementPtrInst::Create(FunctionContext, Idxs, Idxs+2, CallSite = GetElementPtrInst::Create(FunctionContext, Idxs, "call_site",
"call_site",
EntryBB->getTerminator()); EntryBB->getTerminator());
// The exception selector comes back in context->data[1] // The exception selector comes back in context->data[1]
Idxs[1] = ConstantInt::get(Int32Ty, 2); Idxs[1] = ConstantInt::get(Int32Ty, 2);
Value *FCData = GetElementPtrInst::Create(FunctionContext, Idxs, Idxs+2, Value *FCData = GetElementPtrInst::Create(FunctionContext, Idxs, "fc_data",
"fc_data",
EntryBB->getTerminator()); EntryBB->getTerminator());
Idxs[1] = ConstantInt::get(Int32Ty, 1); Idxs[1] = ConstantInt::get(Int32Ty, 1);
Value *SelectorAddr = GetElementPtrInst::Create(FCData, Idxs, Idxs+2, Value *SelectorAddr = GetElementPtrInst::Create(FCData, Idxs,
"exc_selector_gep", "exc_selector_gep",
EntryBB->getTerminator()); EntryBB->getTerminator());
// The exception value comes back in context->data[0] // The exception value comes back in context->data[0]
Idxs[1] = Zero; Idxs[1] = Zero;
Value *ExceptionAddr = GetElementPtrInst::Create(FCData, Idxs, Idxs+2, Value *ExceptionAddr = GetElementPtrInst::Create(FCData, Idxs,
"exception_gep", "exception_gep",
EntryBB->getTerminator()); EntryBB->getTerminator());
@ -466,8 +464,7 @@ bool SjLjEHPass::insertSjLjEHSupport(Function &F) {
Idxs[0] = Zero; Idxs[0] = Zero;
Idxs[1] = ConstantInt::get(Int32Ty, 4); Idxs[1] = ConstantInt::get(Int32Ty, 4);
Value *LSDAFieldPtr = Value *LSDAFieldPtr =
GetElementPtrInst::Create(FunctionContext, Idxs, Idxs+2, GetElementPtrInst::Create(FunctionContext, Idxs, "lsda_gep",
"lsda_gep",
EntryBB->getTerminator()); EntryBB->getTerminator());
Value *LSDA = CallInst::Create(LSDAAddrFn, "lsda_addr", Value *LSDA = CallInst::Create(LSDAAddrFn, "lsda_addr",
EntryBB->getTerminator()); EntryBB->getTerminator());
@ -475,8 +472,7 @@ bool SjLjEHPass::insertSjLjEHSupport(Function &F) {
Idxs[1] = ConstantInt::get(Int32Ty, 3); Idxs[1] = ConstantInt::get(Int32Ty, 3);
Value *PersonalityFieldPtr = Value *PersonalityFieldPtr =
GetElementPtrInst::Create(FunctionContext, Idxs, Idxs+2, GetElementPtrInst::Create(FunctionContext, Idxs, "lsda_gep",
"lsda_gep",
EntryBB->getTerminator()); EntryBB->getTerminator());
new StoreInst(PersonalityFn, PersonalityFieldPtr, true, new StoreInst(PersonalityFn, PersonalityFieldPtr, true,
EntryBB->getTerminator()); EntryBB->getTerminator());
@ -484,12 +480,11 @@ bool SjLjEHPass::insertSjLjEHSupport(Function &F) {
// Save the frame pointer. // Save the frame pointer.
Idxs[1] = ConstantInt::get(Int32Ty, 5); Idxs[1] = ConstantInt::get(Int32Ty, 5);
Value *JBufPtr Value *JBufPtr
= GetElementPtrInst::Create(FunctionContext, Idxs, Idxs+2, = GetElementPtrInst::Create(FunctionContext, Idxs, "jbuf_gep",
"jbuf_gep",
EntryBB->getTerminator()); EntryBB->getTerminator());
Idxs[1] = ConstantInt::get(Int32Ty, 0); Idxs[1] = ConstantInt::get(Int32Ty, 0);
Value *FramePtr = Value *FramePtr =
GetElementPtrInst::Create(JBufPtr, Idxs, Idxs+2, "jbuf_fp_gep", GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_fp_gep",
EntryBB->getTerminator()); EntryBB->getTerminator());
Value *Val = CallInst::Create(FrameAddrFn, Value *Val = CallInst::Create(FrameAddrFn,
@ -501,7 +496,7 @@ bool SjLjEHPass::insertSjLjEHSupport(Function &F) {
// Save the stack pointer. // Save the stack pointer.
Idxs[1] = ConstantInt::get(Int32Ty, 2); Idxs[1] = ConstantInt::get(Int32Ty, 2);
Value *StackPtr = Value *StackPtr =
GetElementPtrInst::Create(JBufPtr, Idxs, Idxs+2, "jbuf_sp_gep", GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_sp_gep",
EntryBB->getTerminator()); EntryBB->getTerminator());
Val = CallInst::Create(StackAddrFn, "sp", EntryBB->getTerminator()); Val = CallInst::Create(StackAddrFn, "sp", EntryBB->getTerminator());

View File

@ -576,9 +576,7 @@ CallGraphNode *ArgPromotion::DoPromotion(Function *F,
for (ScalarizeTable::iterator SI = ArgIndices.begin(), for (ScalarizeTable::iterator SI = ArgIndices.begin(),
E = ArgIndices.end(); SI != E; ++SI) { E = ArgIndices.end(); SI != E; ++SI) {
// not allowed to dereference ->begin() if size() is 0 // not allowed to dereference ->begin() if size() is 0
Params.push_back(GetElementPtrInst::getIndexedType(I->getType(), Params.push_back(GetElementPtrInst::getIndexedType(I->getType(), *SI));
SI->begin(),
SI->end()));
assert(Params.back()); assert(Params.back());
} }
@ -668,7 +666,7 @@ CallGraphNode *ArgPromotion::DoPromotion(Function *F,
ConstantInt::get(Type::getInt32Ty(F->getContext()), 0), 0 }; ConstantInt::get(Type::getInt32Ty(F->getContext()), 0), 0 };
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
Idxs[1] = ConstantInt::get(Type::getInt32Ty(F->getContext()), i); Idxs[1] = ConstantInt::get(Type::getInt32Ty(F->getContext()), i);
Value *Idx = GetElementPtrInst::Create(*AI, Idxs, Idxs+2, Value *Idx = GetElementPtrInst::Create(*AI, Idxs,
(*AI)->getName()+"."+utostr(i), (*AI)->getName()+"."+utostr(i),
Call); Call);
// TODO: Tell AA about the new values? // TODO: Tell AA about the new values?
@ -699,8 +697,7 @@ CallGraphNode *ArgPromotion::DoPromotion(Function *F,
ElTy = cast<CompositeType>(ElTy)->getTypeAtIndex(*II); ElTy = cast<CompositeType>(ElTy)->getTypeAtIndex(*II);
} }
// And create a GEP to extract those indices. // And create a GEP to extract those indices.
V = GetElementPtrInst::Create(V, Ops.begin(), Ops.end(), V = GetElementPtrInst::Create(V, Ops, V->getName()+".idx", Call);
V->getName()+".idx", Call);
Ops.clear(); Ops.clear();
AA.copyValue(OrigLoad->getOperand(0), V); AA.copyValue(OrigLoad->getOperand(0), V);
} }
@ -801,7 +798,7 @@ CallGraphNode *ArgPromotion::DoPromotion(Function *F,
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
Idxs[1] = ConstantInt::get(Type::getInt32Ty(F->getContext()), i); Idxs[1] = ConstantInt::get(Type::getInt32Ty(F->getContext()), i);
Value *Idx = Value *Idx =
GetElementPtrInst::Create(TheAlloca, Idxs, Idxs+2, GetElementPtrInst::Create(TheAlloca, Idxs,
TheAlloca->getName()+"."+Twine(i), TheAlloca->getName()+"."+Twine(i),
InsertPt); InsertPt);
I2->setName(I->getName()+"."+Twine(i)); I2->setName(I->getName()+"."+Twine(i));

View File

@ -603,7 +603,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD) {
Idxs.push_back(NullInt); Idxs.push_back(NullInt);
for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i) for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i)
Idxs.push_back(GEPI->getOperand(i)); Idxs.push_back(GEPI->getOperand(i));
NewPtr = GetElementPtrInst::Create(NewPtr, Idxs.begin(), Idxs.end(), NewPtr = GetElementPtrInst::Create(NewPtr, Idxs,
GEPI->getName()+"."+Twine(Val),GEPI); GEPI->getName()+"."+Twine(Val),GEPI);
} }
} }
@ -1243,8 +1243,7 @@ static void RewriteHeapSROALoadUser(Instruction *LoadUser,
GEPIdx.push_back(GEPI->getOperand(1)); GEPIdx.push_back(GEPI->getOperand(1));
GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end()); GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end());
Value *NGEPI = GetElementPtrInst::Create(NewPtr, Value *NGEPI = GetElementPtrInst::Create(NewPtr, GEPIdx,
GEPIdx.begin(), GEPIdx.end(),
GEPI->getName(), GEPI); GEPI->getName(), GEPI);
GEPI->replaceAllUsesWith(NGEPI); GEPI->replaceAllUsesWith(NGEPI);
GEPI->eraseFromParent(); GEPI->eraseFromParent();

View File

@ -1693,7 +1693,7 @@ Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
// If we found a path from the src to dest, create the getelementptr now. // If we found a path from the src to dest, create the getelementptr now.
if (SrcElTy == DstElTy) { if (SrcElTy == DstElTy) {
SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt);
return GetElementPtrInst::CreateInBounds(Src, Idxs.begin(), Idxs.end()); return GetElementPtrInst::CreateInBounds(Src, Idxs);
} }
} }

View File

@ -58,8 +58,7 @@ Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) {
Idx[0] = NullIdx; Idx[0] = NullIdx;
Idx[1] = NullIdx; Idx[1] = NullIdx;
Instruction *GEP = Instruction *GEP =
GetElementPtrInst::CreateInBounds(New, Idx, Idx + 2, GetElementPtrInst::CreateInBounds(New, Idx, New->getName()+".sub");
New->getName()+".sub");
InsertNewInstBefore(GEP, *It); InsertNewInstBefore(GEP, *It);
// Now make everything use the getelementptr instead of the original // Now make everything use the getelementptr instead of the original

View File

@ -229,8 +229,8 @@ Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) {
Value *Base = FixedOperands[0]; Value *Base = FixedOperands[0];
GetElementPtrInst *NewGEP = GetElementPtrInst *NewGEP =
GetElementPtrInst::Create(Base, FixedOperands.begin()+1, GetElementPtrInst::Create(Base, makeArrayRef(FixedOperands.begin() + 1,
FixedOperands.end()); FixedOperands.end()));
if (AllInBounds) NewGEP->setIsInBounds(); if (AllInBounds) NewGEP->setIsInBounds();
NewGEP->setDebugLoc(FirstInst->getDebugLoc()); NewGEP->setDebugLoc(FirstInst->getDebugLoc());
return NewGEP; return NewGEP;

View File

@ -851,10 +851,9 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
if (!Indices.empty()) if (!Indices.empty())
return (GEP.isInBounds() && Src->isInBounds()) ? return (GEP.isInBounds() && Src->isInBounds()) ?
GetElementPtrInst::CreateInBounds(Src->getOperand(0), Indices.begin(), GetElementPtrInst::CreateInBounds(Src->getOperand(0), Indices,
Indices.end(), GEP.getName()) : GEP.getName()) :
GetElementPtrInst::Create(Src->getOperand(0), Indices.begin(), GetElementPtrInst::Create(Src->getOperand(0), Indices, GEP.getName());
Indices.end(), GEP.getName());
} }
// Handle gep(bitcast x) and gep(gep x, 0, 0, 0). // Handle gep(bitcast x) and gep(gep x, 0, 0, 0).
@ -883,8 +882,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
// -> GEP i8* X, ... // -> GEP i8* X, ...
SmallVector<Value*, 8> Idx(GEP.idx_begin()+1, GEP.idx_end()); SmallVector<Value*, 8> Idx(GEP.idx_begin()+1, GEP.idx_end());
GetElementPtrInst *Res = GetElementPtrInst *Res =
GetElementPtrInst::Create(StrippedPtr, Idx.begin(), GetElementPtrInst::Create(StrippedPtr, Idx, GEP.getName());
Idx.end(), GEP.getName());
Res->setIsInBounds(GEP.isInBounds()); Res->setIsInBounds(GEP.isInBounds());
return Res; return Res;
} }

View File

@ -1029,8 +1029,7 @@ void PathProfiler::insertCounterIncrement(Value* incValue,
gepIndices[1] = incValue; gepIndices[1] = incValue;
GetElementPtrInst* pcPointer = GetElementPtrInst* pcPointer =
GetElementPtrInst::Create(dag->getCounterArray(), GetElementPtrInst::Create(dag->getCounterArray(), gepIndices,
gepIndices.begin(), gepIndices.end(),
"counterInc", insertPoint); "counterInc", insertPoint);
// Load from the array - call it oldPC // Load from the array - call it oldPC

View File

@ -2086,8 +2086,7 @@ void SROA::RewriteGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t Offset,
} }
Instruction *Val = NewElts[Idx]; Instruction *Val = NewElts[Idx];
if (NewArgs.size() > 1) { if (NewArgs.size() > 1) {
Val = GetElementPtrInst::CreateInBounds(Val, NewArgs.begin(), Val = GetElementPtrInst::CreateInBounds(Val, NewArgs, "", GEPI);
NewArgs.end(), "", GEPI);
Val->takeName(GEPI); Val->takeName(GEPI);
} }
if (Val->getType() != GEPI->getType()) if (Val->getType() != GEPI->getType())
@ -2163,7 +2162,7 @@ void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *Inst,
if (OtherPtr) { if (OtherPtr) {
Value *Idx[2] = { Zero, Value *Idx[2] = { Zero,
ConstantInt::get(Type::getInt32Ty(MI->getContext()), i) }; ConstantInt::get(Type::getInt32Ty(MI->getContext()), i) };
OtherElt = GetElementPtrInst::CreateInBounds(OtherPtr, Idx, Idx + 2, OtherElt = GetElementPtrInst::CreateInBounds(OtherPtr, Idx,
OtherPtr->getName()+"."+Twine(i), OtherPtr->getName()+"."+Twine(i),
MI); MI);
uint64_t EltOffset; uint64_t EltOffset;

View File

@ -317,8 +317,7 @@ Function *CodeExtractor::constructFunction(const Values &inputs,
Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), i); Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), i);
TerminatorInst *TI = newFunction->begin()->getTerminator(); TerminatorInst *TI = newFunction->begin()->getTerminator();
GetElementPtrInst *GEP = GetElementPtrInst *GEP =
GetElementPtrInst::Create(AI, Idx, Idx+2, GetElementPtrInst::Create(AI, Idx, "gep_" + inputs[i]->getName(), TI);
"gep_" + inputs[i]->getName(), TI);
RewriteVal = new LoadInst(GEP, "loadgep_" + inputs[i]->getName(), TI); RewriteVal = new LoadInst(GEP, "loadgep_" + inputs[i]->getName(), TI);
} else } else
RewriteVal = AI++; RewriteVal = AI++;
@ -420,7 +419,7 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context)); Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), i); Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), i);
GetElementPtrInst *GEP = GetElementPtrInst *GEP =
GetElementPtrInst::Create(Struct, Idx, Idx + 2, GetElementPtrInst::Create(Struct, Idx,
"gep_" + StructValues[i]->getName()); "gep_" + StructValues[i]->getName());
codeReplacer->getInstList().push_back(GEP); codeReplacer->getInstList().push_back(GEP);
StoreInst *SI = new StoreInst(StructValues[i], GEP); StoreInst *SI = new StoreInst(StructValues[i], GEP);
@ -446,7 +445,7 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context)); Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), FirstOut + i); Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), FirstOut + i);
GetElementPtrInst *GEP GetElementPtrInst *GEP
= GetElementPtrInst::Create(Struct, Idx, Idx + 2, = GetElementPtrInst::Create(Struct, Idx,
"gep_reload_" + outputs[i]->getName()); "gep_reload_" + outputs[i]->getName());
codeReplacer->getInstList().push_back(GEP); codeReplacer->getInstList().push_back(GEP);
Output = GEP; Output = GEP;
@ -561,7 +560,7 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), Idx[1] = ConstantInt::get(Type::getInt32Ty(Context),
FirstOut+out); FirstOut+out);
GetElementPtrInst *GEP = GetElementPtrInst *GEP =
GetElementPtrInst::Create(OAI, Idx, Idx + 2, GetElementPtrInst::Create(OAI, Idx,
"gep_" + outputs[out]->getName(), "gep_" + outputs[out]->getName(),
NTRet); NTRet);
new StoreInst(outputs[out], GEP, NTRet); new StoreInst(outputs[out], GEP, NTRet);

View File

@ -455,8 +455,7 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
Value *Idx[] = { Constant::getNullValue(Type::getInt32Ty(F.getContext())), Value *Idx[] = { Constant::getNullValue(Type::getInt32Ty(F.getContext())),
ConstantInt::get(Type::getInt32Ty(F.getContext()), 1) }; ConstantInt::get(Type::getInt32Ty(F.getContext()), 1) };
OldJmpBufPtr = GetElementPtrInst::Create(JmpBuf, &Idx[0], &Idx[2], OldJmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx, "OldBuf",
"OldBuf",
EntryBB->getTerminator()); EntryBB->getTerminator());
// Copy the JBListHead to the alloca. // Copy the JBListHead to the alloca.
@ -502,8 +501,7 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
"setjmp.cont"); "setjmp.cont");
Idx[1] = ConstantInt::get(Type::getInt32Ty(F.getContext()), 0); Idx[1] = ConstantInt::get(Type::getInt32Ty(F.getContext()), 0);
Value *JmpBufPtr = GetElementPtrInst::Create(JmpBuf, &Idx[0], &Idx[2], Value *JmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx, "TheJmpBuf",
"TheJmpBuf",
EntryBB->getTerminator()); EntryBB->getTerminator());
JmpBufPtr = new BitCastInst(JmpBufPtr, JmpBufPtr = new BitCastInst(JmpBufPtr,
Type::getInt8PtrTy(F.getContext()), Type::getInt8PtrTy(F.getContext()),
@ -557,8 +555,7 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
// Get a pointer to the jmpbuf and longjmp. // Get a pointer to the jmpbuf and longjmp.
Value *Idx[] = { Constant::getNullValue(Type::getInt32Ty(F.getContext())), Value *Idx[] = { Constant::getNullValue(Type::getInt32Ty(F.getContext())),
ConstantInt::get(Type::getInt32Ty(F.getContext()), 0) }; ConstantInt::get(Type::getInt32Ty(F.getContext()), 0) };
Idx[0] = GetElementPtrInst::Create(BufPtr, &Idx[0], &Idx[2], "JmpBuf", Idx[0] = GetElementPtrInst::Create(BufPtr, Idx, "JmpBuf", UnwindBlock);
UnwindBlock);
Idx[0] = new BitCastInst(Idx[0], Idx[0] = new BitCastInst(Idx[0],
Type::getInt8PtrTy(F.getContext()), Type::getInt8PtrTy(F.getContext()),
"tmp", UnwindBlock); "tmp", UnwindBlock);

View File

@ -2173,7 +2173,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C,
if (isa<UndefValue>(C)) { if (isa<UndefValue>(C)) {
PointerType *Ptr = cast<PointerType>(C->getType()); PointerType *Ptr = cast<PointerType>(C->getType());
Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs.begin(), Idxs.end()); Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs);
assert(Ty != 0 && "Invalid indices for GEP!"); assert(Ty != 0 && "Invalid indices for GEP!");
return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace())); return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
} }
@ -2187,8 +2187,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C,
} }
if (isNull) { if (isNull) {
PointerType *Ptr = cast<PointerType>(C->getType()); PointerType *Ptr = cast<PointerType>(C->getType());
Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs.begin(), Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs);
Idxs.end());
assert(Ty != 0 && "Invalid indices for GEP!"); assert(Ty != 0 && "Invalid indices for GEP!");
return ConstantPointerNull::get(PointerType::get(Ty, return ConstantPointerNull::get(PointerType::get(Ty,
Ptr->getAddressSpace())); Ptr->getAddressSpace()));

View File

@ -1598,8 +1598,7 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, ArrayRef<Value *> Idxs,
return FC; // Fold a few common cases. return FC; // Fold a few common cases.
// Get the result type of the getelementptr! // Get the result type of the getelementptr!
Type *Ty = Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), Idxs);
GetElementPtrInst::getIndexedType(C->getType(), Idxs.begin(), Idxs.end());
assert(Ty && "GEP indices invalid!"); assert(Ty && "GEP indices invalid!");
unsigned AS = cast<PointerType>(C->getType())->getAddressSpace(); unsigned AS = cast<PointerType>(C->getType())->getAddressSpace();
Type *ReqTy = Ty->getPointerTo(AS); Type *ReqTy = Ty->getPointerTo(AS);

View File

@ -999,28 +999,11 @@ void StoreInst::setAlignment(unsigned Align) {
// GetElementPtrInst Implementation // GetElementPtrInst Implementation
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
static unsigned retrieveAddrSpace(const Value *Val) { void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
return cast<PointerType>(Val->getType())->getAddressSpace();
}
void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
const Twine &Name) { const Twine &Name) {
assert(NumOperands == 1+NumIdx && "NumOperands not initialized?"); assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
Use *OL = OperandList; OperandList[0] = Ptr;
OL[0] = Ptr; std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
for (unsigned i = 0; i != NumIdx; ++i)
OL[i+1] = Idx[i];
setName(Name);
}
void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
assert(NumOperands == 2 && "NumOperands not initialized?");
Use *OL = OperandList;
OL[0] = Ptr;
OL[1] = Idx;
setName(Name); setName(Name);
} }
@ -1029,34 +1012,10 @@ GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
OperandTraits<GetElementPtrInst>::op_end(this) OperandTraits<GetElementPtrInst>::op_end(this)
- GEPI.getNumOperands(), - GEPI.getNumOperands(),
GEPI.getNumOperands()) { GEPI.getNumOperands()) {
Use *OL = OperandList; std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Use *GEPIOL = GEPI.OperandList;
for (unsigned i = 0, E = NumOperands; i != E; ++i)
OL[i] = GEPIOL[i];
SubclassOptionalData = GEPI.SubclassOptionalData; SubclassOptionalData = GEPI.SubclassOptionalData;
} }
GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
const Twine &Name, Instruction *InBe)
: Instruction(PointerType::get(
checkGEPType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
GetElementPtr,
OperandTraits<GetElementPtrInst>::op_end(this) - 2,
2, InBe) {
init(Ptr, Idx, Name);
}
GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
const Twine &Name, BasicBlock *IAE)
: Instruction(PointerType::get(
checkGEPType(getIndexedType(Ptr->getType(),Idx)),
retrieveAddrSpace(Ptr)),
GetElementPtr,
OperandTraits<GetElementPtrInst>::op_end(this) - 2,
2, IAE) {
init(Ptr, Idx, Name);
}
/// getIndexedType - Returns the type of the element that would be accessed with /// getIndexedType - Returns the type of the element that would be accessed with
/// a gep instruction with the specified parameters. /// a gep instruction with the specified parameters.
/// ///
@ -1067,14 +1026,13 @@ GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
/// pointer type. /// pointer type.
/// ///
template <typename IndexTy> template <typename IndexTy>
static Type *getIndexedTypeInternal(Type *Ptr, IndexTy const *Idxs, static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
unsigned NumIdx) {
PointerType *PTy = dyn_cast<PointerType>(Ptr); PointerType *PTy = dyn_cast<PointerType>(Ptr);
if (!PTy) return 0; // Type isn't a pointer type! if (!PTy) return 0; // Type isn't a pointer type!
Type *Agg = PTy->getElementType(); Type *Agg = PTy->getElementType();
// Handle the special case of the empty set index set, which is always valid. // Handle the special case of the empty set index set, which is always valid.
if (NumIdx == 0) if (IdxList.empty())
return Agg; return Agg;
// If there is at least one index, the top level type must be sized, otherwise // If there is at least one index, the top level type must be sized, otherwise
@ -1083,44 +1041,29 @@ static Type *getIndexedTypeInternal(Type *Ptr, IndexTy const *Idxs,
return 0; return 0;
unsigned CurIdx = 1; unsigned CurIdx = 1;
for (; CurIdx != NumIdx; ++CurIdx) { for (; CurIdx != IdxList.size(); ++CurIdx) {
CompositeType *CT = dyn_cast<CompositeType>(Agg); CompositeType *CT = dyn_cast<CompositeType>(Agg);
if (!CT || CT->isPointerTy()) return 0; if (!CT || CT->isPointerTy()) return 0;
IndexTy Index = Idxs[CurIdx]; IndexTy Index = IdxList[CurIdx];
if (!CT->indexValid(Index)) return 0; if (!CT->indexValid(Index)) return 0;
Agg = CT->getTypeAtIndex(Index); Agg = CT->getTypeAtIndex(Index);
} }
return CurIdx == NumIdx ? Agg : 0; return CurIdx == IdxList.size() ? Agg : 0;
} }
Type *GetElementPtrInst::getIndexedType(Type *Ptr, Value* const *Idxs, Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
unsigned NumIdx) { return getIndexedTypeInternal(Ptr, IdxList);
return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
} }
Type *GetElementPtrInst::getIndexedType(Type *Ptr, Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Constant* const *Idxs, ArrayRef<Constant *> IdxList) {
unsigned NumIdx) { return getIndexedTypeInternal(Ptr, IdxList);
return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
} }
Type *GetElementPtrInst::getIndexedType(Type *Ptr, Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
uint64_t const *Idxs, return getIndexedTypeInternal(Ptr, IdxList);
unsigned NumIdx) {
return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
} }
Type *GetElementPtrInst::getIndexedType(Type *Ptr, Value *Idx) {
PointerType *PTy = dyn_cast<PointerType>(Ptr);
if (!PTy) return 0; // Type isn't a pointer type!
// Check the pointer index.
if (!PTy->indexValid(Idx)) return 0;
return PTy->getElementType();
}
/// hasAllZeroIndices - Return true if all of the indices of this GEP are /// hasAllZeroIndices - Return true if all of the indices of this GEP are
/// zeros. If so, the result pointer and the first operand have the same /// zeros. If so, the result pointer and the first operand have the same
/// value, just potentially different types. /// value, just potentially different types.

View File

@ -1276,8 +1276,7 @@ void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) { void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end()); SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end());
Type *ElTy = Type *ElTy =
GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(), GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(), Idxs);
Idxs.begin(), Idxs.end());
Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP); Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
Assert2(GEP.getType()->isPointerTy() && Assert2(GEP.getType()->isPointerTy() &&
cast<PointerType>(GEP.getType())->getElementType() == ElTy, cast<PointerType>(GEP.getType())->getElementType() == ElTy,

View File

@ -124,7 +124,7 @@ TEST_F(CloneInstruction, Inbounds) {
Constant *Z = Constant::getNullValue(Type::getInt32Ty(context)); Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
std::vector<Value *> ops; std::vector<Value *> ops;
ops.push_back(Z); ops.push_back(Z);
GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops.begin(), ops.end()); GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops);
EXPECT_FALSE(this->clone(GEP)->isInBounds()); EXPECT_FALSE(this->clone(GEP)->isInBounds());
GEP->setIsInBounds(); GEP->setIsInBounds();