summaryrefslogtreecommitdiff
path: root/unittests/Analysis/BlockFrequencyInfoTest.cpp
blob: b3b0fcfb049bf3b6ed63f68e15f03ae6e407d65f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//===- BlockFrequencyInfoTest.cpp - BlockFrequencyInfo unit tests ---------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"

namespace llvm {
namespace {

class BlockFrequencyInfoTest : public testing::Test {
protected:
  std::unique_ptr<BranchProbabilityInfo> BPI;
  std::unique_ptr<DominatorTree> DT;
  std::unique_ptr<LoopInfo> LI;
  LLVMContext C;

  BlockFrequencyInfo buildBFI(Function &F) {
    DT.reset(new DominatorTree(F));
    LI.reset(new LoopInfo(*DT));
    BPI.reset(new BranchProbabilityInfo(F, *LI));
    return BlockFrequencyInfo(F, *BPI, *LI);
  }
  std::unique_ptr<Module> makeLLVMModule() {
    const char *ModuleStrig = "define i32 @f(i32 %x) {\n"
                              "bb0:\n"
                              "  %y1 = icmp eq i32 %x, 0 \n"
                              "  br i1 %y1, label %bb1, label %bb2 \n"
                              "bb1:\n"
                              "  br label %bb3\n"
                              "bb2:\n"
                              "  br label %bb3\n"
                              "bb3:\n"
                              "  %y2 = phi i32 [0, %bb1], [1, %bb2] \n"
                              "  ret i32 %y2\n"
                              "}\n";
    SMDiagnostic Err;
    return parseAssemblyString(ModuleStrig, Err, C);
  }
};

TEST_F(BlockFrequencyInfoTest, Basic) {
  auto M = makeLLVMModule();
  Function *F = M->getFunction("f");
  F->setEntryCount(100);

  BlockFrequencyInfo BFI = buildBFI(*F);
  BasicBlock &BB0 = F->getEntryBlock();
  BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
  BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1);
  BasicBlock *BB3 = BB1->getSingleSuccessor();

  uint64_t BB0Freq = BFI.getBlockFreq(&BB0).getFrequency();
  uint64_t BB1Freq = BFI.getBlockFreq(BB1).getFrequency();
  uint64_t BB2Freq = BFI.getBlockFreq(BB2).getFrequency();
  uint64_t BB3Freq = BFI.getBlockFreq(BB3).getFrequency();

  EXPECT_EQ(BB0Freq, BB3Freq);
  EXPECT_EQ(BB0Freq, BB1Freq + BB2Freq);
  EXPECT_EQ(BB0Freq, BB3Freq);

  EXPECT_EQ(BFI.getBlockProfileCount(&BB0).getValue(), UINT64_C(100));
  EXPECT_EQ(BFI.getBlockProfileCount(BB3).getValue(), UINT64_C(100));
  EXPECT_EQ(BFI.getBlockProfileCount(BB1).getValue(), 100 * BB1Freq / BB0Freq);
  EXPECT_EQ(BFI.getBlockProfileCount(BB2).getValue(), 100 * BB2Freq / BB0Freq);
}

} // end anonymous namespace
} // end namespace llvm