Remove casts between Value** and Constant**, which won't work if a

static_cast from Constant* to Value* has to adjust the "this" pointer.
This is groundwork for PR889.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123435 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jay Foad 2011-01-14 08:07:43 +00:00
parent e0a1a5ba91
commit 25052d8d64
6 changed files with 82 additions and 33 deletions

View File

@ -629,10 +629,12 @@ protected:
Constant *C2); Constant *C2);
static Constant *getSelectTy(const Type *Ty, static Constant *getSelectTy(const Type *Ty,
Constant *C1, Constant *C2, Constant *C3); Constant *C1, Constant *C2, Constant *C3);
template<typename IndexTy>
static Constant *getGetElementPtrTy(const Type *Ty, Constant *C, static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
Value* const *Idxs, unsigned NumIdxs); IndexTy const *Idxs, unsigned NumIdxs);
template<typename IndexTy>
static Constant *getInBoundsGetElementPtrTy(const Type *Ty, Constant *C, static Constant *getInBoundsGetElementPtrTy(const Type *Ty, Constant *C,
Value* const *Idxs, IndexTy const *Idxs,
unsigned NumIdxs); unsigned NumIdxs);
static Constant *getExtractElementTy(const Type *Ty, Constant *Val, static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
Constant *Idx); Constant *Idx);
@ -645,6 +647,14 @@ protected:
static Constant *getInsertValueTy(const Type *Ty, Constant *Agg, static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
Constant *Val, Constant *Val,
const unsigned *Idxs, unsigned NumIdxs); const unsigned *Idxs, unsigned NumIdxs);
template<typename IndexTy>
static Constant *getGetElementPtrImpl(Constant *C,
IndexTy const *IdxList,
unsigned NumIdx);
template<typename IndexTy>
static Constant *getInBoundsGetElementPtrImpl(Constant *C,
IndexTy const *IdxList,
unsigned NumIdx);
public: public:
// Static methods to construct a ConstantExpr of different kinds. Note that // Static methods to construct a ConstantExpr of different kinds. Note that

View File

@ -457,6 +457,9 @@ public:
static const Type *getIndexedType(const Type *Ptr, static const Type *getIndexedType(const Type *Ptr,
Value* const *Idx, unsigned NumIdx); Value* const *Idx, unsigned NumIdx);
static const Type *getIndexedType(const Type *Ptr,
Constant* const *Idx, unsigned NumIdx);
static const Type *getIndexedType(const Type *Ptr, static const Type *getIndexedType(const Type *Ptr,
uint64_t const *Idx, unsigned NumIdx); uint64_t const *Idx, unsigned NumIdx);

View File

