SimplifyLibCalls.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //===- SimplifyLibCalls.h - Library call simplifier -------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file exposes an interface to build some C language libcalls for
  11. // optimization passes that need to call the various functions.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H
  15. #define LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/Analysis/TargetLibraryInfo.h"
  19. #include "llvm/IR/IRBuilder.h"
  20. namespace llvm {
  21. class Value;
  22. class CallInst;
  23. class DataLayout;
  24. class Instruction;
  25. class TargetLibraryInfo;
  26. class BasicBlock;
  27. class Function;
  28. /// \brief This class implements simplifications for calls to fortified library
  29. /// functions (__st*cpy_chk, __memcpy_chk, __memmove_chk, __memset_chk), to,
  30. /// when possible, replace them with their non-checking counterparts.
  31. /// Other optimizations can also be done, but it's possible to disable them and
  32. /// only simplify needless use of the checking versions (when the object size
  33. /// is unknown) by passing true for OnlyLowerUnknownSize.
  34. class FortifiedLibCallSimplifier {
  35. private:
  36. const TargetLibraryInfo *TLI;
  37. bool OnlyLowerUnknownSize;
  38. public:
  39. FortifiedLibCallSimplifier(const TargetLibraryInfo *TLI,
  40. bool OnlyLowerUnknownSize = false);
  41. /// \brief Take the given call instruction and return a more
  42. /// optimal value to replace the instruction with or 0 if a more
  43. /// optimal form can't be found.
  44. /// The call must not be an indirect call.
  45. Value *optimizeCall(CallInst *CI);
  46. private:
  47. Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B);
  48. Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B);
  49. Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B);
  50. // Str/Stp cpy are similar enough to be handled in the same functions.
  51. Value *optimizeStrpCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc::Func Func);
  52. Value *optimizeStrpNCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc::Func Func);
  53. /// \brief Checks whether the call \p CI to a fortified libcall is foldable
  54. /// to the non-fortified version.
  55. bool isFortifiedCallFoldable(CallInst *CI, unsigned ObjSizeOp,
  56. unsigned SizeOp, bool isString);
  57. };
  58. /// LibCallSimplifier - This class implements a collection of optimizations
  59. /// that replace well formed calls to library functions with a more optimal
  60. /// form. For example, replacing 'printf("Hello!")' with 'puts("Hello!")'.
  61. class LibCallSimplifier {
  62. private:
  63. FortifiedLibCallSimplifier FortifiedSimplifier;
  64. const DataLayout &DL;
  65. const TargetLibraryInfo *TLI;
  66. bool UnsafeFPShrink;
  67. function_ref<void(Instruction *, Value *)> Replacer;
  68. /// \brief Internal wrapper for RAUW that is the default implementation.
  69. ///
  70. /// Other users may provide an alternate function with this signature instead
  71. /// of this one.
  72. static void replaceAllUsesWithDefault(Instruction *I, Value *With);
  73. /// \brief Replace an instruction's uses with a value using our replacer.
  74. void replaceAllUsesWith(Instruction *I, Value *With);
  75. public:
  76. LibCallSimplifier(const DataLayout &DL, const TargetLibraryInfo *TLI,
  77. function_ref<void(Instruction *, Value *)> Replacer =
  78. &replaceAllUsesWithDefault);
  79. /// optimizeCall - Take the given call instruction and return a more
  80. /// optimal value to replace the instruction with or 0 if a more
  81. /// optimal form can't be found. Note that the returned value may
  82. /// be equal to the instruction being optimized. In this case all
  83. /// other instructions that use the given instruction were modified
  84. /// and the given instruction is dead.
  85. /// The call must not be an indirect call.
  86. Value *optimizeCall(CallInst *CI);
  87. private:
  88. // String and Memory Library Call Optimizations
  89. Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B);
  90. Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B);
  91. Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B);
  92. Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B);
  93. Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B);
  94. Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B);
  95. Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B);
  96. Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B);
  97. Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B);
  98. Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B);
  99. Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B);
  100. Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B);
  101. Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B);
  102. Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B);
  103. Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B);
  104. Value *optimizeMemChr(CallInst *CI, IRBuilder<> &B);
  105. Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B);
  106. Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B);
  107. Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B);
  108. Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B);
  109. // Wrapper for all String/Memory Library Call Optimizations
  110. Value *optimizeStringMemoryLibCall(CallInst *CI, IRBuilder<> &B);
  111. // Math Library Optimizations
  112. Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B, bool CheckRetType);
  113. Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B);
  114. Value *optimizeCos(CallInst *CI, IRBuilder<> &B);
  115. Value *optimizePow(CallInst *CI, IRBuilder<> &B);
  116. Value *optimizeExp2(CallInst *CI, IRBuilder<> &B);
  117. Value *optimizeFabs(CallInst *CI, IRBuilder<> &B);
  118. Value *optimizeSqrt(CallInst *CI, IRBuilder<> &B);
  119. Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B);
  120. // Integer Library Call Optimizations
  121. Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);
  122. Value *optimizeAbs(CallInst *CI, IRBuilder<> &B);
  123. Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B);
  124. Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B);
  125. Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B);
  126. // Formatting and IO Library Call Optimizations
  127. Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
  128. int StreamArg = -1);
  129. Value *optimizePrintF(CallInst *CI, IRBuilder<> &B);
  130. Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B);
  131. Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B);
  132. Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B);
  133. Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B);
  134. Value *optimizePuts(CallInst *CI, IRBuilder<> &B);
  135. // Helper methods
  136. Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B);
  137. void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
  138. SmallVectorImpl<CallInst *> &SinCalls,
  139. SmallVectorImpl<CallInst *> &CosCalls,
  140. SmallVectorImpl<CallInst *> &SinCosCalls);
  141. void replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls, Value *Res);
  142. Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B);
  143. Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B);
  144. Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B);
  145. /// hasFloatVersion - Checks if there is a float version of the specified
  146. /// function by checking for an existing function with name FuncName + f
  147. bool hasFloatVersion(StringRef FuncName);
  148. };
  149. } // End llvm namespace
  150. #endif