|
Line 0
Source/JavaScriptCore/dfg/DFGObjectAllocationSinkingPhase.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2014 Apple Inc. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* |
| 13 |
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
#include "DFGObjectAllocationSinkingPhase.h" |
| 28 |
|
| 29 |
#if ENABLE(DFG_JIT) |
| 30 |
|
| 31 |
#include "DFGAbstractHeap.h" |
| 32 |
#include "DFGClobberize.h" |
| 33 |
#include "DFGGraph.h" |
| 34 |
#include "DFGInsertionSet.h" |
| 35 |
#include "DFGPhase.h" |
| 36 |
#include "JSCInlines.h" |
| 37 |
|
| 38 |
namespace JSC { namespace DFG { |
| 39 |
|
| 40 |
class ObjectAllocationSinkingPhase : public Phase { |
| 41 |
public: |
| 42 |
ObjectAllocationSinkingPhase(Graph& graph) |
| 43 |
: Phase(graph, "object allocation sinking") |
| 44 |
, m_insertionSet(graph) |
| 45 |
{ |
| 46 |
} |
| 47 |
|
| 48 |
bool run() |
| 49 |
{ |
| 50 |
ASSERT(m_graph.m_fixpointState == FixpointNotConverged); |
| 51 |
|
| 52 |
m_changed = false; |
| 53 |
|
| 54 |
// Logically we wish to consider every NewObject and sink it. However it's definitely not |
| 55 |
// profitable to sink a NewObject that will always escape. So, first we do a very simple |
| 56 |
// forward flow analysis that determines the set of NewObject nodes that have any chance |
| 57 |
// of benefiting from object allocation sinking. Then we fixpoint the following rules: |
| 58 |
// |
| 59 |
// - For each NewObject, we turn the original NewObject into a PhantomNewObject and then |
| 60 |
// we insert MaterializeNewObject just before each escaping site. Note that Upsilons |
| 61 |
// constitute escaping sites. Then we use forward flow to eliminate unnecessary cases of |
| 62 |
// MaterializeNewObject. We use the SSACalculator to insert these materializations. If a |
| 63 |
// Phi is ever created that has the PhantomNewObject as one of its kids, then we insert |
| 64 |
// a MaterializeNewObject in front of it; this way we never have a Phi that is confused |
| 65 |
// about whether or not it has the real object. We then turn each PutByOffset over a |
| 66 |
// PhantomNewObject into a PutByOffsetHint. |
| 67 |
// |
| 68 |
// - We perform the same optimization for MaterializeNewObject. This allows us to cover |
| 69 |
// cases where we had MaterializeNewObject flowing into a PutByOffsetHint. |
| 70 |
// |
| 71 |
// We could also add this rule: |
| 72 |
// |
| 73 |
// - If all of the Upsilons of a Phi have a MaterializeNewObject that isn't used by anyone |
| 74 |
// else, then replace the Phi with the MaterializeNewObject. |
| 75 |
// |
| 76 |
// FIXME: Implement this. Note that this totally doable but it requires some gnarly |
| 77 |
// code, and to be effective the pruner needs to be aware of it. Currently any Upsilon |
| 78 |
// is considered to be an escape even by the pruner, so it's unlikely that we'll see |
| 79 |
// many cases of Phi over Materializations. |
| 80 |
// https://bugs.webkit.org/show_bug.cgi?id=136927 |
| 81 |
|
| 82 |
if (!performSinking(DontConsiderPhis)) |
| 83 |
return false; |
| 84 |
|
| 85 |
while (performSinking(ConsiderPhis)) { } |
| 86 |
|
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
private: |
| 91 |
bool performSinking(PhiMode phiMode) |
| 92 |
{ |
| 93 |
m_graph.computeRefCounts(); |
| 94 |
|
| 95 |
performLivenessAnalysis(); |
| 96 |
performOSRAvailabilityAnalysis(); |
| 97 |
|
| 98 |
// We want to only do sinking for those things that would benefit. We use a rough |
| 99 |
// heuristic: if it's possible for an allocation to make it to a return without escaping, |
| 100 |
// then we assume that it's worth sinking. It's a simple forward flow problem. |
| 101 |
// |
| 102 |
// It just so happens that this forward flow problem also tells us where we would |
| 103 |
// materialize those nodes that we choose to sink. |
| 104 |
pruneSinkingCandidates(); |
| 105 |
if (m_sinkCandidates.isEmpty()) |
| 106 |
return false; |
| 107 |
|
| 108 |
// At this point we are committed to sinking the sinking candidates. |
| 109 |
determineMaterializationPoints(); |
| 110 |
placeMaterializationPoints(); |
| 111 |
lowerOperationsOnPhantomAllocations(); |
| 112 |
promoteSunkenFields(); |
| 113 |
|
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
void pruneSinkingCandidates() |
| 118 |
{ |
| 119 |
m_sinkCandidates.clear(); |
| 120 |
BlockMap<HashSet<Node*>> sinkCandidatesAtHead(m_graph); |
| 121 |
BlockMap<HashSet<Node*>> sinkCandidatesAtTail(m_graph); |
| 122 |
|
| 123 |
bool changed; |
| 124 |
do { |
| 125 |
changed = false; |
| 126 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 127 |
HashSet<Node*> sinkCandidates = sinkCandidatesAtHead[block]; |
| 128 |
for (Node* node : *block) { |
| 129 |
handleNode( |
| 130 |
node, |
| 131 |
[&] () { |
| 132 |
sinkCandidates.add(node); |
| 133 |
}, |
| 134 |
[&] (Node* escapee) { |
| 135 |
sinkCandidates.remove(escapee); |
| 136 |
}); |
| 137 |
} |
| 138 |
if (sinkCandidatesAtTail[block] == sinkCandidates) |
| 139 |
continue; |
| 140 |
|
| 141 |
sinkCandidatesAtTail[block] = sinkCandidates; |
| 142 |
changed = true; |
| 143 |
|
| 144 |
for (BasicBlock* successorBlock : block->successors()) { |
| 145 |
for (Node* sinkCandidate : sinkCandidates) { |
| 146 |
if (!successorBlock->ssa->liveAtHead.contains(sinkCandidate)) |
| 147 |
continue; |
| 148 |
sinkCandidatesAtHead[successorBlock].add(sinkCandidate); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} while (changed); |
| 153 |
|
| 154 |
// A node is a sink candidate if it is in the sinkCandidatesAtTail of a block but not in |
| 155 |
// the liveAtTail of that same block. |
| 156 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 157 |
for (Node* sinkCandidate : sinkCandidatesAtTail[block]) { |
| 158 |
if (!block->ssa->liveAtTail.contains(sinkCandidate)) |
| 159 |
m_sinkCandidates.add(sinkCandidate); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// A node should be materialized if it is a sinking candidate, and we haven't removed it |
| 164 |
// from the sink candidates set yet - since that means that it may not have been |
| 165 |
// materialized yet. |
| 166 |
// FIXME: oddly, anytime that we would have a basic block where something is "maybe |
| 167 |
// materialized", we would make it always materialized by having the not-materializing |
| 168 |
} |
| 169 |
|
| 170 |
void determineMaterializationPoints() |
| 171 |
{ |
| 172 |
// The premise of this pass is that if there exists a point in the program where some |
| 173 |
// path from a phantom allocation site to that point causes materialization, then *all* |
| 174 |
// paths cause materialization. This should mean that there are never any redundant |
| 175 |
// materializations. |
| 176 |
|
| 177 |
m_edgeToMaterializationPoint.clear(); |
| 178 |
|
| 179 |
BlockMap<HashSet<Node*>> materializedAtHead; |
| 180 |
BlockMap<HashSet<Node*>> materializedAtTail; |
| 181 |
|
| 182 |
bool changed; |
| 183 |
do { |
| 184 |
changed = false; |
| 185 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 186 |
HashSet<Node*> materialized = materializedAtHead[block]; |
| 187 |
for (Node* node : *block) { |
| 188 |
handleNode( |
| 189 |
node, |
| 190 |
doNothing, |
| 191 |
[&] (Node* escapee) { |
| 192 |
if (m_sinkCandidates.contains(escapee)) |
| 193 |
materialized.add(escapee); |
| 194 |
}); |
| 195 |
} |
| 196 |
|
| 197 |
if (materialized == materializedAtTail[block]) |
| 198 |
continue; |
| 199 |
|
| 200 |
changed = true; |
| 201 |
|
| 202 |
for (BasicBlock* successorBlock : block->successors()) { |
| 203 |
for (Node* materializee : materialized) { |
| 204 |
if (!successorBlock->ssa->liveAtHead.contains(materializee)) |
| 205 |
continue; |
| 206 |
materializedAtHead[successorBlock].add(materializee); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
} while (changed); |
| 211 |
|
| 212 |
// A materialization edge exists at any point where a node escapes but hasn't been |
| 213 |
// materialized yet. |
| 214 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 215 |
HashSet<Node*> materialized = materializedAtHead[block]; |
| 216 |
for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) { |
| 217 |
Node* node = block->at(nodeIndex); |
| 218 |
|
| 219 |
handleNode( |
| 220 |
node, |
| 221 |
doNothing, |
| 222 |
[&] (Node* escapee) { |
| 223 |
if (!m_sinkCandidates.contains(escapee)) |
| 224 |
return; |
| 225 |
|
| 226 |
if (!materialized.add(escapee)) |
| 227 |
return; |
| 228 |
|
| 229 |
m_edgeToMaterializationPoint.add( |
| 230 |
std::make_pair(where, escapee), |
| 231 |
createMaterialize(escapee, node->origin)); |
| 232 |
}); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void placeMaterializationPoints() |
| 238 |
{ |
| 239 |
SSACalculator ssaCalculator(m_graph); |
| 240 |
|
| 241 |
HashMap<Node*, SSACalculator::Variable*> nodeToVariable; |
| 242 |
Vector<Node*> indexToNode; |
| 243 |
|
| 244 |
for (Node* node : m_sinkCandidates) { |
| 245 |
SSACalculator::Variable* variable = ssaCalculator.newVariable(); |
| 246 |
nodeToVariable.add(node, variable); |
| 247 |
ASSERT(indexToNode.size() == variable->index()); |
| 248 |
indexToNode.append(node); |
| 249 |
} |
| 250 |
|
| 251 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 252 |
for (Node* node : *block) { |
| 253 |
if (SSACalculator::Variable* variable = nodeToVariable.get(node)) |
| 254 |
ssaCalculator.newDef(variable, block, node); |
| 255 |
|
| 256 |
m_graph.doToChildren( |
| 257 |
node, |
| 258 |
[&] (Edge edge) { |
| 259 |
Node* materialize = |
| 260 |
m_edgeToMaterializationPoint.get(std::make_pair(node, edge.node())); |
| 261 |
if (!materialize) |
| 262 |
return; |
| 263 |
|
| 264 |
ssaCalculator.newDef( |
| 265 |
nodeToVariable.get(edge.node()), block, materialize); |
| 266 |
}); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
ssaCalculator.computePhis( |
| 271 |
[&] (SSACalculator::Variable* variable, BasicBlock* block) -> Node* { |
| 272 |
Node* allocation = indexToNode[variable->index()]; |
| 273 |
if (!block->ssa->liveAtHead.contains(allocation)) |
| 274 |
return nullptr; |
| 275 |
|
| 276 |
Node* phiNode = m_graph.addNode( |
| 277 |
allocation->prediction(), Phi, NodeOrigin()); |
| 278 |
phiNode->mergeFlags(NodeResultJS); |
| 279 |
return phiNode; |
| 280 |
}); |
| 281 |
|
| 282 |
// Place Phis in the right places. Replace all uses of any allocation with the appropriate |
| 283 |
// materialization. Create the appropriate Upsilon nodes. |
| 284 |
LocalOSRAvailabilityCalculator availabilityCalculator; |
| 285 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 286 |
HashMap<Node*, Node*> mapping; |
| 287 |
|
| 288 |
for (Node* candidate : block->ssa->liveAtHead) { |
| 289 |
SSACalculator::Variable* variable = nodeToVariable.get(candidate); |
| 290 |
if (!variable) |
| 291 |
continue; |
| 292 |
|
| 293 |
Def* def = ssaCalculator.reachingDefAtHead(block, variable); |
| 294 |
if (!def) |
| 295 |
continue; |
| 296 |
|
| 297 |
mapping.add(indexToNode[variable->index()], def->value()); |
| 298 |
} |
| 299 |
|
| 300 |
availabilityCalculator.beginBlock(block); |
| 301 |
for (SSACalculator::Def* phiDef : ssaCalculator.phisForBlock(block)) { |
| 302 |
m_insertionSet.insert(0, phiDef->value()); |
| 303 |
|
| 304 |
Node* originalNode = indexToNode[phiDef->variable()->index()]; |
| 305 |
insertOSRHintsForUpdate( |
| 306 |
m_insertionSet, 0, availabilityCalculator, originalNode, phiDef->value()); |
| 307 |
|
| 308 |
mapping.add(originalNode, phiDef->value()); |
| 309 |
} |
| 310 |
|
| 311 |
for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) { |
| 312 |
Node* node = block->at(nodeIndex); |
| 313 |
|
| 314 |
m_graph.doToChildEdges( |
| 315 |
node, |
| 316 |
[&] (Edge edge) { |
| 317 |
Node* materialize = m_edgeToMaterializationPoint.get( |
| 318 |
std::make_pair(node, edge.node())); |
| 319 |
if (materialize) { |
| 320 |
m_insertionSet.insert(nodeIndex, materialize); |
| 321 |
insertOSRHintsForUpdate( |
| 322 |
m_insertionSet, nodeIndex, availabilityCalculator, |
| 323 |
edge.node(), materialize); |
| 324 |
} |
| 325 |
}); |
| 326 |
|
| 327 |
availabilityCalculator.executeNode(node); |
| 328 |
|
| 329 |
m_graph.doToChildEdges( |
| 330 |
node, |
| 331 |
[&] (Edge& edge) { |
| 332 |
if (Node* materialize = mapping.get(edge.node())) |
| 333 |
edge.setNode(materialize); |
| 334 |
}); |
| 335 |
} |
| 336 |
|
| 337 |
size_t upsilonInsertionPoint = block->size() - 1; |
| 338 |
NodeOrigin upsilonOrigin = block->last()->origin; |
| 339 |
for (BasicBlock* successorBlock : block->successors()) { |
| 340 |
for (SSACalculator::Def* phiDef : ssaCalculator.phisForBlock(successorBlock)) { |
| 341 |
Node* phiNode = phiDef->value(); |
| 342 |
SSACalculator::Variable* variable = phiDef->variable(); |
| 343 |
Node* allocation = indexToNode[variable->index()]; |
| 344 |
|
| 345 |
Node* originalIncoming = mapping.get(allocation); |
| 346 |
Node* incoming; |
| 347 |
if (originalIncoming == allocation) { |
| 348 |
// If we have a Phi that combines materializations with the original |
| 349 |
// phantom object, then the path with the phantom object must materialize. |
| 350 |
|
| 351 |
incoming = createMaterialize(allocation, upsilonOrigin); |
| 352 |
m_insertionSet.insert(upsilonInsertionPoint, incoming); |
| 353 |
insertOSRHintsForUpdate( |
| 354 |
m_insertionSet, upsilonInsertionPoint, availabilityCalculator, |
| 355 |
originalIncoming, incoming); |
| 356 |
} else |
| 357 |
incoming = originalIncoming; |
| 358 |
|
| 359 |
Node* upsilon = m_insertionSet.insertNode( |
| 360 |
upsilonInsertionPoint, SpecNone, Upsilon, upsilonOrigin, |
| 361 |
OpInfo(phiNode), incoming->defaultEdge()); |
| 362 |
|
| 363 |
if (originalIncoming == allocation) { |
| 364 |
m_edgeToMaterializationPoint.add( |
| 365 |
std::make_pair(upsilon, allocation), incoming); |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
m_insertionSet.execute(block); |
| 371 |
} |
| 372 |
|
| 373 |
// At this point we have dummy materialization nodes along with edges to them. This means |
| 374 |
// that the part of the control flow graph that prefers to see actual object allocations |
| 375 |
// is completely fixed up, except for the materializations themselves. |
| 376 |
} |
| 377 |
|
| 378 |
void lowerNonReadingOperationsOnPhantomAllocations() |
| 379 |
{ |
| 380 |
// Lower everything but reading operations on phantom allocations. We absolutely have to |
| 381 |
// lower all writes so as to reveal them to the SSA calculator. We cannot lower reads |
| 382 |
// because the whole point is that those go away completely. |
| 383 |
|
| 384 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 385 |
for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) { |
| 386 |
Node* node = block->at(nodeIndex); |
| 387 |
switch (node->op()) { |
| 388 |
case PutByOffset: { |
| 389 |
if (m_sinkCandidates.contains(node->child2().node())) |
| 390 |
node->convertToPutByOffsetHint(); |
| 391 |
break; |
| 392 |
} |
| 393 |
|
| 394 |
case PutStructure: { |
| 395 |
if (m_sinkCandidates.contains(node->child1().node())) { |
| 396 |
Node* structure = m_insertionSet.insertNode( |
| 397 |
nodeIndex, SpecCellOther, PhantomStructure, node->origin, |
| 398 |
OpInfo(node->transition()->next)); |
| 399 |
node->convertToPutStructureHint(structure); |
| 400 |
} |
| 401 |
break; |
| 402 |
} |
| 403 |
|
| 404 |
case NewObject: { |
| 405 |
if (m_sinkCandidates.contains(node)) { |
| 406 |
Node* structure = m_insertionSet.insertNode( |
| 407 |
nodeIndex + 1, SpecCellOther, PhantomStructure, node->origin, |
| 408 |
OpInfo(node->structure())); |
| 409 |
m_insertionSet.insertNode( |
| 410 |
nodeIndex + 1, SpecNone, PutStructureHint, node->origin, |
| 411 |
Edge(node, KnownCellUse), Edge(structure, KnownCellUse)); |
| 412 |
node->convertToPhantomNewObject(); |
| 413 |
} |
| 414 |
break; |
| 415 |
} |
| 416 |
|
| 417 |
case MaterializeNewObject: { |
| 418 |
if (m_sinkCandidates.contains(node)) { |
| 419 |
m_insertionSet.insertNode( |
| 420 |
nodeIndex + 1, SpecNone, PutStructureHint, node->origin, |
| 421 |
Edge(node, KnownCellUse), m_graph.varArgChild(node, 0)); |
| 422 |
for (unsigned i = 0; i < node->objectMaterializationData().m_properties.size(); ++i) { |
| 423 |
m_insertionSet.insertNode( |
| 424 |
nodeIndex + 1, SpecNone, PutByOffsetHint, node->origin, |
| 425 |
Edge(node, KnownCellUse),m_graph.varArgChild(node, i + 1)); |
| 426 |
} |
| 427 |
node->convertToPhantomNewObject(); |
| 428 |
} |
| 429 |
break; |
| 430 |
} |
| 431 |
|
| 432 |
default: |
| 433 |
break; |
| 434 |
} |
| 435 |
} |
| 436 |
m_insertionSet.execute(block); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
void promoteSunkenFields() |
| 441 |
{ |
| 442 |
// Henceforth when we encounter a materialization point, we will want to ask *who* it is |
| 443 |
// a materialization for. Invert the map to be able to answer such questions. |
| 444 |
m_materializationPointToEscapee.clear(); |
| 445 |
for (auto pair : m_edgeToMaterializationPoint) |
| 446 |
m_materializationPointToEscapee.add(pair.value, pair.key.second); |
| 447 |
|
| 448 |
// Collect the set of heap locations that we will be operating over. |
| 449 |
HashSet<HeapLocation> locations; |
| 450 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 451 |
for (Node* node : *block) { |
| 452 |
promote( |
| 453 |
node, |
| 454 |
[&] (HeapLocation location, Edge) { |
| 455 |
locations.add(location); |
| 456 |
}, |
| 457 |
[&] (HeapLocation location) { |
| 458 |
locations.add(location); |
| 459 |
}); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
// Figure out which locations belong to which allocations. |
| 464 |
HashMap<Node*, Vector<HeapLocation>> locationsForAllocation; |
| 465 |
for (HeapLocation location : locations) { |
| 466 |
auto result = locationsForAllocation.add(location.base(), Vector<HeapLocation>()); |
| 467 |
ASSERT(!result.iterator->value.contains(location)); |
| 468 |
result.iterator->value.append(location); |
| 469 |
} |
| 470 |
|
| 471 |
// For each sunken thingy, make sure we create Bottom values for all of its fields. |
| 472 |
// Note that this has the hilarious slight inefficiency of creating redundant hints for |
| 473 |
// things that were previously materializations. This should only impact compile times and |
| 474 |
// not code quality, and it's necessary for soundness without some data structure hackage. |
| 475 |
// For example, a MaterializeNewObject that we choose to sink may have new fields added to |
| 476 |
// it conditionally. That would necessitate Bottoms. |
| 477 |
Node* bottom = nullptr; |
| 478 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 479 |
if (block == m_graph.block(0)) |
| 480 |
bottom = m_insertionSet.insertNode(0, SpecNone, Bottom, NodeOrigin()); |
| 481 |
|
| 482 |
for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) { |
| 483 |
Node* node = block->at(nodeIndex); |
| 484 |
for (HeapLocation location : locationsForAllocation.get(node)) { |
| 485 |
switch (location.kind()) { |
| 486 |
case StructureLoc: |
| 487 |
ASSERT(location.heap() == AbstractHeap(JSCell_structureID)); |
| 488 |
m_insertionSet.insertNode( |
| 489 |
nodeIndex + 1, SpecNone, PutStructureHint, node->origin, |
| 490 |
Edge(node, KnownCellUse), bottom); |
| 491 |
break; |
| 492 |
|
| 493 |
case NamedPropertyLoc: |
| 494 |
ASSERT(location.heap().kind() == NamedProperties); |
| 495 |
m_insertionSet.insertNode( |
| 496 |
nodeIndex + 1, SpecNone, PutByOffsetHint, node->origin, |
| 497 |
OpInfo(location.heap().payload().value()), |
| 498 |
Edge(node, KnownCellUse), bottom); |
| 499 |
break; |
| 500 |
|
| 501 |
default: |
| 502 |
DFG_CRASH(m_graph, node, "Bad heap location kind"); |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
m_insertionSet.execute(block); |
| 507 |
} |
| 508 |
|
| 509 |
SSACalculator ssaCalculator(m_graph); |
| 510 |
|
| 511 |
// Collect the set of "variables" that we will be sinking. |
| 512 |
HashMap<HeapLocation, SSACalculator::Variable*> locationToVariable; |
| 513 |
Vector<HeapLocation> indexToLocation; |
| 514 |
for (HeapLocation location : locations) { |
| 515 |
SSACalculator::Variable* variable = ssaCalculator.newVariable(); |
| 516 |
locationToVariable.add(location, variable); |
| 517 |
ASSERT(indexToLocation.size() == variable->index()); |
| 518 |
indexToLocation.append(location); |
| 519 |
} |
| 520 |
|
| 521 |
// Create Defs from the existing hints. |
| 522 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 523 |
for (Node* node : *block) { |
| 524 |
promote( |
| 525 |
node, |
| 526 |
[&] (HeapLocation location, Edge value) { |
| 527 |
SSACalculator::Variable* variable = locationToVariable.get(location); |
| 528 |
ssaCalculator.newDef(variable, block, value.node()); |
| 529 |
}); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
// OMG run the SSA calculator to create Phis! |
| 534 |
ssaCalculator.computePhis( |
| 535 |
[&] (SSACalculator::Variable* variable, BasicBlock* block) { |
| 536 |
|
| 537 |
}); |
| 538 |
} |
| 539 |
|
| 540 |
template<typename... Args> |
| 541 |
static void doNothing(Args...) { } |
| 542 |
|
| 543 |
template<typename SinkCandidateFunctor, typename EscapeFunctor> |
| 544 |
void handleNode( |
| 545 |
Node* node, |
| 546 |
const SinkCandidateFunctor& sinkCandidate, |
| 547 |
const EscapeFunctor& escape) |
| 548 |
{ |
| 549 |
switch (node->op()) { |
| 550 |
case NewObject: |
| 551 |
case MaterializeNewObject: |
| 552 |
sinkCandidate(); |
| 553 |
break; |
| 554 |
|
| 555 |
case CheckStructure: |
| 556 |
case GetByOffset: |
| 557 |
case MultiGetByOffset: |
| 558 |
case PutStructure: |
| 559 |
case GetGetterSetterByOffset: |
| 560 |
case MovHint: |
| 561 |
break; |
| 562 |
|
| 563 |
case PutByOffset: |
| 564 |
escape(node->child3().node()); |
| 565 |
break; |
| 566 |
|
| 567 |
case MultiPutByOffset: |
| 568 |
// FIXME: In the future we should be able to handle this. It's just a matter of |
| 569 |
// building the appropriate *Hint variant of this instruction, along with a |
| 570 |
// PhantomStructureSelect node - since this transforms the Structure in a conditional |
| 571 |
// way. |
| 572 |
// https://bugs.webkit.org/show_bug.cgi?id=136924 |
| 573 |
escape(node->child1().node()); |
| 574 |
escape(node->child2().node()); |
| 575 |
break; |
| 576 |
|
| 577 |
default: |
| 578 |
m_graph.doToChildren( |
| 579 |
node, |
| 580 |
[&escape] (Edge edge) { |
| 581 |
escape(edge.node()); |
| 582 |
}); |
| 583 |
break; |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
Node* createMaterialize(Node* escapee, const NodeOrigin whereOrigin) |
| 588 |
{ |
| 589 |
switch (escapee->op()) { |
| 590 |
case NewObject: |
| 591 |
case MaterializeNewObject: |
| 592 |
return m_graph.addNode( |
| 593 |
escapee->prediction(), MaterializeNewObject, |
| 594 |
NodeOrigin( |
| 595 |
escapee->origin.semantic, |
| 596 |
whereOrigin.forExit), |
| 597 |
OpInfo(m_graph.m_objectMaterializationData.add())); |
| 598 |
|
| 599 |
default: |
| 600 |
DFG_CRASH(m_graph, where, "Bad escapee op"); |
| 601 |
return nullpt; |
| 602 |
} |
| 603 |
|
| 604 |
return result; |
| 605 |
} |
| 606 |
|
| 607 |
template<typename WriteFunctor, typename ReadFunctor> |
| 608 |
void promote(Node* node, const WriteFunctor& write, const ReadFunctor& read) |
| 609 |
{ |
| 610 |
switch (node->op()) { |
| 611 |
case CheckStructure: |
| 612 |
if (m_sinkCandidates.contains(node->child1().node())) |
| 613 |
read(HeapLocation(StructureLoc, JSCell_structureID, node->child1())); |
| 614 |
break; |
| 615 |
|
| 616 |
case GetByOffset: |
| 617 |
case GetGetterSetterByOffset: |
| 618 |
if (m_sinkCandidates.contains(node->child2().node())) { |
| 619 |
unsigned identifierNumber = |
| 620 |
graph.m_storageAccessData[node->storageAccessDataIndex()].identifierNumber; |
| 621 |
AbstractHeap heap(NamedProperties, identifierNumber); |
| 622 |
read(HeapLocation(NamedPropertyLoc, heap, node->child2())); |
| 623 |
} |
| 624 |
break; |
| 625 |
|
| 626 |
case MultiGetByOffset: |
| 627 |
if (m_sinkCandidates.contains(node->child1().node())) { |
| 628 |
AbstractHeap heap(NamedProperties, node->multiGetByOffsetData().identifierNumber); |
| 629 |
read(HeapLocation(NamedPropertyLoc, heap, node->child1())); |
| 630 |
} |
| 631 |
break; |
| 632 |
|
| 633 |
case PutStructureHint: |
| 634 |
if (m_sinkCandidates.contains(node->child1().node())) |
| 635 |
write(HeapLocation(StructureLoc, JSCell_structureID, node->child1()), node->child2()); |
| 636 |
break; |
| 637 |
|
| 638 |
case PutByOffsetHint: |
| 639 |
if (m_sinkCandidates.contains(node->child1().node())) { |
| 640 |
unsigned identifierNumber = node->identifierNumber(); |
| 641 |
AbstractHeap heap(NamedProperties, identifierNumber); |
| 642 |
write(HeapLocation(NamedPropertyLoc, heap, node->child1()), node->child2().node()); |
| 643 |
} |
| 644 |
break; |
| 645 |
|
| 646 |
default: |
| 647 |
break; |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
PhiChildren m_phiChildren; |
| 652 |
HashSet<Node*> m_sinkCandidates; |
| 653 |
HashMap<std::pair<Node*, Node*>, Node*> m_edgeToMaterializationPoint; |
| 654 |
HashMap<Node*, Node*> m_materializationPointToEscapee; |
| 655 |
InsertionSet m_insertionSet; |
| 656 |
}; |
| 657 |
|
| 658 |
bool performObjectAllocationSinking(Graph& graph) |
| 659 |
{ |
| 660 |
SamplingRegion samplingRegion("DFG Object Allocation Sinking Phase"); |
| 661 |
return runPhase<ObjectAllocationSinkingPhase>(graph); |
| 662 |
} |
| 663 |
|
| 664 |
} } // namespace JSC::DFG |
| 665 |
|
| 666 |
#endif // ENABLE(DFG_JIT) |
| 667 |
|