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"
30 class MachineInstrBuilder {
33 MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
35 /// Allow automatic conversion to the machine instruction we are working on.
37 operator MachineInstr*() const { return MI; }
39 /// addReg - Add a new virtual register operand...
41 const MachineInstrBuilder &addReg(
43 MachineOperand::UseType Ty = MachineOperand::Use) const {
44 MI->addRegOperand(RegNo, Ty);
48 /// addReg - Add an LLVM value that is to be used as a register...
50 const MachineInstrBuilder &addReg(
52 MachineOperand::UseType Ty = MachineOperand::Use) const {
53 MI->addRegOperand(V, Ty);
57 /// addReg - Add an LLVM value that is to be used as a register...
59 const MachineInstrBuilder &addCCReg(
61 MachineOperand::UseType Ty = MachineOperand::Use) const {
62 MI->addCCRegOperand(V, Ty);
66 /// addRegDef - Add an LLVM value that is to be defined as a register... this
67 /// is the same as addReg(V, MachineOperand::Def).
69 const MachineInstrBuilder &addRegDef(Value *V) const {
70 return addReg(V, MachineOperand::Def);
73 /// addPCDisp - Add an LLVM value to be treated as a PC relative
76 const MachineInstrBuilder &addPCDisp(Value *V) const {
77 MI->addPCDispOperand(V);
81 /// addMReg - Add a machine register operand...
83 const MachineInstrBuilder &addMReg(
85 MachineOperand::UseType Ty = MachineOperand::Use) const {
86 MI->addMachineRegOperand(Reg, Ty);
90 /// addSImm - Add a new sign extended immediate operand...
92 const MachineInstrBuilder &addSImm(int64_t val) const {
93 MI->addSignExtImmOperand(val);
97 /// addZImm - Add a new zero extended immediate operand...
99 const MachineInstrBuilder &addZImm(int64_t Val) const {
100 MI->addZeroExtImmOperand(Val);
104 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
105 MI->addMachineBasicBlockOperand(MBB);
109 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
110 MI->addFrameIndexOperand(Idx);
114 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx) const {
115 MI->addConstantPoolIndexOperand(Idx);
119 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
120 bool isPCRelative = false) const {
121 MI->addGlobalAddressOperand(GV, isPCRelative);
125 const MachineInstrBuilder &addExternalSymbol(const std::string &Name,
126 bool isPCRelative = false) const{
127 MI->addExternalSymbolOperand(Name, isPCRelative);
132 /// BuildMI - Builder interface. Specify how to create the initial instruction
133 /// itself. NumOperands is the number of operands to the machine instruction to
134 /// allow for memory efficient representation of machine instructions.
136 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
137 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
140 /// BuildMI - This version of the builder also sets up the first "operand" as a
141 /// destination virtual register. NumOperands is the number of additional add*
142 /// calls that are expected, it does not include the destination register.
144 inline MachineInstrBuilder BuildMI(
145 int Opcode, unsigned NumOperands,
147 MachineOperand::UseType useType = MachineOperand::Def) {
148 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
149 true, true)).addReg(DestReg, useType);
153 /// BuildMI - This version of the builder inserts the built MachineInstr into
154 /// the specified MachineBasicBlock.
156 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
157 unsigned NumOperands) {
158 return MachineInstrBuilder(new MachineInstr(BB, Opcode, NumOperands));
161 /// BuildMI - This version of the builder inserts the built MachineInstr into
162 /// the specified MachineBasicBlock, and also sets up the first "operand" as a
163 /// destination virtual register. NumOperands is the number of additional add*
164 /// calls that are expected, it does not include the destination register.
166 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
167 unsigned NumOperands, unsigned DestReg) {
168 return MachineInstrBuilder(
169 new MachineInstr(BB, Opcode, NumOperands+1))
170 .addReg(DestReg, MachineOperand::Def);
173 } // End llvm namespace