LLVM API Documentation
00001 //===- SimplifyLibCalls.h - Library call simplifier -------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file exposes an interface to build some C language libcalls for 00011 // optimization passes that need to call the various functions. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H 00016 #define LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H 00017 00018 #include "llvm/ADT/StringRef.h" 00019 #include "llvm/IR/IRBuilder.h" 00020 00021 namespace llvm { 00022 class Value; 00023 class CallInst; 00024 class DataLayout; 00025 class Instruction; 00026 class TargetLibraryInfo; 00027 class BasicBlock; 00028 class Function; 00029 00030 /// LibCallSimplifier - This class implements a collection of optimizations 00031 /// that replace well formed calls to library functions with a more optimal 00032 /// form. For example, replacing 'printf("Hello!")' with 'puts("Hello!")'. 00033 class LibCallSimplifier { 00034 private: 00035 const DataLayout *DL; 00036 const TargetLibraryInfo *TLI; 00037 bool UnsafeFPShrink; 00038 00039 protected: 00040 ~LibCallSimplifier() {} 00041 00042 public: 00043 LibCallSimplifier(const DataLayout *TD, const TargetLibraryInfo *TLI, 00044 bool UnsafeFPShrink); 00045 00046 /// optimizeCall - Take the given call instruction and return a more 00047 /// optimal value to replace the instruction with or 0 if a more 00048 /// optimal form can't be found. Note that the returned value may 00049 /// be equal to the instruction being optimized. In this case all 00050 /// other instructions that use the given instruction were modified 00051 /// and the given instruction is dead. 00052 Value *optimizeCall(CallInst *CI); 00053 00054 /// replaceAllUsesWith - This method is used when the library call 00055 /// simplifier needs to replace instructions other than the library 00056 /// call being modified. 00057 virtual void replaceAllUsesWith(Instruction *I, Value *With) const; 00058 00059 private: 00060 // Fortified Library Call Optimizations 00061 Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B); 00062 Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B); 00063 Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B); 00064 Value *optimizeStrCpyChk(CallInst *CI, IRBuilder<> &B); 00065 Value *optimizeStpCpyChk(CallInst *CI, IRBuilder<> &B); 00066 Value *optimizeStrNCpyChk(CallInst *CI, IRBuilder<> &B); 00067 00068 // String and Memory Library Call Optimizations 00069 Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B); 00070 Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B); 00071 Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B); 00072 Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B); 00073 Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B); 00074 Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B); 00075 Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B); 00076 Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B); 00077 Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B); 00078 Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B); 00079 Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B); 00080 Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B); 00081 Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B); 00082 Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B); 00083 Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B); 00084 Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B); 00085 Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B); 00086 Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B); 00087 Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B); 00088 00089 // Math Library Optimizations 00090 Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B, bool CheckRetType); 00091 Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B); 00092 Value *optimizeCos(CallInst *CI, IRBuilder<> &B); 00093 Value *optimizePow(CallInst *CI, IRBuilder<> &B); 00094 Value *optimizeExp2(CallInst *CI, IRBuilder<> &B); 00095 Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B); 00096 00097 // Integer Library Call Optimizations 00098 Value *optimizeFFS(CallInst *CI, IRBuilder<> &B); 00099 Value *optimizeAbs(CallInst *CI, IRBuilder<> &B); 00100 Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B); 00101 Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B); 00102 Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B); 00103 00104 // Formatting and IO Library Call Optimizations 00105 Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B, 00106 int StreamArg = -1); 00107 Value *optimizePrintF(CallInst *CI, IRBuilder<> &B); 00108 Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B); 00109 Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B); 00110 Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B); 00111 Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B); 00112 Value *optimizePuts(CallInst *CI, IRBuilder<> &B); 00113 00114 // Helper methods 00115 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B); 00116 void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat, 00117 SmallVectorImpl<CallInst *> &SinCalls, 00118 SmallVectorImpl<CallInst *> &CosCalls, 00119 SmallVectorImpl<CallInst *> &SinCosCalls); 00120 void replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls, Value *Res); 00121 Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B); 00122 Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B); 00123 Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B); 00124 00125 /// hasFloatVersion - Checks if there is a float version of the specified 00126 /// function by checking for an existing function with name FuncName + f 00127 bool hasFloatVersion(StringRef FuncName); 00128 }; 00129 } // End llvm namespace 00130 00131 #endif