-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathBitDataflow.cpp
151 lines (138 loc) · 4.62 KB
/
BitDataflow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//===--- BitDataflow.cpp --------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "bit-dataflow"
#include "swift/SIL/BitDataflow.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/MemoryLocations.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
BitDataflow::BitDataflow(SILFunction *function, unsigned numLocations) :
blockStates(function, [numLocations](SILBasicBlock *block) {
return BlockState(numLocations);
}) {}
void BitDataflow::entryReachabilityAnalysis() {
llvm::SmallVector<SILBasicBlock *, 16> workList;
auto entry = blockStates.entry();
entry.data.reachableFromEntry = true;
workList.push_back(&entry.block);
while (!workList.empty()) {
SILBasicBlock *block = workList.pop_back_val();
for (SILBasicBlock *succ : block->getSuccessorBlocks()) {
BlockState &succState = blockStates[succ];
if (!succState.reachableFromEntry) {
succState.reachableFromEntry = true;
workList.push_back(succ);
}
}
}
}
void BitDataflow::exitReachableAnalysis() {
llvm::SmallVector<SILBasicBlock *, 16> workList;
for (auto bd : blockStates) {
if (bd.block.getTerminator()->isFunctionExiting()) {
bd.data.exitReachability = ExitReachability::ReachesExit;
workList.push_back(&bd.block);
} else if (isa<UnreachableInst>(bd.block.getTerminator())) {
bd.data.exitReachability = ExitReachability::ReachesUnreachable;
workList.push_back(&bd.block);
}
}
while (!workList.empty()) {
SILBasicBlock *block = workList.pop_back_val();
BlockState &state = blockStates[block];
for (SILBasicBlock *pred : block->getPredecessorBlocks()) {
BlockState &predState = blockStates[pred];
if (predState.exitReachability < state.exitReachability) {
// As there are 3 states, each block can be put into the workList 2
// times maximum.
predState.exitReachability = state.exitReachability;
workList.push_back(pred);
}
}
}
}
void BitDataflow::solveForward(JoinOperation join) {
// Pretty standard data flow solving.
bool changed = false;
bool firstRound = true;
do {
changed = false;
for (auto bd : blockStates) {
Bits bits = bd.data.entrySet;
assert(!bits.empty());
for (SILBasicBlock *pred : bd.block.getPredecessorBlocks()) {
join(bits, blockStates[pred].exitSet);
}
if (firstRound || bits != bd.data.entrySet) {
changed = true;
bd.data.entrySet = bits;
bits |= bd.data.genSet;
bits.reset(bd.data.killSet);
bd.data.exitSet = bits;
}
}
firstRound = false;
} while (changed);
}
void BitDataflow::solveForwardWithIntersect() {
solveForward([](Bits &entry, const Bits &predExit){
entry &= predExit;
});
}
void BitDataflow::solveForwardWithUnion() {
solveForward([](Bits &entry, const Bits &predExit){
entry |= predExit;
});
}
void BitDataflow::solveBackward(JoinOperation join) {
// Pretty standard data flow solving.
bool changed = false;
bool firstRound = true;
do {
changed = false;
for (auto bd : llvm::reverse(blockStates)) {
Bits bits = bd.data.exitSet;
assert(!bits.empty());
for (SILBasicBlock *succ : bd.block.getSuccessorBlocks()) {
join(bits, blockStates[succ].entrySet);
}
if (firstRound || bits != bd.data.exitSet) {
changed = true;
bd.data.exitSet = bits;
bits |= bd.data.genSet;
bits.reset(bd.data.killSet);
bd.data.entrySet = bits;
}
}
firstRound = false;
} while (changed);
}
void BitDataflow::solveBackwardWithIntersect() {
solveBackward([](Bits &entry, const Bits &predExit){
entry &= predExit;
});
}
void BitDataflow::solveBackwardWithUnion() {
solveBackward([](Bits &entry, const Bits &predExit){
entry |= predExit;
});
}
void BitDataflow::dump() const {
for (auto bd : blockStates) {
llvm::dbgs() << "bb" << bd.block.getDebugID() << ":\n"
<< " entry: " << bd.data.entrySet << '\n'
<< " gen: " << bd.data.genSet << '\n'
<< " kill: " << bd.data.killSet << '\n'
<< " exit: " << bd.data.exitSet << '\n';
}
}