SyncDependenceAnalysis.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SyncDependenceAnalysis.h - Divergent Branch Dependence -*- 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
  15. // This file defines the SyncDependenceAnalysis class, which computes for
  16. // every divergent branch the set of phi nodes that the branch will make
  17. // divergent.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_ANALYSIS_SYNCDEPENDENCEANALYSIS_H
  21. #define LLVM_ANALYSIS_SYNCDEPENDENCEANALYSIS_H
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/PostOrderIterator.h"
  24. #include "llvm/ADT/SmallPtrSet.h"
  25. #include "llvm/Analysis/LoopInfo.h"
  26. #include <map>
  27. #include <memory>
  28. #include <unordered_map>
  29. namespace llvm {
  30. class BasicBlock;
  31. class DominatorTree;
  32. class PostDominatorTree;
  33. using ConstBlockSet = SmallPtrSet<const BasicBlock *, 4>;
  34. struct ControlDivergenceDesc {
  35. // Join points of divergent disjoint paths.
  36. ConstBlockSet JoinDivBlocks;
  37. // Divergent loop exits
  38. ConstBlockSet LoopDivBlocks;
  39. };
  40. struct ModifiedPO {
  41. std::vector<const BasicBlock *> LoopPO;
  42. std::unordered_map<const BasicBlock *, unsigned> POIndex;
  43. void appendBlock(const BasicBlock &BB) {
  44. POIndex[&BB] = LoopPO.size();
  45. LoopPO.push_back(&BB);
  46. }
  47. unsigned getIndexOf(const BasicBlock &BB) const {
  48. return POIndex.find(&BB)->second;
  49. }
  50. unsigned size() const { return LoopPO.size(); }
  51. const BasicBlock *getBlockAt(unsigned Idx) const { return LoopPO[Idx]; }
  52. };
  53. /// \brief Relates points of divergent control to join points in
  54. /// reducible CFGs.
  55. ///
  56. /// This analysis relates points of divergent control to points of converging
  57. /// divergent control. The analysis requires all loops to be reducible.
  58. class SyncDependenceAnalysis {
  59. public:
  60. ~SyncDependenceAnalysis();
  61. SyncDependenceAnalysis(const DominatorTree &DT, const PostDominatorTree &PDT,
  62. const LoopInfo &LI);
  63. /// \brief Computes divergent join points and loop exits caused by branch
  64. /// divergence in \p Term.
  65. ///
  66. /// The set of blocks which are reachable by disjoint paths from \p Term.
  67. /// The set also contains loop exits if there two disjoint paths:
  68. /// one from \p Term to the loop exit and another from \p Term to the loop
  69. /// header. Those exit blocks are added to the returned set.
  70. /// If L is the parent loop of \p Term and an exit of L is in the returned
  71. /// set then L is a divergent loop.
  72. const ControlDivergenceDesc &getJoinBlocks(const Instruction &Term);
  73. private:
  74. static ControlDivergenceDesc EmptyDivergenceDesc;
  75. ModifiedPO LoopPO;
  76. const DominatorTree &DT;
  77. const PostDominatorTree &PDT;
  78. const LoopInfo &LI;
  79. std::map<const Instruction *, std::unique_ptr<ControlDivergenceDesc>>
  80. CachedControlDivDescs;
  81. };
  82. } // namespace llvm
  83. #endif // LLVM_ANALYSIS_SYNCDEPENDENCEANALYSIS_H
  84. #ifdef __GNUC__
  85. #pragma GCC diagnostic pop
  86. #endif