Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1 | //===--- CodeGenPGO.cpp - PGO Instrumentation for LLVM CodeGen --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Instrumentation-based profile-guided optimization |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenPGO.h" |
| 15 | #include "CodeGenFunction.h" |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 16 | #include "CoverageMappingGen.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 17 | #include "clang/AST/RecursiveASTVisitor.h" |
| 18 | #include "clang/AST/StmtVisitor.h" |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Intrinsics.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 20 | #include "llvm/IR/MDBuilder.h" |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 21 | #include "llvm/ProfileData/InstrProfReader.h" |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Endian.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MD5.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 29 | void CodeGenPGO::setFuncName(StringRef Name, |
| 30 | llvm::GlobalValue::LinkageTypes Linkage) { |
Justin Bogner | 111c653 | 2014-12-02 23:15:30 +0000 | [diff] [blame] | 31 | StringRef RawFuncName = Name; |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 32 | |
| 33 | // Function names may be prefixed with a binary '1' to indicate |
| 34 | // that the backend should not modify the symbols due to any platform |
| 35 | // naming convention. Do not include that '1' in the PGO profile name. |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 36 | if (RawFuncName[0] == '\1') |
| 37 | RawFuncName = RawFuncName.substr(1); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 38 | |
Justin Bogner | 111c653 | 2014-12-02 23:15:30 +0000 | [diff] [blame] | 39 | FuncName = RawFuncName; |
| 40 | if (llvm::GlobalValue::isLocalLinkage(Linkage)) { |
| 41 | // For local symbols, prepend the main file name to distinguish them. |
| 42 | // Do not include the full path in the file name since there's no guarantee |
| 43 | // that it will stay the same, e.g., if the files are checked out from |
| 44 | // version control in different locations. |
| 45 | if (CGM.getCodeGenOpts().MainFileName.empty()) |
| 46 | FuncName = FuncName.insert(0, "<unknown>:"); |
| 47 | else |
| 48 | FuncName = FuncName.insert(0, CGM.getCodeGenOpts().MainFileName + ":"); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 49 | } |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 50 | |
| 51 | // If we're generating a profile, create a variable for the name. |
| 52 | if (CGM.getCodeGenOpts().ProfileInstrGenerate) |
| 53 | createFuncNameVar(Linkage); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 56 | void CodeGenPGO::setFuncName(llvm::Function *Fn) { |
| 57 | setFuncName(Fn->getName(), Fn->getLinkage()); |
| 58 | } |
| 59 | |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 60 | void CodeGenPGO::createFuncNameVar(llvm::GlobalValue::LinkageTypes Linkage) { |
| 61 | // Usually, we want to match the function's linkage, but |
| 62 | // available_externally and extern_weak both have the wrong semantics. |
| 63 | if (Linkage == llvm::GlobalValue::ExternalWeakLinkage) |
| 64 | Linkage = llvm::GlobalValue::LinkOnceAnyLinkage; |
| 65 | else if (Linkage == llvm::GlobalValue::AvailableExternallyLinkage) |
| 66 | Linkage = llvm::GlobalValue::LinkOnceODRLinkage; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 67 | |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 68 | auto *Value = |
| 69 | llvm::ConstantDataArray::getString(CGM.getLLVMContext(), FuncName, false); |
| 70 | FuncNameVar = |
| 71 | new llvm::GlobalVariable(CGM.getModule(), Value->getType(), true, Linkage, |
| 72 | Value, "__llvm_profile_name_" + FuncName); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 73 | |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 74 | // Hide the symbol so that we correctly get a copy for each executable. |
| 75 | if (!llvm::GlobalValue::isLocalLinkage(FuncNameVar->getLinkage())) |
| 76 | FuncNameVar->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | namespace { |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 80 | /// \brief Stable hasher for PGO region counters. |
| 81 | /// |
| 82 | /// PGOHash produces a stable hash of a given function's control flow. |
| 83 | /// |
| 84 | /// Changing the output of this hash will invalidate all previously generated |
| 85 | /// profiles -- i.e., don't do it. |
| 86 | /// |
| 87 | /// \note When this hash does eventually change (years?), we still need to |
| 88 | /// support old hashes. We'll need to pull in the version number from the |
| 89 | /// profile data format and use the matching hash function. |
| 90 | class PGOHash { |
| 91 | uint64_t Working; |
| 92 | unsigned Count; |
| 93 | llvm::MD5 MD5; |
| 94 | |
| 95 | static const int NumBitsPerType = 6; |
| 96 | static const unsigned NumTypesPerWord = sizeof(uint64_t) * 8 / NumBitsPerType; |
| 97 | static const unsigned TooBig = 1u << NumBitsPerType; |
| 98 | |
| 99 | public: |
| 100 | /// \brief Hash values for AST nodes. |
| 101 | /// |
| 102 | /// Distinct values for AST nodes that have region counters attached. |
| 103 | /// |
| 104 | /// These values must be stable. All new members must be added at the end, |
| 105 | /// and no members should be removed. Changing the enumeration value for an |
| 106 | /// AST node will affect the hash of every function that contains that node. |
| 107 | enum HashType : unsigned char { |
| 108 | None = 0, |
| 109 | LabelStmt = 1, |
| 110 | WhileStmt, |
| 111 | DoStmt, |
| 112 | ForStmt, |
| 113 | CXXForRangeStmt, |
| 114 | ObjCForCollectionStmt, |
| 115 | SwitchStmt, |
| 116 | CaseStmt, |
| 117 | DefaultStmt, |
| 118 | IfStmt, |
| 119 | CXXTryStmt, |
| 120 | CXXCatchStmt, |
| 121 | ConditionalOperator, |
| 122 | BinaryOperatorLAnd, |
| 123 | BinaryOperatorLOr, |
| 124 | BinaryConditionalOperator, |
| 125 | |
| 126 | // Keep this last. It's for the static assert that follows. |
| 127 | LastHashType |
| 128 | }; |
| 129 | static_assert(LastHashType <= TooBig, "Too many types in HashType"); |
| 130 | |
| 131 | // TODO: When this format changes, take in a version number here, and use the |
| 132 | // old hash calculation for file formats that used the old hash. |
| 133 | PGOHash() : Working(0), Count(0) {} |
| 134 | void combine(HashType Type); |
| 135 | uint64_t finalize(); |
| 136 | }; |
| 137 | const int PGOHash::NumBitsPerType; |
| 138 | const unsigned PGOHash::NumTypesPerWord; |
| 139 | const unsigned PGOHash::TooBig; |
| 140 | |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 141 | /// A RecursiveASTVisitor that fills a map of statements to PGO counters. |
| 142 | struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> { |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 143 | /// The next counter value to assign. |
| 144 | unsigned NextCounter; |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 145 | /// The function hash. |
| 146 | PGOHash Hash; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 147 | /// The map of statements to counters. |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 148 | llvm::DenseMap<const Stmt *, unsigned> &CounterMap; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 149 | |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 150 | MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap) |
| 151 | : NextCounter(0), CounterMap(CounterMap) {} |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 152 | |
Justin Bogner | 191ec63 | 2014-04-11 23:06:35 +0000 | [diff] [blame] | 153 | // Blocks and lambdas are handled as separate functions, so we need not |
| 154 | // traverse them in the parent context. |
| 155 | bool TraverseBlockExpr(BlockExpr *BE) { return true; } |
| 156 | bool TraverseLambdaBody(LambdaExpr *LE) { return true; } |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 157 | bool TraverseCapturedStmt(CapturedStmt *CS) { return true; } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 158 | |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 159 | bool VisitDecl(const Decl *D) { |
| 160 | switch (D->getKind()) { |
| 161 | default: |
| 162 | break; |
| 163 | case Decl::Function: |
| 164 | case Decl::CXXMethod: |
| 165 | case Decl::CXXConstructor: |
| 166 | case Decl::CXXDestructor: |
| 167 | case Decl::CXXConversion: |
| 168 | case Decl::ObjCMethod: |
| 169 | case Decl::Block: |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 170 | case Decl::Captured: |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 171 | CounterMap[D->getBody()] = NextCounter++; |
| 172 | break; |
| 173 | } |
| 174 | return true; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 175 | } |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 176 | |
| 177 | bool VisitStmt(const Stmt *S) { |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 178 | auto Type = getHashType(S); |
| 179 | if (Type == PGOHash::None) |
| 180 | return true; |
| 181 | |
| 182 | CounterMap[S] = NextCounter++; |
| 183 | Hash.combine(Type); |
| 184 | return true; |
| 185 | } |
| 186 | PGOHash::HashType getHashType(const Stmt *S) { |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 187 | switch (S->getStmtClass()) { |
| 188 | default: |
| 189 | break; |
| 190 | case Stmt::LabelStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 191 | return PGOHash::LabelStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 192 | case Stmt::WhileStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 193 | return PGOHash::WhileStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 194 | case Stmt::DoStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 195 | return PGOHash::DoStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 196 | case Stmt::ForStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 197 | return PGOHash::ForStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 198 | case Stmt::CXXForRangeStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 199 | return PGOHash::CXXForRangeStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 200 | case Stmt::ObjCForCollectionStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 201 | return PGOHash::ObjCForCollectionStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 202 | case Stmt::SwitchStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 203 | return PGOHash::SwitchStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 204 | case Stmt::CaseStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 205 | return PGOHash::CaseStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 206 | case Stmt::DefaultStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 207 | return PGOHash::DefaultStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 208 | case Stmt::IfStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 209 | return PGOHash::IfStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 210 | case Stmt::CXXTryStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 211 | return PGOHash::CXXTryStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 212 | case Stmt::CXXCatchStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 213 | return PGOHash::CXXCatchStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 214 | case Stmt::ConditionalOperatorClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 215 | return PGOHash::ConditionalOperator; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 216 | case Stmt::BinaryConditionalOperatorClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 217 | return PGOHash::BinaryConditionalOperator; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 218 | case Stmt::BinaryOperatorClass: { |
| 219 | const BinaryOperator *BO = cast<BinaryOperator>(S); |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 220 | if (BO->getOpcode() == BO_LAnd) |
| 221 | return PGOHash::BinaryOperatorLAnd; |
| 222 | if (BO->getOpcode() == BO_LOr) |
| 223 | return PGOHash::BinaryOperatorLOr; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | } |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 227 | return PGOHash::None; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 228 | } |
| 229 | }; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 230 | |
| 231 | /// A StmtVisitor that propagates the raw counts through the AST and |
| 232 | /// records the count at statements where the value may change. |
| 233 | struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> { |
| 234 | /// PGO state. |
| 235 | CodeGenPGO &PGO; |
| 236 | |
| 237 | /// A flag that is set when the current count should be recorded on the |
| 238 | /// next statement, such as at the exit of a loop. |
| 239 | bool RecordNextStmtCount; |
| 240 | |
| 241 | /// The map of statements to count values. |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 242 | llvm::DenseMap<const Stmt *, uint64_t> &CountMap; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 243 | |
Justin Bogner | fc83c11 | 2014-04-18 21:51:09 +0000 | [diff] [blame] | 244 | /// BreakContinueStack - Keep counts of breaks and continues inside loops. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 245 | struct BreakContinue { |
| 246 | uint64_t BreakCount; |
| 247 | uint64_t ContinueCount; |
| 248 | BreakContinue() : BreakCount(0), ContinueCount(0) {} |
| 249 | }; |
| 250 | SmallVector<BreakContinue, 8> BreakContinueStack; |
| 251 | |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 252 | ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap, |
| 253 | CodeGenPGO &PGO) |
| 254 | : PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {} |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 255 | |
| 256 | void RecordStmtCount(const Stmt *S) { |
| 257 | if (RecordNextStmtCount) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 258 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 259 | RecordNextStmtCount = false; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void VisitStmt(const Stmt *S) { |
| 264 | RecordStmtCount(S); |
| 265 | for (Stmt::const_child_range I = S->children(); I; ++I) { |
| 266 | if (*I) |
| 267 | this->Visit(*I); |
| 268 | } |
| 269 | } |
| 270 | |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 271 | void VisitFunctionDecl(const FunctionDecl *D) { |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 272 | // Counter tracks entry to the function body. |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 273 | RegionCounter Cnt(PGO, D->getBody()); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 274 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 275 | CountMap[D->getBody()] = PGO.getCurrentRegionCount(); |
| 276 | Visit(D->getBody()); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Justin Bogner | 191ec63 | 2014-04-11 23:06:35 +0000 | [diff] [blame] | 279 | // Skip lambda expressions. We visit these as FunctionDecls when we're |
| 280 | // generating them and aren't interested in the body when generating a |
| 281 | // parent context. |
| 282 | void VisitLambdaExpr(const LambdaExpr *LE) {} |
| 283 | |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 284 | void VisitCapturedDecl(const CapturedDecl *D) { |
| 285 | // Counter tracks entry to the capture body. |
| 286 | RegionCounter Cnt(PGO, D->getBody()); |
| 287 | Cnt.beginRegion(); |
| 288 | CountMap[D->getBody()] = PGO.getCurrentRegionCount(); |
| 289 | Visit(D->getBody()); |
| 290 | } |
| 291 | |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 292 | void VisitObjCMethodDecl(const ObjCMethodDecl *D) { |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 293 | // Counter tracks entry to the method body. |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 294 | RegionCounter Cnt(PGO, D->getBody()); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 295 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 296 | CountMap[D->getBody()] = PGO.getCurrentRegionCount(); |
| 297 | Visit(D->getBody()); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 300 | void VisitBlockDecl(const BlockDecl *D) { |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 301 | // Counter tracks entry to the block body. |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 302 | RegionCounter Cnt(PGO, D->getBody()); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 303 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 304 | CountMap[D->getBody()] = PGO.getCurrentRegionCount(); |
| 305 | Visit(D->getBody()); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 308 | void VisitReturnStmt(const ReturnStmt *S) { |
| 309 | RecordStmtCount(S); |
| 310 | if (S->getRetValue()) |
| 311 | Visit(S->getRetValue()); |
| 312 | PGO.setCurrentRegionUnreachable(); |
| 313 | RecordNextStmtCount = true; |
| 314 | } |
| 315 | |
| 316 | void VisitGotoStmt(const GotoStmt *S) { |
| 317 | RecordStmtCount(S); |
| 318 | PGO.setCurrentRegionUnreachable(); |
| 319 | RecordNextStmtCount = true; |
| 320 | } |
| 321 | |
| 322 | void VisitLabelStmt(const LabelStmt *S) { |
| 323 | RecordNextStmtCount = false; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 324 | // Counter tracks the block following the label. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 325 | RegionCounter Cnt(PGO, S); |
| 326 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 327 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 328 | Visit(S->getSubStmt()); |
| 329 | } |
| 330 | |
| 331 | void VisitBreakStmt(const BreakStmt *S) { |
| 332 | RecordStmtCount(S); |
| 333 | assert(!BreakContinueStack.empty() && "break not in a loop or switch!"); |
| 334 | BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount(); |
| 335 | PGO.setCurrentRegionUnreachable(); |
| 336 | RecordNextStmtCount = true; |
| 337 | } |
| 338 | |
| 339 | void VisitContinueStmt(const ContinueStmt *S) { |
| 340 | RecordStmtCount(S); |
| 341 | assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); |
| 342 | BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount(); |
| 343 | PGO.setCurrentRegionUnreachable(); |
| 344 | RecordNextStmtCount = true; |
| 345 | } |
| 346 | |
| 347 | void VisitWhileStmt(const WhileStmt *S) { |
| 348 | RecordStmtCount(S); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 349 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 350 | RegionCounter Cnt(PGO, S); |
| 351 | BreakContinueStack.push_back(BreakContinue()); |
| 352 | // Visit the body region first so the break/continue adjustments can be |
| 353 | // included when visiting the condition. |
| 354 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 355 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 356 | Visit(S->getBody()); |
| 357 | Cnt.adjustForControlFlow(); |
| 358 | |
| 359 | // ...then go back and propagate counts through the condition. The count |
| 360 | // at the start of the condition is the sum of the incoming edges, |
| 361 | // the backedge from the end of the loop body, and the edges from |
| 362 | // continue statements. |
| 363 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 364 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 365 | Cnt.getAdjustedCount() + BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 366 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 367 | Visit(S->getCond()); |
| 368 | Cnt.adjustForControlFlow(); |
| 369 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 370 | RecordNextStmtCount = true; |
| 371 | } |
| 372 | |
| 373 | void VisitDoStmt(const DoStmt *S) { |
| 374 | RecordStmtCount(S); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 375 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 376 | RegionCounter Cnt(PGO, S); |
| 377 | BreakContinueStack.push_back(BreakContinue()); |
| 378 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 379 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 380 | Visit(S->getBody()); |
| 381 | Cnt.adjustForControlFlow(); |
| 382 | |
| 383 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 384 | // The count at the start of the condition is equal to the count at the |
| 385 | // end of the body. The adjusted count does not include either the |
| 386 | // fall-through count coming into the loop or the continue count, so add |
| 387 | // both of those separately. This is coincidentally the same equation as |
| 388 | // with while loops but for different reasons. |
| 389 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 390 | Cnt.getAdjustedCount() + BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 391 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 392 | Visit(S->getCond()); |
| 393 | Cnt.adjustForControlFlow(); |
| 394 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 395 | RecordNextStmtCount = true; |
| 396 | } |
| 397 | |
| 398 | void VisitForStmt(const ForStmt *S) { |
| 399 | RecordStmtCount(S); |
| 400 | if (S->getInit()) |
| 401 | Visit(S->getInit()); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 402 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 403 | RegionCounter Cnt(PGO, S); |
| 404 | BreakContinueStack.push_back(BreakContinue()); |
| 405 | // Visit the body region first. (This is basically the same as a while |
| 406 | // loop; see further comments in VisitWhileStmt.) |
| 407 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 408 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 409 | Visit(S->getBody()); |
| 410 | Cnt.adjustForControlFlow(); |
| 411 | |
| 412 | // The increment is essentially part of the body but it needs to include |
| 413 | // the count for all the continue statements. |
| 414 | if (S->getInc()) { |
| 415 | Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() + |
| 416 | BreakContinueStack.back().ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 417 | CountMap[S->getInc()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 418 | Visit(S->getInc()); |
| 419 | Cnt.adjustForControlFlow(); |
| 420 | } |
| 421 | |
| 422 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 423 | |
| 424 | // ...then go back and propagate counts through the condition. |
| 425 | if (S->getCond()) { |
| 426 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 427 | Cnt.getAdjustedCount() + |
| 428 | BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 429 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 430 | Visit(S->getCond()); |
| 431 | Cnt.adjustForControlFlow(); |
| 432 | } |
| 433 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 434 | RecordNextStmtCount = true; |
| 435 | } |
| 436 | |
| 437 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
| 438 | RecordStmtCount(S); |
| 439 | Visit(S->getRangeStmt()); |
| 440 | Visit(S->getBeginEndStmt()); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 441 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 442 | RegionCounter Cnt(PGO, S); |
| 443 | BreakContinueStack.push_back(BreakContinue()); |
| 444 | // Visit the body region first. (This is basically the same as a while |
| 445 | // loop; see further comments in VisitWhileStmt.) |
| 446 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 447 | CountMap[S->getLoopVarStmt()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 448 | Visit(S->getLoopVarStmt()); |
| 449 | Visit(S->getBody()); |
| 450 | Cnt.adjustForControlFlow(); |
| 451 | |
| 452 | // The increment is essentially part of the body but it needs to include |
| 453 | // the count for all the continue statements. |
| 454 | Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() + |
| 455 | BreakContinueStack.back().ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 456 | CountMap[S->getInc()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 457 | Visit(S->getInc()); |
| 458 | Cnt.adjustForControlFlow(); |
| 459 | |
| 460 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 461 | |
| 462 | // ...then go back and propagate counts through the condition. |
| 463 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 464 | Cnt.getAdjustedCount() + |
| 465 | BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 466 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 467 | Visit(S->getCond()); |
| 468 | Cnt.adjustForControlFlow(); |
| 469 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 470 | RecordNextStmtCount = true; |
| 471 | } |
| 472 | |
| 473 | void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) { |
| 474 | RecordStmtCount(S); |
| 475 | Visit(S->getElement()); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 476 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 477 | RegionCounter Cnt(PGO, S); |
| 478 | BreakContinueStack.push_back(BreakContinue()); |
| 479 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 480 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 481 | Visit(S->getBody()); |
| 482 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 483 | Cnt.adjustForControlFlow(); |
| 484 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 485 | RecordNextStmtCount = true; |
| 486 | } |
| 487 | |
| 488 | void VisitSwitchStmt(const SwitchStmt *S) { |
| 489 | RecordStmtCount(S); |
| 490 | Visit(S->getCond()); |
| 491 | PGO.setCurrentRegionUnreachable(); |
| 492 | BreakContinueStack.push_back(BreakContinue()); |
| 493 | Visit(S->getBody()); |
| 494 | // If the switch is inside a loop, add the continue counts. |
| 495 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 496 | if (!BreakContinueStack.empty()) |
| 497 | BreakContinueStack.back().ContinueCount += BC.ContinueCount; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 498 | // Counter tracks the exit block of the switch. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 499 | RegionCounter ExitCnt(PGO, S); |
| 500 | ExitCnt.beginRegion(); |
| 501 | RecordNextStmtCount = true; |
| 502 | } |
| 503 | |
| 504 | void VisitCaseStmt(const CaseStmt *S) { |
| 505 | RecordNextStmtCount = false; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 506 | // Counter for this particular case. This counts only jumps from the |
| 507 | // switch header and does not include fallthrough from the case before |
| 508 | // this one. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 509 | RegionCounter Cnt(PGO, S); |
| 510 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 511 | CountMap[S] = Cnt.getCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 512 | RecordNextStmtCount = true; |
| 513 | Visit(S->getSubStmt()); |
| 514 | } |
| 515 | |
| 516 | void VisitDefaultStmt(const DefaultStmt *S) { |
| 517 | RecordNextStmtCount = false; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 518 | // Counter for this default case. This does not include fallthrough from |
| 519 | // the previous case. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 520 | RegionCounter Cnt(PGO, S); |
| 521 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 522 | CountMap[S] = Cnt.getCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 523 | RecordNextStmtCount = true; |
| 524 | Visit(S->getSubStmt()); |
| 525 | } |
| 526 | |
| 527 | void VisitIfStmt(const IfStmt *S) { |
| 528 | RecordStmtCount(S); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 529 | // Counter tracks the "then" part of an if statement. The count for |
| 530 | // the "else" part, if it exists, will be calculated from this counter. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 531 | RegionCounter Cnt(PGO, S); |
| 532 | Visit(S->getCond()); |
| 533 | |
| 534 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 535 | CountMap[S->getThen()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 536 | Visit(S->getThen()); |
| 537 | Cnt.adjustForControlFlow(); |
| 538 | |
| 539 | if (S->getElse()) { |
| 540 | Cnt.beginElseRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 541 | CountMap[S->getElse()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 542 | Visit(S->getElse()); |
| 543 | Cnt.adjustForControlFlow(); |
| 544 | } |
| 545 | Cnt.applyAdjustmentsToRegion(0); |
| 546 | RecordNextStmtCount = true; |
| 547 | } |
| 548 | |
| 549 | void VisitCXXTryStmt(const CXXTryStmt *S) { |
| 550 | RecordStmtCount(S); |
| 551 | Visit(S->getTryBlock()); |
| 552 | for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I) |
| 553 | Visit(S->getHandler(I)); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 554 | // Counter tracks the continuation block of the try statement. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 555 | RegionCounter Cnt(PGO, S); |
| 556 | Cnt.beginRegion(); |
| 557 | RecordNextStmtCount = true; |
| 558 | } |
| 559 | |
| 560 | void VisitCXXCatchStmt(const CXXCatchStmt *S) { |
| 561 | RecordNextStmtCount = false; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 562 | // Counter tracks the catch statement's handler block. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 563 | RegionCounter Cnt(PGO, S); |
| 564 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 565 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 566 | Visit(S->getHandlerBlock()); |
| 567 | } |
| 568 | |
Justin Bogner | 53c55d9 | 2014-04-11 06:10:10 +0000 | [diff] [blame] | 569 | void VisitAbstractConditionalOperator( |
| 570 | const AbstractConditionalOperator *E) { |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 571 | RecordStmtCount(E); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 572 | // Counter tracks the "true" part of a conditional operator. The |
| 573 | // count in the "false" part will be calculated from this counter. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 574 | RegionCounter Cnt(PGO, E); |
| 575 | Visit(E->getCond()); |
| 576 | |
| 577 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 578 | CountMap[E->getTrueExpr()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 579 | Visit(E->getTrueExpr()); |
| 580 | Cnt.adjustForControlFlow(); |
| 581 | |
| 582 | Cnt.beginElseRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 583 | CountMap[E->getFalseExpr()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 584 | Visit(E->getFalseExpr()); |
| 585 | Cnt.adjustForControlFlow(); |
| 586 | |
| 587 | Cnt.applyAdjustmentsToRegion(0); |
| 588 | RecordNextStmtCount = true; |
| 589 | } |
| 590 | |
| 591 | void VisitBinLAnd(const BinaryOperator *E) { |
| 592 | RecordStmtCount(E); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 593 | // Counter tracks the right hand side of a logical and operator. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 594 | RegionCounter Cnt(PGO, E); |
| 595 | Visit(E->getLHS()); |
| 596 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 597 | CountMap[E->getRHS()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 598 | Visit(E->getRHS()); |
| 599 | Cnt.adjustForControlFlow(); |
| 600 | Cnt.applyAdjustmentsToRegion(0); |
| 601 | RecordNextStmtCount = true; |
| 602 | } |
| 603 | |
| 604 | void VisitBinLOr(const BinaryOperator *E) { |
| 605 | RecordStmtCount(E); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 606 | // Counter tracks the right hand side of a logical or operator. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 607 | RegionCounter Cnt(PGO, E); |
| 608 | Visit(E->getLHS()); |
| 609 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 610 | CountMap[E->getRHS()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 611 | Visit(E->getRHS()); |
| 612 | Cnt.adjustForControlFlow(); |
| 613 | Cnt.applyAdjustmentsToRegion(0); |
| 614 | RecordNextStmtCount = true; |
| 615 | } |
| 616 | }; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 619 | void PGOHash::combine(HashType Type) { |
| 620 | // Check that we never combine 0 and only have six bits. |
| 621 | assert(Type && "Hash is invalid: unexpected type 0"); |
| 622 | assert(unsigned(Type) < TooBig && "Hash is invalid: too many types"); |
| 623 | |
| 624 | // Pass through MD5 if enough work has built up. |
| 625 | if (Count && Count % NumTypesPerWord == 0) { |
| 626 | using namespace llvm::support; |
| 627 | uint64_t Swapped = endian::byte_swap<uint64_t, little>(Working); |
| 628 | MD5.update(llvm::makeArrayRef((uint8_t *)&Swapped, sizeof(Swapped))); |
| 629 | Working = 0; |
| 630 | } |
| 631 | |
| 632 | // Accumulate the current type. |
| 633 | ++Count; |
| 634 | Working = Working << NumBitsPerType | Type; |
| 635 | } |
| 636 | |
| 637 | uint64_t PGOHash::finalize() { |
| 638 | // Use Working as the hash directly if we never used MD5. |
| 639 | if (Count <= NumTypesPerWord) |
| 640 | // No need to byte swap here, since none of the math was endian-dependent. |
| 641 | // This number will be byte-swapped as required on endianness transitions, |
| 642 | // so we will see the same value on the other side. |
| 643 | return Working; |
| 644 | |
| 645 | // Check for remaining work in Working. |
| 646 | if (Working) |
| 647 | MD5.update(Working); |
| 648 | |
| 649 | // Finalize the MD5 and return the hash. |
| 650 | llvm::MD5::MD5Result Result; |
| 651 | MD5.final(Result); |
| 652 | using namespace llvm::support; |
| 653 | return endian::read<uint64_t, little, unaligned>(Result); |
| 654 | } |
| 655 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 656 | void CodeGenPGO::checkGlobalDecl(GlobalDecl GD) { |
| 657 | // Make sure we only emit coverage mapping for one constructor/destructor. |
| 658 | // Clang emits several functions for the constructor and the destructor of |
| 659 | // a class. Every function is instrumented, but we only want to provide |
| 660 | // coverage for one of them. Because of that we only emit the coverage mapping |
| 661 | // for the base constructor/destructor. |
| 662 | if ((isa<CXXConstructorDecl>(GD.getDecl()) && |
Alex Lorenz | d423674 | 2014-08-11 18:21:34 +0000 | [diff] [blame] | 663 | GD.getCtorType() != Ctor_Base) || |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 664 | (isa<CXXDestructorDecl>(GD.getDecl()) && |
| 665 | GD.getDtorType() != Dtor_Base)) { |
| 666 | SkipCoverageMapping = true; |
| 667 | } |
| 668 | } |
| 669 | |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 670 | void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) { |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 671 | bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate; |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 672 | llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader(); |
| 673 | if (!InstrumentRegions && !PGOReader) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 674 | return; |
Justin Bogner | 3212b18 | 2014-04-25 07:20:05 +0000 | [diff] [blame] | 675 | if (D->isImplicit()) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 676 | return; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 677 | CGM.ClearUnusedCoverageMapping(D); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 678 | setFuncName(Fn); |
Duncan P. N. Exon Smith | 7c41451 | 2014-03-20 22:50:08 +0000 | [diff] [blame] | 679 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 680 | mapRegionCounters(D); |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 681 | if (CGM.getCodeGenOpts().CoverageMapping) |
| 682 | emitCounterRegionMapping(D); |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 683 | if (PGOReader) { |
Justin Bogner | 40b8ba1 | 2014-06-26 01:45:07 +0000 | [diff] [blame] | 684 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 685 | loadRegionCounts(PGOReader, SM.isInMainFile(D->getLocation())); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 686 | computeRegionCounts(D); |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 687 | applyFunctionAttributes(PGOReader, Fn); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 688 | } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | void CodeGenPGO::mapRegionCounters(const Decl *D) { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 692 | RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 693 | MapRegionCounters Walker(*RegionCounterMap); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 694 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 695 | Walker.TraverseDecl(const_cast<FunctionDecl *>(FD)); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 696 | else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 697 | Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD)); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 698 | else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D)) |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 699 | Walker.TraverseDecl(const_cast<BlockDecl *>(BD)); |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 700 | else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D)) |
| 701 | Walker.TraverseDecl(const_cast<CapturedDecl *>(CD)); |
Justin Bogner | 3212b18 | 2014-04-25 07:20:05 +0000 | [diff] [blame] | 702 | assert(Walker.NextCounter > 0 && "no entry counter mapped for decl"); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 703 | NumRegionCounters = Walker.NextCounter; |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 704 | FunctionHash = Walker.Hash.finalize(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 707 | void CodeGenPGO::emitCounterRegionMapping(const Decl *D) { |
| 708 | if (SkipCoverageMapping) |
| 709 | return; |
| 710 | // Don't map the functions inside the system headers |
| 711 | auto Loc = D->getBody()->getLocStart(); |
| 712 | if (CGM.getContext().getSourceManager().isInSystemHeader(Loc)) |
| 713 | return; |
| 714 | |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 715 | std::string CoverageMapping; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 716 | llvm::raw_string_ostream OS(CoverageMapping); |
| 717 | CoverageMappingGen MappingGen(*CGM.getCoverageMapping(), |
| 718 | CGM.getContext().getSourceManager(), |
Justin Bogner | e5ee6c5 | 2014-10-02 16:44:01 +0000 | [diff] [blame] | 719 | CGM.getLangOpts(), RegionCounterMap.get()); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 720 | MappingGen.emitCounterMapping(D, OS); |
| 721 | OS.flush(); |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 722 | |
| 723 | if (CoverageMapping.empty()) |
| 724 | return; |
| 725 | |
| 726 | CGM.getCoverageMapping()->addFunctionMappingRecord( |
| 727 | FuncNameVar, FuncName, FunctionHash, CoverageMapping); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | void |
| 731 | CodeGenPGO::emitEmptyCounterMapping(const Decl *D, StringRef FuncName, |
| 732 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 733 | if (SkipCoverageMapping) |
| 734 | return; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 735 | // Don't map the functions inside the system headers |
| 736 | auto Loc = D->getBody()->getLocStart(); |
| 737 | if (CGM.getContext().getSourceManager().isInSystemHeader(Loc)) |
| 738 | return; |
| 739 | |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 740 | std::string CoverageMapping; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 741 | llvm::raw_string_ostream OS(CoverageMapping); |
| 742 | CoverageMappingGen MappingGen(*CGM.getCoverageMapping(), |
| 743 | CGM.getContext().getSourceManager(), |
| 744 | CGM.getLangOpts()); |
| 745 | MappingGen.emitEmptyMapping(D, OS); |
| 746 | OS.flush(); |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 747 | |
| 748 | if (CoverageMapping.empty()) |
| 749 | return; |
| 750 | |
Justin Bogner | 00270df | 2015-01-22 02:17:23 +0000 | [diff] [blame^] | 751 | setFuncName(FuncName, Linkage); |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 752 | CGM.getCoverageMapping()->addFunctionMappingRecord( |
| 753 | FuncNameVar, FuncName, FunctionHash, CoverageMapping); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 756 | void CodeGenPGO::computeRegionCounts(const Decl *D) { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 757 | StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 758 | ComputeRegionCounts Walker(*StmtCountMap, *this); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 759 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 760 | Walker.VisitFunctionDecl(FD); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 761 | else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) |
| 762 | Walker.VisitObjCMethodDecl(MD); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 763 | else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D)) |
| 764 | Walker.VisitBlockDecl(BD); |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 765 | else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D)) |
| 766 | Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD)); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 769 | void |
| 770 | CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader, |
| 771 | llvm::Function *Fn) { |
Justin Bogner | 4c9c45c | 2014-03-12 18:14:32 +0000 | [diff] [blame] | 772 | if (!haveRegionCounts()) |
| 773 | return; |
| 774 | |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 775 | uint64_t MaxFunctionCount = PGOReader->getMaximumFunctionCount(); |
Justin Bogner | 4c9c45c | 2014-03-12 18:14:32 +0000 | [diff] [blame] | 776 | uint64_t FunctionCount = getRegionCount(0); |
| 777 | if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount)) |
| 778 | // Turn on InlineHint attribute for hot functions. |
| 779 | // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal. |
| 780 | Fn->addFnAttr(llvm::Attribute::InlineHint); |
| 781 | else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount)) |
| 782 | // Turn on Cold attribute for cold functions. |
| 783 | // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal. |
| 784 | Fn->addFnAttr(llvm::Attribute::Cold); |
| 785 | } |
| 786 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 787 | void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) { |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 788 | if (!CGM.getCodeGenOpts().ProfileInstrGenerate || !RegionCounterMap) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 789 | return; |
Justin Bogner | 203f91e | 2015-01-09 01:46:40 +0000 | [diff] [blame] | 790 | if (!Builder.GetInsertPoint()) |
| 791 | return; |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 792 | auto *I8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 793 | Builder.CreateCall4(CGM.getIntrinsic(llvm::Intrinsic::instrprof_increment), |
| 794 | llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy), |
| 795 | Builder.getInt64(FunctionHash), |
| 796 | Builder.getInt32(NumRegionCounters), |
| 797 | Builder.getInt32(Counter)); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Justin Bogner | 40b8ba1 | 2014-06-26 01:45:07 +0000 | [diff] [blame] | 800 | void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader, |
| 801 | bool IsInMainFile) { |
| 802 | CGM.getPGOStats().addVisited(IsInMainFile); |
Justin Bogner | 7f8cf5b | 2014-12-02 22:38:52 +0000 | [diff] [blame] | 803 | RegionCounts.clear(); |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 804 | if (std::error_code EC = |
| 805 | PGOReader->getFunctionCounts(FuncName, FunctionHash, RegionCounts)) { |
Justin Bogner | 9c6818e | 2014-08-01 22:50:16 +0000 | [diff] [blame] | 806 | if (EC == llvm::instrprof_error::unknown_function) |
| 807 | CGM.getPGOStats().addMissing(IsInMainFile); |
| 808 | else if (EC == llvm::instrprof_error::hash_mismatch) |
| 809 | CGM.getPGOStats().addMismatched(IsInMainFile); |
| 810 | else if (EC == llvm::instrprof_error::malformed) |
| 811 | // TODO: Consider a more specific warning for this case. |
| 812 | CGM.getPGOStats().addMismatched(IsInMainFile); |
Justin Bogner | 7f8cf5b | 2014-12-02 22:38:52 +0000 | [diff] [blame] | 813 | RegionCounts.clear(); |
Justin Bogner | e2ef2a0 | 2014-04-15 21:22:35 +0000 | [diff] [blame] | 814 | } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 817 | /// \brief Calculate what to divide by to scale weights. |
| 818 | /// |
| 819 | /// Given the maximum weight, calculate a divisor that will scale all the |
| 820 | /// weights to strictly less than UINT32_MAX. |
| 821 | static uint64_t calculateWeightScale(uint64_t MaxWeight) { |
| 822 | return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1; |
| 823 | } |
| 824 | |
| 825 | /// \brief Scale an individual branch weight (and add 1). |
| 826 | /// |
| 827 | /// Scale a 64-bit weight down to 32-bits using \c Scale. |
| 828 | /// |
| 829 | /// According to Laplace's Rule of Succession, it is better to compute the |
| 830 | /// weight based on the count plus 1, so universally add 1 to the value. |
| 831 | /// |
| 832 | /// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no |
| 833 | /// greater than \c Weight. |
| 834 | static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) { |
| 835 | assert(Scale && "scale by 0?"); |
| 836 | uint64_t Scaled = Weight / Scale + 1; |
| 837 | assert(Scaled <= UINT32_MAX && "overflow 32-bits"); |
| 838 | return Scaled; |
| 839 | } |
| 840 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 841 | llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount, |
| 842 | uint64_t FalseCount) { |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 843 | // Check for empty weights. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 844 | if (!TrueCount && !FalseCount) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 845 | return nullptr; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 846 | |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 847 | // Calculate how to scale down to 32-bits. |
| 848 | uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount)); |
| 849 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 850 | llvm::MDBuilder MDHelper(CGM.getLLVMContext()); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 851 | return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale), |
| 852 | scaleBranchWeight(FalseCount, Scale)); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Bob Wilson | 95a27b0 | 2014-02-17 19:20:59 +0000 | [diff] [blame] | 855 | llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) { |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 856 | // We need at least two elements to create meaningful weights. |
| 857 | if (Weights.size() < 2) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 858 | return nullptr; |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 859 | |
Justin Bogner | f3aefca | 2014-04-04 02:48:51 +0000 | [diff] [blame] | 860 | // Check for empty weights. |
| 861 | uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end()); |
| 862 | if (MaxWeight == 0) |
| 863 | return nullptr; |
| 864 | |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 865 | // Calculate how to scale down to 32-bits. |
Justin Bogner | f3aefca | 2014-04-04 02:48:51 +0000 | [diff] [blame] | 866 | uint64_t Scale = calculateWeightScale(MaxWeight); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 867 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 868 | SmallVector<uint32_t, 16> ScaledWeights; |
| 869 | ScaledWeights.reserve(Weights.size()); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 870 | for (uint64_t W : Weights) |
| 871 | ScaledWeights.push_back(scaleBranchWeight(W, Scale)); |
| 872 | |
| 873 | llvm::MDBuilder MDHelper(CGM.getLLVMContext()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 874 | return MDHelper.createBranchWeights(ScaledWeights); |
| 875 | } |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 876 | |
| 877 | llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond, |
| 878 | RegionCounter &Cnt) { |
| 879 | if (!haveRegionCounts()) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 880 | return nullptr; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 881 | uint64_t LoopCount = Cnt.getCount(); |
| 882 | uint64_t CondCount = 0; |
| 883 | bool Found = getStmtCount(Cond, CondCount); |
| 884 | assert(Found && "missing expected loop condition count"); |
| 885 | (void)Found; |
| 886 | if (CondCount == 0) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 887 | return nullptr; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 888 | return createBranchWeights(LoopCount, |
| 889 | std::max(CondCount, LoopCount) - LoopCount); |
| 890 | } |