1 #include "llvm/Support/BlockFrequency.h"
2 #include "llvm/Support/BranchProbability.h"
3 #include "llvm/Support/DataTypes.h"
4 #include "gtest/gtest.h"
5 #include <climits>
6
7 using namespace llvm;
8
9 namespace {
10
TEST(BlockFrequencyTest,OneToZero)11 TEST(BlockFrequencyTest, OneToZero) {
12 BlockFrequency Freq(1);
13 BranchProbability Prob(UINT32_MAX - 1, UINT32_MAX);
14 Freq *= Prob;
15 EXPECT_EQ(Freq.getFrequency(), 0u);
16 }
17
TEST(BlockFrequencyTest,OneToOne)18 TEST(BlockFrequencyTest, OneToOne) {
19 BlockFrequency Freq(1);
20 BranchProbability Prob(UINT32_MAX, UINT32_MAX);
21 Freq *= Prob;
22 EXPECT_EQ(Freq.getFrequency(), 1u);
23 }
24
TEST(BlockFrequencyTest,ThreeToOne)25 TEST(BlockFrequencyTest, ThreeToOne) {
26 BlockFrequency Freq(3);
27 BranchProbability Prob(3000000, 9000000);
28 Freq *= Prob;
29 EXPECT_EQ(Freq.getFrequency(), 1u);
30 }
31
TEST(BlockFrequencyTest,MaxToHalfMax)32 TEST(BlockFrequencyTest, MaxToHalfMax) {
33 BlockFrequency Freq(UINT64_MAX);
34 BranchProbability Prob(UINT32_MAX / 2, UINT32_MAX);
35 Freq *= Prob;
36 EXPECT_EQ(Freq.getFrequency(), 9223372034707292159ULL);
37 }
38
TEST(BlockFrequencyTest,BigToBig)39 TEST(BlockFrequencyTest, BigToBig) {
40 const uint64_t Big = 387246523487234346LL;
41 const uint32_t P = 123456789;
42 BlockFrequency Freq(Big);
43 BranchProbability Prob(P, P);
44 Freq *= Prob;
45 EXPECT_EQ(Freq.getFrequency(), Big);
46 }
47
TEST(BlockFrequencyTest,MaxToMax)48 TEST(BlockFrequencyTest, MaxToMax) {
49 BlockFrequency Freq(UINT64_MAX);
50 BranchProbability Prob(UINT32_MAX, UINT32_MAX);
51 Freq *= Prob;
52 EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
53 }
54
TEST(BlockFrequency,Divide)55 TEST(BlockFrequency, Divide) {
56 BlockFrequency Freq(0x3333333333333333ULL);
57 Freq /= BranchProbability(1, 2);
58 EXPECT_EQ(Freq.getFrequency(), 0x6666666666666666ULL);
59 }
60
TEST(BlockFrequencyTest,Saturate)61 TEST(BlockFrequencyTest, Saturate) {
62 BlockFrequency Freq(0x3333333333333333ULL);
63 Freq /= BranchProbability(100, 300);
64 EXPECT_EQ(Freq.getFrequency(), 0x9999999999999999ULL);
65 Freq /= BranchProbability(1, 2);
66 EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
67
68 Freq = 0x1000000000000000ULL;
69 Freq /= BranchProbability(10000, 160000);
70 EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
71
72 // Try to cheat the multiplication overflow check.
73 Freq = 0x00000001f0000001ull;
74 Freq /= BranchProbability(1000, 0xf000000f);
75 EXPECT_EQ(33506781356485509ULL, Freq.getFrequency());
76 }
77
TEST(BlockFrequencyTest,ProbabilityCompare)78 TEST(BlockFrequencyTest, ProbabilityCompare) {
79 BranchProbability A(4, 5);
80 BranchProbability B(4U << 29, 5U << 29);
81 BranchProbability C(3, 4);
82
83 EXPECT_TRUE(A == B);
84 EXPECT_FALSE(A != B);
85 EXPECT_FALSE(A < B);
86 EXPECT_FALSE(A > B);
87 EXPECT_TRUE(A <= B);
88 EXPECT_TRUE(A >= B);
89
90 EXPECT_FALSE(B == C);
91 EXPECT_TRUE(B != C);
92 EXPECT_FALSE(B < C);
93 EXPECT_TRUE(B > C);
94 EXPECT_FALSE(B <= C);
95 EXPECT_TRUE(B >= C);
96
97 BranchProbability BigZero(0, UINT32_MAX);
98 BranchProbability BigOne(UINT32_MAX, UINT32_MAX);
99 EXPECT_FALSE(BigZero == BigOne);
100 EXPECT_TRUE(BigZero != BigOne);
101 EXPECT_TRUE(BigZero < BigOne);
102 EXPECT_FALSE(BigZero > BigOne);
103 EXPECT_TRUE(BigZero <= BigOne);
104 EXPECT_FALSE(BigZero >= BigOne);
105 }
106
107 }
108