@ -2067,53 +2067,52 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
/// isInBoundsIndices - Test whether the given sequence of *normalized* indices /// isInBoundsIndices - Test whether the given sequence of *normalized* indices
/// is "inbounds". /// is "inbounds".
static bool isInBoundsIndices(Constant *const *Idxs, size_t NumIdx) { template<typename IndexTy>
static bool isInBoundsIndices(IndexTy const *Idxs, size_t NumIdx) {
// No indices means nothing that could be out of bounds. // No indices means nothing that could be out of bounds.
if (NumIdx == 0) return true; if (NumIdx == 0) return true;
// If the first index is zero, it's in bounds. // If the first index is zero, it's in bounds.
if (Idxs[0]->isNullValue()) return true; if (cast<Constant>(Idxs[0])->isNullValue()) return true;
// If the first index is one and all the rest are zero, it's in bounds, // If the first index is one and all the rest are zero, it's in bounds,
// by the one-past-the-end rule. // by the one-past-the-end rule.
if (!cast<ConstantInt>(Idxs[0])->isOne()) if (!cast<ConstantInt>(Idxs[0])->isOne())
return false; return false;
for (unsigned i = 1, e = NumIdx; i != e; ++i) for (unsigned i = 1, e = NumIdx; i != e; ++i)
if (!Idxs[i]->isNullValue()) if (!cast<Constant>(Idxs[i])->isNullValue())
return false; return false;
return true; return true;
} }
Constant *llvm::ConstantFoldGetElementPtr(Constant *C, template<typename IndexTy>
bool inBounds, static Constant *ConstantFoldGetElementPtrImpl(Constant *C,
Constant* const *Idxs, bool inBounds,
unsigned NumIdx) { IndexTy const *Idxs,
unsigned NumIdx) {
Constant *Idx0 = cast<Constant>(Idxs[0]);
if (NumIdx == 0 || if (NumIdx == 0 ||
(NumIdx == 1 && Idxs[0]->isNullValue())) (NumIdx == 1 && Idx0->isNullValue()))
return C; return C;
if (isa<UndefValue>(C)) { if (isa<UndefValue>(C)) {
const PointerType *Ptr = cast<PointerType>(C->getType()); const PointerType *Ptr = cast<PointerType>(C->getType());
const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs, Idxs+NumIdx);
(Value **)Idxs,
(Value **)Idxs+NumIdx);
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()));
} }
Constant *Idx0 = Idxs[0];
if (C->isNullValue()) { if (C->isNullValue()) {
bool isNull = true; bool isNull = true;
for (unsigned i = 0, e = NumIdx; i != e; ++i) for (unsigned i = 0, e = NumIdx; i != e; ++i)
if (!Idxs[i]->isNullValue()) { if (!cast<Constant>(Idxs[i])->isNullValue()) {
isNull = false; isNull = false;
break; break;
} }
if (isNull) { if (isNull) {
const PointerType *Ptr = cast<PointerType>(C->getType()); const PointerType *Ptr = cast<PointerType>(C->getType());
const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs,
(Value**)Idxs, Idxs+NumIdx);
(Value**)Idxs+NumIdx);
assert(Ty != 0 && "Invalid indices for GEP!"); assert(Ty != 0 && "Invalid indices for GEP!");
return ConstantPointerNull::get( return ConstantPointerNull::get(
PointerType::get(Ty,Ptr->getAddressSpace())); PointerType::get(Ty,Ptr->getAddressSpace()));
@ -2208,7 +2207,7 @@ Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
ATy->getNumElements()); ATy->getNumElements());
NewIdxs[i] = ConstantExpr::getSRem(CI, Factor); NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
Constant *PrevIdx = Idxs[i-1]; Constant *PrevIdx = cast<Constant>(Idxs[i-1]);
Constant *Div = ConstantExpr::getSDiv(CI, Factor); Constant *Div = ConstantExpr::getSDiv(CI, Factor);
// Before adding, extend both operands to i64 to avoid // Before adding, extend both operands to i64 to avoid
@ -2236,7 +2235,7 @@ Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
// If we did any factoring, start over with the adjusted indices. // If we did any factoring, start over with the adjusted indices.
if (!NewIdxs.empty()) { if (!NewIdxs.empty()) {
for (unsigned i = 0; i != NumIdx; ++i) for (unsigned i = 0; i != NumIdx; ++i)
if (!NewIdxs[i]) NewIdxs[i] = Idxs[i]; if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
return inBounds ? return inBounds ?
ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs.data(), ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs.data(),
NewIdxs.size()) : NewIdxs.size()) :
@ -2251,3 +2250,17 @@ Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
return 0; return 0;
} }
Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
bool inBounds,
Constant* const *Idxs,
unsigned NumIdx) {
return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs, NumIdx);
}
Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
bool inBounds,
Value* const *Idxs,
unsigned NumIdx) {
return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs, NumIdx);
}

View File

@ -49,6 +49,8 @@ namespace llvm {
Constant *C1, Constant *C2); Constant *C1, Constant *C2);
Constant *ConstantFoldGetElementPtr(Constant *C, bool inBounds, Constant *ConstantFoldGetElementPtr(Constant *C, bool inBounds,
Constant* const *Idxs, unsigned NumIdx); Constant* const *Idxs, unsigned NumIdx);
Constant *ConstantFoldGetElementPtr(Constant *C, bool inBounds,
Value* const *Idxs, unsigned NumIdx);
} // End llvm namespace } // End llvm namespace
#endif #endif

View File

