]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - opencl/llvm.git/blob - include/llvm/Transforms/Utils/SimplifyLibCalls.h
Shrinkify libcalls: use float versions of double libm functions with fast-math (bug...
[opencl/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 //===----------------------------------------------------------------------===//
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"
21 namespace llvm {
22 class Value;
23 class CallInst;
24 class DataLayout;
25 class Instruction;
26 class TargetLibraryInfo;
27 class BasicBlock;
28 class Function;
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;
39 protected:
40   ~LibCallSimplifier() {}
42 public:
43   LibCallSimplifier(const DataLayout *TD, const TargetLibraryInfo *TLI);
45   /// optimizeCall - Take the given call instruction and return a more
46   /// optimal value to replace the instruction with or 0 if a more
47   /// optimal form can't be found.  Note that the returned value may
48   /// be equal to the instruction being optimized.  In this case all
49   /// other instructions that use the given instruction were modified
50   /// and the given instruction is dead.
51   Value *optimizeCall(CallInst *CI);
53   /// replaceAllUsesWith - This method is used when the library call
54   /// simplifier needs to replace instructions other than the library
55   /// call being modified.
56   virtual void replaceAllUsesWith(Instruction *I, Value *With) const;
58 private:
59   // Fortified Library Call Optimizations
60   Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B);
61   Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B);
62   Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B);
63   Value *optimizeStrCpyChk(CallInst *CI, IRBuilder<> &B);
64   Value *optimizeStpCpyChk(CallInst *CI, IRBuilder<> &B);
65   Value *optimizeStrNCpyChk(CallInst *CI, IRBuilder<> &B);
67   // String and Memory Library Call Optimizations
68   Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B);
69   Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B);
70   Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B);
71   Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B);
72   Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B);
73   Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B);
74   Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B);
75   Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B);
76   Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B);
77   Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B);
78   Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B);
79   Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B);
80   Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B);
81   Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B);
82   Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B);
83   Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B);
84   Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B);
85   Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B);
86   Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B);
88   // Math Library Optimizations
89   Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B, bool CheckRetType);
90   Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B);
91   Value *optimizeCos(CallInst *CI, IRBuilder<> &B);
92   Value *optimizePow(CallInst *CI, IRBuilder<> &B);
93   Value *optimizeExp2(CallInst *CI, IRBuilder<> &B);
94   Value *optimizeFabs(CallInst *CI, IRBuilder<> &B);
95   Value *optimizeSqrt(CallInst *CI, IRBuilder<> &B);
96   Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B);
98   // Integer Library Call Optimizations
99   Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);
100   Value *optimizeAbs(CallInst *CI, IRBuilder<> &B);
101   Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B);
102   Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B);
103   Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B);
105   // Formatting and IO Library Call Optimizations
106   Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
107                                 int StreamArg = -1);
108   Value *optimizePrintF(CallInst *CI, IRBuilder<> &B);
109   Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B);
110   Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B);
111   Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B);
112   Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B);
113   Value *optimizePuts(CallInst *CI, IRBuilder<> &B);
115   // Helper methods
116   Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B);
117   void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
118                       SmallVectorImpl<CallInst *> &SinCalls,
119                       SmallVectorImpl<CallInst *> &CosCalls,
120                       SmallVectorImpl<CallInst *> &SinCosCalls);
121   void replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls, Value *Res);
122   Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B);
123   Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B);
124   Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B);
126   /// hasFloatVersion - Checks if there is a float version of the specified
127   /// function by checking for an existing function with name FuncName + f
128   bool hasFloatVersion(StringRef FuncName);
129 };
130 } // End llvm namespace
132 #endif