Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 1 | //===-- InstrProfiling.cpp - Frontend instrumentation based profiling -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 10 | // This pass lowers instrprof_* intrinsics emitted by a frontend for profiling. |
| 11 | // It also builds the data structures and initialization code needed for |
| 12 | // updating execution counts and emitting the profile at runtime. |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/InstrProfiling.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Twine.h" |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/LoopInfo.h" |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Attributes.h" |
| 25 | #include "llvm/IR/BasicBlock.h" |
| 26 | #include "llvm/IR/Constant.h" |
| 27 | #include "llvm/IR/Constants.h" |
| 28 | #include "llvm/IR/DerivedTypes.h" |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Function.h" |
| 31 | #include "llvm/IR/GlobalValue.h" |
| 32 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 33 | #include "llvm/IR/IRBuilder.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Instruction.h" |
| 35 | #include "llvm/IR/Instructions.h" |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 36 | #include "llvm/IR/IntrinsicInst.h" |
| 37 | #include "llvm/IR/Module.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Type.h" |
| 39 | #include "llvm/Pass.h" |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 40 | #include "llvm/ProfileData/InstrProf.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Casting.h" |
| 42 | #include "llvm/Support/CommandLine.h" |
| 43 | #include "llvm/Support/Error.h" |
| 44 | #include "llvm/Support/ErrorHandling.h" |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 45 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 46 | #include "llvm/Transforms/Utils/LoopSimplify.h" |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 49 | #include <algorithm> |
| 50 | #include <cassert> |
| 51 | #include <cstddef> |
| 52 | #include <cstdint> |
| 53 | #include <string> |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 54 | |
| 55 | using namespace llvm; |
| 56 | |
| 57 | #define DEBUG_TYPE "instrprof" |
| 58 | |
Rong Xu | 48596b6 | 2017-04-04 16:42:20 +0000 | [diff] [blame] | 59 | // The start and end values of precise value profile range for memory |
| 60 | // intrinsic sizes |
| 61 | cl::opt<std::string> MemOPSizeRange( |
| 62 | "memop-size-range", |
| 63 | cl::desc("Set the range of size in memory intrinsic calls to be profiled " |
| 64 | "precisely, in a format of <start_val>:<end_val>"), |
| 65 | cl::init("")); |
| 66 | |
| 67 | // The value that considered to be large value in memory intrinsic. |
| 68 | cl::opt<unsigned> MemOPSizeLarge( |
| 69 | "memop-size-large", |
| 70 | cl::desc("Set large value thresthold in memory intrinsic size profiling. " |
| 71 | "Value of 0 disables the large value profiling."), |
| 72 | cl::init(8192)); |
| 73 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 74 | namespace { |
| 75 | |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 76 | cl::opt<bool> DoNameCompression("enable-name-compression", |
| 77 | cl::desc("Enable name string compression"), |
| 78 | cl::init(true)); |
| 79 | |
Rong Xu | 20f5df1 | 2017-01-11 20:19:41 +0000 | [diff] [blame] | 80 | cl::opt<bool> DoHashBasedCounterSplit( |
| 81 | "hash-based-counter-split", |
| 82 | cl::desc("Rename counter variable of a comdat function based on cfg hash"), |
| 83 | cl::init(true)); |
| 84 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 85 | cl::opt<bool> ValueProfileStaticAlloc( |
| 86 | "vp-static-alloc", |
| 87 | cl::desc("Do static counter allocation for value profiler"), |
| 88 | cl::init(true)); |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 89 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 90 | cl::opt<double> NumCountersPerValueSite( |
| 91 | "vp-counters-per-site", |
| 92 | cl::desc("The average number of profile counters allocated " |
| 93 | "per value profiling site."), |
| 94 | // This is set to a very small value because in real programs, only |
| 95 | // a very small percentage of value sites have non-zero targets, e.g, 1/30. |
| 96 | // For those sites with non-zero profile, the average number of targets |
| 97 | // is usually smaller than 2. |
| 98 | cl::init(1.0)); |
| 99 | |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 100 | cl::opt<bool> AtomicCounterUpdatePromoted( |
| 101 | "atomic-counter-update-promoted", cl::ZeroOrMore, |
| 102 | cl::desc("Do counter update using atomic fetch add " |
| 103 | " for promoted counters only"), |
| 104 | cl::init(false)); |
| 105 | |
| 106 | // If the option is not specified, the default behavior about whether |
| 107 | // counter promotion is done depends on how instrumentaiton lowering |
| 108 | // pipeline is setup, i.e., the default value of true of this option |
| 109 | // does not mean the promotion will be done by default. Explicitly |
| 110 | // setting this option can override the default behavior. |
| 111 | cl::opt<bool> DoCounterPromotion("do-counter-promotion", cl::ZeroOrMore, |
| 112 | cl::desc("Do counter register promotion"), |
| 113 | cl::init(false)); |
| 114 | cl::opt<unsigned> MaxNumOfPromotionsPerLoop( |
| 115 | cl::ZeroOrMore, "max-counter-promotions-per-loop", cl::init(10), |
| 116 | cl::desc("Max number counter promotions per loop to avoid" |
| 117 | " increasing register pressure too much")); |
| 118 | |
| 119 | // A debug option |
| 120 | cl::opt<int> |
| 121 | MaxNumOfPromotions(cl::ZeroOrMore, "max-counter-promotions", cl::init(-1), |
| 122 | cl::desc("Max number of allowed counter promotions")); |
| 123 | |
| 124 | cl::opt<bool> SpeculativeCounterPromotion( |
| 125 | cl::ZeroOrMore, "speculative-counter-promotion", cl::init(false), |
| 126 | cl::desc("Allow counter promotion for loops with multiple exiting blocks " |
| 127 | " or top-tested loops. ")); |
| 128 | |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 129 | class InstrProfilingLegacyPass : public ModulePass { |
| 130 | InstrProfiling InstrProf; |
| 131 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 132 | public: |
| 133 | static char ID; |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 134 | |
| 135 | InstrProfilingLegacyPass() : ModulePass(ID) {} |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 136 | InstrProfilingLegacyPass(const InstrProfOptions &Options) |
| 137 | : ModulePass(ID), InstrProf(Options) {} |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 138 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 139 | StringRef getPassName() const override { |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 140 | return "Frontend instrumentation-based coverage lowering"; |
| 141 | } |
| 142 | |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 143 | bool runOnModule(Module &M) override { |
| 144 | return InstrProf.run(M, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()); |
| 145 | } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 146 | |
| 147 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 148 | AU.setPreservesCFG(); |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 149 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 150 | } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 153 | /// A helper class to promote one counter RMW operation in the loop |
| 154 | /// into register update. |
| 155 | /// |
| 156 | /// RWM update for the counter will be sinked out of the loop after |
| 157 | /// the transformation. |
| 158 | /// |
| 159 | class PGOCounterPromoterHelper : public LoadAndStorePromoter { |
| 160 | public: |
| 161 | PGOCounterPromoterHelper(Instruction *L, Instruction *S, SSAUpdater &SSA, |
| 162 | Value *Init, BasicBlock *PH, |
| 163 | ArrayRef<BasicBlock *> ExitBlocks, |
| 164 | ArrayRef<Instruction *> InsertPts) |
| 165 | : LoadAndStorePromoter({L, S}, SSA), Store(S), ExitBlocks(ExitBlocks), |
| 166 | InsertPts(InsertPts) { |
| 167 | assert(isa<LoadInst>(L)); |
| 168 | assert(isa<StoreInst>(S)); |
| 169 | SSA.AddAvailableValue(PH, Init); |
| 170 | } |
| 171 | void doExtraRewritesBeforeFinalDeletion() const override { |
| 172 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 173 | BasicBlock *ExitBlock = ExitBlocks[i]; |
| 174 | Instruction *InsertPos = InsertPts[i]; |
| 175 | // Get LiveIn value into the ExitBlock. If there are multiple |
| 176 | // predecessors, the value is defined by a PHI node in this |
| 177 | // block. |
| 178 | Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock); |
| 179 | Value *Addr = cast<StoreInst>(Store)->getPointerOperand(); |
| 180 | IRBuilder<> Builder(InsertPos); |
| 181 | if (AtomicCounterUpdatePromoted) |
| 182 | Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue, |
| 183 | AtomicOrdering::SequentiallyConsistent); |
| 184 | else { |
| 185 | LoadInst *OldVal = Builder.CreateLoad(Addr, "pgocount.promoted"); |
| 186 | auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue); |
| 187 | Builder.CreateStore(NewVal, Addr); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | private: |
| 193 | Instruction *Store; |
| 194 | ArrayRef<BasicBlock *> ExitBlocks; |
| 195 | ArrayRef<Instruction *> InsertPts; |
| 196 | }; |
| 197 | |
| 198 | /// A helper class to do register promotion for all profile counter |
| 199 | /// updates in a loop. |
| 200 | /// |
| 201 | class PGOCounterPromoter { |
| 202 | public: |
| 203 | PGOCounterPromoter(ArrayRef<LoadStorePair> Cands, Loop &Loop) |
| 204 | : Candidates(Cands), ExitBlocks(), InsertPts(), ParentLoop(Loop) { |
| 205 | |
| 206 | SmallVector<BasicBlock *, 8> LoopExitBlocks; |
| 207 | SmallPtrSet<BasicBlock *, 8> BlockSet; |
| 208 | ParentLoop.getExitBlocks(LoopExitBlocks); |
| 209 | |
| 210 | for (BasicBlock *ExitBlock : LoopExitBlocks) { |
| 211 | if (BlockSet.insert(ExitBlock).second) { |
| 212 | ExitBlocks.push_back(ExitBlock); |
| 213 | InsertPts.push_back(&*ExitBlock->getFirstInsertionPt()); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | bool run(int64_t *NumPromoted) { |
| 219 | // We can't insert into a catchswitch. |
| 220 | bool HasCatchSwitch = llvm::any_of(ExitBlocks, [](BasicBlock *Exit) { |
| 221 | return isa<CatchSwitchInst>(Exit->getTerminator()); |
| 222 | }); |
| 223 | |
| 224 | if (HasCatchSwitch) |
| 225 | return false; |
| 226 | |
| 227 | if (!ParentLoop.hasDedicatedExits()) |
| 228 | return false; |
| 229 | |
| 230 | BasicBlock *PH = ParentLoop.getLoopPreheader(); |
| 231 | if (!PH) |
| 232 | return false; |
| 233 | |
| 234 | BasicBlock *H = ParentLoop.getHeader(); |
| 235 | bool TopTested = |
| 236 | ((ParentLoop.getBlocks().size() > 1) && ParentLoop.isLoopExiting(H)); |
| 237 | if (!SpeculativeCounterPromotion && |
| 238 | (TopTested || ParentLoop.getExitingBlock() == nullptr)) |
| 239 | return false; |
| 240 | |
| 241 | unsigned Promoted = 0; |
| 242 | for (auto &Cand : Candidates) { |
| 243 | |
| 244 | SmallVector<PHINode *, 4> NewPHIs; |
| 245 | SSAUpdater SSA(&NewPHIs); |
| 246 | Value *InitVal = ConstantInt::get(Cand.first->getType(), 0); |
| 247 | PGOCounterPromoterHelper Promoter(Cand.first, Cand.second, SSA, InitVal, |
| 248 | PH, ExitBlocks, InsertPts); |
| 249 | Promoter.run(SmallVector<Instruction *, 2>({Cand.first, Cand.second})); |
| 250 | Promoted++; |
| 251 | if (Promoted >= MaxNumOfPromotionsPerLoop) |
| 252 | break; |
| 253 | (*NumPromoted)++; |
| 254 | if (MaxNumOfPromotions != -1 && *NumPromoted >= MaxNumOfPromotions) |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | DEBUG(dbgs() << Promoted << " counters promoted for loop (depth=" |
| 259 | << ParentLoop.getLoopDepth() << ")\n"); |
| 260 | return Promoted != 0; |
| 261 | } |
| 262 | |
| 263 | private: |
| 264 | ArrayRef<LoadStorePair> Candidates; |
| 265 | SmallVector<BasicBlock *, 8> ExitBlocks; |
| 266 | SmallVector<Instruction *, 8> InsertPts; |
| 267 | Loop &ParentLoop; |
| 268 | }; |
| 269 | |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 270 | } // end anonymous namespace |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 271 | |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 272 | PreservedAnalyses InstrProfiling::run(Module &M, ModuleAnalysisManager &AM) { |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 273 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(M); |
| 274 | if (!run(M, TLI)) |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 275 | return PreservedAnalyses::all(); |
| 276 | |
| 277 | return PreservedAnalyses::none(); |
| 278 | } |
| 279 | |
| 280 | char InstrProfilingLegacyPass::ID = 0; |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 281 | INITIALIZE_PASS_BEGIN( |
| 282 | InstrProfilingLegacyPass, "instrprof", |
| 283 | "Frontend instrumentation-based coverage lowering.", false, false) |
| 284 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 285 | INITIALIZE_PASS_END( |
| 286 | InstrProfilingLegacyPass, "instrprof", |
| 287 | "Frontend instrumentation-based coverage lowering.", false, false) |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 288 | |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 289 | ModulePass * |
| 290 | llvm::createInstrProfilingLegacyPass(const InstrProfOptions &Options) { |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 291 | return new InstrProfilingLegacyPass(Options); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 294 | static InstrProfIncrementInst *castToIncrementInst(Instruction *Instr) { |
| 295 | InstrProfIncrementInst *Inc = dyn_cast<InstrProfIncrementInstStep>(Instr); |
| 296 | if (Inc) |
| 297 | return Inc; |
| 298 | return dyn_cast<InstrProfIncrementInst>(Instr); |
| 299 | } |
| 300 | |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 301 | bool InstrProfiling::lowerIntrinsics(Function *F) { |
| 302 | bool MadeChange = false; |
| 303 | PromotionCandidates.clear(); |
| 304 | for (BasicBlock &BB : *F) { |
| 305 | for (auto I = BB.begin(), E = BB.end(); I != E;) { |
| 306 | auto Instr = I++; |
| 307 | InstrProfIncrementInst *Inc = castToIncrementInst(&*Instr); |
| 308 | if (Inc) { |
| 309 | lowerIncrement(Inc); |
| 310 | MadeChange = true; |
| 311 | } else if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(Instr)) { |
| 312 | lowerValueProfileInst(Ind); |
| 313 | MadeChange = true; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if (!MadeChange) |
| 319 | return false; |
| 320 | |
| 321 | promoteCounterLoadStores(F); |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | bool InstrProfiling::isCounterPromotionEnabled() const { |
| 326 | if (DoCounterPromotion.getNumOccurrences() > 0) |
| 327 | return DoCounterPromotion; |
| 328 | |
| 329 | return Options.DoCounterPromotion; |
| 330 | } |
| 331 | |
| 332 | void InstrProfiling::promoteCounterLoadStores(Function *F) { |
| 333 | if (!isCounterPromotionEnabled()) |
| 334 | return; |
| 335 | |
| 336 | DominatorTree DT(*F); |
| 337 | LoopInfo LI(DT); |
| 338 | DenseMap<Loop *, SmallVector<LoadStorePair, 8>> LoopPromotionCandidates; |
| 339 | |
| 340 | for (const auto &LoadStore : PromotionCandidates) { |
| 341 | auto *CounterLoad = LoadStore.first; |
| 342 | auto *CounterStore = LoadStore.second; |
| 343 | BasicBlock *BB = CounterLoad->getParent(); |
| 344 | Loop *ParentLoop = LI.getLoopFor(BB); |
| 345 | if (!ParentLoop) |
| 346 | continue; |
| 347 | LoopPromotionCandidates[ParentLoop].emplace_back(CounterLoad, CounterStore); |
| 348 | } |
| 349 | |
| 350 | SmallVector<Loop *, 4> Loops = LI.getLoopsInPreorder(); |
| 351 | |
| 352 | for (auto *Loop : Loops) { |
| 353 | PGOCounterPromoter Promoter(LoopPromotionCandidates[Loop], *Loop); |
| 354 | Promoter.run(&TotalCountersPromoted); |
| 355 | } |
| 356 | } |
| 357 | |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 358 | bool InstrProfiling::run(Module &M, const TargetLibraryInfo &TLI) { |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 359 | bool MadeChange = false; |
| 360 | |
| 361 | this->M = &M; |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 362 | this->TLI = &TLI; |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 363 | NamesVar = nullptr; |
| 364 | NamesSize = 0; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 365 | ProfileDataMap.clear(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 366 | UsedVars.clear(); |
Rong Xu | 48596b6 | 2017-04-04 16:42:20 +0000 | [diff] [blame] | 367 | getMemOPSizeRangeFromOption(MemOPSizeRange, MemOPSizeRangeStart, |
| 368 | MemOPSizeRangeLast); |
Vedant Kumar | 1a6a2b6 | 2017-04-15 00:09:57 +0000 | [diff] [blame] | 369 | TT = Triple(M.getTargetTriple()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 370 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 371 | // We did not know how many value sites there would be inside |
| 372 | // the instrumented function. This is counting the number of instrumented |
| 373 | // target value sites to enter it as field in the profile data variable. |
Rong Xu | 294572f | 2016-01-19 18:29:54 +0000 | [diff] [blame] | 374 | for (Function &F : M) { |
| 375 | InstrProfIncrementInst *FirstProfIncInst = nullptr; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 376 | for (BasicBlock &BB : F) |
Rong Xu | 294572f | 2016-01-19 18:29:54 +0000 | [diff] [blame] | 377 | for (auto I = BB.begin(), E = BB.end(); I != E; I++) |
| 378 | if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I)) |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 379 | computeNumValueSiteCounts(Ind); |
Rong Xu | 294572f | 2016-01-19 18:29:54 +0000 | [diff] [blame] | 380 | else if (FirstProfIncInst == nullptr) |
| 381 | FirstProfIncInst = dyn_cast<InstrProfIncrementInst>(I); |
| 382 | |
| 383 | // Value profiling intrinsic lowering requires per-function profile data |
| 384 | // variable to be created first. |
| 385 | if (FirstProfIncInst != nullptr) |
| 386 | static_cast<void>(getOrCreateRegionCounters(FirstProfIncInst)); |
| 387 | } |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 388 | |
| 389 | for (Function &F : M) |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 390 | MadeChange |= lowerIntrinsics(&F); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 391 | |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 392 | if (GlobalVariable *CoverageNamesVar = |
Xinliang David Li | 440cd70 | 2016-01-20 00:24:36 +0000 | [diff] [blame] | 393 | M.getNamedGlobal(getCoverageUnusedNamesVarName())) { |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 394 | lowerCoverageData(CoverageNamesVar); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 395 | MadeChange = true; |
| 396 | } |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 397 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 398 | if (!MadeChange) |
| 399 | return false; |
| 400 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 401 | emitVNodes(); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 402 | emitNameData(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 403 | emitRegistration(); |
| 404 | emitRuntimeHook(); |
| 405 | emitUses(); |
| 406 | emitInitialization(); |
| 407 | return true; |
| 408 | } |
| 409 | |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 410 | static Constant *getOrInsertValueProfilingCall(Module &M, |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 411 | const TargetLibraryInfo &TLI, |
| 412 | bool IsRange = false) { |
Xinliang David Li | c767323 | 2015-11-22 00:22:07 +0000 | [diff] [blame] | 413 | LLVMContext &Ctx = M.getContext(); |
| 414 | auto *ReturnTy = Type::getVoidTy(M.getContext()); |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 415 | |
| 416 | Constant *Res; |
| 417 | if (!IsRange) { |
| 418 | Type *ParamTypes[] = { |
Xinliang David Li | c767323 | 2015-11-22 00:22:07 +0000 | [diff] [blame] | 419 | #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType |
| 420 | #include "llvm/ProfileData/InstrProfData.inc" |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 421 | }; |
| 422 | auto *ValueProfilingCallTy = |
| 423 | FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false); |
| 424 | Res = M.getOrInsertFunction(getInstrProfValueProfFuncName(), |
| 425 | ValueProfilingCallTy); |
| 426 | } else { |
| 427 | Type *RangeParamTypes[] = { |
| 428 | #define VALUE_RANGE_PROF 1 |
| 429 | #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType |
| 430 | #include "llvm/ProfileData/InstrProfData.inc" |
| 431 | #undef VALUE_RANGE_PROF |
| 432 | }; |
| 433 | auto *ValueRangeProfilingCallTy = |
| 434 | FunctionType::get(ReturnTy, makeArrayRef(RangeParamTypes), false); |
| 435 | Res = M.getOrInsertFunction(getInstrProfValueRangeProfFuncName(), |
| 436 | ValueRangeProfilingCallTy); |
| 437 | } |
| 438 | |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 439 | if (Function *FunRes = dyn_cast<Function>(Res)) { |
| 440 | if (auto AK = TLI.getExtAttrForI32Param(false)) |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 441 | FunRes->addParamAttr(2, AK); |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 442 | } |
| 443 | return Res; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) { |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 447 | GlobalVariable *Name = Ind->getName(); |
| 448 | uint64_t ValueKind = Ind->getValueKind()->getZExtValue(); |
| 449 | uint64_t Index = Ind->getIndex()->getZExtValue(); |
| 450 | auto It = ProfileDataMap.find(Name); |
| 451 | if (It == ProfileDataMap.end()) { |
| 452 | PerFunctionProfileData PD; |
| 453 | PD.NumValueSites[ValueKind] = Index + 1; |
| 454 | ProfileDataMap[Name] = PD; |
| 455 | } else if (It->second.NumValueSites[ValueKind] <= Index) |
| 456 | It->second.NumValueSites[ValueKind] = Index + 1; |
| 457 | } |
| 458 | |
| 459 | void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) { |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 460 | GlobalVariable *Name = Ind->getName(); |
| 461 | auto It = ProfileDataMap.find(Name); |
| 462 | assert(It != ProfileDataMap.end() && It->second.DataVar && |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 463 | "value profiling detected in function with no counter incerement"); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 464 | |
| 465 | GlobalVariable *DataVar = It->second.DataVar; |
| 466 | uint64_t ValueKind = Ind->getValueKind()->getZExtValue(); |
| 467 | uint64_t Index = Ind->getIndex()->getZExtValue(); |
| 468 | for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind) |
| 469 | Index += It->second.NumValueSites[Kind]; |
| 470 | |
| 471 | IRBuilder<> Builder(Ind); |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 472 | bool IsRange = (Ind->getValueKind()->getZExtValue() == |
| 473 | llvm::InstrProfValueKind::IPVK_MemOPSize); |
| 474 | CallInst *Call = nullptr; |
| 475 | if (!IsRange) { |
| 476 | Value *Args[3] = {Ind->getTargetValue(), |
| 477 | Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()), |
| 478 | Builder.getInt32(Index)}; |
| 479 | Call = Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI), Args); |
| 480 | } else { |
Rong Xu | 48596b6 | 2017-04-04 16:42:20 +0000 | [diff] [blame] | 481 | Value *Args[6] = { |
| 482 | Ind->getTargetValue(), |
| 483 | Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()), |
| 484 | Builder.getInt32(Index), |
| 485 | Builder.getInt64(MemOPSizeRangeStart), |
| 486 | Builder.getInt64(MemOPSizeRangeLast), |
| 487 | Builder.getInt64(MemOPSizeLarge == 0 ? INT64_MIN : MemOPSizeLarge)}; |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 488 | Call = |
| 489 | Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI, true), Args); |
| 490 | } |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 491 | if (auto AK = TLI->getExtAttrForI32Param(false)) |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 492 | Call->addParamAttr(2, AK); |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 493 | Ind->replaceAllUsesWith(Call); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 494 | Ind->eraseFromParent(); |
| 495 | } |
| 496 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 497 | void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) { |
| 498 | GlobalVariable *Counters = getOrCreateRegionCounters(Inc); |
| 499 | |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 500 | IRBuilder<> Builder(Inc); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 501 | uint64_t Index = Inc->getIndex()->getZExtValue(); |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 502 | Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Index); |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 503 | Value *Load = Builder.CreateLoad(Addr, "pgocount"); |
| 504 | auto *Count = Builder.CreateAdd(Load, Inc->getStep()); |
| 505 | auto *Store = Builder.CreateStore(Count, Addr); |
| 506 | Inc->replaceAllUsesWith(Store); |
| 507 | if (isCounterPromotionEnabled()) |
| 508 | PromotionCandidates.emplace_back(cast<Instruction>(Load), Store); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 509 | Inc->eraseFromParent(); |
| 510 | } |
| 511 | |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 512 | void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageNamesVar) { |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 513 | ConstantArray *Names = |
| 514 | cast<ConstantArray>(CoverageNamesVar->getInitializer()); |
| 515 | for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) { |
| 516 | Constant *NC = Names->getOperand(I); |
| 517 | Value *V = NC->stripPointerCasts(); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 518 | assert(isa<GlobalVariable>(V) && "Missing reference to function name"); |
| 519 | GlobalVariable *Name = cast<GlobalVariable>(V); |
| 520 | |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 521 | Name->setLinkage(GlobalValue::PrivateLinkage); |
| 522 | ReferencedNames.push_back(Name); |
Vedant Kumar | 55891fc | 2017-02-14 20:03:48 +0000 | [diff] [blame] | 523 | NC->dropAllReferences(); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 524 | } |
Vedant Kumar | 55891fc | 2017-02-14 20:03:48 +0000 | [diff] [blame] | 525 | CoverageNamesVar->eraseFromParent(); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 528 | /// Get the name of a profiling variable for a particular function. |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 529 | static std::string getVarName(InstrProfIncrementInst *Inc, StringRef Prefix) { |
Xinliang David Li | d1bab96 | 2015-12-12 17:28:03 +0000 | [diff] [blame] | 530 | StringRef NamePrefix = getInstrProfNameVarPrefix(); |
| 531 | StringRef Name = Inc->getName()->getName().substr(NamePrefix.size()); |
Rong Xu | 20f5df1 | 2017-01-11 20:19:41 +0000 | [diff] [blame] | 532 | Function *F = Inc->getParent()->getParent(); |
| 533 | Module *M = F->getParent(); |
| 534 | if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) || |
| 535 | !canRenameComdatFunc(*F)) |
| 536 | return (Prefix + Name).str(); |
| 537 | uint64_t FuncHash = Inc->getHash()->getZExtValue(); |
| 538 | SmallVector<char, 24> HashPostfix; |
| 539 | if (Name.endswith((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix))) |
| 540 | return (Prefix + Name).str(); |
| 541 | return (Prefix + Name + "." + Twine(FuncHash)).str(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 544 | static inline bool shouldRecordFunctionAddr(Function *F) { |
| 545 | // Check the linkage |
Vedant Kumar | 9c056c9 | 2017-06-13 22:12:35 +0000 | [diff] [blame] | 546 | bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage(); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 547 | if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() && |
Vedant Kumar | 9c056c9 | 2017-06-13 22:12:35 +0000 | [diff] [blame] | 548 | !HasAvailableExternallyLinkage) |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 549 | return true; |
Vedant Kumar | 9c056c9 | 2017-06-13 22:12:35 +0000 | [diff] [blame] | 550 | |
| 551 | // A function marked 'alwaysinline' with available_externally linkage can't |
| 552 | // have its address taken. Doing so would create an undefined external ref to |
| 553 | // the function, which would fail to link. |
| 554 | if (HasAvailableExternallyLinkage && |
| 555 | F->hasFnAttribute(Attribute::AlwaysInline)) |
| 556 | return false; |
| 557 | |
Rong Xu | af5aeba | 2016-04-27 21:17:30 +0000 | [diff] [blame] | 558 | // Prohibit function address recording if the function is both internal and |
| 559 | // COMDAT. This avoids the profile data variable referencing internal symbols |
| 560 | // in COMDAT. |
| 561 | if (F->hasLocalLinkage() && F->hasComdat()) |
| 562 | return false; |
Vedant Kumar | 9c056c9 | 2017-06-13 22:12:35 +0000 | [diff] [blame] | 563 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 564 | // Check uses of this function for other than direct calls or invokes to it. |
Xinliang David Li | 7008ce3 | 2016-06-02 16:33:41 +0000 | [diff] [blame] | 565 | // Inline virtual functions have linkeOnceODR linkage. When a key method |
| 566 | // exists, the vtable will only be emitted in the TU where the key method |
| 567 | // is defined. In a TU where vtable is not available, the function won't |
Xinliang David Li | 6c44e9e | 2016-06-03 23:02:28 +0000 | [diff] [blame] | 568 | // be 'addresstaken'. If its address is not recorded here, the profile data |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 569 | // with missing address may be picked by the linker leading to missing |
Xinliang David Li | 6c44e9e | 2016-06-03 23:02:28 +0000 | [diff] [blame] | 570 | // indirect call target info. |
| 571 | return F->hasAddressTaken() || F->hasLinkOnceLinkage(); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Xinliang David Li | 985ff20 | 2016-02-27 23:11:30 +0000 | [diff] [blame] | 574 | static inline Comdat *getOrCreateProfileComdat(Module &M, Function &F, |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 575 | InstrProfIncrementInst *Inc) { |
Xinliang David Li | 985ff20 | 2016-02-27 23:11:30 +0000 | [diff] [blame] | 576 | if (!needsComdatForCounter(F, M)) |
| 577 | return nullptr; |
| 578 | |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 579 | // COFF format requires a COMDAT section to have a key symbol with the same |
Vedant Kumar | 2d5b5d3 | 2016-02-03 23:22:43 +0000 | [diff] [blame] | 580 | // name. The linker targeting COFF also requires that the COMDAT |
Xinliang David Li | 5fe0455 | 2015-12-22 00:11:15 +0000 | [diff] [blame] | 581 | // a section is associated to must precede the associating section. For this |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 582 | // reason, we must choose the counter var's name as the name of the comdat. |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 583 | StringRef ComdatPrefix = (Triple(M.getTargetTriple()).isOSBinFormatCOFF() |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 584 | ? getInstrProfCountersVarPrefix() |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 585 | : getInstrProfComdatPrefix()); |
| 586 | return M.getOrInsertComdat(StringRef(getVarName(Inc, ComdatPrefix))); |
| 587 | } |
| 588 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 589 | static bool needsRuntimeRegistrationOfSectionRange(const Module &M) { |
| 590 | // Don't do this for Darwin. compiler-rt uses linker magic. |
| 591 | if (Triple(M.getTargetTriple()).isOSDarwin()) |
| 592 | return false; |
| 593 | |
| 594 | // Use linker script magic to get data/cnts/name start/end. |
| 595 | if (Triple(M.getTargetTriple()).isOSLinux() || |
| 596 | Triple(M.getTargetTriple()).isOSFreeBSD() || |
| 597 | Triple(M.getTargetTriple()).isPS4CPU()) |
| 598 | return false; |
| 599 | |
| 600 | return true; |
| 601 | } |
| 602 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 603 | GlobalVariable * |
| 604 | InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) { |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 605 | GlobalVariable *NamePtr = Inc->getName(); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 606 | auto It = ProfileDataMap.find(NamePtr); |
| 607 | PerFunctionProfileData PD; |
| 608 | if (It != ProfileDataMap.end()) { |
| 609 | if (It->second.RegionCounters) |
| 610 | return It->second.RegionCounters; |
| 611 | PD = It->second; |
| 612 | } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 613 | |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 614 | // Move the name variable to the right section. Place them in a COMDAT group |
| 615 | // if the associated function is a COMDAT. This will make sure that |
| 616 | // only one copy of counters of the COMDAT function will be emitted after |
| 617 | // linking. |
Diego Novillo | df4837b | 2015-05-27 19:34:01 +0000 | [diff] [blame] | 618 | Function *Fn = Inc->getParent()->getParent(); |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 619 | Comdat *ProfileVarsComdat = nullptr; |
Xinliang David Li | 985ff20 | 2016-02-27 23:11:30 +0000 | [diff] [blame] | 620 | ProfileVarsComdat = getOrCreateProfileComdat(*M, *Fn, Inc); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 621 | |
| 622 | uint64_t NumCounters = Inc->getNumCounters()->getZExtValue(); |
| 623 | LLVMContext &Ctx = M->getContext(); |
| 624 | ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters); |
| 625 | |
| 626 | // Create the counters variable. |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 627 | auto *CounterPtr = |
| 628 | new GlobalVariable(*M, CounterTy, false, NamePtr->getLinkage(), |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 629 | Constant::getNullValue(CounterTy), |
| 630 | getVarName(Inc, getInstrProfCountersVarPrefix())); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 631 | CounterPtr->setVisibility(NamePtr->getVisibility()); |
Vedant Kumar | 1a6a2b6 | 2017-04-15 00:09:57 +0000 | [diff] [blame] | 632 | CounterPtr->setSection( |
| 633 | getInstrProfSectionName(IPSK_cnts, TT.getObjectFormat())); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 634 | CounterPtr->setAlignment(8); |
| 635 | CounterPtr->setComdat(ProfileVarsComdat); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 636 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 637 | auto *Int8PtrTy = Type::getInt8PtrTy(Ctx); |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 638 | // Allocate statically the array of pointers to value profile nodes for |
| 639 | // the current function. |
| 640 | Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy); |
| 641 | if (ValueProfileStaticAlloc && !needsRuntimeRegistrationOfSectionRange(*M)) { |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 642 | uint64_t NS = 0; |
| 643 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| 644 | NS += PD.NumValueSites[Kind]; |
| 645 | if (NS) { |
| 646 | ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS); |
| 647 | |
| 648 | auto *ValuesVar = |
| 649 | new GlobalVariable(*M, ValuesTy, false, NamePtr->getLinkage(), |
| 650 | Constant::getNullValue(ValuesTy), |
| 651 | getVarName(Inc, getInstrProfValuesVarPrefix())); |
| 652 | ValuesVar->setVisibility(NamePtr->getVisibility()); |
Vedant Kumar | 1a6a2b6 | 2017-04-15 00:09:57 +0000 | [diff] [blame] | 653 | ValuesVar->setSection( |
| 654 | getInstrProfSectionName(IPSK_vals, TT.getObjectFormat())); |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 655 | ValuesVar->setAlignment(8); |
| 656 | ValuesVar->setComdat(ProfileVarsComdat); |
| 657 | ValuesPtrExpr = |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 658 | ConstantExpr::getBitCast(ValuesVar, Type::getInt8PtrTy(Ctx)); |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | // Create data variable. |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 663 | auto *Int16Ty = Type::getInt16Ty(Ctx); |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 664 | auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 665 | Type *DataTypes[] = { |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 666 | #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType, |
| 667 | #include "llvm/ProfileData/InstrProfData.inc" |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 668 | }; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 669 | auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes)); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 670 | |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 671 | Constant *FunctionAddr = shouldRecordFunctionAddr(Fn) |
| 672 | ? ConstantExpr::getBitCast(Fn, Int8PtrTy) |
| 673 | : ConstantPointerNull::get(Int8PtrTy); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 674 | |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 675 | Constant *Int16ArrayVals[IPVK_Last + 1]; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 676 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| 677 | Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]); |
| 678 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 679 | Constant *DataVals[] = { |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 680 | #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init, |
| 681 | #include "llvm/ProfileData/InstrProfData.inc" |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 682 | }; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 683 | auto *Data = new GlobalVariable(*M, DataTy, false, NamePtr->getLinkage(), |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 684 | ConstantStruct::get(DataTy, DataVals), |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 685 | getVarName(Inc, getInstrProfDataVarPrefix())); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 686 | Data->setVisibility(NamePtr->getVisibility()); |
Vedant Kumar | 1a6a2b6 | 2017-04-15 00:09:57 +0000 | [diff] [blame] | 687 | Data->setSection(getInstrProfSectionName(IPSK_data, TT.getObjectFormat())); |
Xinliang David Li | c7c1f85 | 2015-11-23 18:02:59 +0000 | [diff] [blame] | 688 | Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT); |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 689 | Data->setComdat(ProfileVarsComdat); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 690 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 691 | PD.RegionCounters = CounterPtr; |
| 692 | PD.DataVar = Data; |
| 693 | ProfileDataMap[NamePtr] = PD; |
| 694 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 695 | // Mark the data variable as used so that it isn't stripped out. |
| 696 | UsedVars.push_back(Data); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 697 | // Now that the linkage set by the FE has been passed to the data and counter |
| 698 | // variables, reset Name variable's linkage and visibility to private so that |
| 699 | // it can be removed later by the compiler. |
| 700 | NamePtr->setLinkage(GlobalValue::PrivateLinkage); |
| 701 | // Collect the referenced names to be used by emitNameData. |
| 702 | ReferencedNames.push_back(NamePtr); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 703 | |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 704 | return CounterPtr; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 707 | void InstrProfiling::emitVNodes() { |
| 708 | if (!ValueProfileStaticAlloc) |
| 709 | return; |
Xinliang David Li | 8da773b | 2016-05-17 20:19:03 +0000 | [diff] [blame] | 710 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 711 | // For now only support this on platforms that do |
| 712 | // not require runtime registration to discover |
| 713 | // named section start/end. |
| 714 | if (needsRuntimeRegistrationOfSectionRange(*M)) |
| 715 | return; |
Xinliang David Li | 8da773b | 2016-05-17 20:19:03 +0000 | [diff] [blame] | 716 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 717 | size_t TotalNS = 0; |
| 718 | for (auto &PD : ProfileDataMap) { |
| 719 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| 720 | TotalNS += PD.second.NumValueSites[Kind]; |
| 721 | } |
| 722 | |
| 723 | if (!TotalNS) |
| 724 | return; |
| 725 | |
| 726 | uint64_t NumCounters = TotalNS * NumCountersPerValueSite; |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 727 | // Heuristic for small programs with very few total value sites. |
| 728 | // The default value of vp-counters-per-site is chosen based on |
| 729 | // the observation that large apps usually have a low percentage |
| 730 | // of value sites that actually have any profile data, and thus |
| 731 | // the average number of counters per site is low. For small |
| 732 | // apps with very few sites, this may not be true. Bump up the |
| 733 | // number of counters in this case. |
Xinliang David Li | e452076 | 2016-05-23 19:29:26 +0000 | [diff] [blame] | 734 | #define INSTR_PROF_MIN_VAL_COUNTS 10 |
| 735 | if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS) |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 736 | NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS, (int)NumCounters * 2); |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 737 | |
| 738 | auto &Ctx = M->getContext(); |
| 739 | Type *VNodeTypes[] = { |
| 740 | #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType, |
| 741 | #include "llvm/ProfileData/InstrProfData.inc" |
| 742 | }; |
| 743 | auto *VNodeTy = StructType::get(Ctx, makeArrayRef(VNodeTypes)); |
| 744 | |
| 745 | ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters); |
| 746 | auto *VNodesVar = new GlobalVariable( |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 747 | *M, VNodesTy, false, GlobalValue::PrivateLinkage, |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 748 | Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName()); |
Vedant Kumar | 1a6a2b6 | 2017-04-15 00:09:57 +0000 | [diff] [blame] | 749 | VNodesVar->setSection( |
| 750 | getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat())); |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 751 | UsedVars.push_back(VNodesVar); |
Xinliang David Li | 8da773b | 2016-05-17 20:19:03 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 754 | void InstrProfiling::emitNameData() { |
| 755 | std::string UncompressedData; |
| 756 | |
| 757 | if (ReferencedNames.empty()) |
| 758 | return; |
| 759 | |
| 760 | std::string CompressedNameStr; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 761 | if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr, |
Vedant Kumar | 43cba73 | 2016-05-03 16:53:17 +0000 | [diff] [blame] | 762 | DoNameCompression)) { |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 763 | report_fatal_error(toString(std::move(E)), false); |
Vedant Kumar | 43cba73 | 2016-05-03 16:53:17 +0000 | [diff] [blame] | 764 | } |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 765 | |
| 766 | auto &Ctx = M->getContext(); |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 767 | auto *NamesVal = ConstantDataArray::getString( |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 768 | Ctx, StringRef(CompressedNameStr), false); |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 769 | NamesVar = new GlobalVariable(*M, NamesVal->getType(), true, |
| 770 | GlobalValue::PrivateLinkage, NamesVal, |
| 771 | getInstrProfNamesVarName()); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 772 | NamesSize = CompressedNameStr.size(); |
Vedant Kumar | 1a6a2b6 | 2017-04-15 00:09:57 +0000 | [diff] [blame] | 773 | NamesVar->setSection( |
| 774 | getInstrProfSectionName(IPSK_name, TT.getObjectFormat())); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 775 | UsedVars.push_back(NamesVar); |
Vedant Kumar | 55891fc | 2017-02-14 20:03:48 +0000 | [diff] [blame] | 776 | |
| 777 | for (auto *NamePtr : ReferencedNames) |
| 778 | NamePtr->eraseFromParent(); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 781 | void InstrProfiling::emitRegistration() { |
Xinliang David Li | 8da773b | 2016-05-17 20:19:03 +0000 | [diff] [blame] | 782 | if (!needsRuntimeRegistrationOfSectionRange(*M)) |
Xinliang David Li | aa0592c | 2015-10-19 04:17:10 +0000 | [diff] [blame] | 783 | return; |
Xinliang David Li | 3dd8817 | 2015-10-13 18:39:48 +0000 | [diff] [blame] | 784 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 785 | // Construct the function. |
| 786 | auto *VoidTy = Type::getVoidTy(M->getContext()); |
| 787 | auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext()); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 788 | auto *Int64Ty = Type::getInt64Ty(M->getContext()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 789 | auto *RegisterFTy = FunctionType::get(VoidTy, false); |
| 790 | auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 791 | getInstrProfRegFuncsName(), M); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 792 | RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 793 | if (Options.NoRedZone) |
| 794 | RegisterF->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 795 | |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 796 | auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 797 | auto *RuntimeRegisterF = |
| 798 | Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 799 | getInstrProfRegFuncName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 800 | |
| 801 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF)); |
| 802 | for (Value *Data : UsedVars) |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 803 | if (Data != NamesVar) |
| 804 | IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy)); |
| 805 | |
| 806 | if (NamesVar) { |
| 807 | Type *ParamTypes[] = {VoidPtrTy, Int64Ty}; |
| 808 | auto *NamesRegisterTy = |
| 809 | FunctionType::get(VoidTy, makeArrayRef(ParamTypes), false); |
| 810 | auto *NamesRegisterF = |
| 811 | Function::Create(NamesRegisterTy, GlobalVariable::ExternalLinkage, |
| 812 | getInstrProfNamesRegFuncName(), M); |
| 813 | IRB.CreateCall(NamesRegisterF, {IRB.CreateBitCast(NamesVar, VoidPtrTy), |
| 814 | IRB.getInt64(NamesSize)}); |
| 815 | } |
| 816 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 817 | IRB.CreateRetVoid(); |
| 818 | } |
| 819 | |
| 820 | void InstrProfiling::emitRuntimeHook() { |
Xinliang David Li | 7a88ad6 | 2015-10-29 04:08:31 +0000 | [diff] [blame] | 821 | // We expect the linker to be invoked with -u<hook_var> flag for linux, |
| 822 | // for which case there is no need to emit the user function. |
| 823 | if (Triple(M->getTargetTriple()).isOSLinux()) |
| 824 | return; |
| 825 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 826 | // If the module's provided its own runtime, we don't need to do anything. |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 827 | if (M->getGlobalVariable(getInstrProfRuntimeHookVarName())) |
| 828 | return; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 829 | |
| 830 | // Declare an external variable that will pull in the runtime initialization. |
| 831 | auto *Int32Ty = Type::getInt32Ty(M->getContext()); |
| 832 | auto *Var = |
| 833 | new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 834 | nullptr, getInstrProfRuntimeHookVarName()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 835 | |
| 836 | // Make a function that uses it. |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 837 | auto *User = Function::Create(FunctionType::get(Int32Ty, false), |
| 838 | GlobalValue::LinkOnceODRLinkage, |
| 839 | getInstrProfRuntimeHookVarUseFuncName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 840 | User->addFnAttr(Attribute::NoInline); |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 841 | if (Options.NoRedZone) |
| 842 | User->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 2e427d4 | 2015-02-25 22:52:20 +0000 | [diff] [blame] | 843 | User->setVisibility(GlobalValue::HiddenVisibility); |
Xinliang David Li | a228608 | 2016-05-25 17:17:51 +0000 | [diff] [blame] | 844 | if (Triple(M->getTargetTriple()).supportsCOMDAT()) |
Xinliang David Li | f4edae6 | 2016-05-24 18:47:38 +0000 | [diff] [blame] | 845 | User->setComdat(M->getOrInsertComdat(User->getName())); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 846 | |
| 847 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", User)); |
| 848 | auto *Load = IRB.CreateLoad(Var); |
| 849 | IRB.CreateRet(Load); |
| 850 | |
| 851 | // Mark the user variable as used so that it isn't stripped out. |
| 852 | UsedVars.push_back(User); |
| 853 | } |
| 854 | |
| 855 | void InstrProfiling::emitUses() { |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 856 | if (!UsedVars.empty()) |
| 857 | appendToUsed(*M, UsedVars); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | void InstrProfiling::emitInitialization() { |
Vedant Kumar | cd32eba | 2016-07-21 17:50:07 +0000 | [diff] [blame] | 861 | StringRef InstrProfileOutput = Options.InstrProfileOutput; |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 862 | |
Xinliang David Li | 6f8c504 | 2016-07-21 23:19:10 +0000 | [diff] [blame] | 863 | if (!InstrProfileOutput.empty()) { |
| 864 | // Create variable for profile name. |
| 865 | Constant *ProfileNameConst = |
| 866 | ConstantDataArray::getString(M->getContext(), InstrProfileOutput, true); |
| 867 | GlobalVariable *ProfileNameVar = new GlobalVariable( |
| 868 | *M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage, |
| 869 | ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR)); |
Xinliang David Li | 6f8c504 | 2016-07-21 23:19:10 +0000 | [diff] [blame] | 870 | if (TT.supportsCOMDAT()) { |
| 871 | ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage); |
| 872 | ProfileNameVar->setComdat(M->getOrInsertComdat( |
| 873 | StringRef(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR)))); |
| 874 | } |
| 875 | } |
| 876 | |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 877 | Constant *RegisterF = M->getFunction(getInstrProfRegFuncsName()); |
Xinliang David Li | 6f8c504 | 2016-07-21 23:19:10 +0000 | [diff] [blame] | 878 | if (!RegisterF) |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 879 | return; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 880 | |
| 881 | // Create the initialization function. |
| 882 | auto *VoidTy = Type::getVoidTy(M->getContext()); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 883 | auto *F = Function::Create(FunctionType::get(VoidTy, false), |
| 884 | GlobalValue::InternalLinkage, |
| 885 | getInstrProfInitFuncName(), M); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 886 | F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 887 | F->addFnAttr(Attribute::NoInline); |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 888 | if (Options.NoRedZone) |
| 889 | F->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 890 | |
| 891 | // Add the basic block and the necessary calls. |
| 892 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F)); |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 893 | if (RegisterF) |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 894 | IRB.CreateCall(RegisterF, {}); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 895 | IRB.CreateRetVoid(); |
| 896 | |
| 897 | appendToGlobalCtors(*M, F, 0); |
| 898 | } |