Make static variables const if possible. Makes them go into a read-only section.
[oota-llvm.git] / include / llvm / Analysis / TargetLibraryInfo.h
1 //===-- TargetLibraryInfo.h - Library information ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
11 #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
12
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"
19
20 namespace llvm {
21 class PreservedAnalyses;
22
23   namespace LibFunc {
24     enum Func {
25 #define TLI_DEFINE_ENUM
26 #include "llvm/Analysis/TargetLibraryInfo.def"
27
28       NumLibFuncs
29     };
30   }
31
32 /// \brief Implementation of the target library information.
33 ///
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;
40
41   unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
42   llvm::DenseMap<unsigned, std::string> CustomNames;
43   static const char *const StandardNames[LibFunc::NumLibFuncs];
44
45   enum AvailabilityState {
46     StandardName = 3, // (memset to all ones)
47     CustomName = 1,
48     Unavailable = 0  // (memset to all zeros)
49   };
50   void setState(LibFunc::Func F, AvailabilityState State) {
51     AvailableArray[F/4] &= ~(3 << 2*(F&3));
52     AvailableArray[F/4] |= State << 2*(F&3);
53   }
54   AvailabilityState getState(LibFunc::Func F) const {
55     return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
56   }
57
58 public:
59   TargetLibraryInfoImpl();
60   explicit TargetLibraryInfoImpl(const Triple &T);
61
62   // Provide value semantics.
63   TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
64   TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
65   TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
66   TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
67
68   /// \brief Searches for a particular function name.
69   ///
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;
73
74   /// \brief Forces a function to be marked as unavailable.
75   void setUnavailable(LibFunc::Func F) {
76     setState(F, Unavailable);
77   }
78
79   /// \brief Forces a function to be marked as available.
80   void setAvailable(LibFunc::Func F) {
81     setState(F, StandardName);
82   }
83
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());
91     } else {
92       setState(F, StandardName);
93     }
94   }
95
96   /// \brief Disables all builtins.
97   ///
98   /// This can be used for options like -fno-builtin.
99   void disableAllFunctions();
100 };
101
102 /// \brief Provides information about what library functions are available for
103 /// the current target.
104 ///
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;
110
111   const TargetLibraryInfoImpl *Impl;
112
113 public:
114   explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
115
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) {
120     Impl = TLI.Impl;
121     return *this;
122   }
123   TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
124     Impl = TLI.Impl;
125     return *this;
126   }
127
128   /// \brief Searches for a particular function name.
129   ///
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);
134   }
135
136   /// \brief Tests whether a library function is available.
137   bool has(LibFunc::Func F) const {
138     return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
139   }
140
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)
145       return false;
146     switch (F) {
147     default: break;
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:
168       return true;
169     }
170     return false;
171   }
172
173   StringRef getName(LibFunc::Func F) const {
174     auto State = Impl->getState(F);
175     if (State == TargetLibraryInfoImpl::Unavailable)
176       return StringRef();
177     if (State == TargetLibraryInfoImpl::StandardName)
178       return Impl->StandardNames[F];
179     assert(State == TargetLibraryInfoImpl::CustomName);
180     return Impl->CustomNames.find(F)->second;
181   }
182
183   /// \brief Handle invalidation from the pass manager.
184   ///
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; }
188 };
189
190 /// \brief Analysis pass providing the \c TargetLibraryInfo.
191 ///
192 /// Note that this pass's result cannot be invalidated, it is immutable for the
193 /// life of the module.
194 class TargetLibraryAnalysis {
195 public:
196   typedef TargetLibraryInfo Result;
197
198   /// \brief Opaque, unique identifier for this analysis pass.
199   static void *ID() { return (void *)&PassID; }
200
201   /// \brief Default construct the library analysis.
202   ///
203   /// This will use the module's triple to construct the library info for that
204   /// module.
205   TargetLibraryAnalysis() {}
206
207   /// \brief Construct a library analysis with preset info.
208   ///
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)) {}
213
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);
220     return *this;
221   }
222
223   TargetLibraryInfo run(Module &M);
224   TargetLibraryInfo run(Function &F);
225
226   /// \brief Provide access to a name for this pass for debugging purposes.
227   static StringRef name() { return "TargetLibraryAnalysis"; }
228
229 private:
230   static char PassID;
231
232   Optional<TargetLibraryInfoImpl> PresetInfoImpl;
233
234   StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
235
236   TargetLibraryInfoImpl &lookupInfoImpl(Triple T);
237 };
238
239 class TargetLibraryInfoWrapperPass : public ImmutablePass {
240   TargetLibraryInfoImpl TLIImpl;
241   TargetLibraryInfo TLI;
242
243   virtual void anchor();
244
245 public:
246   static char ID;
247   TargetLibraryInfoWrapperPass();
248   explicit TargetLibraryInfoWrapperPass(const Triple &T);
249   explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
250
251   TargetLibraryInfo &getTLI() { return TLI; }
252   const TargetLibraryInfo &getTLI() const { return TLI; }
253 };
254
255 } // end namespace llvm
256
257 #endif