Add capability to have machine instruction autoinsert when it is created
[oota-llvm.git] / include / llvm / CodeGen / MachineInstrBuilder.h
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
21 struct 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   MachineInstrBuilder &addReg(int RegNo) {
33     MI->addRegOperand(RegNo);
34     return *this;
35   }
36
37   /// addReg - Add an LLVM value that is to be used as a register...x
38   ///
39   MachineInstrBuilder &addReg(Value *V, bool isDef = false, bool isDNU = false){
40     MI->addRegOperand(V, isDef, isDNU);
41     return *this;
42   }
43
44   /// addPCDisp - Add an LLVM value to be treated as a PC relative
45   /// displacement...
46   ///
47   MachineInstrBuilder &addPCDisp(Value *V) {
48     MI->addPCDispOperand(V);
49     return *this;
50   }
51
52   /// addMReg - Add a machine register operand...
53   ///
54   MachineInstrBuilder &addMReg(int Reg, bool isDef=false) {
55     MI->addMachineRegOperand(Reg, isDef);
56     return *this;
57   }
58
59   /// addSImm - Add a new sign extended immediate operand...
60   ///
61   MachineInstrBuilder &addSImm(int64_t val) {
62     MI->addSignExtImmOperand(val);
63     return *this;
64   }
65
66   /// addZImm - Add a new zero extended immediate operand...
67   ///
68   MachineInstrBuilder &addZImm(int64_t Val) {
69     MI->addZeroExtImmOperand(Val);
70     return *this;
71   }
72 };
73
74 /// BuildMI - Builder interface.  Specify how to create the initial instruction
75 /// itself.
76 ///
77 inline MachineInstrBuilder BuildMI(MachineOpCode Opcode, unsigned NumOperands) {
78   return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
79 }
80
81 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, MachineOpCode Opcode,
82                                    unsigned NumOperands) {
83   return MachineInstrBuilder(new MachineInstr(BB, Opcode, NumOperands));
84 }
85
86 #endif