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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
/*
* Copyright (C) 2013-2023 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.
*/
#pragma once
#if ENABLE(DFG_JIT)
#include "DFGAbstractInterpreterClobberState.h"
#include "DFGAbstractValue.h"
#include "DFGBranchDirection.h"
#include "DFGFlowMap.h"
#include "DFGGraph.h"
#include "DFGNode.h"
#include <wtf/TZoneMalloc.h>
namespace JSC { namespace DFG {
class InPlaceAbstractState {
WTF_MAKE_TZONE_ALLOCATED(InPlaceAbstractState);
public:
InPlaceAbstractState(Graph&);
~InPlaceAbstractState();
explicit operator bool() const { return true; }
void createValueForNode(NodeFlowProjection) { }
ALWAYS_INLINE bool hasClearedAbstractState(NodeFlowProjection node)
{
return !m_abstractValues.at(node);
}
ALWAYS_INLINE AbstractValue& fastForward(AbstractValue& value)
{
value.fastForwardTo(m_effectEpoch);
return value;
}
ALWAYS_INLINE void fastForwardAndFilterUnproven(AbstractValue& value, SpeculatedType type)
{
value.fastForwardToAndFilterUnproven(m_effectEpoch, type);
}
ALWAYS_INLINE AbstractValue& forNodeWithoutFastForward(NodeFlowProjection node)
{
ASSERT(!node->isTuple());
return m_abstractValues.at(node);
}
ALWAYS_INLINE AbstractValue& forNodeWithoutFastForward(Edge edge)
{
ASSERT(!edge.node()->isTuple());
return forNodeWithoutFastForward(edge.node());
}
ALWAYS_INLINE AbstractValue& forNode(NodeFlowProjection node)
{
ASSERT(!node->isTuple());
return fastForward(m_abstractValues.at(node));
}
ALWAYS_INLINE AbstractValue& forNode(Edge edge)
{
return forNode(edge.node());
}
ALWAYS_INLINE void clearForNode(NodeFlowProjection node)
{
AbstractValue& value = m_abstractValues.at(node);
value.clear();
value.m_effectEpoch = m_effectEpoch;
}
ALWAYS_INLINE void clearForNode(Edge edge)
{
clearForNode(edge.node());
}
template<typename... Arguments>
ALWAYS_INLINE void setForNode(NodeFlowProjection node, Arguments&&... arguments)
{
ASSERT(!node->isTuple());
AbstractValue& value = m_abstractValues.at(node);
value.set(m_graph, std::forward<Arguments>(arguments)...);
value.m_effectEpoch = m_effectEpoch;
}
template<typename... Arguments>
ALWAYS_INLINE void setForNode(Edge edge, Arguments&&... arguments)
{
setForNode(edge.node(), std::forward<Arguments>(arguments)...);
}
template<typename... Arguments>
ALWAYS_INLINE void setTypeForNode(NodeFlowProjection node, Arguments&&... arguments)
{
ASSERT(!node->isTuple());
AbstractValue& value = m_abstractValues.at(node);
value.setType(m_graph, std::forward<Arguments>(arguments)...);
value.m_effectEpoch = m_effectEpoch;
}
template<typename... Arguments>
ALWAYS_INLINE void setTypeForNode(Edge edge, Arguments&&... arguments)
{
setTypeForNode(edge.node(), std::forward<Arguments>(arguments)...);
}
template<typename... Arguments>
ALWAYS_INLINE void setNonCellTypeForNode(NodeFlowProjection node, Arguments&&... arguments)
{
ASSERT(!node->isTuple());
AbstractValue& value = m_abstractValues.at(node);
value.setNonCellType(std::forward<Arguments>(arguments)...);
value.m_effectEpoch = m_effectEpoch;
}
template<typename... Arguments>
ALWAYS_INLINE void setNonCellTypeForNode(Edge edge, Arguments&&... arguments)
{
setNonCellTypeForNode(edge.node(), std::forward<Arguments>(arguments)...);
}
ALWAYS_INLINE void makeBytecodeTopForNode(NodeFlowProjection node)
{
ASSERT(!node->isTuple());
AbstractValue& value = m_abstractValues.at(node);
value.makeBytecodeTop();
value.m_effectEpoch = m_effectEpoch;
}
ALWAYS_INLINE void makeBytecodeTopForNode(Edge edge)
{
makeBytecodeTopForNode(edge.node());
}
ALWAYS_INLINE void makeHeapTopForNode(NodeFlowProjection node)
{
ASSERT(!node->isTuple());
AbstractValue& value = m_abstractValues.at(node);
value.makeHeapTop();
value.m_effectEpoch = m_effectEpoch;
}
ALWAYS_INLINE void makeHeapTopForNode(Edge edge)
{
makeHeapTopForNode(edge.node());
}
ALWAYS_INLINE AbstractValue& forTupleNodeWithoutFastForward(NodeFlowProjection node, unsigned index)
{
ASSERT(node->isTuple());
ASSERT(index < node->tupleSize());
return m_tupleAbstractValues.at(node->tupleOffset() + index);
}
ALWAYS_INLINE AbstractValue& forTupleNode(NodeFlowProjection node, unsigned index)
{
ASSERT(index < node->tupleSize());
return fastForward(m_tupleAbstractValues.at(node->tupleOffset() + index));
}
ALWAYS_INLINE AbstractValue& forTupleNode(Edge edge, unsigned index)
{
return forTupleNode(edge.node(), index);
}
ALWAYS_INLINE void clearForTupleNode(NodeFlowProjection node, unsigned index)
{
ASSERT(index < node->tupleSize());
AbstractValue& value = m_tupleAbstractValues.at(node->tupleOffset() + index);
value.clear();
value.m_effectEpoch = m_effectEpoch;
}
ALWAYS_INLINE void clearForTupleNode(Edge edge, unsigned index)
{
clearForTupleNode(edge.node(), index);
}
template<typename... Arguments>
ALWAYS_INLINE void setForTupleNode(NodeFlowProjection node, unsigned index, Arguments&&... arguments)
{
ASSERT(index < node->tupleSize());
AbstractValue& value = m_tupleAbstractValues.at(node->tupleOffset() + index);
value.set(m_graph, std::forward<Arguments>(arguments)...);
value.m_effectEpoch = m_effectEpoch;
}
template<typename... Arguments>
ALWAYS_INLINE void setForTupleNode(Edge edge, unsigned index, Arguments&&... arguments)
{
setForTupleNode(edge.node(), index, std::forward<Arguments>(arguments)...);
}
template<typename... Arguments>
ALWAYS_INLINE void setTypeForTupleNode(NodeFlowProjection node, unsigned index, Arguments&&... arguments)
{
ASSERT(index < node->tupleSize());
AbstractValue& value = m_tupleAbstractValues.at(node->tupleOffset() + index);
value.setType(m_graph, std::forward<Arguments>(arguments)...);
value.m_effectEpoch = m_effectEpoch;
}
template<typename... Arguments>
ALWAYS_INLINE void setTypeForTupleNode(Edge edge, unsigned index, Arguments&&... arguments)
{
setTypeForTupleNode(edge.node(), index, std::forward<Arguments>(arguments)...);
}
template<typename... Arguments>
ALWAYS_INLINE void setNonCellTypeForTupleNode(NodeFlowProjection node, unsigned index, Arguments&&... arguments)
{
ASSERT(index < node->tupleSize());
AbstractValue& value = m_tupleAbstractValues.at(node->tupleOffset() + index);
value.setNonCellType(std::forward<Arguments>(arguments)...);
value.m_effectEpoch = m_effectEpoch;
}
template<typename... Arguments>
ALWAYS_INLINE void setNonCellTypeForTupleNode(Edge edge, unsigned index, Arguments&&... arguments)
{
setNonCellTypeForTupleNode(edge.node(), index, std::forward<Arguments>(arguments)...);
}
ALWAYS_INLINE void makeBytecodeTopForTupleNode(NodeFlowProjection node, unsigned index)
{
ASSERT(index < node->tupleSize());
AbstractValue& value = m_tupleAbstractValues.at(node->tupleOffset() + index);
value.makeBytecodeTop();
value.m_effectEpoch = m_effectEpoch;
}
ALWAYS_INLINE void makeBytecodeTopForTupleNode(Edge edge, unsigned index)
{
makeBytecodeTopForTupleNode(edge.node(), index);
}
ALWAYS_INLINE void makeHeapTopForTupleNode(NodeFlowProjection node, unsigned index)
{
ASSERT(index < node->tupleSize());
AbstractValue& value = m_tupleAbstractValues.at(node->tupleOffset() + index);
value.makeHeapTop();
value.m_effectEpoch = m_effectEpoch;
}
ALWAYS_INLINE void makeHeapTopForTupleNode(Edge edge, unsigned index)
{
makeHeapTopForTupleNode(edge.node(), index);
}
Operands<AbstractValue>& variablesForDebugging();
unsigned size() const { return m_variables.size(); }
unsigned numberOfArguments() const { return m_variables.numberOfArguments(); }
unsigned numberOfLocals() const { return m_variables.numberOfLocals(); }
unsigned numberOfTmps() const { return m_variables.numberOfTmps(); }
AbstractValue& atIndex(size_t index)
{
activateVariableIfNecessary(index);
return fastForward(m_variables[index]);
}
AbstractValue& operand(Operand operand)
{
return atIndex(m_variables.operandIndex(operand));
}
AbstractValue& local(size_t index)
{
return atIndex(m_variables.localIndex(index));
}
AbstractValue& argument(size_t index)
{
return atIndex(m_variables.argumentIndex(index));
}
// Call this before beginning CFA to initialize the abstract values of
// arguments, and to indicate which blocks should be listed for CFA
// execution.
void initialize();
// Start abstractly executing the given basic block. Initializes the
// notion of abstract state to what we believe it to be at the head
// of the basic block, according to the basic block's data structures.
// This method also sets cfaShouldRevisit to false.
void beginBasicBlock(BasicBlock*);
BasicBlock* block() const { return m_block; }
// Finish abstractly executing a basic block. If MergeToTail or
// MergeToSuccessors is passed, then this merges everything we have
// learned about how the state changes during this block's execution into
// the block's data structures.
//
// Returns true if the state of the block at the tail was changed,
// and, if the state at the heads of successors was changed.
// A true return means that you must revisit (at least) the successor
// blocks. This also sets cfaShouldRevisit to true for basic blocks
// that must be visited next.
bool endBasicBlock();
// Reset the AbstractState. This throws away any results, and at this point
// you can safely call beginBasicBlock() on any basic block.
void reset();
AbstractInterpreterClobberState clobberState() const { return m_clobberState; }
// Would have the last executed node clobbered things had we not found a way to fold it?
bool didClobberOrFolded() const { return clobberState() != AbstractInterpreterClobberState::NotClobbered; }
// Did the last executed node clobber the world?
bool didClobber() const { return clobberState() == AbstractInterpreterClobberState::ClobberedStructures; }
// Are structures currently clobbered?
StructureClobberState structureClobberState() const { return m_structureClobberState; }
// Is the execution state still valid? This will be false if execute() has
// returned false previously.
bool isValid() const { return m_isValid; }
// Merge the abstract state stored at the first block's tail into the second
// block's head. Returns true if the second block's state changed. If so,
// that block must be abstractly interpreted again. This also sets
// to->cfaShouldRevisit to true, if it returns true, or if to has not been
// visited yet.
bool merge(BasicBlock* from, BasicBlock* to);
// Merge the abstract state stored at the block's tail into all of its
// successors. Returns true if any of the successors' states changed. Note
// that this is automatically called in endBasicBlock() if MergeMode is
// MergeToSuccessors.
bool mergeToSuccessors(BasicBlock*);
void clobberStructures() { m_effectEpoch.clobber(); }
void observeInvalidationPoint() { m_effectEpoch.observeInvalidationPoint(); }
// Methods intended to be called from AbstractInterpreter.
void setClobberState(AbstractInterpreterClobberState state) { m_clobberState = state; }
void mergeClobberState(AbstractInterpreterClobberState state) { m_clobberState = mergeClobberStates(m_clobberState, state); }
void setStructureClobberState(StructureClobberState value) { m_structureClobberState = value; }
void setIsValid(bool isValid) { m_isValid = isValid; }
void setBranchDirection(BranchDirection branchDirection) { m_branchDirection = branchDirection; }
void setShouldTryConstantFolding(bool) { }
void setProofStatus(Edge& edge, ProofStatus status)
{
edge.setProofStatus(status);
}
private:
ALWAYS_INLINE void activateVariableIfNecessary(size_t variableIndex)
{
if (!m_activeVariables[variableIndex])
activateVariable(variableIndex);
}
void activateVariable(size_t variableIndex);
void activateAllVariables();
static bool mergeVariableBetweenBlocks(AbstractValue& destination, AbstractValue& source, Node* destinationNode, Node* sourceNode);
Graph& m_graph;
FlowMap<AbstractValue>& m_abstractValues;
Operands<AbstractValue> m_variables;
Vector<AbstractValue> m_tupleAbstractValues;
FastBitVector m_activeVariables;
BasicBlock* m_block;
bool m_isValid;
AbstractInterpreterClobberState m_clobberState;
StructureClobberState m_structureClobberState;
AbstractValueClobberEpoch m_epochAtHead;
AbstractValueClobberEpoch m_effectEpoch;
BranchDirection m_branchDirection; // This is only set for blocks that end in Branch and that execute to completion (i.e. m_isValid == true).
};
} } // namespace JSC::DFG
#endif // ENABLE(DFG_JIT)
|