ReachingDefAnalysis.h 11 KB

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