<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Agreed. Yours is the better solution. If you’d like I can make the change.<div class=""><br class=""></div><div class="">-Chris</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 17, 2014, at 3:28 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com" class="">dblaikie@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><br class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Sep 17, 2014 at 3:27 PM, Chris Bieneman <span dir="ltr" class=""><<a href="mailto:beanz@apple.com" target="_blank" class="">beanz@apple.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">I put in a change in r217988 that should have resolved it. I think your change will fail to build because now there are two destructors declared.</div></blockquote><div class=""><br class=""></div><div class="">Yep - sorry, I seem to be a bit slow off the mark.<br class=""><br class="">Reverted my fix in r217989. <br class=""><br class="">Though it might be worth considering my fix over yours as it doesn't look like objects of this type are polymorphically owned - so no virtual dtor is necessary.<br class=""><br class="">- David</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><br class=""></div><div class="">-Chris</div><div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div class="">On Sep 17, 2014, at 3:25 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank" class="">dblaikie@gmail.com</a>> wrote:</div><br class=""><div class=""><div dir="ltr" class="">This introduced a -Wnon-virtual-dtor warning which I've fixed in r217988. Let me know if that's not the right fix, etc.<br class=""><br class="">- David</div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Sep 17, 2014 at 1:55 PM, Chris Bieneman <span dir="ltr" class=""><<a href="mailto:beanz@apple.com" target="_blank" class="">beanz@apple.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: cbieneman<br class="">
Date: Wed Sep 17 15:55:46 2014<br class="">
New Revision: 217982<br class="">
<br class="">
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=217982&view=rev" target="_blank" class="">http://llvm.org/viewvc/llvm-project?rev=217982&view=rev</a><br class="">
Log:<br class="">
Refactoring SimplifyLibCalls to remove static initializers and generally cleaning up the code.<br class="">
<br class="">
Summary: This eliminates ~200 lines of code mostly file scoped struct definitions that were unnecessary.<br class="">
<br class="">
Reviewers: chandlerc, resistor<br class="">
<br class="">
Reviewed By: resistor<br class="">
<br class="">
Subscribers: morisset, resistor, llvm-commits<br class="">
<br class="">
Differential Revision: <a href="http://reviews.llvm.org/D5364" target="_blank" class="">http://reviews.llvm.org/D5364</a><br class="">
<br class="">
Modified:<br class="">
llvm/trunk/include/llvm/Transforms/Utils/SimplifyLibCalls.h<br class="">
llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp<br class="">
llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp<br class="">
<br class="">
Modified: llvm/trunk/include/llvm/Transforms/Utils/SimplifyLibCalls.h<br class="">
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/SimplifyLibCalls.h?rev=217982&r1=217981&r2=217982&view=diff" target="_blank" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/SimplifyLibCalls.h?rev=217982&r1=217981&r2=217982&view=diff</a><br class="">
==============================================================================<br class="">
--- llvm/trunk/include/llvm/Transforms/Utils/SimplifyLibCalls.h (original)<br class="">
+++ llvm/trunk/include/llvm/Transforms/Utils/SimplifyLibCalls.h Wed Sep 17 15:55:46 2014<br class="">
@@ -15,40 +15,114 @@<br class="">
#ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H<br class="">
#define LLVM_TRANSFORMS_UTILS_SIMPLIFYLIBCALLS_H<br class="">
<br class="">
+#include "llvm/ADT/StringRef.h"<br class="">
+#include "llvm/IR/IRBuilder.h"<br class="">
+<br class="">
namespace llvm {<br class="">
- class Value;<br class="">
- class CallInst;<br class="">
- class DataLayout;<br class="">
- class Instruction;<br class="">
- class TargetLibraryInfo;<br class="">
- class LibCallSimplifierImpl;<br class="">
-<br class="">
- /// LibCallSimplifier - This class implements a collection of optimizations<br class="">
- /// that replace well formed calls to library functions with a more optimal<br class="">
- /// form. For example, replacing 'printf("Hello!")' with 'puts("Hello!")'.<br class="">
- class LibCallSimplifier {<br class="">
- /// Impl - A pointer to the actual implementation of the library call<br class="">
- /// simplifier.<br class="">
- LibCallSimplifierImpl *Impl;<br class="">
-<br class="">
- public:<br class="">
- LibCallSimplifier(const DataLayout *TD, const TargetLibraryInfo *TLI,<br class="">
- bool UnsafeFPShrink);<br class="">
- virtual ~LibCallSimplifier();<br class="">
-<br class="">
- /// optimizeCall - Take the given call instruction and return a more<br class="">
- /// optimal value to replace the instruction with or 0 if a more<br class="">
- /// optimal form can't be found. Note that the returned value may<br class="">
- /// be equal to the instruction being optimized. In this case all<br class="">
- /// other instructions that use the given instruction were modified<br class="">
- /// and the given instruction is dead.<br class="">
- Value *optimizeCall(CallInst *CI);<br class="">
-<br class="">
- /// replaceAllUsesWith - This method is used when the library call<br class="">
- /// simplifier needs to replace instructions other than the library<br class="">
- /// call being modified.<br class="">
- virtual void replaceAllUsesWith(Instruction *I, Value *With) const;<br class="">
- };<br class="">
+class Value;<br class="">
+class CallInst;<br class="">
+class DataLayout;<br class="">
+class Instruction;<br class="">
+class TargetLibraryInfo;<br class="">
+class BasicBlock;<br class="">
+class Function;<br class="">
+<br class="">
+/// LibCallSimplifier - This class implements a collection of optimizations<br class="">
+/// that replace well formed calls to library functions with a more optimal<br class="">
+/// form. For example, replacing 'printf("Hello!")' with 'puts("Hello!")'.<br class="">
+class LibCallSimplifier {<br class="">
+private:<br class="">
+ const DataLayout *DL;<br class="">
+ const TargetLibraryInfo *TLI;<br class="">
+ bool UnsafeFPShrink;<br class="">
+<br class="">
+public:<br class="">
+ LibCallSimplifier(const DataLayout *TD, const TargetLibraryInfo *TLI,<br class="">
+ bool UnsafeFPShrink);<br class="">
+<br class="">
+ /// optimizeCall - Take the given call instruction and return a more<br class="">
+ /// optimal value to replace the instruction with or 0 if a more<br class="">
+ /// optimal form can't be found. Note that the returned value may<br class="">
+ /// be equal to the instruction being optimized. In this case all<br class="">
+ /// other instructions that use the given instruction were modified<br class="">
+ /// and the given instruction is dead.<br class="">
+ Value *optimizeCall(CallInst *CI);<br class="">
+<br class="">
+ /// replaceAllUsesWith - This method is used when the library call<br class="">
+ /// simplifier needs to replace instructions other than the library<br class="">
+ /// call being modified.<br class="">
+ virtual void replaceAllUsesWith(Instruction *I, Value *With) const;<br class="">
+<br class="">
+private:<br class="">
+ // Fortified Library Call Optimizations<br class="">
+ Value *optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeMemSetChk(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrCpyChk(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStpCpyChk(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrNCpyChk(CallInst *CI, IRBuilder<> &B);<br class="">
+<br class="">
+ // String and Memory Library Call Optimizations<br class="">
+ Value *optimizeStrCat(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrNCat(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrChr(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrLen(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrPBrk(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrTo(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrSpn(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrCSpn(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B);<br class="">
+<br class="">
+ // Math Library Optimizations<br class="">
+ Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B, bool CheckRetType);<br class="">
+ Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeCos(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizePow(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeExp2(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B);<br class="">
+<br class="">
+ // Integer Library Call Optimizations<br class="">
+ Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeAbs(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B);<br class="">
+<br class="">
+ // Formatting and IO Library Call Optimizations<br class="">
+ Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,<br class="">
+ int StreamArg = -1);<br class="">
+ Value *optimizePrintF(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeSPrintF(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeFPrintF(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeFWrite(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeFPuts(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizePuts(CallInst *CI, IRBuilder<> &B);<br class="">
+<br class="">
+ // Helper methods<br class="">
+ Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B);<br class="">
+ void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,<br class="">
+ SmallVectorImpl<CallInst *> &SinCalls,<br class="">
+ SmallVectorImpl<CallInst *> &CosCalls,<br class="">
+ SmallVectorImpl<CallInst *> &SinCosCalls);<br class="">
+ void replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls, Value *Res);<br class="">
+ Value *optimizePrintFString(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeSPrintFString(CallInst *CI, IRBuilder<> &B);<br class="">
+ Value *optimizeFPrintFString(CallInst *CI, IRBuilder<> &B);<br class="">
+<br class="">
+ /// hasFloatVersion - Checks if there is a float version of the specified<br class="">
+ /// function by checking for an existing function with name FuncName + f<br class="">
+ bool hasFloatVersion(StringRef FuncName);<br class="">
+};<br class="">
} // End llvm namespace<br class="">
<br class="">
#endif<br class="">
<br class="">
Modified: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp<br class="">
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=217982&r1=217981&r2=217982&view=diff" target="_blank" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=217982&r1=217981&r2=217982&view=diff</a><br class="">
==============================================================================<br class="">
--- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp (original)<br class="">
+++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp Wed Sep 17 15:55:46 2014<br class="">
@@ -70,10 +70,11 @@ STATISTIC(NumExpand, "Number of expan<br class="">
STATISTIC(NumFactor , "Number of factorizations");<br class="">
STATISTIC(NumReassoc , "Number of reassociations");<br class="">
<br class="">
-static cl::opt<bool> UnsafeFPShrink("enable-double-float-shrink", cl::Hidden,<br class="">
- cl::init(false),<br class="">
- cl::desc("Enable unsafe double to float "<br class="">
- "shrinking for math lib calls"));<br class="">
+static cl::opt<bool><br class="">
+ EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden,<br class="">
+ cl::init(false),<br class="">
+ cl::desc("Enable unsafe double to float "<br class="">
+ "shrinking for math lib calls"));<br class="">
<br class="">
// Initialization Routines<br class="">
void llvm::initializeInstCombine(PassRegistry &Registry) {<br class="">
@@ -2913,7 +2914,7 @@ public:<br class="">
InstCombinerLibCallSimplifier(const DataLayout *DL,<br class="">
const TargetLibraryInfo *TLI,<br class="">
InstCombiner *IC)<br class="">
- : LibCallSimplifier(DL, TLI, UnsafeFPShrink) {<br class="">
+ : LibCallSimplifier(DL, TLI, EnableUnsafeFPShrink) {<br class="">
this->IC = IC;<br class="">
}<br class="">
<br class="">
<br class="">
Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp<br class="">
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=217982&r1=217981&r2=217982&view=diff" target="_blank" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=217982&r1=217981&r2=217982&view=diff</a><br class="">
==============================================================================<br class="">
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)<br class="">
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Wed Sep 17 15:55:46 2014<br class="">
@@ -35,57 +35,26 @@<br class="">
using namespace llvm;<br class="">
<br class="">
static cl::opt<bool><br class="">
-ColdErrorCalls("error-reporting-is-cold", cl::init(true),<br class="">
- cl::Hidden, cl::desc("Treat error-reporting calls as cold"));<br class="">
-<br class="">
-/// This class is the abstract base class for the set of optimizations that<br class="">
-/// corresponds to one library call.<br class="">
-namespace {<br class="">
-class LibCallOptimization {<br class="">
-protected:<br class="">
- Function *Caller;<br class="">
- const DataLayout *DL;<br class="">
- const TargetLibraryInfo *TLI;<br class="">
- const LibCallSimplifier *LCS;<br class="">
- LLVMContext* Context;<br class="">
-public:<br class="">
- LibCallOptimization() { }<br class="">
- virtual ~LibCallOptimization() {}<br class="">
-<br class="">
- /// callOptimizer - This pure virtual method is implemented by base classes to<br class="">
- /// do various optimizations. If this returns null then no transformation was<br class="">
- /// performed. If it returns CI, then it transformed the call and CI is to be<br class="">
- /// deleted. If it returns something else, replace CI with the new value and<br class="">
- /// delete CI.<br class="">
- virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)<br class="">
- =0;<br class="">
-<br class="">
- /// ignoreCallingConv - Returns false if this transformation could possibly<br class="">
- /// change the calling convention.<br class="">
- virtual bool ignoreCallingConv() { return false; }<br class="">
-<br class="">
- Value *optimizeCall(CallInst *CI, const DataLayout *DL,<br class="">
- const TargetLibraryInfo *TLI,<br class="">
- const LibCallSimplifier *LCS, IRBuilder<> &B) {<br class="">
- Caller = CI->getParent()->getParent();<br class="">
- this->DL = DL;<br class="">
- this->TLI = TLI;<br class="">
- this->LCS = LCS;<br class="">
- if (CI->getCalledFunction())<br class="">
- Context = &CI->getCalledFunction()->getContext();<br class="">
-<br class="">
- // We never change the calling convention.<br class="">
- if (!ignoreCallingConv() && CI->getCallingConv() != llvm::CallingConv::C)<br class="">
- return nullptr;<br class="">
-<br class="">
- return callOptimizer(CI->getCalledFunction(), CI, B);<br class="">
- }<br class="">
-};<br class="">
+ ColdErrorCalls("error-reporting-is-cold", cl::init(true), cl::Hidden,<br class="">
+ cl::desc("Treat error-reporting calls as cold"));<br class="">
<br class="">
//===----------------------------------------------------------------------===//<br class="">
// Helper Functions<br class="">
//===----------------------------------------------------------------------===//<br class="">
<br class="">
+static bool ignoreCallingConv(LibFunc::Func Func) {<br class="">
+ switch (Func) {<br class="">
+ case LibFunc::abs:<br class="">
+ case LibFunc::labs:<br class="">
+ case LibFunc::llabs:<br class="">
+ case LibFunc::strlen:<br class="">
+ return true;<br class="">
+ default:<br class="">
+ return false;<br class="">
+ }<br class="">
+ llvm_unreachable();<br class="">
+}<br class="">
+<br class="">
/// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the<br class="">
/// value is equal or not-equal to zero.<br class="">
static bool isOnlyUsedInZeroEqualityComparison(Value *V) {<br class="">
@@ -142,967 +111,912 @@ static bool hasUnaryFloatFn(const Target<br class="">
// Fortified Library Call Optimizations<br class="">
//===----------------------------------------------------------------------===//<br class="">
<br class="">
-struct FortifiedLibCallOptimization : public LibCallOptimization {<br class="">
-protected:<br class="">
- virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,<br class="">
- bool isString) const = 0;<br class="">
-};<br class="">
-<br class="">
-struct InstFortifiedLibCallOptimization : public FortifiedLibCallOptimization {<br class="">
- CallInst *CI;<br class="">
-<br class="">
- bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,<br class="">
- bool isString) const override {<br class="">
- if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))<br class="">
+static bool isFortifiedCallFoldable(CallInst *CI, unsigned SizeCIOp, unsigned SizeArgOp,<br class="">
+ bool isString) {<br class="">
+ if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))<br class="">
+ return true;<br class="">
+ if (ConstantInt *SizeCI =<br class="">
+ dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {<br class="">
+ if (SizeCI->isAllOnesValue())<br class="">
return true;<br class="">
- if (ConstantInt *SizeCI =<br class="">
- dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {<br class="">
- if (SizeCI->isAllOnesValue())<br class="">
- return true;<br class="">
- if (isString) {<br class="">
- uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));<br class="">
- // If the length is 0 we don't know how long it is and so we can't<br class="">
- // remove the check.<br class="">
- if (Len == 0) return false;<br class="">
- return SizeCI->getZExtValue() >= Len;<br class="">
- }<br class="">
- if (ConstantInt *Arg = dyn_cast<ConstantInt>(<br class="">
- CI->getArgOperand(SizeArgOp)))<br class="">
- return SizeCI->getZExtValue() >= Arg->getZExtValue();<br class="">
+ if (isString) {<br class="">
+ uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));<br class="">
+ // If the length is 0 we don't know how long it is and so we can't<br class="">
+ // remove the check.<br class="">
+ if (Len == 0)<br class="">
+ return false;<br class="">
+ return SizeCI->getZExtValue() >= Len;<br class="">
}<br class="">
- return false;<br class="">
+ if (ConstantInt *Arg = dyn_cast<ConstantInt>(CI->getArgOperand(SizeArgOp)))<br class="">
+ return SizeCI->getZExtValue() >= Arg->getZExtValue();<br class="">
}<br class="">
-};<br class="">
+ return false;<br class="">
+}<br class="">
<br class="">
-struct MemCpyChkOpt : public InstFortifiedLibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- this->CI = CI;<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- LLVMContext &Context = CI->getParent()->getContext();<br class="">
-<br class="">
- // Check if this has the right signature.<br class="">
- if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isPointerTy() ||<br class="">
- FT->getParamType(2) != DL->getIntPtrType(Context) ||<br class="">
- FT->getParamType(3) != DL->getIntPtrType(Context))<br class="">
- return nullptr;<br class="">
-<br class="">
- if (isFoldable(3, 2, false)) {<br class="">
- B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
- CI->getArgOperand(2), 1);<br class="">
- return CI->getArgOperand(0);<br class="">
- }<br class="">
- return nullptr;<br class="">
- }<br class="">
-};<br class="">
+Value *LibCallSimplifier::optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ LLVMContext &Context = CI->getContext();<br class="">
<br class="">
-struct MemMoveChkOpt : public InstFortifiedLibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- this->CI = CI;<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- LLVMContext &Context = CI->getParent()->getContext();<br class="">
-<br class="">
- // Check if this has the right signature.<br class="">
- if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isPointerTy() ||<br class="">
- FT->getParamType(2) != DL->getIntPtrType(Context) ||<br class="">
- FT->getParamType(3) != DL->getIntPtrType(Context))<br class="">
- return nullptr;<br class="">
-<br class="">
- if (isFoldable(3, 2, false)) {<br class="">
- B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
- CI->getArgOperand(2), 1);<br class="">
- return CI->getArgOperand(0);<br class="">
- }<br class="">
+ // Check if this has the right signature.<br class="">
+ if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isPointerTy() ||<br class="">
+ FT->getParamType(2) != DL->getIntPtrType(Context) ||<br class="">
+ FT->getParamType(3) != DL->getIntPtrType(Context))<br class="">
return nullptr;<br class="">
- }<br class="">
-};<br class="">
<br class="">
-struct MemSetChkOpt : public InstFortifiedLibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- this->CI = CI;<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- LLVMContext &Context = CI->getParent()->getContext();<br class="">
-<br class="">
- // Check if this has the right signature.<br class="">
- if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isIntegerTy() ||<br class="">
- FT->getParamType(2) != DL->getIntPtrType(Context) ||<br class="">
- FT->getParamType(3) != DL->getIntPtrType(Context))<br class="">
- return nullptr;<br class="">
-<br class="">
- if (isFoldable(3, 2, false)) {<br class="">
- Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),<br class="">
- false);<br class="">
- B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);<br class="">
- return CI->getArgOperand(0);<br class="">
- }<br class="">
- return nullptr;<br class="">
+ if (isFortifiedCallFoldable(CI, 3, 2, false)) {<br class="">
+ B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
+ CI->getArgOperand(2), 1);<br class="">
+ return CI->getArgOperand(0);<br class="">
}<br class="">
-};<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
-struct StrCpyChkOpt : public InstFortifiedLibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- this->CI = CI;<br class="">
- StringRef Name = Callee->getName();<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- LLVMContext &Context = CI->getParent()->getContext();<br class="">
-<br class="">
- // Check if this has the right signature.<br class="">
- if (FT->getNumParams() != 3 ||<br class="">
- FT->getReturnType() != FT->getParamType(0) ||<br class="">
- FT->getParamType(0) != FT->getParamType(1) ||<br class="">
- FT->getParamType(0) != Type::getInt8PtrTy(Context) ||<br class="">
- FT->getParamType(2) != DL->getIntPtrType(Context))<br class="">
- return nullptr;<br class="">
-<br class="">
- Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);<br class="">
- if (Dst == Src) // __strcpy_chk(x,x) -> x<br class="">
- return Src;<br class="">
-<br class="">
- // If a) we don't have any length information, or b) we know this will<br class="">
- // fit then just lower to a plain strcpy. Otherwise we'll keep our<br class="">
- // strcpy_chk call which may fail at runtime if the size is too long.<br class="">
- // TODO: It might be nice to get a maximum length out of the possible<br class="">
- // string lengths for varying.<br class="">
- if (isFoldable(2, 1, true)) {<br class="">
- Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6));<br class="">
- return Ret;<br class="">
- } else {<br class="">
- // Maybe we can stil fold __strcpy_chk to __memcpy_chk.<br class="">
- uint64_t Len = GetStringLength(Src);<br class="">
- if (Len == 0) return nullptr;<br class="">
-<br class="">
- // This optimization require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
-<br class="">
- Value *Ret =<br class="">
- EmitMemCpyChk(Dst, Src,<br class="">
- ConstantInt::get(DL->getIntPtrType(Context), Len),<br class="">
- CI->getArgOperand(2), B, DL, TLI);<br class="">
- return Ret;<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ LLVMContext &Context = CI->getContext();<br class="">
+<br class="">
+ // Check if this has the right signature.<br class="">
+ if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isPointerTy() ||<br class="">
+ FT->getParamType(2) != DL->getIntPtrType(Context) ||<br class="">
+ FT->getParamType(3) != DL->getIntPtrType(Context))<br class="">
return nullptr;<br class="">
+<br class="">
+ if (isFortifiedCallFoldable(CI, 3, 2, false)) {<br class="">
+ B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
+ CI->getArgOperand(2), 1);<br class="">
+ return CI->getArgOperand(0);<br class="">
}<br class="">
-};<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
-struct StpCpyChkOpt : public InstFortifiedLibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- this->CI = CI;<br class="">
- StringRef Name = Callee->getName();<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- LLVMContext &Context = CI->getParent()->getContext();<br class="">
-<br class="">
- // Check if this has the right signature.<br class="">
- if (FT->getNumParams() != 3 ||<br class="">
- FT->getReturnType() != FT->getParamType(0) ||<br class="">
- FT->getParamType(0) != FT->getParamType(1) ||<br class="">
- FT->getParamType(0) != Type::getInt8PtrTy(Context) ||<br class="">
- FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0)))<br class="">
- return nullptr;<br class="">
-<br class="">
- Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);<br class="">
- if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)<br class="">
- Value *StrLen = EmitStrLen(Src, B, DL, TLI);<br class="">
- return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : nullptr;<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeMemSetChk(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ LLVMContext &Context = CI->getContext();<br class="">
<br class="">
- // If a) we don't have any length information, or b) we know this will<br class="">
- // fit then just lower to a plain stpcpy. Otherwise we'll keep our<br class="">
- // stpcpy_chk call which may fail at runtime if the size is too long.<br class="">
- // TODO: It might be nice to get a maximum length out of the possible<br class="">
- // string lengths for varying.<br class="">
- if (isFoldable(2, 1, true)) {<br class="">
- Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6));<br class="">
- return Ret;<br class="">
- } else {<br class="">
- // Maybe we can stil fold __stpcpy_chk to __memcpy_chk.<br class="">
- uint64_t Len = GetStringLength(Src);<br class="">
- if (Len == 0) return nullptr;<br class="">
-<br class="">
- // This optimization require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
-<br class="">
- Type *PT = FT->getParamType(0);<br class="">
- Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len);<br class="">
- Value *DstEnd = B.CreateGEP(Dst,<br class="">
- ConstantInt::get(DL->getIntPtrType(PT),<br class="">
- Len - 1));<br class="">
- if (!EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, DL, TLI))<br class="">
- return nullptr;<br class="">
- return DstEnd;<br class="">
- }<br class="">
+ // Check if this has the right signature.<br class="">
+ if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isIntegerTy() ||<br class="">
+ FT->getParamType(2) != DL->getIntPtrType(Context) ||<br class="">
+ FT->getParamType(3) != DL->getIntPtrType(Context))<br class="">
return nullptr;<br class="">
- }<br class="">
-};<br class="">
<br class="">
-struct StrNCpyChkOpt : public InstFortifiedLibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- this->CI = CI;<br class="">
- StringRef Name = Callee->getName();<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- LLVMContext &Context = CI->getParent()->getContext();<br class="">
-<br class="">
- // Check if this has the right signature.<br class="">
- if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- FT->getParamType(0) != FT->getParamType(1) ||<br class="">
- FT->getParamType(0) != Type::getInt8PtrTy(Context) ||<br class="">
- !FT->getParamType(2)->isIntegerTy() ||<br class="">
- FT->getParamType(3) != DL->getIntPtrType(Context))<br class="">
- return nullptr;<br class="">
-<br class="">
- if (isFoldable(3, 2, false)) {<br class="">
- Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
- CI->getArgOperand(2), B, DL, TLI,<br class="">
- Name.substr(2, 7));<br class="">
- return Ret;<br class="">
- }<br class="">
- return nullptr;<br class="">
+ if (isFortifiedCallFoldable(CI, 3, 2, false)) {<br class="">
+ Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);<br class="">
+ B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);<br class="">
+ return CI->getArgOperand(0);<br class="">
}<br class="">
-};<br class="">
-<br class="">
-//===----------------------------------------------------------------------===//<br class="">
-// String and Memory Library Call Optimizations<br class="">
-//===----------------------------------------------------------------------===//<br class="">
-<br class="">
-struct StrCatOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Verify the "strcat" function prototype.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 ||<br class="">
- FT->getReturnType() != B.getInt8PtrTy() ||<br class="">
- FT->getParamType(0) != FT->getReturnType() ||<br class="">
- FT->getParamType(1) != FT->getReturnType())<br class="">
- return nullptr;<br class="">
-<br class="">
- // Extract some information from the instruction<br class="">
- Value *Dst = CI->getArgOperand(0);<br class="">
- Value *Src = CI->getArgOperand(1);<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- // See if we can get the length of the input string.<br class="">
+Value *LibCallSimplifier::optimizeStrCpyChk(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ StringRef Name = Callee->getName();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ LLVMContext &Context = CI->getContext();<br class="">
+<br class="">
+ // Check if this has the right signature.<br class="">
+ if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ FT->getParamType(0) != FT->getParamType(1) ||<br class="">
+ FT->getParamType(0) != Type::getInt8PtrTy(Context) ||<br class="">
+ FT->getParamType(2) != DL->getIntPtrType(Context))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);<br class="">
+ if (Dst == Src) // __strcpy_chk(x,x) -> x<br class="">
+ return Src;<br class="">
+<br class="">
+ // If a) we don't have any length information, or b) we know this will<br class="">
+ // fit then just lower to a plain strcpy. Otherwise we'll keep our<br class="">
+ // strcpy_chk call which may fail at runtime if the size is too long.<br class="">
+ // TODO: It might be nice to get a maximum length out of the possible<br class="">
+ // string lengths for varying.<br class="">
+ if (isFortifiedCallFoldable(CI, 2, 1, true)) {<br class="">
+ Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6));<br class="">
+ return Ret;<br class="">
+ } else {<br class="">
+ // Maybe we can stil fold __strcpy_chk to __memcpy_chk.<br class="">
uint64_t Len = GetStringLength(Src);<br class="">
- if (Len == 0) return nullptr;<br class="">
- --Len; // Unbias length.<br class="">
-<br class="">
- // Handle the simple, do-nothing case: strcat(x, "") -> x<br class="">
if (Len == 0)<br class="">
- return Dst;<br class="">
+ return nullptr;<br class="">
<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
+ // This optimization require DataLayout.<br class="">
+ if (!DL)<br class="">
+ return nullptr;<br class="">
<br class="">
- return emitStrLenMemCpy(Src, Dst, Len, B);<br class="">
+ Value *Ret = EmitMemCpyChk(<br class="">
+ Dst, Src, ConstantInt::get(DL->getIntPtrType(Context), Len),<br class="">
+ CI->getArgOperand(2), B, DL, TLI);<br class="">
+ return Ret;<br class="">
}<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,<br class="">
- IRBuilder<> &B) {<br class="">
- // We need to find the end of the destination string. That's where the<br class="">
- // memory is to be moved to. We just generate a call to strlen.<br class="">
- Value *DstLen = EmitStrLen(Dst, B, DL, TLI);<br class="">
- if (!DstLen)<br class="">
+Value *LibCallSimplifier::optimizeStpCpyChk(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ StringRef Name = Callee->getName();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ LLVMContext &Context = CI->getContext();<br class="">
+<br class="">
+ // Check if this has the right signature.<br class="">
+ if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ FT->getParamType(0) != FT->getParamType(1) ||<br class="">
+ FT->getParamType(0) != Type::getInt8PtrTy(Context) ||<br class="">
+ FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0)))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);<br class="">
+ if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)<br class="">
+ Value *StrLen = EmitStrLen(Src, B, DL, TLI);<br class="">
+ return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : nullptr;<br class="">
+ }<br class="">
+<br class="">
+ // If a) we don't have any length information, or b) we know this will<br class="">
+ // fit then just lower to a plain stpcpy. Otherwise we'll keep our<br class="">
+ // stpcpy_chk call which may fail at runtime if the size is too long.<br class="">
+ // TODO: It might be nice to get a maximum length out of the possible<br class="">
+ // string lengths for varying.<br class="">
+ if (isFortifiedCallFoldable(CI, 2, 1, true)) {<br class="">
+ Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6));<br class="">
+ return Ret;<br class="">
+ } else {<br class="">
+ // Maybe we can stil fold __stpcpy_chk to __memcpy_chk.<br class="">
+ uint64_t Len = GetStringLength(Src);<br class="">
+ if (Len == 0)<br class="">
return nullptr;<br class="">
<br class="">
- // Now that we have the destination's length, we must index into the<br class="">
- // destination's pointer to get the actual memcpy destination (end of<br class="">
- // the string .. we're concatenating).<br class="">
- Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");<br class="">
+ // This optimization require DataLayout.<br class="">
+ if (!DL)<br class="">
+ return nullptr;<br class="">
<br class="">
- // We have enough information to now generate the memcpy call to do the<br class="">
- // concatenation for us. Make a memcpy to copy the nul byte with align = 1.<br class="">
- B.CreateMemCpy(CpyDst, Src,<br class="">
- ConstantInt::get(DL->getIntPtrType(*Context), Len + 1), 1);<br class="">
- return Dst;<br class="">
+ Type *PT = FT->getParamType(0);<br class="">
+ Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len);<br class="">
+ Value *DstEnd =<br class="">
+ B.CreateGEP(Dst, ConstantInt::get(DL->getIntPtrType(PT), Len - 1));<br class="">
+ if (!EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, DL, TLI))<br class="">
+ return nullptr;<br class="">
+ return DstEnd;<br class="">
}<br class="">
-};<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
-struct StrNCatOpt : public StrCatOpt {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Verify the "strncat" function prototype.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 3 ||<br class="">
- FT->getReturnType() != B.getInt8PtrTy() ||<br class="">
- FT->getParamType(0) != FT->getReturnType() ||<br class="">
- FT->getParamType(1) != FT->getReturnType() ||<br class="">
- !FT->getParamType(2)->isIntegerTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- // Extract some information from the instruction<br class="">
- Value *Dst = CI->getArgOperand(0);<br class="">
- Value *Src = CI->getArgOperand(1);<br class="">
- uint64_t Len;<br class="">
-<br class="">
- // We don't do anything if length is not constant<br class="">
- if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))<br class="">
- Len = LengthArg->getZExtValue();<br class="">
- else<br class="">
- return nullptr;<br class="">
-<br class="">
- // See if we can get the length of the input string.<br class="">
- uint64_t SrcLen = GetStringLength(Src);<br class="">
- if (SrcLen == 0) return nullptr;<br class="">
- --SrcLen; // Unbias length.<br class="">
-<br class="">
- // Handle the simple, do-nothing cases:<br class="">
- // strncat(x, "", c) -> x<br class="">
- // strncat(x, c, 0) -> x<br class="">
- if (SrcLen == 0 || Len == 0) return Dst;<br class="">
+Value *LibCallSimplifier::optimizeStrNCpyChk(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ StringRef Name = Callee->getName();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ LLVMContext &Context = CI->getContext();<br class="">
+<br class="">
+ // Check if this has the right signature.<br class="">
+ if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ FT->getParamType(0) != FT->getParamType(1) ||<br class="">
+ FT->getParamType(0) != Type::getInt8PtrTy(Context) ||<br class="">
+ !FT->getParamType(2)->isIntegerTy() ||<br class="">
+ FT->getParamType(3) != DL->getIntPtrType(Context))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ if (isFortifiedCallFoldable(CI, 3, 2, false)) {<br class="">
+ Value *Ret =<br class="">
+ EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
+ CI->getArgOperand(2), B, DL, TLI, Name.substr(2, 7));<br class="">
+ return Ret;<br class="">
+ }<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
+//===----------------------------------------------------------------------===//<br class="">
+// String and Memory Library Call Optimizations<br class="">
+//===----------------------------------------------------------------------===//<br class="">
<br class="">
- // We don't optimize this case<br class="">
- if (Len < SrcLen) return nullptr;<br class="">
+Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Verify the "strcat" function prototype.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2||<br class="">
+ FT->getReturnType() != B.getInt8PtrTy() ||<br class="">
+ FT->getParamType(0) != FT->getReturnType() ||<br class="">
+ FT->getParamType(1) != FT->getReturnType())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // Extract some information from the instruction<br class="">
+ Value *Dst = CI->getArgOperand(0);<br class="">
+ Value *Src = CI->getArgOperand(1);<br class="">
+<br class="">
+ // See if we can get the length of the input string.<br class="">
+ uint64_t Len = GetStringLength(Src);<br class="">
+ if (Len == 0)<br class="">
+ return nullptr;<br class="">
+ --Len; // Unbias length.<br class="">
<br class="">
- // strncat(x, s, c) -> strcat(x, s)<br class="">
- // s is constant so the strcat can be optimized further<br class="">
- return emitStrLenMemCpy(Src, Dst, SrcLen, B);<br class="">
- }<br class="">
-};<br class="">
-<br class="">
-struct StrChrOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Verify the "strchr" function prototype.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 ||<br class="">
- FT->getReturnType() != B.getInt8PtrTy() ||<br class="">
- FT->getParamType(0) != FT->getReturnType() ||<br class="">
- !FT->getParamType(1)->isIntegerTy(32))<br class="">
- return nullptr;<br class="">
-<br class="">
- Value *SrcStr = CI->getArgOperand(0);<br class="">
-<br class="">
- // If the second operand is non-constant, see if we can compute the length<br class="">
- // of the input string and turn this into memchr.<br class="">
- ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));<br class="">
- if (!CharC) {<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
+ // Handle the simple, do-nothing case: strcat(x, "") -> x<br class="">
+ if (Len == 0)<br class="">
+ return Dst;<br class="">
<br class="">
- uint64_t Len = GetStringLength(SrcStr);<br class="">
- if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.<br class="">
- return nullptr;<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
+ return nullptr;<br class="">
<br class="">
- return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.<br class="">
- ConstantInt::get(DL->getIntPtrType(*Context), Len),<br class="">
- B, DL, TLI);<br class="">
- }<br class="">
+ return emitStrLenMemCpy(Src, Dst, Len, B);<br class="">
+}<br class="">
<br class="">
- // Otherwise, the character is a constant, see if the first argument is<br class="">
- // a string literal. If so, we can constant fold.<br class="">
- StringRef Str;<br class="">
- if (!getConstantStringInfo(SrcStr, Str)) {<br class="">
- if (DL && CharC->isZero()) // strchr(p, 0) -> p + strlen(p)<br class="">
- return B.CreateGEP(SrcStr, EmitStrLen(SrcStr, B, DL, TLI), "strchr");<br class="">
- return nullptr;<br class="">
- }<br class="">
+Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,<br class="">
+ IRBuilder<> &B) {<br class="">
+ // We need to find the end of the destination string. That's where the<br class="">
+ // memory is to be moved to. We just generate a call to strlen.<br class="">
+ Value *DstLen = EmitStrLen(Dst, B, DL, TLI);<br class="">
+ if (!DstLen)<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // Now that we have the destination's length, we must index into the<br class="">
+ // destination's pointer to get the actual memcpy destination (end of<br class="">
+ // the string .. we're concatenating).<br class="">
+ Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");<br class="">
+<br class="">
+ // We have enough information to now generate the memcpy call to do the<br class="">
+ // concatenation for us. Make a memcpy to copy the nul byte with align = 1.<br class="">
+ B.CreateMemCpy(<br class="">
+ CpyDst, Src,<br class="">
+ ConstantInt::get(DL->getIntPtrType(Src->getContext()), Len + 1), 1);<br class="">
+ return Dst;<br class="">
+}<br class="">
<br class="">
- // Compute the offset, make sure to handle the case when we're searching for<br class="">
- // zero (a weird way to spell strlen).<br class="">
- size_t I = (0xFF & CharC->getSExtValue()) == 0 ?<br class="">
- Str.size() : Str.find(CharC->getSExtValue());<br class="">
- if (I == StringRef::npos) // Didn't find the char. strchr returns null.<br class="">
- return Constant::getNullValue(CI->getType());<br class="">
+Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Verify the "strncat" function prototype.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 3 || FT->getReturnType() != B.getInt8PtrTy() ||<br class="">
+ FT->getParamType(0) != FT->getReturnType() ||<br class="">
+ FT->getParamType(1) != FT->getReturnType() ||<br class="">
+ !FT->getParamType(2)->isIntegerTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // Extract some information from the instruction<br class="">
+ Value *Dst = CI->getArgOperand(0);<br class="">
+ Value *Src = CI->getArgOperand(1);<br class="">
+ uint64_t Len;<br class="">
+<br class="">
+ // We don't do anything if length is not constant<br class="">
+ if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))<br class="">
+ Len = LengthArg->getZExtValue();<br class="">
+ else<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // See if we can get the length of the input string.<br class="">
+ uint64_t SrcLen = GetStringLength(Src);<br class="">
+ if (SrcLen == 0)<br class="">
+ return nullptr;<br class="">
+ --SrcLen; // Unbias length.<br class="">
+<br class="">
+ // Handle the simple, do-nothing cases:<br class="">
+ // strncat(x, "", c) -> x<br class="">
+ // strncat(x, c, 0) -> x<br class="">
+ if (SrcLen == 0 || Len == 0)<br class="">
+ return Dst;<br class="">
<br class="">
- // strchr(s+n,c) -> gep(s+n+i,c)<br class="">
- return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");<br class="">
- }<br class="">
-};<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // We don't optimize this case<br class="">
+ if (Len < SrcLen)<br class="">
+ return nullptr;<br class="">
<br class="">
-struct StrRChrOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Verify the "strrchr" function prototype.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 ||<br class="">
- FT->getReturnType() != B.getInt8PtrTy() ||<br class="">
- FT->getParamType(0) != FT->getReturnType() ||<br class="">
- !FT->getParamType(1)->isIntegerTy(32))<br class="">
- return nullptr;<br class="">
-<br class="">
- Value *SrcStr = CI->getArgOperand(0);<br class="">
- ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));<br class="">
-<br class="">
- // Cannot fold anything if we're not looking for a constant.<br class="">
- if (!CharC)<br class="">
- return nullptr;<br class="">
-<br class="">
- StringRef Str;<br class="">
- if (!getConstantStringInfo(SrcStr, Str)) {<br class="">
- // strrchr(s, 0) -> strchr(s, 0)<br class="">
- if (DL && CharC->isZero())<br class="">
- return EmitStrChr(SrcStr, '\0', B, DL, TLI);<br class="">
+ // strncat(x, s, c) -> strcat(x, s)<br class="">
+ // s is constant so the strcat can be optimized further<br class="">
+ return emitStrLenMemCpy(Src, Dst, SrcLen, B);<br class="">
+}<br class="">
+<br class="">
+Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Verify the "strchr" function prototype.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || FT->getReturnType() != B.getInt8PtrTy() ||<br class="">
+ FT->getParamType(0) != FT->getReturnType() ||<br class="">
+ !FT->getParamType(1)->isIntegerTy(32))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ Value *SrcStr = CI->getArgOperand(0);<br class="">
+<br class="">
+ // If the second operand is non-constant, see if we can compute the length<br class="">
+ // of the input string and turn this into memchr.<br class="">
+ ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));<br class="">
+ if (!CharC) {<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
return nullptr;<br class="">
- }<br class="">
<br class="">
- // Compute the offset.<br class="">
- size_t I = (0xFF & CharC->getSExtValue()) == 0 ?<br class="">
- Str.size() : Str.rfind(CharC->getSExtValue());<br class="">
- if (I == StringRef::npos) // Didn't find the char. Return null.<br class="">
- return Constant::getNullValue(CI->getType());<br class="">
+ uint64_t Len = GetStringLength(SrcStr);<br class="">
+ if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32)) // memchr needs i32.<br class="">
+ return nullptr;<br class="">
<br class="">
- // strrchr(s+n,c) -> gep(s+n+i,c)<br class="">
- return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");<br class="">
+ return EmitMemChr(<br class="">
+ SrcStr, CI->getArgOperand(1), // include nul.<br class="">
+ ConstantInt::get(DL->getIntPtrType(CI->getContext()), Len), B, DL, TLI);<br class="">
}<br class="">
-};<br class="">
-<br class="">
-struct StrCmpOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Verify the "strcmp" function prototype.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 ||<br class="">
- !FT->getReturnType()->isIntegerTy(32) ||<br class="">
- FT->getParamType(0) != FT->getParamType(1) ||<br class="">
- FT->getParamType(0) != B.getInt8PtrTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);<br class="">
- if (Str1P == Str2P) // strcmp(x,x) -> 0<br class="">
- return ConstantInt::get(CI->getType(), 0);<br class="">
-<br class="">
- StringRef Str1, Str2;<br class="">
- bool HasStr1 = getConstantStringInfo(Str1P, Str1);<br class="">
- bool HasStr2 = getConstantStringInfo(Str2P, Str2);<br class="">
-<br class="">
- // strcmp(x, y) -> cnst (if both x and y are constant strings)<br class="">
- if (HasStr1 && HasStr2)<br class="">
- return ConstantInt::get(CI->getType(), Str1.compare(Str2));<br class="">
-<br class="">
- if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x<br class="">
- return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),<br class="">
- CI->getType()));<br class="">
-<br class="">
- if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x<br class="">
- return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());<br class="">
-<br class="">
- // strcmp(P, "x") -> memcmp(P, "x", 2)<br class="">
- uint64_t Len1 = GetStringLength(Str1P);<br class="">
- uint64_t Len2 = GetStringLength(Str2P);<br class="">
- if (Len1 && Len2) {<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
-<br class="">
- return EmitMemCmp(Str1P, Str2P,<br class="">
- ConstantInt::get(DL->getIntPtrType(*Context),<br class="">
- std::min(Len1, Len2)), B, DL, TLI);<br class="">
- }<br class="">
<br class="">
+ // Otherwise, the character is a constant, see if the first argument is<br class="">
+ // a string literal. If so, we can constant fold.<br class="">
+ StringRef Str;<br class="">
+ if (!getConstantStringInfo(SrcStr, Str)) {<br class="">
+ if (DL && CharC->isZero()) // strchr(p, 0) -> p + strlen(p)<br class="">
+ return B.CreateGEP(SrcStr, EmitStrLen(SrcStr, B, DL, TLI), "strchr");<br class="">
return nullptr;<br class="">
}<br class="">
-};<br class="">
<br class="">
-struct StrNCmpOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Verify the "strncmp" function prototype.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 3 ||<br class="">
- !FT->getReturnType()->isIntegerTy(32) ||<br class="">
- FT->getParamType(0) != FT->getParamType(1) ||<br class="">
- FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
- !FT->getParamType(2)->isIntegerTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);<br class="">
- if (Str1P == Str2P) // strncmp(x,x,n) -> 0<br class="">
- return ConstantInt::get(CI->getType(), 0);<br class="">
-<br class="">
- // Get the length argument if it is constant.<br class="">
- uint64_t Length;<br class="">
- if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))<br class="">
- Length = LengthArg->getZExtValue();<br class="">
- else<br class="">
- return nullptr;<br class="">
-<br class="">
- if (Length == 0) // strncmp(x,y,0) -> 0<br class="">
- return ConstantInt::get(CI->getType(), 0);<br class="">
-<br class="">
- if (DL && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)<br class="">
- return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI);<br class="">
-<br class="">
- StringRef Str1, Str2;<br class="">
- bool HasStr1 = getConstantStringInfo(Str1P, Str1);<br class="">
- bool HasStr2 = getConstantStringInfo(Str2P, Str2);<br class="">
-<br class="">
- // strncmp(x, y) -> cnst (if both x and y are constant strings)<br class="">
- if (HasStr1 && HasStr2) {<br class="">
- StringRef SubStr1 = Str1.substr(0, Length);<br class="">
- StringRef SubStr2 = Str2.substr(0, Length);<br class="">
- return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));<br class="">
- }<br class="">
+ // Compute the offset, make sure to handle the case when we're searching for<br class="">
+ // zero (a weird way to spell strlen).<br class="">
+ size_t I = (0xFF & CharC->getSExtValue()) == 0<br class="">
+ ? Str.size()<br class="">
+ : Str.find(CharC->getSExtValue());<br class="">
+ if (I == StringRef::npos) // Didn't find the char. strchr returns null.<br class="">
+ return Constant::getNullValue(CI->getType());<br class="">
<br class="">
- if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x<br class="">
- return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),<br class="">
- CI->getType()));<br class="">
+ // strchr(s+n,c) -> gep(s+n+i,c)<br class="">
+ return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");<br class="">
+}<br class="">
<br class="">
- if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x<br class="">
- return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());<br class="">
+Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Verify the "strrchr" function prototype.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || FT->getReturnType() != B.getInt8PtrTy() ||<br class="">
+ FT->getParamType(0) != FT->getReturnType() ||<br class="">
+ !FT->getParamType(1)->isIntegerTy(32))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ Value *SrcStr = CI->getArgOperand(0);<br class="">
+ ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));<br class="">
+<br class="">
+ // Cannot fold anything if we're not looking for a constant.<br class="">
+ if (!CharC)<br class="">
+ return nullptr;<br class="">
<br class="">
+ StringRef Str;<br class="">
+ if (!getConstantStringInfo(SrcStr, Str)) {<br class="">
+ // strrchr(s, 0) -> strchr(s, 0)<br class="">
+ if (DL && CharC->isZero())<br class="">
+ return EmitStrChr(SrcStr, '\0', B, DL, TLI);<br class="">
return nullptr;<br class="">
}<br class="">
-};<br class="">
<br class="">
-struct StrCpyOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Verify the "strcpy" function prototype.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 ||<br class="">
- FT->getReturnType() != FT->getParamType(0) ||<br class="">
- FT->getParamType(0) != FT->getParamType(1) ||<br class="">
- FT->getParamType(0) != B.getInt8PtrTy())<br class="">
- return nullptr;<br class="">
+ // Compute the offset.<br class="">
+ size_t I = (0xFF & CharC->getSExtValue()) == 0<br class="">
+ ? Str.size()<br class="">
+ : Str.rfind(CharC->getSExtValue());<br class="">
+ if (I == StringRef::npos) // Didn't find the char. Return null.<br class="">
+ return Constant::getNullValue(CI->getType());<br class="">
<br class="">
- Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);<br class="">
- if (Dst == Src) // strcpy(x,x) -> x<br class="">
- return Src;<br class="">
+ // strrchr(s+n,c) -> gep(s+n+i,c)<br class="">
+ return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");<br class="">
+}<br class="">
<br class="">
+Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Verify the "strcmp" function prototype.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || !FT->getReturnType()->isIntegerTy(32) ||<br class="">
+ FT->getParamType(0) != FT->getParamType(1) ||<br class="">
+ FT->getParamType(0) != B.getInt8PtrTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);<br class="">
+ if (Str1P == Str2P) // strcmp(x,x) -> 0<br class="">
+ return ConstantInt::get(CI->getType(), 0);<br class="">
+<br class="">
+ StringRef Str1, Str2;<br class="">
+ bool HasStr1 = getConstantStringInfo(Str1P, Str1);<br class="">
+ bool HasStr2 = getConstantStringInfo(Str2P, Str2);<br class="">
+<br class="">
+ // strcmp(x, y) -> cnst (if both x and y are constant strings)<br class="">
+ if (HasStr1 && HasStr2)<br class="">
+ return ConstantInt::get(CI->getType(), Str1.compare(Str2));<br class="">
+<br class="">
+ if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x<br class="">
+ return B.CreateNeg(<br class="">
+ B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()));<br class="">
+<br class="">
+ if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x<br class="">
+ return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());<br class="">
+<br class="">
+ // strcmp(P, "x") -> memcmp(P, "x", 2)<br class="">
+ uint64_t Len1 = GetStringLength(Str1P);<br class="">
+ uint64_t Len2 = GetStringLength(Str2P);<br class="">
+ if (Len1 && Len2) {<br class="">
// These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
-<br class="">
- // See if we can get the length of the input string.<br class="">
- uint64_t Len = GetStringLength(Src);<br class="">
- if (Len == 0) return nullptr;<br class="">
+ if (!DL)<br class="">
+ return nullptr;<br class="">
<br class="">
- // We have enough information to now generate the memcpy call to do the<br class="">
- // copy for us. Make a memcpy to copy the nul byte with align = 1.<br class="">
- B.CreateMemCpy(Dst, Src,<br class="">
- ConstantInt::get(DL->getIntPtrType(*Context), Len), 1);<br class="">
- return Dst;<br class="">
+ return EmitMemCmp(Str1P, Str2P,<br class="">
+ ConstantInt::get(DL->getIntPtrType(CI->getContext()),<br class="">
+ std::min(Len1, Len2)),<br class="">
+ B, DL, TLI);<br class="">
}<br class="">
-};<br class="">
<br class="">
-struct StpCpyOpt: public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Verify the "stpcpy" function prototype.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 ||<br class="">
- FT->getReturnType() != FT->getParamType(0) ||<br class="">
- FT->getParamType(0) != FT->getParamType(1) ||<br class="">
- FT->getParamType(0) != B.getInt8PtrTy())<br class="">
- return nullptr;<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
+Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Verify the "strncmp" function prototype.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 3 || !FT->getReturnType()->isIntegerTy(32) ||<br class="">
+ FT->getParamType(0) != FT->getParamType(1) ||<br class="">
+ FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
+ !FT->getParamType(2)->isIntegerTy())<br class="">
+ return nullptr;<br class="">
<br class="">
- Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);<br class="">
- if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)<br class="">
- Value *StrLen = EmitStrLen(Src, B, DL, TLI);<br class="">
- return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : nullptr;<br class="">
- }<br class="">
+ Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);<br class="">
+ if (Str1P == Str2P) // strncmp(x,x,n) -> 0<br class="">
+ return ConstantInt::get(CI->getType(), 0);<br class="">
<br class="">
- // See if we can get the length of the input string.<br class="">
- uint64_t Len = GetStringLength(Src);<br class="">
- if (Len == 0) return nullptr;<br class="">
+ // Get the length argument if it is constant.<br class="">
+ uint64_t Length;<br class="">
+ if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))<br class="">
+ Length = LengthArg->getZExtValue();<br class="">
+ else<br class="">
+ return nullptr;<br class="">
<br class="">
- Type *PT = FT->getParamType(0);<br class="">
- Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len);<br class="">
- Value *DstEnd = B.CreateGEP(Dst,<br class="">
- ConstantInt::get(DL->getIntPtrType(PT),<br class="">
- Len - 1));<br class="">
-<br class="">
- // We have enough information to now generate the memcpy call to do the<br class="">
- // copy for us. Make a memcpy to copy the nul byte with align = 1.<br class="">
- B.CreateMemCpy(Dst, Src, LenV, 1);<br class="">
- return DstEnd;<br class="">
- }<br class="">
-};<br class="">
+ if (Length == 0) // strncmp(x,y,0) -> 0<br class="">
+ return ConstantInt::get(CI->getType(), 0);<br class="">
<br class="">
-struct StrNCpyOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- FT->getParamType(0) != FT->getParamType(1) ||<br class="">
- FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
- !FT->getParamType(2)->isIntegerTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- Value *Dst = CI->getArgOperand(0);<br class="">
- Value *Src = CI->getArgOperand(1);<br class="">
- Value *LenOp = CI->getArgOperand(2);<br class="">
-<br class="">
- // See if we can get the length of the input string.<br class="">
- uint64_t SrcLen = GetStringLength(Src);<br class="">
- if (SrcLen == 0) return nullptr;<br class="">
- --SrcLen;<br class="">
-<br class="">
- if (SrcLen == 0) {<br class="">
- // strncpy(x, "", y) -> memset(x, '\0', y, 1)<br class="">
- B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);<br class="">
- return Dst;<br class="">
- }<br class="">
+ if (DL && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)<br class="">
+ return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI);<br class="">
<br class="">
- uint64_t Len;<br class="">
- if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))<br class="">
- Len = LengthArg->getZExtValue();<br class="">
- else<br class="">
- return nullptr;<br class="">
+ StringRef Str1, Str2;<br class="">
+ bool HasStr1 = getConstantStringInfo(Str1P, Str1);<br class="">
+ bool HasStr2 = getConstantStringInfo(Str2P, Str2);<br class="">
<br class="">
- if (Len == 0) return Dst; // strncpy(x, y, 0) -> x<br class="">
+ // strncmp(x, y) -> cnst (if both x and y are constant strings)<br class="">
+ if (HasStr1 && HasStr2) {<br class="">
+ StringRef SubStr1 = Str1.substr(0, Length);<br class="">
+ StringRef SubStr2 = Str2.substr(0, Length);<br class="">
+ return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));<br class="">
+ }<br class="">
<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
+ if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x<br class="">
+ return B.CreateNeg(<br class="">
+ B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()));<br class="">
<br class="">
- // Let strncpy handle the zero padding<br class="">
- if (Len > SrcLen+1) return nullptr;<br class="">
+ if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x<br class="">
+ return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());<br class="">
<br class="">
- Type *PT = FT->getParamType(0);<br class="">
- // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]<br class="">
- B.CreateMemCpy(Dst, Src,<br class="">
- ConstantInt::get(DL->getIntPtrType(PT), Len), 1);<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- return Dst;<br class="">
- }<br class="">
-};<br class="">
+Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Verify the "strcpy" function prototype.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ FT->getParamType(0) != FT->getParamType(1) ||<br class="">
+ FT->getParamType(0) != B.getInt8PtrTy())<br class="">
+ return nullptr;<br class="">
<br class="">
-struct StrLenOpt : public LibCallOptimization {<br class="">
- bool ignoreCallingConv() override { return true; }<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 1 ||<br class="">
- FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
- !FT->getReturnType()->isIntegerTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- Value *Src = CI->getArgOperand(0);<br class="">
-<br class="">
- // Constant folding: strlen("xyz") -> 3<br class="">
- if (uint64_t Len = GetStringLength(Src))<br class="">
- return ConstantInt::get(CI->getType(), Len-1);<br class="">
-<br class="">
- // strlen(x?"foo":"bars") --> x ? 3 : 4<br class="">
- if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {<br class="">
- uint64_t LenTrue = GetStringLength(SI->getTrueValue());<br class="">
- uint64_t LenFalse = GetStringLength(SI->getFalseValue());<br class="">
- if (LenTrue && LenFalse) {<br class="">
- emitOptimizationRemark(*Context, "simplify-libcalls", *Caller,<br class="">
- SI->getDebugLoc(),<br class="">
- "folded strlen(select) to select of constants");<br class="">
- return B.CreateSelect(SI->getCondition(),<br class="">
- ConstantInt::get(CI->getType(), LenTrue-1),<br class="">
- ConstantInt::get(CI->getType(), LenFalse-1));<br class="">
- }<br class="">
- }<br class="">
+ Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);<br class="">
+ if (Dst == Src) // strcpy(x,x) -> x<br class="">
+ return Src;<br class="">
<br class="">
- // strlen(x) != 0 --> *x != 0<br class="">
- // strlen(x) == 0 --> *x == 0<br class="">
- if (isOnlyUsedInZeroEqualityComparison(CI))<br class="">
- return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
+ return nullptr;<br class="">
<br class="">
+ // See if we can get the length of the input string.<br class="">
+ uint64_t Len = GetStringLength(Src);<br class="">
+ if (Len == 0)<br class="">
return nullptr;<br class="">
- }<br class="">
-};<br class="">
<br class="">
-struct StrPBrkOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 ||<br class="">
- FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
- FT->getParamType(1) != FT->getParamType(0) ||<br class="">
- FT->getReturnType() != FT->getParamType(0))<br class="">
- return nullptr;<br class="">
+ // We have enough information to now generate the memcpy call to do the<br class="">
+ // copy for us. Make a memcpy to copy the nul byte with align = 1.<br class="">
+ B.CreateMemCpy(Dst, Src,<br class="">
+ ConstantInt::get(DL->getIntPtrType(CI->getContext()), Len), 1);<br class="">
+ return Dst;<br class="">
+}<br class="">
<br class="">
- StringRef S1, S2;<br class="">
- bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);<br class="">
- bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);<br class="">
+Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Verify the "stpcpy" function prototype.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ FT->getParamType(0) != FT->getParamType(1) ||<br class="">
+ FT->getParamType(0) != B.getInt8PtrTy())<br class="">
+ return nullptr;<br class="">
<br class="">
- // strpbrk(s, "") -> NULL<br class="">
- // strpbrk("", s) -> NULL<br class="">
- if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))<br class="">
- return Constant::getNullValue(CI->getType());<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
+ return nullptr;<br class="">
<br class="">
- // Constant folding.<br class="">
- if (HasS1 && HasS2) {<br class="">
- size_t I = S1.find_first_of(S2);<br class="">
- if (I == StringRef::npos) // No match.<br class="">
- return Constant::getNullValue(CI->getType());<br class="">
+ Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);<br class="">
+ if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)<br class="">
+ Value *StrLen = EmitStrLen(Src, B, DL, TLI);<br class="">
+ return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : nullptr;<br class="">
+ }<br class="">
<br class="">
- return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");<br class="">
- }<br class="">
+ // See if we can get the length of the input string.<br class="">
+ uint64_t Len = GetStringLength(Src);<br class="">
+ if (Len == 0)<br class="">
+ return nullptr;<br class="">
<br class="">
- // strpbrk(s, "a") -> strchr(s, 'a')<br class="">
- if (DL && HasS2 && S2.size() == 1)<br class="">
- return EmitStrChr(CI->getArgOperand(0), S2[0], B, DL, TLI);<br class="">
+ Type *PT = FT->getParamType(0);<br class="">
+ Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len);<br class="">
+ Value *DstEnd =<br class="">
+ B.CreateGEP(Dst, ConstantInt::get(DL->getIntPtrType(PT), Len - 1));<br class="">
<br class="">
- return nullptr;<br class="">
+ // We have enough information to now generate the memcpy call to do the<br class="">
+ // copy for us. Make a memcpy to copy the nul byte with align = 1.<br class="">
+ B.CreateMemCpy(Dst, Src, LenV, 1);<br class="">
+ return DstEnd;<br class="">
+}<br class="">
+<br class="">
+Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ FT->getParamType(0) != FT->getParamType(1) ||<br class="">
+ FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
+ !FT->getParamType(2)->isIntegerTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ Value *Dst = CI->getArgOperand(0);<br class="">
+ Value *Src = CI->getArgOperand(1);<br class="">
+ Value *LenOp = CI->getArgOperand(2);<br class="">
+<br class="">
+ // See if we can get the length of the input string.<br class="">
+ uint64_t SrcLen = GetStringLength(Src);<br class="">
+ if (SrcLen == 0)<br class="">
+ return nullptr;<br class="">
+ --SrcLen;<br class="">
+<br class="">
+ if (SrcLen == 0) {<br class="">
+ // strncpy(x, "", y) -> memset(x, '\0', y, 1)<br class="">
+ B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);<br class="">
+ return Dst;<br class="">
}<br class="">
-};<br class="">
<br class="">
-struct StrToOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||<br class="">
- !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isPointerTy())<br class="">
- return nullptr;<br class="">
+ uint64_t Len;<br class="">
+ if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))<br class="">
+ Len = LengthArg->getZExtValue();<br class="">
+ else<br class="">
+ return nullptr;<br class="">
<br class="">
- Value *EndPtr = CI->getArgOperand(1);<br class="">
- if (isa<ConstantPointerNull>(EndPtr)) {<br class="">
- // With a null EndPtr, this function won't capture the main argument.<br class="">
- // It would be readonly too, except that it still may write to errno.<br class="">
- CI->addAttribute(1, Attribute::NoCapture);<br class="">
- }<br class="">
+ if (Len == 0)<br class="">
+ return Dst; // strncpy(x, y, 0) -> x<br class="">
<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
return nullptr;<br class="">
- }<br class="">
-};<br class="">
<br class="">
-struct StrSpnOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 ||<br class="">
- FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
- FT->getParamType(1) != FT->getParamType(0) ||<br class="">
- !FT->getReturnType()->isIntegerTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- StringRef S1, S2;<br class="">
- bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);<br class="">
- bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);<br class="">
-<br class="">
- // strspn(s, "") -> 0<br class="">
- // strspn("", s) -> 0<br class="">
- if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))<br class="">
- return Constant::getNullValue(CI->getType());<br class="">
+ // Let strncpy handle the zero padding<br class="">
+ if (Len > SrcLen + 1)<br class="">
+ return nullptr;<br class="">
<br class="">
- // Constant folding.<br class="">
- if (HasS1 && HasS2) {<br class="">
- size_t Pos = S1.find_first_not_of(S2);<br class="">
- if (Pos == StringRef::npos) Pos = S1.size();<br class="">
- return ConstantInt::get(CI->getType(), Pos);<br class="">
- }<br class="">
+ Type *PT = FT->getParamType(0);<br class="">
+ // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]<br class="">
+ B.CreateMemCpy(Dst, Src, ConstantInt::get(DL->getIntPtrType(PT), Len), 1);<br class="">
<br class="">
- return nullptr;<br class="">
+ return Dst;<br class="">
+}<br class="">
+<br class="">
+Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 1 || FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
+ !FT->getReturnType()->isIntegerTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ Value *Src = CI->getArgOperand(0);<br class="">
+<br class="">
+ // Constant folding: strlen("xyz") -> 3<br class="">
+ if (uint64_t Len = GetStringLength(Src))<br class="">
+ return ConstantInt::get(CI->getType(), Len - 1);<br class="">
+<br class="">
+ // strlen(x?"foo":"bars") --> x ? 3 : 4<br class="">
+ if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {<br class="">
+ uint64_t LenTrue = GetStringLength(SI->getTrueValue());<br class="">
+ uint64_t LenFalse = GetStringLength(SI->getFalseValue());<br class="">
+ if (LenTrue && LenFalse) {<br class="">
+ Function *Caller = CI->getParent()->getParent();<br class="">
+ emitOptimizationRemark(CI->getContext(), "simplify-libcalls", *Caller,<br class="">
+ SI->getDebugLoc(),<br class="">
+ "folded strlen(select) to select of constants");<br class="">
+ return B.CreateSelect(SI->getCondition(),<br class="">
+ ConstantInt::get(CI->getType(), LenTrue - 1),<br class="">
+ ConstantInt::get(CI->getType(), LenFalse - 1));<br class="">
+ }<br class="">
}<br class="">
-};<br class="">
<br class="">
-struct StrCSpnOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 ||<br class="">
- FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
- FT->getParamType(1) != FT->getParamType(0) ||<br class="">
- !FT->getReturnType()->isIntegerTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- StringRef S1, S2;<br class="">
- bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);<br class="">
- bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);<br class="">
+ // strlen(x) != 0 --> *x != 0<br class="">
+ // strlen(x) == 0 --> *x == 0<br class="">
+ if (isOnlyUsedInZeroEqualityComparison(CI))<br class="">
+ return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());<br class="">
+<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- // strcspn("", s) -> 0<br class="">
- if (HasS1 && S1.empty())<br class="">
+Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
+ FT->getParamType(1) != FT->getParamType(0) ||<br class="">
+ FT->getReturnType() != FT->getParamType(0))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ StringRef S1, S2;<br class="">
+ bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);<br class="">
+ bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);<br class="">
+<br class="">
+ // strpbrk(s, "") -> NULL<br class="">
+ // strpbrk("", s) -> NULL<br class="">
+ if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))<br class="">
+ return Constant::getNullValue(CI->getType());<br class="">
+<br class="">
+ // Constant folding.<br class="">
+ if (HasS1 && HasS2) {<br class="">
+ size_t I = S1.find_first_of(S2);<br class="">
+ if (I == StringRef::npos) // No match.<br class="">
return Constant::getNullValue(CI->getType());<br class="">
<br class="">
- // Constant folding.<br class="">
- if (HasS1 && HasS2) {<br class="">
- size_t Pos = S1.find_first_of(S2);<br class="">
- if (Pos == StringRef::npos) Pos = S1.size();<br class="">
- return ConstantInt::get(CI->getType(), Pos);<br class="">
- }<br class="">
+ return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");<br class="">
+ }<br class="">
<br class="">
- // strcspn(s, "") -> strlen(s)<br class="">
- if (DL && HasS2 && S2.empty())<br class="">
- return EmitStrLen(CI->getArgOperand(0), B, DL, TLI);<br class="">
+ // strpbrk(s, "a") -> strchr(s, 'a')<br class="">
+ if (DL && HasS2 && S2.size() == 1)<br class="">
+ return EmitStrChr(CI->getArgOperand(0), S2[0], B, DL, TLI);<br class="">
<br class="">
+ return nullptr;<br class="">
+}<br class="">
+<br class="">
+Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||<br class="">
+ !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isPointerTy())<br class="">
return nullptr;<br class="">
+<br class="">
+ Value *EndPtr = CI->getArgOperand(1);<br class="">
+ if (isa<ConstantPointerNull>(EndPtr)) {<br class="">
+ // With a null EndPtr, this function won't capture the main argument.<br class="">
+ // It would be readonly too, except that it still may write to errno.<br class="">
+ CI->addAttribute(1, Attribute::NoCapture);<br class="">
}<br class="">
-};<br class="">
<br class="">
-struct StrStrOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 ||<br class="">
- !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isPointerTy() ||<br class="">
- !FT->getReturnType()->isPointerTy())<br class="">
- return nullptr;<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- // fold strstr(x, x) -> x.<br class="">
- if (CI->getArgOperand(0) == CI->getArgOperand(1))<br class="">
- return B.CreateBitCast(CI->getArgOperand(0), CI->getType());<br class="">
+Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
+ FT->getParamType(1) != FT->getParamType(0) ||<br class="">
+ !FT->getReturnType()->isIntegerTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ StringRef S1, S2;<br class="">
+ bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);<br class="">
+ bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);<br class="">
+<br class="">
+ // strspn(s, "") -> 0<br class="">
+ // strspn("", s) -> 0<br class="">
+ if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))<br class="">
+ return Constant::getNullValue(CI->getType());<br class="">
+<br class="">
+ // Constant folding.<br class="">
+ if (HasS1 && HasS2) {<br class="">
+ size_t Pos = S1.find_first_not_of(S2);<br class="">
+ if (Pos == StringRef::npos)<br class="">
+ Pos = S1.size();<br class="">
+ return ConstantInt::get(CI->getType(), Pos);<br class="">
+ }<br class="">
<br class="">
- // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0<br class="">
- if (DL && isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {<br class="">
- Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, DL, TLI);<br class="">
- if (!StrLen)<br class="">
- return nullptr;<br class="">
- Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
- StrLen, B, DL, TLI);<br class="">
- if (!StrNCmp)<br class="">
- return nullptr;<br class="">
- for (auto UI = CI->user_begin(), UE = CI->user_end(); UI != UE;) {<br class="">
- ICmpInst *Old = cast<ICmpInst>(*UI++);<br class="">
- Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,<br class="">
- ConstantInt::getNullValue(StrNCmp->getType()),<br class="">
- "cmp");<br class="">
- LCS->replaceAllUsesWith(Old, Cmp);<br class="">
- }<br class="">
- return CI;<br class="">
- }<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- // See if either input string is a constant string.<br class="">
- StringRef SearchStr, ToFindStr;<br class="">
- bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);<br class="">
- bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);<br class="">
-<br class="">
- // fold strstr(x, "") -> x.<br class="">
- if (HasStr2 && ToFindStr.empty())<br class="">
- return B.CreateBitCast(CI->getArgOperand(0), CI->getType());<br class="">
-<br class="">
- // If both strings are known, constant fold it.<br class="">
- if (HasStr1 && HasStr2) {<br class="">
- size_t Offset = SearchStr.find(ToFindStr);<br class="">
-<br class="">
- if (Offset == StringRef::npos) // strstr("foo", "bar") -> null<br class="">
- return Constant::getNullValue(CI->getType());<br class="">
-<br class="">
- // strstr("abcd", "bc") -> gep((char*)"abcd", 1)<br class="">
- Value *Result = CastToCStr(CI->getArgOperand(0), B);<br class="">
- Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");<br class="">
- return B.CreateBitCast(Result, CI->getType());<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||<br class="">
+ FT->getParamType(1) != FT->getParamType(0) ||<br class="">
+ !FT->getReturnType()->isIntegerTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ StringRef S1, S2;<br class="">
+ bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);<br class="">
+ bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);<br class="">
+<br class="">
+ // strcspn("", s) -> 0<br class="">
+ if (HasS1 && S1.empty())<br class="">
+ return Constant::getNullValue(CI->getType());<br class="">
+<br class="">
+ // Constant folding.<br class="">
+ if (HasS1 && HasS2) {<br class="">
+ size_t Pos = S1.find_first_of(S2);<br class="">
+ if (Pos == StringRef::npos)<br class="">
+ Pos = S1.size();<br class="">
+ return ConstantInt::get(CI->getType(), Pos);<br class="">
+ }<br class="">
+<br class="">
+ // strcspn(s, "") -> strlen(s)<br class="">
+ if (DL && HasS2 && S2.empty())<br class="">
+ return EmitStrLen(CI->getArgOperand(0), B, DL, TLI);<br class="">
<br class="">
- // fold strstr(x, "y") -> strchr(x, 'y').<br class="">
- if (HasStr2 && ToFindStr.size() == 1) {<br class="">
- Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, DL, TLI);<br class="">
- return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr;<br class="">
- }<br class="">
+ return nullptr;<br class="">
+}<br class="">
+<br class="">
+Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isPointerTy() ||<br class="">
+ !FT->getReturnType()->isPointerTy())<br class="">
return nullptr;<br class="">
- }<br class="">
-};<br class="">
<br class="">
-struct MemCmpOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isPointerTy() ||<br class="">
- !FT->getReturnType()->isIntegerTy(32))<br class="">
+ // fold strstr(x, x) -> x.<br class="">
+ if (CI->getArgOperand(0) == CI->getArgOperand(1))<br class="">
+ return B.CreateBitCast(CI->getArgOperand(0), CI->getType());<br class="">
+<br class="">
+ // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0<br class="">
+ if (DL && isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {<br class="">
+ Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, DL, TLI);<br class="">
+ if (!StrLen)<br class="">
return nullptr;<br class="">
+ Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
+ StrLen, B, DL, TLI);<br class="">
+ if (!StrNCmp)<br class="">
+ return nullptr;<br class="">
+ for (auto UI = CI->user_begin(), UE = CI->user_end(); UI != UE;) {<br class="">
+ ICmpInst *Old = cast<ICmpInst>(*UI++);<br class="">
+ Value *Cmp =<br class="">
+ B.CreateICmp(Old->getPredicate(), StrNCmp,<br class="">
+ ConstantInt::getNullValue(StrNCmp->getType()), "cmp");<br class="">
+ replaceAllUsesWith(Old, Cmp);<br class="">
+ }<br class="">
+ return CI;<br class="">
+ }<br class="">
<br class="">
- Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);<br class="">
+ // See if either input string is a constant string.<br class="">
+ StringRef SearchStr, ToFindStr;<br class="">
+ bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);<br class="">
+ bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);<br class="">
<br class="">
- if (LHS == RHS) // memcmp(s,s,x) -> 0<br class="">
- return Constant::getNullValue(CI->getType());<br class="">
+ // fold strstr(x, "") -> x.<br class="">
+ if (HasStr2 && ToFindStr.empty())<br class="">
+ return B.CreateBitCast(CI->getArgOperand(0), CI->getType());<br class="">
<br class="">
- // Make sure we have a constant length.<br class="">
- ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));<br class="">
- if (!LenC) return nullptr;<br class="">
- uint64_t Len = LenC->getZExtValue();<br class="">
+ // If both strings are known, constant fold it.<br class="">
+ if (HasStr1 && HasStr2) {<br class="">
+ size_t Offset = SearchStr.find(ToFindStr);<br class="">
<br class="">
- if (Len == 0) // memcmp(s1,s2,0) -> 0<br class="">
+ if (Offset == StringRef::npos) // strstr("foo", "bar") -> null<br class="">
return Constant::getNullValue(CI->getType());<br class="">
<br class="">
- // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS<br class="">
- if (Len == 1) {<br class="">
- Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),<br class="">
- CI->getType(), "lhsv");<br class="">
- Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),<br class="">
- CI->getType(), "rhsv");<br class="">
- return B.CreateSub(LHSV, RHSV, "chardiff");<br class="">
- }<br class="">
+ // strstr("abcd", "bc") -> gep((char*)"abcd", 1)<br class="">
+ Value *Result = CastToCStr(CI->getArgOperand(0), B);<br class="">
+ Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");<br class="">
+ return B.CreateBitCast(Result, CI->getType());<br class="">
+ }<br class="">
<br class="">
- // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)<br class="">
- StringRef LHSStr, RHSStr;<br class="">
- if (getConstantStringInfo(LHS, LHSStr) &&<br class="">
- getConstantStringInfo(RHS, RHSStr)) {<br class="">
- // Make sure we're not reading out-of-bounds memory.<br class="">
- if (Len > LHSStr.size() || Len > RHSStr.size())<br class="">
- return nullptr;<br class="">
- // Fold the memcmp and normalize the result. This way we get consistent<br class="">
- // results across multiple platforms.<br class="">
- uint64_t Ret = 0;<br class="">
- int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);<br class="">
- if (Cmp < 0)<br class="">
- Ret = -1;<br class="">
- else if (Cmp > 0)<br class="">
- Ret = 1;<br class="">
- return ConstantInt::get(CI->getType(), Ret);<br class="">
- }<br class="">
+ // fold strstr(x, "y") -> strchr(x, 'y').<br class="">
+ if (HasStr2 && ToFindStr.size() == 1) {<br class="">
+ Value *StrChr = EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, DL, TLI);<br class="">
+ return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr;<br class="">
+ }<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- return nullptr;<br class="">
+Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isPointerTy() ||<br class="">
+ !FT->getReturnType()->isIntegerTy(32))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);<br class="">
+<br class="">
+ if (LHS == RHS) // memcmp(s,s,x) -> 0<br class="">
+ return Constant::getNullValue(CI->getType());<br class="">
+<br class="">
+ // Make sure we have a constant length.<br class="">
+ ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));<br class="">
+ if (!LenC)<br class="">
+ return nullptr;<br class="">
+ uint64_t Len = LenC->getZExtValue();<br class="">
+<br class="">
+ if (Len == 0) // memcmp(s1,s2,0) -> 0<br class="">
+ return Constant::getNullValue(CI->getType());<br class="">
+<br class="">
+ // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS<br class="">
+ if (Len == 1) {<br class="">
+ Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),<br class="">
+ CI->getType(), "lhsv");<br class="">
+ Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),<br class="">
+ CI->getType(), "rhsv");<br class="">
+ return B.CreateSub(LHSV, RHSV, "chardiff");<br class="">
+ }<br class="">
+<br class="">
+ // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)<br class="">
+ StringRef LHSStr, RHSStr;<br class="">
+ if (getConstantStringInfo(LHS, LHSStr) &&<br class="">
+ getConstantStringInfo(RHS, RHSStr)) {<br class="">
+ // Make sure we're not reading out-of-bounds memory.<br class="">
+ if (Len > LHSStr.size() || Len > RHSStr.size())<br class="">
+ return nullptr;<br class="">
+ // Fold the memcmp and normalize the result. This way we get consistent<br class="">
+ // results across multiple platforms.<br class="">
+ uint64_t Ret = 0;<br class="">
+ int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);<br class="">
+ if (Cmp < 0)<br class="">
+ Ret = -1;<br class="">
+ else if (Cmp > 0)<br class="">
+ Ret = 1;<br class="">
+ return ConstantInt::get(CI->getType(), Ret);<br class="">
}<br class="">
-};<br class="">
<br class="">
-struct MemCpyOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isPointerTy() ||<br class="">
- FT->getParamType(2) != DL->getIntPtrType(*Context))<br class="">
- return nullptr;<br class="">
+Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
+ return nullptr;<br class="">
<br class="">
- // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)<br class="">
- B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
- CI->getArgOperand(2), 1);<br class="">
- return CI->getArgOperand(0);<br class="">
- }<br class="">
-};<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isPointerTy() ||<br class="">
+ FT->getParamType(2) != DL->getIntPtrType(CI->getContext()))<br class="">
+ return nullptr;<br class="">
<br class="">
-struct MemMoveOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
+ // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)<br class="">
+ B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
+ CI->getArgOperand(2), 1);<br class="">
+ return CI->getArgOperand(0);<br class="">
+}<br class="">
<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isPointerTy() ||<br class="">
- FT->getParamType(2) != DL->getIntPtrType(*Context))<br class="">
- return nullptr;<br class="">
+Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
+ return nullptr;<br class="">
<br class="">
- // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)<br class="">
- B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
- CI->getArgOperand(2), 1);<br class="">
- return CI->getArgOperand(0);<br class="">
- }<br class="">
-};<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isPointerTy() ||<br class="">
+ FT->getParamType(2) != DL->getIntPtrType(CI->getContext()))<br class="">
+ return nullptr;<br class="">
<br class="">
-struct MemSetOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
+ // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)<br class="">
+ B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
+ CI->getArgOperand(2), 1);<br class="">
+ return CI->getArgOperand(0);<br class="">
+}<br class="">
<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isIntegerTy() ||<br class="">
- FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0)))<br class="">
- return nullptr;<br class="">
+Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
+ return nullptr;<br class="">
<br class="">
- // memset(p, v, n) -> llvm.memset(p, v, n, 1)<br class="">
- Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);<br class="">
- B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);<br class="">
- return CI->getArgOperand(0);<br class="">
- }<br class="">
-};<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isIntegerTy() ||<br class="">
+ FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0)))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // memset(p, v, n) -> llvm.memset(p, v, n, 1)<br class="">
+ Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);<br class="">
+ B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);<br class="">
+ return CI->getArgOperand(0);<br class="">
+}<br class="">
<br class="">
//===----------------------------------------------------------------------===//<br class="">
// Math Library Optimizations<br class="">
@@ -1111,935 +1025,847 @@ struct MemSetOpt : public LibCallOptimiz<br class="">
//===----------------------------------------------------------------------===//<br class="">
// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'<br class="">
<br class="">
-struct UnaryDoubleFPOpt : public LibCallOptimization {<br class="">
- bool CheckRetType;<br class="">
- UnaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||<br class="">
- !FT->getParamType(0)->isDoubleTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- if (CheckRetType) {<br class="">
- // Check if all the uses for function like 'sin' are converted to float.<br class="">
- for (User *U : CI->users()) {<br class="">
- FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);<br class="">
- if (!Cast || !Cast->getType()->isFloatTy())<br class="">
- return nullptr;<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B,<br class="">
+ bool CheckRetType) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||<br class="">
+ !FT->getParamType(0)->isDoubleTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ if (CheckRetType) {<br class="">
+ // Check if all the uses for function like 'sin' are converted to float.<br class="">
+ for (User *U : CI->users()) {<br class="">
+ FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);<br class="">
+ if (!Cast || !Cast->getType()->isFloatTy())<br class="">
+ return nullptr;<br class="">
}<br class="">
+ }<br class="">
<br class="">
- // If this is something like 'floor((double)floatval)', convert to floorf.<br class="">
- FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));<br class="">
- if (!Cast || !Cast->getOperand(0)->getType()->isFloatTy())<br class="">
- return nullptr;<br class="">
+ // If this is something like 'floor((double)floatval)', convert to floorf.<br class="">
+ FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));<br class="">
+ if (!Cast || !Cast->getOperand(0)->getType()->isFloatTy())<br class="">
+ return nullptr;<br class="">
<br class="">
- // floor((double)floatval) -> (double)floorf(floatval)<br class="">
- Value *V = Cast->getOperand(0);<br class="">
- V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());<br class="">
- return B.CreateFPExt(V, B.getDoubleTy());<br class="">
- }<br class="">
-};<br class="">
+ // floor((double)floatval) -> (double)floorf(floatval)<br class="">
+ Value *V = Cast->getOperand(0);<br class="">
+ V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());<br class="">
+ return B.CreateFPExt(V, B.getDoubleTy());<br class="">
+}<br class="">
<br class="">
// Double -> Float Shrinking Optimizations for Binary Functions like 'fmin/fmax'<br class="">
-struct BinaryDoubleFPOpt : public LibCallOptimization {<br class="">
- bool CheckRetType;<br class="">
- BinaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- // Just make sure this has 2 arguments of the same FP type, which match the<br class="">
- // result type.<br class="">
- if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- FT->getParamType(0) != FT->getParamType(1) ||<br class="">
- !FT->getParamType(0)->isFloatingPointTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- if (CheckRetType) {<br class="">
- // Check if all the uses for function like 'fmin/fmax' are converted to<br class="">
- // float.<br class="">
- for (User *U : CI->users()) {<br class="">
- FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);<br class="">
- if (!Cast || !Cast->getType()->isFloatTy())<br class="">
- return nullptr;<br class="">
- }<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ // Just make sure this has 2 arguments of the same FP type, which match the<br class="">
+ // result type.<br class="">
+ if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ FT->getParamType(0) != FT->getParamType(1) ||<br class="">
+ !FT->getParamType(0)->isFloatingPointTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // If this is something like 'fmin((double)floatval1, (double)floatval2)',<br class="">
+ // we convert it to fminf.<br class="">
+ FPExtInst *Cast1 = dyn_cast<FPExtInst>(CI->getArgOperand(0));<br class="">
+ FPExtInst *Cast2 = dyn_cast<FPExtInst>(CI->getArgOperand(1));<br class="">
+ if (!Cast1 || !Cast1->getOperand(0)->getType()->isFloatTy() || !Cast2 ||<br class="">
+ !Cast2->getOperand(0)->getType()->isFloatTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // fmin((double)floatval1, (double)floatval2)<br class="">
+ // -> (double)fmin(floatval1, floatval2)<br class="">
+ Value *V = nullptr;<br class="">
+ Value *V1 = Cast1->getOperand(0);<br class="">
+ Value *V2 = Cast2->getOperand(0);<br class="">
+ V = EmitBinaryFloatFnCall(V1, V2, Callee->getName(), B,<br class="">
+ Callee->getAttributes());<br class="">
+ return B.CreateFPExt(V, B.getDoubleTy());<br class="">
+}<br class="">
<br class="">
- // If this is something like 'fmin((double)floatval1, (double)floatval2)',<br class="">
- // we convert it to fminf.<br class="">
- FPExtInst *Cast1 = dyn_cast<FPExtInst>(CI->getArgOperand(0));<br class="">
- FPExtInst *Cast2 = dyn_cast<FPExtInst>(CI->getArgOperand(1));<br class="">
- if (!Cast1 || !Cast1->getOperand(0)->getType()->isFloatTy() ||<br class="">
- !Cast2 || !Cast2->getOperand(0)->getType()->isFloatTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- // fmin((double)floatval1, (double)floatval2)<br class="">
- // -> (double)fmin(floatval1, floatval2)<br class="">
- Value *V = nullptr;<br class="">
- Value *V1 = Cast1->getOperand(0);<br class="">
- Value *V2 = Cast2->getOperand(0);<br class="">
- V = EmitBinaryFloatFnCall(V1, V2, Callee->getName(), B,<br class="">
- Callee->getAttributes());<br class="">
- return B.CreateFPExt(V, B.getDoubleTy());<br class="">
- }<br class="">
-};<br class="">
-<br class="">
-struct UnsafeFPLibCallOptimization : public LibCallOptimization {<br class="">
- bool UnsafeFPShrink;<br class="">
- UnsafeFPLibCallOptimization(bool UnsafeFPShrink) {<br class="">
- this->UnsafeFPShrink = UnsafeFPShrink;<br class="">
- }<br class="">
-};<br class="">
-<br class="">
-struct CosOpt : public UnsafeFPLibCallOptimization {<br class="">
- CosOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- Value *Ret = nullptr;<br class="">
- if (UnsafeFPShrink && Callee->getName() == "cos" &&<br class="">
- TLI->has(LibFunc::cosf)) {<br class="">
- UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);<br class="">
- Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeCos(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ Value *Ret = nullptr;<br class="">
+ if (UnsafeFPShrink && Callee->getName() == "cos" && TLI->has(LibFunc::cosf)) {<br class="">
+ Ret = optimizeUnaryDoubleFP(CI, B, true);<br class="">
+ }<br class="">
<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- // Just make sure this has 1 argument of FP type, which matches the<br class="">
- // result type.<br class="">
- if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- !FT->getParamType(0)->isFloatingPointTy())<br class="">
- return Ret;<br class="">
-<br class="">
- // cos(-x) -> cos(x)<br class="">
- Value *Op1 = CI->getArgOperand(0);<br class="">
- if (BinaryOperator::isFNeg(Op1)) {<br class="">
- BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);<br class="">
- return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");<br class="">
- }<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ // Just make sure this has 1 argument of FP type, which matches the<br class="">
+ // result type.<br class="">
+ if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ !FT->getParamType(0)->isFloatingPointTy())<br class="">
return Ret;<br class="">
+<br class="">
+ // cos(-x) -> cos(x)<br class="">
+ Value *Op1 = CI->getArgOperand(0);<br class="">
+ if (BinaryOperator::isFNeg(Op1)) {<br class="">
+ BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);<br class="">
+ return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");<br class="">
}<br class="">
-};<br class="">
+ return Ret;<br class="">
+}<br class="">
<br class="">
-struct PowOpt : public UnsafeFPLibCallOptimization {<br class="">
- PowOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- Value *Ret = nullptr;<br class="">
- if (UnsafeFPShrink && Callee->getName() == "pow" &&<br class="">
- TLI->has(LibFunc::powf)) {<br class="">
- UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);<br class="">
- Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- // Just make sure this has 2 arguments of the same FP type, which match the<br class="">
- // result type.<br class="">
- if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- FT->getParamType(0) != FT->getParamType(1) ||<br class="">
- !FT->getParamType(0)->isFloatingPointTy())<br class="">
- return Ret;<br class="">
-<br class="">
- Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);<br class="">
- if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {<br class="">
- // pow(1.0, x) -> 1.0<br class="">
- if (Op1C->isExactlyValue(1.0))<br class="">
- return Op1C;<br class="">
- // pow(2.0, x) -> exp2(x)<br class="">
- if (Op1C->isExactlyValue(2.0) &&<br class="">
- hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp2, LibFunc::exp2f,<br class="">
- LibFunc::exp2l))<br class="">
- return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());<br class="">
- // pow(10.0, x) -> exp10(x)<br class="">
- if (Op1C->isExactlyValue(10.0) &&<br class="">
- hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp10, LibFunc::exp10f,<br class="">
- LibFunc::exp10l))<br class="">
- return EmitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp10), B,<br class="">
- Callee->getAttributes());<br class="">
- }<br class="">
+ Value *Ret = nullptr;<br class="">
+ if (UnsafeFPShrink && Callee->getName() == "pow" && TLI->has(LibFunc::powf)) {<br class="">
+ Ret = optimizeUnaryDoubleFP(CI, B, true);<br class="">
+ }<br class="">
<br class="">
- ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);<br class="">
- if (!Op2C) return Ret;<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ // Just make sure this has 2 arguments of the same FP type, which match the<br class="">
+ // result type.<br class="">
+ if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ FT->getParamType(0) != FT->getParamType(1) ||<br class="">
+ !FT->getParamType(0)->isFloatingPointTy())<br class="">
+ return Ret;<br class="">
<br class="">
- if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0<br class="">
- return ConstantFP::get(CI->getType(), 1.0);<br class="">
+ Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);<br class="">
+ if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {<br class="">
+ // pow(1.0, x) -> 1.0<br class="">
+ if (Op1C->isExactlyValue(1.0))<br class="">
+ return Op1C;<br class="">
+ // pow(2.0, x) -> exp2(x)<br class="">
+ if (Op1C->isExactlyValue(2.0) &&<br class="">
+ hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp2, LibFunc::exp2f,<br class="">
+ LibFunc::exp2l))<br class="">
+ return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());<br class="">
+ // pow(10.0, x) -> exp10(x)<br class="">
+ if (Op1C->isExactlyValue(10.0) &&<br class="">
+ hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp10, LibFunc::exp10f,<br class="">
+ LibFunc::exp10l))<br class="">
+ return EmitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp10), B,<br class="">
+ Callee->getAttributes());<br class="">
+ }<br class="">
<br class="">
- if (Op2C->isExactlyValue(0.5) &&<br class="">
- hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::sqrt, LibFunc::sqrtf,<br class="">
- LibFunc::sqrtl) &&<br class="">
- hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::fabs, LibFunc::fabsf,<br class="">
- LibFunc::fabsl)) {<br class="">
- // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).<br class="">
- // This is faster than calling pow, and still handles negative zero<br class="">
- // and negative infinity correctly.<br class="">
- // TODO: In fast-math mode, this could be just sqrt(x).<br class="">
- // TODO: In finite-only mode, this could be just fabs(sqrt(x)).<br class="">
- Value *Inf = ConstantFP::getInfinity(CI->getType());<br class="">
- Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);<br class="">
- Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,<br class="">
- Callee->getAttributes());<br class="">
- Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,<br class="">
- Callee->getAttributes());<br class="">
- Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);<br class="">
- Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);<br class="">
- return Sel;<br class="">
- }<br class="">
+ ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);<br class="">
+ if (!Op2C)<br class="">
+ return Ret;<br class="">
<br class="">
- if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x<br class="">
- return Op1;<br class="">
- if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x<br class="">
- return B.CreateFMul(Op1, Op1, "pow2");<br class="">
- if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x<br class="">
- return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),<br class="">
- Op1, "powrecip");<br class="">
- return nullptr;<br class="">
- }<br class="">
-};<br class="">
-<br class="">
-struct Exp2Opt : public UnsafeFPLibCallOptimization {<br class="">
- Exp2Opt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- Value *Ret = nullptr;<br class="">
- if (UnsafeFPShrink && Callee->getName() == "exp2" &&<br class="">
- TLI->has(LibFunc::exp2f)) {<br class="">
- UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);<br class="">
- Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);<br class="">
- }<br class="">
+ if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0<br class="">
+ return ConstantFP::get(CI->getType(), 1.0);<br class="">
<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- // Just make sure this has 1 argument of FP type, which matches the<br class="">
- // result type.<br class="">
- if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- !FT->getParamType(0)->isFloatingPointTy())<br class="">
- return Ret;<br class="">
-<br class="">
- Value *Op = CI->getArgOperand(0);<br class="">
- // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32<br class="">
- // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32<br class="">
- LibFunc::Func LdExp = LibFunc::ldexpl;<br class="">
- if (Op->getType()->isFloatTy())<br class="">
- LdExp = LibFunc::ldexpf;<br class="">
- else if (Op->getType()->isDoubleTy())<br class="">
- LdExp = LibFunc::ldexp;<br class="">
-<br class="">
- if (TLI->has(LdExp)) {<br class="">
- Value *LdExpArg = nullptr;<br class="">
- if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {<br class="">
- if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)<br class="">
- LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());<br class="">
- } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {<br class="">
- if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)<br class="">
- LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());<br class="">
- }<br class="">
-<br class="">
- if (LdExpArg) {<br class="">
- Constant *One = ConstantFP::get(*Context, APFloat(1.0f));<br class="">
- if (!Op->getType()->isFloatTy())<br class="">
- One = ConstantExpr::getFPExtend(One, Op->getType());<br class="">
-<br class="">
- Module *M = Caller->getParent();<br class="">
- Value *Callee =<br class="">
- M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(),<br class="">
- Op->getType(), B.getInt32Ty(), NULL);<br class="">
- CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);<br class="">
- if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))<br class="">
- CI->setCallingConv(F->getCallingConv());<br class="">
+ if (Op2C->isExactlyValue(0.5) &&<br class="">
+ hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::sqrt, LibFunc::sqrtf,<br class="">
+ LibFunc::sqrtl) &&<br class="">
+ hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::fabs, LibFunc::fabsf,<br class="">
+ LibFunc::fabsl)) {<br class="">
+ // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).<br class="">
+ // This is faster than calling pow, and still handles negative zero<br class="">
+ // and negative infinity correctly.<br class="">
+ // TODO: In fast-math mode, this could be just sqrt(x).<br class="">
+ // TODO: In finite-only mode, this could be just fabs(sqrt(x)).<br class="">
+ Value *Inf = ConstantFP::getInfinity(CI->getType());<br class="">
+ Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);<br class="">
+ Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B, Callee->getAttributes());<br class="">
+ Value *FAbs =<br class="">
+ EmitUnaryFloatFnCall(Sqrt, "fabs", B, Callee->getAttributes());<br class="">
+ Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);<br class="">
+ Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);<br class="">
+ return Sel;<br class="">
+ }<br class="">
+<br class="">
+ if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x<br class="">
+ return Op1;<br class="">
+ if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x<br class="">
+ return B.CreateFMul(Op1, Op1, "pow2");<br class="">
+ if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x<br class="">
+ return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), Op1, "powrecip");<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- return CI;<br class="">
- }<br class="">
- }<br class="">
- return Ret;<br class="">
+Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ Function *Caller = CI->getParent()->getParent();<br class="">
+<br class="">
+ Value *Ret = nullptr;<br class="">
+ if (UnsafeFPShrink && Callee->getName() == "exp2" &&<br class="">
+ TLI->has(LibFunc::exp2f)) {<br class="">
+ Ret = optimizeUnaryDoubleFP(CI, B, true);<br class="">
}<br class="">
-};<br class="">
<br class="">
-struct SinCosPiOpt : public LibCallOptimization {<br class="">
- SinCosPiOpt() {}<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ // Just make sure this has 1 argument of FP type, which matches the<br class="">
+ // result type.<br class="">
+ if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ !FT->getParamType(0)->isFloatingPointTy())<br class="">
+ return Ret;<br class="">
<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Make sure the prototype is as expected, otherwise the rest of the<br class="">
- // function is probably invalid and likely to abort.<br class="">
- if (!isTrigLibCall(CI))<br class="">
- return nullptr;<br class="">
-<br class="">
- Value *Arg = CI->getArgOperand(0);<br class="">
- SmallVector<CallInst *, 1> SinCalls;<br class="">
- SmallVector<CallInst *, 1> CosCalls;<br class="">
- SmallVector<CallInst *, 1> SinCosCalls;<br class="">
-<br class="">
- bool IsFloat = Arg->getType()->isFloatTy();<br class="">
-<br class="">
- // Look for all compatible sinpi, cospi and sincospi calls with the same<br class="">
- // argument. If there are enough (in some sense) we can make the<br class="">
- // substitution.<br class="">
- for (User *U : Arg->users())<br class="">
- classifyArgUse(U, CI->getParent(), IsFloat, SinCalls, CosCalls,<br class="">
- SinCosCalls);<br class="">
-<br class="">
- // It's only worthwhile if both sinpi and cospi are actually used.<br class="">
- if (SinCosCalls.empty() && (SinCalls.empty() || CosCalls.empty()))<br class="">
- return nullptr;<br class="">
-<br class="">
- Value *Sin, *Cos, *SinCos;<br class="">
- insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos,<br class="">
- SinCos);<br class="">
-<br class="">
- replaceTrigInsts(SinCalls, Sin);<br class="">
- replaceTrigInsts(CosCalls, Cos);<br class="">
- replaceTrigInsts(SinCosCalls, SinCos);<br class="">
-<br class="">
- return nullptr;<br class="">
- }<br class="">
-<br class="">
- bool isTrigLibCall(CallInst *CI) {<br class="">
- Function *Callee = CI->getCalledFunction();<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
-<br class="">
- // We can only hope to do anything useful if we can ignore things like errno<br class="">
- // and floating-point exceptions.<br class="">
- bool AttributesSafe = CI->hasFnAttr(Attribute::NoUnwind) &&<br class="">
- CI->hasFnAttr(Attribute::ReadNone);<br class="">
-<br class="">
- // Other than that we need float(float) or double(double)<br class="">
- return AttributesSafe && FT->getNumParams() == 1 &&<br class="">
- FT->getReturnType() == FT->getParamType(0) &&<br class="">
- (FT->getParamType(0)->isFloatTy() ||<br class="">
- FT->getParamType(0)->isDoubleTy());<br class="">
- }<br class="">
-<br class="">
- void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,<br class="">
- SmallVectorImpl<CallInst *> &SinCalls,<br class="">
- SmallVectorImpl<CallInst *> &CosCalls,<br class="">
- SmallVectorImpl<CallInst *> &SinCosCalls) {<br class="">
- CallInst *CI = dyn_cast<CallInst>(Val);<br class="">
-<br class="">
- if (!CI)<br class="">
- return;<br class="">
-<br class="">
- Function *Callee = CI->getCalledFunction();<br class="">
- StringRef FuncName = Callee->getName();<br class="">
- LibFunc::Func Func;<br class="">
- if (!TLI->getLibFunc(FuncName, Func) || !TLI->has(Func) ||<br class="">
- !isTrigLibCall(CI))<br class="">
- return;<br class="">
-<br class="">
- if (IsFloat) {<br class="">
- if (Func == LibFunc::sinpif)<br class="">
- SinCalls.push_back(CI);<br class="">
- else if (Func == LibFunc::cospif)<br class="">
- CosCalls.push_back(CI);<br class="">
- else if (Func == LibFunc::sincospif_stret)<br class="">
- SinCosCalls.push_back(CI);<br class="">
- } else {<br class="">
- if (Func == LibFunc::sinpi)<br class="">
- SinCalls.push_back(CI);<br class="">
- else if (Func == LibFunc::cospi)<br class="">
- CosCalls.push_back(CI);<br class="">
- else if (Func == LibFunc::sincospi_stret)<br class="">
- SinCosCalls.push_back(CI);<br class="">
- }<br class="">
- }<br class="">
+ Value *Op = CI->getArgOperand(0);<br class="">
+ // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32<br class="">
+ // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32<br class="">
+ LibFunc::Func LdExp = LibFunc::ldexpl;<br class="">
+ if (Op->getType()->isFloatTy())<br class="">
+ LdExp = LibFunc::ldexpf;<br class="">
+ else if (Op->getType()->isDoubleTy())<br class="">
+ LdExp = LibFunc::ldexp;<br class="">
+<br class="">
+ if (TLI->has(LdExp)) {<br class="">
+ Value *LdExpArg = nullptr;<br class="">
+ if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {<br class="">
+ if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)<br class="">
+ LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());<br class="">
+ } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {<br class="">
+ if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)<br class="">
+ LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());<br class="">
+ }<br class="">
+<br class="">
+ if (LdExpArg) {<br class="">
+ Constant *One = ConstantFP::get(CI->getContext(), APFloat(1.0f));<br class="">
+ if (!Op->getType()->isFloatTy())<br class="">
+ One = ConstantExpr::getFPExtend(One, Op->getType());<br class="">
+<br class="">
+ Module *M = Caller->getParent();<br class="">
+ Value *Callee =<br class="">
+ M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(),<br class="">
+ Op->getType(), B.getInt32Ty(), NULL);<br class="">
+ CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);<br class="">
+ if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))<br class="">
+ CI->setCallingConv(F->getCallingConv());<br class="">
<br class="">
- void replaceTrigInsts(SmallVectorImpl<CallInst*> &Calls, Value *Res) {<br class="">
- for (SmallVectorImpl<CallInst*>::iterator I = Calls.begin(),<br class="">
- E = Calls.end();<br class="">
- I != E; ++I) {<br class="">
- LCS->replaceAllUsesWith(*I, Res);<br class="">
+ return CI;<br class="">
}<br class="">
}<br class="">
+ return Ret;<br class="">
+}<br class="">
<br class="">
- void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,<br class="">
- bool UseFloat, Value *&Sin, Value *&Cos,<br class="">
- Value *&SinCos) {<br class="">
- Type *ArgTy = Arg->getType();<br class="">
- Type *ResTy;<br class="">
- StringRef Name;<br class="">
-<br class="">
- Triple T(OrigCallee->getParent()->getTargetTriple());<br class="">
- if (UseFloat) {<br class="">
- Name = "__sincospif_stret";<br class="">
-<br class="">
- assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");<br class="">
- // x86_64 can't use {float, float} since that would be returned in both<br class="">
- // xmm0 and xmm1, which isn't what a real struct would do.<br class="">
- ResTy = T.getArch() == Triple::x86_64<br class="">
- ? static_cast<Type *>(VectorType::get(ArgTy, 2))<br class="">
- : static_cast<Type *>(StructType::get(ArgTy, ArgTy, NULL));<br class="">
- } else {<br class="">
- Name = "__sincospi_stret";<br class="">
- ResTy = StructType::get(ArgTy, ArgTy, NULL);<br class="">
- }<br class="">
+static bool isTrigLibCall(CallInst *CI);<br class="">
+static void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,<br class="">
+ bool UseFloat, Value *&Sin, Value *&Cos,<br class="">
+ Value *&SinCos);<br class="">
<br class="">
- Module *M = OrigCallee->getParent();<br class="">
- Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(),<br class="">
- ResTy, ArgTy, NULL);<br class="">
-<br class="">
- if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {<br class="">
- // If the argument is an instruction, it must dominate all uses so put our<br class="">
- // sincos call there.<br class="">
- BasicBlock::iterator Loc = ArgInst;<br class="">
- B.SetInsertPoint(ArgInst->getParent(), ++Loc);<br class="">
- } else {<br class="">
- // Otherwise (e.g. for a constant) the beginning of the function is as<br class="">
- // good a place as any.<br class="">
- BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();<br class="">
- B.SetInsertPoint(&EntryBB, EntryBB.begin());<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilder<> &B) {<br class="">
<br class="">
- SinCos = B.CreateCall(Callee, Arg, "sincospi");<br class="">
+ // Make sure the prototype is as expected, otherwise the rest of the<br class="">
+ // function is probably invalid and likely to abort.<br class="">
+ if (!isTrigLibCall(CI))<br class="">
+ return nullptr;<br class="">
<br class="">
- if (SinCos->getType()->isStructTy()) {<br class="">
- Sin = B.CreateExtractValue(SinCos, 0, "sinpi");<br class="">
- Cos = B.CreateExtractValue(SinCos, 1, "cospi");<br class="">
- } else {<br class="">
- Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),<br class="">
- "sinpi");<br class="">
- Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),<br class="">
- "cospi");<br class="">
- }<br class="">
+ Value *Arg = CI->getArgOperand(0);<br class="">
+ SmallVector<CallInst *, 1> SinCalls;<br class="">
+ SmallVector<CallInst *, 1> CosCalls;<br class="">
+ SmallVector<CallInst *, 1> SinCosCalls;<br class="">
+<br class="">
+ bool IsFloat = Arg->getType()->isFloatTy();<br class="">
+<br class="">
+ // Look for all compatible sinpi, cospi and sincospi calls with the same<br class="">
+ // argument. If there are enough (in some sense) we can make the<br class="">
+ // substitution.<br class="">
+ for (User *U : Arg->users())<br class="">
+ classifyArgUse(U, CI->getParent(), IsFloat, SinCalls, CosCalls,<br class="">
+ SinCosCalls);<br class="">
+<br class="">
+ // It's only worthwhile if both sinpi and cospi are actually used.<br class="">
+ if (SinCosCalls.empty() && (SinCalls.empty() || CosCalls.empty()))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ Value *Sin, *Cos, *SinCos;<br class="">
+ insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos, SinCos);<br class="">
+<br class="">
+ replaceTrigInsts(SinCalls, Sin);<br class="">
+ replaceTrigInsts(CosCalls, Cos);<br class="">
+ replaceTrigInsts(SinCosCalls, SinCos);<br class="">
+<br class="">
+ return nullptr;<br class="">
+}<br class="">
+<br class="">
+static bool isTrigLibCall(CallInst *CI) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+<br class="">
+ // We can only hope to do anything useful if we can ignore things like errno<br class="">
+ // and floating-point exceptions.<br class="">
+ bool AttributesSafe =<br class="">
+ CI->hasFnAttr(Attribute::NoUnwind) && CI->hasFnAttr(Attribute::ReadNone);<br class="">
+<br class="">
+ // Other than that we need float(float) or double(double)<br class="">
+ return AttributesSafe && FT->getNumParams() == 1 &&<br class="">
+ FT->getReturnType() == FT->getParamType(0) &&<br class="">
+ (FT->getParamType(0)->isFloatTy() ||<br class="">
+ FT->getParamType(0)->isDoubleTy());<br class="">
+}<br class="">
+<br class="">
+void<br class="">
+LibCallSimplifier::classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,<br class="">
+ SmallVectorImpl<CallInst *> &SinCalls,<br class="">
+ SmallVectorImpl<CallInst *> &CosCalls,<br class="">
+ SmallVectorImpl<CallInst *> &SinCosCalls) {<br class="">
+ CallInst *CI = dyn_cast<CallInst>(Val);<br class="">
+<br class="">
+ if (!CI)<br class="">
+ return;<br class="">
+<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ StringRef FuncName = Callee->getName();<br class="">
+ LibFunc::Func Func;<br class="">
+ if (!TLI->getLibFunc(FuncName, Func) || !TLI->has(Func) || !isTrigLibCall(CI))<br class="">
+ return;<br class="">
+<br class="">
+ if (IsFloat) {<br class="">
+ if (Func == LibFunc::sinpif)<br class="">
+ SinCalls.push_back(CI);<br class="">
+ else if (Func == LibFunc::cospif)<br class="">
+ CosCalls.push_back(CI);<br class="">
+ else if (Func == LibFunc::sincospif_stret)<br class="">
+ SinCosCalls.push_back(CI);<br class="">
+ } else {<br class="">
+ if (Func == LibFunc::sinpi)<br class="">
+ SinCalls.push_back(CI);<br class="">
+ else if (Func == LibFunc::cospi)<br class="">
+ CosCalls.push_back(CI);<br class="">
+ else if (Func == LibFunc::sincospi_stret)<br class="">
+ SinCosCalls.push_back(CI);<br class="">
}<br class="">
+}<br class="">
<br class="">
-};<br class="">
+void LibCallSimplifier::replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls,<br class="">
+ Value *Res) {<br class="">
+ for (SmallVectorImpl<CallInst *>::iterator I = Calls.begin(), E = Calls.end();<br class="">
+ I != E; ++I) {<br class="">
+ replaceAllUsesWith(*I, Res);<br class="">
+ }<br class="">
+}<br class="">
+<br class="">
+void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,<br class="">
+ bool UseFloat, Value *&Sin, Value *&Cos, Value *&SinCos) {<br class="">
+ Type *ArgTy = Arg->getType();<br class="">
+ Type *ResTy;<br class="">
+ StringRef Name;<br class="">
+<br class="">
+ Triple T(OrigCallee->getParent()->getTargetTriple());<br class="">
+ if (UseFloat) {<br class="">
+ Name = "__sincospif_stret";<br class="">
+<br class="">
+ assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");<br class="">
+ // x86_64 can't use {float, float} since that would be returned in both<br class="">
+ // xmm0 and xmm1, which isn't what a real struct would do.<br class="">
+ ResTy = T.getArch() == Triple::x86_64<br class="">
+ ? static_cast<Type *>(VectorType::get(ArgTy, 2))<br class="">
+ : static_cast<Type *>(StructType::get(ArgTy, ArgTy, NULL));<br class="">
+ } else {<br class="">
+ Name = "__sincospi_stret";<br class="">
+ ResTy = StructType::get(ArgTy, ArgTy, NULL);<br class="">
+ }<br class="">
+<br class="">
+ Module *M = OrigCallee->getParent();<br class="">
+ Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(),<br class="">
+ ResTy, ArgTy, NULL);<br class="">
+<br class="">
+ if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {<br class="">
+ // If the argument is an instruction, it must dominate all uses so put our<br class="">
+ // sincos call there.<br class="">
+ BasicBlock::iterator Loc = ArgInst;<br class="">
+ B.SetInsertPoint(ArgInst->getParent(), ++Loc);<br class="">
+ } else {<br class="">
+ // Otherwise (e.g. for a constant) the beginning of the function is as<br class="">
+ // good a place as any.<br class="">
+ BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();<br class="">
+ B.SetInsertPoint(&EntryBB, EntryBB.begin());<br class="">
+ }<br class="">
+<br class="">
+ SinCos = B.CreateCall(Callee, Arg, "sincospi");<br class="">
+<br class="">
+ if (SinCos->getType()->isStructTy()) {<br class="">
+ Sin = B.CreateExtractValue(SinCos, 0, "sinpi");<br class="">
+ Cos = B.CreateExtractValue(SinCos, 1, "cospi");<br class="">
+ } else {<br class="">
+ Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),<br class="">
+ "sinpi");<br class="">
+ Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),<br class="">
+ "cospi");<br class="">
+ }<br class="">
+}<br class="">
<br class="">
//===----------------------------------------------------------------------===//<br class="">
// Integer Library Call Optimizations<br class="">
//===----------------------------------------------------------------------===//<br class="">
<br class="">
-struct FFSOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- // Just make sure this has 2 arguments of the same FP type, which match the<br class="">
- // result type.<br class="">
- if (FT->getNumParams() != 1 ||<br class="">
- !FT->getReturnType()->isIntegerTy(32) ||<br class="">
- !FT->getParamType(0)->isIntegerTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- Value *Op = CI->getArgOperand(0);<br class="">
-<br class="">
- // Constant fold.<br class="">
- if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {<br class="">
- if (CI->isZero()) // ffs(0) -> 0.<br class="">
- return B.getInt32(0);<br class="">
- // ffs(c) -> cttz(c)+1<br class="">
- return B.getInt32(CI->getValue().countTrailingZeros() + 1);<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ // Just make sure this has 2 arguments of the same FP type, which match the<br class="">
+ // result type.<br class="">
+ if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy(32) ||<br class="">
+ !FT->getParamType(0)->isIntegerTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ Value *Op = CI->getArgOperand(0);<br class="">
+<br class="">
+ // Constant fold.<br class="">
+ if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {<br class="">
+ if (CI->isZero()) // ffs(0) -> 0.<br class="">
+ return B.getInt32(0);<br class="">
+ // ffs(c) -> cttz(c)+1<br class="">
+ return B.getInt32(CI->getValue().countTrailingZeros() + 1);<br class="">
+ }<br class="">
+<br class="">
+ // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0<br class="">
+ Type *ArgType = Op->getType();<br class="">
+ Value *F =<br class="">
+ Intrinsic::getDeclaration(Callee->getParent(), Intrinsic::cttz, ArgType);<br class="">
+ Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz");<br class="">
+ V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));<br class="">
+ V = B.CreateIntCast(V, B.getInt32Ty(), false);<br class="">
<br class="">
- // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0<br class="">
- Type *ArgType = Op->getType();<br class="">
- Value *F = Intrinsic::getDeclaration(Callee->getParent(),<br class="">
- Intrinsic::cttz, ArgType);<br class="">
- Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz");<br class="">
- V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));<br class="">
- V = B.CreateIntCast(V, B.getInt32Ty(), false);<br class="">
-<br class="">
- Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));<br class="">
- return B.CreateSelect(Cond, V, B.getInt32(0));<br class="">
- }<br class="">
-};<br class="">
-<br class="">
-struct AbsOpt : public LibCallOptimization {<br class="">
- bool ignoreCallingConv() override { return true; }<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- // We require integer(integer) where the types agree.<br class="">
- if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||<br class="">
- FT->getParamType(0) != FT->getReturnType())<br class="">
- return nullptr;<br class="">
-<br class="">
- // abs(x) -> x >s -1 ? x : -x<br class="">
- Value *Op = CI->getArgOperand(0);<br class="">
- Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),<br class="">
- "ispos");<br class="">
- Value *Neg = B.CreateNeg(Op, "neg");<br class="">
- return B.CreateSelect(Pos, Op, Neg);<br class="">
- }<br class="">
-};<br class="">
-<br class="">
-struct IsDigitOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- // We require integer(i32)<br class="">
- if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||<br class="">
- !FT->getParamType(0)->isIntegerTy(32))<br class="">
- return nullptr;<br class="">
-<br class="">
- // isdigit(c) -> (c-'0') <u 10<br class="">
- Value *Op = CI->getArgOperand(0);<br class="">
- Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");<br class="">
- Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");<br class="">
- return B.CreateZExt(Op, CI->getType());<br class="">
- }<br class="">
-};<br class="">
-<br class="">
-struct IsAsciiOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- // We require integer(i32)<br class="">
- if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||<br class="">
- !FT->getParamType(0)->isIntegerTy(32))<br class="">
- return nullptr;<br class="">
-<br class="">
- // isascii(c) -> c <u 128<br class="">
- Value *Op = CI->getArgOperand(0);<br class="">
- Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");<br class="">
- return B.CreateZExt(Op, CI->getType());<br class="">
- }<br class="">
-};<br class="">
-<br class="">
-struct ToAsciiOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- // We require i32(i32)<br class="">
- if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
- !FT->getParamType(0)->isIntegerTy(32))<br class="">
- return nullptr;<br class="">
-<br class="">
- // toascii(c) -> c & 0x7f<br class="">
- return B.CreateAnd(CI->getArgOperand(0),<br class="">
- ConstantInt::get(CI->getType(),0x7F));<br class="">
- }<br class="">
-};<br class="">
+ Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));<br class="">
+ return B.CreateSelect(Cond, V, B.getInt32(0));<br class="">
+}<br class="">
+<br class="">
+Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ // We require integer(integer) where the types agree.<br class="">
+ if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||<br class="">
+ FT->getParamType(0) != FT->getReturnType())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // abs(x) -> x >s -1 ? x : -x<br class="">
+ Value *Op = CI->getArgOperand(0);<br class="">
+ Value *Pos =<br class="">
+ B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()), "ispos");<br class="">
+ Value *Neg = B.CreateNeg(Op, "neg");<br class="">
+ return B.CreateSelect(Pos, Op, Neg);<br class="">
+}<br class="">
+<br class="">
+Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ // We require integer(i32)<br class="">
+ if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||<br class="">
+ !FT->getParamType(0)->isIntegerTy(32))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // isdigit(c) -> (c-'0') <u 10<br class="">
+ Value *Op = CI->getArgOperand(0);<br class="">
+ Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");<br class="">
+ Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");<br class="">
+ return B.CreateZExt(Op, CI->getType());<br class="">
+}<br class="">
+<br class="">
+Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ // We require integer(i32)<br class="">
+ if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||<br class="">
+ !FT->getParamType(0)->isIntegerTy(32))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // isascii(c) -> c <u 128<br class="">
+ Value *Op = CI->getArgOperand(0);<br class="">
+ Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");<br class="">
+ return B.CreateZExt(Op, CI->getType());<br class="">
+}<br class="">
+<br class="">
+Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ // We require i32(i32)<br class="">
+ if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||<br class="">
+ !FT->getParamType(0)->isIntegerTy(32))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // toascii(c) -> c & 0x7f<br class="">
+ return B.CreateAnd(CI->getArgOperand(0),<br class="">
+ ConstantInt::get(CI->getType(), 0x7F));<br class="">
+}<br class="">
<br class="">
//===----------------------------------------------------------------------===//<br class="">
// Formatting and IO Library Call Optimizations<br class="">
//===----------------------------------------------------------------------===//<br class="">
<br class="">
-struct ErrorReportingOpt : public LibCallOptimization {<br class="">
- ErrorReportingOpt(int S = -1) : StreamArg(S) {}<br class="">
+static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);<br class="">
<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &) override {<br class="">
- // Error reporting calls should be cold, mark them as such.<br class="">
- // This applies even to non-builtin calls: it is only a hint and applies to<br class="">
- // functions that the frontend might not understand as builtins.<br class="">
-<br class="">
- // This heuristic was suggested in:<br class="">
- // Improving Static Branch Prediction in a Compiler<br class="">
- // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu<br class="">
- // Proceedings of PACT'98, Oct. 1998, IEEE<br class="">
-<br class="">
- if (!CI->hasFnAttr(Attribute::Cold) && isReportingError(Callee, CI)) {<br class="">
- CI->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold);<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,<br class="">
+ int StreamArg) {<br class="">
+ // Error reporting calls should be cold, mark them as such.<br class="">
+ // This applies even to non-builtin calls: it is only a hint and applies to<br class="">
+ // functions that the frontend might not understand as builtins.<br class="">
+<br class="">
+ // This heuristic was suggested in:<br class="">
+ // Improving Static Branch Prediction in a Compiler<br class="">
+ // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu<br class="">
+ // Proceedings of PACT'98, Oct. 1998, IEEE<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
<br class="">
- return nullptr;<br class="">
+ if (!CI->hasFnAttr(Attribute::Cold) &&<br class="">
+ isReportingError(Callee, CI, StreamArg)) {<br class="">
+ CI->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold);<br class="">
}<br class="">
<br class="">
-protected:<br class="">
- bool isReportingError(Function *Callee, CallInst *CI) {<br class="">
- if (!ColdErrorCalls)<br class="">
- return false;<br class="">
-<br class="">
- if (!Callee || !Callee->isDeclaration())<br class="">
- return false;<br class="">
-<br class="">
- if (StreamArg < 0)<br class="">
- return true;<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- // These functions might be considered cold, but only if their stream<br class="">
- // argument is stderr.<br class="">
+static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {<br class="">
+ if (!ColdErrorCalls)<br class="">
+ return false;<br class="">
<br class="">
- if (StreamArg >= (int) CI->getNumArgOperands())<br class="">
- return false;<br class="">
- LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));<br class="">
- if (!LI)<br class="">
- return false;<br class="">
- GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());<br class="">
- if (!GV || !GV->isDeclaration())<br class="">
- return false;<br class="">
- return GV->getName() == "stderr";<br class="">
- }<br class="">
-<br class="">
- int StreamArg;<br class="">
-};<br class="">
-<br class="">
-struct PrintFOpt : public LibCallOptimization {<br class="">
- Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) {<br class="">
- // Check for a fixed format string.<br class="">
- StringRef FormatStr;<br class="">
- if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))<br class="">
- return nullptr;<br class="">
-<br class="">
- // Empty format string -> noop.<br class="">
- if (FormatStr.empty()) // Tolerate printf's declared void.<br class="">
- return CI->use_empty() ? (Value*)CI :<br class="">
- ConstantInt::get(CI->getType(), 0);<br class="">
-<br class="">
- // Do not do any of the following transformations if the printf return value<br class="">
- // is used, in general the printf return value is not compatible with either<br class="">
- // putchar() or puts().<br class="">
- if (!CI->use_empty())<br class="">
- return nullptr;<br class="">
-<br class="">
- // printf("x") -> putchar('x'), even for '%'.<br class="">
- if (FormatStr.size() == 1) {<br class="">
- Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, DL, TLI);<br class="">
- if (CI->use_empty() || !Res) return Res;<br class="">
- return B.CreateIntCast(Res, CI->getType(), true);<br class="">
- }<br class="">
+ if (!Callee || !Callee->isDeclaration())<br class="">
+ return false;<br class="">
<br class="">
- // printf("foo\n") --> puts("foo")<br class="">
- if (FormatStr[FormatStr.size()-1] == '\n' &&<br class="">
- FormatStr.find('%') == StringRef::npos) { // No format characters.<br class="">
- // Create a string literal with no \n on it. We expect the constant merge<br class="">
- // pass to be run after this pass, to merge duplicate strings.<br class="">
- FormatStr = FormatStr.drop_back();<br class="">
- Value *GV = B.CreateGlobalString(FormatStr, "str");<br class="">
- Value *NewCI = EmitPutS(GV, B, DL, TLI);<br class="">
- return (CI->use_empty() || !NewCI) ?<br class="">
- NewCI :<br class="">
- ConstantInt::get(CI->getType(), FormatStr.size()+1);<br class="">
- }<br class="">
+ if (StreamArg < 0)<br class="">
+ return true;<br class="">
<br class="">
- // Optimize specific format strings.<br class="">
- // printf("%c", chr) --> putchar(chr)<br class="">
- if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&<br class="">
- CI->getArgOperand(1)->getType()->isIntegerTy()) {<br class="">
- Value *Res = EmitPutChar(CI->getArgOperand(1), B, DL, TLI);<br class="">
+ // These functions might be considered cold, but only if their stream<br class="">
+ // argument is stderr.<br class="">
<br class="">
- if (CI->use_empty() || !Res) return Res;<br class="">
- return B.CreateIntCast(Res, CI->getType(), true);<br class="">
- }<br class="">
+ if (StreamArg >= (int)CI->getNumArgOperands())<br class="">
+ return false;<br class="">
+ LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));<br class="">
+ if (!LI)<br class="">
+ return false;<br class="">
+ GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());<br class="">
+ if (!GV || !GV->isDeclaration())<br class="">
+ return false;<br class="">
+ return GV->getName() == "stderr";<br class="">
+}<br class="">
<br class="">
- // printf("%s\n", str) --> puts(str)<br class="">
- if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&<br class="">
- CI->getArgOperand(1)->getType()->isPointerTy()) {<br class="">
- return EmitPutS(CI->getArgOperand(1), B, DL, TLI);<br class="">
- }<br class="">
- return nullptr;<br class="">
+Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {<br class="">
+ // Check for a fixed format string.<br class="">
+ StringRef FormatStr;<br class="">
+ if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // Empty format string -> noop.<br class="">
+ if (FormatStr.empty()) // Tolerate printf's declared void.<br class="">
+ return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);<br class="">
+<br class="">
+ // Do not do any of the following transformations if the printf return value<br class="">
+ // is used, in general the printf return value is not compatible with either<br class="">
+ // putchar() or puts().<br class="">
+ if (!CI->use_empty())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // printf("x") -> putchar('x'), even for '%'.<br class="">
+ if (FormatStr.size() == 1) {<br class="">
+ Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, DL, TLI);<br class="">
+ if (CI->use_empty() || !Res)<br class="">
+ return Res;<br class="">
+ return B.CreateIntCast(Res, CI->getType(), true);<br class="">
+ }<br class="">
+<br class="">
+ // printf("foo\n") --> puts("foo")<br class="">
+ if (FormatStr[FormatStr.size() - 1] == '\n' &&<br class="">
+ FormatStr.find('%') == StringRef::npos) { // No format characters.<br class="">
+ // Create a string literal with no \n on it. We expect the constant merge<br class="">
+ // pass to be run after this pass, to merge duplicate strings.<br class="">
+ FormatStr = FormatStr.drop_back();<br class="">
+ Value *GV = B.CreateGlobalString(FormatStr, "str");<br class="">
+ Value *NewCI = EmitPutS(GV, B, DL, TLI);<br class="">
+ return (CI->use_empty() || !NewCI)<br class="">
+ ? NewCI<br class="">
+ : ConstantInt::get(CI->getType(), FormatStr.size() + 1);<br class="">
+ }<br class="">
+<br class="">
+ // Optimize specific format strings.<br class="">
+ // printf("%c", chr) --> putchar(chr)<br class="">
+ if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&<br class="">
+ CI->getArgOperand(1)->getType()->isIntegerTy()) {<br class="">
+ Value *Res = EmitPutChar(CI->getArgOperand(1), B, DL, TLI);<br class="">
+<br class="">
+ if (CI->use_empty() || !Res)<br class="">
+ return Res;<br class="">
+ return B.CreateIntCast(Res, CI->getType(), true);<br class="">
+ }<br class="">
+<br class="">
+ // printf("%s\n", str) --> puts(str)<br class="">
+ if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&<br class="">
+ CI->getArgOperand(1)->getType()->isPointerTy()) {<br class="">
+ return EmitPutS(CI->getArgOperand(1), B, DL, TLI);<br class="">
}<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Require one fixed pointer argument and an integer/void result.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||<br class="">
- !(FT->getReturnType()->isIntegerTy() ||<br class="">
- FT->getReturnType()->isVoidTy()))<br class="">
- return nullptr;<br class="">
+Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilder<> &B) {<br class="">
<br class="">
- if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {<br class="">
- return V;<br class="">
- }<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Require one fixed pointer argument and an integer/void result.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !(FT->getReturnType()->isIntegerTy() || FT->getReturnType()->isVoidTy()))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ if (Value *V = optimizePrintFString(CI, B)) {<br class="">
+ return V;<br class="">
+ }<br class="">
<br class="">
- // printf(format, ...) -> iprintf(format, ...) if no floating point<br class="">
- // arguments.<br class="">
- if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) {<br class="">
- Module *M = B.GetInsertBlock()->getParent()->getParent();<br class="">
- Constant *IPrintFFn =<br class="">
+ // printf(format, ...) -> iprintf(format, ...) if no floating point<br class="">
+ // arguments.<br class="">
+ if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) {<br class="">
+ Module *M = B.GetInsertBlock()->getParent()->getParent();<br class="">
+ Constant *IPrintFFn =<br class="">
M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());<br class="">
- CallInst *New = cast<CallInst>(CI->clone());<br class="">
- New->setCalledFunction(IPrintFFn);<br class="">
- B.Insert(New);<br class="">
- return New;<br class="">
- }<br class="">
- return nullptr;<br class="">
+ CallInst *New = cast<CallInst>(CI->clone());<br class="">
+ New->setCalledFunction(IPrintFFn);<br class="">
+ B.Insert(New);<br class="">
+ return New;<br class="">
}<br class="">
-};<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
-struct SPrintFOpt : public LibCallOptimization {<br class="">
- Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) {<br class="">
- // Check for a fixed format string.<br class="">
- StringRef FormatStr;<br class="">
- if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))<br class="">
- return nullptr;<br class="">
-<br class="">
- // If we just have a format string (nothing else crazy) transform it.<br class="">
- if (CI->getNumArgOperands() == 2) {<br class="">
- // Make sure there's no % in the constant array. We could try to handle<br class="">
- // %% -> % in the future if we cared.<br class="">
- for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)<br class="">
- if (FormatStr[i] == '%')<br class="">
- return nullptr; // we found a format specifier, bail out.<br class="">
-<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
-<br class="">
- // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)<br class="">
- B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
- ConstantInt::get(DL->getIntPtrType(*Context), // Copy the<br class="">
- FormatStr.size() + 1), 1); // nul byte.<br class="">
- return ConstantInt::get(CI->getType(), FormatStr.size());<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {<br class="">
+ // Check for a fixed format string.<br class="">
+ StringRef FormatStr;<br class="">
+ if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))<br class="">
+ return nullptr;<br class="">
<br class="">
- // The remaining optimizations require the format string to be "%s" or "%c"<br class="">
- // and have an extra operand.<br class="">
- if (FormatStr.size() != 2 || FormatStr[0] != '%' ||<br class="">
- CI->getNumArgOperands() < 3)<br class="">
- return nullptr;<br class="">
-<br class="">
- // Decode the second character of the format string.<br class="">
- if (FormatStr[1] == 'c') {<br class="">
- // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0<br class="">
- if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return nullptr;<br class="">
- Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");<br class="">
- Value *Ptr = CastToCStr(CI->getArgOperand(0), B);<br class="">
- B.CreateStore(V, Ptr);<br class="">
- Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");<br class="">
- B.CreateStore(B.getInt8(0), Ptr);<br class="">
+ // If we just have a format string (nothing else crazy) transform it.<br class="">
+ if (CI->getNumArgOperands() == 2) {<br class="">
+ // Make sure there's no % in the constant array. We could try to handle<br class="">
+ // %% -> % in the future if we cared.<br class="">
+ for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)<br class="">
+ if (FormatStr[i] == '%')<br class="">
+ return nullptr; // we found a format specifier, bail out.<br class="">
<br class="">
- return ConstantInt::get(CI->getType(), 1);<br class="">
- }<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
+ return nullptr;<br class="">
<br class="">
- if (FormatStr[1] == 's') {<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
+ // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)<br class="">
+ B.CreateMemCpy(<br class="">
+ CI->getArgOperand(0), CI->getArgOperand(1),<br class="">
+ ConstantInt::get(DL->getIntPtrType(CI->getContext()),<br class="">
+ FormatStr.size() + 1),<br class="">
+ 1); // Copy the null byte.<br class="">
+ return ConstantInt::get(CI->getType(), FormatStr.size());<br class="">
+ }<br class="">
<br class="">
- // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)<br class="">
- if (!CI->getArgOperand(2)->getType()->isPointerTy()) return nullptr;<br class="">
+ // The remaining optimizations require the format string to be "%s" or "%c"<br class="">
+ // and have an extra operand.<br class="">
+ if (FormatStr.size() != 2 || FormatStr[0] != '%' ||<br class="">
+ CI->getNumArgOperands() < 3)<br class="">
+ return nullptr;<br class="">
<br class="">
- Value *Len = EmitStrLen(CI->getArgOperand(2), B, DL, TLI);<br class="">
- if (!Len)<br class="">
- return nullptr;<br class="">
- Value *IncLen = B.CreateAdd(Len,<br class="">
- ConstantInt::get(Len->getType(), 1),<br class="">
- "leninc");<br class="">
- B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);<br class="">
+ // Decode the second character of the format string.<br class="">
+ if (FormatStr[1] == 'c') {<br class="">
+ // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0<br class="">
+ if (!CI->getArgOperand(2)->getType()->isIntegerTy())<br class="">
+ return nullptr;<br class="">
+ Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");<br class="">
+ Value *Ptr = CastToCStr(CI->getArgOperand(0), B);<br class="">
+ B.CreateStore(V, Ptr);<br class="">
+ Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");<br class="">
+ B.CreateStore(B.getInt8(0), Ptr);<br class="">
<br class="">
- // The sprintf result is the unincremented number of bytes in the string.<br class="">
- return B.CreateIntCast(Len, CI->getType(), false);<br class="">
- }<br class="">
- return nullptr;<br class="">
+ return ConstantInt::get(CI->getType(), 1);<br class="">
}<br class="">
<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Require two fixed pointer arguments and an integer result.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isPointerTy() ||<br class="">
- !FT->getReturnType()->isIntegerTy())<br class="">
+ if (FormatStr[1] == 's') {<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
return nullptr;<br class="">
<br class="">
- if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {<br class="">
- return V;<br class="">
- }<br class="">
+ // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)<br class="">
+ if (!CI->getArgOperand(2)->getType()->isPointerTy())<br class="">
+ return nullptr;<br class="">
<br class="">
- // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating<br class="">
- // point arguments.<br class="">
- if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) {<br class="">
- Module *M = B.GetInsertBlock()->getParent()->getParent();<br class="">
- Constant *SIPrintFFn =<br class="">
+ Value *Len = EmitStrLen(CI->getArgOperand(2), B, DL, TLI);<br class="">
+ if (!Len)<br class="">
+ return nullptr;<br class="">
+ Value *IncLen =<br class="">
+ B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");<br class="">
+ B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);<br class="">
+<br class="">
+ // The sprintf result is the unincremented number of bytes in the string.<br class="">
+ return B.CreateIntCast(Len, CI->getType(), false);<br class="">
+ }<br class="">
+ return nullptr;<br class="">
+}<br class="">
+<br class="">
+Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Require two fixed pointer arguments and an integer result.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isPointerTy() ||<br class="">
+ !FT->getReturnType()->isIntegerTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ if (Value *V = optimizeSPrintFString(CI, B)) {<br class="">
+ return V;<br class="">
+ }<br class="">
+<br class="">
+ // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating<br class="">
+ // point arguments.<br class="">
+ if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) {<br class="">
+ Module *M = B.GetInsertBlock()->getParent()->getParent();<br class="">
+ Constant *SIPrintFFn =<br class="">
M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());<br class="">
- CallInst *New = cast<CallInst>(CI->clone());<br class="">
- New->setCalledFunction(SIPrintFFn);<br class="">
- B.Insert(New);<br class="">
- return New;<br class="">
- }<br class="">
- return nullptr;<br class="">
+ CallInst *New = cast<CallInst>(CI->clone());<br class="">
+ New->setCalledFunction(SIPrintFFn);<br class="">
+ B.Insert(New);<br class="">
+ return New;<br class="">
}<br class="">
-};<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
-struct FPrintFOpt : public LibCallOptimization {<br class="">
- Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) {<br class="">
- ErrorReportingOpt ER(/* StreamArg = */ 0);<br class="">
- (void) ER.callOptimizer(Callee, CI, B);<br class="">
-<br class="">
- // All the optimizations depend on the format string.<br class="">
- StringRef FormatStr;<br class="">
- if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))<br class="">
- return nullptr;<br class="">
-<br class="">
- // Do not do any of the following transformations if the fprintf return<br class="">
- // value is used, in general the fprintf return value is not compatible<br class="">
- // with fwrite(), fputc() or fputs().<br class="">
- if (!CI->use_empty())<br class="">
- return nullptr;<br class="">
-<br class="">
- // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)<br class="">
- if (CI->getNumArgOperands() == 2) {<br class="">
- for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)<br class="">
- if (FormatStr[i] == '%') // Could handle %% -> % if we cared.<br class="">
- return nullptr; // We found a format specifier.<br class="">
-<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
-<br class="">
- return EmitFWrite(CI->getArgOperand(1),<br class="">
- ConstantInt::get(DL->getIntPtrType(*Context),<br class="">
- FormatStr.size()),<br class="">
- CI->getArgOperand(0), B, DL, TLI);<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, IRBuilder<> &B) {<br class="">
+ optimizeErrorReporting(CI, B, 0);<br class="">
+<br class="">
+ // All the optimizations depend on the format string.<br class="">
+ StringRef FormatStr;<br class="">
+ if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))<br class="">
+ return nullptr;<br class="">
<br class="">
- // The remaining optimizations require the format string to be "%s" or "%c"<br class="">
- // and have an extra operand.<br class="">
- if (FormatStr.size() != 2 || FormatStr[0] != '%' ||<br class="">
- CI->getNumArgOperands() < 3)<br class="">
+ // Do not do any of the following transformations if the fprintf return<br class="">
+ // value is used, in general the fprintf return value is not compatible<br class="">
+ // with fwrite(), fputc() or fputs().<br class="">
+ if (!CI->use_empty())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)<br class="">
+ if (CI->getNumArgOperands() == 2) {<br class="">
+ for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)<br class="">
+ if (FormatStr[i] == '%') // Could handle %% -> % if we cared.<br class="">
+ return nullptr; // We found a format specifier.<br class="">
+<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
return nullptr;<br class="">
<br class="">
- // Decode the second character of the format string.<br class="">
- if (FormatStr[1] == 'c') {<br class="">
- // fprintf(F, "%c", chr) --> fputc(chr, F)<br class="">
- if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return nullptr;<br class="">
- return EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI);<br class="">
- }<br class="">
+ return EmitFWrite(<br class="">
+ CI->getArgOperand(1),<br class="">
+ ConstantInt::get(DL->getIntPtrType(CI->getContext()), FormatStr.size()),<br class="">
+ CI->getArgOperand(0), B, DL, TLI);<br class="">
+ }<br class="">
<br class="">
- if (FormatStr[1] == 's') {<br class="">
- // fprintf(F, "%s", str) --> fputs(str, F)<br class="">
- if (!CI->getArgOperand(2)->getType()->isPointerTy())<br class="">
- return nullptr;<br class="">
- return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI);<br class="">
- }<br class="">
+ // The remaining optimizations require the format string to be "%s" or "%c"<br class="">
+ // and have an extra operand.<br class="">
+ if (FormatStr.size() != 2 || FormatStr[0] != '%' ||<br class="">
+ CI->getNumArgOperands() < 3)<br class="">
return nullptr;<br class="">
- }<br class="">
<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Require two fixed paramters as pointers and integer result.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isPointerTy() ||<br class="">
- !FT->getReturnType()->isIntegerTy())<br class="">
+ // Decode the second character of the format string.<br class="">
+ if (FormatStr[1] == 'c') {<br class="">
+ // fprintf(F, "%c", chr) --> fputc(chr, F)<br class="">
+ if (!CI->getArgOperand(2)->getType()->isIntegerTy())<br class="">
return nullptr;<br class="">
+ return EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI);<br class="">
+ }<br class="">
<br class="">
- if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {<br class="">
- return V;<br class="">
- }<br class="">
+ if (FormatStr[1] == 's') {<br class="">
+ // fprintf(F, "%s", str) --> fputs(str, F)<br class="">
+ if (!CI->getArgOperand(2)->getType()->isPointerTy())<br class="">
+ return nullptr;<br class="">
+ return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI);<br class="">
+ }<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no<br class="">
- // floating point arguments.<br class="">
- if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) {<br class="">
- Module *M = B.GetInsertBlock()->getParent()->getParent();<br class="">
- Constant *FIPrintFFn =<br class="">
+Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Require two fixed paramters as pointers and integer result.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isPointerTy() ||<br class="">
+ !FT->getReturnType()->isIntegerTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ if (Value *V = optimizeFPrintFString(CI, B)) {<br class="">
+ return V;<br class="">
+ }<br class="">
+<br class="">
+ // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no<br class="">
+ // floating point arguments.<br class="">
+ if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) {<br class="">
+ Module *M = B.GetInsertBlock()->getParent()->getParent();<br class="">
+ Constant *FIPrintFFn =<br class="">
M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());<br class="">
- CallInst *New = cast<CallInst>(CI->clone());<br class="">
- New->setCalledFunction(FIPrintFFn);<br class="">
- B.Insert(New);<br class="">
- return New;<br class="">
- }<br class="">
- return nullptr;<br class="">
+ CallInst *New = cast<CallInst>(CI->clone());<br class="">
+ New->setCalledFunction(FIPrintFFn);<br class="">
+ B.Insert(New);<br class="">
+ return New;<br class="">
}<br class="">
-};<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
-struct FWriteOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- ErrorReportingOpt ER(/* StreamArg = */ 3);<br class="">
- (void) ER.callOptimizer(Callee, CI, B);<br class="">
-<br class="">
- // Require a pointer, an integer, an integer, a pointer, returning integer.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isIntegerTy() ||<br class="">
- !FT->getParamType(2)->isIntegerTy() ||<br class="">
- !FT->getParamType(3)->isPointerTy() ||<br class="">
- !FT->getReturnType()->isIntegerTy())<br class="">
- return nullptr;<br class="">
-<br class="">
- // Get the element size and count.<br class="">
- ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));<br class="">
- ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));<br class="">
- if (!SizeC || !CountC) return nullptr;<br class="">
- uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();<br class="">
-<br class="">
- // If this is writing zero records, remove the call (it's a noop).<br class="">
- if (Bytes == 0)<br class="">
- return ConstantInt::get(CI->getType(), 0);<br class="">
-<br class="">
- // If this is writing one byte, turn it into fputc.<br class="">
- // This optimisation is only valid, if the return value is unused.<br class="">
- if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)<br class="">
- Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");<br class="">
- Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, DL, TLI);<br class="">
- return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;<br class="">
- }<br class="">
+Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilder<> &B) {<br class="">
+ optimizeErrorReporting(CI, B, 3);<br class="">
<br class="">
- return nullptr;<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Require a pointer, an integer, an integer, a pointer, returning integer.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isIntegerTy() ||<br class="">
+ !FT->getParamType(2)->isIntegerTy() ||<br class="">
+ !FT->getParamType(3)->isPointerTy() ||<br class="">
+ !FT->getReturnType()->isIntegerTy())<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // Get the element size and count.<br class="">
+ ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));<br class="">
+ ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));<br class="">
+ if (!SizeC || !CountC)<br class="">
+ return nullptr;<br class="">
+ uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue();<br class="">
+<br class="">
+ // If this is writing zero records, remove the call (it's a noop).<br class="">
+ if (Bytes == 0)<br class="">
+ return ConstantInt::get(CI->getType(), 0);<br class="">
+<br class="">
+ // If this is writing one byte, turn it into fputc.<br class="">
+ // This optimisation is only valid, if the return value is unused.<br class="">
+ if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)<br class="">
+ Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");<br class="">
+ Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, DL, TLI);<br class="">
+ return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;<br class="">
}<br class="">
-};<br class="">
<br class="">
-struct FPutsOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- ErrorReportingOpt ER(/* StreamArg = */ 1);<br class="">
- (void) ER.callOptimizer(Callee, CI, B);<br class="">
+ return nullptr;<br class="">
+}<br class="">
<br class="">
- // These optimizations require DataLayout.<br class="">
- if (!DL) return nullptr;<br class="">
+Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilder<> &B) {<br class="">
+ optimizeErrorReporting(CI, B, 1);<br class="">
<br class="">
- // Require two pointers. Also, we can't optimize if return value is used.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||<br class="">
- !FT->getParamType(1)->isPointerTy() ||<br class="">
- !CI->use_empty())<br class="">
- return nullptr;<br class="">
-<br class="">
- // fputs(s,F) --> fwrite(s,1,strlen(s),F)<br class="">
- uint64_t Len = GetStringLength(CI->getArgOperand(0));<br class="">
- if (!Len) return nullptr;<br class="">
- // Known to have no uses (see above).<br class="">
- return EmitFWrite(CI->getArgOperand(0),<br class="">
- ConstantInt::get(DL->getIntPtrType(*Context), Len-1),<br class="">
- CI->getArgOperand(1), B, DL, TLI);<br class="">
- }<br class="">
-};<br class="">
-<br class="">
-struct PutsOpt : public LibCallOptimization {<br class="">
- Value *callOptimizer(Function *Callee, CallInst *CI,<br class="">
- IRBuilder<> &B) override {<br class="">
- // Require one fixed pointer argument and an integer/void result.<br class="">
- FunctionType *FT = Callee->getFunctionType();<br class="">
- if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||<br class="">
- !(FT->getReturnType()->isIntegerTy() ||<br class="">
- FT->getReturnType()->isVoidTy()))<br class="">
- return nullptr;<br class="">
-<br class="">
- // Check for a constant string.<br class="">
- StringRef Str;<br class="">
- if (!getConstantStringInfo(CI->getArgOperand(0), Str))<br class="">
- return nullptr;<br class="">
-<br class="">
- if (Str.empty() && CI->use_empty()) {<br class="">
- // puts("") -> putchar('\n')<br class="">
- Value *Res = EmitPutChar(B.getInt32('\n'), B, DL, TLI);<br class="">
- if (CI->use_empty() || !Res) return Res;<br class="">
- return B.CreateIntCast(Res, CI->getType(), true);<br class="">
- }<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
<br class="">
+ // These optimizations require DataLayout.<br class="">
+ if (!DL)<br class="">
return nullptr;<br class="">
- }<br class="">
-};<br class="">
<br class="">
-} // End anonymous namespace.<br class="">
+ // Require two pointers. Also, we can't optimize if return value is used.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !FT->getParamType(1)->isPointerTy() || !CI->use_empty())<br class="">
+ return nullptr;<br class="">
<br class="">
-namespace llvm {<br class="">
+ // fputs(s,F) --> fwrite(s,1,strlen(s),F)<br class="">
+ uint64_t Len = GetStringLength(CI->getArgOperand(0));<br class="">
+ if (!Len)<br class="">
+ return nullptr;<br class="">
<br class="">
-class LibCallSimplifierImpl {<br class="">
- const DataLayout *DL;<br class="">
- const TargetLibraryInfo *TLI;<br class="">
- const LibCallSimplifier *LCS;<br class="">
- bool UnsafeFPShrink;<br class="">
-<br class="">
- // Math library call optimizations.<br class="">
- CosOpt Cos;<br class="">
- PowOpt Pow;<br class="">
- Exp2Opt Exp2;<br class="">
-public:<br class="">
- LibCallSimplifierImpl(const DataLayout *DL, const TargetLibraryInfo *TLI,<br class="">
- const LibCallSimplifier *LCS,<br class="">
- bool UnsafeFPShrink = false)<br class="">
- : Cos(UnsafeFPShrink), Pow(UnsafeFPShrink), Exp2(UnsafeFPShrink) {<br class="">
- this->DL = DL;<br class="">
- this->TLI = TLI;<br class="">
- this->LCS = LCS;<br class="">
- this->UnsafeFPShrink = UnsafeFPShrink;<br class="">
- }<br class="">
-<br class="">
- Value *optimizeCall(CallInst *CI);<br class="">
- LibCallOptimization *lookupOptimization(CallInst *CI);<br class="">
- bool hasFloatVersion(StringRef FuncName);<br class="">
-};<br class="">
+ // Known to have no uses (see above).<br class="">
+ return EmitFWrite(<br class="">
+ CI->getArgOperand(0),<br class="">
+ ConstantInt::get(DL->getIntPtrType(CI->getContext()), Len - 1),<br class="">
+ CI->getArgOperand(1), B, DL, TLI);<br class="">
+}<br class="">
<br class="">
-bool LibCallSimplifierImpl::hasFloatVersion(StringRef FuncName) {<br class="">
+Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilder<> &B) {<br class="">
+ Function *Callee = CI->getCalledFunction();<br class="">
+ // Require one fixed pointer argument and an integer/void result.<br class="">
+ FunctionType *FT = Callee->getFunctionType();<br class="">
+ if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||<br class="">
+ !(FT->getReturnType()->isIntegerTy() || FT->getReturnType()->isVoidTy()))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ // Check for a constant string.<br class="">
+ StringRef Str;<br class="">
+ if (!getConstantStringInfo(CI->getArgOperand(0), Str))<br class="">
+ return nullptr;<br class="">
+<br class="">
+ if (Str.empty() && CI->use_empty()) {<br class="">
+ // puts("") -> putchar('\n')<br class="">
+ Value *Res = EmitPutChar(B.getInt32('\n'), B, DL, TLI);<br class="">
+ if (CI->use_empty() || !Res)<br class="">
+ return Res;<br class="">
+ return B.CreateIntCast(Res, CI->getType(), true);<br class="">
+ }<br class="">
+<br class="">
+ return nullptr;<br class="">
+}<br class="">
+<br class="">
+bool LibCallSimplifier::hasFloatVersion(StringRef FuncName) {<br class="">
LibFunc::Func Func;<br class="">
SmallString<20> FloatFuncName = FuncName;<br class="">
FloatFuncName += 'f';<br class="">
@@ -2048,263 +1874,204 @@ bool LibCallSimplifierImpl::hasFloatVers<br class="">
return false;<br class="">
}<br class="">
<br class="">
-// Fortified library call optimizations.<br class="">
-static MemCpyChkOpt MemCpyChk;<br class="">
-static MemMoveChkOpt MemMoveChk;<br class="">
-static MemSetChkOpt MemSetChk;<br class="">
-static StrCpyChkOpt StrCpyChk;<br class="">
-static StpCpyChkOpt StpCpyChk;<br class="">
-static StrNCpyChkOpt StrNCpyChk;<br class="">
-<br class="">
-// String library call optimizations.<br class="">
-static StrCatOpt StrCat;<br class="">
-static StrNCatOpt StrNCat;<br class="">
-static StrChrOpt StrChr;<br class="">
-static StrRChrOpt StrRChr;<br class="">
-static StrCmpOpt StrCmp;<br class="">
-static StrNCmpOpt StrNCmp;<br class="">
-static StrCpyOpt StrCpy;<br class="">
-static StpCpyOpt StpCpy;<br class="">
-static StrNCpyOpt StrNCpy;<br class="">
-static StrLenOpt StrLen;<br class="">
-static StrPBrkOpt StrPBrk;<br class="">
-static StrToOpt StrTo;<br class="">
-static StrSpnOpt StrSpn;<br class="">
-static StrCSpnOpt StrCSpn;<br class="">
-static StrStrOpt StrStr;<br class="">
-<br class="">
-// Memory library call optimizations.<br class="">
-static MemCmpOpt MemCmp;<br class="">
-static MemCpyOpt MemCpy;<br class="">
-static MemMoveOpt MemMove;<br class="">
-static MemSetOpt MemSet;<br class="">
-<br class="">
-// Math library call optimizations.<br class="">
-static UnaryDoubleFPOpt UnaryDoubleFP(false);<br class="">
-static BinaryDoubleFPOpt BinaryDoubleFP(false);<br class="">
-static UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);<br class="">
-static SinCosPiOpt SinCosPi;<br class="">
-<br class="">
- // Integer library call optimizations.<br class="">
-static FFSOpt FFS;<br class="">
-static AbsOpt Abs;<br class="">
-static IsDigitOpt IsDigit;<br class="">
-static IsAsciiOpt IsAscii;<br class="">
-static ToAsciiOpt ToAscii;<br class="">
-<br class="">
-// Formatting and IO library call optimizations.<br class="">
-static ErrorReportingOpt ErrorReporting;<br class="">
-static ErrorReportingOpt ErrorReporting0(0);<br class="">
-static ErrorReportingOpt ErrorReporting1(1);<br class="">
-static PrintFOpt PrintF;<br class="">
-static SPrintFOpt SPrintF;<br class="">
-static FPrintFOpt FPrintF;<br class="">
-static FWriteOpt FWrite;<br class="">
-static FPutsOpt FPuts;<br class="">
-static PutsOpt Puts;<br class="">
+Value *LibCallSimplifier::optimizeCall(CallInst *CI) {<br class="">
+ if (CI->isNoBuiltin())<br class="">
+ return nullptr;<br class="">
<br class="">
-LibCallOptimization *LibCallSimplifierImpl::lookupOptimization(CallInst *CI) {<br class="">
LibFunc::Func Func;<br class="">
Function *Callee = CI->getCalledFunction();<br class="">
StringRef FuncName = Callee->getName();<br class="">
+ IRBuilder<> Builder(CI);<br class="">
+ bool isCallingConvC = CI->getCallingConv() == llvm::CallingConv::C;<br class="">
<br class="">
// Next check for intrinsics.<br class="">
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {<br class="">
+ if (!isCallingConvC)<br class="">
+ return nullptr;<br class="">
switch (II->getIntrinsicID()) {<br class="">
case Intrinsic::pow:<br class="">
- return &Pow;<br class="">
+ return optimizePow(CI, Builder);<br class="">
case Intrinsic::exp2:<br class="">
- return &Exp2;<br class="">
+ return optimizeExp2(CI, Builder);<br class="">
default:<br class="">
- return nullptr;<br class="">
+ return nullptr;<br class="">
}<br class="">
}<br class="">
<br class="">
// Then check for known library functions.<br class="">
if (TLI->getLibFunc(FuncName, Func) && TLI->has(Func)) {<br class="">
+ // We never change the calling convention.<br class="">
+ if (!ignoreCallingConv(Func) && !isCallingConvC)<br class="">
+ return nullptr;<br class="">
switch (Func) {<br class="">
- case LibFunc::strcat:<br class="">
- return &StrCat;<br class="">
- case LibFunc::strncat:<br class="">
- return &StrNCat;<br class="">
- case LibFunc::strchr:<br class="">
- return &StrChr;<br class="">
- case LibFunc::strrchr:<br class="">
- return &StrRChr;<br class="">
- case LibFunc::strcmp:<br class="">
- return &StrCmp;<br class="">
- case LibFunc::strncmp:<br class="">
- return &StrNCmp;<br class="">
- case LibFunc::strcpy:<br class="">
- return &StrCpy;<br class="">
- case LibFunc::stpcpy:<br class="">
- return &StpCpy;<br class="">
- case LibFunc::strncpy:<br class="">
- return &StrNCpy;<br class="">
- case LibFunc::strlen:<br class="">
- return &StrLen;<br class="">
- case LibFunc::strpbrk:<br class="">
- return &StrPBrk;<br class="">
- case LibFunc::strtol:<br class="">
- case LibFunc::strtod:<br class="">
- case LibFunc::strtof:<br class="">
- case LibFunc::strtoul:<br class="">
- case LibFunc::strtoll:<br class="">
- case LibFunc::strtold:<br class="">
- case LibFunc::strtoull:<br class="">
- return &StrTo;<br class="">
- case LibFunc::strspn:<br class="">
- return &StrSpn;<br class="">
- case LibFunc::strcspn:<br class="">
- return &StrCSpn;<br class="">
- case LibFunc::strstr:<br class="">
- return &StrStr;<br class="">
- case LibFunc::memcmp:<br class="">
- return &MemCmp;<br class="">
- case LibFunc::memcpy:<br class="">
- return &MemCpy;<br class="">
- case LibFunc::memmove:<br class="">
- return &MemMove;<br class="">
- case LibFunc::memset:<br class="">
- return &MemSet;<br class="">
- case LibFunc::cosf:<br class="">
- case LibFunc::cos:<br class="">
- case LibFunc::cosl:<br class="">
- return &Cos;<br class="">
- case LibFunc::sinpif:<br class="">
- case LibFunc::sinpi:<br class="">
- case LibFunc::cospif:<br class="">
- case LibFunc::cospi:<br class="">
- return &SinCosPi;<br class="">
- case LibFunc::powf:<br class="">
- case LibFunc::pow:<br class="">
- case LibFunc::powl:<br class="">
- return &Pow;<br class="">
- case LibFunc::exp2l:<br class="">
- case LibFunc::exp2:<br class="">
- case LibFunc::exp2f:<br class="">
- return &Exp2;<br class="">
- case LibFunc::ffs:<br class="">
- case LibFunc::ffsl:<br class="">
- case LibFunc::ffsll:<br class="">
- return &FFS;<br class="">
- case LibFunc::abs:<br class="">
- case LibFunc::labs:<br class="">
- case LibFunc::llabs:<br class="">
- return &Abs;<br class="">
- case LibFunc::isdigit:<br class="">
- return &IsDigit;<br class="">
- case LibFunc::isascii:<br class="">
- return &IsAscii;<br class="">
- case LibFunc::toascii:<br class="">
- return &ToAscii;<br class="">
- case LibFunc::printf:<br class="">
- return &PrintF;<br class="">
- case LibFunc::sprintf:<br class="">
- return &SPrintF;<br class="">
- case LibFunc::fprintf:<br class="">
- return &FPrintF;<br class="">
- case LibFunc::fwrite:<br class="">
- return &FWrite;<br class="">
- case LibFunc::fputs:<br class="">
- return &FPuts;<br class="">
- case LibFunc::puts:<br class="">
- return &Puts;<br class="">
- case LibFunc::perror:<br class="">
- return &ErrorReporting;<br class="">
- case LibFunc::vfprintf:<br class="">
- case LibFunc::fiprintf:<br class="">
- return &ErrorReporting0;<br class="">
- case LibFunc::fputc:<br class="">
- return &ErrorReporting1;<br class="">
- case LibFunc::ceil:<br class="">
- case LibFunc::fabs:<br class="">
- case LibFunc::floor:<br class="">
- case LibFunc::rint:<br class="">
- case LibFunc::round:<br class="">
- case LibFunc::nearbyint:<br class="">
- case LibFunc::trunc:<br class="">
- if (hasFloatVersion(FuncName))<br class="">
- return &UnaryDoubleFP;<br class="">
- return nullptr;<br class="">
- case LibFunc::acos:<br class="">
- case LibFunc::acosh:<br class="">
- case LibFunc::asin:<br class="">
- case LibFunc::asinh:<br class="">
- case LibFunc::atan:<br class="">
- case LibFunc::atanh:<br class="">
- case LibFunc::cbrt:<br class="">
- case LibFunc::cosh:<br class="">
- case LibFunc::exp:<br class="">
- case LibFunc::exp10:<br class="">
- case LibFunc::expm1:<br class="">
- case LibFunc::log:<br class="">
- case LibFunc::log10:<br class="">
- case LibFunc::log1p:<br class="">
- case LibFunc::log2:<br class="">
- case LibFunc::logb:<br class="">
- case LibFunc::sin:<br class="">
- case LibFunc::sinh:<br class="">
- case LibFunc::sqrt:<br class="">
- case LibFunc::tan:<br class="">
- case LibFunc::tanh:<br class="">
- if (UnsafeFPShrink && hasFloatVersion(FuncName))<br class="">
- return &UnsafeUnaryDoubleFP;<br class="">
- return nullptr;<br class="">
- case LibFunc::fmin:<br class="">
- case LibFunc::fmax:<br class="">
- if (hasFloatVersion(FuncName))<br class="">
- return &BinaryDoubleFP;<br class="">
- return nullptr;<br class="">
- case LibFunc::memcpy_chk:<br class="">
- return &MemCpyChk;<br class="">
- default:<br class="">
- return nullptr;<br class="">
- }<br class="">
+ case LibFunc::strcat:<br class="">
+ return optimizeStrCat(CI, Builder);<br class="">
+ case LibFunc::strncat:<br class="">
+ return optimizeStrNCat(CI, Builder);<br class="">
+ case LibFunc::strchr:<br class="">
+ return optimizeStrChr(CI, Builder);<br class="">
+ case LibFunc::strrchr:<br class="">
+ return optimizeStrRChr(CI, Builder);<br class="">
+ case LibFunc::strcmp:<br class="">
+ return optimizeStrCmp(CI, Builder);<br class="">
+ case LibFunc::strncmp:<br class="">
+ return optimizeStrNCmp(CI, Builder);<br class="">
+ case LibFunc::strcpy:<br class="">
+ return optimizeStrCpy(CI, Builder);<br class="">
+ case LibFunc::stpcpy:<br class="">
+ return optimizeStpCpy(CI, Builder);<br class="">
+ case LibFunc::strncpy:<br class="">
+ return optimizeStrNCpy(CI, Builder);<br class="">
+ case LibFunc::strlen:<br class="">
+ return optimizeStrLen(CI, Builder);<br class="">
+ case LibFunc::strpbrk:<br class="">
+ return optimizeStrPBrk(CI, Builder);<br class="">
+ case LibFunc::strtol:<br class="">
+ case LibFunc::strtod:<br class="">
+ case LibFunc::strtof:<br class="">
+ case LibFunc::strtoul:<br class="">
+ case LibFunc::strtoll:<br class="">
+ case LibFunc::strtold:<br class="">
+ case LibFunc::strtoull:<br class="">
+ return optimizeStrTo(CI, Builder);<br class="">
+ case LibFunc::strspn:<br class="">
+ return optimizeStrSpn(CI, Builder);<br class="">
+ case LibFunc::strcspn:<br class="">
+ return optimizeStrCSpn(CI, Builder);<br class="">
+ case LibFunc::strstr:<br class="">
+ return optimizeStrStr(CI, Builder);<br class="">
+ case LibFunc::memcmp:<br class="">
+ return optimizeMemCmp(CI, Builder);<br class="">
+ case LibFunc::memcpy:<br class="">
+ return optimizeMemCpy(CI, Builder);<br class="">
+ case LibFunc::memmove:<br class="">
+ return optimizeMemMove(CI, Builder);<br class="">
+ case LibFunc::memset:<br class="">
+ return optimizeMemSet(CI, Builder);<br class="">
+ case LibFunc::cosf:<br class="">
+ case LibFunc::cos:<br class="">
+ case LibFunc::cosl:<br class="">
+ return optimizeCos(CI, Builder);<br class="">
+ case LibFunc::sinpif:<br class="">
+ case LibFunc::sinpi:<br class="">
+ case LibFunc::cospif:<br class="">
+ case LibFunc::cospi:<br class="">
+ return optimizeSinCosPi(CI, Builder);<br class="">
+ case LibFunc::powf:<br class="">
+ case LibFunc::pow:<br class="">
+ case LibFunc::powl:<br class="">
+ return optimizePow(CI, Builder);<br class="">
+ case LibFunc::exp2l:<br class="">
+ case LibFunc::exp2:<br class="">
+ case LibFunc::exp2f:<br class="">
+ return optimizeExp2(CI, Builder);<br class="">
+ case LibFunc::ffs:<br class="">
+ case LibFunc::ffsl:<br class="">
+ case LibFunc::ffsll:<br class="">
+ return optimizeFFS(CI, Builder);<br class="">
+ case LibFunc::abs:<br class="">
+ case LibFunc::labs:<br class="">
+ case LibFunc::llabs:<br class="">
+ return optimizeAbs(CI, Builder);<br class="">
+ case LibFunc::isdigit:<br class="">
+ return optimizeIsDigit(CI, Builder);<br class="">
+ case LibFunc::isascii:<br class="">
+ return optimizeIsAscii(CI, Builder);<br class="">
+ case LibFunc::toascii:<br class="">
+ return optimizeToAscii(CI, Builder);<br class="">
+ case LibFunc::printf:<br class="">
+ return optimizePrintF(CI, Builder);<br class="">
+ case LibFunc::sprintf:<br class="">
+ return optimizeSPrintF(CI, Builder);<br class="">
+ case LibFunc::fprintf:<br class="">
+ return optimizeFPrintF(CI, Builder);<br class="">
+ case LibFunc::fwrite:<br class="">
+ return optimizeFWrite(CI, Builder);<br class="">
+ case LibFunc::fputs:<br class="">
+ return optimizeFPuts(CI, Builder);<br class="">
+ case LibFunc::puts:<br class="">
+ return optimizePuts(CI, Builder);<br class="">
+ case LibFunc::perror:<br class="">
+ return optimizeErrorReporting(CI, Builder);<br class="">
+ case LibFunc::vfprintf:<br class="">
+ case LibFunc::fiprintf:<br class="">
+ return optimizeErrorReporting(CI, Builder, 0);<br class="">
+ case LibFunc::fputc:<br class="">
+ return optimizeErrorReporting(CI, Builder, 1);<br class="">
+ case LibFunc::ceil:<br class="">
+ case LibFunc::fabs:<br class="">
+ case LibFunc::floor:<br class="">
+ case LibFunc::rint:<br class="">
+ case LibFunc::round:<br class="">
+ case LibFunc::nearbyint:<br class="">
+ case LibFunc::trunc:<br class="">
+ if (hasFloatVersion(FuncName))<br class="">
+ return optimizeUnaryDoubleFP(CI, Builder, false);<br class="">
+ return nullptr;<br class="">
+ case LibFunc::acos:<br class="">
+ case LibFunc::acosh:<br class="">
+ case LibFunc::asin:<br class="">
+ case LibFunc::asinh:<br class="">
+ case LibFunc::atan:<br class="">
+ case LibFunc::atanh:<br class="">
+ case LibFunc::cbrt:<br class="">
+ case LibFunc::cosh:<br class="">
+ case LibFunc::exp:<br class="">
+ case LibFunc::exp10:<br class="">
+ case LibFunc::expm1:<br class="">
+ case LibFunc::log:<br class="">
+ case LibFunc::log10:<br class="">
+ case LibFunc::log1p:<br class="">
+ case LibFunc::log2:<br class="">
+ case LibFunc::logb:<br class="">
+ case LibFunc::sin:<br class="">
+ case LibFunc::sinh:<br class="">
+ case LibFunc::sqrt:<br class="">
+ case LibFunc::tan:<br class="">
+ case LibFunc::tanh:<br class="">
+ if (UnsafeFPShrink && hasFloatVersion(FuncName))<br class="">
+ return optimizeUnaryDoubleFP(CI, Builder, true);<br class="">
+ return nullptr;<br class="">
+ case LibFunc::fmin:<br class="">
+ case LibFunc::fmax:<br class="">
+ if (hasFloatVersion(FuncName))<br class="">
+ return optimizeBinaryDoubleFP(CI, Builder);<br class="">
+ return nullptr;<br class="">
+ case LibFunc::memcpy_chk:<br class="">
+ return optimizeMemCpyChk(CI, Builder);<br class="">
+ default:<br class="">
+ return nullptr;<br class="">
+ }<br class="">
}<br class="">
<br class="">
+ if (!isCallingConvC)<br class="">
+ return nullptr;<br class="">
+<br class="">
// Finally check for fortified library calls.<br class="">
if (FuncName.endswith("_chk")) {<br class="">
if (FuncName == "__memmove_chk")<br class="">
- return &MemMoveChk;<br class="">
+ return optimizeMemMoveChk(CI, Builder);<br class="">
else if (FuncName == "__memset_chk")<br class="">
- return &MemSetChk;<br class="">
+ return optimizeMemSetChk(CI, Builder);<br class="">
else if (FuncName == "__strcpy_chk")<br class="">
- return &StrCpyChk;<br class="">
+ return optimizeStrCpyChk(CI, Builder);<br class="">
else if (FuncName == "__stpcpy_chk")<br class="">
- return &StpCpyChk;<br class="">
+ return optimizeStpCpyChk(CI, Builder);<br class="">
else if (FuncName == "__strncpy_chk")<br class="">
- return &StrNCpyChk;<br class="">
+ return optimizeStrNCpyChk(CI, Builder);<br class="">
else if (FuncName == "__stpncpy_chk")<br class="">
- return &StrNCpyChk;<br class="">
+ return optimizeStrNCpyChk(CI, Builder);<br class="">
}<br class="">
<br class="">
return nullptr;<br class="">
-<br class="">
-}<br class="">
-<br class="">
-Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) {<br class="">
- LibCallOptimization *LCO = lookupOptimization(CI);<br class="">
- if (LCO) {<br class="">
- IRBuilder<> Builder(CI);<br class="">
- return LCO->optimizeCall(CI, DL, TLI, LCS, Builder);<br class="">
- }<br class="">
- return nullptr;<br class="">
}<br class="">
<br class="">
LibCallSimplifier::LibCallSimplifier(const DataLayout *DL,<br class="">
const TargetLibraryInfo *TLI,<br class="">
- bool UnsafeFPShrink) {<br class="">
- Impl = new LibCallSimplifierImpl(DL, TLI, this, UnsafeFPShrink);<br class="">
-}<br class="">
-<br class="">
-LibCallSimplifier::~LibCallSimplifier() {<br class="">
- delete Impl;<br class="">
-}<br class="">
-<br class="">
-Value *LibCallSimplifier::optimizeCall(CallInst *CI) {<br class="">
- if (CI->isNoBuiltin()) return nullptr;<br class="">
- return Impl->optimizeCall(CI);<br class="">
+ bool UnsafeFPShrink) :<br class="">
+ DL(DL),<br class="">
+ TLI(TLI),<br class="">
+ UnsafeFPShrink(UnsafeFPShrink) {<br class="">
}<br class="">
<br class="">
void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const {<br class="">
@@ -2312,8 +2079,6 @@ void LibCallSimplifier::replaceAllUsesWi<br class="">
I->eraseFromParent();<br class="">
}<br class="">
<br class="">
-}<br class="">
-<br class="">
// TODO:<br class="">
// Additional cases that we need to add to this file:<br class="">
//<br class="">
<br class="">
<br class="">
_______________________________________________<br class="">
llvm-commits mailing list<br class="">
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank" class="">llvm-commits@cs.uiuc.edu</a><br class="">
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank" class="">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br class="">
</blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></div>
</div></blockquote></div><br class=""></div></body></html>