1 //===- BlockFrequencyInfoTest.cpp - BlockFrequencyInfo unit tests ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Analysis/BlockFrequencyInfo.h" 11 #include "llvm/Analysis/BranchProbabilityInfo.h" 12 #include "llvm/Analysis/LoopInfo.h" 13 #include "llvm/AsmParser/Parser.h" 14 #include "llvm/IR/BasicBlock.h" 15 #include "llvm/IR/Dominators.h" 16 #include "llvm/IR/Function.h" 17 #include "llvm/IR/LLVMContext.h" 18 #include "llvm/IR/Module.h" 19 #include "llvm/Support/DataTypes.h" 20 #include "llvm/Support/SourceMgr.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include "gtest/gtest.h" 23 24 namespace llvm { 25 namespace { 26 27 class BlockFrequencyInfoTest : public testing::Test { 28 protected: 29 std::unique_ptr<BranchProbabilityInfo> BPI; 30 std::unique_ptr<DominatorTree> DT; 31 std::unique_ptr<LoopInfo> LI; 32 LLVMContext C; 33 34 BlockFrequencyInfo buildBFI(Function &F) { 35 DT.reset(new DominatorTree(F)); 36 LI.reset(new LoopInfo(*DT)); 37 BPI.reset(new BranchProbabilityInfo(F, *LI)); 38 return BlockFrequencyInfo(F, *BPI, *LI); 39 } 40 std::unique_ptr<Module> makeLLVMModule() { 41 const char *ModuleStrig = "define i32 @f(i32 %x) {\n" 42 "bb0:\n" 43 " %y1 = icmp eq i32 %x, 0 \n" 44 " br i1 %y1, label %bb1, label %bb2 \n" 45 "bb1:\n" 46 " br label %bb3\n" 47 "bb2:\n" 48 " br label %bb3\n" 49 "bb3:\n" 50 " %y2 = phi i32 [0, %bb1], [1, %bb2] \n" 51 " ret i32 %y2\n" 52 "}\n"; 53 SMDiagnostic Err; 54 return parseAssemblyString(ModuleStrig, Err, C); 55 } 56 }; 57 58 TEST_F(BlockFrequencyInfoTest, Basic) { 59 auto M = makeLLVMModule(); 60 Function *F = M->getFunction("f"); 61 F->setEntryCount(100); 62 63 BlockFrequencyInfo BFI = buildBFI(*F); 64 BasicBlock &BB0 = F->getEntryBlock(); 65 BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0); 66 BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1); 67 BasicBlock *BB3 = BB1->getSingleSuccessor(); 68 69 uint64_t BB0Freq = BFI.getBlockFreq(&BB0).getFrequency(); 70 uint64_t BB1Freq = BFI.getBlockFreq(BB1).getFrequency(); 71 uint64_t BB2Freq = BFI.getBlockFreq(BB2).getFrequency(); 72 uint64_t BB3Freq = BFI.getBlockFreq(BB3).getFrequency(); 73 74 EXPECT_EQ(BB0Freq, BB3Freq); 75 EXPECT_EQ(BB0Freq, BB1Freq + BB2Freq); 76 EXPECT_EQ(BB0Freq, BB3Freq); 77 78 EXPECT_EQ(BFI.getBlockProfileCount(&BB0).getValue(), UINT64_C(100)); 79 EXPECT_EQ(BFI.getBlockProfileCount(BB3).getValue(), UINT64_C(100)); 80 EXPECT_EQ(BFI.getBlockProfileCount(BB1).getValue(), 100 * BB1Freq / BB0Freq); 81 EXPECT_EQ(BFI.getBlockProfileCount(BB2).getValue(), 100 * BB2Freq / BB0Freq); 82 83 // Scale the frequencies of BB0, BB1 and BB2 by a factor of two. 84 SmallPtrSet<BasicBlock *, 4> BlocksToScale({BB1, BB2}); 85 BFI.setBlockFreqAndScale(&BB0, BB0Freq * 2, BlocksToScale); 86 EXPECT_EQ(BFI.getBlockFreq(&BB0).getFrequency(), 2 * BB0Freq); 87 EXPECT_EQ(BFI.getBlockFreq(BB1).getFrequency(), 2 * BB1Freq); 88 EXPECT_EQ(BFI.getBlockFreq(BB2).getFrequency(), 2 * BB2Freq); 89 EXPECT_EQ(BFI.getBlockFreq(BB3).getFrequency(), BB3Freq); 90 } 91 92 } // end anonymous namespace 93 } // end namespace llvm 94