1 //===- BuildLibCalls.h - Utility builder for libcalls -----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file exposes an interface to build some C language libcalls for
10 // optimization passes that need to call the various functions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
15 #define LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
17 #include "llvm/Analysis/TargetLibraryInfo.h"
18 #include "llvm/IR/IRBuilder.h"
23 class TargetLibraryInfo
;
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.
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
);
33 /// Check whether the overloaded unary floating point function
34 /// corresponding to \a Ty is available.
35 bool hasUnaryFloatFn(const TargetLibraryInfo
*TLI
, Type
*Ty
,
36 LibFunc DoubleFn
, LibFunc FloatFn
,
37 LibFunc LongDoubleFn
);
39 /// Get the name of the overloaded unary floating point function
40 /// corresponding to \a Ty.
41 StringRef
getUnaryFloatFn(const TargetLibraryInfo
*TLI
, Type
*Ty
,
42 LibFunc DoubleFn
, LibFunc FloatFn
,
43 LibFunc LongDoubleFn
);
45 /// Return V if it is an i8*, otherwise cast it to i8*.
46 Value
*castToCStr(Value
*V
, IRBuilder
<> &B
);
48 /// Emit a call to the strlen function to the builder, for the specified
49 /// pointer. Ptr is required to be some pointer type, and the return value has
51 Value
*emitStrLen(Value
*Ptr
, IRBuilder
<> &B
, const DataLayout
&DL
,
52 const TargetLibraryInfo
*TLI
);
54 /// Emit a call to the strnlen function to the builder, for the specified
55 /// pointer. Ptr is required to be some pointer type, MaxLen must be of size_t
56 /// type, and the return value has 'intptr_t' type.
57 Value
*emitStrNLen(Value
*Ptr
, Value
*MaxLen
, IRBuilder
<> &B
,
58 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
);
60 /// Emit a call to the strchr function to the builder, for the specified
61 /// pointer and character. Ptr is required to be some pointer type, and the
62 /// return value has 'i8*' type.
63 Value
*emitStrChr(Value
*Ptr
, char C
, IRBuilder
<> &B
,
64 const TargetLibraryInfo
*TLI
);
66 /// Emit a call to the strncmp function to the builder.
67 Value
*emitStrNCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
68 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
);
70 /// Emit a call to the strcpy function to the builder, for the specified
71 /// pointer arguments.
72 Value
*emitStrCpy(Value
*Dst
, Value
*Src
, IRBuilder
<> &B
,
73 const TargetLibraryInfo
*TLI
, StringRef Name
= "strcpy");
75 /// Emit a call to the strncpy function to the builder, for the specified
76 /// pointer arguments and length.
77 Value
*emitStrNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilder
<> &B
,
78 const TargetLibraryInfo
*TLI
, StringRef Name
= "strncpy");
80 /// Emit a call to the __memcpy_chk function to the builder. This expects that
81 /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
82 Value
*emitMemCpyChk(Value
*Dst
, Value
*Src
, Value
*Len
, Value
*ObjSize
,
83 IRBuilder
<> &B
, const DataLayout
&DL
,
84 const TargetLibraryInfo
*TLI
);
86 /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
87 /// Val is an i32 value, and Len is an 'intptr_t' value.
88 Value
*emitMemChr(Value
*Ptr
, Value
*Val
, Value
*Len
, IRBuilder
<> &B
,
89 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
);
91 /// Emit a call to the memcmp function.
92 Value
*emitMemCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
93 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
);
95 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
96 /// function is known to take a single of type matching 'Op' and returns one
97 /// value with the same type. If 'Op' is a long double, 'l' is added as the
98 /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
99 Value
*emitUnaryFloatFnCall(Value
*Op
, StringRef Name
, IRBuilder
<> &B
,
100 const AttributeList
&Attrs
);
102 /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
103 /// depending of the type of Op.
104 Value
*emitUnaryFloatFnCall(Value
*Op
, const TargetLibraryInfo
*TLI
,
105 LibFunc DoubleFn
, LibFunc FloatFn
,
106 LibFunc LongDoubleFn
, IRBuilder
<> &B
,
107 const AttributeList
&Attrs
);
109 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
110 /// function is known to take type matching 'Op1' and 'Op2' and return one
111 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
112 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
113 Value
*emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
, StringRef Name
,
114 IRBuilder
<> &B
, const AttributeList
&Attrs
);
116 /// Emit a call to the putchar function. This assumes that Char is an integer.
117 Value
*emitPutChar(Value
*Char
, IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
);
119 /// Emit a call to the puts function. This assumes that Str is some pointer.
120 Value
*emitPutS(Value
*Str
, IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
);
122 /// Emit a call to the fputc function. This assumes that Char is an i32, and
123 /// File is a pointer to FILE.
124 Value
*emitFPutC(Value
*Char
, Value
*File
, IRBuilder
<> &B
,
125 const TargetLibraryInfo
*TLI
);
127 /// Emit a call to the fputc_unlocked function. This assumes that Char is an
128 /// i32, and File is a pointer to FILE.
129 Value
*emitFPutCUnlocked(Value
*Char
, Value
*File
, IRBuilder
<> &B
,
130 const TargetLibraryInfo
*TLI
);
132 /// Emit a call to the fputs function. Str is required to be a pointer and
133 /// File is a pointer to FILE.
134 Value
*emitFPutS(Value
*Str
, Value
*File
, IRBuilder
<> &B
,
135 const TargetLibraryInfo
*TLI
);
137 /// Emit a call to the fputs_unlocked function. Str is required to be a
138 /// pointer and File is a pointer to FILE.
139 Value
*emitFPutSUnlocked(Value
*Str
, Value
*File
, IRBuilder
<> &B
,
140 const TargetLibraryInfo
*TLI
);
142 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
143 /// Size is an 'intptr_t', and File is a pointer to FILE.
144 Value
*emitFWrite(Value
*Ptr
, Value
*Size
, Value
*File
, IRBuilder
<> &B
,
145 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
);
147 /// Emit a call to the malloc function.
148 Value
*emitMalloc(Value
*Num
, IRBuilder
<> &B
, const DataLayout
&DL
,
149 const TargetLibraryInfo
*TLI
);
151 /// Emit a call to the calloc function.
152 Value
*emitCalloc(Value
*Num
, Value
*Size
, const AttributeList
&Attrs
,
153 IRBuilder
<> &B
, const TargetLibraryInfo
&TLI
);
155 /// Emit a call to the fwrite_unlocked function. This assumes that Ptr is a
156 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
157 Value
*emitFWriteUnlocked(Value
*Ptr
, Value
*Size
, Value
*N
, Value
*File
,
158 IRBuilder
<> &B
, const DataLayout
&DL
,
159 const TargetLibraryInfo
*TLI
);
161 /// Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
162 Value
*emitFGetCUnlocked(Value
*File
, IRBuilder
<> &B
,
163 const TargetLibraryInfo
*TLI
);
165 /// Emit a call to the fgets_unlocked function. Str is required to be a
166 /// pointer, Size is an i32 and File is a pointer to FILE.
167 Value
*emitFGetSUnlocked(Value
*Str
, Value
*Size
, Value
*File
, IRBuilder
<> &B
,
168 const TargetLibraryInfo
*TLI
);
170 /// Emit a call to the fread_unlocked function. This assumes that Ptr is a
171 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
172 Value
*emitFReadUnlocked(Value
*Ptr
, Value
*Size
, Value
*N
, Value
*File
,
173 IRBuilder
<> &B
, const DataLayout
&DL
,
174 const TargetLibraryInfo
*TLI
);