1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 02:52:53 +02:00

Allow creation of GEP constantexprs with a vector of value* operands as

well as a vector of constant*'s.  It turns out that this is more efficient
and all of the clients want to do that, so we should cater to them.

llvm-svn: 16923
This commit is contained in:
Chris Lattner 2004-10-11 22:52:25 +00:00
parent 6496308d18
commit 63ba63bb56
4 changed files with 42 additions and 28 deletions

View File

@ -533,7 +533,7 @@ protected:
static Constant *getSelectTy(const Type *Ty, static Constant *getSelectTy(const Type *Ty,
Constant *C1, Constant *C2, Constant *C3); Constant *C1, Constant *C2, Constant *C3);
static Constant *getGetElementPtrTy(const Type *Ty, Constant *C, static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
const std::vector<Constant*> &IdxList); const std::vector<Value*> &IdxList);
public: public:
// Static methods to construct a ConstantExpr of different kinds. Note that // Static methods to construct a ConstantExpr of different kinds. Note that
@ -584,10 +584,13 @@ public:
static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
static Constant *getSShr(Constant *C1, Constant *C2); // signed shr static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
/// Getelementptr form... /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
/// all elements must be Constant's.
/// ///
static Constant *getGetElementPtr(Constant *C, static Constant *getGetElementPtr(Constant *C,
const std::vector<Constant*> &IdxList); const std::vector<Constant*> &IdxList);
static Constant *getGetElementPtr(Constant *C,
const std::vector<Value*> &IdxList);
/// isNullValue - Return true if this is the value that would be returned by /// isNullValue - Return true if this is the value that would be returned by
/// getNullValue. /// getNullValue.

View File

@ -905,7 +905,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
if (CE1->getOpcode() == Instruction::Cast && if (CE1->getOpcode() == Instruction::Cast &&
isa<GlobalValue>(CE1->getOperand(0))) { isa<GlobalValue>(CE1->getOperand(0))) {
GlobalValue *CPR =cast<GlobalValue>(CE1->getOperand(0)); GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
// Functions are at least 4-byte aligned. If and'ing the address of a // Functions are at least 4-byte aligned. If and'ing the address of a
// function with a constant < 4, fold it to zero. // function with a constant < 4, fold it to zero.
@ -960,21 +960,21 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
} }
Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
const std::vector<Constant*> &IdxList) { const std::vector<Value*> &IdxList) {
if (IdxList.size() == 0 || if (IdxList.size() == 0 ||
(IdxList.size() == 1 && IdxList[0]->isNullValue())) (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
return const_cast<Constant*>(C); return const_cast<Constant*>(C);
Constant *Idx0 = cast<Constant>(IdxList[0]);
if (C->isNullValue()) { if (C->isNullValue()) {
bool isNull = true; bool isNull = true;
for (unsigned i = 0, e = IdxList.size(); i != e; ++i) for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
if (!IdxList[i]->isNullValue()) { if (!cast<Constant>(IdxList[i])->isNullValue()) {
isNull = false; isNull = false;
break; break;
} }
if (isNull) { if (isNull) {
std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end()); const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
true); true);
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));
@ -986,8 +986,8 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
// gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
// type, we can statically fold this. // type, we can statically fold this.
Constant *R = ConstantUInt::get(Type::UIntTy, ElSize); Constant *R = ConstantUInt::get(Type::UIntTy, ElSize);
R = ConstantExpr::getCast(R, IdxList[0]->getType()); R = ConstantExpr::getCast(R, Idx0->getType());
R = ConstantExpr::getMul(R, IdxList[0]); R = ConstantExpr::getMul(R, Idx0);
return ConstantExpr::getCast(R, C->getType()); return ConstantExpr::getCast(R, C->getType());
} }
} }
@ -1004,21 +1004,22 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
I != E; ++I) I != E; ++I)
LastTy = *I; LastTy = *I;
if ((LastTy && isa<ArrayType>(LastTy)) || IdxList[0]->isNullValue()) { if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
std::vector<Constant*> NewIndices; std::vector<Value*> NewIndices;
NewIndices.reserve(IdxList.size() + CE->getNumOperands()); NewIndices.reserve(IdxList.size() + CE->getNumOperands());
for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
NewIndices.push_back(cast<Constant>(CE->getOperand(i))); NewIndices.push_back(CE->getOperand(i));
// Add the last index of the source with the first index of the new GEP. // Add the last index of the source with the first index of the new GEP.
// Make sure to handle the case when they are actually different types. // Make sure to handle the case when they are actually different types.
Constant *Combined = CE->getOperand(CE->getNumOperands()-1); Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
if (!IdxList[0]->isNullValue()) { // Otherwise it must be an array // Otherwise it must be an array.
if (!Idx0->isNullValue()) {
const Type *IdxTy = Combined->getType(); const Type *IdxTy = Combined->getType();
if (IdxTy != IdxList[0]->getType()) IdxTy = Type::LongTy; if (IdxTy != Idx0->getType()) IdxTy = Type::LongTy;
Combined = Combined =
ConstantExpr::get(Instruction::Add, ConstantExpr::get(Instruction::Add,
ConstantExpr::getCast(IdxList[0], IdxTy), ConstantExpr::getCast(Idx0, IdxTy),
ConstantExpr::getCast(Combined, IdxTy)); ConstantExpr::getCast(Combined, IdxTy));
} }
@ -1034,7 +1035,7 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
// To: int* getelementptr ([3 x int]* %X, long 0, long 0) // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
// //
if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 && if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
IdxList[0]->isNullValue()) Idx0->isNullValue())
if (const PointerType *SPT = if (const PointerType *SPT =
dyn_cast<PointerType>(CE->getOperand(0)->getType())) dyn_cast<PointerType>(CE->getOperand(0)->getType()))
if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType())) if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))

