forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm-info.h
More file actions
330 lines (295 loc) · 11.4 KB
/
Copy pathasm-info.h
File metadata and controls
330 lines (295 loc) · 11.4 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present 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. |
+----------------------------------------------------------------------+
*/
#ifndef incl_HPHP_JIT_ASM_INFO_H_
#define incl_HPHP_JIT_ASM_INFO_H_
#include "hphp/runtime/vm/jit/block.h"
#include "hphp/runtime/vm/jit/ir-unit.h"
#include "hphp/runtime/vm/jit/state-multi-map.h"
#include "hphp/runtime/vm/jit/state-vector.h"
#include "hphp/runtime/vm/jit/types.h"
#include <algorithm>
namespace HPHP { namespace jit {
// Information about where code was generated, for pretty-printing.
struct AsmInfo {
explicit AsmInfo(const IRUnit& unit)
: mainInstBlockMap(unit, 0)
, coldInstBlockMap(unit, 0)
, frozenInstBlockMap(unit, 0)
{}
// Asm address info for each instruction and block
struct OffsetInstRanges : StateMultiMap<IRInstruction, TcaRange> {
// the offset between logical and physical addresses.
ptrdiff_t offset{0};
};
typedef StateMultiMap<Block, TcaRange> BlockRanges;
// Map from IRInstruction to Block id.
typedef StateVector<IRInstruction, uint32_t> InstBlockMap;
OffsetInstRanges mainInstRanges;
OffsetInstRanges coldInstRanges;
OffsetInstRanges frozenInstRanges;
BlockRanges mainBlockRanges;
BlockRanges coldBlockRanges;
BlockRanges frozenBlockRanges;
InstBlockMap mainInstBlockMap;
InstBlockMap coldInstBlockMap;
InstBlockMap frozenInstBlockMap;
template<typename T>
bool instRangeExists(const T& haystack, const TcaRange& needle) const {
for (const auto& rng : haystack) {
if (contains(needle, rng.second)) return true;
}
return false;
}
bool instRangeExists(AreaIndex /*area*/, const TcaRange& rng) const {
return (instRangeExists(instRangesForArea(AreaIndex::Main), rng) ||
instRangeExists(instRangesForArea(AreaIndex::Cold), rng) ||
instRangeExists(instRangesForArea(AreaIndex::Frozen), rng));
}
OffsetInstRanges& instRangesForArea(AreaIndex area) {
switch (area) {
case AreaIndex::Main:
return mainInstRanges;
case AreaIndex::Cold:
return coldInstRanges;
case AreaIndex::Frozen:
return frozenInstRanges;
}
not_reached();
}
const OffsetInstRanges& instRangesForArea(AreaIndex area) const {
return const_cast<AsmInfo*>(this)->instRangesForArea(area);
}
BlockRanges& blockRangesForArea(AreaIndex area) {
switch (area) {
case AreaIndex::Main:
return mainBlockRanges;
case AreaIndex::Cold:
return coldBlockRanges;
case AreaIndex::Frozen:
return frozenBlockRanges;
}
not_reached();
}
const BlockRanges& blockRangesForArea(AreaIndex area) const {
return const_cast<AsmInfo*>(this)->blockRangesForArea(area);
}
void updateForInstruction(const IRInstruction* inst,
AreaIndex area,
TCA start,
TCA end) {
if (start != end) {
auto newRange = updateRange(instRangesForArea(area),
inst->id(),
area,
start,
end,
false);
instBlockMapForArea(area)[inst] = inst->block()->id();
updateRange(blockRangesForArea(area),
inst->block()->id(),
area,
newRange.begin(),
newRange.end(),
true);
}
}
void updateForBlock(AreaIndex area,
uint32_t instId,
const TcaRange& instRange) {
if (!instRange.empty()) {
updateRange(blockRangesForArea(area),
instBlockMapForArea(area)[instId],
area,
instRange.begin(),
instRange.end(),
true);
}
}
void clearBlockRangesForArea(AreaIndex area) {
blockRangesForArea(area).clear();
}
bool validate() const {
return (validateInstructionRanges(AreaIndex::Main, AreaIndex::Main) &&
validateInstructionRanges(AreaIndex::Main, AreaIndex::Cold) &&
validateInstructionRanges(AreaIndex::Main, AreaIndex::Frozen) &&
validateInstructionRanges(AreaIndex::Cold, AreaIndex::Cold) &&
validateInstructionRanges(AreaIndex::Cold, AreaIndex::Frozen) &&
validateInstructionRanges(AreaIndex::Frozen, AreaIndex::Frozen) &&
validateBlockRanges(AreaIndex::Main) &&
validateBlockRanges(AreaIndex::Cold) &&
validateBlockRanges(AreaIndex::Frozen) &&
validateBlocks(AreaIndex::Main, AreaIndex::Main) &&
validateBlocks(AreaIndex::Main, AreaIndex::Cold) &&
validateBlocks(AreaIndex::Main, AreaIndex::Frozen) &&
validateBlocks(AreaIndex::Cold, AreaIndex::Cold) &&
validateBlocks(AreaIndex::Cold, AreaIndex::Frozen) &&
validateBlocks(AreaIndex::Frozen, AreaIndex::Frozen));
}
void dump() const {
dumpBlockRanges("Main", mainBlockRanges);
dumpInstructionRanges("Main", mainInstRanges);
dumpBlockRanges("Cold", coldBlockRanges);
dumpInstructionRanges("Cold", coldInstRanges);
dumpBlockRanges("Frozen", frozenBlockRanges);
dumpInstructionRanges("Frozen", frozenInstRanges);
}
private:
InstBlockMap& instBlockMapForArea(AreaIndex area) {
switch (area) {
case AreaIndex::Main:
return mainInstBlockMap;
case AreaIndex::Cold:
return coldInstBlockMap;
case AreaIndex::Frozen:
return frozenInstBlockMap;
}
not_reached();
}
const InstBlockMap& instBlockMapForArea(AreaIndex area) const {
return const_cast<AsmInfo*>(this)->instBlockMapForArea(area);
}
template <typename MM>
TcaRange updateRange(MM& stateMap, uint32_t id, AreaIndex /*area*/, TCA start,
TCA end, bool merge) {
auto ranges = stateMap[id];
auto itr = ranges.first;
if (merge) {
// update range multi-map for the given id. If the new (start,end)
// range overlaps an existing range, just grow the existing entry
// rather than adding a new one.
while (itr != ranges.second) {
if (overlappingOrAdjacent(itr->second, TcaRange{start, end})) {
break;
}
++itr;
}
if (itr != ranges.second) {
auto& oldRange = itr->second;
start = oldRange.start() ? std::min(oldRange.start(), start) : start;
end = oldRange.end() ? std::max(oldRange.end(), end) : end;
oldRange = TcaRange { start, end };
return oldRange;
}
}
auto newRange = TcaRange{start, end};
if (itr == ranges.second ||
itr->second.start() != start ||
itr->second.end() != end) {
stateMap.insert(id, newRange);
}
return newRange;
}
bool validateInstructionRanges(AreaIndex area_a, AreaIndex area_b) const {
const auto& arngs = instRangesForArea(area_a);
const auto& brngs = instRangesForArea(area_b);
bool sawBadInst = false;
for (auto irnga = arngs.begin(); irnga != arngs.end(); ++irnga) {
for (auto irngb = area_a == area_b ? std::next(irnga) : brngs.begin();
irngb != brngs.end(); ++irngb) {
if (!disjoint(irnga->second, irngb->second)) {
std::cout << areaAsString(area_a) << " instructions: "
<< irnga->first << " ("
<< (void*)irnga->second.begin() << ", "
<< (void*)irnga->second.end()
<< ") and " << areaAsString(area_b) << " "
<< irngb->first << " ("
<< (void*)irngb->second.begin() << ", "
<< (void*)irngb->second.end()
<< ") are not disjoint.\n";
sawBadInst = true;
}
}
}
return !sawBadInst;
}
bool validateBlockRanges(AreaIndex area) const {
bool sawBadInst = false;
for (auto& irng : instRangesForArea(area)) {
bool ok = false;
for (auto& brng : blockRangesForArea(area)) {
if (contains(brng.second, irng.second)) {
ok = true;
break;
}
}
if (!ok) {
sawBadInst = true;
std::cout << areaAsString(area) << " instruction: "
<< irng.first << " ("
<< (void*)irng.second.begin() << ", "
<< (void*)irng.second.end()
<< ") is not properly contained in any block.\n";
}
}
return !sawBadInst;
}
bool validateBlocks(AreaIndex area_a, AreaIndex area_b) const {
auto& arng = blockRangesForArea(area_a);
auto& brng = blockRangesForArea(area_b);
bool sawBadBlock = false;
for (auto itra = arng.begin(); itra != arng.end(); ++itra) {
for (auto itrb = area_a == area_b ? std::next(itra) : brng.begin();
itrb != brng.end(); ++itrb) {
if (!disjoint(itra->second, itrb->second)) {
std::cout << areaAsString(area_a) << " block: " << itra->first << " ("
<< (void*)itra->second.begin() << ", "
<< (void*)itra->second.end()
<< ") and " << areaAsString(area_b) << " "
<< itrb->first << " ("
<< (void*)itrb->second.begin() << ", "
<< (void*)itrb->second.end()
<< ") are not disjoint.\n";
sawBadBlock = true;
}
}
}
return !sawBadBlock;
}
void dumpInstructionRanges(const char* area,
const OffsetInstRanges& rngs) const {
std::cout << area << " instructions:\n";
for (auto& rng : rngs) {
std::cout << rng.first << " = (" << (void*)rng.second.begin()
<< ", " << (void*)rng.second.end() << ")\n";
}
std::cout << "\n";
}
void dumpBlockRanges(const char* area, const BlockRanges& rngs) const {
std::cout << area << " blocks:\n";
for (auto itr = rngs.begin(); itr != rngs.end(); ++itr) {
if (!itr->second.empty()) {
std::cout << itr->first << " = (" << (void*)itr->second.begin()
<< ", " << (void*)itr->second.end() << ")\n";
}
}
std::cout << "\n";
}
static bool overlappingOrAdjacent(const TcaRange& a, const TcaRange& b) {
return ((a.begin() >= b.begin() && a.begin() <= b.end()) ||
(a.end() >= b.begin() && a.end() <= b.end()) ||
(b.begin() >= a.begin() && b.begin() <= a.end()) ||
(b.end() >= a.begin() && b.end() <= a.end()));
}
// does a contain b?
static bool contains(const TcaRange& a, const TcaRange& b) {
return b.begin() >= a.begin() && b.end() <= a.end();
}
static bool disjoint(const TcaRange& a, const TcaRange& b) {
return a.begin() >= b.end() || a.end() <= b.begin();
}
};
}}
#endif