Implementing the MachineInstrBuilder class

The MachineInstrBuilder class exposes a function called BuildMI(). This function is used to build machine instructions.

How to do it…

Machine instructions are created by using the BuildMI functions, located in the include/llvm/CodeGen/MachineInstrBuilder.h file. The BuildMI functions make it easy to build arbitrary machine instructions.

For example, you can use BuildMI in code snippets for the following purposes:

  1. To create a DestReg = mov 42 (rendered in the x86 assembly as mov DestReg, 42) instruction:
    MachineInstr *MI = BuildMI(X86::MOV32ri, 1, DestReg).addImm(42);
  2. To create the same instruction, but insert it at the end of a basic block:
    MachineBasicBlock &MBB = BuildMI(MBB, X86::MOV32ri, 1, DestReg).addImm(42);
  3. To create the same instruction, but insert it before a specified iterator point:
    MachineBasicBlock::iterator MBBI = 
    BuildMI(MBB, MBBI, X86::MOV32ri, 1, DestReg).addImm(42)
  4. To create a self-looping branch instruction:
    BuildMI(MBB, X86::JNE, 1).addMBB(&MBB);

How it works…

The BuildMI() function is required for specifying the number of operands that the machine instruction will take, which facilitates efficient memory allocation. It is also required to specify whether operands use values or definitions.

..................Content has been hidden....................

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