ReachingDefAnalysis.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //==--- llvm/CodeGen/ReachingDefAnalysis.h - Reaching Def Analysis -*- 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. /// \file Reaching Defs Analysis pass.
  10. ///
  11. /// This pass tracks for each instruction what is the "closest" reaching def of
  12. /// a given register. It is used by BreakFalseDeps (for clearance calculation)
  13. /// and ExecutionDomainFix (for arbitrating conflicting domains).
  14. ///
  15. /// Note that this is different from the usual definition notion of liveness.
  16. /// The CPU doesn't care whether or not we consider a register killed.
  17. ///
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_CODEGEN_REACHINGDEFANALYSIS_H
  21. #define LLVM_CODEGEN_REACHINGDEFANALYSIS_H
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/TinyPtrVector.h"
  25. #include "llvm/CodeGen/LoopTraversal.h"
  26. #include "llvm/CodeGen/MachineFunctionPass.h"
  27. #include "llvm/InitializePasses.h"
  28. namespace llvm {
  29. class MachineBasicBlock;
  30. class MachineInstr;
  31. /// Thin wrapper around "int" used to store reaching definitions,
  32. /// using an encoding that makes it compatible with TinyPtrVector.
  33. /// The 0th LSB is forced zero (and will be used for pointer union tagging),
  34. /// The 1st LSB is forced one (to make sure the value is non-zero).
  35. class ReachingDef {
  36. uintptr_t Encoded;
  37. friend struct PointerLikeTypeTraits<ReachingDef>;
  38. explicit ReachingDef(uintptr_t Encoded) : Encoded(Encoded) {}
  39. public:
  40. ReachingDef(std::nullptr_t) : Encoded(0) {}
  41. ReachingDef(int Instr) : Encoded(((uintptr_t) Instr << 2) | 2) {}
  42. operator int() const { return ((int) Encoded) >> 2; }
  43. };
  44. template<>
  45. struct PointerLikeTypeTraits<ReachingDef> {
  46. static constexpr int NumLowBitsAvailable = 1;
  47. static inline void *getAsVoidPointer(const ReachingDef &RD) {
  48. return reinterpret_cast<void *>(RD.Encoded);
  49. }
  50. static inline ReachingDef getFromVoidPointer(void *P) {
  51. return ReachingDef(reinterpret_cast<uintptr_t>(P));
  52. }
  53. static inline ReachingDef getFromVoidPointer(const void *P) {
  54. return ReachingDef(reinterpret_cast<uintptr_t>(P));
  55. }
  56. };
  57. /// This class provides the reaching def analysis.
  58. class ReachingDefAnalysis : public MachineFunctionPass {
  59. private:
  60. MachineFunction *MF;
  61. const TargetRegisterInfo *TRI;
  62. LoopTraversal::TraversalOrder TraversedMBBOrder;
  63. unsigned NumRegUnits;
  64. /// Instruction that defined each register, relative to the beginning of the
  65. /// current basic block. When a LiveRegsDefInfo is used to represent a
  66. /// live-out register, this value is relative to the end of the basic block,
  67. /// so it will be a negative number.
  68. using LiveRegsDefInfo = std::vector<int>;
  69. LiveRegsDefInfo LiveRegs;
  70. /// Keeps clearance information for all registers. Note that this
  71. /// is different from the usual definition notion of liveness. The CPU
  72. /// doesn't care whether or not we consider a register killed.
  73. using OutRegsInfoMap = SmallVector<LiveRegsDefInfo, 4>;
  74. OutRegsInfoMap MBBOutRegsInfos;
  75. /// Current instruction number.
  76. /// The first instruction in each basic block is 0.
  77. int CurInstr;
  78. /// Maps instructions to their instruction Ids, relative to the beginning of
  79. /// their basic blocks.
  80. DenseMap<MachineInstr *, int> InstIds;
  81. /// All reaching defs of a given RegUnit for a given MBB.
  82. using MBBRegUnitDefs = TinyPtrVector<ReachingDef>;
  83. /// All reaching defs of all reg units for a given MBB
  84. using MBBDefsInfo = std::vector<MBBRegUnitDefs>;
  85. /// All reaching defs of all reg units for a all MBBs
  86. using MBBReachingDefsInfo = SmallVector<MBBDefsInfo, 4>;
  87. MBBReachingDefsInfo MBBReachingDefs;
  88. /// Default values are 'nothing happened a long time ago'.
  89. const int ReachingDefDefaultVal = -(1 << 20);
  90. using InstSet = SmallPtrSetImpl<MachineInstr*>;
  91. using BlockSet = SmallPtrSetImpl<MachineBasicBlock*>;
  92. public:
  93. static char ID; // Pass identification, replacement for typeid
  94. ReachingDefAnalysis() : MachineFunctionPass(ID) {
  95. initializeReachingDefAnalysisPass(*PassRegistry::getPassRegistry());
  96. }
  97. void releaseMemory() override;
  98. void getAnalysisUsage(AnalysisUsage &AU) const override {
  99. AU.setPreservesAll();
  100. MachineFunctionPass::getAnalysisUsage(AU);
  101. }
  102. bool runOnMachineFunction(MachineFunction &MF) override;
  103. MachineFunctionProperties getRequiredProperties() const override {
  104. return MachineFunctionProperties().set(
  105. MachineFunctionProperties::Property::NoVRegs).set(
  106. MachineFunctionProperties::Property::TracksLiveness);
  107. }
  108. /// Re-run the analysis.
  109. void reset();
  110. /// Initialize data structures.
  111. void init();
  112. /// Traverse the machine function, mapping definitions.
  113. void traverse();
  114. /// Provides the instruction id of the closest reaching def instruction of
  115. /// PhysReg that reaches MI, relative to the begining of MI's basic block.
  116. int getReachingDef(MachineInstr *MI, MCRegister PhysReg) const;
  117. /// Return whether A and B use the same def of PhysReg.
  118. bool hasSameReachingDef(MachineInstr *A, MachineInstr *B,
  119. MCRegister PhysReg) const;
  120. /// Return whether the reaching def for MI also is live out of its parent
  121. /// block.
  122. bool isReachingDefLiveOut(MachineInstr *MI, MCRegister PhysReg) const;
  123. /// Return the local MI that produces the live out value for PhysReg, or
  124. /// nullptr for a non-live out or non-local def.
  125. MachineInstr *getLocalLiveOutMIDef(MachineBasicBlock *MBB,
  126. MCRegister PhysReg) const;
  127. /// If a single MachineInstr creates the reaching definition, then return it.
  128. /// Otherwise return null.
  129. MachineInstr *getUniqueReachingMIDef(MachineInstr *MI,
  130. MCRegister PhysReg) const;
  131. /// If a single MachineInstr creates the reaching definition, for MIs operand
  132. /// at Idx, then return it. Otherwise return null.
  133. MachineInstr *getMIOperand(MachineInstr *MI, unsigned Idx) const;
  134. /// If a single MachineInstr creates the reaching definition, for MIs MO,
  135. /// then return it. Otherwise return null.
  136. MachineInstr *getMIOperand(MachineInstr *MI, MachineOperand &MO) const;
  137. /// Provide whether the register has been defined in the same basic block as,
  138. /// and before, MI.
  139. bool hasLocalDefBefore(MachineInstr *MI, MCRegister PhysReg) const;
  140. /// Return whether the given register is used after MI, whether it's a local
  141. /// use or a live out.
  142. bool isRegUsedAfter(MachineInstr *MI, MCRegister PhysReg) const;
  143. /// Return whether the given register is defined after MI.
  144. bool isRegDefinedAfter(MachineInstr *MI, MCRegister PhysReg) const;
  145. /// Provides the clearance - the number of instructions since the closest
  146. /// reaching def instuction of PhysReg that reaches MI.
  147. int getClearance(MachineInstr *MI, MCRegister PhysReg) const;
  148. /// Provides the uses, in the same block as MI, of register that MI defines.
  149. /// This does not consider live-outs.
  150. void getReachingLocalUses(MachineInstr *MI, MCRegister PhysReg,
  151. InstSet &Uses) const;
  152. /// Search MBB for a definition of PhysReg and insert it into Defs. If no
  153. /// definition is found, recursively search the predecessor blocks for them.
  154. void getLiveOuts(MachineBasicBlock *MBB, MCRegister PhysReg, InstSet &Defs,
  155. BlockSet &VisitedBBs) const;
  156. void getLiveOuts(MachineBasicBlock *MBB, MCRegister PhysReg,
  157. InstSet &Defs) const;
  158. /// For the given block, collect the instructions that use the live-in
  159. /// value of the provided register. Return whether the value is still
  160. /// live on exit.
  161. bool getLiveInUses(MachineBasicBlock *MBB, MCRegister PhysReg,
  162. InstSet &Uses) const;
  163. /// Collect the users of the value stored in PhysReg, which is defined
  164. /// by MI.
  165. void getGlobalUses(MachineInstr *MI, MCRegister PhysReg, InstSet &Uses) const;
  166. /// Collect all possible definitions of the value stored in PhysReg, which is
  167. /// used by MI.
  168. void getGlobalReachingDefs(MachineInstr *MI, MCRegister PhysReg,
  169. InstSet &Defs) const;
  170. /// Return whether From can be moved forwards to just before To.
  171. bool isSafeToMoveForwards(MachineInstr *From, MachineInstr *To) const;
  172. /// Return whether From can be moved backwards to just after To.
  173. bool isSafeToMoveBackwards(MachineInstr *From, MachineInstr *To) const;
  174. /// Assuming MI is dead, recursively search the incoming operands which are
  175. /// killed by MI and collect those that would become dead.
  176. void collectKilledOperands(MachineInstr *MI, InstSet &Dead) const;
  177. /// Return whether removing this instruction will have no effect on the
  178. /// program, returning the redundant use-def chain.
  179. bool isSafeToRemove(MachineInstr *MI, InstSet &ToRemove) const;
  180. /// Return whether removing this instruction will have no effect on the
  181. /// program, ignoring the possible effects on some instructions, returning
  182. /// the redundant use-def chain.
  183. bool isSafeToRemove(MachineInstr *MI, InstSet &ToRemove,
  184. InstSet &Ignore) const;
  185. /// Return whether a MachineInstr could be inserted at MI and safely define
  186. /// the given register without affecting the program.
  187. bool isSafeToDefRegAt(MachineInstr *MI, MCRegister PhysReg) const;
  188. /// Return whether a MachineInstr could be inserted at MI and safely define
  189. /// the given register without affecting the program, ignoring any effects
  190. /// on the provided instructions.
  191. bool isSafeToDefRegAt(MachineInstr *MI, MCRegister PhysReg,
  192. InstSet &Ignore) const;
  193. private:
  194. /// Set up LiveRegs by merging predecessor live-out values.
  195. void enterBasicBlock(MachineBasicBlock *MBB);
  196. /// Update live-out values.
  197. void leaveBasicBlock(MachineBasicBlock *MBB);
  198. /// Process he given basic block.
  199. void processBasicBlock(const LoopTraversal::TraversedMBBInfo &TraversedMBB);
  200. /// Process block that is part of a loop again.
  201. void reprocessBasicBlock(MachineBasicBlock *MBB);
  202. /// Update def-ages for registers defined by MI.
  203. /// Also break dependencies on partial defs and undef uses.
  204. void processDefs(MachineInstr *);
  205. /// Utility function for isSafeToMoveForwards/Backwards.
  206. template<typename Iterator>
  207. bool isSafeToMove(MachineInstr *From, MachineInstr *To) const;
  208. /// Return whether removing this instruction will have no effect on the
  209. /// program, ignoring the possible effects on some instructions, returning
  210. /// the redundant use-def chain.
  211. bool isSafeToRemove(MachineInstr *MI, InstSet &Visited,
  212. InstSet &ToRemove, InstSet &Ignore) const;
  213. /// Provides the MI, from the given block, corresponding to the Id or a
  214. /// nullptr if the id does not refer to the block.
  215. MachineInstr *getInstFromId(MachineBasicBlock *MBB, int InstId) const;
  216. /// Provides the instruction of the closest reaching def instruction of
  217. /// PhysReg that reaches MI, relative to the begining of MI's basic block.
  218. MachineInstr *getReachingLocalMIDef(MachineInstr *MI,
  219. MCRegister PhysReg) const;
  220. };
  221. } // namespace llvm
  222. #endif // LLVM_CODEGEN_REACHINGDEFANALYSIS_H