MachineInstrBuilder.h revision c3c106ca59e44032041414c72ce584451f1d885f
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
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///
136inline 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///
144inline 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 - Insert the instruction before a specified location in the basic
154/// block.
155inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
156                                   MachineBasicBlock::iterator I,
157                                   int Opcode, unsigned NumOperands,
158                                   unsigned DestReg) {
159  MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
160  BB.insert(I, MI);
161  return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def);
162}
163
164/// BMI - A special BuildMI variant that takes an iterator to insert the
165/// instruction at as well as a basic block.
166inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
167                                   MachineBasicBlock::iterator I,
168                                   int Opcode, unsigned NumOperands) {
169  MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
170  BB.insert(I, MI);
171  return MachineInstrBuilder(MI);
172}
173
174/// BuildMI - This version of the builder inserts the built MachineInstr into
175/// the specified MachineBasicBlock.
176///
177inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
178                                   unsigned NumOperands) {
179  return BuildMI(*BB, BB->end(), Opcode, NumOperands);
180}
181
182/// BuildMI - This version of the builder inserts the built MachineInstr into
183/// the specified MachineBasicBlock, and also sets up the first "operand" as a
184/// destination virtual register.  NumOperands is the number of additional add*
185/// calls that are expected, it does not include the destination register.
186///
187inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
188                                   unsigned NumOperands, unsigned DestReg) {
189  return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg);
190}
191
192} // End llvm namespace
193
194#endif
195