xref: /openbsd-src/gnu/llvm/llvm/include/llvm/ExecutionEngine/Orc/SpeculateAnalyses.h (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick //===-- SpeculateAnalyses.h  --*- C++ -*-===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
873471bf0Spatrick /// \file
909467b48Spatrick /// Contains the Analyses and Result Interpretation to select likely functions
1009467b48Spatrick /// to Speculatively compile before they are called. [Purely Experimentation]
1109467b48Spatrick //===----------------------------------------------------------------------===//
1209467b48Spatrick 
1309467b48Spatrick #ifndef LLVM_EXECUTIONENGINE_ORC_SPECULATEANALYSES_H
1409467b48Spatrick #define LLVM_EXECUTIONENGINE_ORC_SPECULATEANALYSES_H
1509467b48Spatrick 
1609467b48Spatrick #include "llvm/Analysis/BranchProbabilityInfo.h"
1709467b48Spatrick #include "llvm/ExecutionEngine/Orc/Core.h"
1809467b48Spatrick #include "llvm/ExecutionEngine/Orc/Speculation.h"
1909467b48Spatrick 
2009467b48Spatrick #include <vector>
2109467b48Spatrick 
2209467b48Spatrick namespace llvm {
2309467b48Spatrick 
2409467b48Spatrick namespace orc {
2509467b48Spatrick 
2609467b48Spatrick // Provides common code.
2709467b48Spatrick class SpeculateQuery {
2809467b48Spatrick protected:
2909467b48Spatrick   void findCalles(const BasicBlock *, DenseSet<StringRef> &);
3009467b48Spatrick   bool isStraightLine(const Function &F);
3109467b48Spatrick 
3209467b48Spatrick public:
33*d415bd75Srobert   using ResultTy = std::optional<DenseMap<StringRef, DenseSet<StringRef>>>;
3409467b48Spatrick };
3509467b48Spatrick 
3609467b48Spatrick // Direct calls in high frequency basic blocks are extracted.
3709467b48Spatrick class BlockFreqQuery : public SpeculateQuery {
3809467b48Spatrick   size_t numBBToGet(size_t);
3909467b48Spatrick 
4009467b48Spatrick public:
4109467b48Spatrick   // Find likely next executables based on IR Block Frequency
4209467b48Spatrick   ResultTy operator()(Function &F);
4309467b48Spatrick };
4409467b48Spatrick 
4509467b48Spatrick // This Query generates a sequence of basic blocks which follows the order of
4609467b48Spatrick // execution.
4709467b48Spatrick // A handful of BB with higher block frequencies are taken, then path to entry
4809467b48Spatrick // and end BB are discovered by traversing up & down the CFG.
4909467b48Spatrick class SequenceBBQuery : public SpeculateQuery {
5009467b48Spatrick   struct WalkDirection {
5109467b48Spatrick     bool Upward = true, Downward = true;
5209467b48Spatrick     // the block associated contain a call
5309467b48Spatrick     bool CallerBlock = false;
5409467b48Spatrick   };
5509467b48Spatrick 
5609467b48Spatrick public:
5709467b48Spatrick   using VisitedBlocksInfoTy = DenseMap<const BasicBlock *, WalkDirection>;
5809467b48Spatrick   using BlockListTy = SmallVector<const BasicBlock *, 8>;
5909467b48Spatrick   using BackEdgesInfoTy =
6009467b48Spatrick       SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 8>;
6109467b48Spatrick   using BlockFreqInfoTy =
6209467b48Spatrick       SmallVector<std::pair<const BasicBlock *, uint64_t>, 8>;
6309467b48Spatrick 
6409467b48Spatrick private:
6509467b48Spatrick   std::size_t getHottestBlocks(std::size_t TotalBlocks);
6609467b48Spatrick   BlockListTy rearrangeBB(const Function &, const BlockListTy &);
6709467b48Spatrick   BlockListTy queryCFG(Function &, const BlockListTy &);
6809467b48Spatrick   void traverseToEntryBlock(const BasicBlock *, const BlockListTy &,
6909467b48Spatrick                             const BackEdgesInfoTy &,
7009467b48Spatrick                             const BranchProbabilityInfo *,
7109467b48Spatrick                             VisitedBlocksInfoTy &);
7209467b48Spatrick   void traverseToExitBlock(const BasicBlock *, const BlockListTy &,
7309467b48Spatrick                            const BackEdgesInfoTy &,
7409467b48Spatrick                            const BranchProbabilityInfo *,
7509467b48Spatrick                            VisitedBlocksInfoTy &);
7609467b48Spatrick 
7709467b48Spatrick public:
7809467b48Spatrick   ResultTy operator()(Function &F);
7909467b48Spatrick };
8009467b48Spatrick 
8109467b48Spatrick } // namespace orc
8209467b48Spatrick } // namespace llvm
8309467b48Spatrick 
8409467b48Spatrick #endif // LLVM_EXECUTIONENGINE_ORC_SPECULATEANALYSES_H
85