1 //===- InlineCostTest.cpp - test for InlineCost ---------------------------===// 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/InlineCost.h" 10 #include "llvm/Analysis/TargetTransformInfo.h" 11 #include "llvm/AsmParser/Parser.h" 12 #include "llvm/IR/Instructions.h" 13 #include "llvm/IR/LLVMContext.h" 14 #include "llvm/IR/Module.h" 15 #include "gtest/gtest.h" 16 17 namespace { 18 19 // Tests that we can retrieve the CostFeatures without an error 20 TEST(InlineCostTest, CostFeatures) { 21 using namespace llvm; 22 23 const auto *const IR = R"IR( 24 define i32 @f(i32) { 25 ret i32 4 26 } 27 28 define i32 @g(i32) { 29 %2 = call i32 @f(i32 0) 30 ret i32 %2 31 } 32 )IR"; 33 34 LLVMContext C; 35 SMDiagnostic Err; 36 std::unique_ptr<Module> M = parseAssemblyString(IR, Err, C); 37 ASSERT_TRUE(M); 38 39 auto *G = M->getFunction("g"); 40 ASSERT_TRUE(G); 41 42 // find the call to f in g 43 CallBase *CB = nullptr; 44 for (auto &BB : *G) { 45 for (auto &I : BB) { 46 if ((CB = dyn_cast<CallBase>(&I))) 47 break; 48 } 49 } 50 ASSERT_TRUE(CB); 51 52 ModuleAnalysisManager MAM; 53 FunctionAnalysisManager FAM; 54 FAM.registerPass([&] { return TargetIRAnalysis(); }); 55 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); }); 56 FAM.registerPass([&] { return AssumptionAnalysis(); }); 57 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); }); 58 59 MAM.registerPass([&] { return PassInstrumentationAnalysis(); }); 60 FAM.registerPass([&] { return PassInstrumentationAnalysis(); }); 61 62 ModulePassManager MPM; 63 MPM.run(*M, MAM); 64 65 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & { 66 return FAM.getResult<AssumptionAnalysis>(F); 67 }; 68 auto &TIR = FAM.getResult<TargetIRAnalysis>(*G); 69 70 const auto Features = 71 llvm::getInliningCostFeatures(*CB, TIR, GetAssumptionCache); 72 73 // Check that the optional is not empty 74 ASSERT_TRUE(Features); 75 } 76 77 } // namespace 78