1 //===- unittests/Support/BlockFrequencyTest.cpp - BlockFrequency tests ----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/BlockFrequency.h"
10 #include "llvm/Support/BranchProbability.h"
11 #include "llvm/Support/DataTypes.h"
12 #include "gtest/gtest.h"
19 TEST(BlockFrequencyTest
, OneToZero
) {
20 BlockFrequency
Freq(1);
21 BranchProbability
Prob(UINT32_MAX
/ 3, UINT32_MAX
);
23 EXPECT_EQ(Freq
.getFrequency(), 0u);
25 Freq
= BlockFrequency(1);
27 EXPECT_EQ(Freq
.getFrequency(), 0u);
30 TEST(BlockFrequencyTest
, OneToOne
) {
31 BlockFrequency
Freq(1);
32 BranchProbability
Prob(UINT32_MAX
, UINT32_MAX
);
34 EXPECT_EQ(Freq
.getFrequency(), 1u);
36 Freq
= BlockFrequency(1);
38 EXPECT_EQ(Freq
.getFrequency(), 1u);
41 TEST(BlockFrequencyTest
, ThreeToOne
) {
42 BlockFrequency
Freq(3);
43 BranchProbability
Prob(3000000, 9000000);
45 EXPECT_EQ(Freq
.getFrequency(), 1u);
47 Freq
= BlockFrequency(3);
49 EXPECT_EQ(Freq
.getFrequency(), 1u);
52 TEST(BlockFrequencyTest
, MaxToHalfMax
) {
53 BlockFrequency
Freq(UINT64_MAX
);
54 BranchProbability
Prob(UINT32_MAX
/ 2, UINT32_MAX
);
56 EXPECT_EQ(Freq
.getFrequency(), 9223372036854775807ULL);
58 Freq
= BlockFrequency(UINT64_MAX
);
60 EXPECT_EQ(Freq
.getFrequency(), 9223372036854775807ULL);
63 TEST(BlockFrequencyTest
, BigToBig
) {
64 const uint64_t Big
= 387246523487234346LL;
65 const uint32_t P
= 123456789;
66 BlockFrequency
Freq(Big
);
67 BranchProbability
Prob(P
, P
);
69 EXPECT_EQ(Freq
.getFrequency(), Big
);
71 Freq
= BlockFrequency(Big
);
73 EXPECT_EQ(Freq
.getFrequency(), Big
);
76 TEST(BlockFrequencyTest
, MaxToMax
) {
77 BlockFrequency
Freq(UINT64_MAX
);
78 BranchProbability
Prob(UINT32_MAX
, UINT32_MAX
);
80 EXPECT_EQ(Freq
.getFrequency(), UINT64_MAX
);
82 // This additionally makes sure if we have a value equal to our saturating
83 // value, we do not signal saturation if the result equals said value, but
84 // saturating does not occur.
85 Freq
= BlockFrequency(UINT64_MAX
);
87 EXPECT_EQ(Freq
.getFrequency(), UINT64_MAX
);
90 TEST(BlockFrequencyTest
, Subtract
) {
91 BlockFrequency
Freq1(0), Freq2(1);
92 EXPECT_EQ((Freq1
- Freq2
).getFrequency(), 0u);
93 EXPECT_EQ((Freq2
- Freq1
).getFrequency(), 1u);
96 TEST(BlockFrequency
, Divide
) {
97 BlockFrequency
Freq(0x3333333333333333ULL
);
98 Freq
/= BranchProbability(1, 2);
99 EXPECT_EQ(Freq
.getFrequency(), 0x6666666666666666ULL
);
102 TEST(BlockFrequencyTest
, Saturate
) {
103 BlockFrequency
Freq(0x3333333333333333ULL
);
104 Freq
/= BranchProbability(100, 300);
105 EXPECT_EQ(Freq
.getFrequency(), 0x9999999866666668ULL
);
106 Freq
/= BranchProbability(1, 2);
107 EXPECT_EQ(Freq
.getFrequency(), UINT64_MAX
);
109 Freq
= 0x1000000000000000ULL
;
110 Freq
/= BranchProbability(10000, 170000);
111 EXPECT_EQ(Freq
.getFrequency(), UINT64_MAX
);
113 // Try to cheat the multiplication overflow check.
114 Freq
= 0x00000001f0000001ull
;
115 Freq
/= BranchProbability(1000, 0xf000000f);
116 EXPECT_EQ(33527736066704712ULL, Freq
.getFrequency());
119 TEST(BlockFrequencyTest
, SaturatingRightShift
) {
120 BlockFrequency
Freq(0x10080ULL
);
122 EXPECT_EQ(Freq
.getFrequency(), 0x4020ULL
);
124 EXPECT_EQ(Freq
.getFrequency(), 0x1ULL
);