InstrEmitter.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 TargetLowering;
  25. class TargetMachine;
  26. class LLVM_LIBRARY_VISIBILITY InstrEmitter {
  27. MachineFunction *MF;
  28. MachineRegisterInfo *MRI;
  29. const TargetInstrInfo *TII;
  30. const TargetRegisterInfo *TRI;
  31. const TargetLowering *TLI;
  32. MachineBasicBlock *MBB;
  33. MachineBasicBlock::iterator InsertPos;
  34. /// Should we try to produce DBG_INSTR_REF instructions?
  35. bool EmitDebugInstrRefs;
  36. /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
  37. /// implicit physical register output.
  38. void EmitCopyFromReg(SDNode *Node, unsigned ResNo,
  39. bool IsClone, bool IsCloned,
  40. Register SrcReg,
  41. DenseMap<SDValue, Register> &VRBaseMap);
  42. void CreateVirtualRegisters(SDNode *Node,
  43. MachineInstrBuilder &MIB,
  44. const MCInstrDesc &II,
  45. bool IsClone, bool IsCloned,
  46. DenseMap<SDValue, Register> &VRBaseMap);
  47. /// getVR - Return the virtual register corresponding to the specified result
  48. /// of the specified node.
  49. Register getVR(SDValue Op,
  50. DenseMap<SDValue, Register> &VRBaseMap);
  51. /// AddRegisterOperand - Add the specified register as an operand to the
  52. /// specified machine instr. Insert register copies if the register is
  53. /// not in the required register class.
  54. void AddRegisterOperand(MachineInstrBuilder &MIB,
  55. SDValue Op,
  56. unsigned IIOpNum,
  57. const MCInstrDesc *II,
  58. DenseMap<SDValue, Register> &VRBaseMap,
  59. bool IsDebug, bool IsClone, bool IsCloned);
  60. /// AddOperand - Add the specified operand to the specified machine instr. II
  61. /// specifies the instruction information for the node, and IIOpNum is the
  62. /// operand number (in the II) that we are adding. IIOpNum and II are used for
  63. /// assertions only.
  64. void AddOperand(MachineInstrBuilder &MIB,
  65. SDValue Op,
  66. unsigned IIOpNum,
  67. const MCInstrDesc *II,
  68. DenseMap<SDValue, Register> &VRBaseMap,
  69. bool IsDebug, bool IsClone, bool IsCloned);
  70. /// ConstrainForSubReg - Try to constrain VReg to a register class that
  71. /// supports SubIdx sub-registers. Emit a copy if that isn't possible.
  72. /// Return the virtual register to use.
  73. Register ConstrainForSubReg(Register VReg, unsigned SubIdx, MVT VT,
  74. bool isDivergent, const DebugLoc &DL);
  75. /// EmitSubregNode - Generate machine code for subreg nodes.
  76. ///
  77. void EmitSubregNode(SDNode *Node, DenseMap<SDValue, Register> &VRBaseMap,
  78. bool IsClone, bool IsCloned);
  79. /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
  80. /// COPY_TO_REGCLASS is just a normal copy, except that the destination
  81. /// register is constrained to be in a particular register class.
  82. ///
  83. void EmitCopyToRegClassNode(SDNode *Node,
  84. DenseMap<SDValue, Register> &VRBaseMap);
  85. /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
  86. ///
  87. void EmitRegSequence(SDNode *Node, DenseMap<SDValue, Register> &VRBaseMap,
  88. bool IsClone, bool IsCloned);
  89. public:
  90. /// CountResults - The results of target nodes have register or immediate
  91. /// operands first, then an optional chain, and optional flag operands
  92. /// (which do not go into the machine instrs.)
  93. static unsigned CountResults(SDNode *Node);
  94. /// EmitDbgValue - Generate machine instruction for a dbg_value node.
  95. ///
  96. MachineInstr *EmitDbgValue(SDDbgValue *SD,
  97. DenseMap<SDValue, Register> &VRBaseMap);
  98. /// Attempt to emit a dbg_value as a DBG_INSTR_REF. May fail and return
  99. /// nullptr, in which case we fall back to plain EmitDbgValue.
  100. MachineInstr *EmitDbgInstrRef(SDDbgValue *SD,
  101. DenseMap<SDValue, Register> &VRBaseMap);
  102. /// Generate machine instruction for a dbg_label node.
  103. MachineInstr *EmitDbgLabel(SDDbgLabel *SD);
  104. /// EmitNode - Generate machine code for a node and needed dependencies.
  105. ///
  106. void EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
  107. DenseMap<SDValue, Register> &VRBaseMap) {
  108. if (Node->isMachineOpcode())
  109. EmitMachineNode(Node, IsClone, IsCloned, VRBaseMap);
  110. else
  111. EmitSpecialNode(Node, IsClone, IsCloned, VRBaseMap);
  112. }
  113. /// getBlock - Return the current basic block.
  114. MachineBasicBlock *getBlock() { return MBB; }
  115. /// getInsertPos - Return the current insertion position.
  116. MachineBasicBlock::iterator getInsertPos() { return InsertPos; }
  117. /// InstrEmitter - Construct an InstrEmitter and set it to start inserting
  118. /// at the given position in the given block.
  119. InstrEmitter(const TargetMachine &TM, MachineBasicBlock *mbb,
  120. MachineBasicBlock::iterator insertpos);
  121. private:
  122. void EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
  123. DenseMap<SDValue, Register> &VRBaseMap);
  124. void EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
  125. DenseMap<SDValue, Register> &VRBaseMap);
  126. };
  127. }
  128. #endif