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/MachineFunction.h"
24 class TargetInstrDesc;
26 class MachineInstrBuilder {
29 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
31 /// Allow automatic conversion to the machine instruction we are working on.
33 operator MachineInstr*() const { return MI; }
34 operator MachineBasicBlock::iterator() const { return MI; }
36 /// addReg - Add a new virtual register operand...
39 MachineInstrBuilder &addReg(unsigned RegNo, bool isDef = false,
40 bool isImp = false, bool isKill = false,
41 bool isDead = false, unsigned SubReg = 0) const {
42 MI->addOperand(MachineOperand::CreateReg(RegNo, isDef, isImp, isKill,
47 /// addImm - Add a new immediate operand.
49 const MachineInstrBuilder &addImm(int64_t Val) const {
50 MI->addOperand(MachineOperand::CreateImm(Val));
54 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
55 MI->addOperand(MachineOperand::CreateFPImm(Val));
59 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
60 MI->addOperand(MachineOperand::CreateMBB(MBB));
64 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
65 MI->addOperand(MachineOperand::CreateFI(Idx));
69 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
70 int Offset = 0) const {
71 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
75 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
76 MI->addOperand(MachineOperand::CreateJTI(Idx));
80 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
81 int64_t Offset = 0) const {
82 MI->addOperand(MachineOperand::CreateGA(GV, Offset));
86 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
87 int64_t Offset = 0) const {
88 MI->addOperand(MachineOperand::CreateES(FnName, Offset));
92 const MachineInstrBuilder &addMemOperand(const MachineMemOperand &MMO) const {
93 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
98 /// BuildMI - Builder interface. Specify how to create the initial instruction
101 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
103 const TargetInstrDesc &TID) {
104 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
107 /// BuildMI - This version of the builder sets up the first operand as a
108 /// destination virtual register.
110 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
112 const TargetInstrDesc &TID,
114 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
115 .addReg(DestReg, true);
118 /// BuildMI - This version of the builder inserts the newly-built
119 /// instruction before the given position in the given MachineBasicBlock, and
120 /// sets up the first operand as a destination virtual register.
122 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
123 MachineBasicBlock::iterator I,
125 const TargetInstrDesc &TID,
127 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
129 return MachineInstrBuilder(MI).addReg(DestReg, true);
132 /// BuildMI - This version of the builder inserts the newly-built
133 /// instruction before the given position in the given MachineBasicBlock, and
134 /// does NOT take a destination register.
136 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
137 MachineBasicBlock::iterator I,
139 const TargetInstrDesc &TID) {
140 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
142 return MachineInstrBuilder(MI);
145 /// BuildMI - This version of the builder inserts the newly-built
146 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
147 /// destination register.
149 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
151 const TargetInstrDesc &TID) {
152 return BuildMI(*BB, BB->end(), DL, TID);
155 /// BuildMI - This version of the builder inserts the newly-built
156 /// instruction at the end of the given MachineBasicBlock, and sets up the first
157 /// operand as a destination virtual register.
159 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
161 const TargetInstrDesc &TID,
163 return BuildMI(*BB, BB->end(), DL, TID, DestReg);
166 } // End llvm namespace