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/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 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); 38 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); 44 45 /// Return V if it is an i8*, otherwise cast it to i8*. 46 Value *castToCStr(Value *V, IRBuilder<> &B); 47 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 50 /// 'intptr_t' type. 51 Value *emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL, 52 const TargetLibraryInfo *TLI); 53 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); 59 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); 65 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); 69 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); 74 75 /// Emit a call to the stpcpy function to the builder, for the specified 76 /// pointer arguments. 77 Value *emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B, 78 const TargetLibraryInfo *TLI); 79 80 /// Emit a call to the strncpy function to the builder, for the specified 81 /// pointer arguments and length. 82 Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, 83 const TargetLibraryInfo *TLI); 84 85 /// Emit a call to the stpncpy function to the builder, for the specified 86 /// pointer arguments and length. 87 Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, 88 const TargetLibraryInfo *TLI); 89 90 /// Emit a call to the __memcpy_chk function to the builder. This expects that 91 /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers. 92 Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, 93 IRBuilder<> &B, const DataLayout &DL, 94 const TargetLibraryInfo *TLI); 95 96 /// Emit a call to the memchr function. This assumes that Ptr is a pointer, 97 /// Val is an i32 value, and Len is an 'intptr_t' value. 98 Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B, 99 const DataLayout &DL, const TargetLibraryInfo *TLI); 100 101 /// Emit a call to the memcmp function. 102 Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, 103 const DataLayout &DL, const TargetLibraryInfo *TLI); 104 105 /// Emit a call to the bcmp function. 106 Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, 107 const DataLayout &DL, const TargetLibraryInfo *TLI); 108 109 /// Emit a call to the memccpy function. 110 Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, 111 IRBuilder<> &B, const TargetLibraryInfo *TLI); 112 113 /// Emit a call to the snprintf function. 114 Value *emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, 115 ArrayRef<Value *> Args, IRBuilder<> &B, 116 const TargetLibraryInfo *TLI); 117 118 /// Emit a call to the sprintf function. 119 Value *emitSPrintf(Value *Dest, Value *Fmt, ArrayRef<Value *> VariadicArgs, 120 IRBuilder<> &B, const TargetLibraryInfo *TLI); 121 122 /// Emit a call to the strcat function. 123 Value *emitStrCat(Value *Dest, Value *Src, IRBuilder<> &B, 124 const TargetLibraryInfo *TLI); 125 126 /// Emit a call to the strlcpy function. 127 Value *emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, 128 const TargetLibraryInfo *TLI); 129 130 /// Emit a call to the strlcat function. 131 Value *emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, 132 const TargetLibraryInfo *TLI); 133 134 /// Emit a call to the strncat function. 135 Value *emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, 136 const TargetLibraryInfo *TLI); 137 138 /// Emit a call to the vsnprintf function. 139 Value *emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, 140 IRBuilder<> &B, const TargetLibraryInfo *TLI); 141 142 /// Emit a call to the vsprintf function. 143 Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilder<> &B, 144 const TargetLibraryInfo *TLI); 145 146 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This 147 /// function is known to take a single of type matching 'Op' and returns one 148 /// value with the same type. If 'Op' is a long double, 'l' is added as the 149 /// suffix of name, if 'Op' is a float, we add a 'f' suffix. 150 Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, 151 const AttributeList &Attrs); 152 153 /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn, 154 /// depending of the type of Op. 155 Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 156 LibFunc DoubleFn, LibFunc FloatFn, 157 LibFunc LongDoubleFn, IRBuilder<> &B, 158 const AttributeList &Attrs); 159 160 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This 161 /// function is known to take type matching 'Op1' and 'Op2' and return one 162 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as 163 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix. 164 Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, 165 IRBuilder<> &B, const AttributeList &Attrs); 166 167 /// Emit a call to the putchar function. This assumes that Char is an integer. 168 Value *emitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI); 169 170 /// Emit a call to the puts function. This assumes that Str is some pointer. 171 Value *emitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI); 172 173 /// Emit a call to the fputc function. This assumes that Char is an i32, and 174 /// File is a pointer to FILE. 175 Value *emitFPutC(Value *Char, Value *File, IRBuilder<> &B, 176 const TargetLibraryInfo *TLI); 177 178 /// Emit a call to the fputc_unlocked function. This assumes that Char is an 179 /// i32, and File is a pointer to FILE. 180 Value *emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B, 181 const TargetLibraryInfo *TLI); 182 183 /// Emit a call to the fputs function. Str is required to be a pointer and 184 /// File is a pointer to FILE. 185 Value *emitFPutS(Value *Str, Value *File, IRBuilder<> &B, 186 const TargetLibraryInfo *TLI); 187 188 /// Emit a call to the fputs_unlocked function. Str is required to be a 189 /// pointer and File is a pointer to FILE. 190 Value *emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B, 191 const TargetLibraryInfo *TLI); 192 193 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer, 194 /// Size is an 'intptr_t', and File is a pointer to FILE. 195 Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, 196 const DataLayout &DL, const TargetLibraryInfo *TLI); 197 198 /// Emit a call to the malloc function. 199 Value *emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL, 200 const TargetLibraryInfo *TLI); 201 202 /// Emit a call to the calloc function. 203 Value *emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs, 204 IRBuilder<> &B, const TargetLibraryInfo &TLI); 205 206 /// Emit a call to the fwrite_unlocked function. This assumes that Ptr is a 207 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE. 208 Value *emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, 209 IRBuilder<> &B, const DataLayout &DL, 210 const TargetLibraryInfo *TLI); 211 212 /// Emit a call to the fgetc_unlocked function. File is a pointer to FILE. 213 Value *emitFGetCUnlocked(Value *File, IRBuilder<> &B, 214 const TargetLibraryInfo *TLI); 215 216 /// Emit a call to the fgets_unlocked function. Str is required to be a 217 /// pointer, Size is an i32 and File is a pointer to FILE. 218 Value *emitFGetSUnlocked(Value *Str, Value *Size, Value *File, IRBuilder<> &B, 219 const TargetLibraryInfo *TLI); 220 221 /// Emit a call to the fread_unlocked function. This assumes that Ptr is a 222 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE. 223 Value *emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, 224 IRBuilder<> &B, const DataLayout &DL, 225 const TargetLibraryInfo *TLI); 226 } 227 228 #endif 229