Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/CodeGen/CodeGenPGO.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CodeGenPGO.cpp - PGO Instrumentation for LLVM CodeGen --*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// Instrumentation-based profile-guided optimization
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "CodeGenPGO.h"
14
#include "CodeGenFunction.h"
15
#include "CoverageMappingGen.h"
16
#include "clang/AST/RecursiveASTVisitor.h"
17
#include "clang/AST/StmtVisitor.h"
18
#include "llvm/IR/Intrinsics.h"
19
#include "llvm/IR/MDBuilder.h"
20
#include "llvm/Support/CommandLine.h"
21
#include "llvm/Support/Endian.h"
22
#include "llvm/Support/FileSystem.h"
23
#include "llvm/Support/MD5.h"
24
#include <optional>
25
26
static llvm::cl::opt<bool>
27
    EnableValueProfiling("enable-value-profiling",
28
                         llvm::cl::desc("Enable value profiling"),
29
                         llvm::cl::Hidden, llvm::cl::init(false));
30
31
using namespace clang;
32
using namespace CodeGen;
33
34
void CodeGenPGO::setFuncName(StringRef Name,
35
0
                             llvm::GlobalValue::LinkageTypes Linkage) {
36
0
  llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
37
0
  FuncName = llvm::getPGOFuncName(
38
0
      Name, Linkage, CGM.getCodeGenOpts().MainFileName,
39
0
      PGOReader ? PGOReader->getVersion() : llvm::IndexedInstrProf::Version);
40
41
  // If we're generating a profile, create a variable for the name.
42
0
  if (CGM.getCodeGenOpts().hasProfileClangInstr())
43
0
    FuncNameVar = llvm::createPGOFuncNameVar(CGM.getModule(), Linkage, FuncName);
44
0
}
45
46
0
void CodeGenPGO::setFuncName(llvm::Function *Fn) {
47
0
  setFuncName(Fn->getName(), Fn->getLinkage());
48
  // Create PGOFuncName meta data.
49
0
  llvm::createPGOFuncNameMetadata(*Fn, FuncName);
50
0
}
51
52
/// The version of the PGO hash algorithm.
53
enum PGOHashVersion : unsigned {
54
  PGO_HASH_V1,
55
  PGO_HASH_V2,
56
  PGO_HASH_V3,
57
58
  // Keep this set to the latest hash version.
59
  PGO_HASH_LATEST = PGO_HASH_V3
60
};
61
62
namespace {
63
/// Stable hasher for PGO region counters.
64
///
65
/// PGOHash produces a stable hash of a given function's control flow.
66
///
67
/// Changing the output of this hash will invalidate all previously generated
68
/// profiles -- i.e., don't do it.
69
///
70
/// \note  When this hash does eventually change (years?), we still need to
71
/// support old hashes.  We'll need to pull in the version number from the
72
/// profile data format and use the matching hash function.
73
class PGOHash {
74
  uint64_t Working;
75
  unsigned Count;
76
  PGOHashVersion HashVersion;
77
  llvm::MD5 MD5;
78
79
  static const int NumBitsPerType = 6;
80
  static const unsigned NumTypesPerWord = sizeof(uint64_t) * 8 / NumBitsPerType;
81
  static const unsigned TooBig = 1u << NumBitsPerType;
82
83
public:
84
  /// Hash values for AST nodes.
85
  ///
86
  /// Distinct values for AST nodes that have region counters attached.
87
  ///
88
  /// These values must be stable.  All new members must be added at the end,
89
  /// and no members should be removed.  Changing the enumeration value for an
90
  /// AST node will affect the hash of every function that contains that node.
91
  enum HashType : unsigned char {
92
    None = 0,
93
    LabelStmt = 1,
94
    WhileStmt,
95
    DoStmt,
96
    ForStmt,
97
    CXXForRangeStmt,
98
    ObjCForCollectionStmt,
99
    SwitchStmt,
100
    CaseStmt,
101
    DefaultStmt,
102
    IfStmt,
103
    CXXTryStmt,
104
    CXXCatchStmt,
105
    ConditionalOperator,
106
    BinaryOperatorLAnd,
107
    BinaryOperatorLOr,
108
    BinaryConditionalOperator,
109
    // The preceding values are available with PGO_HASH_V1.
110
111
    EndOfScope,
112
    IfThenBranch,
113
    IfElseBranch,
114
    GotoStmt,
115
    IndirectGotoStmt,
116
    BreakStmt,
117
    ContinueStmt,
118
    ReturnStmt,
119
    ThrowExpr,
120
    UnaryOperatorLNot,
121
    BinaryOperatorLT,
122
    BinaryOperatorGT,
123
    BinaryOperatorLE,
124
    BinaryOperatorGE,
125
    BinaryOperatorEQ,
126
    BinaryOperatorNE,
127
    // The preceding values are available since PGO_HASH_V2.
128
129
    // Keep this last.  It's for the static assert that follows.
130
    LastHashType
131
  };
132
  static_assert(LastHashType <= TooBig, "Too many types in HashType");
133
134
  PGOHash(PGOHashVersion HashVersion)
135
0
      : Working(0), Count(0), HashVersion(HashVersion) {}
136
  void combine(HashType Type);
137
  uint64_t finalize();
138
0
  PGOHashVersion getHashVersion() const { return HashVersion; }
139
};
140
const int PGOHash::NumBitsPerType;
141
const unsigned PGOHash::NumTypesPerWord;
142
const unsigned PGOHash::TooBig;
143
144
/// Get the PGO hash version used in the given indexed profile.
145
static PGOHashVersion getPGOHashVersion(llvm::IndexedInstrProfReader *PGOReader,
146
0
                                        CodeGenModule &CGM) {
147
0
  if (PGOReader->getVersion() <= 4)
148
0
    return PGO_HASH_V1;
149
0
  if (PGOReader->getVersion() <= 5)
150
0
    return PGO_HASH_V2;
151
0
  return PGO_HASH_V3;
152
0
}
153
154
/// A RecursiveASTVisitor that fills a map of statements to PGO counters.
155
struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
156
  using Base = RecursiveASTVisitor<MapRegionCounters>;
157
158
  /// The next counter value to assign.
159
  unsigned NextCounter;
160
  /// The function hash.
161
  PGOHash Hash;
162
  /// The map of statements to counters.
163
  llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
164
  /// The next bitmap byte index to assign.
165
  unsigned NextMCDCBitmapIdx;
166
  /// The map of statements to MC/DC bitmap coverage objects.
167
  llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap;
168
  /// Maximum number of supported MC/DC conditions in a boolean expression.
169
  unsigned MCDCMaxCond;
170
  /// The profile version.
171
  uint64_t ProfileVersion;
172
  /// Diagnostics Engine used to report warnings.
173
  DiagnosticsEngine &Diag;
174
175
  MapRegionCounters(PGOHashVersion HashVersion, uint64_t ProfileVersion,
176
                    llvm::DenseMap<const Stmt *, unsigned> &CounterMap,
177
                    llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap,
178
                    unsigned MCDCMaxCond, DiagnosticsEngine &Diag)
179
      : NextCounter(0), Hash(HashVersion), CounterMap(CounterMap),
180
        NextMCDCBitmapIdx(0), MCDCBitmapMap(MCDCBitmapMap),
181
0
        MCDCMaxCond(MCDCMaxCond), ProfileVersion(ProfileVersion), Diag(Diag) {}
182
183
  // Blocks and lambdas are handled as separate functions, so we need not
184
  // traverse them in the parent context.
185
0
  bool TraverseBlockExpr(BlockExpr *BE) { return true; }
186
0
  bool TraverseLambdaExpr(LambdaExpr *LE) {
187
    // Traverse the captures, but not the body.
188
0
    for (auto C : zip(LE->captures(), LE->capture_inits()))
189
0
      TraverseLambdaCapture(LE, &std::get<0>(C), std::get<1>(C));
190
0
    return true;
191
0
  }
192
0
  bool TraverseCapturedStmt(CapturedStmt *CS) { return true; }
193
194
0
  bool VisitDecl(const Decl *D) {
195
0
    switch (D->getKind()) {
196
0
    default:
197
0
      break;
198
0
    case Decl::Function:
199
0
    case Decl::CXXMethod:
200
0
    case Decl::CXXConstructor:
201
0
    case Decl::CXXDestructor:
202
0
    case Decl::CXXConversion:
203
0
    case Decl::ObjCMethod:
204
0
    case Decl::Block:
205
0
    case Decl::Captured:
206
0
      CounterMap[D->getBody()] = NextCounter++;
207
0
      break;
208
0
    }
209
0
    return true;
210
0
  }
211
212
  /// If \p S gets a fresh counter, update the counter mappings. Return the
213
  /// V1 hash of \p S.
214
0
  PGOHash::HashType updateCounterMappings(Stmt *S) {
215
0
    auto Type = getHashType(PGO_HASH_V1, S);
216
0
    if (Type != PGOHash::None)
217
0
      CounterMap[S] = NextCounter++;
218
0
    return Type;
219
0
  }
220
221
  /// The following stacks are used with dataTraverseStmtPre() and
222
  /// dataTraverseStmtPost() to track the depth of nested logical operators in a
223
  /// boolean expression in a function.  The ultimate purpose is to keep track
224
  /// of the number of leaf-level conditions in the boolean expression so that a
225
  /// profile bitmap can be allocated based on that number.
226
  ///
227
  /// The stacks are also used to find error cases and notify the user.  A
228
  /// standard logical operator nest for a boolean expression could be in a form
229
  /// similar to this: "x = a && b && c && (d || f)"
230
  unsigned NumCond = 0;
231
  bool SplitNestedLogicalOp = false;
232
  SmallVector<const Stmt *, 16> NonLogOpStack;
233
  SmallVector<const BinaryOperator *, 16> LogOpStack;
234
235
  // Hook: dataTraverseStmtPre() is invoked prior to visiting an AST Stmt node.
236
0
  bool dataTraverseStmtPre(Stmt *S) {
237
    /// If MC/DC is not enabled, MCDCMaxCond will be set to 0. Do nothing.
238
0
    if (MCDCMaxCond == 0)
239
0
      return true;
240
241
    /// At the top of the logical operator nest, reset the number of conditions.
242
0
    if (LogOpStack.empty())
243
0
      NumCond = 0;
244
245
0
    if (const Expr *E = dyn_cast<Expr>(S)) {
246
0
      const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E->IgnoreParens());
247
0
      if (BinOp && BinOp->isLogicalOp()) {
248
        /// Check for "split-nested" logical operators. This happens when a new
249
        /// boolean expression logical-op nest is encountered within an existing
250
        /// boolean expression, separated by a non-logical operator.  For
251
        /// example, in "x = (a && b && c && foo(d && f))", the "d && f" case
252
        /// starts a new boolean expression that is separated from the other
253
        /// conditions by the operator foo(). Split-nested cases are not
254
        /// supported by MC/DC.
255
0
        SplitNestedLogicalOp = SplitNestedLogicalOp || !NonLogOpStack.empty();
256
257
0
        LogOpStack.push_back(BinOp);
258
0
        return true;
259
0
      }
260
0
    }
261
262
    /// Keep track of non-logical operators. These are OK as long as we don't
263
    /// encounter a new logical operator after seeing one.
264
0
    if (!LogOpStack.empty())
265
0
      NonLogOpStack.push_back(S);
266
267
0
    return true;
268
0
  }
