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