Index: include/llvm/Transforms/Utils/SimplifyLibCalls.h =================================================================== --- include/llvm/Transforms/Utils/SimplifyLibCalls.h +++ include/llvm/Transforms/Utils/SimplifyLibCalls.h @@ -160,6 +160,7 @@ Value *optimizeStrRChr(CallInst *CI, IRBuilder<> &B); Value *optimizeStrCmp(CallInst *CI, IRBuilder<> &B); Value *optimizeStrNCmp(CallInst *CI, IRBuilder<> &B); + Value *optimizeStrNDup(CallInst *CI, IRBuilder<> &B); Value *optimizeStrCpy(CallInst *CI, IRBuilder<> &B); Value *optimizeStpCpy(CallInst *CI, IRBuilder<> &B); Value *optimizeStrNCpy(CallInst *CI, IRBuilder<> &B); Index: lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCalls.cpp +++ lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -4223,7 +4223,8 @@ annotateAnyAllocSite(Call, &TLI); if (isAllocLikeFn(&Call, &TLI)) - return visitAllocSite(Call); + if (Instruction *I = visitAllocSite(Call)) + return I; bool Changed = false; Index: lib/Transforms/Utils/BuildLibCalls.cpp =================================================================== --- lib/Transforms/Utils/BuildLibCalls.cpp +++ lib/Transforms/Utils/BuildLibCalls.cpp @@ -842,6 +842,13 @@ B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI); } +Value *llvm::emitStrDup(Value *Ptr, IRBuilder<> &B, + const TargetLibraryInfo *TLI) { + LLVMContext &Context = B.GetInsertBlock()->getContext(); + return emitLibCall(LibFunc_strdup, B.getInt8PtrTy(), B.getInt8PtrTy(), + castToCStr(Ptr, B), B, TLI); +} + Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); Index: lib/Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- lib/Transforms/Utils/SimplifyLibCalls.cpp +++ lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -531,6 +531,21 @@ return nullptr; } +Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilder<> &B) { + Value *Src = CI->getArgOperand(0); + ConstantInt *Size = dyn_cast(CI->getArgOperand(1)); + uint64_t SrcLen = GetStringLength(Src); + if (SrcLen && Size) { + annotateDereferenceableBytes(CI, 0, SrcLen); + if (!Size->isNullValue()) { + if (SrcLen <= Size->getZExtValue() + 1) + return emitStrDup(Src, B, TLI); + } + } + + return nullptr; +} + Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilder<> &B) { Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); if (Dst == Src) // strcpy(x,x) -> x @@ -2716,6 +2731,8 @@ return optimizeStrLen(CI, Builder); case LibFunc_strpbrk: return optimizeStrPBrk(CI, Builder); + case LibFunc_strndup: + return optimizeStrNDup(CI, Builder); case LibFunc_strtol: case LibFunc_strtod: case LibFunc_strtof: Index: test/Transforms/InstCombine/strndup.ll =================================================================== --- test/Transforms/InstCombine/strndup.ll +++ test/Transforms/InstCombine/strndup.ll @@ -0,0 +1,67 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instcombine -S | FileCheck %s + +@hello = constant [6 x i8] c"hello\00" +@null = constant [1 x i8] zeroinitializer + +declare i8* @strndup(i8*, i32) + +define i8* @test1() { +; CHECK-LABEL: @test1( +; CHECK-NEXT: [[RET:%.*]] = call i8* @strndup(i8* dereferenceable(1) getelementptr inbounds ([1 x i8], [1 x i8]* @null, i64 0, i64 0), i32 0) +; CHECK-NEXT: ret i8* [[RET]] +; + %src = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0 + %ret = call i8* @strndup(i8* %src, i32 0) + ret i8* %ret +} + +define i8* @test2() { +; CHECK-LABEL: @test2( +; CHECK-NEXT: [[RET:%.*]] = call i8* @strndup(i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i64 0, i64 0), i32 4) +; CHECK-NEXT: ret i8* [[RET]] +; + %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0 + %ret = call i8* @strndup(i8* %src, i32 4) + ret i8* %ret +} + +define i8* @test3() { +; CHECK-LABEL: @test3( +; CHECK-NEXT: [[STRDUP:%.*]] = call dereferenceable_or_null(6) i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i64 0, i64 0)) +; CHECK-NEXT: ret i8* [[STRDUP]] +; + %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0 + %ret = call i8* @strndup(i8* %src, i32 5) + ret i8* %ret +} + +define i8* @test4() { +; CHECK-LABEL: @test4( +; CHECK-NEXT: [[STRDUP:%.*]] = call dereferenceable_or_null(6) i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i64 0, i64 0)) +; CHECK-NEXT: ret i8* [[STRDUP]] +; + %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0 + %ret = call i8* @strndup(i8* %src, i32 6) + ret i8* %ret +} + +define i8* @test5() { +; CHECK-LABEL: @test5( +; CHECK-NEXT: [[STRDUP:%.*]] = call dereferenceable_or_null(6) i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i64 0, i64 0)) +; CHECK-NEXT: ret i8* [[STRDUP]] +; + %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0 + %ret = call i8* @strndup(i8* %src, i32 7) + ret i8* %ret +} + +define i8* @test6(i32 %n) { +; CHECK-LABEL: @test6( +; CHECK-NEXT: [[RET:%.*]] = call i8* @strndup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i64 0, i64 0), i32 [[N:%.*]]) +; CHECK-NEXT: ret i8* [[RET]] +; + %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0 + %ret = call i8* @strndup(i8* %src, i32 %n) + ret i8* %ret +}