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