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/Support/Compiler.h" 19 20 namespace llvm { 21 class Value; 22 class DataLayout; 23 class IRBuilderBase; 24 25 /// Analyze the name and prototype of the given function and set any 26 /// applicable attributes. Note that this merely helps optimizations on an 27 /// already existing function but does not consider mandatory attributes. 28 /// 29 /// If the library function is unavailable, this doesn't modify it. 30 /// 31 /// Returns true if any attributes were set and false otherwise. 32 LLVM_ABI bool inferNonMandatoryLibFuncAttrs(Module *M, StringRef Name, 33 const TargetLibraryInfo &TLI); 34 LLVM_ABI bool inferNonMandatoryLibFuncAttrs(Function &F, 35 const TargetLibraryInfo &TLI); 36 37 /// Calls getOrInsertFunction() and then makes sure to add mandatory 38 /// argument attributes. 39 LLVM_ABI FunctionCallee getOrInsertLibFunc(Module *M, 40 const TargetLibraryInfo &TLI, 41 LibFunc TheLibFunc, 42 FunctionType *T, 43 AttributeList AttributeList); 44 LLVM_ABI FunctionCallee getOrInsertLibFunc(Module *M, 45 const TargetLibraryInfo &TLI, 46 LibFunc TheLibFunc, 47 FunctionType *T); 48 template <typename... ArgsTy> getOrInsertLibFunc(Module * M,const TargetLibraryInfo & TLI,LibFunc TheLibFunc,AttributeList AttributeList,Type * RetTy,ArgsTy...Args)49 FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI, 50 LibFunc TheLibFunc, AttributeList AttributeList, 51 Type *RetTy, ArgsTy... Args) { 52 SmallVector<Type*, sizeof...(ArgsTy)> ArgTys{Args...}; 53 return getOrInsertLibFunc(M, TLI, TheLibFunc, 54 FunctionType::get(RetTy, ArgTys, false), 55 AttributeList); 56 } 57 /// Same as above, but without the attributes. 58 template <typename... ArgsTy> getOrInsertLibFunc(Module * M,const TargetLibraryInfo & TLI,LibFunc TheLibFunc,Type * RetTy,ArgsTy...Args)59 FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI, 60 LibFunc TheLibFunc, Type *RetTy, ArgsTy... Args) { 61 return getOrInsertLibFunc(M, TLI, TheLibFunc, AttributeList{}, RetTy, 62 Args...); 63 } 64 // Avoid an incorrect ordering that'd otherwise compile incorrectly. 65 template <typename... ArgsTy> 66 FunctionCallee 67 getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI, 68 LibFunc TheLibFunc, AttributeList AttributeList, 69 FunctionType *Invalid, ArgsTy... Args) = delete; 70 71 // Handle -mregparm for the given function. 72 // Note that this function is a rough approximation that only works for simple 73 // function signatures; it does not apply other relevant attributes for 74 // function signatures, including sign/zero-extension for arguments and return 75 // values. 76 LLVM_ABI void markRegisterParameterAttributes(Function *F); 77 78 /// Check whether the library function is available on target and also that 79 /// it in the current Module is a Function with the right type. 80 LLVM_ABI bool isLibFuncEmittable(const Module *M, 81 const TargetLibraryInfo *TLI, 82 LibFunc TheLibFunc); 83 LLVM_ABI bool isLibFuncEmittable(const Module *M, 84 const TargetLibraryInfo *TLI, 85 StringRef Name); 86 87 /// Check whether the overloaded floating point function 88 /// corresponding to \a Ty is available. 89 LLVM_ABI bool hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, 90 Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, 91 LibFunc LongDoubleFn); 92 93 /// Get the name of the overloaded floating point function 94 /// corresponding to \a Ty. Return the LibFunc in \a TheLibFunc. 95 LLVM_ABI StringRef getFloatFn(const Module *M, const TargetLibraryInfo *TLI, 96 Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, 97 LibFunc LongDoubleFn, LibFunc &TheLibFunc); 98 99 /// Emit a call to the strlen function to the builder, for the specified 100 /// pointer. Ptr is required to be some pointer type, and the return value has 101 /// 'size_t' type. 102 LLVM_ABI Value *emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, 103 const TargetLibraryInfo *TLI); 104 105 /// Emit a call to the wcslen function to the builder, for the specified 106 /// pointer. Ptr is required to be some pointer type, and the return value has 107 /// 'size_t' type. 108 LLVM_ABI Value *emitWcsLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, 109 const TargetLibraryInfo *TLI); 110 111 /// Emit a call to the strdup function to the builder, for the specified 112 /// pointer. Ptr is required to be some pointer type, and the return value has 113 /// 'i8*' type. 114 LLVM_ABI Value *emitStrDup(Value *Ptr, IRBuilderBase &B, 115 const TargetLibraryInfo *TLI); 116 117 /// Emit a call to the strchr function to the builder, for the specified 118 /// pointer and character. Ptr is required to be some pointer type, and the 119 /// return value has 'i8*' type. 120 LLVM_ABI Value *emitStrChr(Value *Ptr, char C, IRBuilderBase &B, 121 const TargetLibraryInfo *TLI); 122 123 /// Emit a call to the strncmp function to the builder. 124 LLVM_ABI Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, 125 IRBuilderBase &B, const DataLayout &DL, 126 const TargetLibraryInfo *TLI); 127 128 /// Emit a call to the strcpy function to the builder, for the specified 129 /// pointer arguments. 130 LLVM_ABI Value *emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B, 131 const TargetLibraryInfo *TLI); 132 133 /// Emit a call to the stpcpy function to the builder, for the specified 134 /// pointer arguments. 135 LLVM_ABI Value *emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B, 136 const TargetLibraryInfo *TLI); 137 138 /// Emit a call to the strncpy function to the builder, for the specified 139 /// pointer arguments and length. 140 LLVM_ABI Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, 141 IRBuilderBase &B, const TargetLibraryInfo *TLI); 142 143 /// Emit a call to the stpncpy function to the builder, for the specified 144 /// pointer arguments and length. 145 LLVM_ABI Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, 146 IRBuilderBase &B, const TargetLibraryInfo *TLI); 147 148 /// Emit a call to the __memcpy_chk function to the builder. This expects that 149 /// the Len and ObjSize have type 'size_t' and Dst/Src are pointers. 150 LLVM_ABI Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, 151 Value *ObjSize, IRBuilderBase &B, 152 const DataLayout &DL, 153 const TargetLibraryInfo *TLI); 154 155 /// Emit a call to the mempcpy function. 156 LLVM_ABI Value *emitMemPCpy(Value *Dst, Value *Src, Value *Len, 157 IRBuilderBase &B, const DataLayout &DL, 158 const TargetLibraryInfo *TLI); 159 160 /// Emit a call to the memchr function. This assumes that Ptr is a pointer, 161 /// Val is an 'int' value, and Len is an 'size_t' value. 162 LLVM_ABI Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, 163 IRBuilderBase &B, const DataLayout &DL, 164 const TargetLibraryInfo *TLI); 165 166 /// Emit a call to the memrchr function, analogously to emitMemChr. 167 LLVM_ABI Value *emitMemRChr(Value *Ptr, Value *Val, Value *Len, 168 IRBuilderBase &B, const DataLayout &DL, 169 const TargetLibraryInfo *TLI); 170 171 /// Emit a call to the memcmp function. 172 LLVM_ABI Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, 173 IRBuilderBase &B, const DataLayout &DL, 174 const TargetLibraryInfo *TLI); 175 176 /// Emit a call to the bcmp function. 177 LLVM_ABI Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, 178 IRBuilderBase &B, const DataLayout &DL, 179 const TargetLibraryInfo *TLI); 180 181 /// Emit a call to the memccpy function. 182 LLVM_ABI Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, 183 IRBuilderBase &B, const TargetLibraryInfo *TLI); 184 185 /// Emit a call to the snprintf function. 186 LLVM_ABI Value *emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, 187 ArrayRef<Value *> Args, IRBuilderBase &B, 188 const TargetLibraryInfo *TLI); 189 190 /// Emit a call to the sprintf function. 191 LLVM_ABI Value *emitSPrintf(Value *Dest, Value *Fmt, 192 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B, 193 const TargetLibraryInfo *TLI); 194 195 /// Emit a call to the strcat function. 196 LLVM_ABI Value *emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, 197 const TargetLibraryInfo *TLI); 198 199 /// Emit a call to the strlcpy function. 200 LLVM_ABI Value *emitStrLCpy(Value *Dest, Value *Src, Value *Size, 201 IRBuilderBase &B, const TargetLibraryInfo *TLI); 202 203 /// Emit a call to the strlcat function. 204 LLVM_ABI Value *emitStrLCat(Value *Dest, Value *Src, Value *Size, 205 IRBuilderBase &B, const TargetLibraryInfo *TLI); 206 207 /// Emit a call to the strncat function. 208 LLVM_ABI Value *emitStrNCat(Value *Dest, Value *Src, Value *Size, 209 IRBuilderBase &B, const TargetLibraryInfo *TLI); 210 211 /// Emit a call to the vsnprintf function. 212 LLVM_ABI Value *emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, 213 Value *VAList, IRBuilderBase &B, 214 const TargetLibraryInfo *TLI); 215 216 /// Emit a call to the vsprintf function. 217 LLVM_ABI Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, 218 IRBuilderBase &B, const TargetLibraryInfo *TLI); 219 220 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This 221 /// function is known to take a single of type matching 'Op' and returns one 222 /// value with the same type. If 'Op' is a long double, 'l' is added as the 223 /// suffix of name, if 'Op' is a float, we add a 'f' suffix. 224 LLVM_ABI Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 225 StringRef Name, IRBuilderBase &B, 226 const AttributeList &Attrs); 227 228 /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn, 229 /// depending of the type of Op. 230 LLVM_ABI Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 231 LibFunc DoubleFn, LibFunc FloatFn, 232 LibFunc LongDoubleFn, IRBuilderBase &B, 233 const AttributeList &Attrs); 234 235 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This 236 /// function is known to take type matching 'Op1' and 'Op2' and return one 237 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as 238 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix. 239 LLVM_ABI Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, 240 const TargetLibraryInfo *TLI, 241 StringRef Name, IRBuilderBase &B, 242 const AttributeList &Attrs); 243 244 /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn, 245 /// depending of the type of Op1. 246 LLVM_ABI Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, 247 const TargetLibraryInfo *TLI, 248 LibFunc DoubleFn, LibFunc FloatFn, 249 LibFunc LongDoubleFn, IRBuilderBase &B, 250 const AttributeList &Attrs); 251 252 /// Emit a call to the putchar function. This assumes that Char is an 'int'. 253 LLVM_ABI Value *emitPutChar(Value *Char, IRBuilderBase &B, 254 const TargetLibraryInfo *TLI); 255 256 /// Emit a call to the puts function. This assumes that Str is some pointer. 257 LLVM_ABI Value *emitPutS(Value *Str, IRBuilderBase &B, 258 const TargetLibraryInfo *TLI); 259 260 /// Emit a call to the fputc function. This assumes that Char is an 'int', and 261 /// File is a pointer to FILE. 262 LLVM_ABI Value *emitFPutC(Value *Char, Value *File, IRBuilderBase &B, 263 const TargetLibraryInfo *TLI); 264 265 /// Emit a call to the fputs function. Str is required to be a pointer and 266 /// File is a pointer to FILE. 267 LLVM_ABI Value *emitFPutS(Value *Str, Value *File, IRBuilderBase &B, 268 const TargetLibraryInfo *TLI); 269 270 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer, 271 /// Size is an 'size_t', and File is a pointer to FILE. 272 LLVM_ABI Value *emitFWrite(Value *Ptr, Value *Size, Value *File, 273 IRBuilderBase &B, const DataLayout &DL, 274 const TargetLibraryInfo *TLI); 275 276 /// Emit a call to the malloc function. 277 LLVM_ABI Value *emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, 278 const TargetLibraryInfo *TLI); 279 280 /// Emit a call to the calloc function. 281 LLVM_ABI Value *emitCalloc(Value *Num, Value *Size, IRBuilderBase &B, 282 const TargetLibraryInfo &TLI, unsigned AddrSpace); 283 284 /// Emit a call to the hot/cold operator new function. 285 LLVM_ABI Value *emitHotColdNew(Value *Num, IRBuilderBase &B, 286 const TargetLibraryInfo *TLI, LibFunc NewFunc, 287 uint8_t HotCold); 288 LLVM_ABI Value *emitHotColdNewNoThrow(Value *Num, Value *NoThrow, 289 IRBuilderBase &B, 290 const TargetLibraryInfo *TLI, 291 LibFunc NewFunc, uint8_t HotCold); 292 LLVM_ABI Value *emitHotColdNewAligned(Value *Num, Value *Align, 293 IRBuilderBase &B, 294 const TargetLibraryInfo *TLI, 295 LibFunc NewFunc, uint8_t HotCold); 296 LLVM_ABI Value *emitHotColdNewAlignedNoThrow(Value *Num, Value *Align, 297 Value *NoThrow, IRBuilderBase &B, 298 const TargetLibraryInfo *TLI, 299 LibFunc NewFunc, 300 uint8_t HotCold); 301 LLVM_ABI Value *emitHotColdSizeReturningNew(Value *Num, IRBuilderBase &B, 302 const TargetLibraryInfo *TLI, 303 LibFunc NewFunc, uint8_t HotCold); 304 LLVM_ABI Value * 305 emitHotColdSizeReturningNewAligned(Value *Num, Value *Align, IRBuilderBase &B, 306 const TargetLibraryInfo *TLI, 307 LibFunc NewFunc, uint8_t HotCold); 308 } 309 310 #endif 311