@ -1546,8 +1546,9 @@ Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
return pImpl->ExprConstants.getOrCreate(ReqTy, Key); return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
} }
template<typename IndexTy>
Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Value* const *Idxs, IndexTy const *Idxs,
unsigned NumIdx) { unsigned NumIdx) {
assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
Idxs+NumIdx) == Idxs+NumIdx) ==
@ -1555,7 +1556,7 @@ Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
"GEP indices invalid!"); "GEP indices invalid!");
if (Constant *FC = ConstantFoldGetElementPtr(C, /*inBounds=*/false, if (Constant *FC = ConstantFoldGetElementPtr(C, /*inBounds=*/false,
(Constant**)Idxs, NumIdx)) Idxs, NumIdx))
return FC; // Fold a few common cases... return FC; // Fold a few common cases...
assert(C->getType()->isPointerTy() && assert(C->getType()->isPointerTy() &&
@ -1572,9 +1573,10 @@ Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
return pImpl->ExprConstants.getOrCreate(ReqTy, Key); return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
} }
template<typename IndexTy>
Constant *ConstantExpr::getInBoundsGetElementPtrTy(const Type *ReqTy, Constant *ConstantExpr::getInBoundsGetElementPtrTy(const Type *ReqTy,
Constant *C, Constant *C,
Value *const *Idxs, IndexTy const *Idxs,
unsigned NumIdx) { unsigned NumIdx) {
assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
Idxs+NumIdx) == Idxs+NumIdx) ==
@ -1582,7 +1584,7 @@ Constant *ConstantExpr::getInBoundsGetElementPtrTy(const Type *ReqTy,
"GEP indices invalid!"); "GEP indices invalid!");
if (Constant *FC = ConstantFoldGetElementPtr(C, /*inBounds=*/true, if (Constant *FC = ConstantFoldGetElementPtr(C, /*inBounds=*/true,
(Constant**)Idxs, NumIdx)) Idxs, NumIdx))
return FC; // Fold a few common cases... return FC; // Fold a few common cases...
assert(C->getType()->isPointerTy() && assert(C->getType()->isPointerTy() &&
@ -1600,8 +1602,9 @@ Constant *ConstantExpr::getInBoundsGetElementPtrTy(const Type *ReqTy,
return pImpl->ExprConstants.getOrCreate(ReqTy, Key); return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
} }
Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs, template<typename IndexTy>
unsigned NumIdx) { Constant *ConstantExpr::getGetElementPtrImpl(Constant *C, IndexTy const *Idxs,
unsigned NumIdx) {
// Get the result type of the getelementptr! // Get the result type of the getelementptr!
const Type *Ty = const Type *Ty =
GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx); GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
@ -1610,9 +1613,10 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx); return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
} }
Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C, template<typename IndexTy>
Value* const *Idxs, Constant *ConstantExpr::getInBoundsGetElementPtrImpl(Constant *C,
unsigned NumIdx) { IndexTy const *Idxs,
unsigned NumIdx) {
// Get the result type of the getelementptr! // Get the result type of the getelementptr!
const Type *Ty = const Type *Ty =
GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx); GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
@ -1621,15 +1625,26 @@ Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
return getInBoundsGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx); return getInBoundsGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
} }
Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs, Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
unsigned NumIdx) { unsigned NumIdx) {
return getGetElementPtr(C, (Value* const *)Idxs, NumIdx); return getGetElementPtrImpl(C, Idxs, NumIdx);
}
Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant *const *Idxs,
unsigned NumIdx) {
return getGetElementPtrImpl(C, Idxs, NumIdx);
} }
Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C, Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
Constant* const *Idxs, Value* const *Idxs,
unsigned NumIdx) { unsigned NumIdx) {
return getInBoundsGetElementPtr(C, (Value* const *)Idxs, NumIdx); return getInBoundsGetElementPtrImpl(C, Idxs, NumIdx);
}
Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
Constant *const *Idxs,
unsigned NumIdx) {
return getInBoundsGetElementPtrImpl(C, Idxs, NumIdx);
} }
Constant * Constant *

View File

@ -1173,6 +1173,12 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
return getIndexedTypeInternal(Ptr, Idxs, NumIdx); return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
} }
const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Constant* const *Idxs,
unsigned NumIdx) {
return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
}
const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
uint64_t const *Idxs, uint64_t const *Idxs,
unsigned NumIdx) { unsigned NumIdx) {