Add an MO_Undef MachineOperandType, intended for INSERT_SUBREG. Next up MO_Undead.
[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/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22
23 namespace llvm {
24
25 class TargetInstrDesc;
26
27 class MachineInstrBuilder {
28   MachineInstr *MI;
29 public:
30   explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
31
32   /// Allow automatic conversion to the machine instruction we are working on.
33   ///
34   operator MachineInstr*() const { return MI; }
35   operator MachineBasicBlock::iterator() const { return MI; }
36
37   /// addReg - Add a new virtual register operand...
38   ///
39   const
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,
44                                              isDead, SubReg));
45     return *this;
46   }
47
48   /// addImm - Add a new immediate operand.
49   ///
50   const MachineInstrBuilder &addImm(int64_t Val) const {
51     MI->addOperand(MachineOperand::CreateImm(Val));
52     return *this;
53   }
54
55   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
56     MI->addOperand(MachineOperand::CreateMBB(MBB));
57     return *this;
58   }
59
60   const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
61     MI->addOperand(MachineOperand::CreateFI(Idx));
62     return *this;
63   }
64
65   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
66                                                   int Offset = 0) const {
67     MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
68     return *this;
69   }
70
71   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
72     MI->addOperand(MachineOperand::CreateJTI(Idx));
73     return *this;
74   }
75
76   const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
77                                               int Offset = 0) const {
78     MI->addOperand(MachineOperand::CreateGA(GV, Offset));
79     return *this;
80   }
81
82   const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{
83     MI->addOperand(MachineOperand::CreateES(FnName, 0));
84     return *this;
85   }
86
87   const MachineInstrBuilder &addUndef() const {
88     MI->addOperand(MachineOperand::CreateUndef());
89     return *this;
90   }
91
92   /// addMemOperand - Add a memory operand to the machine instruction.
93   const MachineInstrBuilder &addMemOperand(const MemOperand &MO) const {
94     MI->addMemOperand(MO);
95     return *this;
96   }
97 };
98
99 /// BuildMI - Builder interface.  Specify how to create the initial instruction
100 /// itself.
101 ///
102 inline MachineInstrBuilder BuildMI(const TargetInstrDesc &TID) {
103   return MachineInstrBuilder(new MachineInstr(TID));
104 }
105
106 /// BuildMI - This version of the builder sets up the first operand as a
107 /// destination virtual register.
108 ///
109 inline MachineInstrBuilder  BuildMI(const TargetInstrDesc &TID,
110                                     unsigned DestReg) {
111   return MachineInstrBuilder(new MachineInstr(TID)).addReg(DestReg, true);
112 }
113
114 /// BuildMI - This version of the builder inserts the newly-built
115 /// instruction before the given position in the given MachineBasicBlock, and
116 /// sets up the first operand as a destination virtual register.
117 ///
118 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
119                                    MachineBasicBlock::iterator I,
120                                    const TargetInstrDesc &TID,
121                                    unsigned DestReg) {
122   MachineInstr *MI = new MachineInstr(TID);
123   BB.insert(I, MI);
124   return MachineInstrBuilder(MI).addReg(DestReg, true);
125 }
126
127 /// BuildMI - This version of the builder inserts the newly-built
128 /// instruction before the given position in the given MachineBasicBlock, and
129 /// does NOT take a destination register.
130 ///
131 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
132                                    MachineBasicBlock::iterator I,
133                                    const TargetInstrDesc &TID) {
134   MachineInstr *MI = new MachineInstr(TID);
135   BB.insert(I, MI);
136   return MachineInstrBuilder(MI);
137 }
138
139 /// BuildMI - This version of the builder inserts the newly-built
140 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
141 /// destination register.
142 ///
143 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
144                                    const TargetInstrDesc &TID) {
145   return BuildMI(*BB, BB->end(), TID);
146 }
147
148 /// BuildMI - This version of the builder inserts the newly-built
149 /// instruction at the end of the given MachineBasicBlock, and sets up the first
150 /// operand as a destination virtual register. 
151 ///
152 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
153                                    const TargetInstrDesc &TID,
154                                    unsigned DestReg) {
155   return BuildMI(*BB, BB->end(), TID, DestReg);
156 }
157
158 } // End llvm namespace
159
160 #endif