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