Constant *C2);
static Constant *getSelectTy(const Type *Ty,
Constant *C1, Constant *C2, Constant *C3);
+ template<typename IndexTy>
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,
- Value* const *Idxs,
+ IndexTy const *Idxs,
unsigned NumIdxs);
static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
Constant *Idx);
static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
Constant *Val,
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:
// Static methods to construct a ConstantExpr of different kinds. Note that
static const Type *getIndexedType(const Type *Ptr,
Value* const *Idx, unsigned NumIdx);
+ static const Type *getIndexedType(const Type *Ptr,
+ Constant* const *Idx, unsigned NumIdx);
+
static const Type *getIndexedType(const Type *Ptr,
uint64_t const *Idx, unsigned NumIdx);
/// isInBoundsIndices - Test whether the given sequence of *normalized* indices
/// 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.
if (NumIdx == 0) return true;
// 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,
// by the one-past-the-end rule.
if (!cast<ConstantInt>(Idxs[0])->isOne())
return false;
for (unsigned i = 1, e = NumIdx; i != e; ++i)
- if (!Idxs[i]->isNullValue())
+ if (!cast<Constant>(Idxs[i])->isNullValue())
return false;
return true;
}
-Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
- bool inBounds,
- Constant* const *Idxs,
- unsigned NumIdx) {
+template<typename IndexTy>
+static Constant *ConstantFoldGetElementPtrImpl(Constant *C,
+ bool inBounds,
+ IndexTy const *Idxs,
+ unsigned NumIdx) {
+ Constant *Idx0 = cast<Constant>(Idxs[0]);
if (NumIdx == 0 ||
- (NumIdx == 1 && Idxs[0]->isNullValue()))
+ (NumIdx == 1 && Idx0->isNullValue()))
return C;
if (isa<UndefValue>(C)) {
const PointerType *Ptr = cast<PointerType>(C->getType());
- const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
- (Value **)Idxs,
- (Value **)Idxs+NumIdx);
+ const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs, Idxs+NumIdx);
assert(Ty != 0 && "Invalid indices for GEP!");
return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
}
- Constant *Idx0 = Idxs[0];
if (C->isNullValue()) {
bool isNull = true;
for (unsigned i = 0, e = NumIdx; i != e; ++i)
- if (!Idxs[i]->isNullValue()) {
+ if (!cast<Constant>(Idxs[i])->isNullValue()) {
isNull = false;
break;
}
if (isNull) {
const PointerType *Ptr = cast<PointerType>(C->getType());
- const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
- (Value**)Idxs,
- (Value**)Idxs+NumIdx);
+ const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs,
+ Idxs+NumIdx);
assert(Ty != 0 && "Invalid indices for GEP!");
return ConstantPointerNull::get(
PointerType::get(Ty,Ptr->getAddressSpace()));
ATy->getNumElements());
NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
- Constant *PrevIdx = Idxs[i-1];
+ Constant *PrevIdx = cast<Constant>(Idxs[i-1]);
Constant *Div = ConstantExpr::getSDiv(CI, Factor);
// Before adding, extend both operands to i64 to avoid
// If we did any factoring, start over with the adjusted indices.
if (!NewIdxs.empty()) {
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 ?
ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs.data(),
NewIdxs.size()) :
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);
+}
Constant *C1, Constant *C2);
Constant *ConstantFoldGetElementPtr(Constant *C, bool inBounds,
Constant* const *Idxs, unsigned NumIdx);
+ Constant *ConstantFoldGetElementPtr(Constant *C, bool inBounds,
+ Value* const *Idxs, unsigned NumIdx);
} // End llvm namespace
#endif
return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
}
+template<typename IndexTy>
Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
- Value* const *Idxs,
+ IndexTy const *Idxs,
unsigned NumIdx) {
assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
Idxs+NumIdx) ==
"GEP indices invalid!");
if (Constant *FC = ConstantFoldGetElementPtr(C, /*inBounds=*/false,
- (Constant**)Idxs, NumIdx))
+ Idxs, NumIdx))
return FC; // Fold a few common cases...
assert(C->getType()->isPointerTy() &&
return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
}
+template<typename IndexTy>
Constant *ConstantExpr::getInBoundsGetElementPtrTy(const Type *ReqTy,
Constant *C,
- Value *const *Idxs,
+ IndexTy const *Idxs,
unsigned NumIdx) {
assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
Idxs+NumIdx) ==
"GEP indices invalid!");
if (Constant *FC = ConstantFoldGetElementPtr(C, /*inBounds=*/true,
- (Constant**)Idxs, NumIdx))
+ Idxs, NumIdx))
return FC; // Fold a few common cases...
assert(C->getType()->isPointerTy() &&
return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
}
-Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
- unsigned NumIdx) {
+template<typename IndexTy>
+Constant *ConstantExpr::getGetElementPtrImpl(Constant *C, IndexTy const *Idxs,
+ unsigned NumIdx) {
// Get the result type of the getelementptr!
const Type *Ty =
GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
}
-Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
- Value* const *Idxs,
- unsigned NumIdx) {
+template<typename IndexTy>
+Constant *ConstantExpr::getInBoundsGetElementPtrImpl(Constant *C,
+ IndexTy const *Idxs,
+ unsigned NumIdx) {
// Get the result type of the getelementptr!
const Type *Ty =
GetElementPtrInst::getIndexedType(C->getType(), Idxs, 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) {
+ return getGetElementPtrImpl(C, Idxs, NumIdx);
+}
+
+Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant *const *Idxs,
unsigned NumIdx) {
- return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
+ return getGetElementPtrImpl(C, Idxs, NumIdx);
+}
+
+Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
+ Value* const *Idxs,
+ unsigned NumIdx) {
+ return getInBoundsGetElementPtrImpl(C, Idxs, NumIdx);
}
Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
- Constant* const *Idxs,
+ Constant *const *Idxs,
unsigned NumIdx) {
- return getInBoundsGetElementPtr(C, (Value* const *)Idxs, NumIdx);
+ return getInBoundsGetElementPtrImpl(C, Idxs, NumIdx);
}
Constant *
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,
uint64_t const *Idxs,
unsigned NumIdx) {