Revise APIs for creating constantexpr GEPs to not require the use of vectors.

This allows us to eliminate many temporary vectors, and theirassociated malloc/free pairs.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33692 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2007-01-31 04:40:28 +00:00
parent fb11053815
commit 2b9a5daf7c
5 changed files with 48 additions and 49 deletions

View File

@ -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<PointerType>(V->getType()))
if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
std::vector<Value*> IdxList;
SmallVector<Value*, 8> 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<Constant*>(V),IdxList);
const_cast<Constant*>(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<Value*> &IdxList) {
if (IdxList.size() == 0 ||
(IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Constant* const *Idxs,
unsigned NumIdx) {
if (NumIdx == 0 ||
(NumIdx == 1 && Idxs[0]->isNullValue()))
return const_cast<Constant*>(C);
if (isa<UndefValue>(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<Constant>(IdxList[0]);
Constant *Idx0 = Idxs[0];
if (C->isNullValue()) {
bool isNull = true;
for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
if (!cast<Constant>(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<ArrayType>(LastTy)) || Idx0->isNullValue()) {
std::vector<Value*> NewIndices;
NewIndices.reserve(IdxList.size() + CE->getNumOperands());
SmallVector<Value*, 16> 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<PointerType>(CE->getOperand(0)->getType()))
if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
@ -1371,7 +1376,7 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
if (CAT->getElementType() == SAT->getElementType())
return ConstantExpr::getGetElementPtr(
(Constant*)CE->getOperand(0), IdxList);
(Constant*)CE->getOperand(0), Idxs, NumIdx);
}
return 0;
}

View File

@ -19,8 +19,6 @@
#ifndef CONSTANTFOLDING_H
#define CONSTANTFOLDING_H
#include <vector>
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<Value*> &IdxList);
Constant* const *Idxs, unsigned NumIdx);
} // End llvm namespace
#endif

View File

@ -19,8 +19,6 @@
#ifndef CONSTANTFOLDING_H
#define CONSTANTFOLDING_H
#include <vector>
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<Value*> &IdxList);
Constant* const *Idxs, unsigned NumIdx);
} // End llvm namespace
#endif

View File

@ -1377,7 +1377,8 @@ namespace llvm {
case Instruction::GetElementPtr:
// Make everyone now use a constant of the new type...
std::vector<Value*> 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<Value*> &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<PointerType>(C->getType()) &&
"Non-pointer type for constant GetElementPtr expression");
// Look up the constant in the table first to ensure uniqueness
std::vector<Constant*> 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<Constant>(IdxList[i]));
const ExprMapKeyType Key(Instruction::GetElementPtr,ArgVec);
for (unsigned i = 0; i != NumIdx; ++i)
ArgVec.push_back(cast<Constant>(Idxs[i]));
const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
return ExprConstants->getOrCreate(ReqTy, Key);
}
Constant *ConstantExpr::getGetElementPtr(Constant *C,
const std::vector<Constant*> &IdxList){
Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
unsigned NumIdx) {
// Get the result type of the getelementptr!
std::vector<Value*> 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<Value*> &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());

View File

@ -765,12 +765,13 @@ GetElementPtrInst::~GetElementPtrInst() {
// pointer type.
//
const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
const std::vector<Value*> &Idx,
Value* const *Idxs,
unsigned NumIdx,
bool AllowCompositeLeaf) {
if (!isa<PointerType>(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<PointerType>(Ptr)->getElementType()->isFirstClassType())
return cast<PointerType>(Ptr)->getElementType();
@ -779,12 +780,12 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
unsigned CurIdx = 0;
while (const CompositeType *CT = dyn_cast<CompositeType>(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<PointerType>(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,