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