Skip to content

Commit 123758b

Browse files
authored
[IRBuilder] Add versions of createInsertVector/createExtractVector that take a uint64_t index. (llvm#138324)
Most callers want a constant index. Instead of making every caller create a ConstantInt, we can do it in IRBuilder. This is similar to createInsertElement/createExtractElement.
1 parent 105ce58 commit 123758b

File tree

11 files changed

+52
-59
lines changed

11 files changed

+52
-59
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,9 +1372,8 @@ static llvm::Value *CreateCoercedLoad(Address Src, llvm::Type *Ty,
13721372
if (ScalableDstTy->getElementType() == FixedSrcTy->getElementType()) {
13731373
auto *Load = CGF.Builder.CreateLoad(Src);
13741374
auto *PoisonVec = llvm::PoisonValue::get(ScalableDstTy);
1375-
auto *Zero = llvm::Constant::getNullValue(CGF.CGM.Int64Ty);
13761375
llvm::Value *Result = CGF.Builder.CreateInsertVector(
1377-
ScalableDstTy, PoisonVec, Load, Zero, "cast.scalable");
1376+
ScalableDstTy, PoisonVec, Load, uint64_t(0), "cast.scalable");
13781377
if (ScalableDstTy != Ty)
13791378
Result = CGF.Builder.CreateBitCast(Result, Ty);
13801379
return Result;
@@ -1482,10 +1481,8 @@ CoerceScalableToFixed(CodeGenFunction &CGF, llvm::FixedVectorType *ToTy,
14821481
V = CGF.Builder.CreateBitCast(V, FromTy);
14831482
}
14841483
if (FromTy->getElementType() == ToTy->getElementType()) {
1485-
llvm::Value *Zero = llvm::Constant::getNullValue(CGF.CGM.Int64Ty);
1486-
14871484
V->setName(Name + ".coerce");
1488-
V = CGF.Builder.CreateExtractVector(ToTy, V, Zero, "cast.fixed");
1485+
V = CGF.Builder.CreateExtractVector(ToTy, V, uint64_t(0), "cast.fixed");
14891486
return {V, true};
14901487
}
14911488
return {V, false};
@@ -6110,8 +6107,7 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
61106107
dyn_cast<llvm::ScalableVectorType>(V->getType())) {
61116108
if (FixedDstTy->getElementType() ==
61126109
ScalableSrcTy->getElementType()) {
6113-
llvm::Value *Zero = llvm::Constant::getNullValue(CGM.Int64Ty);
6114-
V = Builder.CreateExtractVector(FixedDstTy, V, Zero,
6110+
V = Builder.CreateExtractVector(FixedDstTy, V, uint64_t(0),
61156111
"cast.fixed");
61166112
return RValue::get(V);
61176113
}

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,9 +2487,8 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
24872487
}
24882488
if (FixedSrcTy->getElementType() == ScalableDstTy->getElementType()) {
24892489
llvm::Value *PoisonVec = llvm::PoisonValue::get(ScalableDstTy);
2490-
llvm::Value *Zero = llvm::Constant::getNullValue(CGF.CGM.Int64Ty);
24912490
llvm::Value *Result = Builder.CreateInsertVector(
2492-
ScalableDstTy, PoisonVec, Src, Zero, "cast.scalable");
2491+
ScalableDstTy, PoisonVec, Src, uint64_t(0), "cast.scalable");
24932492
if (Result->getType() != DstTy)
24942493
Result = Builder.CreateBitCast(Result, DstTy);
24952494
return Result;
@@ -2512,10 +2511,9 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
25122511
ScalableSrcTy->getElementCount().getKnownMinValue() / 8);
25132512
Src = Builder.CreateBitCast(Src, ScalableSrcTy);
25142513
}
2515-
if (ScalableSrcTy->getElementType() == FixedDstTy->getElementType()) {
2516-
llvm::Value *Zero = llvm::Constant::getNullValue(CGF.CGM.Int64Ty);
2517-
return Builder.CreateExtractVector(DstTy, Src, Zero, "cast.fixed");
2518-
}
2514+
if (ScalableSrcTy->getElementType() == FixedDstTy->getElementType())
2515+
return Builder.CreateExtractVector(DstTy, Src, uint64_t(0),
2516+
"cast.fixed");
25192517
}
25202518
}
25212519

