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