View File

@ -22,6 +22,7 @@
#include <vector> #include <vector>
namespace llvm { namespace llvm {
class Value;
class Constant; class Constant;
struct Type; struct Type;
@ -33,7 +34,7 @@ namespace llvm {
Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1, Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
const Constant *V2); const Constant *V2);
Constant *ConstantFoldGetElementPtr(const Constant *C, Constant *ConstantFoldGetElementPtr(const Constant *C,
const std::vector<Constant*> &IdxList); const std::vector<Value*> &IdxList);
} // End llvm namespace } // End llvm namespace
#endif #endif

View File

@ -1148,10 +1148,8 @@ namespace llvm {
break; break;
case Instruction::GetElementPtr: case Instruction::GetElementPtr:
// Make everyone now use a constant of the new type... // Make everyone now use a constant of the new type...
std::vector<Constant*> C; std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i) New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
C.push_back(cast<Constant>(OldC->getOperand(i)));
New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
break; break;
} }
@ -1298,9 +1296,8 @@ Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
const std::vector<Constant*> &IdxList) { const std::vector<Value*> &IdxList) {
assert(GetElementPtrInst::getIndexedType(C->getType(), assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
"GEP indices invalid!"); "GEP indices invalid!");
if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList)) if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
@ -1309,9 +1306,12 @@ Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
assert(isa<PointerType>(C->getType()) && assert(isa<PointerType>(C->getType()) &&
"Non-pointer type for constant GetElementPtr expression"); "Non-pointer type for constant GetElementPtr expression");
// Look up the constant in the table first to ensure uniqueness // Look up the constant in the table first to ensure uniqueness
std::vector<Constant*> argVec(1, C); std::vector<Constant*> ArgVec;
argVec.insert(argVec.end(), IdxList.begin(), IdxList.end()); ArgVec.reserve(IdxList.size()+1);
const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec); ArgVec.push_back(C);
for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
ArgVec.push_back(cast<Constant>(IdxList[i]));
const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
return ExprConstants.getOrCreate(ReqTy, Key); return ExprConstants.getOrCreate(ReqTy, Key);
} }
@ -1323,6 +1323,15 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C,
const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList, const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
true); true);
assert(Ty && "GEP indices invalid!"); assert(Ty && "GEP indices invalid!");
return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
}
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); return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
} }