269
270
  // Hook: dataTraverseStmtPost() is invoked by the AST visitor after visiting
271
  // an AST Stmt node.  MC/DC will use it to to signal when the top of a
272
  // logical operation (boolean expression) nest is encountered.
273
0
  bool dataTraverseStmtPost(Stmt *S) {
274
    /// If MC/DC is not enabled, MCDCMaxCond will be set to 0. Do nothing.
275
0
    if (MCDCMaxCond == 0)
276
0
      return true;
277
278
0
    if (const Expr *E = dyn_cast<Expr>(S)) {
279
0
      const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E->IgnoreParens());
280
0
      if (BinOp && BinOp->isLogicalOp()) {
281
0
        assert(LogOpStack.back() == BinOp);
282
0
        LogOpStack.pop_back();
283
284
        /// At the top of logical operator nest:
285
0
        if (LogOpStack.empty()) {
286
          /// Was the "split-nested" logical operator case encountered?
287
0
          if (SplitNestedLogicalOp) {
288
0
            unsigned DiagID = Diag.getCustomDiagID(
289
0
                DiagnosticsEngine::Warning,
290
0
                "unsupported MC/DC boolean expression; "
291
0
                "contains an operation with a nested boolean expression. "
292
0
                "Expression will not be covered");
293
0
            Diag.Report(S->getBeginLoc(), DiagID);
294
0
            return false;
295
0
          }
296
297
          /// Was the maximum number of conditions encountered?
298
0
          if (NumCond > MCDCMaxCond) {
299
0
            unsigned DiagID = Diag.getCustomDiagID(
300
0
                DiagnosticsEngine::Warning,
301
0
                "unsupported MC/DC boolean expression; "
302
0
                "number of conditions (%0) exceeds max (%1). "
303
0
                "Expression will not be covered");
304
0
            Diag.Report(S->getBeginLoc(), DiagID) << NumCond << MCDCMaxCond;
305
0
            return false;
306
0
          }
307
308
          // Otherwise, allocate the number of bytes required for the bitmap
309
          // based on the number of conditions. Must be at least 1-byte long.
310
0
          MCDCBitmapMap[BinOp] = NextMCDCBitmapIdx;
311
0
          unsigned SizeInBits = std::max<unsigned>(1L << NumCond, CHAR_BIT);
312
0
          NextMCDCBitmapIdx += SizeInBits / CHAR_BIT;
313
0
        }
314
0
        return true;
315
0
      }
316
0
    }
317
318
0
    if (!LogOpStack.empty())
319
0
      NonLogOpStack.pop_back();
320
321
0
    return true;
322
0
  }
323
324
  /// The RHS of all logical operators gets a fresh counter in order to count
325
  /// how many times the RHS evaluates to true or false, depending on the
326
  /// semantics of the operator. This is only valid for ">= v7" of the profile
327
  /// version so that we facilitate backward compatibility. In addition, in
328
  /// order to use MC/DC, count the number of total LHS and RHS conditions.
329
0
  bool VisitBinaryOperator(BinaryOperator *S) {
330
0
    if (S->isLogicalOp()) {
331
0
      if (CodeGenFunction::isInstrumentedCondition(S->getLHS()))
332
0
        NumCond++;
333
334
0
      if (CodeGenFunction::isInstrumentedCondition(S->getRHS())) {
335
0
        if (ProfileVersion >= llvm::IndexedInstrProf::Version7)
336
0
          CounterMap[S->getRHS()] = NextCounter++;
337
338
0
        NumCond++;
339
0
      }
340
0
    }
341
0
    return Base::VisitBinaryOperator(S);
342
0
  }
343
344
  /// Include \p S in the function hash.
345
0
  bool VisitStmt(Stmt *S) {
346
0
    auto Type = updateCounterMappings(S);
347
0
    if (Hash.getHashVersion() != PGO_HASH_V1)
348
0
      Type = getHashType(Hash.getHashVersion(), S);
349
0
    if (Type != PGOHash::None)
350
0
      Hash.combine(Type);
351
0
    return true;
352
0
  }
353
354
0
  bool TraverseIfStmt(IfStmt *If) {
355
    // If we used the V1 hash, use the default traversal.
356
0
    if (Hash.getHashVersion() == PGO_HASH_V1)
357
0
      return Base::TraverseIfStmt(If);
358
359
    // Otherwise, keep track of which branch we're in while traversing.
360
0
    VisitStmt(If);
361
0
    for (Stmt *CS : If->children()) {
362
0
      if (!CS)
363
0
        continue;
364
0
      if (CS == If->getThen())
365
0
        Hash.combine(PGOHash::IfThenBranch);
366
0
      else if (CS == If->getElse())
367
0
        Hash.combine(PGOHash::IfElseBranch);
368
0
      TraverseStmt(CS);
369
0
    }
370
0
    Hash.combine(PGOHash::EndOfScope);
371
0
    return true;
372
0
  }
