Skip to content

Commit

Permalink
Convert ConstantExpr::getGetElementPtr and
Browse files Browse the repository at this point in the history
ConstantExpr::getInBoundsGetElementPtr to use ArrayRef.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@135673 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
jayfoad committed Jul 21, 2011
1 parent 14732a1 commit dab3d29
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 106 deletions.
2 changes: 2 additions & 0 deletions docs/ReleaseNotes.html
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,8 @@ <h3>
<li><code>ComputeLinearIndex</code> (in <code>llvm/CodeGen/Analysis.h</code>)</li>
<li><code>ConstantArray::get</code></li>
<li><code>ConstantExpr::getExtractElement</code></li>
<li><code>ConstantExpr::getGetElementPtr</code></li>
<li><code>ConstantExpr::getInBoundsGetElementPtr</code></li>
<li><code>ConstantExpr::getIndices</code></li>
<li><code>ConstantExpr::getInsertElement</code></li>
<li><code>ConstantExpr::getWithOperands</code></li>
Expand Down
3 changes: 1 addition & 2 deletions examples/BrainF/BrainF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 24 additions & 9 deletions include/llvm/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -788,25 +788,40 @@ class ConstantExpr : public Constant {
/// all elements must be Constant's.
///
static Constant *getGetElementPtr(Constant *C,
Constant *const *IdxList, unsigned NumIdx,
ArrayRef<Constant *> 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<Constant *> or
// ArrayRef<Value *>.
return getGetElementPtr(C, cast<Value>(Idx), InBounds);
}
static Constant *getGetElementPtr(Constant *C,
ArrayRef<Value *> 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<Constant *> IdxList) {
return getGetElementPtr(C, IdxList, true);
}
static Constant *getInBoundsGetElementPtr(Constant *C,
Constant *Idx) {
// This form of the function only exists to avoid ambiguous overload
// warnings about whether to convert Idx to ArrayRef<Constant *> or
// ArrayRef<Value *>.
return getGetElementPtr(C, Idx, true);
}
static Constant *getInBoundsGetElementPtr(Constant *C,
Value* const *IdxList,
unsigned NumIdx) {
return getGetElementPtr(C, IdxList, NumIdx, true);
ArrayRef<Value *> IdxList) {
return getGetElementPtr(C, IdxList, true);
}

static Constant *getExtractElement(Constant *Vec, Constant *Idx);
Expand Down
14 changes: 6 additions & 8 deletions include/llvm/Support/ConstantFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,34 +120,32 @@ class ConstantFolder {

Constant *CreateGetElementPtr(Constant *C,
ArrayRef<Constant *> 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<Constant *> or
// ArrayRef<Value *>.
return ConstantExpr::getGetElementPtr(C, &Idx, 1);
return ConstantExpr::getGetElementPtr(C, Idx);
}
Constant *CreateGetElementPtr(Constant *C,
ArrayRef<Value *> IdxList) const {
return ConstantExpr::getGetElementPtr(C, IdxList.data(), IdxList.size());
return ConstantExpr::getGetElementPtr(C, IdxList);
}

Constant *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef<Constant *> 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<Constant *> or
// ArrayRef<Value *>.
return ConstantExpr::getInBoundsGetElementPtr(C, &Idx, 1);
return ConstantExpr::getInBoundsGetElementPtr(C, Idx);
}
Constant *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef<Value *> IdxList) const {
return ConstantExpr::getInBoundsGetElementPtr(C, IdxList.data(),
IdxList.size());
return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
}

