2015-02-03 02:18:07 +08:00
|
|
|
//===- unittests/Support/BlockFrequencyTest.cpp - BlockFrequency tests ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-28 02:57:40 +08:00
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
|
|
|
#include "llvm/Support/BranchProbability.h"
|
2012-12-04 18:23:08 +08:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2011-07-28 02:57:40 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <climits>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(BlockFrequencyTest, OneToZero) {
|
|
|
|
BlockFrequency Freq(1);
|
2015-09-26 07:09:59 +08:00
|
|
|
BranchProbability Prob(UINT32_MAX / 3, UINT32_MAX);
|
2011-07-28 02:57:40 +08:00
|
|
|
Freq *= Prob;
|
2013-06-22 04:20:27 +08:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 0u);
|
2013-11-17 11:25:24 +08:00
|
|
|
|
|
|
|
Freq = BlockFrequency(1);
|
2014-04-30 00:31:29 +08:00
|
|
|
Freq *= Prob;
|
2013-11-17 11:25:24 +08:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 0u);
|
2011-07-28 02:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BlockFrequencyTest, OneToOne) {
|
|
|
|
BlockFrequency Freq(1);
|
|
|
|
BranchProbability Prob(UINT32_MAX, UINT32_MAX);
|
|
|
|
Freq *= Prob;
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
2013-11-17 11:25:24 +08:00
|
|
|
|
|
|
|
Freq = BlockFrequency(1);
|
2014-04-30 00:31:29 +08:00
|
|
|
Freq *= Prob;
|
2013-11-17 11:25:24 +08:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
2011-07-28 02:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BlockFrequencyTest, ThreeToOne) {
|
|
|
|
BlockFrequency Freq(3);
|
|
|
|
BranchProbability Prob(3000000, 9000000);
|
|
|
|
Freq *= Prob;
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
2013-11-17 11:25:24 +08:00
|
|
|
|
|
|
|
Freq = BlockFrequency(3);
|
2014-04-30 00:31:29 +08:00
|
|
|
Freq *= Prob;
|
2013-11-17 11:25:24 +08:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
2011-07-28 02:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BlockFrequencyTest, MaxToHalfMax) {
|
|
|
|
BlockFrequency Freq(UINT64_MAX);
|
|
|
|
BranchProbability Prob(UINT32_MAX / 2, UINT32_MAX);
|
|
|
|
Freq *= Prob;
|
2015-09-26 07:09:59 +08:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 9223372036854775807ULL);
|
2013-11-17 11:25:24 +08:00
|
|
|
|
|
|
|
Freq = BlockFrequency(UINT64_MAX);
|
2014-04-30 00:31:29 +08:00
|
|
|
Freq *= Prob;
|
2015-09-26 07:09:59 +08:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 9223372036854775807ULL);
|
2011-07-28 02:57:40 +08: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 11:25:24 +08:00
|
|
|
|
|
|
|
Freq = BlockFrequency(Big);
|
2014-04-30 00:31:29 +08:00
|
|
|
Freq *= Prob;
|
2013-11-17 11:25:24 +08:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), Big);
|
2011-07-28 02:57:40 +08: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 11:25:24 +08: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-30 00:31:29 +08:00
|
|
|
Freq *= Prob;
|
2013-11-17 11:25:24 +08:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
|
|
|
}
|
|
|
|
|
2015-10-13 02:34:00 +08:00
|
|
|
TEST(BlockFrequencyTest, Subtract) {
|
|
|
|
BlockFrequency Freq1(0), Freq2(1);
|
|
|
|
EXPECT_EQ((Freq1 - Freq2).getFrequency(), 0u);
|
|
|
|
EXPECT_EQ((Freq2 - Freq1).getFrequency(), 1u);
|
|
|
|
}
|
|
|
|
|
2013-06-29 02:23:42 +08: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 07:09:59 +08:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), 0x9999999866666668ULL);
|
2013-06-29 02:23:42 +08:00
|
|
|
Freq /= BranchProbability(1, 2);
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
|
|
|
|
|
|
|
Freq = 0x1000000000000000ULL;
|
2015-09-26 07:09:59 +08:00
|
|
|
Freq /= BranchProbability(10000, 170000);
|
2013-06-29 02:23:42 +08:00
|
|
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
2013-06-29 05:51:18 +08:00
|
|
|
|
|
|
|
// Try to cheat the multiplication overflow check.
|
|
|
|
Freq = 0x00000001f0000001ull;
|
|
|
|
Freq /= BranchProbability(1000, 0xf000000f);
|
2015-09-26 07:09:59 +08:00
|
|
|
EXPECT_EQ(33527736066704712ULL, Freq.getFrequency());
|
2013-06-29 02:23:42 +08:00
|
|
|
}
|
|
|
|
|
2013-12-14 10:24:22 +08:00
|
|
|
TEST(BlockFrequencyTest, SaturatingRightShift) {
|
|
|
|
BlockFrequency Freq(0x10080ULL);
|
|
|
|
Freq >>= 2;
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), 0x4020ULL);
|
|
|
|
Freq >>= 20;
|
|
|
|
EXPECT_EQ(Freq.getFrequency(), 0x1ULL);
|
|
|
|
}
|
|
|
|
|
2011-07-28 02:57:40 +08:00
|
|
|
}
|