1 //===- unittests/Support/BlockFrequencyTest.cpp - BlockFrequency tests ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/BlockFrequency.h"
11 #include "llvm/Support/BranchProbability.h"
12 #include "llvm/Support/DataTypes.h"
13 #include "gtest/gtest.h"
20 TEST(BlockFrequencyTest, OneToZero) {
21 BlockFrequency Freq(1);
22 BranchProbability Prob(UINT32_MAX / 3, UINT32_MAX);
24 EXPECT_EQ(Freq.getFrequency(), 0u);
26 Freq = BlockFrequency(1);
28 EXPECT_EQ(Freq.getFrequency(), 0u);
31 TEST(BlockFrequencyTest, OneToOne) {
32 BlockFrequency Freq(1);
33 BranchProbability Prob(UINT32_MAX, UINT32_MAX);
35 EXPECT_EQ(Freq.getFrequency(), 1u);
37 Freq = BlockFrequency(1);
39 EXPECT_EQ(Freq.getFrequency(), 1u);
42 TEST(BlockFrequencyTest, ThreeToOne) {
43 BlockFrequency Freq(3);
44 BranchProbability Prob(3000000, 9000000);
46 EXPECT_EQ(Freq.getFrequency(), 1u);
48 Freq = BlockFrequency(3);
50 EXPECT_EQ(Freq.getFrequency(), 1u);
53 TEST(BlockFrequencyTest, MaxToHalfMax) {
54 BlockFrequency Freq(UINT64_MAX);
55 BranchProbability Prob(UINT32_MAX / 2, UINT32_MAX);
57 EXPECT_EQ(Freq.getFrequency(), 9223372036854775807ULL);
59 Freq = BlockFrequency(UINT64_MAX);
61 EXPECT_EQ(Freq.getFrequency(), 9223372036854775807ULL);
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);
70 EXPECT_EQ(Freq.getFrequency(), Big);
72 Freq = BlockFrequency(Big);
74 EXPECT_EQ(Freq.getFrequency(), Big);
77 TEST(BlockFrequencyTest, MaxToMax) {
78 BlockFrequency Freq(UINT64_MAX);
79 BranchProbability Prob(UINT32_MAX, UINT32_MAX);
81 EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
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);
88 EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
91 TEST(BlockFrequency, Divide) {
92 BlockFrequency Freq(0x3333333333333333ULL);
93 Freq /= BranchProbability(1, 2);
94 EXPECT_EQ(Freq.getFrequency(), 0x6666666666666666ULL);
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);
104 Freq = 0x1000000000000000ULL;
105 Freq /= BranchProbability(10000, 170000);
106 EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
108 // Try to cheat the multiplication overflow check.
109 Freq = 0x00000001f0000001ull;
110 Freq /= BranchProbability(1000, 0xf000000f);
111 EXPECT_EQ(33527736066704712ULL, Freq.getFrequency());
114 TEST(BlockFrequencyTest, SaturatingRightShift) {
115 BlockFrequency Freq(0x10080ULL);
117 EXPECT_EQ(Freq.getFrequency(), 0x4020ULL);
119 EXPECT_EQ(Freq.getFrequency(), 0x1ULL);