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