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