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