ArrayRef<Value *> Idxs, bool InBounds,
std::optional<unsigned> InRangeIndex,
Type *OnlyIfReducedTy) {
- PointerType *OrigPtrTy = cast<PointerType>(C->getType()->getScalarType());
assert(Ty && "Must specify element type");
assert(isSupportedGetElementPtr(Ty) && "Element type is unsupported!");
ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs))
return FC; // Fold a few common cases.
+ assert(GetElementPtrInst::getIndexedType(Ty, Idxs) &&
+ "GEP indices invalid!");;
+
// Get the result type of the getelementptr!
- Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
- assert(DestTy && "GEP indices invalid!");
- unsigned AS = OrigPtrTy->getAddressSpace();
- Type *ReqTy = OrigPtrTy->isOpaque()
- ? PointerType::get(OrigPtrTy->getContext(), AS)
- : DestTy->getPointerTo(AS);
+ Type *ReqTy = GetElementPtrInst::getGEPReturnType(C, Idxs);
+ if (OnlyIfReducedTy == ReqTy)
+ return nullptr;
auto EltCount = ElementCount::getFixed(0);
- if (VectorType *VecTy = dyn_cast<VectorType>(C->getType()))
+ if (VectorType *VecTy = dyn_cast<VectorType>(ReqTy))
EltCount = VecTy->getElementCount();
- else
- for (auto *Idx : Idxs)
- if (VectorType *VecTy = dyn_cast<VectorType>(Idx->getType()))
- EltCount = VecTy->getElementCount();
-
- if (EltCount.isNonZero())
- ReqTy = VectorType::get(ReqTy, EltCount);
-
- if (OnlyIfReducedTy == ReqTy)
- return nullptr;
// Look up the constant in the table first to ensure uniqueness
std::vector<Constant*> ArgVec;