[PM] Replace an abuse of inheritance to override a single function with
[oota-llvm.git] / include / llvm / Transforms / Utils / SimplifyLibCalls.h
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/StringRef.h"
19 #include "llvm/IR/IRBuilder.h"
20
21 namespace llvm {
22 class Value;
23 class CallInst;
24 class DataLayout;
25 class Instruction;
26 class TargetLibraryInfo;
27 class BasicBlock;
28 class Function;
29
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 {
37 private:
38   const DataLayout *DL;
39   const TargetLibraryInfo *TLI;
40   bool OnlyLowerUnknownSize;
41
42 public:
43   FortifiedLibCallSimplifier(const DataLayout *DL, const TargetLibraryInfo *TLI,
44                              bool OnlyLowerUnknownSize = false);
45
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   /// The call must not be an indirect call.
50   Value *optimizeCall(CallInst *CI);
51
52 private:
53   Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B);
54   Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B);
55   Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B);
56   Value *optimizeStrCpyChk(CallInst *CI, IRBuilder<> &B);
57   Value *optimizeStrNCpyChk(CallInst *CI, IRBuilder<> &B);
58
59   /// \brief Checks whether the call \p CI to a fortified libcall is foldable
60   /// to the non-fortified version.
61   bool isFortifiedCallFoldable(CallInst *CI, unsigned ObjSizeOp,
62                                unsigned SizeOp, bool isString);
63 };
64
65 /// LibCallSimplifier - This class implements a collection of optimizations
66 /// that replace well formed calls to library functions with a more optimal
67 /// form.  For example, replacing 'printf("Hello!")' with 'puts("Hello!")'.
68 class LibCallSimplifier {
69 private:
70   FortifiedLibCallSimplifier FortifiedSimplifier;
71   const DataLayout *DL;
72   const TargetLibraryInfo *TLI;
73   bool UnsafeFPShrink;
74   function_ref<void(Instruction *, Value *)> Replacer;
75
76   /// \brief Internal wrapper for RAUW that is the default implementation.
77   ///
78   /// Other users may provide an alternate function with this signature instead
79   /// of this one.
80   static void replaceAllUsesWithDefault(Instruction *I, Value *With);
81
82   /// \brief Replace an instruction's uses with a value using our replacer.
83   void replaceAllUsesWith(Instruction *I, Value *With);
84
85 public:
86   LibCallSimplifier(const DataLayout *TD, const TargetLibraryInfo *TLI,
87                     function_ref<void(Instruction *, Value *)> Replacer =
88                         &replaceAllUsesWithDefault);
89
90   /// optimizeCall - Take the given call instruction and return a more
91   /// optimal value to replace the instruction with or 0 if a more
92   /// optimal form can't be found.  Note that the returned value may
93   /// be equal to the instruction being optimized.  In this case all
94   /// other instructions that use the given instruction were modified
95   /// and the given instruction is dead.
96   /// The call must not be an indirect call.
97   Value *optimizeCall(CallInst *CI);
98
99 private:
100   // String and Memory Library Call Optimizations
101   Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B);
102   Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B);
103   Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B);
104   Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B);
105   Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B);
106   Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B);
107   Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B);
108   Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B);
109   Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B);
110   Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B);
111   Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B);
112   Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B);
113   Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B);
114   Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B);
115   Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B);
116   Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B);
117   Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B);
118   Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B);
119   Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B);
120   // Wrapper for all String/Memory Library Call Optimizations
121   Value *optimizeStringMemoryLibCall(CallInst *CI, IRBuilder<> &B);
122
123   // Math Library Optimizations
124   Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B, bool CheckRetType);
125   Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B);
126   Value *optimizeCos(CallInst *CI, IRBuilder<> &B);
127   Value *optimizePow(CallInst *CI, IRBuilder<> &B);
128   Value *optimizeExp2(CallInst *CI, IRBuilder<> &B);
129   Value *optimizeFabs(CallInst *CI, IRBuilder<> &B);
130   Value *optimizeSqrt(CallInst *CI, IRBuilder<> &B);
131   Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B);
132
133   // Integer Library Call Optimizations
134   Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);
135   Value *optimizeAbs(CallInst *CI, IRBuilder<> &B);
136   Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B);
137   Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B);
138   Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B);
139
140   // Formatting and IO Library Call Optimizations
141   Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
142                                 int StreamArg = -1);
143   Value *optimizePrintF(CallInst *CI, IRBuilder<> &B);
144   Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B);
145   Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B);
146   Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B);
147   Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B);
148   Value *optimizePuts(CallInst *CI, IRBuilder<> &B);
149
150   // Helper methods
151   Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B);
152   void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
153                       SmallVectorImpl<CallInst *> &SinCalls,
154                       SmallVectorImpl<CallInst *> &CosCalls,
155                       SmallVectorImpl<CallInst *> &SinCosCalls);
156   void replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls, Value *Res);
157   Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B);
158   Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B);
159   Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B);
160
161   /// hasFloatVersion - Checks if there is a float version of the specified
162   /// function by checking for an existing function with name FuncName + f
163   bool hasFloatVersion(StringRef FuncName);
164 };
165 } // End llvm namespace
166
167 #endif