Skip to content

Commit 003ac23

Browse files
committed
[SROA] Reduce the number of times a IRBuilder is constructed (NFC).
This patch reduces the number of times IRBuilders need to be constructed in SROA.cpp by passing existing ones by reference to the appropriate places.
1 parent 55d96ac commit 003ac23

File tree

1 file changed

+48
-41
lines changed

1 file changed

+48
-41
lines changed

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,14 +1264,14 @@ static bool isSafePHIToSpeculate(PHINode &PN) {
12641264
return true;
12651265
}
12661266

1267-
static void speculatePHINodeLoads(PHINode &PN) {
1267+
static void speculatePHINodeLoads(IRBuilderTy &IRB, PHINode &PN) {
12681268
LLVM_DEBUG(dbgs() << " original: " << PN << "\n");
12691269

12701270
LoadInst *SomeLoad = cast<LoadInst>(PN.user_back());
12711271
Type *LoadTy = SomeLoad->getType();
1272-
IRBuilderTy PHIBuilder(&PN);
1273-
PHINode *NewPN = PHIBuilder.CreatePHI(LoadTy, PN.getNumIncomingValues(),
1274-
PN.getName() + ".sroa.speculated");
1272+
IRB.SetInsertPoint(&PN);
1273+
PHINode *NewPN = IRB.CreatePHI(LoadTy, PN.getNumIncomingValues(),
1274+
PN.getName() + ".sroa.speculated");
12751275

12761276
// Get the AA tags and alignment to use from one of the loads. It does not
12771277
// matter which one we get and if any differ.
@@ -1301,9 +1301,9 @@ static void speculatePHINodeLoads(PHINode &PN) {
13011301
}
13021302

13031303
Instruction *TI = Pred->getTerminator();
1304-
IRBuilderTy PredBuilder(TI);
1304+
IRB.SetInsertPoint(TI);
13051305

1306-
LoadInst *Load = PredBuilder.CreateAlignedLoad(
1306+
LoadInst *Load = IRB.CreateAlignedLoad(
13071307
LoadTy, InVal, Alignment,
13081308
(PN.getName() + ".sroa.speculate.load." + Pred->getName()));
13091309
++NumLoadsSpeculated;
@@ -1361,10 +1361,10 @@ static bool isSafeSelectToSpeculate(SelectInst &SI) {
13611361
return true;
13621362
}
13631363

1364-
static void speculateSelectInstLoads(SelectInst &SI) {
1364+
static void speculateSelectInstLoads(IRBuilderTy &IRB, SelectInst &SI) {
13651365
LLVM_DEBUG(dbgs() << " original: " << SI << "\n");
13661366

1367-
IRBuilderTy IRB(&SI);
1367+
IRB.SetInsertPoint(&SI);
13681368
Value *TV = SI.getTrueValue();
13691369
Value *FV = SI.getFalseValue();
13701370
// Replace the loads of the select with a select of two loads.
@@ -3223,8 +3223,11 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
32233223
/// Used to calculate offsets, and hence alignment, of subobjects.
32243224
const DataLayout &DL;
32253225

3226+
IRBuilderTy &IRB;
3227+
32263228
public:
3227-
AggLoadStoreRewriter(const DataLayout &DL) : DL(DL) {}
3229+
AggLoadStoreRewriter(const DataLayout &DL, IRBuilderTy &IRB)
3230+
: DL(DL), IRB(IRB) {}
32283231

32293232
/// Rewrite loads and stores through a pointer and all pointers derived from
32303233
/// it.
@@ -3255,7 +3258,7 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
32553258
template <typename Derived> class OpSplitter {
32563259
protected:
32573260
/// The builder used to form new instructions.
3258-
IRBuilderTy IRB;
3261+
IRBuilderTy &IRB;
32593262

32603263
/// The indices which to be used with insert- or extractvalue to select the
32613264
/// appropriate value within the aggregate.
@@ -3282,9 +3285,11 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
32823285
/// Initialize the splitter with an insertion point, Ptr and start with a
32833286
/// single zero GEP index.
32843287
OpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy,
3285-
Align BaseAlign, const DataLayout &DL)
3286-
: IRB(InsertionPoint), GEPIndices(1, IRB.getInt32(0)), Ptr(Ptr),
3287-
BaseTy(BaseTy), BaseAlign(BaseAlign), DL(DL) {}
3288+
Align BaseAlign, const DataLayout &DL, IRBuilderTy &IRB)
3289+
: IRB(IRB), GEPIndices(1, IRB.getInt32(0)), Ptr(Ptr), BaseTy(BaseTy),
3290+
BaseAlign(BaseAlign), DL(DL) {
3291+
IRB.SetInsertPoint(InsertionPoint);
3292+
}
32883293

32893294
public:
32903295
/// Generic recursive split emission routine.
@@ -3345,9 +3350,10 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
33453350
AAMDNodes AATags;
33463351

33473352
LoadOpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy,
3348-
AAMDNodes AATags, Align BaseAlign, const DataLayout &DL)
3349-
: OpSplitter<LoadOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign,
3350-
DL),
3353+
AAMDNodes AATags, Align BaseAlign, const DataLayout &DL,
3354+
IRBuilderTy &IRB)
3355+
: OpSplitter<LoadOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign, DL,
3356+
IRB),
33513357
AATags(AATags) {}
33523358

