clang API Documentation
00001 //===--- CodeGenPGO.cpp - PGO Instrumentation for LLVM CodeGen --*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Instrumentation-based profile-guided optimization 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "CodeGenPGO.h" 00015 #include "CodeGenFunction.h" 00016 #include "CoverageMappingGen.h" 00017 #include "clang/AST/RecursiveASTVisitor.h" 00018 #include "clang/AST/StmtVisitor.h" 00019 #include "llvm/IR/MDBuilder.h" 00020 #include "llvm/ProfileData/InstrProfReader.h" 00021 #include "llvm/Support/Endian.h" 00022 #include "llvm/Support/FileSystem.h" 00023 #include "llvm/Support/MD5.h" 00024 00025 using namespace clang; 00026 using namespace CodeGen; 00027 00028 void CodeGenPGO::setFuncName(StringRef Name, 00029 llvm::GlobalValue::LinkageTypes Linkage) { 00030 RawFuncName = Name; 00031 00032 // Function names may be prefixed with a binary '1' to indicate 00033 // that the backend should not modify the symbols due to any platform 00034 // naming convention. Do not include that '1' in the PGO profile name. 00035 if (RawFuncName[0] == '\1') 00036 RawFuncName = RawFuncName.substr(1); 00037 00038 if (!llvm::GlobalValue::isLocalLinkage(Linkage)) { 00039 PrefixedFuncName.reset(new std::string(RawFuncName)); 00040 return; 00041 } 00042 00043 // For local symbols, prepend the main file name to distinguish them. 00044 // Do not include the full path in the file name since there's no guarantee 00045 // that it will stay the same, e.g., if the files are checked out from 00046 // version control in different locations. 00047 PrefixedFuncName.reset(new std::string(CGM.getCodeGenOpts().MainFileName)); 00048 if (PrefixedFuncName->empty()) 00049 PrefixedFuncName->assign("<unknown>"); 00050 PrefixedFuncName->append(":"); 00051 PrefixedFuncName->append(RawFuncName); 00052 } 00053 00054 void CodeGenPGO::setFuncName(llvm::Function *Fn) { 00055 setFuncName(Fn->getName(), Fn->getLinkage()); 00056 } 00057 00058 void CodeGenPGO::setVarLinkage(llvm::GlobalValue::LinkageTypes Linkage) { 00059 // Set the linkage for variables based on the function linkage. Usually, we 00060 // want to match it, but available_externally and extern_weak both have the 00061 // wrong semantics. 00062 VarLinkage = Linkage; 00063 switch (VarLinkage) { 00064 case llvm::GlobalValue::ExternalWeakLinkage: 00065 VarLinkage = llvm::GlobalValue::LinkOnceAnyLinkage; 00066 break; 00067 case llvm::GlobalValue::AvailableExternallyLinkage: 00068 VarLinkage = llvm::GlobalValue::LinkOnceODRLinkage; 00069 break; 00070 default: 00071 break; 00072 } 00073 } 00074 00075 static llvm::Function *getRegisterFunc(CodeGenModule &CGM) { 00076 return CGM.getModule().getFunction("__llvm_profile_register_functions"); 00077 } 00078 00079 static llvm::BasicBlock *getOrInsertRegisterBB(CodeGenModule &CGM) { 00080 // Don't do this for Darwin. compiler-rt uses linker magic. 00081 if (CGM.getTarget().getTriple().isOSDarwin()) 00082 return nullptr; 00083 00084 // Only need to insert this once per module. 00085 if (llvm::Function *RegisterF = getRegisterFunc(CGM)) 00086 return &RegisterF->getEntryBlock(); 00087 00088 // Construct the function. 00089 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext()); 00090 auto *RegisterFTy = llvm::FunctionType::get(VoidTy, false); 00091 auto *RegisterF = llvm::Function::Create(RegisterFTy, 00092 llvm::GlobalValue::InternalLinkage, 00093 "__llvm_profile_register_functions", 00094 &CGM.getModule()); 00095 RegisterF->setUnnamedAddr(true); 00096 if (CGM.getCodeGenOpts().DisableRedZone) 00097 RegisterF->addFnAttr(llvm::Attribute::NoRedZone); 00098 00099 // Construct and return the entry block. 00100 auto *BB = llvm::BasicBlock::Create(CGM.getLLVMContext(), "", RegisterF); 00101 CGBuilderTy Builder(BB); 00102 Builder.CreateRetVoid(); 00103 return BB; 00104 } 00105 00106 static llvm::Constant *getOrInsertRuntimeRegister(CodeGenModule &CGM) { 00107 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext()); 00108 auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); 00109 auto *RuntimeRegisterTy = llvm::FunctionType::get(VoidTy, VoidPtrTy, false); 00110 return CGM.getModule().getOrInsertFunction("__llvm_profile_register_function", 00111 RuntimeRegisterTy); 00112 } 00113 00114 static bool isMachO(const CodeGenModule &CGM) { 00115 return CGM.getTarget().getTriple().isOSBinFormatMachO(); 00116 } 00117 00118 static StringRef getCountersSection(const CodeGenModule &CGM) { 00119 return isMachO(CGM) ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts"; 00120 } 00121 00122 static StringRef getNameSection(const CodeGenModule &CGM) { 00123 return isMachO(CGM) ? "__DATA,__llvm_prf_names" : "__llvm_prf_names"; 00124 } 00125 00126 static StringRef getDataSection(const CodeGenModule &CGM) { 00127 return isMachO(CGM) ? "__DATA,__llvm_prf_data" : "__llvm_prf_data"; 00128 } 00129 00130 llvm::GlobalVariable *CodeGenPGO::buildDataVar() { 00131 // Create name variable. 00132 llvm::LLVMContext &Ctx = CGM.getLLVMContext(); 00133 auto *VarName = llvm::ConstantDataArray::getString(Ctx, getFuncName(), 00134 false); 00135 auto *Name = new llvm::GlobalVariable(CGM.getModule(), VarName->getType(), 00136 true, VarLinkage, VarName, 00137 getFuncVarName("name")); 00138 Name->setSection(getNameSection(CGM)); 00139 Name->setAlignment(1); 00140 00141 // Create data variable. 00142 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); 00143 auto *Int64Ty = llvm::Type::getInt64Ty(Ctx); 00144 auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx); 00145 auto *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx); 00146 llvm::GlobalVariable *Data = nullptr; 00147 if (RegionCounters) { 00148 llvm::Type *DataTypes[] = { 00149 Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int64PtrTy 00150 }; 00151 auto *DataTy = llvm::StructType::get(Ctx, makeArrayRef(DataTypes)); 00152 llvm::Constant *DataVals[] = { 00153 llvm::ConstantInt::get(Int32Ty, getFuncName().size()), 00154 llvm::ConstantInt::get(Int32Ty, NumRegionCounters), 00155 llvm::ConstantInt::get(Int64Ty, FunctionHash), 00156 llvm::ConstantExpr::getBitCast(Name, Int8PtrTy), 00157 llvm::ConstantExpr::getBitCast(RegionCounters, Int64PtrTy) 00158 }; 00159 Data = 00160 new llvm::GlobalVariable(CGM.getModule(), DataTy, true, VarLinkage, 00161 llvm::ConstantStruct::get(DataTy, DataVals), 00162 getFuncVarName("data")); 00163 00164 // All the data should be packed into an array in its own section. 00165 Data->setSection(getDataSection(CGM)); 00166 Data->setAlignment(8); 00167 } 00168 00169 // Create coverage mapping data variable. 00170 if (!CoverageMapping.empty()) 00171 CGM.getCoverageMapping()->addFunctionMappingRecord(Name, getFuncName(), 00172 FunctionHash, 00173 CoverageMapping); 00174 00175 // Hide all these symbols so that we correctly get a copy for each 00176 // executable. The profile format expects names and counters to be 00177 // contiguous, so references into shared objects would be invalid. 00178 if (!llvm::GlobalValue::isLocalLinkage(VarLinkage)) { 00179 Name->setVisibility(llvm::GlobalValue::HiddenVisibility); 00180 if (Data) { 00181 Data->setVisibility(llvm::GlobalValue::HiddenVisibility); 00182 RegionCounters->setVisibility(llvm::GlobalValue::HiddenVisibility); 00183 } 00184 } 00185 00186 // Make sure the data doesn't get deleted. 00187 if (Data) CGM.addUsedGlobal(Data); 00188 return Data; 00189 } 00190 00191 void CodeGenPGO::emitInstrumentationData() { 00192 if (!RegionCounters) 00193 return; 00194 00195 // Build the data. 00196 auto *Data = buildDataVar(); 00197 00198 // Register the data. 00199 auto *RegisterBB = getOrInsertRegisterBB(CGM); 00200 if (!RegisterBB) 00201 return; 00202 CGBuilderTy Builder(RegisterBB->getTerminator()); 00203 auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); 00204 Builder.CreateCall(getOrInsertRuntimeRegister(CGM), 00205 Builder.CreateBitCast(Data, VoidPtrTy)); 00206 } 00207 00208 llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) { 00209 if (!CGM.getCodeGenOpts().ProfileInstrGenerate) 00210 return nullptr; 00211 00212 assert(CGM.getModule().getFunction("__llvm_profile_init") == nullptr && 00213 "profile initialization already emitted"); 00214 00215 // Get the function to call at initialization. 00216 llvm::Constant *RegisterF = getRegisterFunc(CGM); 00217 if (!RegisterF) 00218 return nullptr; 00219 00220 // Create the initialization function. 00221 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext()); 00222 auto *F = llvm::Function::Create(llvm::FunctionType::get(VoidTy, false), 00223 llvm::GlobalValue::InternalLinkage, 00224 "__llvm_profile_init", &CGM.getModule()); 00225 F->setUnnamedAddr(true); 00226 F->addFnAttr(llvm::Attribute::NoInline); 00227 if (CGM.getCodeGenOpts().DisableRedZone) 00228 F->addFnAttr(llvm::Attribute::NoRedZone); 00229 00230 // Add the basic block and the necessary calls. 00231 CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", F)); 00232 Builder.CreateCall(RegisterF); 00233 Builder.CreateRetVoid(); 00234 00235 return F; 00236 } 00237 00238 namespace { 00239 /// \brief Stable hasher for PGO region counters. 00240 /// 00241 /// PGOHash produces a stable hash of a given function's control flow. 00242 /// 00243 /// Changing the output of this hash will invalidate all previously generated 00244 /// profiles -- i.e., don't do it. 00245 /// 00246 /// \note When this hash does eventually change (years?), we still need to 00247 /// support old hashes. We'll need to pull in the version number from the 00248 /// profile data format and use the matching hash function. 00249 class PGOHash { 00250 uint64_t Working; 00251 unsigned Count; 00252 llvm::MD5 MD5; 00253 00254 static const int NumBitsPerType = 6; 00255 static const unsigned NumTypesPerWord = sizeof(uint64_t) * 8 / NumBitsPerType; 00256 static const unsigned TooBig = 1u << NumBitsPerType; 00257 00258 public: 00259 /// \brief Hash values for AST nodes. 00260 /// 00261 /// Distinct values for AST nodes that have region counters attached. 00262 /// 00263 /// These values must be stable. All new members must be added at the end, 00264 /// and no members should be removed. Changing the enumeration value for an 00265 /// AST node will affect the hash of every function that contains that node. 00266 enum HashType : unsigned char { 00267 None = 0, 00268 LabelStmt = 1, 00269 WhileStmt, 00270 DoStmt, 00271 ForStmt, 00272 CXXForRangeStmt, 00273 ObjCForCollectionStmt, 00274 SwitchStmt, 00275 CaseStmt, 00276 DefaultStmt, 00277 IfStmt, 00278 CXXTryStmt, 00279 CXXCatchStmt, 00280 ConditionalOperator, 00281 BinaryOperatorLAnd, 00282 BinaryOperatorLOr, 00283 BinaryConditionalOperator, 00284 00285 // Keep this last. It's for the static assert that follows. 00286 LastHashType 00287 }; 00288 static_assert(LastHashType <= TooBig, "Too many types in HashType"); 00289 00290 // TODO: When this format changes, take in a version number here, and use the 00291 // old hash calculation for file formats that used the old hash. 00292 PGOHash() : Working(0), Count(0) {} 00293 void combine(HashType Type); 00294 uint64_t finalize(); 00295 }; 00296 const int PGOHash::NumBitsPerType; 00297 const unsigned PGOHash::NumTypesPerWord; 00298 const unsigned PGOHash::TooBig; 00299 00300 /// A RecursiveASTVisitor that fills a map of statements to PGO counters. 00301 struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> { 00302 /// The next counter value to assign. 00303 unsigned NextCounter; 00304 /// The function hash. 00305 PGOHash Hash; 00306 /// The map of statements to counters. 00307 llvm::DenseMap<const Stmt *, unsigned> &CounterMap; 00308 00309 MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap) 00310 : NextCounter(0), CounterMap(CounterMap) {} 00311 00312 // Blocks and lambdas are handled as separate functions, so we need not 00313 // traverse them in the parent context. 00314 bool TraverseBlockExpr(BlockExpr *BE) { return true; } 00315 bool TraverseLambdaBody(LambdaExpr *LE) { return true; } 00316 bool TraverseCapturedStmt(CapturedStmt *CS) { return true; } 00317 00318 bool VisitDecl(const Decl *D) { 00319 switch (D->getKind()) { 00320 default: 00321 break; 00322 case Decl::Function: 00323 case Decl::CXXMethod: 00324 case Decl::CXXConstructor: 00325 case Decl::CXXDestructor: 00326 case Decl::CXXConversion: 00327 case Decl::ObjCMethod: 00328 case Decl::Block: 00329 case Decl::Captured: 00330 CounterMap[D->getBody()] = NextCounter++; 00331 break; 00332 } 00333 return true; 00334 } 00335 00336 bool VisitStmt(const Stmt *S) { 00337 auto Type = getHashType(S); 00338 if (Type == PGOHash::None) 00339 return true; 00340 00341 CounterMap[S] = NextCounter++; 00342 Hash.combine(Type); 00343 return true; 00344 } 00345 PGOHash::HashType getHashType(const Stmt *S) { 00346 switch (S->getStmtClass()) { 00347 default: 00348 break; 00349 case Stmt::LabelStmtClass: 00350 return PGOHash::LabelStmt; 00351 case Stmt::WhileStmtClass: 00352 return PGOHash::WhileStmt; 00353 case Stmt::DoStmtClass: 00354 return PGOHash::DoStmt; 00355 case Stmt::ForStmtClass: 00356 return PGOHash::ForStmt; 00357 case Stmt::CXXForRangeStmtClass: 00358 return PGOHash::CXXForRangeStmt; 00359 case Stmt::ObjCForCollectionStmtClass: 00360 return PGOHash::ObjCForCollectionStmt; 00361 case Stmt::SwitchStmtClass: 00362 return PGOHash::SwitchStmt; 00363 case Stmt::CaseStmtClass: 00364 return PGOHash::CaseStmt; 00365 case Stmt::DefaultStmtClass: 00366 return PGOHash::DefaultStmt; 00367 case Stmt::IfStmtClass: 00368 return PGOHash::IfStmt; 00369 case Stmt::CXXTryStmtClass: 00370 return PGOHash::CXXTryStmt; 00371 case Stmt::CXXCatchStmtClass: 00372 return PGOHash::CXXCatchStmt; 00373 case Stmt::ConditionalOperatorClass: 00374 return PGOHash::ConditionalOperator; 00375 case Stmt::BinaryConditionalOperatorClass: 00376 return PGOHash::BinaryConditionalOperator; 00377 case Stmt::BinaryOperatorClass: { 00378 const BinaryOperator *BO = cast<BinaryOperator>(S); 00379 if (BO->getOpcode() == BO_LAnd) 00380 return PGOHash::BinaryOperatorLAnd; 00381 if (BO->getOpcode() == BO_LOr) 00382 return PGOHash::BinaryOperatorLOr; 00383 break; 00384 } 00385 } 00386 return PGOHash::None; 00387 } 00388 }; 00389 00390 /// A StmtVisitor that propagates the raw counts through the AST and 00391 /// records the count at statements where the value may change. 00392 struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> { 00393 /// PGO state. 00394 CodeGenPGO &PGO; 00395 00396 /// A flag that is set when the current count should be recorded on the 00397 /// next statement, such as at the exit of a loop. 00398 bool RecordNextStmtCount; 00399 00400 /// The map of statements to count values. 00401 llvm::DenseMap<const Stmt *, uint64_t> &CountMap; 00402 00403 /// BreakContinueStack - Keep counts of breaks and continues inside loops. 00404 struct BreakContinue { 00405 uint64_t BreakCount; 00406 uint64_t ContinueCount; 00407 BreakContinue() : BreakCount(0), ContinueCount(0) {} 00408 }; 00409 SmallVector<BreakContinue, 8> BreakContinueStack; 00410 00411 ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap, 00412 CodeGenPGO &PGO) 00413 : PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {} 00414 00415 void RecordStmtCount(const Stmt *S) { 00416 if (RecordNextStmtCount) { 00417 CountMap[S] = PGO.getCurrentRegionCount(); 00418 RecordNextStmtCount = false; 00419 } 00420 } 00421 00422 void VisitStmt(const Stmt *S) { 00423 RecordStmtCount(S); 00424 for (Stmt::const_child_range I = S->children(); I; ++I) { 00425 if (*I) 00426 this->Visit(*I); 00427 } 00428 } 00429 00430 void VisitFunctionDecl(const FunctionDecl *D) { 00431 // Counter tracks entry to the function body. 00432 RegionCounter Cnt(PGO, D->getBody()); 00433 Cnt.beginRegion(); 00434 CountMap[D->getBody()] = PGO.getCurrentRegionCount(); 00435 Visit(D->getBody()); 00436 } 00437 00438 // Skip lambda expressions. We visit these as FunctionDecls when we're 00439 // generating them and aren't interested in the body when generating a 00440 // parent context. 00441 void VisitLambdaExpr(const LambdaExpr *LE) {} 00442 00443 void VisitCapturedDecl(const CapturedDecl *D) { 00444 // Counter tracks entry to the capture body. 00445 RegionCounter Cnt(PGO, D->getBody()); 00446 Cnt.beginRegion(); 00447 CountMap[D->getBody()] = PGO.getCurrentRegionCount(); 00448 Visit(D->getBody()); 00449 } 00450 00451 void VisitObjCMethodDecl(const ObjCMethodDecl *D) { 00452 // Counter tracks entry to the method body. 00453 RegionCounter Cnt(PGO, D->getBody()); 00454 Cnt.beginRegion(); 00455 CountMap[D->getBody()] = PGO.getCurrentRegionCount(); 00456 Visit(D->getBody()); 00457 } 00458 00459 void VisitBlockDecl(const BlockDecl *D) { 00460 // Counter tracks entry to the block body. 00461 RegionCounter Cnt(PGO, D->getBody()); 00462 Cnt.beginRegion(); 00463 CountMap[D->getBody()] = PGO.getCurrentRegionCount(); 00464 Visit(D->getBody()); 00465 } 00466 00467 void VisitReturnStmt(const ReturnStmt *S) { 00468 RecordStmtCount(S); 00469 if (S->getRetValue()) 00470 Visit(S->getRetValue()); 00471 PGO.setCurrentRegionUnreachable(); 00472 RecordNextStmtCount = true; 00473 } 00474 00475 void VisitGotoStmt(const GotoStmt *S) { 00476 RecordStmtCount(S); 00477 PGO.setCurrentRegionUnreachable(); 00478 RecordNextStmtCount = true; 00479 } 00480 00481 void VisitLabelStmt(const LabelStmt *S) { 00482 RecordNextStmtCount = false; 00483 // Counter tracks the block following the label. 00484 RegionCounter Cnt(PGO, S); 00485 Cnt.beginRegion(); 00486 CountMap[S] = PGO.getCurrentRegionCount(); 00487 Visit(S->getSubStmt()); 00488 } 00489 00490 void VisitBreakStmt(const BreakStmt *S) { 00491 RecordStmtCount(S); 00492 assert(!BreakContinueStack.empty() && "break not in a loop or switch!"); 00493 BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount(); 00494 PGO.setCurrentRegionUnreachable(); 00495 RecordNextStmtCount = true; 00496 } 00497 00498 void VisitContinueStmt(const ContinueStmt *S) { 00499 RecordStmtCount(S); 00500 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); 00501 BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount(); 00502 PGO.setCurrentRegionUnreachable(); 00503 RecordNextStmtCount = true; 00504 } 00505 00506 void VisitWhileStmt(const WhileStmt *S) { 00507 RecordStmtCount(S); 00508 // Counter tracks the body of the loop. 00509 RegionCounter Cnt(PGO, S); 00510 BreakContinueStack.push_back(BreakContinue()); 00511 // Visit the body region first so the break/continue adjustments can be 00512 // included when visiting the condition. 00513 Cnt.beginRegion(); 00514 CountMap[S->getBody()] = PGO.getCurrentRegionCount(); 00515 Visit(S->getBody()); 00516 Cnt.adjustForControlFlow(); 00517 00518 // ...then go back and propagate counts through the condition. The count 00519 // at the start of the condition is the sum of the incoming edges, 00520 // the backedge from the end of the loop body, and the edges from 00521 // continue statements. 00522 BreakContinue BC = BreakContinueStack.pop_back_val(); 00523 Cnt.setCurrentRegionCount(Cnt.getParentCount() + 00524 Cnt.getAdjustedCount() + BC.ContinueCount); 00525 CountMap[S->getCond()] = PGO.getCurrentRegionCount(); 00526 Visit(S->getCond()); 00527 Cnt.adjustForControlFlow(); 00528 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); 00529 RecordNextStmtCount = true; 00530 } 00531 00532 void VisitDoStmt(const DoStmt *S) { 00533 RecordStmtCount(S); 00534 // Counter tracks the body of the loop. 00535 RegionCounter Cnt(PGO, S); 00536 BreakContinueStack.push_back(BreakContinue()); 00537 Cnt.beginRegion(/*AddIncomingFallThrough=*/true); 00538 CountMap[S->getBody()] = PGO.getCurrentRegionCount(); 00539 Visit(S->getBody()); 00540 Cnt.adjustForControlFlow(); 00541 00542 BreakContinue BC = BreakContinueStack.pop_back_val(); 00543 // The count at the start of the condition is equal to the count at the 00544 // end of the body. The adjusted count does not include either the 00545 // fall-through count coming into the loop or the continue count, so add 00546 // both of those separately. This is coincidentally the same equation as 00547 // with while loops but for different reasons. 00548 Cnt.setCurrentRegionCount(Cnt.getParentCount() + 00549 Cnt.getAdjustedCount() + BC.ContinueCount); 00550 CountMap[S->getCond()] = PGO.getCurrentRegionCount(); 00551 Visit(S->getCond()); 00552 Cnt.adjustForControlFlow(); 00553 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); 00554 RecordNextStmtCount = true; 00555 } 00556 00557 void VisitForStmt(const ForStmt *S) { 00558 RecordStmtCount(S); 00559 if (S->getInit()) 00560 Visit(S->getInit()); 00561 // Counter tracks the body of the loop. 00562 RegionCounter Cnt(PGO, S); 00563 BreakContinueStack.push_back(BreakContinue()); 00564 // Visit the body region first. (This is basically the same as a while 00565 // loop; see further comments in VisitWhileStmt.) 00566 Cnt.beginRegion(); 00567 CountMap[S->getBody()] = PGO.getCurrentRegionCount(); 00568 Visit(S->getBody()); 00569 Cnt.adjustForControlFlow(); 00570 00571 // The increment is essentially part of the body but it needs to include 00572 // the count for all the continue statements. 00573 if (S->getInc()) { 00574 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() + 00575 BreakContinueStack.back().ContinueCount); 00576 CountMap[S->getInc()] = PGO.getCurrentRegionCount(); 00577 Visit(S->getInc()); 00578 Cnt.adjustForControlFlow(); 00579 } 00580 00581 BreakContinue BC = BreakContinueStack.pop_back_val(); 00582 00583 // ...then go back and propagate counts through the condition. 00584 if (S->getCond()) { 00585 Cnt.setCurrentRegionCount(Cnt.getParentCount() + 00586 Cnt.getAdjustedCount() + 00587 BC.ContinueCount); 00588 CountMap[S->getCond()] = PGO.getCurrentRegionCount(); 00589 Visit(S->getCond()); 00590 Cnt.adjustForControlFlow(); 00591 } 00592 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); 00593 RecordNextStmtCount = true; 00594 } 00595 00596 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) { 00597 RecordStmtCount(S); 00598 Visit(S->getRangeStmt()); 00599 Visit(S->getBeginEndStmt()); 00600 // Counter tracks the body of the loop. 00601 RegionCounter Cnt(PGO, S); 00602 BreakContinueStack.push_back(BreakContinue()); 00603 // Visit the body region first. (This is basically the same as a while 00604 // loop; see further comments in VisitWhileStmt.) 00605 Cnt.beginRegion(); 00606 CountMap[S->getLoopVarStmt()] = PGO.getCurrentRegionCount(); 00607 Visit(S->getLoopVarStmt()); 00608 Visit(S->getBody()); 00609 Cnt.adjustForControlFlow(); 00610 00611 // The increment is essentially part of the body but it needs to include 00612 // the count for all the continue statements. 00613 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() + 00614 BreakContinueStack.back().ContinueCount); 00615 CountMap[S->getInc()] = PGO.getCurrentRegionCount(); 00616 Visit(S->getInc()); 00617 Cnt.adjustForControlFlow(); 00618 00619 BreakContinue BC = BreakContinueStack.pop_back_val(); 00620 00621 // ...then go back and propagate counts through the condition. 00622 Cnt.setCurrentRegionCount(Cnt.getParentCount() + 00623 Cnt.getAdjustedCount() + 00624 BC.ContinueCount); 00625 CountMap[S->getCond()] = PGO.getCurrentRegionCount(); 00626 Visit(S->getCond()); 00627 Cnt.adjustForControlFlow(); 00628 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); 00629 RecordNextStmtCount = true; 00630 } 00631 00632 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) { 00633 RecordStmtCount(S); 00634 Visit(S->getElement()); 00635 // Counter tracks the body of the loop. 00636 RegionCounter Cnt(PGO, S); 00637 BreakContinueStack.push_back(BreakContinue()); 00638 Cnt.beginRegion(); 00639 CountMap[S->getBody()] = PGO.getCurrentRegionCount(); 00640 Visit(S->getBody()); 00641 BreakContinue BC = BreakContinueStack.pop_back_val(); 00642 Cnt.adjustForControlFlow(); 00643 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); 00644 RecordNextStmtCount = true; 00645 } 00646 00647 void VisitSwitchStmt(const SwitchStmt *S) { 00648 RecordStmtCount(S); 00649 Visit(S->getCond()); 00650 PGO.setCurrentRegionUnreachable(); 00651 BreakContinueStack.push_back(BreakContinue()); 00652 Visit(S->getBody()); 00653 // If the switch is inside a loop, add the continue counts. 00654 BreakContinue BC = BreakContinueStack.pop_back_val(); 00655 if (!BreakContinueStack.empty()) 00656 BreakContinueStack.back().ContinueCount += BC.ContinueCount; 00657 // Counter tracks the exit block of the switch. 00658 RegionCounter ExitCnt(PGO, S); 00659 ExitCnt.beginRegion(); 00660 RecordNextStmtCount = true; 00661 } 00662 00663 void VisitCaseStmt(const CaseStmt *S) { 00664 RecordNextStmtCount = false; 00665 // Counter for this particular case. This counts only jumps from the 00666 // switch header and does not include fallthrough from the case before 00667 // this one. 00668 RegionCounter Cnt(PGO, S); 00669 Cnt.beginRegion(/*AddIncomingFallThrough=*/true); 00670 CountMap[S] = Cnt.getCount(); 00671 RecordNextStmtCount = true; 00672 Visit(S->getSubStmt()); 00673 } 00674 00675 void VisitDefaultStmt(const DefaultStmt *S) { 00676 RecordNextStmtCount = false; 00677 // Counter for this default case. This does not include fallthrough from 00678 // the previous case. 00679 RegionCounter Cnt(PGO, S); 00680 Cnt.beginRegion(/*AddIncomingFallThrough=*/true); 00681 CountMap[S] = Cnt.getCount(); 00682 RecordNextStmtCount = true; 00683 Visit(S->getSubStmt()); 00684 } 00685 00686 void VisitIfStmt(const IfStmt *S) { 00687 RecordStmtCount(S); 00688 // Counter tracks the "then" part of an if statement. The count for 00689 // the "else" part, if it exists, will be calculated from this counter. 00690 RegionCounter Cnt(PGO, S); 00691 Visit(S->getCond()); 00692 00693 Cnt.beginRegion(); 00694 CountMap[S->getThen()] = PGO.getCurrentRegionCount(); 00695 Visit(S->getThen()); 00696 Cnt.adjustForControlFlow(); 00697 00698 if (S->getElse()) { 00699 Cnt.beginElseRegion(); 00700 CountMap[S->getElse()] = PGO.getCurrentRegionCount(); 00701 Visit(S->getElse()); 00702 Cnt.adjustForControlFlow(); 00703 } 00704 Cnt.applyAdjustmentsToRegion(0); 00705 RecordNextStmtCount = true; 00706 } 00707 00708 void VisitCXXTryStmt(const CXXTryStmt *S) { 00709 RecordStmtCount(S); 00710 Visit(S->getTryBlock()); 00711 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I) 00712 Visit(S->getHandler(I)); 00713 // Counter tracks the continuation block of the try statement. 00714 RegionCounter Cnt(PGO, S); 00715 Cnt.beginRegion(); 00716 RecordNextStmtCount = true; 00717 } 00718 00719 void VisitCXXCatchStmt(const CXXCatchStmt *S) { 00720 RecordNextStmtCount = false; 00721 // Counter tracks the catch statement's handler block. 00722 RegionCounter Cnt(PGO, S); 00723 Cnt.beginRegion(); 00724 CountMap[S] = PGO.getCurrentRegionCount(); 00725 Visit(S->getHandlerBlock()); 00726 } 00727 00728 void VisitAbstractConditionalOperator( 00729 const AbstractConditionalOperator *E) { 00730 RecordStmtCount(E); 00731 // Counter tracks the "true" part of a conditional operator. The 00732 // count in the "false" part will be calculated from this counter. 00733 RegionCounter Cnt(PGO, E); 00734 Visit(E->getCond()); 00735 00736 Cnt.beginRegion(); 00737 CountMap[E->getTrueExpr()] = PGO.getCurrentRegionCount(); 00738 Visit(E->getTrueExpr()); 00739 Cnt.adjustForControlFlow(); 00740 00741 Cnt.beginElseRegion(); 00742 CountMap[E->getFalseExpr()] = PGO.getCurrentRegionCount(); 00743 Visit(E->getFalseExpr()); 00744 Cnt.adjustForControlFlow(); 00745 00746 Cnt.applyAdjustmentsToRegion(0); 00747 RecordNextStmtCount = true; 00748 } 00749 00750 void VisitBinLAnd(const BinaryOperator *E) { 00751 RecordStmtCount(E); 00752 // Counter tracks the right hand side of a logical and operator. 00753 RegionCounter Cnt(PGO, E); 00754 Visit(E->getLHS()); 00755 Cnt.beginRegion(); 00756 CountMap[E->getRHS()] = PGO.getCurrentRegionCount(); 00757 Visit(E->getRHS()); 00758 Cnt.adjustForControlFlow(); 00759 Cnt.applyAdjustmentsToRegion(0); 00760 RecordNextStmtCount = true; 00761 } 00762 00763 void VisitBinLOr(const BinaryOperator *E) { 00764 RecordStmtCount(E); 00765 // Counter tracks the right hand side of a logical or operator. 00766 RegionCounter Cnt(PGO, E); 00767 Visit(E->getLHS()); 00768 Cnt.beginRegion(); 00769 CountMap[E->getRHS()] = PGO.getCurrentRegionCount(); 00770 Visit(E->getRHS()); 00771 Cnt.adjustForControlFlow(); 00772 Cnt.applyAdjustmentsToRegion(0); 00773 RecordNextStmtCount = true; 00774 } 00775 }; 00776 } 00777 00778 void PGOHash::combine(HashType Type) { 00779 // Check that we never combine 0 and only have six bits. 00780 assert(Type && "Hash is invalid: unexpected type 0"); 00781 assert(unsigned(Type) < TooBig && "Hash is invalid: too many types"); 00782 00783 // Pass through MD5 if enough work has built up. 00784 if (Count && Count % NumTypesPerWord == 0) { 00785 using namespace llvm::support; 00786 uint64_t Swapped = endian::byte_swap<uint64_t, little>(Working); 00787 MD5.update(llvm::makeArrayRef((uint8_t *)&Swapped, sizeof(Swapped))); 00788 Working = 0; 00789 } 00790 00791 // Accumulate the current type. 00792 ++Count; 00793 Working = Working << NumBitsPerType | Type; 00794 } 00795 00796 uint64_t PGOHash::finalize() { 00797 // Use Working as the hash directly if we never used MD5. 00798 if (Count <= NumTypesPerWord) 00799 // No need to byte swap here, since none of the math was endian-dependent. 00800 // This number will be byte-swapped as required on endianness transitions, 00801 // so we will see the same value on the other side. 00802 return Working; 00803 00804 // Check for remaining work in Working. 00805 if (Working) 00806 MD5.update(Working); 00807 00808 // Finalize the MD5 and return the hash. 00809 llvm::MD5::MD5Result Result; 00810 MD5.final(Result); 00811 using namespace llvm::support; 00812 return endian::read<uint64_t, little, unaligned>(Result); 00813 } 00814 00815 static void emitRuntimeHook(CodeGenModule &CGM) { 00816 const char *const RuntimeVarName = "__llvm_profile_runtime"; 00817 const char *const RuntimeUserName = "__llvm_profile_runtime_user"; 00818 if (CGM.getModule().getGlobalVariable(RuntimeVarName)) 00819 return; 00820 00821 // Declare the runtime hook. 00822 llvm::LLVMContext &Ctx = CGM.getLLVMContext(); 00823 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); 00824 auto *Var = new llvm::GlobalVariable(CGM.getModule(), Int32Ty, false, 00825 llvm::GlobalValue::ExternalLinkage, 00826 nullptr, RuntimeVarName); 00827 00828 // Make a function that uses it. 00829 auto *User = llvm::Function::Create(llvm::FunctionType::get(Int32Ty, false), 00830 llvm::GlobalValue::LinkOnceODRLinkage, 00831 RuntimeUserName, &CGM.getModule()); 00832 User->addFnAttr(llvm::Attribute::NoInline); 00833 if (CGM.getCodeGenOpts().DisableRedZone) 00834 User->addFnAttr(llvm::Attribute::NoRedZone); 00835 CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", User)); 00836 auto *Load = Builder.CreateLoad(Var); 00837 Builder.CreateRet(Load); 00838 00839 // Create a use of the function. Now the definition of the runtime variable 00840 // should get pulled in, along with any static initializears. 00841 CGM.addUsedGlobal(User); 00842 } 00843 00844 void CodeGenPGO::checkGlobalDecl(GlobalDecl GD) { 00845 // Make sure we only emit coverage mapping for one constructor/destructor. 00846 // Clang emits several functions for the constructor and the destructor of 00847 // a class. Every function is instrumented, but we only want to provide 00848 // coverage for one of them. Because of that we only emit the coverage mapping 00849 // for the base constructor/destructor. 00850 if ((isa<CXXConstructorDecl>(GD.getDecl()) && 00851 GD.getCtorType() != Ctor_Base) || 00852 (isa<CXXDestructorDecl>(GD.getDecl()) && 00853 GD.getDtorType() != Dtor_Base)) { 00854 SkipCoverageMapping = true; 00855 } 00856 } 00857 00858 void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) { 00859 bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate; 00860 llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader(); 00861 if (!InstrumentRegions && !PGOReader) 00862 return; 00863 if (D->isImplicit()) 00864 return; 00865 CGM.ClearUnusedCoverageMapping(D); 00866 setFuncName(Fn); 00867 setVarLinkage(Fn->getLinkage()); 00868 00869 mapRegionCounters(D); 00870 if (InstrumentRegions) { 00871 emitRuntimeHook(CGM); 00872 emitCounterVariables(); 00873 if (CGM.getCodeGenOpts().CoverageMapping) 00874 emitCounterRegionMapping(D); 00875 } 00876 if (PGOReader) { 00877 SourceManager &SM = CGM.getContext().getSourceManager(); 00878 loadRegionCounts(PGOReader, SM.isInMainFile(D->getLocation())); 00879 computeRegionCounts(D); 00880 applyFunctionAttributes(PGOReader, Fn); 00881 } 00882 } 00883 00884 void CodeGenPGO::mapRegionCounters(const Decl *D) { 00885 RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>); 00886 MapRegionCounters Walker(*RegionCounterMap); 00887 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) 00888 Walker.TraverseDecl(const_cast<FunctionDecl *>(FD)); 00889 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) 00890 Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD)); 00891 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D)) 00892 Walker.TraverseDecl(const_cast<BlockDecl *>(BD)); 00893 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D)) 00894 Walker.TraverseDecl(const_cast<CapturedDecl *>(CD)); 00895 assert(Walker.NextCounter > 0 && "no entry counter mapped for decl"); 00896 NumRegionCounters = Walker.NextCounter; 00897 FunctionHash = Walker.Hash.finalize(); 00898 } 00899 00900 void CodeGenPGO::emitCounterRegionMapping(const Decl *D) { 00901 if (SkipCoverageMapping) 00902 return; 00903 // Don't map the functions inside the system headers 00904 auto Loc = D->getBody()->getLocStart(); 00905 if (CGM.getContext().getSourceManager().isInSystemHeader(Loc)) 00906 return; 00907 00908 llvm::raw_string_ostream OS(CoverageMapping); 00909 CoverageMappingGen MappingGen(*CGM.getCoverageMapping(), 00910 CGM.getContext().getSourceManager(), 00911 CGM.getLangOpts(), RegionCounterMap.get()); 00912 MappingGen.emitCounterMapping(D, OS); 00913 OS.flush(); 00914 } 00915 00916 void 00917 CodeGenPGO::emitEmptyCounterMapping(const Decl *D, StringRef FuncName, 00918 llvm::GlobalValue::LinkageTypes Linkage) { 00919 if (SkipCoverageMapping) 00920 return; 00921 setFuncName(FuncName, Linkage); 00922 setVarLinkage(Linkage); 00923 00924 // Don't map the functions inside the system headers 00925 auto Loc = D->getBody()->getLocStart(); 00926 if (CGM.getContext().getSourceManager().isInSystemHeader(Loc)) 00927 return; 00928 00929 llvm::raw_string_ostream OS(CoverageMapping); 00930 CoverageMappingGen MappingGen(*CGM.getCoverageMapping(), 00931 CGM.getContext().getSourceManager(), 00932 CGM.getLangOpts()); 00933 MappingGen.emitEmptyMapping(D, OS); 00934 OS.flush(); 00935 buildDataVar(); 00936 } 00937 00938 void CodeGenPGO::computeRegionCounts(const Decl *D) { 00939 StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>); 00940 ComputeRegionCounts Walker(*StmtCountMap, *this); 00941 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) 00942 Walker.VisitFunctionDecl(FD); 00943 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) 00944 Walker.VisitObjCMethodDecl(MD); 00945 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D)) 00946 Walker.VisitBlockDecl(BD); 00947 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D)) 00948 Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD)); 00949 } 00950 00951 void 00952 CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader, 00953 llvm::Function *Fn) { 00954 if (!haveRegionCounts()) 00955 return; 00956 00957 uint64_t MaxFunctionCount = PGOReader->getMaximumFunctionCount(); 00958 uint64_t FunctionCount = getRegionCount(0); 00959 if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount)) 00960 // Turn on InlineHint attribute for hot functions. 00961 // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal. 00962 Fn->addFnAttr(llvm::Attribute::InlineHint); 00963 else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount)) 00964 // Turn on Cold attribute for cold functions. 00965 // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal. 00966 Fn->addFnAttr(llvm::Attribute::Cold); 00967 } 00968 00969 void CodeGenPGO::emitCounterVariables() { 00970 llvm::LLVMContext &Ctx = CGM.getLLVMContext(); 00971 llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx), 00972 NumRegionCounters); 00973 RegionCounters = 00974 new llvm::GlobalVariable(CGM.getModule(), CounterTy, false, VarLinkage, 00975 llvm::Constant::getNullValue(CounterTy), 00976 getFuncVarName("counters")); 00977 RegionCounters->setAlignment(8); 00978 RegionCounters->setSection(getCountersSection(CGM)); 00979 } 00980 00981 void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) { 00982 if (!RegionCounters) 00983 return; 00984 llvm::Value *Addr = 00985 Builder.CreateConstInBoundsGEP2_64(RegionCounters, 0, Counter); 00986 llvm::Value *Count = Builder.CreateLoad(Addr, "pgocount"); 00987 Count = Builder.CreateAdd(Count, Builder.getInt64(1)); 00988 Builder.CreateStore(Count, Addr); 00989 } 00990 00991 void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader, 00992 bool IsInMainFile) { 00993 CGM.getPGOStats().addVisited(IsInMainFile); 00994 RegionCounts.reset(new std::vector<uint64_t>); 00995 if (std::error_code EC = PGOReader->getFunctionCounts( 00996 getFuncName(), FunctionHash, *RegionCounts)) { 00997 if (EC == llvm::instrprof_error::unknown_function) 00998 CGM.getPGOStats().addMissing(IsInMainFile); 00999 else if (EC == llvm::instrprof_error::hash_mismatch) 01000 CGM.getPGOStats().addMismatched(IsInMainFile); 01001 else if (EC == llvm::instrprof_error::malformed) 01002 // TODO: Consider a more specific warning for this case. 01003 CGM.getPGOStats().addMismatched(IsInMainFile); 01004 RegionCounts.reset(); 01005 } 01006 } 01007 01008 void CodeGenPGO::destroyRegionCounters() { 01009 RegionCounterMap.reset(); 01010 StmtCountMap.reset(); 01011 RegionCounts.reset(); 01012 RegionCounters = nullptr; 01013 } 01014 01015 /// \brief Calculate what to divide by to scale weights. 01016 /// 01017 /// Given the maximum weight, calculate a divisor that will scale all the 01018 /// weights to strictly less than UINT32_MAX. 01019 static uint64_t calculateWeightScale(uint64_t MaxWeight) { 01020 return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1; 01021 } 01022 01023 /// \brief Scale an individual branch weight (and add 1). 01024 /// 01025 /// Scale a 64-bit weight down to 32-bits using \c Scale. 01026 /// 01027 /// According to Laplace's Rule of Succession, it is better to compute the 01028 /// weight based on the count plus 1, so universally add 1 to the value. 01029 /// 01030 /// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no 01031 /// greater than \c Weight. 01032 static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) { 01033 assert(Scale && "scale by 0?"); 01034 uint64_t Scaled = Weight / Scale + 1; 01035 assert(Scaled <= UINT32_MAX && "overflow 32-bits"); 01036 return Scaled; 01037 } 01038 01039 llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount, 01040 uint64_t FalseCount) { 01041 // Check for empty weights. 01042 if (!TrueCount && !FalseCount) 01043 return nullptr; 01044 01045 // Calculate how to scale down to 32-bits. 01046 uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount)); 01047 01048 llvm::MDBuilder MDHelper(CGM.getLLVMContext()); 01049 return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale), 01050 scaleBranchWeight(FalseCount, Scale)); 01051 } 01052 01053 llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) { 01054 // We need at least two elements to create meaningful weights. 01055 if (Weights.size() < 2) 01056 return nullptr; 01057 01058 // Check for empty weights. 01059 uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end()); 01060 if (MaxWeight == 0) 01061 return nullptr; 01062 01063 // Calculate how to scale down to 32-bits. 01064 uint64_t Scale = calculateWeightScale(MaxWeight); 01065 01066 SmallVector<uint32_t, 16> ScaledWeights; 01067 ScaledWeights.reserve(Weights.size()); 01068 for (uint64_t W : Weights) 01069 ScaledWeights.push_back(scaleBranchWeight(W, Scale)); 01070 01071 llvm::MDBuilder MDHelper(CGM.getLLVMContext()); 01072 return MDHelper.createBranchWeights(ScaledWeights); 01073 } 01074 01075 llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond, 01076 RegionCounter &Cnt) { 01077 if (!haveRegionCounts()) 01078 return nullptr; 01079 uint64_t LoopCount = Cnt.getCount(); 01080 uint64_t CondCount = 0; 01081 bool Found = getStmtCount(Cond, CondCount); 01082 assert(Found && "missing expected loop condition count"); 01083 (void)Found; 01084 if (CondCount == 0) 01085 return nullptr; 01086 return createBranchWeights(LoopCount, 01087 std::max(CondCount, LoopCount) - LoopCount); 01088 }