Move MOTy::UseType enum into MachineOperand. This eliminates the
[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::ADDrr32);
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/MachineInstr.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
39   /// addReg - Add a new virtual register operand...
40   ///
41   const MachineInstrBuilder &addReg(
42     int RegNo,
43     MachineOperand::UseType Ty = MachineOperand::Use) const {
44     MI->addRegOperand(RegNo, Ty);
45     return *this;
46   }
47
48   /// addReg - Add an LLVM value that is to be used as a register...
49   ///
50   const MachineInstrBuilder &addReg(
51     Value *V,
52     MachineOperand::UseType Ty = MachineOperand::Use) const {
53     MI->addRegOperand(V, Ty);
54     return *this;
55   }
56
57   /// addReg - Add an LLVM value that is to be used as a register...
58   ///
59   const MachineInstrBuilder &addCCReg(
60     Value *V,
61     MachineOperand::UseType Ty = MachineOperand::Use) const {
62     MI->addCCRegOperand(V, Ty);
63     return *this;
64   }
65
66   /// addRegDef - Add an LLVM value that is to be defined as a register... this
67   /// is the same as addReg(V, MachineOperand::Def).
68   ///
69   const MachineInstrBuilder &addRegDef(Value *V) const {
70     return addReg(V, MachineOperand::Def);
71   }
72
73   /// addPCDisp - Add an LLVM value to be treated as a PC relative
74   /// displacement...
75   ///
76   const MachineInstrBuilder &addPCDisp(Value *V) const {
77     MI->addPCDispOperand(V);
78     return *this;
79   }
80
81   /// addMReg - Add a machine register operand...
82   ///
83   const MachineInstrBuilder &addMReg(
84     int Reg,
85     MachineOperand::UseType Ty = MachineOperand::Use) const {
86     MI->addMachineRegOperand(Reg, Ty);
87     return *this;
88   }
89
90   /// addSImm - Add a new sign extended immediate operand...
91   ///
92   const MachineInstrBuilder &addSImm(int64_t val) const {
93     MI->addSignExtImmOperand(val);
94     return *this;
95   }
96
97   /// addZImm - Add a new zero extended immediate operand...
98   ///
99   const MachineInstrBuilder &addZImm(int64_t Val) const {
100     MI->addZeroExtImmOperand(Val);
101     return *this;
102   }
103
104   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
105     MI->addMachineBasicBlockOperand(MBB);
106     return *this;
107   }
108
109   const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
110     MI->addFrameIndexOperand(Idx);
111     return *this;
112   }
113
114   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx) const {
115     MI->addConstantPoolIndexOperand(Idx);
116     return *this;
117   }
118
119   const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
120                                               bool isPCRelative = false) const {
121     MI->addGlobalAddressOperand(GV, isPCRelative);
122     return *this;
123   }
124
125   const MachineInstrBuilder &addExternalSymbol(const std::string &Name,
126                                                bool isPCRelative = false) const{
127     MI->addExternalSymbolOperand(Name, isPCRelative);
128     return *this;
129   }
130 };
131
132 /// BuildMI - Builder interface.  Specify how to create the initial instruction
133 /// itself.  NumOperands is the number of operands to the machine instruction to
134 /// allow for memory efficient representation of machine instructions.
135 ///
136 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
137   return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
138 }
139
140 /// BuildMI - This version of the builder also sets up the first "operand" as a
141 /// destination virtual register.  NumOperands is the number of additional add*
142 /// calls that are expected, it does not include the destination register.
143 ///
144 inline MachineInstrBuilder BuildMI(
145   int Opcode, unsigned NumOperands,
146   unsigned DestReg,
147   MachineOperand::UseType useType = MachineOperand::Def) {
148   return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
149                                    true, true)).addReg(DestReg, useType);
150 }
151
152
153 /// BuildMI - This version of the builder inserts the built MachineInstr into
154 /// the specified MachineBasicBlock.
155 ///
156 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
157                                    unsigned NumOperands) {
158   return MachineInstrBuilder(new MachineInstr(BB, Opcode, NumOperands));
159 }
160
161 /// BuildMI - This version of the builder inserts the built MachineInstr into
162 /// the specified MachineBasicBlock, and also sets up the first "operand" as a
163 /// destination virtual register.  NumOperands is the number of additional add*
164 /// calls that are expected, it does not include the destination register.
165 ///
166 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
167                                    unsigned NumOperands, unsigned DestReg) {
168   return MachineInstrBuilder(
169     new MachineInstr(BB, Opcode, NumOperands+1))
170       .addReg(DestReg, MachineOperand::Def);
171 }
172
173 } // End llvm namespace
174
175 #endif