373
374
// If the statement type \p N is nestable, and its nesting impacts profile
375
// stability, define a custom traversal which tracks the end of the statement
376
// in the hash (provided we're not using the V1 hash).
377
#define DEFINE_NESTABLE_TRAVERSAL(N)                                           \
378
0
  bool Traverse##N(N *S) {                                                     \
379
0
    Base::Traverse##N(S);                                                      \
380
0
    if (Hash.getHashVersion() != PGO_HASH_V1)                                  \
381
0
      Hash.combine(PGOHash::EndOfScope);                                       \
382
0
    return true;                                                               \
383
0
  }
Unexecuted instantiation: CodeGenPGO.cpp:(anonymous namespace)::MapRegionCounters::TraverseWhileStmt(clang::WhileStmt*)
Unexecuted instantiation: CodeGenPGO.cpp:(anonymous namespace)::MapRegionCounters::TraverseObjCForCollectionStmt(clang::ObjCForCollectionStmt*)
Unexecuted instantiation: CodeGenPGO.cpp:(anonymous namespace)::MapRegionCounters::TraverseForStmt(clang::ForStmt*)
Unexecuted instantiation: CodeGenPGO.cpp:(anonymous namespace)::MapRegionCounters::TraverseDoStmt(clang::DoStmt*)
Unexecuted instantiation: CodeGenPGO.cpp:(anonymous namespace)::MapRegionCounters::TraverseCXXTryStmt(clang::CXXTryStmt*)
Unexecuted instantiation: CodeGenPGO.cpp:(anonymous namespace)::MapRegionCounters::TraverseCXXForRangeStmt(clang::CXXForRangeStmt*)
Unexecuted instantiation: CodeGenPGO.cpp:(anonymous namespace)::MapRegionCounters::TraverseCXXCatchStmt(clang::CXXCatchStmt*)
384
385
  DEFINE_NESTABLE_TRAVERSAL(WhileStmt)
386
  DEFINE_NESTABLE_TRAVERSAL(DoStmt)
387
  DEFINE_NESTABLE_TRAVERSAL(ForStmt)
388
  DEFINE_NESTABLE_TRAVERSAL(CXXForRangeStmt)
389
  DEFINE_NESTABLE_TRAVERSAL(ObjCForCollectionStmt)
390
  DEFINE_NESTABLE_TRAVERSAL(CXXTryStmt)
391
  DEFINE_NESTABLE_TRAVERSAL(CXXCatchStmt)
392
393
  /// Get version \p HashVersion of the PGO hash for \p S.
394
0
  PGOHash::HashType getHashType(PGOHashVersion HashVersion, const Stmt *S) {
395
0
    switch (S->getStmtClass()) {
396
0
    default:
397
0
      break;
398
0
    case Stmt::LabelStmtClass:
399
0
      return PGOHash::LabelStmt;
400
0
    case Stmt::WhileStmtClass:
401
0
      return PGOHash::WhileStmt;
402
0
    case Stmt::DoStmtClass:
403
0
      return PGOHash::DoStmt;
404
0
    case Stmt::ForStmtClass:
405
0
      return PGOHash::ForStmt;
406
0
    case Stmt::CXXForRangeStmtClass:
407
0
      return PGOHash::CXXForRangeStmt;
408
0
    case Stmt::ObjCForCollectionStmtClass:
409
0
      return PGOHash::ObjCForCollectionStmt;
410
0
    case Stmt::SwitchStmtClass:
411
0
      return PGOHash::SwitchStmt;
412
0
    case Stmt::CaseStmtClass:
413
0
      return PGOHash::CaseStmt;
414
0
    case Stmt::DefaultStmtClass:
415
0
      return PGOHash::DefaultStmt;
416
0
    case Stmt::IfStmtClass:
417
0
      return PGOHash::IfStmt;
418
0
    case Stmt::CXXTryStmtClass:
419
0
      return PGOHash::CXXTryStmt;
420
0
    case Stmt::CXXCatchStmtClass:
421
0
      return PGOHash::CXXCatchStmt;
422
0
    case Stmt::ConditionalOperatorClass:
423
0
      return PGOHash::ConditionalOperator;
424
0
    case Stmt::BinaryConditionalOperatorClass:
425
0
      return PGOHash::BinaryConditionalOperator;
426
0
    case Stmt::BinaryOperatorClass: {
427
0
      const BinaryOperator *BO = cast<BinaryOperator>(S);
428
0
      if (BO->getOpcode() == BO_LAnd)
429
0
        return PGOHash::BinaryOperatorLAnd;
430
0
      if (BO->getOpcode() == BO_LOr)
431
0
        return PGOHash::BinaryOperatorLOr;
432
0
      if (HashVersion >= PGO_HASH_V2) {
433
0
        switch (BO->getOpcode()) {
434
0
        default:
435
0
          break;
436
0
        case BO_LT:
437
0
          return PGOHash::BinaryOperatorLT;
438
0
        case BO_GT:
439
0
          return PGOHash::BinaryOperatorGT;
440
0
        case BO_LE:
441
0
          return PGOHash::BinaryOperatorLE;
442
0
        case BO_GE:
443
0
          return PGOHash::BinaryOperatorGE;
444
0
        case BO_EQ:
445
0
          return PGOHash::BinaryOperatorEQ;
446
0
        case BO_NE:
447
0
          return PGOHash::BinaryOperatorNE;
448
0
        }
449
0
      }
450
0
      break;
451
0
    }
452
0
    }
453
454
0
    if (HashVersion >= PGO_HASH_V2) {
455
0
      switch (S->getStmtClass()) {
456
0
      default:
457
0
        break;
458
0
      case Stmt::GotoStmtClass:
459
0
        return PGOHash::GotoStmt;
460
0
      case Stmt::IndirectGotoStmtClass:
461
0
        return PGOHash::IndirectGotoStmt;
462
0
      case Stmt::BreakStmtClass:
463
0
        return PGOHash::BreakStmt;
464
0
      case Stmt::ContinueStmtClass:
465
0
        return PGOHash::ContinueStmt;
466
0
      case Stmt::ReturnStmtClass:
467
0
        return PGOHash::ReturnStmt;
468
0
      case Stmt::CXXThrowExprClass:
469
0
        return PGOHash::ThrowExpr;
470
0
      case Stmt::UnaryOperatorClass: {
471
0
        const UnaryOperator *UO = cast<UnaryOperator>(S);
472
0
        if (UO->getOpcode() == UO_LNot)
473
0
          return PGOHash::UnaryOperatorLNot;
474
0
        break;
475
0
      }
476
0
      }
477
0
    }
478
479
0
    return PGOHash::None;
480
0
  }
481
};
482
483
/// A StmtVisitor that propagates the raw counts through the AST and
484
/// records the count at statements where the value may change.
485
struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> {
486
  /// PGO state.
487
  CodeGenPGO &PGO;
488
489
  /// A flag that is set when the current count should be recorded on the
490
  /// next statement, such as at the exit of a loop.
491
  bool RecordNextStmtCount;
492
493
  /// The count at the current location in the traversal.
494
  uint64_t CurrentCount;
495
496
  /// The map of statements to count values.
497
  llvm::DenseMap<const Stmt *, uint64_t> &CountMap;
498
499
  /// BreakContinueStack - Keep counts of breaks and continues inside loops.
500
  struct BreakContinue {
501
    uint64_t BreakCount = 0;
502
    uint64_t ContinueCount = 0;
503
0
    BreakContinue() = default;
504
  };
505
  SmallVector<BreakContinue, 8> BreakContinueStack;
506
507
  ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap,
508
                      CodeGenPGO &PGO)
509
0
      : PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {}
510
511
0
  void RecordStmtCount(const Stmt *S) {
512
0
    if (RecordNextStmtCount) {
513
0
      CountMap[S] = CurrentCount;
514
0
      RecordNextStmtCount = false;
515
0
    }
516
0
  }
517
518
  /// Set and return the current count.
519
0
  uint64_t setCount(uint64_t Count) {
520
0
    CurrentCount = Count;
521
0
    return Count;
522
0
  }
