blob: 2e740f78924315a3543361489f64f03d1c1c862d [file] [log] [blame]
Justin Bogneref512b92014-01-06 22:27:43 +00001//===--- CodeGenPGO.h - PGO Instrumentation for LLVM CodeGen ----*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Justin Bogneref512b92014-01-06 22:27:43 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Instrumentation-based profile-guided optimization
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
14#define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
Justin Bogneref512b92014-01-06 22:27:43 +000015
16#include "CGBuilder.h"
17#include "CodeGenModule.h"
18#include "CodeGenTypes.h"
Betul Buyukkurt518276a2016-01-23 22:50:44 +000019#include "llvm/ProfileData/InstrProfReader.h"
Betul Buyukkurt7e152722016-01-24 00:56:19 +000020#include <array>
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000021#include <memory>
Justin Bogneref512b92014-01-06 22:27:43 +000022
23namespace clang {
24namespace CodeGen {
Justin Bogneref512b92014-01-06 22:27:43 +000025
Justin Bogner66242d62015-04-23 23:06:47 +000026/// Per-function PGO state.
Justin Bogneref512b92014-01-06 22:27:43 +000027class CodeGenPGO {
28private:
29 CodeGenModule &CGM;
Justin Bogner111c6532014-12-02 23:15:30 +000030 std::string FuncName;
Justin Bogner970ac602014-12-08 19:04:51 +000031 llvm::GlobalVariable *FuncNameVar;
Justin Bogneref512b92014-01-06 22:27:43 +000032
Betul Buyukkurt7e152722016-01-24 00:56:19 +000033 std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
Justin Bogneref512b92014-01-06 22:27:43 +000034 unsigned NumRegionCounters;
Justin Bognerb4416f52014-03-18 21:58:06 +000035 uint64_t FunctionHash;
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +000036 std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
37 std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
Betul Buyukkurt518276a2016-01-23 22:50:44 +000038 std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
Justin Bogner7f8cf5b2014-12-02 22:38:52 +000039 std::vector<uint64_t> RegionCounts;
Justin Bogneref512b92014-01-06 22:27:43 +000040 uint64_t CurrentRegionCount;
41
42public:
43 CodeGenPGO(CodeGenModule &CGM)
Vedant Kumar9e32aa22017-04-24 20:54:36 +000044 : CGM(CGM), NumValueSites({{0}}), NumRegionCounters(0), FunctionHash(0),
45 CurrentRegionCount(0) {}
Justin Bogneref512b92014-01-06 22:27:43 +000046
47 /// Whether or not we have PGO region data for the current function. This is
48 /// false both when we have no data at all and when our data has been
49 /// discarded.
Justin Bogner7f8cf5b2014-12-02 22:38:52 +000050 bool haveRegionCounts() const { return !RegionCounts.empty(); }
Justin Bogneref512b92014-01-06 22:27:43 +000051
52 /// Return the counter value of the current region.
53 uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
Bob Wilsonbf854f02014-02-17 19:21:09 +000054
Justin Bogneref512b92014-01-06 22:27:43 +000055 /// Set the counter value for the current region. This is used to keep track
56 /// of changes to the most recent counter from control flow and non-local
57 /// exits.
58 void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
Bob Wilsonbf854f02014-02-17 19:21:09 +000059
Bob Wilsonbf854f02014-02-17 19:21:09 +000060 /// Check if an execution count is known for a given statement. If so, return
61 /// true and put the value in Count; else return false.
Justin Bogner1c21c282015-04-13 12:23:19 +000062 Optional<uint64_t> getStmtCount(const Stmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +000063 if (!StmtCountMap)
Justin Bogner1c21c282015-04-13 12:23:19 +000064 return None;
65 auto I = StmtCountMap->find(S);
Bob Wilsonbf854f02014-02-17 19:21:09 +000066 if (I == StmtCountMap->end())
Justin Bogner1c21c282015-04-13 12:23:19 +000067 return None;
68 return I->second;
Bob Wilsonbf854f02014-02-17 19:21:09 +000069 }
70
71 /// If the execution count for the current statement is known, record that
72 /// as the current count.
73 void setCurrentStmt(const Stmt *S) {
Justin Bogner1c21c282015-04-13 12:23:19 +000074 if (auto Count = getStmtCount(S))
75 setCurrentRegionCount(*Count);
Bob Wilsonbf854f02014-02-17 19:21:09 +000076 }
77
Justin Bogneref512b92014-01-06 22:27:43 +000078 /// Assign counters to regions and configure them for PGO of a given
79 /// function. Does nothing if instrumentation is not enabled and either
80 /// generates global variables or associates PGO data with each of the
81 /// counters depending on whether we are generating or using instrumentation.
Serge Pavlov3a561452015-12-06 14:32:39 +000082 void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn);
Alex Lorenzee024992014-08-04 18:41:51 +000083 /// Emit a coverage mapping range with a counter zero
84 /// for an unused declaration.
85 void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
86 llvm::GlobalValue::LinkageTypes Linkage);
Betul Buyukkurt518276a2016-01-23 22:50:44 +000087 // Insert instrumentation or attach profile metadata at value sites
88 void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
89 llvm::Instruction *ValueSite, llvm::Value *ValuePtr);
Justin Bogneref512b92014-01-06 22:27:43 +000090private:
Bob Wilsonda1ebed2014-03-06 04:55:41 +000091 void setFuncName(llvm::Function *Fn);
Alex Lorenzee024992014-08-04 18:41:51 +000092 void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
Justin Bogneref512b92014-01-06 22:27:43 +000093 void mapRegionCounters(const Decl *D);
Bob Wilsonbf854f02014-02-17 19:21:09 +000094 void computeRegionCounts(const Decl *D);
Justin Bogner837a6f62014-04-18 21:52:00 +000095 void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
96 llvm::Function *Fn);
Justin Bogner40b8ba12014-06-26 01:45:07 +000097 void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
98 bool IsInMainFile);
Vedant Kumarc468bb82016-07-11 22:57:44 +000099 bool skipRegionMappingForDecl(const Decl *D);
Alex Lorenzee024992014-08-04 18:41:51 +0000100 void emitCounterRegionMapping(const Decl *D);
Justin Bogneref512b92014-01-06 22:27:43 +0000101
Justin Bogner66242d62015-04-23 23:06:47 +0000102public:
Vedant Kumar502bbfa2017-02-25 06:35:45 +0000103 void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S,
104 llvm::Value *StepV);
Justin Bogneref512b92014-01-06 22:27:43 +0000105
106 /// Return the region count for the counter at the given index.
Justin Bogner66242d62015-04-23 23:06:47 +0000107 uint64_t getRegionCount(const Stmt *S) {
108 if (!RegionCounterMap)
109 return 0;
Justin Bogneref512b92014-01-06 22:27:43 +0000110 if (!haveRegionCounts())
111 return 0;
Justin Bogner66242d62015-04-23 23:06:47 +0000112 return RegionCounts[(*RegionCounterMap)[S]];
Justin Bogneref512b92014-01-06 22:27:43 +0000113 }
Justin Bogneref512b92014-01-06 22:27:43 +0000114};
115
Justin Bogneref512b92014-01-06 22:27:43 +0000116} // end namespace CodeGen
117} // end namespace clang
118
119#endif