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 //
9c9157d92SDimitry 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"
17c9157d92SDimitry 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"
27c9157d92SDimitry 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"
410b57cec5SDimitry Andric #include "llvm/IR/Module.h"
420b57cec5SDimitry Andric #include "llvm/IR/Type.h"
43480093f4SDimitry Andric #include "llvm/InitializePasses.h"
440b57cec5SDimitry Andric #include "llvm/Pass.h"
450b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProf.h"
460eae32dcSDimitry Andric #include "llvm/ProfileData/InstrProfCorrelator.h"
470b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
480b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
490b57cec5SDimitry Andric #include "llvm/Support/Error.h"
500b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
51fe013be4SDimitry Andric #include "llvm/TargetParser/Triple.h"
52c9157d92SDimitry Andric #include "llvm/Transforms/Instrumentation.h"
53c9157d92SDimitry Andric #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
54c9157d92SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
550b57cec5SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
560b57cec5SDimitry Andric #include "llvm/Transforms/Utils/SSAUpdater.h"
570b57cec5SDimitry Andric #include <algorithm>
580b57cec5SDimitry Andric #include <cassert>
590b57cec5SDimitry Andric #include <cstdint>
600b57cec5SDimitry Andric #include <string>
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric using namespace llvm;
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric #define DEBUG_TYPE "instrprof"
650b57cec5SDimitry Andric
660eae32dcSDimitry Andric namespace llvm {
67c9157d92SDimitry Andric // TODO: Remove -debug-info-correlate in next LLVM release, in favor of
68c9157d92SDimitry Andric // -profile-correlate=debug-info.
69c9157d92SDimitry Andric cl::opt<bool> DebugInfoCorrelate(
70c9157d92SDimitry Andric "debug-info-correlate",
71c9157d92SDimitry Andric cl::desc("Use debug info to correlate profiles. (Deprecated, use "
72c9157d92SDimitry Andric "-profile-correlate=debug-info)"),
730eae32dcSDimitry Andric cl::init(false));
74c9157d92SDimitry Andric
75c9157d92SDimitry Andric cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate(
76c9157d92SDimitry Andric "profile-correlate",
77c9157d92SDimitry Andric cl::desc("Use debug info or binary file to correlate profiles."),
78c9157d92SDimitry Andric cl::init(InstrProfCorrelator::NONE),
79c9157d92SDimitry Andric cl::values(clEnumValN(InstrProfCorrelator::NONE, "",
80c9157d92SDimitry Andric "No profile correlation"),
81c9157d92SDimitry Andric clEnumValN(InstrProfCorrelator::DEBUG_INFO, "debug-info",
82c9157d92SDimitry Andric "Use debug info to correlate"),
83c9157d92SDimitry Andric clEnumValN(InstrProfCorrelator::BINARY, "binary",
84c9157d92SDimitry Andric "Use binary to correlate")));
850eae32dcSDimitry Andric } // namespace llvm
860eae32dcSDimitry Andric
870b57cec5SDimitry Andric namespace {
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric cl::opt<bool> DoHashBasedCounterSplit(
900b57cec5SDimitry Andric "hash-based-counter-split",
910b57cec5SDimitry Andric cl::desc("Rename counter variable of a comdat function based on cfg hash"),
920b57cec5SDimitry Andric cl::init(true));
930b57cec5SDimitry Andric
944824e7fdSDimitry Andric cl::opt<bool>
954824e7fdSDimitry Andric RuntimeCounterRelocation("runtime-counter-relocation",
965ffd83dbSDimitry Andric cl::desc("Enable relocating counters at runtime."),
975ffd83dbSDimitry Andric cl::init(false));
985ffd83dbSDimitry Andric
990b57cec5SDimitry Andric cl::opt<bool> ValueProfileStaticAlloc(
1000b57cec5SDimitry Andric "vp-static-alloc",
1010b57cec5SDimitry Andric cl::desc("Do static counter allocation for value profiler"),
1020b57cec5SDimitry Andric cl::init(true));
1030b57cec5SDimitry Andric
1040b57cec5SDimitry Andric cl::opt<double> NumCountersPerValueSite(
1050b57cec5SDimitry Andric "vp-counters-per-site",
1060b57cec5SDimitry Andric cl::desc("The average number of profile counters allocated "
1070b57cec5SDimitry Andric "per value profiling site."),
1080b57cec5SDimitry Andric // This is set to a very small value because in real programs, only
1090b57cec5SDimitry Andric // a very small percentage of value sites have non-zero targets, e.g, 1/30.
1100b57cec5SDimitry Andric // For those sites with non-zero profile, the average number of targets
1110b57cec5SDimitry Andric // is usually smaller than 2.
1120b57cec5SDimitry Andric cl::init(1.0));
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric cl::opt<bool> AtomicCounterUpdateAll(
11581ad6265SDimitry Andric "instrprof-atomic-counter-update-all",
1160b57cec5SDimitry Andric cl::desc("Make all profile counter updates atomic (for testing only)"),
1170b57cec5SDimitry Andric cl::init(false));
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric cl::opt<bool> AtomicCounterUpdatePromoted(
12081ad6265SDimitry Andric "atomic-counter-update-promoted",
1210b57cec5SDimitry Andric cl::desc("Do counter update using atomic fetch add "
1220b57cec5SDimitry Andric " for promoted counters only"),
1230b57cec5SDimitry Andric cl::init(false));
1240b57cec5SDimitry Andric
1255ffd83dbSDimitry Andric cl::opt<bool> AtomicFirstCounter(
12681ad6265SDimitry Andric "atomic-first-counter",
1275ffd83dbSDimitry Andric cl::desc("Use atomic fetch add for first counter in a function (usually "
1285ffd83dbSDimitry Andric "the entry counter)"),
1295ffd83dbSDimitry Andric cl::init(false));
1305ffd83dbSDimitry Andric
1310b57cec5SDimitry Andric // If the option is not specified, the default behavior about whether
1320b57cec5SDimitry Andric // counter promotion is done depends on how instrumentaiton lowering
1330b57cec5SDimitry Andric // pipeline is setup, i.e., the default value of true of this option
1340b57cec5SDimitry Andric // does not mean the promotion will be done by default. Explicitly
1350b57cec5SDimitry Andric // setting this option can override the default behavior.
13681ad6265SDimitry Andric cl::opt<bool> DoCounterPromotion("do-counter-promotion",
1370b57cec5SDimitry Andric cl::desc("Do counter register promotion"),
1380b57cec5SDimitry Andric cl::init(false));
1390b57cec5SDimitry Andric cl::opt<unsigned> MaxNumOfPromotionsPerLoop(
14081ad6265SDimitry Andric "max-counter-promotions-per-loop", cl::init(20),
1410b57cec5SDimitry Andric cl::desc("Max number counter promotions per loop to avoid"
1420b57cec5SDimitry Andric " increasing register pressure too much"));
1430b57cec5SDimitry Andric
1440b57cec5SDimitry Andric // A debug option
1450b57cec5SDimitry Andric cl::opt<int>
14681ad6265SDimitry Andric MaxNumOfPromotions("max-counter-promotions", cl::init(-1),
1470b57cec5SDimitry Andric cl::desc("Max number of allowed counter promotions"));
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric cl::opt<unsigned> SpeculativeCounterPromotionMaxExiting(
15081ad6265SDimitry Andric "speculative-counter-promotion-max-exiting", cl::init(3),
1510b57cec5SDimitry Andric cl::desc("The max number of exiting blocks of a loop to allow "
1520b57cec5SDimitry Andric " speculative counter promotion"));
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric cl::opt<bool> SpeculativeCounterPromotionToLoop(
15581ad6265SDimitry Andric "speculative-counter-promotion-to-loop",
1560b57cec5SDimitry Andric cl::desc("When the option is false, if the target block is in a loop, "
1570b57cec5SDimitry Andric "the promotion will be disallowed unless the promoted counter "
1580b57cec5SDimitry Andric " update can be further/iteratively promoted into an acyclic "
1590b57cec5SDimitry Andric " region."));
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric cl::opt<bool> IterativeCounterPromotion(
16281ad6265SDimitry Andric "iterative-counter-promotion", cl::init(true),
1630b57cec5SDimitry Andric cl::desc("Allow counter promotion across the whole loop nest."));
1640b57cec5SDimitry Andric
165e8d8bef9SDimitry Andric cl::opt<bool> SkipRetExitBlock(
16681ad6265SDimitry Andric "skip-ret-exit-block", cl::init(true),
167e8d8bef9SDimitry Andric cl::desc("Suppress counter promotion if exit blocks contain ret."));
168e8d8bef9SDimitry Andric
169c9157d92SDimitry Andric using LoadStorePair = std::pair<Instruction *, Instruction *>;
170c9157d92SDimitry Andric
171c9157d92SDimitry Andric class InstrLowerer final {
172c9157d92SDimitry Andric public:
InstrLowerer(Module & M,const InstrProfOptions & Options,std::function<const TargetLibraryInfo & (Function & F)> GetTLI,bool IsCS)173c9157d92SDimitry Andric InstrLowerer(Module &M, const InstrProfOptions &Options,
174c9157d92SDimitry Andric std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
175c9157d92SDimitry Andric bool IsCS)
176c9157d92SDimitry Andric : M(M), Options(Options), TT(Triple(M.getTargetTriple())), IsCS(IsCS),
177c9157d92SDimitry Andric GetTLI(GetTLI) {}
178c9157d92SDimitry Andric
179c9157d92SDimitry Andric bool lower();
180c9157d92SDimitry Andric
181c9157d92SDimitry Andric private:
182c9157d92SDimitry Andric Module &M;
183c9157d92SDimitry Andric const InstrProfOptions Options;
184c9157d92SDimitry Andric const Triple TT;
185c9157d92SDimitry Andric // Is this lowering for the context-sensitive instrumentation.
186c9157d92SDimitry Andric const bool IsCS;
187c9157d92SDimitry Andric
188c9157d92SDimitry Andric std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
189c9157d92SDimitry Andric struct PerFunctionProfileData {
190c9157d92SDimitry Andric uint32_t NumValueSites[IPVK_Last + 1] = {};
191c9157d92SDimitry Andric GlobalVariable *RegionCounters = nullptr;
192c9157d92SDimitry Andric GlobalVariable *DataVar = nullptr;
193c9157d92SDimitry Andric GlobalVariable *RegionBitmaps = nullptr;
194c9157d92SDimitry Andric uint32_t NumBitmapBytes = 0;
195c9157d92SDimitry Andric
196c9157d92SDimitry Andric PerFunctionProfileData() = default;
197c9157d92SDimitry Andric };
198c9157d92SDimitry Andric DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
199c9157d92SDimitry Andric /// If runtime relocation is enabled, this maps functions to the load
200c9157d92SDimitry Andric /// instruction that produces the profile relocation bias.
201c9157d92SDimitry Andric DenseMap<const Function *, LoadInst *> FunctionToProfileBiasMap;
202c9157d92SDimitry Andric std::vector<GlobalValue *> CompilerUsedVars;
203c9157d92SDimitry Andric std::vector<GlobalValue *> UsedVars;
204c9157d92SDimitry Andric std::vector<GlobalVariable *> ReferencedNames;
205c9157d92SDimitry Andric GlobalVariable *NamesVar = nullptr;
206c9157d92SDimitry Andric size_t NamesSize = 0;
207c9157d92SDimitry Andric
208c9157d92SDimitry Andric // vector of counter load/store pairs to be register promoted.
209c9157d92SDimitry Andric std::vector<LoadStorePair> PromotionCandidates;
210c9157d92SDimitry Andric
211c9157d92SDimitry Andric int64_t TotalCountersPromoted = 0;
212c9157d92SDimitry Andric
213c9157d92SDimitry Andric /// Lower instrumentation intrinsics in the function. Returns true if there
214c9157d92SDimitry Andric /// any lowering.
215c9157d92SDimitry Andric bool lowerIntrinsics(Function *F);
216c9157d92SDimitry Andric
217c9157d92SDimitry Andric /// Register-promote counter loads and stores in loops.
218c9157d92SDimitry Andric void promoteCounterLoadStores(Function *F);
219c9157d92SDimitry Andric
220c9157d92SDimitry Andric /// Returns true if relocating counters at runtime is enabled.
221c9157d92SDimitry Andric bool isRuntimeCounterRelocationEnabled() const;
222c9157d92SDimitry Andric
223c9157d92SDimitry Andric /// Returns true if profile counter update register promotion is enabled.
224c9157d92SDimitry Andric bool isCounterPromotionEnabled() const;
225c9157d92SDimitry Andric
226c9157d92SDimitry Andric /// Count the number of instrumented value sites for the function.
227c9157d92SDimitry Andric void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
228c9157d92SDimitry Andric
229c9157d92SDimitry Andric /// Replace instrprof.value.profile with a call to runtime library.
230c9157d92SDimitry Andric void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
231c9157d92SDimitry Andric
232c9157d92SDimitry Andric /// Replace instrprof.cover with a store instruction to the coverage byte.
233c9157d92SDimitry Andric void lowerCover(InstrProfCoverInst *Inc);
234c9157d92SDimitry Andric
235c9157d92SDimitry Andric /// Replace instrprof.timestamp with a call to
236c9157d92SDimitry Andric /// INSTR_PROF_PROFILE_SET_TIMESTAMP.
237c9157d92SDimitry Andric void lowerTimestamp(InstrProfTimestampInst *TimestampInstruction);
238c9157d92SDimitry Andric
239c9157d92SDimitry Andric /// Replace instrprof.increment with an increment of the appropriate value.
240c9157d92SDimitry Andric void lowerIncrement(InstrProfIncrementInst *Inc);
241c9157d92SDimitry Andric
242c9157d92SDimitry Andric /// Force emitting of name vars for unused functions.
243c9157d92SDimitry Andric void lowerCoverageData(GlobalVariable *CoverageNamesVar);
244c9157d92SDimitry Andric
245c9157d92SDimitry Andric /// Replace instrprof.mcdc.tvbitmask.update with a shift and or instruction
246c9157d92SDimitry Andric /// using the index represented by the a temp value into a bitmap.
247c9157d92SDimitry Andric void lowerMCDCTestVectorBitmapUpdate(InstrProfMCDCTVBitmapUpdate *Ins);
248c9157d92SDimitry Andric
249c9157d92SDimitry Andric /// Replace instrprof.mcdc.temp.update with a shift and or instruction using
250c9157d92SDimitry Andric /// the corresponding condition ID.
251c9157d92SDimitry Andric void lowerMCDCCondBitmapUpdate(InstrProfMCDCCondBitmapUpdate *Ins);
252c9157d92SDimitry Andric
253c9157d92SDimitry Andric /// Compute the address of the counter value that this profiling instruction
254c9157d92SDimitry Andric /// acts on.
255c9157d92SDimitry Andric Value *getCounterAddress(InstrProfCntrInstBase *I);
256c9157d92SDimitry Andric
257c9157d92SDimitry Andric /// Get the region counters for an increment, creating them if necessary.
258c9157d92SDimitry Andric ///
259c9157d92SDimitry Andric /// If the counter array doesn't yet exist, the profile data variables
260c9157d92SDimitry Andric /// referring to them will also be created.
261c9157d92SDimitry Andric GlobalVariable *getOrCreateRegionCounters(InstrProfCntrInstBase *Inc);
262c9157d92SDimitry Andric
263c9157d92SDimitry Andric /// Create the region counters.
264c9157d92SDimitry Andric GlobalVariable *createRegionCounters(InstrProfCntrInstBase *Inc,
265c9157d92SDimitry Andric StringRef Name,
266c9157d92SDimitry Andric GlobalValue::LinkageTypes Linkage);
267c9157d92SDimitry Andric
268c9157d92SDimitry Andric /// Compute the address of the test vector bitmap that this profiling
269c9157d92SDimitry Andric /// instruction acts on.
270c9157d92SDimitry Andric Value *getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I);
271c9157d92SDimitry Andric
272c9157d92SDimitry Andric /// Get the region bitmaps for an increment, creating them if necessary.
273c9157d92SDimitry Andric ///
274c9157d92SDimitry Andric /// If the bitmap array doesn't yet exist, the profile data variables
275c9157d92SDimitry Andric /// referring to them will also be created.
276c9157d92SDimitry Andric GlobalVariable *getOrCreateRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc);
277c9157d92SDimitry Andric
278c9157d92SDimitry Andric /// Create the MC/DC bitmap as a byte-aligned array of bytes associated with
279c9157d92SDimitry Andric /// an MC/DC Decision region. The number of bytes required is indicated by
280c9157d92SDimitry Andric /// the intrinsic used (type InstrProfMCDCBitmapInstBase). This is called
281c9157d92SDimitry Andric /// as part of setupProfileSection() and is conceptually very similar to
282c9157d92SDimitry Andric /// what is done for profile data counters in createRegionCounters().
283c9157d92SDimitry Andric GlobalVariable *createRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc,
284c9157d92SDimitry Andric StringRef Name,
285c9157d92SDimitry Andric GlobalValue::LinkageTypes Linkage);
286c9157d92SDimitry Andric
287c9157d92SDimitry Andric /// Set Comdat property of GV, if required.
288c9157d92SDimitry Andric void maybeSetComdat(GlobalVariable *GV, Function *Fn, StringRef VarName);
289c9157d92SDimitry Andric
290c9157d92SDimitry Andric /// Setup the sections into which counters and bitmaps are allocated.
291c9157d92SDimitry Andric GlobalVariable *setupProfileSection(InstrProfInstBase *Inc,
292c9157d92SDimitry Andric InstrProfSectKind IPSK);
293c9157d92SDimitry Andric
294c9157d92SDimitry Andric /// Create INSTR_PROF_DATA variable for counters and bitmaps.
295c9157d92SDimitry Andric void createDataVariable(InstrProfCntrInstBase *Inc);
296c9157d92SDimitry Andric
297c9157d92SDimitry Andric /// Emit the section with compressed function names.
298c9157d92SDimitry Andric void emitNameData();
299c9157d92SDimitry Andric
300c9157d92SDimitry Andric /// Emit value nodes section for value profiling.
301c9157d92SDimitry Andric void emitVNodes();
302c9157d92SDimitry Andric
303c9157d92SDimitry Andric /// Emit runtime registration functions for each profile data variable.
304c9157d92SDimitry Andric void emitRegistration();
305c9157d92SDimitry Andric
306c9157d92SDimitry Andric /// Emit the necessary plumbing to pull in the runtime initialization.
307c9157d92SDimitry Andric /// Returns true if a change was made.
308c9157d92SDimitry Andric bool emitRuntimeHook();
309c9157d92SDimitry Andric
310c9157d92SDimitry Andric /// Add uses of our data variables and runtime hook.
311c9157d92SDimitry Andric void emitUses();
312c9157d92SDimitry Andric
313c9157d92SDimitry Andric /// Create a static initializer for our data, on platforms that need it,
314c9157d92SDimitry Andric /// and for any profile output file that was specified.
315c9157d92SDimitry Andric void emitInitialization();
316c9157d92SDimitry Andric };
317c9157d92SDimitry Andric
3180b57cec5SDimitry Andric ///
3190b57cec5SDimitry Andric /// A helper class to promote one counter RMW operation in the loop
3200b57cec5SDimitry Andric /// into register update.
3210b57cec5SDimitry Andric ///
3220b57cec5SDimitry Andric /// RWM update for the counter will be sinked out of the loop after
3230b57cec5SDimitry Andric /// the transformation.
3240b57cec5SDimitry Andric ///
3250b57cec5SDimitry Andric class PGOCounterPromoterHelper : public LoadAndStorePromoter {
3260b57cec5SDimitry 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)3270b57cec5SDimitry Andric PGOCounterPromoterHelper(
3280b57cec5SDimitry Andric Instruction *L, Instruction *S, SSAUpdater &SSA, Value *Init,
3290b57cec5SDimitry Andric BasicBlock *PH, ArrayRef<BasicBlock *> ExitBlocks,
3300b57cec5SDimitry Andric ArrayRef<Instruction *> InsertPts,
3310b57cec5SDimitry Andric DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands,
3320b57cec5SDimitry Andric LoopInfo &LI)
3330b57cec5SDimitry Andric : LoadAndStorePromoter({L, S}, SSA), Store(S), ExitBlocks(ExitBlocks),
3340b57cec5SDimitry Andric InsertPts(InsertPts), LoopToCandidates(LoopToCands), LI(LI) {
3350b57cec5SDimitry Andric assert(isa<LoadInst>(L));
3360b57cec5SDimitry Andric assert(isa<StoreInst>(S));
3370b57cec5SDimitry Andric SSA.AddAvailableValue(PH, Init);
3380b57cec5SDimitry Andric }
3390b57cec5SDimitry Andric
doExtraRewritesBeforeFinalDeletion()3400b57cec5SDimitry Andric void doExtraRewritesBeforeFinalDeletion() override {
3410b57cec5SDimitry Andric for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
3420b57cec5SDimitry Andric BasicBlock *ExitBlock = ExitBlocks[i];
3430b57cec5SDimitry Andric Instruction *InsertPos = InsertPts[i];
3440b57cec5SDimitry Andric // Get LiveIn value into the ExitBlock. If there are multiple
3450b57cec5SDimitry Andric // predecessors, the value is defined by a PHI node in this
3460b57cec5SDimitry Andric // block.
3470b57cec5SDimitry Andric Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
3480b57cec5SDimitry Andric Value *Addr = cast<StoreInst>(Store)->getPointerOperand();
3490b57cec5SDimitry Andric Type *Ty = LiveInValue->getType();
3500b57cec5SDimitry Andric IRBuilder<> Builder(InsertPos);
35181ad6265SDimitry Andric if (auto *AddrInst = dyn_cast_or_null<IntToPtrInst>(Addr)) {
35281ad6265SDimitry Andric // If isRuntimeCounterRelocationEnabled() is true then the address of
35381ad6265SDimitry Andric // the store instruction is computed with two instructions in
35481ad6265SDimitry Andric // InstrProfiling::getCounterAddress(). We need to copy those
35581ad6265SDimitry Andric // instructions to this block to compute Addr correctly.
35681ad6265SDimitry Andric // %BiasAdd = add i64 ptrtoint <__profc_>, <__llvm_profile_counter_bias>
35781ad6265SDimitry Andric // %Addr = inttoptr i64 %BiasAdd to i64*
35881ad6265SDimitry Andric auto *OrigBiasInst = dyn_cast<BinaryOperator>(AddrInst->getOperand(0));
35981ad6265SDimitry Andric assert(OrigBiasInst->getOpcode() == Instruction::BinaryOps::Add);
36081ad6265SDimitry Andric Value *BiasInst = Builder.Insert(OrigBiasInst->clone());
361c9157d92SDimitry Andric Addr = Builder.CreateIntToPtr(BiasInst,
362c9157d92SDimitry Andric PointerType::getUnqual(Ty->getContext()));
36381ad6265SDimitry Andric }
3640b57cec5SDimitry Andric if (AtomicCounterUpdatePromoted)
3650b57cec5SDimitry Andric // automic update currently can only be promoted across the current
3660b57cec5SDimitry Andric // loop, not the whole loop nest.
3670b57cec5SDimitry Andric Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue,
368fe6060f1SDimitry Andric MaybeAlign(),
3690b57cec5SDimitry Andric AtomicOrdering::SequentiallyConsistent);
3700b57cec5SDimitry Andric else {
3710b57cec5SDimitry Andric LoadInst *OldVal = Builder.CreateLoad(Ty, Addr, "pgocount.promoted");
3720b57cec5SDimitry Andric auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue);
3730b57cec5SDimitry Andric auto *NewStore = Builder.CreateStore(NewVal, Addr);
3740b57cec5SDimitry Andric
3750b57cec5SDimitry Andric // Now update the parent loop's candidate list:
3760b57cec5SDimitry Andric if (IterativeCounterPromotion) {
3770b57cec5SDimitry Andric auto *TargetLoop = LI.getLoopFor(ExitBlock);
3780b57cec5SDimitry Andric if (TargetLoop)
3790b57cec5SDimitry Andric LoopToCandidates[TargetLoop].emplace_back(OldVal, NewStore);
3800b57cec5SDimitry Andric }
3810b57cec5SDimitry Andric }
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric
3850b57cec5SDimitry Andric private:
3860b57cec5SDimitry Andric Instruction *Store;
3870b57cec5SDimitry Andric ArrayRef<BasicBlock *> ExitBlocks;
3880b57cec5SDimitry Andric ArrayRef<Instruction *> InsertPts;
3890b57cec5SDimitry Andric DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
3900b57cec5SDimitry Andric LoopInfo &LI;
3910b57cec5SDimitry Andric };
3920b57cec5SDimitry Andric
3930b57cec5SDimitry Andric /// A helper class to do register promotion for all profile counter
3940b57cec5SDimitry Andric /// updates in a loop.
3950b57cec5SDimitry Andric ///
3960b57cec5SDimitry Andric class PGOCounterPromoter {
3970b57cec5SDimitry Andric public:
PGOCounterPromoter(DenseMap<Loop *,SmallVector<LoadStorePair,8>> & LoopToCands,Loop & CurLoop,LoopInfo & LI,BlockFrequencyInfo * BFI)3980b57cec5SDimitry Andric PGOCounterPromoter(
3990b57cec5SDimitry Andric DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands,
4000b57cec5SDimitry Andric Loop &CurLoop, LoopInfo &LI, BlockFrequencyInfo *BFI)
40104eeddc0SDimitry Andric : LoopToCandidates(LoopToCands), L(CurLoop), LI(LI), BFI(BFI) {
4020b57cec5SDimitry Andric
4035ffd83dbSDimitry Andric // Skip collection of ExitBlocks and InsertPts for loops that will not be
4045ffd83dbSDimitry Andric // able to have counters promoted.
4050b57cec5SDimitry Andric SmallVector<BasicBlock *, 8> LoopExitBlocks;
4060b57cec5SDimitry Andric SmallPtrSet<BasicBlock *, 8> BlockSet;
4075ffd83dbSDimitry Andric
4080b57cec5SDimitry Andric L.getExitBlocks(LoopExitBlocks);
4095ffd83dbSDimitry Andric if (!isPromotionPossible(&L, LoopExitBlocks))
4105ffd83dbSDimitry Andric return;
4110b57cec5SDimitry Andric
4120b57cec5SDimitry Andric for (BasicBlock *ExitBlock : LoopExitBlocks) {
413c9157d92SDimitry Andric if (BlockSet.insert(ExitBlock).second &&
414c9157d92SDimitry Andric llvm::none_of(predecessors(ExitBlock), [&](const BasicBlock *Pred) {
415c9157d92SDimitry Andric return llvm::isPresplitCoroSuspendExitEdge(*Pred, *ExitBlock);
416c9157d92SDimitry Andric })) {
4170b57cec5SDimitry Andric ExitBlocks.push_back(ExitBlock);
4180b57cec5SDimitry Andric InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
4190b57cec5SDimitry Andric }
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric
run(int64_t * NumPromoted)4230b57cec5SDimitry Andric bool run(int64_t *NumPromoted) {
4240b57cec5SDimitry Andric // Skip 'infinite' loops:
4250b57cec5SDimitry Andric if (ExitBlocks.size() == 0)
4260b57cec5SDimitry Andric return false;
427e8d8bef9SDimitry Andric
428e8d8bef9SDimitry Andric // Skip if any of the ExitBlocks contains a ret instruction.
429e8d8bef9SDimitry Andric // This is to prevent dumping of incomplete profile -- if the
430e8d8bef9SDimitry Andric // the loop is a long running loop and dump is called in the middle
431e8d8bef9SDimitry Andric // of the loop, the result profile is incomplete.
432e8d8bef9SDimitry Andric // FIXME: add other heuristics to detect long running loops.
433e8d8bef9SDimitry Andric if (SkipRetExitBlock) {
434bdd1243dSDimitry Andric for (auto *BB : ExitBlocks)
435e8d8bef9SDimitry Andric if (isa<ReturnInst>(BB->getTerminator()))
436e8d8bef9SDimitry Andric return false;
437e8d8bef9SDimitry Andric }
438e8d8bef9SDimitry Andric
4390b57cec5SDimitry Andric unsigned MaxProm = getMaxNumOfPromotionsInLoop(&L);
4400b57cec5SDimitry Andric if (MaxProm == 0)
4410b57cec5SDimitry Andric return false;
4420b57cec5SDimitry Andric
4430b57cec5SDimitry Andric unsigned Promoted = 0;
4440b57cec5SDimitry Andric for (auto &Cand : LoopToCandidates[&L]) {
4450b57cec5SDimitry Andric
4460b57cec5SDimitry Andric SmallVector<PHINode *, 4> NewPHIs;
4470b57cec5SDimitry Andric SSAUpdater SSA(&NewPHIs);
4480b57cec5SDimitry Andric Value *InitVal = ConstantInt::get(Cand.first->getType(), 0);
4490b57cec5SDimitry Andric
4500b57cec5SDimitry Andric // If BFI is set, we will use it to guide the promotions.
4510b57cec5SDimitry Andric if (BFI) {
4520b57cec5SDimitry Andric auto *BB = Cand.first->getParent();
4530b57cec5SDimitry Andric auto InstrCount = BFI->getBlockProfileCount(BB);
4540b57cec5SDimitry Andric if (!InstrCount)
4550b57cec5SDimitry Andric continue;
4560b57cec5SDimitry Andric auto PreheaderCount = BFI->getBlockProfileCount(L.getLoopPreheader());
4570b57cec5SDimitry Andric // If the average loop trip count is not greater than 1.5, we skip
4580b57cec5SDimitry Andric // promotion.
45981ad6265SDimitry Andric if (PreheaderCount && (*PreheaderCount * 3) >= (*InstrCount * 2))
4600b57cec5SDimitry Andric continue;
4610b57cec5SDimitry Andric }
4620b57cec5SDimitry Andric
4630b57cec5SDimitry Andric PGOCounterPromoterHelper Promoter(Cand.first, Cand.second, SSA, InitVal,
4640b57cec5SDimitry Andric L.getLoopPreheader(), ExitBlocks,
4650b57cec5SDimitry Andric InsertPts, LoopToCandidates, LI);
4660b57cec5SDimitry Andric Promoter.run(SmallVector<Instruction *, 2>({Cand.first, Cand.second}));
4670b57cec5SDimitry Andric Promoted++;
4680b57cec5SDimitry Andric if (Promoted >= MaxProm)
4690b57cec5SDimitry Andric break;
4700b57cec5SDimitry Andric
4710b57cec5SDimitry Andric (*NumPromoted)++;
4720b57cec5SDimitry Andric if (MaxNumOfPromotions != -1 && *NumPromoted >= MaxNumOfPromotions)
4730b57cec5SDimitry Andric break;
4740b57cec5SDimitry Andric }
4750b57cec5SDimitry Andric
4760b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << Promoted << " counters promoted for loop (depth="
4770b57cec5SDimitry Andric << L.getLoopDepth() << ")\n");
4780b57cec5SDimitry Andric return Promoted != 0;
4790b57cec5SDimitry Andric }
4800b57cec5SDimitry Andric
4810b57cec5SDimitry Andric private:
allowSpeculativeCounterPromotion(Loop * LP)4820b57cec5SDimitry Andric bool allowSpeculativeCounterPromotion(Loop *LP) {
4830b57cec5SDimitry Andric SmallVector<BasicBlock *, 8> ExitingBlocks;
4840b57cec5SDimitry Andric L.getExitingBlocks(ExitingBlocks);
4850b57cec5SDimitry Andric // Not considierered speculative.
4860b57cec5SDimitry Andric if (ExitingBlocks.size() == 1)
4870b57cec5SDimitry Andric return true;
4880b57cec5SDimitry Andric if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
4890b57cec5SDimitry Andric return false;
4900b57cec5SDimitry Andric return true;
4910b57cec5SDimitry Andric }
4920b57cec5SDimitry Andric
4935ffd83dbSDimitry Andric // Check whether the loop satisfies the basic conditions needed to perform
4945ffd83dbSDimitry Andric // Counter Promotions.
4954824e7fdSDimitry Andric bool
isPromotionPossible(Loop * LP,const SmallVectorImpl<BasicBlock * > & LoopExitBlocks)4964824e7fdSDimitry Andric isPromotionPossible(Loop *LP,
4975ffd83dbSDimitry Andric const SmallVectorImpl<BasicBlock *> &LoopExitBlocks) {
4980b57cec5SDimitry Andric // We can't insert into a catchswitch.
4990b57cec5SDimitry Andric if (llvm::any_of(LoopExitBlocks, [](BasicBlock *Exit) {
5000b57cec5SDimitry Andric return isa<CatchSwitchInst>(Exit->getTerminator());
5010b57cec5SDimitry Andric }))
5025ffd83dbSDimitry Andric return false;
5030b57cec5SDimitry Andric
5040b57cec5SDimitry Andric if (!LP->hasDedicatedExits())
5055ffd83dbSDimitry Andric return false;
5060b57cec5SDimitry Andric
5070b57cec5SDimitry Andric BasicBlock *PH = LP->getLoopPreheader();
5080b57cec5SDimitry Andric if (!PH)
5095ffd83dbSDimitry Andric return false;
5105ffd83dbSDimitry Andric
5115ffd83dbSDimitry Andric return true;
5125ffd83dbSDimitry Andric }
5135ffd83dbSDimitry Andric
5145ffd83dbSDimitry Andric // Returns the max number of Counter Promotions for LP.
getMaxNumOfPromotionsInLoop(Loop * LP)5155ffd83dbSDimitry Andric unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
5165ffd83dbSDimitry Andric SmallVector<BasicBlock *, 8> LoopExitBlocks;
5175ffd83dbSDimitry Andric LP->getExitBlocks(LoopExitBlocks);
5185ffd83dbSDimitry Andric if (!isPromotionPossible(LP, LoopExitBlocks))
5190b57cec5SDimitry Andric return 0;
5200b57cec5SDimitry Andric
5210b57cec5SDimitry Andric SmallVector<BasicBlock *, 8> ExitingBlocks;
5220b57cec5SDimitry Andric LP->getExitingBlocks(ExitingBlocks);
5230b57cec5SDimitry Andric
5240b57cec5SDimitry Andric // If BFI is set, we do more aggressive promotions based on BFI.
5250b57cec5SDimitry Andric if (BFI)
5260b57cec5SDimitry Andric return (unsigned)-1;
5270b57cec5SDimitry Andric
5280b57cec5SDimitry Andric // Not considierered speculative.
5290b57cec5SDimitry Andric if (ExitingBlocks.size() == 1)
5300b57cec5SDimitry Andric return MaxNumOfPromotionsPerLoop;
5310b57cec5SDimitry Andric
5320b57cec5SDimitry Andric if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting)
5330b57cec5SDimitry Andric return 0;
5340b57cec5SDimitry Andric
5350b57cec5SDimitry Andric // Whether the target block is in a loop does not matter:
5360b57cec5SDimitry Andric if (SpeculativeCounterPromotionToLoop)
5370b57cec5SDimitry Andric return MaxNumOfPromotionsPerLoop;
5380b57cec5SDimitry Andric
5390b57cec5SDimitry Andric // Now check the target block:
5400b57cec5SDimitry Andric unsigned MaxProm = MaxNumOfPromotionsPerLoop;
5410b57cec5SDimitry Andric for (auto *TargetBlock : LoopExitBlocks) {
5420b57cec5SDimitry Andric auto *TargetLoop = LI.getLoopFor(TargetBlock);
5430b57cec5SDimitry Andric if (!TargetLoop)
5440b57cec5SDimitry Andric continue;
5450b57cec5SDimitry Andric unsigned MaxPromForTarget = getMaxNumOfPromotionsInLoop(TargetLoop);
5460b57cec5SDimitry Andric unsigned PendingCandsInTarget = LoopToCandidates[TargetLoop].size();
5470b57cec5SDimitry Andric MaxProm =
5480b57cec5SDimitry Andric std::min(MaxProm, std::max(MaxPromForTarget, PendingCandsInTarget) -
5490b57cec5SDimitry Andric PendingCandsInTarget);
5500b57cec5SDimitry Andric }
5510b57cec5SDimitry Andric return MaxProm;
5520b57cec5SDimitry Andric }
5530b57cec5SDimitry Andric
5540b57cec5SDimitry Andric DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates;
5550b57cec5SDimitry Andric SmallVector<BasicBlock *, 8> ExitBlocks;
5560b57cec5SDimitry Andric SmallVector<Instruction *, 8> InsertPts;
5570b57cec5SDimitry Andric Loop &L;
5580b57cec5SDimitry Andric LoopInfo &LI;
5590b57cec5SDimitry Andric BlockFrequencyInfo *BFI;
5600b57cec5SDimitry Andric };
5610b57cec5SDimitry Andric
562e8d8bef9SDimitry Andric enum class ValueProfilingCallType {
563e8d8bef9SDimitry Andric // Individual values are tracked. Currently used for indiret call target
564e8d8bef9SDimitry Andric // profiling.
565e8d8bef9SDimitry Andric Default,
566e8d8bef9SDimitry Andric
567e8d8bef9SDimitry Andric // MemOp: the memop size value profiling.
568e8d8bef9SDimitry Andric MemOp
569e8d8bef9SDimitry Andric };
570e8d8bef9SDimitry Andric
5710b57cec5SDimitry Andric } // end anonymous namespace
5720b57cec5SDimitry Andric
run(Module & M,ModuleAnalysisManager & AM)573c9157d92SDimitry Andric PreservedAnalyses InstrProfilingLoweringPass::run(Module &M,
574c9157d92SDimitry Andric ModuleAnalysisManager &AM) {
5758bcb0991SDimitry Andric FunctionAnalysisManager &FAM =
5768bcb0991SDimitry Andric AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
5778bcb0991SDimitry Andric auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
5788bcb0991SDimitry Andric return FAM.getResult<TargetLibraryAnalysis>(F);
5798bcb0991SDimitry Andric };
580c9157d92SDimitry Andric InstrLowerer Lowerer(M, Options, GetTLI, IsCS);
581c9157d92SDimitry Andric if (!Lowerer.lower())
5820b57cec5SDimitry Andric return PreservedAnalyses::all();
5830b57cec5SDimitry Andric
5840b57cec5SDimitry Andric return PreservedAnalyses::none();
5850b57cec5SDimitry Andric }
5860b57cec5SDimitry Andric
lowerIntrinsics(Function * F)587c9157d92SDimitry Andric bool InstrLowerer::lowerIntrinsics(Function *F) {
5880b57cec5SDimitry Andric bool MadeChange = false;
5890b57cec5SDimitry Andric PromotionCandidates.clear();
5900b57cec5SDimitry Andric for (BasicBlock &BB : *F) {
591349cc55cSDimitry Andric for (Instruction &Instr : llvm::make_early_inc_range(BB)) {
59204eeddc0SDimitry Andric if (auto *IPIS = dyn_cast<InstrProfIncrementInstStep>(&Instr)) {
59304eeddc0SDimitry Andric lowerIncrement(IPIS);
5940b57cec5SDimitry Andric MadeChange = true;
59504eeddc0SDimitry Andric } else if (auto *IPI = dyn_cast<InstrProfIncrementInst>(&Instr)) {
59604eeddc0SDimitry Andric lowerIncrement(IPI);
59704eeddc0SDimitry Andric MadeChange = true;
598fe013be4SDimitry Andric } else if (auto *IPC = dyn_cast<InstrProfTimestampInst>(&Instr)) {
599fe013be4SDimitry Andric lowerTimestamp(IPC);
600fe013be4SDimitry Andric MadeChange = true;
6011fd87a68SDimitry Andric } else if (auto *IPC = dyn_cast<InstrProfCoverInst>(&Instr)) {
6021fd87a68SDimitry Andric lowerCover(IPC);
6031fd87a68SDimitry Andric MadeChange = true;
60404eeddc0SDimitry Andric } else if (auto *IPVP = dyn_cast<InstrProfValueProfileInst>(&Instr)) {
60504eeddc0SDimitry Andric lowerValueProfileInst(IPVP);
6060b57cec5SDimitry Andric MadeChange = true;
607c9157d92SDimitry Andric } else if (auto *IPMP = dyn_cast<InstrProfMCDCBitmapParameters>(&Instr)) {
608c9157d92SDimitry Andric IPMP->eraseFromParent();
609c9157d92SDimitry Andric MadeChange = true;
610c9157d92SDimitry Andric } else if (auto *IPBU = dyn_cast<InstrProfMCDCTVBitmapUpdate>(&Instr)) {
611c9157d92SDimitry Andric lowerMCDCTestVectorBitmapUpdate(IPBU);
612c9157d92SDimitry Andric MadeChange = true;
613c9157d92SDimitry Andric } else if (auto *IPTU = dyn_cast<InstrProfMCDCCondBitmapUpdate>(&Instr)) {
614c9157d92SDimitry Andric lowerMCDCCondBitmapUpdate(IPTU);
615c9157d92SDimitry Andric MadeChange = true;
6160b57cec5SDimitry Andric }
6170b57cec5SDimitry Andric }
6180b57cec5SDimitry Andric }
6190b57cec5SDimitry Andric
6200b57cec5SDimitry Andric if (!MadeChange)
6210b57cec5SDimitry Andric return false;
6220b57cec5SDimitry Andric
6230b57cec5SDimitry Andric promoteCounterLoadStores(F);
6240b57cec5SDimitry Andric return true;
6250b57cec5SDimitry Andric }
6260b57cec5SDimitry Andric
isRuntimeCounterRelocationEnabled() const627c9157d92SDimitry Andric bool InstrLowerer::isRuntimeCounterRelocationEnabled() const {
628fe6060f1SDimitry Andric // Mach-O don't support weak external references.
629fe6060f1SDimitry Andric if (TT.isOSBinFormatMachO())
630fe6060f1SDimitry Andric return false;
631fe6060f1SDimitry Andric
6325ffd83dbSDimitry Andric if (RuntimeCounterRelocation.getNumOccurrences() > 0)
6335ffd83dbSDimitry Andric return RuntimeCounterRelocation;
6345ffd83dbSDimitry Andric
635fe6060f1SDimitry Andric // Fuchsia uses runtime counter relocation by default.
6365ffd83dbSDimitry Andric return TT.isOSFuchsia();
6375ffd83dbSDimitry Andric }
6385ffd83dbSDimitry Andric
isCounterPromotionEnabled() const639c9157d92SDimitry Andric bool InstrLowerer::isCounterPromotionEnabled() const {
6400b57cec5SDimitry Andric if (DoCounterPromotion.getNumOccurrences() > 0)
6410b57cec5SDimitry Andric return DoCounterPromotion;
6420b57cec5SDimitry Andric
6430b57cec5SDimitry Andric return Options.DoCounterPromotion;
6440b57cec5SDimitry Andric }
6450b57cec5SDimitry Andric
promoteCounterLoadStores(Function * F)646c9157d92SDimitry Andric void InstrLowerer::promoteCounterLoadStores(Function *F) {
6470b57cec5SDimitry Andric if (!isCounterPromotionEnabled())
6480b57cec5SDimitry Andric return;
6490b57cec5SDimitry Andric
6500b57cec5SDimitry Andric DominatorTree DT(*F);
6510b57cec5SDimitry Andric LoopInfo LI(DT);
6520b57cec5SDimitry Andric DenseMap<Loop *, SmallVector<LoadStorePair, 8>> LoopPromotionCandidates;
6530b57cec5SDimitry Andric
6540b57cec5SDimitry Andric std::unique_ptr<BlockFrequencyInfo> BFI;
6550b57cec5SDimitry Andric if (Options.UseBFIInPromotion) {
6560b57cec5SDimitry Andric std::unique_ptr<BranchProbabilityInfo> BPI;
6578bcb0991SDimitry Andric BPI.reset(new BranchProbabilityInfo(*F, LI, &GetTLI(*F)));
6580b57cec5SDimitry Andric BFI.reset(new BlockFrequencyInfo(*F, *BPI, LI));
6590b57cec5SDimitry Andric }
6600b57cec5SDimitry Andric
6610b57cec5SDimitry Andric for (const auto &LoadStore : PromotionCandidates) {
6620b57cec5SDimitry Andric auto *CounterLoad = LoadStore.first;
6630b57cec5SDimitry Andric auto *CounterStore = LoadStore.second;
6640b57cec5SDimitry Andric BasicBlock *BB = CounterLoad->getParent();
6650b57cec5SDimitry Andric Loop *ParentLoop = LI.getLoopFor(BB);
6660b57cec5SDimitry Andric if (!ParentLoop)
6670b57cec5SDimitry Andric continue;
6680b57cec5SDimitry Andric LoopPromotionCandidates[ParentLoop].emplace_back(CounterLoad, CounterStore);
6690b57cec5SDimitry Andric }
6700b57cec5SDimitry Andric
6710b57cec5SDimitry Andric SmallVector<Loop *, 4> Loops = LI.getLoopsInPreorder();
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andric // Do a post-order traversal of the loops so that counter updates can be
6740b57cec5SDimitry Andric // iteratively hoisted outside the loop nest.
6750b57cec5SDimitry Andric for (auto *Loop : llvm::reverse(Loops)) {
6760b57cec5SDimitry Andric PGOCounterPromoter Promoter(LoopPromotionCandidates, *Loop, LI, BFI.get());
6770b57cec5SDimitry Andric Promoter.run(&TotalCountersPromoted);
6780b57cec5SDimitry Andric }
6790b57cec5SDimitry Andric }
6800b57cec5SDimitry Andric
needsRuntimeHookUnconditionally(const Triple & TT)681349cc55cSDimitry Andric static bool needsRuntimeHookUnconditionally(const Triple &TT) {
682349cc55cSDimitry Andric // On Fuchsia, we only need runtime hook if any counters are present.
683349cc55cSDimitry Andric if (TT.isOSFuchsia())
684349cc55cSDimitry Andric return false;
685349cc55cSDimitry Andric
686349cc55cSDimitry Andric return true;
687349cc55cSDimitry Andric }
688349cc55cSDimitry Andric
6890b57cec5SDimitry Andric /// Check if the module contains uses of any profiling intrinsics.
containsProfilingIntrinsics(Module & M)6900b57cec5SDimitry Andric static bool containsProfilingIntrinsics(Module &M) {
69104eeddc0SDimitry Andric auto containsIntrinsic = [&](int ID) {
69204eeddc0SDimitry Andric if (auto *F = M.getFunction(Intrinsic::getName(ID)))
69304eeddc0SDimitry Andric return !F->use_empty();
6940b57cec5SDimitry Andric return false;
69504eeddc0SDimitry Andric };
6961fd87a68SDimitry Andric return containsIntrinsic(llvm::Intrinsic::instrprof_cover) ||
6971fd87a68SDimitry Andric containsIntrinsic(llvm::Intrinsic::instrprof_increment) ||
69804eeddc0SDimitry Andric containsIntrinsic(llvm::Intrinsic::instrprof_increment_step) ||
699fe013be4SDimitry Andric containsIntrinsic(llvm::Intrinsic::instrprof_timestamp) ||
70004eeddc0SDimitry Andric containsIntrinsic(llvm::Intrinsic::instrprof_value_profile);
7010b57cec5SDimitry Andric }
7020b57cec5SDimitry Andric
lower()703c9157d92SDimitry Andric bool InstrLowerer::lower() {
704349cc55cSDimitry Andric bool MadeChange = false;
705bdd1243dSDimitry Andric bool NeedsRuntimeHook = needsRuntimeHookUnconditionally(TT);
706bdd1243dSDimitry Andric if (NeedsRuntimeHook)
707349cc55cSDimitry Andric MadeChange = emitRuntimeHook();
7080b57cec5SDimitry Andric
709bdd1243dSDimitry Andric bool ContainsProfiling = containsProfilingIntrinsics(M);
7100b57cec5SDimitry Andric GlobalVariable *CoverageNamesVar =
7110b57cec5SDimitry Andric M.getNamedGlobal(getCoverageUnusedNamesVarName());
712bdd1243dSDimitry Andric // Improve compile time by avoiding linear scans when there is no work.
713bdd1243dSDimitry Andric if (!ContainsProfiling && !CoverageNamesVar)
7140b57cec5SDimitry Andric return MadeChange;
7150b57cec5SDimitry Andric
7160b57cec5SDimitry Andric // We did not know how many value sites there would be inside
7170b57cec5SDimitry Andric // the instrumented function. This is counting the number of instrumented
7180b57cec5SDimitry Andric // target value sites to enter it as field in the profile data variable.
7190b57cec5SDimitry Andric for (Function &F : M) {
720c9157d92SDimitry Andric InstrProfCntrInstBase *FirstProfInst = nullptr;
721c9157d92SDimitry Andric for (BasicBlock &BB : F) {
722c9157d92SDimitry Andric for (auto I = BB.begin(), E = BB.end(); I != E; I++) {
7230b57cec5SDimitry Andric if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I))
7240b57cec5SDimitry Andric computeNumValueSiteCounts(Ind);
725c9157d92SDimitry Andric else {
726c9157d92SDimitry Andric if (FirstProfInst == nullptr &&
727fe013be4SDimitry Andric (isa<InstrProfIncrementInst>(I) || isa<InstrProfCoverInst>(I)))
728c9157d92SDimitry Andric FirstProfInst = dyn_cast<InstrProfCntrInstBase>(I);
729c9157d92SDimitry Andric // If the MCDCBitmapParameters intrinsic seen, create the bitmaps.
730c9157d92SDimitry Andric if (const auto &Params = dyn_cast<InstrProfMCDCBitmapParameters>(I))
731c9157d92SDimitry Andric static_cast<void>(getOrCreateRegionBitmaps(Params));
732c9157d92SDimitry Andric }
733c9157d92SDimitry Andric }
734c9157d92SDimitry Andric }
7350b57cec5SDimitry Andric
736c9157d92SDimitry Andric // Use a profile intrinsic to create the region counters and data variable.
737c9157d92SDimitry Andric // Also create the data variable based on the MCDCParams.
738c9157d92SDimitry Andric if (FirstProfInst != nullptr) {
739fe013be4SDimitry Andric static_cast<void>(getOrCreateRegionCounters(FirstProfInst));
7400b57cec5SDimitry Andric }
741c9157d92SDimitry Andric }
7420b57cec5SDimitry Andric
7430b57cec5SDimitry Andric for (Function &F : M)
7440b57cec5SDimitry Andric MadeChange |= lowerIntrinsics(&F);
7450b57cec5SDimitry Andric
7460b57cec5SDimitry Andric if (CoverageNamesVar) {
7470b57cec5SDimitry Andric lowerCoverageData(CoverageNamesVar);
7480b57cec5SDimitry Andric MadeChange = true;
7490b57cec5SDimitry Andric }
7500b57cec5SDimitry Andric
7510b57cec5SDimitry Andric if (!MadeChange)
7520b57cec5SDimitry Andric return false;
7530b57cec5SDimitry Andric
7540b57cec5SDimitry Andric emitVNodes();
7550b57cec5SDimitry Andric emitNameData();
756bdd1243dSDimitry Andric
757bdd1243dSDimitry Andric // Emit runtime hook for the cases where the target does not unconditionally
758bdd1243dSDimitry Andric // require pulling in profile runtime, and coverage is enabled on code that is
759bdd1243dSDimitry Andric // not eliminated by the front-end, e.g. unused functions with internal
760bdd1243dSDimitry Andric // linkage.
761bdd1243dSDimitry Andric if (!NeedsRuntimeHook && ContainsProfiling)
762349cc55cSDimitry Andric emitRuntimeHook();
763bdd1243dSDimitry Andric
7640b57cec5SDimitry Andric emitRegistration();
7650b57cec5SDimitry Andric emitUses();
7660b57cec5SDimitry Andric emitInitialization();
7670b57cec5SDimitry Andric return true;
7680b57cec5SDimitry Andric }
7690b57cec5SDimitry Andric
getOrInsertValueProfilingCall(Module & M,const TargetLibraryInfo & TLI,ValueProfilingCallType CallType=ValueProfilingCallType::Default)770e8d8bef9SDimitry Andric static FunctionCallee getOrInsertValueProfilingCall(
771e8d8bef9SDimitry Andric Module &M, const TargetLibraryInfo &TLI,
772e8d8bef9SDimitry Andric ValueProfilingCallType CallType = ValueProfilingCallType::Default) {
7730b57cec5SDimitry Andric LLVMContext &Ctx = M.getContext();
7740b57cec5SDimitry Andric auto *ReturnTy = Type::getVoidTy(M.getContext());
7750b57cec5SDimitry Andric
7760b57cec5SDimitry Andric AttributeList AL;
7770b57cec5SDimitry Andric if (auto AK = TLI.getExtAttrForI32Param(false))
7780b57cec5SDimitry Andric AL = AL.addParamAttribute(M.getContext(), 2, AK);
7790b57cec5SDimitry Andric
780e8d8bef9SDimitry Andric assert((CallType == ValueProfilingCallType::Default ||
781e8d8bef9SDimitry Andric CallType == ValueProfilingCallType::MemOp) &&
782e8d8bef9SDimitry Andric "Must be Default or MemOp");
7830b57cec5SDimitry Andric Type *ParamTypes[] = {
7840b57cec5SDimitry Andric #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
7850b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProfData.inc"
7860b57cec5SDimitry Andric };
7870b57cec5SDimitry Andric auto *ValueProfilingCallTy =
788bdd1243dSDimitry Andric FunctionType::get(ReturnTy, ArrayRef(ParamTypes), false);
789e8d8bef9SDimitry Andric StringRef FuncName = CallType == ValueProfilingCallType::Default
790e8d8bef9SDimitry Andric ? getInstrProfValueProfFuncName()
791e8d8bef9SDimitry Andric : getInstrProfValueProfMemOpFuncName();
792e8d8bef9SDimitry Andric return M.getOrInsertFunction(FuncName, ValueProfilingCallTy, AL);
7930b57cec5SDimitry Andric }
7940b57cec5SDimitry Andric
computeNumValueSiteCounts(InstrProfValueProfileInst * Ind)795c9157d92SDimitry Andric void InstrLowerer::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
7960b57cec5SDimitry Andric GlobalVariable *Name = Ind->getName();
7970b57cec5SDimitry Andric uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
7980b57cec5SDimitry Andric uint64_t Index = Ind->getIndex()->getZExtValue();
7994824e7fdSDimitry Andric auto &PD = ProfileDataMap[Name];
8004824e7fdSDimitry Andric PD.NumValueSites[ValueKind] =
8014824e7fdSDimitry Andric std::max(PD.NumValueSites[ValueKind], (uint32_t)(Index + 1));
8020b57cec5SDimitry Andric }
8030b57cec5SDimitry Andric
lowerValueProfileInst(InstrProfValueProfileInst * Ind)804c9157d92SDimitry Andric void InstrLowerer::lowerValueProfileInst(InstrProfValueProfileInst *Ind) {
8050eae32dcSDimitry Andric // TODO: Value profiling heavily depends on the data section which is omitted
8060eae32dcSDimitry Andric // in lightweight mode. We need to move the value profile pointer to the
8070eae32dcSDimitry Andric // Counter struct to get this working.
8080eae32dcSDimitry Andric assert(
809c9157d92SDimitry Andric !DebugInfoCorrelate && ProfileCorrelate == InstrProfCorrelator::NONE &&
8100eae32dcSDimitry Andric "Value profiling is not yet supported with lightweight instrumentation");
8110b57cec5SDimitry Andric GlobalVariable *Name = Ind->getName();
8120b57cec5SDimitry Andric auto It = ProfileDataMap.find(Name);
8130b57cec5SDimitry Andric assert(It != ProfileDataMap.end() && It->second.DataVar &&
8140b57cec5SDimitry Andric "value profiling detected in function with no counter incerement");
8150b57cec5SDimitry Andric
8160b57cec5SDimitry Andric GlobalVariable *DataVar = It->second.DataVar;
8170b57cec5SDimitry Andric uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
8180b57cec5SDimitry Andric uint64_t Index = Ind->getIndex()->getZExtValue();
8190b57cec5SDimitry Andric for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind)
8200b57cec5SDimitry Andric Index += It->second.NumValueSites[Kind];
8210b57cec5SDimitry Andric
8220b57cec5SDimitry Andric IRBuilder<> Builder(Ind);
823e8d8bef9SDimitry Andric bool IsMemOpSize = (Ind->getValueKind()->getZExtValue() ==
8240b57cec5SDimitry Andric llvm::InstrProfValueKind::IPVK_MemOPSize);
8250b57cec5SDimitry Andric CallInst *Call = nullptr;
8268bcb0991SDimitry Andric auto *TLI = &GetTLI(*Ind->getFunction());
8275ffd83dbSDimitry Andric
8285ffd83dbSDimitry Andric // To support value profiling calls within Windows exception handlers, funclet
8295ffd83dbSDimitry Andric // information contained within operand bundles needs to be copied over to
8305ffd83dbSDimitry Andric // the library call. This is required for the IR to be processed by the
8315ffd83dbSDimitry Andric // WinEHPrepare pass.
8325ffd83dbSDimitry Andric SmallVector<OperandBundleDef, 1> OpBundles;
8335ffd83dbSDimitry Andric Ind->getOperandBundlesAsDefs(OpBundles);
834e8d8bef9SDimitry Andric if (!IsMemOpSize) {
835c9157d92SDimitry Andric Value *Args[3] = {Ind->getTargetValue(), DataVar, Builder.getInt32(Index)};
836c9157d92SDimitry Andric Call = Builder.CreateCall(getOrInsertValueProfilingCall(M, *TLI), Args,
8375ffd83dbSDimitry Andric OpBundles);
8380b57cec5SDimitry Andric } else {
839c9157d92SDimitry Andric Value *Args[3] = {Ind->getTargetValue(), DataVar, Builder.getInt32(Index)};
840e8d8bef9SDimitry Andric Call = Builder.CreateCall(
841c9157d92SDimitry Andric getOrInsertValueProfilingCall(M, *TLI, ValueProfilingCallType::MemOp),
8425ffd83dbSDimitry Andric Args, OpBundles);
8430b57cec5SDimitry Andric }
8440b57cec5SDimitry Andric if (auto AK = TLI->getExtAttrForI32Param(false))
8450b57cec5SDimitry Andric Call->addParamAttr(2, AK);
8460b57cec5SDimitry Andric Ind->replaceAllUsesWith(Call);
8470b57cec5SDimitry Andric Ind->eraseFromParent();
8480b57cec5SDimitry Andric }
8490b57cec5SDimitry Andric
getCounterAddress(InstrProfCntrInstBase * I)850c9157d92SDimitry Andric Value *InstrLowerer::getCounterAddress(InstrProfCntrInstBase *I) {
8511fd87a68SDimitry Andric auto *Counters = getOrCreateRegionCounters(I);
8521fd87a68SDimitry Andric IRBuilder<> Builder(I);
8530b57cec5SDimitry Andric
854fe013be4SDimitry Andric if (isa<InstrProfTimestampInst>(I))
855fe013be4SDimitry Andric Counters->setAlignment(Align(8));
856fe013be4SDimitry Andric
8571fd87a68SDimitry Andric auto *Addr = Builder.CreateConstInBoundsGEP2_32(
8581fd87a68SDimitry Andric Counters->getValueType(), Counters, 0, I->getIndex()->getZExtValue());
8590b57cec5SDimitry Andric
8601fd87a68SDimitry Andric if (!isRuntimeCounterRelocationEnabled())
8611fd87a68SDimitry Andric return Addr;
8621fd87a68SDimitry Andric
863c9157d92SDimitry Andric Type *Int64Ty = Type::getInt64Ty(M.getContext());
8641fd87a68SDimitry Andric Function *Fn = I->getParent()->getParent();
86581ad6265SDimitry Andric LoadInst *&BiasLI = FunctionToProfileBiasMap[Fn];
86681ad6265SDimitry Andric if (!BiasLI) {
86781ad6265SDimitry Andric IRBuilder<> EntryBuilder(&Fn->getEntryBlock().front());
868c9157d92SDimitry Andric auto *Bias = M.getGlobalVariable(getInstrProfCounterBiasVarName());
8695ffd83dbSDimitry Andric if (!Bias) {
870fe6060f1SDimitry Andric // Compiler must define this variable when runtime counter relocation
871fe6060f1SDimitry Andric // is being used. Runtime has a weak external reference that is used
872fe6060f1SDimitry Andric // to check whether that's the case or not.
8734824e7fdSDimitry Andric Bias = new GlobalVariable(
874c9157d92SDimitry Andric M, Int64Ty, false, GlobalValue::LinkOnceODRLinkage,
8754824e7fdSDimitry Andric Constant::getNullValue(Int64Ty), getInstrProfCounterBiasVarName());
8765ffd83dbSDimitry Andric Bias->setVisibility(GlobalVariable::HiddenVisibility);
877fe6060f1SDimitry Andric // A definition that's weak (linkonce_odr) without being in a COMDAT
878fe6060f1SDimitry Andric // section wouldn't lead to link errors, but it would lead to a dead
879fe6060f1SDimitry Andric // data word from every TU but one. Putting it in COMDAT ensures there
880fe6060f1SDimitry Andric // will be exactly one data slot in the link.
881fe6060f1SDimitry Andric if (TT.supportsCOMDAT())
882c9157d92SDimitry Andric Bias->setComdat(M.getOrInsertComdat(Bias->getName()));
8835ffd83dbSDimitry Andric }
88481ad6265SDimitry Andric BiasLI = EntryBuilder.CreateLoad(Int64Ty, Bias);
8855ffd83dbSDimitry Andric }
88681ad6265SDimitry Andric auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), BiasLI);
8871fd87a68SDimitry Andric return Builder.CreateIntToPtr(Add, Addr->getType());
8885ffd83dbSDimitry Andric }
8895ffd83dbSDimitry Andric
getBitmapAddress(InstrProfMCDCTVBitmapUpdate * I)890c9157d92SDimitry Andric Value *InstrLowerer::getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I) {
891c9157d92SDimitry Andric auto *Bitmaps = getOrCreateRegionBitmaps(I);
892c9157d92SDimitry Andric IRBuilder<> Builder(I);
893c9157d92SDimitry Andric
894c9157d92SDimitry Andric auto *Addr = Builder.CreateConstInBoundsGEP2_32(
895c9157d92SDimitry Andric Bitmaps->getValueType(), Bitmaps, 0, I->getBitmapIndex()->getZExtValue());
896c9157d92SDimitry Andric
897c9157d92SDimitry Andric if (isRuntimeCounterRelocationEnabled()) {
898c9157d92SDimitry Andric LLVMContext &Ctx = M.getContext();
899c9157d92SDimitry Andric Ctx.diagnose(DiagnosticInfoPGOProfile(
900c9157d92SDimitry Andric M.getName().data(),
901c9157d92SDimitry Andric Twine("Runtime counter relocation is presently not supported for MC/DC "
902c9157d92SDimitry Andric "bitmaps."),
903c9157d92SDimitry Andric DS_Warning));
904c9157d92SDimitry Andric }
905c9157d92SDimitry Andric
906c9157d92SDimitry Andric return Addr;
907c9157d92SDimitry Andric }
908c9157d92SDimitry Andric
lowerCover(InstrProfCoverInst * CoverInstruction)909c9157d92SDimitry Andric void InstrLowerer::lowerCover(InstrProfCoverInst *CoverInstruction) {
9101fd87a68SDimitry Andric auto *Addr = getCounterAddress(CoverInstruction);
9111fd87a68SDimitry Andric IRBuilder<> Builder(CoverInstruction);
9121fd87a68SDimitry Andric // We store zero to represent that this block is covered.
9131fd87a68SDimitry Andric Builder.CreateStore(Builder.getInt8(0), Addr);
9141fd87a68SDimitry Andric CoverInstruction->eraseFromParent();
9151fd87a68SDimitry Andric }
9161fd87a68SDimitry Andric
lowerTimestamp(InstrProfTimestampInst * TimestampInstruction)917c9157d92SDimitry Andric void InstrLowerer::lowerTimestamp(
918fe013be4SDimitry Andric InstrProfTimestampInst *TimestampInstruction) {
919fe013be4SDimitry Andric assert(TimestampInstruction->getIndex()->isZeroValue() &&
920fe013be4SDimitry Andric "timestamp probes are always the first probe for a function");
921c9157d92SDimitry Andric auto &Ctx = M.getContext();
922fe013be4SDimitry Andric auto *TimestampAddr = getCounterAddress(TimestampInstruction);
923fe013be4SDimitry Andric IRBuilder<> Builder(TimestampInstruction);
924fe013be4SDimitry Andric auto *CalleeTy =
925fe013be4SDimitry Andric FunctionType::get(Type::getVoidTy(Ctx), TimestampAddr->getType(), false);
926c9157d92SDimitry Andric auto Callee = M.getOrInsertFunction(
927fe013be4SDimitry Andric INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_SET_TIMESTAMP), CalleeTy);
928fe013be4SDimitry Andric Builder.CreateCall(Callee, {TimestampAddr});
929fe013be4SDimitry Andric TimestampInstruction->eraseFromParent();
930fe013be4SDimitry Andric }
931fe013be4SDimitry Andric
lowerIncrement(InstrProfIncrementInst * Inc)932c9157d92SDimitry Andric void InstrLowerer::lowerIncrement(InstrProfIncrementInst *Inc) {
9331fd87a68SDimitry Andric auto *Addr = getCounterAddress(Inc);
9341fd87a68SDimitry Andric
9351fd87a68SDimitry Andric IRBuilder<> Builder(Inc);
9365ffd83dbSDimitry Andric if (Options.Atomic || AtomicCounterUpdateAll ||
9371fd87a68SDimitry Andric (Inc->getIndex()->isZeroValue() && AtomicFirstCounter)) {
9380b57cec5SDimitry Andric Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(),
939fe6060f1SDimitry Andric MaybeAlign(), AtomicOrdering::Monotonic);
9400b57cec5SDimitry Andric } else {
9410b57cec5SDimitry Andric Value *IncStep = Inc->getStep();
9420b57cec5SDimitry Andric Value *Load = Builder.CreateLoad(IncStep->getType(), Addr, "pgocount");
9430b57cec5SDimitry Andric auto *Count = Builder.CreateAdd(Load, Inc->getStep());
9440b57cec5SDimitry Andric auto *Store = Builder.CreateStore(Count, Addr);
9450b57cec5SDimitry Andric if (isCounterPromotionEnabled())
9460b57cec5SDimitry Andric PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
9470b57cec5SDimitry Andric }
9480b57cec5SDimitry Andric Inc->eraseFromParent();
9490b57cec5SDimitry Andric }
9500b57cec5SDimitry Andric
lowerCoverageData(GlobalVariable * CoverageNamesVar)951c9157d92SDimitry Andric void InstrLowerer::lowerCoverageData(GlobalVariable *CoverageNamesVar) {
9520b57cec5SDimitry Andric ConstantArray *Names =
9530b57cec5SDimitry Andric cast<ConstantArray>(CoverageNamesVar->getInitializer());
9540b57cec5SDimitry Andric for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) {
9550b57cec5SDimitry Andric Constant *NC = Names->getOperand(I);
9560b57cec5SDimitry Andric Value *V = NC->stripPointerCasts();
9570b57cec5SDimitry Andric assert(isa<GlobalVariable>(V) && "Missing reference to function name");
9580b57cec5SDimitry Andric GlobalVariable *Name = cast<GlobalVariable>(V);
9590b57cec5SDimitry Andric
9600b57cec5SDimitry Andric Name->setLinkage(GlobalValue::PrivateLinkage);
9610b57cec5SDimitry Andric ReferencedNames.push_back(Name);
96281ad6265SDimitry Andric if (isa<ConstantExpr>(NC))
9630b57cec5SDimitry Andric NC->dropAllReferences();
9640b57cec5SDimitry Andric }
9650b57cec5SDimitry Andric CoverageNamesVar->eraseFromParent();
9660b57cec5SDimitry Andric }
9670b57cec5SDimitry Andric
lowerMCDCTestVectorBitmapUpdate(InstrProfMCDCTVBitmapUpdate * Update)968c9157d92SDimitry Andric void InstrLowerer::lowerMCDCTestVectorBitmapUpdate(
969c9157d92SDimitry Andric InstrProfMCDCTVBitmapUpdate *Update) {
970c9157d92SDimitry Andric IRBuilder<> Builder(Update);
971c9157d92SDimitry Andric auto *Int8Ty = Type::getInt8Ty(M.getContext());
972c9157d92SDimitry Andric auto *Int8PtrTy = PointerType::getUnqual(M.getContext());
973c9157d92SDimitry Andric auto *Int32Ty = Type::getInt32Ty(M.getContext());
974c9157d92SDimitry Andric auto *Int64Ty = Type::getInt64Ty(M.getContext());
975c9157d92SDimitry Andric auto *MCDCCondBitmapAddr = Update->getMCDCCondBitmapAddr();
976c9157d92SDimitry Andric auto *BitmapAddr = getBitmapAddress(Update);
977c9157d92SDimitry Andric
978c9157d92SDimitry Andric // Load Temp Val.
979c9157d92SDimitry Andric // %mcdc.temp = load i32, ptr %mcdc.addr, align 4
980c9157d92SDimitry Andric auto *Temp = Builder.CreateLoad(Int32Ty, MCDCCondBitmapAddr, "mcdc.temp");
981c9157d92SDimitry Andric
982c9157d92SDimitry Andric // Calculate byte offset using div8.
983c9157d92SDimitry Andric // %1 = lshr i32 %mcdc.temp, 3
984c9157d92SDimitry Andric auto *BitmapByteOffset = Builder.CreateLShr(Temp, 0x3);
985c9157d92SDimitry Andric
986c9157d92SDimitry Andric // Add byte offset to section base byte address.
987c9157d92SDimitry Andric // %2 = zext i32 %1 to i64
988c9157d92SDimitry Andric // %3 = add i64 ptrtoint (ptr @__profbm_test to i64), %2
989c9157d92SDimitry Andric auto *BitmapByteAddr =
990c9157d92SDimitry Andric Builder.CreateAdd(Builder.CreatePtrToInt(BitmapAddr, Int64Ty),
991c9157d92SDimitry Andric Builder.CreateZExtOrBitCast(BitmapByteOffset, Int64Ty));
992c9157d92SDimitry Andric
993c9157d92SDimitry Andric // Convert to a pointer.
994c9157d92SDimitry Andric // %4 = inttoptr i32 %3 to ptr
995c9157d92SDimitry Andric BitmapByteAddr = Builder.CreateIntToPtr(BitmapByteAddr, Int8PtrTy);
996c9157d92SDimitry Andric
997c9157d92SDimitry Andric // Calculate bit offset into bitmap byte by using div8 remainder (AND ~8)
998c9157d92SDimitry Andric // %5 = and i32 %mcdc.temp, 7
999c9157d92SDimitry Andric // %6 = trunc i32 %5 to i8
1000c9157d92SDimitry Andric auto *BitToSet = Builder.CreateTrunc(Builder.CreateAnd(Temp, 0x7), Int8Ty);
1001c9157d92SDimitry Andric
1002c9157d92SDimitry Andric // Shift bit offset left to form a bitmap.
1003c9157d92SDimitry Andric // %7 = shl i8 1, %6
1004c9157d92SDimitry Andric auto *ShiftedVal = Builder.CreateShl(Builder.getInt8(0x1), BitToSet);
1005c9157d92SDimitry Andric
1006c9157d92SDimitry Andric // Load profile bitmap byte.
1007c9157d92SDimitry Andric // %mcdc.bits = load i8, ptr %4, align 1
1008c9157d92SDimitry Andric auto *Bitmap = Builder.CreateLoad(Int8Ty, BitmapByteAddr, "mcdc.bits");
1009c9157d92SDimitry Andric
1010c9157d92SDimitry Andric // Perform logical OR of profile bitmap byte and shifted bit offset.
1011c9157d92SDimitry Andric // %8 = or i8 %mcdc.bits, %7
1012c9157d92SDimitry Andric auto *Result = Builder.CreateOr(Bitmap, ShiftedVal);
1013c9157d92SDimitry Andric
1014c9157d92SDimitry Andric // Store the updated profile bitmap byte.
1015c9157d92SDimitry Andric // store i8 %8, ptr %3, align 1
1016c9157d92SDimitry Andric Builder.CreateStore(Result, BitmapByteAddr);
1017c9157d92SDimitry Andric Update->eraseFromParent();
1018c9157d92SDimitry Andric }
1019c9157d92SDimitry Andric
lowerMCDCCondBitmapUpdate(InstrProfMCDCCondBitmapUpdate * Update)1020c9157d92SDimitry Andric void InstrLowerer::lowerMCDCCondBitmapUpdate(
1021c9157d92SDimitry Andric InstrProfMCDCCondBitmapUpdate *Update) {
1022c9157d92SDimitry Andric IRBuilder<> Builder(Update);
1023c9157d92SDimitry Andric auto *Int32Ty = Type::getInt32Ty(M.getContext());
1024c9157d92SDimitry Andric auto *MCDCCondBitmapAddr = Update->getMCDCCondBitmapAddr();
1025c9157d92SDimitry Andric
1026c9157d92SDimitry Andric // Load the MCDC temporary value from the stack.
1027c9157d92SDimitry Andric // %mcdc.temp = load i32, ptr %mcdc.addr, align 4
1028c9157d92SDimitry Andric auto *Temp = Builder.CreateLoad(Int32Ty, MCDCCondBitmapAddr, "mcdc.temp");
1029c9157d92SDimitry Andric
1030c9157d92SDimitry Andric // Zero-extend the evaluated condition boolean value (0 or 1) by 32bits.
1031c9157d92SDimitry Andric // %1 = zext i1 %tobool to i32
1032c9157d92SDimitry Andric auto *CondV_32 = Builder.CreateZExt(Update->getCondBool(), Int32Ty);
1033c9157d92SDimitry Andric
1034c9157d92SDimitry Andric // Shift the boolean value left (by the condition's ID) to form a bitmap.
1035c9157d92SDimitry Andric // %2 = shl i32 %1, <Update->getCondID()>
1036c9157d92SDimitry Andric auto *ShiftedVal = Builder.CreateShl(CondV_32, Update->getCondID());
1037c9157d92SDimitry Andric
1038c9157d92SDimitry Andric // Perform logical OR of the bitmap against the loaded MCDC temporary value.
1039c9157d92SDimitry Andric // %3 = or i32 %mcdc.temp, %2
1040c9157d92SDimitry Andric auto *Result = Builder.CreateOr(Temp, ShiftedVal);
1041c9157d92SDimitry Andric
1042c9157d92SDimitry Andric // Store the updated temporary value back to the stack.
1043c9157d92SDimitry Andric // store i32 %3, ptr %mcdc.addr, align 4
1044c9157d92SDimitry Andric Builder.CreateStore(Result, MCDCCondBitmapAddr);
1045c9157d92SDimitry Andric Update->eraseFromParent();
1046c9157d92SDimitry Andric }
1047c9157d92SDimitry Andric
10480b57cec5SDimitry Andric /// Get the name of a profiling variable for a particular function.
getVarName(InstrProfInstBase * Inc,StringRef Prefix,bool & Renamed)104904eeddc0SDimitry Andric static std::string getVarName(InstrProfInstBase *Inc, StringRef Prefix,
1050349cc55cSDimitry Andric bool &Renamed) {
10510b57cec5SDimitry Andric StringRef NamePrefix = getInstrProfNameVarPrefix();
10520b57cec5SDimitry Andric StringRef Name = Inc->getName()->getName().substr(NamePrefix.size());
10530b57cec5SDimitry Andric Function *F = Inc->getParent()->getParent();
10540b57cec5SDimitry Andric Module *M = F->getParent();
10550b57cec5SDimitry Andric if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) ||
1056349cc55cSDimitry Andric !canRenameComdatFunc(*F)) {
1057349cc55cSDimitry Andric Renamed = false;
10580b57cec5SDimitry Andric return (Prefix + Name).str();
1059349cc55cSDimitry Andric }
1060349cc55cSDimitry Andric Renamed = true;
10610b57cec5SDimitry Andric uint64_t FuncHash = Inc->getHash()->getZExtValue();
10620b57cec5SDimitry Andric SmallVector<char, 24> HashPostfix;
1063c9157d92SDimitry Andric if (Name.ends_with((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix)))
10640b57cec5SDimitry Andric return (Prefix + Name).str();
10650b57cec5SDimitry Andric return (Prefix + Name + "." + Twine(FuncHash)).str();
10660b57cec5SDimitry Andric }
10670b57cec5SDimitry Andric
getIntModuleFlagOrZero(const Module & M,StringRef Flag)1068fe6060f1SDimitry Andric static uint64_t getIntModuleFlagOrZero(const Module &M, StringRef Flag) {
1069fe6060f1SDimitry Andric auto *MD = dyn_cast_or_null<ConstantAsMetadata>(M.getModuleFlag(Flag));
1070fe6060f1SDimitry Andric if (!MD)
1071fe6060f1SDimitry Andric return 0;
1072fe6060f1SDimitry Andric
1073fe6060f1SDimitry Andric // If the flag is a ConstantAsMetadata, it should be an integer representable
1074fe6060f1SDimitry Andric // in 64-bits.
1075fe6060f1SDimitry Andric return cast<ConstantInt>(MD->getValue())->getZExtValue();
1076fe6060f1SDimitry Andric }
1077fe6060f1SDimitry Andric
enablesValueProfiling(const Module & M)1078fe6060f1SDimitry Andric static bool enablesValueProfiling(const Module &M) {
1079fe6060f1SDimitry Andric return isIRPGOFlagSet(&M) ||
1080fe6060f1SDimitry Andric getIntModuleFlagOrZero(M, "EnableValueProfiling") != 0;
1081fe6060f1SDimitry Andric }
1082fe6060f1SDimitry Andric
1083fe6060f1SDimitry Andric // Conservatively returns true if data variables may be referenced by code.
profDataReferencedByCode(const Module & M)1084fe6060f1SDimitry Andric static bool profDataReferencedByCode(const Module &M) {
1085fe6060f1SDimitry Andric return enablesValueProfiling(M);
1086fe6060f1SDimitry Andric }
1087fe6060f1SDimitry Andric
shouldRecordFunctionAddr(Function * F)10880b57cec5SDimitry Andric static inline bool shouldRecordFunctionAddr(Function *F) {
1089fe6060f1SDimitry Andric // Only record function addresses if IR PGO is enabled or if clang value
1090fe6060f1SDimitry Andric // profiling is enabled. Recording function addresses greatly increases object
1091fe6060f1SDimitry Andric // file size, because it prevents the inliner from deleting functions that
1092fe6060f1SDimitry Andric // have been inlined everywhere.
1093fe6060f1SDimitry Andric if (!profDataReferencedByCode(*F->getParent()))
1094fe6060f1SDimitry Andric return false;
1095fe6060f1SDimitry Andric
10960b57cec5SDimitry Andric // Check the linkage
10970b57cec5SDimitry Andric bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage();
10980b57cec5SDimitry Andric if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
10990b57cec5SDimitry Andric !HasAvailableExternallyLinkage)
11000b57cec5SDimitry Andric return true;
11010b57cec5SDimitry Andric
11020b57cec5SDimitry Andric // A function marked 'alwaysinline' with available_externally linkage can't
11030b57cec5SDimitry Andric // have its address taken. Doing so would create an undefined external ref to
11040b57cec5SDimitry Andric // the function, which would fail to link.
11050b57cec5SDimitry Andric if (HasAvailableExternallyLinkage &&
11060b57cec5SDimitry Andric F->hasFnAttribute(Attribute::AlwaysInline))
11070b57cec5SDimitry Andric return false;
11080b57cec5SDimitry Andric
11090b57cec5SDimitry Andric // Prohibit function address recording if the function is both internal and
11100b57cec5SDimitry Andric // COMDAT. This avoids the profile data variable referencing internal symbols
11110b57cec5SDimitry Andric // in COMDAT.
11120b57cec5SDimitry Andric if (F->hasLocalLinkage() && F->hasComdat())
11130b57cec5SDimitry Andric return false;
11140b57cec5SDimitry Andric
11150b57cec5SDimitry Andric // Check uses of this function for other than direct calls or invokes to it.
11160b57cec5SDimitry Andric // Inline virtual functions have linkeOnceODR linkage. When a key method
11170b57cec5SDimitry Andric // exists, the vtable will only be emitted in the TU where the key method
11180b57cec5SDimitry Andric // is defined. In a TU where vtable is not available, the function won't
11190b57cec5SDimitry Andric // be 'addresstaken'. If its address is not recorded here, the profile data
11200b57cec5SDimitry Andric // with missing address may be picked by the linker leading to missing
11210b57cec5SDimitry Andric // indirect call target info.
11220b57cec5SDimitry Andric return F->hasAddressTaken() || F->hasLinkOnceLinkage();
11230b57cec5SDimitry Andric }
11240b57cec5SDimitry Andric
shouldUsePublicSymbol(Function * Fn)1125fe013be4SDimitry Andric static inline bool shouldUsePublicSymbol(Function *Fn) {
1126fe013be4SDimitry Andric // It isn't legal to make an alias of this function at all
1127fe013be4SDimitry Andric if (Fn->isDeclarationForLinker())
1128fe013be4SDimitry Andric return true;
1129fe013be4SDimitry Andric
1130fe013be4SDimitry Andric // Symbols with local linkage can just use the symbol directly without
1131fe013be4SDimitry Andric // introducing relocations
1132fe013be4SDimitry Andric if (Fn->hasLocalLinkage())
1133fe013be4SDimitry Andric return true;
1134fe013be4SDimitry Andric
1135fe013be4SDimitry Andric // PGO + ThinLTO + CFI cause duplicate symbols to be introduced due to some
1136fe013be4SDimitry Andric // unfavorable interaction between the new alias and the alias renaming done
1137fe013be4SDimitry Andric // in LowerTypeTests under ThinLTO. For comdat functions that would normally
1138fe013be4SDimitry Andric // be deduplicated, but the renaming scheme ends up preventing renaming, since
1139fe013be4SDimitry Andric // it creates unique names for each alias, resulting in duplicated symbols. In
1140fe013be4SDimitry Andric // the future, we should update the CFI related passes to migrate these
1141fe013be4SDimitry Andric // aliases to the same module as the jump-table they refer to will be defined.
1142fe013be4SDimitry Andric if (Fn->hasMetadata(LLVMContext::MD_type))
1143fe013be4SDimitry Andric return true;
1144fe013be4SDimitry Andric
1145fe013be4SDimitry Andric // For comdat functions, an alias would need the same linkage as the original
1146fe013be4SDimitry Andric // function and hidden visibility. There is no point in adding an alias with
1147fe013be4SDimitry Andric // identical linkage an visibility to avoid introducing symbolic relocations.
1148fe013be4SDimitry Andric if (Fn->hasComdat() &&
1149fe013be4SDimitry Andric (Fn->getVisibility() == GlobalValue::VisibilityTypes::HiddenVisibility))
1150fe013be4SDimitry Andric return true;
1151fe013be4SDimitry Andric
1152fe013be4SDimitry Andric // its OK to use an alias
1153fe013be4SDimitry Andric return false;
1154fe013be4SDimitry Andric }
1155fe013be4SDimitry Andric
getFuncAddrForProfData(Function * Fn)1156fe013be4SDimitry Andric static inline Constant *getFuncAddrForProfData(Function *Fn) {
1157c9157d92SDimitry Andric auto *Int8PtrTy = PointerType::getUnqual(Fn->getContext());
1158fe013be4SDimitry Andric // Store a nullptr in __llvm_profd, if we shouldn't use a real address
1159fe013be4SDimitry Andric if (!shouldRecordFunctionAddr(Fn))
1160fe013be4SDimitry Andric return ConstantPointerNull::get(Int8PtrTy);
1161fe013be4SDimitry Andric
1162fe013be4SDimitry Andric // If we can't use an alias, we must use the public symbol, even though this
1163fe013be4SDimitry Andric // may require a symbolic relocation.
1164fe013be4SDimitry Andric if (shouldUsePublicSymbol(Fn))
1165c9157d92SDimitry Andric return Fn;
1166fe013be4SDimitry Andric
1167fe013be4SDimitry Andric // When possible use a private alias to avoid symbolic relocations.
1168fe013be4SDimitry Andric auto *GA = GlobalAlias::create(GlobalValue::LinkageTypes::PrivateLinkage,
1169fe013be4SDimitry Andric Fn->getName() + ".local", Fn);
1170fe013be4SDimitry Andric
1171fe013be4SDimitry Andric // When the instrumented function is a COMDAT function, we cannot use a
1172fe013be4SDimitry Andric // private alias. If we did, we would create reference to a local label in
1173fe013be4SDimitry Andric // this function's section. If this version of the function isn't selected by
1174fe013be4SDimitry Andric // the linker, then the metadata would introduce a reference to a discarded
1175fe013be4SDimitry Andric // section. So, for COMDAT functions, we need to adjust the linkage of the
1176fe013be4SDimitry Andric // alias. Using hidden visibility avoids a dynamic relocation and an entry in
1177fe013be4SDimitry Andric // the dynamic symbol table.
1178fe013be4SDimitry Andric //
1179fe013be4SDimitry Andric // Note that this handles COMDAT functions with visibility other than Hidden,
1180fe013be4SDimitry Andric // since that case is covered in shouldUsePublicSymbol()
1181fe013be4SDimitry Andric if (Fn->hasComdat()) {
1182fe013be4SDimitry Andric GA->setLinkage(Fn->getLinkage());
1183fe013be4SDimitry Andric GA->setVisibility(GlobalValue::VisibilityTypes::HiddenVisibility);
1184fe013be4SDimitry Andric }
1185fe013be4SDimitry Andric
1186fe013be4SDimitry Andric // appendToCompilerUsed(*Fn->getParent(), {GA});
1187fe013be4SDimitry Andric
1188c9157d92SDimitry Andric return GA;
1189fe013be4SDimitry Andric }
1190fe013be4SDimitry Andric
needsRuntimeRegistrationOfSectionRange(const Triple & TT)11910b57cec5SDimitry Andric static bool needsRuntimeRegistrationOfSectionRange(const Triple &TT) {
1192*cdc20ff6SDimitry Andric // compiler-rt uses linker support to get data/counters/name start/end for
1193*cdc20ff6SDimitry Andric // ELF, COFF, Mach-O and XCOFF.
1194*cdc20ff6SDimitry Andric if (TT.isOSBinFormatELF() || TT.isOSBinFormatCOFF() ||
1195*cdc20ff6SDimitry Andric TT.isOSBinFormatMachO() || TT.isOSBinFormatXCOFF())
11960b57cec5SDimitry Andric return false;
11970b57cec5SDimitry Andric
11980b57cec5SDimitry Andric return true;
11990b57cec5SDimitry Andric }
12000b57cec5SDimitry Andric
maybeSetComdat(GlobalVariable * GV,Function * Fn,StringRef VarName)1201c9157d92SDimitry Andric void InstrLowerer::maybeSetComdat(GlobalVariable *GV, Function *Fn,
1202c9157d92SDimitry Andric StringRef VarName) {
1203c9157d92SDimitry Andric bool DataReferencedByCode = profDataReferencedByCode(M);
1204c9157d92SDimitry Andric bool NeedComdat = needsComdatForCounter(*Fn, M);
1205c9157d92SDimitry Andric bool UseComdat = (NeedComdat || TT.isOSBinFormatELF());
1206c9157d92SDimitry Andric
1207c9157d92SDimitry Andric if (!UseComdat)
1208c9157d92SDimitry Andric return;
1209c9157d92SDimitry Andric
1210c9157d92SDimitry Andric StringRef GroupName =
1211c9157d92SDimitry Andric TT.isOSBinFormatCOFF() && DataReferencedByCode ? GV->getName() : VarName;
1212c9157d92SDimitry Andric Comdat *C = M.getOrInsertComdat(GroupName);
1213c9157d92SDimitry Andric if (!NeedComdat)
1214c9157d92SDimitry Andric C->setSelectionKind(Comdat::NoDeduplicate);
1215c9157d92SDimitry Andric GV->setComdat(C);
1216c9157d92SDimitry Andric // COFF doesn't allow the comdat group leader to have private linkage, so
1217c9157d92SDimitry Andric // upgrade private linkage to internal linkage to produce a symbol table
1218c9157d92SDimitry Andric // entry.
1219c9157d92SDimitry Andric if (TT.isOSBinFormatCOFF() && GV->hasPrivateLinkage())
1220c9157d92SDimitry Andric GV->setLinkage(GlobalValue::InternalLinkage);
12211fd87a68SDimitry Andric }
12221fd87a68SDimitry Andric
setupProfileSection(InstrProfInstBase * Inc,InstrProfSectKind IPSK)1223c9157d92SDimitry Andric GlobalVariable *InstrLowerer::setupProfileSection(InstrProfInstBase *Inc,
1224c9157d92SDimitry Andric InstrProfSectKind IPSK) {
12250b57cec5SDimitry Andric GlobalVariable *NamePtr = Inc->getName();
12260b57cec5SDimitry Andric
1227fe6060f1SDimitry Andric // Match the linkage and visibility of the name global.
12280b57cec5SDimitry Andric Function *Fn = Inc->getParent()->getParent();
12290b57cec5SDimitry Andric GlobalValue::LinkageTypes Linkage = NamePtr->getLinkage();
12300b57cec5SDimitry Andric GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
12310b57cec5SDimitry Andric
12320eae32dcSDimitry Andric // Use internal rather than private linkage so the counter variable shows up
12330eae32dcSDimitry Andric // in the symbol table when using debug info for correlation.
1234c9157d92SDimitry Andric if ((DebugInfoCorrelate ||
1235c9157d92SDimitry Andric ProfileCorrelate == InstrProfCorrelator::DEBUG_INFO) &&
1236c9157d92SDimitry Andric TT.isOSBinFormatMachO() && Linkage == GlobalValue::PrivateLinkage)
12370eae32dcSDimitry Andric Linkage = GlobalValue::InternalLinkage;
12380eae32dcSDimitry Andric
1239349cc55cSDimitry Andric // Due to the limitation of binder as of 2021/09/28, the duplicate weak
1240349cc55cSDimitry Andric // symbols in the same csect won't be discarded. When there are duplicate weak
1241349cc55cSDimitry Andric // symbols, we can NOT guarantee that the relocations get resolved to the
1242349cc55cSDimitry Andric // intended weak symbol, so we can not ensure the correctness of the relative
1243349cc55cSDimitry Andric // CounterPtr, so we have to use private linkage for counter and data symbols.
1244349cc55cSDimitry Andric if (TT.isOSBinFormatXCOFF()) {
1245349cc55cSDimitry Andric Linkage = GlobalValue::PrivateLinkage;
1246349cc55cSDimitry Andric Visibility = GlobalValue::DefaultVisibility;
1247349cc55cSDimitry Andric }
12480b57cec5SDimitry Andric // Move the name variable to the right section. Place them in a COMDAT group
12490b57cec5SDimitry Andric // if the associated function is a COMDAT. This will make sure that only one
12500b57cec5SDimitry Andric // copy of counters of the COMDAT function will be emitted after linking. Keep
12510b57cec5SDimitry Andric // in mind that this pass may run before the inliner, so we need to create a
12520b57cec5SDimitry Andric // new comdat group for the counters and profiling data. If we use the comdat
12530b57cec5SDimitry Andric // of the parent function, that will result in relocations against discarded
12540b57cec5SDimitry Andric // sections.
1255fe6060f1SDimitry Andric //
1256fe6060f1SDimitry Andric // If the data variable is referenced by code, counters and data have to be
1257fe6060f1SDimitry Andric // in different comdats for COFF because the Visual C++ linker will report
1258fe6060f1SDimitry Andric // duplicate symbol errors if there are multiple external symbols with the
1259fe6060f1SDimitry Andric // same name marked IMAGE_COMDAT_SELECT_ASSOCIATIVE.
1260fe6060f1SDimitry Andric //
1261fe6060f1SDimitry Andric // For ELF, when not using COMDAT, put counters, data and values into a
1262fe6060f1SDimitry Andric // nodeduplicate COMDAT which is lowered to a zero-flag section group. This
1263fe6060f1SDimitry Andric // allows -z start-stop-gc to discard the entire group when the function is
1264fe6060f1SDimitry Andric // discarded.
1265349cc55cSDimitry Andric bool Renamed;
1266c9157d92SDimitry Andric GlobalVariable *Ptr;
1267c9157d92SDimitry Andric StringRef VarPrefix;
1268c9157d92SDimitry Andric std::string VarName;
1269c9157d92SDimitry Andric if (IPSK == IPSK_cnts) {
1270c9157d92SDimitry Andric VarPrefix = getInstrProfCountersVarPrefix();
1271c9157d92SDimitry Andric VarName = getVarName(Inc, VarPrefix, Renamed);
1272c9157d92SDimitry Andric InstrProfCntrInstBase *CntrIncrement = dyn_cast<InstrProfCntrInstBase>(Inc);
1273c9157d92SDimitry Andric Ptr = createRegionCounters(CntrIncrement, VarName, Linkage);
1274c9157d92SDimitry Andric } else if (IPSK == IPSK_bitmap) {
1275c9157d92SDimitry Andric VarPrefix = getInstrProfBitmapVarPrefix();
1276c9157d92SDimitry Andric VarName = getVarName(Inc, VarPrefix, Renamed);
1277c9157d92SDimitry Andric InstrProfMCDCBitmapInstBase *BitmapUpdate =
1278c9157d92SDimitry Andric dyn_cast<InstrProfMCDCBitmapInstBase>(Inc);
1279c9157d92SDimitry Andric Ptr = createRegionBitmaps(BitmapUpdate, VarName, Linkage);
1280c9157d92SDimitry Andric } else {
1281c9157d92SDimitry Andric llvm_unreachable("Profile Section must be for Counters or Bitmaps");
1282fe6060f1SDimitry Andric }
12830b57cec5SDimitry Andric
1284c9157d92SDimitry Andric Ptr->setVisibility(Visibility);
1285c9157d92SDimitry Andric // Put the counters and bitmaps in their own sections so linkers can
1286c9157d92SDimitry Andric // remove unneeded sections.
1287c9157d92SDimitry Andric Ptr->setSection(getInstrProfSectionName(IPSK, TT.getObjectFormat()));
1288c9157d92SDimitry Andric Ptr->setLinkage(Linkage);
1289c9157d92SDimitry Andric maybeSetComdat(Ptr, Fn, VarName);
1290c9157d92SDimitry Andric return Ptr;
1291c9157d92SDimitry Andric }
1292c9157d92SDimitry Andric
1293c9157d92SDimitry Andric GlobalVariable *
createRegionBitmaps(InstrProfMCDCBitmapInstBase * Inc,StringRef Name,GlobalValue::LinkageTypes Linkage)1294c9157d92SDimitry Andric InstrLowerer::createRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc,
1295c9157d92SDimitry Andric StringRef Name,
1296c9157d92SDimitry Andric GlobalValue::LinkageTypes Linkage) {
1297c9157d92SDimitry Andric uint64_t NumBytes = Inc->getNumBitmapBytes()->getZExtValue();
1298c9157d92SDimitry Andric auto *BitmapTy = ArrayType::get(Type::getInt8Ty(M.getContext()), NumBytes);
1299c9157d92SDimitry Andric auto GV = new GlobalVariable(M, BitmapTy, false, Linkage,
1300c9157d92SDimitry Andric Constant::getNullValue(BitmapTy), Name);
1301c9157d92SDimitry Andric GV->setAlignment(Align(1));
1302c9157d92SDimitry Andric return GV;
1303c9157d92SDimitry Andric }
1304c9157d92SDimitry Andric
1305c9157d92SDimitry Andric GlobalVariable *
getOrCreateRegionBitmaps(InstrProfMCDCBitmapInstBase * Inc)1306c9157d92SDimitry Andric InstrLowerer::getOrCreateRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc) {
1307c9157d92SDimitry Andric GlobalVariable *NamePtr = Inc->getName();
1308c9157d92SDimitry Andric auto &PD = ProfileDataMap[NamePtr];
1309c9157d92SDimitry Andric if (PD.RegionBitmaps)
1310c9157d92SDimitry Andric return PD.RegionBitmaps;
1311c9157d92SDimitry Andric
1312c9157d92SDimitry Andric // If RegionBitmaps doesn't already exist, create it by first setting up
1313c9157d92SDimitry Andric // the corresponding profile section.
1314c9157d92SDimitry Andric auto *BitmapPtr = setupProfileSection(Inc, IPSK_bitmap);
1315c9157d92SDimitry Andric PD.RegionBitmaps = BitmapPtr;
1316c9157d92SDimitry Andric PD.NumBitmapBytes = Inc->getNumBitmapBytes()->getZExtValue();
1317c9157d92SDimitry Andric return PD.RegionBitmaps;
1318c9157d92SDimitry Andric }
1319c9157d92SDimitry Andric
1320c9157d92SDimitry Andric GlobalVariable *
createRegionCounters(InstrProfCntrInstBase * Inc,StringRef Name,GlobalValue::LinkageTypes Linkage)1321c9157d92SDimitry Andric InstrLowerer::createRegionCounters(InstrProfCntrInstBase *Inc, StringRef Name,
1322c9157d92SDimitry Andric GlobalValue::LinkageTypes Linkage) {
13230b57cec5SDimitry Andric uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
1324c9157d92SDimitry Andric auto &Ctx = M.getContext();
1325c9157d92SDimitry Andric GlobalVariable *GV;
1326c9157d92SDimitry Andric if (isa<InstrProfCoverInst>(Inc)) {
1327c9157d92SDimitry Andric auto *CounterTy = Type::getInt8Ty(Ctx);
1328c9157d92SDimitry Andric auto *CounterArrTy = ArrayType::get(CounterTy, NumCounters);
1329c9157d92SDimitry Andric // TODO: `Constant::getAllOnesValue()` does not yet accept an array type.
1330c9157d92SDimitry Andric std::vector<Constant *> InitialValues(NumCounters,
1331c9157d92SDimitry Andric Constant::getAllOnesValue(CounterTy));
1332c9157d92SDimitry Andric GV = new GlobalVariable(M, CounterArrTy, false, Linkage,
1333c9157d92SDimitry Andric ConstantArray::get(CounterArrTy, InitialValues),
1334c9157d92SDimitry Andric Name);
1335c9157d92SDimitry Andric GV->setAlignment(Align(1));
1336c9157d92SDimitry Andric } else {
1337c9157d92SDimitry Andric auto *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
1338c9157d92SDimitry Andric GV = new GlobalVariable(M, CounterTy, false, Linkage,
1339c9157d92SDimitry Andric Constant::getNullValue(CounterTy), Name);
1340c9157d92SDimitry Andric GV->setAlignment(Align(8));
1341c9157d92SDimitry Andric }
1342c9157d92SDimitry Andric return GV;
1343c9157d92SDimitry Andric }
13440b57cec5SDimitry Andric
1345c9157d92SDimitry Andric GlobalVariable *
getOrCreateRegionCounters(InstrProfCntrInstBase * Inc)1346c9157d92SDimitry Andric InstrLowerer::getOrCreateRegionCounters(InstrProfCntrInstBase *Inc) {
1347c9157d92SDimitry Andric GlobalVariable *NamePtr = Inc->getName();
1348c9157d92SDimitry Andric auto &PD = ProfileDataMap[NamePtr];
1349c9157d92SDimitry Andric if (PD.RegionCounters)
1350c9157d92SDimitry Andric return PD.RegionCounters;
1351c9157d92SDimitry Andric
1352c9157d92SDimitry Andric // If RegionCounters doesn't already exist, create it by first setting up
1353c9157d92SDimitry Andric // the corresponding profile section.
1354c9157d92SDimitry Andric auto *CounterPtr = setupProfileSection(Inc, IPSK_cnts);
13554824e7fdSDimitry Andric PD.RegionCounters = CounterPtr;
1356c9157d92SDimitry Andric
1357c9157d92SDimitry Andric if (DebugInfoCorrelate ||
1358c9157d92SDimitry Andric ProfileCorrelate == InstrProfCorrelator::DEBUG_INFO) {
1359c9157d92SDimitry Andric LLVMContext &Ctx = M.getContext();
1360c9157d92SDimitry Andric Function *Fn = Inc->getParent()->getParent();
13610eae32dcSDimitry Andric if (auto *SP = Fn->getSubprogram()) {
1362c9157d92SDimitry Andric DIBuilder DB(M, true, SP->getUnit());
13630eae32dcSDimitry Andric Metadata *FunctionNameAnnotation[] = {
13640eae32dcSDimitry Andric MDString::get(Ctx, InstrProfCorrelator::FunctionNameAttributeName),
13650eae32dcSDimitry Andric MDString::get(Ctx, getPGOFuncNameVarInitializer(NamePtr)),
13660eae32dcSDimitry Andric };
13670eae32dcSDimitry Andric Metadata *CFGHashAnnotation[] = {
13680eae32dcSDimitry Andric MDString::get(Ctx, InstrProfCorrelator::CFGHashAttributeName),
13690eae32dcSDimitry Andric ConstantAsMetadata::get(Inc->getHash()),
13700eae32dcSDimitry Andric };
13710eae32dcSDimitry Andric Metadata *NumCountersAnnotation[] = {
13720eae32dcSDimitry Andric MDString::get(Ctx, InstrProfCorrelator::NumCountersAttributeName),
13730eae32dcSDimitry Andric ConstantAsMetadata::get(Inc->getNumCounters()),
13740eae32dcSDimitry Andric };
13750eae32dcSDimitry Andric auto Annotations = DB.getOrCreateArray({
13760eae32dcSDimitry Andric MDNode::get(Ctx, FunctionNameAnnotation),
13770eae32dcSDimitry Andric MDNode::get(Ctx, CFGHashAnnotation),
13780eae32dcSDimitry Andric MDNode::get(Ctx, NumCountersAnnotation),
13790eae32dcSDimitry Andric });
13800eae32dcSDimitry Andric auto *DICounter = DB.createGlobalVariableExpression(
13810eae32dcSDimitry Andric SP, CounterPtr->getName(), /*LinkageName=*/StringRef(), SP->getFile(),
13820eae32dcSDimitry Andric /*LineNo=*/0, DB.createUnspecifiedType("Profile Data Type"),
13830eae32dcSDimitry Andric CounterPtr->hasLocalLinkage(), /*IsDefined=*/true, /*Expr=*/nullptr,
13840eae32dcSDimitry Andric /*Decl=*/nullptr, /*TemplateParams=*/nullptr, /*AlignInBits=*/0,
13850eae32dcSDimitry Andric Annotations);
13860eae32dcSDimitry Andric CounterPtr->addDebugInfo(DICounter);
13870eae32dcSDimitry Andric DB.finalize();
13880eae32dcSDimitry Andric }
13890b57cec5SDimitry Andric
1390c9157d92SDimitry Andric // Mark the counter variable as used so that it isn't optimized out.
1391c9157d92SDimitry Andric CompilerUsedVars.push_back(PD.RegionCounters);
1392c9157d92SDimitry Andric }
1393c9157d92SDimitry Andric
1394c9157d92SDimitry Andric // Create the data variable (if it doesn't already exist).
1395c9157d92SDimitry Andric createDataVariable(Inc);
1396c9157d92SDimitry Andric
1397c9157d92SDimitry Andric return PD.RegionCounters;
1398c9157d92SDimitry Andric }
1399c9157d92SDimitry Andric
createDataVariable(InstrProfCntrInstBase * Inc)1400c9157d92SDimitry Andric void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) {
1401c9157d92SDimitry Andric // When debug information is correlated to profile data, a data variable
1402c9157d92SDimitry Andric // is not needed.
1403c9157d92SDimitry Andric if (DebugInfoCorrelate || ProfileCorrelate == InstrProfCorrelator::DEBUG_INFO)
1404c9157d92SDimitry Andric return;
1405c9157d92SDimitry Andric
1406c9157d92SDimitry Andric GlobalVariable *NamePtr = Inc->getName();
1407c9157d92SDimitry Andric auto &PD = ProfileDataMap[NamePtr];
1408c9157d92SDimitry Andric
1409c9157d92SDimitry Andric // Return if data variable was already created.
1410c9157d92SDimitry Andric if (PD.DataVar)
1411c9157d92SDimitry Andric return;
1412c9157d92SDimitry Andric
1413c9157d92SDimitry Andric LLVMContext &Ctx = M.getContext();
1414c9157d92SDimitry Andric
1415c9157d92SDimitry Andric Function *Fn = Inc->getParent()->getParent();
1416c9157d92SDimitry Andric GlobalValue::LinkageTypes Linkage = NamePtr->getLinkage();
1417c9157d92SDimitry Andric GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
1418c9157d92SDimitry Andric
1419c9157d92SDimitry Andric // Due to the limitation of binder as of 2021/09/28, the duplicate weak
1420c9157d92SDimitry Andric // symbols in the same csect won't be discarded. When there are duplicate weak
1421c9157d92SDimitry Andric // symbols, we can NOT guarantee that the relocations get resolved to the
1422c9157d92SDimitry Andric // intended weak symbol, so we can not ensure the correctness of the relative
1423c9157d92SDimitry Andric // CounterPtr, so we have to use private linkage for counter and data symbols.
1424c9157d92SDimitry Andric if (TT.isOSBinFormatXCOFF()) {
1425c9157d92SDimitry Andric Linkage = GlobalValue::PrivateLinkage;
1426c9157d92SDimitry Andric Visibility = GlobalValue::DefaultVisibility;
1427c9157d92SDimitry Andric }
1428c9157d92SDimitry Andric
1429c9157d92SDimitry Andric bool DataReferencedByCode = profDataReferencedByCode(M);
1430c9157d92SDimitry Andric bool NeedComdat = needsComdatForCounter(*Fn, M);
1431c9157d92SDimitry Andric bool Renamed;
1432c9157d92SDimitry Andric
1433c9157d92SDimitry Andric // The Data Variable section is anchored to profile counters.
1434c9157d92SDimitry Andric std::string CntsVarName =
1435c9157d92SDimitry Andric getVarName(Inc, getInstrProfCountersVarPrefix(), Renamed);
1436c9157d92SDimitry Andric std::string DataVarName =
1437c9157d92SDimitry Andric getVarName(Inc, getInstrProfDataVarPrefix(), Renamed);
1438c9157d92SDimitry Andric
1439c9157d92SDimitry Andric auto *Int8PtrTy = PointerType::getUnqual(Ctx);
14400b57cec5SDimitry Andric // Allocate statically the array of pointers to value profile nodes for
14410b57cec5SDimitry Andric // the current function.
14420b57cec5SDimitry Andric Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy);
14430b57cec5SDimitry Andric uint64_t NS = 0;
14440b57cec5SDimitry Andric for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
14450b57cec5SDimitry Andric NS += PD.NumValueSites[Kind];
1446fe6060f1SDimitry Andric if (NS > 0 && ValueProfileStaticAlloc &&
1447fe6060f1SDimitry Andric !needsRuntimeRegistrationOfSectionRange(TT)) {
14480b57cec5SDimitry Andric ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS);
1449fe6060f1SDimitry Andric auto *ValuesVar = new GlobalVariable(
1450c9157d92SDimitry Andric M, ValuesTy, false, Linkage, Constant::getNullValue(ValuesTy),
1451349cc55cSDimitry Andric getVarName(Inc, getInstrProfValuesVarPrefix(), Renamed));
14520b57cec5SDimitry Andric ValuesVar->setVisibility(Visibility);
1453c9157d92SDimitry Andric setGlobalVariableLargeSection(TT, *ValuesVar);
14540b57cec5SDimitry Andric ValuesVar->setSection(
14550b57cec5SDimitry Andric getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
14568bcb0991SDimitry Andric ValuesVar->setAlignment(Align(8));
1457c9157d92SDimitry Andric maybeSetComdat(ValuesVar, Fn, CntsVarName);
1458c9157d92SDimitry Andric ValuesPtrExpr = ValuesVar;
14590b57cec5SDimitry Andric }
14600b57cec5SDimitry Andric
1461c9157d92SDimitry Andric uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
1462c9157d92SDimitry Andric auto *CounterPtr = PD.RegionCounters;
1463c9157d92SDimitry Andric
1464c9157d92SDimitry Andric uint64_t NumBitmapBytes = PD.NumBitmapBytes;
14650eae32dcSDimitry Andric
14660b57cec5SDimitry Andric // Create data variable.
1467c9157d92SDimitry Andric auto *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext());
14680b57cec5SDimitry Andric auto *Int16Ty = Type::getInt16Ty(Ctx);
14690b57cec5SDimitry Andric auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1);
14700b57cec5SDimitry Andric Type *DataTypes[] = {
14710b57cec5SDimitry Andric #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType,
14720b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProfData.inc"
14730b57cec5SDimitry Andric };
1474bdd1243dSDimitry Andric auto *DataTy = StructType::get(Ctx, ArrayRef(DataTypes));
14750b57cec5SDimitry Andric
1476fe013be4SDimitry Andric Constant *FunctionAddr = getFuncAddrForProfData(Fn);
14770b57cec5SDimitry Andric
14780b57cec5SDimitry Andric Constant *Int16ArrayVals[IPVK_Last + 1];
14790b57cec5SDimitry Andric for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
14800b57cec5SDimitry Andric Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]);
14810b57cec5SDimitry Andric
1482fe6060f1SDimitry Andric // If the data variable is not referenced by code (if we don't emit
1483fe6060f1SDimitry Andric // @llvm.instrprof.value.profile, NS will be 0), and the counter keeps the
1484fe6060f1SDimitry Andric // data variable live under linker GC, the data variable can be private. This
1485fe6060f1SDimitry Andric // optimization applies to ELF.
1486fe6060f1SDimitry Andric //
1487fe6060f1SDimitry Andric // On COFF, a comdat leader cannot be local so we require DataReferencedByCode
1488fe6060f1SDimitry Andric // to be false.
1489349cc55cSDimitry Andric //
1490349cc55cSDimitry Andric // If profd is in a deduplicate comdat, NS==0 with a hash suffix guarantees
1491349cc55cSDimitry Andric // that other copies must have the same CFG and cannot have value profiling.
1492349cc55cSDimitry Andric // If no hash suffix, other profd copies may be referenced by code.
1493349cc55cSDimitry Andric if (NS == 0 && !(DataReferencedByCode && NeedComdat && !Renamed) &&
1494349cc55cSDimitry Andric (TT.isOSBinFormatELF() ||
1495fe6060f1SDimitry Andric (!DataReferencedByCode && TT.isOSBinFormatCOFF()))) {
1496fe6060f1SDimitry Andric Linkage = GlobalValue::PrivateLinkage;
1497fe6060f1SDimitry Andric Visibility = GlobalValue::DefaultVisibility;
1498fe6060f1SDimitry Andric }
1499e8d8bef9SDimitry Andric auto *Data =
1500c9157d92SDimitry Andric new GlobalVariable(M, DataTy, false, Linkage, nullptr, DataVarName);
1501c9157d92SDimitry Andric Constant *RelativeCounterPtr;
1502c9157d92SDimitry Andric GlobalVariable *BitmapPtr = PD.RegionBitmaps;
1503c9157d92SDimitry Andric Constant *RelativeBitmapPtr = ConstantInt::get(IntPtrTy, 0);
1504c9157d92SDimitry Andric InstrProfSectKind DataSectionKind;
1505c9157d92SDimitry Andric // With binary profile correlation, profile data is not loaded into memory.
1506c9157d92SDimitry Andric // profile data must reference profile counter with an absolute relocation.
1507c9157d92SDimitry Andric if (ProfileCorrelate == InstrProfCorrelator::BINARY) {
1508c9157d92SDimitry Andric DataSectionKind = IPSK_covdata;
1509c9157d92SDimitry Andric RelativeCounterPtr = ConstantExpr::getPtrToInt(CounterPtr, IntPtrTy);
1510c9157d92SDimitry Andric if (BitmapPtr != nullptr)
1511c9157d92SDimitry Andric RelativeBitmapPtr = ConstantExpr::getPtrToInt(BitmapPtr, IntPtrTy);
1512c9157d92SDimitry Andric } else {
1513349cc55cSDimitry Andric // Reference the counter variable with a label difference (link-time
1514349cc55cSDimitry Andric // constant).
1515c9157d92SDimitry Andric DataSectionKind = IPSK_data;
1516c9157d92SDimitry Andric RelativeCounterPtr =
1517349cc55cSDimitry Andric ConstantExpr::getSub(ConstantExpr::getPtrToInt(CounterPtr, IntPtrTy),
1518349cc55cSDimitry Andric ConstantExpr::getPtrToInt(Data, IntPtrTy));
1519c9157d92SDimitry Andric if (BitmapPtr != nullptr)
1520c9157d92SDimitry Andric RelativeBitmapPtr =
1521c9157d92SDimitry Andric ConstantExpr::getSub(ConstantExpr::getPtrToInt(BitmapPtr, IntPtrTy),
1522c9157d92SDimitry Andric ConstantExpr::getPtrToInt(Data, IntPtrTy));
1523c9157d92SDimitry Andric }
1524349cc55cSDimitry Andric
1525349cc55cSDimitry Andric Constant *DataVals[] = {
1526349cc55cSDimitry Andric #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init,
1527349cc55cSDimitry Andric #include "llvm/ProfileData/InstrProfData.inc"
1528349cc55cSDimitry Andric };
1529349cc55cSDimitry Andric Data->setInitializer(ConstantStruct::get(DataTy, DataVals));
1530349cc55cSDimitry Andric
15310b57cec5SDimitry Andric Data->setVisibility(Visibility);
1532c9157d92SDimitry Andric Data->setSection(
1533c9157d92SDimitry Andric getInstrProfSectionName(DataSectionKind, TT.getObjectFormat()));
15348bcb0991SDimitry Andric Data->setAlignment(Align(INSTR_PROF_DATA_ALIGNMENT));
1535c9157d92SDimitry Andric maybeSetComdat(Data, Fn, CntsVarName);
15360b57cec5SDimitry Andric
15370b57cec5SDimitry Andric PD.DataVar = Data;
15380b57cec5SDimitry Andric
15390b57cec5SDimitry Andric // Mark the data variable as used so that it isn't stripped out.
1540fe6060f1SDimitry Andric CompilerUsedVars.push_back(Data);
15410b57cec5SDimitry Andric // Now that the linkage set by the FE has been passed to the data and counter
15420b57cec5SDimitry Andric // variables, reset Name variable's linkage and visibility to private so that
15430b57cec5SDimitry Andric // it can be removed later by the compiler.
15440b57cec5SDimitry Andric NamePtr->setLinkage(GlobalValue::PrivateLinkage);
15450b57cec5SDimitry Andric // Collect the referenced names to be used by emitNameData.
15460b57cec5SDimitry Andric ReferencedNames.push_back(NamePtr);
15470b57cec5SDimitry Andric }
15480b57cec5SDimitry Andric
emitVNodes()1549c9157d92SDimitry Andric void InstrLowerer::emitVNodes() {
15500b57cec5SDimitry Andric if (!ValueProfileStaticAlloc)
15510b57cec5SDimitry Andric return;
15520b57cec5SDimitry Andric
15530b57cec5SDimitry Andric // For now only support this on platforms that do
15540b57cec5SDimitry Andric // not require runtime registration to discover
15550b57cec5SDimitry Andric // named section start/end.
15560b57cec5SDimitry Andric if (needsRuntimeRegistrationOfSectionRange(TT))
15570b57cec5SDimitry Andric return;
15580b57cec5SDimitry Andric
15590b57cec5SDimitry Andric size_t TotalNS = 0;
15600b57cec5SDimitry Andric for (auto &PD : ProfileDataMap) {
15610b57cec5SDimitry Andric for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
15620b57cec5SDimitry Andric TotalNS += PD.second.NumValueSites[Kind];
15630b57cec5SDimitry Andric }
15640b57cec5SDimitry Andric
15650b57cec5SDimitry Andric if (!TotalNS)
15660b57cec5SDimitry Andric return;
15670b57cec5SDimitry Andric
15680b57cec5SDimitry Andric uint64_t NumCounters = TotalNS * NumCountersPerValueSite;
15690b57cec5SDimitry Andric // Heuristic for small programs with very few total value sites.
15700b57cec5SDimitry Andric // The default value of vp-counters-per-site is chosen based on
15710b57cec5SDimitry Andric // the observation that large apps usually have a low percentage
15720b57cec5SDimitry Andric // of value sites that actually have any profile data, and thus
15730b57cec5SDimitry Andric // the average number of counters per site is low. For small
15740b57cec5SDimitry Andric // apps with very few sites, this may not be true. Bump up the
15750b57cec5SDimitry Andric // number of counters in this case.
15760b57cec5SDimitry Andric #define INSTR_PROF_MIN_VAL_COUNTS 10
15770b57cec5SDimitry Andric if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS)
15780b57cec5SDimitry Andric NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS, (int)NumCounters * 2);
15790b57cec5SDimitry Andric
1580c9157d92SDimitry Andric auto &Ctx = M.getContext();
15810b57cec5SDimitry Andric Type *VNodeTypes[] = {
15820b57cec5SDimitry Andric #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType,
15830b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProfData.inc"
15840b57cec5SDimitry Andric };
1585bdd1243dSDimitry Andric auto *VNodeTy = StructType::get(Ctx, ArrayRef(VNodeTypes));
15860b57cec5SDimitry Andric
15870b57cec5SDimitry Andric ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters);
15880b57cec5SDimitry Andric auto *VNodesVar = new GlobalVariable(
1589c9157d92SDimitry Andric M, VNodesTy, false, GlobalValue::PrivateLinkage,
15900b57cec5SDimitry Andric Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName());
1591c9157d92SDimitry Andric setGlobalVariableLargeSection(TT, *VNodesVar);
15920b57cec5SDimitry Andric VNodesVar->setSection(
15930b57cec5SDimitry Andric getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
1594c9157d92SDimitry Andric VNodesVar->setAlignment(M.getDataLayout().getABITypeAlign(VNodesTy));
1595fe6060f1SDimitry Andric // VNodesVar is used by runtime but not referenced via relocation by other
1596fe6060f1SDimitry Andric // sections. Conservatively make it linker retained.
15970b57cec5SDimitry Andric UsedVars.push_back(VNodesVar);
15980b57cec5SDimitry Andric }
15990b57cec5SDimitry Andric
emitNameData()1600c9157d92SDimitry Andric void InstrLowerer::emitNameData() {
16010b57cec5SDimitry Andric std::string UncompressedData;
16020b57cec5SDimitry Andric
16030b57cec5SDimitry Andric if (ReferencedNames.empty())
16040b57cec5SDimitry Andric return;
16050b57cec5SDimitry Andric
16060b57cec5SDimitry Andric std::string CompressedNameStr;
16070b57cec5SDimitry Andric if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
16085ffd83dbSDimitry Andric DoInstrProfNameCompression)) {
1609349cc55cSDimitry Andric report_fatal_error(Twine(toString(std::move(E))), false);
16100b57cec5SDimitry Andric }
16110b57cec5SDimitry Andric
1612c9157d92SDimitry Andric auto &Ctx = M.getContext();
16134824e7fdSDimitry Andric auto *NamesVal =
16144824e7fdSDimitry Andric ConstantDataArray::getString(Ctx, StringRef(CompressedNameStr), false);
1615c9157d92SDimitry Andric NamesVar = new GlobalVariable(M, NamesVal->getType(), true,
16160b57cec5SDimitry Andric GlobalValue::PrivateLinkage, NamesVal,
16170b57cec5SDimitry Andric getInstrProfNamesVarName());
16180b57cec5SDimitry Andric NamesSize = CompressedNameStr.size();
1619c9157d92SDimitry Andric setGlobalVariableLargeSection(TT, *NamesVar);
16200b57cec5SDimitry Andric NamesVar->setSection(
1621c9157d92SDimitry Andric ProfileCorrelate == InstrProfCorrelator::BINARY
1622c9157d92SDimitry Andric ? getInstrProfSectionName(IPSK_covname, TT.getObjectFormat())
1623c9157d92SDimitry Andric : getInstrProfSectionName(IPSK_name, TT.getObjectFormat()));
16240b57cec5SDimitry Andric // On COFF, it's important to reduce the alignment down to 1 to prevent the
16250b57cec5SDimitry Andric // linker from inserting padding before the start of the names section or
16260b57cec5SDimitry Andric // between names entries.
16275ffd83dbSDimitry Andric NamesVar->setAlignment(Align(1));
1628fe6060f1SDimitry Andric // NamesVar is used by runtime but not referenced via relocation by other
1629fe6060f1SDimitry Andric // sections. Conservatively make it linker retained.
16300b57cec5SDimitry Andric UsedVars.push_back(NamesVar);
16310b57cec5SDimitry Andric
16320b57cec5SDimitry Andric for (auto *NamePtr : ReferencedNames)
16330b57cec5SDimitry Andric NamePtr->eraseFromParent();
16340b57cec5SDimitry Andric }
16350b57cec5SDimitry Andric
emitRegistration()1636c9157d92SDimitry Andric void InstrLowerer::emitRegistration() {
16370b57cec5SDimitry Andric if (!needsRuntimeRegistrationOfSectionRange(TT))
16380b57cec5SDimitry Andric return;
16390b57cec5SDimitry Andric
16400b57cec5SDimitry Andric // Construct the function.
1641c9157d92SDimitry Andric auto *VoidTy = Type::getVoidTy(M.getContext());
1642c9157d92SDimitry Andric auto *VoidPtrTy = PointerType::getUnqual(M.getContext());
1643c9157d92SDimitry Andric auto *Int64Ty = Type::getInt64Ty(M.getContext());
16440b57cec5SDimitry Andric auto *RegisterFTy = FunctionType::get(VoidTy, false);
16450b57cec5SDimitry Andric auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage,
16460b57cec5SDimitry Andric getInstrProfRegFuncsName(), M);
16470b57cec5SDimitry Andric RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
16480b57cec5SDimitry Andric if (Options.NoRedZone)
16490b57cec5SDimitry Andric RegisterF->addFnAttr(Attribute::NoRedZone);
16500b57cec5SDimitry Andric
16510b57cec5SDimitry Andric auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false);
16520b57cec5SDimitry Andric auto *RuntimeRegisterF =
16530b57cec5SDimitry Andric Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage,
16540b57cec5SDimitry Andric getInstrProfRegFuncName(), M);
16550b57cec5SDimitry Andric
1656c9157d92SDimitry Andric IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", RegisterF));
1657fe6060f1SDimitry Andric for (Value *Data : CompilerUsedVars)
1658fe6060f1SDimitry Andric if (!isa<Function>(Data))
1659c9157d92SDimitry Andric IRB.CreateCall(RuntimeRegisterF, Data);
16600b57cec5SDimitry Andric for (Value *Data : UsedVars)
16610b57cec5SDimitry Andric if (Data != NamesVar && !isa<Function>(Data))
1662c9157d92SDimitry Andric IRB.CreateCall(RuntimeRegisterF, Data);
16630b57cec5SDimitry Andric
16640b57cec5SDimitry Andric if (NamesVar) {
16650b57cec5SDimitry Andric Type *ParamTypes[] = {VoidPtrTy, Int64Ty};
16660b57cec5SDimitry Andric auto *NamesRegisterTy =
1667bdd1243dSDimitry Andric FunctionType::get(VoidTy, ArrayRef(ParamTypes), false);
16680b57cec5SDimitry Andric auto *NamesRegisterF =
16690b57cec5SDimitry Andric Function::Create(NamesRegisterTy, GlobalVariable::ExternalLinkage,
16700b57cec5SDimitry Andric getInstrProfNamesRegFuncName(), M);
1671c9157d92SDimitry Andric IRB.CreateCall(NamesRegisterF, {NamesVar, IRB.getInt64(NamesSize)});
16720b57cec5SDimitry Andric }
16730b57cec5SDimitry Andric
16740b57cec5SDimitry Andric IRB.CreateRetVoid();
16750b57cec5SDimitry Andric }
16760b57cec5SDimitry Andric
emitRuntimeHook()1677c9157d92SDimitry Andric bool InstrLowerer::emitRuntimeHook() {
1678349cc55cSDimitry Andric // We expect the linker to be invoked with -u<hook_var> flag for Linux
1679349cc55cSDimitry Andric // in which case there is no need to emit the external variable.
1680bdd1243dSDimitry Andric if (TT.isOSLinux() || TT.isOSAIX())
16810b57cec5SDimitry Andric return false;
16820b57cec5SDimitry Andric
16830b57cec5SDimitry Andric // If the module's provided its own runtime, we don't need to do anything.
1684c9157d92SDimitry Andric if (M.getGlobalVariable(getInstrProfRuntimeHookVarName()))
16850b57cec5SDimitry Andric return false;
16860b57cec5SDimitry Andric
16870b57cec5SDimitry Andric // Declare an external variable that will pull in the runtime initialization.
1688c9157d92SDimitry Andric auto *Int32Ty = Type::getInt32Ty(M.getContext());
16890b57cec5SDimitry Andric auto *Var =
1690c9157d92SDimitry Andric new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage,
16910b57cec5SDimitry Andric nullptr, getInstrProfRuntimeHookVarName());
1692753f127fSDimitry Andric Var->setVisibility(GlobalValue::HiddenVisibility);
16930b57cec5SDimitry Andric
169481ad6265SDimitry Andric if (TT.isOSBinFormatELF() && !TT.isPS()) {
1695349cc55cSDimitry Andric // Mark the user variable as used so that it isn't stripped out.
1696349cc55cSDimitry Andric CompilerUsedVars.push_back(Var);
1697349cc55cSDimitry Andric } else {
16980b57cec5SDimitry Andric // Make a function that uses it.
16990b57cec5SDimitry Andric auto *User = Function::Create(FunctionType::get(Int32Ty, false),
17000b57cec5SDimitry Andric GlobalValue::LinkOnceODRLinkage,
17010b57cec5SDimitry Andric getInstrProfRuntimeHookVarUseFuncName(), M);
17020b57cec5SDimitry Andric User->addFnAttr(Attribute::NoInline);
17030b57cec5SDimitry Andric if (Options.NoRedZone)
17040b57cec5SDimitry Andric User->addFnAttr(Attribute::NoRedZone);
17050b57cec5SDimitry Andric User->setVisibility(GlobalValue::HiddenVisibility);
17060b57cec5SDimitry Andric if (TT.supportsCOMDAT())
1707c9157d92SDimitry Andric User->setComdat(M.getOrInsertComdat(User->getName()));
17080b57cec5SDimitry Andric
1709c9157d92SDimitry Andric IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", User));
17100b57cec5SDimitry Andric auto *Load = IRB.CreateLoad(Int32Ty, Var);
17110b57cec5SDimitry Andric IRB.CreateRet(Load);
17120b57cec5SDimitry Andric
1713349cc55cSDimitry Andric // Mark the function as used so that it isn't stripped out.
1714fe6060f1SDimitry Andric CompilerUsedVars.push_back(User);
1715349cc55cSDimitry Andric }
17160b57cec5SDimitry Andric return true;
17170b57cec5SDimitry Andric }
17180b57cec5SDimitry Andric
emitUses()1719c9157d92SDimitry Andric void InstrLowerer::emitUses() {
1720fe6060f1SDimitry Andric // The metadata sections are parallel arrays. Optimizers (e.g.
1721fe6060f1SDimitry Andric // GlobalOpt/ConstantMerge) may not discard associated sections as a unit, so
1722fe6060f1SDimitry Andric // we conservatively retain all unconditionally in the compiler.
1723fe6060f1SDimitry Andric //
1724349cc55cSDimitry Andric // On ELF and Mach-O, the linker can guarantee the associated sections will be
1725349cc55cSDimitry Andric // retained or discarded as a unit, so llvm.compiler.used is sufficient.
1726349cc55cSDimitry Andric // Similarly on COFF, if prof data is not referenced by code we use one comdat
1727349cc55cSDimitry Andric // and ensure this GC property as well. Otherwise, we have to conservatively
1728349cc55cSDimitry Andric // make all of the sections retained by the linker.
1729349cc55cSDimitry Andric if (TT.isOSBinFormatELF() || TT.isOSBinFormatMachO() ||
1730c9157d92SDimitry Andric (TT.isOSBinFormatCOFF() && !profDataReferencedByCode(M)))
1731c9157d92SDimitry Andric appendToCompilerUsed(M, CompilerUsedVars);
1732fe6060f1SDimitry Andric else
1733c9157d92SDimitry Andric appendToUsed(M, CompilerUsedVars);
1734fe6060f1SDimitry Andric
1735fe6060f1SDimitry Andric // We do not add proper references from used metadata sections to NamesVar and
1736fe6060f1SDimitry Andric // VNodesVar, so we have to be conservative and place them in llvm.used
1737fe6060f1SDimitry Andric // regardless of the target,
1738c9157d92SDimitry Andric appendToUsed(M, UsedVars);
17390b57cec5SDimitry Andric }
17400b57cec5SDimitry Andric
emitInitialization()1741c9157d92SDimitry Andric void InstrLowerer::emitInitialization() {
17420b57cec5SDimitry Andric // Create ProfileFileName variable. Don't don't this for the
17430b57cec5SDimitry Andric // context-sensitive instrumentation lowering: This lowering is after
17440b57cec5SDimitry Andric // LTO/ThinLTO linking. Pass PGOInstrumentationGenCreateVar should
17450b57cec5SDimitry Andric // have already create the variable before LTO/ThinLTO linking.
17460b57cec5SDimitry Andric if (!IsCS)
1747c9157d92SDimitry Andric createProfileFileNameVar(M, Options.InstrProfileOutput);
1748c9157d92SDimitry Andric Function *RegisterF = M.getFunction(getInstrProfRegFuncsName());
17490b57cec5SDimitry Andric if (!RegisterF)
17500b57cec5SDimitry Andric return;
17510b57cec5SDimitry Andric
17520b57cec5SDimitry Andric // Create the initialization function.
1753c9157d92SDimitry Andric auto *VoidTy = Type::getVoidTy(M.getContext());
17540b57cec5SDimitry Andric auto *F = Function::Create(FunctionType::get(VoidTy, false),
17550b57cec5SDimitry Andric GlobalValue::InternalLinkage,
17560b57cec5SDimitry Andric getInstrProfInitFuncName(), M);
17570b57cec5SDimitry Andric F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
17580b57cec5SDimitry Andric F->addFnAttr(Attribute::NoInline);
17590b57cec5SDimitry Andric if (Options.NoRedZone)
17600b57cec5SDimitry Andric F->addFnAttr(Attribute::NoRedZone);
17610b57cec5SDimitry Andric
17620b57cec5SDimitry Andric // Add the basic block and the necessary calls.
1763c9157d92SDimitry Andric IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", F));
17640b57cec5SDimitry Andric IRB.CreateCall(RegisterF, {});
17650b57cec5SDimitry Andric IRB.CreateRetVoid();
17660b57cec5SDimitry Andric
1767c9157d92SDimitry Andric appendToGlobalCtors(M, F, 0);
17680b57cec5SDimitry Andric }
1769