1*6047deb7SPaul Kirth //===----- ProfDataUtils.cpp - Unit tests for ProfDataUtils ---------------===// 2*6047deb7SPaul Kirth // 3*6047deb7SPaul Kirth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*6047deb7SPaul Kirth // See https://llvm.org/LICENSE.txt for license information. 5*6047deb7SPaul Kirth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*6047deb7SPaul Kirth // 7*6047deb7SPaul Kirth //===----------------------------------------------------------------------===// 8*6047deb7SPaul Kirth 9*6047deb7SPaul Kirth #include "llvm/Analysis/AssumptionCache.h" 10*6047deb7SPaul Kirth #include "llvm/Analysis/BasicAliasAnalysis.h" 11*6047deb7SPaul Kirth #include "llvm/Analysis/BlockFrequencyInfo.h" 12*6047deb7SPaul Kirth #include "llvm/Analysis/BranchProbabilityInfo.h" 13*6047deb7SPaul Kirth #include "llvm/Analysis/CFG.h" 14*6047deb7SPaul Kirth #include "llvm/Analysis/DomTreeUpdater.h" 15*6047deb7SPaul Kirth #include "llvm/Analysis/LoopInfo.h" 16*6047deb7SPaul Kirth #include "llvm/Analysis/MemorySSA.h" 17*6047deb7SPaul Kirth #include "llvm/Analysis/MemorySSAUpdater.h" 18*6047deb7SPaul Kirth #include "llvm/Analysis/PostDominators.h" 19*6047deb7SPaul Kirth #include "llvm/Analysis/TargetLibraryInfo.h" 20*6047deb7SPaul Kirth #include "llvm/AsmParser/Parser.h" 21*6047deb7SPaul Kirth #include "llvm/IR/BasicBlock.h" 22*6047deb7SPaul Kirth #include "llvm/IR/Dominators.h" 23*6047deb7SPaul Kirth #include "llvm/IR/LLVMContext.h" 24*6047deb7SPaul Kirth #include "llvm/IR/ProfDataUtils.h" 25*6047deb7SPaul Kirth #include "llvm/Support/SourceMgr.h" 26*6047deb7SPaul Kirth #include "llvm/Transforms/Utils/BreakCriticalEdges.h" 27*6047deb7SPaul Kirth #include "gtest/gtest.h" 28*6047deb7SPaul Kirth 29*6047deb7SPaul Kirth using namespace llvm; 30*6047deb7SPaul Kirth 31*6047deb7SPaul Kirth static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) { 32*6047deb7SPaul Kirth SMDiagnostic Err; 33*6047deb7SPaul Kirth std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C); 34*6047deb7SPaul Kirth if (!Mod) 35*6047deb7SPaul Kirth Err.print("ProfDataUtilsTests", errs()); 36*6047deb7SPaul Kirth return Mod; 37*6047deb7SPaul Kirth } 38*6047deb7SPaul Kirth 39*6047deb7SPaul Kirth TEST(ProfDataUtils, extractWeights) { 40*6047deb7SPaul Kirth LLVMContext C; 41*6047deb7SPaul Kirth std::unique_ptr<Module> M = parseIR(C, R"IR( 42*6047deb7SPaul Kirth define void @foo(i1 %cond0) { 43*6047deb7SPaul Kirth entry: 44*6047deb7SPaul Kirth br i1 %cond0, label %bb0, label %bb1, !prof !1 45*6047deb7SPaul Kirth bb0: 46*6047deb7SPaul Kirth %0 = mul i32 1, 2 47*6047deb7SPaul Kirth br label %bb1 48*6047deb7SPaul Kirth bb1: 49*6047deb7SPaul Kirth ret void 50*6047deb7SPaul Kirth } 51*6047deb7SPaul Kirth 52*6047deb7SPaul Kirth !1 = !{!"branch_weights", i32 1, i32 100000} 53*6047deb7SPaul Kirth )IR"); 54*6047deb7SPaul Kirth Function *F = M->getFunction("foo"); 55*6047deb7SPaul Kirth auto &Entry = F->getEntryBlock(); 56*6047deb7SPaul Kirth auto &I = Entry.front(); 57*6047deb7SPaul Kirth auto *Branch = dyn_cast<BranchInst>(&I); 58*6047deb7SPaul Kirth EXPECT_NE(nullptr, Branch); 59*6047deb7SPaul Kirth auto *ProfileData = Branch->getMetadata(LLVMContext::MD_prof); 60*6047deb7SPaul Kirth EXPECT_NE(ProfileData, nullptr); 61*6047deb7SPaul Kirth EXPECT_TRUE(hasProfMD(I)); 62*6047deb7SPaul Kirth SmallVector<uint32_t> Weights; 63*6047deb7SPaul Kirth EXPECT_TRUE(extractBranchWeights(ProfileData, Weights)); 64*6047deb7SPaul Kirth EXPECT_EQ(Weights[0], 1U); 65*6047deb7SPaul Kirth EXPECT_EQ(Weights[1], 100000U); 66*6047deb7SPaul Kirth EXPECT_EQ(Weights.size(), 2U); 67*6047deb7SPaul Kirth } 68*6047deb7SPaul Kirth 69*6047deb7SPaul Kirth TEST(ProfDataUtils, NoWeights) { 70*6047deb7SPaul Kirth LLVMContext C; 71*6047deb7SPaul Kirth std::unique_ptr<Module> M = parseIR(C, R"IR( 72*6047deb7SPaul Kirth define void @foo(i1 %cond0) { 73*6047deb7SPaul Kirth entry: 74*6047deb7SPaul Kirth br i1 %cond0, label %bb0, label %bb1 75*6047deb7SPaul Kirth bb0: 76*6047deb7SPaul Kirth %0 = mul i32 1, 2 77*6047deb7SPaul Kirth br label %bb1 78*6047deb7SPaul Kirth bb1: 79*6047deb7SPaul Kirth ret void 80*6047deb7SPaul Kirth } 81*6047deb7SPaul Kirth )IR"); 82*6047deb7SPaul Kirth Function *F = M->getFunction("foo"); 83*6047deb7SPaul Kirth auto &Entry = F->getEntryBlock(); 84*6047deb7SPaul Kirth auto &I = Entry.front(); 85*6047deb7SPaul Kirth auto *Branch = dyn_cast<BranchInst>(&I); 86*6047deb7SPaul Kirth EXPECT_NE(nullptr, Branch); 87*6047deb7SPaul Kirth auto *ProfileData = Branch->getMetadata(LLVMContext::MD_prof); 88*6047deb7SPaul Kirth EXPECT_EQ(ProfileData, nullptr); 89*6047deb7SPaul Kirth EXPECT_FALSE(hasProfMD(I)); 90*6047deb7SPaul Kirth SmallVector<uint32_t> Weights; 91*6047deb7SPaul Kirth EXPECT_FALSE(extractBranchWeights(ProfileData, Weights)); 92*6047deb7SPaul Kirth EXPECT_EQ(Weights.size(), 0U); 93*6047deb7SPaul Kirth } 94