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