MachineInstrBuilder.h revision 024e91f6f6b9c65d767efe507df213c5c53744c7
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/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  /// addReg - Add an LLVM value that is to be used as a register...
59  ///
60  const MachineInstrBuilder &addCCReg(
61    Value *V,
62    MachineOperand::UseType Ty = MachineOperand::Use) const {
63    MI->addCCRegOperand(V, Ty);
64    return *this;
65  }
66
67  /// addRegDef - Add an LLVM value that is to be defined as a register... this
68  /// is the same as addReg(V, MachineOperand::Def).
69  ///
70  const MachineInstrBuilder &addRegDef(Value *V) const {
71    return addReg(V, MachineOperand::Def);
72  }
73
74  /// addPCDisp - Add an LLVM value to be treated as a PC relative
75  /// displacement...
76  ///
77  const MachineInstrBuilder &addPCDisp(Value *V) const {
78    MI->addPCDispOperand(V);
79    return *this;
80  }
81
82  /// addMReg - Add a machine register operand...
83  ///
84  const MachineInstrBuilder &addMReg(int Reg, MachineOperand::UseType Ty
85                                        = MachineOperand::Use) const {
86    MI->addMachineRegOperand(Reg, Ty);
87    return *this;
88  }
89
90  /// addImm - Add a new immediate operand.
91  ///
92  const MachineInstrBuilder &addImm(int Val) const {
93    MI->addZeroExtImmOperand(Val);
94    return *this;
95  }
96
97  /// addSImm - Add a new sign extended immediate operand...
98  ///
99  const MachineInstrBuilder &addSImm(int val) const {
100    MI->addSignExtImmOperand(val);
101    return *this;
102  }
103
104  /// addZImm - Add a new zero extended immediate operand...
105  ///
106  const MachineInstrBuilder &addZImm(unsigned Val) const {
107    MI->addZeroExtImmOperand(Val);
108    return *this;
109  }
110
111  const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
112    MI->addMachineBasicBlockOperand(MBB);
113    return *this;
114  }
115
116  const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
117    MI->addFrameIndexOperand(Idx);
118    return *this;
119  }
120
121  const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx) const {
122    MI->addConstantPoolIndexOperand(Idx);
123    return *this;
124  }
125
126  const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
127					      bool isPCRelative = false) const {
128    MI->addGlobalAddressOperand(GV, isPCRelative);
129    return *this;
130  }
131
132  const MachineInstrBuilder &addExternalSymbol(const std::string &Name,
133					       bool isPCRelative = false) const{
134    MI->addExternalSymbolOperand(Name, isPCRelative);
135    return *this;
136  }
137};
138
139/// BuildMI - Builder interface.  Specify how to create the initial instruction
140/// itself.  NumOperands is the number of operands to the machine instruction to
141/// allow for memory efficient representation of machine instructions.
142///
143inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
144  return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
145}
146
147/// BuildMI - This version of the builder also sets up the first "operand" as a
148/// destination virtual register.  NumOperands is the number of additional add*
149/// calls that are expected, it does not include the destination register.
150///
151inline MachineInstrBuilder BuildMI(
152  int Opcode, unsigned NumOperands,
153  unsigned DestReg,
154  MachineOperand::UseType useType = MachineOperand::Def) {
155  return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
156                                   true, true)).addReg(DestReg, useType);
157}
158
159
160/// BuildMI - Insert the instruction before a specified location in the basic
161/// block.
162inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
163                                   MachineBasicBlock::iterator I,
164                                   int Opcode, unsigned NumOperands,
165                                   unsigned DestReg) {
166  MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
167  BB.insert(I, MI);
168  return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def);
169}
170
171/// BMI - A special BuildMI variant that takes an iterator to insert the
172/// instruction at as well as a basic block.
173inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
174                                   MachineBasicBlock::iterator I,
175                                   int Opcode, unsigned NumOperands) {
176  MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
177  BB.insert(I, MI);
178  return MachineInstrBuilder(MI);
179}
180
181/// BuildMI - This version of the builder inserts the built MachineInstr into
182/// the specified MachineBasicBlock.
183///
184inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
185                                   unsigned NumOperands) {
186  return BuildMI(*BB, BB->end(), Opcode, NumOperands);
187}
188
189/// BuildMI - This version of the builder inserts the built MachineInstr into
190/// the specified MachineBasicBlock, and also sets up the first "operand" as a
191/// destination virtual register.  NumOperands is the number of additional add*
192/// calls that are expected, it does not include the destination register.
193///
194inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
195                                   unsigned NumOperands, unsigned DestReg) {
196  return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg);
197}
198
199} // End llvm namespace
200
201#endif
202