Skip to content

Commit b61c24f

Browse files
LebedevRInikic
authored andcommitted
[NFC][IR] Type: add getWithNewType() method
Sometimes you want to get a type with same vector element count as the current type, but different element type, but there's no QOL wrapper to do that. Add one.
1 parent fd23f45 commit b61c24f

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

llvm/include/llvm/IR/DerivedTypes.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -701,14 +701,17 @@ Type *Type::getExtendedType() const {
701701
return cast<IntegerType>(this)->getExtendedType();
702702
}
703703

704+
Type *Type::getWithNewType(Type *EltTy) const {
705+
if (auto *VTy = dyn_cast<VectorType>(this))
706+
return VectorType::get(EltTy, VTy->getElementCount());
707+
return EltTy;
708+
}
709+
704710
Type *Type::getWithNewBitWidth(unsigned NewBitWidth) const {
705711
assert(
706712
isIntOrIntVectorTy() &&
707713
"Original type expected to be a vector of integers or a scalar integer.");
708-
Type *NewType = getIntNTy(getContext(), NewBitWidth);
709-
if (auto *VTy = dyn_cast<VectorType>(this))
710-
NewType = VectorType::get(NewType, VTy->getElementCount());
711-
return NewType;
714+
return getWithNewType(getIntNTy(getContext(), NewBitWidth));
712715
}
713716

714717
unsigned Type::getPointerAddressSpace() const {

llvm/include/llvm/IR/Type.h

+5
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ class Type {
376376
return ContainedTys[0];
377377
}
378378

379+
/// Given vector type, change the element type,
380+
/// whilst keeping the old number of elements.
381+
/// For non-vectors simply returns \p EltTy.
382+
inline Type *getWithNewType(Type *EltTy) const;
383+
379384
/// Given an integer or vector type, change the lane bitwidth to NewBitwidth,
380385
/// whilst keeping the old number of lanes.
381386
inline Type *getWithNewBitWidth(unsigned NewBitWidth) const;

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -1927,11 +1927,8 @@ Instruction *InstCombinerImpl::visitIntToPtr(IntToPtrInst &CI) {
19271927
unsigned AS = CI.getAddressSpace();
19281928
if (CI.getOperand(0)->getType()->getScalarSizeInBits() !=
19291929
DL.getPointerSizeInBits(AS)) {
1930-
Type *Ty = DL.getIntPtrType(CI.getContext(), AS);
1931-
// Handle vectors of pointers.
1932-
if (auto *CIVTy = dyn_cast<VectorType>(CI.getType()))
1933-
Ty = VectorType::get(Ty, CIVTy->getElementCount());
1934-
1930+
Type *Ty = CI.getOperand(0)->getType()->getWithNewType(
1931+
DL.getIntPtrType(CI.getContext(), AS));
19351932
Value *P = Builder.CreateZExtOrTrunc(CI.getOperand(0), Ty);
19361933
return new IntToPtrInst(P, CI.getType());
19371934
}
@@ -1970,16 +1967,14 @@ Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) {
19701967
// do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast
19711968
// to be exposed to other transforms.
19721969
Value *SrcOp = CI.getPointerOperand();
1970+
Type *SrcTy = SrcOp->getType();
19731971
Type *Ty = CI.getType();
19741972
unsigned AS = CI.getPointerAddressSpace();
19751973
unsigned TySize = Ty->getScalarSizeInBits();
19761974
unsigned PtrSize = DL.getPointerSizeInBits(AS);
19771975
if (TySize != PtrSize) {
1978-
Type *IntPtrTy = DL.getIntPtrType(CI.getContext(), AS);
1979-
// Handle vectors of pointers.
1980-
if (auto *VecTy = dyn_cast<VectorType>(Ty))
1981-
IntPtrTy = VectorType::get(IntPtrTy, VecTy->getElementCount());
1982-
1976+
Type *IntPtrTy =
1977+
SrcTy->getWithNewType(DL.getIntPtrType(CI.getContext(), AS));
19831978
Value *P = Builder.CreatePtrToInt(SrcOp, IntPtrTy);
19841979
return CastInst::CreateIntegerCast(P, Ty, /*isSigned=*/false);
19851980
}

0 commit comments

Comments
 (0)