3a1335a15931106077f66e33d95175bce82db8eb
[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(int RegNo, bool isDef = false) const {
37     MI->addRegOperand(RegNo, isDef);
38     return *this;
39   }
40
41   /// addImm - Add a new immediate operand.
42   ///
43   const MachineInstrBuilder &addImm(int64_t Val) const {
44     MI->addImmOperand(Val);
45     return *this;
46   }
47
48   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
49     MI->addMachineBasicBlockOperand(MBB);
50     return *this;
51   }
52
53   const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
54     MI->addFrameIndexOperand(Idx);
55     return *this;
56   }
57
58   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
59                                                   int Offset = 0) const {
60     MI->addConstantPoolIndexOperand(Idx, Offset);
61     return *this;
62   }
63
64   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
65     MI->addJumpTableIndexOperand(Idx);
66     return *this;
67   }
68
69   const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
70                                               int Offset = 0) const {
71     MI->addGlobalAddressOperand(GV, Offset);
72     return *this;
73   }
74
75   const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{
76     MI->addExternalSymbolOperand(FnName);
77     return *this;
78   }
79 };
80
81 /// BuildMI - Builder interface.  Specify how to create the initial instruction
82 /// itself.  NumOperands is the number of operands to the machine instruction to
83 /// allow for memory efficient representation of machine instructions.
84 ///
85 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
86   return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands));
87 }
88
89 /// BuildMI - This version of the builder sets up the first operand as a
90 /// destination virtual register.  NumOperands is the number of additional add*
91 /// calls that are expected, not including the destination register.
92 ///
93 inline MachineInstrBuilder 
94 BuildMI(int Opcode, unsigned NumOperands, unsigned DestReg) {
95   return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1))
96                .addReg(DestReg, true);
97 }
98
99 /// BuildMI - This version of the builder inserts the newly-built
100 /// instruction before the given position in the given MachineBasicBlock, and
101 /// sets up the first operand as a destination virtual register.
102 /// NumOperands is the number of additional add* calls that are expected,
103 /// not including the destination register.
104 ///
105 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
106                                    MachineBasicBlock::iterator I,
107                                    int Opcode, unsigned NumOperands,
108                                    unsigned DestReg) {
109   MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1);
110   BB.insert(I, MI);
111   return MachineInstrBuilder(MI).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 /// does NOT take a destination register.
117 ///
118 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
119                                    MachineBasicBlock::iterator I,
120                                    int Opcode, unsigned NumOperands) {
121   MachineInstr *MI = new MachineInstr(Opcode, NumOperands);
122   BB.insert(I, MI);
123   return MachineInstrBuilder(MI);
124 }
125
126 /// BuildMI - This version of the builder inserts the newly-built
127 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
128 /// destination register.
129 ///
130 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
131                                    unsigned NumOperands) {
132   return BuildMI(*BB, BB->end(), Opcode, NumOperands);
133 }
134
135 /// BuildMI - This version of the builder inserts the newly-built
136 /// instruction at the end of the given MachineBasicBlock, and sets up the first
137 /// operand as a destination virtual register. NumOperands is the number of
138 /// additional add* calls that are expected, not including the destination
139 /// register.
140 ///
141 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
142                                    unsigned NumOperands, unsigned DestReg) {
143   return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg);
144 }
145
146 } // End llvm namespace
147
148 #endif