MachineInstrBuilder.h revision 8cf77137533f1d83f9ea0085b5ca6d241f4bab9a
1//===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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
23namespace llvm {
24
25class TargetInstrDesc;
26
27class MachineInstrBuilder {
28  MachineInstr *MI;
29public:
30  explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
31
32  /// Allow automatic conversion to the machine instruction we are working on.
33  ///
34  operator MachineInstr*() const { return MI; }
35  operator MachineBasicBlock::iterator() const { return MI; }
36
37  /// addReg - Add a new virtual register operand...
38  ///
39  const
40  MachineInstrBuilder &addReg(unsigned RegNo, bool isDef = false,
41                              bool isImp = false, bool isKill = false,
42                              bool isDead = false, unsigned SubReg = 0) const {
43    MI->addOperand(MachineOperand::CreateReg(RegNo, isDef, isImp, isKill,
44                                             isDead, SubReg));
45    return *this;
46  }
47
48  /// addImm - Add a new immediate operand.
49  ///
50  const MachineInstrBuilder &addImm(int64_t Val) const {
51    MI->addOperand(MachineOperand::CreateImm(Val));
52    return *this;
53  }
54
55  const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
56    MI->addOperand(MachineOperand::CreateFPImm(Val));
57    return *this;
58  }
59
60  const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
61    MI->addOperand(MachineOperand::CreateMBB(MBB));
62    return *this;
63  }
64
65  const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
66    MI->addOperand(MachineOperand::CreateFI(Idx));
67    return *this;
68  }
69
70  const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
71                                                  int Offset = 0) const {
72    MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
73    return *this;
74  }
75
76  const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
77    MI->addOperand(MachineOperand::CreateJTI(Idx));
78    return *this;
79  }
80
81  const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
82                                              int64_t Offset = 0) const {
83    MI->addOperand(MachineOperand::CreateGA(GV, Offset));
84    return *this;
85  }
86
87  const MachineInstrBuilder &addExternalSymbol(const char *FnName,
88                                               int64_t Offset = 0) const {
89    MI->addOperand(MachineOperand::CreateES(FnName, Offset));
90    return *this;
91  }
92
93  const MachineInstrBuilder &addMemOperand(const MachineMemOperand &MMO) const {
94    MI->addMemOperand(*MI->getParent()->getParent(), MMO);
95    return *this;
96  }
97};
98
99/// BuildMI - Builder interface.  Specify how to create the initial instruction
100/// itself.
101///
102inline MachineInstrBuilder BuildMI(MachineFunction &MF,
103                                   const TargetInstrDesc &TID) {
104  return MachineInstrBuilder(MF.CreateMachineInstr(TID));
105}
106
107/// BuildMI - This version of the builder sets up the first operand as a
108/// destination virtual register.
109///
110inline MachineInstrBuilder  BuildMI(MachineFunction &MF,
111                                    const TargetInstrDesc &TID,
112                                    unsigned DestReg) {
113  return MachineInstrBuilder(MF.CreateMachineInstr(TID)).addReg(DestReg, true);
114}
115
116/// BuildMI - This version of the builder inserts the newly-built
117/// instruction before the given position in the given MachineBasicBlock, and
118/// sets up the first operand as a destination virtual register.
119///
120inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
121                                   MachineBasicBlock::iterator I,
122                                   const TargetInstrDesc &TID,
123                                   unsigned DestReg) {
124  MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID);
125  BB.insert(I, MI);
126  return MachineInstrBuilder(MI).addReg(DestReg, true);
127}
128
129/// BuildMI - This version of the builder inserts the newly-built
130/// instruction before the given position in the given MachineBasicBlock, and
131/// does NOT take a destination register.
132///
133inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
134                                   MachineBasicBlock::iterator I,
135                                   const TargetInstrDesc &TID) {
136  MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID);
137  BB.insert(I, MI);
138  return MachineInstrBuilder(MI);
139}
140
141/// BuildMI - This version of the builder inserts the newly-built
142/// instruction at the end of the given MachineBasicBlock, and does NOT take a
143/// destination register.
144///
145inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
146                                   const TargetInstrDesc &TID) {
147  return BuildMI(*BB, BB->end(), TID);
148}
149
150/// BuildMI - This version of the builder inserts the newly-built
151/// instruction at the end of the given MachineBasicBlock, and sets up the first
152/// operand as a destination virtual register.
153///
154inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
155                                   const TargetInstrDesc &TID,
156                                   unsigned DestReg) {
157  return BuildMI(*BB, BB->end(), TID, DestReg);
158}
159
160} // End llvm namespace
161
162#endif
163