blob: d03f23535bb93f88d35095a409f256b9830ced90 [file] [log] [blame]
Stephen Hines651f13c2014-04-23 16:59:28 -07001//===--- CodeGenPGO.h - 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
Stephen Hines176edba2014-12-01 14:53:08 -080014#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
15#define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
Stephen Hines651f13c2014-04-23 16:59:28 -070016
17#include "CGBuilder.h"
18#include "CodeGenModule.h"
19#include "CodeGenTypes.h"
20#include "clang/Frontend/CodeGenOptions.h"
21#include "llvm/ADT/StringMap.h"
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070022#include "llvm/ProfileData/InstrProfReader.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070023#include "llvm/Support/MemoryBuffer.h"
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070024#include <array>
Stephen Hines651f13c2014-04-23 16:59:28 -070025#include <memory>
26
27namespace clang {
28namespace CodeGen {
Stephen Hines651f13c2014-04-23 16:59:28 -070029
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -070030/// Per-function PGO state.
Stephen Hines651f13c2014-04-23 16:59:28 -070031class CodeGenPGO {
32private:
33 CodeGenModule &CGM;
Stephen Hines0e2c34f2015-03-23 12:09:02 -070034 std::string FuncName;
35 llvm::GlobalVariable *FuncNameVar;
Stephen Hines651f13c2014-04-23 16:59:28 -070036
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070037 std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
Stephen Hines651f13c2014-04-23 16:59:28 -070038 unsigned NumRegionCounters;
39 uint64_t FunctionHash;
Stephen Hines651f13c2014-04-23 16:59:28 -070040 std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
41 std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070042 std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
Stephen Hines0e2c34f2015-03-23 12:09:02 -070043 std::vector<uint64_t> RegionCounts;
Stephen Hines651f13c2014-04-23 16:59:28 -070044 uint64_t CurrentRegionCount;
Stephen Hines176edba2014-12-01 14:53:08 -080045 /// \brief A flag that is set to true when this function doesn't need
46 /// to have coverage mapping data.
47 bool SkipCoverageMapping;
Stephen Hines651f13c2014-04-23 16:59:28 -070048
49public:
50 CodeGenPGO(CodeGenModule &CGM)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070051 : CGM(CGM), NumValueSites({{0}}), NumRegionCounters(0),
52 FunctionHash(0), CurrentRegionCount(0), SkipCoverageMapping(false) {}
Stephen Hines651f13c2014-04-23 16:59:28 -070053
54 /// Whether or not we have PGO region data for the current function. This is
55 /// false both when we have no data at all and when our data has been
56 /// discarded.
Stephen Hines0e2c34f2015-03-23 12:09:02 -070057 bool haveRegionCounts() const { return !RegionCounts.empty(); }
Stephen Hines651f13c2014-04-23 16:59:28 -070058
59 /// Return the counter value of the current region.
60 uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
61
62 /// Set the counter value for the current region. This is used to keep track
63 /// of changes to the most recent counter from control flow and non-local
64 /// exits.
65 void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
66
Stephen Hines651f13c2014-04-23 16:59:28 -070067 /// Check if an execution count is known for a given statement. If so, return
68 /// true and put the value in Count; else return false.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -070069 Optional<uint64_t> getStmtCount(const Stmt *S) {
Stephen Hines651f13c2014-04-23 16:59:28 -070070 if (!StmtCountMap)
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -070071 return None;
72 auto I = StmtCountMap->find(S);
Stephen Hines651f13c2014-04-23 16:59:28 -070073 if (I == StmtCountMap->end())
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -070074 return None;
75 return I->second;
Stephen Hines651f13c2014-04-23 16:59:28 -070076 }
77
78 /// If the execution count for the current statement is known, record that
79 /// as the current count.
80 void setCurrentStmt(const Stmt *S) {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -070081 if (auto Count = getStmtCount(S))
82 setCurrentRegionCount(*Count);
Stephen Hines651f13c2014-04-23 16:59:28 -070083 }
84
Stephen Hines651f13c2014-04-23 16:59:28 -070085 /// Assign counters to regions and configure them for PGO of a given
86 /// function. Does nothing if instrumentation is not enabled and either
87 /// generates global variables or associates PGO data with each of the
88 /// counters depending on whether we are generating or using instrumentation.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080089 void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn);
Stephen Hines176edba2014-12-01 14:53:08 -080090 /// Emit a coverage mapping range with a counter zero
91 /// for an unused declaration.
92 void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
93 llvm::GlobalValue::LinkageTypes Linkage);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070094 // Insert instrumentation or attach profile metadata at value sites
95 void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
96 llvm::Instruction *ValueSite, llvm::Value *ValuePtr);
Stephen Hines651f13c2014-04-23 16:59:28 -070097private:
98 void setFuncName(llvm::Function *Fn);
Stephen Hines176edba2014-12-01 14:53:08 -080099 void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
Stephen Hines651f13c2014-04-23 16:59:28 -0700100 void mapRegionCounters(const Decl *D);
101 void computeRegionCounts(const Decl *D);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700102 void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
103 llvm::Function *Fn);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700104 void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
105 bool IsInMainFile);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700106 bool skipRegionMappingForDecl(const Decl *D);
Stephen Hines176edba2014-12-01 14:53:08 -0800107 void emitCounterRegionMapping(const Decl *D);
Stephen Hines651f13c2014-04-23 16:59:28 -0700108
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700109public:
110 void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S);
Stephen Hines651f13c2014-04-23 16:59:28 -0700111
112 /// Return the region count for the counter at the given index.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700113 uint64_t getRegionCount(const Stmt *S) {
114 if (!RegionCounterMap)
115 return 0;
Stephen Hines651f13c2014-04-23 16:59:28 -0700116 if (!haveRegionCounts())
117 return 0;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700118 return RegionCounts[(*RegionCounterMap)[S]];
Stephen Hines651f13c2014-04-23 16:59:28 -0700119 }
120};
121
122} // end namespace CodeGen
123} // end namespace clang
124
125#endif