1 //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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:
13 // M = new MachineInstr(X86::ADDrr32);
14 // M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, argVal1);
15 // M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, argVal2);
17 // we can now use code like this:
19 // M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
24 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
26 #include "llvm/CodeGen/MachineInstr.h"
28 class MachineInstrBuilder {
31 MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
33 /// Allow automatic conversion to the machine instruction we are working on.
35 operator MachineInstr*() const { return MI; }
37 /// addReg - Add a new virtual register operand...
39 const MachineInstrBuilder &addReg(int RegNo,
40 MOTy::UseType Ty = MOTy::Use) const {
41 MI->addRegOperand(RegNo, Ty);
45 /// addReg - Add an LLVM value that is to be used as a register...
47 const MachineInstrBuilder &addReg(Value *V,
48 MOTy::UseType Ty = MOTy::Use) const {
49 MI->addRegOperand(V, Ty);
53 /// addReg - Add an LLVM value that is to be used as a register...
55 const MachineInstrBuilder &addCCReg(Value *V,
56 MOTy::UseType Ty = MOTy::Use) const {
57 MI->addCCRegOperand(V, Ty);
61 /// addRegDef - Add an LLVM value that is to be defined as a register... this
62 /// is the same as addReg(V, MOTy::Def).
64 const MachineInstrBuilder &addRegDef(Value *V) const {
65 return addReg(V, MOTy::Def);
68 /// addPCDisp - Add an LLVM value to be treated as a PC relative
71 const MachineInstrBuilder &addPCDisp(Value *V) const {
72 MI->addPCDispOperand(V);
76 /// addMReg - Add a machine register operand...
78 const MachineInstrBuilder &addMReg(int Reg,
79 MOTy::UseType Ty = MOTy::Use) const {
80 MI->addMachineRegOperand(Reg, Ty);
84 /// addSImm - Add a new sign extended immediate operand...
86 const MachineInstrBuilder &addSImm(int64_t val) const {
87 MI->addSignExtImmOperand(val);
91 /// addZImm - Add a new zero extended immediate operand...
93 const MachineInstrBuilder &addZImm(int64_t Val) const {
94 MI->addZeroExtImmOperand(Val);
98 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
99 MI->addMachineBasicBlockOperand(MBB);
103 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
104 MI->addFrameIndexOperand(Idx);
108 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx) const {
109 MI->addConstantPoolIndexOperand(Idx);
113 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
114 bool isPCRelative = false) const {
115 MI->addGlobalAddressOperand(GV, isPCRelative);
119 const MachineInstrBuilder &addExternalSymbol(const std::string &Name,
120 bool isPCRelative = false) const{
121 MI->addExternalSymbolOperand(Name, isPCRelative);
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.
130 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
131 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
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.
138 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands,
140 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
141 true, true)).addReg(DestReg, MOTy::Def);
145 /// BuildMI - This version of the builder inserts the built MachineInstr into
146 /// the specified MachineBasicBlock.
148 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
149 unsigned NumOperands) {
150 return MachineInstrBuilder(new MachineInstr(BB, Opcode, NumOperands));
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.
158 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
159 unsigned NumOperands, unsigned DestReg) {
160 return MachineInstrBuilder(new MachineInstr(BB, Opcode,
161 NumOperands+1)).addReg(DestReg,