blob: ccda5759e673df3bdac9803b0602dcd7b31b8032 [file] [log] [blame]
Justin Bogneref512b92014-01-06 22:27:43 +00001//===--- 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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
15#define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
Justin Bogneref512b92014-01-06 22:27:43 +000016
17#include "CGBuilder.h"
18#include "CodeGenModule.h"
19#include "CodeGenTypes.h"
20#include "clang/Frontend/CodeGenOptions.h"
Justin Bogneref512b92014-01-06 22:27:43 +000021#include "llvm/ADT/StringMap.h"
Betul Buyukkurt518276a2016-01-23 22:50:44 +000022#include "llvm/ProfileData/InstrProfReader.h"
Justin Bogneref512b92014-01-06 22:27:43 +000023#include "llvm/Support/MemoryBuffer.h"
Betul Buyukkurt7e152722016-01-24 00:56:19 +000024#include <array>
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000025#include <memory>
Justin Bogneref512b92014-01-06 22:27:43 +000026
27namespace clang {
28namespace CodeGen {
Justin Bogneref512b92014-01-06 22:27:43 +000029
Justin Bogner66242d62015-04-23 23:06:47 +000030/// Per-function PGO state.
Justin Bogneref512b92014-01-06 22:27:43 +000031class CodeGenPGO {
32private:
33 CodeGenModule &CGM;
Justin Bogner111c6532014-12-02 23:15:30 +000034 std::string FuncName;
Justin Bogner970ac602014-12-08 19:04:51 +000035 llvm::GlobalVariable *FuncNameVar;
Justin Bogneref512b92014-01-06 22:27:43 +000036
Betul Buyukkurt7e152722016-01-24 00:56:19 +000037 std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
Justin Bogneref512b92014-01-06 22:27:43 +000038 unsigned NumRegionCounters;
Justin Bognerb4416f52014-03-18 21:58:06 +000039 uint64_t FunctionHash;
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +000040 std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
41 std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
Betul Buyukkurt518276a2016-01-23 22:50:44 +000042 std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
Justin Bogner7f8cf5b2014-12-02 22:38:52 +000043 std::vector<uint64_t> RegionCounts;
Justin Bogneref512b92014-01-06 22:27:43 +000044 uint64_t CurrentRegionCount;
Alex Lorenzee024992014-08-04 18:41:51 +000045 /// \brief A flag that is set to true when this function doesn't need
46 /// to have coverage mapping data.
47 bool SkipCoverageMapping;
Justin Bogneref512b92014-01-06 22:27:43 +000048
49public:
50 CodeGenPGO(CodeGenModule &CGM)
Betul Buyukkurt7e152722016-01-24 00:56:19 +000051 : CGM(CGM), NumValueSites({{0}}), NumRegionCounters(0),
Betul Buyukkurt518276a2016-01-23 22:50:44 +000052 FunctionHash(0), CurrentRegionCount(0), SkipCoverageMapping(false) {}
Justin Bogneref512b92014-01-06 22:27:43 +000053
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.
Justin Bogner7f8cf5b2014-12-02 22:38:52 +000057 bool haveRegionCounts() const { return !RegionCounts.empty(); }
Justin Bogneref512b92014-01-06 22:27:43 +000058
59 /// Return the counter value of the current region.
60 uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
Bob Wilsonbf854f02014-02-17 19:21:09 +000061
Justin Bogneref512b92014-01-06 22:27:43 +000062 /// 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; }
Bob Wilsonbf854f02014-02-17 19:21:09 +000066
Bob Wilsonbf854f02014-02-17 19:21:09 +000067 /// 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.
Justin Bogner1c21c282015-04-13 12:23:19 +000069 Optional<uint64_t> getStmtCount(const Stmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +000070 if (!StmtCountMap)
Justin Bogner1c21c282015-04-13 12:23:19 +000071 return None;
72 auto I = StmtCountMap->find(S);
Bob Wilsonbf854f02014-02-17 19:21:09 +000073 if (I == StmtCountMap->end())
Justin Bogner1c21c282015-04-13 12:23:19 +000074 return None;
75 return I->second;
Bob Wilsonbf854f02014-02-17 19:21:09 +000076 }
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) {
Justin Bogner1c21c282015-04-13 12:23:19 +000081 if (auto Count = getStmtCount(S))
82 setCurrentRegionCount(*Count);
Bob Wilsonbf854f02014-02-17 19:21:09 +000083 }
84
Justin Bogneref512b92014-01-06 22:27:43 +000085 /// 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.
Serge Pavlov3a561452015-12-06 14:32:39 +000089 void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn);
Alex Lorenzee024992014-08-04 18:41:51 +000090 /// 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);
Betul Buyukkurt518276a2016-01-23 22:50:44 +000094 // Insert instrumentation or attach profile metadata at value sites
95 void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
96 llvm::Instruction *ValueSite, llvm::Value *ValuePtr);
Justin Bogneref512b92014-01-06 22:27:43 +000097private:
Bob Wilsonda1ebed2014-03-06 04:55:41 +000098 void setFuncName(llvm::Function *Fn);
Alex Lorenzee024992014-08-04 18:41:51 +000099 void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
Justin Bogneref512b92014-01-06 22:27:43 +0000100 void mapRegionCounters(const Decl *D);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000101 void computeRegionCounts(const Decl *D);
Justin Bogner837a6f62014-04-18 21:52:00 +0000102 void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
103 llvm::Function *Fn);
Justin Bogner40b8ba12014-06-26 01:45:07 +0000104 void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
105 bool IsInMainFile);
Alex Lorenzee024992014-08-04 18:41:51 +0000106 void emitCounterRegionMapping(const Decl *D);
Justin Bogneref512b92014-01-06 22:27:43 +0000107
Justin Bogner66242d62015-04-23 23:06:47 +0000108public:
109 void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S);
Justin Bogneref512b92014-01-06 22:27:43 +0000110
111 /// Return the region count for the counter at the given index.
Justin Bogner66242d62015-04-23 23:06:47 +0000112 uint64_t getRegionCount(const Stmt *S) {
113 if (!RegionCounterMap)
114 return 0;
Justin Bogneref512b92014-01-06 22:27:43 +0000115 if (!haveRegionCounts())
116 return 0;
Justin Bogner66242d62015-04-23 23:06:47 +0000117 return RegionCounts[(*RegionCounterMap)[S]];
Justin Bogneref512b92014-01-06 22:27:43 +0000118 }
Justin Bogneref512b92014-01-06 22:27:43 +0000119};
120
Justin Bogneref512b92014-01-06 22:27:43 +0000121} // end namespace CodeGen
122} // end namespace clang
123
124#endif