|
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 |
pruneSinkingCandidates(); |
| 111 |
|
| 112 |
// At this point we are committed to sinking the sinking candidates. |
| 113 |
sinkSinkingCandidates(); |
| 114 |
|
| 115 |
return m_changed; |
| 116 |
} |
| 117 |
|
| 118 |
void identifyPhiSinkingCandidates() |
| 119 |
{ |
| 120 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 121 |
for (Node* node : *block) { |
| 122 |
if (node->op() != Phi) |
| 123 |
continue; |
| 124 |
|
| 125 |
PhiChildren::List& upsilons = phiChildren.upsilonsOf(node); |
| 126 |
if (upsilons.isEmpty()) |
| 127 |
continue; |
| 128 |
|
| 129 |
if (upsilons[0]->child1()->op() != MaterializeNewObject) |
| 130 |
continue; |
| 131 |
|
| 132 |
bool allChildrenAreUniquelyReferenced = true; |
| 133 |
for (Node* upsilonNode : upsilons) { |
| 134 |
if (upsilonNode->child1()->refCount() != 1) { |
| 135 |
allChildrenAreUniquelyReferenced = false; |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
if (!allChildrenAreUniquelyReferenced) |
| 140 |
continue; |
| 141 |
|
| 142 |
bool allChildrenAreSameType = true; |
| 143 |
for (unsigned i = upsilons.size(); i-- > 1;) { |
| 144 |
if (upsilons[i]->child1()->op() != upsilons[0]->child1()->op()) { |
| 145 |
allChildrenAreSameType = false; |
| 146 |
break; |
| 147 |
} |
| 148 |
} |
| 149 |
if (!allChildrenAreSameType) |
| 150 |
continue; |
| 151 |
|
| 152 |
// This could be a pessimisation if the various materializations are too |
| 153 |
// different from one another. We use a simple heuristic here: all of the |
| 154 |
// materializations must share at least half of their fields with the first |
| 155 |
// one. |
| 156 |
bool allAreSimilarEnough = true; |
| 157 |
for (unsigned i = upsilons.size(); i-- > 1;) { |
| 158 |
float score = |
| 159 |
upsilons[i]->child1()->objectMaterializationData().similarityScore( |
| 160 |
upsilons[0]->child1()->objectMaterializationData()); |
| 161 |
if (score < 0.5) { |
| 162 |
allAreSimilarEnough = false; |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
if (!allAreSimilarEnough) |
| 167 |
continue; |
| 168 |
|
| 169 |
m_phiSinkCandidates.append(node); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void pruneSinkingCandidates() |
| 175 |
{ |
| 176 |
m_sinkCandidates.clear(); |
| 177 |
BlockMap<HashSet<Node*>> sinkCandidatesAtHead(m_graph); |
| 178 |
BlockMap<HashSet<Node*>> sinkCandidatesAtTail(m_graph); |
| 179 |
|
| 180 |
bool changed; |
| 181 |
do { |
| 182 |
changed = false; |
| 183 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 184 |
HashSet<Node*> sinkCandidates = sinkCandidatesAtHead[block]; |
| 185 |
for (Node* node : *block) { |
| 186 |
handleNode( |
| 187 |
node, |
| 188 |
[&] () { |
| 189 |
sinkCandidates.add(node); |
| 190 |
}, |
| 191 |
[&] (Node* escapee) { |
| 192 |
sinkCandidates.remove(escapee); |
| 193 |
}); |
| 194 |
} |
| 195 |
if (sinkCandidatesAtTail[block] == sinkCandidates) |
| 196 |
continue; |
| 197 |
|
| 198 |
sinkCandidatesAtTail[block] = sinkCandidates; |
| 199 |
changed = true; |
| 200 |
|
| 201 |
for (BasicBlock* successorBlock : block->successors()) { |
| 202 |
for (Node* sinkCandidate : sinkCandidates) { |
| 203 |
if (!successorBlock->ssa->liveAtHead.contains(sinkCandidate)) |
| 204 |
continue; |
| 205 |
sinkCandidatesAtHead[successorBlock].add(sinkCandidate); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} while (changed); |
| 210 |
|
| 211 |
// A node is a sink candidate if it is in the sinkCandidatesAtTail of a block but not in |
| 212 |
// the liveAtTail of that same block. |
| 213 |
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 214 |
for (Node* sinkCandidate : sinkCandidatesAtTail[block]) { |
| 215 |
if (!block->ssa->liveAtTail.contains(sinkCandidate)) |
| 216 |
m_sinkCandidates.add(sinkCandidate); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
void sinkSinkingCandidates() |
| 222 |
{ |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
template<typename... Args> |
| 227 |
static void doNothing(Args...) { } |
| 228 |
|
| 229 |
template<typename SinkCandidateFunctor, typename EscapeFunctor> |
| 230 |
void handleNode( |
| 231 |
Node* node, |
| 232 |
const SinkCandidateFunctor& sinkCandidate, |
| 233 |
const EscapeFunctor& escape) |
| 234 |
{ |
| 235 |
switch (node->op()) { |
| 236 |
case NewObject: |
| 237 |
case MaterializeNewObject: |
| 238 |
sinkCandidate(); |
| 239 |
break; |
| 240 |
|
| 241 |
case Phi: |
| 242 |
if (m_phiSinkCandidates.contains(node)) |
| 243 |
sinkCandidate(); |
| 244 |
break; |
| 245 |
|
| 246 |
case CheckStructure: |
| 247 |
case GetByOffset: |
| 248 |
case MultiGetByOffset: |
| 249 |
case PutStructure: |
| 250 |
case GetGetterSetterByOffset: |
| 251 |
break; |
| 252 |
|
| 253 |
case PutByOffset: |
| 254 |
escape(node->child3().node()); |
| 255 |
break; |
| 256 |
|
| 257 |
case MultiPutByOffset: |
| 258 |
escape(node->child3().node()); |
| 259 |
break; |
| 260 |
|
| 261 |
default: |
| 262 |
m_graph.doToChildren( |
| 263 |
node, |
| 264 |
[&escape] (Edge edge) { |
| 265 |
escape(edge.node()); |
| 266 |
}); |
| 267 |
break; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
PhiChildren m_phiChildren; |
| 272 |
HashSet<Node*> m_phiSinkCandidates; |
| 273 |
HashSet<Node*> m_sinkCandidates; |
| 274 |
InsertionSet m_insertionSet; |
| 275 |
bool m_changed; |
| 276 |
}; |
| 277 |
|
| 278 |
bool performObjectAllocationSinking(Graph& graph) |
| 279 |
{ |
| 280 |
SamplingRegion samplingRegion("DFG Object Allocation Sinking Phase"); |
| 281 |
return runPhase<ObjectAllocationSinkingPhase>(graph); |
| 282 |
} |
| 283 |
|
| 284 |
} } // namespace JSC::DFG |
| 285 |
|
| 286 |
#endif // ENABLE(DFG_JIT) |
| 287 |
|