blob: 56a8c2ec14db713fec58a7ea754f5dfabcfbae81 [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
10#include "llvm/Analysis/BlockFrequencyInfo.h"
11#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
12#include "llvm/Analysis/BranchProbabilityInfo.h"
13#include "llvm/Analysis/LoopInfo.h"
14#include "llvm/Analysis/ProfileSummaryInfo.h"
15#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());
105 // In the absence of profiles, is{Hot|Cold}X methods should always return
106 // false.
107 EXPECT_FALSE(PSI.isHotCount(1000));
108 EXPECT_FALSE(PSI.isHotCount(0));
109 EXPECT_FALSE(PSI.isColdCount(1000));
110 EXPECT_FALSE(PSI.isColdCount(0));
111
112 EXPECT_FALSE(PSI.isFunctionEntryHot(F));
113 EXPECT_FALSE(PSI.isFunctionEntryCold(F));
114
115 BasicBlock &BB0 = F->getEntryBlock();
116 BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
117
118 BlockFrequencyInfo BFI = buildBFI(*F);
119 EXPECT_FALSE(PSI.isHotBB(&BB0, &BFI));
120 EXPECT_FALSE(PSI.isColdBB(&BB0, &BFI));
121
122 CallSite CS1(BB1->getFirstNonPHI());
123 EXPECT_FALSE(PSI.isHotCallSite(CS1, &BFI));
124 EXPECT_FALSE(PSI.isColdCallSite(CS1, &BFI));
125}
Easwaran Ramanb035f912017-01-13 01:34:00 +0000126TEST_F(ProfileSummaryInfoTest, TestCommon) {
127 auto M = makeLLVMModule("InstrProf");
128 Function *F = M->getFunction("f");
129 Function *G = M->getFunction("g");
130 Function *H = M->getFunction("h");
131
132 ProfileSummaryInfo PSI = buildPSI(M.get());
133 EXPECT_TRUE(PSI.isHotCount(400));
134 EXPECT_TRUE(PSI.isColdCount(2));
135 EXPECT_FALSE(PSI.isColdCount(100));
136 EXPECT_FALSE(PSI.isHotCount(100));
137
138 EXPECT_TRUE(PSI.isFunctionEntryHot(F));
139 EXPECT_FALSE(PSI.isFunctionEntryHot(G));
140 EXPECT_FALSE(PSI.isFunctionEntryHot(H));
141}
142
143TEST_F(ProfileSummaryInfoTest, InstrProf) {
144 auto M = makeLLVMModule("InstrProf");
145 Function *F = M->getFunction("f");
146 ProfileSummaryInfo PSI = buildPSI(M.get());
147
148 BasicBlock &BB0 = F->getEntryBlock();
149 BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
150 BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1);
151 BasicBlock *BB3 = BB1->getSingleSuccessor();
152
153 BlockFrequencyInfo BFI = buildBFI(*F);
154 EXPECT_TRUE(PSI.isHotBB(&BB0, &BFI));
155 EXPECT_TRUE(PSI.isHotBB(BB1, &BFI));
156 EXPECT_FALSE(PSI.isHotBB(BB2, &BFI));
157 EXPECT_TRUE(PSI.isHotBB(BB3, &BFI));
158
159 CallSite CS1(BB1->getFirstNonPHI());
160 auto *CI2 = BB2->getFirstNonPHI();
161 CallSite CS2(CI2);
162
163 EXPECT_TRUE(PSI.isHotCallSite(CS1, &BFI));
164 EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI));
165
166 // Test that adding an MD_prof metadata with a hot count on CS2 does not
167 // change itas hotness as it has no effect in instrumented profiling.
168 MDBuilder MDB(M->getContext());
169 CI2->setMetadata(llvm::LLVMContext::MD_prof, MDB.createBranchWeights({400}));
170 EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI));
171}
172
173TEST_F(ProfileSummaryInfoTest, SampleProf) {
174 auto M = makeLLVMModule("SampleProfile");
175 Function *F = M->getFunction("f");
176 ProfileSummaryInfo PSI = buildPSI(M.get());
177
178 BasicBlock &BB0 = F->getEntryBlock();
179 BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
180 BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1);
181 BasicBlock *BB3 = BB1->getSingleSuccessor();
182
183 BlockFrequencyInfo BFI = buildBFI(*F);
184 EXPECT_TRUE(PSI.isHotBB(&BB0, &BFI));
185 EXPECT_TRUE(PSI.isHotBB(BB1, &BFI));
186 EXPECT_FALSE(PSI.isHotBB(BB2, &BFI));
187 EXPECT_TRUE(PSI.isHotBB(BB3, &BFI));
188
189 CallSite CS1(BB1->getFirstNonPHI());
190 auto *CI2 = BB2->getFirstNonPHI();
191 CallSite CS2(CI2);
192
193 EXPECT_TRUE(PSI.isHotCallSite(CS1, &BFI));
194 EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI));
195
196 // Test that CS2 is considered hot when it gets an MD_prof metadata with
197 // weights that exceed the hot count threshold.
198 MDBuilder MDB(M->getContext());
199 CI2->setMetadata(llvm::LLVMContext::MD_prof, MDB.createBranchWeights({400}));
200 EXPECT_TRUE(PSI.isHotCallSite(CS2, &BFI));
201}
202
203} // end anonymous namespace
204} // end namespace llvm