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/STLExtras.h" 19 #include "llvm/Analysis/TargetLibraryInfo.h" 20 #include "llvm/IR/IRBuilder.h" 21 22 namespace llvm { 23 class StringRef; 24 class Value; 25 class CallInst; 26 class DataLayout; 27 class Instruction; 28 class TargetLibraryInfo; 29 class BasicBlock; 30 class Function; 31 class OptimizationRemarkEmitter; 32 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 44 public: 45 FortifiedLibCallSimplifier(const TargetLibraryInfo *TLI, 46 bool OnlyLowerUnknownSize = false); 47 48 /// Take the given call instruction and return a more 49 /// optimal value to replace the instruction with or 0 if a more 50 /// optimal form can't be found. 51 /// The call must not be an indirect call. 52 Value *optimizeCall(CallInst *CI); 53 54 private: 55 Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B); 56 Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B); 57 Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B); 58 59 // Str/Stp cpy are similar enough to be handled in the same functions. 60 Value *optimizeStrpCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc Func); 61 Value *optimizeStrpNCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc Func); 62 63 /// Checks whether the call \p CI to a fortified libcall is foldable 64 /// to the non-fortified version. 65 bool isFortifiedCallFoldable(CallInst *CI, unsigned ObjSizeOp, 66 unsigned SizeOp, bool isString); 67 }; 68 69 /// LibCallSimplifier - This class implements a collection of optimizations 70 /// that replace well formed calls to library functions with a more optimal 71 /// form. For example, replacing 'printf("Hello!")' with 'puts("Hello!")'. 72 class LibCallSimplifier { 73 private: 74 FortifiedLibCallSimplifier FortifiedSimplifier; 75 const DataLayout &DL; 76 const TargetLibraryInfo *TLI; 77 OptimizationRemarkEmitter &ORE; 78 bool UnsafeFPShrink; 79 function_ref<void(Instruction *, Value *)> Replacer; 80 function_ref<void(Instruction *)> Eraser; 81 82 /// Internal wrapper for RAUW that is the default implementation. 83 /// 84 /// Other users may provide an alternate function with this signature instead 85 /// of this one. replaceAllUsesWithDefault(Instruction * I,Value * With)86 static void replaceAllUsesWithDefault(Instruction *I, Value *With) { 87 I->replaceAllUsesWith(With); 88 } 89 90 /// Internal wrapper for eraseFromParent that is the default implementation. eraseFromParentDefault(Instruction * I)91 static void eraseFromParentDefault(Instruction *I) { I->eraseFromParent(); } 92 93 /// Replace an instruction's uses with a value using our replacer. 94 void replaceAllUsesWith(Instruction *I, Value *With); 95 96 /// Erase an instruction from its parent with our eraser. 97 void eraseFromParent(Instruction *I); 98 99 Value *foldMallocMemset(CallInst *Memset, IRBuilder<> &B); 100 101 public: 102 LibCallSimplifier( 103 const DataLayout &DL, const TargetLibraryInfo *TLI, 104 OptimizationRemarkEmitter &ORE, 105 function_ref<void(Instruction *, Value *)> Replacer = 106 &replaceAllUsesWithDefault, 107 function_ref<void(Instruction *)> Eraser = &eraseFromParentDefault); 108 109 /// optimizeCall - Take the given call instruction and return a more 110 /// optimal value to replace the instruction with or 0 if a more 111 /// optimal form can't be found. Note that the returned value may 112 /// be equal to the instruction being optimized. In this case all 113 /// other instructions that use the given instruction were modified 114 /// and the given instruction is dead. 115 /// The call must not be an indirect call. 116 Value *optimizeCall(CallInst *CI); 117 118 private: 119 // String and Memory Library Call Optimizations 120 Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B); 121 Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B); 122 Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B); 123 Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B); 124 Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B); 125 Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B); 126 Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B); 127 Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B); 128 Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B); 129 Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B); 130 Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B); 131 Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B); 132 Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B); 133 Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B); 134 Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B); 135 Value *optimizeMemChr(CallInst *CI, IRBuilder<> &B); 136 Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B); 137 Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B); 138 Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B); 139 Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B); 140 Value *optimizeRealloc(CallInst *CI, IRBuilder<> &B); 141 Value *optimizeWcslen(CallInst *CI, IRBuilder<> &B); 142 // Wrapper for all String/Memory Library Call Optimizations 143 Value *optimizeStringMemoryLibCall(CallInst *CI, IRBuilder<> &B); 144 145 // Math Library Optimizations 146 Value *optimizeCAbs(CallInst *CI, IRBuilder<> &B); 147 Value *optimizePow(CallInst *CI, IRBuilder<> &B); 148 Value *replacePowWithExp(CallInst *Pow, IRBuilder<> &B); 149 Value *replacePowWithSqrt(CallInst *Pow, IRBuilder<> &B); 150 Value *optimizeExp2(CallInst *CI, IRBuilder<> &B); 151 Value *optimizeFMinFMax(CallInst *CI, IRBuilder<> &B); 152 Value *optimizeLog(CallInst *CI, IRBuilder<> &B); 153 Value *optimizeSqrt(CallInst *CI, IRBuilder<> &B); 154 Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B); 155 Value *optimizeTan(CallInst *CI, IRBuilder<> &B); 156 // Wrapper for all floating point library call optimizations 157 Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func, 158 IRBuilder<> &B); 159 160 // Integer Library Call Optimizations 161 Value *optimizeFFS(CallInst *CI, IRBuilder<> &B); 162 Value *optimizeFls(CallInst *CI, IRBuilder<> &B); 163 Value *optimizeAbs(CallInst *CI, IRBuilder<> &B); 164 Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B); 165 Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B); 166 Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B); 167 Value *optimizeAtoi(CallInst *CI, IRBuilder<> &B); 168 Value *optimizeStrtol(CallInst *CI, IRBuilder<> &B); 169 170 // Formatting and IO Library Call Optimizations 171 Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B, 172 int StreamArg = -1); 173 Value *optimizePrintF(CallInst *CI, IRBuilder<> &B); 174 Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B); 175 Value *optimizeSnPrintF(CallInst *CI, IRBuilder<> &B); 176 Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B); 177 Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B); 178 Value *optimizeFRead(CallInst *CI, IRBuilder<> &B); 179 Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B); 180 Value *optimizeFGets(CallInst *CI, IRBuilder<> &B); 181 Value *optimizeFPutc(CallInst *CI, IRBuilder<> &B); 182 Value *optimizeFGetc(CallInst *CI, IRBuilder<> &B); 183 Value *optimizePuts(CallInst *CI, IRBuilder<> &B); 184 185 // Helper methods 186 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B); 187 void classifyArgUse(Value *Val, Function *F, bool IsFloat, 188 SmallVectorImpl<CallInst *> &SinCalls, 189 SmallVectorImpl<CallInst *> &CosCalls, 190 SmallVectorImpl<CallInst *> &SinCosCalls); 191 Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B); 192 Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B); 193 Value *optimizeSnPrintFString(CallInst *CI, IRBuilder<> &B); 194 Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B); 195 196 /// hasFloatVersion - Checks if there is a float version of the specified 197 /// function by checking for an existing function with name FuncName + f 198 bool hasFloatVersion(StringRef FuncName); 199 200 /// Shared code to optimize strlen+wcslen. 201 Value *optimizeStringLength(CallInst *CI, IRBuilder<> &B, unsigned CharSize); 202 }; 203 } // End llvm namespace 204 205 #endif 206