Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
clang
clang-tools-extra
compiler-rt
libcxx
libcxxabi
libunwind
lld
lldb
llvm
bindings
cmake
docs
examples
include
lib
projects
resources
runtimes
scripts
test
tools
unittests
ADT
Analysis
AliasAnalysisTest.cpp
AliasSetTrackerTest.cpp
BlockFrequencyInfoTest.cpp
BranchProbabilityInfoTest.cpp
CFGTest.cpp
CGSCCPassManagerTest.cpp
CMakeLists.txt
CallGraphTest.cpp
GlobalsModRefTest.cpp
LazyCallGraphTest.cpp
LoopInfoTest.cpp
MemoryBuiltinsTest.cpp
MemorySSA.cpp
OrderedBasicBlockTest.cpp
ProfileSummaryInfoTest.cpp
ScalarEvolutionTest.cpp
SparsePropagation.cpp
TBAATest.cpp
TargetLibraryInfoTest.cpp
UnrollAnalyzer.cpp
ValueLatticeTest.cpp
ValueTrackingTest.cpp
AsmParser
BinaryFormat
Bitcode
CodeGen
DebugInfo
ExecutionEngine
FuzzMutate
IR
LineEditor
Linker
MC
MI
Object
ObjectYAML
Option
ProfileData
Support
Target
Transforms
XRay
tools
CMakeLists.txt
utils
.arcconfig
.clang-format
.clang-tidy
.gitattributes
.gitignore
CMakeLists.txt
CODE_OWNERS.TXT
CREDITS.TXT
LICENSE.TXT
LLVMBuild.txt
README.txt
RELEASE_TESTERS.TXT
configure
llvm.spec.in
openmp
polly
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mk
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/external/llvm-project/llvm/unittests/Analysis/BlockFrequencyInfoTest.cpp
Xamarin Public Jenkins (auto-signing) 468663ddbb Imported Upstream version 6.10.0.49
Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
2020-01-16 16:38:04 +00:00

94 lines
3.4 KiB
C++

//===- BlockFrequencyInfoTest.cpp - BlockFrequencyInfo unit tests ---------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
namespace llvm {
namespace {
class BlockFrequencyInfoTest : public testing::Test {
protected:
std::unique_ptr<BranchProbabilityInfo> BPI;
std::unique_ptr<DominatorTree> DT;
std::unique_ptr<LoopInfo> LI;
LLVMContext C;
BlockFrequencyInfo buildBFI(Function &F) {
DT.reset(new DominatorTree(F));
LI.reset(new LoopInfo(*DT));
BPI.reset(new BranchProbabilityInfo(F, *LI));
return BlockFrequencyInfo(F, *BPI, *LI);
}
std::unique_ptr<Module> makeLLVMModule() {
const char *ModuleStrig = "define i32 @f(i32 %x) {\n"
"bb0:\n"
" %y1 = icmp eq i32 %x, 0 \n"
" br i1 %y1, label %bb1, label %bb2 \n"
"bb1:\n"
" br label %bb3\n"
"bb2:\n"
" br label %bb3\n"
"bb3:\n"
" %y2 = phi i32 [0, %bb1], [1, %bb2] \n"
" ret i32 %y2\n"
"}\n";
SMDiagnostic Err;
return parseAssemblyString(ModuleStrig, Err, C);
}
};
TEST_F(BlockFrequencyInfoTest, Basic) {
auto M = makeLLVMModule();
Function *F = M->getFunction("f");
F->setEntryCount(100);
BlockFrequencyInfo BFI = buildBFI(*F);
BasicBlock &BB0 = F->getEntryBlock();
BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1);
BasicBlock *BB3 = BB1->getSingleSuccessor();
uint64_t BB0Freq = BFI.getBlockFreq(&BB0).getFrequency();
uint64_t BB1Freq = BFI.getBlockFreq(BB1).getFrequency();
uint64_t BB2Freq = BFI.getBlockFreq(BB2).getFrequency();
uint64_t BB3Freq = BFI.getBlockFreq(BB3).getFrequency();
EXPECT_EQ(BB0Freq, BB3Freq);
EXPECT_EQ(BB0Freq, BB1Freq + BB2Freq);
EXPECT_EQ(BB0Freq, BB3Freq);
EXPECT_EQ(BFI.getBlockProfileCount(&BB0).getValue(), UINT64_C(100));
EXPECT_EQ(BFI.getBlockProfileCount(BB3).getValue(), UINT64_C(100));
EXPECT_EQ(BFI.getBlockProfileCount(BB1).getValue(), 100 * BB1Freq / BB0Freq);
EXPECT_EQ(BFI.getBlockProfileCount(BB2).getValue(), 100 * BB2Freq / BB0Freq);
// Scale the frequencies of BB0, BB1 and BB2 by a factor of two.
SmallPtrSet<BasicBlock *, 4> BlocksToScale({BB1, BB2});
BFI.setBlockFreqAndScale(&BB0, BB0Freq * 2, BlocksToScale);
EXPECT_EQ(BFI.getBlockFreq(&BB0).getFrequency(), 2 * BB0Freq);
EXPECT_EQ(BFI.getBlockFreq(BB1).getFrequency(), 2 * BB1Freq);
EXPECT_EQ(BFI.getBlockFreq(BB2).getFrequency(), 2 * BB2Freq);
EXPECT_EQ(BFI.getBlockFreq(BB3).getFrequency(), BB3Freq);
}
} // end anonymous namespace
} // end namespace llvm