LLVM 22.0.0git
DeclareRuntimeLibcalls.cpp
Go to the documentation of this file.
1//===- DeclareRuntimeLibcalls.cpp -----------------------------------------===//
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// Insert declarations for all runtime library calls known for the target.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/IR/Module.h"
16
17using namespace llvm;
18
19static void mergeAttributes(LLVMContext &Ctx, const Module &M,
20 const DataLayout &DL, const Triple &TT,
21 Function *Func, FunctionType *FuncTy,
22 AttributeList FuncAttrs) {
23 AttributeList OldAttrs = Func->getAttributes();
24 AttributeList NewAttrs = OldAttrs;
25
26 {
27 AttrBuilder OldBuilder(Ctx, OldAttrs.getFnAttrs());
28 AttrBuilder NewBuilder(Ctx, FuncAttrs.getFnAttrs());
29 OldBuilder.merge(NewBuilder);
30 NewAttrs = NewAttrs.addFnAttributes(Ctx, OldBuilder);
31 }
32
33 {
34 AttrBuilder OldBuilder(Ctx, OldAttrs.getRetAttrs());
35 AttrBuilder NewBuilder(Ctx, FuncAttrs.getRetAttrs());
36 OldBuilder.merge(NewBuilder);
37 NewAttrs = NewAttrs.addRetAttributes(Ctx, OldBuilder);
38 }
39
40 for (unsigned I = 0, E = FuncTy->getNumParams(); I != E; ++I) {
41 AttrBuilder OldBuilder(Ctx, OldAttrs.getParamAttrs(I));
42 AttrBuilder NewBuilder(Ctx, FuncAttrs.getParamAttrs(I));
43 OldBuilder.merge(NewBuilder);
44 NewAttrs = NewAttrs.addParamAttributes(Ctx, I, OldBuilder);
45 }
46
47 Func->setAttributes(NewAttrs);
48}
49
52 RTLIB::RuntimeLibcallsInfo RTLCI(M.getTargetTriple());
53 LLVMContext &Ctx = M.getContext();
54 const DataLayout &DL = M.getDataLayout();
55 const Triple &TT = M.getTargetTriple();
56
57 for (RTLIB::LibcallImpl Impl : RTLCI.getLibcallImpls()) {
58 if (Impl == RTLIB::Unsupported)
59 continue;
60
61 auto [FuncTy, FuncAttrs] = RTLCI.getFunctionTy(Ctx, TT, DL, Impl);
62
63 // TODO: Declare with correct type, calling convention, and attributes.
64 if (!FuncTy)
65 FuncTy = FunctionType::get(Type::getVoidTy(Ctx), {}, /*IsVarArgs=*/true);
66
67 StringRef FuncName = RTLCI.getLibcallImplName(Impl);
68
69 Function *Func =
70 cast<Function>(M.getOrInsertFunction(FuncName, FuncTy).getCallee());
71 if (Func->getFunctionType() == FuncTy) {
72 mergeAttributes(Ctx, M, DL, TT, Func, FuncTy, FuncAttrs);
73 Func->setCallingConv(RTLCI.getLibcallImplCallingConv(Impl));
74 }
75 }
76
78}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void mergeAttributes(LLVMContext &Ctx, const Module &M, const DataLayout &DL, const Triple &TT, Function *Func, FunctionType *FuncTy, AttributeList FuncAttrs)
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:58
ModuleAnalysisManager MAM
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:281
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A simple container for information about the supported runtime calls.
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
std::pair< FunctionType *, AttributeList > getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL, RTLIB::LibcallImpl LibcallImpl) const
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
ArrayRef< RTLIB::LibcallImpl > getLibcallImpls() const