33533359
/// Emit a leaf load of a single value. This is called at the leaves of the
@@ -3379,7 +3385,7 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
33793385
// We have an aggregate being loaded, split it apart.
33803386
LLVM_DEBUG(dbgs() << " original: " << LI << "\n");
33813387
LoadOpSplitter Splitter(&LI, *U, LI.getType(), LI.getAAMetadata(),
3382-
getAdjustedAlignment(&LI, 0), DL);
3388+
getAdjustedAlignment(&LI, 0), DL, IRB);
33833389
Value *V = UndefValue::get(LI.getType());
33843390
Splitter.emitSplitOps(LI.getType(), V, LI.getName() + ".fca");
33853391
Visited.erase(&LI);
@@ -3390,9 +3396,10 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
33903396

33913397
struct StoreOpSplitter : public OpSplitter<StoreOpSplitter> {
33923398
StoreOpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy,
3393-
AAMDNodes AATags, Align BaseAlign, const DataLayout &DL)
3399+
AAMDNodes AATags, Align BaseAlign, const DataLayout &DL,
3400+
IRBuilderTy &IRB)
33943401
: OpSplitter<StoreOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign,
3395-
DL),
3402+
DL, IRB),
33963403
AATags(AATags) {}
33973404
AAMDNodes AATags;
33983405
/// Emit a leaf store of a single value. This is called at the leaves of the
@@ -3430,7 +3437,7 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
34303437
// We have an aggregate being stored, split it apart.
34313438
LLVM_DEBUG(dbgs() << " original: " << SI << "\n");
34323439
StoreOpSplitter Splitter(&SI, *U, V->getType(), SI.getAAMetadata(),
3433-
getAdjustedAlignment(&SI, 0), DL);
3440+
getAdjustedAlignment(&SI, 0), DL, IRB);
34343441
Splitter.emitSplitOps(V->getType(), V, V->getName() + ".fca");
34353442
Visited.erase(&SI);
34363443
SI.eraseFromParent();
@@ -3458,29 +3465,28 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
34583465
<< "\n original: " << *Sel
34593466
<< "\n " << GEPI);
34603467

3461-
IRBuilderTy Builder(&GEPI);
3468+
IRB.SetInsertPoint(&GEPI);
34623469
SmallVector<Value *, 4> Index(GEPI.indices());
34633470
bool IsInBounds = GEPI.isInBounds();
34643471

34653472
Type *Ty = GEPI.getSourceElementType();
34663473
Value *True = Sel->getTrueValue();
34673474
Value *NTrue =
34683475
IsInBounds
3469-
? Builder.CreateInBoundsGEP(Ty, True, Index,
3470-
True->getName() + ".sroa.gep")
3471-
: Builder.CreateGEP(Ty, True, Index, True->getName() + ".sroa.gep");
3476+
? IRB.CreateInBoundsGEP(Ty, True, Index,
3477+
True->getName() + ".sroa.gep")
3478+
: IRB.CreateGEP(Ty, True, Index, True->getName() + ".sroa.gep");
34723479

34733480
Value *False = Sel->getFalseValue();
34743481

