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 strchr function to the builder, for the specified 58 /// pointer and character. Ptr is required to be some pointer type, and the 59 /// return value has 'i8*' type. 60 Value *emitStrChr(Value *Ptr, char C, IRBuilderBase &B, 61 const TargetLibraryInfo *TLI); 62 63 /// Emit a call to the strncmp function to the builder. 64 Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 65 const DataLayout &DL, const TargetLibraryInfo *TLI); 66 67 /// Emit a call to the strcpy function to the builder, for the specified 68 /// pointer arguments. 69 Value *emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B, 70 const TargetLibraryInfo *TLI); 71 72 /// Emit a call to the stpcpy function to the builder, for the specified 73 /// pointer arguments. 74 Value *emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B, 75 const TargetLibraryInfo *TLI); 76 77 /// Emit a call to the strncpy function to the builder, for the specified 78 /// pointer arguments and length. 79 Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 80 const TargetLibraryInfo *TLI); 81 82 /// Emit a call to the stpncpy function to the builder, for the specified 83 /// pointer arguments and length. 84 Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 85 const TargetLibraryInfo *TLI); 86 87 /// Emit a call to the __memcpy_chk function to the builder. This expects that 88 /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers. 89 Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, 90 IRBuilderBase &B, const DataLayout &DL, 91 const TargetLibraryInfo *TLI); 92 93 /// Emit a call to the mempcpy function. 94 Value *emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 95 const DataLayout &DL, const TargetLibraryInfo *TLI); 96 97 /// Emit a call to the memchr function. This assumes that Ptr is a pointer, 98 /// Val is an i32 value, and Len is an 'intptr_t' value. 99 Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, 100 const DataLayout &DL, const TargetLibraryInfo *TLI); 101 102 /// Emit a call to the memcmp function. 103 Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 104 const DataLayout &DL, const TargetLibraryInfo *TLI); 105 106 /// Emit a call to the bcmp function. 107 Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 108 const DataLayout &DL, const TargetLibraryInfo *TLI); 109 110 /// Emit a call to the memccpy function. 111 Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, 112 IRBuilderBase &B, const TargetLibraryInfo *TLI); 113 114 /// Emit a call to the snprintf function. 115 Value *emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, 116 ArrayRef<Value *> Args, IRBuilderBase &B, 117 const TargetLibraryInfo *TLI); 118 119 /// Emit a call to the sprintf function. 120 Value *emitSPrintf(Value *Dest, Value *Fmt, ArrayRef<Value *> VariadicArgs, 121 IRBuilderBase &B, const TargetLibraryInfo *TLI); 122 123 /// Emit a call to the strcat function. 124 Value *emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, 125 const TargetLibraryInfo *TLI); 126 127 /// Emit a call to the strlcpy function. 128 Value *emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 129 const TargetLibraryInfo *TLI); 130 131 /// Emit a call to the strlcat function. 132 Value *emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 133 const TargetLibraryInfo *TLI); 134 135 /// Emit a call to the strncat function. 136 Value *emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 137 const TargetLibraryInfo *TLI); 138 139 /// Emit a call to the vsnprintf function. 140 Value *emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, 141 IRBuilderBase &B, const TargetLibraryInfo *TLI); 142 143 /// Emit a call to the vsprintf function. 144 Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B, 145 const TargetLibraryInfo *TLI); 146 147 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This 148 /// function is known to take a single of type matching 'Op' and returns one 149 /// value with the same type. If 'Op' is a long double, 'l' is added as the 150 /// suffix of name, if 'Op' is a float, we add a 'f' suffix. 151 Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilderBase &B, 152 const AttributeList &Attrs); 153 154 /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn, 155 /// depending of the type of Op. 156 Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 157 LibFunc DoubleFn, LibFunc FloatFn, 158 LibFunc LongDoubleFn, IRBuilderBase &B, 159 const AttributeList &Attrs); 160 161 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This 162 /// function is known to take type matching 'Op1' and 'Op2' and return one 163 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as 164 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix. 165 Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, 166 IRBuilderBase &B, const AttributeList &Attrs); 167 168 /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn, 169 /// depending of the type of Op1. 170 Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, 171 const TargetLibraryInfo *TLI, LibFunc DoubleFn, 172 LibFunc FloatFn, LibFunc LongDoubleFn, 173 IRBuilderBase &B, const AttributeList &Attrs); 174 175 /// Emit a call to the putchar function. This assumes that Char is an integer. 176 Value *emitPutChar(Value *Char, IRBuilderBase &B, 177 const TargetLibraryInfo *TLI); 178 179 /// Emit a call to the puts function. This assumes that Str is some pointer. 180 Value *emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI); 181 182 /// Emit a call to the fputc function. This assumes that Char is an i32, and 183 /// File is a pointer to FILE. 184 Value *emitFPutC(Value *Char, Value *File, IRBuilderBase &B, 185 const TargetLibraryInfo *TLI); 186 187 /// Emit a call to the fputs function. Str is required to be a pointer and 188 /// File is a pointer to FILE. 189 Value *emitFPutS(Value *Str, Value *File, IRBuilderBase &B, 190 const TargetLibraryInfo *TLI); 191 192 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer, 193 /// Size is an 'intptr_t', and File is a pointer to FILE. 194 Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, 195 const DataLayout &DL, const TargetLibraryInfo *TLI); 196 197 /// Emit a call to the malloc function. 198 Value *emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, 199 const TargetLibraryInfo *TLI); 200 201 /// Emit a call to the calloc function. 202 Value *emitCalloc(Value *Num, Value *Size, IRBuilderBase &B, 203 const TargetLibraryInfo &TLI); 204 } 205 206 #endif 207