1 //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes a function named BuildMI, which is useful for dramatically
11 // simplifying how MachineInstr's are created. It allows use of code like this:
13 // M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
18 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineFunction.h"
25 class TargetInstrDesc;
27 class MachineInstrBuilder {
30 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
32 /// Allow automatic conversion to the machine instruction we are working on.
34 operator MachineInstr*() const { return MI; }
35 operator MachineBasicBlock::iterator() const { return MI; }
37 /// addReg - Add a new virtual register operand...
40 MachineInstrBuilder &addReg(unsigned RegNo, bool isDef = false,
41 bool isImp = false, bool isKill = false,
42 bool isDead = false, unsigned SubReg = 0) const {
43 MI->addOperand(MachineOperand::CreateReg(RegNo, isDef, isImp, isKill,
48 /// addImm - Add a new immediate operand.
50 const MachineInstrBuilder &addImm(int64_t Val) const {
51 MI->addOperand(MachineOperand::CreateImm(Val));
55 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
56 MI->addOperand(MachineOperand::CreateFPImm(Val));
60 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
61 MI->addOperand(MachineOperand::CreateMBB(MBB));
65 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
66 MI->addOperand(MachineOperand::CreateFI(Idx));
70 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
71 int Offset = 0) const {
72 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
76 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
77 MI->addOperand(MachineOperand::CreateJTI(Idx));
81 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
82 int64_t Offset = 0) const {
83 MI->addOperand(MachineOperand::CreateGA(GV, Offset));
87 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
88 int64_t Offset = 0) const {
89 MI->addOperand(MachineOperand::CreateES(FnName, Offset));
94 /// BuildMI - Builder interface. Specify how to create the initial instruction
97 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
98 const TargetInstrDesc &TID) {
99 return MachineInstrBuilder(MF.CreateMachineInstr(TID));
102 /// BuildMI - This version of the builder sets up the first operand as a
103 /// destination virtual register.
105 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
106 const TargetInstrDesc &TID,
108 return MachineInstrBuilder(MF.CreateMachineInstr(TID)).addReg(DestReg, true);
111 /// BuildMI - This version of the builder inserts the newly-built
112 /// instruction before the given position in the given MachineBasicBlock, and
113 /// sets up the first operand as a destination virtual register.
115 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
116 MachineBasicBlock::iterator I,
117 const TargetInstrDesc &TID,
119 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID);
121 return MachineInstrBuilder(MI).addReg(DestReg, true);
124 /// BuildMI - This version of the builder inserts the newly-built
125 /// instruction before the given position in the given MachineBasicBlock, and
126 /// does NOT take a destination register.
128 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
129 MachineBasicBlock::iterator I,
130 const TargetInstrDesc &TID) {
131 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID);
133 return MachineInstrBuilder(MI);
136 /// BuildMI - This version of the builder inserts the newly-built
137 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
138 /// destination register.
140 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
141 const TargetInstrDesc &TID) {
142 return BuildMI(*BB, BB->end(), TID);
145 /// BuildMI - This version of the builder inserts the newly-built
146 /// instruction at the end of the given MachineBasicBlock, and sets up the first
147 /// operand as a destination virtual register.
149 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
150 const TargetInstrDesc &TID,
152 return BuildMI(*BB, BB->end(), TID, DestReg);
155 } // End llvm namespace