Use IntEqClasses to compute connected components of live intervals.
[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 #include "llvm/Support/ErrorHandling.h"
22
23 namespace llvm {
24
25 class TargetInstrDesc;
26 class MDNode;
27
28 namespace RegState {
29   enum {
30     Define         = 0x2,
31     Implicit       = 0x4,
32     Kill           = 0x8,
33     Dead           = 0x10,
34     Undef          = 0x20,
35     EarlyClobber   = 0x40,
36     Debug          = 0x80,
37     ImplicitDefine = Implicit | Define,
38     ImplicitKill   = Implicit | Kill
39   };
40 }
41
42 class MachineInstrBuilder {
43   MachineInstr *MI;
44 public:
45   MachineInstrBuilder() : MI(0) {}
46   explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
47
48   /// Allow automatic conversion to the machine instruction we are working on.
49   ///
50   operator MachineInstr*() const { return MI; }
51   operator MachineBasicBlock::iterator() const { return MI; }
52
53   /// addReg - Add a new virtual register operand...
54   ///
55   const
56   MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
57                               unsigned SubReg = 0) const {
58     assert((flags & 0x1) == 0 &&
59            "Passing in 'true' to addReg is forbidden! Use enums instead.");
60     MI->addOperand(MachineOperand::CreateReg(RegNo,
61                                              flags & RegState::Define,
62                                              flags & RegState::Implicit,
63                                              flags & RegState::Kill,
64                                              flags & RegState::Dead,
65                                              flags & RegState::Undef,
66                                              flags & RegState::EarlyClobber,
67                                              SubReg,
68                                              flags & RegState::Debug));
69     return *this;
70   }
71
72   /// addImm - Add a new immediate operand.
73   ///
74   const MachineInstrBuilder &addImm(int64_t Val) const {
75     MI->addOperand(MachineOperand::CreateImm(Val));
76     return *this;
77   }
78
79   const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
80     MI->addOperand(MachineOperand::CreateFPImm(Val));
81     return *this;
82   }
83
84   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
85                                     unsigned char TargetFlags = 0) const {
86     MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
87     return *this;
88   }
89
90   const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
91     MI->addOperand(MachineOperand::CreateFI(Idx));
92     return *this;
93   }
94
95   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
96                                                   int Offset = 0,
97                                           unsigned char TargetFlags = 0) const {
98     MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
99     return *this;
100   }
101
102   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
103                                           unsigned char TargetFlags = 0) const {
104     MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
105     return *this;
106   }
107
108   const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
109                                               int64_t Offset = 0,
110                                           unsigned char TargetFlags = 0) const {
111     MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
112     return *this;
113   }
114
115   const MachineInstrBuilder &addExternalSymbol(const char *FnName,
116                                           unsigned char TargetFlags = 0) const {
117     MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
118     return *this;
119   }
120
121   const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
122     MI->addMemOperand(*MI->getParent()->getParent(), MMO);
123     return *this;
124   }
125
126   const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
127                                         MachineInstr::mmo_iterator e) const {
128     MI->setMemRefs(b, e);
129     return *this;
130   }
131
132
133   const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
134     MI->addOperand(MO);
135     return *this;
136   }
137
138   const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
139     MI->addOperand(MachineOperand::CreateMetadata(MD));
140     return *this;
141   }
142   
143   const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
144     MI->addOperand(MachineOperand::CreateMCSymbol(Sym));
145     return *this;
146   }
147
148   // Add a displacement from an existing MachineOperand with an added offset.
149   const MachineInstrBuilder &addDisp(const MachineOperand &Disp,
150                                      int64_t off) const {
151     switch (Disp.getType()) {
152       default:
153         llvm_unreachable("Unhandled operand type in addDisp()");
154       case MachineOperand::MO_Immediate:
155         return addImm(Disp.getImm() + off);
156       case MachineOperand::MO_GlobalAddress:
157         return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off);
158     }
159   }
160 };
161
162 /// BuildMI - Builder interface.  Specify how to create the initial instruction
163 /// itself.
164 ///
165 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
166                                    DebugLoc DL,
167                                    const TargetInstrDesc &TID) {
168   return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
169 }
170
171 /// BuildMI - This version of the builder sets up the first operand as a
172 /// destination virtual register.
173 ///
174 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
175                                    DebugLoc DL,
176                                    const TargetInstrDesc &TID,
177                                    unsigned DestReg) {
178   return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
179            .addReg(DestReg, RegState::Define);
180 }
181
182 /// BuildMI - This version of the builder inserts the newly-built
183 /// instruction before the given position in the given MachineBasicBlock, and
184 /// sets up the first operand as a destination virtual register.
185 ///
186 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
187                                    MachineBasicBlock::iterator I,
188                                    DebugLoc DL,
189                                    const TargetInstrDesc &TID,
190                                    unsigned DestReg) {
191   MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
192   BB.insert(I, MI);
193   return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
194 }
195
196 /// BuildMI - This version of the builder inserts the newly-built
197 /// instruction before the given position in the given MachineBasicBlock, and
198 /// does NOT take a destination register.
199 ///
200 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
201                                    MachineBasicBlock::iterator I,
202                                    DebugLoc DL,
203                                    const TargetInstrDesc &TID) {
204   MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
205   BB.insert(I, MI);
206   return MachineInstrBuilder(MI);
207 }
208
209 /// BuildMI - This version of the builder inserts the newly-built
210 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
211 /// destination register.
212 ///
213 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
214                                    DebugLoc DL,
215                                    const TargetInstrDesc &TID) {
216   return BuildMI(*BB, BB->end(), DL, TID);
217 }
218
219 /// BuildMI - This version of the builder inserts the newly-built
220 /// instruction at the end of the given MachineBasicBlock, and sets up the first
221 /// operand as a destination virtual register.
222 ///
223 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
224                                    DebugLoc DL,
225                                    const TargetInstrDesc &TID,
226                                    unsigned DestReg) {
227   return BuildMI(*BB, BB->end(), DL, TID, DestReg);
228 }
229
230 inline unsigned getDefRegState(bool B) {
231   return B ? RegState::Define : 0;
232 }
233 inline unsigned getImplRegState(bool B) {
234   return B ? RegState::Implicit : 0;
235 }
236 inline unsigned getKillRegState(bool B) {
237   return B ? RegState::Kill : 0;
238 }
239 inline unsigned getDeadRegState(bool B) {
240   return B ? RegState::Dead : 0;
241 }
242 inline unsigned getUndefRegState(bool B) {
243   return B ? RegState::Undef : 0;
244 }
245
246 } // End llvm namespace
247
248 #endif