-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathAirSimplifyCFG.cpp
More file actions
192 lines (163 loc) · 7.88 KB
/
Copy pathAirSimplifyCFG.cpp
File metadata and controls
192 lines (163 loc) · 7.88 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (C) 2015 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "AirSimplifyCFG.h"
#if ENABLE(B3_JIT)
#include "AirCode.h"
#include "AirPhaseScope.h"
namespace JSC { namespace B3 { namespace Air {
bool simplifyCFG(Code& code)
{
constexpr bool verbose = false;
PhaseScope phaseScope(code, "simplifyCFG"_s);
// We have three easy simplification rules:
//
// 1) If a successor is a block that just jumps to another block, then jump directly to
// that block.
//
// 2) If all successors are the same and the operation has no effects, then use a jump
// instead.
//
// 3) If you jump to a block that is not you and has one predecessor, then merge.
//
// Note that because of the first rule, this phase may introduce critical edges. That's fine.
// If you need broken critical edges, then you have to break them yourself.
if (verbose) {
dataLog("Air before simplifyCFG:\n");
dataLog(code);
}
bool changed = false;
bool validateAtEachPhase = shouldValidateIRAtEachPhase();
for (BasicBlock* block : code) {
// We rely on predecessors being conservatively correct. Verify this here.
if (validateAtEachPhase) [[unlikely]] {
for (BasicBlock* block : code) {
for (BasicBlock* successor : block->successorBlocks())
RELEASE_ASSERT(successor->containsPredecessor(block));
}
}
// We don't care about blocks that don't have successors.
if (!block->numSuccessors())
continue;
// First check if any of the successors of this block can be forwarded over.
// We compress multiple basic blocks into one jump so long as isForwardable is met.
for (BasicBlock*& successor : block->successorBlocks()) {
auto isForwardable = [&](BasicBlock* candidate) {
return candidate != block
&& candidate->size() == 1
&& candidate->last().kind.opcode == Jump
&& candidate->successorBlock(0) != candidate;
};
// Trace the chain of jump-only blocks with a Floyd tortoise/hare walk. The hare
// (fast) moves two hops at a time, so it reaches the end of the chain first. When it
// lands on a non-forwardable block, that block is the chain end and there is no cycle.
// Orphan basic blocks can instead form a cycle with no end, in which case the hare
// laps the tortoise (slow) and we bail. Cycles are extremely rare.
bool sawCycle = false;
BasicBlock* slow = successor;
BasicBlock* fast = successor;
while (true) {
if (!isForwardable(fast))
break;
fast = fast->successorBlock(0);
if (!isForwardable(fast))
break;
fast = fast->successorBlock(0);
slow = slow->successorBlock(0);
if (slow == fast) {
sawCycle = true;
break;
}
}
// When there is no cycle, fast is the end of the chain.
if (!sawCycle && fast != successor) {
BasicBlock* newSuccessor = fast;
dataLogLnIf(verbose, "Replacing ", pointerDump(block), "->", pointerDump(successor), " with ", pointerDump(block), "->", pointerDump(newSuccessor));
for (BasicBlock* current = successor; current != newSuccessor;) {
BasicBlock* next = current->successorBlock(0);
current->successorBlock(0) = newSuccessor;
newSuccessor->addPredecessor(current);
current = next;
}
newSuccessor->addPredecessor(block);
successor = newSuccessor;
changed = true;
}
}
// Now check if the block's terminal can be replaced with a jump. The terminal must not
// have weird effects.
if (block->numSuccessors() > 1
&& !block->last().hasNonControlEffects()) {
// All of the successors must be the same.
bool allSame = true;
BasicBlock* firstSuccessor = block->successorBlock(0);
for (unsigned i = 1; i < block->numSuccessors(); ++i) {
if (block->successorBlock(i) != firstSuccessor) {
allSame = false;
break;
}
}
if (allSame) {
dataLogLnIf(verbose, "Changing ", pointerDump(block), "'s terminal to a Jump.");
block->last() = Inst(Jump, block->last().origin);
block->successors().resize(1);
block->successors()[0].frequency() = FrequencyClass::Normal;
changed = true;
}
}
// Finally handle jumps to a block with one predecessor.
if (block->numSuccessors() == 1
&& !block->last().hasNonControlEffects()) {
BasicBlock* successor = block->successorBlock(0);
if (successor != block && successor->numPredecessors() == 1) {
RELEASE_ASSERT(successor->predecessor(0) == block);
// We can merge the two blocks because the predecessor only jumps to the successor
// and the successor is only reachable from the predecessor.
// Remove the terminal.
Value* origin = block->insts().takeLast().origin;
// Append the full contents of the successor to the predecessor.
block->insts().reserveCapacity(block->size() + successor->size());
for (Inst& inst : *successor)
block->appendInst(WTF::move(inst));
// Make sure that our successors are the successor's successors.
block->successors() = WTF::move(successor->successors());
// Make sure that the successor has nothing left in it except an oops.
successor->resize(1);
successor->last() = Inst(Oops, origin);
successor->successors().clear();
// Ensure that the predecessors of block's new successors know what's up.
for (BasicBlock* newSuccessor : block->successorBlocks())
newSuccessor->replacePredecessor(successor, block);
dataLogLnIf(verbose, "Merged ", pointerDump(block), "->", pointerDump(successor));
changed = true;
}
}
}
if (changed)
code.resetReachability();
return changed;
}
} } } // namespace JSC::B3::Air
#endif // ENABLE(B3_JIT)