blob: cc053066a26906abfb8c033422bfc693d818b970 [file] [log] [blame]
Easwaran Ramane34db462016-03-23 18:18:26 +00001//===- BlockFrequencyInfoTest.cpp - BlockFrequencyInfo 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"
Easwaran Ramane34db462016-03-23 18:18:26 +000011#include "llvm/Analysis/BranchProbabilityInfo.h"
12#include "llvm/Analysis/LoopInfo.h"
13#include "llvm/AsmParser/Parser.h"
14#include "llvm/IR/BasicBlock.h"
15#include "llvm/IR/Dominators.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/LLVMContext.h"
18#include "llvm/IR/Module.h"
19#include "llvm/Support/DataTypes.h"
20#include "llvm/Support/SourceMgr.h"
21#include "llvm/Support/raw_ostream.h"
22#include "gtest/gtest.h"
23
24namespace llvm {
25namespace {
26
27class BlockFrequencyInfoTest : public testing::Test {
28protected:
29 std::unique_ptr<BranchProbabilityInfo> BPI;
30 std::unique_ptr<DominatorTree> DT;
31 std::unique_ptr<LoopInfo> LI;
Mehdi Amini8be77072016-04-14 21:59:01 +000032 LLVMContext C;
Easwaran Ramane34db462016-03-23 18:18:26 +000033
34 BlockFrequencyInfo buildBFI(Function &F) {
35 DT.reset(new DominatorTree(F));
36 LI.reset(new LoopInfo(*DT));
37 BPI.reset(new BranchProbabilityInfo(F, *LI));
38 return BlockFrequencyInfo(F, *BPI, *LI);
39 }
40 std::unique_ptr<Module> makeLLVMModule() {
41 const char *ModuleStrig = "define i32 @f(i32 %x) {\n"
42 "bb0:\n"
43 " %y1 = icmp eq i32 %x, 0 \n"
44 " br i1 %y1, label %bb1, label %bb2 \n"
45 "bb1:\n"
46 " br label %bb3\n"
47 "bb2:\n"
48 " br label %bb3\n"
49 "bb3:\n"
50 " %y2 = phi i32 [0, %bb1], [1, %bb2] \n"
51 " ret i32 %y2\n"
52 "}\n";
Easwaran Ramane34db462016-03-23 18:18:26 +000053 SMDiagnostic Err;
54 return parseAssemblyString(ModuleStrig, Err, C);
55 }
56};
57
58TEST_F(BlockFrequencyInfoTest, Basic) {
59 auto M = makeLLVMModule();
60 Function *F = M->getFunction("f");
61 F->setEntryCount(100);
62
63 BlockFrequencyInfo BFI = buildBFI(*F);
64 BasicBlock &BB0 = F->getEntryBlock();
65 BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
66 BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1);
67 BasicBlock *BB3 = BB1->getSingleSuccessor();
68
69 uint64_t BB0Freq = BFI.getBlockFreq(&BB0).getFrequency();
70 uint64_t BB1Freq = BFI.getBlockFreq(BB1).getFrequency();
71 uint64_t BB2Freq = BFI.getBlockFreq(BB2).getFrequency();
72 uint64_t BB3Freq = BFI.getBlockFreq(BB3).getFrequency();
73
74 EXPECT_EQ(BB0Freq, BB3Freq);
75 EXPECT_EQ(BB0Freq, BB1Freq + BB2Freq);
76 EXPECT_EQ(BB0Freq, BB3Freq);
77
78 EXPECT_EQ(BFI.getBlockProfileCount(&BB0).getValue(), UINT64_C(100));
79 EXPECT_EQ(BFI.getBlockProfileCount(BB3).getValue(), UINT64_C(100));
Easwaran Raman21f571c2018-08-16 00:26:59 +000080 EXPECT_EQ(BFI.getBlockProfileCount(BB1).getValue(),
81 (100 * BB1Freq + BB0Freq / 2) / BB0Freq);
82 EXPECT_EQ(BFI.getBlockProfileCount(BB2).getValue(),
83 (100 * BB2Freq + BB0Freq / 2) / BB0Freq);
Easwaran Ramand91b01b2017-01-19 18:53:16 +000084
85 // Scale the frequencies of BB0, BB1 and BB2 by a factor of two.
86 SmallPtrSet<BasicBlock *, 4> BlocksToScale({BB1, BB2});
87 BFI.setBlockFreqAndScale(&BB0, BB0Freq * 2, BlocksToScale);
88 EXPECT_EQ(BFI.getBlockFreq(&BB0).getFrequency(), 2 * BB0Freq);
89 EXPECT_EQ(BFI.getBlockFreq(BB1).getFrequency(), 2 * BB1Freq);
90 EXPECT_EQ(BFI.getBlockFreq(BB2).getFrequency(), 2 * BB2Freq);
91 EXPECT_EQ(BFI.getBlockFreq(BB3).getFrequency(), BB3Freq);
Easwaran Ramane34db462016-03-23 18:18:26 +000092}
93
94} // end anonymous namespace
95} // end namespace llvm