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