xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- InstrProfiling.cpp - Frontend instrumentation based profiling -----===//
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 //
95f757f3fSDimitry Andric // This pass lowers instrprof_* intrinsics emitted by an instrumentor.
100b57cec5SDimitry Andric // It also builds the data structures and initialization code needed for
110b57cec5SDimitry Andric // updating execution counts and emitting the profile at runtime.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "llvm/Transforms/Instrumentation/InstrProfiling.h"
160b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
175f757f3fSDimitry Andric #include "llvm/ADT/STLExtras.h"
180b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
190b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
200b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
210b57cec5SDimitry Andric #include "llvm/Analysis/BlockFrequencyInfo.h"
220b57cec5SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h"
230b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
240b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
250b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
260b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
275f757f3fSDimitry Andric #include "llvm/IR/CFG.h"
280b57cec5SDimitry Andric #include "llvm/IR/Constant.h"
290b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
300eae32dcSDimitry Andric #include "llvm/IR/DIBuilder.h"
310b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
320eae32dcSDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
330b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
340b57cec5SDimitry Andric #include "llvm/IR/Function.h"
350b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
360b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
370b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
380b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
390b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
400b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
41*0fca6ea1SDimitry Andric #include "llvm/IR/MDBuilder.h"
420b57cec5SDimitry Andric #include "llvm/IR/Module.h"
430b57cec5SDimitry Andric #include "llvm/IR/Type.h"
44480093f4SDimitry Andric #include "llvm/InitializePasses.h"
450b57cec5SDimitry Andric #include "llvm/Pass.h"
460b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProf.h"
470eae32dcSDimitry Andric #include "llvm/ProfileData/InstrProfCorrelator.h"
480b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
490b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
500b57cec5SDimitry Andric #include "llvm/Support/Error.h"
510b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
5206c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
535f757f3fSDimitry Andric #include "llvm/Transforms/Instrumentation.h"
545f757f3fSDimitry Andric #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
555f757f3fSDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
560b57cec5SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
570b57cec5SDimitry Andric #include "llvm/Transforms/Utils/SSAUpdater.h"
580b57cec5SDimitry Andric #include <algorithm>
590b57cec5SDimitry Andric #include <cassert>
600b57cec5SDimitry Andric #include <cstdint>
610b57cec5SDimitry Andric #include <string>
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric using namespace llvm;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric #define DEBUG_TYPE "instrprof"
660b57cec5SDimitry Andric 
670eae32dcSDimitry Andric namespace llvm {
68*0fca6ea1SDimitry Andric // Command line option to enable vtable value profiling. Defined in
69*0fca6ea1SDimitry Andric // ProfileData/InstrProf.cpp: -enable-vtable-value-profiling=
70*0fca6ea1SDimitry Andric extern cl::opt<bool> EnableVTableValueProfiling;
715f757f3fSDimitry Andric // TODO: Remove -debug-info-correlate in next LLVM release, in favor of
725f757f3fSDimitry Andric // -profile-correlate=debug-info.
735f757f3fSDimitry Andric cl::opt<bool> DebugInfoCorrelate(
745f757f3fSDimitry Andric     "debug-info-correlate",
755f757f3fSDimitry Andric     cl::desc("Use debug info to correlate profiles. (Deprecated, use "
765f757f3fSDimitry Andric              "-profile-correlate=debug-info)"),
770eae32dcSDimitry Andric     cl::init(false));
785f757f3fSDimitry Andric 
795f757f3fSDimitry Andric cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate(
805f757f3fSDimitry Andric     "profile-correlate",
815f757f3fSDimitry Andric     cl::desc("Use debug info or binary file to correlate profiles."),
825f757f3fSDimitry Andric     cl::init(InstrProfCorrelator::NONE),
835f757f3fSDimitry Andric     cl::values(clEnumValN(InstrProfCorrelator::NONE, "",
845f757f3fSDimitry Andric                           "No profile correlation"),
855f757f3fSDimitry Andric                clEnumValN(InstrProfCorrelator::DEBUG_INFO, "debug-info",
865f757f3fSDimitry Andric                           "Use debug info to correlate"),
875f757f3fSDimitry Andric                clEnumValN(InstrProfCorrelator::BINARY, "binary",
885f757f3fSDimitry Andric                           "Use binary to correlate")));
890eae32dcSDimitry Andric } // namespace llvm
900eae32dcSDimitry Andric 
910b57cec5SDimitry Andric namespace {
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric cl::opt<bool> DoHashBasedCounterSplit(
940b57cec5SDimitry Andric     "hash-based-counter-split",
950b57cec5SDimitry Andric     cl::desc("Rename counter variable of a comdat function based on cfg hash"),
960b57cec5SDimitry Andric     cl::init(true));
970b57cec5SDimitry Andric 
984824e7fdSDimitry Andric cl::opt<bool>
994824e7fdSDimitry Andric     RuntimeCounterRelocation("runtime-counter-relocation",
1005ffd83dbSDimitry Andric                              cl::desc("Enable relocating counters at runtime."),
1015ffd83dbSDimitry Andric                              cl::init(false));
1025ffd83dbSDimitry Andric 
1030b57cec5SDimitry Andric cl::opt<bool> ValueProfileStaticAlloc(
1040b57cec5SDimitry Andric     "vp-static-alloc",
1050b57cec5SDimitry Andric     cl::desc("Do static counter allocation for value profiler"),
1060b57cec5SDimitry Andric     cl::init(true));
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric cl::opt<double> NumCountersPerValueSite(
1090b57cec5SDimitry Andric     "vp-counters-per-site",
1100b57cec5SDimitry Andric     cl::desc("The average number of profile counters allocated "
1110b57cec5SDimitry Andric              "per value profiling site."),
1120b57cec5SDimitry Andric     // This is set to a very small value because in real programs, only
1130b57cec5SDimitry Andric     // a very small percentage of value sites have non-zero targets, e.g, 1/30.
1140b57cec5SDimitry Andric     // For those sites with non-zero profile, the average number of targets
1150b57cec5SDimitry Andric     // is usually smaller than 2.
1160b57cec5SDimitry Andric     cl::init(1.0));
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric cl::opt<bool> AtomicCounterUpdateAll(
11981ad6265SDimitry Andric     "instrprof-atomic-counter-update-all",
1200b57cec5SDimitry Andric     cl::desc("Make all profile counter updates atomic (for testing only)"),
1210b57cec5SDimitry Andric     cl::init(false));
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric cl::opt<bool> AtomicCounterUpdatePromoted(
12481ad6265SDimitry Andric     "atomic-counter-update-promoted",
1250b57cec5SDimitry Andric     cl::desc("Do counter update using atomic fetch add "
1260b57cec5SDimitry Andric              " for promoted counters only"),
1270b57cec5SDimitry Andric     cl::init(false));
1280b57cec5SDimitry Andric 
1295ffd83dbSDimitry Andric cl::opt<bool> AtomicFirstCounter(
13081ad6265SDimitry Andric     "atomic-first-counter",
1315ffd83dbSDimitry Andric     cl::desc("Use atomic fetch add for first counter in a function (usually "
1325ffd83dbSDimitry Andric              "the entry counter)"),
1335ffd83dbSDimitry Andric     cl::init(false));
1345ffd83dbSDimitry Andric 
1350b57cec5SDimitry Andric // If the option is not specified, the default behavior about whether
1360b57cec5SDimitry Andric // counter promotion is done depends on how instrumentaiton lowering
1370b57cec5SDimitry Andric // pipeline is setup, i.e., the default value of true of this option
1380b57cec5SDimitry Andric // does not mean the promotion will be done by default. Explicitly
1390b57cec5SDimitry Andric // setting this option can override the default behavior.
14081ad6265SDimitry Andric cl::opt<bool> DoCounterPromotion("do-counter-promotion",
1410b57cec5SDimitry Andric                                  cl::desc("Do counter register promotion"),
1420b57cec5SDimitry Andric                                  cl::init(false));
1430b57cec5SDimitry Andric cl::opt<unsigned> MaxNumOfPromotionsPerLoop(
14481ad6265SDimitry Andric     "max-counter-promotions-per-loop", cl::init(20),
1450b57cec5SDimitry Andric     cl::desc("Max number counter promotions per loop to avoid"
1460b57cec5SDimitry Andric              " increasing register pressure too much"));
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric // A debug option
1490b57cec5SDimitry Andric cl::opt<int>
15081ad6265SDimitry Andric     MaxNumOfPromotions("max-counter-promotions", cl::init(-1),
1510b57cec5SDimitry Andric                        cl::desc("Max number of allowed counter promotions"));
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric cl::opt<unsigned> SpeculativeCounterPromotionMaxExiting(
15481ad6265SDimitry Andric     "speculative-counter-promotion-max-exiting", cl::init(3),
1550b57cec5SDimitry Andric     cl::desc("The max number of exiting blocks of a loop to allow "
1560b57cec5SDimitry Andric              " speculative counter promotion"));
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric cl::opt<bool> SpeculativeCounterPromotionToLoop(
15981ad6265SDimitry Andric     "speculative-counter-promotion-to-loop",
1600b57cec5SDimitry Andric     cl::desc("When the option is false, if the target block is in a loop, "
1610b57cec5SDimitry Andric              "the promotion will be disallowed unless the promoted counter "
1620b57cec5SDimitry Andric              " update can be further/iteratively promoted into an acyclic "
1630b57cec5SDimitry Andric              " region."));
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric cl::opt<bool> IterativeCounterPromotion(
16681ad6265SDimitry Andric     "iterative-counter-promotion", cl::init(true),
1670b57cec5SDimitry Andric     cl::desc("Allow counter promotion across the whole loop nest."));
1680b57cec5SDimitry Andric 
169e8d8bef9SDimitry Andric cl::opt<bool> SkipRetExitBlock(
17081ad6265SDimitry Andric     "skip-ret-exit-block", cl::init(true),
171e8d8bef9SDimitry Andric     cl::desc("Suppress counter promotion if exit blocks contain ret."));
172e8d8bef9SDimitry Andric 
173*0fca6ea1SDimitry Andric static cl::opt<bool> SampledInstr("sampled-instrumentation", cl::ZeroOrMore,
174*0fca6ea1SDimitry Andric                                   cl::init(false),
175*0fca6ea1SDimitry Andric                                   cl::desc("Do PGO instrumentation sampling"));
176*0fca6ea1SDimitry Andric 
177*0fca6ea1SDimitry Andric static cl::opt<unsigned> SampledInstrPeriod(
178*0fca6ea1SDimitry Andric     "sampled-instr-period",
179*0fca6ea1SDimitry Andric     cl::desc("Set the profile instrumentation sample period. For each sample "
180*0fca6ea1SDimitry Andric              "period, a fixed number of consecutive samples will be recorded. "
181*0fca6ea1SDimitry Andric              "The number is controlled by 'sampled-instr-burst-duration' flag. "
182*0fca6ea1SDimitry Andric              "The default sample period of 65535 is optimized for generating "
183*0fca6ea1SDimitry Andric              "efficient code that leverages unsigned integer wrapping in "
184*0fca6ea1SDimitry Andric              "overflow."),
185*0fca6ea1SDimitry Andric     cl::init(65535));
186*0fca6ea1SDimitry Andric 
187*0fca6ea1SDimitry Andric static cl::opt<unsigned> SampledInstrBurstDuration(
188*0fca6ea1SDimitry Andric     "sampled-instr-burst-duration",
189*0fca6ea1SDimitry Andric     cl::desc("Set the profile instrumentation burst duration, which can range "
190*0fca6ea1SDimitry Andric              "from 0 to one less than the value of 'sampled-instr-period'. "
191*0fca6ea1SDimitry Andric              "This number of samples will be recorded for each "
192*0fca6ea1SDimitry Andric              "'sampled-instr-period' count update. Setting to 1 enables "
193*0fca6ea1SDimitry Andric              "simple sampling, in which case it is recommended to set "
194*0fca6ea1SDimitry Andric              "'sampled-instr-period' to a prime number."),
195*0fca6ea1SDimitry Andric     cl::init(200));
196*0fca6ea1SDimitry Andric 
1975f757f3fSDimitry Andric using LoadStorePair = std::pair<Instruction *, Instruction *>;
1985f757f3fSDimitry Andric 
getIntModuleFlagOrZero(const Module & M,StringRef Flag)199*0fca6ea1SDimitry Andric static uint64_t getIntModuleFlagOrZero(const Module &M, StringRef Flag) {
200*0fca6ea1SDimitry Andric   auto *MD = dyn_cast_or_null<ConstantAsMetadata>(M.getModuleFlag(Flag));
201*0fca6ea1SDimitry Andric   if (!MD)
202*0fca6ea1SDimitry Andric     return 0;
203*0fca6ea1SDimitry Andric 
204*0fca6ea1SDimitry Andric   // If the flag is a ConstantAsMetadata, it should be an integer representable
205*0fca6ea1SDimitry Andric   // in 64-bits.
206*0fca6ea1SDimitry Andric   return cast<ConstantInt>(MD->getValue())->getZExtValue();
207*0fca6ea1SDimitry Andric }
208*0fca6ea1SDimitry Andric 
enablesValueProfiling(const Module & M)209*0fca6ea1SDimitry Andric static bool enablesValueProfiling(const Module &M) {
210*0fca6ea1SDimitry Andric   return isIRPGOFlagSet(&M) ||
211*0fca6ea1SDimitry Andric          getIntModuleFlagOrZero(M, "EnableValueProfiling") != 0;
212*0fca6ea1SDimitry Andric }
213*0fca6ea1SDimitry Andric 
214*0fca6ea1SDimitry Andric // Conservatively returns true if value profiling is enabled.
profDataReferencedByCode(const Module & M)215*0fca6ea1SDimitry Andric static bool profDataReferencedByCode(const Module &M) {
216*0fca6ea1SDimitry Andric   return enablesValueProfiling(M);
217*0fca6ea1SDimitry Andric }
218*0fca6ea1SDimitry Andric 
2195f757f3fSDimitry Andric class InstrLowerer final {
2205f757f3fSDimitry Andric public:
InstrLowerer(Module & M,const InstrProfOptions & Options,std::function<const TargetLibraryInfo & (Function & F)> GetTLI,bool IsCS)2215f757f3fSDimitry Andric   InstrLowerer(Module &M, const InstrProfOptions &Options,
2225f757f3fSDimitry Andric                std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
2235f757f3fSDimitry Andric                bool IsCS)
2245f757f3fSDimitry Andric       : M(M), Options(Options), TT(Triple(M.getTargetTriple())), IsCS(IsCS),
225*0fca6ea1SDimitry Andric         GetTLI(GetTLI), DataReferencedByCode(profDataReferencedByCode(M)) {}
2265f757f3fSDimitry Andric 
2275f757f3fSDimitry Andric   bool lower();
2285f757f3fSDimitry Andric 
2295f757f3fSDimitry Andric private:
2305f757f3fSDimitry Andric   Module &M;
2315f757f3fSDimitry Andric   const InstrProfOptions Options;
2325f757f3fSDimitry Andric   const Triple TT;
2335f757f3fSDimitry Andric   // Is this lowering for the context-sensitive instrumentation.
2345f757f3fSDimitry Andric   const bool IsCS;
2355f757f3fSDimitry Andric 
2365f757f3fSDimitry Andric   std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
237*0fca6ea1SDimitry Andric 
238*0fca6ea1SDimitry Andric   const bool DataReferencedByCode;
239*0fca6ea1SDimitry Andric 
2405f757f3fSDimitry Andric   struct PerFunctionProfileData {
2415f757f3fSDimitry Andric     uint32_t NumValueSites[IPVK_Last + 1] = {};
2425f757f3fSDimitry Andric     GlobalVariable *RegionCounters = nullptr;
2435f757f3fSDimitry Andric     GlobalVariable *DataVar = nullptr;
2445f757f3fSDimitry Andric     GlobalVariable *RegionBitmaps = nullptr;
2455f757f3fSDimitry Andric     uint32_t NumBitmapBytes = 0;
2465f757f3fSDimitry Andric 
2475f757f3fSDimitry Andric     PerFunctionProfileData() = default;
2485f757f3fSDimitry Andric   };
2495f757f3fSDimitry Andric   DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
250*0fca6ea1SDimitry Andric   // Key is virtual table variable, value is 'VTableProfData' in the form of
251*0fca6ea1SDimitry Andric   // GlobalVariable.
252*0fca6ea1SDimitry Andric   DenseMap<GlobalVariable *, GlobalVariable *> VTableDataMap;
2535f757f3fSDimitry Andric   /// If runtime relocation is enabled, this maps functions to the load
2545f757f3fSDimitry Andric   /// instruction that produces the profile relocation bias.
2555f757f3fSDimitry Andric   DenseMap<const Function *, LoadInst *> FunctionToProfileBiasMap;
2565f757f3fSDimitry Andric   std::vector<GlobalValue *> CompilerUsedVars;
2575f757f3fSDimitry Andric   std::vector<GlobalValue *> UsedVars;
2585f757f3fSDimitry Andric   std::vector<GlobalVariable *> ReferencedNames;
259*0fca6ea1SDimitry Andric   // The list of virtual table variables of which the VTableProfData is
260*0fca6ea1SDimitry Andric   // collected.
261*0fca6ea1SDimitry Andric   std::vector<GlobalVariable *> ReferencedVTables;
2625f757f3fSDimitry Andric   GlobalVariable *NamesVar = nullptr;
2635f757f3fSDimitry Andric   size_t NamesSize = 0;
2645f757f3fSDimitry Andric 
265*0fca6ea1SDimitry Andric   /// The instance of [[alwaysinline]] rmw_or(ptr, i8).
266*0fca6ea1SDimitry Andric   /// This is name-insensitive.
267*0fca6ea1SDimitry Andric   Function *RMWOrFunc = nullptr;
268*0fca6ea1SDimitry Andric 
2695f757f3fSDimitry Andric   // vector of counter load/store pairs to be register promoted.
2705f757f3fSDimitry Andric   std::vector<LoadStorePair> PromotionCandidates;
2715f757f3fSDimitry Andric 
2725f757f3fSDimitry Andric   int64_t TotalCountersPromoted = 0;
2735f757f3fSDimitry Andric 
2745f757f3fSDimitry Andric   /// Lower instrumentation intrinsics in the function. Returns true if there
2755f757f3fSDimitry Andric   /// any lowering.
2765f757f3fSDimitry Andric   bool lowerIntrinsics(Function *F);
2775f757f3fSDimitry Andric 
2785f757f3fSDimitry Andric   /// Register-promote counter loads and stores in loops.
2795f757f3fSDimitry Andric   void promoteCounterLoadStores(Function *F);
2805f757f3fSDimitry Andric 
2815f757f3fSDimitry Andric   /// Returns true if relocating counters at runtime is enabled.
2825f757f3fSDimitry Andric   bool isRuntimeCounterRelocationEnabled() const;
2835f757f3fSDimitry Andric 
2845f757f3fSDimitry Andric   /// Returns true if profile counter update register promotion is enabled.
2855f757f3fSDimitry Andric   bool isCounterPromotionEnabled() const;
2865f757f3fSDimitry Andric 
287*0fca6ea1SDimitry Andric   /// Return true if profile sampling is enabled.
288*0fca6ea1SDimitry Andric   bool isSamplingEnabled() const;
289*0fca6ea1SDimitry Andric 
2905f757f3fSDimitry Andric   /// Count the number of instrumented value sites for the function.
2915f757f3fSDimitry Andric   void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
2925f757f3fSDimitry Andric 
2935f757f3fSDimitry Andric   /// Replace instrprof.value.profile with a call to runtime library.
2945f757f3fSDimitry Andric   void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
2955f757f3fSDimitry Andric 
2965f757f3fSDimitry Andric   /// Replace instrprof.cover with a store instruction to the coverage byte.
2975f757f3fSDimitry Andric   void lowerCover(InstrProfCoverInst *Inc);
2985f757f3fSDimitry Andric 
2995f757f3fSDimitry Andric   /// Replace instrprof.timestamp with a call to
3005f757f3fSDimitry Andric   /// INSTR_PROF_PROFILE_SET_TIMESTAMP.
3015f757f3fSDimitry Andric   void lowerTimestamp(InstrProfTimestampInst *TimestampInstruction);
3025f757f3fSDimitry Andric 
3035f757f3fSDimitry Andric   /// Replace instrprof.increment with an increment of the appropriate value.
3045f757f3fSDimitry Andric   void lowerIncrement(InstrProfIncrementInst *Inc);
3055f757f3fSDimitry Andric 
3065f757f3fSDimitry Andric   /// Force emitting of name vars for unused functions.
3075f757f3fSDimitry Andric   void lowerCoverageData(GlobalVariable *CoverageNamesVar);
3085f757f3fSDimitry Andric 
3095f757f3fSDimitry Andric   /// Replace instrprof.mcdc.tvbitmask.update with a shift and or instruction
3105f757f3fSDimitry Andric   /// using the index represented by the a temp value into a bitmap.
3115f757f3fSDimitry Andric   void lowerMCDCTestVectorBitmapUpdate(InstrProfMCDCTVBitmapUpdate *Ins);
3125f757f3fSDimitry Andric 
313*0fca6ea1SDimitry Andric   /// Get the Bias value for data to access mmap-ed area.
314*0fca6ea1SDimitry Andric   /// Create it if it hasn't been seen.
315*0fca6ea1SDimitry Andric   GlobalVariable *getOrCreateBiasVar(StringRef VarName);
3165f757f3fSDimitry Andric 
3175f757f3fSDimitry Andric   /// Compute the address of the counter value that this profiling instruction
3185f757f3fSDimitry Andric   /// acts on.
3195f757f3fSDimitry Andric   Value *getCounterAddress(InstrProfCntrInstBase *I);
3205f757f3fSDimitry Andric 
321*0fca6ea1SDimitry Andric   /// Lower the incremental instructions under profile sampling predicates.
322*0fca6ea1SDimitry Andric   void doSampling(Instruction *I);
323*0fca6ea1SDimitry Andric 
3245f757f3fSDimitry Andric   /// Get the region counters for an increment, creating them if necessary.
3255f757f3fSDimitry Andric   ///
3265f757f3fSDimitry Andric   /// If the counter array doesn't yet exist, the profile data variables
3275f757f3fSDimitry Andric   /// referring to them will also be created.
3285f757f3fSDimitry Andric   GlobalVariable *getOrCreateRegionCounters(InstrProfCntrInstBase *Inc);
3295f757f3fSDimitry Andric 
3305f757f3fSDimitry Andric   /// Create the region counters.
3315f757f3fSDimitry Andric   GlobalVariable *createRegionCounters(InstrProfCntrInstBase *Inc,
3325f757f3fSDimitry Andric                                        StringRef Name,
3335f757f3fSDimitry Andric                                        GlobalValue::LinkageTypes Linkage);
3345f757f3fSDimitry Andric 
335*0fca6ea1SDimitry Andric   /// Create [[alwaysinline]] rmw_or(ptr, i8).
336*0fca6ea1SDimitry Andric   /// This doesn't update `RMWOrFunc`.
337*0fca6ea1SDimitry Andric   Function *createRMWOrFunc();
338*0fca6ea1SDimitry Andric 
339*0fca6ea1SDimitry Andric   /// Get the call to `rmw_or`.
340*0fca6ea1SDimitry Andric   /// Create the instance if it is unknown.
341*0fca6ea1SDimitry Andric   CallInst *getRMWOrCall(Value *Addr, Value *Val);
342*0fca6ea1SDimitry Andric 
3435f757f3fSDimitry Andric   /// Compute the address of the test vector bitmap that this profiling
3445f757f3fSDimitry Andric   /// instruction acts on.
3455f757f3fSDimitry Andric   Value *getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I);
3465f757f3fSDimitry Andric 
3475f757f3fSDimitry Andric   /// Get the region bitmaps for an increment, creating them if necessary.
3485f757f3fSDimitry Andric   ///
3495f757f3fSDimitry Andric   /// If the bitmap array doesn't yet exist, the profile data variables
3505f757f3fSDimitry Andric   /// referring to them will also be created.
3515f757f3fSDimitry Andric   GlobalVariable *getOrCreateRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc);
3525f757f3fSDimitry Andric 
3535f757f3fSDimitry Andric   /// Create the MC/DC bitmap as a byte-aligned array of bytes associated with
3545f757f3fSDimitry Andric   /// an MC/DC Decision region. The number of bytes required is indicated by
3555f757f3fSDimitry Andric   /// the intrinsic used (type InstrProfMCDCBitmapInstBase).  This is called
3565f757f3fSDimitry Andric   /// as part of setupProfileSection() and is conceptually very similar to
3575f757f3fSDimitry Andric   /// what is done for profile data counters in createRegionCounters().
3585f757f3fSDimitry Andric   GlobalVariable *createRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc,
3595f757f3fSDimitry Andric                                       StringRef Name,
3605f757f3fSDimitry Andric                                       GlobalValue::LinkageTypes Linkage);
3615f757f3fSDimitry Andric 
3625f757f3fSDimitry Andric   /// Set Comdat property of GV, if required.
363*0fca6ea1SDimitry Andric   void maybeSetComdat(GlobalVariable *GV, GlobalObject *GO, StringRef VarName);
3645f757f3fSDimitry Andric 
3655f757f3fSDimitry Andric   /// Setup the sections into which counters and bitmaps are allocated.
3665f757f3fSDimitry Andric   GlobalVariable *setupProfileSection(InstrProfInstBase *Inc,
3675f757f3fSDimitry Andric                                       InstrProfSectKind IPSK);
3685f757f3fSDimitry Andric 
3695f757f3fSDimitry Andric   /// Create INSTR_PROF_DATA variable for counters and bitmaps.
3705f757f3fSDimitry Andric   void createDataVariable(InstrProfCntrInstBase *Inc);
3715f757f3fSDimitry Andric 
372*0fca6ea1SDimitry Andric   /// Get the counters for virtual table values, creating them if necessary.
373*0fca6ea1SDimitry Andric   void getOrCreateVTableProfData(GlobalVariable *GV);
374*0fca6ea1SDimitry Andric 
3755f757f3fSDimitry Andric   /// Emit the section with compressed function names.
3765f757f3fSDimitry Andric   void emitNameData();
3775f757f3fSDimitry Andric 
378*0fca6ea1SDimitry Andric   /// Emit the section with compressed vtable names.
379*0fca6ea1SDimitry Andric   void emitVTableNames();
380*0fca6ea1SDimitry Andric 
3815f757f3fSDimitry Andric   /// Emit value nodes section for value profiling.
3825f757f3fSDimitry Andric   void emitVNodes();
3835f757f3fSDimitry Andric 
3845f757f3fSDimitry Andric   /// Emit runtime registration functions for each profile data variable.
3855f757f3fSDimitry Andric   void emitRegistration();
3865f757f3fSDimitry Andric 
3875f757f3fSDimitry Andric   /// Emit the necessary plumbing to pull in the runtime initialization.
3885f757f3fSDimitry Andric   /// Returns true if a change was made.
3895f757f3fSDimitry Andric   bool emitRuntimeHook();
3905f757f3fSDimitry Andric 
3915f757f3fSDimitry Andric   /// Add uses of our data variables and runtime hook.
3925f757f3fSDimitry Andric   void emitUses();
3935f757f3fSDimitry Andric 
3945f757f3fSDimitry Andric   /// Create a static initializer for our data, on platforms that need it,
3955f757f3fSDimitry Andric   /// and for any profile output file that was specified.
3965f757f3fSDimitry Andric   void emitInitialization();
3975f757f3fSDimitry Andric };
3985f757f3fSDimitry Andric 
3990b57cec5SDimitry Andric ///
4000b57cec5SDimitry Andric /// A helper class to promote one counter RMW operation in the loop
4010b57cec5SDimitry Andric /// into register update.
4020b57cec5SDimitry Andric ///
4030b57cec5SDimitry Andric /// RWM update for the counter will be sinked out of the loop after
4040b57cec5SDimitry Andric /// the transformation.
4050b57cec5SDimitry Andric ///
4060b57cec5SDimitry Andric class PGOCounterPromoterHelper : public LoadAndStorePromoter {
4070b57cec5SDimitry Andric public:
PGOCounterPromoterHelper(Instruction * L,Instruction * S,SSAUpdater & SSA,Value * Init,BasicBlock * PH,ArrayRef<BasicBlock * > ExitBlocks,ArrayRef<Instruction * > InsertPts,DenseMap<Loop *,SmallVector<LoadStorePair,8>> & LoopToCands,LoopInfo & LI)4080b57cec5SDimitry Andric   PGOCounterPromoterHelper(
4090b57cec5SDimitry Andric       Instruction *L, Instruction *S, SSAUpdater &SSA, Value *Init,
4100b57cec5SDimitry Andric       BasicBlock *PH, ArrayRef<BasicBlock *> ExitBlocks,
4110b57cec5SDimitry Andric       ArrayRef<Instruction *> InsertPts,
4120b57cec5SDimitry Andric       DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands,
4130b57cec5SDimitry Andric       LoopInfo &LI)
4140b57cec5SDimitry Andric       : LoadAndStorePromoter({L, S}, SSA), Store(S), ExitBlocks(ExitBlocks),
4150b57cec5SDimitry Andric         InsertPts(InsertPts), LoopToCandidates(LoopToCands), LI(LI) {
4160b57cec5SDimitry Andric     assert(isa<LoadInst>(L));
4170b57cec5SDimitry Andric     assert(isa<StoreInst>(S));
4180b57cec5SDimitry Andric     SSA.AddAvailableValue(PH, Init);
4190b57cec5SDimitry Andric   }
4200b57cec5SDimitry Andric 
doExtraRewritesBeforeFinalDeletion()4210b57cec5SDimitry Andric   void doExtraRewritesBeforeFinalDeletion() override {
4220b57cec5SDimitry Andric     for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
4230b57cec5SDimitry Andric       BasicBlock *ExitBlock = ExitBlocks[i];
4240b57cec5SDimitry Andric       Instruction *InsertPos = InsertPts[i];
4250b57cec5SDimitry Andric       // Get LiveIn value into the ExitBlock. If there are multiple
4260b57cec5SDimitry Andric       // predecessors, the value is defined by a PHI node in this
4270b57cec5SDimitry Andric       // block.
4280b57cec5SDimitry Andric       Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
4290b57cec5SDimitry Andric       Value *Addr = cast<StoreInst>(Store)->getPointerOperand();
4300b57cec5SDimitry Andric       Type *Ty = LiveInValue->getType();
4310b57cec5SDimitry Andric       IRBuilder<> Builder(InsertPos);
43281ad6265SDimitry Andric       if (auto *AddrInst = dyn_cast_or_null<IntToPtrInst>(Addr)) {
43381ad6265SDimitry Andric         // If isRuntimeCounterRelocationEnabled() is true then the address of
43481ad6265SDimitry Andric         // the store instruction is computed with two instructions in
43581ad6265SDimitry Andric         // InstrProfiling::getCounterAddress(). We need to copy those
43681ad6265SDimitry Andric         // instructions to this block to compute Addr correctly.
43781ad6265SDimitry Andric         // %BiasAdd = add i64 ptrtoint <__profc_>, <__llvm_profile_counter_bias>
43881ad6265SDimitry Andric         // %Addr = inttoptr i64 %BiasAdd to i64*
43981ad6265SDimitry Andric         auto *OrigBiasInst = dyn_cast<BinaryOperator>(AddrInst->getOperand(0));
44081ad6265SDimitry Andric         assert(OrigBiasInst->getOpcode() == Instruction::BinaryOps::Add);
44181ad6265SDimitry Andric         Value *BiasInst = Builder.Insert(OrigBiasInst->clone());
4425f757f3fSDimitry Andric         Addr = Builder.CreateIntToPtr(BiasInst,
4435f757f3fSDimitry Andric                                       PointerType::getUnqual(Ty->getContext()));
44481ad6265SDimitry Andric       }
4450b57cec5SDimitry Andric       if (AtomicCounterUpdatePromoted)
4460b57cec5SDimitry Andric         // automic update currently can only be promoted across the current
4470b57cec5SDimitry Andric         // loop, not the whole loop nest.
4480b57cec5SDimitry Andric         Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue,
449fe6060f1SDimitry Andric                                 MaybeAlign(),
4500b57cec5SDimitry Andric                                 AtomicOrdering::SequentiallyConsistent);
4510b57cec5SDimitry Andric       else {
4520b57cec5SDimitry Andric         LoadInst *OldVal = Builder.CreateLoad(Ty, Addr, "pgocount.promoted");
4530b57cec5SDimitry Andric         auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue);
4540b57cec5SDimitry Andric         auto *NewStore = Builder.CreateStore(NewVal, Addr);
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric         // Now update the parent loop's candidate list:
4570b57cec5SDimitry Andric         if (IterativeCounterPromotion) {
4580b57cec5SDimitry Andric           auto *TargetLoop = LI.getLoopFor(ExitBlock);
4590b57cec5SDimitry Andric           if (TargetLoop)
4600b57cec5SDimitry Andric             LoopToCandidates[TargetLoop].emplace_back(OldVal, NewStore);
4610b57cec5SDimitry Andric         }
4620b57cec5SDimitry Andric       }
4630b57cec5SDimitry Andric     }
4640b57cec5SDimitry Andric   }
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric private:
4670b57cec5SDimitry Andric   Instruction *Store;
4680b57cec5SDimitry Andric   ArrayRef<BasicBlock *> ExitBlocks;
4690b57cec5SDimitry Andric   ArrayRef<Instruction *> InsertPts;
4700b57cec5SDimitry Andric   DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
4710b57cec5SDimitry Andric   LoopInfo &LI;
4720b57cec5SDimitry Andric };
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric /// A helper class to do register promotion for all profile counter
4750b57cec5SDimitry Andric /// updates in a loop.
4760b57cec5SDimitry Andric ///
4770b57cec5SDimitry Andric class PGOCounterPromoter {
4780b57cec5SDimitry Andric public:
PGOCounterPromoter(DenseMap<Loop *,SmallVector<LoadStorePair,8>> & LoopToCands,Loop & CurLoop,LoopInfo & LI,BlockFrequencyInfo * BFI)4790b57cec5SDimitry Andric   PGOCounterPromoter(
4800b57cec5SDimitry Andric       DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands,
4810b57cec5SDimitry Andric       Loop &CurLoop, LoopInfo &LI, BlockFrequencyInfo *BFI)
48204eeddc0SDimitry Andric       : LoopToCandidates(LoopToCands), L(CurLoop), LI(LI), BFI(BFI) {
4830b57cec5SDimitry Andric 
4845ffd83dbSDimitry Andric     // Skip collection of ExitBlocks and InsertPts for loops that will not be
4855ffd83dbSDimitry Andric     // able to have counters promoted.
4860b57cec5SDimitry Andric     SmallVector<BasicBlock *, 8> LoopExitBlocks;
4870b57cec5SDimitry Andric     SmallPtrSet<BasicBlock *, 8> BlockSet;
4885ffd83dbSDimitry Andric 
4890b57cec5SDimitry Andric     L.getExitBlocks(LoopExitBlocks);
4905ffd83dbSDimitry Andric     if (!isPromotionPossible(&L, LoopExitBlocks))
4915ffd83dbSDimitry Andric       return;
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric     for (BasicBlock *ExitBlock : LoopExitBlocks) {
4945f757f3fSDimitry Andric       if (BlockSet.insert(ExitBlock).second &&
4955f757f3fSDimitry Andric           llvm::none_of(predecessors(ExitBlock), [&](const BasicBlock *Pred) {
4965f757f3fSDimitry Andric             return llvm::isPresplitCoroSuspendExitEdge(*Pred, *ExitBlock);
4975f757f3fSDimitry Andric           })) {
4980b57cec5SDimitry Andric         ExitBlocks.push_back(ExitBlock);
4990b57cec5SDimitry Andric         InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
5000b57cec5SDimitry Andric       }
5010b57cec5SDimitry Andric     }
5020b57cec5SDimitry Andric   }
5030b57cec5SDimitry Andric 
run(int64_t * NumPromoted)5040b57cec5SDimitry Andric   bool run(int64_t *NumPromoted) {
5050b57cec5SDimitry Andric     // Skip 'infinite' loops:
5060b57cec5SDimitry Andric     if (ExitBlocks.size() == 0)
5070b57cec5SDimitry Andric       return false;
508e8d8bef9SDimitry Andric 
509e8d8bef9SDimitry Andric     // Skip if any of the ExitBlocks contains a ret instruction.
510e8d8bef9SDimitry Andric     // This is to prevent dumping of incomplete profile -- if the
511e8d8bef9SDimitry Andric     // the loop is a long running loop and dump is called in the middle
512e8d8bef9SDimitry Andric     // of the loop, the result profile is incomplete.
513e8d8bef9SDimitry Andric     // FIXME: add other heuristics to detect long running loops.
514e8d8bef9SDimitry Andric     if (SkipRetExitBlock) {
515bdd1243dSDimitry Andric       for (auto *BB : ExitBlocks)
516e8d8bef9SDimitry Andric         if (isa<ReturnInst>(BB->getTerminator()))
517e8d8bef9SDimitry Andric           return false;
518e8d8bef9SDimitry Andric     }
519e8d8bef9SDimitry Andric 
5200b57cec5SDimitry Andric     unsigned MaxProm = getMaxNumOfPromotionsInLoop(&L);
5210b57cec5SDimitry Andric     if (MaxProm == 0)
5220b57cec5SDimitry Andric       return false;
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric     unsigned Promoted = 0;
5250b57cec5SDimitry Andric     for (auto &Cand : LoopToCandidates[&L]) {
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric       SmallVector<PHINode *, 4> NewPHIs;
5280b57cec5SDimitry Andric       SSAUpdater SSA(&NewPHIs);
5290b57cec5SDimitry Andric       Value *InitVal = ConstantInt::get(Cand.first->getType(), 0);
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric       // If BFI is set, we will use it to guide the promotions.
5320b57cec5SDimitry Andric       if (BFI) {
5330b57cec5SDimitry Andric         auto *BB = Cand.first->getParent();
5340b57cec5SDimitry Andric         auto InstrCount = BFI->getBlockProfileCount(BB);
5350b57cec5SDimitry Andric         if (!InstrCount)
5360b57cec5SDimitry Andric           continue;
5370b57cec5SDimitry Andric         auto PreheaderCount = BFI->getBlockProfileCount(L.getLoopPreheader());
5380b57cec5SDimitry Andric         // If the average loop trip count is not greater than 1.5, we skip
5390b57cec5SDimitry Andric         // promotion.
54081ad6265SDimitry Andric         if (PreheaderCount && (*PreheaderCount * 3) >= (*InstrCount * 2))
5410b57cec5SDimitry Andric           continue;
5420b57cec5SDimitry Andric       }
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric       PGOCounterPromoterHelper Promoter(Cand.first, Cand.second, SSA, InitVal,
5450b57cec5SDimitry Andric                                         L.getLoopPreheader(), ExitBlocks,
5460b57cec5SDimitry Andric                                         InsertPts, LoopToCandidates, LI);
5470b57cec5SDimitry Andric       Promoter.run(SmallVector<Instruction *, 2>({Cand.first, Cand.second}));
5480b57cec5SDimitry Andric       Promoted++;
5490b57cec5SDimitry Andric       if (Promoted >= MaxProm)
5500b57cec5SDimitry Andric         break;
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric       (*NumPromoted)++;
5530b57cec5SDimitry Andric       if (MaxNumOfPromotions != -1 && *NumPromoted >= MaxNumOfPromotions)
5540b57cec5SDimitry Andric         break;
5550b57cec5SDimitry Andric     }
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << Promoted << " counters promoted for loop (depth="
5580b57cec5SDimitry Andric                       << L.getLoopDepth() << ")\n");
5590b57cec5SDimitry Andric     return Promoted != 0;
5600b57cec5SDimitry Andric   }
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric private:
allowSpeculativeCounterPromotion(Loop * LP)5630b57cec5SDimitry Andric   bool allowSpeculativeCounterPromotion(Loop *LP) {
5640b57cec5SDimitry Andric     SmallVector<BasicBlock *, 8> ExitingBlocks;
5650b57cec5SDimitry Andric     L.getExitingBlocks(ExitingBlocks);
5660b57cec5SDimitry Andric     // Not considierered speculative.
5670b57cec5SDimitry Andric     if (ExitingBlocks.size() == 1)
5680b57cec5SDimitry Andric       return true;
5690b57cec5SDimitry Andric     if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
5700b57cec5SDimitry Andric       return false;
5710b57cec5SDimitry Andric     return true;
5720b57cec5SDimitry Andric   }
5730b57cec5SDimitry Andric 
5745ffd83dbSDimitry Andric   // Check whether the loop satisfies the basic conditions needed to perform
5755ffd83dbSDimitry Andric   // Counter Promotions.
5764824e7fdSDimitry Andric   bool
isPromotionPossible(Loop * LP,const SmallVectorImpl<BasicBlock * > & LoopExitBlocks)5774824e7fdSDimitry Andric   isPromotionPossible(Loop *LP,
5785ffd83dbSDimitry Andric                       const SmallVectorImpl<BasicBlock *> &LoopExitBlocks) {
5790b57cec5SDimitry Andric     // We can't insert into a catchswitch.
5800b57cec5SDimitry Andric     if (llvm::any_of(LoopExitBlocks, [](BasicBlock *Exit) {
5810b57cec5SDimitry Andric           return isa<CatchSwitchInst>(Exit->getTerminator());
5820b57cec5SDimitry Andric         }))
5835ffd83dbSDimitry Andric       return false;
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric     if (!LP->hasDedicatedExits())
5865ffd83dbSDimitry Andric       return false;
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric     BasicBlock *PH = LP->getLoopPreheader();
5890b57cec5SDimitry Andric     if (!PH)
5905ffd83dbSDimitry Andric       return false;
5915ffd83dbSDimitry Andric 
5925ffd83dbSDimitry Andric     return true;
5935ffd83dbSDimitry Andric   }
5945ffd83dbSDimitry Andric 
5955ffd83dbSDimitry Andric   // Returns the max number of Counter Promotions for LP.
getMaxNumOfPromotionsInLoop(Loop * LP)5965ffd83dbSDimitry Andric   unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
5975ffd83dbSDimitry Andric     SmallVector<BasicBlock *, 8> LoopExitBlocks;
5985ffd83dbSDimitry Andric     LP->getExitBlocks(LoopExitBlocks);
5995ffd83dbSDimitry Andric     if (!isPromotionPossible(LP, LoopExitBlocks))
6000b57cec5SDimitry Andric       return 0;
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric     SmallVector<BasicBlock *, 8> ExitingBlocks;
6030b57cec5SDimitry Andric     LP->getExitingBlocks(ExitingBlocks);
6040b57cec5SDimitry Andric 
6050b57cec5SDimitry Andric     // If BFI is set, we do more aggressive promotions based on BFI.
6060b57cec5SDimitry Andric     if (BFI)
6070b57cec5SDimitry Andric       return (unsigned)-1;
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric     // Not considierered speculative.
6100b57cec5SDimitry Andric     if (ExitingBlocks.size() == 1)
6110b57cec5SDimitry Andric       return MaxNumOfPromotionsPerLoop;
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric     if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
6140b57cec5SDimitry Andric       return 0;
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric     // Whether the target block is in a loop does not matter:
6170b57cec5SDimitry Andric     if (SpeculativeCounterPromotionToLoop)
6180b57cec5SDimitry Andric       return MaxNumOfPromotionsPerLoop;
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric     // Now check the target block:
6210b57cec5SDimitry Andric     unsigned MaxProm = MaxNumOfPromotionsPerLoop;
6220b57cec5SDimitry Andric     for (auto *TargetBlock : LoopExitBlocks) {
6230b57cec5SDimitry Andric       auto *TargetLoop = LI.getLoopFor(TargetBlock);
6240b57cec5SDimitry Andric       if (!TargetLoop)
6250b57cec5SDimitry Andric         continue;
6260b57cec5SDimitry Andric       unsigned MaxPromForTarget = getMaxNumOfPromotionsInLoop(TargetLoop);
6270b57cec5SDimitry Andric       unsigned PendingCandsInTarget = LoopToCandidates[TargetLoop].size();
6280b57cec5SDimitry Andric       MaxProm =
6290b57cec5SDimitry Andric           std::min(MaxProm, std::max(MaxPromForTarget, PendingCandsInTarget) -
6300b57cec5SDimitry Andric                                 PendingCandsInTarget);
6310b57cec5SDimitry Andric     }
6320b57cec5SDimitry Andric     return MaxProm;
6330b57cec5SDimitry Andric   }
6340b57cec5SDimitry Andric 
6350b57cec5SDimitry Andric   DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
6360b57cec5SDimitry Andric   SmallVector<BasicBlock *, 8> ExitBlocks;
6370b57cec5SDimitry Andric   SmallVector<Instruction *, 8> InsertPts;
6380b57cec5SDimitry Andric   Loop &L;
6390b57cec5SDimitry Andric   LoopInfo &LI;
6400b57cec5SDimitry Andric   BlockFrequencyInfo *BFI;
6410b57cec5SDimitry Andric };
6420b57cec5SDimitry Andric 
643e8d8bef9SDimitry Andric enum class ValueProfilingCallType {
644e8d8bef9SDimitry Andric   // Individual values are tracked. Currently used for indiret call target
645e8d8bef9SDimitry Andric   // profiling.
646e8d8bef9SDimitry Andric   Default,
647e8d8bef9SDimitry Andric 
648e8d8bef9SDimitry Andric   // MemOp: the memop size value profiling.
649e8d8bef9SDimitry Andric   MemOp
650e8d8bef9SDimitry Andric };
651e8d8bef9SDimitry Andric 
6520b57cec5SDimitry Andric } // end anonymous namespace
6530b57cec5SDimitry Andric 
run(Module & M,ModuleAnalysisManager & AM)6545f757f3fSDimitry Andric PreservedAnalyses InstrProfilingLoweringPass::run(Module &M,
6555f757f3fSDimitry Andric                                                   ModuleAnalysisManager &AM) {
6568bcb0991SDimitry Andric   FunctionAnalysisManager &FAM =
6578bcb0991SDimitry Andric       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
6588bcb0991SDimitry Andric   auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
6598bcb0991SDimitry Andric     return FAM.getResult<TargetLibraryAnalysis>(F);
6608bcb0991SDimitry Andric   };
6615f757f3fSDimitry Andric   InstrLowerer Lowerer(M, Options, GetTLI, IsCS);
6625f757f3fSDimitry Andric   if (!Lowerer.lower())
6630b57cec5SDimitry Andric     return PreservedAnalyses::all();
6640b57cec5SDimitry Andric 
6650b57cec5SDimitry Andric   return PreservedAnalyses::none();
6660b57cec5SDimitry Andric }
6670b57cec5SDimitry Andric 
668*0fca6ea1SDimitry Andric //
669*0fca6ea1SDimitry Andric // Perform instrumentation sampling.
670*0fca6ea1SDimitry Andric //
671*0fca6ea1SDimitry Andric // There are 3 favors of sampling:
672*0fca6ea1SDimitry Andric // (1) Full burst sampling: We transform:
673*0fca6ea1SDimitry Andric //   Increment_Instruction;
674*0fca6ea1SDimitry Andric // to:
675*0fca6ea1SDimitry Andric //   if (__llvm_profile_sampling__ < SampledInstrBurstDuration) {
676*0fca6ea1SDimitry Andric //     Increment_Instruction;
677*0fca6ea1SDimitry Andric //   }
678*0fca6ea1SDimitry Andric //   __llvm_profile_sampling__ += 1;
679*0fca6ea1SDimitry Andric //   if (__llvm_profile_sampling__ >= SampledInstrPeriod) {
680*0fca6ea1SDimitry Andric //     __llvm_profile_sampling__ = 0;
681*0fca6ea1SDimitry Andric //   }
682*0fca6ea1SDimitry Andric //
683*0fca6ea1SDimitry Andric // "__llvm_profile_sampling__" is a thread-local global shared by all PGO
684*0fca6ea1SDimitry Andric // counters (value-instrumentation and edge instrumentation).
685*0fca6ea1SDimitry Andric //
686*0fca6ea1SDimitry Andric // (2) Fast burst sampling:
687*0fca6ea1SDimitry Andric // "__llvm_profile_sampling__" variable is an unsigned type, meaning it will
688*0fca6ea1SDimitry Andric // wrap around to zero when overflows. In this case, the second check is
689*0fca6ea1SDimitry Andric // unnecessary, so we won't generate check2 when the SampledInstrPeriod is
690*0fca6ea1SDimitry Andric // set to 65535 (64K - 1). The code after:
691*0fca6ea1SDimitry Andric //   if (__llvm_profile_sampling__ < SampledInstrBurstDuration) {
692*0fca6ea1SDimitry Andric //     Increment_Instruction;
693*0fca6ea1SDimitry Andric //   }
694*0fca6ea1SDimitry Andric //   __llvm_profile_sampling__ += 1;
695*0fca6ea1SDimitry Andric //
696*0fca6ea1SDimitry Andric // (3) Simple sampling:
697*0fca6ea1SDimitry Andric // When SampledInstrBurstDuration sets to 1, we do a simple sampling:
698*0fca6ea1SDimitry Andric //   __llvm_profile_sampling__ += 1;
699*0fca6ea1SDimitry Andric //   if (__llvm_profile_sampling__ >= SampledInstrPeriod) {
700*0fca6ea1SDimitry Andric //     __llvm_profile_sampling__ = 0;
701*0fca6ea1SDimitry Andric //     Increment_Instruction;
702*0fca6ea1SDimitry Andric //   }
703*0fca6ea1SDimitry Andric //
704*0fca6ea1SDimitry Andric // Note that, the code snippet after the transformation can still be counter
705*0fca6ea1SDimitry Andric // promoted. However, with sampling enabled, counter updates are expected to
706*0fca6ea1SDimitry Andric // be infrequent, making the benefits of counter promotion negligible.
707*0fca6ea1SDimitry Andric // Moreover, counter promotion can potentially cause issues in server
708*0fca6ea1SDimitry Andric // applications, particularly when the counters are dumped without a clean
709*0fca6ea1SDimitry Andric // exit. To mitigate this risk, counter promotion is disabled by default when
710*0fca6ea1SDimitry Andric // sampling is enabled. This behavior can be overridden using the internal
711*0fca6ea1SDimitry Andric // option.
doSampling(Instruction * I)712*0fca6ea1SDimitry Andric void InstrLowerer::doSampling(Instruction *I) {
713*0fca6ea1SDimitry Andric   if (!isSamplingEnabled())
714*0fca6ea1SDimitry Andric     return;
715*0fca6ea1SDimitry Andric 
716*0fca6ea1SDimitry Andric   unsigned SampledBurstDuration = SampledInstrBurstDuration.getValue();
717*0fca6ea1SDimitry Andric   unsigned SampledPeriod = SampledInstrPeriod.getValue();
718*0fca6ea1SDimitry Andric   if (SampledBurstDuration >= SampledPeriod) {
719*0fca6ea1SDimitry Andric     report_fatal_error(
720*0fca6ea1SDimitry Andric         "SampledPeriod needs to be greater than SampledBurstDuration");
721*0fca6ea1SDimitry Andric   }
722*0fca6ea1SDimitry Andric   bool UseShort = (SampledPeriod <= USHRT_MAX);
723*0fca6ea1SDimitry Andric   bool IsSimpleSampling = (SampledBurstDuration == 1);
724*0fca6ea1SDimitry Andric   // If (SampledBurstDuration == 1 && SampledPeriod == 65535), generate
725*0fca6ea1SDimitry Andric   // the simple sampling style code.
726*0fca6ea1SDimitry Andric   bool IsFastSampling = (!IsSimpleSampling && SampledPeriod == 65535);
727*0fca6ea1SDimitry Andric 
728*0fca6ea1SDimitry Andric   auto GetConstant = [UseShort](IRBuilder<> &Builder, uint32_t C) {
729*0fca6ea1SDimitry Andric     if (UseShort)
730*0fca6ea1SDimitry Andric       return Builder.getInt16(C);
731*0fca6ea1SDimitry Andric     else
732*0fca6ea1SDimitry Andric       return Builder.getInt32(C);
733*0fca6ea1SDimitry Andric   };
734*0fca6ea1SDimitry Andric 
735*0fca6ea1SDimitry Andric   IntegerType *SamplingVarTy;
736*0fca6ea1SDimitry Andric   if (UseShort)
737*0fca6ea1SDimitry Andric     SamplingVarTy = Type::getInt16Ty(M.getContext());
738*0fca6ea1SDimitry Andric   else
739*0fca6ea1SDimitry Andric     SamplingVarTy = Type::getInt32Ty(M.getContext());
740*0fca6ea1SDimitry Andric   auto *SamplingVar =
741*0fca6ea1SDimitry Andric       M.getGlobalVariable(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_SAMPLING_VAR));
742*0fca6ea1SDimitry Andric   assert(SamplingVar && "SamplingVar not set properly");
743*0fca6ea1SDimitry Andric 
744*0fca6ea1SDimitry Andric   // Create the condition for checking the burst duration.
745*0fca6ea1SDimitry Andric   Instruction *SamplingVarIncr;
746*0fca6ea1SDimitry Andric   Value *NewSamplingVarVal;
747*0fca6ea1SDimitry Andric   MDBuilder MDB(I->getContext());
748*0fca6ea1SDimitry Andric   MDNode *BranchWeight;
749*0fca6ea1SDimitry Andric   IRBuilder<> CondBuilder(I);
750*0fca6ea1SDimitry Andric   auto *LoadSamplingVar = CondBuilder.CreateLoad(SamplingVarTy, SamplingVar);
751*0fca6ea1SDimitry Andric   if (IsSimpleSampling) {
752*0fca6ea1SDimitry Andric     // For the simple sampling, just create the load and increments.
753*0fca6ea1SDimitry Andric     IRBuilder<> IncBuilder(I);
754*0fca6ea1SDimitry Andric     NewSamplingVarVal =
755*0fca6ea1SDimitry Andric         IncBuilder.CreateAdd(LoadSamplingVar, GetConstant(IncBuilder, 1));
756*0fca6ea1SDimitry Andric     SamplingVarIncr = IncBuilder.CreateStore(NewSamplingVarVal, SamplingVar);
757*0fca6ea1SDimitry Andric   } else {
758*0fca6ea1SDimitry Andric     // For the bust-sampling, create the conditonal update.
759*0fca6ea1SDimitry Andric     auto *DurationCond = CondBuilder.CreateICmpULE(
760*0fca6ea1SDimitry Andric         LoadSamplingVar, GetConstant(CondBuilder, SampledBurstDuration));
761*0fca6ea1SDimitry Andric     BranchWeight = MDB.createBranchWeights(
762*0fca6ea1SDimitry Andric         SampledBurstDuration, SampledPeriod + 1 - SampledBurstDuration);
763*0fca6ea1SDimitry Andric     Instruction *ThenTerm = SplitBlockAndInsertIfThen(
764*0fca6ea1SDimitry Andric         DurationCond, I, /* Unreachable */ false, BranchWeight);
765*0fca6ea1SDimitry Andric     IRBuilder<> IncBuilder(I);
766*0fca6ea1SDimitry Andric     NewSamplingVarVal =
767*0fca6ea1SDimitry Andric         IncBuilder.CreateAdd(LoadSamplingVar, GetConstant(IncBuilder, 1));
768*0fca6ea1SDimitry Andric     SamplingVarIncr = IncBuilder.CreateStore(NewSamplingVarVal, SamplingVar);
769*0fca6ea1SDimitry Andric     I->moveBefore(ThenTerm);
770*0fca6ea1SDimitry Andric   }
771*0fca6ea1SDimitry Andric 
772*0fca6ea1SDimitry Andric   if (IsFastSampling)
773*0fca6ea1SDimitry Andric     return;
774*0fca6ea1SDimitry Andric 
775*0fca6ea1SDimitry Andric   // Create the condtion for checking the period.
776*0fca6ea1SDimitry Andric   Instruction *ThenTerm, *ElseTerm;
777*0fca6ea1SDimitry Andric   IRBuilder<> PeriodCondBuilder(SamplingVarIncr);
778*0fca6ea1SDimitry Andric   auto *PeriodCond = PeriodCondBuilder.CreateICmpUGE(
779*0fca6ea1SDimitry Andric       NewSamplingVarVal, GetConstant(PeriodCondBuilder, SampledPeriod));
780*0fca6ea1SDimitry Andric   BranchWeight = MDB.createBranchWeights(1, SampledPeriod);
781*0fca6ea1SDimitry Andric   SplitBlockAndInsertIfThenElse(PeriodCond, SamplingVarIncr, &ThenTerm,
782*0fca6ea1SDimitry Andric                                 &ElseTerm, BranchWeight);
783*0fca6ea1SDimitry Andric 
784*0fca6ea1SDimitry Andric   // For the simple sampling, the counter update happens in sampling var reset.
785*0fca6ea1SDimitry Andric   if (IsSimpleSampling)
786*0fca6ea1SDimitry Andric     I->moveBefore(ThenTerm);
787*0fca6ea1SDimitry Andric 
788*0fca6ea1SDimitry Andric   IRBuilder<> ResetBuilder(ThenTerm);
789*0fca6ea1SDimitry Andric   ResetBuilder.CreateStore(GetConstant(ResetBuilder, 0), SamplingVar);
790*0fca6ea1SDimitry Andric   SamplingVarIncr->moveBefore(ElseTerm);
791*0fca6ea1SDimitry Andric }
792*0fca6ea1SDimitry Andric 
lowerIntrinsics(Function * F)7935f757f3fSDimitry Andric bool InstrLowerer::lowerIntrinsics(Function *F) {
7940b57cec5SDimitry Andric   bool MadeChange = false;
7950b57cec5SDimitry Andric   PromotionCandidates.clear();
796*0fca6ea1SDimitry Andric   SmallVector<InstrProfInstBase *, 8> InstrProfInsts;
797*0fca6ea1SDimitry Andric 
798*0fca6ea1SDimitry Andric   // To ensure compatibility with sampling, we save the intrinsics into
799*0fca6ea1SDimitry Andric   // a buffer to prevent potential breakage of the iterator (as the
800*0fca6ea1SDimitry Andric   // intrinsics will be moved to a different BB).
8010b57cec5SDimitry Andric   for (BasicBlock &BB : *F) {
802349cc55cSDimitry Andric     for (Instruction &Instr : llvm::make_early_inc_range(BB)) {
803*0fca6ea1SDimitry Andric       if (auto *IP = dyn_cast<InstrProfInstBase>(&Instr))
804*0fca6ea1SDimitry Andric         InstrProfInsts.push_back(IP);
805*0fca6ea1SDimitry Andric     }
806*0fca6ea1SDimitry Andric   }
807*0fca6ea1SDimitry Andric 
808*0fca6ea1SDimitry Andric   for (auto *Instr : InstrProfInsts) {
809*0fca6ea1SDimitry Andric     doSampling(Instr);
810*0fca6ea1SDimitry Andric     if (auto *IPIS = dyn_cast<InstrProfIncrementInstStep>(Instr)) {
81104eeddc0SDimitry Andric       lowerIncrement(IPIS);
8120b57cec5SDimitry Andric       MadeChange = true;
813*0fca6ea1SDimitry Andric     } else if (auto *IPI = dyn_cast<InstrProfIncrementInst>(Instr)) {
81404eeddc0SDimitry Andric       lowerIncrement(IPI);
81504eeddc0SDimitry Andric       MadeChange = true;
816*0fca6ea1SDimitry Andric     } else if (auto *IPC = dyn_cast<InstrProfTimestampInst>(Instr)) {
81706c3fb27SDimitry Andric       lowerTimestamp(IPC);
81806c3fb27SDimitry Andric       MadeChange = true;
819*0fca6ea1SDimitry Andric     } else if (auto *IPC = dyn_cast<InstrProfCoverInst>(Instr)) {
8201fd87a68SDimitry Andric       lowerCover(IPC);
8211fd87a68SDimitry Andric       MadeChange = true;
822*0fca6ea1SDimitry Andric     } else if (auto *IPVP = dyn_cast<InstrProfValueProfileInst>(Instr)) {
82304eeddc0SDimitry Andric       lowerValueProfileInst(IPVP);
8240b57cec5SDimitry Andric       MadeChange = true;
825*0fca6ea1SDimitry Andric     } else if (auto *IPMP = dyn_cast<InstrProfMCDCBitmapParameters>(Instr)) {
8265f757f3fSDimitry Andric       IPMP->eraseFromParent();
8275f757f3fSDimitry Andric       MadeChange = true;
828*0fca6ea1SDimitry Andric     } else if (auto *IPBU = dyn_cast<InstrProfMCDCTVBitmapUpdate>(Instr)) {
8295f757f3fSDimitry Andric       lowerMCDCTestVectorBitmapUpdate(IPBU);
8305f757f3fSDimitry Andric       MadeChange = true;
8310b57cec5SDimitry Andric     }
8320b57cec5SDimitry Andric   }
8330b57cec5SDimitry Andric 
8340b57cec5SDimitry Andric   if (!MadeChange)
8350b57cec5SDimitry Andric     return false;
8360b57cec5SDimitry Andric 
8370b57cec5SDimitry Andric   promoteCounterLoadStores(F);
8380b57cec5SDimitry Andric   return true;
8390b57cec5SDimitry Andric }
8400b57cec5SDimitry Andric 
isRuntimeCounterRelocationEnabled() const8415f757f3fSDimitry Andric bool InstrLowerer::isRuntimeCounterRelocationEnabled() const {
842fe6060f1SDimitry Andric   // Mach-O don't support weak external references.
843fe6060f1SDimitry Andric   if (TT.isOSBinFormatMachO())
844fe6060f1SDimitry Andric     return false;
845fe6060f1SDimitry Andric 
8465ffd83dbSDimitry Andric   if (RuntimeCounterRelocation.getNumOccurrences() > 0)
8475ffd83dbSDimitry Andric     return RuntimeCounterRelocation;
8485ffd83dbSDimitry Andric 
849fe6060f1SDimitry Andric   // Fuchsia uses runtime counter relocation by default.
8505ffd83dbSDimitry Andric   return TT.isOSFuchsia();
8515ffd83dbSDimitry Andric }
8525ffd83dbSDimitry Andric 
isSamplingEnabled() const853*0fca6ea1SDimitry Andric bool InstrLowerer::isSamplingEnabled() const {
854*0fca6ea1SDimitry Andric   if (SampledInstr.getNumOccurrences() > 0)
855*0fca6ea1SDimitry Andric     return SampledInstr;
856*0fca6ea1SDimitry Andric   return Options.Sampling;
857*0fca6ea1SDimitry Andric }
858*0fca6ea1SDimitry Andric 
isCounterPromotionEnabled() const8595f757f3fSDimitry Andric bool InstrLowerer::isCounterPromotionEnabled() const {
8600b57cec5SDimitry Andric   if (DoCounterPromotion.getNumOccurrences() > 0)
8610b57cec5SDimitry Andric     return DoCounterPromotion;
8620b57cec5SDimitry Andric 
8630b57cec5SDimitry Andric   return Options.DoCounterPromotion;
8640b57cec5SDimitry Andric }
8650b57cec5SDimitry Andric 
promoteCounterLoadStores(Function * F)8665f757f3fSDimitry Andric void InstrLowerer::promoteCounterLoadStores(Function *F) {
8670b57cec5SDimitry Andric   if (!isCounterPromotionEnabled())
8680b57cec5SDimitry Andric     return;
8690b57cec5SDimitry Andric 
8700b57cec5SDimitry Andric   DominatorTree DT(*F);
8710b57cec5SDimitry Andric   LoopInfo LI(DT);
8720b57cec5SDimitry Andric   DenseMap<Loop *, SmallVector<LoadStorePair, 8>> LoopPromotionCandidates;
8730b57cec5SDimitry Andric 
8740b57cec5SDimitry Andric   std::unique_ptr<BlockFrequencyInfo> BFI;
8750b57cec5SDimitry Andric   if (Options.UseBFIInPromotion) {
8760b57cec5SDimitry Andric     std::unique_ptr<BranchProbabilityInfo> BPI;
8778bcb0991SDimitry Andric     BPI.reset(new BranchProbabilityInfo(*F, LI, &GetTLI(*F)));
8780b57cec5SDimitry Andric     BFI.reset(new BlockFrequencyInfo(*F, *BPI, LI));
8790b57cec5SDimitry Andric   }
8800b57cec5SDimitry Andric 
8810b57cec5SDimitry Andric   for (const auto &LoadStore : PromotionCandidates) {
8820b57cec5SDimitry Andric     auto *CounterLoad = LoadStore.first;
8830b57cec5SDimitry Andric     auto *CounterStore = LoadStore.second;
8840b57cec5SDimitry Andric     BasicBlock *BB = CounterLoad->getParent();
8850b57cec5SDimitry Andric     Loop *ParentLoop = LI.getLoopFor(BB);
8860b57cec5SDimitry Andric     if (!ParentLoop)
8870b57cec5SDimitry Andric       continue;
8880b57cec5SDimitry Andric     LoopPromotionCandidates[ParentLoop].emplace_back(CounterLoad, CounterStore);
8890b57cec5SDimitry Andric   }
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric   SmallVector<Loop *, 4> Loops = LI.getLoopsInPreorder();
8920b57cec5SDimitry Andric 
8930b57cec5SDimitry Andric   // Do a post-order traversal of the loops so that counter updates can be
8940b57cec5SDimitry Andric   // iteratively hoisted outside the loop nest.
8950b57cec5SDimitry Andric   for (auto *Loop : llvm::reverse(Loops)) {
8960b57cec5SDimitry Andric     PGOCounterPromoter Promoter(LoopPromotionCandidates, *Loop, LI, BFI.get());
8970b57cec5SDimitry Andric     Promoter.run(&TotalCountersPromoted);
8980b57cec5SDimitry Andric   }
8990b57cec5SDimitry Andric }
9000b57cec5SDimitry Andric 
needsRuntimeHookUnconditionally(const Triple & TT)901349cc55cSDimitry Andric static bool needsRuntimeHookUnconditionally(const Triple &TT) {
902349cc55cSDimitry Andric   // On Fuchsia, we only need runtime hook if any counters are present.
903349cc55cSDimitry Andric   if (TT.isOSFuchsia())
904349cc55cSDimitry Andric     return false;
905349cc55cSDimitry Andric 
906349cc55cSDimitry Andric   return true;
907349cc55cSDimitry Andric }
908349cc55cSDimitry Andric 
9090b57cec5SDimitry Andric /// Check if the module contains uses of any profiling intrinsics.
containsProfilingIntrinsics(Module & M)9100b57cec5SDimitry Andric static bool containsProfilingIntrinsics(Module &M) {
91104eeddc0SDimitry Andric   auto containsIntrinsic = [&](int ID) {
91204eeddc0SDimitry Andric     if (auto *F = M.getFunction(Intrinsic::getName(ID)))
91304eeddc0SDimitry Andric       return !F->use_empty();
9140b57cec5SDimitry Andric     return false;
91504eeddc0SDimitry Andric   };
9161fd87a68SDimitry Andric   return containsIntrinsic(llvm::Intrinsic::instrprof_cover) ||
9171fd87a68SDimitry Andric          containsIntrinsic(llvm::Intrinsic::instrprof_increment) ||
91804eeddc0SDimitry Andric          containsIntrinsic(llvm::Intrinsic::instrprof_increment_step) ||
91906c3fb27SDimitry Andric          containsIntrinsic(llvm::Intrinsic::instrprof_timestamp) ||
92004eeddc0SDimitry Andric          containsIntrinsic(llvm::Intrinsic::instrprof_value_profile);
9210b57cec5SDimitry Andric }
9220b57cec5SDimitry Andric 
lower()9235f757f3fSDimitry Andric bool InstrLowerer::lower() {
924349cc55cSDimitry Andric   bool MadeChange = false;
925bdd1243dSDimitry Andric   bool NeedsRuntimeHook = needsRuntimeHookUnconditionally(TT);
926bdd1243dSDimitry Andric   if (NeedsRuntimeHook)
927349cc55cSDimitry Andric     MadeChange = emitRuntimeHook();
9280b57cec5SDimitry Andric 
929*0fca6ea1SDimitry Andric   if (!IsCS && isSamplingEnabled())
930*0fca6ea1SDimitry Andric     createProfileSamplingVar(M);
931*0fca6ea1SDimitry Andric 
932bdd1243dSDimitry Andric   bool ContainsProfiling = containsProfilingIntrinsics(M);
9330b57cec5SDimitry Andric   GlobalVariable *CoverageNamesVar =
9340b57cec5SDimitry Andric       M.getNamedGlobal(getCoverageUnusedNamesVarName());
935bdd1243dSDimitry Andric   // Improve compile time by avoiding linear scans when there is no work.
936bdd1243dSDimitry Andric   if (!ContainsProfiling && !CoverageNamesVar)
9370b57cec5SDimitry Andric     return MadeChange;
9380b57cec5SDimitry Andric 
9390b57cec5SDimitry Andric   // We did not know how many value sites there would be inside
9400b57cec5SDimitry Andric   // the instrumented function. This is counting the number of instrumented
9410b57cec5SDimitry Andric   // target value sites to enter it as field in the profile data variable.
9420b57cec5SDimitry Andric   for (Function &F : M) {
9435f757f3fSDimitry Andric     InstrProfCntrInstBase *FirstProfInst = nullptr;
9445f757f3fSDimitry Andric     for (BasicBlock &BB : F) {
9455f757f3fSDimitry Andric       for (auto I = BB.begin(), E = BB.end(); I != E; I++) {
9460b57cec5SDimitry Andric         if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I))
9470b57cec5SDimitry Andric           computeNumValueSiteCounts(Ind);
9485f757f3fSDimitry Andric         else {
9495f757f3fSDimitry Andric           if (FirstProfInst == nullptr &&
95006c3fb27SDimitry Andric               (isa<InstrProfIncrementInst>(I) || isa<InstrProfCoverInst>(I)))
9515f757f3fSDimitry Andric             FirstProfInst = dyn_cast<InstrProfCntrInstBase>(I);
9525f757f3fSDimitry Andric           // If the MCDCBitmapParameters intrinsic seen, create the bitmaps.
9535f757f3fSDimitry Andric           if (const auto &Params = dyn_cast<InstrProfMCDCBitmapParameters>(I))
9545f757f3fSDimitry Andric             static_cast<void>(getOrCreateRegionBitmaps(Params));
9555f757f3fSDimitry Andric         }
9565f757f3fSDimitry Andric       }
9575f757f3fSDimitry Andric     }
9580b57cec5SDimitry Andric 
9595f757f3fSDimitry Andric     // Use a profile intrinsic to create the region counters and data variable.
9605f757f3fSDimitry Andric     // Also create the data variable based on the MCDCParams.
9615f757f3fSDimitry Andric     if (FirstProfInst != nullptr) {
96206c3fb27SDimitry Andric       static_cast<void>(getOrCreateRegionCounters(FirstProfInst));
9630b57cec5SDimitry Andric     }
9645f757f3fSDimitry Andric   }
9650b57cec5SDimitry Andric 
966*0fca6ea1SDimitry Andric   if (EnableVTableValueProfiling)
967*0fca6ea1SDimitry Andric     for (GlobalVariable &GV : M.globals())
968*0fca6ea1SDimitry Andric       // Global variables with type metadata are virtual table variables.
969*0fca6ea1SDimitry Andric       if (GV.hasMetadata(LLVMContext::MD_type))
970*0fca6ea1SDimitry Andric         getOrCreateVTableProfData(&GV);
971*0fca6ea1SDimitry Andric 
9720b57cec5SDimitry Andric   for (Function &F : M)
9730b57cec5SDimitry Andric     MadeChange |= lowerIntrinsics(&F);
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric   if (CoverageNamesVar) {
9760b57cec5SDimitry Andric     lowerCoverageData(CoverageNamesVar);
9770b57cec5SDimitry Andric     MadeChange = true;
9780b57cec5SDimitry Andric   }
9790b57cec5SDimitry Andric 
9800b57cec5SDimitry Andric   if (!MadeChange)
9810b57cec5SDimitry Andric     return false;
9820b57cec5SDimitry Andric 
9830b57cec5SDimitry Andric   emitVNodes();
9840b57cec5SDimitry Andric   emitNameData();
985*0fca6ea1SDimitry Andric   emitVTableNames();
986bdd1243dSDimitry Andric 
987bdd1243dSDimitry Andric   // Emit runtime hook for the cases where the target does not unconditionally
988bdd1243dSDimitry Andric   // require pulling in profile runtime, and coverage is enabled on code that is
989bdd1243dSDimitry Andric   // not eliminated by the front-end, e.g. unused functions with internal
990bdd1243dSDimitry Andric   // linkage.
991bdd1243dSDimitry Andric   if (!NeedsRuntimeHook && ContainsProfiling)
992349cc55cSDimitry Andric     emitRuntimeHook();
993bdd1243dSDimitry Andric 
9940b57cec5SDimitry Andric   emitRegistration();
9950b57cec5SDimitry Andric   emitUses();
9960b57cec5SDimitry Andric   emitInitialization();
9970b57cec5SDimitry Andric   return true;
9980b57cec5SDimitry Andric }
9990b57cec5SDimitry Andric 
getOrInsertValueProfilingCall(Module & M,const TargetLibraryInfo & TLI,ValueProfilingCallType CallType=ValueProfilingCallType::Default)1000e8d8bef9SDimitry Andric static FunctionCallee getOrInsertValueProfilingCall(
1001e8d8bef9SDimitry Andric     Module &M, const TargetLibraryInfo &TLI,
1002e8d8bef9SDimitry Andric     ValueProfilingCallType CallType = ValueProfilingCallType::Default) {
10030b57cec5SDimitry Andric   LLVMContext &Ctx = M.getContext();
10040b57cec5SDimitry Andric   auto *ReturnTy = Type::getVoidTy(M.getContext());
10050b57cec5SDimitry Andric 
10060b57cec5SDimitry Andric   AttributeList AL;
10070b57cec5SDimitry Andric   if (auto AK = TLI.getExtAttrForI32Param(false))
10080b57cec5SDimitry Andric     AL = AL.addParamAttribute(M.getContext(), 2, AK);
10090b57cec5SDimitry Andric 
1010e8d8bef9SDimitry Andric   assert((CallType == ValueProfilingCallType::Default ||
1011e8d8bef9SDimitry Andric           CallType == ValueProfilingCallType::MemOp) &&
1012e8d8bef9SDimitry Andric          "Must be Default or MemOp");
10130b57cec5SDimitry Andric   Type *ParamTypes[] = {
10140b57cec5SDimitry Andric #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
10150b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProfData.inc"
10160b57cec5SDimitry Andric   };
10170b57cec5SDimitry Andric   auto *ValueProfilingCallTy =
1018bdd1243dSDimitry Andric       FunctionType::get(ReturnTy, ArrayRef(ParamTypes), false);
1019e8d8bef9SDimitry Andric   StringRef FuncName = CallType == ValueProfilingCallType::Default
1020e8d8bef9SDimitry Andric                            ? getInstrProfValueProfFuncName()
1021e8d8bef9SDimitry Andric                            : getInstrProfValueProfMemOpFuncName();
1022e8d8bef9SDimitry Andric   return M.getOrInsertFunction(FuncName, ValueProfilingCallTy, AL);
10230b57cec5SDimitry Andric }
10240b57cec5SDimitry Andric 
computeNumValueSiteCounts(InstrProfValueProfileInst * Ind)10255f757f3fSDimitry Andric void InstrLowerer::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
10260b57cec5SDimitry Andric   GlobalVariable *Name = Ind->getName();
10270b57cec5SDimitry Andric   uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
10280b57cec5SDimitry Andric   uint64_t Index = Ind->getIndex()->getZExtValue();
10294824e7fdSDimitry Andric   auto &PD = ProfileDataMap[Name];
10304824e7fdSDimitry Andric   PD.NumValueSites[ValueKind] =
10314824e7fdSDimitry Andric       std::max(PD.NumValueSites[ValueKind], (uint32_t)(Index + 1));
10320b57cec5SDimitry Andric }
10330b57cec5SDimitry Andric 
lowerValueProfileInst(InstrProfValueProfileInst * Ind)10345f757f3fSDimitry Andric void InstrLowerer::lowerValueProfileInst(InstrProfValueProfileInst *Ind) {
10350eae32dcSDimitry Andric   // TODO: Value profiling heavily depends on the data section which is omitted
10360eae32dcSDimitry Andric   // in lightweight mode. We need to move the value profile pointer to the
10370eae32dcSDimitry Andric   // Counter struct to get this working.
10380eae32dcSDimitry Andric   assert(
10395f757f3fSDimitry Andric       !DebugInfoCorrelate && ProfileCorrelate == InstrProfCorrelator::NONE &&
10400eae32dcSDimitry Andric       "Value profiling is not yet supported with lightweight instrumentation");
10410b57cec5SDimitry Andric   GlobalVariable *Name = Ind->getName();
10420b57cec5SDimitry Andric   auto It = ProfileDataMap.find(Name);
10430b57cec5SDimitry Andric   assert(It != ProfileDataMap.end() && It->second.DataVar &&
10440b57cec5SDimitry Andric          "value profiling detected in function with no counter incerement");
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric   GlobalVariable *DataVar = It->second.DataVar;
10470b57cec5SDimitry Andric   uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
10480b57cec5SDimitry Andric   uint64_t Index = Ind->getIndex()->getZExtValue();
10490b57cec5SDimitry Andric   for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind)
10500b57cec5SDimitry Andric     Index += It->second.NumValueSites[Kind];
10510b57cec5SDimitry Andric 
10520b57cec5SDimitry Andric   IRBuilder<> Builder(Ind);
1053e8d8bef9SDimitry Andric   bool IsMemOpSize = (Ind->getValueKind()->getZExtValue() ==
10540b57cec5SDimitry Andric                       llvm::InstrProfValueKind::IPVK_MemOPSize);
10550b57cec5SDimitry Andric   CallInst *Call = nullptr;
10568bcb0991SDimitry Andric   auto *TLI = &GetTLI(*Ind->getFunction());
10575ffd83dbSDimitry Andric 
10585ffd83dbSDimitry Andric   // To support value profiling calls within Windows exception handlers, funclet
10595ffd83dbSDimitry Andric   // information contained within operand bundles needs to be copied over to
10605ffd83dbSDimitry Andric   // the library call. This is required for the IR to be processed by the
10615ffd83dbSDimitry Andric   // WinEHPrepare pass.
10625ffd83dbSDimitry Andric   SmallVector<OperandBundleDef, 1> OpBundles;
10635ffd83dbSDimitry Andric   Ind->getOperandBundlesAsDefs(OpBundles);
1064e8d8bef9SDimitry Andric   if (!IsMemOpSize) {
10655f757f3fSDimitry Andric     Value *Args[3] = {Ind->getTargetValue(), DataVar, Builder.getInt32(Index)};
10665f757f3fSDimitry Andric     Call = Builder.CreateCall(getOrInsertValueProfilingCall(M, *TLI), Args,
10675ffd83dbSDimitry Andric                               OpBundles);
10680b57cec5SDimitry Andric   } else {
10695f757f3fSDimitry Andric     Value *Args[3] = {Ind->getTargetValue(), DataVar, Builder.getInt32(Index)};
1070e8d8bef9SDimitry Andric     Call = Builder.CreateCall(
10715f757f3fSDimitry Andric         getOrInsertValueProfilingCall(M, *TLI, ValueProfilingCallType::MemOp),
10725ffd83dbSDimitry Andric         Args, OpBundles);
10730b57cec5SDimitry Andric   }
10740b57cec5SDimitry Andric   if (auto AK = TLI->getExtAttrForI32Param(false))
10750b57cec5SDimitry Andric     Call->addParamAttr(2, AK);
10760b57cec5SDimitry Andric   Ind->replaceAllUsesWith(Call);
10770b57cec5SDimitry Andric   Ind->eraseFromParent();
10780b57cec5SDimitry Andric }
10790b57cec5SDimitry Andric 
getOrCreateBiasVar(StringRef VarName)1080*0fca6ea1SDimitry Andric GlobalVariable *InstrLowerer::getOrCreateBiasVar(StringRef VarName) {
1081*0fca6ea1SDimitry Andric   GlobalVariable *Bias = M.getGlobalVariable(VarName);
1082*0fca6ea1SDimitry Andric   if (Bias)
1083*0fca6ea1SDimitry Andric     return Bias;
1084*0fca6ea1SDimitry Andric 
1085*0fca6ea1SDimitry Andric   Type *Int64Ty = Type::getInt64Ty(M.getContext());
1086*0fca6ea1SDimitry Andric 
1087*0fca6ea1SDimitry Andric   // Compiler must define this variable when runtime counter relocation
1088*0fca6ea1SDimitry Andric   // is being used. Runtime has a weak external reference that is used
1089*0fca6ea1SDimitry Andric   // to check whether that's the case or not.
1090*0fca6ea1SDimitry Andric   Bias = new GlobalVariable(M, Int64Ty, false, GlobalValue::LinkOnceODRLinkage,
1091*0fca6ea1SDimitry Andric                             Constant::getNullValue(Int64Ty), VarName);
1092*0fca6ea1SDimitry Andric   Bias->setVisibility(GlobalVariable::HiddenVisibility);
1093*0fca6ea1SDimitry Andric   // A definition that's weak (linkonce_odr) without being in a COMDAT
1094*0fca6ea1SDimitry Andric   // section wouldn't lead to link errors, but it would lead to a dead
1095*0fca6ea1SDimitry Andric   // data word from every TU but one. Putting it in COMDAT ensures there
1096*0fca6ea1SDimitry Andric   // will be exactly one data slot in the link.
1097*0fca6ea1SDimitry Andric   if (TT.supportsCOMDAT())
1098*0fca6ea1SDimitry Andric     Bias->setComdat(M.getOrInsertComdat(VarName));
1099*0fca6ea1SDimitry Andric 
1100*0fca6ea1SDimitry Andric   return Bias;
1101*0fca6ea1SDimitry Andric }
1102*0fca6ea1SDimitry Andric 
getCounterAddress(InstrProfCntrInstBase * I)11035f757f3fSDimitry Andric Value *InstrLowerer::getCounterAddress(InstrProfCntrInstBase *I) {
11041fd87a68SDimitry Andric   auto *Counters = getOrCreateRegionCounters(I);
11051fd87a68SDimitry Andric   IRBuilder<> Builder(I);
11060b57cec5SDimitry Andric 
110706c3fb27SDimitry Andric   if (isa<InstrProfTimestampInst>(I))
110806c3fb27SDimitry Andric     Counters->setAlignment(Align(8));
110906c3fb27SDimitry Andric 
11101fd87a68SDimitry Andric   auto *Addr = Builder.CreateConstInBoundsGEP2_32(
11111fd87a68SDimitry Andric       Counters->getValueType(), Counters, 0, I->getIndex()->getZExtValue());
11120b57cec5SDimitry Andric 
11131fd87a68SDimitry Andric   if (!isRuntimeCounterRelocationEnabled())
11141fd87a68SDimitry Andric     return Addr;
11151fd87a68SDimitry Andric 
11165f757f3fSDimitry Andric   Type *Int64Ty = Type::getInt64Ty(M.getContext());
11171fd87a68SDimitry Andric   Function *Fn = I->getParent()->getParent();
111881ad6265SDimitry Andric   LoadInst *&BiasLI = FunctionToProfileBiasMap[Fn];
111981ad6265SDimitry Andric   if (!BiasLI) {
112081ad6265SDimitry Andric     IRBuilder<> EntryBuilder(&Fn->getEntryBlock().front());
1121*0fca6ea1SDimitry Andric     auto *Bias = getOrCreateBiasVar(getInstrProfCounterBiasVarName());
1122*0fca6ea1SDimitry Andric     BiasLI = EntryBuilder.CreateLoad(Int64Ty, Bias, "profc_bias");
1123*0fca6ea1SDimitry Andric     // Bias doesn't change after startup.
1124*0fca6ea1SDimitry Andric     BiasLI->setMetadata(LLVMContext::MD_invariant_load,
1125*0fca6ea1SDimitry Andric                         MDNode::get(M.getContext(), std::nullopt));
11265ffd83dbSDimitry Andric   }
112781ad6265SDimitry Andric   auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), BiasLI);
11281fd87a68SDimitry Andric   return Builder.CreateIntToPtr(Add, Addr->getType());
11295ffd83dbSDimitry Andric }
11305ffd83dbSDimitry Andric 
1131*0fca6ea1SDimitry Andric /// Create `void [[alwaysinline]] rmw_or(uint8_t *ArgAddr, uint8_t ArgVal)`
1132*0fca6ea1SDimitry Andric /// "Basic" sequence is `*ArgAddr |= ArgVal`
createRMWOrFunc()1133*0fca6ea1SDimitry Andric Function *InstrLowerer::createRMWOrFunc() {
1134*0fca6ea1SDimitry Andric   auto &Ctx = M.getContext();
1135*0fca6ea1SDimitry Andric   auto *Int8Ty = Type::getInt8Ty(Ctx);
1136*0fca6ea1SDimitry Andric   Function *Fn = Function::Create(
1137*0fca6ea1SDimitry Andric       FunctionType::get(Type::getVoidTy(Ctx),
1138*0fca6ea1SDimitry Andric                         {PointerType::getUnqual(Ctx), Int8Ty}, false),
1139*0fca6ea1SDimitry Andric       Function::LinkageTypes::PrivateLinkage, "rmw_or", M);
1140*0fca6ea1SDimitry Andric   Fn->addFnAttr(Attribute::AlwaysInline);
1141*0fca6ea1SDimitry Andric   auto *ArgAddr = Fn->getArg(0);
1142*0fca6ea1SDimitry Andric   auto *ArgVal = Fn->getArg(1);
1143*0fca6ea1SDimitry Andric   IRBuilder<> Builder(BasicBlock::Create(Ctx, "", Fn));
1144*0fca6ea1SDimitry Andric 
1145*0fca6ea1SDimitry Andric   // Load profile bitmap byte.
1146*0fca6ea1SDimitry Andric   //  %mcdc.bits = load i8, ptr %4, align 1
1147*0fca6ea1SDimitry Andric   auto *Bitmap = Builder.CreateLoad(Int8Ty, ArgAddr, "mcdc.bits");
1148*0fca6ea1SDimitry Andric 
1149*0fca6ea1SDimitry Andric   if (Options.Atomic || AtomicCounterUpdateAll) {
1150*0fca6ea1SDimitry Andric     // If ((Bitmap & Val) != Val), then execute atomic (Bitmap |= Val).
1151*0fca6ea1SDimitry Andric     // Note, just-loaded Bitmap might not be up-to-date. Use it just for
1152*0fca6ea1SDimitry Andric     // early testing.
1153*0fca6ea1SDimitry Andric     auto *Masked = Builder.CreateAnd(Bitmap, ArgVal);
1154*0fca6ea1SDimitry Andric     auto *ShouldStore = Builder.CreateICmpNE(Masked, ArgVal);
1155*0fca6ea1SDimitry Andric     auto *ThenTerm = BasicBlock::Create(Ctx, "", Fn);
1156*0fca6ea1SDimitry Andric     auto *ElseTerm = BasicBlock::Create(Ctx, "", Fn);
1157*0fca6ea1SDimitry Andric     // Assume updating will be rare.
1158*0fca6ea1SDimitry Andric     auto *Unlikely = MDBuilder(Ctx).createUnlikelyBranchWeights();
1159*0fca6ea1SDimitry Andric     Builder.CreateCondBr(ShouldStore, ThenTerm, ElseTerm, Unlikely);
1160*0fca6ea1SDimitry Andric 
1161*0fca6ea1SDimitry Andric     IRBuilder<> ThenBuilder(ThenTerm);
1162*0fca6ea1SDimitry Andric     ThenBuilder.CreateAtomicRMW(AtomicRMWInst::Or, ArgAddr, ArgVal,
1163*0fca6ea1SDimitry Andric                                 MaybeAlign(), AtomicOrdering::Monotonic);
1164*0fca6ea1SDimitry Andric     ThenBuilder.CreateRetVoid();
1165*0fca6ea1SDimitry Andric 
1166*0fca6ea1SDimitry Andric     IRBuilder<> ElseBuilder(ElseTerm);
1167*0fca6ea1SDimitry Andric     ElseBuilder.CreateRetVoid();
1168*0fca6ea1SDimitry Andric 
1169*0fca6ea1SDimitry Andric     return Fn;
1170*0fca6ea1SDimitry Andric   }
1171*0fca6ea1SDimitry Andric 
1172*0fca6ea1SDimitry Andric   // Perform logical OR of profile bitmap byte and shifted bit offset.
1173*0fca6ea1SDimitry Andric   //  %8 = or i8 %mcdc.bits, %7
1174*0fca6ea1SDimitry Andric   auto *Result = Builder.CreateOr(Bitmap, ArgVal);
1175*0fca6ea1SDimitry Andric 
1176*0fca6ea1SDimitry Andric   // Store the updated profile bitmap byte.
1177*0fca6ea1SDimitry Andric   //  store i8 %8, ptr %3, align 1
1178*0fca6ea1SDimitry Andric   Builder.CreateStore(Result, ArgAddr);
1179*0fca6ea1SDimitry Andric 
1180*0fca6ea1SDimitry Andric   // Terminator
1181*0fca6ea1SDimitry Andric   Builder.CreateRetVoid();
1182*0fca6ea1SDimitry Andric 
1183*0fca6ea1SDimitry Andric   return Fn;
1184*0fca6ea1SDimitry Andric }
1185*0fca6ea1SDimitry Andric 
getRMWOrCall(Value * Addr,Value * Val)1186*0fca6ea1SDimitry Andric CallInst *InstrLowerer::getRMWOrCall(Value *Addr, Value *Val) {
1187*0fca6ea1SDimitry Andric   if (!RMWOrFunc)
1188*0fca6ea1SDimitry Andric     RMWOrFunc = createRMWOrFunc();
1189*0fca6ea1SDimitry Andric 
1190*0fca6ea1SDimitry Andric   return CallInst::Create(RMWOrFunc, {Addr, Val});
1191*0fca6ea1SDimitry Andric }
1192*0fca6ea1SDimitry Andric 
getBitmapAddress(InstrProfMCDCTVBitmapUpdate * I)11935f757f3fSDimitry Andric Value *InstrLowerer::getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I) {
11945f757f3fSDimitry Andric   auto *Bitmaps = getOrCreateRegionBitmaps(I);
11955f757f3fSDimitry Andric   IRBuilder<> Builder(I);
11965f757f3fSDimitry Andric 
11975f757f3fSDimitry Andric   if (isRuntimeCounterRelocationEnabled()) {
11985f757f3fSDimitry Andric     LLVMContext &Ctx = M.getContext();
11995f757f3fSDimitry Andric     Ctx.diagnose(DiagnosticInfoPGOProfile(
12005f757f3fSDimitry Andric         M.getName().data(),
12015f757f3fSDimitry Andric         Twine("Runtime counter relocation is presently not supported for MC/DC "
12025f757f3fSDimitry Andric               "bitmaps."),
12035f757f3fSDimitry Andric         DS_Warning));
12045f757f3fSDimitry Andric   }
12055f757f3fSDimitry Andric 
1206*0fca6ea1SDimitry Andric   return Bitmaps;
12075f757f3fSDimitry Andric }
12085f757f3fSDimitry Andric 
lowerCover(InstrProfCoverInst * CoverInstruction)12095f757f3fSDimitry Andric void InstrLowerer::lowerCover(InstrProfCoverInst *CoverInstruction) {
12101fd87a68SDimitry Andric   auto *Addr = getCounterAddress(CoverInstruction);
12111fd87a68SDimitry Andric   IRBuilder<> Builder(CoverInstruction);
12121fd87a68SDimitry Andric   // We store zero to represent that this block is covered.
12131fd87a68SDimitry Andric   Builder.CreateStore(Builder.getInt8(0), Addr);
12141fd87a68SDimitry Andric   CoverInstruction->eraseFromParent();
12151fd87a68SDimitry Andric }
12161fd87a68SDimitry Andric 
lowerTimestamp(InstrProfTimestampInst * TimestampInstruction)12175f757f3fSDimitry Andric void InstrLowerer::lowerTimestamp(
121806c3fb27SDimitry Andric     InstrProfTimestampInst *TimestampInstruction) {
121906c3fb27SDimitry Andric   assert(TimestampInstruction->getIndex()->isZeroValue() &&
122006c3fb27SDimitry Andric          "timestamp probes are always the first probe for a function");
12215f757f3fSDimitry Andric   auto &Ctx = M.getContext();
122206c3fb27SDimitry Andric   auto *TimestampAddr = getCounterAddress(TimestampInstruction);
122306c3fb27SDimitry Andric   IRBuilder<> Builder(TimestampInstruction);
122406c3fb27SDimitry Andric   auto *CalleeTy =
122506c3fb27SDimitry Andric       FunctionType::get(Type::getVoidTy(Ctx), TimestampAddr->getType(), false);
12265f757f3fSDimitry Andric   auto Callee = M.getOrInsertFunction(
122706c3fb27SDimitry Andric       INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_SET_TIMESTAMP), CalleeTy);
122806c3fb27SDimitry Andric   Builder.CreateCall(Callee, {TimestampAddr});
122906c3fb27SDimitry Andric   TimestampInstruction->eraseFromParent();
123006c3fb27SDimitry Andric }
123106c3fb27SDimitry Andric 
lowerIncrement(InstrProfIncrementInst * Inc)12325f757f3fSDimitry Andric void InstrLowerer::lowerIncrement(InstrProfIncrementInst *Inc) {
12331fd87a68SDimitry Andric   auto *Addr = getCounterAddress(Inc);
12341fd87a68SDimitry Andric 
12351fd87a68SDimitry Andric   IRBuilder<> Builder(Inc);
12365ffd83dbSDimitry Andric   if (Options.Atomic || AtomicCounterUpdateAll ||
12371fd87a68SDimitry Andric       (Inc->getIndex()->isZeroValue() && AtomicFirstCounter)) {
12380b57cec5SDimitry Andric     Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(),
1239fe6060f1SDimitry Andric                             MaybeAlign(), AtomicOrdering::Monotonic);
12400b57cec5SDimitry Andric   } else {
12410b57cec5SDimitry Andric     Value *IncStep = Inc->getStep();
12420b57cec5SDimitry Andric     Value *Load = Builder.CreateLoad(IncStep->getType(), Addr, "pgocount");
12430b57cec5SDimitry Andric     auto *Count = Builder.CreateAdd(Load, Inc->getStep());
12440b57cec5SDimitry Andric     auto *Store = Builder.CreateStore(Count, Addr);
12450b57cec5SDimitry Andric     if (isCounterPromotionEnabled())
12460b57cec5SDimitry Andric       PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
12470b57cec5SDimitry Andric   }
12480b57cec5SDimitry Andric   Inc->eraseFromParent();
12490b57cec5SDimitry Andric }
12500b57cec5SDimitry Andric 
lowerCoverageData(GlobalVariable * CoverageNamesVar)12515f757f3fSDimitry Andric void InstrLowerer::lowerCoverageData(GlobalVariable *CoverageNamesVar) {
12520b57cec5SDimitry Andric   ConstantArray *Names =
12530b57cec5SDimitry Andric       cast<ConstantArray>(CoverageNamesVar->getInitializer());
12540b57cec5SDimitry Andric   for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) {
12550b57cec5SDimitry Andric     Constant *NC = Names->getOperand(I);
12560b57cec5SDimitry Andric     Value *V = NC->stripPointerCasts();
12570b57cec5SDimitry Andric     assert(isa<GlobalVariable>(V) && "Missing reference to function name");
12580b57cec5SDimitry Andric     GlobalVariable *Name = cast<GlobalVariable>(V);
12590b57cec5SDimitry Andric 
12600b57cec5SDimitry Andric     Name->setLinkage(GlobalValue::PrivateLinkage);
12610b57cec5SDimitry Andric     ReferencedNames.push_back(Name);
126281ad6265SDimitry Andric     if (isa<ConstantExpr>(NC))
12630b57cec5SDimitry Andric       NC->dropAllReferences();
12640b57cec5SDimitry Andric   }
12650b57cec5SDimitry Andric   CoverageNamesVar->eraseFromParent();
12660b57cec5SDimitry Andric }
12670b57cec5SDimitry Andric 
lowerMCDCTestVectorBitmapUpdate(InstrProfMCDCTVBitmapUpdate * Update)12685f757f3fSDimitry Andric void InstrLowerer::lowerMCDCTestVectorBitmapUpdate(
12695f757f3fSDimitry Andric     InstrProfMCDCTVBitmapUpdate *Update) {
12705f757f3fSDimitry Andric   IRBuilder<> Builder(Update);
12715f757f3fSDimitry Andric   auto *Int8Ty = Type::getInt8Ty(M.getContext());
12725f757f3fSDimitry Andric   auto *Int32Ty = Type::getInt32Ty(M.getContext());
12735f757f3fSDimitry Andric   auto *MCDCCondBitmapAddr = Update->getMCDCCondBitmapAddr();
12745f757f3fSDimitry Andric   auto *BitmapAddr = getBitmapAddress(Update);
12755f757f3fSDimitry Andric 
1276*0fca6ea1SDimitry Andric   // Load Temp Val + BitmapIdx.
12775f757f3fSDimitry Andric   //  %mcdc.temp = load i32, ptr %mcdc.addr, align 4
1278*0fca6ea1SDimitry Andric   auto *Temp = Builder.CreateAdd(
1279*0fca6ea1SDimitry Andric       Builder.CreateLoad(Int32Ty, MCDCCondBitmapAddr, "mcdc.temp"),
1280*0fca6ea1SDimitry Andric       Update->getBitmapIndex());
12815f757f3fSDimitry Andric 
12825f757f3fSDimitry Andric   // Calculate byte offset using div8.
12835f757f3fSDimitry Andric   //  %1 = lshr i32 %mcdc.temp, 3
12845f757f3fSDimitry Andric   auto *BitmapByteOffset = Builder.CreateLShr(Temp, 0x3);
12855f757f3fSDimitry Andric 
12865f757f3fSDimitry Andric   // Add byte offset to section base byte address.
1287*0fca6ea1SDimitry Andric   // %4 = getelementptr inbounds i8, ptr @__profbm_test, i32 %1
12885f757f3fSDimitry Andric   auto *BitmapByteAddr =
1289*0fca6ea1SDimitry Andric       Builder.CreateInBoundsPtrAdd(BitmapAddr, BitmapByteOffset);
12905f757f3fSDimitry Andric 
12915f757f3fSDimitry Andric   // Calculate bit offset into bitmap byte by using div8 remainder (AND ~8)
12925f757f3fSDimitry Andric   //  %5 = and i32 %mcdc.temp, 7
12935f757f3fSDimitry Andric   //  %6 = trunc i32 %5 to i8
12945f757f3fSDimitry Andric   auto *BitToSet = Builder.CreateTrunc(Builder.CreateAnd(Temp, 0x7), Int8Ty);
12955f757f3fSDimitry Andric 
12965f757f3fSDimitry Andric   // Shift bit offset left to form a bitmap.
12975f757f3fSDimitry Andric   //  %7 = shl i8 1, %6
12985f757f3fSDimitry Andric   auto *ShiftedVal = Builder.CreateShl(Builder.getInt8(0x1), BitToSet);
12995f757f3fSDimitry Andric 
1300*0fca6ea1SDimitry Andric   Builder.Insert(getRMWOrCall(BitmapByteAddr, ShiftedVal));
13015f757f3fSDimitry Andric   Update->eraseFromParent();
13025f757f3fSDimitry Andric }
13035f757f3fSDimitry Andric 
13040b57cec5SDimitry Andric /// Get the name of a profiling variable for a particular function.
getVarName(InstrProfInstBase * Inc,StringRef Prefix,bool & Renamed)130504eeddc0SDimitry Andric static std::string getVarName(InstrProfInstBase *Inc, StringRef Prefix,
1306349cc55cSDimitry Andric                               bool &Renamed) {
13070b57cec5SDimitry Andric   StringRef NamePrefix = getInstrProfNameVarPrefix();
13080b57cec5SDimitry Andric   StringRef Name = Inc->getName()->getName().substr(NamePrefix.size());
13090b57cec5SDimitry Andric   Function *F = Inc->getParent()->getParent();
13100b57cec5SDimitry Andric   Module *M = F->getParent();
13110b57cec5SDimitry Andric   if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) ||
1312349cc55cSDimitry Andric       !canRenameComdatFunc(*F)) {
1313349cc55cSDimitry Andric     Renamed = false;
13140b57cec5SDimitry Andric     return (Prefix + Name).str();
1315349cc55cSDimitry Andric   }
1316349cc55cSDimitry Andric   Renamed = true;
13170b57cec5SDimitry Andric   uint64_t FuncHash = Inc->getHash()->getZExtValue();
13180b57cec5SDimitry Andric   SmallVector<char, 24> HashPostfix;
13195f757f3fSDimitry Andric   if (Name.ends_with((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix)))
13200b57cec5SDimitry Andric     return (Prefix + Name).str();
13210b57cec5SDimitry Andric   return (Prefix + Name + "." + Twine(FuncHash)).str();
13220b57cec5SDimitry Andric }
13230b57cec5SDimitry Andric 
shouldRecordFunctionAddr(Function * F)13240b57cec5SDimitry Andric static inline bool shouldRecordFunctionAddr(Function *F) {
1325fe6060f1SDimitry Andric   // Only record function addresses if IR PGO is enabled or if clang value
1326fe6060f1SDimitry Andric   // profiling is enabled. Recording function addresses greatly increases object
1327fe6060f1SDimitry Andric   // file size, because it prevents the inliner from deleting functions that
1328fe6060f1SDimitry Andric   // have been inlined everywhere.
1329fe6060f1SDimitry Andric   if (!profDataReferencedByCode(*F->getParent()))
1330fe6060f1SDimitry Andric     return false;
1331fe6060f1SDimitry Andric 
13320b57cec5SDimitry Andric   // Check the linkage
13330b57cec5SDimitry Andric   bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage();
13340b57cec5SDimitry Andric   if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
13350b57cec5SDimitry Andric       !HasAvailableExternallyLinkage)
13360b57cec5SDimitry Andric     return true;
13370b57cec5SDimitry Andric 
13380b57cec5SDimitry Andric   // A function marked 'alwaysinline' with available_externally linkage can't
13390b57cec5SDimitry Andric   // have its address taken. Doing so would create an undefined external ref to
13400b57cec5SDimitry Andric   // the function, which would fail to link.
13410b57cec5SDimitry Andric   if (HasAvailableExternallyLinkage &&
13420b57cec5SDimitry Andric       F->hasFnAttribute(Attribute::AlwaysInline))
13430b57cec5SDimitry Andric     return false;
13440b57cec5SDimitry Andric 
13450b57cec5SDimitry Andric   // Prohibit function address recording if the function is both internal and
13460b57cec5SDimitry Andric   // COMDAT. This avoids the profile data variable referencing internal symbols
13470b57cec5SDimitry Andric   // in COMDAT.
13480b57cec5SDimitry Andric   if (F->hasLocalLinkage() && F->hasComdat())
13490b57cec5SDimitry Andric     return false;
13500b57cec5SDimitry Andric 
13510b57cec5SDimitry Andric   // Check uses of this function for other than direct calls or invokes to it.
13520b57cec5SDimitry Andric   // Inline virtual functions have linkeOnceODR linkage. When a key method
13530b57cec5SDimitry Andric   // exists, the vtable will only be emitted in the TU where the key method
13540b57cec5SDimitry Andric   // is defined. In a TU where vtable is not available, the function won't
13550b57cec5SDimitry Andric   // be 'addresstaken'. If its address is not recorded here, the profile data
13560b57cec5SDimitry Andric   // with missing address may be picked by the linker leading  to missing
13570b57cec5SDimitry Andric   // indirect call target info.
13580b57cec5SDimitry Andric   return F->hasAddressTaken() || F->hasLinkOnceLinkage();
13590b57cec5SDimitry Andric }
13600b57cec5SDimitry Andric 
shouldUsePublicSymbol(Function * Fn)136106c3fb27SDimitry Andric static inline bool shouldUsePublicSymbol(Function *Fn) {
136206c3fb27SDimitry Andric   // It isn't legal to make an alias of this function at all
136306c3fb27SDimitry Andric   if (Fn->isDeclarationForLinker())
136406c3fb27SDimitry Andric     return true;
136506c3fb27SDimitry Andric 
136606c3fb27SDimitry Andric   // Symbols with local linkage can just use the symbol directly without
136706c3fb27SDimitry Andric   // introducing relocations
136806c3fb27SDimitry Andric   if (Fn->hasLocalLinkage())
136906c3fb27SDimitry Andric     return true;
137006c3fb27SDimitry Andric 
137106c3fb27SDimitry Andric   // PGO + ThinLTO + CFI cause duplicate symbols to be introduced due to some
137206c3fb27SDimitry Andric   // unfavorable interaction between the new alias and the alias renaming done
137306c3fb27SDimitry Andric   // in LowerTypeTests under ThinLTO. For comdat functions that would normally
137406c3fb27SDimitry Andric   // be deduplicated, but the renaming scheme ends up preventing renaming, since
137506c3fb27SDimitry Andric   // it creates unique names for each alias, resulting in duplicated symbols. In
137606c3fb27SDimitry Andric   // the future, we should update the CFI related passes to migrate these
137706c3fb27SDimitry Andric   // aliases to the same module as the jump-table they refer to will be defined.
137806c3fb27SDimitry Andric   if (Fn->hasMetadata(LLVMContext::MD_type))
137906c3fb27SDimitry Andric     return true;
138006c3fb27SDimitry Andric 
138106c3fb27SDimitry Andric   // For comdat functions, an alias would need the same linkage as the original
138206c3fb27SDimitry Andric   // function and hidden visibility. There is no point in adding an alias with
138306c3fb27SDimitry Andric   // identical linkage an visibility to avoid introducing symbolic relocations.
138406c3fb27SDimitry Andric   if (Fn->hasComdat() &&
138506c3fb27SDimitry Andric       (Fn->getVisibility() == GlobalValue::VisibilityTypes::HiddenVisibility))
138606c3fb27SDimitry Andric     return true;
138706c3fb27SDimitry Andric 
138806c3fb27SDimitry Andric   // its OK to use an alias
138906c3fb27SDimitry Andric   return false;
139006c3fb27SDimitry Andric }
139106c3fb27SDimitry Andric 
getFuncAddrForProfData(Function * Fn)139206c3fb27SDimitry Andric static inline Constant *getFuncAddrForProfData(Function *Fn) {
13935f757f3fSDimitry Andric   auto *Int8PtrTy = PointerType::getUnqual(Fn->getContext());
139406c3fb27SDimitry Andric   // Store a nullptr in __llvm_profd, if we shouldn't use a real address
139506c3fb27SDimitry Andric   if (!shouldRecordFunctionAddr(Fn))
139606c3fb27SDimitry Andric     return ConstantPointerNull::get(Int8PtrTy);
139706c3fb27SDimitry Andric 
139806c3fb27SDimitry Andric   // If we can't use an alias, we must use the public symbol, even though this
139906c3fb27SDimitry Andric   // may require a symbolic relocation.
140006c3fb27SDimitry Andric   if (shouldUsePublicSymbol(Fn))
14015f757f3fSDimitry Andric     return Fn;
140206c3fb27SDimitry Andric 
140306c3fb27SDimitry Andric   // When possible use a private alias to avoid symbolic relocations.
140406c3fb27SDimitry Andric   auto *GA = GlobalAlias::create(GlobalValue::LinkageTypes::PrivateLinkage,
140506c3fb27SDimitry Andric                                  Fn->getName() + ".local", Fn);
140606c3fb27SDimitry Andric 
140706c3fb27SDimitry Andric   // When the instrumented function is a COMDAT function, we cannot use a
140806c3fb27SDimitry Andric   // private alias. If we did, we would create reference to a local label in
140906c3fb27SDimitry Andric   // this function's section. If this version of the function isn't selected by
141006c3fb27SDimitry Andric   // the linker, then the metadata would introduce a reference to a discarded
141106c3fb27SDimitry Andric   // section. So, for COMDAT functions, we need to adjust the linkage of the
141206c3fb27SDimitry Andric   // alias. Using hidden visibility avoids a dynamic relocation and an entry in
141306c3fb27SDimitry Andric   // the dynamic symbol table.
141406c3fb27SDimitry Andric   //
141506c3fb27SDimitry Andric   // Note that this handles COMDAT functions with visibility other than Hidden,
141606c3fb27SDimitry Andric   // since that case is covered in shouldUsePublicSymbol()
141706c3fb27SDimitry Andric   if (Fn->hasComdat()) {
141806c3fb27SDimitry Andric     GA->setLinkage(Fn->getLinkage());
141906c3fb27SDimitry Andric     GA->setVisibility(GlobalValue::VisibilityTypes::HiddenVisibility);
142006c3fb27SDimitry Andric   }
142106c3fb27SDimitry Andric 
142206c3fb27SDimitry Andric   // appendToCompilerUsed(*Fn->getParent(), {GA});
142306c3fb27SDimitry Andric 
14245f757f3fSDimitry Andric   return GA;
142506c3fb27SDimitry Andric }
142606c3fb27SDimitry Andric 
needsRuntimeRegistrationOfSectionRange(const Triple & TT)14270b57cec5SDimitry Andric static bool needsRuntimeRegistrationOfSectionRange(const Triple &TT) {
14281db9f3b2SDimitry Andric   // compiler-rt uses linker support to get data/counters/name start/end for
14291db9f3b2SDimitry Andric   // ELF, COFF, Mach-O and XCOFF.
14301db9f3b2SDimitry Andric   if (TT.isOSBinFormatELF() || TT.isOSBinFormatCOFF() ||
14311db9f3b2SDimitry Andric       TT.isOSBinFormatMachO() || TT.isOSBinFormatXCOFF())
14320b57cec5SDimitry Andric     return false;
14330b57cec5SDimitry Andric 
14340b57cec5SDimitry Andric   return true;
14350b57cec5SDimitry Andric }
14360b57cec5SDimitry Andric 
maybeSetComdat(GlobalVariable * GV,GlobalObject * GO,StringRef CounterGroupName)1437*0fca6ea1SDimitry Andric void InstrLowerer::maybeSetComdat(GlobalVariable *GV, GlobalObject *GO,
1438*0fca6ea1SDimitry Andric                                   StringRef CounterGroupName) {
1439*0fca6ea1SDimitry Andric   // Place lowered global variables in a comdat group if the associated function
1440*0fca6ea1SDimitry Andric   // or global variable is a COMDAT. This will make sure that only one copy of
1441*0fca6ea1SDimitry Andric   // global variable (e.g. function counters) of the COMDAT function will be
1442*0fca6ea1SDimitry Andric   // emitted after linking.
1443*0fca6ea1SDimitry Andric   bool NeedComdat = needsComdatForCounter(*GO, M);
14445f757f3fSDimitry Andric   bool UseComdat = (NeedComdat || TT.isOSBinFormatELF());
14455f757f3fSDimitry Andric 
14465f757f3fSDimitry Andric   if (!UseComdat)
14475f757f3fSDimitry Andric     return;
14485f757f3fSDimitry Andric 
1449*0fca6ea1SDimitry Andric   // Keep in mind that this pass may run before the inliner, so we need to
1450*0fca6ea1SDimitry Andric   // create a new comdat group (for counters, profiling data, etc). If we use
1451*0fca6ea1SDimitry Andric   // the comdat of the parent function, that will result in relocations against
1452*0fca6ea1SDimitry Andric   // discarded sections.
1453*0fca6ea1SDimitry Andric   //
1454*0fca6ea1SDimitry Andric   // If the data variable is referenced by code, non-counter variables (notably
1455*0fca6ea1SDimitry Andric   // profiling data) and counters have to be in different comdats for COFF
1456*0fca6ea1SDimitry Andric   // because the Visual C++ linker will report duplicate symbol errors if there
1457*0fca6ea1SDimitry Andric   // are multiple external symbols with the same name marked
1458*0fca6ea1SDimitry Andric   // IMAGE_COMDAT_SELECT_ASSOCIATIVE.
1459*0fca6ea1SDimitry Andric   StringRef GroupName = TT.isOSBinFormatCOFF() && DataReferencedByCode
1460*0fca6ea1SDimitry Andric                             ? GV->getName()
1461*0fca6ea1SDimitry Andric                             : CounterGroupName;
14625f757f3fSDimitry Andric   Comdat *C = M.getOrInsertComdat(GroupName);
1463*0fca6ea1SDimitry Andric 
1464*0fca6ea1SDimitry Andric   if (!NeedComdat) {
1465*0fca6ea1SDimitry Andric     // Object file format must be ELF since `UseComdat && !NeedComdat` is true.
1466*0fca6ea1SDimitry Andric     //
1467*0fca6ea1SDimitry Andric     // For ELF, when not using COMDAT, put counters, data and values into a
1468*0fca6ea1SDimitry Andric     // nodeduplicate COMDAT which is lowered to a zero-flag section group. This
1469*0fca6ea1SDimitry Andric     // allows -z start-stop-gc to discard the entire group when the function is
1470*0fca6ea1SDimitry Andric     // discarded.
14715f757f3fSDimitry Andric     C->setSelectionKind(Comdat::NoDeduplicate);
1472*0fca6ea1SDimitry Andric   }
14735f757f3fSDimitry Andric   GV->setComdat(C);
14745f757f3fSDimitry Andric   // COFF doesn't allow the comdat group leader to have private linkage, so
14755f757f3fSDimitry Andric   // upgrade private linkage to internal linkage to produce a symbol table
14765f757f3fSDimitry Andric   // entry.
14775f757f3fSDimitry Andric   if (TT.isOSBinFormatCOFF() && GV->hasPrivateLinkage())
14785f757f3fSDimitry Andric     GV->setLinkage(GlobalValue::InternalLinkage);
14791fd87a68SDimitry Andric }
14801fd87a68SDimitry Andric 
shouldRecordVTableAddr(GlobalVariable * GV)1481*0fca6ea1SDimitry Andric static inline bool shouldRecordVTableAddr(GlobalVariable *GV) {
1482*0fca6ea1SDimitry Andric   if (!profDataReferencedByCode(*GV->getParent()))
1483*0fca6ea1SDimitry Andric     return false;
1484*0fca6ea1SDimitry Andric 
1485*0fca6ea1SDimitry Andric   if (!GV->hasLinkOnceLinkage() && !GV->hasLocalLinkage() &&
1486*0fca6ea1SDimitry Andric       !GV->hasAvailableExternallyLinkage())
1487*0fca6ea1SDimitry Andric     return true;
1488*0fca6ea1SDimitry Andric 
1489*0fca6ea1SDimitry Andric   // This avoids the profile data from referencing internal symbols in
1490*0fca6ea1SDimitry Andric   // COMDAT.
1491*0fca6ea1SDimitry Andric   if (GV->hasLocalLinkage() && GV->hasComdat())
1492*0fca6ea1SDimitry Andric     return false;
1493*0fca6ea1SDimitry Andric 
1494*0fca6ea1SDimitry Andric   return true;
1495*0fca6ea1SDimitry Andric }
1496*0fca6ea1SDimitry Andric 
1497*0fca6ea1SDimitry Andric // FIXME: Introduce an internal alias like what's done for functions to reduce
1498*0fca6ea1SDimitry Andric // the number of relocation entries.
getVTableAddrForProfData(GlobalVariable * GV)1499*0fca6ea1SDimitry Andric static inline Constant *getVTableAddrForProfData(GlobalVariable *GV) {
1500*0fca6ea1SDimitry Andric   auto *Int8PtrTy = PointerType::getUnqual(GV->getContext());
1501*0fca6ea1SDimitry Andric 
1502*0fca6ea1SDimitry Andric   // Store a nullptr in __profvt_ if a real address shouldn't be used.
1503*0fca6ea1SDimitry Andric   if (!shouldRecordVTableAddr(GV))
1504*0fca6ea1SDimitry Andric     return ConstantPointerNull::get(Int8PtrTy);
1505*0fca6ea1SDimitry Andric 
1506*0fca6ea1SDimitry Andric   return ConstantExpr::getBitCast(GV, Int8PtrTy);
1507*0fca6ea1SDimitry Andric }
1508*0fca6ea1SDimitry Andric 
getOrCreateVTableProfData(GlobalVariable * GV)1509*0fca6ea1SDimitry Andric void InstrLowerer::getOrCreateVTableProfData(GlobalVariable *GV) {
1510*0fca6ea1SDimitry Andric   assert(!DebugInfoCorrelate &&
1511*0fca6ea1SDimitry Andric          "Value profiling is not supported with lightweight instrumentation");
1512*0fca6ea1SDimitry Andric   if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
1513*0fca6ea1SDimitry Andric     return;
1514*0fca6ea1SDimitry Andric 
1515*0fca6ea1SDimitry Andric   // Skip llvm internal global variable or __prof variables.
1516*0fca6ea1SDimitry Andric   if (GV->getName().starts_with("llvm.") ||
1517*0fca6ea1SDimitry Andric       GV->getName().starts_with("__llvm") ||
1518*0fca6ea1SDimitry Andric       GV->getName().starts_with("__prof"))
1519*0fca6ea1SDimitry Andric     return;
1520*0fca6ea1SDimitry Andric 
1521*0fca6ea1SDimitry Andric   // VTableProfData already created
1522*0fca6ea1SDimitry Andric   auto It = VTableDataMap.find(GV);
1523*0fca6ea1SDimitry Andric   if (It != VTableDataMap.end() && It->second)
1524*0fca6ea1SDimitry Andric     return;
1525*0fca6ea1SDimitry Andric 
1526*0fca6ea1SDimitry Andric   GlobalValue::LinkageTypes Linkage = GV->getLinkage();
1527*0fca6ea1SDimitry Andric   GlobalValue::VisibilityTypes Visibility = GV->getVisibility();
1528*0fca6ea1SDimitry Andric 
1529*0fca6ea1SDimitry Andric   // This is to keep consistent with per-function profile data
1530*0fca6ea1SDimitry Andric   // for correctness.
1531*0fca6ea1SDimitry Andric   if (TT.isOSBinFormatXCOFF()) {
1532*0fca6ea1SDimitry Andric     Linkage = GlobalValue::InternalLinkage;
1533*0fca6ea1SDimitry Andric     Visibility = GlobalValue::DefaultVisibility;
1534*0fca6ea1SDimitry Andric   }
1535*0fca6ea1SDimitry Andric 
1536*0fca6ea1SDimitry Andric   LLVMContext &Ctx = M.getContext();
1537*0fca6ea1SDimitry Andric   Type *DataTypes[] = {
1538*0fca6ea1SDimitry Andric #define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Init) LLVMType,
1539*0fca6ea1SDimitry Andric #include "llvm/ProfileData/InstrProfData.inc"
1540*0fca6ea1SDimitry Andric #undef INSTR_PROF_VTABLE_DATA
1541*0fca6ea1SDimitry Andric   };
1542*0fca6ea1SDimitry Andric 
1543*0fca6ea1SDimitry Andric   auto *DataTy = StructType::get(Ctx, ArrayRef(DataTypes));
1544*0fca6ea1SDimitry Andric 
1545*0fca6ea1SDimitry Andric   // Used by INSTR_PROF_VTABLE_DATA MACRO
1546*0fca6ea1SDimitry Andric   Constant *VTableAddr = getVTableAddrForProfData(GV);
1547*0fca6ea1SDimitry Andric   const std::string PGOVTableName = getPGOName(*GV);
1548*0fca6ea1SDimitry Andric   // Record the length of the vtable. This is needed since vtable pointers
1549*0fca6ea1SDimitry Andric   // loaded from C++ objects might be from the middle of a vtable definition.
1550*0fca6ea1SDimitry Andric   uint32_t VTableSizeVal =
1551*0fca6ea1SDimitry Andric       M.getDataLayout().getTypeAllocSize(GV->getValueType());
1552*0fca6ea1SDimitry Andric 
1553*0fca6ea1SDimitry Andric   Constant *DataVals[] = {
1554*0fca6ea1SDimitry Andric #define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Init) Init,
1555*0fca6ea1SDimitry Andric #include "llvm/ProfileData/InstrProfData.inc"
1556*0fca6ea1SDimitry Andric #undef INSTR_PROF_VTABLE_DATA
1557*0fca6ea1SDimitry Andric   };
1558*0fca6ea1SDimitry Andric 
1559*0fca6ea1SDimitry Andric   auto *Data =
1560*0fca6ea1SDimitry Andric       new GlobalVariable(M, DataTy, /*constant=*/false, Linkage,
1561*0fca6ea1SDimitry Andric                          ConstantStruct::get(DataTy, DataVals),
1562*0fca6ea1SDimitry Andric                          getInstrProfVTableVarPrefix() + PGOVTableName);
1563*0fca6ea1SDimitry Andric 
1564*0fca6ea1SDimitry Andric   Data->setVisibility(Visibility);
1565*0fca6ea1SDimitry Andric   Data->setSection(getInstrProfSectionName(IPSK_vtab, TT.getObjectFormat()));
1566*0fca6ea1SDimitry Andric   Data->setAlignment(Align(8));
1567*0fca6ea1SDimitry Andric 
1568*0fca6ea1SDimitry Andric   maybeSetComdat(Data, GV, Data->getName());
1569*0fca6ea1SDimitry Andric 
1570*0fca6ea1SDimitry Andric   VTableDataMap[GV] = Data;
1571*0fca6ea1SDimitry Andric 
1572*0fca6ea1SDimitry Andric   ReferencedVTables.push_back(GV);
1573*0fca6ea1SDimitry Andric 
1574*0fca6ea1SDimitry Andric   // VTable <Hash, Addr> is used by runtime but not referenced by other
1575*0fca6ea1SDimitry Andric   // sections. Conservatively mark it linker retained.
1576*0fca6ea1SDimitry Andric   UsedVars.push_back(Data);
1577*0fca6ea1SDimitry Andric }
1578*0fca6ea1SDimitry Andric 
setupProfileSection(InstrProfInstBase * Inc,InstrProfSectKind IPSK)15795f757f3fSDimitry Andric GlobalVariable *InstrLowerer::setupProfileSection(InstrProfInstBase *Inc,
15805f757f3fSDimitry Andric                                                   InstrProfSectKind IPSK) {
15810b57cec5SDimitry Andric   GlobalVariable *NamePtr = Inc->getName();
15820b57cec5SDimitry Andric 
1583fe6060f1SDimitry Andric   // Match the linkage and visibility of the name global.
15840b57cec5SDimitry Andric   Function *Fn = Inc->getParent()->getParent();
15850b57cec5SDimitry Andric   GlobalValue::LinkageTypes Linkage = NamePtr->getLinkage();
15860b57cec5SDimitry Andric   GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
15870b57cec5SDimitry Andric 
15880eae32dcSDimitry Andric   // Use internal rather than private linkage so the counter variable shows up
15890eae32dcSDimitry Andric   // in the symbol table when using debug info for correlation.
15905f757f3fSDimitry Andric   if ((DebugInfoCorrelate ||
15915f757f3fSDimitry Andric        ProfileCorrelate == InstrProfCorrelator::DEBUG_INFO) &&
15925f757f3fSDimitry Andric       TT.isOSBinFormatMachO() && Linkage == GlobalValue::PrivateLinkage)
15930eae32dcSDimitry Andric     Linkage = GlobalValue::InternalLinkage;
15940eae32dcSDimitry Andric 
1595349cc55cSDimitry Andric   // Due to the limitation of binder as of 2021/09/28, the duplicate weak
1596349cc55cSDimitry Andric   // symbols in the same csect won't be discarded. When there are duplicate weak
1597349cc55cSDimitry Andric   // symbols, we can NOT guarantee that the relocations get resolved to the
1598349cc55cSDimitry Andric   // intended weak symbol, so we can not ensure the correctness of the relative
1599349cc55cSDimitry Andric   // CounterPtr, so we have to use private linkage for counter and data symbols.
1600349cc55cSDimitry Andric   if (TT.isOSBinFormatXCOFF()) {
1601349cc55cSDimitry Andric     Linkage = GlobalValue::PrivateLinkage;
1602349cc55cSDimitry Andric     Visibility = GlobalValue::DefaultVisibility;
1603349cc55cSDimitry Andric   }
1604*0fca6ea1SDimitry Andric   // Move the name variable to the right section.
1605349cc55cSDimitry Andric   bool Renamed;
16065f757f3fSDimitry Andric   GlobalVariable *Ptr;
16075f757f3fSDimitry Andric   StringRef VarPrefix;
16085f757f3fSDimitry Andric   std::string VarName;
16095f757f3fSDimitry Andric   if (IPSK == IPSK_cnts) {
16105f757f3fSDimitry Andric     VarPrefix = getInstrProfCountersVarPrefix();
16115f757f3fSDimitry Andric     VarName = getVarName(Inc, VarPrefix, Renamed);
16125f757f3fSDimitry Andric     InstrProfCntrInstBase *CntrIncrement = dyn_cast<InstrProfCntrInstBase>(Inc);
16135f757f3fSDimitry Andric     Ptr = createRegionCounters(CntrIncrement, VarName, Linkage);
16145f757f3fSDimitry Andric   } else if (IPSK == IPSK_bitmap) {
16155f757f3fSDimitry Andric     VarPrefix = getInstrProfBitmapVarPrefix();
16165f757f3fSDimitry Andric     VarName = getVarName(Inc, VarPrefix, Renamed);
16175f757f3fSDimitry Andric     InstrProfMCDCBitmapInstBase *BitmapUpdate =
16185f757f3fSDimitry Andric         dyn_cast<InstrProfMCDCBitmapInstBase>(Inc);
16195f757f3fSDimitry Andric     Ptr = createRegionBitmaps(BitmapUpdate, VarName, Linkage);
16205f757f3fSDimitry Andric   } else {
16215f757f3fSDimitry Andric     llvm_unreachable("Profile Section must be for Counters or Bitmaps");
1622fe6060f1SDimitry Andric   }
16230b57cec5SDimitry Andric 
16245f757f3fSDimitry Andric   Ptr->setVisibility(Visibility);
16255f757f3fSDimitry Andric   // Put the counters and bitmaps in their own sections so linkers can
16265f757f3fSDimitry Andric   // remove unneeded sections.
16275f757f3fSDimitry Andric   Ptr->setSection(getInstrProfSectionName(IPSK, TT.getObjectFormat()));
16285f757f3fSDimitry Andric   Ptr->setLinkage(Linkage);
16295f757f3fSDimitry Andric   maybeSetComdat(Ptr, Fn, VarName);
16305f757f3fSDimitry Andric   return Ptr;
16315f757f3fSDimitry Andric }
16325f757f3fSDimitry Andric 
16335f757f3fSDimitry Andric GlobalVariable *
createRegionBitmaps(InstrProfMCDCBitmapInstBase * Inc,StringRef Name,GlobalValue::LinkageTypes Linkage)16345f757f3fSDimitry Andric InstrLowerer::createRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc,
16355f757f3fSDimitry Andric                                   StringRef Name,
16365f757f3fSDimitry Andric                                   GlobalValue::LinkageTypes Linkage) {
1637*0fca6ea1SDimitry Andric   uint64_t NumBytes = Inc->getNumBitmapBytes();
16385f757f3fSDimitry Andric   auto *BitmapTy = ArrayType::get(Type::getInt8Ty(M.getContext()), NumBytes);
16395f757f3fSDimitry Andric   auto GV = new GlobalVariable(M, BitmapTy, false, Linkage,
16405f757f3fSDimitry Andric                                Constant::getNullValue(BitmapTy), Name);
16415f757f3fSDimitry Andric   GV->setAlignment(Align(1));
16425f757f3fSDimitry Andric   return GV;
16435f757f3fSDimitry Andric }
16445f757f3fSDimitry Andric 
16455f757f3fSDimitry Andric GlobalVariable *
getOrCreateRegionBitmaps(InstrProfMCDCBitmapInstBase * Inc)16465f757f3fSDimitry Andric InstrLowerer::getOrCreateRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc) {
16475f757f3fSDimitry Andric   GlobalVariable *NamePtr = Inc->getName();
16485f757f3fSDimitry Andric   auto &PD = ProfileDataMap[NamePtr];
16495f757f3fSDimitry Andric   if (PD.RegionBitmaps)
16505f757f3fSDimitry Andric     return PD.RegionBitmaps;
16515f757f3fSDimitry Andric 
16525f757f3fSDimitry Andric   // If RegionBitmaps doesn't already exist, create it by first setting up
16535f757f3fSDimitry Andric   // the corresponding profile section.
16545f757f3fSDimitry Andric   auto *BitmapPtr = setupProfileSection(Inc, IPSK_bitmap);
16555f757f3fSDimitry Andric   PD.RegionBitmaps = BitmapPtr;
1656*0fca6ea1SDimitry Andric   PD.NumBitmapBytes = Inc->getNumBitmapBytes();
16575f757f3fSDimitry Andric   return PD.RegionBitmaps;
16585f757f3fSDimitry Andric }
16595f757f3fSDimitry Andric 
16605f757f3fSDimitry Andric GlobalVariable *
createRegionCounters(InstrProfCntrInstBase * Inc,StringRef Name,GlobalValue::LinkageTypes Linkage)16615f757f3fSDimitry Andric InstrLowerer::createRegionCounters(InstrProfCntrInstBase *Inc, StringRef Name,
16625f757f3fSDimitry Andric                                    GlobalValue::LinkageTypes Linkage) {
16630b57cec5SDimitry Andric   uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
16645f757f3fSDimitry Andric   auto &Ctx = M.getContext();
16655f757f3fSDimitry Andric   GlobalVariable *GV;
16665f757f3fSDimitry Andric   if (isa<InstrProfCoverInst>(Inc)) {
16675f757f3fSDimitry Andric     auto *CounterTy = Type::getInt8Ty(Ctx);
16685f757f3fSDimitry Andric     auto *CounterArrTy = ArrayType::get(CounterTy, NumCounters);
16695f757f3fSDimitry Andric     // TODO: `Constant::getAllOnesValue()` does not yet accept an array type.
16705f757f3fSDimitry Andric     std::vector<Constant *> InitialValues(NumCounters,
16715f757f3fSDimitry Andric                                           Constant::getAllOnesValue(CounterTy));
16725f757f3fSDimitry Andric     GV = new GlobalVariable(M, CounterArrTy, false, Linkage,
16735f757f3fSDimitry Andric                             ConstantArray::get(CounterArrTy, InitialValues),
16745f757f3fSDimitry Andric                             Name);
16755f757f3fSDimitry Andric     GV->setAlignment(Align(1));
16765f757f3fSDimitry Andric   } else {
16775f757f3fSDimitry Andric     auto *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
16785f757f3fSDimitry Andric     GV = new GlobalVariable(M, CounterTy, false, Linkage,
16795f757f3fSDimitry Andric                             Constant::getNullValue(CounterTy), Name);
16805f757f3fSDimitry Andric     GV->setAlignment(Align(8));
16815f757f3fSDimitry Andric   }
16825f757f3fSDimitry Andric   return GV;
16835f757f3fSDimitry Andric }
16840b57cec5SDimitry Andric 
16855f757f3fSDimitry Andric GlobalVariable *
getOrCreateRegionCounters(InstrProfCntrInstBase * Inc)16865f757f3fSDimitry Andric InstrLowerer::getOrCreateRegionCounters(InstrProfCntrInstBase *Inc) {
16875f757f3fSDimitry Andric   GlobalVariable *NamePtr = Inc->getName();
16885f757f3fSDimitry Andric   auto &PD = ProfileDataMap[NamePtr];
16895f757f3fSDimitry Andric   if (PD.RegionCounters)
16905f757f3fSDimitry Andric     return PD.RegionCounters;
16915f757f3fSDimitry Andric 
16925f757f3fSDimitry Andric   // If RegionCounters doesn't already exist, create it by first setting up
16935f757f3fSDimitry Andric   // the corresponding profile section.
16945f757f3fSDimitry Andric   auto *CounterPtr = setupProfileSection(Inc, IPSK_cnts);
16954824e7fdSDimitry Andric   PD.RegionCounters = CounterPtr;
16965f757f3fSDimitry Andric 
16975f757f3fSDimitry Andric   if (DebugInfoCorrelate ||
16985f757f3fSDimitry Andric       ProfileCorrelate == InstrProfCorrelator::DEBUG_INFO) {
16995f757f3fSDimitry Andric     LLVMContext &Ctx = M.getContext();
17005f757f3fSDimitry Andric     Function *Fn = Inc->getParent()->getParent();
17010eae32dcSDimitry Andric     if (auto *SP = Fn->getSubprogram()) {
17025f757f3fSDimitry Andric       DIBuilder DB(M, true, SP->getUnit());
17030eae32dcSDimitry Andric       Metadata *FunctionNameAnnotation[] = {
17040eae32dcSDimitry Andric           MDString::get(Ctx, InstrProfCorrelator::FunctionNameAttributeName),
17050eae32dcSDimitry Andric           MDString::get(Ctx, getPGOFuncNameVarInitializer(NamePtr)),
17060eae32dcSDimitry Andric       };
17070eae32dcSDimitry Andric       Metadata *CFGHashAnnotation[] = {
17080eae32dcSDimitry Andric           MDString::get(Ctx, InstrProfCorrelator::CFGHashAttributeName),
17090eae32dcSDimitry Andric           ConstantAsMetadata::get(Inc->getHash()),
17100eae32dcSDimitry Andric       };
17110eae32dcSDimitry Andric       Metadata *NumCountersAnnotation[] = {
17120eae32dcSDimitry Andric           MDString::get(Ctx, InstrProfCorrelator::NumCountersAttributeName),
17130eae32dcSDimitry Andric           ConstantAsMetadata::get(Inc->getNumCounters()),
17140eae32dcSDimitry Andric       };
17150eae32dcSDimitry Andric       auto Annotations = DB.getOrCreateArray({
17160eae32dcSDimitry Andric           MDNode::get(Ctx, FunctionNameAnnotation),
17170eae32dcSDimitry Andric           MDNode::get(Ctx, CFGHashAnnotation),
17180eae32dcSDimitry Andric           MDNode::get(Ctx, NumCountersAnnotation),
17190eae32dcSDimitry Andric       });
17200eae32dcSDimitry Andric       auto *DICounter = DB.createGlobalVariableExpression(
17210eae32dcSDimitry Andric           SP, CounterPtr->getName(), /*LinkageName=*/StringRef(), SP->getFile(),
17220eae32dcSDimitry Andric           /*LineNo=*/0, DB.createUnspecifiedType("Profile Data Type"),
17230eae32dcSDimitry Andric           CounterPtr->hasLocalLinkage(), /*IsDefined=*/true, /*Expr=*/nullptr,
17240eae32dcSDimitry Andric           /*Decl=*/nullptr, /*TemplateParams=*/nullptr, /*AlignInBits=*/0,
17250eae32dcSDimitry Andric           Annotations);
17260eae32dcSDimitry Andric       CounterPtr->addDebugInfo(DICounter);
17270eae32dcSDimitry Andric       DB.finalize();
17280eae32dcSDimitry Andric     }
17290b57cec5SDimitry Andric 
17305f757f3fSDimitry Andric     // Mark the counter variable as used so that it isn't optimized out.
17315f757f3fSDimitry Andric     CompilerUsedVars.push_back(PD.RegionCounters);
17325f757f3fSDimitry Andric   }
17335f757f3fSDimitry Andric 
17345f757f3fSDimitry Andric   // Create the data variable (if it doesn't already exist).
17355f757f3fSDimitry Andric   createDataVariable(Inc);
17365f757f3fSDimitry Andric 
17375f757f3fSDimitry Andric   return PD.RegionCounters;
17385f757f3fSDimitry Andric }
17395f757f3fSDimitry Andric 
createDataVariable(InstrProfCntrInstBase * Inc)17405f757f3fSDimitry Andric void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) {
17415f757f3fSDimitry Andric   // When debug information is correlated to profile data, a data variable
17425f757f3fSDimitry Andric   // is not needed.
17435f757f3fSDimitry Andric   if (DebugInfoCorrelate || ProfileCorrelate == InstrProfCorrelator::DEBUG_INFO)
17445f757f3fSDimitry Andric     return;
17455f757f3fSDimitry Andric 
17465f757f3fSDimitry Andric   GlobalVariable *NamePtr = Inc->getName();
17475f757f3fSDimitry Andric   auto &PD = ProfileDataMap[NamePtr];
17485f757f3fSDimitry Andric 
17495f757f3fSDimitry Andric   // Return if data variable was already created.
17505f757f3fSDimitry Andric   if (PD.DataVar)
17515f757f3fSDimitry Andric     return;
17525f757f3fSDimitry Andric 
17535f757f3fSDimitry Andric   LLVMContext &Ctx = M.getContext();
17545f757f3fSDimitry Andric 
17555f757f3fSDimitry Andric   Function *Fn = Inc->getParent()->getParent();
17565f757f3fSDimitry Andric   GlobalValue::LinkageTypes Linkage = NamePtr->getLinkage();
17575f757f3fSDimitry Andric   GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
17585f757f3fSDimitry Andric 
17595f757f3fSDimitry Andric   // Due to the limitation of binder as of 2021/09/28, the duplicate weak
17605f757f3fSDimitry Andric   // symbols in the same csect won't be discarded. When there are duplicate weak
17615f757f3fSDimitry Andric   // symbols, we can NOT guarantee that the relocations get resolved to the
17625f757f3fSDimitry Andric   // intended weak symbol, so we can not ensure the correctness of the relative
17635f757f3fSDimitry Andric   // CounterPtr, so we have to use private linkage for counter and data symbols.
17645f757f3fSDimitry Andric   if (TT.isOSBinFormatXCOFF()) {
17655f757f3fSDimitry Andric     Linkage = GlobalValue::PrivateLinkage;
17665f757f3fSDimitry Andric     Visibility = GlobalValue::DefaultVisibility;
17675f757f3fSDimitry Andric   }
17685f757f3fSDimitry Andric 
17695f757f3fSDimitry Andric   bool NeedComdat = needsComdatForCounter(*Fn, M);
17705f757f3fSDimitry Andric   bool Renamed;
17715f757f3fSDimitry Andric 
17725f757f3fSDimitry Andric   // The Data Variable section is anchored to profile counters.
17735f757f3fSDimitry Andric   std::string CntsVarName =
17745f757f3fSDimitry Andric       getVarName(Inc, getInstrProfCountersVarPrefix(), Renamed);
17755f757f3fSDimitry Andric   std::string DataVarName =
17765f757f3fSDimitry Andric       getVarName(Inc, getInstrProfDataVarPrefix(), Renamed);
17775f757f3fSDimitry Andric 
17785f757f3fSDimitry Andric   auto *Int8PtrTy = PointerType::getUnqual(Ctx);
17790b57cec5SDimitry Andric   // Allocate statically the array of pointers to value profile nodes for
17800b57cec5SDimitry Andric   // the current function.
17810b57cec5SDimitry Andric   Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy);
17820b57cec5SDimitry Andric   uint64_t NS = 0;
17830b57cec5SDimitry Andric   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
17840b57cec5SDimitry Andric     NS += PD.NumValueSites[Kind];
1785fe6060f1SDimitry Andric   if (NS > 0 && ValueProfileStaticAlloc &&
1786fe6060f1SDimitry Andric       !needsRuntimeRegistrationOfSectionRange(TT)) {
17870b57cec5SDimitry Andric     ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS);
1788fe6060f1SDimitry Andric     auto *ValuesVar = new GlobalVariable(
17895f757f3fSDimitry Andric         M, ValuesTy, false, Linkage, Constant::getNullValue(ValuesTy),
1790349cc55cSDimitry Andric         getVarName(Inc, getInstrProfValuesVarPrefix(), Renamed));
17910b57cec5SDimitry Andric     ValuesVar->setVisibility(Visibility);
17925f757f3fSDimitry Andric     setGlobalVariableLargeSection(TT, *ValuesVar);
17930b57cec5SDimitry Andric     ValuesVar->setSection(
17940b57cec5SDimitry Andric         getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
17958bcb0991SDimitry Andric     ValuesVar->setAlignment(Align(8));
17965f757f3fSDimitry Andric     maybeSetComdat(ValuesVar, Fn, CntsVarName);
17975f757f3fSDimitry Andric     ValuesPtrExpr = ValuesVar;
17980b57cec5SDimitry Andric   }
17990b57cec5SDimitry Andric 
18005f757f3fSDimitry Andric   uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
18015f757f3fSDimitry Andric   auto *CounterPtr = PD.RegionCounters;
18025f757f3fSDimitry Andric 
18035f757f3fSDimitry Andric   uint64_t NumBitmapBytes = PD.NumBitmapBytes;
18040eae32dcSDimitry Andric 
18050b57cec5SDimitry Andric   // Create data variable.
18065f757f3fSDimitry Andric   auto *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext());
18070b57cec5SDimitry Andric   auto *Int16Ty = Type::getInt16Ty(Ctx);
18080b57cec5SDimitry Andric   auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1);
18090b57cec5SDimitry Andric   Type *DataTypes[] = {
18100b57cec5SDimitry Andric #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType,
18110b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProfData.inc"
18120b57cec5SDimitry Andric   };
1813bdd1243dSDimitry Andric   auto *DataTy = StructType::get(Ctx, ArrayRef(DataTypes));
18140b57cec5SDimitry Andric 
181506c3fb27SDimitry Andric   Constant *FunctionAddr = getFuncAddrForProfData(Fn);
18160b57cec5SDimitry Andric 
18170b57cec5SDimitry Andric   Constant *Int16ArrayVals[IPVK_Last + 1];
18180b57cec5SDimitry Andric   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
18190b57cec5SDimitry Andric     Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]);
18200b57cec5SDimitry Andric 
1821fe6060f1SDimitry Andric   // If the data variable is not referenced by code (if we don't emit
1822fe6060f1SDimitry Andric   // @llvm.instrprof.value.profile, NS will be 0), and the counter keeps the
1823fe6060f1SDimitry Andric   // data variable live under linker GC, the data variable can be private. This
1824fe6060f1SDimitry Andric   // optimization applies to ELF.
1825fe6060f1SDimitry Andric   //
1826fe6060f1SDimitry Andric   // On COFF, a comdat leader cannot be local so we require DataReferencedByCode
1827fe6060f1SDimitry Andric   // to be false.
1828349cc55cSDimitry Andric   //
1829349cc55cSDimitry Andric   // If profd is in a deduplicate comdat, NS==0 with a hash suffix guarantees
1830349cc55cSDimitry Andric   // that other copies must have the same CFG and cannot have value profiling.
1831349cc55cSDimitry Andric   // If no hash suffix, other profd copies may be referenced by code.
1832349cc55cSDimitry Andric   if (NS == 0 && !(DataReferencedByCode && NeedComdat && !Renamed) &&
1833349cc55cSDimitry Andric       (TT.isOSBinFormatELF() ||
1834fe6060f1SDimitry Andric        (!DataReferencedByCode && TT.isOSBinFormatCOFF()))) {
1835fe6060f1SDimitry Andric     Linkage = GlobalValue::PrivateLinkage;
1836fe6060f1SDimitry Andric     Visibility = GlobalValue::DefaultVisibility;
1837fe6060f1SDimitry Andric   }
1838e8d8bef9SDimitry Andric   auto *Data =
18395f757f3fSDimitry Andric       new GlobalVariable(M, DataTy, false, Linkage, nullptr, DataVarName);
18405f757f3fSDimitry Andric   Constant *RelativeCounterPtr;
18415f757f3fSDimitry Andric   GlobalVariable *BitmapPtr = PD.RegionBitmaps;
18425f757f3fSDimitry Andric   Constant *RelativeBitmapPtr = ConstantInt::get(IntPtrTy, 0);
18435f757f3fSDimitry Andric   InstrProfSectKind DataSectionKind;
18445f757f3fSDimitry Andric   // With binary profile correlation, profile data is not loaded into memory.
18455f757f3fSDimitry Andric   // profile data must reference profile counter with an absolute relocation.
18465f757f3fSDimitry Andric   if (ProfileCorrelate == InstrProfCorrelator::BINARY) {
18475f757f3fSDimitry Andric     DataSectionKind = IPSK_covdata;
18485f757f3fSDimitry Andric     RelativeCounterPtr = ConstantExpr::getPtrToInt(CounterPtr, IntPtrTy);
18495f757f3fSDimitry Andric     if (BitmapPtr != nullptr)
18505f757f3fSDimitry Andric       RelativeBitmapPtr = ConstantExpr::getPtrToInt(BitmapPtr, IntPtrTy);
18515f757f3fSDimitry Andric   } else {
1852349cc55cSDimitry Andric     // Reference the counter variable with a label difference (link-time
1853349cc55cSDimitry Andric     // constant).
18545f757f3fSDimitry Andric     DataSectionKind = IPSK_data;
18555f757f3fSDimitry Andric     RelativeCounterPtr =
1856349cc55cSDimitry Andric         ConstantExpr::getSub(ConstantExpr::getPtrToInt(CounterPtr, IntPtrTy),
1857349cc55cSDimitry Andric                              ConstantExpr::getPtrToInt(Data, IntPtrTy));
18585f757f3fSDimitry Andric     if (BitmapPtr != nullptr)
18595f757f3fSDimitry Andric       RelativeBitmapPtr =
18605f757f3fSDimitry Andric           ConstantExpr::getSub(ConstantExpr::getPtrToInt(BitmapPtr, IntPtrTy),
18615f757f3fSDimitry Andric                                ConstantExpr::getPtrToInt(Data, IntPtrTy));
18625f757f3fSDimitry Andric   }
1863349cc55cSDimitry Andric 
1864349cc55cSDimitry Andric   Constant *DataVals[] = {
1865349cc55cSDimitry Andric #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init,
1866349cc55cSDimitry Andric #include "llvm/ProfileData/InstrProfData.inc"
1867349cc55cSDimitry Andric   };
1868349cc55cSDimitry Andric   Data->setInitializer(ConstantStruct::get(DataTy, DataVals));
1869349cc55cSDimitry Andric 
18700b57cec5SDimitry Andric   Data->setVisibility(Visibility);
18715f757f3fSDimitry Andric   Data->setSection(
18725f757f3fSDimitry Andric       getInstrProfSectionName(DataSectionKind, TT.getObjectFormat()));
18738bcb0991SDimitry Andric   Data->setAlignment(Align(INSTR_PROF_DATA_ALIGNMENT));
18745f757f3fSDimitry Andric   maybeSetComdat(Data, Fn, CntsVarName);
18750b57cec5SDimitry Andric 
18760b57cec5SDimitry Andric   PD.DataVar = Data;
18770b57cec5SDimitry Andric 
18780b57cec5SDimitry Andric   // Mark the data variable as used so that it isn't stripped out.
1879fe6060f1SDimitry Andric   CompilerUsedVars.push_back(Data);
18800b57cec5SDimitry Andric   // Now that the linkage set by the FE has been passed to the data and counter
18810b57cec5SDimitry Andric   // variables, reset Name variable's linkage and visibility to private so that
18820b57cec5SDimitry Andric   // it can be removed later by the compiler.
18830b57cec5SDimitry Andric   NamePtr->setLinkage(GlobalValue::PrivateLinkage);
18840b57cec5SDimitry Andric   // Collect the referenced names to be used by emitNameData.
18850b57cec5SDimitry Andric   ReferencedNames.push_back(NamePtr);
18860b57cec5SDimitry Andric }
18870b57cec5SDimitry Andric 
emitVNodes()18885f757f3fSDimitry Andric void InstrLowerer::emitVNodes() {
18890b57cec5SDimitry Andric   if (!ValueProfileStaticAlloc)
18900b57cec5SDimitry Andric     return;
18910b57cec5SDimitry Andric 
18920b57cec5SDimitry Andric   // For now only support this on platforms that do
18930b57cec5SDimitry Andric   // not require runtime registration to discover
18940b57cec5SDimitry Andric   // named section start/end.
18950b57cec5SDimitry Andric   if (needsRuntimeRegistrationOfSectionRange(TT))
18960b57cec5SDimitry Andric     return;
18970b57cec5SDimitry Andric 
18980b57cec5SDimitry Andric   size_t TotalNS = 0;
18990b57cec5SDimitry Andric   for (auto &PD : ProfileDataMap) {
19000b57cec5SDimitry Andric     for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
19010b57cec5SDimitry Andric       TotalNS += PD.second.NumValueSites[Kind];
19020b57cec5SDimitry Andric   }
19030b57cec5SDimitry Andric 
19040b57cec5SDimitry Andric   if (!TotalNS)
19050b57cec5SDimitry Andric     return;
19060b57cec5SDimitry Andric 
19070b57cec5SDimitry Andric   uint64_t NumCounters = TotalNS * NumCountersPerValueSite;
19080b57cec5SDimitry Andric // Heuristic for small programs with very few total value sites.
19090b57cec5SDimitry Andric // The default value of vp-counters-per-site is chosen based on
19100b57cec5SDimitry Andric // the observation that large apps usually have a low percentage
19110b57cec5SDimitry Andric // of value sites that actually have any profile data, and thus
19120b57cec5SDimitry Andric // the average number of counters per site is low. For small
19130b57cec5SDimitry Andric // apps with very few sites, this may not be true. Bump up the
19140b57cec5SDimitry Andric // number of counters in this case.
19150b57cec5SDimitry Andric #define INSTR_PROF_MIN_VAL_COUNTS 10
19160b57cec5SDimitry Andric   if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS)
19170b57cec5SDimitry Andric     NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS, (int)NumCounters * 2);
19180b57cec5SDimitry Andric 
19195f757f3fSDimitry Andric   auto &Ctx = M.getContext();
19200b57cec5SDimitry Andric   Type *VNodeTypes[] = {
19210b57cec5SDimitry Andric #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType,
19220b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProfData.inc"
19230b57cec5SDimitry Andric   };
1924bdd1243dSDimitry Andric   auto *VNodeTy = StructType::get(Ctx, ArrayRef(VNodeTypes));
19250b57cec5SDimitry Andric 
19260b57cec5SDimitry Andric   ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters);
19270b57cec5SDimitry Andric   auto *VNodesVar = new GlobalVariable(
19285f757f3fSDimitry Andric       M, VNodesTy, false, GlobalValue::PrivateLinkage,
19290b57cec5SDimitry Andric       Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName());
19305f757f3fSDimitry Andric   setGlobalVariableLargeSection(TT, *VNodesVar);
19310b57cec5SDimitry Andric   VNodesVar->setSection(
19320b57cec5SDimitry Andric       getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
19335f757f3fSDimitry Andric   VNodesVar->setAlignment(M.getDataLayout().getABITypeAlign(VNodesTy));
1934fe6060f1SDimitry Andric   // VNodesVar is used by runtime but not referenced via relocation by other
1935fe6060f1SDimitry Andric   // sections. Conservatively make it linker retained.
19360b57cec5SDimitry Andric   UsedVars.push_back(VNodesVar);
19370b57cec5SDimitry Andric }
19380b57cec5SDimitry Andric 
emitNameData()19395f757f3fSDimitry Andric void InstrLowerer::emitNameData() {
19400b57cec5SDimitry Andric   std::string UncompressedData;
19410b57cec5SDimitry Andric 
19420b57cec5SDimitry Andric   if (ReferencedNames.empty())
19430b57cec5SDimitry Andric     return;
19440b57cec5SDimitry Andric 
19450b57cec5SDimitry Andric   std::string CompressedNameStr;
19460b57cec5SDimitry Andric   if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
19475ffd83dbSDimitry Andric                                           DoInstrProfNameCompression)) {
1948349cc55cSDimitry Andric     report_fatal_error(Twine(toString(std::move(E))), false);
19490b57cec5SDimitry Andric   }
19500b57cec5SDimitry Andric 
19515f757f3fSDimitry Andric   auto &Ctx = M.getContext();
19524824e7fdSDimitry Andric   auto *NamesVal =
19534824e7fdSDimitry Andric       ConstantDataArray::getString(Ctx, StringRef(CompressedNameStr), false);
19545f757f3fSDimitry Andric   NamesVar = new GlobalVariable(M, NamesVal->getType(), true,
19550b57cec5SDimitry Andric                                 GlobalValue::PrivateLinkage, NamesVal,
19560b57cec5SDimitry Andric                                 getInstrProfNamesVarName());
19570b57cec5SDimitry Andric   NamesSize = CompressedNameStr.size();
19585f757f3fSDimitry Andric   setGlobalVariableLargeSection(TT, *NamesVar);
19590b57cec5SDimitry Andric   NamesVar->setSection(
19605f757f3fSDimitry Andric       ProfileCorrelate == InstrProfCorrelator::BINARY
19615f757f3fSDimitry Andric           ? getInstrProfSectionName(IPSK_covname, TT.getObjectFormat())
19625f757f3fSDimitry Andric           : getInstrProfSectionName(IPSK_name, TT.getObjectFormat()));
19630b57cec5SDimitry Andric   // On COFF, it's important to reduce the alignment down to 1 to prevent the
19640b57cec5SDimitry Andric   // linker from inserting padding before the start of the names section or
19650b57cec5SDimitry Andric   // between names entries.
19665ffd83dbSDimitry Andric   NamesVar->setAlignment(Align(1));
1967fe6060f1SDimitry Andric   // NamesVar is used by runtime but not referenced via relocation by other
1968fe6060f1SDimitry Andric   // sections. Conservatively make it linker retained.
19690b57cec5SDimitry Andric   UsedVars.push_back(NamesVar);
19700b57cec5SDimitry Andric 
19710b57cec5SDimitry Andric   for (auto *NamePtr : ReferencedNames)
19720b57cec5SDimitry Andric     NamePtr->eraseFromParent();
19730b57cec5SDimitry Andric }
19740b57cec5SDimitry Andric 
emitVTableNames()1975*0fca6ea1SDimitry Andric void InstrLowerer::emitVTableNames() {
1976*0fca6ea1SDimitry Andric   if (!EnableVTableValueProfiling || ReferencedVTables.empty())
1977*0fca6ea1SDimitry Andric     return;
1978*0fca6ea1SDimitry Andric 
1979*0fca6ea1SDimitry Andric   // Collect the PGO names of referenced vtables and compress them.
1980*0fca6ea1SDimitry Andric   std::string CompressedVTableNames;
1981*0fca6ea1SDimitry Andric   if (Error E = collectVTableStrings(ReferencedVTables, CompressedVTableNames,
1982*0fca6ea1SDimitry Andric                                      DoInstrProfNameCompression)) {
1983*0fca6ea1SDimitry Andric     report_fatal_error(Twine(toString(std::move(E))), false);
1984*0fca6ea1SDimitry Andric   }
1985*0fca6ea1SDimitry Andric 
1986*0fca6ea1SDimitry Andric   auto &Ctx = M.getContext();
1987*0fca6ea1SDimitry Andric   auto *VTableNamesVal = ConstantDataArray::getString(
1988*0fca6ea1SDimitry Andric       Ctx, StringRef(CompressedVTableNames), false /* AddNull */);
1989*0fca6ea1SDimitry Andric   GlobalVariable *VTableNamesVar =
1990*0fca6ea1SDimitry Andric       new GlobalVariable(M, VTableNamesVal->getType(), true /* constant */,
1991*0fca6ea1SDimitry Andric                          GlobalValue::PrivateLinkage, VTableNamesVal,
1992*0fca6ea1SDimitry Andric                          getInstrProfVTableNamesVarName());
1993*0fca6ea1SDimitry Andric   VTableNamesVar->setSection(
1994*0fca6ea1SDimitry Andric       getInstrProfSectionName(IPSK_vname, TT.getObjectFormat()));
1995*0fca6ea1SDimitry Andric   VTableNamesVar->setAlignment(Align(1));
1996*0fca6ea1SDimitry Andric   // Make VTableNames linker retained.
1997*0fca6ea1SDimitry Andric   UsedVars.push_back(VTableNamesVar);
1998*0fca6ea1SDimitry Andric }
1999*0fca6ea1SDimitry Andric 
emitRegistration()20005f757f3fSDimitry Andric void InstrLowerer::emitRegistration() {
20010b57cec5SDimitry Andric   if (!needsRuntimeRegistrationOfSectionRange(TT))
20020b57cec5SDimitry Andric     return;
20030b57cec5SDimitry Andric 
20040b57cec5SDimitry Andric   // Construct the function.
20055f757f3fSDimitry Andric   auto *VoidTy = Type::getVoidTy(M.getContext());
20065f757f3fSDimitry Andric   auto *VoidPtrTy = PointerType::getUnqual(M.getContext());
20075f757f3fSDimitry Andric   auto *Int64Ty = Type::getInt64Ty(M.getContext());
20080b57cec5SDimitry Andric   auto *RegisterFTy = FunctionType::get(VoidTy, false);
20090b57cec5SDimitry Andric   auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage,
20100b57cec5SDimitry Andric                                      getInstrProfRegFuncsName(), M);
20110b57cec5SDimitry Andric   RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
20120b57cec5SDimitry Andric   if (Options.NoRedZone)
20130b57cec5SDimitry Andric     RegisterF->addFnAttr(Attribute::NoRedZone);
20140b57cec5SDimitry Andric 
20150b57cec5SDimitry Andric   auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false);
20160b57cec5SDimitry Andric   auto *RuntimeRegisterF =
20170b57cec5SDimitry Andric       Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage,
20180b57cec5SDimitry Andric                        getInstrProfRegFuncName(), M);
20190b57cec5SDimitry Andric 
20205f757f3fSDimitry Andric   IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", RegisterF));
2021fe6060f1SDimitry Andric   for (Value *Data : CompilerUsedVars)
2022fe6060f1SDimitry Andric     if (!isa<Function>(Data))
20235f757f3fSDimitry Andric       IRB.CreateCall(RuntimeRegisterF, Data);
20240b57cec5SDimitry Andric   for (Value *Data : UsedVars)
20250b57cec5SDimitry Andric     if (Data != NamesVar && !isa<Function>(Data))
20265f757f3fSDimitry Andric       IRB.CreateCall(RuntimeRegisterF, Data);
20270b57cec5SDimitry Andric 
20280b57cec5SDimitry Andric   if (NamesVar) {
20290b57cec5SDimitry Andric     Type *ParamTypes[] = {VoidPtrTy, Int64Ty};
20300b57cec5SDimitry Andric     auto *NamesRegisterTy =
2031bdd1243dSDimitry Andric         FunctionType::get(VoidTy, ArrayRef(ParamTypes), false);
20320b57cec5SDimitry Andric     auto *NamesRegisterF =
20330b57cec5SDimitry Andric         Function::Create(NamesRegisterTy, GlobalVariable::ExternalLinkage,
20340b57cec5SDimitry Andric                          getInstrProfNamesRegFuncName(), M);
20355f757f3fSDimitry Andric     IRB.CreateCall(NamesRegisterF, {NamesVar, IRB.getInt64(NamesSize)});
20360b57cec5SDimitry Andric   }
20370b57cec5SDimitry Andric 
20380b57cec5SDimitry Andric   IRB.CreateRetVoid();
20390b57cec5SDimitry Andric }
20400b57cec5SDimitry Andric 
emitRuntimeHook()20415f757f3fSDimitry Andric bool InstrLowerer::emitRuntimeHook() {
2042349cc55cSDimitry Andric   // We expect the linker to be invoked with -u<hook_var> flag for Linux
2043349cc55cSDimitry Andric   // in which case there is no need to emit the external variable.
2044bdd1243dSDimitry Andric   if (TT.isOSLinux() || TT.isOSAIX())
20450b57cec5SDimitry Andric     return false;
20460b57cec5SDimitry Andric 
20470b57cec5SDimitry Andric   // If the module's provided its own runtime, we don't need to do anything.
20485f757f3fSDimitry Andric   if (M.getGlobalVariable(getInstrProfRuntimeHookVarName()))
20490b57cec5SDimitry Andric     return false;
20500b57cec5SDimitry Andric 
20510b57cec5SDimitry Andric   // Declare an external variable that will pull in the runtime initialization.
20525f757f3fSDimitry Andric   auto *Int32Ty = Type::getInt32Ty(M.getContext());
20530b57cec5SDimitry Andric   auto *Var =
20545f757f3fSDimitry Andric       new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage,
20550b57cec5SDimitry Andric                          nullptr, getInstrProfRuntimeHookVarName());
2056753f127fSDimitry Andric   Var->setVisibility(GlobalValue::HiddenVisibility);
20570b57cec5SDimitry Andric 
205881ad6265SDimitry Andric   if (TT.isOSBinFormatELF() && !TT.isPS()) {
2059349cc55cSDimitry Andric     // Mark the user variable as used so that it isn't stripped out.
2060349cc55cSDimitry Andric     CompilerUsedVars.push_back(Var);
2061349cc55cSDimitry Andric   } else {
20620b57cec5SDimitry Andric     // Make a function that uses it.
20630b57cec5SDimitry Andric     auto *User = Function::Create(FunctionType::get(Int32Ty, false),
20640b57cec5SDimitry Andric                                   GlobalValue::LinkOnceODRLinkage,
20650b57cec5SDimitry Andric                                   getInstrProfRuntimeHookVarUseFuncName(), M);
20660b57cec5SDimitry Andric     User->addFnAttr(Attribute::NoInline);
20670b57cec5SDimitry Andric     if (Options.NoRedZone)
20680b57cec5SDimitry Andric       User->addFnAttr(Attribute::NoRedZone);
20690b57cec5SDimitry Andric     User->setVisibility(GlobalValue::HiddenVisibility);
20700b57cec5SDimitry Andric     if (TT.supportsCOMDAT())
20715f757f3fSDimitry Andric       User->setComdat(M.getOrInsertComdat(User->getName()));
20720b57cec5SDimitry Andric 
20735f757f3fSDimitry Andric     IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", User));
20740b57cec5SDimitry Andric     auto *Load = IRB.CreateLoad(Int32Ty, Var);
20750b57cec5SDimitry Andric     IRB.CreateRet(Load);
20760b57cec5SDimitry Andric 
2077349cc55cSDimitry Andric     // Mark the function as used so that it isn't stripped out.
2078fe6060f1SDimitry Andric     CompilerUsedVars.push_back(User);
2079349cc55cSDimitry Andric   }
20800b57cec5SDimitry Andric   return true;
20810b57cec5SDimitry Andric }
20820b57cec5SDimitry Andric 
emitUses()20835f757f3fSDimitry Andric void InstrLowerer::emitUses() {
2084fe6060f1SDimitry Andric   // The metadata sections are parallel arrays. Optimizers (e.g.
2085fe6060f1SDimitry Andric   // GlobalOpt/ConstantMerge) may not discard associated sections as a unit, so
2086fe6060f1SDimitry Andric   // we conservatively retain all unconditionally in the compiler.
2087fe6060f1SDimitry Andric   //
2088349cc55cSDimitry Andric   // On ELF and Mach-O, the linker can guarantee the associated sections will be
2089349cc55cSDimitry Andric   // retained or discarded as a unit, so llvm.compiler.used is sufficient.
2090349cc55cSDimitry Andric   // Similarly on COFF, if prof data is not referenced by code we use one comdat
2091349cc55cSDimitry Andric   // and ensure this GC property as well. Otherwise, we have to conservatively
2092349cc55cSDimitry Andric   // make all of the sections retained by the linker.
2093349cc55cSDimitry Andric   if (TT.isOSBinFormatELF() || TT.isOSBinFormatMachO() ||
2094*0fca6ea1SDimitry Andric       (TT.isOSBinFormatCOFF() && !DataReferencedByCode))
20955f757f3fSDimitry Andric     appendToCompilerUsed(M, CompilerUsedVars);
2096fe6060f1SDimitry Andric   else
20975f757f3fSDimitry Andric     appendToUsed(M, CompilerUsedVars);
2098fe6060f1SDimitry Andric 
2099fe6060f1SDimitry Andric   // We do not add proper references from used metadata sections to NamesVar and
2100fe6060f1SDimitry Andric   // VNodesVar, so we have to be conservative and place them in llvm.used
2101fe6060f1SDimitry Andric   // regardless of the target,
21025f757f3fSDimitry Andric   appendToUsed(M, UsedVars);
21030b57cec5SDimitry Andric }
21040b57cec5SDimitry Andric 
emitInitialization()21055f757f3fSDimitry Andric void InstrLowerer::emitInitialization() {
21060b57cec5SDimitry Andric   // Create ProfileFileName variable. Don't don't this for the
21070b57cec5SDimitry Andric   // context-sensitive instrumentation lowering: This lowering is after
21080b57cec5SDimitry Andric   // LTO/ThinLTO linking. Pass PGOInstrumentationGenCreateVar should
21090b57cec5SDimitry Andric   // have already create the variable before LTO/ThinLTO linking.
21100b57cec5SDimitry Andric   if (!IsCS)
21115f757f3fSDimitry Andric     createProfileFileNameVar(M, Options.InstrProfileOutput);
21125f757f3fSDimitry Andric   Function *RegisterF = M.getFunction(getInstrProfRegFuncsName());
21130b57cec5SDimitry Andric   if (!RegisterF)
21140b57cec5SDimitry Andric     return;
21150b57cec5SDimitry Andric 
21160b57cec5SDimitry Andric   // Create the initialization function.
21175f757f3fSDimitry Andric   auto *VoidTy = Type::getVoidTy(M.getContext());
21180b57cec5SDimitry Andric   auto *F = Function::Create(FunctionType::get(VoidTy, false),
21190b57cec5SDimitry Andric                              GlobalValue::InternalLinkage,
21200b57cec5SDimitry Andric                              getInstrProfInitFuncName(), M);
21210b57cec5SDimitry Andric   F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
21220b57cec5SDimitry Andric   F->addFnAttr(Attribute::NoInline);
21230b57cec5SDimitry Andric   if (Options.NoRedZone)
21240b57cec5SDimitry Andric     F->addFnAttr(Attribute::NoRedZone);
21250b57cec5SDimitry Andric 
21260b57cec5SDimitry Andric   // Add the basic block and the necessary calls.
21275f757f3fSDimitry Andric   IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", F));
21280b57cec5SDimitry Andric   IRB.CreateCall(RegisterF, {});
21290b57cec5SDimitry Andric   IRB.CreateRetVoid();
21300b57cec5SDimitry Andric 
21315f757f3fSDimitry Andric   appendToGlobalCtors(M, F, 0);
21320b57cec5SDimitry Andric }
2133*0fca6ea1SDimitry Andric 
2134*0fca6ea1SDimitry Andric namespace llvm {
2135*0fca6ea1SDimitry Andric // Create the variable for profile sampling.
createProfileSamplingVar(Module & M)2136*0fca6ea1SDimitry Andric void createProfileSamplingVar(Module &M) {
2137*0fca6ea1SDimitry Andric   const StringRef VarName(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_SAMPLING_VAR));
2138*0fca6ea1SDimitry Andric   IntegerType *SamplingVarTy;
2139*0fca6ea1SDimitry Andric   Constant *ValueZero;
2140*0fca6ea1SDimitry Andric   if (SampledInstrPeriod.getValue() <= USHRT_MAX) {
2141*0fca6ea1SDimitry Andric     SamplingVarTy = Type::getInt16Ty(M.getContext());
2142*0fca6ea1SDimitry Andric     ValueZero = Constant::getIntegerValue(SamplingVarTy, APInt(16, 0));
2143*0fca6ea1SDimitry Andric   } else {
2144*0fca6ea1SDimitry Andric     SamplingVarTy = Type::getInt32Ty(M.getContext());
2145*0fca6ea1SDimitry Andric     ValueZero = Constant::getIntegerValue(SamplingVarTy, APInt(32, 0));
2146*0fca6ea1SDimitry Andric   }
2147*0fca6ea1SDimitry Andric   auto SamplingVar = new GlobalVariable(
2148*0fca6ea1SDimitry Andric       M, SamplingVarTy, false, GlobalValue::WeakAnyLinkage, ValueZero, VarName);
2149*0fca6ea1SDimitry Andric   SamplingVar->setVisibility(GlobalValue::DefaultVisibility);
2150*0fca6ea1SDimitry Andric   SamplingVar->setThreadLocal(true);
2151*0fca6ea1SDimitry Andric   Triple TT(M.getTargetTriple());
2152*0fca6ea1SDimitry Andric   if (TT.supportsCOMDAT()) {
2153*0fca6ea1SDimitry Andric     SamplingVar->setLinkage(GlobalValue::ExternalLinkage);
2154*0fca6ea1SDimitry Andric     SamplingVar->setComdat(M.getOrInsertComdat(VarName));
2155*0fca6ea1SDimitry Andric   }
2156*0fca6ea1SDimitry Andric   appendToCompilerUsed(M, SamplingVar);
2157*0fca6ea1SDimitry Andric }
2158*0fca6ea1SDimitry Andric } // namespace llvm
2159