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