InstrEmitter.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //===- InstrEmitter.h - Emit MachineInstrs for the SelectionDAG -*- C++ -*--==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This declares the Emit routines for the SelectionDAG class, which creates
  10. // MachineInstrs based on the decisions of the SelectionDAG instruction
  11. // selection.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_INSTREMITTER_H
  15. #define LLVM_LIB_CODEGEN_SELECTIONDAG_INSTREMITTER_H
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/CodeGen/MachineBasicBlock.h"
  18. #include "llvm/CodeGen/SelectionDAGNodes.h"
  19. namespace llvm {
  20. class MachineInstrBuilder;
  21. class MCInstrDesc;
  22. class SDDbgLabel;
  23. class SDDbgValue;
  24. class SDDbgOperand;
  25. class TargetLowering;
  26. class TargetMachine;
  27. class LLVM_LIBRARY_VISIBILITY InstrEmitter {
  28. MachineFunction *MF;
  29. MachineRegisterInfo *MRI;
  30. const TargetInstrInfo *TII;
  31. const TargetRegisterInfo *TRI;
  32. const TargetLowering *TLI;
  33. MachineBasicBlock *MBB;
  34. MachineBasicBlock::iterator InsertPos;
  35. /// Should we try to produce DBG_INSTR_REF instructions?
  36. bool EmitDebugInstrRefs;
  37. /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
  38. /// implicit physical register output.
  39. void EmitCopyFromReg(SDNode *Node, unsigned ResNo,
  40. bool IsClone, bool IsCloned,
  41. Register SrcReg,
  42. DenseMap<SDValue, Register> &VRBaseMap);
  43. void CreateVirtualRegisters(SDNode *Node,
  44. MachineInstrBuilder &MIB,
  45. const MCInstrDesc &II,
  46. bool IsClone, bool IsCloned,
  47. DenseMap<SDValue, Register> &VRBaseMap);
  48. /// getVR - Return the virtual register corresponding to the specified result
  49. /// of the specified node.
  50. Register getVR(SDValue Op,
  51. DenseMap<SDValue, Register> &VRBaseMap);
  52. /// AddRegisterOperand - Add the specified register as an operand to the
  53. /// specified machine instr. Insert register copies if the register is
  54. /// not in the required register class.
  55. void AddRegisterOperand(MachineInstrBuilder &MIB,
  56. SDValue Op,
  57. unsigned IIOpNum,
  58. const MCInstrDesc *II,
  59. DenseMap<SDValue, Register> &VRBaseMap,
  60. bool IsDebug, bool IsClone, bool IsCloned);
  61. /// AddOperand - Add the specified operand to the specified machine instr. II
  62. /// specifies the instruction information for the node, and IIOpNum is the
  63. /// operand number (in the II) that we are adding. IIOpNum and II are used for
  64. /// assertions only.
  65. void AddOperand(MachineInstrBuilder &MIB,
  66. SDValue Op,
  67. unsigned IIOpNum,
  68. const MCInstrDesc *II,
  69. DenseMap<SDValue, Register> &VRBaseMap,
  70. bool IsDebug, bool IsClone, bool IsCloned);
  71. /// ConstrainForSubReg - Try to constrain VReg to a register class that
  72. /// supports SubIdx sub-registers. Emit a copy if that isn't possible.
  73. /// Return the virtual register to use.
  74. Register ConstrainForSubReg(Register VReg, unsigned SubIdx, MVT VT,
  75. bool isDivergent, const DebugLoc &DL);
  76. /// EmitSubregNode - Generate machine code for subreg nodes.
  77. ///
  78. void EmitSubregNode(SDNode *Node, DenseMap<SDValue, Register> &VRBaseMap,
  79. bool IsClone, bool IsCloned);
  80. /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
  81. /// COPY_TO_REGCLASS is just a normal copy, except that the destination
  82. /// register is constrained to be in a particular register class.
  83. ///
  84. void EmitCopyToRegClassNode(SDNode *Node,
  85. DenseMap<SDValue, Register> &VRBaseMap);
  86. /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
  87. ///
  88. void EmitRegSequence(SDNode *Node, DenseMap<SDValue, Register> &VRBaseMap,
  89. bool IsClone, bool IsCloned);
  90. public:
  91. /// CountResults - The results of target nodes have register or immediate
  92. /// operands first, then an optional chain, and optional flag operands
  93. /// (which do not go into the machine instrs.)
  94. static unsigned CountResults(SDNode *Node);
  95. void AddDbgValueLocationOps(MachineInstrBuilder &MIB,
  96. const MCInstrDesc &DbgValDesc,
  97. ArrayRef<SDDbgOperand> Locations,
  98. DenseMap<SDValue, Register> &VRBaseMap);
  99. /// EmitDbgValue - Generate machine instruction for a dbg_value node.
  100. ///
  101. MachineInstr *EmitDbgValue(SDDbgValue *SD,
  102. DenseMap<SDValue, Register> &VRBaseMap);
  103. /// Emit a dbg_value as a DBG_INSTR_REF. May produce DBG_VALUE $noreg instead
  104. /// if there is no variable location; alternately a half-formed DBG_INSTR_REF
  105. /// that refers to a virtual register and is corrected later in isel.
  106. MachineInstr *EmitDbgInstrRef(SDDbgValue *SD,
  107. DenseMap<SDValue, Register> &VRBaseMap);
  108. /// Emit a DBG_VALUE $noreg, indicating a variable has no location.
  109. MachineInstr *EmitDbgNoLocation(SDDbgValue *SD);
  110. /// Emit a DBG_VALUE from the operands to SDDbgValue.
  111. MachineInstr *EmitDbgValueFromSingleOp(SDDbgValue *SD,
  112. DenseMap<SDValue, Register> &VRBaseMap);
  113. /// Generate machine instruction for a dbg_label node.
  114. MachineInstr *EmitDbgLabel(SDDbgLabel *SD);
  115. /// EmitNode - Generate machine code for a node and needed dependencies.
  116. ///
  117. void EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
  118. DenseMap<SDValue, Register> &VRBaseMap) {
  119. if (Node->isMachineOpcode())
  120. EmitMachineNode(Node, IsClone, IsCloned, VRBaseMap);
  121. else
  122. EmitSpecialNode(Node, IsClone, IsCloned, VRBaseMap);
  123. }
  124. /// getBlock - Return the current basic block.
  125. MachineBasicBlock *getBlock() { return MBB; }
  126. /// getInsertPos - Return the current insertion position.
  127. MachineBasicBlock::iterator getInsertPos() { return InsertPos; }
  128. /// InstrEmitter - Construct an InstrEmitter and set it to start inserting
  129. /// at the given position in the given block.
  130. InstrEmitter(const TargetMachine &TM, MachineBasicBlock *mbb,
  131. MachineBasicBlock::iterator insertpos,
  132. bool UseInstrRefDebugInfo);
  133. private:
  134. void EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
  135. DenseMap<SDValue, Register> &VRBaseMap);
  136. void EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
  137. DenseMap<SDValue, Register> &VRBaseMap);
  138. };
  139. } // namespace llvm
  140. #endif