Index: include/llvm/Transforms/Utils/SimplifyLibCalls.h =================================================================== --- include/llvm/Transforms/Utils/SimplifyLibCalls.h +++ include/llvm/Transforms/Utils/SimplifyLibCalls.h @@ -167,9 +167,9 @@ Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B); Value *optimizeBCmp(CallInst *CI, IRBuilder<> &B); Value *optimizeMemCmpBCmpCommon(CallInst *CI, IRBuilder<> &B); - Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B); - Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B); - Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B); + Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B, bool isIntrinsic = false); + Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B, bool isIntrinsic = false); + Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B, bool isIntrinsic = false); Value *optimizeRealloc(CallInst *CI, IRBuilder<> &B); Value *optimizeWcslen(CallInst *CI, IRBuilder<> &B); // Wrapper for all String/Memory Library Call Optimizations Index: lib/Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- lib/Transforms/Utils/SimplifyLibCalls.cpp +++ lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -186,6 +186,18 @@ return true; } +static void inferDereferenceableBytes(Value *V, uint64_t DerefBytes) { + if (auto *Arg = dyn_cast(V)) { + Function *F = Arg->getParent(); + unsigned ArgNo = Arg->getArgNo(); + if (F->getParamDereferenceableBytes(ArgNo) < DerefBytes) { + F->removeParamAttr(ArgNo, Attribute::Dereferenceable); + F->removeParamAttr(ArgNo, Attribute::DereferenceableOrNull); + F->addDereferenceableParamAttr(ArgNo, DerefBytes); + } + } +} + //===----------------------------------------------------------------------===// // String and Memory Library Call Optimizations //===----------------------------------------------------------------------===// @@ -926,10 +938,13 @@ return Constant::getNullValue(CI->getType()); // Handle constant lengths. - if (ConstantInt *LenC = dyn_cast(Size)) + if (ConstantInt *LenC = dyn_cast(Size)) { + inferDereferenceableBytes(LHS, LenC->getZExtValue()); + inferDereferenceableBytes(RHS, LenC->getZExtValue()); if (Value *Res = optimizeMemCmpConstantSize(CI, LHS, RHS, LenC->getZExtValue(), B, DL)) return Res; + } return nullptr; } @@ -955,18 +970,37 @@ return optimizeMemCmpBCmpCommon(CI, B); } -Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilder<> &B, + bool isIntrinsic) { + Value *Op0 = CI->getArgOperand(0), *Op1 = CI->getArgOperand(1); + Value *Size = CI->getArgOperand(2); + if (ConstantInt *LenC = dyn_cast(Size)) { + inferDereferenceableBytes(Op0, LenC->getZExtValue()); + inferDereferenceableBytes(Op1, LenC->getZExtValue()); + } + + if (isIntrinsic) + return nullptr; + // memcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n) - B.CreateMemCpy(CI->getArgOperand(0), 1, CI->getArgOperand(1), 1, - CI->getArgOperand(2)); - return CI->getArgOperand(0); + B.CreateMemCpy(Op0, 1, Op1, 1, Size); + return Op0; } -Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilder<> &B, bool isIntrinsic) { + Value *Op0 = CI->getArgOperand(0), *Op1 = CI->getArgOperand(1); + Value *Size = CI->getArgOperand(2); + if (ConstantInt *LenC = dyn_cast(Size)) { + inferDereferenceableBytes(Op0, LenC->getZExtValue()); + inferDereferenceableBytes(Op1, LenC->getZExtValue()); + } + + if (isIntrinsic) + return nullptr; + // memmove(x, y, n) -> llvm.memmove(align 1 x, align 1 y, n) - B.CreateMemMove(CI->getArgOperand(0), 1, CI->getArgOperand(1), 1, - CI->getArgOperand(2)); - return CI->getArgOperand(0); + B.CreateMemMove(Op0, 1, Op1, 1, Size); + return Op0; } /// Fold memset[_chk](malloc(n), 0, n) --> calloc(1, n). @@ -1015,14 +1049,25 @@ return Calloc; } -Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B, + bool isIntrinsic) { + Value *Op0 = CI->getArgOperand(0), *Op1 = CI->getArgOperand(1); + Value *Size = CI->getArgOperand(2); + if (ConstantInt *LenC = dyn_cast(Size)) { + inferDereferenceableBytes(Op0, LenC->getZExtValue()); + inferDereferenceableBytes(Op1, LenC->getZExtValue()); + } + + if (isIntrinsic) + return nullptr; + if (auto *Calloc = foldMallocMemset(CI, B)) return Calloc; // memset(p, v, n) -> llvm.memset(align 1 p, v, n) - Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false); - B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1); - return CI->getArgOperand(0); + Value *Val = B.CreateIntCast(Op1, B.getInt8Ty(), false); + B.CreateMemSet(Op0, Val, Size, 1); + return Op0; } Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilder<> &B) { @@ -2725,6 +2770,12 @@ case Intrinsic::sqrt: return optimizeSqrt(CI, Builder); // TODO: Use foldMallocMemset() with memset intrinsic. + case Intrinsic::memset: + return optimizeMemSet(CI, Builder, true); + case Intrinsic::memcpy: + return optimizeMemCpy(CI, Builder, true); + case Intrinsic::memmove: + return optimizeMemMove(CI, Builder, true); default: return nullptr; }