384cb709679c2ca3c8d0ca066feac70de6e24c00
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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
22 namespace llvm {
23
24 class MachineInstrBuilder {
25   MachineInstr *MI;
26 public:
27   MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
28
29   /// Allow automatic conversion to the machine instruction we are working on.
30   ///
31   operator MachineInstr*() const { return MI; }
32   operator MachineBasicBlock::iterator() const { return MI; }
33
34   /// addReg - Add a new virtual register operand...
35   ///
36   const MachineInstrBuilder &addReg(
37     int RegNo,
38     MachineOperand::UseType Ty = MachineOperand::Use) const {
39     MI->addRegOperand(RegNo, Ty);
40     return *this;
41   }
42
43   /// addImm - Add a new immediate operand.
44   ///
45   const MachineInstrBuilder &addImm(int Val) const {
46     MI->addZeroExtImmOperand(Val);
47     return *this;
48   }
49
50   /// addSImm - Add a new sign extended immediate operand...
51   ///
52   const MachineInstrBuilder &addSImm(int val) const {
53     MI->addSignExtImmOperand(val);
54     return *this;
55   }
56
57   /// addZImm - Add a new zero extended immediate operand...
58   ///
59   const MachineInstrBuilder &addZImm(unsigned Val) const {
60     MI->addZeroExtImmOperand(Val);
61     return *this;
62   }
63
64   /// addImm64 - Add a new 64-bit immediate operand...
65   ///
66   const MachineInstrBuilder &addImm64(uint64_t Val) const {
67     MI->addZeroExtImm64Operand(Val);
68     return *this;
69   }
70
71   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
72     MI->addMachineBasicBlockOperand(MBB);
73     return *this;
74   }
75
76   const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
77     MI->addFrameIndexOperand(Idx);
78     return *this;
79   }
80
81   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
82                                                   int Offset = 0) const {
83     MI->addConstantPoolIndexOperand(Idx, Offset);
84     return *this;
85   }
86
87   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
88     MI->addJumpTableIndexOperand(Idx);
89     return *this;
90   }
91
92   const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
93                                               int Offset = 0) const {
94     MI->addGlobalAddressOperand(GV, Offset);
95     return *this;
96   }
97
98   const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{
99     MI->addExternalSymbolOperand(FnName);
100     return *this;
101   }
102 };
103
104 /// BuildMI - Builder interface.  Specify how to create the initial instruction
105 /// itself.  NumOperands is the number of operands to the machine instruction to
106 /// allow for memory efficient representation of machine instructions.
107 ///
108 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
109   return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
110 }
111
112 /// BuildMI - This version of the builder sets up the first operand as a
113 /// destination virtual register.  NumOperands is the number of additional add*
114 /// calls that are expected, not including the destination register.
115 ///
116 inline MachineInstrBuilder BuildMI(
117   int Opcode, unsigned NumOperands,
118   unsigned DestReg,
119   MachineOperand::UseType useType = MachineOperand::Def) {
120   return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
121                                    true, true)).addReg(DestReg, useType);
122 }
123
124 /// BuildMI - This version of the builder inserts the newly-built
125 /// instruction before the given position in the given MachineBasicBlock, and
126 /// sets up the first operand as a destination virtual register.
127 /// NumOperands is the number of additional add* calls that are expected,
128 /// not including the destination register.
129 ///
130 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
131                                    MachineBasicBlock::iterator I,
132                                    int Opcode, unsigned NumOperands,
133                                    unsigned DestReg) {
134   MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
135   BB.insert(I, MI);
136   return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def);
137 }
138
139 /// BuildMI - This version of the builder inserts the newly-built
140 /// instruction before the given position in the given MachineBasicBlock, and
141 /// does NOT take a destination register.
142 ///
143 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
144                                    MachineBasicBlock::iterator I,
145                                    int Opcode, unsigned NumOperands) {
146   MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
147   BB.insert(I, MI);
148   return MachineInstrBuilder(MI);
149 }
150
151 /// BuildMI - This version of the builder inserts the newly-built
152 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
153 /// destination register.
154 ///
155 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
156                                    unsigned NumOperands) {
157   return BuildMI(*BB, BB->end(), Opcode, NumOperands);
158 }
159
160 /// BuildMI - This version of the builder inserts the newly-built
161 /// instruction at the end of the given MachineBasicBlock, and sets up the first
162 /// operand as a destination virtual register. NumOperands is the number of
163 /// additional add* calls that are expected, not including the destination
164 /// register.
165 ///
166 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
167                                    unsigned NumOperands, unsigned DestReg) {
168   return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg);
169 }
170
171 } // End llvm namespace
172
173 #endif