LLVM  mainline
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/ArrayRef.h"
00015 #include "llvm/ADT/Optional.h"
00016 #include "llvm/ADT/Triple.h"
00017 #include "llvm/IR/Function.h"
00018 #include "llvm/IR/Module.h"
00019 #include "llvm/Pass.h"
00020 
00021 namespace llvm {
00022 /// VecDesc - Describes a possible vectorization of a function.
00023 /// Function 'VectorFnName' is equivalent to 'ScalarFnName' vectorized
00024 /// by a factor 'VectorizationFactor'.
00025 struct VecDesc {
00026   const char *ScalarFnName;
00027   const char *VectorFnName;
00028   unsigned VectorizationFactor;
00029 };
00030 class PreservedAnalyses;
00031 
00032   namespace LibFunc {
00033     enum Func {
00034 #define TLI_DEFINE_ENUM
00035 #include "llvm/Analysis/TargetLibraryInfo.def"
00036 
00037       NumLibFuncs
00038     };
00039   }
00040 
00041 /// \brief Implementation of the target library information.
00042 ///
00043 /// This class constructs tables that hold the target library information and
00044 /// make it available. However, it is somewhat expensive to compute and only
00045 /// depends on the triple. So users typicaly interact with the \c
00046 /// TargetLibraryInfo wrapper below.
00047 class TargetLibraryInfoImpl {
00048   friend class TargetLibraryInfo;
00049 
00050   unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
00051   llvm::DenseMap<unsigned, std::string> CustomNames;
00052   static const char *const StandardNames[LibFunc::NumLibFuncs];
00053 
00054   enum AvailabilityState {
00055     StandardName = 3, // (memset to all ones)
00056     CustomName = 1,
00057     Unavailable = 0  // (memset to all zeros)
00058   };
00059   void setState(LibFunc::Func F, AvailabilityState State) {
00060     AvailableArray[F/4] &= ~(3 << 2*(F&3));
00061     AvailableArray[F/4] |= State << 2*(F&3);
00062   }
00063   AvailabilityState getState(LibFunc::Func F) const {
00064     return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
00065   }
00066 
00067   /// Vectorization descriptors - sorted by ScalarFnName.
00068   std::vector<VecDesc> VectorDescs;
00069   /// Scalarization descriptors - same content as VectorDescs but sorted based
00070   /// on VectorFnName rather than ScalarFnName.
00071   std::vector<VecDesc> ScalarDescs;
00072 
00073 public:
00074   /// \brief  List of known vector-functions libraries.
00075   ///
00076   /// The vector-functions library defines, which functions are vectorizable
00077   /// and with which factor. The library can be specified by either frontend,
00078   /// or a commandline option, and then used by
00079   /// addVectorizableFunctionsFromVecLib for filling up the tables of
00080   /// vectorizable functions.
00081   enum VectorLibrary {
00082     NoLibrary, // Don't use any vector library.
00083     Accelerate // Use Accelerate framework.
00084   };
00085 
00086   TargetLibraryInfoImpl();
00087   explicit TargetLibraryInfoImpl(const Triple &T);
00088 
00089   // Provide value semantics.
00090   TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
00091   TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
00092   TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
00093   TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
00094 
00095   /// \brief Searches for a particular function name.
00096   ///
00097   /// If it is one of the known library functions, return true and set F to the
00098   /// corresponding value.
00099   bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
00100 
00101   /// \brief Forces a function to be marked as unavailable.
00102   void setUnavailable(LibFunc::Func F) {
00103     setState(F, Unavailable);
00104   }
00105 
00106   /// \brief Forces a function to be marked as available.
00107   void setAvailable(LibFunc::Func F) {
00108     setState(F, StandardName);
00109   }
00110 
00111   /// \brief Forces a function to be marked as available and provide an
00112   /// alternate name that must be used.
00113   void setAvailableWithName(LibFunc::Func F, StringRef Name) {
00114     if (StandardNames[F] != Name) {
00115       setState(F, CustomName);
00116       CustomNames[F] = Name;
00117       assert(CustomNames.find(F) != CustomNames.end());
00118     } else {
00119       setState(F, StandardName);
00120     }
00121   }
00122 
00123   /// \brief Disables all builtins.
00124   ///
00125   /// This can be used for options like -fno-builtin.
00126   void disableAllFunctions();
00127 
00128   /// addVectorizableFunctions - Add a set of scalar -> vector mappings,
00129   /// queryable via getVectorizedFunction and getScalarizedFunction.
00130   void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
00131 
00132   /// Calls addVectorizableFunctions with a known preset of functions for the
00133   /// given vector library.
00134   void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
00135 
00136   /// isFunctionVectorizable - Return true if the function F has a
00137   /// vector equivalent with vectorization factor VF.
00138   bool isFunctionVectorizable(StringRef F, unsigned VF) const {
00139     return !getVectorizedFunction(F, VF).empty();
00140   }
00141 
00142   /// isFunctionVectorizable - Return true if the function F has a
00143   /// vector equivalent with any vectorization factor.
00144   bool isFunctionVectorizable(StringRef F) const;
00145 
00146   /// getVectorizedFunction - Return the name of the equivalent of
00147   /// F, vectorized with factor VF. If no such mapping exists,
00148   /// return the empty string.
00149   StringRef getVectorizedFunction(StringRef F, unsigned VF) const;
00150 
00151   /// isFunctionScalarizable - Return true if the function F has a
00152   /// scalar equivalent, and set VF to be the vectorization factor.
00153   bool isFunctionScalarizable(StringRef F, unsigned &VF) const {
00154     return !getScalarizedFunction(F, VF).empty();
00155   }
00156 
00157   /// getScalarizedFunction - Return the name of the equivalent of
00158   /// F, scalarized. If no such mapping exists, return the empty string.
00159   ///
00160   /// Set VF to the vectorization factor.
00161   StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
00162 };
00163 
00164 /// \brief Provides information about what library functions are available for
00165 /// the current target.
00166 ///
00167 /// This both allows optimizations to handle them specially and frontends to
00168 /// disable such optimizations through -fno-builtin etc.
00169 class TargetLibraryInfo {
00170   friend class TargetLibraryAnalysis;
00171   friend class TargetLibraryInfoWrapperPass;
00172 
00173   const TargetLibraryInfoImpl *Impl;
00174 
00175 public:
00176   explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
00177 
00178   // Provide value semantics.
00179   TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
00180   TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
00181   TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
00182     Impl = TLI.Impl;
00183     return *this;
00184   }
00185   TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
00186     Impl = TLI.Impl;
00187     return *this;
00188   }
00189 
00190   /// \brief Searches for a particular function name.
00191   ///
00192   /// If it is one of the known library functions, return true and set F to the
00193   /// corresponding value.
00194   bool getLibFunc(StringRef funcName, LibFunc::Func &F) const {
00195     return Impl->getLibFunc(funcName, F);
00196   }
00197 
00198   /// \brief Tests whether a library function is available.
00199   bool has(LibFunc::Func F) const {
00200     return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
00201   }
00202   bool isFunctionVectorizable(StringRef F, unsigned VF) const {
00203     return Impl->isFunctionVectorizable(F, VF);
00204   };
00205   bool isFunctionVectorizable(StringRef F) const {
00206     return Impl->isFunctionVectorizable(F);
00207   };
00208   StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
00209     return Impl->getVectorizedFunction(F, VF);
00210   };
00211 
00212   /// \brief Tests if the function is both available and a candidate for
00213   /// optimized code generation.
00214   bool hasOptimizedCodeGen(LibFunc::Func F) const {
00215     if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
00216       return false;
00217     switch (F) {
00218     default: break;
00219     case LibFunc::copysign:  case LibFunc::copysignf:  case LibFunc::copysignl:
00220     case LibFunc::fabs:      case LibFunc::fabsf:      case LibFunc::fabsl:
00221     case LibFunc::sin:       case LibFunc::sinf:       case LibFunc::sinl:
00222     case LibFunc::cos:       case LibFunc::cosf:       case LibFunc::cosl:
00223     case LibFunc::sqrt:      case LibFunc::sqrtf:      case LibFunc::sqrtl:
00224     case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
00225                                                   case LibFunc::sqrtl_finite:
00226     case LibFunc::fmax:      case LibFunc::fmaxf:      case LibFunc::fmaxl:
00227     case LibFunc::fmin:      case LibFunc::fminf:      case LibFunc::fminl:
00228     case LibFunc::floor:     case LibFunc::floorf:     case LibFunc::floorl:
00229     case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
00230     case LibFunc::ceil:      case LibFunc::ceilf:      case LibFunc::ceill:
00231     case LibFunc::rint:      case LibFunc::rintf:      case LibFunc::rintl:
00232     case LibFunc::round:     case LibFunc::roundf:     case LibFunc::roundl:
00233     case LibFunc::trunc:     case LibFunc::truncf:     case LibFunc::truncl:
00234     case LibFunc::log2:      case LibFunc::log2f:      case LibFunc::log2l:
00235     case LibFunc::exp2:      case LibFunc::exp2f:      case LibFunc::exp2l:
00236     case LibFunc::memcmp:    case LibFunc::strcmp:     case LibFunc::strcpy:
00237     case LibFunc::stpcpy:    case LibFunc::strlen:     case LibFunc::strnlen:
00238     case LibFunc::memchr:
00239       return true;
00240     }
00241     return false;
00242   }
00243 
00244   StringRef getName(LibFunc::Func F) const {
00245     auto State = Impl->getState(F);
00246     if (State == TargetLibraryInfoImpl::Unavailable)
00247       return StringRef();
00248     if (State == TargetLibraryInfoImpl::StandardName)
00249       return Impl->StandardNames[F];
00250     assert(State == TargetLibraryInfoImpl::CustomName);
00251     return Impl->CustomNames.find(F)->second;
00252   }
00253 
00254   /// \brief Handle invalidation from the pass manager.
00255   ///
00256   /// If we try to invalidate this info, just return false. It cannot become
00257   /// invalid even if the module changes.
00258   bool invalidate(Module &, const PreservedAnalyses &) { return false; }
00259 };
00260 
00261 /// \brief Analysis pass providing the \c TargetLibraryInfo.
00262 ///
00263 /// Note that this pass's result cannot be invalidated, it is immutable for the
00264 /// life of the module.
00265 class TargetLibraryAnalysis {
00266 public:
00267   typedef TargetLibraryInfo Result;
00268 
00269   /// \brief Opaque, unique identifier for this analysis pass.
00270   static void *ID() { return (void *)&PassID; }
00271 
00272   /// \brief Default construct the library analysis.
00273   ///
00274   /// This will use the module's triple to construct the library info for that
00275   /// module.
00276   TargetLibraryAnalysis() {}
00277 
00278   /// \brief Construct a library analysis with preset info.
00279   ///
00280   /// This will directly copy the preset info into the result without
00281   /// consulting the module's triple.
00282   TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
00283       : PresetInfoImpl(std::move(PresetInfoImpl)) {}
00284 
00285   // Move semantics. We spell out the constructors for MSVC.
00286   TargetLibraryAnalysis(TargetLibraryAnalysis &&Arg)
00287       : PresetInfoImpl(std::move(Arg.PresetInfoImpl)), Impls(std::move(Arg.Impls)) {}
00288   TargetLibraryAnalysis &operator=(TargetLibraryAnalysis &&RHS) {
00289     PresetInfoImpl = std::move(RHS.PresetInfoImpl);
00290     Impls = std::move(RHS.Impls);
00291     return *this;
00292   }
00293 
00294   TargetLibraryInfo run(Module &M);
00295   TargetLibraryInfo run(Function &F);
00296 
00297   /// \brief Provide access to a name for this pass for debugging purposes.
00298   static StringRef name() { return "TargetLibraryAnalysis"; }
00299 
00300 private:
00301   static char PassID;
00302 
00303   Optional<TargetLibraryInfoImpl> PresetInfoImpl;
00304 
00305   StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
00306 
00307   TargetLibraryInfoImpl &lookupInfoImpl(Triple T);
00308 };
00309 
00310 class TargetLibraryInfoWrapperPass : public ImmutablePass {
00311   TargetLibraryInfoImpl TLIImpl;
00312   TargetLibraryInfo TLI;
00313 
00314   virtual void anchor();
00315 
00316 public:
00317   static char ID;
00318   TargetLibraryInfoWrapperPass();
00319   explicit TargetLibraryInfoWrapperPass(const Triple &T);
00320   explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
00321 
00322   TargetLibraryInfo &getTLI() { return TLI; }
00323   const TargetLibraryInfo &getTLI() const { return TLI; }
00324 };
00325 
00326 } // end namespace llvm
00327 
00328 #endif