InstrEmitter.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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, bool IsClone,
  40. Register SrcReg, DenseMap<SDValue, Register> &VRBaseMap);
  41. void CreateVirtualRegisters(SDNode *Node,
  42. MachineInstrBuilder &MIB,
  43. const MCInstrDesc &II,
  44. bool IsClone, bool IsCloned,
  45. DenseMap<SDValue, Register> &VRBaseMap);
  46. /// getVR - Return the virtual register corresponding to the specified result
  47. /// of the specified node.
  48. Register getVR(SDValue Op,
  49. DenseMap<SDValue, Register> &VRBaseMap);
  50. /// AddRegisterOperand - Add the specified register as an operand to the
  51. /// specified machine instr. Insert register copies if the register is
  52. /// not in the required register class.
  53. void AddRegisterOperand(MachineInstrBuilder &MIB,
  54. SDValue Op,
  55. unsigned IIOpNum,
  56. const MCInstrDesc *II,
  57. DenseMap<SDValue, Register> &VRBaseMap,
  58. bool IsDebug, bool IsClone, bool IsCloned);
  59. /// AddOperand - Add the specified operand to the specified machine instr. II
  60. /// specifies the instruction information for the node, and IIOpNum is the
  61. /// operand number (in the II) that we are adding. IIOpNum and II are used for
  62. /// assertions only.
  63. void AddOperand(MachineInstrBuilder &MIB,
  64. SDValue Op,
  65. unsigned IIOpNum,
  66. const MCInstrDesc *II,
  67. DenseMap<SDValue, Register> &VRBaseMap,
  68. bool IsDebug, bool IsClone, bool IsCloned);
  69. /// ConstrainForSubReg - Try to constrain VReg to a register class that
  70. /// supports SubIdx sub-registers. Emit a copy if that isn't possible.
  71. /// Return the virtual register to use.
  72. Register ConstrainForSubReg(Register VReg, unsigned SubIdx, MVT VT,
  73. bool isDivergent, const DebugLoc &DL);
  74. /// EmitSubregNode - Generate machine code for subreg nodes.
  75. ///
  76. void EmitSubregNode(SDNode *Node, DenseMap<SDValue, Register> &VRBaseMap,
  77. bool IsClone, bool IsCloned);
  78. /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
  79. /// COPY_TO_REGCLASS is just a normal copy, except that the destination
  80. /// register is constrained to be in a particular register class.
  81. ///
  82. void EmitCopyToRegClassNode(SDNode *Node,
  83. DenseMap<SDValue, Register> &VRBaseMap);
  84. /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
  85. ///
  86. void EmitRegSequence(SDNode *Node, DenseMap<SDValue, Register> &VRBaseMap,
  87. bool IsClone, bool IsCloned);
  88. public:
  89. /// CountResults - The results of target nodes have register or immediate
  90. /// operands first, then an optional chain, and optional flag operands
  91. /// (which do not go into the machine instrs.)
  92. static unsigned CountResults(SDNode *Node);
  93. void AddDbgValueLocationOps(MachineInstrBuilder &MIB,
  94. const MCInstrDesc &DbgValDesc,
  95. ArrayRef<SDDbgOperand> Locations,
  96. DenseMap<SDValue, Register> &VRBaseMap);
  97. /// EmitDbgValue - Generate machine instruction for a dbg_value node.
  98. ///
  99. MachineInstr *EmitDbgValue(SDDbgValue *SD,
  100. DenseMap<SDValue, Register> &VRBaseMap);
  101. /// Emit a dbg_value as a DBG_INSTR_REF. May produce DBG_VALUE $noreg instead
  102. /// if there is no variable location; alternately a half-formed DBG_INSTR_REF
  103. /// that refers to a virtual register and is corrected later in isel.
  104. MachineInstr *EmitDbgInstrRef(SDDbgValue *SD,
  105. DenseMap<SDValue, Register> &VRBaseMap);
  106. /// Emit a DBG_VALUE $noreg, indicating a variable has no location.
  107. MachineInstr *EmitDbgNoLocation(SDDbgValue *SD);
  108. /// Emit a DBG_VALUE_LIST from the operands to SDDbgValue.
  109. MachineInstr *EmitDbgValueList(SDDbgValue *SD,
  110. DenseMap<SDValue, Register> &VRBaseMap);
  111. /// Emit a DBG_VALUE from the operands to SDDbgValue.
  112. MachineInstr *EmitDbgValueFromSingleOp(SDDbgValue *SD,
  113. DenseMap<SDValue, Register> &VRBaseMap);
  114. /// Generate machine instruction for a dbg_label node.
  115. MachineInstr *EmitDbgLabel(SDDbgLabel *SD);
  116. /// EmitNode - Generate machine code for a node and needed dependencies.
  117. ///
  118. void EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
  119. DenseMap<SDValue, Register> &VRBaseMap) {
  120. if (Node->isMachineOpcode())
  121. EmitMachineNode(Node, IsClone, IsCloned, VRBaseMap);
  122. else
  123. EmitSpecialNode(Node, IsClone, IsCloned, VRBaseMap);
  124. }
  125. /// getBlock - Return the current basic block.
  126. MachineBasicBlock *getBlock() { return MBB; }
  127. /// getInsertPos - Return the current insertion position.
  128. MachineBasicBlock::iterator getInsertPos() { return InsertPos; }
  129. /// InstrEmitter - Construct an InstrEmitter and set it to start inserting
  130. /// at the given position in the given block.
  131. InstrEmitter(const TargetMachine &TM, MachineBasicBlock *mbb,
  132. MachineBasicBlock::iterator insertpos);
  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