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 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Triple.h" |
| 17 | #include "llvm/IR/IRBuilder.h" |
| 18 | #include "llvm/IR/IntrinsicInst.h" |
| 19 | #include "llvm/IR/Module.h" |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 20 | #include "llvm/ProfileData/InstrProf.h" |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/InstrProfiling.h" |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | #define DEBUG_TYPE "instrprof" |
| 27 | |
| 28 | namespace { |
| 29 | |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 30 | cl::opt<bool> DoNameCompression("enable-name-compression", |
| 31 | cl::desc("Enable name string compression"), |
| 32 | cl::init(true)); |
| 33 | |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 34 | class InstrProfilingLegacyPass : public ModulePass { |
| 35 | InstrProfiling InstrProf; |
| 36 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 37 | public: |
| 38 | static char ID; |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 39 | InstrProfilingLegacyPass() : ModulePass(ID), InstrProf() {} |
| 40 | InstrProfilingLegacyPass(const InstrProfOptions &Options) |
| 41 | : ModulePass(ID), InstrProf(Options) {} |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 42 | const char *getPassName() const override { |
| 43 | return "Frontend instrumentation-based coverage lowering"; |
| 44 | } |
| 45 | |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 46 | bool runOnModule(Module &M) override { return InstrProf.run(M); } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 47 | |
| 48 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 49 | AU.setPreservesCFG(); |
| 50 | } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | } // anonymous namespace |
| 54 | |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 55 | PreservedAnalyses InstrProfiling::run(Module &M, AnalysisManager<Module> &AM) { |
| 56 | if (!run(M)) |
| 57 | return PreservedAnalyses::all(); |
| 58 | |
| 59 | return PreservedAnalyses::none(); |
| 60 | } |
| 61 | |
| 62 | char InstrProfilingLegacyPass::ID = 0; |
| 63 | INITIALIZE_PASS(InstrProfilingLegacyPass, "instrprof", |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 64 | "Frontend instrumentation-based coverage lowering.", false, |
| 65 | false) |
| 66 | |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 67 | ModulePass *llvm::createInstrProfilingLegacyPass(const InstrProfOptions &Options) { |
| 68 | return new InstrProfilingLegacyPass(Options); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 71 | bool InstrProfiling::isMachO() const { |
| 72 | return Triple(M->getTargetTriple()).isOSBinFormatMachO(); |
| 73 | } |
| 74 | |
| 75 | /// Get the section name for the counter variables. |
| 76 | StringRef InstrProfiling::getCountersSection() const { |
| 77 | return getInstrProfCountersSectionName(isMachO()); |
| 78 | } |
| 79 | |
| 80 | /// Get the section name for the name variables. |
| 81 | StringRef InstrProfiling::getNameSection() const { |
| 82 | return getInstrProfNameSectionName(isMachO()); |
| 83 | } |
| 84 | |
| 85 | /// Get the section name for the profile data variables. |
| 86 | StringRef InstrProfiling::getDataSection() const { |
| 87 | return getInstrProfDataSectionName(isMachO()); |
| 88 | } |
| 89 | |
| 90 | /// Get the section name for the coverage mapping data. |
| 91 | StringRef InstrProfiling::getCoverageSection() const { |
| 92 | return getInstrProfCoverageSectionName(isMachO()); |
| 93 | } |
| 94 | |
| 95 | bool InstrProfiling::run(Module &M) { |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 96 | bool MadeChange = false; |
| 97 | |
| 98 | this->M = &M; |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 99 | NamesVar = nullptr; |
| 100 | NamesSize = 0; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 101 | ProfileDataMap.clear(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 102 | UsedVars.clear(); |
| 103 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 104 | // We did not know how many value sites there would be inside |
| 105 | // the instrumented function. This is counting the number of instrumented |
| 106 | // 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] | 107 | for (Function &F : M) { |
| 108 | InstrProfIncrementInst *FirstProfIncInst = nullptr; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 109 | for (BasicBlock &BB : F) |
Rong Xu | 294572f | 2016-01-19 18:29:54 +0000 | [diff] [blame] | 110 | for (auto I = BB.begin(), E = BB.end(); I != E; I++) |
| 111 | if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I)) |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 112 | computeNumValueSiteCounts(Ind); |
Rong Xu | 294572f | 2016-01-19 18:29:54 +0000 | [diff] [blame] | 113 | else if (FirstProfIncInst == nullptr) |
| 114 | FirstProfIncInst = dyn_cast<InstrProfIncrementInst>(I); |
| 115 | |
| 116 | // Value profiling intrinsic lowering requires per-function profile data |
| 117 | // variable to be created first. |
| 118 | if (FirstProfIncInst != nullptr) |
| 119 | static_cast<void>(getOrCreateRegionCounters(FirstProfIncInst)); |
| 120 | } |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 121 | |
| 122 | for (Function &F : M) |
| 123 | for (BasicBlock &BB : F) |
| 124 | for (auto I = BB.begin(), E = BB.end(); I != E;) { |
| 125 | auto Instr = I++; |
| 126 | if (auto *Inc = dyn_cast<InstrProfIncrementInst>(Instr)) { |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 127 | lowerIncrement(Inc); |
| 128 | MadeChange = true; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 129 | } else if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(Instr)) { |
| 130 | lowerValueProfileInst(Ind); |
| 131 | MadeChange = true; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 132 | } |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 135 | if (GlobalVariable *CoverageNamesVar = |
Xinliang David Li | 440cd70 | 2016-01-20 00:24:36 +0000 | [diff] [blame] | 136 | M.getNamedGlobal(getCoverageUnusedNamesVarName())) { |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 137 | lowerCoverageData(CoverageNamesVar); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 138 | MadeChange = true; |
| 139 | } |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 140 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 141 | if (!MadeChange) |
| 142 | return false; |
| 143 | |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 144 | emitNameData(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 145 | emitRegistration(); |
| 146 | emitRuntimeHook(); |
| 147 | emitUses(); |
| 148 | emitInitialization(); |
| 149 | return true; |
| 150 | } |
| 151 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 152 | static Constant *getOrInsertValueProfilingCall(Module &M) { |
Xinliang David Li | c767323 | 2015-11-22 00:22:07 +0000 | [diff] [blame] | 153 | LLVMContext &Ctx = M.getContext(); |
| 154 | auto *ReturnTy = Type::getVoidTy(M.getContext()); |
| 155 | Type *ParamTypes[] = { |
| 156 | #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType |
| 157 | #include "llvm/ProfileData/InstrProfData.inc" |
| 158 | }; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 159 | auto *ValueProfilingCallTy = |
Xinliang David Li | c767323 | 2015-11-22 00:22:07 +0000 | [diff] [blame] | 160 | FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false); |
Xinliang David Li | 924e058 | 2015-11-22 05:42:31 +0000 | [diff] [blame] | 161 | return M.getOrInsertFunction(getInstrProfValueProfFuncName(), |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 162 | ValueProfilingCallTy); |
| 163 | } |
| 164 | |
| 165 | void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) { |
| 166 | |
| 167 | GlobalVariable *Name = Ind->getName(); |
| 168 | uint64_t ValueKind = Ind->getValueKind()->getZExtValue(); |
| 169 | uint64_t Index = Ind->getIndex()->getZExtValue(); |
| 170 | auto It = ProfileDataMap.find(Name); |
| 171 | if (It == ProfileDataMap.end()) { |
| 172 | PerFunctionProfileData PD; |
| 173 | PD.NumValueSites[ValueKind] = Index + 1; |
| 174 | ProfileDataMap[Name] = PD; |
| 175 | } else if (It->second.NumValueSites[ValueKind] <= Index) |
| 176 | It->second.NumValueSites[ValueKind] = Index + 1; |
| 177 | } |
| 178 | |
| 179 | void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) { |
| 180 | |
| 181 | GlobalVariable *Name = Ind->getName(); |
| 182 | auto It = ProfileDataMap.find(Name); |
| 183 | assert(It != ProfileDataMap.end() && It->second.DataVar && |
| 184 | "value profiling detected in function with no counter incerement"); |
| 185 | |
| 186 | GlobalVariable *DataVar = It->second.DataVar; |
| 187 | uint64_t ValueKind = Ind->getValueKind()->getZExtValue(); |
| 188 | uint64_t Index = Ind->getIndex()->getZExtValue(); |
| 189 | for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind) |
| 190 | Index += It->second.NumValueSites[Kind]; |
| 191 | |
| 192 | IRBuilder<> Builder(Ind); |
| 193 | Value* Args[3] = {Ind->getTargetValue(), |
| 194 | Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()), |
| 195 | Builder.getInt32(Index)}; |
| 196 | Ind->replaceAllUsesWith( |
| 197 | Builder.CreateCall(getOrInsertValueProfilingCall(*M), Args)); |
| 198 | Ind->eraseFromParent(); |
| 199 | } |
| 200 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 201 | void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) { |
| 202 | GlobalVariable *Counters = getOrCreateRegionCounters(Inc); |
| 203 | |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 204 | IRBuilder<> Builder(Inc); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 205 | uint64_t Index = Inc->getIndex()->getZExtValue(); |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 206 | Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Index); |
| 207 | Value *Count = Builder.CreateLoad(Addr, "pgocount"); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 208 | Count = Builder.CreateAdd(Count, Builder.getInt64(1)); |
| 209 | Inc->replaceAllUsesWith(Builder.CreateStore(Count, Addr)); |
| 210 | Inc->eraseFromParent(); |
| 211 | } |
| 212 | |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 213 | void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageNamesVar) { |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 214 | |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 215 | ConstantArray *Names = |
| 216 | cast<ConstantArray>(CoverageNamesVar->getInitializer()); |
| 217 | for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) { |
| 218 | Constant *NC = Names->getOperand(I); |
| 219 | Value *V = NC->stripPointerCasts(); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 220 | assert(isa<GlobalVariable>(V) && "Missing reference to function name"); |
| 221 | GlobalVariable *Name = cast<GlobalVariable>(V); |
| 222 | |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 223 | Name->setLinkage(GlobalValue::PrivateLinkage); |
| 224 | ReferencedNames.push_back(Name); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 228 | /// Get the name of a profiling variable for a particular function. |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 229 | static std::string getVarName(InstrProfIncrementInst *Inc, StringRef Prefix) { |
Xinliang David Li | d1bab96 | 2015-12-12 17:28:03 +0000 | [diff] [blame] | 230 | StringRef NamePrefix = getInstrProfNameVarPrefix(); |
| 231 | StringRef Name = Inc->getName()->getName().substr(NamePrefix.size()); |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 232 | return (Prefix + Name).str(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 235 | static inline bool shouldRecordFunctionAddr(Function *F) { |
| 236 | // Check the linkage |
| 237 | if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() && |
| 238 | !F->hasAvailableExternallyLinkage()) |
| 239 | return true; |
| 240 | // Check uses of this function for other than direct calls or invokes to it. |
| 241 | return F->hasAddressTaken(); |
| 242 | } |
| 243 | |
Xinliang David Li | 985ff20 | 2016-02-27 23:11:30 +0000 | [diff] [blame] | 244 | static inline bool needsComdatForCounter(Function &F, Module &M) { |
| 245 | |
| 246 | if (F.hasComdat()) |
| 247 | return true; |
| 248 | |
| 249 | Triple TT(M.getTargetTriple()); |
| 250 | if (!TT.isOSBinFormatELF()) |
| 251 | return false; |
| 252 | |
| 253 | // See createPGOFuncNameVar for more details. To avoid link errors, profile |
| 254 | // counters for function with available_externally linkage needs to be changed |
| 255 | // to linkonce linkage. On ELF based systems, this leads to weak symbols to be |
| 256 | // created. Without using comdat, duplicate entries won't be removed by the |
| 257 | // linker leading to increased data segement size and raw profile size. Even |
| 258 | // worse, since the referenced counter from profile per-function data object |
| 259 | // will be resolved to the common strong definition, the profile counts for |
| 260 | // available_externally functions will end up being duplicated in raw profile |
| 261 | // data. This can result in distorted profile as the counts of those dups |
| 262 | // will be accumulated by the profile merger. |
| 263 | GlobalValue::LinkageTypes Linkage = F.getLinkage(); |
| 264 | if (Linkage != GlobalValue::ExternalWeakLinkage && |
| 265 | Linkage != GlobalValue::AvailableExternallyLinkage) |
| 266 | return false; |
| 267 | |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | static inline Comdat *getOrCreateProfileComdat(Module &M, Function &F, |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 272 | InstrProfIncrementInst *Inc) { |
Xinliang David Li | 985ff20 | 2016-02-27 23:11:30 +0000 | [diff] [blame] | 273 | if (!needsComdatForCounter(F, M)) |
| 274 | return nullptr; |
| 275 | |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 276 | // 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] | 277 | // name. The linker targeting COFF also requires that the COMDAT |
Xinliang David Li | 5fe0455 | 2015-12-22 00:11:15 +0000 | [diff] [blame] | 278 | // 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] | 279 | // 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] | 280 | StringRef ComdatPrefix = (Triple(M.getTargetTriple()).isOSBinFormatCOFF() |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 281 | ? getInstrProfCountersVarPrefix() |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 282 | : getInstrProfComdatPrefix()); |
| 283 | return M.getOrInsertComdat(StringRef(getVarName(Inc, ComdatPrefix))); |
| 284 | } |
| 285 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 286 | GlobalVariable * |
| 287 | InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) { |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 288 | GlobalVariable *NamePtr = Inc->getName(); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 289 | auto It = ProfileDataMap.find(NamePtr); |
| 290 | PerFunctionProfileData PD; |
| 291 | if (It != ProfileDataMap.end()) { |
| 292 | if (It->second.RegionCounters) |
| 293 | return It->second.RegionCounters; |
| 294 | PD = It->second; |
| 295 | } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 296 | |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 297 | // Move the name variable to the right section. Place them in a COMDAT group |
| 298 | // if the associated function is a COMDAT. This will make sure that |
| 299 | // only one copy of counters of the COMDAT function will be emitted after |
| 300 | // linking. |
Diego Novillo | df4837b | 2015-05-27 19:34:01 +0000 | [diff] [blame] | 301 | Function *Fn = Inc->getParent()->getParent(); |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 302 | Comdat *ProfileVarsComdat = nullptr; |
Xinliang David Li | 985ff20 | 2016-02-27 23:11:30 +0000 | [diff] [blame] | 303 | ProfileVarsComdat = getOrCreateProfileComdat(*M, *Fn, Inc); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 304 | |
| 305 | uint64_t NumCounters = Inc->getNumCounters()->getZExtValue(); |
| 306 | LLVMContext &Ctx = M->getContext(); |
| 307 | ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters); |
| 308 | |
| 309 | // Create the counters variable. |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 310 | auto *CounterPtr = |
| 311 | new GlobalVariable(*M, CounterTy, false, NamePtr->getLinkage(), |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 312 | Constant::getNullValue(CounterTy), |
| 313 | getVarName(Inc, getInstrProfCountersVarPrefix())); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 314 | CounterPtr->setVisibility(NamePtr->getVisibility()); |
| 315 | CounterPtr->setSection(getCountersSection()); |
| 316 | CounterPtr->setAlignment(8); |
| 317 | CounterPtr->setComdat(ProfileVarsComdat); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 318 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 319 | // Create data variable. |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 320 | auto *Int8PtrTy = Type::getInt8PtrTy(Ctx); |
| 321 | auto *Int16Ty = Type::getInt16Ty(Ctx); |
| 322 | auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last+1); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 323 | Type *DataTypes[] = { |
| 324 | #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType, |
| 325 | #include "llvm/ProfileData/InstrProfData.inc" |
| 326 | }; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 327 | auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes)); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 328 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 329 | Constant *FunctionAddr = shouldRecordFunctionAddr(Fn) ? |
| 330 | ConstantExpr::getBitCast(Fn, Int8PtrTy) : |
| 331 | ConstantPointerNull::get(Int8PtrTy); |
| 332 | |
| 333 | Constant *Int16ArrayVals[IPVK_Last+1]; |
| 334 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| 335 | Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]); |
| 336 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 337 | Constant *DataVals[] = { |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 338 | #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init, |
| 339 | #include "llvm/ProfileData/InstrProfData.inc" |
| 340 | }; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 341 | auto *Data = new GlobalVariable(*M, DataTy, false, NamePtr->getLinkage(), |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 342 | ConstantStruct::get(DataTy, DataVals), |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 343 | getVarName(Inc, getInstrProfDataVarPrefix())); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 344 | Data->setVisibility(NamePtr->getVisibility()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 345 | Data->setSection(getDataSection()); |
Xinliang David Li | c7c1f85 | 2015-11-23 18:02:59 +0000 | [diff] [blame] | 346 | Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT); |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 347 | Data->setComdat(ProfileVarsComdat); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 348 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 349 | PD.RegionCounters = CounterPtr; |
| 350 | PD.DataVar = Data; |
| 351 | ProfileDataMap[NamePtr] = PD; |
| 352 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 353 | // Mark the data variable as used so that it isn't stripped out. |
| 354 | UsedVars.push_back(Data); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 355 | // Now that the linkage set by the FE has been passed to the data and counter |
| 356 | // variables, reset Name variable's linkage and visibility to private so that |
| 357 | // it can be removed later by the compiler. |
| 358 | NamePtr->setLinkage(GlobalValue::PrivateLinkage); |
| 359 | // Collect the referenced names to be used by emitNameData. |
| 360 | ReferencedNames.push_back(NamePtr); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 361 | |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 362 | return CounterPtr; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 365 | void InstrProfiling::emitNameData() { |
| 366 | std::string UncompressedData; |
| 367 | |
| 368 | if (ReferencedNames.empty()) |
| 369 | return; |
| 370 | |
| 371 | std::string CompressedNameStr; |
| 372 | collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr, |
| 373 | DoNameCompression); |
| 374 | |
| 375 | auto &Ctx = M->getContext(); |
| 376 | auto *NamesVal = llvm::ConstantDataArray::getString( |
| 377 | Ctx, StringRef(CompressedNameStr), false); |
| 378 | NamesVar = new llvm::GlobalVariable(*M, NamesVal->getType(), true, |
| 379 | llvm::GlobalValue::PrivateLinkage, |
| 380 | NamesVal, getInstrProfNamesVarName()); |
| 381 | NamesSize = CompressedNameStr.size(); |
| 382 | NamesVar->setSection(getNameSection()); |
| 383 | UsedVars.push_back(NamesVar); |
| 384 | } |
| 385 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 386 | void InstrProfiling::emitRegistration() { |
| 387 | // Don't do this for Darwin. compiler-rt uses linker magic. |
| 388 | if (Triple(M->getTargetTriple()).isOSDarwin()) |
| 389 | return; |
| 390 | |
Xinliang David Li | 3dd8817 | 2015-10-13 18:39:48 +0000 | [diff] [blame] | 391 | // Use linker script magic to get data/cnts/name start/end. |
Xinliang David Li | aa0592c | 2015-10-19 04:17:10 +0000 | [diff] [blame] | 392 | if (Triple(M->getTargetTriple()).isOSLinux() || |
Sean Silva | ea399f0 | 2016-02-27 06:01:26 +0000 | [diff] [blame] | 393 | Triple(M->getTargetTriple()).isOSFreeBSD() || |
| 394 | Triple(M->getTargetTriple()).isPS4CPU()) |
Xinliang David Li | aa0592c | 2015-10-19 04:17:10 +0000 | [diff] [blame] | 395 | return; |
Xinliang David Li | 3dd8817 | 2015-10-13 18:39:48 +0000 | [diff] [blame] | 396 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 397 | // Construct the function. |
| 398 | auto *VoidTy = Type::getVoidTy(M->getContext()); |
| 399 | auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext()); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 400 | auto *Int64Ty = Type::getInt64Ty(M->getContext()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 401 | auto *RegisterFTy = FunctionType::get(VoidTy, false); |
| 402 | auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 403 | getInstrProfRegFuncsName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 404 | RegisterF->setUnnamedAddr(true); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 405 | if (Options.NoRedZone) RegisterF->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 406 | |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 407 | auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 408 | auto *RuntimeRegisterF = |
| 409 | Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 410 | getInstrProfRegFuncName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 411 | |
| 412 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF)); |
| 413 | for (Value *Data : UsedVars) |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 414 | if (Data != NamesVar) |
| 415 | IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy)); |
| 416 | |
| 417 | if (NamesVar) { |
| 418 | Type *ParamTypes[] = {VoidPtrTy, Int64Ty}; |
| 419 | auto *NamesRegisterTy = |
| 420 | FunctionType::get(VoidTy, makeArrayRef(ParamTypes), false); |
| 421 | auto *NamesRegisterF = |
| 422 | Function::Create(NamesRegisterTy, GlobalVariable::ExternalLinkage, |
| 423 | getInstrProfNamesRegFuncName(), M); |
| 424 | IRB.CreateCall(NamesRegisterF, {IRB.CreateBitCast(NamesVar, VoidPtrTy), |
| 425 | IRB.getInt64(NamesSize)}); |
| 426 | } |
| 427 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 428 | IRB.CreateRetVoid(); |
| 429 | } |
| 430 | |
| 431 | void InstrProfiling::emitRuntimeHook() { |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 432 | |
Xinliang David Li | 7a88ad6 | 2015-10-29 04:08:31 +0000 | [diff] [blame] | 433 | // We expect the linker to be invoked with -u<hook_var> flag for linux, |
| 434 | // for which case there is no need to emit the user function. |
| 435 | if (Triple(M->getTargetTriple()).isOSLinux()) |
| 436 | return; |
| 437 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 438 | // If the module's provided its own runtime, we don't need to do anything. |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 439 | if (M->getGlobalVariable(getInstrProfRuntimeHookVarName())) return; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 440 | |
| 441 | // Declare an external variable that will pull in the runtime initialization. |
| 442 | auto *Int32Ty = Type::getInt32Ty(M->getContext()); |
| 443 | auto *Var = |
| 444 | new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 445 | nullptr, getInstrProfRuntimeHookVarName()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 446 | |
| 447 | // Make a function that uses it. |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 448 | auto *User = Function::Create(FunctionType::get(Int32Ty, false), |
| 449 | GlobalValue::LinkOnceODRLinkage, |
| 450 | getInstrProfRuntimeHookVarUseFuncName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 451 | User->addFnAttr(Attribute::NoInline); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 452 | if (Options.NoRedZone) User->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 2e427d4 | 2015-02-25 22:52:20 +0000 | [diff] [blame] | 453 | User->setVisibility(GlobalValue::HiddenVisibility); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 454 | |
| 455 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", User)); |
| 456 | auto *Load = IRB.CreateLoad(Var); |
| 457 | IRB.CreateRet(Load); |
| 458 | |
| 459 | // Mark the user variable as used so that it isn't stripped out. |
| 460 | UsedVars.push_back(User); |
| 461 | } |
| 462 | |
| 463 | void InstrProfiling::emitUses() { |
| 464 | if (UsedVars.empty()) |
| 465 | return; |
| 466 | |
| 467 | GlobalVariable *LLVMUsed = M->getGlobalVariable("llvm.used"); |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 468 | std::vector<Constant *> MergedVars; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 469 | if (LLVMUsed) { |
| 470 | // Collect the existing members of llvm.used. |
| 471 | ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer()); |
| 472 | for (unsigned I = 0, E = Inits->getNumOperands(); I != E; ++I) |
| 473 | MergedVars.push_back(Inits->getOperand(I)); |
| 474 | LLVMUsed->eraseFromParent(); |
| 475 | } |
| 476 | |
| 477 | Type *i8PTy = Type::getInt8PtrTy(M->getContext()); |
| 478 | // Add uses for our data. |
| 479 | for (auto *Value : UsedVars) |
| 480 | MergedVars.push_back( |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 481 | ConstantExpr::getBitCast(cast<Constant>(Value), i8PTy)); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 482 | |
| 483 | // Recreate llvm.used. |
| 484 | ArrayType *ATy = ArrayType::get(i8PTy, MergedVars.size()); |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 485 | LLVMUsed = |
| 486 | new GlobalVariable(*M, ATy, false, GlobalValue::AppendingLinkage, |
| 487 | ConstantArray::get(ATy, MergedVars), "llvm.used"); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 488 | LLVMUsed->setSection("llvm.metadata"); |
| 489 | } |
| 490 | |
| 491 | void InstrProfiling::emitInitialization() { |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 492 | std::string InstrProfileOutput = Options.InstrProfileOutput; |
| 493 | |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 494 | Constant *RegisterF = M->getFunction(getInstrProfRegFuncsName()); |
| 495 | if (!RegisterF && InstrProfileOutput.empty()) return; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 496 | |
| 497 | // Create the initialization function. |
| 498 | auto *VoidTy = Type::getVoidTy(M->getContext()); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 499 | auto *F = Function::Create(FunctionType::get(VoidTy, false), |
| 500 | GlobalValue::InternalLinkage, |
| 501 | getInstrProfInitFuncName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 502 | F->setUnnamedAddr(true); |
| 503 | F->addFnAttr(Attribute::NoInline); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 504 | if (Options.NoRedZone) F->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 505 | |
| 506 | // Add the basic block and the necessary calls. |
| 507 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F)); |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 508 | if (RegisterF) |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 509 | IRB.CreateCall(RegisterF, {}); |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 510 | if (!InstrProfileOutput.empty()) { |
| 511 | auto *Int8PtrTy = Type::getInt8PtrTy(M->getContext()); |
| 512 | auto *SetNameTy = FunctionType::get(VoidTy, Int8PtrTy, false); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 513 | auto *SetNameF = Function::Create(SetNameTy, GlobalValue::ExternalLinkage, |
| 514 | getInstrProfFileOverriderFuncName(), M); |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 515 | |
Diego Novillo | b0257c8 | 2015-06-29 20:03:46 +0000 | [diff] [blame] | 516 | // Create variable for profile name. |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 517 | Constant *ProfileNameConst = |
| 518 | ConstantDataArray::getString(M->getContext(), InstrProfileOutput, true); |
| 519 | GlobalVariable *ProfileName = |
| 520 | new GlobalVariable(*M, ProfileNameConst->getType(), true, |
| 521 | GlobalValue::PrivateLinkage, ProfileNameConst); |
| 522 | |
| 523 | IRB.CreateCall(SetNameF, IRB.CreatePointerCast(ProfileName, Int8PtrTy)); |
| 524 | } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 525 | IRB.CreateRetVoid(); |
| 526 | |
| 527 | appendToGlobalCtors(*M, F, 0); |
| 528 | } |