LLVM 24.0.0git
LibcallLoweringInfo.h
Go to the documentation of this file.
1//===- LibcallLoweringInfo.h ------------------------------------*- C++ -*-===//
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// Tracks which library function implementations to use for depending on a
10// calling context (e.g., which library function a particular subtarget should
11// use).
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ANALYSIS_LIBCALLLOWERINGINFO_H
16#define LLVM_ANALYSIS_LIBCALLLOWERINGINFO_H
17
18#include "llvm/ADT/DenseMap.h"
22#include "llvm/Pass.h"
23
24namespace llvm {
25
26/// Tracks which library functions to use for a particular subtarget or
27/// function.
29private:
30 const RTLIB::RuntimeLibcallsInfo &RTLCI;
31 /// Stores the implementation choice for each libcall.
32 RTLIB::LibcallImpl LibcallImpls[RTLIB::UNKNOWN_LIBCALL + 1] = {
33 RTLIB::Unsupported};
34
35public:
36 /// Callback applying the caller's context-specific (e.g. subtarget) libcall
37 /// rules on top of the module-level defaults.
39
40 /// Construct the lowering info from the module-level \p RTLCI, seeding the
41 /// default implementation for every available libcall, then applying the
42 /// optional \p ApplyContextRules callback for the caller's context-specific
43 /// libcall rules.
45 ApplyContextRulesFn ApplyContextRules = {});
46
48 return RTLCI;
49 }
50
51 /// Get the libcall routine name for the specified libcall.
52 // FIXME: This should be removed. Only LibcallImpl should have a name.
53 const char *getLibcallName(RTLIB::Libcall Call) const {
54 // FIXME: Return StringRef
56 .data();
57 }
58
59 /// Return the lowering's selection of implementation call for \p Call
60 RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const {
61 return LibcallImpls[Call];
62 }
63
64 /// Remap the default libcall routine name for the specified libcall.
65 void setLibcallImpl(RTLIB::Libcall Call, RTLIB::LibcallImpl Impl) {
66 LibcallImpls[Call] = Impl;
67 }
68
69 // FIXME: Remove this wrapper in favor of directly using
70 // getLibcallImplCallingConv
72 return RTLCI.LibcallImplCallingConvs[LibcallImpls[Call]];
73 }
74
75 /// Get the CallingConv that should be used for the specified libcall.
77 return RTLCI.LibcallImplCallingConvs[Call];
78 }
79
80 /// Return a function impl compatible with RTLIB::MEMCPY, or
81 /// RTLIB::Unsupported if fully unsupported.
82 RTLIB::LibcallImpl getMemcpyImpl() const {
83 RTLIB::LibcallImpl Memcpy = getLibcallImpl(RTLIB::MEMCPY);
84 if (Memcpy == RTLIB::Unsupported) {
85 // Fallback to memmove if memcpy isn't available.
86 return getLibcallImpl(RTLIB::MEMMOVE);
87 }
88
89 return Memcpy;
90 }
91};
92
93/// Records a mapping from an opaque lowering context to its
94/// LibcallLoweringInfo.
95///
96/// The context is identified by an opaque key (which should be
97/// TargetSubtargetInfo*)
99private:
100 using LibcallLoweringMap = DenseMap<const void *, LibcallLoweringInfo>;
101 mutable LibcallLoweringMap LoweringMap;
102 const RTLIB::RuntimeLibcallsInfo *RTLCI = nullptr;
103
104public:
108
109 void init(const RTLIB::RuntimeLibcallsInfo *RT) { RTLCI = RT; }
110
111 void clear() {
112 RTLCI = nullptr;
113 LoweringMap.clear();
114 }
115
116 operator bool() const { return RTLCI != nullptr; }
117
119 ModuleAnalysisManager::Invalidator &);
120
121 /// Return the LibcallLoweringInfo for the context identified by \p Key,
122 /// creating it (via \p ApplyContextRules) on first request. \p Key should be
123 /// TargetSubtargetInfo*.
124 template <typename KeyT>
126 const KeyT *Key,
127 LibcallLoweringInfo::ApplyContextRulesFn ApplyContextRules = {}) const {
128 return getLibcallLoweringForKey(static_cast<const void *>(Key),
129 ApplyContextRules);
130 }
131
132private:
133 const LibcallLoweringInfo &getLibcallLoweringForKey(
134 const void *Key,
135 LibcallLoweringInfo::ApplyContextRulesFn ApplyContextRules) const {
136 auto It = LoweringMap.find(Key);
137 if (It != LoweringMap.end())
138 return It->second;
139 return LoweringMap.try_emplace(Key, *RTLCI, ApplyContextRules)
140 .first->second;
141 }
142};
143
145 : public AnalysisInfoMixin<LibcallLoweringModuleAnalysis> {
146private:
148 LLVM_ABI static AnalysisKey Key;
149
150 ModuleLibcallLoweringInfo LibcallLoweringMap;
151
152public:
154
156};
157
158} // end namespace llvm
159
160#endif // LLVM_ANALYSIS_LIBCALLLOWERINGINFO_H
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:223
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:299
iterator end()
Definition DenseMap.h:141
Tracks which library functions to use for a particular subtarget or function.
const RTLIB::RuntimeLibcallsInfo & getRuntimeLibcallsInfo() const
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const
function_ref< void(LibcallLoweringInfo &)> ApplyContextRulesFn
Callback applying the caller's context-specific (e.g.
RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const
Return the lowering's selection of implementation call for Call.
LLVM_ABI LibcallLoweringInfo(const RTLIB::RuntimeLibcallsInfo &RTLCI, ApplyContextRulesFn ApplyContextRules={})
Construct the lowering info from the module-level RTLCI, seeding the default implementation for every...
void setLibcallImpl(RTLIB::Libcall Call, RTLIB::LibcallImpl Impl)
Remap the default libcall routine name for the specified libcall.
RTLIB::LibcallImpl getMemcpyImpl() const
Return a function impl compatible with RTLIB::MEMCPY, or RTLIB::Unsupported if fully unsupported.
LLVM_ABI Result run(Module &M, ModuleAnalysisManager &)
Records a mapping from an opaque lowering context to its LibcallLoweringInfo.
void init(const RTLIB::RuntimeLibcallsInfo *RT)
const LibcallLoweringInfo & getLibcallLowering(const KeyT *Key, LibcallLoweringInfo::ApplyContextRulesFn ApplyContextRules={}) const
Return the LibcallLoweringInfo for the context identified by Key, creating it (via ApplyContextRules)...
ModuleLibcallLoweringInfo(RTLIB::RuntimeLibcallsInfo &RTLCI)
LLVM_ABI bool invalidate(Module &, const PreservedAnalyses &, ModuleAnalysisManager::Invalidator &)
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
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
An efficient, type-erasing, non-owning reference to a callable.
CallInst * Call
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A simple container for information about the supported runtime calls.
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.