1ef512b99SJustin Bogner //===--- CodeGenPGO.cpp - PGO Instrumentation for LLVM CodeGen --*- C++ -*-===//
2ef512b99SJustin Bogner //
3ef512b99SJustin Bogner //                     The LLVM Compiler Infrastructure
4ef512b99SJustin Bogner //
5ef512b99SJustin Bogner // This file is distributed under the University of Illinois Open Source
6ef512b99SJustin Bogner // License. See LICENSE.TXT for details.
7ef512b99SJustin Bogner //
8ef512b99SJustin Bogner //===----------------------------------------------------------------------===//
9ef512b99SJustin Bogner //
10ef512b99SJustin Bogner // Instrumentation-based profile-guided optimization
11ef512b99SJustin Bogner //
12ef512b99SJustin Bogner //===----------------------------------------------------------------------===//
13ef512b99SJustin Bogner 
14ef512b99SJustin Bogner #include "CodeGenPGO.h"
15ef512b99SJustin Bogner #include "CodeGenFunction.h"
16ef512b99SJustin Bogner #include "clang/AST/RecursiveASTVisitor.h"
17ef512b99SJustin Bogner #include "clang/AST/StmtVisitor.h"
18e9624291SDuncan P. N. Exon Smith #include "llvm/Config/config.h" // for strtoull()/strtoul() define
19ef512b99SJustin Bogner #include "llvm/IR/MDBuilder.h"
20ef512b99SJustin Bogner #include "llvm/Support/FileSystem.h"
21ef512b99SJustin Bogner 
22ef512b99SJustin Bogner using namespace clang;
23ef512b99SJustin Bogner using namespace CodeGen;
24ef512b99SJustin Bogner 
25d66a17d0SJustin Bogner static void ReportBadPGOData(CodeGenModule &CGM, const char *Message) {
26d66a17d0SJustin Bogner   DiagnosticsEngine &Diags = CGM.getDiags();
27d66a17d0SJustin Bogner   unsigned diagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0");
28d66a17d0SJustin Bogner   Diags.Report(diagID) << Message;
29d66a17d0SJustin Bogner }
30d66a17d0SJustin Bogner 
31d66a17d0SJustin Bogner PGOProfileData::PGOProfileData(CodeGenModule &CGM, std::string Path)
32d66a17d0SJustin Bogner   : CGM(CGM) {
33d66a17d0SJustin Bogner   if (llvm::MemoryBuffer::getFile(Path, DataBuffer)) {
34d66a17d0SJustin Bogner     ReportBadPGOData(CGM, "failed to open pgo data file");
35d66a17d0SJustin Bogner     return;
36d66a17d0SJustin Bogner   }
37d66a17d0SJustin Bogner 
38d66a17d0SJustin Bogner   if (DataBuffer->getBufferSize() > std::numeric_limits<unsigned>::max()) {
39d66a17d0SJustin Bogner     ReportBadPGOData(CGM, "pgo data file too big");
40d66a17d0SJustin Bogner     return;
41d66a17d0SJustin Bogner   }
42d66a17d0SJustin Bogner 
43d66a17d0SJustin Bogner   // Scan through the data file and map each function to the corresponding
44d66a17d0SJustin Bogner   // file offset where its counts are stored.
45d66a17d0SJustin Bogner   const char *BufferStart = DataBuffer->getBufferStart();
46d66a17d0SJustin Bogner   const char *BufferEnd = DataBuffer->getBufferEnd();
47d66a17d0SJustin Bogner   const char *CurPtr = BufferStart;
48d66a17d0SJustin Bogner   uint64_t MaxCount = 0;
49d66a17d0SJustin Bogner   while (CurPtr < BufferEnd) {
50d66a17d0SJustin Bogner     // Read the function name.
51d66a17d0SJustin Bogner     const char *FuncStart = CurPtr;
52d66a17d0SJustin Bogner     // For Objective-C methods, the name may include whitespace, so search
53d66a17d0SJustin Bogner     // backward from the end of the line to find the space that separates the
54d66a17d0SJustin Bogner     // name from the number of counters. (This is a temporary hack since we are
55d66a17d0SJustin Bogner     // going to completely replace this file format in the near future.)
56d66a17d0SJustin Bogner     CurPtr = strchr(CurPtr, '\n');
57d66a17d0SJustin Bogner     if (!CurPtr) {
58d66a17d0SJustin Bogner       ReportBadPGOData(CGM, "pgo data file has malformed function entry");
59d66a17d0SJustin Bogner       return;
60d66a17d0SJustin Bogner     }
61d66a17d0SJustin Bogner     StringRef FuncName(FuncStart, CurPtr - FuncStart);
62d66a17d0SJustin Bogner 
63b4416f58SJustin Bogner     // Skip over the function hash.
64b4416f58SJustin Bogner     CurPtr = strchr(++CurPtr, '\n');
65b4416f58SJustin Bogner     if (!CurPtr) {
66b4416f58SJustin Bogner       ReportBadPGOData(CGM, "pgo data file is missing the function hash");
67b4416f58SJustin Bogner       return;
68b4416f58SJustin Bogner     }
69b4416f58SJustin Bogner 
70d66a17d0SJustin Bogner     // Read the number of counters.
71d66a17d0SJustin Bogner     char *EndPtr;
72e9624291SDuncan P. N. Exon Smith     unsigned NumCounters = strtoul(++CurPtr, &EndPtr, 10);
73d66a17d0SJustin Bogner     if (EndPtr == CurPtr || *EndPtr != '\n' || NumCounters <= 0) {
74d66a17d0SJustin Bogner       ReportBadPGOData(CGM, "pgo data file has unexpected number of counters");
75d66a17d0SJustin Bogner       return;
76d66a17d0SJustin Bogner     }
77d66a17d0SJustin Bogner     CurPtr = EndPtr;
78d66a17d0SJustin Bogner 
79d66a17d0SJustin Bogner     // Read function count.
80e9624291SDuncan P. N. Exon Smith     uint64_t Count = strtoull(CurPtr, &EndPtr, 10);
81d66a17d0SJustin Bogner     if (EndPtr == CurPtr || *EndPtr != '\n') {
82d66a17d0SJustin Bogner       ReportBadPGOData(CGM, "pgo-data file has bad count value");
83d66a17d0SJustin Bogner       return;
84d66a17d0SJustin Bogner     }
85d66a17d0SJustin Bogner     CurPtr = EndPtr; // Point to '\n'.
86d66a17d0SJustin Bogner     FunctionCounts[FuncName] = Count;
87d66a17d0SJustin Bogner     MaxCount = Count > MaxCount ? Count : MaxCount;
88d66a17d0SJustin Bogner 
89d66a17d0SJustin Bogner     // There is one line for each counter; skip over those lines.
90d66a17d0SJustin Bogner     // Since function count is already read, we start the loop from 1.
91d66a17d0SJustin Bogner     for (unsigned N = 1; N < NumCounters; ++N) {
92d66a17d0SJustin Bogner       CurPtr = strchr(++CurPtr, '\n');
93d66a17d0SJustin Bogner       if (!CurPtr) {
94d66a17d0SJustin Bogner         ReportBadPGOData(CGM, "pgo data file is missing some counter info");
95d66a17d0SJustin Bogner         return;
96d66a17d0SJustin Bogner       }
97d66a17d0SJustin Bogner     }
98d66a17d0SJustin Bogner 
99d66a17d0SJustin Bogner     // Skip over the blank line separating functions.
100d66a17d0SJustin Bogner     CurPtr += 2;
101d66a17d0SJustin Bogner 
102d66a17d0SJustin Bogner     DataOffsets[FuncName] = FuncStart - BufferStart;
103d66a17d0SJustin Bogner   }
104d66a17d0SJustin Bogner   MaxFunctionCount = MaxCount;
105d66a17d0SJustin Bogner }
106d66a17d0SJustin Bogner 
107b4416f58SJustin Bogner bool PGOProfileData::getFunctionCounts(StringRef FuncName, uint64_t &FuncHash,
108d66a17d0SJustin Bogner                                        std::vector<uint64_t> &Counts) {
109d66a17d0SJustin Bogner   // Find the relevant section of the pgo-data file.
110d66a17d0SJustin Bogner   llvm::StringMap<unsigned>::const_iterator OffsetIter =
111d66a17d0SJustin Bogner     DataOffsets.find(FuncName);
112d66a17d0SJustin Bogner   if (OffsetIter == DataOffsets.end())
113d66a17d0SJustin Bogner     return true;
114d66a17d0SJustin Bogner   const char *CurPtr = DataBuffer->getBufferStart() + OffsetIter->getValue();
115d66a17d0SJustin Bogner 
116d66a17d0SJustin Bogner   // Skip over the function name.
117d66a17d0SJustin Bogner   CurPtr = strchr(CurPtr, '\n');
118d66a17d0SJustin Bogner   assert(CurPtr && "pgo-data has corrupted function entry");
119b4416f58SJustin Bogner 
120b4416f58SJustin Bogner   char *EndPtr;
121b4416f58SJustin Bogner   // Read the function hash.
122e9624291SDuncan P. N. Exon Smith   FuncHash = strtoull(++CurPtr, &EndPtr, 10);
123b4416f58SJustin Bogner   assert(EndPtr != CurPtr && *EndPtr == '\n' &&
124b4416f58SJustin Bogner          "pgo-data file has corrupted function hash");
125b4416f58SJustin Bogner   CurPtr = EndPtr;
126d66a17d0SJustin Bogner 
127d66a17d0SJustin Bogner   // Read the number of counters.
128e9624291SDuncan P. N. Exon Smith   unsigned NumCounters = strtoul(++CurPtr, &EndPtr, 10);
129d66a17d0SJustin Bogner   assert(EndPtr != CurPtr && *EndPtr == '\n' && NumCounters > 0 &&
130d66a17d0SJustin Bogner          "pgo-data file has corrupted number of counters");
131d66a17d0SJustin Bogner   CurPtr = EndPtr;
132d66a17d0SJustin Bogner 
133d66a17d0SJustin Bogner   Counts.reserve(NumCounters);
134d66a17d0SJustin Bogner 
135d66a17d0SJustin Bogner   for (unsigned N = 0; N < NumCounters; ++N) {
136d66a17d0SJustin Bogner     // Read the count value.
137e9624291SDuncan P. N. Exon Smith     uint64_t Count = strtoull(CurPtr, &EndPtr, 10);
138d66a17d0SJustin Bogner     if (EndPtr == CurPtr || *EndPtr != '\n') {
139d66a17d0SJustin Bogner       ReportBadPGOData(CGM, "pgo-data file has bad count value");
140d66a17d0SJustin Bogner       return true;
141d66a17d0SJustin Bogner     }
142d66a17d0SJustin Bogner     Counts.push_back(Count);
143d66a17d0SJustin Bogner     CurPtr = EndPtr + 1;
144d66a17d0SJustin Bogner   }
145d66a17d0SJustin Bogner 
146d66a17d0SJustin Bogner   // Make sure the number of counters matches up.
147d66a17d0SJustin Bogner   if (Counts.size() != NumCounters) {
148d66a17d0SJustin Bogner     ReportBadPGOData(CGM, "pgo-data file has inconsistent counters");
149d66a17d0SJustin Bogner     return true;
150d66a17d0SJustin Bogner   }
151d66a17d0SJustin Bogner 
152d66a17d0SJustin Bogner   return false;
153d66a17d0SJustin Bogner }
154d66a17d0SJustin Bogner 
155da1ebedeSBob Wilson void CodeGenPGO::setFuncName(llvm::Function *Fn) {
1562fe531cbSDuncan P. N. Exon Smith   RawFuncName = Fn->getName();
157da1ebedeSBob Wilson 
158da1ebedeSBob Wilson   // Function names may be prefixed with a binary '1' to indicate
159da1ebedeSBob Wilson   // that the backend should not modify the symbols due to any platform
160da1ebedeSBob Wilson   // naming convention. Do not include that '1' in the PGO profile name.
1612fe531cbSDuncan P. N. Exon Smith   if (RawFuncName[0] == '\1')
1622fe531cbSDuncan P. N. Exon Smith     RawFuncName = RawFuncName.substr(1);
163da1ebedeSBob Wilson 
164da1ebedeSBob Wilson   if (!Fn->hasLocalLinkage()) {
1651b67cfd4SDuncan P. N. Exon Smith     PrefixedFuncName.reset(new std::string(RawFuncName));
166da1ebedeSBob Wilson     return;
167da1ebedeSBob Wilson   }
168da1ebedeSBob Wilson 
169da1ebedeSBob Wilson   // For local symbols, prepend the main file name to distinguish them.
170da1ebedeSBob Wilson   // Do not include the full path in the file name since there's no guarantee
171da1ebedeSBob Wilson   // that it will stay the same, e.g., if the files are checked out from
172da1ebedeSBob Wilson   // version control in different locations.
1731b67cfd4SDuncan P. N. Exon Smith   PrefixedFuncName.reset(new std::string(CGM.getCodeGenOpts().MainFileName));
1742fe531cbSDuncan P. N. Exon Smith   if (PrefixedFuncName->empty())
1752fe531cbSDuncan P. N. Exon Smith     PrefixedFuncName->assign("<unknown>");
1762fe531cbSDuncan P. N. Exon Smith   PrefixedFuncName->append(":");
1772fe531cbSDuncan P. N. Exon Smith   PrefixedFuncName->append(RawFuncName);
178da1ebedeSBob Wilson }
179da1ebedeSBob Wilson 
1802fe531cbSDuncan P. N. Exon Smith static llvm::Function *getRegisterFunc(CodeGenModule &CGM) {
181a7807637SDuncan P. N. Exon Smith   return CGM.getModule().getFunction("__llvm_profile_register_functions");
1822fe531cbSDuncan P. N. Exon Smith }
1832fe531cbSDuncan P. N. Exon Smith 
1842fe531cbSDuncan P. N. Exon Smith static llvm::BasicBlock *getOrInsertRegisterBB(CodeGenModule &CGM) {
185780443e8SDuncan P. N. Exon Smith   // Don't do this for Darwin.  compiler-rt uses linker magic.
186780443e8SDuncan P. N. Exon Smith   if (CGM.getTarget().getTriple().isOSDarwin())
187780443e8SDuncan P. N. Exon Smith     return nullptr;
188780443e8SDuncan P. N. Exon Smith 
1892fe531cbSDuncan P. N. Exon Smith   // Only need to insert this once per module.
1902fe531cbSDuncan P. N. Exon Smith   if (llvm::Function *RegisterF = getRegisterFunc(CGM))
1912fe531cbSDuncan P. N. Exon Smith     return &RegisterF->getEntryBlock();
1922fe531cbSDuncan P. N. Exon Smith 
1932fe531cbSDuncan P. N. Exon Smith   // Construct the function.
1942fe531cbSDuncan P. N. Exon Smith   auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
1952fe531cbSDuncan P. N. Exon Smith   auto *RegisterFTy = llvm::FunctionType::get(VoidTy, false);
1962fe531cbSDuncan P. N. Exon Smith   auto *RegisterF = llvm::Function::Create(RegisterFTy,
1972fe531cbSDuncan P. N. Exon Smith                                            llvm::GlobalValue::InternalLinkage,
198a7807637SDuncan P. N. Exon Smith                                            "__llvm_profile_register_functions",
1992fe531cbSDuncan P. N. Exon Smith                                            &CGM.getModule());
2002fe531cbSDuncan P. N. Exon Smith   RegisterF->setUnnamedAddr(true);
2012fe531cbSDuncan P. N. Exon Smith   if (CGM.getCodeGenOpts().DisableRedZone)
2022fe531cbSDuncan P. N. Exon Smith     RegisterF->addFnAttr(llvm::Attribute::NoRedZone);
2032fe531cbSDuncan P. N. Exon Smith 
2042fe531cbSDuncan P. N. Exon Smith   // Construct and return the entry block.
2052fe531cbSDuncan P. N. Exon Smith   auto *BB = llvm::BasicBlock::Create(CGM.getLLVMContext(), "", RegisterF);
2062fe531cbSDuncan P. N. Exon Smith   CGBuilderTy Builder(BB);
2072fe531cbSDuncan P. N. Exon Smith   Builder.CreateRetVoid();
2082fe531cbSDuncan P. N. Exon Smith   return BB;
2092fe531cbSDuncan P. N. Exon Smith }
2102fe531cbSDuncan P. N. Exon Smith 
2112fe531cbSDuncan P. N. Exon Smith static llvm::Constant *getOrInsertRuntimeRegister(CodeGenModule &CGM) {
2122fe531cbSDuncan P. N. Exon Smith   auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
2132fe531cbSDuncan P. N. Exon Smith   auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
2142fe531cbSDuncan P. N. Exon Smith   auto *RuntimeRegisterTy = llvm::FunctionType::get(VoidTy, VoidPtrTy, false);
215a7807637SDuncan P. N. Exon Smith   return CGM.getModule().getOrInsertFunction("__llvm_profile_register_function",
2162fe531cbSDuncan P. N. Exon Smith                                              RuntimeRegisterTy);
2172fe531cbSDuncan P. N. Exon Smith }
2182fe531cbSDuncan P. N. Exon Smith 
2197134d47dSDuncan P. N. Exon Smith static bool isMachO(const CodeGenModule &CGM) {
2207134d47dSDuncan P. N. Exon Smith   return CGM.getTarget().getTriple().isOSBinFormatMachO();
2217134d47dSDuncan P. N. Exon Smith }
2227134d47dSDuncan P. N. Exon Smith 
2232fe531cbSDuncan P. N. Exon Smith static StringRef getCountersSection(const CodeGenModule &CGM) {
224a7807637SDuncan P. N. Exon Smith   return isMachO(CGM) ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts";
2252fe531cbSDuncan P. N. Exon Smith }
2262fe531cbSDuncan P. N. Exon Smith 
2272fe531cbSDuncan P. N. Exon Smith static StringRef getNameSection(const CodeGenModule &CGM) {
228a7807637SDuncan P. N. Exon Smith   return isMachO(CGM) ? "__DATA,__llvm_prf_names" : "__llvm_prf_names";
2292fe531cbSDuncan P. N. Exon Smith }
2302fe531cbSDuncan P. N. Exon Smith 
2312fe531cbSDuncan P. N. Exon Smith static StringRef getDataSection(const CodeGenModule &CGM) {
232a7807637SDuncan P. N. Exon Smith   return isMachO(CGM) ? "__DATA,__llvm_prf_data" : "__llvm_prf_data";
2332fe531cbSDuncan P. N. Exon Smith }
2342fe531cbSDuncan P. N. Exon Smith 
2352fe531cbSDuncan P. N. Exon Smith llvm::GlobalVariable *CodeGenPGO::buildDataVar() {
2362fe531cbSDuncan P. N. Exon Smith   // Create name variable.
2372fe531cbSDuncan P. N. Exon Smith   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
2382fe531cbSDuncan P. N. Exon Smith   auto *VarName = llvm::ConstantDataArray::getString(Ctx, getFuncName(),
2392fe531cbSDuncan P. N. Exon Smith                                                      false);
2402fe531cbSDuncan P. N. Exon Smith   auto *Name = new llvm::GlobalVariable(CGM.getModule(), VarName->getType(),
24173f78627SDuncan P. N. Exon Smith                                         true, VarLinkage, VarName,
2422fe531cbSDuncan P. N. Exon Smith                                         getFuncVarName("name"));
2432fe531cbSDuncan P. N. Exon Smith   Name->setSection(getNameSection(CGM));
2442fe531cbSDuncan P. N. Exon Smith   Name->setAlignment(1);
2452fe531cbSDuncan P. N. Exon Smith 
2462fe531cbSDuncan P. N. Exon Smith   // Create data variable.
2472fe531cbSDuncan P. N. Exon Smith   auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
248b4416f58SJustin Bogner   auto *Int64Ty = llvm::Type::getInt64Ty(Ctx);
2492fe531cbSDuncan P. N. Exon Smith   auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx);
2502fe531cbSDuncan P. N. Exon Smith   auto *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx);
2512fe531cbSDuncan P. N. Exon Smith   llvm::Type *DataTypes[] = {
252b4416f58SJustin Bogner     Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int64PtrTy
2532fe531cbSDuncan P. N. Exon Smith   };
2542fe531cbSDuncan P. N. Exon Smith   auto *DataTy = llvm::StructType::get(Ctx, makeArrayRef(DataTypes));
2552fe531cbSDuncan P. N. Exon Smith   llvm::Constant *DataVals[] = {
2562fe531cbSDuncan P. N. Exon Smith     llvm::ConstantInt::get(Int32Ty, getFuncName().size()),
2572fe531cbSDuncan P. N. Exon Smith     llvm::ConstantInt::get(Int32Ty, NumRegionCounters),
258b4416f58SJustin Bogner     llvm::ConstantInt::get(Int64Ty, FunctionHash),
2592fe531cbSDuncan P. N. Exon Smith     llvm::ConstantExpr::getBitCast(Name, Int8PtrTy),
2602fe531cbSDuncan P. N. Exon Smith     llvm::ConstantExpr::getBitCast(RegionCounters, Int64PtrTy)
2612fe531cbSDuncan P. N. Exon Smith   };
2622fe531cbSDuncan P. N. Exon Smith   auto *Data =
26373f78627SDuncan P. N. Exon Smith     new llvm::GlobalVariable(CGM.getModule(), DataTy, true, VarLinkage,
2642fe531cbSDuncan P. N. Exon Smith                              llvm::ConstantStruct::get(DataTy, DataVals),
2652fe531cbSDuncan P. N. Exon Smith                              getFuncVarName("data"));
2662fe531cbSDuncan P. N. Exon Smith 
2672fe531cbSDuncan P. N. Exon Smith   // All the data should be packed into an array in its own section.
2682fe531cbSDuncan P. N. Exon Smith   Data->setSection(getDataSection(CGM));
2692fe531cbSDuncan P. N. Exon Smith   Data->setAlignment(8);
2702fe531cbSDuncan P. N. Exon Smith 
2712fe531cbSDuncan P. N. Exon Smith   // Make sure the data doesn't get deleted.
2722fe531cbSDuncan P. N. Exon Smith   CGM.addUsedGlobal(Data);
2732fe531cbSDuncan P. N. Exon Smith   return Data;
2742fe531cbSDuncan P. N. Exon Smith }
2752fe531cbSDuncan P. N. Exon Smith 
2762fe531cbSDuncan P. N. Exon Smith void CodeGenPGO::emitInstrumentationData() {
277ef512b99SJustin Bogner   if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
278ef512b99SJustin Bogner     return;
279ef512b99SJustin Bogner 
2802fe531cbSDuncan P. N. Exon Smith   // Build the data.
2812fe531cbSDuncan P. N. Exon Smith   auto *Data = buildDataVar();
282ef512b99SJustin Bogner 
2832fe531cbSDuncan P. N. Exon Smith   // Register the data.
284780443e8SDuncan P. N. Exon Smith   auto *RegisterBB = getOrInsertRegisterBB(CGM);
285780443e8SDuncan P. N. Exon Smith   if (!RegisterBB)
286780443e8SDuncan P. N. Exon Smith     return;
287780443e8SDuncan P. N. Exon Smith   CGBuilderTy Builder(RegisterBB->getTerminator());
2882fe531cbSDuncan P. N. Exon Smith   auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
2892fe531cbSDuncan P. N. Exon Smith   Builder.CreateCall(getOrInsertRuntimeRegister(CGM),
2902fe531cbSDuncan P. N. Exon Smith                      Builder.CreateBitCast(Data, VoidPtrTy));
291ef512b99SJustin Bogner }
292ef512b99SJustin Bogner 
293ef512b99SJustin Bogner llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) {
2942fe531cbSDuncan P. N. Exon Smith   if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
295a5f804a7SDuncan P. N. Exon Smith     return nullptr;
296ef512b99SJustin Bogner 
297f2ea775eSJustin Bogner   assert(CGM.getModule().getFunction("__llvm_profile_init") == nullptr &&
298f2ea775eSJustin Bogner          "profile initialization already emitted");
299ef512b99SJustin Bogner 
3005188e91cSDuncan P. N. Exon Smith   // Get the function to call at initialization.
3012fe531cbSDuncan P. N. Exon Smith   llvm::Constant *RegisterF = getRegisterFunc(CGM);
3025188e91cSDuncan P. N. Exon Smith   if (!RegisterF)
303a5f804a7SDuncan P. N. Exon Smith     return nullptr;
3042fe531cbSDuncan P. N. Exon Smith 
3052fe531cbSDuncan P. N. Exon Smith   // Create the initialization function.
3062fe531cbSDuncan P. N. Exon Smith   auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
3072fe531cbSDuncan P. N. Exon Smith   auto *F = llvm::Function::Create(llvm::FunctionType::get(VoidTy, false),
3082fe531cbSDuncan P. N. Exon Smith                                    llvm::GlobalValue::InternalLinkage,
309a7807637SDuncan P. N. Exon Smith                                    "__llvm_profile_init", &CGM.getModule());
310ef512b99SJustin Bogner   F->setUnnamedAddr(true);
311ef512b99SJustin Bogner   F->addFnAttr(llvm::Attribute::NoInline);
312ef512b99SJustin Bogner   if (CGM.getCodeGenOpts().DisableRedZone)
313ef512b99SJustin Bogner     F->addFnAttr(llvm::Attribute::NoRedZone);
314ef512b99SJustin Bogner 
3152fe531cbSDuncan P. N. Exon Smith   // Add the basic block and the necessary calls.
3162fe531cbSDuncan P. N. Exon Smith   CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", F));
3172fe531cbSDuncan P. N. Exon Smith   Builder.CreateCall(RegisterF);
3182fe531cbSDuncan P. N. Exon Smith   Builder.CreateRetVoid();
319ef512b99SJustin Bogner 
320ef512b99SJustin Bogner   return F;
321ef512b99SJustin Bogner }
322ef512b99SJustin Bogner 
323ef512b99SJustin Bogner namespace {
324d8931425SBob Wilson   /// A RecursiveASTVisitor that fills a map of statements to PGO counters.
325d8931425SBob Wilson   struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
326ef512b99SJustin Bogner     /// The next counter value to assign.
327ef512b99SJustin Bogner     unsigned NextCounter;
328ef512b99SJustin Bogner     /// The map of statements to counters.
3293586be72SDuncan P. N. Exon Smith     llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
330ef512b99SJustin Bogner 
3313586be72SDuncan P. N. Exon Smith     MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap)
3323586be72SDuncan P. N. Exon Smith         : NextCounter(0), CounterMap(CounterMap) {}
333ef512b99SJustin Bogner 
334191ec63bSJustin Bogner     // Blocks and lambdas are handled as separate functions, so we need not
335191ec63bSJustin Bogner     // traverse them in the parent context.
336191ec63bSJustin Bogner     bool TraverseBlockExpr(BlockExpr *BE) { return true; }
337191ec63bSJustin Bogner     bool TraverseLambdaBody(LambdaExpr *LE) { return true; }
33881ab90f7SJustin Bogner     bool TraverseCapturedStmt(CapturedStmt *CS) { return true; }
339ef512b99SJustin Bogner 
340d8931425SBob Wilson     bool VisitDecl(const Decl *D) {
341d8931425SBob Wilson       switch (D->getKind()) {
342d8931425SBob Wilson       default:
343d8931425SBob Wilson         break;
344d8931425SBob Wilson       case Decl::Function:
345d8931425SBob Wilson       case Decl::CXXMethod:
346d8931425SBob Wilson       case Decl::CXXConstructor:
347d8931425SBob Wilson       case Decl::CXXDestructor:
348d8931425SBob Wilson       case Decl::CXXConversion:
349d8931425SBob Wilson       case Decl::ObjCMethod:
350d8931425SBob Wilson       case Decl::Block:
35181ab90f7SJustin Bogner       case Decl::Captured:
3524a2f5ae8SDuncan P. N. Exon Smith         CounterMap[D->getBody()] = NextCounter++;
353d8931425SBob Wilson         break;
354ef512b99SJustin Bogner       }
355d8931425SBob Wilson       return true;
3565ec8fe19SBob Wilson     }
357d8931425SBob Wilson 
358d8931425SBob Wilson     bool VisitStmt(const Stmt *S) {
359d8931425SBob Wilson       switch (S->getStmtClass()) {
360d8931425SBob Wilson       default:
361d8931425SBob Wilson         break;
362d8931425SBob Wilson       case Stmt::LabelStmtClass:
363d8931425SBob Wilson       case Stmt::WhileStmtClass:
364d8931425SBob Wilson       case Stmt::DoStmtClass:
365d8931425SBob Wilson       case Stmt::ForStmtClass:
366d8931425SBob Wilson       case Stmt::CXXForRangeStmtClass:
367d8931425SBob Wilson       case Stmt::ObjCForCollectionStmtClass:
368d8931425SBob Wilson       case Stmt::SwitchStmtClass:
369d8931425SBob Wilson       case Stmt::CaseStmtClass:
370d8931425SBob Wilson       case Stmt::DefaultStmtClass:
371d8931425SBob Wilson       case Stmt::IfStmtClass:
372d8931425SBob Wilson       case Stmt::CXXTryStmtClass:
373d8931425SBob Wilson       case Stmt::CXXCatchStmtClass:
374d8931425SBob Wilson       case Stmt::ConditionalOperatorClass:
375d8931425SBob Wilson       case Stmt::BinaryConditionalOperatorClass:
3763586be72SDuncan P. N. Exon Smith         CounterMap[S] = NextCounter++;
377d8931425SBob Wilson         break;
378d8931425SBob Wilson       case Stmt::BinaryOperatorClass: {
379d8931425SBob Wilson         const BinaryOperator *BO = cast<BinaryOperator>(S);
380d8931425SBob Wilson         if (BO->getOpcode() == BO_LAnd || BO->getOpcode() == BO_LOr)
3813586be72SDuncan P. N. Exon Smith           CounterMap[S] = NextCounter++;
382d8931425SBob Wilson         break;
383ef512b99SJustin Bogner       }
384ef512b99SJustin Bogner       }
385d8931425SBob Wilson       return true;
386ef512b99SJustin Bogner     }
387ef512b99SJustin Bogner   };
388bf854f0fSBob Wilson 
389bf854f0fSBob Wilson   /// A StmtVisitor that propagates the raw counts through the AST and
390bf854f0fSBob Wilson   /// records the count at statements where the value may change.
391bf854f0fSBob Wilson   struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> {
392bf854f0fSBob Wilson     /// PGO state.
393bf854f0fSBob Wilson     CodeGenPGO &PGO;
394bf854f0fSBob Wilson 
395bf854f0fSBob Wilson     /// A flag that is set when the current count should be recorded on the
396bf854f0fSBob Wilson     /// next statement, such as at the exit of a loop.
397bf854f0fSBob Wilson     bool RecordNextStmtCount;
398bf854f0fSBob Wilson 
399bf854f0fSBob Wilson     /// The map of statements to count values.
4003586be72SDuncan P. N. Exon Smith     llvm::DenseMap<const Stmt *, uint64_t> &CountMap;
401bf854f0fSBob Wilson 
402bf854f0fSBob Wilson     /// BreakContinueStack - Keep counts of breaks and continues inside loops.
403bf854f0fSBob Wilson     struct BreakContinue {
404bf854f0fSBob Wilson       uint64_t BreakCount;
405bf854f0fSBob Wilson       uint64_t ContinueCount;
406bf854f0fSBob Wilson       BreakContinue() : BreakCount(0), ContinueCount(0) {}
407bf854f0fSBob Wilson     };
408bf854f0fSBob Wilson     SmallVector<BreakContinue, 8> BreakContinueStack;
409bf854f0fSBob Wilson 
4103586be72SDuncan P. N. Exon Smith     ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap,
4113586be72SDuncan P. N. Exon Smith                         CodeGenPGO &PGO)
4123586be72SDuncan P. N. Exon Smith         : PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {}
413bf854f0fSBob Wilson 
414bf854f0fSBob Wilson     void RecordStmtCount(const Stmt *S) {
415bf854f0fSBob Wilson       if (RecordNextStmtCount) {
4163586be72SDuncan P. N. Exon Smith         CountMap[S] = PGO.getCurrentRegionCount();
417bf854f0fSBob Wilson         RecordNextStmtCount = false;
418bf854f0fSBob Wilson       }
419bf854f0fSBob Wilson     }
420bf854f0fSBob Wilson 
421bf854f0fSBob Wilson     void VisitStmt(const Stmt *S) {
422bf854f0fSBob Wilson       RecordStmtCount(S);
423bf854f0fSBob Wilson       for (Stmt::const_child_range I = S->children(); I; ++I) {
424bf854f0fSBob Wilson         if (*I)
425bf854f0fSBob Wilson          this->Visit(*I);
426bf854f0fSBob Wilson       }
427bf854f0fSBob Wilson     }
428bf854f0fSBob Wilson 
4294a2f5ae8SDuncan P. N. Exon Smith     void VisitFunctionDecl(const FunctionDecl *D) {
430d8931425SBob Wilson       // Counter tracks entry to the function body.
4314a2f5ae8SDuncan P. N. Exon Smith       RegionCounter Cnt(PGO, D->getBody());
432bf854f0fSBob Wilson       Cnt.beginRegion();
4334a2f5ae8SDuncan P. N. Exon Smith       CountMap[D->getBody()] = PGO.getCurrentRegionCount();
4344a2f5ae8SDuncan P. N. Exon Smith       Visit(D->getBody());
435bf854f0fSBob Wilson     }
436bf854f0fSBob Wilson 
437191ec63bSJustin Bogner     // Skip lambda expressions. We visit these as FunctionDecls when we're
438191ec63bSJustin Bogner     // generating them and aren't interested in the body when generating a
439191ec63bSJustin Bogner     // parent context.
440191ec63bSJustin Bogner     void VisitLambdaExpr(const LambdaExpr *LE) {}
441191ec63bSJustin Bogner 
44281ab90f7SJustin Bogner     void VisitCapturedDecl(const CapturedDecl *D) {
44381ab90f7SJustin Bogner       // Counter tracks entry to the capture body.
44481ab90f7SJustin Bogner       RegionCounter Cnt(PGO, D->getBody());
44581ab90f7SJustin Bogner       Cnt.beginRegion();
44681ab90f7SJustin Bogner       CountMap[D->getBody()] = PGO.getCurrentRegionCount();
44781ab90f7SJustin Bogner       Visit(D->getBody());
44881ab90f7SJustin Bogner     }
44981ab90f7SJustin Bogner 
4504a2f5ae8SDuncan P. N. Exon Smith     void VisitObjCMethodDecl(const ObjCMethodDecl *D) {
451d8931425SBob Wilson       // Counter tracks entry to the method body.
4524a2f5ae8SDuncan P. N. Exon Smith       RegionCounter Cnt(PGO, D->getBody());
4535ec8fe19SBob Wilson       Cnt.beginRegion();
4544a2f5ae8SDuncan P. N. Exon Smith       CountMap[D->getBody()] = PGO.getCurrentRegionCount();
4554a2f5ae8SDuncan P. N. Exon Smith       Visit(D->getBody());
4565ec8fe19SBob Wilson     }
4575ec8fe19SBob Wilson 
4584a2f5ae8SDuncan P. N. Exon Smith     void VisitBlockDecl(const BlockDecl *D) {
459d8931425SBob Wilson       // Counter tracks entry to the block body.
4604a2f5ae8SDuncan P. N. Exon Smith       RegionCounter Cnt(PGO, D->getBody());
461c845c00aSBob Wilson       Cnt.beginRegion();
4624a2f5ae8SDuncan P. N. Exon Smith       CountMap[D->getBody()] = PGO.getCurrentRegionCount();
4634a2f5ae8SDuncan P. N. Exon Smith       Visit(D->getBody());
464c845c00aSBob Wilson     }
465c845c00aSBob Wilson 
466bf854f0fSBob Wilson     void VisitReturnStmt(const ReturnStmt *S) {
467bf854f0fSBob Wilson       RecordStmtCount(S);
468bf854f0fSBob Wilson       if (S->getRetValue())
469bf854f0fSBob Wilson         Visit(S->getRetValue());
470bf854f0fSBob Wilson       PGO.setCurrentRegionUnreachable();
471bf854f0fSBob Wilson       RecordNextStmtCount = true;
472bf854f0fSBob Wilson     }
473bf854f0fSBob Wilson 
474bf854f0fSBob Wilson     void VisitGotoStmt(const GotoStmt *S) {
475bf854f0fSBob Wilson       RecordStmtCount(S);
476bf854f0fSBob Wilson       PGO.setCurrentRegionUnreachable();
477bf854f0fSBob Wilson       RecordNextStmtCount = true;
478bf854f0fSBob Wilson     }
479bf854f0fSBob Wilson 
480bf854f0fSBob Wilson     void VisitLabelStmt(const LabelStmt *S) {
481bf854f0fSBob Wilson       RecordNextStmtCount = false;
482d8931425SBob Wilson       // Counter tracks the block following the label.
483bf854f0fSBob Wilson       RegionCounter Cnt(PGO, S);
484bf854f0fSBob Wilson       Cnt.beginRegion();
4853586be72SDuncan P. N. Exon Smith       CountMap[S] = PGO.getCurrentRegionCount();
486bf854f0fSBob Wilson       Visit(S->getSubStmt());
487bf854f0fSBob Wilson     }
488bf854f0fSBob Wilson 
489bf854f0fSBob Wilson     void VisitBreakStmt(const BreakStmt *S) {
490bf854f0fSBob Wilson       RecordStmtCount(S);
491bf854f0fSBob Wilson       assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
492bf854f0fSBob Wilson       BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount();
493bf854f0fSBob Wilson       PGO.setCurrentRegionUnreachable();
494bf854f0fSBob Wilson       RecordNextStmtCount = true;
495bf854f0fSBob Wilson     }
496bf854f0fSBob Wilson 
497bf854f0fSBob Wilson     void VisitContinueStmt(const ContinueStmt *S) {
498bf854f0fSBob Wilson       RecordStmtCount(S);
499bf854f0fSBob Wilson       assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
500bf854f0fSBob Wilson       BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount();
501bf854f0fSBob Wilson       PGO.setCurrentRegionUnreachable();
502bf854f0fSBob Wilson       RecordNextStmtCount = true;
503bf854f0fSBob Wilson     }
504bf854f0fSBob Wilson 
505bf854f0fSBob Wilson     void VisitWhileStmt(const WhileStmt *S) {
506bf854f0fSBob Wilson       RecordStmtCount(S);
507d8931425SBob Wilson       // Counter tracks the body of the loop.
508bf854f0fSBob Wilson       RegionCounter Cnt(PGO, S);
509bf854f0fSBob Wilson       BreakContinueStack.push_back(BreakContinue());
510bf854f0fSBob Wilson       // Visit the body region first so the break/continue adjustments can be
511bf854f0fSBob Wilson       // included when visiting the condition.
512bf854f0fSBob Wilson       Cnt.beginRegion();
5133586be72SDuncan P. N. Exon Smith       CountMap[S->getBody()] = PGO.getCurrentRegionCount();
514bf854f0fSBob Wilson       Visit(S->getBody());
515bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
516bf854f0fSBob Wilson 
517bf854f0fSBob Wilson       // ...then go back and propagate counts through the condition. The count
518bf854f0fSBob Wilson       // at the start of the condition is the sum of the incoming edges,
519bf854f0fSBob Wilson       // the backedge from the end of the loop body, and the edges from
520bf854f0fSBob Wilson       // continue statements.
521bf854f0fSBob Wilson       BreakContinue BC = BreakContinueStack.pop_back_val();
522bf854f0fSBob Wilson       Cnt.setCurrentRegionCount(Cnt.getParentCount() +
523bf854f0fSBob Wilson                                 Cnt.getAdjustedCount() + BC.ContinueCount);
5243586be72SDuncan P. N. Exon Smith       CountMap[S->getCond()] = PGO.getCurrentRegionCount();
525bf854f0fSBob Wilson       Visit(S->getCond());
526bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
527bf854f0fSBob Wilson       Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
528bf854f0fSBob Wilson       RecordNextStmtCount = true;
529bf854f0fSBob Wilson     }
530bf854f0fSBob Wilson 
531bf854f0fSBob Wilson     void VisitDoStmt(const DoStmt *S) {
532bf854f0fSBob Wilson       RecordStmtCount(S);
533d8931425SBob Wilson       // Counter tracks the body of the loop.
534bf854f0fSBob Wilson       RegionCounter Cnt(PGO, S);
535bf854f0fSBob Wilson       BreakContinueStack.push_back(BreakContinue());
536bf854f0fSBob Wilson       Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
5373586be72SDuncan P. N. Exon Smith       CountMap[S->getBody()] = PGO.getCurrentRegionCount();
538bf854f0fSBob Wilson       Visit(S->getBody());
539bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
540bf854f0fSBob Wilson 
541bf854f0fSBob Wilson       BreakContinue BC = BreakContinueStack.pop_back_val();
542bf854f0fSBob Wilson       // The count at the start of the condition is equal to the count at the
543bf854f0fSBob Wilson       // end of the body. The adjusted count does not include either the
544bf854f0fSBob Wilson       // fall-through count coming into the loop or the continue count, so add
545bf854f0fSBob Wilson       // both of those separately. This is coincidentally the same equation as
546bf854f0fSBob Wilson       // with while loops but for different reasons.
547bf854f0fSBob Wilson       Cnt.setCurrentRegionCount(Cnt.getParentCount() +
548bf854f0fSBob Wilson                                 Cnt.getAdjustedCount() + BC.ContinueCount);
5493586be72SDuncan P. N. Exon Smith       CountMap[S->getCond()] = PGO.getCurrentRegionCount();
550bf854f0fSBob Wilson       Visit(S->getCond());
551bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
552bf854f0fSBob Wilson       Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
553bf854f0fSBob Wilson       RecordNextStmtCount = true;
554bf854f0fSBob Wilson     }
555bf854f0fSBob Wilson 
556bf854f0fSBob Wilson     void VisitForStmt(const ForStmt *S) {
557bf854f0fSBob Wilson       RecordStmtCount(S);
558bf854f0fSBob Wilson       if (S->getInit())
559bf854f0fSBob Wilson         Visit(S->getInit());
560d8931425SBob Wilson       // Counter tracks the body of the loop.
561bf854f0fSBob Wilson       RegionCounter Cnt(PGO, S);
562bf854f0fSBob Wilson       BreakContinueStack.push_back(BreakContinue());
563bf854f0fSBob Wilson       // Visit the body region first. (This is basically the same as a while
564bf854f0fSBob Wilson       // loop; see further comments in VisitWhileStmt.)
565bf854f0fSBob Wilson       Cnt.beginRegion();
5663586be72SDuncan P. N. Exon Smith       CountMap[S->getBody()] = PGO.getCurrentRegionCount();
567bf854f0fSBob Wilson       Visit(S->getBody());
568bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
569bf854f0fSBob Wilson 
570bf854f0fSBob Wilson       // The increment is essentially part of the body but it needs to include
571bf854f0fSBob Wilson       // the count for all the continue statements.
572bf854f0fSBob Wilson       if (S->getInc()) {
573bf854f0fSBob Wilson         Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
574bf854f0fSBob Wilson                                   BreakContinueStack.back().ContinueCount);
5753586be72SDuncan P. N. Exon Smith         CountMap[S->getInc()] = PGO.getCurrentRegionCount();
576bf854f0fSBob Wilson         Visit(S->getInc());
577bf854f0fSBob Wilson         Cnt.adjustForControlFlow();
578bf854f0fSBob Wilson       }
579bf854f0fSBob Wilson 
580bf854f0fSBob Wilson       BreakContinue BC = BreakContinueStack.pop_back_val();
581bf854f0fSBob Wilson 
582bf854f0fSBob Wilson       // ...then go back and propagate counts through the condition.
583bf854f0fSBob Wilson       if (S->getCond()) {
584bf854f0fSBob Wilson         Cnt.setCurrentRegionCount(Cnt.getParentCount() +
585bf854f0fSBob Wilson                                   Cnt.getAdjustedCount() +
586bf854f0fSBob Wilson                                   BC.ContinueCount);
5873586be72SDuncan P. N. Exon Smith         CountMap[S->getCond()] = PGO.getCurrentRegionCount();
588bf854f0fSBob Wilson         Visit(S->getCond());
589bf854f0fSBob Wilson         Cnt.adjustForControlFlow();
590bf854f0fSBob Wilson       }
591bf854f0fSBob Wilson       Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
592bf854f0fSBob Wilson       RecordNextStmtCount = true;
593bf854f0fSBob Wilson     }
594bf854f0fSBob Wilson 
595bf854f0fSBob Wilson     void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
596bf854f0fSBob Wilson       RecordStmtCount(S);
597bf854f0fSBob Wilson       Visit(S->getRangeStmt());
598bf854f0fSBob Wilson       Visit(S->getBeginEndStmt());
599d8931425SBob Wilson       // Counter tracks the body of the loop.
600bf854f0fSBob Wilson       RegionCounter Cnt(PGO, S);
601bf854f0fSBob Wilson       BreakContinueStack.push_back(BreakContinue());
602bf854f0fSBob Wilson       // Visit the body region first. (This is basically the same as a while
603bf854f0fSBob Wilson       // loop; see further comments in VisitWhileStmt.)
604bf854f0fSBob Wilson       Cnt.beginRegion();
6053586be72SDuncan P. N. Exon Smith       CountMap[S->getLoopVarStmt()] = PGO.getCurrentRegionCount();
606bf854f0fSBob Wilson       Visit(S->getLoopVarStmt());
607bf854f0fSBob Wilson       Visit(S->getBody());
608bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
609bf854f0fSBob Wilson 
610bf854f0fSBob Wilson       // The increment is essentially part of the body but it needs to include
611bf854f0fSBob Wilson       // the count for all the continue statements.
612bf854f0fSBob Wilson       Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
613bf854f0fSBob Wilson                                 BreakContinueStack.back().ContinueCount);
6143586be72SDuncan P. N. Exon Smith       CountMap[S->getInc()] = PGO.getCurrentRegionCount();
615bf854f0fSBob Wilson       Visit(S->getInc());
616bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
617bf854f0fSBob Wilson 
618bf854f0fSBob Wilson       BreakContinue BC = BreakContinueStack.pop_back_val();
619bf854f0fSBob Wilson 
620bf854f0fSBob Wilson       // ...then go back and propagate counts through the condition.
621bf854f0fSBob Wilson       Cnt.setCurrentRegionCount(Cnt.getParentCount() +
622bf854f0fSBob Wilson                                 Cnt.getAdjustedCount() +
623bf854f0fSBob Wilson                                 BC.ContinueCount);
6243586be72SDuncan P. N. Exon Smith       CountMap[S->getCond()] = PGO.getCurrentRegionCount();
625bf854f0fSBob Wilson       Visit(S->getCond());
626bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
627bf854f0fSBob Wilson       Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
628bf854f0fSBob Wilson       RecordNextStmtCount = true;
629bf854f0fSBob Wilson     }
630bf854f0fSBob Wilson 
631bf854f0fSBob Wilson     void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
632bf854f0fSBob Wilson       RecordStmtCount(S);
633bf854f0fSBob Wilson       Visit(S->getElement());
634d8931425SBob Wilson       // Counter tracks the body of the loop.
635bf854f0fSBob Wilson       RegionCounter Cnt(PGO, S);
636bf854f0fSBob Wilson       BreakContinueStack.push_back(BreakContinue());
637bf854f0fSBob Wilson       Cnt.beginRegion();
6383586be72SDuncan P. N. Exon Smith       CountMap[S->getBody()] = PGO.getCurrentRegionCount();
639bf854f0fSBob Wilson       Visit(S->getBody());
640bf854f0fSBob Wilson       BreakContinue BC = BreakContinueStack.pop_back_val();
641bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
642bf854f0fSBob Wilson       Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
643bf854f0fSBob Wilson       RecordNextStmtCount = true;
644bf854f0fSBob Wilson     }
645bf854f0fSBob Wilson 
646bf854f0fSBob Wilson     void VisitSwitchStmt(const SwitchStmt *S) {
647bf854f0fSBob Wilson       RecordStmtCount(S);
648bf854f0fSBob Wilson       Visit(S->getCond());
649bf854f0fSBob Wilson       PGO.setCurrentRegionUnreachable();
650bf854f0fSBob Wilson       BreakContinueStack.push_back(BreakContinue());
651bf854f0fSBob Wilson       Visit(S->getBody());
652bf854f0fSBob Wilson       // If the switch is inside a loop, add the continue counts.
653bf854f0fSBob Wilson       BreakContinue BC = BreakContinueStack.pop_back_val();
654bf854f0fSBob Wilson       if (!BreakContinueStack.empty())
655bf854f0fSBob Wilson         BreakContinueStack.back().ContinueCount += BC.ContinueCount;
656d8931425SBob Wilson       // Counter tracks the exit block of the switch.
657bf854f0fSBob Wilson       RegionCounter ExitCnt(PGO, S);
658bf854f0fSBob Wilson       ExitCnt.beginRegion();
659bf854f0fSBob Wilson       RecordNextStmtCount = true;
660bf854f0fSBob Wilson     }
661bf854f0fSBob Wilson 
662bf854f0fSBob Wilson     void VisitCaseStmt(const CaseStmt *S) {
663bf854f0fSBob Wilson       RecordNextStmtCount = false;
664d8931425SBob Wilson       // Counter for this particular case. This counts only jumps from the
665d8931425SBob Wilson       // switch header and does not include fallthrough from the case before
666d8931425SBob Wilson       // this one.
667bf854f0fSBob Wilson       RegionCounter Cnt(PGO, S);
668bf854f0fSBob Wilson       Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
6693586be72SDuncan P. N. Exon Smith       CountMap[S] = Cnt.getCount();
670bf854f0fSBob Wilson       RecordNextStmtCount = true;
671bf854f0fSBob Wilson       Visit(S->getSubStmt());
672bf854f0fSBob Wilson     }
673bf854f0fSBob Wilson 
674bf854f0fSBob Wilson     void VisitDefaultStmt(const DefaultStmt *S) {
675bf854f0fSBob Wilson       RecordNextStmtCount = false;
676d8931425SBob Wilson       // Counter for this default case. This does not include fallthrough from
677d8931425SBob Wilson       // the previous case.
678bf854f0fSBob Wilson       RegionCounter Cnt(PGO, S);
679bf854f0fSBob Wilson       Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
6803586be72SDuncan P. N. Exon Smith       CountMap[S] = Cnt.getCount();
681bf854f0fSBob Wilson       RecordNextStmtCount = true;
682bf854f0fSBob Wilson       Visit(S->getSubStmt());
683bf854f0fSBob Wilson     }
684bf854f0fSBob Wilson 
685bf854f0fSBob Wilson     void VisitIfStmt(const IfStmt *S) {
686bf854f0fSBob Wilson       RecordStmtCount(S);
687d8931425SBob Wilson       // Counter tracks the "then" part of an if statement. The count for
688d8931425SBob Wilson       // the "else" part, if it exists, will be calculated from this counter.
689bf854f0fSBob Wilson       RegionCounter Cnt(PGO, S);
690bf854f0fSBob Wilson       Visit(S->getCond());
691bf854f0fSBob Wilson 
692bf854f0fSBob Wilson       Cnt.beginRegion();
6933586be72SDuncan P. N. Exon Smith       CountMap[S->getThen()] = PGO.getCurrentRegionCount();
694bf854f0fSBob Wilson       Visit(S->getThen());
695bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
696bf854f0fSBob Wilson 
697bf854f0fSBob Wilson       if (S->getElse()) {
698bf854f0fSBob Wilson         Cnt.beginElseRegion();
6993586be72SDuncan P. N. Exon Smith         CountMap[S->getElse()] = PGO.getCurrentRegionCount();
700bf854f0fSBob Wilson         Visit(S->getElse());
701bf854f0fSBob Wilson         Cnt.adjustForControlFlow();
702bf854f0fSBob Wilson       }
703bf854f0fSBob Wilson       Cnt.applyAdjustmentsToRegion(0);
704bf854f0fSBob Wilson       RecordNextStmtCount = true;
705bf854f0fSBob Wilson     }
706bf854f0fSBob Wilson 
707bf854f0fSBob Wilson     void VisitCXXTryStmt(const CXXTryStmt *S) {
708bf854f0fSBob Wilson       RecordStmtCount(S);
709bf854f0fSBob Wilson       Visit(S->getTryBlock());
710bf854f0fSBob Wilson       for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
711bf854f0fSBob Wilson         Visit(S->getHandler(I));
712d8931425SBob Wilson       // Counter tracks the continuation block of the try statement.
713bf854f0fSBob Wilson       RegionCounter Cnt(PGO, S);
714bf854f0fSBob Wilson       Cnt.beginRegion();
715bf854f0fSBob Wilson       RecordNextStmtCount = true;
716bf854f0fSBob Wilson     }
717bf854f0fSBob Wilson 
718bf854f0fSBob Wilson     void VisitCXXCatchStmt(const CXXCatchStmt *S) {
719bf854f0fSBob Wilson       RecordNextStmtCount = false;
720d8931425SBob Wilson       // Counter tracks the catch statement's handler block.
721bf854f0fSBob Wilson       RegionCounter Cnt(PGO, S);
722bf854f0fSBob Wilson       Cnt.beginRegion();
7233586be72SDuncan P. N. Exon Smith       CountMap[S] = PGO.getCurrentRegionCount();
724bf854f0fSBob Wilson       Visit(S->getHandlerBlock());
725bf854f0fSBob Wilson     }
726bf854f0fSBob Wilson 
72753c55d99SJustin Bogner     void VisitAbstractConditionalOperator(
72853c55d99SJustin Bogner         const AbstractConditionalOperator *E) {
729bf854f0fSBob Wilson       RecordStmtCount(E);
730d8931425SBob Wilson       // Counter tracks the "true" part of a conditional operator. The
731d8931425SBob Wilson       // count in the "false" part will be calculated from this counter.
732bf854f0fSBob Wilson       RegionCounter Cnt(PGO, E);
733bf854f0fSBob Wilson       Visit(E->getCond());
734bf854f0fSBob Wilson 
735bf854f0fSBob Wilson       Cnt.beginRegion();
7363586be72SDuncan P. N. Exon Smith       CountMap[E->getTrueExpr()] = PGO.getCurrentRegionCount();
737bf854f0fSBob Wilson       Visit(E->getTrueExpr());
738bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
739bf854f0fSBob Wilson 
740bf854f0fSBob Wilson       Cnt.beginElseRegion();
7413586be72SDuncan P. N. Exon Smith       CountMap[E->getFalseExpr()] = PGO.getCurrentRegionCount();
742bf854f0fSBob Wilson       Visit(E->getFalseExpr());
743bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
744bf854f0fSBob Wilson 
745bf854f0fSBob Wilson       Cnt.applyAdjustmentsToRegion(0);
746bf854f0fSBob Wilson       RecordNextStmtCount = true;
747bf854f0fSBob Wilson     }
748bf854f0fSBob Wilson 
749bf854f0fSBob Wilson     void VisitBinLAnd(const BinaryOperator *E) {
750bf854f0fSBob Wilson       RecordStmtCount(E);
751d8931425SBob Wilson       // Counter tracks the right hand side of a logical and operator.
752bf854f0fSBob Wilson       RegionCounter Cnt(PGO, E);
753bf854f0fSBob Wilson       Visit(E->getLHS());
754bf854f0fSBob Wilson       Cnt.beginRegion();
7553586be72SDuncan P. N. Exon Smith       CountMap[E->getRHS()] = PGO.getCurrentRegionCount();
756bf854f0fSBob Wilson       Visit(E->getRHS());
757bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
758bf854f0fSBob Wilson       Cnt.applyAdjustmentsToRegion(0);
759bf854f0fSBob Wilson       RecordNextStmtCount = true;
760bf854f0fSBob Wilson     }
761bf854f0fSBob Wilson 
762bf854f0fSBob Wilson     void VisitBinLOr(const BinaryOperator *E) {
763bf854f0fSBob Wilson       RecordStmtCount(E);
764d8931425SBob Wilson       // Counter tracks the right hand side of a logical or operator.
765bf854f0fSBob Wilson       RegionCounter Cnt(PGO, E);
766bf854f0fSBob Wilson       Visit(E->getLHS());
767bf854f0fSBob Wilson       Cnt.beginRegion();
7683586be72SDuncan P. N. Exon Smith       CountMap[E->getRHS()] = PGO.getCurrentRegionCount();
769bf854f0fSBob Wilson       Visit(E->getRHS());
770bf854f0fSBob Wilson       Cnt.adjustForControlFlow();
771bf854f0fSBob Wilson       Cnt.applyAdjustmentsToRegion(0);
772bf854f0fSBob Wilson       RecordNextStmtCount = true;
773bf854f0fSBob Wilson     }
774bf854f0fSBob Wilson   };
775ef512b99SJustin Bogner }
776ef512b99SJustin Bogner 
777d971cd1bSDuncan P. N. Exon Smith static void emitRuntimeHook(CodeGenModule &CGM) {
7783fefedb2SDuncan P. N. Exon Smith   const char *const RuntimeVarName = "__llvm_profile_runtime";
7793fefedb2SDuncan P. N. Exon Smith   const char *const RuntimeUserName = "__llvm_profile_runtime_user";
780d971cd1bSDuncan P. N. Exon Smith   if (CGM.getModule().getGlobalVariable(RuntimeVarName))
781d971cd1bSDuncan P. N. Exon Smith     return;
782d971cd1bSDuncan P. N. Exon Smith 
783d971cd1bSDuncan P. N. Exon Smith   // Declare the runtime hook.
784d971cd1bSDuncan P. N. Exon Smith   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
785d971cd1bSDuncan P. N. Exon Smith   auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
786d971cd1bSDuncan P. N. Exon Smith   auto *Var = new llvm::GlobalVariable(CGM.getModule(), Int32Ty, false,
787d971cd1bSDuncan P. N. Exon Smith                                        llvm::GlobalValue::ExternalLinkage,
788d971cd1bSDuncan P. N. Exon Smith                                        nullptr, RuntimeVarName);
789d971cd1bSDuncan P. N. Exon Smith 
790d971cd1bSDuncan P. N. Exon Smith   // Make a function that uses it.
791d971cd1bSDuncan P. N. Exon Smith   auto *User = llvm::Function::Create(llvm::FunctionType::get(Int32Ty, false),
792d971cd1bSDuncan P. N. Exon Smith                                       llvm::GlobalValue::LinkOnceODRLinkage,
793d971cd1bSDuncan P. N. Exon Smith                                       RuntimeUserName, &CGM.getModule());
794d971cd1bSDuncan P. N. Exon Smith   User->addFnAttr(llvm::Attribute::NoInline);
795d971cd1bSDuncan P. N. Exon Smith   if (CGM.getCodeGenOpts().DisableRedZone)
796d971cd1bSDuncan P. N. Exon Smith     User->addFnAttr(llvm::Attribute::NoRedZone);
797d971cd1bSDuncan P. N. Exon Smith   CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", User));
798d971cd1bSDuncan P. N. Exon Smith   auto *Load = Builder.CreateLoad(Var);
799d971cd1bSDuncan P. N. Exon Smith   Builder.CreateRet(Load);
800d971cd1bSDuncan P. N. Exon Smith 
801d971cd1bSDuncan P. N. Exon Smith   // Create a use of the function.  Now the definition of the runtime variable
802d971cd1bSDuncan P. N. Exon Smith   // should get pulled in, along with any static initializears.
803d971cd1bSDuncan P. N. Exon Smith   CGM.addUsedGlobal(User);
804d971cd1bSDuncan P. N. Exon Smith }
805d971cd1bSDuncan P. N. Exon Smith 
806da1ebedeSBob Wilson void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
807ef512b99SJustin Bogner   bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
808d66a17d0SJustin Bogner   PGOProfileData *PGOData = CGM.getPGOData();
809d66a17d0SJustin Bogner   if (!InstrumentRegions && !PGOData)
810ef512b99SJustin Bogner     return;
811ef512b99SJustin Bogner   if (!D)
812ef512b99SJustin Bogner     return;
813da1ebedeSBob Wilson   setFuncName(Fn);
8147c41451bSDuncan P. N. Exon Smith 
8157c41451bSDuncan P. N. Exon Smith   // Set the linkage for variables based on the function linkage.  Usually, we
8167c41451bSDuncan P. N. Exon Smith   // want to match it, but available_externally and extern_weak both have the
8177c41451bSDuncan P. N. Exon Smith   // wrong semantics.
81873f78627SDuncan P. N. Exon Smith   VarLinkage = Fn->getLinkage();
8197c41451bSDuncan P. N. Exon Smith   switch (VarLinkage) {
8207c41451bSDuncan P. N. Exon Smith   case llvm::GlobalValue::ExternalWeakLinkage:
8217c41451bSDuncan P. N. Exon Smith     VarLinkage = llvm::GlobalValue::LinkOnceAnyLinkage;
8227c41451bSDuncan P. N. Exon Smith     break;
8237c41451bSDuncan P. N. Exon Smith   case llvm::GlobalValue::AvailableExternallyLinkage:
8247c41451bSDuncan P. N. Exon Smith     VarLinkage = llvm::GlobalValue::LinkOnceODRLinkage;
8257c41451bSDuncan P. N. Exon Smith     break;
8267c41451bSDuncan P. N. Exon Smith   default:
8277c41451bSDuncan P. N. Exon Smith     break;
8287c41451bSDuncan P. N. Exon Smith   }
8297c41451bSDuncan P. N. Exon Smith 
830ef512b99SJustin Bogner   mapRegionCounters(D);
831d971cd1bSDuncan P. N. Exon Smith   if (InstrumentRegions) {
832d971cd1bSDuncan P. N. Exon Smith     emitRuntimeHook(CGM);
833ef512b99SJustin Bogner     emitCounterVariables();
834d971cd1bSDuncan P. N. Exon Smith   }
835d66a17d0SJustin Bogner   if (PGOData) {
836d66a17d0SJustin Bogner     loadRegionCounts(PGOData);
837bf854f0fSBob Wilson     computeRegionCounts(D);
838d66a17d0SJustin Bogner     applyFunctionAttributes(PGOData, Fn);
839bf854f0fSBob Wilson   }
840ef512b99SJustin Bogner }
841ef512b99SJustin Bogner 
842ef512b99SJustin Bogner void CodeGenPGO::mapRegionCounters(const Decl *D) {
8431b67cfd4SDuncan P. N. Exon Smith   RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
8443586be72SDuncan P. N. Exon Smith   MapRegionCounters Walker(*RegionCounterMap);
845ef512b99SJustin Bogner   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
846d8931425SBob Wilson     Walker.TraverseDecl(const_cast<FunctionDecl *>(FD));
8475ec8fe19SBob Wilson   else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
848d8931425SBob Wilson     Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD));
849c845c00aSBob Wilson   else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
850d8931425SBob Wilson     Walker.TraverseDecl(const_cast<BlockDecl *>(BD));
85181ab90f7SJustin Bogner   else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
85281ab90f7SJustin Bogner     Walker.TraverseDecl(const_cast<CapturedDecl *>(CD));
853ef512b99SJustin Bogner   NumRegionCounters = Walker.NextCounter;
854b4416f58SJustin Bogner   // FIXME: The number of counters isn't sufficient for the hash
855b4416f58SJustin Bogner   FunctionHash = NumRegionCounters;
856ef512b99SJustin Bogner }
857ef512b99SJustin Bogner 
858bf854f0fSBob Wilson void CodeGenPGO::computeRegionCounts(const Decl *D) {
8591b67cfd4SDuncan P. N. Exon Smith   StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>);
8603586be72SDuncan P. N. Exon Smith   ComputeRegionCounts Walker(*StmtCountMap, *this);
861bf854f0fSBob Wilson   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
862bf854f0fSBob Wilson     Walker.VisitFunctionDecl(FD);
8635ec8fe19SBob Wilson   else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
8645ec8fe19SBob Wilson     Walker.VisitObjCMethodDecl(MD);
865c845c00aSBob Wilson   else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
866c845c00aSBob Wilson     Walker.VisitBlockDecl(BD);
86781ab90f7SJustin Bogner   else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
86881ab90f7SJustin Bogner     Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD));
869bf854f0fSBob Wilson }
870bf854f0fSBob Wilson 
871d66a17d0SJustin Bogner void CodeGenPGO::applyFunctionAttributes(PGOProfileData *PGOData,
8724c9c45c0SJustin Bogner                                          llvm::Function *Fn) {
8734c9c45c0SJustin Bogner   if (!haveRegionCounts())
8744c9c45c0SJustin Bogner     return;
8754c9c45c0SJustin Bogner 
876d66a17d0SJustin Bogner   uint64_t MaxFunctionCount = PGOData->getMaximumFunctionCount();
8774c9c45c0SJustin Bogner   uint64_t FunctionCount = getRegionCount(0);
8784c9c45c0SJustin Bogner   if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount))
8794c9c45c0SJustin Bogner     // Turn on InlineHint attribute for hot functions.
8804c9c45c0SJustin Bogner     // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal.
8814c9c45c0SJustin Bogner     Fn->addFnAttr(llvm::Attribute::InlineHint);
8824c9c45c0SJustin Bogner   else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount))
8834c9c45c0SJustin Bogner     // Turn on Cold attribute for cold functions.
8844c9c45c0SJustin Bogner     // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal.
8854c9c45c0SJustin Bogner     Fn->addFnAttr(llvm::Attribute::Cold);
8864c9c45c0SJustin Bogner }
8874c9c45c0SJustin Bogner 
888ef512b99SJustin Bogner void CodeGenPGO::emitCounterVariables() {
889ef512b99SJustin Bogner   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
890ef512b99SJustin Bogner   llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx),
891ef512b99SJustin Bogner                                                     NumRegionCounters);
892ef512b99SJustin Bogner   RegionCounters =
89373f78627SDuncan P. N. Exon Smith     new llvm::GlobalVariable(CGM.getModule(), CounterTy, false, VarLinkage,
894ef512b99SJustin Bogner                              llvm::Constant::getNullValue(CounterTy),
8952fe531cbSDuncan P. N. Exon Smith                              getFuncVarName("counters"));
8962fe531cbSDuncan P. N. Exon Smith   RegionCounters->setAlignment(8);
8972fe531cbSDuncan P. N. Exon Smith   RegionCounters->setSection(getCountersSection(CGM));
898ef512b99SJustin Bogner }
899ef512b99SJustin Bogner 
900ef512b99SJustin Bogner void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) {
901749ebc7fSBob Wilson   if (!RegionCounters)
902ef512b99SJustin Bogner     return;
903ef512b99SJustin Bogner   llvm::Value *Addr =
904ef512b99SJustin Bogner     Builder.CreateConstInBoundsGEP2_64(RegionCounters, 0, Counter);
905ef512b99SJustin Bogner   llvm::Value *Count = Builder.CreateLoad(Addr, "pgocount");
906ef512b99SJustin Bogner   Count = Builder.CreateAdd(Count, Builder.getInt64(1));
907ef512b99SJustin Bogner   Builder.CreateStore(Count, Addr);
908ef512b99SJustin Bogner }
909ef512b99SJustin Bogner 
910d66a17d0SJustin Bogner void CodeGenPGO::loadRegionCounts(PGOProfileData *PGOData) {
911*e2ef2a09SJustin Bogner   CGM.getPGOStats().Visited++;
9121b67cfd4SDuncan P. N. Exon Smith   RegionCounts.reset(new std::vector<uint64_t>);
913b4416f58SJustin Bogner   uint64_t Hash;
914*e2ef2a09SJustin Bogner   if (PGOData->getFunctionCounts(getFuncName(), Hash, *RegionCounts)) {
915*e2ef2a09SJustin Bogner     CGM.getPGOStats().Missing++;
9161b67cfd4SDuncan P. N. Exon Smith     RegionCounts.reset();
917*e2ef2a09SJustin Bogner   } else if (Hash != FunctionHash ||
918*e2ef2a09SJustin Bogner              RegionCounts->size() != NumRegionCounters) {
919*e2ef2a09SJustin Bogner     CGM.getPGOStats().Mismatched++;
920*e2ef2a09SJustin Bogner     RegionCounts.reset();
921*e2ef2a09SJustin Bogner   }
922ef512b99SJustin Bogner }
923ef512b99SJustin Bogner 
924ef512b99SJustin Bogner void CodeGenPGO::destroyRegionCounters() {
9251b67cfd4SDuncan P. N. Exon Smith   RegionCounterMap.reset();
9261b67cfd4SDuncan P. N. Exon Smith   StmtCountMap.reset();
9271b67cfd4SDuncan P. N. Exon Smith   RegionCounts.reset();
928ef512b99SJustin Bogner }
929ef512b99SJustin Bogner 
93038402dc9SDuncan P. N. Exon Smith /// \brief Calculate what to divide by to scale weights.
93138402dc9SDuncan P. N. Exon Smith ///
93238402dc9SDuncan P. N. Exon Smith /// Given the maximum weight, calculate a divisor that will scale all the
93338402dc9SDuncan P. N. Exon Smith /// weights to strictly less than UINT32_MAX.
93438402dc9SDuncan P. N. Exon Smith static uint64_t calculateWeightScale(uint64_t MaxWeight) {
93538402dc9SDuncan P. N. Exon Smith   return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1;
93638402dc9SDuncan P. N. Exon Smith }
93738402dc9SDuncan P. N. Exon Smith 
93838402dc9SDuncan P. N. Exon Smith /// \brief Scale an individual branch weight (and add 1).
93938402dc9SDuncan P. N. Exon Smith ///
94038402dc9SDuncan P. N. Exon Smith /// Scale a 64-bit weight down to 32-bits using \c Scale.
94138402dc9SDuncan P. N. Exon Smith ///
94238402dc9SDuncan P. N. Exon Smith /// According to Laplace's Rule of Succession, it is better to compute the
94338402dc9SDuncan P. N. Exon Smith /// weight based on the count plus 1, so universally add 1 to the value.
94438402dc9SDuncan P. N. Exon Smith ///
94538402dc9SDuncan P. N. Exon Smith /// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no
94638402dc9SDuncan P. N. Exon Smith /// greater than \c Weight.
94738402dc9SDuncan P. N. Exon Smith static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) {
94838402dc9SDuncan P. N. Exon Smith   assert(Scale && "scale by 0?");
94938402dc9SDuncan P. N. Exon Smith   uint64_t Scaled = Weight / Scale + 1;
95038402dc9SDuncan P. N. Exon Smith   assert(Scaled <= UINT32_MAX && "overflow 32-bits");
95138402dc9SDuncan P. N. Exon Smith   return Scaled;
95238402dc9SDuncan P. N. Exon Smith }
95338402dc9SDuncan P. N. Exon Smith 
954ef512b99SJustin Bogner llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount,
955ef512b99SJustin Bogner                                               uint64_t FalseCount) {
95638402dc9SDuncan P. N. Exon Smith   // Check for empty weights.
957ef512b99SJustin Bogner   if (!TrueCount && !FalseCount)
958a5f804a7SDuncan P. N. Exon Smith     return nullptr;
959ef512b99SJustin Bogner 
96038402dc9SDuncan P. N. Exon Smith   // Calculate how to scale down to 32-bits.
96138402dc9SDuncan P. N. Exon Smith   uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount));
96238402dc9SDuncan P. N. Exon Smith 
963ef512b99SJustin Bogner   llvm::MDBuilder MDHelper(CGM.getLLVMContext());
96438402dc9SDuncan P. N. Exon Smith   return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale),
96538402dc9SDuncan P. N. Exon Smith                                       scaleBranchWeight(FalseCount, Scale));
966ef512b99SJustin Bogner }
967ef512b99SJustin Bogner 
96895a27b0eSBob Wilson llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) {
96938402dc9SDuncan P. N. Exon Smith   // We need at least two elements to create meaningful weights.
97038402dc9SDuncan P. N. Exon Smith   if (Weights.size() < 2)
971a5f804a7SDuncan P. N. Exon Smith     return nullptr;
97238402dc9SDuncan P. N. Exon Smith 
973f3aefca7SJustin Bogner   // Check for empty weights.
974f3aefca7SJustin Bogner   uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end());
975f3aefca7SJustin Bogner   if (MaxWeight == 0)
976f3aefca7SJustin Bogner     return nullptr;
977f3aefca7SJustin Bogner 
97838402dc9SDuncan P. N. Exon Smith   // Calculate how to scale down to 32-bits.
979f3aefca7SJustin Bogner   uint64_t Scale = calculateWeightScale(MaxWeight);
98038402dc9SDuncan P. N. Exon Smith 
981ef512b99SJustin Bogner   SmallVector<uint32_t, 16> ScaledWeights;
982ef512b99SJustin Bogner   ScaledWeights.reserve(Weights.size());
98338402dc9SDuncan P. N. Exon Smith   for (uint64_t W : Weights)
98438402dc9SDuncan P. N. Exon Smith     ScaledWeights.push_back(scaleBranchWeight(W, Scale));
98538402dc9SDuncan P. N. Exon Smith 
98638402dc9SDuncan P. N. Exon Smith   llvm::MDBuilder MDHelper(CGM.getLLVMContext());
987ef512b99SJustin Bogner   return MDHelper.createBranchWeights(ScaledWeights);
988ef512b99SJustin Bogner }
989bf854f0fSBob Wilson 
990bf854f0fSBob Wilson llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond,
991bf854f0fSBob Wilson                                             RegionCounter &Cnt) {
992bf854f0fSBob Wilson   if (!haveRegionCounts())
993a5f804a7SDuncan P. N. Exon Smith     return nullptr;
994bf854f0fSBob Wilson   uint64_t LoopCount = Cnt.getCount();
995bf854f0fSBob Wilson   uint64_t CondCount = 0;
996bf854f0fSBob Wilson   bool Found = getStmtCount(Cond, CondCount);
997bf854f0fSBob Wilson   assert(Found && "missing expected loop condition count");
998bf854f0fSBob Wilson   (void)Found;
999bf854f0fSBob Wilson   if (CondCount == 0)
1000a5f804a7SDuncan P. N. Exon Smith     return nullptr;
1001bf854f0fSBob Wilson   return createBranchWeights(LoopCount,
1002bf854f0fSBob Wilson                              std::max(CondCount, LoopCount) - LoopCount);
1003bf854f0fSBob Wilson }
1004