schedule.cc 15.6 KB
Newer Older
1 2 3 4
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#include "src/compiler/schedule.h"

7
#include "src/compiler/node-properties.h"
8
#include "src/compiler/node.h"
9 10 11 12 13 14
#include "src/ostreams.h"

namespace v8 {
namespace internal {
namespace compiler {

15
BasicBlock::BasicBlock(Zone* zone, Id id)
16
    : loop_number_(-1),
17 18
      rpo_number_(-1),
      deferred_(false),
19
      dominator_depth_(-1),
20 21 22 23
      dominator_(nullptr),
      rpo_next_(nullptr),
      loop_header_(nullptr),
      loop_end_(nullptr),
24 25
      loop_depth_(0),
      control_(kNone),
26
      control_input_(nullptr),
27 28 29
      nodes_(zone),
      successors_(zone),
      predecessors_(zone),
30 31 32 33 34
#if DEBUG
      debug_info_(AssemblerDebugInfo(nullptr, nullptr, -1)),
#endif
      id_(id) {
}
35 36 37 38 39

bool BasicBlock::LoopContains(BasicBlock* block) const {
  // RPO numbers must be initialized.
  DCHECK(rpo_number_ >= 0);
  DCHECK(block->rpo_number_ >= 0);
40
  if (loop_end_ == nullptr) return false;  // This is not a loop.
41 42
  return block->rpo_number_ >= rpo_number_ &&
         block->rpo_number_ < loop_end_->rpo_number_;
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
}


void BasicBlock::AddSuccessor(BasicBlock* successor) {
  successors_.push_back(successor);
}


void BasicBlock::AddPredecessor(BasicBlock* predecessor) {
  predecessors_.push_back(predecessor);
}


void BasicBlock::AddNode(Node* node) { nodes_.push_back(node); }


void BasicBlock::set_control(Control control) {
  control_ = control;
}


void BasicBlock::set_control_input(Node* control_input) {
  control_input_ = control_input;
}


void BasicBlock::set_loop_depth(int32_t loop_depth) {
  loop_depth_ = loop_depth;
}


void BasicBlock::set_rpo_number(int32_t rpo_number) {
  rpo_number_ = rpo_number;
}


79
void BasicBlock::set_loop_end(BasicBlock* loop_end) { loop_end_ = loop_end; }
80 81 82 83 84 85 86


void BasicBlock::set_loop_header(BasicBlock* loop_header) {
  loop_header_ = loop_header;
}


87 88 89 90 91 92 93 94 95 96 97 98
// static
BasicBlock* BasicBlock::GetCommonDominator(BasicBlock* b1, BasicBlock* b2) {
  while (b1 != b2) {
    if (b1->dominator_depth() < b2->dominator_depth()) {
      b2 = b2->dominator();
    } else {
      b1 = b1->dominator();
    }
  }
  return b1;
}

99 100
void BasicBlock::Print() { OFStream(stdout) << this; }

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
std::ostream& operator<<(std::ostream& os, const BasicBlock& block) {
  os << "B" << block.id();
#if DEBUG
  AssemblerDebugInfo info = block.debug_info();
  if (info.name) os << info;
  // Print predecessor blocks for better debugging.
  const int kMaxDisplayedBlocks = 4;
  int i = 0;
  const BasicBlock* current_block = &block;
  while (current_block->PredecessorCount() > 0 && i++ < kMaxDisplayedBlocks) {
    current_block = current_block->predecessors().front();
    os << " <= B" << current_block->id();
    info = current_block->debug_info();
    if (info.name) os << info;
  }
#endif
  return os;
}
119

120
std::ostream& operator<<(std::ostream& os, const BasicBlock::Control& c) {
121
  switch (c) {
122
    case BasicBlock::kNone:
123
      return os << "none";
124
    case BasicBlock::kGoto:
125
      return os << "goto";
126 127
    case BasicBlock::kCall:
      return os << "call";
128
    case BasicBlock::kBranch:
129
      return os << "branch";
130 131
    case BasicBlock::kSwitch:
      return os << "switch";
132 133
    case BasicBlock::kDeoptimize:
      return os << "deoptimize";
134 135
    case BasicBlock::kTailCall:
      return os << "tailcall";
136
    case BasicBlock::kReturn:
137
      return os << "return";
138
    case BasicBlock::kThrow:
139 140 141 142 143 144
      return os << "throw";
  }
  UNREACHABLE();
}


145
std::ostream& operator<<(std::ostream& os, const BasicBlock::Id& id) {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  return os << id.ToSize();
}


Schedule::Schedule(Zone* zone, size_t node_count_hint)
    : zone_(zone),
      all_blocks_(zone),
      nodeid_to_block_(zone),
      rpo_order_(zone),
      start_(NewBasicBlock()),
      end_(NewBasicBlock()) {
  nodeid_to_block_.reserve(node_count_hint);
}


BasicBlock* Schedule::block(Node* node) const {
  if (node->id() < static_cast<NodeId>(nodeid_to_block_.size())) {
    return nodeid_to_block_[node->id()];
  }
165
  return nullptr;
166 167 168 169
}


bool Schedule::IsScheduled(Node* node) {
170
  if (node->id() >= nodeid_to_block_.size()) return false;
171
  return nodeid_to_block_[node->id()] != nullptr;
172 173 174 175 176 177 178 179 180 181 182
}


BasicBlock* Schedule::GetBlockById(BasicBlock::Id block_id) {
  DCHECK(block_id.ToSize() < all_blocks_.size());
  return all_blocks_[block_id.ToSize()];
}


bool Schedule::SameBasicBlock(Node* a, Node* b) const {
  BasicBlock* block = this->block(a);
183
  return block != nullptr && block == this->block(b);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
}


BasicBlock* Schedule::NewBasicBlock() {
  BasicBlock* block = new (zone_)
      BasicBlock(zone_, BasicBlock::Id::FromSize(all_blocks_.size()));
  all_blocks_.push_back(block);
  return block;
}


void Schedule::PlanNode(BasicBlock* block, Node* node) {
  if (FLAG_trace_turbo_scheduler) {
    OFStream os(stdout);
    os << "Planning #" << node->id() << ":" << node->op()->mnemonic()
       << " for future add to B" << block->id() << "\n";
  }
201
  DCHECK(this->block(node) == nullptr);
202 203 204 205 206 207 208 209 210 211
  SetBlockForNode(block, node);
}


void Schedule::AddNode(BasicBlock* block, Node* node) {
  if (FLAG_trace_turbo_scheduler) {
    OFStream os(stdout);
    os << "Adding #" << node->id() << ":" << node->op()->mnemonic() << " to B"
       << block->id() << "\n";
  }
212
  DCHECK(this->block(node) == nullptr || this->block(node) == block);
213 214 215 216 217 218
  block->AddNode(node);
  SetBlockForNode(block, node);
}


void Schedule::AddGoto(BasicBlock* block, BasicBlock* succ) {
219
  DCHECK_EQ(BasicBlock::kNone, block->control());
220 221 222 223
  block->set_control(BasicBlock::kGoto);
  AddSuccessor(block, succ);
}

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
#if DEBUG
namespace {

bool IsPotentiallyThrowingCall(IrOpcode::Value opcode) {
  switch (opcode) {
#define BUILD_BLOCK_JS_CASE(Name) case IrOpcode::k##Name:
    JS_OP_LIST(BUILD_BLOCK_JS_CASE)
#undef BUILD_BLOCK_JS_CASE
    case IrOpcode::kCall:
      return true;
    default:
      return false;
  }
}

}  // namespace
#endif  // DEBUG
241

242 243 244
void Schedule::AddCall(BasicBlock* block, Node* call, BasicBlock* success_block,
                       BasicBlock* exception_block) {
  DCHECK_EQ(BasicBlock::kNone, block->control());
245
  DCHECK(IsPotentiallyThrowingCall(call->opcode()));
246 247 248 249 250 251 252
  block->set_control(BasicBlock::kCall);
  AddSuccessor(block, success_block);
  AddSuccessor(block, exception_block);
  SetControlInput(block, call);
}


253 254
void Schedule::AddBranch(BasicBlock* block, Node* branch, BasicBlock* tblock,
                         BasicBlock* fblock) {
255 256
  DCHECK_EQ(BasicBlock::kNone, block->control());
  DCHECK_EQ(IrOpcode::kBranch, branch->opcode());
257 258 259 260 261 262 263
  block->set_control(BasicBlock::kBranch);
  AddSuccessor(block, tblock);
  AddSuccessor(block, fblock);
  SetControlInput(block, branch);
}


264 265 266 267 268 269 270 271 272
void Schedule::AddSwitch(BasicBlock* block, Node* sw, BasicBlock** succ_blocks,
                         size_t succ_count) {
  DCHECK_EQ(BasicBlock::kNone, block->control());
  DCHECK_EQ(IrOpcode::kSwitch, sw->opcode());
  block->set_control(BasicBlock::kSwitch);
  for (size_t index = 0; index < succ_count; ++index) {
    AddSuccessor(block, succ_blocks[index]);
  }
  SetControlInput(block, sw);
273 274 275 276 277 278 279 280
}


void Schedule::AddTailCall(BasicBlock* block, Node* input) {
  DCHECK_EQ(BasicBlock::kNone, block->control());
  block->set_control(BasicBlock::kTailCall);
  SetControlInput(block, input);
  if (block != end()) AddSuccessor(block, end());
281 282 283
}


284
void Schedule::AddReturn(BasicBlock* block, Node* input) {
285
  DCHECK_EQ(BasicBlock::kNone, block->control());
286 287
  block->set_control(BasicBlock::kReturn);
  SetControlInput(block, input);
288
  if (block != end()) AddSuccessor(block, end());
289 290 291
}


292 293 294 295 296 297 298 299
void Schedule::AddDeoptimize(BasicBlock* block, Node* input) {
  DCHECK_EQ(BasicBlock::kNone, block->control());
  block->set_control(BasicBlock::kDeoptimize);
  SetControlInput(block, input);
  if (block != end()) AddSuccessor(block, end());
}


300
void Schedule::AddThrow(BasicBlock* block, Node* input) {
301
  DCHECK_EQ(BasicBlock::kNone, block->control());
302 303 304 305 306 307
  block->set_control(BasicBlock::kThrow);
  SetControlInput(block, input);
  if (block != end()) AddSuccessor(block, end());
}


308 309
void Schedule::InsertBranch(BasicBlock* block, BasicBlock* end, Node* branch,
                            BasicBlock* tblock, BasicBlock* fblock) {
310 311
  DCHECK_NE(BasicBlock::kNone, block->control());
  DCHECK_EQ(BasicBlock::kNone, end->control());
312 313 314 315 316
  end->set_control(block->control());
  block->set_control(BasicBlock::kBranch);
  MoveSuccessors(block, end);
  AddSuccessor(block, tblock);
  AddSuccessor(block, fblock);
317
  if (block->control_input() != nullptr) {
318 319 320 321 322 323
    SetControlInput(end, block->control_input());
  }
  SetControlInput(block, branch);
}


324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
void Schedule::InsertSwitch(BasicBlock* block, BasicBlock* end, Node* sw,
                            BasicBlock** succ_blocks, size_t succ_count) {
  DCHECK_NE(BasicBlock::kNone, block->control());
  DCHECK_EQ(BasicBlock::kNone, end->control());
  end->set_control(block->control());
  block->set_control(BasicBlock::kSwitch);
  MoveSuccessors(block, end);
  for (size_t index = 0; index < succ_count; ++index) {
    AddSuccessor(block, succ_blocks[index]);
  }
  if (block->control_input() != nullptr) {
    SetControlInput(end, block->control_input());
  }
  SetControlInput(block, sw);
}

340
void Schedule::EnsureCFGWellFormedness() {
341 342 343 344 345 346
  // Make a copy of all the blocks for the iteration, since adding the split
  // edges will allocate new blocks.
  BasicBlockVector all_blocks_copy(all_blocks_);

  // Insert missing split edge blocks.
  for (auto block : all_blocks_copy) {
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    if (block->PredecessorCount() > 1) {
      if (block != end_) {
        EnsureSplitEdgeForm(block);
      }
      if (block->deferred()) {
        EnsureDeferredCodeSingleEntryPoint(block);
      }
    }
  }
}

void Schedule::EnsureSplitEdgeForm(BasicBlock* block) {
  DCHECK(block->PredecessorCount() > 1 && block != end_);
  for (auto current_pred = block->predecessors().begin();
       current_pred != block->predecessors().end(); ++current_pred) {
    BasicBlock* pred = *current_pred;
    if (pred->SuccessorCount() > 1) {
      // Found a predecessor block with multiple successors.
      BasicBlock* split_edge_block = NewBasicBlock();
      split_edge_block->set_control(BasicBlock::kGoto);
      split_edge_block->successors().push_back(block);
      split_edge_block->predecessors().push_back(pred);
369
      split_edge_block->set_deferred(block->deferred());
370 371 372 373 374 375 376 377 378
      *current_pred = split_edge_block;
      // Find a corresponding successor in the previous block, replace it
      // with the split edge block... but only do it once, since we only
      // replace the previous blocks in the current block one at a time.
      for (auto successor = pred->successors().begin();
           successor != pred->successors().end(); ++successor) {
        if (*successor == block) {
          *successor = split_edge_block;
          break;
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 410 411 412 413 414 415 416 417 418
void Schedule::EnsureDeferredCodeSingleEntryPoint(BasicBlock* block) {
  // If a deferred block has multiple predecessors, they have to
  // all be deferred. Otherwise, we can run into a situation where a range
  // that spills only in deferred blocks inserts its spill in the block, but
  // other ranges need moves inserted by ResolveControlFlow in the predecessors,
  // which may clobber the register of this range.
  // To ensure that, when a deferred block has multiple predecessors, and some
  // are not deferred, we add a non-deferred block to collect all such edges.

  DCHECK(block->deferred() && block->PredecessorCount() > 1);
  bool all_deferred = true;
  for (auto current_pred = block->predecessors().begin();
       current_pred != block->predecessors().end(); ++current_pred) {
    BasicBlock* pred = *current_pred;
    if (!pred->deferred()) {
      all_deferred = false;
      break;
    }
  }

  if (all_deferred) return;
  BasicBlock* merger = NewBasicBlock();
  merger->set_control(BasicBlock::kGoto);
  merger->successors().push_back(block);
  for (auto current_pred = block->predecessors().begin();
       current_pred != block->predecessors().end(); ++current_pred) {
    BasicBlock* pred = *current_pred;
    merger->predecessors().push_back(pred);
    pred->successors().clear();
    pred->successors().push_back(merger);
  }
  merger->set_deferred(false);
  block->predecessors().clear();
  block->predecessors().push_back(merger);
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
  MovePhis(block, merger);
}

void Schedule::MovePhis(BasicBlock* from, BasicBlock* to) {
  for (size_t i = 0; i < from->NodeCount();) {
    Node* node = from->NodeAt(i);
    if (node->opcode() == IrOpcode::kPhi) {
      to->AddNode(node);
      from->RemoveNode(from->begin() + i);
      DCHECK_EQ(nodeid_to_block_[node->id()], from);
      nodeid_to_block_[node->id()] = to;
    } else {
      ++i;
    }
  }
434 435
}

436 437 438 439 440 441 442 443 444 445 446
void Schedule::PropagateDeferredMark() {
  // Push forward the deferred block marks through newly inserted blocks and
  // other improperly marked blocks until a fixed point is reached.
  // TODO(danno): optimize the propagation
  bool done = false;
  while (!done) {
    done = true;
    for (auto block : all_blocks_) {
      if (!block->deferred()) {
        bool deferred = block->PredecessorCount() > 0;
        for (auto pred : block->predecessors()) {
447
          if (!pred->deferred() && (pred->rpo_number() < block->rpo_number())) {
448 449 450 451 452 453 454 455 456 457 458
            deferred = false;
          }
        }
        if (deferred) {
          block->set_deferred(true);
          done = false;
        }
      }
    }
  }
}
459

460 461 462 463 464 465
void Schedule::AddSuccessor(BasicBlock* block, BasicBlock* succ) {
  block->AddSuccessor(succ);
  succ->AddPredecessor(block);
}


466
void Schedule::MoveSuccessors(BasicBlock* from, BasicBlock* to) {
467 468 469 470
  for (BasicBlock* const successor : from->successors()) {
    to->AddSuccessor(successor);
    for (BasicBlock*& predecessor : successor->predecessors()) {
      if (predecessor == from) predecessor = to;
471 472 473 474 475 476
    }
  }
  from->ClearSuccessors();
}


477 478 479 480 481 482 483
void Schedule::SetControlInput(BasicBlock* block, Node* node) {
  block->set_control_input(node);
  SetBlockForNode(block, node);
}


void Schedule::SetBlockForNode(BasicBlock* block, Node* node) {
484
  if (node->id() >= nodeid_to_block_.size()) {
485 486 487 488 489 490
    nodeid_to_block_.resize(node->id() + 1);
  }
  nodeid_to_block_[node->id()] = block;
}


491
std::ostream& operator<<(std::ostream& os, const Schedule& s) {
492 493 494
  for (BasicBlock* block :
       ((s.RpoBlockCount() == 0) ? *s.all_blocks() : *s.rpo_order())) {
    if (block->rpo_number() == -1) {
495
      os << "--- BLOCK id:" << block->id().ToInt();
496 497 498
    } else {
      os << "--- BLOCK B" << block->rpo_number();
    }
499
    if (block->deferred()) os << " (deferred)";
500 501
    if (block->PredecessorCount() != 0) os << " <- ";
    bool comma = false;
502
    for (BasicBlock const* predecessor : block->predecessors()) {
503 504
      if (comma) os << ", ";
      comma = true;
505
      if (predecessor->rpo_number() == -1) {
506
        os << "id:" << predecessor->id().ToInt();
507 508 509
      } else {
        os << "B" << predecessor->rpo_number();
      }
510 511
    }
    os << " ---\n";
512
    for (Node* node : *block) {
513
      os << "  " << *node;
514
      if (NodeProperties::IsTyped(node)) {
515
        Type* type = NodeProperties::GetType(node);
516
        os << " : ";
517
        type->PrintTo(os);
518 519 520
      }
      os << "\n";
    }
521
    BasicBlock::Control control = block->control();
522 523
    if (control != BasicBlock::kNone) {
      os << "  ";
524
      if (block->control_input() != nullptr) {
525
        os << *block->control_input();
526 527 528 529 530
      } else {
        os << "Goto";
      }
      os << " -> ";
      comma = false;
531
      for (BasicBlock const* successor : block->successors()) {
532 533
        if (comma) os << ", ";
        comma = true;
534
        if (successor->rpo_number() == -1) {
535
          os << "id:" << successor->id().ToInt();
536 537 538
        } else {
          os << "B" << successor->rpo_number();
        }
539 540 541 542 543 544
      }
      os << "\n";
    }
  }
  return os;
}
545

546 547 548
}  // namespace compiler
}  // namespace internal
}  // namespace v8