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#include <memory>
24
25namespace llvm {
26
27/// Tracks which library functions to use for a particular subtarget or
28/// function.
30private:
31 const RTLIB::RuntimeLibcallsInfo &RTLCI;
32 /// Stores the implementation choice for each libcall.
33 RTLIB::LibcallImpl LibcallImpls[RTLIB::UNKNOWN_LIBCALL + 1] = {
34 RTLIB::Unsupported};
35
36public:
37 /// Callback applying the caller's context-specific (e.g. subtarget) libcall
38 /// rules on top of the module-level defaults.
40
41 /// Construct the lowering info from the module-level \p RTLCI, seeding the
42 /// default implementation for every available libcall, then applying the
43 /// optional \p ApplyContextRules callback for the caller's context-specific
44 /// libcall rules.
46 ApplyContextRulesFn ApplyContextRules = {});
47
49 return RTLCI;
50 }
51
52 /// Get the libcall routine name for the specified libcall.
53 // FIXME: This should be removed. Only LibcallImpl should have a name.
54 const char *getLibcallName(RTLIB::Libcall Call) const {
55 // FIXME: Return StringRef
57 .data();
58 }
59
60 /// Return the lowering's selection of implementation call for \p Call
61 RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const {
62 return LibcallImpls[Call];
63 }
64
65 /// Return the implementation for \p Call, or for its masked variant if \p
66 /// Call has no implementation.
67 ///
68 /// FIXME: Callers should explicitly try the masked variant and track whether
69 /// a mask argument is needed, instead of re-deriving it from the result with
70 /// hasVectorMaskArgument.
71 RTLIB::LibcallImpl getVectorLibcallImplOrMasked(RTLIB::Libcall Call) const {
72 RTLIB::LibcallImpl Impl = getLibcallImpl(Call);
73 if (Impl != RTLIB::Unsupported)
74 return Impl;
75 return getLibcallImpl(
77 }
78
79 /// Remap the default libcall routine name for the specified libcall.
80 void setLibcallImpl(RTLIB::Libcall Call, RTLIB::LibcallImpl Impl) {
81 LibcallImpls[Call] = Impl;
82 }
83
84 // FIXME: Remove this wrapper in favor of directly using
85 // getLibcallImplCallingConv
87 return RTLCI.LibcallImplCallingConvs[LibcallImpls[Call]];
88 }
89
90 /// Get the CallingConv that should be used for the specified libcall.
92 return RTLCI.LibcallImplCallingConvs[Call];
93 }
94
95 /// Return a function impl compatible with RTLIB::MEMCPY, or
96 /// RTLIB::Unsupported if fully unsupported.
97 RTLIB::LibcallImpl getMemcpyImpl() const {
98 RTLIB::LibcallImpl Memcpy = getLibcallImpl(RTLIB::MEMCPY);
99 if (Memcpy == RTLIB::Unsupported) {
100 // Fallback to memmove if memcpy isn't available.
101 return getLibcallImpl(RTLIB::MEMMOVE);
102 }
103
104 return Memcpy;
105 }
106};
107
108/// Records a mapping from an opaque lowering context to its
109/// LibcallLoweringInfo.
110///
111/// The context is identified by an opaque key (which should be
112/// TargetSubtargetInfo*)
114private:
115 using LibcallLoweringMap =
117 mutable LibcallLoweringMap LoweringMap;
118 const RTLIB::RuntimeLibcallsInfo *RTLCI = nullptr;
119
120public:
124
125 void init(const RTLIB::RuntimeLibcallsInfo *RT) { RTLCI = RT; }
126
127 void clear() {
128 RTLCI = nullptr;
129 LoweringMap.clear();
130 }
131
132 operator bool() const { return RTLCI != nullptr; }
133
135 ModuleAnalysisManager::Invalidator &);
136
137 /// Return the LibcallLoweringInfo for the context identified by \p Key,
138 /// creating it (via \p ApplyContextRules) on first request. \p Key should be
139 /// TargetSubtargetInfo*.
140 template <typename KeyT>
142 const KeyT *Key,
143 LibcallLoweringInfo::ApplyContextRulesFn ApplyContextRules = {}) const {
144 return getLibcallLoweringForKey(static_cast<const void *>(Key),
145 ApplyContextRules);
146 }
147
148private:
149 const LibcallLoweringInfo &getLibcallLoweringForKey(
150 const void *Key,
151 LibcallLoweringInfo::ApplyContextRulesFn ApplyContextRules) const {
152 std::unique_ptr<LibcallLoweringInfo> &Entry = LoweringMap[Key];
153 if (!Entry)
154 Entry = std::make_unique<LibcallLoweringInfo>(*RTLCI, ApplyContextRules);
155 return *Entry;
156 }
157};
158
160 : public AnalysisInfoMixin<LibcallLoweringModuleAnalysis> {
161private:
163 LLVM_ABI static AnalysisKey Key;
164
165public:
167
169};
170
171} // end namespace llvm
172
173#endif // LLVM_ANALYSIS_LIBCALLLOWERINGINFO_H
#define LLVM_ABI
Definition Compiler.h:211
This file defines the DenseMap class.
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...