1 //===- BuildLibCalls.h - Utility builder for libcalls -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file exposes an interface to build some C language libcalls for
10 // optimization passes that need to call the various functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
15 #define LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
16 
17 #include "llvm/Analysis/TargetLibraryInfo.h"
18 #include "llvm/IR/IRBuilder.h"
19 
20 namespace llvm {
21   class Value;
22   class DataLayout;
23   class TargetLibraryInfo;
24 
25   /// Analyze the name and prototype of the given function and set any
26   /// applicable attributes.
27   /// If the library function is unavailable, this doesn't modify it.
28   ///
29   /// Returns true if any attributes were set and false otherwise.
30   bool inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI);
31   bool inferLibFuncAttributes(Module *M, StringRef Name, const TargetLibraryInfo &TLI);
32 
33   /// Check whether the overloaded floating point function
34   /// corresponding to \a Ty is available.
35   bool hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
36                   LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn);
37 
38   /// Get the name of the overloaded floating point function
39   /// corresponding to \a Ty.
40   StringRef getFloatFnName(const TargetLibraryInfo *TLI, Type *Ty,
41                            LibFunc DoubleFn, LibFunc FloatFn,
42                            LibFunc LongDoubleFn);
43 
44   /// Return V if it is an i8*, otherwise cast it to i8*.
45   Value *castToCStr(Value *V, IRBuilder<> &B);
46 
47   /// Emit a call to the strlen function to the builder, for the specified
48   /// pointer. Ptr is required to be some pointer type, and the return value has
49   /// 'intptr_t' type.
50   Value *emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
51                     const TargetLibraryInfo *TLI);
52 
53   /// Emit a call to the strdup function to the builder, for the specified
54   /// pointer. Ptr is required to be some pointer type, and the return value has
55   /// 'i8*' type.
56   Value *emitStrDup(Value *Ptr, IRBuilder<> &B, const TargetLibraryInfo *TLI);
57 
58   /// Emit a call to the strnlen function to the builder, for the specified
59   /// pointer. Ptr is required to be some pointer type, MaxLen must be of size_t
60   /// type, and the return value has 'intptr_t' type.
61   Value *emitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
62                      const DataLayout &DL, const TargetLibraryInfo *TLI);
63 
64   /// Emit a call to the strchr function to the builder, for the specified
65   /// pointer and character. Ptr is required to be some pointer type, and the
66   /// return value has 'i8*' type.
67   Value *emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
68                     const TargetLibraryInfo *TLI);
69 
70   /// Emit a call to the strncmp function to the builder.
71   Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
72                      const DataLayout &DL, const TargetLibraryInfo *TLI);
73 
74   /// Emit a call to the strcpy function to the builder, for the specified
75   /// pointer arguments.
76   Value *emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
77                     const TargetLibraryInfo *TLI);
78 
79   /// Emit a call to the stpcpy function to the builder, for the specified
80   /// pointer arguments.
81   Value *emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B,
82                     const TargetLibraryInfo *TLI);
83 
84   /// Emit a call to the strncpy function to the builder, for the specified
85   /// pointer arguments and length.
86   Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
87                      const TargetLibraryInfo *TLI);
88 
89   /// Emit a call to the stpncpy function to the builder, for the specified
90   /// pointer arguments and length.
91   Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
92                      const TargetLibraryInfo *TLI);
93 
94   /// Emit a call to the __memcpy_chk function to the builder. This expects that
95   /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
96   Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
97                        IRBuilder<> &B, const DataLayout &DL,
98                        const TargetLibraryInfo *TLI);
99 
100   /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
101   /// Val is an i32 value, and Len is an 'intptr_t' value.
102   Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
103                     const DataLayout &DL, const TargetLibraryInfo *TLI);
104 
105   /// Emit a call to the memcmp function.
106   Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
107                     const DataLayout &DL, const TargetLibraryInfo *TLI);
108 
109   /// Emit a call to the bcmp function.
110   Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
111                   const DataLayout &DL, const TargetLibraryInfo *TLI);
112 
113   /// Emit a call to the memccpy function.
114   Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
115                      IRBuilder<> &B, const TargetLibraryInfo *TLI);
116 
117   /// Emit a call to the snprintf function.
118   Value *emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
119                       ArrayRef<Value *> Args, IRBuilder<> &B,
120                       const TargetLibraryInfo *TLI);
121 
122   /// Emit a call to the sprintf function.
123   Value *emitSPrintf(Value *Dest, Value *Fmt, ArrayRef<Value *> VariadicArgs,
124                      IRBuilder<> &B, const TargetLibraryInfo *TLI);
125 
126   /// Emit a call to the strcat function.
127   Value *emitStrCat(Value *Dest, Value *Src, IRBuilder<> &B,
128                     const TargetLibraryInfo *TLI);
129 
130   /// Emit a call to the strlcpy function.
131   Value *emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
132                      const TargetLibraryInfo *TLI);
133 
134   /// Emit a call to the strlcat function.
135   Value *emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
136                      const TargetLibraryInfo *TLI);
137 
138   /// Emit a call to the strncat function.
139   Value *emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
140                      const TargetLibraryInfo *TLI);
141 
142   /// Emit a call to the vsnprintf function.
143   Value *emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
144                        IRBuilder<> &B, const TargetLibraryInfo *TLI);
145 
146   /// Emit a call to the vsprintf function.
147   Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilder<> &B,
148                       const TargetLibraryInfo *TLI);
149 
150   /// Emit a call to the unary function named 'Name' (e.g.  'floor'). This
151   /// function is known to take a single of type matching 'Op' and returns one
152   /// value with the same type. If 'Op' is a long double, 'l' is added as the
153   /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
154   Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
155                               const AttributeList &Attrs);
156 
157   /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
158   /// depending of the type of Op.
159   Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
160                               LibFunc DoubleFn, LibFunc FloatFn,
161                               LibFunc LongDoubleFn, IRBuilder<> &B,
162                               const AttributeList &Attrs);
163 
164   /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
165   /// function is known to take type matching 'Op1' and 'Op2' and return one
166   /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
167   /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
168   Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
169                                IRBuilder<> &B, const AttributeList &Attrs);
170 
171   /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn,
172   /// depending of the type of Op1.
173   Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2,
174                                const TargetLibraryInfo *TLI, LibFunc DoubleFn,
175                                LibFunc FloatFn, LibFunc LongDoubleFn,
176                                IRBuilder<> &B, const AttributeList &Attrs);
177 
178   /// Emit a call to the putchar function. This assumes that Char is an integer.
179   Value *emitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI);
180 
181   /// Emit a call to the puts function. This assumes that Str is some pointer.
182   Value *emitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI);
183 
184   /// Emit a call to the fputc function. This assumes that Char is an i32, and
185   /// File is a pointer to FILE.
186   Value *emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
187                    const TargetLibraryInfo *TLI);
188 
189   /// Emit a call to the fputc_unlocked function. This assumes that Char is an
190   /// i32, and File is a pointer to FILE.
191   Value *emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
192                            const TargetLibraryInfo *TLI);
193 
194   /// Emit a call to the fputs function. Str is required to be a pointer and
195   /// File is a pointer to FILE.
196   Value *emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
197                    const TargetLibraryInfo *TLI);
198 
199   /// Emit a call to the fputs_unlocked function. Str is required to be a
200   /// pointer and File is a pointer to FILE.
201   Value *emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
202                            const TargetLibraryInfo *TLI);
203 
204   /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
205   /// Size is an 'intptr_t', and File is a pointer to FILE.
206   Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
207                     const DataLayout &DL, const TargetLibraryInfo *TLI);
208 
209   /// Emit a call to the malloc function.
210   Value *emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
211                     const TargetLibraryInfo *TLI);
212 
213   /// Emit a call to the calloc function.
214   Value *emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
215                     IRBuilder<> &B, const TargetLibraryInfo &TLI);
216 
217   /// Emit a call to the fwrite_unlocked function. This assumes that Ptr is a
218   /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
219   Value *emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
220                             IRBuilder<> &B, const DataLayout &DL,
221                             const TargetLibraryInfo *TLI);
222 
223   /// Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
224   Value *emitFGetCUnlocked(Value *File, IRBuilder<> &B,
225                            const TargetLibraryInfo *TLI);
226 
227   /// Emit a call to the fgets_unlocked function. Str is required to be a
228   /// pointer, Size is an i32 and File is a pointer to FILE.
229   Value *emitFGetSUnlocked(Value *Str, Value *Size, Value *File, IRBuilder<> &B,
230                            const TargetLibraryInfo *TLI);
231 
232   /// Emit a call to the fread_unlocked function. This assumes that Ptr is a
233   /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
234   Value *emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
235                            IRBuilder<> &B, const DataLayout &DL,
236                            const TargetLibraryInfo *TLI);
237 }
238 
239 #endif
240