BuildLibCalls.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- BuildLibCalls.h - Utility builder for libcalls -----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file exposes an interface to build some C language libcalls for
  15. // optimization passes that need to call the various functions.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
  19. #define LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
  20. #include "llvm/Analysis/TargetLibraryInfo.h"
  21. namespace llvm {
  22. class Value;
  23. class DataLayout;
  24. class IRBuilderBase;
  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. bool inferNonMandatoryLibFuncAttrs(Module *M, StringRef Name,
  33. const TargetLibraryInfo &TLI);
  34. bool inferNonMandatoryLibFuncAttrs(Function &F, const TargetLibraryInfo &TLI);
  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. /// Check whether the library function is available on target and also that
  65. /// it in the current Module is a Function with the right type.
  66. bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI,
  67. LibFunc TheLibFunc);
  68. bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI,
  69. StringRef Name);
  70. /// Check whether the overloaded floating point function
  71. /// corresponding to \a Ty is available.
  72. bool hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty,
  73. LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn);
  74. /// Get the name of the overloaded floating point function
  75. /// corresponding to \a Ty. Return the LibFunc in \a TheLibFunc.
  76. StringRef getFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty,
  77. LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn,
  78. LibFunc &TheLibFunc);
  79. /// Return V if it is an i8*, otherwise cast it to i8*.
  80. Value *castToCStr(Value *V, IRBuilderBase &B);
  81. /// Emit a call to the strlen function to the builder, for the specified
  82. /// pointer. Ptr is required to be some pointer type, and the return value has
  83. /// 'size_t' type.
  84. Value *emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL,
  85. const TargetLibraryInfo *TLI);
  86. /// Emit a call to the strdup function to the builder, for the specified
  87. /// pointer. Ptr is required to be some pointer type, and the return value has
  88. /// 'i8*' type.
  89. Value *emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI);
  90. /// Emit a call to the strchr function to the builder, for the specified
  91. /// pointer and character. Ptr is required to be some pointer type, and the
  92. /// return value has 'i8*' type.
  93. Value *emitStrChr(Value *Ptr, char C, IRBuilderBase &B,
  94. const TargetLibraryInfo *TLI);
  95. /// Emit a call to the strncmp function to the builder.
  96. Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
  97. const DataLayout &DL, const TargetLibraryInfo *TLI);
  98. /// Emit a call to the strcpy function to the builder, for the specified
  99. /// pointer arguments.
  100. Value *emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B,
  101. const TargetLibraryInfo *TLI);
  102. /// Emit a call to the stpcpy function to the builder, for the specified
  103. /// pointer arguments.
  104. Value *emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B,
  105. const TargetLibraryInfo *TLI);
  106. /// Emit a call to the strncpy function to the builder, for the specified
  107. /// pointer arguments and length.
  108. Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
  109. const TargetLibraryInfo *TLI);
  110. /// Emit a call to the stpncpy function to the builder, for the specified
  111. /// pointer arguments and length.
  112. Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
  113. const TargetLibraryInfo *TLI);
  114. /// Emit a call to the __memcpy_chk function to the builder. This expects that
  115. /// the Len and ObjSize have type 'size_t' and Dst/Src are pointers.
  116. Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
  117. IRBuilderBase &B, const DataLayout &DL,
  118. const TargetLibraryInfo *TLI);
  119. /// Emit a call to the mempcpy function.
  120. Value *emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
  121. const DataLayout &DL, const TargetLibraryInfo *TLI);
  122. /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
  123. /// Val is an 'int' value, and Len is an 'size_t' value.
  124. Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
  125. const DataLayout &DL, const TargetLibraryInfo *TLI);
  126. /// Emit a call to the memrchr function, analogously to emitMemChr.
  127. Value *emitMemRChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
  128. const DataLayout &DL, const TargetLibraryInfo *TLI);
  129. /// Emit a call to the memcmp function.
  130. Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
  131. const DataLayout &DL, const TargetLibraryInfo *TLI);
  132. /// Emit a call to the bcmp function.
  133. Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
  134. const DataLayout &DL, const TargetLibraryInfo *TLI);
  135. /// Emit a call to the memccpy function.
  136. Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
  137. IRBuilderBase &B, const TargetLibraryInfo *TLI);
  138. /// Emit a call to the snprintf function.
  139. Value *emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
  140. ArrayRef<Value *> Args, IRBuilderBase &B,
  141. const TargetLibraryInfo *TLI);
  142. /// Emit a call to the sprintf function.
  143. Value *emitSPrintf(Value *Dest, Value *Fmt, ArrayRef<Value *> VariadicArgs,
  144. IRBuilderBase &B, const TargetLibraryInfo *TLI);
  145. /// Emit a call to the strcat function.
  146. Value *emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B,
  147. const TargetLibraryInfo *TLI);
  148. /// Emit a call to the strlcpy function.
  149. Value *emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
  150. const TargetLibraryInfo *TLI);
  151. /// Emit a call to the strlcat function.
  152. Value *emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
  153. const TargetLibraryInfo *TLI);
  154. /// Emit a call to the strncat function.
  155. Value *emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
  156. const TargetLibraryInfo *TLI);
  157. /// Emit a call to the vsnprintf function.
  158. Value *emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
  159. IRBuilderBase &B, const TargetLibraryInfo *TLI);
  160. /// Emit a call to the vsprintf function.
  161. Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B,
  162. const TargetLibraryInfo *TLI);
  163. /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
  164. /// function is known to take a single of type matching 'Op' and returns one
  165. /// value with the same type. If 'Op' is a long double, 'l' is added as the
  166. /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
  167. Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
  168. StringRef Name, IRBuilderBase &B,
  169. const AttributeList &Attrs);
  170. /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
  171. /// depending of the type of Op.
  172. Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
  173. LibFunc DoubleFn, LibFunc FloatFn,
  174. LibFunc LongDoubleFn, IRBuilderBase &B,
  175. const AttributeList &Attrs);
  176. /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
  177. /// function is known to take type matching 'Op1' and 'Op2' and return one
  178. /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
  179. /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
  180. Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2,
  181. const TargetLibraryInfo *TLI,
  182. StringRef Name, IRBuilderBase &B,
  183. const AttributeList &Attrs);
  184. /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn,
  185. /// depending of the type of Op1.
  186. Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2,
  187. const TargetLibraryInfo *TLI, LibFunc DoubleFn,
  188. LibFunc FloatFn, LibFunc LongDoubleFn,
  189. IRBuilderBase &B, const AttributeList &Attrs);
  190. /// Emit a call to the putchar function. This assumes that Char is an 'int'.
  191. Value *emitPutChar(Value *Char, IRBuilderBase &B,
  192. const TargetLibraryInfo *TLI);
  193. /// Emit a call to the puts function. This assumes that Str is some pointer.
  194. Value *emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI);
  195. /// Emit a call to the fputc function. This assumes that Char is an 'int', and
  196. /// File is a pointer to FILE.
  197. Value *emitFPutC(Value *Char, Value *File, IRBuilderBase &B,
  198. const TargetLibraryInfo *TLI);
  199. /// Emit a call to the fputs function. Str is required to be a pointer and
  200. /// File is a pointer to FILE.
  201. Value *emitFPutS(Value *Str, Value *File, IRBuilderBase &B,
  202. const TargetLibraryInfo *TLI);
  203. /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
  204. /// Size is an 'size_t', and File is a pointer to FILE.
  205. Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B,
  206. const DataLayout &DL, const TargetLibraryInfo *TLI);
  207. /// Emit a call to the malloc function.
  208. Value *emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL,
  209. const TargetLibraryInfo *TLI);
  210. /// Emit a call to the calloc function.
  211. Value *emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
  212. const TargetLibraryInfo &TLI);
  213. }
  214. #endif
  215. #ifdef __GNUC__
  216. #pragma GCC diagnostic pop
  217. #endif