Implementing the MachineBasicBlock class

Similar to basic blocks in the LLVM IR, a MachineBasicBlock class has a set of machine instructions in sequential order. Mostly, a MachineBasicBlock class maps to a single LLVM IR basic block. However, there can be cases where multiple MachineBasicBlocks classes map to a single LLVM IR basic block. The MachineBasicBlock class has a method, called getBasicBlock(), that returns the IR basic block to which it is mapping.

How to do it…

The following steps show how machine basic blocks are added:

  1. The getBasicBlock method will return only the current basic block:
    const BasicBlock *getBasicBlock() const { return BB; }
  2. The basic blocks have successor as well as predecessor basic blocks. To keep track of those, vectors are defined as follows:
    std::vector<MachineBasicBlock *> Predecessors;
    std::vector<MachineBasicBlock *> Successors;
  3. An insert function should be added to insert a machine instruction into the basic block:
      MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) {
    assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc()  && "Cannot insert instruction with bundle flags");
    
    if (I != instr_end() && I->isBundledWithPred()) {        MI->setFlag(MachineInstr::BundledPred);              MI->setFlag(MachineInstr::BundledSucc);
    }
        return Insts.insert(I, MI);
    }
  4. A function called SplitCriticalEdge() splits the critical edges from this block to the given successor block, and returns the newly created block, or null if splitting is not possible. This function updates the LiveVariables, MachineDominatorTree, and MachineLoopInfo classes:
    MachineBasicBlock *
    MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) {
    …
    …
    …
    }

Note

The full implementation of the preceding code is shown in the MachineBasicBlock.cpp file located at lib/CodeGen/.

How it works…

As listed previously, several representative functions of different categories form the interface definition of the MachineBasicBlock class. The MachineBasicBlock class keeps a list of machine instructions such as typedef ilist<MachineInstr> instructions, instructions Insts, and the original LLVM BB (basic block). It also provides methods for purposes such as these:

  • BB information querying (for example, getBasicBlock() and setHasAddressTaken())
  • BB-level manipulation (for example, moveBefore(), moveAfter(), and addSuccessor())
  • Instruction-level manipulation (for example, push_back(), insertAfter(), and so on)

See also

  • To see a detailed implementation of the MachineBasicBlock class, go through the MachineBasicBlock.cpp file located at lib/CodeGen/
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.199.122