clang/lib/CodeGen/TargetBuiltins/ARM.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ llvm::Value *CodeGenFunction::EmitFP8NeonFDOTCall(
466466
if (ExtendLaneArg) {
467467
auto *VT = llvm::FixedVectorType::get(Int8Ty, 16);
468468
Ops[2] = Builder.CreateInsertVector(VT, PoisonValue::get(VT), Ops[2],
469-
Builder.getInt64(0));
469+
uint64_t(0));
470470
}
471471
return EmitFP8NeonCall(IID, Tys, Ops, E, name);
472472
}
@@ -478,7 +478,7 @@ llvm::Value *CodeGenFunction::EmitFP8NeonFMLACall(
478478
if (ExtendLaneArg) {
479479
auto *VT = llvm::FixedVectorType::get(Int8Ty, 16);
480480
Ops[2] = Builder.CreateInsertVector(VT, PoisonValue::get(VT), Ops[2],
481-
Builder.getInt64(0));
481+
uint64_t(0));
482482
}
483483
const unsigned ElemCount = Ops[0]->getType()->getPrimitiveSizeInBits() /
484484
RetTy->getPrimitiveSizeInBits();
@@ -502,7 +502,7 @@ Value *CodeGenFunction::EmitFP8NeonCvtCall(unsigned IID, llvm::Type *Ty0,
502502
// Op[0] is mfloat8x16_t, but the intrinsic converts only the lower part of
503503
// the vector.
504504
Tys[1] = llvm::FixedVectorType::get(Int8Ty, 8);
505-
Ops[0] = Builder.CreateExtractVector(Tys[1], Ops[0], Builder.getInt64(0));
505+
Ops[0] = Builder.CreateExtractVector(Tys[1], Ops[0], uint64_t(0));
506506
}
507507
return EmitFP8NeonCall(IID, Tys, Ops, E, name);
508508
}
@@ -4727,7 +4727,7 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID,
47274727

47284728
llvm::Type *OverloadedTy = getSVEVectorForElementType(EltTy);
47294729
Value *InsertSubVec = Builder.CreateInsertVector(
4730-
OverloadedTy, PoisonValue::get(OverloadedTy), Vec, Builder.getInt64(0));
4730+
OverloadedTy, PoisonValue::get(OverloadedTy), Vec, uint64_t(0));
47314731

47324732
Function *F =
47334733
CGM.getIntrinsic(Intrinsic::aarch64_sve_dupq_lane, OverloadedTy);
@@ -4810,7 +4810,7 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID,
48104810
case SVE::BI__builtin_sve_svset_neonq_f32:
48114811
case SVE::BI__builtin_sve_svset_neonq_f64:
48124812
case SVE::BI__builtin_sve_svset_neonq_bf16: {
4813-
return Builder.CreateInsertVector(Ty, Ops[0], Ops[1], Builder.getInt64(0));
4813+
return Builder.CreateInsertVector(Ty, Ops[0], Ops[1], uint64_t(0));
48144814
}
48154815

48164816
case SVE::BI__builtin_sve_svget_neonq_s8:
@@ -4825,7 +4825,7 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID,
48254825
case SVE::BI__builtin_sve_svget_neonq_f32:
48264826
case SVE::BI__builtin_sve_svget_neonq_f64:
48274827
case SVE::BI__builtin_sve_svget_neonq_bf16: {
4828-
return Builder.CreateExtractVector(Ty, Ops[0], Builder.getInt64(0));
4828+
return Builder.CreateExtractVector(Ty, Ops[0], uint64_t(0));
48294829
}
48304830

48314831
case SVE::BI__builtin_sve_svdup_neonq_s8:
@@ -4841,7 +4841,7 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID,
48414841
case SVE::BI__builtin_sve_svdup_neonq_f64:
48424842
case SVE::BI__builtin_sve_svdup_neonq_bf16: {
48434843
Value *Insert = Builder.CreateInsertVector(Ty, PoisonValue::get(Ty), Ops[0],
4844-
Builder.getInt64(0));
4844+
uint64_t(0));
48454845
return Builder.CreateIntrinsic(Intrinsic::aarch64_sve_dupq_lane, {Ty},
48464846
{Insert, Builder.getInt64(0)});
48474847
}
@@ -7767,7 +7767,7 @@ Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned BuiltinID,
77677767
case NEON::BI__builtin_neon_vcvt_high_mf8_f32_fpm: {
77687768
llvm::Type *Ty = llvm::FixedVectorType::get(Int8Ty, 16);
77697769
Ops[0] = Builder.CreateInsertVector(Ty, PoisonValue::get(Ty), Ops[0],
7770-
Builder.getInt64(0));
7770+
uint64_t(0));
77717771
return EmitFP8NeonCvtCall(Intrinsic::aarch64_neon_fp8_fcvtn2, Ty,
77727772
Ops[1]->getType(), false, Ops, E, "vfcvtn2");
77737773
}

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,12 @@ class IRBuilderBase {
11011101
Name);
11021102
}
11031103

