LLVM  10.0.0svn
BuildLibCalls.h
Go to the documentation of this file.
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 
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 strnlen function to the builder, for the specified
54  /// pointer. Ptr is required to be some pointer type, MaxLen must be of size_t
55  /// type, and the return value has 'intptr_t' type.
56  Value *emitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
57  const DataLayout &DL, const TargetLibraryInfo *TLI);
58 
59  /// Emit a call to the strchr function to the builder, for the specified
60  /// pointer and character. Ptr is required to be some pointer type, and the
61  /// return value has 'i8*' type.
62  Value *emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
63  const TargetLibraryInfo *TLI);
64 
65  /// Emit a call to the strncmp function to the builder.
66  Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
67  const DataLayout &DL, const TargetLibraryInfo *TLI);
68 
69  /// Emit a call to the strcpy function to the builder, for the specified
70  /// pointer arguments.
71  Value *emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
72  const TargetLibraryInfo *TLI);
73 
74  /// Emit a call to the stpcpy function to the builder, for the specified
75  /// pointer arguments.
76  Value *emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B,
77  const TargetLibraryInfo *TLI);
78 
79  /// Emit a call to the strncpy function to the builder, for the specified
80  /// pointer arguments and length.
81  Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
82  const TargetLibraryInfo *TLI);
83 
84  /// Emit a call to the stpncpy function to the builder, for the specified
85  /// pointer arguments and length.
86  Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
87  const TargetLibraryInfo *TLI);
88 
89  /// Emit a call to the __memcpy_chk function to the builder. This expects that
90  /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
91  Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
92  IRBuilder<> &B, const DataLayout &DL,
93  const TargetLibraryInfo *TLI);
94 
95  /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
96  /// Val is an i32 value, and Len is an 'intptr_t' value.
97  Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
98  const DataLayout &DL, const TargetLibraryInfo *TLI);
99 
100  /// Emit a call to the memcmp function.
101  Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
102  const DataLayout &DL, const TargetLibraryInfo *TLI);
103 
104  /// Emit a call to the bcmp function.
105  Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
106  const DataLayout &DL, const TargetLibraryInfo *TLI);
107 
108  /// Emit a call to the memccpy function.
109  Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
110  IRBuilder<> &B, const TargetLibraryInfo *TLI);
111 
112  /// Emit a call to the snprintf function.
113  Value *emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
114  ArrayRef<Value *> Args, IRBuilder<> &B,
115  const TargetLibraryInfo *TLI);
116 
117  /// Emit a call to the sprintf function.
118  Value *emitSPrintf(Value *Dest, Value *Fmt, ArrayRef<Value *> VariadicArgs,
119  IRBuilder<> &B, const TargetLibraryInfo *TLI);
120 
121  /// Emit a call to the strcat function.
122  Value *emitStrCat(Value *Dest, Value *Src, IRBuilder<> &B,
123  const TargetLibraryInfo *TLI);
124 
125  /// Emit a call to the strlcpy function.
126  Value *emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
127  const TargetLibraryInfo *TLI);
128 
129  /// Emit a call to the strlcat function.
130  Value *emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
131  const TargetLibraryInfo *TLI);
132 
133  /// Emit a call to the strncat function.
134  Value *emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
135  const TargetLibraryInfo *TLI);
136 
137  /// Emit a call to the vsnprintf function.
138  Value *emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
139  IRBuilder<> &B, const TargetLibraryInfo *TLI);
140 
141  /// Emit a call to the vsprintf function.
142  Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilder<> &B,
143  const TargetLibraryInfo *TLI);
144 
145  /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
146  /// function is known to take a single of type matching 'Op' and returns one
147  /// value with the same type. If 'Op' is a long double, 'l' is added as the
148  /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
149  Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
150  const AttributeList &Attrs);
151 
152  /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
153  /// depending of the type of Op.
154  Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
155  LibFunc DoubleFn, LibFunc FloatFn,
156  LibFunc LongDoubleFn, IRBuilder<> &B,
157  const AttributeList &Attrs);
158 
159  /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
160  /// function is known to take type matching 'Op1' and 'Op2' and return one
161  /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
162  /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
163  Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
164  IRBuilder<> &B, const AttributeList &Attrs);
165 
166  /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn,
167  /// depending of the type of Op1.
169  const TargetLibraryInfo *TLI, LibFunc DoubleFn,
170  LibFunc FloatFn, LibFunc LongDoubleFn,
171  IRBuilder<> &B, const AttributeList &Attrs);
172 
173  /// Emit a call to the putchar function. This assumes that Char is an integer.
174  Value *emitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI);
175 
176  /// Emit a call to the puts function. This assumes that Str is some pointer.
177  Value *emitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI);
178 
179  /// Emit a call to the fputc function. This assumes that Char is an i32, and
180  /// File is a pointer to FILE.
181  Value *emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
182  const TargetLibraryInfo *TLI);
183 
184  /// Emit a call to the fputc_unlocked function. This assumes that Char is an
185  /// i32, and File is a pointer to FILE.
186  Value *emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
187  const TargetLibraryInfo *TLI);
188 
189  /// Emit a call to the fputs function. Str is required to be a pointer and
190  /// File is a pointer to FILE.
191  Value *emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
192  const TargetLibraryInfo *TLI);
193 
194  /// Emit a call to the fputs_unlocked function. Str is required to be a
195  /// pointer and File is a pointer to FILE.
196  Value *emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
197  const TargetLibraryInfo *TLI);
198 
199  /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
200  /// Size is an 'intptr_t', and File is a pointer to FILE.
201  Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
202  const DataLayout &DL, const TargetLibraryInfo *TLI);
203 
204  /// Emit a call to the malloc function.
205  Value *emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
206  const TargetLibraryInfo *TLI);
207 
208  /// Emit a call to the calloc function.
209  Value *emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
210  IRBuilder<> &B, const TargetLibraryInfo &TLI);
211 
212  /// Emit a call to the fwrite_unlocked function. This assumes that Ptr is a
213  /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
215  IRBuilder<> &B, const DataLayout &DL,
216  const TargetLibraryInfo *TLI);
217 
218  /// Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
219  Value *emitFGetCUnlocked(Value *File, IRBuilder<> &B,
220  const TargetLibraryInfo *TLI);
221 
222  /// Emit a call to the fgets_unlocked function. Str is required to be a
223  /// pointer, Size is an i32 and File is a pointer to FILE.
224  Value *emitFGetSUnlocked(Value *Str, Value *Size, Value *File, IRBuilder<> &B,
225  const TargetLibraryInfo *TLI);
226 
227  /// Emit a call to the fread_unlocked function. This assumes that Ptr is a
228  /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
230  IRBuilder<> &B, const DataLayout &DL,
231  const TargetLibraryInfo *TLI);
232 }
233 
234 #endif
uint64_t CallInst * C
Value * emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the stpcpy function to the builder, for the specified pointer arguments.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Value * emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, ArrayRef< Value *> Args, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the snprintf function.
F(f)
Value * emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the memccpy function.
Value * emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fputs_unlocked function.
Value * emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the malloc function.
Value * emitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the putchar function. This assumes that Char is an integer.
Value * emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcpy function.
Value * emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, const AttributeList &Attrs)
Emit a call to the unary function named &#39;Name&#39; (e.g.
Value * emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fputc_unlocked function.
Value * emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the __memcpy_chk function to the builder.
Value * emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the vsprintf function.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
Value * emitFPutS(Value *Str, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fputs function.
bool inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI)
Analyze the name and prototype of the given function and set any applicable attributes.
StringRef getFloatFnName(const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Get the name of the overloaded floating point function corresponding to Ty.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Instrumentation for Order File
Value * castToCStr(Value *V, IRBuilder<> &B)
Return V if it is an i8*, otherwise cast it to i8*.
Value * emitStrChr(Value *Ptr, char C, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strchr function to the builder, for the specified pointer and character.
Value * emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fwrite_unlocked function.
Value * emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strlen function to the builder, for the specified pointer.
Value * emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, IRBuilder<> &B, const AttributeList &Attrs)
Emit a call to the binary function named &#39;Name&#39; (e.g.
Value * emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strncpy function to the builder, for the specified pointer arguments and length...
Value * emitFGetCUnlocked(Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
Value * emitStrCat(Value *Dest, Value *Src, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strcat function.
Value * emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strcpy function to the builder, for the specified pointer arguments.
Value * emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the vsnprintf function.
Value * emitFPutC(Value *Char, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fputc function.
Value * emitFGetSUnlocked(Value *Str, Value *Size, Value *File, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the fgets_unlocked function.
Value * emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strncmp function to the builder.
#define N
Value * emitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strnlen function to the builder, for the specified pointer.
Value * emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcat function.
uint32_t Size
Definition: Profile.cpp:46
Value * emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the bcmp function.
Value * emitSPrintf(Value *Dest, Value *Fmt, ArrayRef< Value *> VariadicArgs, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the sprintf function.
Value * emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fwrite function.
Value * emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memcmp function.
Value * emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fread_unlocked function.
Value * emitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the puts function. This assumes that Str is some pointer.
Value * emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the stpncpy function to the builder, for the specified pointer arguments and length...
Value * emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memchr function.
Value * emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs, IRBuilder<> &B, const TargetLibraryInfo &TLI)
Emit a call to the calloc function.
Value * emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, const TargetLibraryInfo *TLI)
Emit a call to the strncat function.
bool hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Check whether the overloaded floating point function corresponding to Ty is available.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.