Lib/Liveness/CFG.cpp
/**
 * Build a CFG for a given instruction sequence.
 */
Build a CFG for a given instruction sequence.
Build a CFG for a given instruction sequence .
void CFG::build(Instr::List &instrs) {
  assert(empty());
  resize(instrs.size());
   1. Each instruction is a successor of the previous
      instruction, unless the previous instruction
      is an unconditional jump or halt instruction.
   2. Compute a mapping from labels to instruction ids.
   Number of labels in program
  int numLabels = getFreshLabelCount();
   Mapping from labels to instruction ids
  InstrId* labelMap = new InstrId [numLabels];
   Initialise label mapping
  for (int i = 0; i < numLabels; i++)
    labelMap[i] = -1;
  for (int i = 0; i < instrs.size(); i++) {
     Get instruction
    Instr instr = instrs[i];
     Is it an unconditional jump?
    bool uncond = instr.tag == BRL && instr.branch_cond().is_always();
     Is it a final instruction?
    bool end = instr.tag == END || i+1 == instrs.size();
     Add successor
    if (!(uncond || end))
      (*this)[i].insert(i+1);
     Remember location of each label
    if (instr.tag == LAB) {
      assert(instr.label() >= 0 && instr.label() < numLabels);
      labelMap[instr.label()] = i;
    }
  }
  -
   Second pass
  -
   Add a successor for each conditional jump.
  for (int i = 0; i < instrs.size(); i++) {
    Instr instr = instrs[i];
    if (instr.tag == BRL) {
      assert(labelMap[instr.branch_label()] >= 0);
      (*this)[i].insert(labelMap[instr.branch_label()]);
    }
  }
   Free memory
  delete [] labelMap;
  blocks.build(*this);
}
void CFG :: build ( Instr :: List & instrs ) { assert ( empty ( ) ) ; resize ( instrs . size ( ) ) ; int numLabels = getFreshLabelCount ( ) ; InstrId * labelMap = new InstrId [ numLabels ] ; for ( int i = 0 ; i < numLabels ; i ++ ) labelMap [ i ] = -1 ; for ( int i = 0 ; i < instrs . size ( ) ; i ++ ) { Instr instr = instrs [ i ] ; bool uncond = instr . tag == BRL && instr . branch_cond ( ) . is_always ( ) ; bool end = instr . tag == END || i + 1 == instrs . size ( ) ; if ( ! ( uncond || end ) ) ( * this ) [ i ] . insert ( i + 1 ) ; if ( instr . tag == LAB ) { assert ( instr . label ( ) >= 0 && instr . label ( ) < numLabels ) ; labelMap [ instr . label ( ) ] = i ; } } for ( int i = 0 ; i < instrs . size ( ) ; i ++ ) { Instr instr = instrs [ i ] ; if ( instr . tag == BRL ) { assert ( labelMap [ instr . branch_label ( ) ] >= 0 ) ; ( * this ) [ i ] . insert ( labelMap [ instr . branch_label ( ) ] ) ; } } delete [ ] labelMap ; blocks . build ( * this ) ; }
Build a CFG for a given instruction sequence.
Build a CFG for a given instruction sequence .
//set_size(instrs.size()); // ---------- // First pass // ---------- // // 1. Each instruction is a successor of the previous //    instruction, unless the previous instruction //    is an unconditional jump or halt instruction. // // 2. Compute a mapping from labels to instruction ids. // Number of labels in program // Mapping from labels to instruction ids // Initialise label mapping // Get instruction // Is it an unconditional jump? // Is it a final instruction? // Add successor // Remember location of each label // ----------- // Second pass // ----------- // // Add a successor for each conditional jump. // Free memory