LLVM API Documentation

InstrEmitter.h
Go to the documentation of this file.
00001 //===- InstrEmitter.h - Emit MachineInstrs for the SelectionDAG -*- C++ -*--==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This declares the Emit routines for the SelectionDAG class, which creates
00011 // MachineInstrs based on the decisions of the SelectionDAG instruction
00012 // selection.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_INSTREMITTER_H
00017 #define LLVM_LIB_CODEGEN_SELECTIONDAG_INSTREMITTER_H
00018 
00019 #include "llvm/ADT/DenseMap.h"
00020 #include "llvm/CodeGen/MachineBasicBlock.h"
00021 #include "llvm/CodeGen/SelectionDAG.h"
00022 
00023 namespace llvm {
00024 
00025 class MachineInstrBuilder;
00026 class MCInstrDesc;
00027 class SDDbgValue;
00028 
00029 class InstrEmitter {
00030   MachineFunction *MF;
00031   MachineRegisterInfo *MRI;
00032   const TargetMachine *TM;
00033   const TargetInstrInfo *TII;
00034   const TargetRegisterInfo *TRI;
00035   const TargetLowering *TLI;
00036 
00037   MachineBasicBlock *MBB;
00038   MachineBasicBlock::iterator InsertPos;
00039 
00040   /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
00041   /// implicit physical register output.
00042   void EmitCopyFromReg(SDNode *Node, unsigned ResNo,
00043                        bool IsClone, bool IsCloned,
00044                        unsigned SrcReg,
00045                        DenseMap<SDValue, unsigned> &VRBaseMap);
00046 
00047   /// getDstOfCopyToRegUse - If the only use of the specified result number of
00048   /// node is a CopyToReg, return its destination register. Return 0 otherwise.
00049   unsigned getDstOfOnlyCopyToRegUse(SDNode *Node,
00050                                     unsigned ResNo) const;
00051 
00052   void CreateVirtualRegisters(SDNode *Node,
00053                               MachineInstrBuilder &MIB,
00054                               const MCInstrDesc &II,
00055                               bool IsClone, bool IsCloned,
00056                               DenseMap<SDValue, unsigned> &VRBaseMap);
00057 
00058   /// getVR - Return the virtual register corresponding to the specified result
00059   /// of the specified node.
00060   unsigned getVR(SDValue Op,
00061                  DenseMap<SDValue, unsigned> &VRBaseMap);
00062 
00063   /// AddRegisterOperand - Add the specified register as an operand to the
00064   /// specified machine instr. Insert register copies if the register is
00065   /// not in the required register class.
00066   void AddRegisterOperand(MachineInstrBuilder &MIB,
00067                           SDValue Op,
00068                           unsigned IIOpNum,
00069                           const MCInstrDesc *II,
00070                           DenseMap<SDValue, unsigned> &VRBaseMap,
00071                           bool IsDebug, bool IsClone, bool IsCloned);
00072 
00073   /// AddOperand - Add the specified operand to the specified machine instr.  II
00074   /// specifies the instruction information for the node, and IIOpNum is the
00075   /// operand number (in the II) that we are adding. IIOpNum and II are used for
00076   /// assertions only.
00077   void AddOperand(MachineInstrBuilder &MIB,
00078                   SDValue Op,
00079                   unsigned IIOpNum,
00080                   const MCInstrDesc *II,
00081                   DenseMap<SDValue, unsigned> &VRBaseMap,
00082                   bool IsDebug, bool IsClone, bool IsCloned);
00083 
00084   /// ConstrainForSubReg - Try to constrain VReg to a register class that
00085   /// supports SubIdx sub-registers.  Emit a copy if that isn't possible.
00086   /// Return the virtual register to use.
00087   unsigned ConstrainForSubReg(unsigned VReg, unsigned SubIdx,
00088                               MVT VT, DebugLoc DL);
00089 
00090   /// EmitSubregNode - Generate machine code for subreg nodes.
00091   ///
00092   void EmitSubregNode(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap,
00093                       bool IsClone, bool IsCloned);
00094 
00095   /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
00096   /// COPY_TO_REGCLASS is just a normal copy, except that the destination
00097   /// register is constrained to be in a particular register class.
00098   ///
00099   void EmitCopyToRegClassNode(SDNode *Node,
00100                               DenseMap<SDValue, unsigned> &VRBaseMap);
00101 
00102   /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
00103   ///
00104   void EmitRegSequence(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap,
00105                        bool IsClone, bool IsCloned);
00106 public:
00107   /// CountResults - The results of target nodes have register or immediate
00108   /// operands first, then an optional chain, and optional flag operands
00109   /// (which do not go into the machine instrs.)
00110   static unsigned CountResults(SDNode *Node);
00111 
00112   /// EmitDbgValue - Generate machine instruction for a dbg_value node.
00113   ///
00114   MachineInstr *EmitDbgValue(SDDbgValue *SD,
00115                              DenseMap<SDValue, unsigned> &VRBaseMap);
00116 
00117   /// EmitNode - Generate machine code for a node and needed dependencies.
00118   ///
00119   void EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
00120                 DenseMap<SDValue, unsigned> &VRBaseMap) {
00121     if (Node->isMachineOpcode())
00122       EmitMachineNode(Node, IsClone, IsCloned, VRBaseMap);
00123     else
00124       EmitSpecialNode(Node, IsClone, IsCloned, VRBaseMap);
00125   }
00126 
00127   /// getBlock - Return the current basic block.
00128   MachineBasicBlock *getBlock() { return MBB; }
00129 
00130   /// getInsertPos - Return the current insertion position.
00131   MachineBasicBlock::iterator getInsertPos() { return InsertPos; }
00132 
00133   /// InstrEmitter - Construct an InstrEmitter and set it to start inserting
00134   /// at the given position in the given block.
00135   InstrEmitter(MachineBasicBlock *mbb, MachineBasicBlock::iterator insertpos);
00136   
00137 private:
00138   void EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
00139                        DenseMap<SDValue, unsigned> &VRBaseMap);
00140   void EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
00141                        DenseMap<SDValue, unsigned> &VRBaseMap);
00142 };
00143 
00144 }
00145 
00146 #endif