Initial checkin of MachineInstrBuilder class
[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).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 register operand...
31   ///
32   MachineInstrBuilder &addReg(int RegNo) {
33     MI->addRegOperand(RegNo);
34     return *this;
35   }
36
37   MachineInstrBuilder &addReg(Value *V, bool isDef = false, bool isDNU = false){
38     MI->addRegOperand(V, isDef, isDNU);
39     return *this;
40   }
41
42   MachineInstrBuilder &addPCDisp(Value *V) {
43     MI->addPCDispOperand(V);
44     return *this;
45   }
46
47   MachineInstrBuilder &addMReg(int Reg, bool isDef=false) {
48     MI->addMachineRegOperand(Reg, isDef);
49     return *this;
50   }
51
52   /// addSImm - Add a new sign extended immediate operand...
53   ///
54   MachineInstrBuilder &addSImm(int64_t val) {
55     MI->addSignExtImmOperand(val);
56     return *this;
57   }
58
59   /// addZImm - Add a new zero extended immediate operand...
60   ///
61   MachineInstrBuilder &addZImm(int64_t Val) {
62     MI->addZeroExtImmOperand(Val);
63     return *this;
64   }
65 };
66
67 /// BuildMI - Builder interface.  Specify how to create the initial instruction
68 /// itself.
69 ///
70 inline MachineInstrBuilder BuildMI(MachineOpCode Opcode, unsigned NumOperands) {
71   return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
72 }
73
74 #if 0
75 inline MachineInstrBuilder BuildMI(MBasicBlock *BB, MachineOpCode Opcode,
76                                    unsigned DestReg = 0) {
77   return MachineInstrBuilder(new MachineInstr(BB, Opcode, DestReg));
78 }
79 #endif
80                                 
81 #endif