Skip to content

[NFC][IR] Type: add getWithNewType() method #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions llvm/include/llvm/IR/DerivedTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,14 +701,17 @@ Type *Type::getExtendedType() const {
return cast<IntegerType>(this)->getExtendedType();
}

Type *Type::getWithNewType(Type *EltTy) const {
if (auto *VTy = dyn_cast<VectorType>(this))
return VectorType::get(EltTy, VTy->getElementCount());
return EltTy;
}

Type *Type::getWithNewBitWidth(unsigned NewBitWidth) const {
assert(
isIntOrIntVectorTy() &&
"Original type expected to be a vector of integers or a scalar integer.");
Type *NewType = getIntNTy(getContext(), NewBitWidth);
if (auto *VTy = dyn_cast<VectorType>(this))
NewType = VectorType::get(NewType, VTy->getElementCount());
return NewType;
return getWithNewType(getIntNTy(getContext(), NewBitWidth));
}

unsigned Type::getPointerAddressSpace() const {
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/IR/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ class Type {
return ContainedTys[0];
}

/// Given vector type, change the element type,
/// whilst keeping the old number of elements.
/// For non-vectors simply returns \p EltTy.
inline Type *getWithNewType(Type *EltTy) const;

/// Given an integer or vector type, change the lane bitwidth to NewBitwidth,
/// whilst keeping the old number of lanes.
inline Type *getWithNewBitWidth(unsigned NewBitWidth) const;
Expand Down
15 changes: 5 additions & 10 deletions llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1927,11 +1927,8 @@ Instruction *InstCombinerImpl::visitIntToPtr(IntToPtrInst &CI) {
unsigned AS = CI.getAddressSpace();
if (CI.getOperand(0)->getType()->getScalarSizeInBits() !=
DL.getPointerSizeInBits(AS)) {
Type *Ty = DL.getIntPtrType(CI.getContext(), AS);
// Handle vectors of pointers.
if (auto *CIVTy = dyn_cast<VectorType>(CI.getType()))
Ty = VectorType::get(Ty, CIVTy->getElementCount());

Type *Ty = CI.getOperand(0)->getType()->getWithNewType(
DL.getIntPtrType(CI.getContext(), AS));
Value *P = Builder.CreateZExtOrTrunc(CI.getOperand(0), Ty);
return new IntToPtrInst(P, CI.getType());
}
Expand Down Expand Up @@ -1970,16 +1967,14 @@ Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) {
// do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast
// to be exposed to other transforms.
Value *SrcOp = CI.getPointerOperand();
Type *SrcTy = SrcOp->getType();
Type *Ty = CI.getType();
unsigned AS = CI.getPointerAddressSpace();
unsigned TySize = Ty->getScalarSizeInBits();
unsigned PtrSize = DL.getPointerSizeInBits(AS);
if (TySize != PtrSize) {
Type *IntPtrTy = DL.getIntPtrType(CI.getContext(), AS);
// Handle vectors of pointers.
if (auto *VecTy = dyn_cast<VectorType>(Ty))
IntPtrTy = VectorType::get(IntPtrTy, VecTy->getElementCount());

Type *IntPtrTy =
SrcTy->getWithNewType(DL.getIntPtrType(CI.getContext(), AS));
Value *P = Builder.CreatePtrToInt(SrcOp, IntPtrTy);
return CastInst::CreateIntegerCast(P, Ty, /*isSigned=*/false);
}
Expand Down