|
69 | 69 | #include "swift/SIL/SILBuilder.h"
|
70 | 70 | #include "swift/SIL/SILValueProjection.h"
|
71 | 71 | #include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
|
| 72 | +#include "swift/SILOptimizer/Analysis/EscapeAnalysis.h" |
72 | 73 | #include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h"
|
73 | 74 | #include "swift/SILOptimizer/Analysis/ValueTracking.h"
|
74 | 75 | #include "swift/SILOptimizer/PassManager/Passes.h"
|
@@ -301,6 +302,9 @@ class DSEContext {
|
301 | 302 | /// Alias Analysis.
|
302 | 303 | AliasAnalysis *AA;
|
303 | 304 |
|
| 305 | + /// Escape analysis. |
| 306 | + EscapeAnalysis *EA; |
| 307 | + |
304 | 308 | /// Type Expansion Analysis.
|
305 | 309 | TypeExpansionAnalysis *TE;
|
306 | 310 |
|
@@ -408,8 +412,8 @@ class DSEContext {
|
408 | 412 | public:
|
409 | 413 | /// Constructor.
|
410 | 414 | DSEContext(SILFunction *F, SILModule *M, SILPassManager *PM,
|
411 |
| - AliasAnalysis *AA, TypeExpansionAnalysis *TE) |
412 |
| - : Mod(M), F(F), PM(PM), AA(AA), TE(TE) {} |
| 415 | + AliasAnalysis *AA, EscapeAnalysis *EA, TypeExpansionAnalysis *TE) |
| 416 | + : Mod(M), F(F), PM(PM), AA(AA), EA(EA), TE(TE) {} |
413 | 417 |
|
414 | 418 | /// Entry point for dead store elimination.
|
415 | 419 | bool run();
|
@@ -516,10 +520,24 @@ void DSEContext::mergeSuccessorStates(SILBasicBlock *BB) {
|
516 | 520 | if (BB->succ_empty()) {
|
517 | 521 | if (DisableLocalStoreDSE)
|
518 | 522 | return;
|
| 523 | + |
| 524 | + auto *ConGraph = EA->getConnectionGraph(F); |
| 525 | + |
519 | 526 | for (unsigned i = 0; i < LocationVault.size(); ++i) {
|
520 |
| - if (!LocationVault[i].isNonEscapingLocalLSLocation()) |
| 527 | + SILValue Base = LocationVault[i].getBase(); |
| 528 | + if (isa<AllocStackInst>(Base)) { |
| 529 | + // An alloc_stack is definitely dead at the end of the function. |
| 530 | + C->startTrackingLocation(C->BBWriteSetOut, i); |
521 | 531 | continue;
|
522 |
| - C->startTrackingLocation(C->BBWriteSetOut, i); |
| 532 | + } |
| 533 | + if (isa<AllocationInst>(Base)) { |
| 534 | + // For other allocations we ask escape analysis. |
| 535 | + auto *Node = ConGraph->getNodeOrNull(Base, EA); |
| 536 | + if (Node && !Node->escapes()) { |
| 537 | + C->startTrackingLocation(C->BBWriteSetOut, i); |
| 538 | + continue; |
| 539 | + } |
| 540 | + } |
523 | 541 | }
|
524 | 542 | return;
|
525 | 543 | }
|
@@ -1009,11 +1027,12 @@ class DeadStoreElimination : public SILFunctionTransform {
|
1009 | 1027 | /// The entry point to the transformation.
|
1010 | 1028 | void run() override {
|
1011 | 1029 | auto *AA = PM->getAnalysis<AliasAnalysis>();
|
| 1030 | + auto *EA = PM->getAnalysis<EscapeAnalysis>(); |
1012 | 1031 | auto *TE = PM->getAnalysis<TypeExpansionAnalysis>();
|
1013 | 1032 | SILFunction *F = getFunction();
|
1014 | 1033 | DEBUG(llvm::dbgs() << "*** DSE on function: " << F->getName() << " ***\n");
|
1015 | 1034 |
|
1016 |
| - DSEContext DSE(F, &F->getModule(), PM, AA, TE); |
| 1035 | + DSEContext DSE(F, &F->getModule(), PM, AA, EA, TE); |
1017 | 1036 | if (DSE.run()) {
|
1018 | 1037 | invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions);
|
1019 | 1038 | }
|
|
0 commit comments