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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
/*
* Copyright (C) 2015-2016 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 "B3Procedure.h"
#if ENABLE(B3_JIT)
#include "AirCode.h"
#include "B3BasicBlockInlines.h"
#include "B3BasicBlockUtils.h"
#include "B3BlockWorklist.h"
#include "B3CFG.h"
#include "B3DataSection.h"
#include "B3Dominators.h"
#include "B3OpaqueByproducts.h"
#include "B3StackSlot.h"
#include "B3ValueInlines.h"
#include "B3Variable.h"
namespace JSC { namespace B3 {
Procedure::Procedure()
: m_cfg(new CFG(*this))
, m_lastPhaseName("initial")
, m_byproducts(std::make_unique<OpaqueByproducts>())
, m_code(new Air::Code(*this))
{
}
Procedure::~Procedure()
{
}
void Procedure::printOrigin(PrintStream& out, Origin origin) const
{
if (m_originPrinter)
m_originPrinter->run(out, origin);
else
out.print(origin);
}
BasicBlock* Procedure::addBlock(double frequency)
{
std::unique_ptr<BasicBlock> block(new BasicBlock(m_blocks.size(), frequency));
BasicBlock* result = block.get();
m_blocks.append(WTFMove(block));
return result;
}
StackSlot* Procedure::addStackSlot(unsigned byteSize)
{
return m_stackSlots.addNew(byteSize);
}
Variable* Procedure::addVariable(Type type)
{
return m_variables.addNew(type);
}
Value* Procedure::clone(Value* value)
{
std::unique_ptr<Value> clone(value->cloneImpl());
clone->m_index = UINT_MAX;
clone->owner = nullptr;
return m_values.add(WTFMove(clone));
}
Value* Procedure::addIntConstant(Origin origin, Type type, int64_t value)
{
switch (type) {
case Int32:
return add<Const32Value>(origin, static_cast<int32_t>(value));
case Int64:
return add<Const64Value>(origin, value);
case Double:
return add<ConstDoubleValue>(origin, static_cast<double>(value));
case Float:
return add<ConstFloatValue>(origin, static_cast<float>(value));
default:
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
}
Value* Procedure::addIntConstant(Value* likeValue, int64_t value)
{
return addIntConstant(likeValue->origin(), likeValue->type(), value);
}
Value* Procedure::addBottom(Origin origin, Type type)
{
return addIntConstant(origin, type, 0);
}
Value* Procedure::addBottom(Value* value)
{
return addBottom(value->origin(), value->type());
}
Value* Procedure::addBoolConstant(Origin origin, TriState triState)
{
int32_t value = 0;
switch (triState) {
case FalseTriState:
value = 0;
break;
case TrueTriState:
value = 1;
break;
case MixedTriState:
return nullptr;
}
return addIntConstant(origin, Int32, value);
}
void Procedure::resetValueOwners()
{
for (BasicBlock* block : *this) {
for (Value* value : *block)
value->owner = block;
}
}
void Procedure::resetReachability()
{
if (shouldValidateIR()) {
// Validate the basic properties that we need for resetting reachability. We often reset
// reachability before IR validation, so without this mini-validation, you would crash inside
// B3::resetReachability() without getting any IR dump.
BasicBlock* badBlock = nullptr;
for (BasicBlock* block : *this) {
if (!block->size()) {
badBlock = block;
break;
}
if (!block->last()->as<ControlValue>()) {
badBlock = block;
break;
}
}
if (badBlock) {
dataLog("FATAL: Invalid basic block ", *badBlock, " while running Procedure::resetReachability().\n");
dataLog(*this);
RELEASE_ASSERT_NOT_REACHED();
}
}
B3::resetReachability(
m_blocks,
[&] (BasicBlock* deleted) {
// Gotta delete the values in this block.
for (Value* value : *deleted)
deleteValue(value);
});
}
void Procedure::invalidateCFG()
{
m_dominators = nullptr;
}
void Procedure::dump(PrintStream& out) const
{
IndexSet<Value> valuesInBlocks;
for (BasicBlock* block : *this) {
out.print(deepDump(*this, block));
valuesInBlocks.addAll(*block);
}
bool didPrint = false;
for (Value* value : values()) {
if (valuesInBlocks.contains(value))
continue;
if (!didPrint) {
dataLog("Orphaned values:\n");
didPrint = true;
}
dataLog(" ", deepDump(*this, value), "\n");
}
if (variables().size()) {
out.print("Variables:\n");
for (Variable* variable : variables())
out.print(" ", deepDump(variable), "\n");
}
if (stackSlots().size()) {
out.print("Stack slots:\n");
for (StackSlot* slot : stackSlots())
out.print(" ", pointerDump(slot), ": ", deepDump(slot), "\n");
}
if (m_byproducts->count())
out.print(*m_byproducts);
}
Vector<BasicBlock*> Procedure::blocksInPreOrder()
{
return B3::blocksInPreOrder(at(0));
}
Vector<BasicBlock*> Procedure::blocksInPostOrder()
{
return B3::blocksInPostOrder(at(0));
}
void Procedure::deleteStackSlot(StackSlot* stackSlot)
{
m_stackSlots.remove(stackSlot);
}
void Procedure::deleteVariable(Variable* variable)
{
m_variables.remove(variable);
}
void Procedure::deleteValue(Value* value)
{
m_values.remove(value);
}
void Procedure::deleteOrphans()
{
IndexSet<Value> valuesInBlocks;
for (BasicBlock* block : *this)
valuesInBlocks.addAll(*block);
// Since this method is not on any hot path, we do it conservatively: first a pass to
// identify the values to be removed, and then a second pass to remove them. This avoids any
// risk of the value iteration being broken by removals.
Vector<Value*, 16> toRemove;
for (Value* value : values()) {
if (!valuesInBlocks.contains(value))
toRemove.append(value);
}
for (Value* value : toRemove)
deleteValue(value);
}
Dominators& Procedure::dominators()
{
if (!m_dominators)
m_dominators = std::make_unique<Dominators>(*this);
return *m_dominators;
}
void Procedure::addFastConstant(const ValueKey& constant)
{
RELEASE_ASSERT(constant.isConstant());
m_fastConstants.add(constant);
}
bool Procedure::isFastConstant(const ValueKey& constant)
{
if (!constant)
return false;
return m_fastConstants.contains(constant);
}
void* Procedure::addDataSection(size_t size)
{
if (!size)
return nullptr;
std::unique_ptr<DataSection> dataSection = std::make_unique<DataSection>(size);
void* result = dataSection->data();
m_byproducts->add(WTFMove(dataSection));
return result;
}
unsigned Procedure::callArgAreaSize() const
{
return code().callArgAreaSize();
}
void Procedure::requestCallArgAreaSize(unsigned size)
{
code().requestCallArgAreaSize(size);
}
unsigned Procedure::frameSize() const
{
return code().frameSize();
}
const RegisterAtOffsetList& Procedure::calleeSaveRegisters() const
{
return code().calleeSaveRegisters();
}
Value* Procedure::addValueImpl(Value* value)
{
return m_values.add(std::unique_ptr<Value>(value));
}
void Procedure::setBlockOrderImpl(Vector<BasicBlock*>& blocks)
{
IndexSet<BasicBlock> blocksSet;
blocksSet.addAll(blocks);
for (BasicBlock* block : *this) {
if (!blocksSet.contains(block))
blocks.append(block);
}
// Place blocks into this's block list by first leaking all of the blocks and then readopting
// them.
for (auto& entry : m_blocks)
entry.release();
m_blocks.resize(blocks.size());
for (unsigned i = 0; i < blocks.size(); ++i) {
BasicBlock* block = blocks[i];
block->m_index = i;
m_blocks[i] = std::unique_ptr<BasicBlock>(block);
}
}
} } // namespace JSC::B3
#endif // ENABLE(B3_JIT)
|