523
524
0
  void VisitStmt(const Stmt *S) {
525
0
    RecordStmtCount(S);
526
0
    for (const Stmt *Child : S->children())
527
0
      if (Child)
528
0
        this->Visit(Child);
529
0
  }
530
531
0
  void VisitFunctionDecl(const FunctionDecl *D) {
532
    // Counter tracks entry to the function body.
533
0
    uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
534
0
    CountMap[D->getBody()] = BodyCount;
535
0
    Visit(D->getBody());
536
0
  }
537
538
  // Skip lambda expressions. We visit these as FunctionDecls when we're
539
  // generating them and aren't interested in the body when generating a
540
  // parent context.
541
0
  void VisitLambdaExpr(const LambdaExpr *LE) {}
542
543
0
  void VisitCapturedDecl(const CapturedDecl *D) {
544
    // Counter tracks entry to the capture body.
545
0
    uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
546
0
    CountMap[D->getBody()] = BodyCount;
547
0
    Visit(D->getBody());
548
0
  }
549
550
0
  void VisitObjCMethodDecl(const ObjCMethodDecl *D) {
551
    // Counter tracks entry to the method body.
552
0
    uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
553
0
    CountMap[D->getBody()] = BodyCount;
554
0
    Visit(D->getBody());
555
0
  }
556
557
0
  void VisitBlockDecl(const BlockDecl *D) {
558
    // Counter tracks entry to the block body.
559
0
    uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
560
0
    CountMap[D->getBody()] = BodyCount;
561
0
    Visit(D->getBody());
562
0
  }
563
564
0
  void VisitReturnStmt(const ReturnStmt *S) {
565
0
    RecordStmtCount(S);
566
0
    if (S->getRetValue())
567
0
      Visit(S->getRetValue());
568
0
    CurrentCount = 0;
569
0
    RecordNextStmtCount = true;
570
0
  }
571
572
0
  void VisitCXXThrowExpr(const CXXThrowExpr *E) {
573
0
    RecordStmtCount(E);
574
0
    if (E->getSubExpr())
575
0
      Visit(E->getSubExpr());
576
0
    CurrentCount = 0;
577
0
    RecordNextStmtCount = true;
578
0
  }
579
580
0
  void VisitGotoStmt(const GotoStmt *S) {
581
0
    RecordStmtCount(S);
582
0
    CurrentCount = 0;
583
0
    RecordNextStmtCount = true;
584
0
  }
585
586
0
  void VisitLabelStmt(const LabelStmt *S) {
587
0
    RecordNextStmtCount = false;
588
    // Counter tracks the block following the label.
589
0
    uint64_t BlockCount = setCount(PGO.getRegionCount(S));
590
0
    CountMap[S] = BlockCount;
591
0
    Visit(S->getSubStmt());
592
0
  }
593
594
0
  void VisitBreakStmt(const BreakStmt *S) {
595
0
    RecordStmtCount(S);
596
0
    assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
597
0
    BreakContinueStack.back().BreakCount += CurrentCount;
598
0
    CurrentCount = 0;
599
0
    RecordNextStmtCount = true;
600
0
  }
601
602
0
  void VisitContinueStmt(const ContinueStmt *S) {
603
0
    RecordStmtCount(S);
604
0
    assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
605
0
    BreakContinueStack.back().ContinueCount += CurrentCount;
606
0
    CurrentCount = 0;
607
0
    RecordNextStmtCount = true;
608
0
  }
609
610
0
  void VisitWhileStmt(const WhileStmt *S) {
611
0
    RecordStmtCount(S);
612
0
    uint64_t ParentCount = CurrentCount;
613
614
0
    BreakContinueStack.push_back(BreakContinue());
615
    // Visit the body region first so the break/continue adjustments can be
616
    // included when visiting the condition.
617
0
    uint64_t BodyCount = setCount(PGO.getRegionCount(S));
618
0
    CountMap[S->getBody()] = CurrentCount;
619
0
    Visit(S->getBody());
620
0
    uint64_t BackedgeCount = CurrentCount;
621
622
    // ...then go back and propagate counts through the condition. The count
623
    // at the start of the condition is the sum of the incoming edges,
624
    // the backedge from the end of the loop body, and the edges from
625
    // continue statements.
626
0
    BreakContinue BC = BreakContinueStack.pop_back_val();
627
0
    uint64_t CondCount =
628
0
        setCount(ParentCount + BackedgeCount + BC.ContinueCount);
629
0
    CountMap[S->getCond()] = CondCount;
630
0
    Visit(S->getCond());
631
0
    setCount(BC.BreakCount + CondCount - BodyCount);
632
0
    RecordNextStmtCount = true;
633
0
  }
634
635
0
  void VisitDoStmt(const DoStmt *S) {
636
0
    RecordStmtCount(S);
637
0
    uint64_t LoopCount = PGO.getRegionCount(S);
638
639
0
    BreakContinueStack.push_back(BreakContinue());
640
    // The count doesn't include the fallthrough from the parent scope. Add it.
641
0
    uint64_t BodyCount = setCount(LoopCount + CurrentCount);
642
0
    CountMap[S->getBody()] = BodyCount;
643
0
    Visit(S->getBody());
644
0
    uint64_t BackedgeCount = CurrentCount;
645
646
0
    BreakContinue BC = BreakContinueStack.pop_back_val();
647
    // The count at the start of the condition is equal to the count at the
648
    // end of the body, plus any continues.
649
0
    uint64_t CondCount = setCount(BackedgeCount + BC.ContinueCount);
650
0
    CountMap[S->getCond()] = CondCount;
651
0
    Visit(S->getCond());
652
0
    setCount(BC.BreakCount + CondCount - LoopCount);
653
0
    RecordNextStmtCount = true;
654
0
  }
655
656
0
  void VisitForStmt(const ForStmt *S) {
657
0
    RecordStmtCount(S);
658
0
    if (S->getInit())
659
0
      Visit(S->getInit());
660
661
0
    uint64_t ParentCount = CurrentCount;
662
663
0
    BreakContinueStack.push_back(BreakContinue());
664
    // Visit the body region first. (This is basically the same as a while
665
    // loop; see further comments in VisitWhileStmt.)
666
0
    uint64_t BodyCount = setCount(PGO.getRegionCount(S));
667
0
    CountMap[S->getBody()] = BodyCount;
668
0
    Visit(S->getBody());
669
0
    uint64_t BackedgeCount = CurrentCount;
670
0
    BreakContinue BC = BreakContinueStack.pop_back_val();
671
672
    // The increment is essentially part of the body but it needs to include
673
    // the count for all the continue statements.
674
0
    if (S->getInc()) {
675
0
      uint64_t IncCount = setCount(BackedgeCount + BC.ContinueCount);
676
0
      CountMap[S->getInc()] = IncCount;
677
0
      Visit(S->getInc());
678
0
    }
679
680
    // ...then go back and propagate counts through the condition.
681
0
    uint64_t CondCount =
682
0
        setCount(ParentCount + BackedgeCount + BC.ContinueCount);
683
0
    if (S->getCond()) {
684
0
      CountMap[S->getCond()] = CondCount;
685
0
      Visit(S->getCond());
686
0
    }
687
0
    setCount(BC.BreakCount + CondCount - BodyCount);
688
0
    RecordNextStmtCount = true;
689
0
  }
690
691
0
  void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
692
0
    RecordStmtCount(S);
693
0
    if (S->getInit())
694
0
      Visit(S->getInit());
695
0
    Visit(S->getLoopVarStmt());
696
0
    Visit(S->getRangeStmt());
697
0
    Visit(S->getBeginStmt());
698
0
    Visit(S->getEndStmt());
699
700
0
    uint64_t ParentCount = CurrentCount;
701
0
    BreakContinueStack.push_back(BreakContinue());
702
    // Visit the body region first. (This is basically the same as a while
703
    // loop; see further comments in VisitWhileStmt.)
704
0
    uint64_t BodyCount = setCount(PGO.getRegionCount(S));
705
0
    CountMap[S->getBody()] = BodyCount;
706
0
    Visit(S->getBody());
