| 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/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" |
| 18 |
|
| 19 |
namespace { |
| 20 |
|
| 21 |
// Tests that we can retrieve the CostFeatures without an error |
| 22 |
TEST(InlineCostTest, CostFeatures) { |
| 23 |
using namespace llvm; |
| 24 |
|
| 25 |
const auto *const IR = R"IR( |
| 26 |
define i32 @f(i32) { |
| 27 |
ret i32 4 |
| 28 |
} |
| 29 |
|
| 30 |
define i32 @g(i32) { |
| 31 |
%2 = call i32 @f(i32 0) |
| 32 |
ret i32 %2 |
| 33 |
} |
| 34 |
)IR"; |
| 35 |
|
| 36 |
LLVMContext C; |
| 37 |
SMDiagnostic Err; |
| 38 |
std::unique_ptr<Module> M = parseAssemblyString(IR, Err, C); |
| 39 |
ASSERT_TRUE(M); |
| 40 |
|
| 41 |
auto *G = M->getFunction("g"); |
| 42 |
ASSERT_TRUE(G); |
| 43 |
|
| 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; |
| 50 |
} |
| 51 |
} |
| 52 |
ASSERT_TRUE(CB); |
| 53 |
|
| 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); }); |
| 60 |
|
| 61 |
MAM.registerPass([&] { return PassInstrumentationAnalysis(); }); |
| 62 |
FAM.registerPass([&] { return PassInstrumentationAnalysis(); }); |
| 63 |
|
| 64 |
ModulePassManager MPM; |
| 65 |
MPM.run(*M, MAM); |
| 66 |
|
| 67 |
auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & { |
| 68 |
return FAM.getResult<AssumptionAnalysis>(F); |
| 69 |
}; |
| 70 |
auto &TIR = FAM.getResult<TargetIRAnalysis>(*G); |
| 71 |
|
| 72 |
const auto Features = |
| 73 |
llvm::getInliningCostFeatures(*CB, TIR, GetAssumptionCache); |
| 74 |
|
| 75 |
// Check that the optional is not empty |
| 76 |
ASSERT_TRUE(Features); |
| 77 |
} |
| 78 |
|
| 79 |
} // namespace |