@@ -1264,14 +1264,14 @@ static bool isSafePHIToSpeculate(PHINode &PN) {
1264
1264
return true ;
1265
1265
}
1266
1266
1267
- static void speculatePHINodeLoads (PHINode &PN) {
1267
+ static void speculatePHINodeLoads (IRBuilderTy &IRB, PHINode &PN) {
1268
1268
LLVM_DEBUG (dbgs () << " original: " << PN << " \n " );
1269
1269
1270
1270
LoadInst *SomeLoad = cast<LoadInst>(PN.user_back ());
1271
1271
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" );
1275
1275
1276
1276
// Get the AA tags and alignment to use from one of the loads. It does not
1277
1277
// matter which one we get and if any differ.
@@ -1301,9 +1301,9 @@ static void speculatePHINodeLoads(PHINode &PN) {
1301
1301
}
1302
1302
1303
1303
Instruction *TI = Pred->getTerminator ();
1304
- IRBuilderTy PredBuilder (TI);
1304
+ IRB. SetInsertPoint (TI);
1305
1305
1306
- LoadInst *Load = PredBuilder .CreateAlignedLoad (
1306
+ LoadInst *Load = IRB .CreateAlignedLoad (
1307
1307
LoadTy, InVal, Alignment,
1308
1308
(PN.getName () + " .sroa.speculate.load." + Pred->getName ()));
1309
1309
++NumLoadsSpeculated;
@@ -1361,10 +1361,10 @@ static bool isSafeSelectToSpeculate(SelectInst &SI) {
1361
1361
return true ;
1362
1362
}
1363
1363
1364
- static void speculateSelectInstLoads (SelectInst &SI) {
1364
+ static void speculateSelectInstLoads (IRBuilderTy &IRB, SelectInst &SI) {
1365
1365
LLVM_DEBUG (dbgs () << " original: " << SI << " \n " );
1366
1366
1367
- IRBuilderTy IRB (&SI);
1367
+ IRB. SetInsertPoint (&SI);
1368
1368
Value *TV = SI.getTrueValue ();
1369
1369
Value *FV = SI.getFalseValue ();
1370
1370
// Replace the loads of the select with a select of two loads.
@@ -3223,8 +3223,11 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
3223
3223
// / Used to calculate offsets, and hence alignment, of subobjects.
3224
3224
const DataLayout &DL;
3225
3225
3226
+ IRBuilderTy &IRB;
3227
+
3226
3228
public:
3227
- AggLoadStoreRewriter (const DataLayout &DL) : DL(DL) {}
3229
+ AggLoadStoreRewriter (const DataLayout &DL, IRBuilderTy &IRB)
3230
+ : DL(DL), IRB(IRB) {}
3228
3231
3229
3232
// / Rewrite loads and stores through a pointer and all pointers derived from
3230
3233
// / it.
@@ -3255,7 +3258,7 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
3255
3258
template <typename Derived> class OpSplitter {
3256
3259
protected:
3257
3260
// / The builder used to form new instructions.
3258
- IRBuilderTy IRB;
3261
+ IRBuilderTy & IRB;
3259
3262
3260
3263
// / The indices which to be used with insert- or extractvalue to select the
3261
3264
// / appropriate value within the aggregate.
@@ -3282,9 +3285,11 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
3282
3285
// / Initialize the splitter with an insertion point, Ptr and start with a
3283
3286
// / single zero GEP index.
3284
3287
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
+ }
3288
3293
3289
3294
public:
3290
3295
// / Generic recursive split emission routine.
@@ -3345,9 +3350,10 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
3345
3350
AAMDNodes AATags;
3346
3351
3347
3352
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),
3351
3357
AATags (AATags) {}
3352
3358
3353
3359
// / 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> {
3379
3385
// We have an aggregate being loaded, split it apart.
3380
3386
LLVM_DEBUG (dbgs () << " original: " << LI << " \n " );
3381
3387
LoadOpSplitter Splitter (&LI, *U, LI.getType (), LI.getAAMetadata (),
3382
- getAdjustedAlignment (&LI, 0 ), DL);
3388
+ getAdjustedAlignment (&LI, 0 ), DL, IRB );
3383
3389
Value *V = UndefValue::get (LI.getType ());
3384
3390
Splitter.emitSplitOps (LI.getType (), V, LI.getName () + " .fca" );
3385
3391
Visited.erase (&LI);
@@ -3390,9 +3396,10 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
3390
3396
3391
3397
struct StoreOpSplitter : public OpSplitter <StoreOpSplitter> {
3392
3398
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)
3394
3401
: OpSplitter<StoreOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign,
3395
- DL),
3402
+ DL, IRB ),
3396
3403
AATags (AATags) {}
3397
3404
AAMDNodes AATags;
3398
3405
// / 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> {
3430
3437
// We have an aggregate being stored, split it apart.
3431
3438
LLVM_DEBUG (dbgs () << " original: " << SI << " \n " );
3432
3439
StoreOpSplitter Splitter (&SI, *U, V->getType (), SI.getAAMetadata (),
3433
- getAdjustedAlignment (&SI, 0 ), DL);
3440
+ getAdjustedAlignment (&SI, 0 ), DL, IRB );
3434
3441
Splitter.emitSplitOps (V->getType (), V, V->getName () + " .fca" );
3435
3442
Visited.erase (&SI);
3436
3443
SI.eraseFromParent ();
@@ -3458,29 +3465,28 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
3458
3465
<< " \n original: " << *Sel
3459
3466
<< " \n " << GEPI);
3460
3467
3461
- IRBuilderTy Builder (&GEPI);
3468
+ IRB. SetInsertPoint (&GEPI);
3462
3469
SmallVector<Value *, 4 > Index (GEPI.indices ());
3463
3470
bool IsInBounds = GEPI.isInBounds ();
3464
3471
3465
3472
Type *Ty = GEPI.getSourceElementType ();
3466
3473
Value *True = Sel->getTrueValue ();
3467
3474
Value *NTrue =
3468
3475
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" );
3472
3479
3473
3480
Value *False = Sel->getFalseValue ();
3474
3481
3475
3482
Value *NFalse =
3476
3483
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" );
3481
3487
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" );
3484
3490
Visited.erase (&GEPI);
3485
3491
GEPI.replaceAllUsesWith (NSel);
3486
3492
GEPI.eraseFromParent ();
@@ -3517,10 +3523,9 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
3517
3523
3518
3524
SmallVector<Value *, 4 > Index (GEPI.indices ());
3519
3525
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" );
3524
3529
for (unsigned I = 0 , E = PHI->getNumIncomingValues (); I != E; ++I) {
3525
3530
BasicBlock *B = PHI->getIncomingBlock (I);
3526
3531
Value *NewVal = nullptr ;
@@ -3530,11 +3535,12 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
3530
3535
} else {
3531
3536
Instruction *In = cast<Instruction>(PHI->getIncomingValue (I));
3532
3537
3533
- IRBuilderTy B (In->getParent (), std::next (In->getIterator ()));
3538
+ IRB. SetInsertPoint (In->getParent (), std::next (In->getIterator ()));
3534
3539
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" );
3538
3544
}
3539
3545
NewPN->addIncoming (NewVal, B);
3540
3546
}
@@ -4598,7 +4604,8 @@ bool SROAPass::runOnAlloca(AllocaInst &AI) {
4598
4604
4599
4605
// First, split any FCA loads and stores touching this alloca to promote
4600
4606
// better splitting and promotion opportunities.
4601
- AggLoadStoreRewriter AggRewriter (DL);
4607
+ IRBuilderTy IRB (&AI);
4608
+ AggLoadStoreRewriter AggRewriter (DL, IRB);
4602
4609
Changed |= AggRewriter.rewrite (AI);
4603
4610
4604
4611
// Build the slices using a recursive instruction-visiting builder.
@@ -4633,11 +4640,11 @@ bool SROAPass::runOnAlloca(AllocaInst &AI) {
4633
4640
4634
4641
LLVM_DEBUG (dbgs () << " Speculating PHIs\n " );
4635
4642
while (!SpeculatablePHIs.empty ())
4636
- speculatePHINodeLoads (*SpeculatablePHIs.pop_back_val ());
4643
+ speculatePHINodeLoads (IRB, *SpeculatablePHIs.pop_back_val ());
4637
4644
4638
4645
LLVM_DEBUG (dbgs () << " Speculating Selects\n " );
4639
4646
while (!SpeculatableSelects.empty ())
4640
- speculateSelectInstLoads (*SpeculatableSelects.pop_back_val ());
4647
+ speculateSelectInstLoads (IRB, *SpeculatableSelects.pop_back_val ());
4641
4648
4642
4649
return Changed;
4643
4650
}
0 commit comments