1 //===- FunctionPropertiesAnalysisTest.cpp - Function Properties Unit Tests-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Analysis/FunctionPropertiesAnalysis.h" 10 #include "llvm/AsmParser/Parser.h" 11 #include "llvm/IR/Dominators.h" 12 #include "llvm/IR/Instructions.h" 13 #include "llvm/IR/LLVMContext.h" 14 #include "llvm/IR/Module.h" 15 #include "llvm/Support/SourceMgr.h" 16 #include "gtest/gtest.h" 17 18 using namespace llvm; 19 namespace { 20 21 class FunctionPropertiesAnalysisTest : public testing::Test { 22 protected: 23 std::unique_ptr<DominatorTree> DT; 24 std::unique_ptr<LoopInfo> LI; 25 26 FunctionPropertiesInfo buildFPI(Function &F) { 27 DT.reset(new DominatorTree(F)); 28 LI.reset(new LoopInfo(*DT)); 29 return FunctionPropertiesInfo::getFunctionPropertiesInfo(F, *LI); 30 } 31 32 std::unique_ptr<Module> makeLLVMModule(LLVMContext &C, const char *IR) { 33 SMDiagnostic Err; 34 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C); 35 if (!Mod) 36 Err.print("MLAnalysisTests", errs()); 37 return Mod; 38 } 39 }; 40 41 TEST_F(FunctionPropertiesAnalysisTest, BasicTest) { 42 LLVMContext C; 43 std::unique_ptr<Module> M = makeLLVMModule(C, 44 R"IR( 45 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" 46 target triple = "x86_64-pc-linux-gnu" 47 declare i32 @f1(i32) 48 declare i32 @f2(i32) 49 define i32 @branches(i32) { 50 %cond = icmp slt i32 %0, 3 51 br i1 %cond, label %then, label %else 52 then: 53 %ret.1 = call i32 @f1(i32 %0) 54 br label %last.block 55 else: 56 %ret.2 = call i32 @f2(i32 %0) 57 br label %last.block 58 last.block: 59 %ret = phi i32 [%ret.1, %then], [%ret.2, %else] 60 ret i32 %ret 61 } 62 define internal i32 @top() { 63 %1 = call i32 @branches(i32 2) 64 %2 = call i32 @f1(i32 %1) 65 ret i32 %2 66 } 67 )IR"); 68 69 Function *BranchesFunction = M->getFunction("branches"); 70 FunctionPropertiesInfo BranchesFeatures = buildFPI(*BranchesFunction); 71 EXPECT_EQ(BranchesFeatures.BasicBlockCount, 4); 72 EXPECT_EQ(BranchesFeatures.BlocksReachedFromConditionalInstruction, 2); 73 // 2 Users: top is one. The other is added because @branches is not internal, 74 // so it may have external callers. 75 EXPECT_EQ(BranchesFeatures.Uses, 2); 76 EXPECT_EQ(BranchesFeatures.DirectCallsToDefinedFunctions, 0); 77 EXPECT_EQ(BranchesFeatures.LoadInstCount, 0); 78 EXPECT_EQ(BranchesFeatures.StoreInstCount, 0); 79 EXPECT_EQ(BranchesFeatures.MaxLoopDepth, 0); 80 EXPECT_EQ(BranchesFeatures.TopLevelLoopCount, 0); 81 82 Function *TopFunction = M->getFunction("top"); 83 FunctionPropertiesInfo TopFeatures = buildFPI(*TopFunction); 84 EXPECT_EQ(TopFeatures.BasicBlockCount, 1); 85 EXPECT_EQ(TopFeatures.BlocksReachedFromConditionalInstruction, 0); 86 EXPECT_EQ(TopFeatures.Uses, 0); 87 EXPECT_EQ(TopFeatures.DirectCallsToDefinedFunctions, 1); 88 EXPECT_EQ(BranchesFeatures.LoadInstCount, 0); 89 EXPECT_EQ(BranchesFeatures.StoreInstCount, 0); 90 EXPECT_EQ(BranchesFeatures.MaxLoopDepth, 0); 91 EXPECT_EQ(BranchesFeatures.TopLevelLoopCount, 0); 92 } 93 } // end anonymous namespace