SimplifyLibCalls.h revision 360784
10SN/A//===- SimplifyLibCalls.h - Library call simplifier -------------*- C++ -*-===// 2157SN/A// 30SN/A// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40SN/A// See https://llvm.org/LICENSE.txt for license information. 50SN/A// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60SN/A// 7157SN/A//===----------------------------------------------------------------------===// 80SN/A// 9157SN/A// This file exposes an interface to build some C language libcalls for 100SN/A// optimization passes that need to call the various functions. 110SN/A// 120SN/A//===----------------------------------------------------------------------===// 130SN/A 140SN/A#ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H 150SN/A#define LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H 160SN/A 170SN/A#include "llvm/ADT/STLExtras.h" 180SN/A#include "llvm/Analysis/TargetLibraryInfo.h" 190SN/A#include "llvm/IR/IRBuilder.h" 200SN/A 21157SN/Anamespace llvm { 22157SN/Aclass StringRef; 23157SN/Aclass Value; 240SN/Aclass CallInst; 250SN/Aclass DataLayout; 260SN/Aclass Instruction; 270SN/Aclass TargetLibraryInfo; 280SN/Aclass BasicBlock; 290SN/Aclass Function; 300SN/Aclass OptimizationRemarkEmitter; 310SN/Aclass BlockFrequencyInfo; 320SN/Aclass ProfileSummaryInfo; 330SN/A 340SN/A/// This class implements simplifications for calls to fortified library 350SN/A/// functions (__st*cpy_chk, __memcpy_chk, __memmove_chk, __memset_chk), to, 360SN/A/// when possible, replace them with their non-checking counterparts. 370SN/A/// Other optimizations can also be done, but it's possible to disable them and 380SN/A/// only simplify needless use of the checking versions (when the object size 390SN/A/// is unknown) by passing true for OnlyLowerUnknownSize. 400SN/Aclass FortifiedLibCallSimplifier { 410SN/Aprivate: 420SN/A const TargetLibraryInfo *TLI; 430SN/A bool OnlyLowerUnknownSize; 440SN/A 450SN/Apublic: 460SN/A FortifiedLibCallSimplifier(const TargetLibraryInfo *TLI, 470SN/A bool OnlyLowerUnknownSize = false); 480SN/A 490SN/A /// Take the given call instruction and return a more 500SN/A /// optimal value to replace the instruction with or 0 if a more 510SN/A /// optimal form can't be found. 520SN/A /// The call must not be an indirect call. 530SN/A Value *optimizeCall(CallInst *CI); 540SN/A 550SN/Aprivate: 560SN/A Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B); 570SN/A Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B); 580SN/A Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B); 590SN/A 600SN/A /// Str/Stp cpy are similar enough to be handled in the same functions. 610SN/A Value *optimizeStrpCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc Func); 620SN/A Value *optimizeStrpNCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc Func); 630SN/A Value *optimizeMemCCpyChk(CallInst *CI, IRBuilder<> &B); 640SN/A Value *optimizeSNPrintfChk(CallInst *CI, IRBuilder<> &B); 650SN/A Value *optimizeSPrintfChk(CallInst *CI,IRBuilder<> &B); 660SN/A Value *optimizeStrCatChk(CallInst *CI, IRBuilder<> &B); 670SN/A Value *optimizeStrLCat(CallInst *CI, IRBuilder<> &B); 680SN/A Value *optimizeStrNCatChk(CallInst *CI, IRBuilder<> &B); 690SN/A Value *optimizeStrLCpyChk(CallInst *CI, IRBuilder<> &B); 700SN/A Value *optimizeVSNPrintfChk(CallInst *CI, IRBuilder<> &B); 710SN/A Value *optimizeVSPrintfChk(CallInst *CI, IRBuilder<> &B); 720SN/A 730SN/A /// Checks whether the call \p CI to a fortified libcall is foldable 740SN/A /// to the non-fortified version. 750SN/A /// 760SN/A /// \param CI the call to the fortified libcall. 770SN/A /// 780SN/A /// \param ObjSizeOp the index of the object size parameter of this chk 790SN/A /// function. Not optional since this is mandatory. 800SN/A /// 810SN/A /// \param SizeOp optionally set to the parameter index of an explicit buffer 820SN/A /// size argument. For instance, set to '2' for __strncpy_chk. 83 /// 84 /// \param StrOp optionally set to the parameter index of the source string 85 /// parameter to strcpy-like functions, where only the strlen of the source 86 /// will be writtin into the destination. 87 /// 88 /// \param FlagsOp optionally set to the parameter index of a 'flags' 89 /// parameter. These are used by an implementation to opt-into stricter 90 /// checking. 91 bool isFortifiedCallFoldable(CallInst *CI, unsigned ObjSizeOp, 92 Optional<unsigned> SizeOp = None, 93 Optional<unsigned> StrOp = None, 94 Optional<unsigned> FlagsOp = None); 95}; 96 97/// LibCallSimplifier - This class implements a collection of optimizations 98/// that replace well formed calls to library functions with a more optimal 99/// form. For example, replacing 'printf("Hello!")' with 'puts("Hello!")'. 100class LibCallSimplifier { 101private: 102 FortifiedLibCallSimplifier FortifiedSimplifier; 103 const DataLayout &DL; 104 const TargetLibraryInfo *TLI; 105 OptimizationRemarkEmitter &ORE; 106 BlockFrequencyInfo *BFI; 107 ProfileSummaryInfo *PSI; 108 bool UnsafeFPShrink; 109 function_ref<void(Instruction *, Value *)> Replacer; 110 function_ref<void(Instruction *)> Eraser; 111 112 /// Internal wrapper for RAUW that is the default implementation. 113 /// 114 /// Other users may provide an alternate function with this signature instead 115 /// of this one. 116 static void replaceAllUsesWithDefault(Instruction *I, Value *With) { 117 I->replaceAllUsesWith(With); 118 } 119 120 /// Internal wrapper for eraseFromParent that is the default implementation. 121 static void eraseFromParentDefault(Instruction *I) { I->eraseFromParent(); } 122 123 /// Replace an instruction's uses with a value using our replacer. 124 void replaceAllUsesWith(Instruction *I, Value *With); 125 126 /// Erase an instruction from its parent with our eraser. 127 void eraseFromParent(Instruction *I); 128 129 /// Replace an instruction with a value and erase it from its parent. 130 void substituteInParent(Instruction *I, Value *With) { 131 replaceAllUsesWith(I, With); 132 eraseFromParent(I); 133 } 134 135 Value *foldMallocMemset(CallInst *Memset, IRBuilder<> &B); 136 137public: 138 LibCallSimplifier( 139 const DataLayout &DL, const TargetLibraryInfo *TLI, 140 OptimizationRemarkEmitter &ORE, 141 BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, 142 function_ref<void(Instruction *, Value *)> Replacer = 143 &replaceAllUsesWithDefault, 144 function_ref<void(Instruction *)> Eraser = &eraseFromParentDefault); 145 146 /// optimizeCall - Take the given call instruction and return a more 147 /// optimal value to replace the instruction with or 0 if a more 148 /// optimal form can't be found. Note that the returned value may 149 /// be equal to the instruction being optimized. In this case all 150 /// other instructions that use the given instruction were modified 151 /// and the given instruction is dead. 152 /// The call must not be an indirect call. 153 Value *optimizeCall(CallInst *CI); 154 155private: 156 // String and Memory Library Call Optimizations 157 Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B); 158 Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B); 159 Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B); 160 Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B); 161 Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B); 162 Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B); 163 Value *optimizeStrNDup(CallInst *CI, IRBuilder<> &B); 164 Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B); 165 Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B); 166 Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B); 167 Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B); 168 Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B); 169 Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B); 170 Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B); 171 Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B); 172 Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B); 173 Value *optimizeMemChr(CallInst *CI, IRBuilder<> &B); 174 Value *optimizeMemRChr(CallInst *CI, IRBuilder<> &B); 175 Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B); 176 Value *optimizeBCmp(CallInst *CI, IRBuilder<> &B); 177 Value *optimizeMemCmpBCmpCommon(CallInst *CI, IRBuilder<> &B); 178 Value *optimizeMemCCpy(CallInst *CI, IRBuilder<> &B); 179 Value *optimizeMemPCpy(CallInst *CI, IRBuilder<> &B); 180 Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B); 181 Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B); 182 Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B); 183 Value *optimizeRealloc(CallInst *CI, IRBuilder<> &B); 184 Value *optimizeWcslen(CallInst *CI, IRBuilder<> &B); 185 Value *optimizeBCopy(CallInst *CI, IRBuilder<> &B); 186 // Wrapper for all String/Memory Library Call Optimizations 187 Value *optimizeStringMemoryLibCall(CallInst *CI, IRBuilder<> &B); 188 189 // Math Library Optimizations 190 Value *optimizeCAbs(CallInst *CI, IRBuilder<> &B); 191 Value *optimizePow(CallInst *CI, IRBuilder<> &B); 192 Value *replacePowWithExp(CallInst *Pow, IRBuilder<> &B); 193 Value *replacePowWithSqrt(CallInst *Pow, IRBuilder<> &B); 194 Value *optimizeExp2(CallInst *CI, IRBuilder<> &B); 195 Value *optimizeFMinFMax(CallInst *CI, IRBuilder<> &B); 196 Value *optimizeLog(CallInst *CI, IRBuilder<> &B); 197 Value *optimizeSqrt(CallInst *CI, IRBuilder<> &B); 198 Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B); 199 Value *optimizeTan(CallInst *CI, IRBuilder<> &B); 200 // Wrapper for all floating point library call optimizations 201 Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func, 202 IRBuilder<> &B); 203 204 // Integer Library Call Optimizations 205 Value *optimizeFFS(CallInst *CI, IRBuilder<> &B); 206 Value *optimizeFls(CallInst *CI, IRBuilder<> &B); 207 Value *optimizeAbs(CallInst *CI, IRBuilder<> &B); 208 Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B); 209 Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B); 210 Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B); 211 Value *optimizeAtoi(CallInst *CI, IRBuilder<> &B); 212 Value *optimizeStrtol(CallInst *CI, IRBuilder<> &B); 213 214 // Formatting and IO Library Call Optimizations 215 Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B, 216 int StreamArg = -1); 217 Value *optimizePrintF(CallInst *CI, IRBuilder<> &B); 218 Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B); 219 Value *optimizeSnPrintF(CallInst *CI, IRBuilder<> &B); 220 Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B); 221 Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B); 222 Value *optimizeFRead(CallInst *CI, IRBuilder<> &B); 223 Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B); 224 Value *optimizeFGets(CallInst *CI, IRBuilder<> &B); 225 Value *optimizeFPutc(CallInst *CI, IRBuilder<> &B); 226 Value *optimizeFGetc(CallInst *CI, IRBuilder<> &B); 227 Value *optimizePuts(CallInst *CI, IRBuilder<> &B); 228 229 // Helper methods 230 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B); 231 void classifyArgUse(Value *Val, Function *F, bool IsFloat, 232 SmallVectorImpl<CallInst *> &SinCalls, 233 SmallVectorImpl<CallInst *> &CosCalls, 234 SmallVectorImpl<CallInst *> &SinCosCalls); 235 Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B); 236 Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B); 237 Value *optimizeSnPrintFString(CallInst *CI, IRBuilder<> &B); 238 Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B); 239 240 /// hasFloatVersion - Checks if there is a float version of the specified 241 /// function by checking for an existing function with name FuncName + f 242 bool hasFloatVersion(StringRef FuncName); 243 244 /// Shared code to optimize strlen+wcslen. 245 Value *optimizeStringLength(CallInst *CI, IRBuilder<> &B, unsigned CharSize); 246}; 247} // End llvm namespace 248 249#endif 250