MachineInstrBuilder.h revision 8cbfc75d17ee1c274116dc1aca3bc8e8ed2326c9
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
21struct MachineInstrBuilder {
22  MachineInstr *MI;
23
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  /// addClobber - Assert that this MI is going to clobber a specific
47  /// register. Useful for instructions that always clobber certain hard regs.
48  /// (Same as addReg(RegNo, true) but shorter and more obvious).
49  ///
50  const MachineInstrBuilder &addClobber(int RegNo) const {
51    MI->addRegOperand(RegNo, true);
52    return *this;
53  }
54
55  /// addPCDisp - Add an LLVM value to be treated as a PC relative
56  /// displacement...
57  ///
58  const MachineInstrBuilder &addPCDisp(Value *V) const {
59    MI->addPCDispOperand(V);
60    return *this;
61  }
62
63  /// addMReg - Add a machine register operand...
64  ///
65  const MachineInstrBuilder &addMReg(int Reg,
66                                     MOTy::UseType Ty = MOTy::Use) const {
67    MI->addMachineRegOperand(Reg, Ty);
68    return *this;
69  }
70
71  /// addSImm - Add a new sign extended immediate operand...
72  ///
73  const MachineInstrBuilder &addSImm(int64_t val) const {
74    MI->addSignExtImmOperand(val);
75    return *this;
76  }
77
78  /// addZImm - Add a new zero extended immediate operand...
79  ///
80  const MachineInstrBuilder &addZImm(int64_t Val) const {
81    MI->addZeroExtImmOperand(Val);
82    return *this;
83  }
84
85  const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
86    MI->addMachineBasicBlockOperand(MBB);
87    return *this;
88  }
89};
90
91/// BuildMI - Builder interface.  Specify how to create the initial instruction
92/// itself.  NumOperands is the number of operands to the machine instruction to
93/// allow for memory efficient representation of machine instructions.
94///
95inline MachineInstrBuilder BuildMI(MachineOpCode Opcode, unsigned NumOperands) {
96  return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
97}
98
99/// BuildMI - This version of the builder also sets up the first "operand" as a
100/// destination virtual register.  NumOperands is the number of additional add*
101/// calls that are expected, it does not include the destination register.
102///
103inline MachineInstrBuilder BuildMI(MachineOpCode Opcode, unsigned NumOperands,
104                                   unsigned DestReg) {
105  return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
106                                   true, true)).addReg(DestReg, MOTy::Def);
107}
108
109
110/// BuildMI - This version of the builder inserts the built MachineInstr into
111/// the specified MachineBasicBlock.
112///
113inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, MachineOpCode Opcode,
114                                   unsigned NumOperands) {
115  return MachineInstrBuilder(new MachineInstr(BB, Opcode, NumOperands));
116}
117
118/// BuildMI - This version of the builder inserts the built MachineInstr into
119/// the specified MachineBasicBlock, and also sets up the first "operand" as a
120/// destination virtual register.  NumOperands is the number of additional add*
121/// calls that are expected, it does not include the destination register.
122///
123inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, MachineOpCode Opcode,
124                                   unsigned NumOperands, unsigned DestReg) {
125  return MachineInstrBuilder(new MachineInstr(BB, Opcode,
126                                              NumOperands+1)).addReg(DestReg,
127                                                                     MOTy::Def);
128}
129
130#endif
131