blob: 68a6d7bb458431da121132063b2b1707139b2b67 [file] [log] [blame]
Easwaran Ramanb035f912017-01-13 01:34:00 +00001//===- ProfileSummaryInfoTest.cpp - ProfileSummaryInfo unit tests ---------===//
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
Chandler Carruth9a67b072017-06-06 11:06:56 +000010#include "llvm/Analysis/ProfileSummaryInfo.h"
Easwaran Ramanb035f912017-01-13 01:34:00 +000011#include "llvm/Analysis/BlockFrequencyInfo.h"
12#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
13#include "llvm/Analysis/BranchProbabilityInfo.h"
14#include "llvm/Analysis/LoopInfo.h"
Easwaran Ramanb035f912017-01-13 01:34:00 +000015#include "llvm/AsmParser/Parser.h"
16#include "llvm/IR/BasicBlock.h"
17#include "llvm/IR/CallSite.h"
18#include "llvm/IR/Dominators.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/MDBuilder.h"
22#include "llvm/IR/Module.h"
23#include "llvm/Support/DataTypes.h"
24#include "llvm/Support/FormatVariadic.h"
25#include "llvm/Support/SourceMgr.h"
26#include "llvm/Support/raw_ostream.h"
27#include "gtest/gtest.h"
28
29namespace llvm {
30namespace {
31
32class ProfileSummaryInfoTest : public testing::Test {
33protected:
34 LLVMContext C;
35 std::unique_ptr<BranchProbabilityInfo> BPI;
36 std::unique_ptr<DominatorTree> DT;
37 std::unique_ptr<LoopInfo> LI;
38
39 ProfileSummaryInfo buildPSI(Module *M) {
40 return ProfileSummaryInfo(*M);
41 }
42 BlockFrequencyInfo buildBFI(Function &F) {
43 DT.reset(new DominatorTree(F));
44 LI.reset(new LoopInfo(*DT));
45 BPI.reset(new BranchProbabilityInfo(F, *LI));
46 return BlockFrequencyInfo(F, *BPI, *LI);
47 }
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000048 std::unique_ptr<Module> makeLLVMModule(const char *ProfKind = nullptr) {
49 const char *ModuleString =
Easwaran Ramanb035f912017-01-13 01:34:00 +000050 "define i32 @g(i32 %x) !prof !21 {{\n"
51 " ret i32 0\n"
52 "}\n"
53 "define i32 @h(i32 %x) !prof !22 {{\n"
54 " ret i32 0\n"
55 "}\n"
56 "define i32 @f(i32 %x) !prof !20 {{\n"
57 "bb0:\n"
58 " %y1 = icmp eq i32 %x, 0 \n"
59 " br i1 %y1, label %bb1, label %bb2, !prof !23 \n"
60 "bb1:\n"
61 " %z1 = call i32 @g(i32 %x)\n"
62 " br label %bb3\n"
63 "bb2:\n"
64 " %z2 = call i32 @h(i32 %x)\n"
65 " br label %bb3\n"
66 "bb3:\n"
67 " %y2 = phi i32 [0, %bb1], [1, %bb2] \n"
68 " ret i32 %y2\n"
69 "}\n"
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000070 "!20 = !{{!\"function_entry_count\", i64 400}\n"
71 "!21 = !{{!\"function_entry_count\", i64 1}\n"
72 "!22 = !{{!\"function_entry_count\", i64 100}\n"
73 "!23 = !{{!\"branch_weights\", i32 64, i32 4}\n"
74 "{0}";
75 const char *SummaryString = "!llvm.module.flags = !{{!1}"
76 "!1 = !{{i32 1, !\"ProfileSummary\", !2}"
77 "!2 = !{{!3, !4, !5, !6, !7, !8, !9, !10}"
78 "!3 = !{{!\"ProfileFormat\", !\"{0}\"}"
79 "!4 = !{{!\"TotalCount\", i64 10000}"
80 "!5 = !{{!\"MaxCount\", i64 10}"
81 "!6 = !{{!\"MaxInternalCount\", i64 1}"
82 "!7 = !{{!\"MaxFunctionCount\", i64 1000}"
83 "!8 = !{{!\"NumCounts\", i64 3}"
84 "!9 = !{{!\"NumFunctions\", i64 3}"
85 "!10 = !{{!\"DetailedSummary\", !11}"
86 "!11 = !{{!12, !13, !14}"
87 "!12 = !{{i32 10000, i64 1000, i32 1}"
88 "!13 = !{{i32 999000, i64 300, i32 3}"
89 "!14 = !{{i32 999999, i64 5, i32 10}";
Easwaran Ramanb035f912017-01-13 01:34:00 +000090 SMDiagnostic Err;
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000091 if (ProfKind)
92 return parseAssemblyString(
93 formatv(ModuleString, formatv(SummaryString, ProfKind).str()).str(),
94 Err, C);
95 else
96 return parseAssemblyString(formatv(ModuleString, "").str(), Err, C);
Easwaran Ramanb035f912017-01-13 01:34:00 +000097 }
98};
99
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000100TEST_F(ProfileSummaryInfoTest, TestNoProfile) {
101 auto M = makeLLVMModule(/*ProfKind=*/nullptr);
102 Function *F = M->getFunction("f");
103
104 ProfileSummaryInfo PSI = buildPSI(M.get());
Easwaran Ramandadc0f12017-05-16 20:14:39 +0000105 EXPECT_FALSE(PSI.hasProfileSummary());
106 EXPECT_FALSE(PSI.hasSampleProfile());
107 EXPECT_FALSE(PSI.hasInstrumentationProfile());
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000108 // In the absence of profiles, is{Hot|Cold}X methods should always return
109 // false.
110 EXPECT_FALSE(PSI.isHotCount(1000));
111 EXPECT_FALSE(PSI.isHotCount(0));
112 EXPECT_FALSE(PSI.isColdCount(1000));
113 EXPECT_FALSE(PSI.isColdCount(0));
114
115 EXPECT_FALSE(PSI.isFunctionEntryHot(F));
116 EXPECT_FALSE(PSI.isFunctionEntryCold(F));
117
118 BasicBlock &BB0 = F->getEntryBlock();
119 BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
120
121 BlockFrequencyInfo BFI = buildBFI(*F);
122 EXPECT_FALSE(PSI.isHotBB(&BB0, &BFI));
123 EXPECT_FALSE(PSI.isColdBB(&BB0, &BFI));
124
125 CallSite CS1(BB1->getFirstNonPHI());
126 EXPECT_FALSE(PSI.isHotCallSite(CS1, &BFI));
127 EXPECT_FALSE(PSI.isColdCallSite(CS1, &BFI));
128}
Easwaran Ramanb035f912017-01-13 01:34:00 +0000129TEST_F(ProfileSummaryInfoTest, TestCommon) {
130 auto M = makeLLVMModule("InstrProf");
131 Function *F = M->getFunction("f");
132 Function *G = M->getFunction("g");
133 Function *H = M->getFunction("h");
134
135 ProfileSummaryInfo PSI = buildPSI(M.get());
Easwaran Ramandadc0f12017-05-16 20:14:39 +0000136 EXPECT_TRUE(PSI.hasProfileSummary());
Easwaran Ramanb035f912017-01-13 01:34:00 +0000137 EXPECT_TRUE(PSI.isHotCount(400));
138 EXPECT_TRUE(PSI.isColdCount(2));
139 EXPECT_FALSE(PSI.isColdCount(100));
140 EXPECT_FALSE(PSI.isHotCount(100));
141
142 EXPECT_TRUE(PSI.isFunctionEntryHot(F));
143 EXPECT_FALSE(PSI.isFunctionEntryHot(G));
144 EXPECT_FALSE(PSI.isFunctionEntryHot(H));
145}
146
147TEST_F(ProfileSummaryInfoTest, InstrProf) {
148 auto M = makeLLVMModule("InstrProf");
149 Function *F = M->getFunction("f");
150 ProfileSummaryInfo PSI = buildPSI(M.get());
Easwaran Ramandadc0f12017-05-16 20:14:39 +0000151 EXPECT_TRUE(PSI.hasProfileSummary());
152 EXPECT_TRUE(PSI.hasInstrumentationProfile());
Easwaran Ramanb035f912017-01-13 01:34:00 +0000153
154 BasicBlock &BB0 = F->getEntryBlock();
155 BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
156 BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1);
157 BasicBlock *BB3 = BB1->getSingleSuccessor();
158
159 BlockFrequencyInfo BFI = buildBFI(*F);
160 EXPECT_TRUE(PSI.isHotBB(&BB0, &BFI));
161 EXPECT_TRUE(PSI.isHotBB(BB1, &BFI));
162 EXPECT_FALSE(PSI.isHotBB(BB2, &BFI));
163 EXPECT_TRUE(PSI.isHotBB(BB3, &BFI));
164
165 CallSite CS1(BB1->getFirstNonPHI());
166 auto *CI2 = BB2->getFirstNonPHI();
167 CallSite CS2(CI2);
168
169 EXPECT_TRUE(PSI.isHotCallSite(CS1, &BFI));
170 EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI));
Teresa Johnson2a6b7992017-05-11 23:18:05 +0000171
172 // Test that adding an MD_prof metadata with a hot count on CS2 does not
173 // change its hotness as it has no effect in instrumented profiling.
174 MDBuilder MDB(M->getContext());
175 CI2->setMetadata(llvm::LLVMContext::MD_prof, MDB.createBranchWeights({400}));
176 EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI));
Easwaran Ramanb035f912017-01-13 01:34:00 +0000177}
178
179TEST_F(ProfileSummaryInfoTest, SampleProf) {
180 auto M = makeLLVMModule("SampleProfile");
181 Function *F = M->getFunction("f");
182 ProfileSummaryInfo PSI = buildPSI(M.get());
Easwaran Ramandadc0f12017-05-16 20:14:39 +0000183 EXPECT_TRUE(PSI.hasProfileSummary());
184 EXPECT_TRUE(PSI.hasSampleProfile());
Easwaran Ramanb035f912017-01-13 01:34:00 +0000185
186 BasicBlock &BB0 = F->getEntryBlock();
187 BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
188 BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1);
189 BasicBlock *BB3 = BB1->getSingleSuccessor();
190
191 BlockFrequencyInfo BFI = buildBFI(*F);
192 EXPECT_TRUE(PSI.isHotBB(&BB0, &BFI));
193 EXPECT_TRUE(PSI.isHotBB(BB1, &BFI));
194 EXPECT_FALSE(PSI.isHotBB(BB2, &BFI));
195 EXPECT_TRUE(PSI.isHotBB(BB3, &BFI));
196
197 CallSite CS1(BB1->getFirstNonPHI());
198 auto *CI2 = BB2->getFirstNonPHI();
199 CallSite CS2(CI2);
200
201 EXPECT_TRUE(PSI.isHotCallSite(CS1, &BFI));
202 EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI));
203
204 // Test that CS2 is considered hot when it gets an MD_prof metadata with
205 // weights that exceed the hot count threshold.
206 MDBuilder MDB(M->getContext());
207 CI2->setMetadata(llvm::LLVMContext::MD_prof, MDB.createBranchWeights({400}));
208 EXPECT_TRUE(PSI.isHotCallSite(CS2, &BFI));
209}
210
211} // end anonymous namespace
212} // end namespace llvm