• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   Freq = BlockFrequency(1);
18   Freq *= Prob;
19   EXPECT_EQ(Freq.getFrequency(), 0u);
20 }
21 
TEST(BlockFrequencyTest,OneToOne)22 TEST(BlockFrequencyTest, OneToOne) {
23   BlockFrequency Freq(1);
24   BranchProbability Prob(UINT32_MAX, UINT32_MAX);
25   Freq *= Prob;
26   EXPECT_EQ(Freq.getFrequency(), 1u);
27 
28   Freq = BlockFrequency(1);
29   Freq *= Prob;
30   EXPECT_EQ(Freq.getFrequency(), 1u);
31 }
32 
TEST(BlockFrequencyTest,ThreeToOne)33 TEST(BlockFrequencyTest, ThreeToOne) {
34   BlockFrequency Freq(3);
35   BranchProbability Prob(3000000, 9000000);
36   Freq *= Prob;
37   EXPECT_EQ(Freq.getFrequency(), 1u);
38 
39   Freq = BlockFrequency(3);
40   Freq *= Prob;
41   EXPECT_EQ(Freq.getFrequency(), 1u);
42 }
43 
TEST(BlockFrequencyTest,MaxToHalfMax)44 TEST(BlockFrequencyTest, MaxToHalfMax) {
45   BlockFrequency Freq(UINT64_MAX);
46   BranchProbability Prob(UINT32_MAX / 2, UINT32_MAX);
47   Freq *= Prob;
48   EXPECT_EQ(Freq.getFrequency(), 9223372034707292159ULL);
49 
50   Freq = BlockFrequency(UINT64_MAX);
51   Freq *= Prob;
52   EXPECT_EQ(Freq.getFrequency(), 9223372034707292159ULL);
53 }
54 
TEST(BlockFrequencyTest,BigToBig)55 TEST(BlockFrequencyTest, BigToBig) {
56   const uint64_t Big = 387246523487234346LL;
57   const uint32_t P = 123456789;
58   BlockFrequency Freq(Big);
59   BranchProbability Prob(P, P);
60   Freq *= Prob;
61   EXPECT_EQ(Freq.getFrequency(), Big);
62 
63   Freq = BlockFrequency(Big);
64   Freq *= Prob;
65   EXPECT_EQ(Freq.getFrequency(), Big);
66 }
67 
TEST(BlockFrequencyTest,MaxToMax)68 TEST(BlockFrequencyTest, MaxToMax) {
69   BlockFrequency Freq(UINT64_MAX);
70   BranchProbability Prob(UINT32_MAX, UINT32_MAX);
71   Freq *= Prob;
72   EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
73 
74   // This additionally makes sure if we have a value equal to our saturating
75   // value, we do not signal saturation if the result equals said value, but
76   // saturating does not occur.
77   Freq = BlockFrequency(UINT64_MAX);
78   Freq *= Prob;
79   EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
80 }
81 
TEST(BlockFrequency,Divide)82 TEST(BlockFrequency, Divide) {
83   BlockFrequency Freq(0x3333333333333333ULL);
84   Freq /= BranchProbability(1, 2);
85   EXPECT_EQ(Freq.getFrequency(), 0x6666666666666666ULL);
86 }
87 
TEST(BlockFrequencyTest,Saturate)88 TEST(BlockFrequencyTest, Saturate) {
89   BlockFrequency Freq(0x3333333333333333ULL);
90   Freq /= BranchProbability(100, 300);
91   EXPECT_EQ(Freq.getFrequency(), 0x9999999999999999ULL);
92   Freq /= BranchProbability(1, 2);
93   EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
94 
95   Freq = 0x1000000000000000ULL;
96   Freq /= BranchProbability(10000, 160000);
97   EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
98 
99   // Try to cheat the multiplication overflow check.
100   Freq = 0x00000001f0000001ull;
101   Freq /= BranchProbability(1000, 0xf000000f);
102   EXPECT_EQ(33506781356485509ULL, Freq.getFrequency());
103 }
104 
TEST(BlockFrequencyTest,SaturatingRightShift)105 TEST(BlockFrequencyTest, SaturatingRightShift) {
106   BlockFrequency Freq(0x10080ULL);
107   Freq >>= 2;
108   EXPECT_EQ(Freq.getFrequency(), 0x4020ULL);
109   Freq >>= 20;
110   EXPECT_EQ(Freq.getFrequency(), 0x1ULL);
111 }
112 
113 }
114