SimplifyLibCalls.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SimplifyLibCalls.h - Library call simplifier -------------*- 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_SIMPLIFYLIBCALLS_H
  19. #define LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H
  20. #include "llvm/ADT/STLFunctionalExtras.h"
  21. #include "llvm/Analysis/TargetLibraryInfo.h"
  22. namespace llvm {
  23. class StringRef;
  24. class Value;
  25. class CallInst;
  26. class DataLayout;
  27. class Instruction;
  28. class IRBuilderBase;
  29. class Function;
  30. class OptimizationRemarkEmitter;
  31. class BlockFrequencyInfo;
  32. class ProfileSummaryInfo;
  33. /// This class implements simplifications for calls to fortified library
  34. /// functions (__st*cpy_chk, __memcpy_chk, __memmove_chk, __memset_chk), to,
  35. /// when possible, replace them with their non-checking counterparts.
  36. /// Other optimizations can also be done, but it's possible to disable them and
  37. /// only simplify needless use of the checking versions (when the object size
  38. /// is unknown) by passing true for OnlyLowerUnknownSize.
  39. class FortifiedLibCallSimplifier {
  40. private:
  41. const TargetLibraryInfo *TLI;
  42. bool OnlyLowerUnknownSize;
  43. public:
  44. FortifiedLibCallSimplifier(const TargetLibraryInfo *TLI,
  45. bool OnlyLowerUnknownSize = false);
  46. /// Take the given call instruction and return a more
  47. /// optimal value to replace the instruction with or 0 if a more
  48. /// optimal form can't be found.
  49. /// The call must not be an indirect call.
  50. Value *optimizeCall(CallInst *CI, IRBuilderBase &B);
  51. private:
  52. Value *optimizeMemCpyChk(CallInst *CI, IRBuilderBase &B);
  53. Value *optimizeMemMoveChk(CallInst *CI, IRBuilderBase &B);
  54. Value *optimizeMemSetChk(CallInst *CI, IRBuilderBase &B);
  55. /// Str/Stp cpy are similar enough to be handled in the same functions.
  56. Value *optimizeStrpCpyChk(CallInst *CI, IRBuilderBase &B, LibFunc Func);
  57. Value *optimizeStrpNCpyChk(CallInst *CI, IRBuilderBase &B, LibFunc Func);
  58. Value *optimizeStrLenChk(CallInst *CI, IRBuilderBase &B);
  59. Value *optimizeMemPCpyChk(CallInst *CI, IRBuilderBase &B);
  60. Value *optimizeMemCCpyChk(CallInst *CI, IRBuilderBase &B);
  61. Value *optimizeSNPrintfChk(CallInst *CI, IRBuilderBase &B);
  62. Value *optimizeSPrintfChk(CallInst *CI,IRBuilderBase &B);
  63. Value *optimizeStrCatChk(CallInst *CI, IRBuilderBase &B);
  64. Value *optimizeStrLCat(CallInst *CI, IRBuilderBase &B);
  65. Value *optimizeStrNCatChk(CallInst *CI, IRBuilderBase &B);
  66. Value *optimizeStrLCpyChk(CallInst *CI, IRBuilderBase &B);
  67. Value *optimizeVSNPrintfChk(CallInst *CI, IRBuilderBase &B);
  68. Value *optimizeVSPrintfChk(CallInst *CI, IRBuilderBase &B);
  69. /// Checks whether the call \p CI to a fortified libcall is foldable
  70. /// to the non-fortified version.
  71. ///
  72. /// \param CI the call to the fortified libcall.
  73. ///
  74. /// \param ObjSizeOp the index of the object size parameter of this chk
  75. /// function. Not optional since this is mandatory.
  76. ///
  77. /// \param SizeOp optionally set to the parameter index of an explicit buffer
  78. /// size argument. For instance, set to '2' for __strncpy_chk.
  79. ///
  80. /// \param StrOp optionally set to the parameter index of the source string
  81. /// parameter to strcpy-like functions, where only the strlen of the source
  82. /// will be writtin into the destination.
  83. ///
  84. /// \param FlagsOp optionally set to the parameter index of a 'flags'
  85. /// parameter. These are used by an implementation to opt-into stricter
  86. /// checking.
  87. bool isFortifiedCallFoldable(CallInst *CI, unsigned ObjSizeOp,
  88. std::optional<unsigned> SizeOp = std::nullopt,
  89. std::optional<unsigned> StrOp = std::nullopt,
  90. std::optional<unsigned> FlagsOp = std::nullopt);
  91. };
  92. /// LibCallSimplifier - This class implements a collection of optimizations
  93. /// that replace well formed calls to library functions with a more optimal
  94. /// form. For example, replacing 'printf("Hello!")' with 'puts("Hello!")'.
  95. class LibCallSimplifier {
  96. private:
  97. FortifiedLibCallSimplifier FortifiedSimplifier;
  98. const DataLayout &DL;
  99. const TargetLibraryInfo *TLI;
  100. OptimizationRemarkEmitter &ORE;
  101. BlockFrequencyInfo *BFI;
  102. ProfileSummaryInfo *PSI;
  103. bool UnsafeFPShrink = false;
  104. function_ref<void(Instruction *, Value *)> Replacer;
  105. function_ref<void(Instruction *)> Eraser;
  106. /// Internal wrapper for RAUW that is the default implementation.
  107. ///
  108. /// Other users may provide an alternate function with this signature instead
  109. /// of this one.
  110. static void replaceAllUsesWithDefault(Instruction *I, Value *With) {
  111. I->replaceAllUsesWith(With);
  112. }
  113. /// Internal wrapper for eraseFromParent that is the default implementation.
  114. static void eraseFromParentDefault(Instruction *I) { I->eraseFromParent(); }
  115. /// Replace an instruction's uses with a value using our replacer.
  116. void replaceAllUsesWith(Instruction *I, Value *With);
  117. /// Erase an instruction from its parent with our eraser.
  118. void eraseFromParent(Instruction *I);
  119. /// Replace an instruction with a value and erase it from its parent.
  120. void substituteInParent(Instruction *I, Value *With) {
  121. replaceAllUsesWith(I, With);
  122. eraseFromParent(I);
  123. }
  124. public:
  125. LibCallSimplifier(
  126. const DataLayout &DL, const TargetLibraryInfo *TLI,
  127. OptimizationRemarkEmitter &ORE,
  128. BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
  129. function_ref<void(Instruction *, Value *)> Replacer =
  130. &replaceAllUsesWithDefault,
  131. function_ref<void(Instruction *)> Eraser = &eraseFromParentDefault);
  132. /// optimizeCall - Take the given call instruction and return a more
  133. /// optimal value to replace the instruction with or 0 if a more
  134. /// optimal form can't be found. Note that the returned value may
  135. /// be equal to the instruction being optimized. In this case all
  136. /// other instructions that use the given instruction were modified
  137. /// and the given instruction is dead.
  138. /// The call must not be an indirect call.
  139. Value *optimizeCall(CallInst *CI, IRBuilderBase &B);
  140. private:
  141. // String and Memory Library Call Optimizations
  142. Value *optimizeStrCat(CallInst *CI, IRBuilderBase &B);
  143. Value *optimizeStrNCat(CallInst *CI, IRBuilderBase &B);
  144. Value *optimizeStrChr(CallInst *CI, IRBuilderBase &B);
  145. Value *optimizeStrRChr(CallInst *CI, IRBuilderBase &B);
  146. Value *optimizeStrCmp(CallInst *CI, IRBuilderBase &B);
  147. Value *optimizeStrNCmp(CallInst *CI, IRBuilderBase &B);
  148. Value *optimizeStrNDup(CallInst *CI, IRBuilderBase &B);
  149. Value *optimizeStrCpy(CallInst *CI, IRBuilderBase &B);
  150. Value *optimizeStpCpy(CallInst *CI, IRBuilderBase &B);
  151. Value *optimizeStrLCpy(CallInst *CI, IRBuilderBase &B);
  152. Value *optimizeStrNCpy(CallInst *CI, IRBuilderBase &B);
  153. Value *optimizeStrLen(CallInst *CI, IRBuilderBase &B);
  154. Value *optimizeStrNLen(CallInst *CI, IRBuilderBase &B);
  155. Value *optimizeStrPBrk(CallInst *CI, IRBuilderBase &B);
  156. Value *optimizeStrTo(CallInst *CI, IRBuilderBase &B);
  157. Value *optimizeStrSpn(CallInst *CI, IRBuilderBase &B);
  158. Value *optimizeStrCSpn(CallInst *CI, IRBuilderBase &B);
  159. Value *optimizeStrStr(CallInst *CI, IRBuilderBase &B);
  160. Value *optimizeMemChr(CallInst *CI, IRBuilderBase &B);
  161. Value *optimizeMemRChr(CallInst *CI, IRBuilderBase &B);
  162. Value *optimizeMemCmp(CallInst *CI, IRBuilderBase &B);
  163. Value *optimizeBCmp(CallInst *CI, IRBuilderBase &B);
  164. Value *optimizeMemCmpBCmpCommon(CallInst *CI, IRBuilderBase &B);
  165. Value *optimizeMemCCpy(CallInst *CI, IRBuilderBase &B);
  166. Value *optimizeMemPCpy(CallInst *CI, IRBuilderBase &B);
  167. Value *optimizeMemCpy(CallInst *CI, IRBuilderBase &B);
  168. Value *optimizeMemMove(CallInst *CI, IRBuilderBase &B);
  169. Value *optimizeMemSet(CallInst *CI, IRBuilderBase &B);
  170. Value *optimizeRealloc(CallInst *CI, IRBuilderBase &B);
  171. Value *optimizeWcslen(CallInst *CI, IRBuilderBase &B);
  172. Value *optimizeBCopy(CallInst *CI, IRBuilderBase &B);
  173. // Helper to optimize stpncpy and strncpy.
  174. Value *optimizeStringNCpy(CallInst *CI, bool RetEnd, IRBuilderBase &B);
  175. // Wrapper for all String/Memory Library Call Optimizations
  176. Value *optimizeStringMemoryLibCall(CallInst *CI, IRBuilderBase &B);
  177. // Math Library Optimizations
  178. Value *optimizeCAbs(CallInst *CI, IRBuilderBase &B);
  179. Value *optimizePow(CallInst *CI, IRBuilderBase &B);
  180. Value *replacePowWithExp(CallInst *Pow, IRBuilderBase &B);
  181. Value *replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B);
  182. Value *optimizeExp2(CallInst *CI, IRBuilderBase &B);
  183. Value *optimizeFMinFMax(CallInst *CI, IRBuilderBase &B);
  184. Value *optimizeLog(CallInst *CI, IRBuilderBase &B);
  185. Value *optimizeSqrt(CallInst *CI, IRBuilderBase &B);
  186. Value *optimizeSinCosPi(CallInst *CI, IRBuilderBase &B);
  187. Value *optimizeTan(CallInst *CI, IRBuilderBase &B);
  188. // Wrapper for all floating point library call optimizations
  189. Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
  190. IRBuilderBase &B);
  191. // Integer Library Call Optimizations
  192. Value *optimizeFFS(CallInst *CI, IRBuilderBase &B);
  193. Value *optimizeFls(CallInst *CI, IRBuilderBase &B);
  194. Value *optimizeAbs(CallInst *CI, IRBuilderBase &B);
  195. Value *optimizeIsDigit(CallInst *CI, IRBuilderBase &B);
  196. Value *optimizeIsAscii(CallInst *CI, IRBuilderBase &B);
  197. Value *optimizeToAscii(CallInst *CI, IRBuilderBase &B);
  198. Value *optimizeAtoi(CallInst *CI, IRBuilderBase &B);
  199. Value *optimizeStrToInt(CallInst *CI, IRBuilderBase &B, bool AsSigned);
  200. // Formatting and IO Library Call Optimizations
  201. Value *optimizeErrorReporting(CallInst *CI, IRBuilderBase &B,
  202. int StreamArg = -1);
  203. Value *optimizePrintF(CallInst *CI, IRBuilderBase &B);
  204. Value *optimizeSPrintF(CallInst *CI, IRBuilderBase &B);
  205. Value *optimizeSnPrintF(CallInst *CI, IRBuilderBase &B);
  206. Value *optimizeFPrintF(CallInst *CI, IRBuilderBase &B);
  207. Value *optimizeFWrite(CallInst *CI, IRBuilderBase &B);
  208. Value *optimizeFPuts(CallInst *CI, IRBuilderBase &B);
  209. Value *optimizePuts(CallInst *CI, IRBuilderBase &B);
  210. // Helper methods
  211. Value* emitSnPrintfMemCpy(CallInst *CI, Value *StrArg, StringRef Str,
  212. uint64_t N, IRBuilderBase &B);
  213. Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
  214. IRBuilderBase &B);
  215. void classifyArgUse(Value *Val, Function *F, bool IsFloat,
  216. SmallVectorImpl<CallInst *> &SinCalls,
  217. SmallVectorImpl<CallInst *> &CosCalls,
  218. SmallVectorImpl<CallInst *> &SinCosCalls);
  219. Value *optimizePrintFString(CallInst *CI, IRBuilderBase &B);
  220. Value *optimizeSPrintFString(CallInst *CI, IRBuilderBase &B);
  221. Value *optimizeSnPrintFString(CallInst *CI, IRBuilderBase &B);
  222. Value *optimizeFPrintFString(CallInst *CI, IRBuilderBase &B);
  223. /// hasFloatVersion - Checks if there is a float version of the specified
  224. /// function by checking for an existing function with name FuncName + f
  225. bool hasFloatVersion(const Module *M, StringRef FuncName);
  226. /// Shared code to optimize strlen+wcslen and strnlen+wcsnlen.
  227. Value *optimizeStringLength(CallInst *CI, IRBuilderBase &B, unsigned CharSize,
  228. Value *Bound = nullptr);
  229. };
  230. } // End llvm namespace
  231. #endif
  232. #ifdef __GNUC__
  233. #pragma GCC diagnostic pop
  234. #endif