2015-02-02 19:18:07 +01:00
|
|
|
//===- unittests/Support/BlockFrequencyTest.cpp - BlockFrequency tests ----===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-02-02 19:18:07 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-27 20:57:40 +02:00
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
|
|
|
#include "llvm/Support/BranchProbability.h"
|
2012-12-04 11:23:08 +01:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2011-07-27 20:57:40 +02:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <climits>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(BlockFrequencyTest, OneToZero) {
|
|
|
|
BlockFrequency Freq(1);
|
2015-09-26 01:09:59 +02:00
|
|
|
BranchProbability Prob(UINT32_MAX / 3, UINT32_MAX);
|
2011-07-27 20:57:40 +02:00
|
|
|
Freq *= Prob;
|
2013-06-21 22:20:27 +02:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 0u);
|
2013-11-17 04:25:24 +01:00
|
|
|
|
|
|
|
Freq = BlockFrequency(1);
|
2014-04-29 18:31:29 +02:00
|
|
|
Freq *= Prob;
|
2013-11-17 04:25:24 +01:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 0u);
|
2011-07-27 20:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BlockFrequencyTest, OneToOne) {
|
|
|
|
BlockFrequency Freq(1);
|
|
|
|
BranchProbability Prob(UINT32_MAX, UINT32_MAX);
|
|
|
|
Freq *= Prob;
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
2013-11-17 04:25:24 +01:00
|
|
|
|
|
|
|
Freq = BlockFrequency(1);
|
2014-04-29 18:31:29 +02:00
|
|
|
Freq *= Prob;
|
2013-11-17 04:25:24 +01:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
2011-07-27 20:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BlockFrequencyTest, ThreeToOne) {
|
|
|
|
BlockFrequency Freq(3);
|
|
|
|
BranchProbability Prob(3000000, 9000000);
|
|
|
|
Freq *= Prob;
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
2013-11-17 04:25:24 +01:00
|
|
|
|
|
|
|
Freq = BlockFrequency(3);
|
2014-04-29 18:31:29 +02:00
|
|
|
Freq *= Prob;
|
2013-11-17 04:25:24 +01:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
2011-07-27 20:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BlockFrequencyTest, MaxToHalfMax) {
|
|
|
|
BlockFrequency Freq(UINT64_MAX);
|
|
|
|
BranchProbability Prob(UINT32_MAX / 2, UINT32_MAX);
|
|
|
|
Freq *= Prob;
|
2015-09-26 01:09:59 +02:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 9223372036854775807ULL);
|
2013-11-17 04:25:24 +01:00
|
|
|
|
|
|
|
Freq = BlockFrequency(UINT64_MAX);
|
2014-04-29 18:31:29 +02:00
|
|
|
Freq *= Prob;
|
2015-09-26 01:09:59 +02:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 9223372036854775807ULL);
|
2011-07-27 20:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BlockFrequencyTest, BigToBig) {
|
|
|
|
const uint64_t Big = 387246523487234346LL;
|
|
|
|
const uint32_t P = 123456789;
|
|
|
|
BlockFrequency Freq(Big);
|
|
|
|
BranchProbability Prob(P, P);
|
|
|
|
Freq *= Prob;
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), Big);
|
2013-11-17 04:25:24 +01:00
|
|
|
|
|
|
|
Freq = BlockFrequency(Big);
|
2014-04-29 18:31:29 +02:00
|
|
|
Freq *= Prob;
|
2013-11-17 04:25:24 +01:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), Big);
|
2011-07-27 20:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BlockFrequencyTest, MaxToMax) {
|
|
|
|
BlockFrequency Freq(UINT64_MAX);
|
|
|
|
BranchProbability Prob(UINT32_MAX, UINT32_MAX);
|
|
|
|
Freq *= Prob;
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
2013-11-17 04:25:24 +01:00
|
|
|
|
|
|
|
// This additionally makes sure if we have a value equal to our saturating
|
|
|
|
// value, we do not signal saturation if the result equals said value, but
|
|
|
|
// saturating does not occur.
|
|
|
|
Freq = BlockFrequency(UINT64_MAX);
|
2014-04-29 18:31:29 +02:00
|
|
|
Freq *= Prob;
|
2013-11-17 04:25:24 +01:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
|
|
|
}
|
|
|
|
|
2015-10-12 20:34:00 +02:00
|
|
|
TEST(BlockFrequencyTest, Subtract) {
|
|
|
|
BlockFrequency Freq1(0), Freq2(1);
|
|
|
|
EXPECT_EQ((Freq1 - Freq2).getFrequency(), 0u);
|
|
|
|
EXPECT_EQ((Freq2 - Freq1).getFrequency(), 1u);
|
|
|
|
}
|
|
|
|
|
2013-06-28 20:23:42 +02:00
|
|
|
TEST(BlockFrequency, Divide) {
|
|
|
|
BlockFrequency Freq(0x3333333333333333ULL);
|
|
|
|
Freq /= BranchProbability(1, 2);
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), 0x6666666666666666ULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BlockFrequencyTest, Saturate) {
|
|
|
|
BlockFrequency Freq(0x3333333333333333ULL);
|
|
|
|
Freq /= BranchProbability(100, 300);
|
2015-09-26 01:09:59 +02:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 0x9999999866666668ULL);
|
2013-06-28 20:23:42 +02:00
|
|
|
Freq /= BranchProbability(1, 2);
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
|
|
|
|
|
|
|
Freq = 0x1000000000000000ULL;
|
2015-09-26 01:09:59 +02:00
|
|
|
Freq /= BranchProbability(10000, 170000);
|
2013-06-28 20:23:42 +02:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
2013-06-28 23:51:18 +02:00
|
|
|
|
|
|
|
// Try to cheat the multiplication overflow check.
|
|
|
|
Freq = 0x00000001f0000001ull;
|
|
|
|
Freq /= BranchProbability(1000, 0xf000000f);
|
2015-09-26 01:09:59 +02:00
|
|
|
EXPECT_EQ(33527736066704712ULL, Freq.getFrequency());
|
2013-06-28 20:23:42 +02:00
|
|
|
}
|
|
|
|
|
2013-12-14 03:24:22 +01:00
|
|
|
TEST(BlockFrequencyTest, SaturatingRightShift) {
|
|
|
|
BlockFrequency Freq(0x10080ULL);
|
|
|
|
Freq >>= 2;
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), 0x4020ULL);
|
|
|
|
Freq >>= 20;
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), 0x1ULL);
|
|
|
|
}
|
|
|
|
|
2011-07-27 20:57:40 +02:00
|
|
|
}
|