forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfg-clean.cpp
More file actions
209 lines (186 loc) · 7.03 KB
/
cfg-clean.cpp
File metadata and controls
209 lines (186 loc) · 7.03 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2016 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/vm/jit/block.h"
#include "hphp/runtime/vm/jit/cfg.h"
#include "hphp/runtime/vm/jit/ir-unit.h"
#include "hphp/runtime/vm/jit/mutation.h"
#include "hphp/runtime/vm/jit/pass-tracer.h"
#include "hphp/runtime/vm/jit/timer.h"
namespace HPHP { namespace jit {
TRACE_SET_MOD(hhir_cfg);
namespace {
// Different from simplify(), this works even when `block' is unreachable.
// Returns true iff changes are made.
bool convertCondBranchToJmp(IRUnit& unit, Block* block) {
auto takenBlk = block->taken();
if (takenBlk == nullptr) return false;
auto nextBlk = block->next();
if (nextBlk == nullptr) return false;
auto& term = block->back();
// Only look at some conditional branches with no side effects.
if (!term.is(JmpZero,
JmpNZero,
CheckTypeMem,
CheckLoc,
CheckStk,
CheckInit,
CheckInitMem,
CheckInitProps,
CheckInitSProps,
CheckClosureStaticLocInit,
CheckRefInner,
CheckCtxThis)) {
return false;
}
// Case 1: both ways lead to the same block.
bool isUnconditional = (nextBlk == takenBlk);
// Case 2: branch constantly goes one way.
if (!isUnconditional && term.numSrcs() == 1) {
auto const src = term.src(0);
if (src->hasConstVal() && term.is(JmpZero, JmpNZero)) {
auto const v = src->isA(TBool) ? src->boolVal()
: static_cast<bool>(src->intVal());
if (v == term.is(JmpZero)) {
// Branch never taken.
takenBlk = block->next();
}
isUnconditional = true;
} else if (term.is(CheckInit)) {
if (!src->type().maybe(TUninit)) {
// Never taken.
takenBlk = block->next();
isUnconditional = true;
} else if (src->isA(TUninit)) {
// Always taken.
isUnconditional = true;
}
}
}
if (isUnconditional) {
assert(takenBlk);
auto const marker = term.marker();
term.convertToNop(); // Removes edges to original dests.
block->push_back(unit.gen(Jmp, marker, takenBlk));
FTRACE(1, "Replaced conditional branch in B{} with Jmp to B{}.\n",
block->id(), takenBlk->id());
return true;
}
return false;
}
// If `block' only goes to `takenBlk' and `takenBlk' can only be reached
// directly from `block', merge them and leave `takenBlk' unreachable.
// Returns true iff merging happend.
bool absorbDstBlock(IRUnit& unit, Block* block) {
auto& term = block->back();
if (!term.is(Jmp)) return false;
auto takenBlk = block->taken();
if (takenBlk == block) return false;
if (takenBlk->numPreds() != 1) return false;
// Replace DefLabel with Mov's
if (takenBlk->begin()->is(DefLabel)) {
auto& defLabel = *takenBlk->begin();
for (auto i = 0; i < defLabel.numDsts(); ++i) {
auto mov = unit.gen(Mov, defLabel.marker(), term.src(i));
mov->setDst(defLabel.dst(i));
mov->dst()->setInstruction(mov);
block->push_back(mov);
}
defLabel.convertToNop();
}
term.convertToNop();
block->splice(block->end(), takenBlk, takenBlk->skipHeader(),
takenBlk->end());
// takenBlk is in a zombie now, it will be removed later.
assertx(takenBlk->numPreds() == 0);
takenBlk->instrs().clear();
FTRACE(1, "Merged B{} into B{}\n", takenBlk->id(), block->id());
return true;
}
// Jmp->(Jmp2|Jcc)->... ==> (Jmp2|Jcc)->...
// Jcc->(non-phi)Jmp->... ==> Jcc->...
// Returns true iff changes were made.
bool foldJmp(IRUnit& unit, Block* block) {
auto& term = block->back();
if (term.is(Jmp)) {
auto takenBlk = block->taken();
if (takenBlk == block) return false;
// If we reach here, takenBlk cannot be merged into block as it has other
// predecessors. We could duplicate whatever code in takenBlk in block, but
// we only do it if takenBlk contains only a cheap instruction.
if (takenBlk->begin()->is(Jmp, JmpZero, JmpNZero, CheckInit)) {
block->back().become(unit, &*takenBlk->begin());
FTRACE(1, "Duplicated B{} into B{}\n", takenBlk->id(), block->id());
// takenBlk is probably still reachable from other blocks.
return true;
}
return false;
}
if (auto next = term.next()) {
auto jmp = next->begin();
if (jmp->is(Jmp) && jmp->numSrcs() == 0) {
assertx(jmp->taken());
FTRACE(1, "Setting {} next to skip {}\n", term, *jmp);
term.setNext(jmp->taken());
return true;
}
}
if (auto taken = term.taken()) {
auto jmp = taken->begin();
if (jmp->is(Jmp) && jmp->numSrcs() == 0) {
assertx(jmp->taken());
FTRACE(1, "Setting {} taken to skip {}\n", term, *jmp);
term.setTaken(jmp->taken());
return true;
}
}
return false;
}
}
/*
* This pass tries to merge blocks and cleanup the CFG.
*
* In each pass, it visits blocks in reverse post order and tries to
* (1) convert the conditional branch at the end of the block into a Jmp;
* (2) merge the block with its unique successor block, if it is the unique
* predecessor of its successor;
* (3) fold Jmps, if it fits the Jmp -> Jmp|Jcc or Jcc -> Jmp pattern.
*
* The reverse post order is not essential to the transformation; in the current
* implementation it helps skipping some blocks after a change happens.
*/
void cleanCfg(IRUnit& unit) {
PassTracer tracer { &unit, Trace::hhir_cfg, "cleanCfg" };
Timer timer(Timer::optimize_cleancfg);
bool changed = false;
do {
auto const blocks = rpoSortCfg(unit);
for (auto block : blocks) {
// Skip malformed unreachable blocks that can appear transiently.
if (block->empty()) continue;
if (block->numPreds() == 0 && block != unit.entry()) continue;
// Keep working on the current block until no further changes are made.
for ( ; ; changed = true) {
if (convertCondBranchToJmp(unit, block)) continue;
if (absorbDstBlock(unit, block)) continue;
if (foldJmp(unit, block)) continue;
break;
}
}
} while (removeUnreachable(unit));
// If any block is removed, reflow all types.
if (changed) reflowTypes(unit);
}
}}