707
0
    uint64_t BackedgeCount = CurrentCount;
708
0
    BreakContinue BC = BreakContinueStack.pop_back_val();
709
710
    // The increment is essentially part of the body but it needs to include
711
    // the count for all the continue statements.
712
0
    uint64_t IncCount = setCount(BackedgeCount + BC.ContinueCount);
713
0
    CountMap[S->getInc()] = IncCount;
714
0
    Visit(S->getInc());
715
716
    // ...then go back and propagate counts through the condition.
717
0
    uint64_t CondCount =
718
0
        setCount(ParentCount + BackedgeCount + BC.ContinueCount);
719
0
    CountMap[S->getCond()] = CondCount;
720
0
    Visit(S->getCond());
721
0
    setCount(BC.BreakCount + CondCount - BodyCount);
722
0
    RecordNextStmtCount = true;
723
0
  }
724
725
0
  void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
726
0
    RecordStmtCount(S);
727
0
    Visit(S->getElement());
728
0
    uint64_t ParentCount = CurrentCount;
729
0
    BreakContinueStack.push_back(BreakContinue());
730
    // Counter tracks the body of the loop.
731
0
    uint64_t BodyCount = setCount(PGO.getRegionCount(S));
732
0
    CountMap[S->getBody()] = BodyCount;
733
0
    Visit(S->getBody());
734
0
    uint64_t BackedgeCount = CurrentCount;
735
0
    BreakContinue BC = BreakContinueStack.pop_back_val();
736
737
0
    setCount(BC.BreakCount + ParentCount + BackedgeCount + BC.ContinueCount -
738
0
             BodyCount);
739
0
    RecordNextStmtCount = true;
740
0
  }
741
742
0
  void VisitSwitchStmt(const SwitchStmt *S) {
743
0
    RecordStmtCount(S);
744
0
    if (S->getInit())
745
0
      Visit(S->getInit());
746
0
    Visit(S->getCond());
747
0
    CurrentCount = 0;
748
0
    BreakContinueStack.push_back(BreakContinue());
749
0
    Visit(S->getBody());
750
    // If the switch is inside a loop, add the continue counts.
751
0
    BreakContinue BC = BreakContinueStack.pop_back_val();
752
0
    if (!BreakContinueStack.empty())
753
0
      BreakContinueStack.back().ContinueCount += BC.ContinueCount;
754
    // Counter tracks the exit block of the switch.
755
0
    setCount(PGO.getRegionCount(S));
756
0
    RecordNextStmtCount = true;
757
0
  }
758
759
0
  void VisitSwitchCase(const SwitchCase *S) {
760
0
    RecordNextStmtCount = false;
761
    // Counter for this particular case. This counts only jumps from the
762
    // switch header and does not include fallthrough from the case before
763
    // this one.
764
0
    uint64_t CaseCount = PGO.getRegionCount(S);
765
0
    setCount(CurrentCount + CaseCount);
766
    // We need the count without fallthrough in the mapping, so it's more useful
767
    // for branch probabilities.
768
0
    CountMap[S] = CaseCount;
769
0
    RecordNextStmtCount = true;
770
0
    Visit(S->getSubStmt());
771
0
  }
772
773
0
  void VisitIfStmt(const IfStmt *S) {
774
0
    RecordStmtCount(S);
775
776
0
    if (S->isConsteval()) {
777
0
      const Stmt *Stm = S->isNegatedConsteval() ? S->getThen() : S->getElse();
778
0
      if (Stm)
779
0
        Visit(Stm);
780
0
      return;
781
0
    }
782
783
0
    uint64_t ParentCount = CurrentCount;
784
0
    if (S->getInit())
785
0
      Visit(S->getInit());
786
0
    Visit(S->getCond());
787
788
    // Counter tracks the "then" part of an if statement. The count for
789
    // the "else" part, if it exists, will be calculated from this counter.
790
0
    uint64_t ThenCount = setCount(PGO.getRegionCount(S));
791
0
    CountMap[S->getThen()] = ThenCount;
792
0
    Visit(S->getThen());
793
0
    uint64_t OutCount = CurrentCount;
794
795
0
    uint64_t ElseCount = ParentCount - ThenCount;
796
0
    if (S->getElse()) {
797
0
      setCount(ElseCount);
798
0
      CountMap[S->getElse()] = ElseCount;
799
0
      Visit(S->getElse());
800
0
      OutCount += CurrentCount;
801
0
    } else
802
0
      OutCount += ElseCount;
803
0
    setCount(OutCount);
804
0
    RecordNextStmtCount = true;
805
0
  }
806
807
0
  void VisitCXXTryStmt(const CXXTryStmt *S) {
808
0
    RecordStmtCount(S);
809
0
    Visit(S->getTryBlock());
810
0
    for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
811
0
      Visit(S->getHandler(I));
812
    // Counter tracks the continuation block of the try statement.
813
0
    setCount(PGO.getRegionCount(S));
814
0
    RecordNextStmtCount = true;
815
0
  }
816
817
0
  void VisitCXXCatchStmt(const CXXCatchStmt *S) {
818
0
    RecordNextStmtCount = false;
819
    // Counter tracks the catch statement's handler block.
820
0
    uint64_t CatchCount = setCount(PGO.getRegionCount(S));
821
0
    CountMap[S] = CatchCount;
822
0
    Visit(S->getHandlerBlock());
823
0
  }
824
825
0
  void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
826
0
    RecordStmtCount(E);
827
0
    uint64_t ParentCount = CurrentCount;
828
0
    Visit(E->getCond());
829
830
    // Counter tracks the "true" part of a conditional operator. The
831
    // count in the "false" part will be calculated from this counter.
832
0
    uint64_t TrueCount = setCount(PGO.getRegionCount(E));
833
0
    CountMap[E->getTrueExpr()] = TrueCount;
834
0
    Visit(E->getTrueExpr());
835
0
    uint64_t OutCount = CurrentCount;
836
837
0
    uint64_t FalseCount = setCount(ParentCount - TrueCount);
838
0
    CountMap[E->getFalseExpr()] = FalseCount;
839
0
    Visit(E->getFalseExpr());
840
0
    OutCount += CurrentCount;
841
842
0
    setCount(OutCount);
843
0
    RecordNextStmtCount = true;
844
0
  }
845
846
0
  void VisitBinLAnd(const BinaryOperator *E) {
847
0
    RecordStmtCount(E);
848
0
    uint64_t ParentCount = CurrentCount;
849
0
    Visit(E->getLHS());
850
    // Counter tracks the right hand side of a logical and operator.
851
0
    uint64_t RHSCount = setCount(PGO.getRegionCount(E));
852
0
    CountMap[E->getRHS()] = RHSCount;
853
0
    Visit(E->getRHS());
854
0
    setCount(ParentCount + RHSCount - CurrentCount);
855
0
    RecordNextStmtCount = true;
856
0
  }
857
858
0
  void VisitBinLOr(const BinaryOperator *E) {
859
0
    RecordStmtCount(E);
860
0
    uint64_t ParentCount = CurrentCount;
861
0
    Visit(E->getLHS());
862
    // Counter tracks the right hand side of a logical or operator.
863
0
    uint64_t RHSCount = setCount(PGO.getRegionCount(E));
864
0
    CountMap[E->getRHS()] = RHSCount;
865
0
    Visit(E->getRHS());
866
0
    setCount(ParentCount + RHSCount - CurrentCount);
867
0
    RecordNextStmtCount = true;
868
0
  }
