SpeculateAnalyses.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- SpeculateAnalyses.h --*- 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. /// \file
  14. /// Contains the Analyses and Result Interpretation to select likely functions
  15. /// to Speculatively compile before they are called. [Purely Experimentation]
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_SPECULATEANALYSES_H
  18. #define LLVM_EXECUTIONENGINE_ORC_SPECULATEANALYSES_H
  19. #include "llvm/Analysis/BranchProbabilityInfo.h"
  20. #include "llvm/ExecutionEngine/Orc/Core.h"
  21. #include "llvm/ExecutionEngine/Orc/Speculation.h"
  22. #include <vector>
  23. namespace llvm {
  24. namespace orc {
  25. // Provides common code.
  26. class SpeculateQuery {
  27. protected:
  28. void findCalles(const BasicBlock *, DenseSet<StringRef> &);
  29. bool isStraightLine(const Function &F);
  30. public:
  31. using ResultTy = Optional<DenseMap<StringRef, DenseSet<StringRef>>>;
  32. };
  33. // Direct calls in high frequency basic blocks are extracted.
  34. class BlockFreqQuery : public SpeculateQuery {
  35. size_t numBBToGet(size_t);
  36. public:
  37. // Find likely next executables based on IR Block Frequency
  38. ResultTy operator()(Function &F);
  39. };
  40. // This Query generates a sequence of basic blocks which follows the order of
  41. // execution.
  42. // A handful of BB with higher block frequencies are taken, then path to entry
  43. // and end BB are discovered by traversing up & down the CFG.
  44. class SequenceBBQuery : public SpeculateQuery {
  45. struct WalkDirection {
  46. bool Upward = true, Downward = true;
  47. // the block associated contain a call
  48. bool CallerBlock = false;
  49. };
  50. public:
  51. using VisitedBlocksInfoTy = DenseMap<const BasicBlock *, WalkDirection>;
  52. using BlockListTy = SmallVector<const BasicBlock *, 8>;
  53. using BackEdgesInfoTy =
  54. SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 8>;
  55. using BlockFreqInfoTy =
  56. SmallVector<std::pair<const BasicBlock *, uint64_t>, 8>;
  57. private:
  58. std::size_t getHottestBlocks(std::size_t TotalBlocks);
  59. BlockListTy rearrangeBB(const Function &, const BlockListTy &);
  60. BlockListTy queryCFG(Function &, const BlockListTy &);
  61. void traverseToEntryBlock(const BasicBlock *, const BlockListTy &,
  62. const BackEdgesInfoTy &,
  63. const BranchProbabilityInfo *,
  64. VisitedBlocksInfoTy &);
  65. void traverseToExitBlock(const BasicBlock *, const BlockListTy &,
  66. const BackEdgesInfoTy &,
  67. const BranchProbabilityInfo *,
  68. VisitedBlocksInfoTy &);
  69. public:
  70. ResultTy operator()(Function &F);
  71. };
  72. } // namespace orc
  73. } // namespace llvm
  74. #endif // LLVM_EXECUTIONENGINE_ORC_SPECULATEANALYSES_H
  75. #ifdef __GNUC__
  76. #pragma GCC diagnostic pop
  77. #endif