void LinearScan::removeUnusedSpills() {
  for (SlotInfo& slot : m_slots) {
    IRInstruction* spill = slot.m_spillTmp->getInstruction();
    if (spill->getDst()->getUseCount() == 0) {
      Block* block = spill->getBlock();
      block->erase(block->iteratorTo(spill));
      SSATmp* src = spill->getSrc(0);
      if (src->decUseCount() == 0) {
        Opcode srcOpc = src->getInstruction()->getOpcode();
        // Not all instructions are able to take noreg as its dest
        // reg.  We pick LdLoc and IncRef because they occur often.
        if (srcOpc == IncRef || srcOpc == LdLoc) {
          for (int i = 0, n = src->numNeededRegs(); i < n; ++i) {
            src->setReg(InvalidReg, i);
          }
        }
      }
    }
  }
}
void LinearScan::allocRegsToTrace() {
  // First, visit every instruction, allocating registers as we go,
  // and inserting Reload instructions where necessary.
  for (Block* block : m_blocks) {
    // clear remembered reloads that don't dominate this block
    for (SlotInfo& slot : m_slots) {
      if (SSATmp* reload = slot.m_latestReload) {
        if (!dominates(reload->getInstruction()->getBlock(), block, m_idoms)) {
          slot.m_latestReload = nullptr;
        }
      }
    }
    for (auto it = block->begin(), end = block->end(); it != end; ++it) {
      allocRegToInstruction(it);
      if (RuntimeOption::EvalDumpIR > 3) {
        std::cout << "--- allocated to instruction: ";
        it->print(std::cout);
        std::cout << "\n";
      }
    }
  }

  // Now that we have visited all instructions and inserted Reloads
  // for SSATmps which needed to be spilled, we can go back and insert
  // the spills.  All uses must have been visited before we do this.
  // For each spill slot, insert the spill right after the instruction
  // that generated the value (without traversing everything else).
  for (SlotInfo& slot : m_slots) {
    IRInstruction* spill = slot.m_spillTmp->getInstruction();
    IRInstruction* inst = spill->getSrc(0)->getInstruction();
    Block* block = inst->getBlock();
    if (inst->isBlockEnd()) {
      block->getNext()->prepend(spill);
    } else {
      auto pos = block->iteratorTo(inst);
      block->insert(++pos, spill);
    }
  }
}
Beispiel #3
0
/*
 * Insert a DbgAssertTv instruction for each stack location stored to by
 * a SpillStack instruction
 */
static void insertSpillStackAsserts(IRInstruction& inst, IRFactory* factory) {
  SSATmp* sp = inst.getDst();
  auto const vals = inst.getSrcs().subpiece(2);
  auto* block = inst.getBlock();
  auto pos = block->iteratorTo(&inst); ++pos;
  for (unsigned i = 0, offset = 0, n = vals.size(); i < n; ++i) {
    Type t = vals[i]->getType();
    if (t == Type::ActRec) {
      offset += kNumActRecCells;
      i += kSpillStackActRecExtraArgs;
    } else {
      if (t.subtypeOf(Type::Gen)) {
        IRInstruction* addr = factory->gen(LdStackAddr, sp,
                                           factory->defConst(offset));
        block->insert(pos, addr);
        IRInstruction* check = factory->gen(DbgAssertPtr, addr->getDst());
        block->insert(pos, check);
      }
      ++offset;
    }
  }
}