10b57cec5SDimitry Andric //===- Transforms/Instrumentation/PGOInstrumentation.h ----------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric /// \file 100b57cec5SDimitry Andric /// This file provides the interface for IR based instrumentation passes ( 110b57cec5SDimitry Andric /// (profile-gen, and profile-use). 120b57cec5SDimitry Andric // 130b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 140b57cec5SDimitry Andric 15fe6060f1SDimitry Andric #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H 16fe6060f1SDimitry Andric #define LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 1906c3fb27SDimitry Andric #include "llvm/ADT/IntrusiveRefCntPtr.h" 200b57cec5SDimitry Andric #include "llvm/IR/PassManager.h" 215f757f3fSDimitry Andric #include "llvm/Support/CommandLine.h" 220b57cec5SDimitry Andric #include <cstdint> 230b57cec5SDimitry Andric #include <string> 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric namespace llvm { 260b57cec5SDimitry Andric 275f757f3fSDimitry Andric extern cl::opt<bool> DebugInfoCorrelate; 285f757f3fSDimitry Andric 290b57cec5SDimitry Andric class Function; 300b57cec5SDimitry Andric class Instruction; 310b57cec5SDimitry Andric class Module; 320b57cec5SDimitry Andric 3306c3fb27SDimitry Andric namespace vfs { 3406c3fb27SDimitry Andric class FileSystem; 3506c3fb27SDimitry Andric } // namespace vfs 3606c3fb27SDimitry Andric 370b57cec5SDimitry Andric /// The instrumentation (profile-instr-gen) pass for IR based PGO. 380b57cec5SDimitry Andric // We use this pass to create COMDAT profile variables for context 390b57cec5SDimitry Andric // sensitive PGO (CSPGO). The reason to have a pass for this is CSPGO 400b57cec5SDimitry Andric // can be run after LTO/ThinLTO linking. Lld linker needs to see 410b57cec5SDimitry Andric // all the COMDAT variables before linking. So we have this pass 420b57cec5SDimitry Andric // always run before linking for CSPGO. 430b57cec5SDimitry Andric class PGOInstrumentationGenCreateVar 440b57cec5SDimitry Andric : public PassInfoMixin<PGOInstrumentationGenCreateVar> { 450b57cec5SDimitry Andric public: 46*0fca6ea1SDimitry Andric PGOInstrumentationGenCreateVar(std::string CSInstrName = "", 47*0fca6ea1SDimitry Andric bool Sampling = false) 48*0fca6ea1SDimitry Andric : CSInstrName(CSInstrName), ProfileSampling(Sampling) {} 4906c3fb27SDimitry Andric PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric private: 520b57cec5SDimitry Andric std::string CSInstrName; 53*0fca6ea1SDimitry Andric bool ProfileSampling; 540b57cec5SDimitry Andric }; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric /// The instrumentation (profile-instr-gen) pass for IR based PGO. 570b57cec5SDimitry Andric class PGOInstrumentationGen : public PassInfoMixin<PGOInstrumentationGen> { 580b57cec5SDimitry Andric public: 590b57cec5SDimitry Andric PGOInstrumentationGen(bool IsCS = false) : IsCS(IsCS) {} 6006c3fb27SDimitry Andric PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric private: 630b57cec5SDimitry Andric // If this is a context sensitive instrumentation. 640b57cec5SDimitry Andric bool IsCS; 650b57cec5SDimitry Andric }; 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric /// The profile annotation (profile-instr-use) pass for IR based PGO. 680b57cec5SDimitry Andric class PGOInstrumentationUse : public PassInfoMixin<PGOInstrumentationUse> { 690b57cec5SDimitry Andric public: 700b57cec5SDimitry Andric PGOInstrumentationUse(std::string Filename = "", 7106c3fb27SDimitry Andric std::string RemappingFilename = "", bool IsCS = false, 7206c3fb27SDimitry Andric IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr); 730b57cec5SDimitry Andric 7406c3fb27SDimitry Andric PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric private: 770b57cec5SDimitry Andric std::string ProfileFileName; 780b57cec5SDimitry Andric std::string ProfileRemappingFileName; 790b57cec5SDimitry Andric // If this is a context sensitive instrumentation. 800b57cec5SDimitry Andric bool IsCS; 8106c3fb27SDimitry Andric IntrusiveRefCntPtr<vfs::FileSystem> FS; 820b57cec5SDimitry Andric }; 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric /// The indirect function call promotion pass. 850b57cec5SDimitry Andric class PGOIndirectCallPromotion : public PassInfoMixin<PGOIndirectCallPromotion> { 860b57cec5SDimitry Andric public: 870b57cec5SDimitry Andric PGOIndirectCallPromotion(bool IsInLTO = false, bool SamplePGO = false) 880b57cec5SDimitry Andric : InLTO(IsInLTO), SamplePGO(SamplePGO) {} 890b57cec5SDimitry Andric 9006c3fb27SDimitry Andric PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric private: 930b57cec5SDimitry Andric bool InLTO; 940b57cec5SDimitry Andric bool SamplePGO; 950b57cec5SDimitry Andric }; 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric /// The profile size based optimization pass for memory intrinsics. 980b57cec5SDimitry Andric class PGOMemOPSizeOpt : public PassInfoMixin<PGOMemOPSizeOpt> { 990b57cec5SDimitry Andric public: 1000b57cec5SDimitry Andric PGOMemOPSizeOpt() = default; 1010b57cec5SDimitry Andric 10206c3fb27SDimitry Andric PreservedAnalyses run(Function &F, FunctionAnalysisManager &MAM); 1030b57cec5SDimitry Andric }; 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric void setProfMetadata(Module *M, Instruction *TI, ArrayRef<uint64_t> EdgeCounts, 1060b57cec5SDimitry Andric uint64_t MaxCount); 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric void setIrrLoopHeaderMetadata(Module *M, Instruction *TI, uint64_t Count); 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric } // end namespace llvm 1110b57cec5SDimitry Andric 112fe6060f1SDimitry Andric #endif // LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H 113