1 //===- SimplifyLibCalls.h - Library call simplifier -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes an interface to build some C language libcalls for
11 // optimization passes that need to call the various functions.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H
16 #define LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/IR/IRBuilder.h"
26 class TargetLibraryInfo;
30 /// \brief This class implements simplifications for calls to fortified library
31 /// functions (__st*cpy_chk, __memcpy_chk, __memmove_chk, __memset_chk), to,
32 /// when possible, replace them with their non-checking counterparts.
33 /// Other optimizations can also be done, but it's possible to disable them and
34 /// only simplify needless use of the checking versions (when the object size
35 /// is unknown) by passing true for OnlyLowerUnknownSize.
36 class FortifiedLibCallSimplifier {
39 const TargetLibraryInfo *TLI;
40 bool OnlyLowerUnknownSize;
43 FortifiedLibCallSimplifier(const DataLayout *DL, const TargetLibraryInfo *TLI,
44 bool OnlyLowerUnknownSize = false);
46 /// \brief Take the given call instruction and return a more
47 /// optimal value to replace the instruction with or 0 if a more
48 /// optimal form can't be found.
49 Value *optimizeCall(CallInst *CI);
52 Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B);
53 Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B);
54 Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B);
55 Value *optimizeStrCpyChk(CallInst *CI, IRBuilder<> &B);
56 Value *optimizeStrNCpyChk(CallInst *CI, IRBuilder<> &B);
58 /// \brief Checks whether the call \p CI to a fortified libcall is foldable
59 /// to the non-fortified version.
60 bool isFortifiedCallFoldable(CallInst *CI, unsigned ObjSizeOp,
61 unsigned SizeOp, bool isString);
64 /// LibCallSimplifier - This class implements a collection of optimizations
65 /// that replace well formed calls to library functions with a more optimal
66 /// form. For example, replacing 'printf("Hello!")' with 'puts("Hello!")'.
67 class LibCallSimplifier {
69 FortifiedLibCallSimplifier FortifiedSimplifier;
71 const TargetLibraryInfo *TLI;
75 ~LibCallSimplifier() {}
78 LibCallSimplifier(const DataLayout *TD, const TargetLibraryInfo *TLI);
80 /// optimizeCall - Take the given call instruction and return a more
81 /// optimal value to replace the instruction with or 0 if a more
82 /// optimal form can't be found. Note that the returned value may
83 /// be equal to the instruction being optimized. In this case all
84 /// other instructions that use the given instruction were modified
85 /// and the given instruction is dead.
86 Value *optimizeCall(CallInst *CI);
88 /// replaceAllUsesWith - This method is used when the library call
89 /// simplifier needs to replace instructions other than the library
90 /// call being modified.
91 virtual void replaceAllUsesWith(Instruction *I, Value *With) const;
94 // String and Memory Library Call Optimizations
95 Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B);
96 Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B);
97 Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B);
98 Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B);
99 Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B);
100 Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B);
101 Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B);
102 Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B);
103 Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B);
104 Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B);
105 Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B);
106 Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B);
107 Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B);
108 Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B);
109 Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B);
110 Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B);
111 Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B);
112 Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B);
113 Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B);
114 // Wrapper for all String/Memory Library Call Optimizations
115 Value *optimizeStringMemoryLibCall(CallInst *CI, IRBuilder<> &B);
117 // Math Library Optimizations
118 Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B, bool CheckRetType);
119 Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B);
120 Value *optimizeCos(CallInst *CI, IRBuilder<> &B);
121 Value *optimizePow(CallInst *CI, IRBuilder<> &B);
122 Value *optimizeExp2(CallInst *CI, IRBuilder<> &B);
123 Value *optimizeFabs(CallInst *CI, IRBuilder<> &B);
124 Value *optimizeSqrt(CallInst *CI, IRBuilder<> &B);
125 Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B);
127 // Integer Library Call Optimizations
128 Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);
129 Value *optimizeAbs(CallInst *CI, IRBuilder<> &B);
130 Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B);
131 Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B);
132 Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B);
134 // Formatting and IO Library Call Optimizations
135 Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
137 Value *optimizePrintF(CallInst *CI, IRBuilder<> &B);
138 Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B);
139 Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B);
140 Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B);
141 Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B);
142 Value *optimizePuts(CallInst *CI, IRBuilder<> &B);
145 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B);
146 void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
147 SmallVectorImpl<CallInst *> &SinCalls,
148 SmallVectorImpl<CallInst *> &CosCalls,
149 SmallVectorImpl<CallInst *> &SinCosCalls);
150 void replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls, Value *Res);
151 Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B);
152 Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B);
153 Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B);
155 /// hasFloatVersion - Checks if there is a float version of the specified
156 /// function by checking for an existing function with name FuncName + f
157 bool hasFloatVersion(StringRef FuncName);
159 } // End llvm namespace