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"
24 /// Analyze the name and prototype of the given function and set any
25 /// applicable attributes.
26 /// If the library function is unavailable, this doesn't modify it.
28 /// Returns true if any attributes were set and false otherwise.
29 bool inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI);
30 bool inferLibFuncAttributes(Module *M, StringRef Name, const TargetLibraryInfo &TLI);
32 /// Check whether the overloaded floating point function
33 /// corresponding to \a Ty is available.
34 bool hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
35 LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn);
37 /// Get the name of the overloaded floating point function
38 /// corresponding to \a Ty.
39 StringRef getFloatFnName(const TargetLibraryInfo *TLI, Type *Ty,
40 LibFunc DoubleFn, LibFunc FloatFn,
41 LibFunc LongDoubleFn);
43 /// Return V if it is an i8*, otherwise cast it to i8*.
44 Value *castToCStr(Value *V, IRBuilderBase &B);
46 /// Emit a call to the strlen function to the builder, for the specified
47 /// pointer. Ptr is required to be some pointer type, and the return value has
49 Value *emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL,
50 const TargetLibraryInfo *TLI);
52 /// Emit a call to the strdup function to the builder, for the specified
53 /// pointer. Ptr is required to be some pointer type, and the return value has
55 Value *emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI);
57 /// Emit a call to the strnlen function to the builder, for the specified
58 /// pointer. Ptr is required to be some pointer type, MaxLen must be of size_t
59 /// type, and the return value has 'intptr_t' type.
60 Value *emitStrNLen(Value *Ptr, Value *MaxLen, IRBuilderBase &B,
61 const DataLayout &DL, const TargetLibraryInfo *TLI);
63 /// Emit a call to the strchr function to the builder, for the specified
64 /// pointer and character. Ptr is required to be some pointer type, and the
65 /// return value has 'i8*' type.
66 Value *emitStrChr(Value *Ptr, char C, IRBuilderBase &B,
67 const TargetLibraryInfo *TLI);
69 /// Emit a call to the strncmp function to the builder.
70 Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
71 const DataLayout &DL, const TargetLibraryInfo *TLI);
73 /// Emit a call to the strcpy function to the builder, for the specified
74 /// pointer arguments.
75 Value *emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B,
76 const TargetLibraryInfo *TLI);
78 /// Emit a call to the stpcpy function to the builder, for the specified
79 /// pointer arguments.
80 Value *emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B,
81 const TargetLibraryInfo *TLI);
83 /// Emit a call to the strncpy function to the builder, for the specified
84 /// pointer arguments and length.
85 Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
86 const TargetLibraryInfo *TLI);
88 /// Emit a call to the stpncpy function to the builder, for the specified
89 /// pointer arguments and length.
90 Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
91 const TargetLibraryInfo *TLI);
93 /// Emit a call to the __memcpy_chk function to the builder. This expects that
94 /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
95 Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
96 IRBuilderBase &B, const DataLayout &DL,
97 const TargetLibraryInfo *TLI);
99 /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
100 /// Val is an i32 value, and Len is an 'intptr_t' value.
101 Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
102 const DataLayout &DL, const TargetLibraryInfo *TLI);
104 /// Emit a call to the memcmp function.
105 Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
106 const DataLayout &DL, const TargetLibraryInfo *TLI);
108 /// Emit a call to the bcmp function.
109 Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
110 const DataLayout &DL, const TargetLibraryInfo *TLI);
112 /// Emit a call to the memccpy function.
113 Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
114 IRBuilderBase &B, const TargetLibraryInfo *TLI);
116 /// Emit a call to the snprintf function.
117 Value *emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
118 ArrayRef<Value *> Args, IRBuilderBase &B,
119 const TargetLibraryInfo *TLI);
121 /// Emit a call to the sprintf function.
122 Value *emitSPrintf(Value *Dest, Value *Fmt, ArrayRef<Value *> VariadicArgs,
123 IRBuilderBase &B, const TargetLibraryInfo *TLI);
125 /// Emit a call to the strcat function.
126 Value *emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B,
127 const TargetLibraryInfo *TLI);
129 /// Emit a call to the strlcpy function.
130 Value *emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
131 const TargetLibraryInfo *TLI);
133 /// Emit a call to the strlcat function.
134 Value *emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
135 const TargetLibraryInfo *TLI);
137 /// Emit a call to the strncat function.
138 Value *emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
139 const TargetLibraryInfo *TLI);
141 /// Emit a call to the vsnprintf function.
142 Value *emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
143 IRBuilderBase &B, const TargetLibraryInfo *TLI);
145 /// Emit a call to the vsprintf function.
146 Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B,
147 const TargetLibraryInfo *TLI);
149 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
150 /// function is known to take a single of type matching 'Op' and returns one
151 /// value with the same type. If 'Op' is a long double, 'l' is added as the
152 /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
153 Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilderBase &B,
154 const AttributeList &Attrs);
156 /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
157 /// depending of the type of Op.
158 Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
159 LibFunc DoubleFn, LibFunc FloatFn,
160 LibFunc LongDoubleFn, IRBuilderBase &B,
161 const AttributeList &Attrs);
163 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
164 /// function is known to take type matching 'Op1' and 'Op2' and return one
165 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
166 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
167 Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
168 IRBuilderBase &B, const AttributeList &Attrs);
170 /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn,
171 /// depending of the type of Op1.
172 Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2,
173 const TargetLibraryInfo *TLI, LibFunc DoubleFn,
174 LibFunc FloatFn, LibFunc LongDoubleFn,
175 IRBuilderBase &B, const AttributeList &Attrs);
177 /// Emit a call to the putchar function. This assumes that Char is an integer.
178 Value *emitPutChar(Value *Char, IRBuilderBase &B,
179 const TargetLibraryInfo *TLI);
181 /// Emit a call to the puts function. This assumes that Str is some pointer.
182 Value *emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI);
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, IRBuilderBase &B,
187 const TargetLibraryInfo *TLI);
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, IRBuilderBase &B,
192 const TargetLibraryInfo *TLI);
194 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
195 /// Size is an 'intptr_t', and File is a pointer to FILE.
196 Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B,
197 const DataLayout &DL, const TargetLibraryInfo *TLI);
199 /// Emit a call to the malloc function.
200 Value *emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL,
201 const TargetLibraryInfo *TLI);
203 /// Emit a call to the calloc function.
204 Value *emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
205 IRBuilderBase &B, const TargetLibraryInfo &TLI);