TargetLibraryInfo.h revision 3d925d24e8c54cde05228258c25cc21687cad922
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
16namespace llvm {
17  class Triple;
18
19  namespace LibFunc {
20    enum Func {
21      /// void *memset(void *b, int c, size_t len);
22      memset,
23
24      // void *memcpy(void *s1, const void *s2, size_t n);
25      memcpy,
26
27      // void *memmove(void *s1, const void *s2, size_t n);
28      memmove,
29
30      /// void memset_pattern16(void *b, const void *pattern16, size_t len);
31      memset_pattern16,
32
33      /// int iprintf(const char *format, ...);
34      iprintf,
35
36      /// int siprintf(char *str, const char *format, ...);
37      siprintf,
38
39      /// double sqrt(double x);
40      sqrt,
41
42      /// long double sqrtl(long double x);
43      sqrtl,
44
45      /// float sqrtf(float x);
46      sqrtf,
47
48      /// int fiprintf(FILE *stream, const char *format, ...);
49      fiprintf,
50
51      // size_t fwrite(const void *ptr, size_t size, size_t nitems,
52      //               FILE *stream);
53      fwrite,
54
55      // int fputs(const char *s, FILE *stream);
56      fputs,
57
58      NumLibFuncs
59    };
60  }
61
62/// TargetLibraryInfo - This immutable pass captures information about what
63/// library functions are available for the current target, and allows a
64/// frontend to disable optimizations through -fno-builtin etc.
65class TargetLibraryInfo : public ImmutablePass {
66  unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
67  llvm::DenseMap<unsigned, std::string> CustomNames;
68  static const char* StandardNames[LibFunc::NumLibFuncs];
69
70  enum AvailabilityState {
71    StandardName = 3, // (memset to all ones)
72    CustomName = 1,
73    Unavailable = 0  // (memset to all zeros)
74  };
75  void setState(LibFunc::Func F, AvailabilityState State) {
76    AvailableArray[F/4] &= ~(3 << 2*(F&3));
77    AvailableArray[F/4] |= State << 2*(F&3);
78  }
79  AvailabilityState getState(LibFunc::Func F) const {
80    return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
81  }
82
83public:
84  static char ID;
85  TargetLibraryInfo();
86  TargetLibraryInfo(const Triple &T);
87  explicit TargetLibraryInfo(const TargetLibraryInfo &TLI);
88
89  /// has - This function is used by optimizations that want to match on or form
90  /// a given library function.
91  bool has(LibFunc::Func F) const {
92    return getState(F) != Unavailable;
93  }
94
95  StringRef getName(LibFunc::Func F) const {
96    AvailabilityState State = getState(F);
97    if (State == Unavailable)
98      return StringRef();
99    if (State == StandardName)
100      return StandardNames[F];
101    assert(State == CustomName);
102    return CustomNames.find(F)->second;
103  }
104
105  /// setUnavailable - this can be used by whatever sets up TargetLibraryInfo to
106  /// ban use of specific library functions.
107  void setUnavailable(LibFunc::Func F) {
108    setState(F, Unavailable);
109  }
110
111  void setAvailable(LibFunc::Func F) {
112    setState(F, StandardName);
113  }
114
115  void setAvailableWithName(LibFunc::Func F, StringRef Name) {
116    if (StandardNames[F] != Name) {
117      setState(F, CustomName);
118      CustomNames[F] = Name;
119      assert(CustomNames.find(F) != CustomNames.end());
120    } else {
121      setState(F, StandardName);
122    }
123  }
124
125  /// disableAllFunctions - This disables all builtins, which is used for
126  /// options like -fno-builtin.
127  void disableAllFunctions();
128};
129
130} // end namespace llvm
131
132#endif
133