|
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 |
// - If all of the Upsilons of a Phi have a MaterializeNewObject that isn't used by anyone |
| 72 |
// else, then replace the Phi with the MaterializeNewObject. |
| 73 |
|
| 74 |
if (!performSinking(DontConsiderPhis)) |
| 75 |
return false; |
| 76 |
|
| 77 |
while (performSinking(ConsiderPhis)) { } |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
private: |
| 83 |
enum PhiMode { |
| 84 |
ConsiderPhis, |
| 85 |
DontConsiderPhis |
| 86 |
}; |
| 87 |
|
| 88 |
bool performSinking(PhiMode phiMode) |
| 89 |
{ |
| 90 |
m_changed = false; |
| 91 |
|
| 92 |
m_graph.computeRefCounts(); |
| 93 |
m_phiChildren = PhiChildren(m_graph); |
| 94 |
performLivenessAnalysis(); |
| 95 |
|
| 96 |
// Conduct one fixpoint iteration, that involves finding nodes that are candidates for |
| 97 |
// sinking and then sinking them. We proceed roughly as follows: |
| 98 |
// |
| 99 |
// 1) Identify Phis that it would be meaningful to sink. |
| 100 |
// 2) Figure out if the things that are sinkable would be profitable to sink. |
| 101 |
// 3) Sink them. |
| 102 |
|
| 103 |
m_phiSinkCandidates.clear(); |
| 104 |
if (phiMode == ConsiderPhis) |
| 105 |
identifyPhiSinkingCandidates(); |
| 106 |
|
| 107 |
// We want to only do sinking for those things that would benefit. We use a rough |
| 108 |
// heuristic: if it's possible for an allocation to make it to a return without escaping, |
| 109 |
// then we assume that it's worth sinking. It's a simple forward flow problem. |
| 110 |
// |
| 111 |
// It just so happens that this forward flow problem also tells us where we would |
| 112 |
// materialize those nodes that we choose to sink. |
| 113 |
pruneSinkingCandidates(); |
| 114 |
|
| 115 |
// At this point we are committed to sinking the sinking candidates. |
| 116 |
determineMaterializationPoints(); |
| 117 |
placeMaterializationPoints(); |
| 118 |
promoteSunkenFields(); |
| 119 |
|
| 120 |
return m_changed; |
| 121 |
} |
| 122 |
|
| 123 |
void identifyPhiSinkingCandidates() |
| 124 |
{ |
| 125 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 126 |
for (Node* node : *block) { |
| 127 |
if (node->op() != Phi) |
| 128 |
continue; |
| 129 |
|
| 130 |
PhiChildren::List& upsilons = phiChildren.upsilonsOf(node); |
| 131 |
if (upsilons.isEmpty()) |
| 132 |
continue; |
| 133 |
|
| 134 |
if (upsilons[0]->child1()->op() != MaterializeNewObject) |
| 135 |
continue; |
| 136 |
|
| 137 |
bool allChildrenAreUniquelyReferenced = true; |
| 138 |
for (Node* upsilonNode : upsilons) { |
| 139 |
if (upsilonNode->child1()->refCount() != 1) { |
| 140 |
allChildrenAreUniquelyReferenced = false; |
| 141 |
break; |
| 142 |
} |
| 143 |
} |
| 144 |
if (!allChildrenAreUniquelyReferenced) |
| 145 |
continue; |
| 146 |
|
| 147 |
bool allChildrenAreSameType = true; |
| 148 |
for (unsigned i = upsilons.size(); i-- > 1;) { |
| 149 |
if (upsilons[i]->child1()->op() != upsilons[0]->child1()->op()) { |
| 150 |
allChildrenAreSameType = false; |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
if (!allChildrenAreSameType) |
| 155 |
continue; |
| 156 |
|
| 157 |
// This could be a pessimisation if the various materializations are too |
| 158 |
// different from one another. We use a simple heuristic here: all of the |
| 159 |
// materializations must share at least half of their fields with the first |
| 160 |
// one. |
| 161 |
bool allAreSimilarEnough = true; |
| 162 |
for (unsigned i = upsilons.size(); i-- > 1;) { |
| 163 |
float score = |
| 164 |
upsilons[i]->child1()->objectMaterializationData().similarityScore( |
| 165 |
upsilons[0]->child1()->objectMaterializationData()); |
| 166 |
if (score < 0.5) { |
| 167 |
allAreSimilarEnough = false; |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
if (!allAreSimilarEnough) |
| 172 |
continue; |
| 173 |
|
| 174 |
m_phiSinkCandidates.append(node); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
void pruneSinkingCandidates() |
| 180 |
{ |
| 181 |
m_sinkCandidates.clear(); |
| 182 |
BlockMap<HashSet<Node*>> sinkCandidatesAtHead(m_graph); |
| 183 |
BlockMap<HashSet<Node*>> sinkCandidatesAtTail(m_graph); |
| 184 |
|
| 185 |
bool changed; |
| 186 |
do { |
| 187 |
changed = false; |
| 188 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 189 |
HashSet<Node*> sinkCandidates = sinkCandidatesAtHead[block]; |
| 190 |
for (Node* node : *block) { |
| 191 |
handleNode( |
| 192 |
node, |
| 193 |
[&] () { |
| 194 |
sinkCandidates.add(node); |
| 195 |
}, |
| 196 |
[&] (Node* escapee) { |
| 197 |
sinkCandidates.remove(escapee); |
| 198 |
}); |
| 199 |
} |
| 200 |
if (sinkCandidatesAtTail[block] == sinkCandidates) |
| 201 |
continue; |
| 202 |
|
| 203 |
sinkCandidatesAtTail[block] = sinkCandidates; |
| 204 |
changed = true; |
| 205 |
|
| 206 |
for (BasicBlock* successorBlock : block->successors()) { |
| 207 |
for (Node* sinkCandidate : sinkCandidates) { |
| 208 |
if (!successorBlock->ssa->liveAtHead.contains(sinkCandidate)) |
| 209 |
continue; |
| 210 |
sinkCandidatesAtHead[successorBlock].add(sinkCandidate); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} while (changed); |
| 215 |
|
| 216 |
// A node is a sink candidate if it is in the sinkCandidatesAtTail of a block but not in |
| 217 |
// the liveAtTail of that same block. |
| 218 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 219 |
for (Node* sinkCandidate : sinkCandidatesAtTail[block]) { |
| 220 |
if (!block->ssa->liveAtTail.contains(sinkCandidate)) |
| 221 |
m_sinkCandidates.add(sinkCandidate); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// A node should be materialized if it is a sinking candidate, and we haven't removed it |
| 226 |
// from the sink candidates set yet - since that means that it may not have been |
| 227 |
// materialized yet. |
| 228 |
// FIXME: oddly, anytime that we would have a basic block where something is "maybe |
| 229 |
// materialized", we would make it always materialized by having the not-materializing |
| 230 |
} |
| 231 |
|
| 232 |
void determineMaterializationPoints() |
| 233 |
{ |
| 234 |
// The premise of this pass is that if there exists a point in the program where some |
| 235 |
// path from a phantom allocation site to that point causes materialization, then *all* |
| 236 |
// paths cause materialization. |
| 237 |
|
| 238 |
m_materializationPoints.clear(); |
| 239 |
|
| 240 |
BlockMap<HashSet<Node*>> materializedAtHead; |
| 241 |
BlockMap<HashSet<Node*>> materializedAtTail; |
| 242 |
|
| 243 |
bool changed; |
| 244 |
do { |
| 245 |
changed = false; |
| 246 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 247 |
HashSet<Node*> materialized = materializedAtHead[block]; |
| 248 |
for (Node* node : *block) { |
| 249 |
handleNode( |
| 250 |
node, |
| 251 |
doNothing, |
| 252 |
[&] (Node* escapee) { |
| 253 |
materialized.add(escapee); |
| 254 |
}); |
| 255 |
} |
| 256 |
|
| 257 |
if (materialized == materializedAtTail[block]) |
| 258 |
continue; |
| 259 |
|
| 260 |
changed = true; |
| 261 |
|
| 262 |
for (BasicBlock* successorBlock : block->successors()) { |
| 263 |
for (Node* materializee : materialized) { |
| 264 |
if (!successorBlock->ssa->liveAtHead.contains(materializee)) |
| 265 |
continue; |
| 266 |
materializedAtHead[successorBlock].add(materializee); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} while (changed); |
| 271 |
|
| 272 |
// A materialization edge exists at any point where a node escapes but hasn't been |
| 273 |
// materialized yet. |
| 274 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 275 |
HashSet<Node*> materialized = materializedAtHead[block]; |
| 276 |
for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) { |
| 277 |
handleNode( |
| 278 |
node, |
| 279 |
doNothing, |
| 280 |
[&] (Node* escapee) { |
| 281 |
if (!materialized.add(escapee)) |
| 282 |
return; |
| 283 |
|
| 284 |
Node* materialize = m_insertionSet.insertNode( |
| 285 |
nodeIndex, escapee->prediction(), MaterializeNewObject, |
| 286 |
NodeOrigin( |
| 287 |
escapee->origin.semantic, |
| 288 |
node->origin.forExit), |
| 289 |
OpInfo(m_graph.m_objectMaterializationData.add())); |
| 290 |
|
| 291 |
m_materializationPoints.add(std::make_pair(node, escapee), materialize); |
| 292 |
}); |
| 293 |
} |
| 294 |
m_insertionSet.execute(block); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
void placeMaterializationPoints() |
| 299 |
{ |
| 300 |
SSACalculator ssaCalculator(m_graph); |
| 301 |
|
| 302 |
HashMap<Node*, SSACalculator::Variable*> nodeToVariable; |
| 303 |
Vector<Node*> indexToNode; |
| 304 |
|
| 305 |
for (Node* node : m_sinkCandidates) { |
| 306 |
SSACalculator::Variable* variable = ssaCalculator.newVariable(); |
| 307 |
nodeToVariable.add(node, variable); |
| 308 |
ASSERT(indexToNode.size() == variable->index()); |
| 309 |
indexToNode.append(node); |
| 310 |
|
| 311 |
node->setOpAndDefaultFlags(PhantomNewObject); |
| 312 |
} |
| 313 |
|
| 314 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 315 |
for (Node* node : *block) { |
| 316 |
if (SSACalculator::Variable* variable = nodeToVariable.get(node)) |
| 317 |
ssaCalculator.newDef(variable, block, node); |
| 318 |
|
| 319 |
m_graph.doToChildren( |
| 320 |
node, |
| 321 |
[&] (Edge edge) { |
| 322 |
Node* materialize = |
| 323 |
m_materializationPoints.get(std::make_pair(node, edge.node())); |
| 324 |
if (!materialize) |
| 325 |
return; |
| 326 |
|
| 327 |
ssaCalculator.newDef( |
| 328 |
nodeToVariable.get(edge.node()), block, materialize); |
| 329 |
}); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
ssaCalculator.computePhis( |
| 334 |
[&] (SSACalculator::Variable* variable, BasicBlock* block) -> Node* { |
| 335 |
Node* allocation = indexToNode[variable->index()]; |
| 336 |
if (!block->ssa->liveAtHead.contains(allocation)) |
| 337 |
return nullptr; |
| 338 |
|
| 339 |
Node* phiNode = m_graph.addNode( |
| 340 |
allocation->prediction(), Phi, NodeOrigin()); |
| 341 |
phiNode->mergeFlags(NodeResultJS); |
| 342 |
return phiNode; |
| 343 |
}); |
| 344 |
|
| 345 |
// Place Phis in the right places. Replace all uses of any allocation with the appropriate |
| 346 |
// materialization. Create the appropriate Upsilon nodes. |
| 347 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 348 |
HashMap<Node*, Node*> mapping; |
| 349 |
|
| 350 |
for (Node* candidate : block->ssa->liveAtHead) { |
| 351 |
SSACalculator::Variable* variable = nodeToVariable.get(candidate); |
| 352 |
if (!variable) |
| 353 |
continue; |
| 354 |
|
| 355 |
Def* def = ssaCalculator.reachingDefAtHead(block, variable); |
| 356 |
if (!def) |
| 357 |
continue; |
| 358 |
|
| 359 |
mapping.add(indexToNode[variable->index()], def->value()); |
| 360 |
} |
| 361 |
|
| 362 |
// FIXME: I feel like this completely messes up preexisting MovHints. |
| 363 |
for (SSACalculator::Def* phiDef : ssaCalculator.phisForBlock(block)) { |
| 364 |
m_insertionSet.insert(0, phiDef->value()); |
| 365 |
mapping.add(indexToNode[phiDef->variable()->index()], phiDef->value()); |
| 366 |
} |
| 367 |
|
| 368 |
for (Node* node : *block) { |
| 369 |
|
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
template<typename... Args> |
| 375 |
static void doNothing(Args...) { } |
| 376 |
|
| 377 |
template<typename SinkCandidateFunctor, typename EscapeFunctor> |
| 378 |
void handleNode( |
| 379 |
Node* node, |
| 380 |
const SinkCandidateFunctor& sinkCandidate, |
| 381 |
const EscapeFunctor& escape) |
| 382 |
{ |
| 383 |
switch (node->op()) { |
| 384 |
case NewObject: |
| 385 |
case MaterializeNewObject: |
| 386 |
sinkCandidate(); |
| 387 |
break; |
| 388 |
|
| 389 |
case Phi: |
| 390 |
if (m_phiSinkCandidates.contains(node)) |
| 391 |
sinkCandidate(); |
| 392 |
break; |
| 393 |
|
| 394 |
case CheckStructure: |
| 395 |
case GetByOffset: |
| 396 |
case MultiGetByOffset: |
| 397 |
case PutStructure: |
| 398 |
case GetGetterSetterByOffset: |
| 399 |
break; |
| 400 |
|
| 401 |
case PutByOffset: |
| 402 |
escape(node->child3().node()); |
| 403 |
break; |
| 404 |
|
| 405 |
case MultiPutByOffset: |
| 406 |
escape(node->child3().node()); |
| 407 |
break; |
| 408 |
|
| 409 |
default: |
| 410 |
m_graph.doToChildren( |
| 411 |
node, |
| 412 |
[&escape] (Edge edge) { |
| 413 |
escape(edge.node()); |
| 414 |
}); |
| 415 |
break; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
PhiChildren m_phiChildren; |
| 420 |
HashSet<Node*> m_phiSinkCandidates; |
| 421 |
HashSet<Node*> m_sinkCandidates; |
| 422 |
HashMap<std::pair<Node*, Node*>, Node*> m_materializationPoints; |
| 423 |
InsertionSet m_insertionSet; |
| 424 |
bool m_changed; |
| 425 |
}; |
| 426 |
|
| 427 |
bool performObjectAllocationSinking(Graph& graph) |
| 428 |
{ |
| 429 |
SamplingRegion samplingRegion("DFG Object Allocation Sinking Phase"); |
| 430 |
return runPhase<ObjectAllocationSinkingPhase>(graph); |
| 431 |
} |
| 432 |
|
| 433 |
} } // namespace JSC::DFG |
| 434 |
|
| 435 |
#endif // ENABLE(DFG_JIT) |
| 436 |
|