869
};
870
} // end anonymous namespace
871
872
0
void PGOHash::combine(HashType Type) {
873
  // Check that we never combine 0 and only have six bits.
874
0
  assert(Type && "Hash is invalid: unexpected type 0");
875
0
  assert(unsigned(Type) < TooBig && "Hash is invalid: too many types");
876
877
  // Pass through MD5 if enough work has built up.
878
0
  if (Count && Count % NumTypesPerWord == 0) {
879
0
    using namespace llvm::support;
880
0
    uint64_t Swapped =
881
0
        endian::byte_swap<uint64_t, llvm::endianness::little>(Working);
882
0
    MD5.update(llvm::ArrayRef((uint8_t *)&Swapped, sizeof(Swapped)));
883
0
    Working = 0;
884
0
  }
885
886
  // Accumulate the current type.
887
0
  ++Count;
888
0
  Working = Working << NumBitsPerType | Type;
889
0
}
890
891
0
uint64_t PGOHash::finalize() {
892
  // Use Working as the hash directly if we never used MD5.
893
0
  if (Count <= NumTypesPerWord)
894
    // No need to byte swap here, since none of the math was endian-dependent.
895
    // This number will be byte-swapped as required on endianness transitions,
896
    // so we will see the same value on the other side.
897
0
    return Working;
898
899
  // Check for remaining work in Working.
900
0
  if (Working) {
901
    // Keep the buggy behavior from v1 and v2 for backward-compatibility. This
902
    // is buggy because it converts a uint64_t into an array of uint8_t.
903
0
    if (HashVersion < PGO_HASH_V3) {
904
0
      MD5.update({(uint8_t)Working});
905
0
    } else {
906
0
      using namespace llvm::support;
907
0
      uint64_t Swapped =
908
0
          endian::byte_swap<uint64_t, llvm::endianness::little>(Working);
909
0
      MD5.update(llvm::ArrayRef((uint8_t *)&Swapped, sizeof(Swapped)));
910
0
    }
911
0
  }
912
913
  // Finalize the MD5 and return the hash.
914
0
  llvm::MD5::MD5Result Result;
915
0
  MD5.final(Result);
916
0
  return Result.low();
917
0
}
918
919
0
void CodeGenPGO::assignRegionCounters(GlobalDecl GD, llvm::Function *Fn) {
920
0
  const Decl *D = GD.getDecl();
921
0
  if (!D->hasBody())
922
0
    return;
923
924
  // Skip CUDA/HIP kernel launch stub functions.
925
0
  if (CGM.getLangOpts().CUDA && !CGM.getLangOpts().CUDAIsDevice &&
926
0
      D->hasAttr<CUDAGlobalAttr>())
927
0
    return;
928
929
0
  bool InstrumentRegions = CGM.getCodeGenOpts().hasProfileClangInstr();
930
0
  llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
931
0
  if (!InstrumentRegions && !PGOReader)
932
0
    return;
933
0
  if (D->isImplicit())
934
0
    return;
935
  // Constructors and destructors may be represented by several functions in IR.
936
  // If so, instrument only base variant, others are implemented by delegation
937
  // to the base one, it would be counted twice otherwise.
938
0
  if (CGM.getTarget().getCXXABI().hasConstructorVariants()) {
939
0
    if (const auto *CCD = dyn_cast<CXXConstructorDecl>(D))
940
0
      if (GD.getCtorType() != Ctor_Base &&
941
0
          CodeGenFunction::IsConstructorDelegationValid(CCD))
942
0
        return;
943
0
  }
944
0
  if (isa<CXXDestructorDecl>(D) && GD.getDtorType() != Dtor_Base)
945
0
    return;
946
947
0
  CGM.ClearUnusedCoverageMapping(D);
948
0
  if (Fn->hasFnAttribute(llvm::Attribute::NoProfile))
949
0
    return;
950
0
  if (Fn->hasFnAttribute(llvm::Attribute::SkipProfile))
951
0
    return;
952
953
0
  setFuncName(Fn);
954
955
0
  mapRegionCounters(D);
956
0
  if (CGM.getCodeGenOpts().CoverageMapping)
957
0
    emitCounterRegionMapping(D);
958
0
  if (PGOReader) {
959
0
    SourceManager &SM = CGM.getContext().getSourceManager();
960
0
    loadRegionCounts(PGOReader, SM.isInMainFile(D->getLocation()));
961
0
    computeRegionCounts(D);
962
0
    applyFunctionAttributes(PGOReader, Fn);
963
0
  }
964
0
}
965
966
0
void CodeGenPGO::mapRegionCounters(const Decl *D) {
967
  // Use the latest hash version when inserting instrumentation, but use the
968
  // version in the indexed profile if we're reading PGO data.
969
0
  PGOHashVersion HashVersion = PGO_HASH_LATEST;
970
0
  uint64_t ProfileVersion = llvm::IndexedInstrProf::Version;
971
0
  if (auto *PGOReader = CGM.getPGOReader()) {
972
0
    HashVersion = getPGOHashVersion(PGOReader, CGM);
973
0
    ProfileVersion = PGOReader->getVersion();
974
0
  }
975
976
  // If MC/DC is enabled, set the MaxConditions to a preset value. Otherwise,
977
  // set it to zero. This value impacts the number of conditions accepted in a
978
  // given boolean expression, which impacts the size of the bitmap used to
979
  // track test vector execution for that boolean expression.  Because the
980
  // bitmap scales exponentially (2^n) based on the number of conditions seen,
981
  // the maximum value is hard-coded at 6 conditions, which is more than enough
982
  // for most embedded applications. Setting a maximum value prevents the
983
  // bitmap footprint from growing too large without the user's knowledge. In
984
  // the future, this value could be adjusted with a command-line option.
985
0
  unsigned MCDCMaxConditions = (CGM.getCodeGenOpts().MCDCCoverage) ? 6 : 0;
986
987
0
  RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
988
0
  RegionMCDCBitmapMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
989
0
  MapRegionCounters Walker(HashVersion, ProfileVersion, *RegionCounterMap,
990
0
                           *RegionMCDCBitmapMap, MCDCMaxConditions,
991
0
                           CGM.getDiags());
992
0
  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
993
0
    Walker.TraverseDecl(const_cast<FunctionDecl *>(FD));
994
0
  else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
995
0
    Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD));
996
0
  else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
997
0
    Walker.TraverseDecl(const_cast<BlockDecl *>(BD));
998
0
  else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
999
0
    Walker.TraverseDecl(const_cast<CapturedDecl *>(CD));
1000
0
  assert(Walker.NextCounter > 0 && "no entry counter mapped for decl");
1001
0
  NumRegionCounters = Walker.NextCounter;
1002
0
  MCDCBitmapBytes = Walker.NextMCDCBitmapIdx;
1003
0
  FunctionHash = Walker.Hash.finalize();
1004
0
}
1005
1006
0
bool CodeGenPGO::skipRegionMappingForDecl(const Decl *D) {
1007
0
  if (!D->getBody())
1008
0
    return true;
1009
1010
  // Skip host-only functions in the CUDA device compilation and device-only
1011
  // functions in the host compilation. Just roughly filter them out based on
1012
  // the function attributes. If there are effectively host-only or device-only
1013
  // ones, their coverage mapping may still be generated.
1014
0
  if (CGM.getLangOpts().CUDA &&
1015
0
      ((CGM.getLangOpts().CUDAIsDevice && !D->hasAttr<CUDADeviceAttr>() &&
1016
0
        !D->hasAttr<CUDAGlobalAttr>()) ||
1017
0
       (!CGM.getLangOpts().CUDAIsDevice &&
1018
0
        (D->hasAttr<CUDAGlobalAttr>() ||
1019
0
         (!D->hasAttr<CUDAHostAttr>() && D->hasAttr<CUDADeviceAttr>())))))
1020
0
    return true;
1021
1022
  // Don't map the functions in system headers.
1023
0
  const auto &SM = CGM.getContext().getSourceManager();
1024
0
  auto Loc = D->getBody()->getBeginLoc();
1025
0
  return SM.isInSystemHeader(Loc);
1026
0
}
1027
1028
0
void CodeGenPGO::emitCounterRegionMapping(const Decl *D) {
1029
0
  if (skipRegionMappingForDecl(D))
1030
0
    return;
1031
1032
0
  std::string CoverageMapping;
1033
0
  llvm::raw_string_ostream OS(CoverageMapping);
1034
0
  RegionCondIDMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
1035
0
  CoverageMappingGen MappingGen(
1036
0
      *CGM.getCoverageMapping(), CGM.getContext().getSourceManager(),
1037
0
      CGM.getLangOpts(), RegionCounterMap.get(), RegionMCDCBitmapMap.get(),
1038
0
      RegionCondIDMap.get());
1039
0
  MappingGen.emitCounterMapping(D, OS);
1040
0
  OS.flush();
1041
1042
0
  if (CoverageMapping.empty())
1043
0
    return;
1044
1045
0
  CGM.getCoverageMapping()->addFunctionMappingRecord(
1046
0
      FuncNameVar, FuncName, FunctionHash, CoverageMapping);
1047
0
}
1048
1049
void
1050
CodeGenPGO::emitEmptyCounterMapping(const Decl *D, StringRef Name,
1051
0
                                    llvm::GlobalValue::LinkageTypes Linkage) {
1052
0
  if (skipRegionMappingForDecl(D))
1053
0
    return;
1054
1055
0
  std::string CoverageMapping;
1056
0
  llvm::raw_string_ostream OS(CoverageMapping);
1057
0
  CoverageMappingGen MappingGen(*CGM.getCoverageMapping(),
1058
0
                                CGM.getContext().getSourceManager(),
1059
0
                                CGM.getLangOpts());
1060
0
  MappingGen.emitEmptyMapping(D, OS);
1061
0
  OS.flush();
1062
1063
0
  if (CoverageMapping.empty())
1064
0
    return;
1065
1066
0
  setFuncName(Name, Linkage);
1067
0
  CGM.getCoverageMapping()->addFunctionMappingRecord(
1068
0
      FuncNameVar, FuncName, FunctionHash, CoverageMapping, false);
1069
0
}
1070
1071
0
void CodeGenPGO::computeRegionCounts(const Decl *D) {
1072
0
  StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>);
