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,
44 bool UnsafeFPShrink);
46 /// optimizeCall - 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. Note that the returned value may
49 /// be equal to the instruction being optimized. In this case all
50 /// other instructions that use the given instruction were modified
51 /// and the given instruction is dead.
52 Value *optimizeCall(CallInst *CI);
54 /// replaceAllUsesWith - This method is used when the library call
55 /// simplifier needs to replace instructions other than the library
56 /// call being modified.
57 virtual void replaceAllUsesWith(Instruction *I, Value *With) const;
59 private:
60 // Fortified Library Call Optimizations
61 Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B);
62 Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B);
63 Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B);
64 Value *optimizeStrCpyChk(CallInst *CI, IRBuilder<> &B);
65 Value *optimizeStpCpyChk(CallInst *CI, IRBuilder<> &B);
66 Value *optimizeStrNCpyChk(CallInst *CI, IRBuilder<> &B);
68 // String and Memory Library Call Optimizations
69 Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B);
70 Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B);
71 Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B);
72 Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B);
73 Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B);
74 Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B);
75 Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B);
76 Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B);
77 Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B);
78 Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B);
79 Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B);
80 Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B);
81 Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B);
82 Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B);
83 Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B);
84 Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B);
85 Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B);
86 Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B);
87 Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B);
89 // Math Library Optimizations
90 Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B, bool CheckRetType);
91 Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B);
92 Value *optimizeCos(CallInst *CI, IRBuilder<> &B);
93 Value *optimizePow(CallInst *CI, IRBuilder<> &B);
94 Value *optimizeExp2(CallInst *CI, IRBuilder<> &B);
95 Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B);
97 // Integer Library Call Optimizations
98 Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);
99 Value *optimizeAbs(CallInst *CI, IRBuilder<> &B);
100 Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B);
101 Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B);
102 Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B);
104 // Formatting and IO Library Call Optimizations
105 Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
106 int StreamArg = -1);
107 Value *optimizePrintF(CallInst *CI, IRBuilder<> &B);
108 Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B);
109 Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B);
110 Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B);
111 Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B);
112 Value *optimizePuts(CallInst *CI, IRBuilder<> &B);
114 // Helper methods
115 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B);
116 void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
117 SmallVectorImpl<CallInst *> &SinCalls,
118 SmallVectorImpl<CallInst *> &CosCalls,
119 SmallVectorImpl<CallInst *> &SinCosCalls);
120 void replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls, Value *Res);
121 Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B);
122 Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B);
123 Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B);
125 /// hasFloatVersion - Checks if there is a float version of the specified
126 /// function by checking for an existing function with name FuncName + f
127 bool hasFloatVersion(StringRef FuncName);
128 };
129 } // End llvm namespace
131 #endif