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