1073
0
  ComputeRegionCounts Walker(*StmtCountMap, *this);
1074
0
  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
1075
0
    Walker.VisitFunctionDecl(FD);
1076
0
  else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
1077
0
    Walker.VisitObjCMethodDecl(MD);
1078
0
  else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
1079
0
    Walker.VisitBlockDecl(BD);
1080
0
  else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
1081
0
    Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD));
1082
0
}
1083
1084
void
1085
CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
1086
0
                                    llvm::Function *Fn) {
1087
0
  if (!haveRegionCounts())
1088
0
    return;
1089
1090
0
  uint64_t FunctionCount = getRegionCount(nullptr);
1091
0
  Fn->setEntryCount(FunctionCount);
1092
0
}
1093
1094
void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S,
1095
0
                                      llvm::Value *StepV) {
1096
0
  if (!RegionCounterMap || !Builder.GetInsertBlock())
1097
0
    return;
1098
1099
0
  unsigned Counter = (*RegionCounterMap)[S];
1100
1101
0
  llvm::Value *Args[] = {FuncNameVar,
1102
0
                         Builder.getInt64(FunctionHash),
1103
0
                         Builder.getInt32(NumRegionCounters),
1104
0
                         Builder.getInt32(Counter), StepV};
1105
0
  if (!StepV)
1106
0
    Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::instrprof_increment),
1107
0
                       ArrayRef(Args, 4));
1108
0
  else
1109
0
    Builder.CreateCall(
1110
0
        CGM.getIntrinsic(llvm::Intrinsic::instrprof_increment_step),
1111
0
        ArrayRef(Args));
1112
0
}
1113
1114
0
bool CodeGenPGO::canEmitMCDCCoverage(const CGBuilderTy &Builder) {
1115
0
  return (CGM.getCodeGenOpts().hasProfileClangInstr() &&
1116
0
          CGM.getCodeGenOpts().MCDCCoverage && Builder.GetInsertBlock());
1117
0
}
1118
1119
0
void CodeGenPGO::emitMCDCParameters(CGBuilderTy &Builder) {
1120
0
  if (!canEmitMCDCCoverage(Builder) || !RegionMCDCBitmapMap)
1121
0
    return;
1122
1123
0
  auto *I8PtrTy = llvm::PointerType::getUnqual(CGM.getLLVMContext());
1124
1125
  // Emit intrinsic representing MCDC bitmap parameters at function entry.
1126
  // This is used by the instrumentation pass, but it isn't actually lowered to
1127
  // anything.
1128
0
  llvm::Value *Args[3] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
1129
0
                          Builder.getInt64(FunctionHash),
1130
0
                          Builder.getInt32(MCDCBitmapBytes)};
1131
0
  Builder.CreateCall(
1132
0
      CGM.getIntrinsic(llvm::Intrinsic::instrprof_mcdc_parameters), Args);
1133
0
}
1134
1135
void CodeGenPGO::emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder,
1136
                                                const Expr *S,
1137
0
                                                Address MCDCCondBitmapAddr) {
1138
0
  if (!canEmitMCDCCoverage(Builder) || !RegionMCDCBitmapMap)
1139
0
    return;
1140
1141
0
  S = S->IgnoreParens();
1142
1143
0
  auto ExprMCDCBitmapMapIterator = RegionMCDCBitmapMap->find(S);
1144
0
  if (ExprMCDCBitmapMapIterator == RegionMCDCBitmapMap->end())
1145
0
    return;
1146
1147
  // Extract the ID of the global bitmap associated with this expression.
1148
0
  unsigned MCDCTestVectorBitmapID = ExprMCDCBitmapMapIterator->second;
1149
0
  auto *I8PtrTy = llvm::PointerType::getUnqual(CGM.getLLVMContext());
1150
1151
  // Emit intrinsic responsible for updating the global bitmap corresponding to
1152
  // a boolean expression. The index being set is based on the value loaded
1153
  // from a pointer to a dedicated temporary value on the stack that is itself
1154
  // updated via emitMCDCCondBitmapReset() and emitMCDCCondBitmapUpdate(). The
1155
  // index represents an executed test vector.
1156
0
  llvm::Value *Args[5] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
1157
0
                          Builder.getInt64(FunctionHash),
1158
0
                          Builder.getInt32(MCDCBitmapBytes),
1159
0
                          Builder.getInt32(MCDCTestVectorBitmapID),
1160
0
                          MCDCCondBitmapAddr.getPointer()};
1161
0
  Builder.CreateCall(
1162
0
      CGM.getIntrinsic(llvm::Intrinsic::instrprof_mcdc_tvbitmap_update), Args);
1163
0
}
1164
1165
void CodeGenPGO::emitMCDCCondBitmapReset(CGBuilderTy &Builder, const Expr *S,
1166
0
                                         Address MCDCCondBitmapAddr) {
1167
0
  if (!canEmitMCDCCoverage(Builder) || !RegionMCDCBitmapMap)
1168
0
    return;
1169
1170
0
  S = S->IgnoreParens();
1171
1172
0
  if (RegionMCDCBitmapMap->find(S) == RegionMCDCBitmapMap->end())
1173
0
    return;
1174
1175
  // Emit intrinsic that resets a dedicated temporary value on the stack to 0.
1176
0
  Builder.CreateStore(Builder.getInt32(0), MCDCCondBitmapAddr);
1177
0
}
1178
1179
void CodeGenPGO::emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
1180
                                          Address MCDCCondBitmapAddr,
1181
0
                                          llvm::Value *Val) {
1182
0
  if (!canEmitMCDCCoverage(Builder) || !RegionCondIDMap)
1183
0
    return;
1184
1185
  // Even though, for simplicity, parentheses and unary logical-NOT operators
1186
  // are considered part of their underlying condition for both MC/DC and
1187
  // branch coverage, the condition IDs themselves are assigned and tracked
1188
  // using the underlying condition itself.  This is done solely for
1189
  // consistency since parentheses and logical-NOTs are ignored when checking
1190
  // whether the condition is actually an instrumentable condition. This can
1191
  // also make debugging a bit easier.
1192
0
  S = CodeGenFunction::stripCond(S);
1193
1194
0
  auto ExprMCDCConditionIDMapIterator = RegionCondIDMap->find(S);
1195
0
  if (ExprMCDCConditionIDMapIterator == RegionCondIDMap->end())
1196
0
    return;
1197
1198
  // Extract the ID of the condition we are setting in the bitmap.
1199
0
  unsigned CondID = ExprMCDCConditionIDMapIterator->second;
1200
0
  assert(CondID > 0 && "Condition has no ID!");
1201
1202
0
  auto *I8PtrTy = llvm::PointerType::getUnqual(CGM.getLLVMContext());
1203
1204
  // Emit intrinsic that updates a dedicated temporary value on the stack after
1205
  // a condition is evaluated. After the set of conditions has been updated,
1206
  // the resulting value is used to update the boolean expression's bitmap.
1207
0
  llvm::Value *Args[5] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
1208
0
                          Builder.getInt64(FunctionHash),
1209
0
                          Builder.getInt32(CondID - 1),
1210
0
                          MCDCCondBitmapAddr.getPointer(), Val};
