Home | History | Annotate | Download | only in GlobalISel
      1 //===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.h - MIBuilder --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 /// \file
     10 /// This file declares the MachineIRBuilder class.
     11 /// This is a helper class to build MachineInstr.
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
     15 #define LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
     16 
     17 #include "llvm/CodeGen/GlobalISel/Types.h"
     18 
     19 #include "llvm/CodeGen/MachineBasicBlock.h"
     20 #include "llvm/IR/DebugLoc.h"
     21 
     22 namespace llvm {
     23 
     24 // Forward declarations.
     25 class MachineFunction;
     26 class MachineInstr;
     27 class TargetInstrInfo;
     28 
     29 /// Helper class to build MachineInstr.
     30 /// It keeps internally the insertion point and debug location for all
     31 /// the new instructions we want to create.
     32 /// This information can be modify via the related setters.
     33 class MachineIRBuilder {
     34   /// MachineFunction under construction.
     35   MachineFunction *MF;
     36   /// Information used to access the description of the opcodes.
     37   const TargetInstrInfo *TII;
     38   /// Debug location to be set to any instruction we create.
     39   DebugLoc DL;
     40 
     41   /// Fields describing the insertion point.
     42   /// @{
     43   MachineBasicBlock *MBB;
     44   MachineInstr *MI;
     45   bool Before;
     46   /// @}
     47 
     48   const TargetInstrInfo &getTII() {
     49     assert(TII && "TargetInstrInfo is not set");
     50     return *TII;
     51   }
     52 
     53 public:
     54   /// Getter for the function we currently build.
     55   MachineFunction &getMF() {
     56     assert(MF && "MachineFunction is not set");
     57     return *MF;
     58   }
     59 
     60   /// Getter for the basic block we currently build.
     61   MachineBasicBlock &getMBB() {
     62     assert(MBB && "MachineBasicBlock is not set");
     63     return *MBB;
     64   }
     65 
     66   /// Current insertion point for new instructions.
     67   MachineBasicBlock::iterator getInsertPt();
     68 
     69   /// Setters for the insertion point.
     70   /// @{
     71   /// Set the MachineFunction where to build instructions.
     72   void setMF(MachineFunction &);
     73 
     74   /// Set the insertion point to the beginning (\p Beginning = true) or end
     75   /// (\p Beginning = false) of \p MBB.
     76   /// \pre \p MBB must be contained by getMF().
     77   void setMBB(MachineBasicBlock &MBB, bool Beginning = false);
     78 
     79   /// Set the insertion point to before (\p Before = true) or after
     80   /// (\p Before = false) \p MI.
     81   /// \pre MI must be in getMF().
     82   void setInstr(MachineInstr &MI, bool Before = false);
     83   /// @}
     84 
     85   /// Set the debug location to \p DL for all the next build instructions.
     86   void setDebugLoc(const DebugLoc &DL) { this->DL = DL; }
     87 
     88   /// Build and insert <empty> = \p Opcode [\p Ty] <empty>.
     89   /// \p Ty is the type of the instruction if \p Opcode describes
     90   /// a generic machine instruction. \p Ty must be nullptr if \p Opcode
     91   /// does not describe a generic instruction.
     92   /// The insertion point is the one set by the last call of either
     93   /// setBasicBlock or setMI.
     94   ///
     95   /// \pre setBasicBlock or setMI must have been called.
     96   /// \pre Ty == nullptr or isPreISelGenericOpcode(Opcode)
     97   ///
     98   /// \return The newly created instruction.
     99   MachineInstr *buildInstr(unsigned Opcode, Type *Ty);
    100 
    101   /// Build and insert <empty> = \p Opcode [\p Ty] \p BB.
    102   ///
    103   /// \pre setBasicBlock or setMI must have been called.
    104   /// \pre Ty == nullptr or isPreISelGenericOpcode(Opcode)
    105   ///
    106   /// \return The newly created instruction.
    107   MachineInstr *buildInstr(unsigned Opcode, Type *Ty, MachineBasicBlock &BB);
    108 
    109   /// Build and insert \p Res<def> = \p Opcode [\p Ty] \p Op0, \p Op1.
    110   ///
    111   /// \pre setBasicBlock or setMI must have been called.
    112   /// \pre Ty == nullptr or isPreISelGenericOpcode(Opcode)
    113   ///
    114   /// \return The newly created instruction.
    115   MachineInstr *buildInstr(unsigned Opcode, Type *Ty, unsigned Res,
    116                            unsigned Op0, unsigned Op1);
    117 
    118   /// Build and insert \p Res<def> = \p Opcode \p Op0, \p Op1.
    119   /// I.e., instruction with a non-generic opcode.
    120   ///
    121   /// \pre setBasicBlock or setMI must have been called.
    122   /// \pre not isPreISelGenericOpcode(\p Opcode)
    123   ///
    124   /// \return The newly created instruction.
    125   MachineInstr *buildInstr(unsigned Opcode, unsigned Res, unsigned Op0,
    126                            unsigned Op1);
    127 
    128   /// Build and insert \p Res<def> = \p Opcode \p Op0.
    129   ///
    130   /// \pre setBasicBlock or setMI must have been called.
    131   /// \pre not isPreISelGenericOpcode(\p Opcode)
    132   ///
    133   /// \return The newly created instruction.
    134   MachineInstr *buildInstr(unsigned Opcode, unsigned Res, unsigned Op0);
    135 
    136   /// Build and insert <empty> = \p Opcode <empty>.
    137   ///
    138   /// \pre setBasicBlock or setMI must have been called.
    139   /// \pre not isPreISelGenericOpcode(\p Opcode)
    140   ///
    141   /// \return The newly created instruction.
    142   MachineInstr *buildInstr(unsigned Opcode);
    143 };
    144 
    145 } // End namespace llvm.
    146 #endif // LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
    147