Remove non-DebugLoc versions of BuildMI.
[oota-llvm.git] / include / llvm / CodeGen / MachineInstrBuilder.h
1 //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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:
12 //
13 //   M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
18 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
19
20 #include "llvm/CodeGen/MachineFunction.h"
21
22 namespace llvm {
23
24 class TargetInstrDesc;
25
26 class MachineInstrBuilder {
27   MachineInstr *MI;
28 public:
29   explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
30
31   /// Allow automatic conversion to the machine instruction we are working on.
32   ///
33   operator MachineInstr*() const { return MI; }
34   operator MachineBasicBlock::iterator() const { return MI; }
35
36   /// addReg - Add a new virtual register operand...
37   ///
38   const
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,
43                                              isDead, SubReg));
44     return *this;
45   }
46
47   /// addImm - Add a new immediate operand.
48   ///
49   const MachineInstrBuilder &addImm(int64_t Val) const {
50     MI->addOperand(MachineOperand::CreateImm(Val));
51     return *this;
52   }
53
54   const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
55     MI->addOperand(MachineOperand::CreateFPImm(Val));
56     return *this;
57   }
58
59   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
60     MI->addOperand(MachineOperand::CreateMBB(MBB));
61     return *this;
62   }
63
64   const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
65     MI->addOperand(MachineOperand::CreateFI(Idx));
66     return *this;
67   }
68
69   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
70                                                   int Offset = 0) const {
71     MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
72     return *this;
73   }
74
75   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
76     MI->addOperand(MachineOperand::CreateJTI(Idx));
77     return *this;
78   }
79
80   const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
81                                               int64_t Offset = 0) const {
82     MI->addOperand(MachineOperand::CreateGA(GV, Offset));
83     return *this;
84   }
85
86   const MachineInstrBuilder &addExternalSymbol(const char *FnName,
87                                                int64_t Offset = 0) const {
88     MI->addOperand(MachineOperand::CreateES(FnName, Offset));
89     return *this;
90   }
91
92   const MachineInstrBuilder &addMemOperand(const MachineMemOperand &MMO) const {
93     MI->addMemOperand(*MI->getParent()->getParent(), MMO);
94     return *this;
95   }
96 };
97
98 /// BuildMI - Builder interface.  Specify how to create the initial instruction
99 /// itself.
100 ///
101 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
102                                    DebugLoc DL,
103                                    const TargetInstrDesc &TID) {
104   return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
105 }
106
107 /// BuildMI - This version of the builder sets up the first operand as a
108 /// destination virtual register.
109 ///
110 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
111                                    DebugLoc DL,
112                                    const TargetInstrDesc &TID,
113                                    unsigned DestReg) {
114   return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
115            .addReg(DestReg, true);
116 }
117
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.
121 ///
122 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
123                                    MachineBasicBlock::iterator I,
124                                    DebugLoc DL,
125                                    const TargetInstrDesc &TID,
126                                    unsigned DestReg) {
127   MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
128   BB.insert(I, MI);
129   return MachineInstrBuilder(MI).addReg(DestReg, true);
130 }
131
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.
135 ///
136 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
137                                    MachineBasicBlock::iterator I,
138                                    DebugLoc DL,
139                                    const TargetInstrDesc &TID) {
140   MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
141   BB.insert(I, MI);
142   return MachineInstrBuilder(MI);
143 }
144
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.
148 ///
149 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
150                                    DebugLoc DL,
151                                    const TargetInstrDesc &TID) {
152   return BuildMI(*BB, BB->end(), DL, TID);
153 }
154
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. 
158 ///
159 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
160                                    DebugLoc DL,
161                                    const TargetInstrDesc &TID,
162                                    unsigned DestReg) {
163   return BuildMI(*BB, BB->end(), DL, TID, DestReg);
164 }
165
166 } // End llvm namespace
167
168 #endif