1104+
/// Create a call to the vector.extract intrinsic.
1105+
CallInst *CreateExtractVector(Type *DstType, Value *SrcVec, uint64_t Idx,
1106+
const Twine &Name = "") {
1107+
return CreateExtractVector(DstType, SrcVec, getInt64(Idx), Name);
1108+
}
1109+
11041110
/// Create a call to the vector.insert intrinsic.
11051111
CallInst *CreateInsertVector(Type *DstType, Value *SrcVec, Value *SubVec,
11061112
Value *Idx, const Twine &Name = "") {
@@ -1109,6 +1115,12 @@ class IRBuilderBase {
11091115
nullptr, Name);
11101116
}
11111117

1118+
/// Create a call to the vector.extract intrinsic.
1119+
CallInst *CreateInsertVector(Type *DstType, Value *SrcVec, Value *SubVec,
1120+
uint64_t Idx, const Twine &Name = "") {
1121+
return CreateInsertVector(DstType, SrcVec, SubVec, getInt64(Idx), Name);
1122+
}
1123+
11121124
/// Create a call to llvm.stacksave
11131125
CallInst *CreateStackSave(const Twine &Name = "") {
11141126
const DataLayout &DL = BB->getDataLayout();

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4554,9 +4554,8 @@ void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
45544554
Value *NewLdCall = Builder.CreateCall(NewFn, Args);
45554555
Value *Ret = llvm::PoisonValue::get(RetTy);
45564556
for (unsigned I = 0; I < N; I++) {
4557-
Value *Idx = ConstantInt::get(Type::getInt64Ty(C), I * MinElts);
45584557
Value *SRet = Builder.CreateExtractValue(NewLdCall, I);
4559-
Ret = Builder.CreateInsertVector(RetTy, Ret, SRet, Idx);
4558+
Ret = Builder.CreateInsertVector(RetTy, Ret, SRet, I * MinElts);
45604559
}
45614560
NewCall = dyn_cast<CallInst>(Ret);
45624561
break;
@@ -4611,9 +4610,8 @@ void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
46114610
Value *Ret = llvm::PoisonValue::get(RetTy);
46124611
unsigned MinElts = RetTy->getMinNumElements() / N;
46134612
for (unsigned I = 0; I < N; I++) {
4614-
Value *Idx = ConstantInt::get(Type::getInt64Ty(C), I * MinElts);
46154613
Value *V = CI->getArgOperand(I);
4616-
Ret = Builder.CreateInsertVector(RetTy, Ret, V, Idx);
4614+
Ret = Builder.CreateInsertVector(RetTy, Ret, V, I * MinElts);
46174615
}
46184616
NewCall = dyn_cast<CallInst>(Ret);
46194617
}

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17227,9 +17227,7 @@ bool AArch64TargetLowering::lowerInterleavedLoad(
1722717227
Value *SubVec = Builder.CreateExtractValue(LdN, Index);
1722817228

1722917229
if (UseScalable)
17230-
SubVec = Builder.CreateExtractVector(
17231-
FVTy, SubVec,
17232-
ConstantInt::get(Type::getInt64Ty(VTy->getContext()), 0));
17230+
SubVec = Builder.CreateExtractVector(FVTy, SubVec, uint64_t(0));
1723317231

1723417232
// Convert the integer vector to pointer vector if the element is pointer.
1723517233
if (EltTy->isPointerTy())
@@ -17436,9 +17434,8 @@ bool AArch64TargetLowering::lowerInterleavedStore(StoreInst *SI,
1743617434
}
1743717435

1743817436
if (UseScalable)
17439-
Shuffle = Builder.CreateInsertVector(
17440-
STVTy, PoisonValue::get(STVTy), Shuffle,
17441-
ConstantInt::get(Type::getInt64Ty(STVTy->getContext()), 0));
17437+
Shuffle = Builder.CreateInsertVector(STVTy, PoisonValue::get(STVTy),
17438+
Shuffle, uint64_t(0));
1744217439

1744317440
Ops.push_back(Shuffle);
1744417441
}
@@ -30020,31 +30017,26 @@ Value *AArch64TargetLowering::createComplexDeinterleavingIR(
3002030017
.getKnownMinValue() /
3002130018
2;
3002230019
auto *HalfTy = VectorType::getHalfElementsVectorType(Ty);
30023-
auto *LowerSplitA = B.CreateExtractVector(HalfTy, InputA, B.getInt64(0));
30024-
auto *LowerSplitB = B.CreateExtractVector(HalfTy, InputB, B.getInt64(0));
30025-
auto *UpperSplitA =
30026-
B.CreateExtractVector(HalfTy, InputA, B.getInt64(Stride));
30027-
auto *UpperSplitB =
30028-
B.CreateExtractVector(HalfTy, InputB, B.getInt64(Stride));
30020+
auto *LowerSplitA = B.CreateExtractVector(HalfTy, InputA, uint64_t(0));
30021+
auto *LowerSplitB = B.CreateExtractVector(HalfTy, InputB, uint64_t(0));
30022+
auto *UpperSplitA = B.CreateExtractVector(HalfTy, InputA, Stride);
30023+
auto *UpperSplitB = B.CreateExtractVector(HalfTy, InputB, Stride);
3002930024
Value *LowerSplitAcc = nullptr;
3003030025
Value *UpperSplitAcc = nullptr;
3003130026
Type *FullTy = Ty;
3003230027
FullTy = Accumulator->getType();
3003330028
auto *HalfAccTy = VectorType::getHalfElementsVectorType(
3003430029
cast<VectorType>(Accumulator->getType()));
30035-
LowerSplitAcc =
30036-
B.CreateExtractVector(HalfAccTy, Accumulator, B.getInt64(0));
30037-
UpperSplitAcc =
30038-
B.CreateExtractVector(HalfAccTy, Accumulator, B.getInt64(AccStride));
30030+
LowerSplitAcc = B.CreateExtractVector(HalfAccTy, Accumulator, uint64_t(0));
30031+
UpperSplitAcc = B.CreateExtractVector(HalfAccTy, Accumulator, AccStride);
3003930032
auto *LowerSplitInt = createComplexDeinterleavingIR(
3004030033
B, OperationType, Rotation, LowerSplitA, LowerSplitB, LowerSplitAcc);
3004130034
auto *UpperSplitInt = createComplexDeinterleavingIR(
3004230035
B, OperationType, Rotation, UpperSplitA, UpperSplitB, UpperSplitAcc);
3004330036

3004430037
auto *Result = B.CreateInsertVector(FullTy, PoisonValue::get(FullTy),
30045-
LowerSplitInt, B.getInt64(0));
30046-
return B.CreateInsertVector(FullTy, Result, UpperSplitInt,
30047-
B.getInt64(AccStride));
30038+
LowerSplitInt, uint64_t(0));
30039+
return B.CreateInsertVector(FullTy, Result, UpperSplitInt, AccStride);
3004830040
}
3004930041

