1243789Sdim//===- SimplifyLibCalls.h - Library call simplifier -------------*- C++ -*-===// 2243789Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6243789Sdim// 7243789Sdim//===----------------------------------------------------------------------===// 8243789Sdim// 9243789Sdim// This file exposes an interface to build some C language libcalls for 10243789Sdim// optimization passes that need to call the various functions. 11243789Sdim// 12243789Sdim//===----------------------------------------------------------------------===// 13243789Sdim 14243789Sdim#ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H 15243789Sdim#define LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H 16243789Sdim 17288943Sdim#include "llvm/ADT/STLExtras.h" 18288943Sdim#include "llvm/Analysis/TargetLibraryInfo.h" 19280031Sdim#include "llvm/IR/IRBuilder.h" 20280031Sdim 21243789Sdimnamespace llvm { 22309124Sdimclass StringRef; 23280031Sdimclass Value; 24280031Sdimclass CallInst; 25280031Sdimclass DataLayout; 26280031Sdimclass Instruction; 27280031Sdimclass TargetLibraryInfo; 28280031Sdimclass BasicBlock; 29280031Sdimclass Function; 30327952Sdimclass OptimizationRemarkEmitter; 31353358Sdimclass BlockFrequencyInfo; 32353358Sdimclass ProfileSummaryInfo; 33243789Sdim 34341825Sdim/// This class implements simplifications for calls to fortified library 35280031Sdim/// functions (__st*cpy_chk, __memcpy_chk, __memmove_chk, __memset_chk), to, 36280031Sdim/// when possible, replace them with their non-checking counterparts. 37280031Sdim/// Other optimizations can also be done, but it's possible to disable them and 38280031Sdim/// only simplify needless use of the checking versions (when the object size 39280031Sdim/// is unknown) by passing true for OnlyLowerUnknownSize. 40280031Sdimclass FortifiedLibCallSimplifier { 41280031Sdimprivate: 42280031Sdim const TargetLibraryInfo *TLI; 43280031Sdim bool OnlyLowerUnknownSize; 44276479Sdim 45280031Sdimpublic: 46288943Sdim FortifiedLibCallSimplifier(const TargetLibraryInfo *TLI, 47280031Sdim bool OnlyLowerUnknownSize = false); 48243789Sdim 49341825Sdim /// Take the given call instruction and return a more 50280031Sdim /// optimal value to replace the instruction with or 0 if a more 51280031Sdim /// optimal form can't be found. 52280031Sdim /// The call must not be an indirect call. 53280031Sdim Value *optimizeCall(CallInst *CI); 54243789Sdim 55280031Sdimprivate: 56280031Sdim Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B); 57280031Sdim Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B); 58280031Sdim Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B); 59280031Sdim 60353358Sdim /// Str/Stp cpy are similar enough to be handled in the same functions. 61321369Sdim Value *optimizeStrpCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc Func); 62321369Sdim Value *optimizeStrpNCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc Func); 63353358Sdim Value *optimizeMemCCpyChk(CallInst *CI, IRBuilder<> &B); 64353358Sdim Value *optimizeSNPrintfChk(CallInst *CI, IRBuilder<> &B); 65353358Sdim Value *optimizeSPrintfChk(CallInst *CI,IRBuilder<> &B); 66353358Sdim Value *optimizeStrCatChk(CallInst *CI, IRBuilder<> &B); 67353358Sdim Value *optimizeStrLCat(CallInst *CI, IRBuilder<> &B); 68353358Sdim Value *optimizeStrNCatChk(CallInst *CI, IRBuilder<> &B); 69353358Sdim Value *optimizeStrLCpyChk(CallInst *CI, IRBuilder<> &B); 70353358Sdim Value *optimizeVSNPrintfChk(CallInst *CI, IRBuilder<> &B); 71353358Sdim Value *optimizeVSPrintfChk(CallInst *CI, IRBuilder<> &B); 72280031Sdim 73341825Sdim /// Checks whether the call \p CI to a fortified libcall is foldable 74280031Sdim /// to the non-fortified version. 75353358Sdim /// 76353358Sdim /// \param CI the call to the fortified libcall. 77353358Sdim /// 78353358Sdim /// \param ObjSizeOp the index of the object size parameter of this chk 79353358Sdim /// function. Not optional since this is mandatory. 80353358Sdim /// 81353358Sdim /// \param SizeOp optionally set to the parameter index of an explicit buffer 82353358Sdim /// size argument. For instance, set to '2' for __strncpy_chk. 83353358Sdim /// 84353358Sdim /// \param StrOp optionally set to the parameter index of the source string 85353358Sdim /// parameter to strcpy-like functions, where only the strlen of the source 86353358Sdim /// will be writtin into the destination. 87353358Sdim /// 88353358Sdim /// \param FlagsOp optionally set to the parameter index of a 'flags' 89353358Sdim /// parameter. These are used by an implementation to opt-into stricter 90353358Sdim /// checking. 91280031Sdim bool isFortifiedCallFoldable(CallInst *CI, unsigned ObjSizeOp, 92353358Sdim Optional<unsigned> SizeOp = None, 93353358Sdim Optional<unsigned> StrOp = None, 94353358Sdim Optional<unsigned> FlagsOp = None); 95280031Sdim}; 96280031Sdim 97280031Sdim/// LibCallSimplifier - This class implements a collection of optimizations 98280031Sdim/// that replace well formed calls to library functions with a more optimal 99280031Sdim/// form. For example, replacing 'printf("Hello!")' with 'puts("Hello!")'. 100280031Sdimclass LibCallSimplifier { 101280031Sdimprivate: 102280031Sdim FortifiedLibCallSimplifier FortifiedSimplifier; 103288943Sdim const DataLayout &DL; 104280031Sdim const TargetLibraryInfo *TLI; 105327952Sdim OptimizationRemarkEmitter &ORE; 106353358Sdim BlockFrequencyInfo *BFI; 107353358Sdim ProfileSummaryInfo *PSI; 108280031Sdim bool UnsafeFPShrink; 109288943Sdim function_ref<void(Instruction *, Value *)> Replacer; 110344779Sdim function_ref<void(Instruction *)> Eraser; 111280031Sdim 112341825Sdim /// Internal wrapper for RAUW that is the default implementation. 113288943Sdim /// 114288943Sdim /// Other users may provide an alternate function with this signature instead 115288943Sdim /// of this one. 116344779Sdim static void replaceAllUsesWithDefault(Instruction *I, Value *With) { 117344779Sdim I->replaceAllUsesWith(With); 118344779Sdim } 119280031Sdim 120344779Sdim /// Internal wrapper for eraseFromParent that is the default implementation. 121344779Sdim static void eraseFromParentDefault(Instruction *I) { I->eraseFromParent(); } 122344779Sdim 123341825Sdim /// Replace an instruction's uses with a value using our replacer. 124288943Sdim void replaceAllUsesWith(Instruction *I, Value *With); 125288943Sdim 126344779Sdim /// Erase an instruction from its parent with our eraser. 127344779Sdim void eraseFromParent(Instruction *I); 128344779Sdim 129360784Sdim /// Replace an instruction with a value and erase it from its parent. 130360784Sdim void substituteInParent(Instruction *I, Value *With) { 131360784Sdim replaceAllUsesWith(I, With); 132360784Sdim eraseFromParent(I); 133360784Sdim } 134360784Sdim 135344779Sdim Value *foldMallocMemset(CallInst *Memset, IRBuilder<> &B); 136344779Sdim 137280031Sdimpublic: 138344779Sdim LibCallSimplifier( 139344779Sdim const DataLayout &DL, const TargetLibraryInfo *TLI, 140344779Sdim OptimizationRemarkEmitter &ORE, 141353358Sdim BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, 142344779Sdim function_ref<void(Instruction *, Value *)> Replacer = 143344779Sdim &replaceAllUsesWithDefault, 144344779Sdim function_ref<void(Instruction *)> Eraser = &eraseFromParentDefault); 145280031Sdim 146280031Sdim /// optimizeCall - Take the given call instruction and return a more 147280031Sdim /// optimal value to replace the instruction with or 0 if a more 148280031Sdim /// optimal form can't be found. Note that the returned value may 149280031Sdim /// be equal to the instruction being optimized. In this case all 150280031Sdim /// other instructions that use the given instruction were modified 151280031Sdim /// and the given instruction is dead. 152280031Sdim /// The call must not be an indirect call. 153280031Sdim Value *optimizeCall(CallInst *CI); 154280031Sdim 155280031Sdimprivate: 156280031Sdim // String and Memory Library Call Optimizations 157280031Sdim Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B); 158280031Sdim Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B); 159280031Sdim Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B); 160280031Sdim Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B); 161280031Sdim Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B); 162280031Sdim Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B); 163360784Sdim Value *optimizeStrNDup(CallInst *CI, IRBuilder<> &B); 164280031Sdim Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B); 165280031Sdim Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B); 166280031Sdim Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B); 167280031Sdim Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B); 168280031Sdim Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B); 169280031Sdim Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B); 170280031Sdim Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B); 171280031Sdim Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B); 172280031Sdim Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B); 173288943Sdim Value *optimizeMemChr(CallInst *CI, IRBuilder<> &B); 174360784Sdim Value *optimizeMemRChr(CallInst *CI, IRBuilder<> &B); 175280031Sdim Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B); 176353358Sdim Value *optimizeBCmp(CallInst *CI, IRBuilder<> &B); 177353358Sdim Value *optimizeMemCmpBCmpCommon(CallInst *CI, IRBuilder<> &B); 178360784Sdim Value *optimizeMemCCpy(CallInst *CI, IRBuilder<> &B); 179360784Sdim Value *optimizeMemPCpy(CallInst *CI, IRBuilder<> &B); 180280031Sdim Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B); 181280031Sdim Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B); 182280031Sdim Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B); 183341825Sdim Value *optimizeRealloc(CallInst *CI, IRBuilder<> &B); 184321369Sdim Value *optimizeWcslen(CallInst *CI, IRBuilder<> &B); 185360784Sdim Value *optimizeBCopy(CallInst *CI, IRBuilder<> &B); 186280031Sdim // Wrapper for all String/Memory Library Call Optimizations 187280031Sdim Value *optimizeStringMemoryLibCall(CallInst *CI, IRBuilder<> &B); 188280031Sdim 189280031Sdim // Math Library Optimizations 190327952Sdim Value *optimizeCAbs(CallInst *CI, IRBuilder<> &B); 191280031Sdim Value *optimizePow(CallInst *CI, IRBuilder<> &B); 192344779Sdim Value *replacePowWithExp(CallInst *Pow, IRBuilder<> &B); 193327952Sdim Value *replacePowWithSqrt(CallInst *Pow, IRBuilder<> &B); 194280031Sdim Value *optimizeExp2(CallInst *CI, IRBuilder<> &B); 195296417Sdim Value *optimizeFMinFMax(CallInst *CI, IRBuilder<> &B); 196296417Sdim Value *optimizeLog(CallInst *CI, IRBuilder<> &B); 197280031Sdim Value *optimizeSqrt(CallInst *CI, IRBuilder<> &B); 198280031Sdim Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B); 199296417Sdim Value *optimizeTan(CallInst *CI, IRBuilder<> &B); 200327952Sdim // Wrapper for all floating point library call optimizations 201327952Sdim Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func, 202327952Sdim IRBuilder<> &B); 203280031Sdim 204280031Sdim // Integer Library Call Optimizations 205280031Sdim Value *optimizeFFS(CallInst *CI, IRBuilder<> &B); 206314564Sdim Value *optimizeFls(CallInst *CI, IRBuilder<> &B); 207280031Sdim Value *optimizeAbs(CallInst *CI, IRBuilder<> &B); 208280031Sdim Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B); 209280031Sdim Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B); 210280031Sdim Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B); 211341825Sdim Value *optimizeAtoi(CallInst *CI, IRBuilder<> &B); 212341825Sdim Value *optimizeStrtol(CallInst *CI, IRBuilder<> &B); 213280031Sdim 214280031Sdim // Formatting and IO Library Call Optimizations 215280031Sdim Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B, 216280031Sdim int StreamArg = -1); 217280031Sdim Value *optimizePrintF(CallInst *CI, IRBuilder<> &B); 218280031Sdim Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B); 219341825Sdim Value *optimizeSnPrintF(CallInst *CI, IRBuilder<> &B); 220280031Sdim Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B); 221280031Sdim Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B); 222341825Sdim Value *optimizeFRead(CallInst *CI, IRBuilder<> &B); 223280031Sdim Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B); 224341825Sdim Value *optimizeFGets(CallInst *CI, IRBuilder<> &B); 225341825Sdim Value *optimizeFPutc(CallInst *CI, IRBuilder<> &B); 226341825Sdim Value *optimizeFGetc(CallInst *CI, IRBuilder<> &B); 227280031Sdim Value *optimizePuts(CallInst *CI, IRBuilder<> &B); 228280031Sdim 229280031Sdim // Helper methods 230280031Sdim Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B); 231309124Sdim void classifyArgUse(Value *Val, Function *F, bool IsFloat, 232280031Sdim SmallVectorImpl<CallInst *> &SinCalls, 233280031Sdim SmallVectorImpl<CallInst *> &CosCalls, 234280031Sdim SmallVectorImpl<CallInst *> &SinCosCalls); 235280031Sdim Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B); 236280031Sdim Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B); 237341825Sdim Value *optimizeSnPrintFString(CallInst *CI, IRBuilder<> &B); 238280031Sdim Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B); 239280031Sdim 240280031Sdim /// hasFloatVersion - Checks if there is a float version of the specified 241280031Sdim /// function by checking for an existing function with name FuncName + f 242280031Sdim bool hasFloatVersion(StringRef FuncName); 243321369Sdim 244321369Sdim /// Shared code to optimize strlen+wcslen. 245321369Sdim Value *optimizeStringLength(CallInst *CI, IRBuilder<> &B, unsigned CharSize); 246280031Sdim}; 247243789Sdim} // End llvm namespace 248243789Sdim 249243789Sdim#endif 250