@@ -1599,14 +1599,22 @@ class LoadableByAddress : public SILModuleTransform {
1599
1599
1600
1600
private:
1601
1601
void updateLoweredTypes (SILFunction *F);
1602
- void recreateSingleApply (SILInstruction *applyInst);
1603
- bool recreateApply (SILInstruction &I);
1604
- bool recreateConvInstr (SILInstruction &I);
1605
- bool recreateBuiltinInstr (SILInstruction &I);
1606
- bool recreateLoadInstr (SILInstruction &I);
1607
- bool recreateUncheckedEnumDataInstr (SILInstruction &I);
1608
- bool recreateUncheckedTakeEnumDataAddrInst (SILInstruction &I);
1609
- bool fixStoreToBlockStorageInstr (SILInstruction &I);
1602
+ void recreateSingleApply (SILInstruction *applyInst,
1603
+ SmallVectorImpl<SILInstruction *> &Delete);
1604
+ bool recreateApply (SILInstruction &I,
1605
+ SmallVectorImpl<SILInstruction *> &Delete);
1606
+ bool recreateConvInstr (SILInstruction &I,
1607
+ SmallVectorImpl<SILInstruction *> &Delete);
1608
+ bool recreateBuiltinInstr (SILInstruction &I,
1609
+ SmallVectorImpl<SILInstruction *> &Delete);
1610
+ bool recreateLoadInstr (SILInstruction &I,
1611
+ SmallVectorImpl<SILInstruction *> &Delete);
1612
+ bool recreateUncheckedEnumDataInstr (SILInstruction &I,
1613
+ SmallVectorImpl<SILInstruction *> &Delete);
1614
+ bool recreateUncheckedTakeEnumDataAddrInst (SILInstruction &I,
1615
+ SmallVectorImpl<SILInstruction *> &Delete);
1616
+ bool fixStoreToBlockStorageInstr (SILInstruction &I,
1617
+ SmallVectorImpl<SILInstruction *> &Delete);
1610
1618
1611
1619
private:
1612
1620
llvm::SetVector<SILFunction *> modFuncs;
@@ -2369,7 +2377,8 @@ getOperandTypeWithCastIfNecessary(SILInstruction *containingInstr, SILValue op,
2369
2377
return op;
2370
2378
}
2371
2379
2372
- void LoadableByAddress::recreateSingleApply (SILInstruction *applyInst) {
2380
+ void LoadableByAddress::recreateSingleApply (
2381
+ SILInstruction *applyInst, SmallVectorImpl<SILInstruction *> &Delete) {
2373
2382
auto *F = applyInst->getFunction ();
2374
2383
IRGenModule *currIRMod = getIRGenModule ()->IRGen .getGenModule (F);
2375
2384
// Collect common info
@@ -2380,7 +2389,7 @@ void LoadableByAddress::recreateSingleApply(SILInstruction *applyInst) {
2380
2389
// else verification will fail with wrong SubstCalleeType
2381
2390
auto calleInstr = site.getInstruction ();
2382
2391
if (modApplies.remove (calleInstr)) {
2383
- recreateSingleApply (calleInstr);
2392
+ recreateSingleApply (calleInstr, Delete );
2384
2393
callee = applySite.getCallee ();
2385
2394
}
2386
2395
}
@@ -2490,18 +2499,20 @@ void LoadableByAddress::recreateSingleApply(SILInstruction *applyInst) {
2490
2499
default :
2491
2500
llvm_unreachable (" Unexpected instr: unknown apply type" );
2492
2501
}
2493
- applyInst-> getParent ()-> erase (applyInst);
2502
+ Delete. push_back (applyInst);
2494
2503
}
2495
2504
2496
- bool LoadableByAddress::recreateApply (SILInstruction &I) {
2505
+ bool LoadableByAddress::recreateApply (
2506
+ SILInstruction &I, SmallVectorImpl<SILInstruction *> &Delete) {
2497
2507
if (!modApplies.count (&I))
2498
2508
return false ;
2499
- recreateSingleApply (&I);
2509
+ recreateSingleApply (&I, Delete );
2500
2510
modApplies.remove (&I);
2501
2511
return true ;
2502
2512
}
2503
2513
2504
- bool LoadableByAddress::recreateLoadInstr (SILInstruction &I) {
2514
+ bool LoadableByAddress::recreateLoadInstr (
2515
+ SILInstruction &I, SmallVectorImpl<SILInstruction *> &Delete) {
2505
2516
auto *loadInstr = dyn_cast<LoadInst>(&I);
2506
2517
if (!loadInstr || !loadInstrsOfFunc.count (loadInstr))
2507
2518
return false ;
@@ -2515,11 +2526,12 @@ bool LoadableByAddress::recreateLoadInstr(SILInstruction &I) {
2515
2526
auto *newInstr = loadBuilder.createLoad (loadInstr->getLoc (), loadOp,
2516
2527
loadInstr->getOwnershipQualifier ());
2517
2528
loadInstr->replaceAllUsesWith (newInstr);
2518
- loadInstr-> getParent ()-> erase (loadInstr);
2529
+ Delete. push_back (loadInstr);
2519
2530
return true ;
2520
2531
}
2521
2532
2522
- bool LoadableByAddress::recreateUncheckedEnumDataInstr (SILInstruction &I) {
2533
+ bool LoadableByAddress::recreateUncheckedEnumDataInstr (
2534
+ SILInstruction &I, SmallVectorImpl<SILInstruction *> &Delete) {
2523
2535
auto enumInstr = dyn_cast<UncheckedEnumDataInst>(&I);
2524
2536
if (!enumInstr || !uncheckedEnumDataOfFunc.count (enumInstr))
2525
2537
return false ;
@@ -2547,12 +2559,12 @@ bool LoadableByAddress::recreateUncheckedEnumDataInstr(SILInstruction &I) {
2547
2559
newType);
2548
2560
}
2549
2561
enumInstr->replaceAllUsesWith (newInstr);
2550
- enumInstr-> getParent ()-> erase (enumInstr);
2562
+ Delete. push_back (enumInstr);
2551
2563
return false ;
2552
2564
}
2553
2565
2554
2566
bool LoadableByAddress::recreateUncheckedTakeEnumDataAddrInst (
2555
- SILInstruction &I) {
2567
+ SILInstruction &I, SmallVectorImpl<SILInstruction *> &Delete ) {
2556
2568
auto *enumInstr = dyn_cast<UncheckedTakeEnumDataAddrInst>(&I);
2557
2569
if (!enumInstr || !uncheckedTakeEnumDataAddrOfFunc.count (enumInstr))
2558
2570
return false ;
@@ -2577,11 +2589,12 @@ bool LoadableByAddress::recreateUncheckedTakeEnumDataAddrInst(
2577
2589
newType.getAddressType ());
2578
2590
}
2579
2591
enumInstr->replaceAllUsesWith (newInstr);
2580
- enumInstr-> getParent ()-> erase (enumInstr);
2592
+ Delete. push_back (enumInstr);
2581
2593
return true ;
2582
2594
}
2583
2595
2584
- bool LoadableByAddress::fixStoreToBlockStorageInstr (SILInstruction &I) {
2596
+ bool LoadableByAddress::fixStoreToBlockStorageInstr (
2597
+ SILInstruction &I, SmallVectorImpl<SILInstruction *> &Delete) {
2585
2598
auto *instr = dyn_cast<StoreInst>(&I);
2586
2599
if (!instr || !storeToBlockStorageInstrs.count (instr))
2587
2600
return false ;
@@ -2600,7 +2613,8 @@ bool LoadableByAddress::fixStoreToBlockStorageInstr(SILInstruction &I) {
2600
2613
return true ;
2601
2614
}
2602
2615
2603
- bool LoadableByAddress::recreateConvInstr (SILInstruction &I) {
2616
+ bool LoadableByAddress::recreateConvInstr (SILInstruction &I,
2617
+ SmallVectorImpl<SILInstruction *> &Delete) {
2604
2618
auto *convInstr = dyn_cast<SingleValueInstruction>(&I);
2605
2619
if (!convInstr || !conversionInstrs.count (convInstr))
2606
2620
return false ;
@@ -2657,11 +2671,12 @@ bool LoadableByAddress::recreateConvInstr(SILInstruction &I) {
2657
2671
llvm_unreachable (" Unexpected conversion instruction" );
2658
2672
}
2659
2673
convInstr->replaceAllUsesWith (newInstr);
2660
- convInstr-> getParent ()-> erase (convInstr);
2674
+ Delete. push_back (convInstr);
2661
2675
return true ;
2662
2676
}
2663
2677
2664
- bool LoadableByAddress::recreateBuiltinInstr (SILInstruction &I) {
2678
+ bool LoadableByAddress::recreateBuiltinInstr (SILInstruction &I,
2679
+ SmallVectorImpl<SILInstruction *> &Delete) {
2665
2680
auto builtinInstr = dyn_cast<BuiltinInst>(&I);
2666
2681
if (!builtinInstr || !builtinInstrs.count (builtinInstr))
2667
2682
return false ;
@@ -2682,7 +2697,7 @@ bool LoadableByAddress::recreateBuiltinInstr(SILInstruction &I) {
2682
2697
builtinInstr->getLoc (), builtinInstr->getName (), newResultTy,
2683
2698
builtinInstr->getSubstitutions (), newArgs);
2684
2699
builtinInstr->replaceAllUsesWith (newInstr);
2685
- builtinInstr-> getParent ()-> erase (builtinInstr);
2700
+ Delete. push_back (builtinInstr);
2686
2701
return true ;
2687
2702
}
2688
2703
@@ -2833,29 +2848,28 @@ void LoadableByAddress::run() {
2833
2848
// Recreate the instructions in topological order. Some instructions inherit
2834
2849
// their result type from their operand.
2835
2850
for (SILFunction &CurrF : *getModule ()) {
2851
+ SmallVector<SILInstruction *, 32 > Delete;
2836
2852
for (SILBasicBlock &BB : CurrF) {
2837
- SmallVector<SILInstruction *, 32 > InstInBlock;
2838
- for (auto &Inst : BB)
2839
- InstInBlock.push_back (&Inst);
2840
- for (SILInstruction *inst : InstInBlock) {
2841
- SILInstruction &I = *inst;
2842
- if (recreateConvInstr (I))
2853
+ for (SILInstruction &I : BB) {
2854
+ if (recreateConvInstr (I, Delete))
2843
2855
continue ;
2844
- else if (recreateBuiltinInstr (I))
2856
+ else if (recreateBuiltinInstr (I, Delete ))
2845
2857
continue ;
2846
- else if (recreateUncheckedEnumDataInstr (I))
2858
+ else if (recreateUncheckedEnumDataInstr (I, Delete ))
2847
2859
continue ;
2848
- else if (recreateUncheckedTakeEnumDataAddrInst (I))
2860
+ else if (recreateUncheckedTakeEnumDataAddrInst (I, Delete ))
2849
2861
continue ;
2850
- else if (recreateLoadInstr (I))
2862
+ else if (recreateLoadInstr (I, Delete ))
2851
2863
continue ;
2852
- else if (recreateApply (I))
2864
+ else if (recreateApply (I, Delete ))
2853
2865
continue ;
2854
2866
else
2855
- fixStoreToBlockStorageInstr (I);
2867
+ fixStoreToBlockStorageInstr (I, Delete );
2856
2868
}
2857
2869
}
2858
- }
2870
+ for (auto *Inst : Delete)
2871
+ Inst->eraseFromParent ();
2872
+ }
2859
2873
2860
2874
// Clean up the data structs:
2861
2875
modFuncs.clear ();
0 commit comments