3005030042
if (OperationType == ComplexDeinterleavingOperation::CMulPartial) {

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,9 +2406,9 @@ static std::optional<Instruction *> instCombineSVEUzp1(InstCombiner &IC,
24062406
if (TyA == B->getType() &&
24072407
RetTy == ScalableVectorType::getDoubleElementsVectorType(TyA)) {
24082408
auto *SubVec = IC.Builder.CreateInsertVector(
2409-
RetTy, PoisonValue::get(RetTy), A, IC.Builder.getInt64(0));
2410-
auto *ConcatVec = IC.Builder.CreateInsertVector(
2411-
RetTy, SubVec, B, IC.Builder.getInt64(TyA->getMinNumElements()));
2409+
RetTy, PoisonValue::get(RetTy), A, uint64_t(0));
2410+
auto *ConcatVec = IC.Builder.CreateInsertVector(RetTy, SubVec, B,
2411+
TyA->getMinNumElements());
24122412
ConcatVec->takeName(&II);
24132413
return IC.replaceInstUsesWith(II, ConcatVec);
24142414
}
@@ -2602,9 +2602,9 @@ static std::optional<Instruction *> instCombineSVEDupqLane(InstCombiner &IC,
26022602
auto *WideShuffleMaskTy =
26032603
ScalableVectorType::get(IC.Builder.getInt32Ty(), PatternElementCount);
26042604

2605-
auto ZeroIdx = ConstantInt::get(IC.Builder.getInt64Ty(), APInt(64, 0));
26062605
auto InsertSubvector = IC.Builder.CreateInsertVector(
2607-
II.getType(), PoisonValue::get(II.getType()), InsertEltChain, ZeroIdx);
2606+
II.getType(), PoisonValue::get(II.getType()), InsertEltChain,
2607+
uint64_t(0));
26082608
auto WideBitcast =
26092609
IC.Builder.CreateBitOrPointerCast(InsertSubvector, WideScalableTy);
26102610
auto WideShuffleMask = ConstantAggregateZero::get(WideShuffleMaskTy);

llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,8 +2102,7 @@ bool AMDGPUCodeGenPrepareImpl::visitPHINode(PHINode &I) {
21022102
for (VectorSlice &S : Slices) {
21032103
const auto ValName = "largephi.insertslice" + std::to_string(NameSuffix++);
21042104
if (S.NumElts > 1)
2105-
Vec =
2106-
B.CreateInsertVector(FVT, Vec, S.NewPHI, B.getInt64(S.Idx), ValName);
2105+
Vec = B.CreateInsertVector(FVT, Vec, S.NewPHI, S.Idx, ValName);
21072106
else
21082107
Vec = B.CreateInsertElement(Vec, S.NewPHI, S.Idx, ValName);
21092108
}

llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,14 +1440,14 @@ GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
14401440
if (Src0Ty->getNumElements() > Src0NumElts) {
14411441
Src0 = IC.Builder.CreateExtractVector(
14421442
FixedVectorType::get(Src0Ty->getElementType(), Src0NumElts), Src0,
1443-
IC.Builder.getInt64(0));
1443+
uint64_t(0));
14441444
MadeChange = true;
14451445
}
14461446

14471447
if (Src1Ty->getNumElements() > Src1NumElts) {
14481448
Src1 = IC.Builder.CreateExtractVector(
14491449
FixedVectorType::get(Src1Ty->getElementType(), Src1NumElts), Src1,
1450-
IC.Builder.getInt64(0));
1450+
uint64_t(0));
14511451
MadeChange = true;
14521452
}
14531453

llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,9 +1322,8 @@ Value *LoopIdiomVectorize::expandFindFirstByte(
13221322
Needle0, "needle0");
13231323
LoadNeedle = Builder.CreateSelect(PredNeedle, LoadNeedle, Needle0Splat,
13241324
"needle_splat");
1325-
LoadNeedle =
1326-
Builder.CreateExtractVector(FixedVectorType::get(CharTy, VF), LoadNeedle,
1327-
ConstantInt::get(I64Ty, 0), "needle_vec");
1325+
LoadNeedle = Builder.CreateExtractVector(
1326+
FixedVectorType::get(CharTy, VF), LoadNeedle, uint64_t(0), "needle_vec");
13281327

13291328
// (2.c) Test if there's a match.
13301329
Value *MatchPred = Builder.CreateIntrinsic(

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5787,8 +5787,7 @@ static Value *createInsertVector(
57875787
function_ref<Value *(Value *, Value *, ArrayRef<int>)> Generator = {}) {
57885788
const unsigned SubVecVF = getNumElements(V->getType());
57895789
if (Index % SubVecVF == 0) {
5790-
Vec = Builder.CreateInsertVector(Vec->getType(), Vec, V,
5791-
Builder.getInt64(Index));
5790+
Vec = Builder.CreateInsertVector(Vec->getType(), Vec, V, Index);
57925791
} else {
57935792
// Create shuffle, insertvector requires that index is multiple of
57945793
// the subvector length.
@@ -5818,7 +5817,7 @@ static Value *createExtractVector(IRBuilderBase &Builder, Value *Vec,
58185817
if (Index % SubVecVF == 0) {
58195818
VectorType *SubVecTy =
58205819
getWidenedType(Vec->getType()->getScalarType(), SubVecVF);
5821-
return Builder.CreateExtractVector(SubVecTy, Vec, Builder.getInt64(Index));
5820+
return Builder.CreateExtractVector(SubVecTy, Vec, Index);
58225821
}
58235822
// Create shuffle, extract_subvector requires that index is multiple of
58245823
// the subvector length.

0 commit comments

Comments
 (0)