diff options
author | Vedant Kumar <vsk@apple.com> | 2016-12-17 01:02:08 +0000 |
---|---|---|
committer | Vedant Kumar <vsk@apple.com> | 2016-12-17 01:02:08 +0000 |
commit | a4bd1463c8dc04bcd9befd49a667dbfd7fec0f87 (patch) | |
tree | b0fec2fde39353039e21397a164d4bc08fff6023 /llvm/unittests/Analysis/BranchProbabilityInfoTest.cpp | |
parent | 10005d915e36eced0dab09782bf93d398b45936a (diff) | |
download | bcm5719-llvm-a4bd1463c8dc04bcd9befd49a667dbfd7fec0f87.tar.gz bcm5719-llvm-a4bd1463c8dc04bcd9befd49a667dbfd7fec0f87.zip |
Retry: [BPI] Use a safer constructor to calculate branch probabilities
BPI may trigger signed overflow UB while computing branch probabilities for
cold calls or to unreachables. For example, with our current choice of weights,
we'll crash if there are >= 2^12 branches to an unreachable.
Use a safer BranchProbability constructor which is better at handling fractions
with large denominators.
Changes since the initial commit:
- Use explicit casts to ensure that multiplication operands are 64-bit
ints.
rdar://problem/29368161
Differential Revision: https://reviews.llvm.org/D27862
llvm-svn: 290022
Diffstat (limited to 'llvm/unittests/Analysis/BranchProbabilityInfoTest.cpp')
-rw-r--r-- | llvm/unittests/Analysis/BranchProbabilityInfoTest.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/llvm/unittests/Analysis/BranchProbabilityInfoTest.cpp b/llvm/unittests/Analysis/BranchProbabilityInfoTest.cpp new file mode 100644 index 00000000000..cbf8b50c762 --- /dev/null +++ b/llvm/unittests/Analysis/BranchProbabilityInfoTest.cpp @@ -0,0 +1,88 @@ +//===- BranchProbabilityInfoTest.cpp - BranchProbabilityInfo 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/BranchProbabilityInfo.h" +#include "llvm/Analysis/LoopInfo.h" +#include "llvm/AsmParser/Parser.h" +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/Dominators.h" +#include "llvm/IR/Instructions.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 { + +struct BranchProbabilityInfoTest : public testing::Test { + std::unique_ptr<BranchProbabilityInfo> BPI; + std::unique_ptr<DominatorTree> DT; + std::unique_ptr<LoopInfo> LI; + LLVMContext C; + + BranchProbabilityInfo &buildBPI(Function &F) { + DT.reset(new DominatorTree(F)); + LI.reset(new LoopInfo(*DT)); + BPI.reset(new BranchProbabilityInfo(F, *LI)); + return *BPI; + } + + std::unique_ptr<Module> makeLLVMModule() { + const char *ModuleString = "define void @f() { exit: ret void }\n"; + SMDiagnostic Err; + return parseAssemblyString(ModuleString, Err, C); + } +}; + +TEST_F(BranchProbabilityInfoTest, StressUnreachableHeuristic) { + auto M = makeLLVMModule(); + Function *F = M->getFunction("f"); + + // define void @f() { + // entry: + // switch i32 undef, label %exit, [ + // i32 0, label %preexit + // ... ;;< Add lots of cases to stress the heuristic. + // ] + // preexit: + // unreachable + // exit: + // ret void + // } + + auto *ExitBB = &F->back(); + auto *EntryBB = BasicBlock::Create(C, "entry", F, /*insertBefore=*/ExitBB); + + auto *PreExitBB = + BasicBlock::Create(C, "preexit", F, /*insertBefore=*/ExitBB); + new UnreachableInst(C, PreExitBB); + + unsigned NumCases = 4096; + auto *I32 = IntegerType::get(C, 32); + auto *Undef = UndefValue::get(I32); + auto *Switch = SwitchInst::Create(Undef, ExitBB, NumCases, EntryBB); + for (unsigned I = 0; I < NumCases; ++I) + Switch->addCase(ConstantInt::get(I32, I), PreExitBB); + + BranchProbabilityInfo &BPI = buildBPI(*F); + + // FIXME: This doesn't seem optimal. Since all of the cases handled by the + // switch have the *same* destination block ("preexit"), shouldn't it be the + // hot one? I'd expect the results to be reversed here... + EXPECT_FALSE(BPI.isEdgeHot(EntryBB, PreExitBB)); + EXPECT_TRUE(BPI.isEdgeHot(EntryBB, ExitBB)); +} + +} // end anonymous namespace +} // end namespace llvm |