MachineInstrBuilder.h revision 10f3597c4e0c13ecf0272b7ca0be741a91ade48c
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
28namespace llvm {
29
30class MachineInstrBuilder {
31  MachineInstr *MI;
32public:
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  /// addReg - Add an LLVM value that is to be used as a register...
50  ///
51  const MachineInstrBuilder &addReg(
52    Value *V,
53    MachineOperand::UseType Ty = MachineOperand::Use) const {
54    MI->addRegOperand(V, Ty);
55    return *this;
56  }
57
58  /// addRegDef - Add an LLVM value that is to be defined as a register... this
59  /// is the same as addReg(V, MachineOperand::Def).
60  ///
61  const MachineInstrBuilder &addRegDef(Value *V) const {
62    return addReg(V, MachineOperand::Def);
63  }
64
65  /// addImm - Add a new immediate operand.
66  ///
67  const MachineInstrBuilder &addImm(int Val) const {
68    MI->addZeroExtImmOperand(Val);
69    return *this;
70  }
71
72  /// addSImm - Add a new sign extended immediate operand...
73  ///
74  const MachineInstrBuilder &addSImm(int val) const {
75    MI->addSignExtImmOperand(val);
76    return *this;
77  }
78
79  /// addZImm - Add a new zero extended immediate operand...
80  ///
81  const MachineInstrBuilder &addZImm(unsigned Val) const {
82    MI->addZeroExtImmOperand(Val);
83    return *this;
84  }
85
86  /// addImm64 - Add a new 64-bit immediate operand...
87  ///
88  const MachineInstrBuilder &addImm64(uint64_t Val) const {
89    MI->addZeroExtImm64Operand(Val);
90    return *this;
91  }
92
93  const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
94    MI->addMachineBasicBlockOperand(MBB);
95    return *this;
96  }
97
98  const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
99    MI->addFrameIndexOperand(Idx);
100    return *this;
101  }
102
103  const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
104                                                  int Offset = 0) const {
105    MI->addConstantPoolIndexOperand(Idx, Offset);
106    return *this;
107  }
108
109  const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
110    MI->addJumpTableIndexOperand(Idx);
111    return *this;
112  }
113
114  const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
115                                              bool isPCRelative = false,
116                                              int Offset = 0) const {
117    MI->addGlobalAddressOperand(GV, isPCRelative, Offset);
118    return *this;
119  }
120
121  const MachineInstrBuilder &addExternalSymbol(const char *FnName,
122                                               bool isPCRelative = false) const{
123    MI->addExternalSymbolOperand(FnName, isPCRelative);
124    return *this;
125  }
126};
127
128/// BuildMI - Builder interface.  Specify how to create the initial instruction
129/// itself.  NumOperands is the number of operands to the machine instruction to
130/// allow for memory efficient representation of machine instructions.
131///
132inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
133  return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
134}
135
136/// BuildMI - This version of the builder sets up the first operand as a
137/// destination virtual register.  NumOperands is the number of additional add*
138/// calls that are expected, not including the destination register.
139///
140inline MachineInstrBuilder BuildMI(
141  int Opcode, unsigned NumOperands,
142  unsigned DestReg,
143  MachineOperand::UseType useType = MachineOperand::Def) {
144  return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
145                                   true, true)).addReg(DestReg, useType);
146}
147
148/// BuildMI - This version of the builder inserts the newly-built
149/// instruction before the given position in the given MachineBasicBlock, and
150/// sets up the first operand as a destination virtual register.
151/// NumOperands is the number of additional add* calls that are expected,
152/// not including the destination register.
153///
154inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
155                                   MachineBasicBlock::iterator I,
156                                   int Opcode, unsigned NumOperands,
157                                   unsigned DestReg) {
158  MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
159  BB.insert(I, MI);
160  return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def);
161}
162
163/// BuildMI - This version of the builder inserts the newly-built
164/// instruction before the given position in the given MachineBasicBlock, and
165/// does NOT take a destination register.
166///
167inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
168                                   MachineBasicBlock::iterator I,
169                                   int Opcode, unsigned NumOperands) {
170  MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
171  BB.insert(I, MI);
172  return MachineInstrBuilder(MI);
173}
174
175/// BuildMI - This version of the builder inserts the newly-built
176/// instruction at the end of the given MachineBasicBlock, and does NOT take a
177/// destination register.
178///
179inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
180                                   unsigned NumOperands) {
181  return BuildMI(*BB, BB->end(), Opcode, NumOperands);
182}
183
184/// BuildMI - This version of the builder inserts the newly-built
185/// instruction at the end of the given MachineBasicBlock, and sets up the first
186/// operand as a destination virtual register. NumOperands is the number of
187/// additional add* calls that are expected, not including the destination
188/// register.
189///
190inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
191                                   unsigned NumOperands, unsigned DestReg) {
192  return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg);
193}
194
195} // End llvm namespace
196
197#endif
198