[mips] Whitespace cleanup in MIPS16 tests to reduce noise in following changes. NFC.
[oota-llvm.git] / unittests / Support / BlockFrequencyTest.cpp
1 //===- unittests/Support/BlockFrequencyTest.cpp - BlockFrequency 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/Support/BlockFrequency.h"
11 #include "llvm/Support/BranchProbability.h"
12 #include "llvm/Support/DataTypes.h"
13 #include "gtest/gtest.h"
14 #include <climits>
15
16 using namespace llvm;
17
18 namespace {
19
20 TEST(BlockFrequencyTest, OneToZero) {
21   BlockFrequency Freq(1);
22   BranchProbability Prob(UINT32_MAX / 3, UINT32_MAX);
23   Freq *= Prob;
24   EXPECT_EQ(Freq.getFrequency(), 0u);
25
26   Freq = BlockFrequency(1);
27   Freq *= Prob;
28   EXPECT_EQ(Freq.getFrequency(), 0u);
29 }
30
31 TEST(BlockFrequencyTest, OneToOne) {
32   BlockFrequency Freq(1);
33   BranchProbability Prob(UINT32_MAX, UINT32_MAX);
34   Freq *= Prob;
35   EXPECT_EQ(Freq.getFrequency(), 1u);
36
37   Freq = BlockFrequency(1);
38   Freq *= Prob;
39   EXPECT_EQ(Freq.getFrequency(), 1u);
40 }
41
42 TEST(BlockFrequencyTest, ThreeToOne) {
43   BlockFrequency Freq(3);
44   BranchProbability Prob(3000000, 9000000);
45   Freq *= Prob;
46   EXPECT_EQ(Freq.getFrequency(), 1u);
47
48   Freq = BlockFrequency(3);
49   Freq *= Prob;
50   EXPECT_EQ(Freq.getFrequency(), 1u);
51 }
52
53 TEST(BlockFrequencyTest, MaxToHalfMax) {
54   BlockFrequency Freq(UINT64_MAX);
55   BranchProbability Prob(UINT32_MAX / 2, UINT32_MAX);
56   Freq *= Prob;
57   EXPECT_EQ(Freq.getFrequency(), 9223372036854775807ULL);
58
59   Freq = BlockFrequency(UINT64_MAX);
60   Freq *= Prob;
61   EXPECT_EQ(Freq.getFrequency(), 9223372036854775807ULL);
62 }
63
64 TEST(BlockFrequencyTest, BigToBig) {
65   const uint64_t Big = 387246523487234346LL;
66   const uint32_t P = 123456789;
67   BlockFrequency Freq(Big);
68   BranchProbability Prob(P, P);
69   Freq *= Prob;
70   EXPECT_EQ(Freq.getFrequency(), Big);
71
72   Freq = BlockFrequency(Big);
73   Freq *= Prob;
74   EXPECT_EQ(Freq.getFrequency(), Big);
75 }
76
77 TEST(BlockFrequencyTest, MaxToMax) {
78   BlockFrequency Freq(UINT64_MAX);
79   BranchProbability Prob(UINT32_MAX, UINT32_MAX);
80   Freq *= Prob;
81   EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
82
83   // This additionally makes sure if we have a value equal to our saturating
84   // value, we do not signal saturation if the result equals said value, but
85   // saturating does not occur.
86   Freq = BlockFrequency(UINT64_MAX);
87   Freq *= Prob;
88   EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
89 }
90
91 TEST(BlockFrequency, Divide) {
92   BlockFrequency Freq(0x3333333333333333ULL);
93   Freq /= BranchProbability(1, 2);
94   EXPECT_EQ(Freq.getFrequency(), 0x6666666666666666ULL);
95 }
96
97 TEST(BlockFrequencyTest, Saturate) {
98   BlockFrequency Freq(0x3333333333333333ULL);
99   Freq /= BranchProbability(100, 300);
100   EXPECT_EQ(Freq.getFrequency(), 0x9999999866666668ULL);
101   Freq /= BranchProbability(1, 2);
102   EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
103
104   Freq = 0x1000000000000000ULL;
105   Freq /= BranchProbability(10000, 170000);
106   EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
107
108   // Try to cheat the multiplication overflow check.
109   Freq = 0x00000001f0000001ull;
110   Freq /= BranchProbability(1000, 0xf000000f);
111   EXPECT_EQ(33527736066704712ULL, Freq.getFrequency());
112 }
113
114 TEST(BlockFrequencyTest, SaturatingRightShift) {
115   BlockFrequency Freq(0x10080ULL);
116   Freq >>= 2;
117   EXPECT_EQ(Freq.getFrequency(), 0x4020ULL);
118   Freq >>= 20;
119   EXPECT_EQ(Freq.getFrequency(), 0x1ULL);
120 }
121
122 }