//===--------------------------------------------------------------------===//
Expand Down
5 changes: 2 additions & 3 deletions include/llvm/Support/NoFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class NoFolder {

Constant *CreateGetElementPtr(Constant *C,
ArrayRef<Constant *> IdxList) const {
return ConstantExpr::getGetElementPtr(C, IdxList.data(), IdxList.size());
return ConstantExpr::getGetElementPtr(C, IdxList);
}
Instruction *CreateGetElementPtr(Constant *C,
ArrayRef<Value *> IdxList) const {
Expand All @@ -188,8 +188,7 @@ class NoFolder {

Constant *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef<Constant *> IdxList) const {
return ConstantExpr::getInBoundsGetElementPtr(C, IdxList.data(),
IdxList.size());
return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
}
Instruction *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef<Value *> IdxList) const {
Expand Down
16 changes: 6 additions & 10 deletions include/llvm/Support/TargetFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,36 +132,32 @@ class TargetFolder {

Constant *CreateGetElementPtr(Constant *C,
ArrayRef<Constant *> 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<Constant *> or
// ArrayRef<Value *>.
return Fold(ConstantExpr::getGetElementPtr(C, &Idx, 1));
return Fold(ConstantExpr::getGetElementPtr(C, Idx));
}
Constant *CreateGetElementPtr(Constant *C,
ArrayRef<Value *> IdxList) const {
return Fold(ConstantExpr::getGetElementPtr(C, IdxList.data(),
IdxList.size()));
return Fold(ConstantExpr::getGetElementPtr(C, IdxList));
}

Constant *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef<Constant *> 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<Constant *> or
// ArrayRef<Value *>.
return Fold(ConstantExpr::getInBoundsGetElementPtr(C, &Idx, 1));
return Fold(ConstantExpr::getInBoundsGetElementPtr(C, Idx));
}
Constant *CreateInBoundsGetElementPtr(Constant *C,
ArrayRef<Value *> IdxList) const {
return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList.data(),
IdxList.size()));
return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList));
}

//===--------------------------------------------------------------------===//
Expand Down
7 changes: 3 additions & 4 deletions lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ static Constant *CastGEPIndices(ArrayRef<Constant *> 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<ConstantExpr>(C))
if (Constant *Folded = ConstantFoldConstantExpression(CE, TD))
C = Folded;
Expand Down Expand Up @@ -702,7 +702,7 @@ static Constant *SymbolicallyEvaluateGEP(ArrayRef<Constant *> Ops,

// Create a GEP.
Constant *C =
ConstantExpr::getGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size());
ConstantExpr::getGetElementPtr(Ptr, NewIdxs);
assert(cast<PointerType>(C->getType())->getElementType() == Ty &&
"Computed GetElementPtr has unexpected type!");

Expand Down Expand Up @@ -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));
}
}

Expand Down
4 changes: 1 addition & 3 deletions lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2254,9 +2254,7 @@ Value *llvm::SimplifyGEPInst(ArrayRef<Value *> Ops,
if (!isa<Constant>(Ops[i]))
return 0;

return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]),
(Constant *const*)Ops.data() + 1,
Ops.size() - 1);
return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), Ops.slice(1));
}

/// SimplifyPHINode - See if we can fold the given phi. If not, returns null.
Expand Down
2 changes: 1 addition & 1 deletion lib/Analysis/ScalarEvolutionExpander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ Value *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin,
// Fold a GEP with constant operands.
if (Constant *CLHS = dyn_cast<Constant>(V))
if (Constant *CRHS = dyn_cast<Constant>(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;
Expand Down
8 changes: 3 additions & 5 deletions lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Constant *> 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");
Expand Down
7 changes: 3 additions & 4 deletions lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Constant *> 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#]
Expand Down
2 changes: 1 addition & 1 deletion lib/Target/ARM/ARMGlobalMerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ bool ARMGlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &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();
}
Expand Down
10 changes: 4 additions & 6 deletions lib/Transforms/IPO/GlobalOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Constant>(NewPtr),
&Idxs[0], Idxs.size());
NewPtr = ConstantExpr::getGetElementPtr(cast<Constant>(NewPtr), Idxs);
} else {
GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP);
SmallVector<Value*, 8> Idxs;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -2410,8 +2408,8 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal,
i != e; ++i)
GEPOps.push_back(getVal(Values, *i));
InstResult = cast<GEPOperator>(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<LoadInst>(CurInst)) {
if (LI->isVolatile()) return false; // no volatile accesses.
InstResult = ComputeLoadResult(getVal(Values, LI->getOperand(0)),
Expand Down
5 changes: 2 additions & 3 deletions lib/Transforms/Instrumentation/ProfilingUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArrayType>(Array->getType()->getElementType())->getNumElements();
} else {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/Transforms/Scalar/GVN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Transforms/Scalar/SCCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Constant *> Indices(Operands.begin() + 1, Operands.end());
markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Indices));
}

void SCCPSolver::visitStoreInst(StoreInst &SI) {
Expand Down
Loading

0 comments on commit dab3d29

Please sign in to comment.