Book Image

LLVM Cookbook

Book Image

LLVM Cookbook

Overview of this book

Table of Contents (16 chapters)
LLVM Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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...