1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2021 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 #ifndef VC_UTILS_GENERAL_BIF_H
10 #define VC_UTILS_GENERAL_BIF_H
11
12 #include <llvm/IR/Function.h>
13 #include <llvm/IR/LLVMContext.h>
14 #include <llvm/IR/Module.h>
15 #include <llvm/Support/MemoryBuffer.h>
16
17 #include <string>
18 #include <vector>
19
20 namespace vc {
21
22 // Decodes binary module provided via \p BiFModuleBuffer. Returns obtained
23 // llvm::Module. If some errors occured reports fatal error.
24 // Note: wraps parseBitcodeFile.
25 std::unique_ptr<llvm::Module>
26 getBiFModuleOrReportError(llvm::MemoryBufferRef BiFModuleBuffer,
27 llvm::LLVMContext &Ctx);
28
29 // Same as getBiFModuleOrReportError but the decoding is lazy.
30 // Note: wraps getLazyBitcodeModule.
31 std::unique_ptr<llvm::Module>
32 getLazyBiFModuleOrReportError(llvm::MemoryBufferRef BiFModuleBuffer,
33 llvm::LLVMContext &Ctx);
34
35 using FunctionNameSeq = std::vector<std::string>;
36
37 // Collect all functions for which predicate \p Pred returns true.
38 // PredT is a functor that takes const Function &F as an argument and returns
39 // bool.
40 template <typename PredT>
collectFunctionNamesIf(const llvm::Module & M,PredT Pred)41 FunctionNameSeq collectFunctionNamesIf(const llvm::Module &M, PredT Pred) {
42 using namespace llvm;
43 auto Functions = make_filter_range(M.getFunctionList(), Pred);
44 FunctionNameSeq Names;
45 llvm::transform(Functions, std::back_inserter(Names),
46 [](const Function &F) { return F.getName().str(); });
47 return Names;
48 }
49
50 // Set internal linkage for functions whose name is in \p FuncNames.
51 // \p FuncNames may contain declarations, they won't be changed. AlwaysInline
52 // attribute is optionally set.
53 void internalizeImportedFunctions(const llvm::Module &M,
54 const FunctionNameSeq &FuncNames,
55 bool SetAlwaysInline);
56
57 } // namespace vc
58
59 #endif // VC_UTILS_GENERAL_BIF_H
60