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 /// Return the implementation for \p Call, or for its masked variant if \p
65 /// Call has no implementation.
66 ///
67 /// FIXME: Callers should explicitly try the masked variant and track whether
68 /// a mask argument is needed, instead of re-deriving it from the result with
69 /// hasVectorMaskArgument.
70 RTLIB::LibcallImpl getVectorLibcallImplOrMasked(RTLIB::Libcall Call) const {
71 RTLIB::LibcallImpl Impl = getLibcallImpl(Call);
72 if (Impl != RTLIB::Unsupported)
73 return Impl;
74 return getLibcallImpl(
76 }
77
78 /// Remap the default libcall routine name for the specified libcall.
79 void setLibcallImpl(RTLIB::Libcall Call, RTLIB::LibcallImpl Impl) {
80 LibcallImpls[Call] = Impl;
81 }
82
83 // FIXME: Remove this wrapper in favor of directly using
84 // getLibcallImplCallingConv
86 return RTLCI.LibcallImplCallingConvs[LibcallImpls[Call]];
87 }
88
89 /// Get the CallingConv that should be used for the specified libcall.
91 return RTLCI.LibcallImplCallingConvs[Call];
92 }
93
94 /// Return a function impl compatible with RTLIB::MEMCPY, or
95 /// RTLIB::Unsupported if fully unsupported.
96 RTLIB::LibcallImpl getMemcpyImpl() const {
97 RTLIB::LibcallImpl Memcpy = getLibcallImpl(RTLIB::MEMCPY);
98 if (Memcpy == RTLIB::Unsupported) {
99 // Fallback to memmove if memcpy isn't available.
100 return getLibcallImpl(RTLIB::MEMMOVE);
101 }
102
103 return Memcpy;
104 }
105};
106
107/// Records a mapping from an opaque lowering context to its
108/// LibcallLoweringInfo.
109///
110/// The context is identified by an opaque key (which should be
111/// TargetSubtargetInfo*)
113private:
114 using LibcallLoweringMap = DenseMap<const void *, LibcallLoweringInfo>;
115 mutable LibcallLoweringMap LoweringMap;
116 const RTLIB::RuntimeLibcallsInfo *RTLCI = nullptr;
117
118public:
122
123 void init(const RTLIB::RuntimeLibcallsInfo *RT) { RTLCI = RT; }
124
125 void clear() {
126 RTLCI = nullptr;
127 LoweringMap.clear();
128 }
129
130 operator bool() const { return RTLCI != nullptr; }
131
133 ModuleAnalysisManager::Invalidator &);
134
135 /// Return the LibcallLoweringInfo for the context identified by \p Key,
136 /// creating it (via \p ApplyContextRules) on first request. \p Key should be
137 /// TargetSubtargetInfo*.
138 template <typename KeyT>
140 const KeyT *Key,
141 LibcallLoweringInfo::ApplyContextRulesFn ApplyContextRules = {}) const {
142 return getLibcallLoweringForKey(static_cast<const void *>(Key),
143 ApplyContextRules);
144 }
145
146private:
147 const LibcallLoweringInfo &getLibcallLoweringForKey(
148 const void *Key,
149 LibcallLoweringInfo::ApplyContextRulesFn ApplyContextRules) const {
150 auto It = LoweringMap.find(Key);
151 if (It != LoweringMap.end())
152 return It->second;
153 return LoweringMap.try_emplace(Key, *RTLCI, ApplyContextRules)
154 .first->second;
155 }
156};
157
159 : public AnalysisInfoMixin<LibcallLoweringModuleAnalysis> {
160private:
162 LLVM_ABI static AnalysisKey Key;
163
164 ModuleLibcallLoweringInfo LibcallLoweringMap;
165
166public:
168
170};
171
172} // end namespace llvm
173
174#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:766
iterator end()
Definition DenseMap.h:686
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:856
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...
RTLIB::LibcallImpl getVectorLibcallImplOrMasked(RTLIB::Libcall Call) const
Return the implementation for Call, or for its masked variant if Call has no implementation.
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:68
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.
static LLVM_ABI RTLIB::Libcall getMaskedVectorLibcall(RTLIB::Libcall LC)
Return the masked variant of the vector libcall LC, or RTLIB::UNKNOWN_LIBCALL if LC has no masked var...