| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef jit_MIRGraph_h |
| 8 |
#define jit_MIRGraph_h |
| 9 |
|
| 10 |
// This file declares the data structures used to build a control-flow graph |
| 11 |
// containing MIR. |
| 12 |
|
| 13 |
#include "jit/CompileInfo.h" |
| 14 |
#include "jit/FixedList.h" |
| 15 |
#include "jit/InlineScriptTree.h" |
| 16 |
#include "jit/JitAllocPolicy.h" |
| 17 |
#include "jit/MIR-wasm.h" |
| 18 |
#include "jit/MIR.h" |
| 19 |
|
| 20 |
namespace js { |
| 21 |
namespace jit { |
| 22 |
|
| 23 |
class MBasicBlock; |
| 24 |
class MIRGraph; |
| 25 |
class MStart; |
| 26 |
|
| 27 |
class MDefinitionIterator; |
| 28 |
|
| 29 |
using MInstructionIterator = InlineListIterator<MInstruction>; |
| 30 |
using MInstructionReverseIterator = InlineListReverseIterator<MInstruction>; |
| 31 |
using MPhiIterator = InlineListIterator<MPhi>; |
| 32 |
|
| 33 |
#ifdef DEBUG |
| 34 |
using MResumePointIterator = InlineForwardListIterator<MResumePoint>; |
| 35 |
#endif |
| 36 |
|
| 37 |
class LBlock; |
| 38 |
|
| 39 |
class MBasicBlock : public TempObject, public InlineListNode<MBasicBlock> { |
| 40 |
public: |
| 41 |
enum Kind { |
| 42 |
NORMAL, |
| 43 |
PENDING_LOOP_HEADER, |
| 44 |
LOOP_HEADER, |
| 45 |
SPLIT_EDGE, |
| 46 |
FAKE_LOOP_PRED, |
| 47 |
INTERNAL, |
| 48 |
DEAD |
| 49 |
}; |
| 50 |
|
| 51 |
private: |
| 52 |
MBasicBlock(MIRGraph& graph, const CompileInfo& info, BytecodeSite* site, |
| 53 |
Kind kind); |
| 54 |
[[nodiscard]] bool init(); |
| 55 |
void copySlots(MBasicBlock* from); |
| 56 |
[[nodiscard]] bool inherit(TempAllocator& alloc, size_t stackDepth, |
| 57 |
MBasicBlock* maybePred, uint32_t popped); |
| 58 |
|
| 59 |
// This block cannot be reached by any means. |
| 60 |
bool unreachable_ = false; |
| 61 |
|
| 62 |
// This block will unconditionally bail out. |
| 63 |
bool alwaysBails_ = false; |
| 64 |
|
| 65 |
// Will be used for branch hinting in wasm. |
| 66 |
wasm::BranchHint branchHint_ = wasm::BranchHint::Invalid; |
| 67 |
|
| 68 |
// Pushes a copy of a local variable or argument. |
| 69 |
void pushVariable(uint32_t slot) { push(slots_[slot]); } |
| 70 |
|
| 71 |
// Sets a variable slot to the top of the stack, correctly creating copies |
| 72 |
// as needed. |
| 73 |
void setVariable(uint32_t slot) { |
| 74 |
MOZ_ASSERT(stackPosition_ > info_.firstStackSlot()); |
| 75 |
setSlot(slot, slots_[stackPosition_ - 1]); |
| 76 |
} |
| 77 |
|
| 78 |
enum ReferencesType { |
| 79 |
RefType_None = 0, |
| 80 |
|
| 81 |
// Assert that the instruction is unused. |
| 82 |
RefType_AssertNoUses = 1 << 0, |
| 83 |
|
| 84 |
// Discard the operands of the resume point / instructions if the |
| 85 |
// following flag are given too. |
| 86 |
RefType_DiscardOperands = 1 << 1, |
| 87 |
RefType_DiscardResumePoint = 1 << 2, |
| 88 |
RefType_DiscardInstruction = 1 << 3, |
| 89 |
|
| 90 |
// Discard operands of the instruction and its resume point. |
| 91 |
RefType_DefaultNoAssert = RefType_DiscardOperands | |
| 92 |
RefType_DiscardResumePoint | |
| 93 |
RefType_DiscardInstruction, |
| 94 |
|
| 95 |
// Discard everything and assert that the instruction is not used. |
| 96 |
RefType_Default = RefType_AssertNoUses | RefType_DefaultNoAssert, |
| 97 |
|
| 98 |
// Discard resume point operands only, without discarding the operands |
| 99 |
// of the current instruction. Asserts that the instruction is unused. |
| 100 |
RefType_IgnoreOperands = RefType_AssertNoUses | RefType_DiscardOperands | |
| 101 |
RefType_DiscardResumePoint |
| 102 |
}; |
| 103 |
|
| 104 |
void discardResumePoint(MResumePoint* rp, |
| 105 |
ReferencesType refType = RefType_Default); |
| 106 |
void removeResumePoint(MResumePoint* rp); |
| 107 |
|
| 108 |
// Remove all references to an instruction such that it can be removed from |
| 109 |
// the list of instruction, without keeping any dangling pointer to it. This |
| 110 |
// includes the operands of the instruction, and the resume point if |
| 111 |
// present. |
| 112 |
void prepareForDiscard(MInstruction* ins, |
| 113 |
ReferencesType refType = RefType_Default); |
| 114 |
|
| 115 |
public: |
| 116 |
/////////////////////////////////////////////////////// |
| 117 |
////////// BEGIN GRAPH BUILDING INSTRUCTIONS ////////// |
| 118 |
/////////////////////////////////////////////////////// |
| 119 |
|
| 120 |
// Creates a new basic block for a MIR generator. If |pred| is not nullptr, |
| 121 |
// its slots and stack depth are initialized from |pred|. |
| 122 |
static MBasicBlock* New(MIRGraph& graph, size_t stackDepth, |
| 123 |
const CompileInfo& info, MBasicBlock* maybePred, |
| 124 |
BytecodeSite* site, Kind kind); |
| 125 |
static MBasicBlock* New(MIRGraph& graph, const CompileInfo& info, |
| 126 |
MBasicBlock* pred, Kind kind); |
| 127 |
static MBasicBlock* NewPopN(MIRGraph& graph, const CompileInfo& info, |
| 128 |
MBasicBlock* pred, BytecodeSite* site, Kind kind, |
| 129 |
uint32_t popn); |
| 130 |
static MBasicBlock* NewPendingLoopHeader(MIRGraph& graph, |
| 131 |
const CompileInfo& info, |
| 132 |
MBasicBlock* pred, |
| 133 |
BytecodeSite* site); |
| 134 |
static MBasicBlock* NewSplitEdge(MIRGraph& graph, MBasicBlock* pred, |
| 135 |
size_t predEdgeIdx, MBasicBlock* succ); |
| 136 |
static MBasicBlock* NewFakeLoopPredecessor(MIRGraph& graph, |
| 137 |
MBasicBlock* header); |
| 138 |
|
| 139 |
// Create a new basic block for internal control flow not present in the |
| 140 |
// original CFG. |
| 141 |
static MBasicBlock* NewInternal(MIRGraph& graph, MBasicBlock* orig, |
| 142 |
MResumePoint* activeResumePoint); |
| 143 |
|
| 144 |
bool dominates(const MBasicBlock* other) const { |
| 145 |
return other->domIndex() - domIndex() < numDominated(); |
| 146 |
} |
| 147 |
|
| 148 |
void setId(uint32_t id) { id_ = id; } |
| 149 |
|
| 150 |
// Mark this block (and only this block) as unreachable. |
| 151 |
void setUnreachable() { |
| 152 |
MOZ_ASSERT(!unreachable_); |
| 153 |
setUnreachableUnchecked(); |
| 154 |
} |
| 155 |
void setUnreachableUnchecked() { unreachable_ = true; } |
| 156 |
bool unreachable() const { return unreachable_; } |
| 157 |
|
| 158 |
void setAlwaysBails() { alwaysBails_ = true; } |
| 159 |
bool alwaysBails() const { return alwaysBails_; } |
| 160 |
|
| 161 |
// Move the definition to the top of the stack. |
| 162 |
void pick(int32_t depth); |
| 163 |
|
| 164 |
// Move the top of the stack definition under the depth-th stack value. |
| 165 |
void unpick(int32_t depth); |
| 166 |
|
| 167 |
// Exchange 2 stack slots at the defined depth |
| 168 |
void swapAt(int32_t depth); |
| 169 |
|
| 170 |
// Note: most of the methods below are hot. Do not un-inline them without |
| 171 |
// measuring the impact. |
| 172 |
|
| 173 |
// Gets the instruction associated with various slot types. |
| 174 |
MDefinition* peek(int32_t depth) { |
| 175 |
MOZ_ASSERT(depth < 0); |
| 176 |
MOZ_ASSERT(stackPosition_ + depth >= info_.firstStackSlot()); |
| 177 |
return peekUnchecked(depth); |
| 178 |
} |
| 179 |
|
| 180 |
MDefinition* peekUnchecked(int32_t depth) { |
| 181 |
MOZ_ASSERT(depth < 0); |
| 182 |
return getSlot(stackPosition_ + depth); |
| 183 |
} |
| 184 |
|
| 185 |
MDefinition* environmentChain(); |
| 186 |
MDefinition* argumentsObject(); |
| 187 |
|
| 188 |
// Increase the number of slots available |
| 189 |
[[nodiscard]] bool increaseSlots(size_t num); |
| 190 |
[[nodiscard]] bool ensureHasSlots(size_t num); |
| 191 |
|
| 192 |
// Initializes a slot value; must not be called for normal stack |
| 193 |
// operations, as it will not create new SSA names for copies. |
| 194 |
void initSlot(uint32_t slot, MDefinition* ins) { |
| 195 |
slots_[slot] = ins; |
| 196 |
if (entryResumePoint()) { |
| 197 |
entryResumePoint()->initOperand(slot, ins); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
// Sets the instruction associated with various slot types. The |
| 202 |
// instruction must lie at the top of the stack. |
| 203 |
void setLocal(uint32_t local) { setVariable(info_.localSlot(local)); } |
| 204 |
void setArg(uint32_t arg) { setVariable(info_.argSlot(arg)); } |
| 205 |
void setSlot(uint32_t slot, MDefinition* ins) { slots_[slot] = ins; } |
| 206 |
|
| 207 |
// Tracks an instruction as being pushed onto the operand stack. |
| 208 |
void push(MDefinition* ins) { |
| 209 |
MOZ_ASSERT(stackPosition_ < nslots()); |
| 210 |
slots_[stackPosition_++] = ins; |
| 211 |
} |
| 212 |
void pushArg(uint32_t arg) { pushVariable(info_.argSlot(arg)); } |
| 213 |
void pushArgUnchecked(uint32_t arg) { |
| 214 |
pushVariable(info_.argSlotUnchecked(arg)); |
| 215 |
} |
| 216 |
void pushLocal(uint32_t local) { pushVariable(info_.localSlot(local)); } |
| 217 |
void pushSlot(uint32_t slot) { pushVariable(slot); } |
| 218 |
void setEnvironmentChain(MDefinition* ins); |
| 219 |
void setArgumentsObject(MDefinition* ins); |
| 220 |
|
| 221 |
// Returns the top of the stack, then decrements the virtual stack pointer. |
| 222 |
MDefinition* pop() { |
| 223 |
MOZ_ASSERT(stackPosition_ > info_.firstStackSlot()); |
| 224 |
return slots_[--stackPosition_]; |
| 225 |
} |
| 226 |
void popn(uint32_t n) { |
| 227 |
MOZ_ASSERT(stackPosition_ - n >= info_.firstStackSlot()); |
| 228 |
MOZ_ASSERT(stackPosition_ >= stackPosition_ - n); |
| 229 |
stackPosition_ -= n; |
| 230 |
} |
| 231 |
|
| 232 |
// Adds an instruction to this block's instruction list. |
| 233 |
inline void add(MInstruction* ins); |
| 234 |
|
| 235 |
// Marks the last instruction of the block; no further instructions |
| 236 |
// can be added. |
| 237 |
void end(MControlInstruction* ins) { |
| 238 |
MOZ_ASSERT(!hasLastIns()); // Existing control instructions should be |
| 239 |
// removed first. |
| 240 |
MOZ_ASSERT(ins); |
| 241 |
add(ins); |
| 242 |
} |
| 243 |
|
| 244 |
// Adds a phi instruction, but does not set successorWithPhis. |
| 245 |
void addPhi(MPhi* phi); |
| 246 |
|
| 247 |
// Adds a resume point to this block. |
| 248 |
void addResumePoint(MResumePoint* resume) { |
| 249 |
#ifdef DEBUG |
| 250 |
resumePoints_.pushFront(resume); |
| 251 |
#endif |
| 252 |
} |
| 253 |
|
| 254 |
// Discard pre-allocated resume point. |
| 255 |
void discardPreAllocatedResumePoint(MResumePoint* resume) { |
| 256 |
MOZ_ASSERT(!resume->instruction()); |
| 257 |
discardResumePoint(resume); |
| 258 |
} |
| 259 |
|
| 260 |
// Adds a predecessor. Every predecessor must have the same exit stack |
| 261 |
// depth as the entry state to this block. Adding a predecessor |
| 262 |
// automatically creates phi nodes and rewrites uses as needed. |
| 263 |
[[nodiscard]] bool addPredecessor(TempAllocator& alloc, MBasicBlock* pred); |
| 264 |
[[nodiscard]] bool addPredecessorPopN(TempAllocator& alloc, MBasicBlock* pred, |
| 265 |
uint32_t popped); |
| 266 |
|
| 267 |
// Add a predecessor which won't introduce any new phis to this block. |
| 268 |
// This may be called after the contents of this block have been built. |
| 269 |
[[nodiscard]] bool addPredecessorSameInputsAs(MBasicBlock* pred, |
| 270 |
MBasicBlock* existingPred); |
| 271 |
|
| 272 |
// Stranger utilities used for inlining. |
| 273 |
[[nodiscard]] bool addPredecessorWithoutPhis(MBasicBlock* pred); |
| 274 |
void inheritSlots(MBasicBlock* parent); |
| 275 |
[[nodiscard]] bool initEntrySlots(TempAllocator& alloc); |
| 276 |
|
| 277 |
// Replaces an edge for a given block with a new block. This is |
| 278 |
// used for critical edge splitting. |
| 279 |
// |
| 280 |
// Note: If successorWithPhis is set, you must not be replacing it. |
| 281 |
void replacePredecessor(MBasicBlock* old, MBasicBlock* split); |
| 282 |
void replaceSuccessor(size_t pos, MBasicBlock* split); |
| 283 |
|
| 284 |
// Removes `pred` from the predecessor list. If this block defines phis, |
| 285 |
// removes the entry for `pred` and updates the indices of later entries. |
| 286 |
// This may introduce redundant phis if the new block has fewer |
| 287 |
// than two predecessors. |
| 288 |
void removePredecessor(MBasicBlock* pred); |
| 289 |
|
| 290 |
// A version of removePredecessor which expects that phi operands to |
| 291 |
// |pred| have already been removed. |
| 292 |
void removePredecessorWithoutPhiOperands(MBasicBlock* pred, size_t predIndex); |
| 293 |
|
| 294 |
// Resets all the dominator info so that it can be recomputed. |
| 295 |
void clearDominatorInfo(); |
| 296 |
|
| 297 |
// Sets a back edge. This places phi nodes and rewrites instructions within |
| 298 |
// the current loop as necessary. |
| 299 |
[[nodiscard]] bool setBackedge(MBasicBlock* block); |
| 300 |
[[nodiscard]] bool setBackedgeWasm(MBasicBlock* block, size_t paramCount); |
| 301 |
|
| 302 |
// Resets a LOOP_HEADER block to a NORMAL block. This is needed when |
| 303 |
// optimizations remove the backedge. |
| 304 |
void clearLoopHeader(); |
| 305 |
|
| 306 |
// Sets a block to a LOOP_HEADER block, with newBackedge as its backedge. |
| 307 |
// This is needed when optimizations remove the normal entry to a loop |
| 308 |
// with multiple entries. |
| 309 |
void setLoopHeader(MBasicBlock* newBackedge); |
| 310 |
|
| 311 |
// Marks this as a LOOP_HEADER block, but doesn't change anything else. |
| 312 |
void setLoopHeader() { |
| 313 |
MOZ_ASSERT(!isLoopHeader()); |
| 314 |
kind_ = LOOP_HEADER; |
| 315 |
} |
| 316 |
|
| 317 |
// Propagates backedge slots into phis operands of the loop header. |
| 318 |
[[nodiscard]] bool inheritPhisFromBackedge(MBasicBlock* backedge); |
| 319 |
|
| 320 |
void insertBefore(MInstruction* at, MInstruction* ins); |
| 321 |
void insertAfter(MInstruction* at, MInstruction* ins); |
| 322 |
|
| 323 |
void insertAtEnd(MInstruction* ins); |
| 324 |
|
| 325 |
// Move an instruction. Movement may cross block boundaries. |
| 326 |
void moveBefore(MInstruction* at, MInstruction* ins); |
| 327 |
|
| 328 |
enum IgnoreTop { IgnoreNone = 0, IgnoreRecover = 1 << 0 }; |
| 329 |
|
| 330 |
// Locate the top of the |block|, where it is safe to insert a new |
| 331 |
// instruction. |
| 332 |
MInstruction* safeInsertTop(MDefinition* ins = nullptr, |
| 333 |
IgnoreTop ignore = IgnoreNone); |
| 334 |
|
| 335 |
// Removes an instruction with the intention to discard it. |
| 336 |
void discard(MInstruction* ins); |
| 337 |
void discardLastIns(); |
| 338 |
void discardAllInstructions(); |
| 339 |
void discardAllInstructionsStartingAt(MInstructionIterator iter); |
| 340 |
void discardAllPhis(); |
| 341 |
void discardAllResumePoints(bool discardEntry = true); |
| 342 |
void clear(); |
| 343 |
|
| 344 |
// Splits this block in two at a given instruction, inserting a new control |
| 345 |
// flow diamond with |ins| in the slow path, |fastpath| in the other, and |
| 346 |
// |condition| determining which path to take. |
| 347 |
bool wrapInstructionInFastpath(MInstruction* ins, MInstruction* fastpath, |
| 348 |
MInstruction* condition); |
| 349 |
|
| 350 |
void moveOuterResumePointTo(MBasicBlock* dest); |
| 351 |
|
| 352 |
// Move an instruction from this block to a block that has not yet been |
| 353 |
// terminated. |
| 354 |
void moveToNewBlock(MInstruction* ins, MBasicBlock* dst); |
| 355 |
|
| 356 |
// Same as |void discard(MInstruction* ins)| but assuming that |
| 357 |
// all operands are already discarded. |
| 358 |
void discardIgnoreOperands(MInstruction* ins); |
| 359 |
|
| 360 |
// Discards a phi instruction and updates predecessor successorWithPhis. |
| 361 |
void discardPhi(MPhi* phi); |
| 362 |
|
| 363 |
// Some instruction which are guarding against some MIRType value, or |
| 364 |
// against a type expectation should be considered as removing a potenatial |
| 365 |
// branch where the guard does not hold. We need to register such |
| 366 |
// instructions in order to do destructive optimizations correctly, such as |
| 367 |
// Range Analysis. |
| 368 |
void flagOperandsOfPrunedBranches(MInstruction* ins); |
| 369 |
|
| 370 |
// Mark this block as having been removed from the graph. |
| 371 |
void markAsDead() { |
| 372 |
MOZ_ASSERT(kind_ != DEAD); |
| 373 |
kind_ = DEAD; |
| 374 |
} |
| 375 |
|
| 376 |
/////////////////////////////////////////////////////// |
| 377 |
/////////// END GRAPH BUILDING INSTRUCTIONS /////////// |
| 378 |
/////////////////////////////////////////////////////// |
| 379 |
|
| 380 |
MIRGraph& graph() { return graph_; } |
| 381 |
const CompileInfo& info() const { return info_; } |
| 382 |
jsbytecode* pc() const { return trackedSite_->pc(); } |
| 383 |
jsbytecode* entryPC() const { return entryResumePoint()->pc(); } |
| 384 |
uint32_t nslots() const { return slots_.length(); } |
| 385 |
uint32_t id() const { return id_; } |
| 386 |
uint32_t numPredecessors() const { return predecessors_.length(); } |
| 387 |
|
| 388 |
bool branchHintingUnlikely() const { |
| 389 |
return branchHint_ == wasm::BranchHint::Unlikely; |
| 390 |
} |
| 391 |
bool branchHintingLikely() const { |
| 392 |
return branchHint_ == wasm::BranchHint::Likely; |
| 393 |
} |
| 394 |
|
| 395 |
void setBranchHinting(wasm::BranchHint value) { branchHint_ = value; } |
| 396 |
|
| 397 |
uint32_t domIndex() const { |
| 398 |
MOZ_ASSERT(!isDead()); |
| 399 |
return domIndex_; |
| 400 |
} |
| 401 |
void setDomIndex(uint32_t d) { domIndex_ = d; } |
| 402 |
|
| 403 |
MBasicBlock* getPredecessor(uint32_t i) const { return predecessors_[i]; } |
| 404 |
void setPredecessor(uint32_t i, MBasicBlock* p) { predecessors_[i] = p; } |
| 405 |
[[nodiscard]] |
| 406 |
bool appendPredecessor(MBasicBlock* p) { |
| 407 |
return predecessors_.append(p); |
| 408 |
} |
| 409 |
void erasePredecessor(uint32_t i) { predecessors_.erase(&predecessors_[i]); } |
| 410 |
|
| 411 |
size_t indexForPredecessor(MBasicBlock* block) const { |
| 412 |
// This should only be called before critical edge splitting. |
| 413 |
MOZ_ASSERT(!block->successorWithPhis()); |
| 414 |
|
| 415 |
for (size_t i = 0; i < predecessors_.length(); i++) { |
| 416 |
if (predecessors_[i] == block) { |
| 417 |
return i; |
| 418 |
} |
| 419 |
} |
| 420 |
MOZ_CRASH(); |
| 421 |
} |
| 422 |
bool hasAnyIns() const { return !instructions_.empty(); } |
| 423 |
bool hasLastIns() const { |
| 424 |
return hasAnyIns() && instructions_.rbegin()->isControlInstruction(); |
| 425 |
} |
| 426 |
MControlInstruction* lastIns() const { |
| 427 |
MOZ_ASSERT(hasLastIns()); |
| 428 |
return instructions_.rbegin()->toControlInstruction(); |
| 429 |
} |
| 430 |
// Find or allocate an optimized out constant. |
| 431 |
MConstant* optimizedOutConstant(TempAllocator& alloc); |
| 432 |
MPhiIterator phisBegin() const { return phis_.begin(); } |
| 433 |
MPhiIterator phisBegin(MPhi* at) const { return phis_.begin(at); } |
| 434 |
MPhiIterator phisEnd() const { return phis_.end(); } |
| 435 |
bool phisEmpty() const { return phis_.empty(); } |
| 436 |
#ifdef DEBUG |
| 437 |
MResumePointIterator resumePointsBegin() const { |
| 438 |
return resumePoints_.begin(); |
| 439 |
} |
| 440 |
MResumePointIterator resumePointsEnd() const { return resumePoints_.end(); } |
| 441 |
bool resumePointsEmpty() const { return resumePoints_.empty(); } |
| 442 |
#endif |
| 443 |
MInstructionIterator begin() { return instructions_.begin(); } |
| 444 |
MInstructionIterator begin(MInstruction* at) { |
| 445 |
MOZ_ASSERT(at->block() == this); |
| 446 |
return instructions_.begin(at); |
| 447 |
} |
| 448 |
MInstructionIterator end() { return instructions_.end(); } |
| 449 |
MInstructionReverseIterator rbegin() { return instructions_.rbegin(); } |
| 450 |
MInstructionReverseIterator rbegin(MInstruction* at) { |
| 451 |
MOZ_ASSERT(at->block() == this); |
| 452 |
return instructions_.rbegin(at); |
| 453 |
} |
| 454 |
MInstructionReverseIterator rend() { return instructions_.rend(); } |
| 455 |
|
| 456 |
bool isLoopHeader() const { return kind_ == LOOP_HEADER; } |
| 457 |
bool isPendingLoopHeader() const { return kind_ == PENDING_LOOP_HEADER; } |
| 458 |
|
| 459 |
bool hasUniqueBackedge() const { |
| 460 |
MOZ_ASSERT(isLoopHeader()); |
| 461 |
MOZ_ASSERT(numPredecessors() >= 1); |
| 462 |
if (numPredecessors() == 1 || numPredecessors() == 2) { |
| 463 |
return true; |
| 464 |
} |
| 465 |
if (numPredecessors() == 3) { |
| 466 |
// fixup block added by NewFakeLoopPredecessor |
| 467 |
return getPredecessor(1)->numPredecessors() == 0; |
| 468 |
} |
| 469 |
return false; |
| 470 |
} |
| 471 |
MBasicBlock* backedge() const { |
| 472 |
MOZ_ASSERT(hasUniqueBackedge()); |
| 473 |
return getPredecessor(numPredecessors() - 1); |
| 474 |
} |
| 475 |
MBasicBlock* loopHeaderOfBackedge() const { |
| 476 |
MOZ_ASSERT(isLoopBackedge()); |
| 477 |
return getSuccessor(numSuccessors() - 1); |
| 478 |
} |
| 479 |
MBasicBlock* loopPredecessor() const { |
| 480 |
MOZ_ASSERT(isLoopHeader()); |
| 481 |
return getPredecessor(0); |
| 482 |
} |
| 483 |
bool isLoopBackedge() const { |
| 484 |
if (!numSuccessors()) { |
| 485 |
return false; |
| 486 |
} |
| 487 |
MBasicBlock* lastSuccessor = getSuccessor(numSuccessors() - 1); |
| 488 |
return lastSuccessor->isLoopHeader() && |
| 489 |
lastSuccessor->hasUniqueBackedge() && |
| 490 |
lastSuccessor->backedge() == this; |
| 491 |
} |
| 492 |
bool isSplitEdge() const { return kind_ == SPLIT_EDGE; } |
| 493 |
bool isDead() const { return kind_ == DEAD; } |
| 494 |
bool isFakeLoopPred() const { return kind_ == FAKE_LOOP_PRED; } |
| 495 |
|
| 496 |
uint32_t stackDepth() const { return stackPosition_; } |
| 497 |
bool isMarked() const { return mark_; } |
| 498 |
void mark() { |
| 499 |
MOZ_ASSERT(!mark_, "Marking already-marked block"); |
| 500 |
markUnchecked(); |
| 501 |
} |
| 502 |
void markUnchecked() { mark_ = true; } |
| 503 |
void unmark() { |
| 504 |
MOZ_ASSERT(mark_, "Unarking unmarked block"); |
| 505 |
unmarkUnchecked(); |
| 506 |
} |
| 507 |
void unmarkUnchecked() { mark_ = false; } |
| 508 |
|
| 509 |
MBasicBlock* immediateDominator() const { return immediateDominator_; } |
| 510 |
|
| 511 |
void setImmediateDominator(MBasicBlock* dom) { immediateDominator_ = dom; } |
| 512 |
|
| 513 |
MTest* immediateDominatorBranch(BranchDirection* pdirection); |
| 514 |
|
| 515 |
size_t numImmediatelyDominatedBlocks() const { |
| 516 |
return immediatelyDominated_.length(); |
| 517 |
} |
| 518 |
|
| 519 |
MBasicBlock* getImmediatelyDominatedBlock(size_t i) const { |
| 520 |
return immediatelyDominated_[i]; |
| 521 |
} |
| 522 |
|
| 523 |
MBasicBlock** immediatelyDominatedBlocksBegin() { |
| 524 |
return immediatelyDominated_.begin(); |
| 525 |
} |
| 526 |
|
| 527 |
MBasicBlock** immediatelyDominatedBlocksEnd() { |
| 528 |
return immediatelyDominated_.end(); |
| 529 |
} |
| 530 |
|
| 531 |
// Return the number of blocks dominated by this block. All blocks |
| 532 |
// dominate at least themselves, so this will always be non-zero. |
| 533 |
size_t numDominated() const { |
| 534 |
MOZ_ASSERT(numDominated_ != 0); |
| 535 |
return numDominated_; |
| 536 |
} |
| 537 |
|
| 538 |
void addNumDominated(size_t n) { numDominated_ += n; } |
| 539 |
|
| 540 |
// Add |child| to this block's immediately-dominated set. |
| 541 |
bool addImmediatelyDominatedBlock(MBasicBlock* child); |
| 542 |
|
| 543 |
// Remove |child| from this block's immediately-dominated set. |
| 544 |
void removeImmediatelyDominatedBlock(MBasicBlock* child); |
| 545 |
|
| 546 |
// This function retrieves the internal instruction associated with a |
| 547 |
// slot, and should not be used for normal stack operations. It is an |
| 548 |
// internal helper that is also used to enhance spew. |
| 549 |
MDefinition* getSlot(uint32_t index) { |
| 550 |
MOZ_ASSERT(index < stackPosition_); |
| 551 |
return slots_[index]; |
| 552 |
} |
| 553 |
|
| 554 |
MResumePoint* entryResumePoint() const { return entryResumePoint_; } |
| 555 |
void setEntryResumePoint(MResumePoint* rp) { entryResumePoint_ = rp; } |
| 556 |
void clearEntryResumePoint() { |
| 557 |
discardResumePoint(entryResumePoint_); |
| 558 |
entryResumePoint_ = nullptr; |
| 559 |
} |
| 560 |
MResumePoint* outerResumePoint() const { return outerResumePoint_; } |
| 561 |
void setOuterResumePoint(MResumePoint* outer) { |
| 562 |
MOZ_ASSERT(!outerResumePoint_); |
| 563 |
outerResumePoint_ = outer; |
| 564 |
} |
| 565 |
void clearOuterResumePoint() { |
| 566 |
discardResumePoint(outerResumePoint_); |
| 567 |
outerResumePoint_ = nullptr; |
| 568 |
} |
| 569 |
MResumePoint* callerResumePoint() const { return callerResumePoint_; } |
| 570 |
void setCallerResumePoint(MResumePoint* caller) { |
| 571 |
callerResumePoint_ = caller; |
| 572 |
} |
| 573 |
|
| 574 |
LBlock* lir() const { return lir_; } |
| 575 |
void assignLir(LBlock* lir) { |
| 576 |
MOZ_ASSERT(!lir_); |
| 577 |
lir_ = lir; |
| 578 |
} |
| 579 |
|
| 580 |
MBasicBlock* successorWithPhis() const { return successorWithPhis_; } |
| 581 |
uint32_t positionInPhiSuccessor() const { |
| 582 |
MOZ_ASSERT(successorWithPhis()); |
| 583 |
return positionInPhiSuccessor_; |
| 584 |
} |
| 585 |
void setSuccessorWithPhis(MBasicBlock* successor, uint32_t id) { |
| 586 |
successorWithPhis_ = successor; |
| 587 |
positionInPhiSuccessor_ = id; |
| 588 |
} |
| 589 |
void clearSuccessorWithPhis() { successorWithPhis_ = nullptr; } |
| 590 |
size_t numSuccessors() const { |
| 591 |
MOZ_ASSERT(lastIns()); |
| 592 |
return lastIns()->numSuccessors(); |
| 593 |
} |
| 594 |
MBasicBlock* getSuccessor(size_t index) const { |
| 595 |
MOZ_ASSERT(lastIns()); |
| 596 |
return lastIns()->getSuccessor(index); |
| 597 |
} |
| 598 |
MBasicBlock* getSingleSuccessor() const { |
| 599 |
MOZ_ASSERT(numSuccessors() == 1); |
| 600 |
return getSuccessor(0); |
| 601 |
} |
| 602 |
size_t getSuccessorIndex(MBasicBlock*) const; |
| 603 |
size_t getPredecessorIndex(MBasicBlock*) const; |
| 604 |
|
| 605 |
void setLoopDepth(uint32_t loopDepth) { loopDepth_ = loopDepth; } |
| 606 |
uint32_t loopDepth() const { return loopDepth_; } |
| 607 |
|
| 608 |
void dumpStack(GenericPrinter& out); |
| 609 |
void dumpStack(); |
| 610 |
|
| 611 |
void dump(GenericPrinter& out); |
| 612 |
void dump(); |
| 613 |
|
| 614 |
void updateTrackedSite(BytecodeSite* site) { |
| 615 |
MOZ_ASSERT(site->tree() == trackedSite_->tree()); |
| 616 |
trackedSite_ = site; |
| 617 |
} |
| 618 |
BytecodeSite* trackedSite() const { return trackedSite_; } |
| 619 |
InlineScriptTree* trackedTree() const { return trackedSite_->tree(); } |
| 620 |
|
| 621 |
// Find the previous resume point that would be used if this instruction |
| 622 |
// bails out. |
| 623 |
MResumePoint* activeResumePoint(MInstruction* ins); |
| 624 |
|
| 625 |
#ifdef JS_JITSPEW |
| 626 |
const char* nameOfKind() const { |
| 627 |
switch (kind_) { |
| 628 |
case MBasicBlock::Kind::NORMAL: |
| 629 |
return "NORMAL"; |
| 630 |
case MBasicBlock::Kind::PENDING_LOOP_HEADER: |
| 631 |
return "PENDING_LOOP_HEADER"; |
| 632 |
case MBasicBlock::Kind::LOOP_HEADER: |
| 633 |
return "LOOP_HEADER"; |
| 634 |
case MBasicBlock::Kind::SPLIT_EDGE: |
| 635 |
return "SPLIT_EDGE"; |
| 636 |
case MBasicBlock::Kind::FAKE_LOOP_PRED: |
| 637 |
return "FAKE_LOOP_PRED"; |
| 638 |
case MBasicBlock::Kind::INTERNAL: |
| 639 |
return "INTERNAL"; |
| 640 |
case MBasicBlock::Kind::DEAD: |
| 641 |
return "DEAD"; |
| 642 |
default: |
| 643 |
return "MBasicBlock::Kind::???"; |
| 644 |
} |
| 645 |
} |
| 646 |
#endif |
| 647 |
|
| 648 |
private: |
| 649 |
MIRGraph& graph_; |
| 650 |
const CompileInfo& info_; // Each block originates from a particular script. |
| 651 |
InlineList<MInstruction> instructions_; |
| 652 |
Vector<MBasicBlock*, 1, JitAllocPolicy> predecessors_; |
| 653 |
InlineList<MPhi> phis_; |
| 654 |
FixedList<MDefinition*> slots_; |
| 655 |
uint32_t stackPosition_; |
| 656 |
uint32_t id_; |
| 657 |
uint32_t domIndex_; // Index in the dominator tree. |
| 658 |
uint32_t numDominated_; |
| 659 |
LBlock* lir_; |
| 660 |
|
| 661 |
// Copy of a dominator block's outerResumePoint_ which holds the state of |
| 662 |
// caller frame at the time of the call. If not null, this implies that this |
| 663 |
// basic block corresponds to an inlined script. |
| 664 |
MResumePoint* callerResumePoint_; |
| 665 |
|
| 666 |
// Resume point holding baseline-like frame for the PC corresponding to the |
| 667 |
// entry of this basic block. |
| 668 |
MResumePoint* entryResumePoint_; |
| 669 |
|
| 670 |
// Resume point holding baseline-like frame for the PC corresponding to the |
| 671 |
// beginning of the call-site which is being inlined after this block. |
| 672 |
MResumePoint* outerResumePoint_; |
| 673 |
|
| 674 |
#ifdef DEBUG |
| 675 |
// Unordered list used to verify that all the resume points which are |
| 676 |
// registered are correctly removed when a basic block is removed. |
| 677 |
InlineForwardList<MResumePoint> resumePoints_; |
| 678 |
#endif |
| 679 |
|
| 680 |
MBasicBlock* successorWithPhis_; |
| 681 |
uint32_t positionInPhiSuccessor_; |
| 682 |
uint32_t loopDepth_; |
| 683 |
Kind kind_ : 8; |
| 684 |
|
| 685 |
// Utility mark for traversal algorithms. |
| 686 |
bool mark_; |
| 687 |
|
| 688 |
Vector<MBasicBlock*, 1, JitAllocPolicy> immediatelyDominated_; |
| 689 |
MBasicBlock* immediateDominator_; |
| 690 |
|
| 691 |
// Track bailouts by storing the current pc in MIR instruction added at |
| 692 |
// this cycle. This is also used for tracking calls and optimizations when |
| 693 |
// profiling. |
| 694 |
BytecodeSite* trackedSite_; |
| 695 |
}; |
| 696 |
|
| 697 |
using MBasicBlockIterator = InlineListIterator<MBasicBlock>; |
| 698 |
using ReversePostorderIterator = InlineListIterator<MBasicBlock>; |
| 699 |
using PostorderIterator = InlineListReverseIterator<MBasicBlock>; |
| 700 |
|
| 701 |
using MIRGraphReturns = Vector<MBasicBlock*, 1, JitAllocPolicy>; |
| 702 |
|
| 703 |
class MIRGraph { |
| 704 |
InlineList<MBasicBlock> blocks_; |
| 705 |
TempAllocator* alloc_; |
| 706 |
MIRGraphReturns* returnAccumulator_; |
| 707 |
uint32_t blockIdGen_; |
| 708 |
uint32_t idGen_; |
| 709 |
MBasicBlock* osrBlock_; |
| 710 |
|
| 711 |
size_t numBlocks_; |
| 712 |
bool hasTryBlock_; |
| 713 |
|
| 714 |
InlineList<MPhi> phiFreeList_; |
| 715 |
size_t phiFreeListLength_; |
| 716 |
|
| 717 |
public: |
| 718 |
explicit MIRGraph(TempAllocator* alloc) |
| 719 |
: alloc_(alloc), |
| 720 |
returnAccumulator_(nullptr), |
| 721 |
blockIdGen_(0), |
| 722 |
idGen_(0), |
| 723 |
osrBlock_(nullptr), |
| 724 |
numBlocks_(0), |
| 725 |
hasTryBlock_(false), |
| 726 |
phiFreeListLength_(0) {} |
| 727 |
|
| 728 |
TempAllocator& alloc() const { return *alloc_; } |
| 729 |
|
| 730 |
void addBlock(MBasicBlock* block); |
| 731 |
void insertBlockAfter(MBasicBlock* at, MBasicBlock* block); |
| 732 |
void insertBlockBefore(MBasicBlock* at, MBasicBlock* block); |
| 733 |
|
| 734 |
void unmarkBlocks(); |
| 735 |
|
| 736 |
void setReturnAccumulator(MIRGraphReturns* accum) { |
| 737 |
returnAccumulator_ = accum; |
| 738 |
} |
| 739 |
MIRGraphReturns* returnAccumulator() const { return returnAccumulator_; } |
| 740 |
|
| 741 |
[[nodiscard]] bool addReturn(MBasicBlock* returnBlock) { |
| 742 |
if (!returnAccumulator_) { |
| 743 |
return true; |
| 744 |
} |
| 745 |
|
| 746 |
return returnAccumulator_->append(returnBlock); |
| 747 |
} |
| 748 |
|
| 749 |
MBasicBlock* entryBlock() { return *blocks_.begin(); } |
| 750 |
MBasicBlockIterator begin() { return blocks_.begin(); } |
| 751 |
MBasicBlockIterator begin(const MBasicBlock* at) { return blocks_.begin(at); } |
| 752 |
MBasicBlockIterator end() { return blocks_.end(); } |
| 753 |
PostorderIterator poBegin() { return blocks_.rbegin(); } |
| 754 |
PostorderIterator poBegin(const MBasicBlock* at) { |
| 755 |
return blocks_.rbegin(at); |
| 756 |
} |
| 757 |
PostorderIterator poEnd() { return blocks_.rend(); } |
| 758 |
ReversePostorderIterator rpoBegin() { return blocks_.begin(); } |
| 759 |
ReversePostorderIterator rpoBegin(const MBasicBlock* at) { |
| 760 |
return blocks_.begin(at); |
| 761 |
} |
| 762 |
ReversePostorderIterator rpoEnd() { return blocks_.end(); } |
| 763 |
void removeBlock(MBasicBlock* block); |
| 764 |
void moveBlockToEnd(MBasicBlock* block) { |
| 765 |
blocks_.remove(block); |
| 766 |
MOZ_ASSERT_IF(!blocks_.empty(), block->id()); |
| 767 |
blocks_.pushBack(block); |
| 768 |
} |
| 769 |
void moveBlockBefore(MBasicBlock* at, MBasicBlock* block) { |
| 770 |
MOZ_ASSERT(block->id()); |
| 771 |
blocks_.remove(block); |
| 772 |
blocks_.insertBefore(at, block); |
| 773 |
} |
| 774 |
void moveBlockAfter(MBasicBlock* at, MBasicBlock* block) { |
| 775 |
MOZ_ASSERT(block->id()); |
| 776 |
blocks_.remove(block); |
| 777 |
blocks_.insertAfter(at, block); |
| 778 |
} |
| 779 |
size_t numBlocks() const { return numBlocks_; } |
| 780 |
uint32_t numBlockIds() const { return blockIdGen_; } |
| 781 |
void allocDefinitionId(MDefinition* ins) { ins->setId(idGen_++); } |
| 782 |
uint32_t getNumInstructionIds() { return idGen_; } |
| 783 |
MResumePoint* entryResumePoint() { return entryBlock()->entryResumePoint(); } |
| 784 |
|
| 785 |
void setOsrBlock(MBasicBlock* osrBlock) { |
| 786 |
MOZ_ASSERT(!osrBlock_); |
| 787 |
osrBlock_ = osrBlock; |
| 788 |
} |
| 789 |
MBasicBlock* osrBlock() const { return osrBlock_; } |
| 790 |
|
| 791 |
MBasicBlock* osrPreHeaderBlock() const { |
| 792 |
return osrBlock() ? osrBlock()->getSingleSuccessor() : nullptr; |
| 793 |
} |
| 794 |
|
| 795 |
bool hasTryBlock() const { return hasTryBlock_; } |
| 796 |
void setHasTryBlock() { hasTryBlock_ = true; } |
| 797 |
|
| 798 |
void dump(GenericPrinter& out); |
| 799 |
void dump(); |
| 800 |
|
| 801 |
void addPhiToFreeList(MPhi* phi) { |
| 802 |
phiFreeList_.pushBack(phi); |
| 803 |
phiFreeListLength_++; |
| 804 |
} |
| 805 |
size_t phiFreeListLength() const { return phiFreeListLength_; } |
| 806 |
MPhi* takePhiFromFreeList() { |
| 807 |
MOZ_ASSERT(phiFreeListLength_ > 0); |
| 808 |
phiFreeListLength_--; |
| 809 |
return phiFreeList_.popBack(); |
| 810 |
} |
| 811 |
|
| 812 |
void removeFakeLoopPredecessors(); |
| 813 |
|
| 814 |
#ifdef DEBUG |
| 815 |
// Dominators can't be built after we remove fake loop predecessors. |
| 816 |
private: |
| 817 |
bool canBuildDominators_ = true; |
| 818 |
|
| 819 |
public: |
| 820 |
bool canBuildDominators() const { return canBuildDominators_; } |
| 821 |
#endif |
| 822 |
}; |
| 823 |
|
| 824 |
class MDefinitionIterator { |
| 825 |
friend class MBasicBlock; |
| 826 |
friend class MNodeIterator; |
| 827 |
|
| 828 |
private: |
| 829 |
MBasicBlock* block_; |
| 830 |
MPhiIterator phiIter_; |
| 831 |
MInstructionIterator iter_; |
| 832 |
|
| 833 |
bool atPhi() const { return phiIter_ != block_->phisEnd(); } |
| 834 |
|
| 835 |
MDefinition* getIns() { |
| 836 |
if (atPhi()) { |
| 837 |
return *phiIter_; |
| 838 |
} |
| 839 |
return *iter_; |
| 840 |
} |
| 841 |
|
| 842 |
bool more() const { return atPhi() || (*iter_) != block_->lastIns(); } |
| 843 |
|
| 844 |
public: |
| 845 |
explicit MDefinitionIterator(MBasicBlock* block) |
| 846 |
: block_(block), phiIter_(block->phisBegin()), iter_(block->begin()) {} |
| 847 |
|
| 848 |
MDefinitionIterator operator++() { |
| 849 |
MOZ_ASSERT(more()); |
| 850 |
if (atPhi()) { |
| 851 |
++phiIter_; |
| 852 |
} else { |
| 853 |
++iter_; |
| 854 |
} |
| 855 |
return *this; |
| 856 |
} |
| 857 |
|
| 858 |
MDefinitionIterator operator++(int) { |
| 859 |
MDefinitionIterator old(*this); |
| 860 |
operator++(); |
| 861 |
return old; |
| 862 |
} |
| 863 |
|
| 864 |
explicit operator bool() const { return more(); } |
| 865 |
|
| 866 |
MDefinition* operator*() { return getIns(); } |
| 867 |
|
| 868 |
MDefinition* operator->() { return getIns(); } |
| 869 |
}; |
| 870 |
|
| 871 |
// Iterates on all resume points, phis, and instructions of a MBasicBlock. |
| 872 |
// Resume points are visited as long as they have not been discarded. |
| 873 |
class MNodeIterator { |
| 874 |
private: |
| 875 |
// If this is non-null, the resume point that we will visit next (unless |
| 876 |
// it has been discarded). Initialized to the entry resume point. |
| 877 |
// Otherwise, resume point of the most recently visited instruction. |
| 878 |
MResumePoint* resumePoint_; |
| 879 |
|
| 880 |
mozilla::DebugOnly<MInstruction*> lastInstruction_ = nullptr; |
| 881 |
|
| 882 |
// Definition iterator which is one step ahead when visiting resume points. |
| 883 |
// This is in order to avoid incrementing the iterator while it is settled |
| 884 |
// on a discarded instruction. |
| 885 |
MDefinitionIterator defIter_; |
| 886 |
|
| 887 |
MBasicBlock* block() const { return defIter_.block_; } |
| 888 |
|
| 889 |
bool atResumePoint() const { |
| 890 |
MOZ_ASSERT_IF(lastInstruction_ && !lastInstruction_->isDiscarded(), |
| 891 |
lastInstruction_->resumePoint() == resumePoint_); |
| 892 |
return resumePoint_ && !resumePoint_->isDiscarded(); |
| 893 |
} |
| 894 |
|
| 895 |
MNode* getNode() { |
| 896 |
if (atResumePoint()) { |
| 897 |
return resumePoint_; |
| 898 |
} |
| 899 |
return *defIter_; |
| 900 |
} |
| 901 |
|
| 902 |
void next() { |
| 903 |
if (!atResumePoint()) { |
| 904 |
if (defIter_->isInstruction()) { |
| 905 |
resumePoint_ = defIter_->toInstruction()->resumePoint(); |
| 906 |
lastInstruction_ = defIter_->toInstruction(); |
| 907 |
} |
| 908 |
defIter_++; |
| 909 |
} else { |
| 910 |
resumePoint_ = nullptr; |
| 911 |
lastInstruction_ = nullptr; |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
bool more() const { return defIter_ || atResumePoint(); } |
| 916 |
|
| 917 |
public: |
| 918 |
explicit MNodeIterator(MBasicBlock* block) |
| 919 |
: resumePoint_(block->entryResumePoint()), defIter_(block) { |
| 920 |
MOZ_ASSERT(bool(block->entryResumePoint()) == atResumePoint()); |
| 921 |
} |
| 922 |
|
| 923 |
MNodeIterator operator++(int) { |
| 924 |
MNodeIterator old(*this); |
| 925 |
if (more()) { |
| 926 |
next(); |
| 927 |
} |
| 928 |
return old; |
| 929 |
} |
| 930 |
|
| 931 |
explicit operator bool() const { return more(); } |
| 932 |
|
| 933 |
MNode* operator*() { return getNode(); } |
| 934 |
|
| 935 |
MNode* operator->() { return getNode(); } |
| 936 |
}; |
| 937 |
|
| 938 |
void MBasicBlock::add(MInstruction* ins) { |
| 939 |
MOZ_ASSERT(!hasLastIns()); |
| 940 |
ins->setInstructionBlock(this, trackedSite_); |
| 941 |
graph().allocDefinitionId(ins); |
| 942 |
instructions_.pushBack(ins); |
| 943 |
} |
| 944 |
|
| 945 |
} // namespace jit |
| 946 |
} // namespace js |
| 947 |
|
| 948 |
#endif /* jit_MIRGraph_h */ |