@@ -155,6 +155,8 @@ static bool isRLEInertInstruction(SILInstruction *Inst) {
155
155
case SILInstructionKind::EndAccessInst:
156
156
case SILInstructionKind::SetDeallocatingInst:
157
157
case SILInstructionKind::DeallocRefInst:
158
+ case SILInstructionKind::BeginBorrowInst:
159
+ case SILInstructionKind::EndBorrowInst:
158
160
return true ;
159
161
default :
160
162
return false ;
@@ -481,14 +483,17 @@ class RLEContext {
481
483
// / If set, RLE ignores loads from that array type.
482
484
NominalTypeDecl *ArrayType;
483
485
486
+ JointPostDominanceSetComputer &jointPostDomComputer;
487
+
484
488
#ifndef NDEBUG
485
489
SILPrintContext printCtx;
486
490
#endif
487
491
488
492
public:
489
493
RLEContext (SILFunction *F, SILPassManager *PM, AliasAnalysis *AA,
490
494
TypeExpansionAnalysis *TE, PostOrderFunctionInfo *PO,
491
- EpilogueARCFunctionInfo *EAFI, bool disableArrayLoads);
495
+ EpilogueARCFunctionInfo *EAFI, bool disableArrayLoads,
496
+ JointPostDominanceSetComputer &computer);
492
497
493
498
RLEContext (const RLEContext &) = delete ;
494
499
RLEContext (RLEContext &&) = delete ;
@@ -536,6 +541,11 @@ class RLEContext {
536
541
// / Return the BlockState for the basic block this basic block belongs to.
537
542
BlockState &getBlockState (SILBasicBlock *B) { return BBToLocState[B]; }
538
543
544
+ // / Return the initialized jointPostDomComputer
545
+ JointPostDominanceSetComputer *getJointPostDomSetComputer () {
546
+ return &jointPostDomComputer;
547
+ }
548
+
539
549
// / Get the bit representing the LSLocation in the LocationVault.
540
550
unsigned getLocationBit (const LSLocation &L);
541
551
@@ -679,14 +689,17 @@ SILValue BlockState::reduceValuesAtEndOfBlock(RLEContext &Ctx, LSLocation &L) {
679
689
// we do not have a concrete value in the current basic block.
680
690
ValueTableMap &OTM = getForwardValOut ();
681
691
for (unsigned i = 0 ; i < Locs.size (); ++i) {
682
- Values[Locs[i]] = Ctx.getValue (OTM[Ctx.getLocationBit (Locs[i])]);
692
+ auto Val = Ctx.getValue (OTM[Ctx.getLocationBit (Locs[i])]);
693
+ auto AvailVal = makeCopiedValueAvailable (Val.getBase (), BB,
694
+ Ctx.getJointPostDomSetComputer ());
695
+ Values[Locs[i]] = LSValue (AvailVal, Val.getPath ().getValue ());
683
696
}
684
697
685
698
// Second, reduce the available values into a single SILValue we can use to
686
699
// forward.
687
- SILValue TheForwardingValue;
688
- TheForwardingValue =
689
- LSValue::reduce (L, &BB-> getModule (), Values, BB-> getTerminator ());
700
+ SILValue TheForwardingValue =
701
+ LSValue::reduce (L, &BB-> getModule (), Values, BB-> getTerminator (),
702
+ Ctx. getJointPostDomSetComputer ());
690
703
// / Return the forwarding value.
691
704
return TheForwardingValue;
692
705
}
@@ -711,7 +724,9 @@ bool BlockState::setupRLE(RLEContext &Ctx, SILInstruction *I, SILValue Mem) {
711
724
712
725
// Reduce the available values into a single SILValue we can use to forward.
713
726
SILModule *Mod = &I->getModule ();
714
- SILValue TheForwardingValue = LSValue::reduce (L, Mod, Values, I);
727
+ SILValue TheForwardingValue =
728
+ LSValue::reduce (L, Mod, Values, I, Ctx.getJointPostDomSetComputer ());
729
+
715
730
if (!TheForwardingValue)
716
731
return false ;
717
732
@@ -849,11 +864,11 @@ void BlockState::processWrite(RLEContext &Ctx, SILInstruction *I, SILValue Mem,
849
864
return ;
850
865
}
851
866
867
+ auto *Fn = I->getFunction ();
852
868
// Expand the given location and val into individual fields and process
853
869
// them as separate writes.
854
870
LSLocationList Locs;
855
- LSLocation::expand (L, &I->getModule (),
856
- TypeExpansionContext (*I->getFunction ()), Locs,
871
+ LSLocation::expand (L, &I->getModule (), TypeExpansionContext (*Fn), Locs,
857
872
Ctx.getTE ());
858
873
859
874
if (isComputeAvailSetMax (Kind)) {
@@ -873,8 +888,8 @@ void BlockState::processWrite(RLEContext &Ctx, SILInstruction *I, SILValue Mem,
873
888
874
889
// Are we computing available value or performing RLE?
875
890
LSValueList Vals;
876
- LSValue::expand (Val, &I->getModule (), TypeExpansionContext (*I-> getFunction ()) ,
877
- Vals, Ctx.getTE ());
891
+ LSValue::expand (Val, &I->getModule (), TypeExpansionContext (*Fn), Vals ,
892
+ Ctx.getTE ());
878
893
if (isComputeAvailValue (Kind) || isPerformingRLE (Kind)) {
879
894
for (unsigned i = 0 ; i < Locs.size (); ++i) {
880
895
updateForwardSetAndValForWrite (Ctx, Ctx.getLocationBit (Locs[i]),
@@ -903,11 +918,11 @@ void BlockState::processRead(RLEContext &Ctx, SILInstruction *I, SILValue Mem,
903
918
if (!L.isValid ())
904
919
return ;
905
920
921
+ auto *Fn = I->getFunction ();
906
922
// Expand the given LSLocation and Val into individual fields and process
907
923
// them as separate reads.
908
924
LSLocationList Locs;
909
- LSLocation::expand (L, &I->getModule (),
910
- TypeExpansionContext (*I->getFunction ()), Locs,
925
+ LSLocation::expand (L, &I->getModule (), TypeExpansionContext (*Fn), Locs,
911
926
Ctx.getTE ());
912
927
913
928
if (isComputeAvailSetMax (Kind)) {
@@ -928,8 +943,8 @@ void BlockState::processRead(RLEContext &Ctx, SILInstruction *I, SILValue Mem,
928
943
// Are we computing available values ?.
929
944
bool CanForward = true ;
930
945
LSValueList Vals;
931
- LSValue::expand (Val, &I->getModule (), TypeExpansionContext (*I-> getFunction ()) ,
932
- Vals, Ctx.getTE ());
946
+ LSValue::expand (Val, &I->getModule (), TypeExpansionContext (*Fn), Vals ,
947
+ Ctx.getTE ());
933
948
if (isComputeAvailValue (Kind) || isPerformingRLE (Kind)) {
934
949
for (unsigned i = 0 ; i < Locs.size (); ++i) {
935
950
if (isTrackingLocation (ForwardSetIn, Ctx.getLocationBit (Locs[i])))
@@ -1194,10 +1209,13 @@ void BlockState::dump(RLEContext &Ctx) {
1194
1209
1195
1210
RLEContext::RLEContext (SILFunction *F, SILPassManager *PM, AliasAnalysis *AA,
1196
1211
TypeExpansionAnalysis *TE, PostOrderFunctionInfo *PO,
1197
- EpilogueARCFunctionInfo *EAFI, bool disableArrayLoads)
1212
+ EpilogueARCFunctionInfo *EAFI, bool disableArrayLoads,
1213
+ JointPostDominanceSetComputer &computer)
1198
1214
: Fn(F), PM(PM), AA(AA), TE(TE), PO(PO), EAFI(EAFI),
1199
- ArrayType(disableArrayLoads ?
1200
- F->getModule ().getASTContext().getArrayDecl() : nullptr)
1215
+ ArrayType(disableArrayLoads
1216
+ ? F->getModule ().getASTContext().getArrayDecl()
1217
+ : nullptr),
1218
+ jointPostDomComputer(computer)
1201
1219
#ifndef NDEBUG
1202
1220
,
1203
1221
printCtx (llvm::dbgs(), /* Verbose=*/ false, /* Sorted=*/ true)
@@ -1322,8 +1340,8 @@ SILValue RLEContext::computePredecessorLocationValue(SILBasicBlock *BB,
1322
1340
1323
1341
// Reduce the available values into a single SILValue we can use to forward
1324
1342
SILInstruction *IPt = CurBB->getTerminator ();
1325
- Values.push_back (
1326
- {CurBB, LSValue::reduce (L, &BB-> getModule (), LSValues, IPt )});
1343
+ Values.push_back ({CurBB, LSValue::reduce (L, &BB-> getModule (), LSValues, IPt,
1344
+ &jointPostDomComputer )});
1327
1345
}
1328
1346
1329
1347
// Finally, collect all the values for the SILArgument, materialize it using
@@ -1335,7 +1353,8 @@ SILValue RLEContext::computePredecessorLocationValue(SILBasicBlock *BB,
1335
1353
Updater.addAvailableValue (V.first , V.second );
1336
1354
}
1337
1355
1338
- return Updater.getValueInMiddleOfBlock (BB);
1356
+ auto Val = Updater.getValueInMiddleOfBlock (BB);
1357
+ return makeNewValueAvailable (Val, BB, &jointPostDomComputer);
1339
1358
}
1340
1359
1341
1360
bool RLEContext::collectLocationValues (SILBasicBlock *BB, LSLocation &L,
@@ -1350,9 +1369,14 @@ bool RLEContext::collectLocationValues(SILBasicBlock *BB, LSLocation &L,
1350
1369
// Find the locations that this basic block defines and the locations which
1351
1370
// we do not have a concrete value in the current basic block.
1352
1371
for (auto &X : Locs) {
1353
- Values[X] = getValue (VM[getLocationBit (X)]);
1354
- if (!Values[X].isCoveringValue ())
1372
+ auto Val = getValue (VM[getLocationBit (X)]);
1373
+ if (!Val.isCoveringValue ()) {
1374
+ auto AvailValue =
1375
+ makeCopiedValueAvailable (Val.getBase (), BB, &jointPostDomComputer);
1376
+ Values[X] = LSValue (AvailValue, Val.getPath ().getValue ());
1355
1377
continue ;
1378
+ }
1379
+ Values[X] = Val;
1356
1380
CSLocs.push_back (X);
1357
1381
}
1358
1382
@@ -1367,7 +1391,6 @@ bool RLEContext::collectLocationValues(SILBasicBlock *BB, LSLocation &L,
1367
1391
SILValue V = computePredecessorLocationValue (BB, X);
1368
1392
if (!V)
1369
1393
return false ;
1370
-
1371
1394
// We've constructed a concrete value for the covering value. Expand and
1372
1395
// collect the newly created forwardable values.
1373
1396
LSLocationList Locs;
@@ -1610,9 +1633,15 @@ bool RLEContext::run() {
1610
1633
continue ;
1611
1634
LLVM_DEBUG (llvm::dbgs () << " Replacing " << SILValue (Iter->first )
1612
1635
<< " With " << Iter->second );
1636
+ auto *origLoad = cast<LoadInst>(Iter->first );
1637
+ SILValue newValue = Iter->second ;
1638
+ if (origLoad->getOwnershipQualifier () == LoadOwnershipQualifier::Take) {
1639
+ SILBuilderWithScope (origLoad).createDestroyAddr (origLoad->getLoc (),
1640
+ origLoad->getOperand ());
1641
+ }
1613
1642
SILChanged = true ;
1614
- Iter-> first -> replaceAllUsesWith (Iter-> second );
1615
- InstsToDelete.push_back (Iter-> first );
1643
+ origLoad-> replaceAllUsesWith (newValue );
1644
+ InstsToDelete.push_back (origLoad );
1616
1645
++NumForwardedLoads;
1617
1646
}
1618
1647
}
@@ -1650,9 +1679,6 @@ class RedundantLoadElimination : public SILFunctionTransform {
1650
1679
// / The entry point to the transformation.
1651
1680
void run () override {
1652
1681
SILFunction *F = getFunction ();
1653
- // FIXME: Handle ownership.
1654
- if (F->hasOwnership ())
1655
- return ;
1656
1682
1657
1683
LLVM_DEBUG (llvm::dbgs () << " *** RLE on function: " << F->getName ()
1658
1684
<< " ***\n " );
@@ -1662,7 +1688,9 @@ class RedundantLoadElimination : public SILFunctionTransform {
1662
1688
auto *PO = PM->getAnalysis <PostOrderAnalysis>()->get (F);
1663
1689
auto *EAFI = PM->getAnalysis <EpilogueARCAnalysis>()->get (F);
1664
1690
1665
- RLEContext RLE (F, PM, AA, TE, PO, EAFI, disableArrayLoads);
1691
+ DeadEndBlocks deadEndBlocks (F);
1692
+ JointPostDominanceSetComputer computer (deadEndBlocks);
1693
+ RLEContext RLE (F, PM, AA, TE, PO, EAFI, disableArrayLoads, computer);
1666
1694
if (RLE.run ()) {
1667
1695
invalidateAnalysis (SILAnalysis::InvalidationKind::Instructions);
1668
1696
}
0 commit comments