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/StringRef.h"
19 #include "llvm/IR/IRBuilder.h"
26 class TargetLibraryInfo;
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 {
36 const TargetLibraryInfo *TLI;
40 LibCallSimplifier(const DataLayout *TD, const TargetLibraryInfo *TLI,
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);
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;
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);
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);
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);
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);
101 // Formatting and IO Library Call Optimizations
102 Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
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);
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);
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);
126 } // End llvm namespace