TargetLibraryInfo.h revision 683e47b1dd8672b0b026a45022cf91f4faf7df9b
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      /// int fiprintf(FILE *stream, const char *format, ...);
22      fiprintf,
23
24      /// int fputs(const char *s, FILE *stream);
25      fputs,
26
27      /// size_t fwrite(const void *ptr, size_t size, size_t nitems,
28      /// FILE *stream);
29      fwrite,
30
31      /// int iprintf(const char *format, ...);
32      iprintf,
33
34      /// void *memcpy(void *s1, const void *s2, size_t n);
35      memcpy,
36
37      /// void *memmove(void *s1, const void *s2, size_t n);
38      memmove,
39
40      /// void *memset(void *b, int c, size_t len);
41      memset,
42
43      /// void memset_pattern16(void *b, const void *pattern16, size_t len);
44      memset_pattern16,
45
46      /// int siprintf(char *str, const char *format, ...);
47      siprintf,
48
49      /// double sqrt(double x);
50      sqrt,
51
52      /// float sqrtf(float x);
53      sqrtf,
54
55      /// long double sqrtl(long double x);
56      sqrtl,
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