Home | History | Annotate | Download | only in Target
      1 //===-- llvm/Target/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_TARGET_TARGETLIBRARYINFO_H
     11 #define LLVM_TARGET_TARGETLIBRARYINFO_H
     12 
     13 #include "llvm/Pass.h"
     14 #include "llvm/ADT/DenseMap.h"
     15 
     16 namespace llvm {
     17   class Triple;
     18 
     19   namespace LibFunc {
     20     enum Func {
     21       /// void operator delete[](void*);
     22       ZdaPv,
     23       /// void operator delete(void*);
     24       ZdlPv,
     25       /// void *new[](unsigned int);
     26       Znaj,
     27       /// void *new[](unsigned int, nothrow);
     28       ZnajRKSt9nothrow_t,
     29       /// void *new[](unsigned long);
     30       Znam,
     31       /// void *new[](unsigned long, nothrow);
     32       ZnamRKSt9nothrow_t,
     33       /// void *new(unsigned int);
     34       Znwj,
     35       /// void *new(unsigned int, nothrow);
     36       ZnwjRKSt9nothrow_t,
     37       /// void *new(unsigned long);
     38       Znwm,
     39       /// void *new(unsigned long, nothrow);
     40       ZnwmRKSt9nothrow_t,
     41       /// int __cxa_atexit(void (*f)(void *), void *p, void *d);
     42       cxa_atexit,
     43       /// void __cxa_guard_abort(guard_t *guard);
     44       /// guard_t is int64_t in Itanium ABI or int32_t on ARM eabi.
     45       cxa_guard_abort,
     46       /// int __cxa_guard_acquire(guard_t *guard);
     47       cxa_guard_acquire,
     48       /// void __cxa_guard_release(guard_t *guard);
     49       cxa_guard_release,
     50       /// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
     51       memcpy_chk,
     52       /// double acos(double x);
     53       acos,
     54       /// float acosf(float x);
     55       acosf,
     56       /// double acosh(double x);
     57       acosh,
     58       /// float acoshf(float x);
     59       acoshf,
     60       /// long double acoshl(long double x);
     61       acoshl,
     62       /// long double acosl(long double x);
     63       acosl,
     64       /// double asin(double x);
     65       asin,
     66       /// float asinf(float x);
     67       asinf,
     68       /// double asinh(double x);
     69       asinh,
     70       /// float asinhf(float x);
     71       asinhf,
     72       /// long double asinhl(long double x);
     73       asinhl,
     74       /// long double asinl(long double x);
     75       asinl,
     76       /// double atan(double x);
     77       atan,
     78       /// double atan2(double y, double x);
     79       atan2,
     80       /// float atan2f(float y, float x);
     81       atan2f,
     82       /// long double atan2l(long double y, long double x);
     83       atan2l,
     84       /// float atanf(float x);
     85       atanf,
     86       /// double atanh(double x);
     87       atanh,
     88       /// float atanhf(float x);
     89       atanhf,
     90       /// long double atanhl(long double x);
     91       atanhl,
     92       /// long double atanl(long double x);
     93       atanl,
     94       /// void *calloc(size_t count, size_t size);
     95       calloc,
     96       /// double cbrt(double x);
     97       cbrt,
     98       /// float cbrtf(float x);
     99       cbrtf,
    100       /// long double cbrtl(long double x);
    101       cbrtl,
    102       /// double ceil(double x);
    103       ceil,
    104       /// float ceilf(float x);
    105       ceilf,
    106       /// long double ceill(long double x);
    107       ceill,
    108       /// double copysign(double x, double y);
    109       copysign,
    110       /// float copysignf(float x, float y);
    111       copysignf,
    112       /// long double copysignl(long double x, long double y);
    113       copysignl,
    114       /// double cos(double x);
    115       cos,
    116       /// float cosf(float x);
    117       cosf,
    118       /// double cosh(double x);
    119       cosh,
    120       /// float coshf(float x);
    121       coshf,
    122       /// long double coshl(long double x);
    123       coshl,
    124       /// long double cosl(long double x);
    125       cosl,
    126       /// double exp(double x);
    127       exp,
    128       /// double exp10(double x);
    129       exp10,
    130       /// float exp10f(float x);
    131       exp10f,
    132       /// long double exp10l(long double x);
    133       exp10l,
    134       /// double exp2(double x);
    135       exp2,
    136       /// float exp2f(float x);
    137       exp2f,
    138       /// long double exp2l(long double x);
    139       exp2l,
    140       /// float expf(float x);
    141       expf,
    142       /// long double expl(long double x);
    143       expl,
    144       /// double expm1(double x);
    145       expm1,
    146       /// float expm1f(float x);
    147       expm1f,
    148       /// long double expm1l(long double x);
    149       expm1l,
    150       /// double fabs(double x);
    151       fabs,
    152       /// float fabsf(float x);
    153       fabsf,
    154       /// long double fabsl(long double x);
    155       fabsl,
    156       /// int fiprintf(FILE *stream, const char *format, ...);
    157       fiprintf,
    158       /// double floor(double x);
    159       floor,
    160       /// float floorf(float x);
    161       floorf,
    162       /// long double floorl(long double x);
    163       floorl,
    164       /// double fmod(double x, double y);
    165       fmod,
    166       /// float fmodf(float x, float y);
    167       fmodf,
    168       /// long double fmodl(long double x, long double y);
    169       fmodl,
    170       /// int fputc(int c, FILE *stream);
    171       fputc,
    172       /// int fputs(const char *s, FILE *stream);
    173       fputs,
    174       /// void free(void *ptr);
    175       free,
    176       /// size_t fwrite(const void *ptr, size_t size, size_t nitems,
    177       /// FILE *stream);
    178       fwrite,
    179       /// int iprintf(const char *format, ...);
    180       iprintf,
    181       /// double log(double x);
    182       log,
    183       /// double log10(double x);
    184       log10,
    185       /// float log10f(float x);
    186       log10f,
    187       /// long double log10l(long double x);
    188       log10l,
    189       /// double log1p(double x);
    190       log1p,
    191       /// float log1pf(float x);
    192       log1pf,
    193       /// long double log1pl(long double x);
    194       log1pl,
    195       /// double log2(double x);
    196       log2,
    197       /// float log2f(float x);
    198       log2f,
    199       /// double long double log2l(long double x);
    200       log2l,
    201       /// double logb(double x);
    202       logb,
    203       /// float logbf(float x);
    204       logbf,
    205       /// long double logbl(long double x);
    206       logbl,
    207       /// float logf(float x);
    208       logf,
    209       /// long double logl(long double x);
    210       logl,
    211       /// void *malloc(size_t size);
    212       malloc,
    213       /// void *memchr(const void *s, int c, size_t n);
    214       memchr,
    215       /// int memcmp(const void *s1, const void *s2, size_t n);
    216       memcmp,
    217       /// void *memcpy(void *s1, const void *s2, size_t n);
    218       memcpy,
    219       /// void *memmove(void *s1, const void *s2, size_t n);
    220       memmove,
    221       /// void *memset(void *b, int c, size_t len);
    222       memset,
    223       /// void memset_pattern16(void *b, const void *pattern16, size_t len);
    224       memset_pattern16,
    225       /// double nearbyint(double x);
    226       nearbyint,
    227       /// float nearbyintf(float x);
    228       nearbyintf,
    229       /// long double nearbyintl(long double x);
    230       nearbyintl,
    231       /// int posix_memalign(void **memptr, size_t alignment, size_t size);
    232       posix_memalign,
    233       /// double pow(double x, double y);
    234       pow,
    235       /// float powf(float x, float y);
    236       powf,
    237       /// long double powl(long double x, long double y);
    238       powl,
    239       /// int putchar(int c);
    240       putchar,
    241       /// int puts(const char *s);
    242       puts,
    243       /// void *realloc(void *ptr, size_t size);
    244       realloc,
    245       /// void *reallocf(void *ptr, size_t size);
    246       reallocf,
    247       /// double rint(double x);
    248       rint,
    249       /// float rintf(float x);
    250       rintf,
    251       /// long double rintl(long double x);
    252       rintl,
    253       /// double round(double x);
    254       round,
    255       /// float roundf(float x);
    256       roundf,
    257       /// long double roundl(long double x);
    258       roundl,
    259       /// double sin(double x);
    260       sin,
    261       /// float sinf(float x);
    262       sinf,
    263       /// double sinh(double x);
    264       sinh,
    265       /// float sinhf(float x);
    266       sinhf,
    267       /// long double sinhl(long double x);
    268       sinhl,
    269       /// long double sinl(long double x);
    270       sinl,
    271       /// int siprintf(char *str, const char *format, ...);
    272       siprintf,
    273       /// double sqrt(double x);
    274       sqrt,
    275       /// float sqrtf(float x);
    276       sqrtf,
    277       /// long double sqrtl(long double x);
    278       sqrtl,
    279       /// char *strcat(char *s1, const char *s2);
    280       strcat,
    281       /// char *strchr(const char *s, int c);
    282       strchr,
    283       /// char *strcpy(char *s1, const char *s2);
    284       strcpy,
    285       /// char *strdup(const char *s1);
    286       strdup,
    287       /// size_t strlen(const char *s);
    288       strlen,
    289       /// char *strncat(char *s1, const char *s2, size_t n);
    290       strncat,
    291       /// int strncmp(const char *s1, const char *s2, size_t n);
    292       strncmp,
    293       /// char *strncpy(char *s1, const char *s2, size_t n);
    294       strncpy,
    295       /// char *strndup(const char *s1, size_t n);
    296       strndup,
    297       /// size_t strnlen(const char *s, size_t maxlen);
    298       strnlen,
    299       /// double tan(double x);
    300       tan,
    301       /// float tanf(float x);
    302       tanf,
    303       /// double tanh(double x);
    304       tanh,
    305       /// float tanhf(float x);
    306       tanhf,
    307       /// long double tanhl(long double x);
    308       tanhl,
    309       /// long double tanl(long double x);
    310       tanl,
    311       /// double trunc(double x);
    312       trunc,
    313       /// float truncf(float x);
    314       truncf,
    315       /// long double truncl(long double x);
    316       truncl,
    317       /// void *valloc(size_t size);
    318       valloc,
    319 
    320       NumLibFuncs
    321     };
    322   }
    323 
    324 /// TargetLibraryInfo - This immutable pass captures information about what
    325 /// library functions are available for the current target, and allows a
    326 /// frontend to disable optimizations through -fno-builtin etc.
    327 class TargetLibraryInfo : public ImmutablePass {
    328   virtual void anchor();
    329   unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
    330   llvm::DenseMap<unsigned, std::string> CustomNames;
    331   static const char* StandardNames[LibFunc::NumLibFuncs];
    332 
    333   enum AvailabilityState {
    334     StandardName = 3, // (memset to all ones)
    335     CustomName = 1,
    336     Unavailable = 0  // (memset to all zeros)
    337   };
    338   void setState(LibFunc::Func F, AvailabilityState State) {
    339     AvailableArray[F/4] &= ~(3 << 2*(F&3));
    340     AvailableArray[F/4] |= State << 2*(F&3);
    341   }
    342   AvailabilityState getState(LibFunc::Func F) const {
    343     return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
    344   }
    345 
    346 public:
    347   static char ID;
    348   TargetLibraryInfo();
    349   TargetLibraryInfo(const Triple &T);
    350   explicit TargetLibraryInfo(const TargetLibraryInfo &TLI);
    351 
    352   /// getLibFunc - Search for a particular function name.  If it is one of the
    353   /// known library functions, return true and set F to the corresponding value.
    354   bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
    355 
    356   /// has - This function is used by optimizations that want to match on or form
    357   /// a given library function.
    358   bool has(LibFunc::Func F) const {
    359     return getState(F) != Unavailable;
    360   }
    361 
    362   /// hasOptimizedCodeGen - Return true if the function is both available as
    363   /// a builtin and a candidate for optimized code generation.
    364   bool hasOptimizedCodeGen(LibFunc::Func F) const {
    365     if (getState(F) == Unavailable)
    366       return false;
    367     switch (F) {
    368     default: break;
    369     case LibFunc::copysign:  case LibFunc::copysignf:  case LibFunc::copysignl:
    370     case LibFunc::fabs:      case LibFunc::fabsf:      case LibFunc::fabsl:
    371     case LibFunc::sin:       case LibFunc::sinf:       case LibFunc::sinl:
    372     case LibFunc::cos:       case LibFunc::cosf:       case LibFunc::cosl:
    373     case LibFunc::sqrt:      case LibFunc::sqrtf:      case LibFunc::sqrtl:
    374     case LibFunc::floor:     case LibFunc::floorf:     case LibFunc::floorl:
    375     case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
    376     case LibFunc::ceil:      case LibFunc::ceilf:      case LibFunc::ceill:
    377     case LibFunc::rint:      case LibFunc::rintf:      case LibFunc::rintl:
    378     case LibFunc::trunc:     case LibFunc::truncf:     case LibFunc::truncl:
    379     case LibFunc::log2:      case LibFunc::log2f:      case LibFunc::log2l:
    380     case LibFunc::exp2:      case LibFunc::exp2f:      case LibFunc::exp2l:
    381     case LibFunc::memcmp:
    382       return true;
    383     }
    384     return false;
    385   }
    386 
    387   StringRef getName(LibFunc::Func F) const {
    388     AvailabilityState State = getState(F);
    389     if (State == Unavailable)
    390       return StringRef();
    391     if (State == StandardName)
    392       return StandardNames[F];
    393     assert(State == CustomName);
    394     return CustomNames.find(F)->second;
    395   }
    396 
    397   /// setUnavailable - this can be used by whatever sets up TargetLibraryInfo to
    398   /// ban use of specific library functions.
    399   void setUnavailable(LibFunc::Func F) {
    400     setState(F, Unavailable);
    401   }
    402 
    403   void setAvailable(LibFunc::Func F) {
    404     setState(F, StandardName);
    405   }
    406 
    407   void setAvailableWithName(LibFunc::Func F, StringRef Name) {
    408     if (StandardNames[F] != Name) {
    409       setState(F, CustomName);
    410       CustomNames[F] = Name;
    411       assert(CustomNames.find(F) != CustomNames.end());
    412     } else {
    413       setState(F, StandardName);
    414     }
    415   }
    416 
    417   /// disableAllFunctions - This disables all builtins, which is used for
    418   /// options like -fno-builtin.
    419   void disableAllFunctions();
    420 };
    421 
    422 } // end namespace llvm
    423 
    424 #endif
    425