Skip to content

Commit cf17fd4

Browse files
committed
SILOptimizer: Use BasicBlockData in the StackNesting utility
1 parent 273bd35 commit cf17fd4

File tree

11 files changed

+127
-144
lines changed

11 files changed

+127
-144
lines changed

include/swift/SILOptimizer/Utils/StackNesting.h

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
#ifndef SWIFT_SILOPTIMIZER_UTILS_STACKNESTING_H
1414
#define SWIFT_SILOPTIMIZER_UTILS_STACKNESTING_H
1515

16-
#include "swift/SIL/SILInstruction.h"
16+
#include "swift/SIL/SILFunction.h"
17+
#include "swift/SIL/BasicBlockData.h"
1718
#include "llvm/ADT/SmallBitVector.h"
1819

19-
#include <vector>
20-
2120
namespace swift {
2221

2322
/// A utility to correct the nesting of stack allocating/deallocating
@@ -48,17 +47,25 @@ namespace swift {
4847
///
4948
class StackNesting {
5049

51-
typedef SmallBitVector BitVector;
50+
public:
5251

53-
/// Data stored for each block (actually for each block which is not dead).
54-
struct BlockInfo {
52+
/// The possible return values of fixNesting().
53+
enum class Changes {
54+
/// No changes are made.
55+
None,
5556

56-
/// Back-link to the block.
57-
SILBasicBlock *Block;
57+
/// Only instructions were inserted or deleted.
58+
Instructions,
5859

59-
/// The cached list of successors.
60-
llvm::SmallVector<BlockInfo *, 8> Successors;
60+
/// Instructions were inserted or deleted and new blocks were inserted.
61+
CFG
62+
};
63+
64+
private:
65+
typedef SmallBitVector BitVector;
6166

67+
/// Data stored for each block (actually for each block which is not dead).
68+
struct BlockInfo {
6269
/// The list of stack allocating/deallocating instructions in the block.
6370
llvm::SmallVector<SILInstruction *, 8> StackInsts;
6471

@@ -68,7 +75,8 @@ class StackNesting {
6875
/// The bit-set of alive stack locations at the block exit.
6976
BitVector AliveStackLocsAtExit;
7077

71-
BlockInfo(SILBasicBlock *Block) : Block(Block) { }
78+
/// Used in the setup function to walk over the CFG.
79+
bool visited = false;
7280
};
7381

7482
/// Data stored for each stack location (= allocation).
@@ -93,46 +101,35 @@ class StackNesting {
93101
/// number in the bit-sets.
94102
llvm::SmallVector<StackLoc, 8> StackLocs;
95103

96-
/// Block data for all (non-dead) blocks.
97-
std::vector<BlockInfo> BlockInfos;
104+
BasicBlockData<BlockInfo> BlockInfos;
98105

99-
public:
100-
101-
/// The possible return values of correctStackNesting().
102-
enum class Changes {
103-
/// No changes are made.
104-
None,
105-
106-
/// Only instructions were inserted or deleted.
107-
Instructions,
108-
109-
/// Instructions were inserted or deleted and new blocks were inserted.
110-
CFG
111-
};
112-
113-
StackNesting() { }
106+
StackNesting(SILFunction *F) : BlockInfos(F) { }
114107

115108
/// Performs correction of stack nesting by moving stack-deallocation
116109
/// instructions down the control flow.
117110
///
118111
/// Returns the status of what changes were made.
119-
Changes correctStackNesting(SILFunction *F);
112+
Changes run();
120113

121114
/// For debug dumping.
122115
void dump() const;
123116

124117
static void dumpBits(const BitVector &Bits);
125118

126-
private:
127119
/// Initializes the data structures.
128-
void setup(SILFunction *F);
120+
void setup();
129121

130122
/// Solves the dataflow problem.
131123
///
132124
/// Returns true if there is a nesting of locations in any way, which can
133125
/// potentially in the wrong order.
134126
bool solve();
135127

128+
bool analyze() {
129+
setup();
130+
return solve();
131+
}
132+
136133
/// Insert deallocation instructions for all locations which are alive before
137134
/// the InsertionPoint (AliveBefore) but not alive after the InsertionPoint
138135
/// (AliveAfter).
@@ -161,7 +158,15 @@ class StackNesting {
161158
/// Modifies the SIL to end up with a correct stack nesting.
162159
///
163160
/// Returns the status of what changes were made.
164-
bool adaptDeallocs();
161+
Changes adaptDeallocs();
162+
163+
public:
164+
165+
/// Performs correction of stack nesting by moving stack-deallocation
166+
/// instructions down the control flow.
167+
///
168+
/// Returns the status of what changes were made.
169+
static Changes fixNesting(SILFunction *F);
165170
};
166171

167172
} // end namespace swift

lib/SILOptimizer/IPO/ClosureSpecializer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ void ClosureSpecCloner::populateCloned() {
927927
}
928928
}
929929
if (invalidatedStackNesting) {
930-
StackNesting().correctStackNesting(Cloned);
930+
StackNesting::fixNesting(Cloned);
931931
}
932932
}
933933

@@ -992,7 +992,7 @@ void SILClosureSpecializerTransform::run() {
992992
}
993993

994994
if (invalidatedStackNesting) {
995-
StackNesting().correctStackNesting(F);
995+
StackNesting::fixNesting(F);
996996
}
997997
}
998998

lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,9 +1003,8 @@ class ClosureLifetimeFixup : public SILFunctionTransform {
10031003

10041004
if (fixupClosureLifetimes(*getFunction(), checkStackNesting, modifiedCFG)) {
10051005
if (checkStackNesting){
1006-
StackNesting sn;
10071006
modifiedCFG |=
1008-
sn.correctStackNesting(getFunction()) == StackNesting::Changes::CFG;
1007+
StackNesting::fixNesting(getFunction()) == StackNesting::Changes::CFG;
10091008
}
10101009
if (modifiedCFG)
10111010
invalidateAnalysis(SILAnalysis::InvalidationKind::FunctionBody);

lib/SILOptimizer/Mandatory/MandatoryCombine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class MandatoryCombiner final
180180
}
181181

182182
if (invalidatedStackNesting) {
183-
StackNesting().correctStackNesting(&function);
183+
StackNesting::fixNesting(&function);
184184
}
185185

186186
return changed;

lib/SILOptimizer/Mandatory/MandatoryInlining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ runOnFunctionRecursively(SILOptFunctionBuilder &FuncBuilder, SILFunction *F,
10001000
}
10011001

10021002
if (invalidatedStackNesting) {
1003-
StackNesting().correctStackNesting(F);
1003+
StackNesting::fixNesting(F);
10041004
changedFunctions.insert(F);
10051005
}
10061006

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ bool SILCombiner::runOnFunction(SILFunction &F) {
322322
}
323323

324324
if (invalidatedStackNesting) {
325-
StackNesting().correctStackNesting(&F);
325+
StackNesting::fixNesting(&F);
326326
}
327327

328328
// Cleanup the builder and return whether or not we made any changes.

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,7 @@ class AllocBoxToStack : public SILFunctionTransform {
10951095
auto Count = rewritePromotedBoxes(pass);
10961096
NumStackPromoted += Count;
10971097
if (Count) {
1098-
StackNesting SN;
1099-
if (SN.correctStackNesting(getFunction()) == StackNesting::Changes::CFG)
1098+
if (StackNesting::fixNesting(getFunction()) == StackNesting::Changes::CFG)
11001099
pass.CFGChanged = true;
11011100
}
11021101

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ bool SILPerformanceInliner::inlineCallsIntoFunction(SILFunction *Caller) {
987987
mergeBasicBlocks(Caller);
988988

989989
if (invalidatedStackNesting) {
990-
StackNesting().correctStackNesting(Caller);
990+
StackNesting::fixNesting(Caller);
991991
}
992992

993993
// If we were asked to verify our caller after inlining all callees we could

lib/SILOptimizer/Transforms/StackPromotion.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ void StackPromotion::run() {
7373
return;
7474

7575
// Make sure that all stack allocating instructions are nested correctly.
76-
StackNesting SN;
77-
if (SN.correctStackNesting(F) == StackNesting::Changes::CFG) {
76+
if (StackNesting::fixNesting(F) == StackNesting::Changes::CFG) {
7877
invalidateAnalysis(SILAnalysis::InvalidationKind::BranchesAndInstructions);
7978
} else {
8079
invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions);

0 commit comments

Comments
 (0)