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(int RegNo,
42 MOTy::UseType Ty = MOTy::Use) const {
43 MI->addRegOperand(RegNo, Ty);
47 /// addReg - Add an LLVM value that is to be used as a register...
49 const MachineInstrBuilder &addReg(Value *V,
50 MOTy::UseType Ty = MOTy::Use) const {
51 MI->addRegOperand(V, Ty);
55 /// addReg - Add an LLVM value that is to be used as a register...
57 const MachineInstrBuilder &addCCReg(Value *V,
58 MOTy::UseType Ty = MOTy::Use) const {
59 MI->addCCRegOperand(V, Ty);
63 /// addRegDef - Add an LLVM value that is to be defined as a register... this
64 /// is the same as addReg(V, MOTy::Def).
66 const MachineInstrBuilder &addRegDef(Value *V) const {
67 return addReg(V, MOTy::Def);
70 /// addPCDisp - Add an LLVM value to be treated as a PC relative
73 const MachineInstrBuilder &addPCDisp(Value *V) const {
74 MI->addPCDispOperand(V);
78 /// addMReg - Add a machine register operand...
80 const MachineInstrBuilder &addMReg(int Reg,
81 MOTy::UseType Ty = MOTy::Use) const {
82 MI->addMachineRegOperand(Reg, Ty);
86 /// addSImm - Add a new sign extended immediate operand...
88 const MachineInstrBuilder &addSImm(int64_t val) const {
89 MI->addSignExtImmOperand(val);
93 /// addZImm - Add a new zero extended immediate operand...
95 const MachineInstrBuilder &addZImm(int64_t Val) const {
96 MI->addZeroExtImmOperand(Val);
100 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
101 MI->addMachineBasicBlockOperand(MBB);
105 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
106 MI->addFrameIndexOperand(Idx);
110 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx) const {
111 MI->addConstantPoolIndexOperand(Idx);
115 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
116 bool isPCRelative = false) const {
117 MI->addGlobalAddressOperand(GV, isPCRelative);
121 const MachineInstrBuilder &addExternalSymbol(const std::string &Name,
122 bool isPCRelative = false) const{
123 MI->addExternalSymbolOperand(Name, isPCRelative);
128 /// BuildMI - Builder interface. Specify how to create the initial instruction
129 /// itself. NumOperands is the number of operands to the machine instruction to
130 /// allow for memory efficient representation of machine instructions.
132 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
133 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
136 /// BuildMI - This version of the builder also sets up the first "operand" as a
137 /// destination virtual register. NumOperands is the number of additional add*
138 /// calls that are expected, it does not include the destination register.
140 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands,
142 MOTy::UseType useType = MOTy::Def) {
143 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
144 true, true)).addReg(DestReg, useType);
148 /// BuildMI - This version of the builder inserts the built MachineInstr into
149 /// the specified MachineBasicBlock.
151 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
152 unsigned NumOperands) {
153 return MachineInstrBuilder(new MachineInstr(BB, Opcode, NumOperands));
156 /// BuildMI - This version of the builder inserts the built MachineInstr into
157 /// the specified MachineBasicBlock, and also sets up the first "operand" as a
158 /// destination virtual register. NumOperands is the number of additional add*
159 /// calls that are expected, it does not include the destination register.
161 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
162 unsigned NumOperands, unsigned DestReg) {
163 return MachineInstrBuilder(new MachineInstr(BB, Opcode,
164 NumOperands+1)).addReg(DestReg,
168 } // End llvm namespace