Skip to content

Commit

Permalink
[IR] Remove typed pointer handling from getGEPReturnType() (NFC)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jul 18, 2023
1 parent 08fd44b commit b13f280
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
25 changes: 9 additions & 16 deletions llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -1077,26 +1077,19 @@ class GetElementPtrInst : public Instruction {

/// Returns the pointer type returned by the GEP
/// instruction, which may be a vector of pointers.
static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
ArrayRef<Value *> IdxList) {
PointerType *OrigPtrTy = cast<PointerType>(Ptr->getType()->getScalarType());
unsigned AddrSpace = OrigPtrTy->getAddressSpace();
Type *ResultElemTy = checkGEPType(getIndexedType(ElTy, IdxList));
Type *PtrTy = OrigPtrTy->isOpaque()
? PointerType::get(OrigPtrTy->getContext(), AddrSpace)
: PointerType::get(ResultElemTy, AddrSpace);
static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
// Vector GEP
if (auto *PtrVTy = dyn_cast<VectorType>(Ptr->getType())) {
ElementCount EltCount = PtrVTy->getElementCount();
return VectorType::get(PtrTy, EltCount);
}
Type *Ty = Ptr->getType();
if (Ty->isVectorTy())
return Ty;

for (Value *Index : IdxList)
if (auto *IndexVTy = dyn_cast<VectorType>(Index->getType())) {
ElementCount EltCount = IndexVTy->getElementCount();
return VectorType::get(PtrTy, EltCount);
return VectorType::get(Ty, EltCount);
}
// Scalar GEP
return PtrTy;
return Ty;
}

unsigned getNumIndices() const { // Note: always non-negative
Expand Down Expand Up @@ -1154,7 +1147,7 @@ GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
ArrayRef<Value *> IdxList, unsigned Values,
const Twine &NameStr,
Instruction *InsertBefore)
: Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
: Instruction(getGEPReturnType(Ptr, IdxList), GetElementPtr,
OperandTraits<GetElementPtrInst>::op_end(this) - Values,
Values, InsertBefore),
SourceElementType(PointeeType),
Expand All @@ -1166,7 +1159,7 @@ GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
ArrayRef<Value *> IdxList, unsigned Values,
const Twine &NameStr,
BasicBlock *InsertAtEnd)
: Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
: Instruction(getGEPReturnType(Ptr, IdxList), GetElementPtr,
OperandTraits<GetElementPtrInst>::op_end(this) - Values,
Values, InsertAtEnd),
SourceElementType(PointeeType),
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/ConstantFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,7 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
if (Idxs.empty()) return C;

Type *GEPTy = GetElementPtrInst::getGEPReturnType(
PointeeTy, C, ArrayRef((Value *const *)Idxs.data(), Idxs.size()));
C, ArrayRef((Value *const *)Idxs.data(), Idxs.size()));

if (isa<PoisonValue>(C))
return PoisonValue::get(GEPTy);
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/IR/IRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1260,8 +1260,7 @@ Value *IRBuilderBase::CreatePreserveArrayAccessIndex(
SmallVector<Value *, 4> IdxList(Dimension, Zero);
IdxList.push_back(LastIndexV);

Type *ResultType =
GetElementPtrInst::getGEPReturnType(ElTy, Base, IdxList);
Type *ResultType = GetElementPtrInst::getGEPReturnType(Base, IdxList);

Module *M = BB->getParent()->getParent();
Function *FnPreserveArrayAccessIndex = Intrinsic::getDeclaration(
Expand Down Expand Up @@ -1307,7 +1306,7 @@ Value *IRBuilderBase::CreatePreserveStructAccessIndex(
Value *GEPIndex = getInt32(Index);
Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
Type *ResultType =
GetElementPtrInst::getGEPReturnType(ElTy, Base, {Zero, GEPIndex});
GetElementPtrInst::getGEPReturnType(Base, {Zero, GEPIndex});

Module *M = BB->getParent()->getParent();
Function *FnPreserveStructAccessIndex = Intrinsic::getDeclaration(
Expand Down

0 comments on commit b13f280

Please sign in to comment.