1211
0
  Builder.CreateCall(
1212
0
      CGM.getIntrinsic(llvm::Intrinsic::instrprof_mcdc_condbitmap_update),
1213
0
      Args);
1214
0
}
1215
1216
0
void CodeGenPGO::setValueProfilingFlag(llvm::Module &M) {
1217
0
  if (CGM.getCodeGenOpts().hasProfileClangInstr())
1218
0
    M.addModuleFlag(llvm::Module::Warning, "EnableValueProfiling",
1219
0
                    uint32_t(EnableValueProfiling));
1220
0
}
1221
1222
// This method either inserts a call to the profile run-time during
1223
// instrumentation or puts profile data into metadata for PGO use.
1224
void CodeGenPGO::valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
1225
0
    llvm::Instruction *ValueSite, llvm::Value *ValuePtr) {
1226
1227
0
  if (!EnableValueProfiling)
1228
0
    return;
1229
1230
0
  if (!ValuePtr || !ValueSite || !Builder.GetInsertBlock())
1231
0
    return;
1232
1233
0
  if (isa<llvm::Constant>(ValuePtr))
1234
0
    return;
1235
1236
0
  bool InstrumentValueSites = CGM.getCodeGenOpts().hasProfileClangInstr();
1237
0
  if (InstrumentValueSites && RegionCounterMap) {
1238
0
    auto BuilderInsertPoint = Builder.saveIP();
1239
0
    Builder.SetInsertPoint(ValueSite);
1240
0
    llvm::Value *Args[5] = {
1241
0
        FuncNameVar,
1242
0
        Builder.getInt64(FunctionHash),
1243
0
        Builder.CreatePtrToInt(ValuePtr, Builder.getInt64Ty()),
1244
0
        Builder.getInt32(ValueKind),
1245
0
        Builder.getInt32(NumValueSites[ValueKind]++)
1246
0
    };
1247
0
    Builder.CreateCall(
1248
0
        CGM.getIntrinsic(llvm::Intrinsic::instrprof_value_profile), Args);
1249
0
    Builder.restoreIP(BuilderInsertPoint);
1250
0
    return;
1251
0
  }
1252
1253
0
  llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
1254
0
  if (PGOReader && haveRegionCounts()) {
1255
    // We record the top most called three functions at each call site.
1256
    // Profile metadata contains "VP" string identifying this metadata
1257
    // as value profiling data, then a uint32_t value for the value profiling
1258
    // kind, a uint64_t value for the total number of times the call is
1259
    // executed, followed by the function hash and execution count (uint64_t)
1260
    // pairs for each function.
1261
0
    if (NumValueSites[ValueKind] >= ProfRecord->getNumValueSites(ValueKind))
1262
0
      return;
1263
1264
0
    llvm::annotateValueSite(CGM.getModule(), *ValueSite, *ProfRecord,
1265
0
                            (llvm::InstrProfValueKind)ValueKind,
1266
0
                            NumValueSites[ValueKind]);
1267
1268
0
    NumValueSites[ValueKind]++;
1269
0
  }
1270
0
}
1271
1272
void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
1273
0
                                  bool IsInMainFile) {
1274
0
  CGM.getPGOStats().addVisited(IsInMainFile);
1275
0
  RegionCounts.clear();
1276
0
  llvm::Expected<llvm::InstrProfRecord> RecordExpected =
1277
0
      PGOReader->getInstrProfRecord(FuncName, FunctionHash);
1278
0
  if (auto E = RecordExpected.takeError()) {
1279
0
    auto IPE = std::get<0>(llvm::InstrProfError::take(std::move(E)));
1280
0
    if (IPE == llvm::instrprof_error::unknown_function)
1281
0
      CGM.getPGOStats().addMissing(IsInMainFile);
1282
0
    else if (IPE == llvm::instrprof_error::hash_mismatch)
1283
0
      CGM.getPGOStats().addMismatched(IsInMainFile);
1284
0
    else if (IPE == llvm::instrprof_error::malformed)
1285
      // TODO: Consider a more specific warning for this case.
1286
0
      CGM.getPGOStats().addMismatched(IsInMainFile);
1287
0
    return;
1288
0
  }
1289
0
  ProfRecord =
1290
0
      std::make_unique<llvm::InstrProfRecord>(std::move(RecordExpected.get()));
1291
0
  RegionCounts = ProfRecord->Counts;
1292
0
}
1293
1294
/// Calculate what to divide by to scale weights.
1295
///
1296
/// Given the maximum weight, calculate a divisor that will scale all the
1297
/// weights to strictly less than UINT32_MAX.
1298
0
static uint64_t calculateWeightScale(uint64_t MaxWeight) {
1299
0
  return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1;
1300
0
}
1301
1302
/// Scale an individual branch weight (and add 1).
1303
///
1304
/// Scale a 64-bit weight down to 32-bits using \c Scale.
1305
///
1306
/// According to Laplace's Rule of Succession, it is better to compute the
1307
/// weight based on the count plus 1, so universally add 1 to the value.
1308
///
1309
/// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no
1310
/// greater than \c Weight.
1311
0
static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) {
1312
0
  assert(Scale && "scale by 0?");
1313
0
  uint64_t Scaled = Weight / Scale + 1;
1314
0
  assert(Scaled <= UINT32_MAX && "overflow 32-bits");
1315
0
  return Scaled;
1316
0
}
1317
1318
llvm::MDNode *CodeGenFunction::createProfileWeights(uint64_t TrueCount,
1319
0
                                                    uint64_t FalseCount) const {
1320
  // Check for empty weights.
1321
0
  if (!TrueCount && !FalseCount)
1322
0
    return nullptr;
1323
1324
  // Calculate how to scale down to 32-bits.
1325
0
  uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount));
1326
1327
0
  llvm::MDBuilder MDHelper(CGM.getLLVMContext());
1328
0
  return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale),
1329
0
                                      scaleBranchWeight(FalseCount, Scale));
1330
0
}
1331
1332
llvm::MDNode *
1333
0
CodeGenFunction::createProfileWeights(ArrayRef<uint64_t> Weights) const {
1334
  // We need at least two elements to create meaningful weights.
1335
0
  if (Weights.size() < 2)
1336
0
    return nullptr;
1337
1338
  // Check for empty weights.
1339
0
  uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end());
1340
0
  if (MaxWeight == 0)
1341
0
    return nullptr;
1342
1343
  // Calculate how to scale down to 32-bits.
1344
0
  uint64_t Scale = calculateWeightScale(MaxWeight);
1345
1346
0
  SmallVector<uint32_t, 16> ScaledWeights;
1347
0
  ScaledWeights.reserve(Weights.size());
1348
0
  for (uint64_t W : Weights)
1349
0
    ScaledWeights.push_back(scaleBranchWeight(W, Scale));
1350
1351
0
  llvm::MDBuilder MDHelper(CGM.getLLVMContext());
1352
0
  return MDHelper.createBranchWeights(ScaledWeights);
1353
0
}
1354
1355
llvm::MDNode *
1356
CodeGenFunction::createProfileWeightsForLoop(const Stmt *Cond,
1357
0
                                             uint64_t LoopCount) const {
1358
0
  if (!PGO.haveRegionCounts())
1359
0
    return nullptr;
1360
0
  std::optional<uint64_t> CondCount = PGO.getStmtCount(Cond);
1361
0
  if (!CondCount || *CondCount == 0)
1362
0
    return nullptr;
1363
0
  return createProfileWeights(LoopCount,
1364
0
                              std::max(*CondCount, LoopCount) - LoopCount);
1365
0
}