1 //===-- TargetLibraryInfo.h - Library information ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
11 #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Pass.h"
21 class PreservedAnalyses;
25 #define TLI_DEFINE_ENUM
26 #include "llvm/Analysis/TargetLibraryInfo.def"
32 /// \brief Implementation of the target library information.
34 /// This class constructs tables that hold the target library information and
35 /// make it available. However, it is somewhat expensive to compute and only
36 /// depends on the triple. So users typicaly interact with the \c
37 /// TargetLibraryInfo wrapper below.
38 class TargetLibraryInfoImpl {
39 friend class TargetLibraryInfo;
41 unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
42 llvm::DenseMap<unsigned, std::string> CustomNames;
43 static const char *const StandardNames[LibFunc::NumLibFuncs];
45 enum AvailabilityState {
46 StandardName = 3, // (memset to all ones)
48 Unavailable = 0 // (memset to all zeros)
50 void setState(LibFunc::Func F, AvailabilityState State) {
51 AvailableArray[F/4] &= ~(3 << 2*(F&3));
52 AvailableArray[F/4] |= State << 2*(F&3);
54 AvailabilityState getState(LibFunc::Func F) const {
55 return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
59 TargetLibraryInfoImpl();
60 explicit TargetLibraryInfoImpl(const Triple &T);
62 // Provide value semantics.
63 TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
64 TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
65 TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
66 TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
68 /// \brief Searches for a particular function name.
70 /// If it is one of the known library functions, return true and set F to the
71 /// corresponding value.
72 bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
74 /// \brief Forces a function to be marked as unavailable.
75 void setUnavailable(LibFunc::Func F) {
76 setState(F, Unavailable);
79 /// \brief Forces a function to be marked as available.
80 void setAvailable(LibFunc::Func F) {
81 setState(F, StandardName);
84 /// \brief Forces a function to be marked as available and provide an
85 /// alternate name that must be used.
86 void setAvailableWithName(LibFunc::Func F, StringRef Name) {
87 if (StandardNames[F] != Name) {
88 setState(F, CustomName);
89 CustomNames[F] = Name;
90 assert(CustomNames.find(F) != CustomNames.end());
92 setState(F, StandardName);
96 /// \brief Disables all builtins.
98 /// This can be used for options like -fno-builtin.
99 void disableAllFunctions();
102 /// \brief Provides information about what library functions are available for
103 /// the current target.
105 /// This both allows optimizations to handle them specially and frontends to
106 /// disable such optimizations through -fno-builtin etc.
107 class TargetLibraryInfo {
108 friend class TargetLibraryAnalysis;
109 friend class TargetLibraryInfoWrapperPass;
111 const TargetLibraryInfoImpl *Impl;
114 explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
116 // Provide value semantics.
117 TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
118 TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
119 TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
123 TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
128 /// \brief Searches for a particular function name.
130 /// If it is one of the known library functions, return true and set F to the
131 /// corresponding value.
132 bool getLibFunc(StringRef funcName, LibFunc::Func &F) const {
133 return Impl->getLibFunc(funcName, F);
136 /// \brief Tests whether a library function is available.
137 bool has(LibFunc::Func F) const {
138 return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
141 /// \brief Tests if the function is both available and a candidate for
142 /// optimized code generation.
143 bool hasOptimizedCodeGen(LibFunc::Func F) const {
144 if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
148 case LibFunc::copysign: case LibFunc::copysignf: case LibFunc::copysignl:
149 case LibFunc::fabs: case LibFunc::fabsf: case LibFunc::fabsl:
150 case LibFunc::sin: case LibFunc::sinf: case LibFunc::sinl:
151 case LibFunc::cos: case LibFunc::cosf: case LibFunc::cosl:
152 case LibFunc::sqrt: case LibFunc::sqrtf: case LibFunc::sqrtl:
153 case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
154 case LibFunc::sqrtl_finite:
155 case LibFunc::fmax: case LibFunc::fmaxf: case LibFunc::fmaxl:
156 case LibFunc::fmin: case LibFunc::fminf: case LibFunc::fminl:
157 case LibFunc::floor: case LibFunc::floorf: case LibFunc::floorl:
158 case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
159 case LibFunc::ceil: case LibFunc::ceilf: case LibFunc::ceill:
160 case LibFunc::rint: case LibFunc::rintf: case LibFunc::rintl:
161 case LibFunc::round: case LibFunc::roundf: case LibFunc::roundl:
162 case LibFunc::trunc: case LibFunc::truncf: case LibFunc::truncl:
163 case LibFunc::log2: case LibFunc::log2f: case LibFunc::log2l:
164 case LibFunc::exp2: case LibFunc::exp2f: case LibFunc::exp2l:
165 case LibFunc::memcmp: case LibFunc::strcmp: case LibFunc::strcpy:
166 case LibFunc::stpcpy: case LibFunc::strlen: case LibFunc::strnlen:
167 case LibFunc::memchr:
173 StringRef getName(LibFunc::Func F) const {
174 auto State = Impl->getState(F);
175 if (State == TargetLibraryInfoImpl::Unavailable)
177 if (State == TargetLibraryInfoImpl::StandardName)
178 return Impl->StandardNames[F];
179 assert(State == TargetLibraryInfoImpl::CustomName);
180 return Impl->CustomNames.find(F)->second;
183 /// \brief Handle invalidation from the pass manager.
185 /// If we try to invalidate this info, just return false. It cannot become
186 /// invalid even if the module changes.
187 bool invalidate(Module &, const PreservedAnalyses &) { return false; }
190 /// \brief Analysis pass providing the \c TargetLibraryInfo.
192 /// Note that this pass's result cannot be invalidated, it is immutable for the
193 /// life of the module.
194 class TargetLibraryAnalysis {
196 typedef TargetLibraryInfo Result;
198 /// \brief Opaque, unique identifier for this analysis pass.
199 static void *ID() { return (void *)&PassID; }
201 /// \brief Default construct the library analysis.
203 /// This will use the module's triple to construct the library info for that
205 TargetLibraryAnalysis() {}
207 /// \brief Construct a library analysis with preset info.
209 /// This will directly copy the preset info into the result without
210 /// consulting the module's triple.
211 TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
212 : PresetInfoImpl(std::move(PresetInfoImpl)) {}
214 // Move semantics. We spell out the constructors for MSVC.
215 TargetLibraryAnalysis(TargetLibraryAnalysis &&Arg)
216 : PresetInfoImpl(std::move(Arg.PresetInfoImpl)), Impls(std::move(Arg.Impls)) {}
217 TargetLibraryAnalysis &operator=(TargetLibraryAnalysis &&RHS) {
218 PresetInfoImpl = std::move(RHS.PresetInfoImpl);
219 Impls = std::move(RHS.Impls);
223 TargetLibraryInfo run(Module &M);
224 TargetLibraryInfo run(Function &F);
226 /// \brief Provide access to a name for this pass for debugging purposes.
227 static StringRef name() { return "TargetLibraryAnalysis"; }
232 Optional<TargetLibraryInfoImpl> PresetInfoImpl;
234 StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
236 TargetLibraryInfoImpl &lookupInfoImpl(Triple T);
239 class TargetLibraryInfoWrapperPass : public ImmutablePass {
240 TargetLibraryInfoImpl TLIImpl;
241 TargetLibraryInfo TLI;
243 virtual void anchor();
247 TargetLibraryInfoWrapperPass();
248 explicit TargetLibraryInfoWrapperPass(const Triple &T);
249 explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
251 TargetLibraryInfo &getTLI() { return TLI; }
252 const TargetLibraryInfo &getTLI() const { return TLI; }
255 } // end namespace llvm