LLVM API Documentation

TargetLibraryInfo.h
Go to the documentation of this file.
00001 //===-- TargetLibraryInfo.h - Library information ---------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
00011 #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
00012 
00013 #include "llvm/ADT/DenseMap.h"
00014 #include "llvm/ADT/Optional.h"
00015 #include "llvm/ADT/Triple.h"
00016 #include "llvm/IR/Function.h"
00017 #include "llvm/IR/Module.h"
00018 #include "llvm/Pass.h"
00019 
00020 namespace llvm {
00021 class PreservedAnalyses;
00022 
00023   namespace LibFunc {
00024     enum Func {
00025 #define TLI_DEFINE_ENUM
00026 #include "llvm/Analysis/TargetLibraryInfo.def"
00027 
00028       NumLibFuncs
00029     };
00030   }
00031 
00032 /// \brief Implementation of the target library information.
00033 ///
00034 /// This class constructs tables that hold the target library information and
00035 /// make it available. However, it is somewhat expensive to compute and only
00036 /// depends on the triple. So users typicaly interact with the \c
00037 /// TargetLibraryInfo wrapper below.
00038 class TargetLibraryInfoImpl {
00039   friend class TargetLibraryInfo;
00040 
00041   unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
00042   llvm::DenseMap<unsigned, std::string> CustomNames;
00043   static const char* StandardNames[LibFunc::NumLibFuncs];
00044 
00045   enum AvailabilityState {
00046     StandardName = 3, // (memset to all ones)
00047     CustomName = 1,
00048     Unavailable = 0  // (memset to all zeros)
00049   };
00050   void setState(LibFunc::Func F, AvailabilityState State) {
00051     AvailableArray[F/4] &= ~(3 << 2*(F&3));
00052     AvailableArray[F/4] |= State << 2*(F&3);
00053   }
00054   AvailabilityState getState(LibFunc::Func F) const {
00055     return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
00056   }
00057 
00058 public:
00059   TargetLibraryInfoImpl();
00060   explicit TargetLibraryInfoImpl(const Triple &T);
00061 
00062   // Provide value semantics.
00063   TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
00064   TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
00065   TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
00066   TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
00067 
00068   /// \brief Searches for a particular function name.
00069   ///
00070   /// If it is one of the known library functions, return true and set F to the
00071   /// corresponding value.
00072   bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
00073 
00074   /// \brief Forces a function to be marked as unavailable.
00075   void setUnavailable(LibFunc::Func F) {
00076     setState(F, Unavailable);
00077   }
00078 
00079   /// \brief Forces a function to be marked as available.
00080   void setAvailable(LibFunc::Func F) {
00081     setState(F, StandardName);
00082   }
00083 
00084   /// \brief Forces a function to be marked as available and provide an
00085   /// alternate name that must be used.
00086   void setAvailableWithName(LibFunc::Func F, StringRef Name) {
00087     if (StandardNames[F] != Name) {
00088       setState(F, CustomName);
00089       CustomNames[F] = Name;
00090       assert(CustomNames.find(F) != CustomNames.end());
00091     } else {
00092       setState(F, StandardName);
00093     }
00094   }
00095 
00096   /// \brief Disables all builtins.
00097   ///
00098   /// This can be used for options like -fno-builtin.
00099   void disableAllFunctions();
00100 };
00101 
00102 /// \brief Provides information about what library functions are available for
00103 /// the current target.
00104 ///
00105 /// This both allows optimizations to handle them specially and frontends to
00106 /// disable such optimizations through -fno-builtin etc.
00107 class TargetLibraryInfo {
00108   friend class TargetLibraryAnalysis;
00109   friend class TargetLibraryInfoWrapperPass;
00110 
00111   const TargetLibraryInfoImpl *Impl;
00112 
00113 public:
00114   explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
00115 
00116   // Provide value semantics.
00117   TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
00118   TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
00119   TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
00120     Impl = TLI.Impl;
00121     return *this;
00122   }
00123   TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
00124     Impl = TLI.Impl;
00125     return *this;
00126   }
00127 
00128   /// \brief Searches for a particular function name.
00129   ///
00130   /// If it is one of the known library functions, return true and set F to the
00131   /// corresponding value.
00132   bool getLibFunc(StringRef funcName, LibFunc::Func &F) const {
00133     return Impl->getLibFunc(funcName, F);
00134   }
00135 
00136   /// \brief Tests wether a library function is available.
00137   bool has(LibFunc::Func F) const {
00138     return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
00139   }
00140 
00141   /// \brief Tests if the function is both available and a candidate for
00142   /// optimized code generation.
00143   bool hasOptimizedCodeGen(LibFunc::Func F) const {
00144     if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
00145       return false;
00146     switch (F) {
00147     default: break;
00148     case LibFunc::copysign:  case LibFunc::copysignf:  case LibFunc::copysignl:
00149     case LibFunc::fabs:      case LibFunc::fabsf:      case LibFunc::fabsl:
00150     case LibFunc::sin:       case LibFunc::sinf:       case LibFunc::sinl:
00151     case LibFunc::cos:       case LibFunc::cosf:       case LibFunc::cosl:
00152     case LibFunc::sqrt:      case LibFunc::sqrtf:      case LibFunc::sqrtl:
00153     case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
00154                                                   case LibFunc::sqrtl_finite:
00155     case LibFunc::fmax:      case LibFunc::fmaxf:      case LibFunc::fmaxl:
00156     case LibFunc::fmin:      case LibFunc::fminf:      case LibFunc::fminl:
00157     case LibFunc::floor:     case LibFunc::floorf:     case LibFunc::floorl:
00158     case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
00159     case LibFunc::ceil:      case LibFunc::ceilf:      case LibFunc::ceill:
00160     case LibFunc::rint:      case LibFunc::rintf:      case LibFunc::rintl:
00161     case LibFunc::round:     case LibFunc::roundf:     case LibFunc::roundl:
00162     case LibFunc::trunc:     case LibFunc::truncf:     case LibFunc::truncl:
00163     case LibFunc::log2:      case LibFunc::log2f:      case LibFunc::log2l:
00164     case LibFunc::exp2:      case LibFunc::exp2f:      case LibFunc::exp2l:
00165     case LibFunc::memcmp:    case LibFunc::strcmp:     case LibFunc::strcpy:
00166     case LibFunc::stpcpy:    case LibFunc::strlen:     case LibFunc::strnlen:
00167     case LibFunc::memchr:
00168       return true;
00169     }
00170     return false;
00171   }
00172 
00173   StringRef getName(LibFunc::Func F) const {
00174     auto State = Impl->getState(F);
00175     if (State == TargetLibraryInfoImpl::Unavailable)
00176       return StringRef();
00177     if (State == TargetLibraryInfoImpl::StandardName)
00178       return Impl->StandardNames[F];
00179     assert(State == TargetLibraryInfoImpl::CustomName);
00180     return Impl->CustomNames.find(F)->second;
00181   }
00182 
00183   /// \brief Handle invalidation from the pass manager.
00184   ///
00185   /// If we try to invalidate this info, just return false. It cannot become
00186   /// invalid even if the module changes.
00187   bool invalidate(Module &, const PreservedAnalyses &) { return false; }
00188 };
00189 
00190 /// \brief Analysis pass providing the \c TargetLibraryInfo.
00191 ///
00192 /// Note that this pass's result cannot be invalidated, it is immutable for the
00193 /// life of the module.
00194 class TargetLibraryAnalysis {
00195 public:
00196   typedef TargetLibraryInfo Result;
00197 
00198   /// \brief Opaque, unique identifier for this analysis pass.
00199   static void *ID() { return (void *)&PassID; }
00200 
00201   /// \brief Default construct the library analysis.
00202   ///
00203   /// This will use the module's triple to construct the library info for that
00204   /// module.
00205   TargetLibraryAnalysis() {}
00206 
00207   /// \brief Construct a library analysis with preset info.
00208   ///
00209   /// This will directly copy the preset info into the result without
00210   /// consulting the module's triple.
00211   TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
00212       : PresetInfoImpl(std::move(PresetInfoImpl)) {}
00213 
00214   // Move semantics. We spell out the constructors for MSVC.
00215   TargetLibraryAnalysis(TargetLibraryAnalysis &&Arg)
00216       : PresetInfoImpl(std::move(Arg.PresetInfoImpl)), Impls(std::move(Arg.Impls)) {}
00217   TargetLibraryAnalysis &operator=(TargetLibraryAnalysis &&RHS) {
00218     PresetInfoImpl = std::move(RHS.PresetInfoImpl);
00219     Impls = std::move(RHS.Impls);
00220     return *this;
00221   }
00222 
00223   TargetLibraryInfo run(Module &M);
00224   TargetLibraryInfo run(Function &F);
00225 
00226   /// \brief Provide access to a name for this pass for debugging purposes.
00227   static StringRef name() { return "TargetLibraryAnalysis"; }
00228 
00229 private:
00230   static char PassID;
00231 
00232   Optional<TargetLibraryInfoImpl> PresetInfoImpl;
00233 
00234   StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
00235 
00236   TargetLibraryInfoImpl &lookupInfoImpl(Triple T);
00237 };
00238 
00239 class TargetLibraryInfoWrapperPass : public ImmutablePass {
00240   TargetLibraryInfoImpl TLIImpl;
00241   TargetLibraryInfo TLI;
00242 
00243   virtual void anchor();
00244 
00245 public:
00246   static char ID;
00247   TargetLibraryInfoWrapperPass();
00248   explicit TargetLibraryInfoWrapperPass(const Triple &T);
00249   explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
00250 
00251   TargetLibraryInfo &getTLI() { return TLI; }
00252   const TargetLibraryInfo &getTLI() const { return TLI; }
00253 };
00254 
00255 } // end namespace llvm
00256 
00257 #endif