MachineInstrBuilder.h revision 83435fbaf98a89cc5d6f13a68bab7e52e8fb6cb3
1//===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
2//
3// This file exposes a function named BuildMI, which is useful for dramatically
4// simplifying how MachineInstr's are created.  Instead of using code like this:
5//
6//   M = new MachineInstr(X86::ADDrr32);
7//   M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, argVal1);
8//   M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, argVal2);
9//
10// we can now use code like this:
11//
12//   M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
17#define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
18
19#include "llvm/CodeGen/MachineInstr.h"
20
21class MachineInstrBuilder {
22  MachineInstr *MI;
23public:
24  MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
25
26  /// Allow automatic conversion to the machine instruction we are working on.
27  ///
28  operator MachineInstr*() const { return MI; }
29
30  /// addReg - Add a new virtual register operand...
31  ///
32  const MachineInstrBuilder &addReg(int RegNo,
33                                    MOTy::UseType Ty = MOTy::Use) const {
34    MI->addRegOperand(RegNo, Ty);
35    return *this;
36  }
37
38  /// addReg - Add an LLVM value that is to be used as a register...
39  ///
40  const MachineInstrBuilder &addReg(Value *V,
41                                    MOTy::UseType Ty = MOTy::Use) const {
42    MI->addRegOperand(V, Ty);
43    return *this;
44  }
45
46  /// addReg - Add an LLVM value that is to be used as a register...
47  ///
48  const MachineInstrBuilder &addCCReg(Value *V,
49                                      MOTy::UseType Ty = MOTy::Use) const {
50    MI->addCCRegOperand(V, Ty);
51    return *this;
52  }
53
54  /// addRegDef - Add an LLVM value that is to be defined as a register... this
55  /// is the same as addReg(V, MOTy::Def).
56  ///
57  const MachineInstrBuilder &addRegDef(Value *V) const {
58    return addReg(V, MOTy::Def);
59  }
60
61  /// addPCDisp - Add an LLVM value to be treated as a PC relative
62  /// displacement...
63  ///
64  const MachineInstrBuilder &addPCDisp(Value *V) const {
65    MI->addPCDispOperand(V);
66    return *this;
67  }
68
69  /// addMReg - Add a machine register operand...
70  ///
71  const MachineInstrBuilder &addMReg(int Reg,
72                                     MOTy::UseType Ty = MOTy::Use) const {
73    MI->addMachineRegOperand(Reg, Ty);
74    return *this;
75  }
76
77  /// addSImm - Add a new sign extended immediate operand...
78  ///
79  const MachineInstrBuilder &addSImm(int64_t val) const {
80    MI->addSignExtImmOperand(val);
81    return *this;
82  }
83
84  /// addZImm - Add a new zero extended immediate operand...
85  ///
86  const MachineInstrBuilder &addZImm(int64_t Val) const {
87    MI->addZeroExtImmOperand(Val);
88    return *this;
89  }
90
91  const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
92    MI->addMachineBasicBlockOperand(MBB);
93    return *this;
94  }
95
96  const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
97    MI->addFrameIndexOperand(Idx);
98    return *this;
99  }
100
101  const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx) const {
102    MI->addConstantPoolIndexOperand(Idx);
103    return *this;
104  }
105
106  const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
107					      bool isPCRelative = false) const {
108    MI->addGlobalAddressOperand(GV, isPCRelative);
109    return *this;
110  }
111
112  const MachineInstrBuilder &addExternalSymbol(const std::string &Name,
113					       bool isPCRelative = false) const{
114    MI->addExternalSymbolOperand(Name, isPCRelative);
115    return *this;
116  }
117};
118
119/// BuildMI - Builder interface.  Specify how to create the initial instruction
120/// itself.  NumOperands is the number of operands to the machine instruction to
121/// allow for memory efficient representation of machine instructions.
122///
123inline MachineInstrBuilder BuildMI(MachineOpCode Opcode, unsigned NumOperands) {
124  return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
125}
126
127/// BuildMI - This version of the builder also sets up the first "operand" as a
128/// destination virtual register.  NumOperands is the number of additional add*
129/// calls that are expected, it does not include the destination register.
130///
131inline MachineInstrBuilder BuildMI(MachineOpCode Opcode, unsigned NumOperands,
132                                   unsigned DestReg) {
133  return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
134                                   true, true)).addReg(DestReg, MOTy::Def);
135}
136
137
138/// BuildMI - This version of the builder inserts the built MachineInstr into
139/// the specified MachineBasicBlock.
140///
141inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, MachineOpCode Opcode,
142                                   unsigned NumOperands) {
143  return MachineInstrBuilder(new MachineInstr(BB, Opcode, NumOperands));
144}
145
146/// BuildMI - This version of the builder inserts the built MachineInstr into
147/// the specified MachineBasicBlock, and also sets up the first "operand" as a
148/// destination virtual register.  NumOperands is the number of additional add*
149/// calls that are expected, it does not include the destination register.
150///
151inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, MachineOpCode Opcode,
152                                   unsigned NumOperands, unsigned DestReg) {
153  return MachineInstrBuilder(new MachineInstr(BB, Opcode,
154                                              NumOperands+1)).addReg(DestReg,
155                                                                     MOTy::Def);
156}
157
158#endif
159