1 #include "llvm/Support/DataTypes.h"
2 #include "llvm/Support/BlockFrequency.h"
3 #include "llvm/Support/BranchProbability.h"
4
5 #include "gtest/gtest.h"
6 #include <climits>
7
8 using namespace llvm;
9
10 namespace {
11
TEST(BlockFrequencyTest,OneToZero)12 TEST(BlockFrequencyTest, OneToZero) {
13 BlockFrequency Freq(1);
14 BranchProbability Prob(UINT32_MAX - 1, UINT32_MAX);
15 Freq *= Prob;
16 EXPECT_EQ(Freq.getFrequency(), 0u);
17 }
18
TEST(BlockFrequencyTest,OneToOne)19 TEST(BlockFrequencyTest, OneToOne) {
20 BlockFrequency Freq(1);
21 BranchProbability Prob(UINT32_MAX, UINT32_MAX);
22 Freq *= Prob;
23 EXPECT_EQ(Freq.getFrequency(), 1u);
24 }
25
TEST(BlockFrequencyTest,ThreeToOne)26 TEST(BlockFrequencyTest, ThreeToOne) {
27 BlockFrequency Freq(3);
28 BranchProbability Prob(3000000, 9000000);
29 Freq *= Prob;
30 EXPECT_EQ(Freq.getFrequency(), 1u);
31 }
32
TEST(BlockFrequencyTest,MaxToHalfMax)33 TEST(BlockFrequencyTest, MaxToHalfMax) {
34 BlockFrequency Freq(UINT64_MAX);
35 BranchProbability Prob(UINT32_MAX / 2, UINT32_MAX);
36 Freq *= Prob;
37 EXPECT_EQ(Freq.getFrequency(), 9223372034707292159ULL);
38 }
39
TEST(BlockFrequencyTest,BigToBig)40 TEST(BlockFrequencyTest, BigToBig) {
41 const uint64_t Big = 387246523487234346LL;
42 const uint32_t P = 123456789;
43 BlockFrequency Freq(Big);
44 BranchProbability Prob(P, P);
45 Freq *= Prob;
46 EXPECT_EQ(Freq.getFrequency(), Big);
47 }
48
TEST(BlockFrequencyTest,MaxToMax)49 TEST(BlockFrequencyTest, MaxToMax) {
50 BlockFrequency Freq(UINT64_MAX);
51 BranchProbability Prob(UINT32_MAX, UINT32_MAX);
52 Freq *= Prob;
53 EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
54 }
55
TEST(BlockFrequencyTest,ProbabilityCompare)56 TEST(BlockFrequencyTest, ProbabilityCompare) {
57 BranchProbability A(4, 5);
58 BranchProbability B(4U << 29, 5U << 29);
59 BranchProbability C(3, 4);
60
61 EXPECT_TRUE(A == B);
62 EXPECT_FALSE(A != B);
63 EXPECT_FALSE(A < B);
64 EXPECT_FALSE(A > B);
65 EXPECT_TRUE(A <= B);
66 EXPECT_TRUE(A >= B);
67
68 EXPECT_FALSE(B == C);
69 EXPECT_TRUE(B != C);
70 EXPECT_FALSE(B < C);
71 EXPECT_TRUE(B > C);
72 EXPECT_FALSE(B <= C);
73 EXPECT_TRUE(B >= C);
74
75 BranchProbability BigZero(0, UINT32_MAX);
76 BranchProbability BigOne(UINT32_MAX, UINT32_MAX);
77 EXPECT_FALSE(BigZero == BigOne);
78 EXPECT_TRUE(BigZero != BigOne);
79 EXPECT_TRUE(BigZero < BigOne);
80 EXPECT_FALSE(BigZero > BigOne);
81 EXPECT_TRUE(BigZero <= BigOne);
82 EXPECT_FALSE(BigZero >= BigOne);
83 }
84
85 }
86