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. 26 /// If the library function is unavailable, this doesn't modify it. 27 /// 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); 31 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); 36 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); 42 43 /// Return V if it is an i8*, otherwise cast it to i8*. 44 Value *castToCStr(Value *V, IRBuilderBase &B); 45 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 48 /// 'intptr_t' type. 49 Value *emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, 50 const TargetLibraryInfo *TLI); 51 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 54 /// 'i8*' type. 55 Value *emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI); 56 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); 62 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); 68 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); 72 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); 77 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); 82 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); 87 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); 92 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); 98 99 /// Emit a call to the mempcpy function. 100 Value *emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 101 const DataLayout &DL, const TargetLibraryInfo *TLI); 102 103 /// Emit a call to the memchr function. This assumes that Ptr is a pointer, 104 /// Val is an i32 value, and Len is an 'intptr_t' value. 105 Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, 106 const DataLayout &DL, const TargetLibraryInfo *TLI); 107 108 /// Emit a call to the memcmp function. 109 Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 110 const DataLayout &DL, const TargetLibraryInfo *TLI); 111 112 /// Emit a call to the bcmp function. 113 Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 114 const DataLayout &DL, const TargetLibraryInfo *TLI); 115 116 /// Emit a call to the memccpy function. 117 Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, 118 IRBuilderBase &B, const TargetLibraryInfo *TLI); 119 120 /// Emit a call to the snprintf function. 121 Value *emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, 122 ArrayRef<Value *> Args, IRBuilderBase &B, 123 const TargetLibraryInfo *TLI); 124 125 /// Emit a call to the sprintf function. 126 Value *emitSPrintf(Value *Dest, Value *Fmt, ArrayRef<Value *> VariadicArgs, 127 IRBuilderBase &B, const TargetLibraryInfo *TLI); 128 129 /// Emit a call to the strcat function. 130 Value *emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, 131 const TargetLibraryInfo *TLI); 132 133 /// Emit a call to the strlcpy function. 134 Value *emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 135 const TargetLibraryInfo *TLI); 136 137 /// Emit a call to the strlcat function. 138 Value *emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 139 const TargetLibraryInfo *TLI); 140 141 /// Emit a call to the strncat function. 142 Value *emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 143 const TargetLibraryInfo *TLI); 144 145 /// Emit a call to the vsnprintf function. 146 Value *emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, 147 IRBuilderBase &B, const TargetLibraryInfo *TLI); 148 149 /// Emit a call to the vsprintf function. 150 Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B, 151 const TargetLibraryInfo *TLI); 152 153 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This 154 /// function is known to take a single of type matching 'Op' and returns one 155 /// value with the same type. If 'Op' is a long double, 'l' is added as the 156 /// suffix of name, if 'Op' is a float, we add a 'f' suffix. 157 Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilderBase &B, 158 const AttributeList &Attrs); 159 160 /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn, 161 /// depending of the type of Op. 162 Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 163 LibFunc DoubleFn, LibFunc FloatFn, 164 LibFunc LongDoubleFn, IRBuilderBase &B, 165 const AttributeList &Attrs); 166 167 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This 168 /// function is known to take type matching 'Op1' and 'Op2' and return one 169 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as 170 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix. 171 Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, 172 IRBuilderBase &B, const AttributeList &Attrs); 173 174 /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn, 175 /// depending of the type of Op1. 176 Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, 177 const TargetLibraryInfo *TLI, LibFunc DoubleFn, 178 LibFunc FloatFn, LibFunc LongDoubleFn, 179 IRBuilderBase &B, const AttributeList &Attrs); 180 181 /// Emit a call to the putchar function. This assumes that Char is an integer. 182 Value *emitPutChar(Value *Char, IRBuilderBase &B, 183 const TargetLibraryInfo *TLI); 184 185 /// Emit a call to the puts function. This assumes that Str is some pointer. 186 Value *emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI); 187 188 /// Emit a call to the fputc function. This assumes that Char is an i32, and 189 /// File is a pointer to FILE. 190 Value *emitFPutC(Value *Char, Value *File, IRBuilderBase &B, 191 const TargetLibraryInfo *TLI); 192 193 /// Emit a call to the fputs function. Str is required to be a pointer and 194 /// File is a pointer to FILE. 195 Value *emitFPutS(Value *Str, Value *File, IRBuilderBase &B, 196 const TargetLibraryInfo *TLI); 197 198 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer, 199 /// Size is an 'intptr_t', and File is a pointer to FILE. 200 Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, 201 const DataLayout &DL, const TargetLibraryInfo *TLI); 202 203 /// Emit a call to the malloc function. 204 Value *emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, 205 const TargetLibraryInfo *TLI); 206 207 /// Emit a call to the calloc function. 208 Value *emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs, 209 IRBuilderBase &B, const TargetLibraryInfo &TLI); 210 } 211 212 #endif 213