34753482
Value *NFalse =
34763483
IsInBounds
3477-
? Builder.CreateInBoundsGEP(Ty, False, Index,
3478-
False->getName() + ".sroa.gep")
3479-
: Builder.CreateGEP(Ty, False, Index,
3480-
False->getName() + ".sroa.gep");
3484+
? IRB.CreateInBoundsGEP(Ty, False, Index,
3485+
False->getName() + ".sroa.gep")
3486+
: IRB.CreateGEP(Ty, False, Index, False->getName() + ".sroa.gep");
34813487

3482-
Value *NSel = Builder.CreateSelect(Sel->getCondition(), NTrue, NFalse,
3483-
Sel->getName() + ".sroa.sel");
3488+
Value *NSel = IRB.CreateSelect(Sel->getCondition(), NTrue, NFalse,
3489+
Sel->getName() + ".sroa.sel");
34843490
Visited.erase(&GEPI);
34853491
GEPI.replaceAllUsesWith(NSel);
34863492
GEPI.eraseFromParent();
@@ -3517,10 +3523,9 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
35173523

35183524
SmallVector<Value *, 4> Index(GEPI.indices());
35193525
bool IsInBounds = GEPI.isInBounds();
3520-
IRBuilderTy PHIBuilder(GEPI.getParent()->getFirstNonPHI());
3521-
PHINode *NewPN = PHIBuilder.CreatePHI(GEPI.getType(),
3522-
PHI->getNumIncomingValues(),
3523-
PHI->getName() + ".sroa.phi");
3526+
IRB.SetInsertPoint(GEPI.getParent()->getFirstNonPHI());
3527+
PHINode *NewPN = IRB.CreatePHI(GEPI.getType(), PHI->getNumIncomingValues(),
3528+
PHI->getName() + ".sroa.phi");
35243529
for (unsigned I = 0, E = PHI->getNumIncomingValues(); I != E; ++I) {
35253530
BasicBlock *B = PHI->getIncomingBlock(I);
35263531
Value *NewVal = nullptr;
@@ -3530,11 +3535,12 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
35303535
} else {
35313536
Instruction *In = cast<Instruction>(PHI->getIncomingValue(I));
35323537

3533-
IRBuilderTy B(In->getParent(), std::next(In->getIterator()));
3538+
IRB.SetInsertPoint(In->getParent(), std::next(In->getIterator()));
35343539
Type *Ty = GEPI.getSourceElementType();
3535-
NewVal = IsInBounds
3536-
? B.CreateInBoundsGEP(Ty, In, Index, In->getName() + ".sroa.gep")
3537-
: B.CreateGEP(Ty, In, Index, In->getName() + ".sroa.gep");
3540+
NewVal = IsInBounds ? IRB.CreateInBoundsGEP(Ty, In, Index,
3541+
In->getName() + ".sroa.gep")
3542+
: IRB.CreateGEP(Ty, In, Index,
3543+
In->getName() + ".sroa.gep");
35383544
}
35393545
NewPN->addIncoming(NewVal, B);
35403546
}
@@ -4598,7 +4604,8 @@ bool SROAPass::runOnAlloca(AllocaInst &AI) {
45984604

45994605
// First, split any FCA loads and stores touching this alloca to promote
46004606
// better splitting and promotion opportunities.
4601-
AggLoadStoreRewriter AggRewriter(DL);
4607+
IRBuilderTy IRB(&AI);
4608+
AggLoadStoreRewriter AggRewriter(DL, IRB);
46024609
Changed |= AggRewriter.rewrite(AI);
46034610

46044611
// Build the slices using a recursive instruction-visiting builder.
@@ -4633,11 +4640,11 @@ bool SROAPass::runOnAlloca(AllocaInst &AI) {
46334640

46344641
LLVM_DEBUG(dbgs() << " Speculating PHIs\n");
46354642
while (!SpeculatablePHIs.empty())
4636-
speculatePHINodeLoads(*SpeculatablePHIs.pop_back_val());
4643+
speculatePHINodeLoads(IRB, *SpeculatablePHIs.pop_back_val());
46374644

46384645
LLVM_DEBUG(dbgs() << " Speculating Selects\n");
46394646
while (!SpeculatableSelects.empty())
4640-
speculateSelectInstLoads(*SpeculatableSelects.pop_back_val());
4647+
speculateSelectInstLoads(IRB, *SpeculatableSelects.pop_back_val());
46414648

46424649
return Changed;
46434650
}

0 commit comments

Comments
 (0)