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