Skip to content

Commit dea16eb

Browse files
[LLVM][IR] Replace ConstantInt's specialisation of getType() with getIntegerType(). (#75217)
The specialisation will not be valid when ConstantInt gains native support for vector types. This is largely a mechanical change but with extra attention paid to constant folding, InstCombineVectorOps.cpp, LoopFlatten.cpp and Verifier.cpp to remove the need to call `getIntegerType()`. Co-authored-by: Nikita Popov <[email protected]>
1 parent 2f81788 commit dea16eb

File tree

12 files changed

+37
-37
lines changed

12 files changed

+37
-37
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3214,7 +3214,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
32143214
Value *AlignmentValue = EmitScalarExpr(E->getArg(1));
32153215
ConstantInt *AlignmentCI = cast<ConstantInt>(AlignmentValue);
32163216
if (AlignmentCI->getValue().ugt(llvm::Value::MaximumAlignment))
3217-
AlignmentCI = ConstantInt::get(AlignmentCI->getType(),
3217+
AlignmentCI = ConstantInt::get(AlignmentCI->getIntegerType(),
32183218
llvm::Value::MaximumAlignment);
32193219

32203220
emitAlignmentAssumption(PtrValue, Ptr,
@@ -17034,7 +17034,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
1703417034
Value *Op1 = EmitScalarExpr(E->getArg(1));
1703517035
ConstantInt *AlignmentCI = cast<ConstantInt>(Op0);
1703617036
if (AlignmentCI->getValue().ugt(llvm::Value::MaximumAlignment))
17037-
AlignmentCI = ConstantInt::get(AlignmentCI->getType(),
17037+
AlignmentCI = ConstantInt::get(AlignmentCI->getIntegerType(),
1703817038
llvm::Value::MaximumAlignment);
1703917039

1704017040
emitAlignmentAssumption(Op1, E->getArg(1),
@@ -17272,7 +17272,8 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
1727217272
Op0, llvm::FixedVectorType::get(ConvertType(E->getType()), 2));
1727317273

1727417274
if (getTarget().isLittleEndian())
17275-
Index = ConstantInt::get(Index->getType(), 1 - Index->getZExtValue());
17275+
Index =
17276+
ConstantInt::get(Index->getIntegerType(), 1 - Index->getZExtValue());
1727617277

1727717278
return Builder.CreateExtractElement(Unpacked, Index);
1727817279
}

llvm/include/llvm/IR/Constants.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,9 @@ class ConstantInt final : public ConstantData {
171171
/// Determine if this constant's value is same as an unsigned char.
172172
bool equalsInt(uint64_t V) const { return Val == V; }
173173

174-
/// getType - Specialize the getType() method to always return an IntegerType,
175-
/// which reduces the amount of casting needed in parts of the compiler.
176-
///
177-
inline IntegerType *getType() const {
174+
/// Variant of the getType() method to always return an IntegerType, which
175+
/// reduces the amount of casting needed in parts of the compiler.
176+
inline IntegerType *getIntegerType() const {
178177
return cast<IntegerType>(Value::getType());
179178
}
180179

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6079,7 +6079,7 @@ static Value *simplifyRelativeLoad(Constant *Ptr, Constant *Offset,
60796079
Type *Int32Ty = Type::getInt32Ty(Ptr->getContext());
60806080

60816081
auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset);
6082-
if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64)
6082+
if (!OffsetConstInt || OffsetConstInt->getBitWidth() > 64)
60836083
return nullptr;
60846084

60856085
APInt OffsetInt = OffsetConstInt->getValue().sextOrTrunc(

llvm/lib/IR/ConstantFold.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
868868
}
869869

870870
if (GVAlign > 1) {
871-
unsigned DstWidth = CI2->getType()->getBitWidth();
871+
unsigned DstWidth = CI2->getBitWidth();
872872
unsigned SrcWidth = std::min(DstWidth, Log2(GVAlign));
873873
APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
874874

llvm/lib/IR/Verifier.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,10 +2296,9 @@ void Verifier::verifyFunctionMetadata(
22962296
Check(isa<ConstantAsMetadata>(MD->getOperand(0)),
22972297
"expected a constant operand for !kcfi_type", MD);
22982298
Constant *C = cast<ConstantAsMetadata>(MD->getOperand(0))->getValue();
2299-
Check(isa<ConstantInt>(C),
2299+
Check(isa<ConstantInt>(C) && isa<IntegerType>(C->getType()),
23002300
"expected a constant integer operand for !kcfi_type", MD);
2301-
IntegerType *Type = cast<ConstantInt>(C)->getType();
2302-
Check(Type->getBitWidth() == 32,
2301+
Check(cast<ConstantInt>(C)->getBitWidth() == 32,
23032302
"expected a 32-bit integer constant operand for !kcfi_type", MD);
23042303
}
23052304
}
@@ -5690,8 +5689,10 @@ void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
56905689
"vector of ints");
56915690

56925691
auto *Op3 = cast<ConstantInt>(Call.getArgOperand(2));
5693-
Check(Op3->getType()->getBitWidth() <= 32,
5694-
"third argument of [us][mul|div]_fix[_sat] must fit within 32 bits");
5692+
Check(Op3->getType()->isIntegerTy(),
5693+
"third operand of [us][mul|div]_fix[_sat] must be an int type");
5694+
Check(Op3->getBitWidth() <= 32,
5695+
"third operand of [us][mul|div]_fix[_sat] must fit within 32 bits");
56955696

56965697
if (ID == Intrinsic::smul_fix || ID == Intrinsic::smul_fix_sat ||
56975698
ID == Intrinsic::sdiv_fix || ID == Intrinsic::sdiv_fix_sat) {

llvm/lib/Target/Hexagon/HexagonLoopIdiomRecognition.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ void PolynomialMultiplyRecognize::promoteTo(Instruction *In,
10621062
// Promote immediates.
10631063
for (unsigned i = 0, n = In->getNumOperands(); i != n; ++i) {
10641064
if (ConstantInt *CI = dyn_cast<ConstantInt>(In->getOperand(i)))
1065-
if (CI->getType()->getBitWidth() < DestBW)
1065+
if (CI->getBitWidth() < DestBW)
10661066
In->setOperand(i, ConstantInt::get(DestTy, CI->getZExtValue()));
10671067
}
10681068
}
@@ -1577,7 +1577,7 @@ Value *PolynomialMultiplyRecognize::generate(BasicBlock::iterator At,
15771577

15781578
static bool hasZeroSignBit(const Value *V) {
15791579
if (const auto *CI = dyn_cast<const ConstantInt>(V))
1580-
return (CI->getType()->getSignBit() & CI->getSExtValue()) == 0;
1580+
return CI->getValue().isNonNegative();
15811581
const Instruction *I = dyn_cast<const Instruction>(V);
15821582
if (!I)
15831583
return false;
@@ -1688,7 +1688,7 @@ void PolynomialMultiplyRecognize::setupPreSimplifier(Simplifier &S) {
16881688
if (I->getOpcode() != Instruction::Or)
16891689
return nullptr;
16901690
ConstantInt *Msb = dyn_cast<ConstantInt>(I->getOperand(1));
1691-
if (!Msb || Msb->getZExtValue() != Msb->getType()->getSignBit())
1691+
if (!Msb || !Msb->getValue().isSignMask())
16921692
return nullptr;
16931693
if (!hasZeroSignBit(I->getOperand(0)))
16941694
return nullptr;

llvm/lib/Transforms/IPO/OpenMPOpt.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3763,7 +3763,7 @@ struct AAKernelInfoFunction : AAKernelInfo {
37633763
ConstantInt *ExecModeC =
37643764
KernelInfo::getExecModeFromKernelEnvironment(KernelEnvC);
37653765
ConstantInt *AssumedExecModeC = ConstantInt::get(
3766-
ExecModeC->getType(),
3766+
ExecModeC->getIntegerType(),
37673767
ExecModeC->getSExtValue() | OMP_TGT_EXEC_MODE_GENERIC_SPMD);
37683768
if (ExecModeC->getSExtValue() & OMP_TGT_EXEC_MODE_SPMD)
37693769
SPMDCompatibilityTracker.indicateOptimisticFixpoint();
@@ -3792,7 +3792,7 @@ struct AAKernelInfoFunction : AAKernelInfo {
37923792
ConstantInt *MayUseNestedParallelismC =
37933793
KernelInfo::getMayUseNestedParallelismFromKernelEnvironment(KernelEnvC);
37943794
ConstantInt *AssumedMayUseNestedParallelismC = ConstantInt::get(
3795-
MayUseNestedParallelismC->getType(), NestedParallelism);
3795+
MayUseNestedParallelismC->getIntegerType(), NestedParallelism);
37963796
setMayUseNestedParallelismOfKernelEnvironment(
37973797
AssumedMayUseNestedParallelismC);
37983798

@@ -3801,7 +3801,7 @@ struct AAKernelInfoFunction : AAKernelInfo {
38013801
KernelInfo::getUseGenericStateMachineFromKernelEnvironment(
38023802
KernelEnvC);
38033803
ConstantInt *AssumedUseGenericStateMachineC =
3804-
ConstantInt::get(UseGenericStateMachineC->getType(), false);
3804+
ConstantInt::get(UseGenericStateMachineC->getIntegerType(), false);
38053805
setUseGenericStateMachineOfKernelEnvironment(
38063806
AssumedUseGenericStateMachineC);
38073807
}
@@ -4280,8 +4280,9 @@ struct AAKernelInfoFunction : AAKernelInfo {
42804280
// kernel is executed in.
42814281
assert(ExecModeVal == OMP_TGT_EXEC_MODE_GENERIC &&
42824282
"Initially non-SPMD kernel has SPMD exec mode!");
4283-
setExecModeOfKernelEnvironment(ConstantInt::get(
4284-
ExecModeC->getType(), ExecModeVal | OMP_TGT_EXEC_MODE_GENERIC_SPMD));
4283+
setExecModeOfKernelEnvironment(
4284+
ConstantInt::get(ExecModeC->getIntegerType(),
4285+
ExecModeVal | OMP_TGT_EXEC_MODE_GENERIC_SPMD));
42854286

42864287
++NumOpenMPTargetRegionKernelsSPMD;
42874288

@@ -4332,7 +4333,7 @@ struct AAKernelInfoFunction : AAKernelInfo {
43324333

43334334
// If not SPMD mode, indicate we use a custom state machine now.
43344335
setUseGenericStateMachineOfKernelEnvironment(
4335-
ConstantInt::get(UseStateMachineC->getType(), false));
4336+
ConstantInt::get(UseStateMachineC->getIntegerType(), false));
43364337

43374338
// If we don't actually need a state machine we are done here. This can
43384339
// happen if there simply are no parallel regions. In the resulting kernel
@@ -4658,7 +4659,7 @@ struct AAKernelInfoFunction : AAKernelInfo {
46584659
KernelInfo::getMayUseNestedParallelismFromKernelEnvironment(
46594660
AA.KernelEnvC);
46604661
ConstantInt *NewMayUseNestedParallelismC = ConstantInt::get(
4661-
MayUseNestedParallelismC->getType(), AA.NestedParallelism);
4662+
MayUseNestedParallelismC->getIntegerType(), AA.NestedParallelism);
46624663
AA.setMayUseNestedParallelismOfKernelEnvironment(
46634664
NewMayUseNestedParallelismC);
46644665
}

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ static APInt findDemandedEltsByAllUsers(Value *V) {
388388
/// arbitrarily pick 64 bit as our canonical type. The actual bitwidth doesn't
389389
/// matter, we just want a consistent type to simplify CSE.
390390
static ConstantInt *getPreferredVectorIndex(ConstantInt *IndexC) {
391-
const unsigned IndexBW = IndexC->getType()->getBitWidth();
391+
const unsigned IndexBW = IndexC->getBitWidth();
392392
if (IndexBW == 64 || IndexC->getValue().getActiveBits() > 64)
393393
return nullptr;
394394
return ConstantInt::get(IndexC->getContext(),
@@ -2640,7 +2640,7 @@ static Instruction *foldShuffleWithInsert(ShuffleVectorInst &Shuf,
26402640
assert(NewInsIndex != -1 && "Did not fold shuffle with unused operand?");
26412641

26422642
// Index is updated to the potentially translated insertion lane.
2643-
IndexC = ConstantInt::get(IndexC->getType(), NewInsIndex);
2643+
IndexC = ConstantInt::get(IndexC->getIntegerType(), NewInsIndex);
26442644
return true;
26452645
};
26462646

llvm/lib/Transforms/Scalar/ConstantHoisting.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,7 @@ void ConstantHoistingPass::findBaseConstants(GlobalVariable *BaseGV) {
674674
llvm::stable_sort(ConstCandVec, [](const ConstantCandidate &LHS,
675675
const ConstantCandidate &RHS) {
676676
if (LHS.ConstInt->getType() != RHS.ConstInt->getType())
677-
return LHS.ConstInt->getType()->getBitWidth() <
678-
RHS.ConstInt->getType()->getBitWidth();
677+
return LHS.ConstInt->getBitWidth() < RHS.ConstInt->getBitWidth();
679678
return LHS.ConstInt->getValue().ult(RHS.ConstInt->getValue());
680679
});
681680

@@ -890,7 +889,7 @@ bool ConstantHoistingPass::emitBaseConstants(GlobalVariable *BaseGV) {
890889
Type *Ty = ConstInfo.BaseExpr->getType();
891890
Base = new BitCastInst(ConstInfo.BaseExpr, Ty, "const", IP);
892891
} else {
893-
IntegerType *Ty = ConstInfo.BaseInt->getType();
892+
IntegerType *Ty = ConstInfo.BaseInt->getIntegerType();
894893
Base = new BitCastInst(ConstInfo.BaseInt, Ty, "const", IP);
895894
}
896895

llvm/lib/Transforms/Scalar/LoopFlatten.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,8 @@ static bool verifyTripCount(Value *RHS, Loop *L,
343343
// If the RHS of the compare is equal to the backedge taken count we need
344344
// to add one to get the trip count.
345345
if (SCEVRHS == BackedgeTCExt || SCEVRHS == BackedgeTakenCount) {
346-
ConstantInt *One = ConstantInt::get(ConstantRHS->getType(), 1);
347-
Value *NewRHS = ConstantInt::get(
348-
ConstantRHS->getContext(), ConstantRHS->getValue() + One->getValue());
346+
Value *NewRHS = ConstantInt::get(ConstantRHS->getContext(),
347+
ConstantRHS->getValue() + 1);
349348
return setLoopComponents(NewRHS, TripCount, Increment,
350349
IterationInstructions);
351350
}

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6293,7 +6293,7 @@ Value *SwitchLookupTable::BuildLookup(Value *Index, IRBuilder<> &Builder) {
62936293
}
62946294
case BitMapKind: {
62956295
// Type of the bitmap (e.g. i59).
6296-
IntegerType *MapTy = BitMap->getType();
6296+
IntegerType *MapTy = BitMap->getIntegerType();
62976297

62986298
// Cast Index to the same type as the bitmap.
62996299
// Note: The Index is <= the number of elements in the table, so
@@ -6668,7 +6668,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
66686668
Value *TableIndex;
66696669
ConstantInt *TableIndexOffset;
66706670
if (UseSwitchConditionAsTableIndex) {
6671-
TableIndexOffset = ConstantInt::get(MaxCaseVal->getType(), 0);
6671+
TableIndexOffset = ConstantInt::get(MaxCaseVal->getIntegerType(), 0);
66726672
TableIndex = SI->getCondition();
66736673
} else {
66746674
TableIndexOffset = MinCaseVal;
@@ -6752,7 +6752,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
67526752
// Get the TableIndex'th bit of the bitmask.
67536753
// If this bit is 0 (meaning hole) jump to the default destination,
67546754
// else continue with table lookup.
6755-
IntegerType *MapTy = TableMask->getType();
6755+
IntegerType *MapTy = TableMask->getIntegerType();
67566756
Value *MaskIndex =
67576757
Builder.CreateZExtOrTrunc(TableIndex, MapTy, "switch.maskindex");
67586758
Value *Shifted = Builder.CreateLShr(TableMask, MaskIndex, "switch.shifted");
@@ -6975,7 +6975,7 @@ static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder,
69756975
// Replace each case with its trailing zeros number.
69766976
for (auto &Case : SI->cases()) {
69776977
auto *OrigValue = Case.getCaseValue();
6978-
Case.setValue(ConstantInt::get(OrigValue->getType(),
6978+
Case.setValue(ConstantInt::get(OrigValue->getIntegerType(),
69796979
OrigValue->getValue().countr_zero()));
69806980
}
69816981

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ static TypedAttr getScalarConstantAsAttr(OpBuilder &builder,
720720
// Convert scalar intergers.
721721
if (auto *constInt = dyn_cast<llvm::ConstantInt>(constScalar)) {
722722
return builder.getIntegerAttr(
723-
IntegerType::get(context, constInt->getType()->getBitWidth()),
723+
IntegerType::get(context, constInt->getBitWidth()),
724724
constInt->getValue());
725725
}
726726

0 commit comments

Comments
 (0)