Refactoring SimplifyLibCalls to remove static initializers and generally cleaning...
[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 /// LibCallSimplifier - This class implements a collection of optimizations
31 /// that replace well formed calls to library functions with a more optimal
32 /// form.  For example, replacing 'printf("Hello!")' with 'puts("Hello!")'.
33 class LibCallSimplifier {
34 private:
35   const DataLayout *DL;
36   const TargetLibraryInfo *TLI;
37   bool UnsafeFPShrink;
38
39 public:
40   LibCallSimplifier(const DataLayout *TD, const TargetLibraryInfo *TLI,
41                     bool UnsafeFPShrink);
42
43   /// optimizeCall - Take the given call instruction and return a more
44   /// optimal value to replace the instruction with or 0 if a more
45   /// optimal form can't be found.  Note that the returned value may
46   /// be equal to the instruction being optimized.  In this case all
47   /// other instructions that use the given instruction were modified
48   /// and the given instruction is dead.
49   Value *optimizeCall(CallInst *CI);
50
51   /// replaceAllUsesWith - This method is used when the library call
52   /// simplifier needs to replace instructions other than the library
53   /// call being modified.
54   virtual void replaceAllUsesWith(Instruction *I, Value *With) const;
55
56 private:
57   // Fortified Library Call Optimizations
58   Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B);
59   Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B);
60   Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B);
61   Value *optimizeStrCpyChk(CallInst *CI, IRBuilder<> &B);
62   Value *optimizeStpCpyChk(CallInst *CI, IRBuilder<> &B);
63   Value *optimizeStrNCpyChk(CallInst *CI, IRBuilder<> &B);
64
65   // String and Memory Library Call Optimizations
66   Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B);
67   Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B);
68   Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B);
69   Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B);
70   Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B);
71   Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B);
72   Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B);
73   Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B);
74   Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B);
75   Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B);
76   Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B);
77   Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B);
78   Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B);
79   Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B);
80   Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B);
81   Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B);
82   Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B);
83   Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B);
84   Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B);
85
86   // Math Library Optimizations
87   Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B, bool CheckRetType);
88   Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B);
89   Value *optimizeCos(CallInst *CI, IRBuilder<> &B);
90   Value *optimizePow(CallInst *CI, IRBuilder<> &B);
91   Value *optimizeExp2(CallInst *CI, IRBuilder<> &B);
92   Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B);
93
94   // Integer Library Call Optimizations
95   Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);
96   Value *optimizeAbs(CallInst *CI, IRBuilder<> &B);
97   Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B);
98   Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B);
99   Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B);
100
101   // Formatting and IO Library Call Optimizations
102   Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
103                                 int StreamArg = -1);
104   Value *optimizePrintF(CallInst *CI, IRBuilder<> &B);
105   Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B);
106   Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B);
107   Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B);
108   Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B);
109   Value *optimizePuts(CallInst *CI, IRBuilder<> &B);
110
111   // Helper methods
112   Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B);
113   void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
114                       SmallVectorImpl<CallInst *> &SinCalls,
115                       SmallVectorImpl<CallInst *> &CosCalls,
116                       SmallVectorImpl<CallInst *> &SinCosCalls);
117   void replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls, Value *Res);
118   Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B);
119   Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B);
120   Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B);
121
122   /// hasFloatVersion - Checks if there is a float version of the specified
123   /// function by checking for an existing function with name FuncName + f
124   bool hasFloatVersion(StringRef FuncName);
125 };
126 } // End llvm namespace
127
128 #endif