18bcb0991SDimitry Andric //===-- SpeculateAnalyses.h --*- C++ -*-===// 28bcb0991SDimitry Andric // 38bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 48bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 58bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 68bcb0991SDimitry Andric // 78bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric /// \file 98bcb0991SDimitry Andric /// Contains the Analyses and Result Interpretation to select likely functions 108bcb0991SDimitry Andric /// to Speculatively compile before they are called. [Purely Experimentation] 118bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 128bcb0991SDimitry Andric 138bcb0991SDimitry Andric #ifndef LLVM_EXECUTIONENGINE_ORC_SPECULATEANALYSES_H 148bcb0991SDimitry Andric #define LLVM_EXECUTIONENGINE_ORC_SPECULATEANALYSES_H 158bcb0991SDimitry Andric 168bcb0991SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h" 178bcb0991SDimitry Andric #include "llvm/ExecutionEngine/Orc/Core.h" 188bcb0991SDimitry Andric #include "llvm/ExecutionEngine/Orc/Speculation.h" 198bcb0991SDimitry Andric 208bcb0991SDimitry Andric namespace llvm { 218bcb0991SDimitry Andric 228bcb0991SDimitry Andric namespace orc { 238bcb0991SDimitry Andric 248bcb0991SDimitry Andric // Provides common code. 258bcb0991SDimitry Andric class SpeculateQuery { 268bcb0991SDimitry Andric protected: 278bcb0991SDimitry Andric void findCalles(const BasicBlock *, DenseSet<StringRef> &); 288bcb0991SDimitry Andric bool isStraightLine(const Function &F); 298bcb0991SDimitry Andric 308bcb0991SDimitry Andric public: 31*bdd1243dSDimitry Andric using ResultTy = std::optional<DenseMap<StringRef, DenseSet<StringRef>>>; 328bcb0991SDimitry Andric }; 338bcb0991SDimitry Andric 348bcb0991SDimitry Andric // Direct calls in high frequency basic blocks are extracted. 358bcb0991SDimitry Andric class BlockFreqQuery : public SpeculateQuery { 368bcb0991SDimitry Andric size_t numBBToGet(size_t); 378bcb0991SDimitry Andric 388bcb0991SDimitry Andric public: 398bcb0991SDimitry Andric // Find likely next executables based on IR Block Frequency 408bcb0991SDimitry Andric ResultTy operator()(Function &F); 418bcb0991SDimitry Andric }; 428bcb0991SDimitry Andric 438bcb0991SDimitry Andric // This Query generates a sequence of basic blocks which follows the order of 448bcb0991SDimitry Andric // execution. 458bcb0991SDimitry Andric // A handful of BB with higher block frequencies are taken, then path to entry 468bcb0991SDimitry Andric // and end BB are discovered by traversing up & down the CFG. 478bcb0991SDimitry Andric class SequenceBBQuery : public SpeculateQuery { 488bcb0991SDimitry Andric struct WalkDirection { 498bcb0991SDimitry Andric bool Upward = true, Downward = true; 508bcb0991SDimitry Andric // the block associated contain a call 518bcb0991SDimitry Andric bool CallerBlock = false; 528bcb0991SDimitry Andric }; 538bcb0991SDimitry Andric 548bcb0991SDimitry Andric public: 558bcb0991SDimitry Andric using VisitedBlocksInfoTy = DenseMap<const BasicBlock *, WalkDirection>; 568bcb0991SDimitry Andric using BlockListTy = SmallVector<const BasicBlock *, 8>; 578bcb0991SDimitry Andric using BackEdgesInfoTy = 588bcb0991SDimitry Andric SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 8>; 598bcb0991SDimitry Andric using BlockFreqInfoTy = 608bcb0991SDimitry Andric SmallVector<std::pair<const BasicBlock *, uint64_t>, 8>; 618bcb0991SDimitry Andric 628bcb0991SDimitry Andric private: 638bcb0991SDimitry Andric std::size_t getHottestBlocks(std::size_t TotalBlocks); 648bcb0991SDimitry Andric BlockListTy rearrangeBB(const Function &, const BlockListTy &); 658bcb0991SDimitry Andric BlockListTy queryCFG(Function &, const BlockListTy &); 668bcb0991SDimitry Andric void traverseToEntryBlock(const BasicBlock *, const BlockListTy &, 678bcb0991SDimitry Andric const BackEdgesInfoTy &, 688bcb0991SDimitry Andric const BranchProbabilityInfo *, 698bcb0991SDimitry Andric VisitedBlocksInfoTy &); 708bcb0991SDimitry Andric void traverseToExitBlock(const BasicBlock *, const BlockListTy &, 718bcb0991SDimitry Andric const BackEdgesInfoTy &, 728bcb0991SDimitry Andric const BranchProbabilityInfo *, 738bcb0991SDimitry Andric VisitedBlocksInfoTy &); 748bcb0991SDimitry Andric 758bcb0991SDimitry Andric public: 768bcb0991SDimitry Andric ResultTy operator()(Function &F); 778bcb0991SDimitry Andric }; 788bcb0991SDimitry Andric 798bcb0991SDimitry Andric } // namespace orc 808bcb0991SDimitry Andric } // namespace llvm 818bcb0991SDimitry Andric 828bcb0991SDimitry Andric #endif // LLVM_EXECUTIONENGINE_ORC_SPECULATEANALYSES_H 83