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/STLExtras.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Analysis/TargetLibraryInfo.h"
21 #include "llvm/IR/IRBuilder.h"
28 class TargetLibraryInfo;
32 /// \brief This class implements simplifications for calls to fortified library
33 /// functions (__st*cpy_chk, __memcpy_chk, __memmove_chk, __memset_chk), to,
34 /// when possible, replace them with their non-checking counterparts.
35 /// Other optimizations can also be done, but it's possible to disable them and
36 /// only simplify needless use of the checking versions (when the object size
37 /// is unknown) by passing true for OnlyLowerUnknownSize.
38 class FortifiedLibCallSimplifier {
41 const TargetLibraryInfo *TLI;
42 bool OnlyLowerUnknownSize;
45 FortifiedLibCallSimplifier(const DataLayout *DL, const TargetLibraryInfo *TLI,
46 bool OnlyLowerUnknownSize = false);
48 /// \brief 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);
55 Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B);
56 Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B);
57 Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B);
59 // Str/Stp cpy are similar enough to be handled in the same functions.
60 Value *optimizeStrpCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc::Func Func);
61 Value *optimizeStrpNCpyChk(CallInst *CI, IRBuilder<> &B, LibFunc::Func Func);
63 /// \brief 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);
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 {
74 FortifiedLibCallSimplifier FortifiedSimplifier;
76 const TargetLibraryInfo *TLI;
78 function_ref<void(Instruction *, Value *)> Replacer;
80 /// \brief Internal wrapper for RAUW that is the default implementation.
82 /// Other users may provide an alternate function with this signature instead
84 static void replaceAllUsesWithDefault(Instruction *I, Value *With);
86 /// \brief Replace an instruction's uses with a value using our replacer.
87 void replaceAllUsesWith(Instruction *I, Value *With);
90 LibCallSimplifier(const DataLayout *TD, const TargetLibraryInfo *TLI,
91 function_ref<void(Instruction *, Value *)> Replacer =
92 &replaceAllUsesWithDefault);
94 /// optimizeCall - Take the given call instruction and return a more
95 /// optimal value to replace the instruction with or 0 if a more
96 /// optimal form can't be found. Note that the returned value may
97 /// be equal to the instruction being optimized. In this case all
98 /// other instructions that use the given instruction were modified
99 /// and the given instruction is dead.
100 /// The call must not be an indirect call.
101 Value *optimizeCall(CallInst *CI);
104 // String and Memory Library Call Optimizations
105 Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B);
106 Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B);
107 Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B);
108 Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B);
109 Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B);
110 Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B);
111 Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B);
112 Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B);
113 Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B);
114 Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B);
115 Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B);
116 Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B);
117 Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B);
118 Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B);
119 Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B);
120 Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B);
121 Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B);
122 Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B);
123 Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B);
124 // Wrapper for all String/Memory Library Call Optimizations
125 Value *optimizeStringMemoryLibCall(CallInst *CI, IRBuilder<> &B);
127 // Math Library Optimizations
128 Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B, bool CheckRetType);
129 Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B);
130 Value *optimizeCos(CallInst *CI, IRBuilder<> &B);
131 Value *optimizePow(CallInst *CI, IRBuilder<> &B);
132 Value *optimizeExp2(CallInst *CI, IRBuilder<> &B);
133 Value *optimizeFabs(CallInst *CI, IRBuilder<> &B);
134 Value *optimizeSqrt(CallInst *CI, IRBuilder<> &B);
135 Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B);
137 // Integer Library Call Optimizations
138 Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);
139 Value *optimizeAbs(CallInst *CI, IRBuilder<> &B);
140 Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B);
141 Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B);
142 Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B);
144 // Formatting and IO Library Call Optimizations
145 Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
147 Value *optimizePrintF(CallInst *CI, IRBuilder<> &B);
148 Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B);
149 Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B);
150 Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B);
151 Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B);
152 Value *optimizePuts(CallInst *CI, IRBuilder<> &B);
155 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B);
156 void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
157 SmallVectorImpl<CallInst *> &SinCalls,
158 SmallVectorImpl<CallInst *> &CosCalls,
159 SmallVectorImpl<CallInst *> &SinCosCalls);
160 void replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls, Value *Res);
161 Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B);
162 Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B);
163 Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B);
165 /// hasFloatVersion - Checks if there is a float version of the specified
166 /// function by checking for an existing function with name FuncName + f
167 bool hasFloatVersion(StringRef FuncName);
169 } // End llvm namespace