make all Emit*() functions consult the TargetLibraryInfo information before creating...
[oota-llvm.git] / include / llvm / Transforms / Utils / BuildLibCalls.h
1 //===- BuildLibCalls.h - Utility builder for libcalls -----------*- 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 // This file exposes an interface to build some C language libcalls for
11 // optimization passes that need to call the various functions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef TRANSFORMS_UTILS_BUILDLIBCALLS_H
16 #define TRANSFORMS_UTILS_BUILDLIBCALLS_H
17
18 #include "llvm/IRBuilder.h"
19
20 namespace llvm {
21   class Value;
22   class TargetData;
23   class TargetLibraryInfo;
24   
25   /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
26   Value *CastToCStr(Value *V, IRBuilder<> &B);
27
28   /// EmitStrLen - Emit a call to the strlen function to the builder, for the
29   /// specified pointer.  Ptr is required to be some pointer type, and the
30   /// return value has 'intptr_t' type.
31   Value *EmitStrLen(Value *Ptr, IRBuilder<> &B, const TargetData *TD,
32                     const TargetLibraryInfo *TLI);
33
34   /// EmitStrChr - Emit a call to the strchr function to the builder, for the
35   /// specified pointer and character.  Ptr is required to be some pointer type,
36   /// and the return value has 'i8*' type.
37   Value *EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, const TargetData *TD,
38                     const TargetLibraryInfo *TLI);
39
40   /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
41   Value *EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
42                      const TargetData *TD, const TargetLibraryInfo *TLI);
43
44   /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
45   /// specified pointer arguments.
46   Value *EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
47                     const TargetData *TD, const TargetLibraryInfo *TLI,
48                     StringRef Name = "strcpy");
49
50   /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
51   /// specified pointer arguments and length.
52   Value *EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
53                      const TargetData *TD, const TargetLibraryInfo *TLI,
54                      StringRef Name = "strncpy");
55
56   /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
57   /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
58   /// are pointers.
59   Value *EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
60                        IRBuilder<> &B, const TargetData *TD,
61                        const TargetLibraryInfo *TLI);
62
63   /// EmitMemChr - Emit a call to the memchr function.  This assumes that Ptr is
64   /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
65   Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
66                     const TargetData *TD, const TargetLibraryInfo *TLI);
67
68   /// EmitMemCmp - Emit a call to the memcmp function.
69   Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
70                     const TargetData *TD, const TargetLibraryInfo *TLI);
71
72   /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name'
73   /// (e.g.  'floor').  This function is known to take a single of type matching
74   /// 'Op' and returns one value with the same type.  If 'Op' is a long double,
75   /// 'l' is added as the suffix of name, if 'Op' is a float, we add a 'f'
76   /// suffix.
77   Value *EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
78                               const AttrListPtr &Attrs);
79
80   /// EmitPutChar - Emit a call to the putchar function.  This assumes that Char
81   /// is an integer.
82   Value *EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD,
83                      const TargetLibraryInfo *TLI);
84
85   /// EmitPutS - Emit a call to the puts function.  This assumes that Str is
86   /// some pointer.
87   Value *EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD,
88                   const TargetLibraryInfo *TLI);
89
90   /// EmitFPutC - Emit a call to the fputc function.  This assumes that Char is
91   /// an i32, and File is a pointer to FILE.
92   Value *EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
93                    const TargetData *TD, const TargetLibraryInfo *TLI);
94
95   /// EmitFPutS - Emit a call to the puts function.  Str is required to be a
96   /// pointer and File is a pointer to FILE.
97   Value *EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, const TargetData *TD,
98                    const TargetLibraryInfo *TLI);
99
100   /// EmitFWrite - Emit a call to the fwrite function.  This assumes that Ptr is
101   /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
102   Value *EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
103                     const TargetData *TD, const TargetLibraryInfo *TLI);
104
105   /// SimplifyFortifiedLibCalls - Helper class for folding checked library
106   /// calls (e.g. __strcpy_chk) into their unchecked counterparts.
107   class SimplifyFortifiedLibCalls {
108   protected:
109     CallInst *CI;
110     virtual void replaceCall(Value *With) = 0;
111     virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
112                             bool isString) const = 0;
113   public:
114     virtual ~SimplifyFortifiedLibCalls();
115     bool fold(CallInst *CI, const TargetData *TD, const TargetLibraryInfo *TLI);
116   };
117 }
118
119 #endif