BuildLibCalls.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.
  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. /// 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. /// Get the name of the overloaded floating point function
  37. /// corresponding to \a Ty.
  38. StringRef getFloatFnName(const TargetLibraryInfo *TLI, Type *Ty,
  39. LibFunc DoubleFn, LibFunc FloatFn,
  40. LibFunc LongDoubleFn);
  41. /// Return V if it is an i8*, otherwise cast it to i8*.
  42. Value *castToCStr(Value *V, IRBuilderBase &B);
  43. /// Emit a call to the strlen function to the builder, for the specified
  44. /// pointer. Ptr is required to be some pointer type, and the return value has
  45. /// 'intptr_t' type.
  46. Value *emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL,
  47. const TargetLibraryInfo *TLI);
  48. /// Emit a call to the strdup function to the builder, for the specified
  49. /// pointer. Ptr is required to be some pointer type, and the return value has
  50. /// 'i8*' type.
  51. Value *emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI);
  52. /// Emit a call to the strchr function to the builder, for the specified
  53. /// pointer and character. Ptr is required to be some pointer type, and the
  54. /// return value has 'i8*' type.
  55. Value *emitStrChr(Value *Ptr, char C, IRBuilderBase &B,
  56. const TargetLibraryInfo *TLI);
  57. /// Emit a call to the strncmp function to the builder.
  58. Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
  59. const DataLayout &DL, const TargetLibraryInfo *TLI);
  60. /// Emit a call to the strcpy function to the builder, for the specified
  61. /// pointer arguments.
  62. Value *emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B,
  63. const TargetLibraryInfo *TLI);
  64. /// Emit a call to the stpcpy function to the builder, for the specified
  65. /// pointer arguments.
  66. Value *emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B,
  67. const TargetLibraryInfo *TLI);
  68. /// Emit a call to the strncpy function to the builder, for the specified
  69. /// pointer arguments and length.
  70. Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
  71. const TargetLibraryInfo *TLI);
  72. /// Emit a call to the stpncpy function to the builder, for the specified
  73. /// pointer arguments and length.
  74. Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
  75. const TargetLibraryInfo *TLI);
  76. /// Emit a call to the __memcpy_chk function to the builder. This expects that
  77. /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
  78. Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
  79. IRBuilderBase &B, const DataLayout &DL,
  80. const TargetLibraryInfo *TLI);
  81. /// Emit a call to the mempcpy function.
  82. Value *emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
  83. const DataLayout &DL, const TargetLibraryInfo *TLI);
  84. /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
  85. /// Val is an i32 value, and Len is an 'intptr_t' value.
  86. Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
  87. const DataLayout &DL, const TargetLibraryInfo *TLI);
  88. /// Emit a call to the memcmp function.
  89. Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
  90. const DataLayout &DL, const TargetLibraryInfo *TLI);
  91. /// Emit a call to the bcmp function.
  92. Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
  93. const DataLayout &DL, const TargetLibraryInfo *TLI);
  94. /// Emit a call to the memccpy function.
  95. Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
  96. IRBuilderBase &B, const TargetLibraryInfo *TLI);
  97. /// Emit a call to the snprintf function.
  98. Value *emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
  99. ArrayRef<Value *> Args, IRBuilderBase &B,
  100. const TargetLibraryInfo *TLI);
  101. /// Emit a call to the sprintf function.
  102. Value *emitSPrintf(Value *Dest, Value *Fmt, ArrayRef<Value *> VariadicArgs,
  103. IRBuilderBase &B, const TargetLibraryInfo *TLI);
  104. /// Emit a call to the strcat function.
  105. Value *emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B,
  106. const TargetLibraryInfo *TLI);
  107. /// Emit a call to the strlcpy function.
  108. Value *emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
  109. const TargetLibraryInfo *TLI);
  110. /// Emit a call to the strlcat function.
  111. Value *emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
  112. const TargetLibraryInfo *TLI);
  113. /// Emit a call to the strncat function.
  114. Value *emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
  115. const TargetLibraryInfo *TLI);
  116. /// Emit a call to the vsnprintf function.
  117. Value *emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
  118. IRBuilderBase &B, const TargetLibraryInfo *TLI);
  119. /// Emit a call to the vsprintf function.
  120. Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B,
  121. const TargetLibraryInfo *TLI);
  122. /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
  123. /// function is known to take a single of type matching 'Op' and returns one
  124. /// value with the same type. If 'Op' is a long double, 'l' is added as the
  125. /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
  126. Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilderBase &B,
  127. const AttributeList &Attrs);
  128. /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
  129. /// depending of the type of Op.
  130. Value *emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
  131. LibFunc DoubleFn, LibFunc FloatFn,
  132. LibFunc LongDoubleFn, IRBuilderBase &B,
  133. const AttributeList &Attrs);
  134. /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
  135. /// function is known to take type matching 'Op1' and 'Op2' and return one
  136. /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
  137. /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
  138. Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
  139. IRBuilderBase &B, const AttributeList &Attrs);
  140. /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn,
  141. /// depending of the type of Op1.
  142. Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2,
  143. const TargetLibraryInfo *TLI, LibFunc DoubleFn,
  144. LibFunc FloatFn, LibFunc LongDoubleFn,
  145. IRBuilderBase &B, const AttributeList &Attrs);
  146. /// Emit a call to the putchar function. This assumes that Char is an integer.
  147. Value *emitPutChar(Value *Char, IRBuilderBase &B,
  148. const TargetLibraryInfo *TLI);
  149. /// Emit a call to the puts function. This assumes that Str is some pointer.
  150. Value *emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI);
  151. /// Emit a call to the fputc function. This assumes that Char is an i32, and
  152. /// File is a pointer to FILE.
  153. Value *emitFPutC(Value *Char, Value *File, IRBuilderBase &B,
  154. const TargetLibraryInfo *TLI);
  155. /// Emit a call to the fputs function. Str is required to be a pointer and
  156. /// File is a pointer to FILE.
  157. Value *emitFPutS(Value *Str, Value *File, IRBuilderBase &B,
  158. const TargetLibraryInfo *TLI);
  159. /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
  160. /// Size is an 'intptr_t', and File is a pointer to FILE.
  161. Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B,
  162. const DataLayout &DL, const TargetLibraryInfo *TLI);
  163. /// Emit a call to the malloc function.
  164. Value *emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL,
  165. const TargetLibraryInfo *TLI);
  166. /// Emit a call to the calloc function.
  167. Value *emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
  168. const TargetLibraryInfo &TLI);
  169. }
  170. #endif
  171. #ifdef __GNUC__
  172. #pragma GCC diagnostic pop
  173. #endif