Index: llvm/include/llvm/Transforms/Scalar/MemCpyOptimizer.h =================================================================== --- llvm/include/llvm/Transforms/Scalar/MemCpyOptimizer.h +++ llvm/include/llvm/Transforms/Scalar/MemCpyOptimizer.h @@ -20,6 +20,7 @@ namespace llvm { class AAResults; +class AllocaInst; class BatchAAResults; class AssumptionCache; class CallBase; @@ -77,6 +78,9 @@ Instruction *tryMergingIntoMemset(Instruction *I, Value *StartPtr, Value *ByteVal); bool moveUp(StoreInst *SI, Instruction *P, const LoadInst *LI); + bool performStackMoveOptzn(Instruction *Load, Instruction *Store, + AllocaInst *DestAlloca, AllocaInst *SrcAlloca, + uint64_t Size); void eraseInstruction(Instruction *I); bool iterateOnFunction(Function &F); Index: llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp =================================================================== --- llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -69,6 +69,7 @@ STATISTIC(NumMoveToCpy, "Number of memmoves converted to memcpy"); STATISTIC(NumCpyToSet, "Number of memcpys converted to memset"); STATISTIC(NumCallSlot, "Number of call slot optimizations performed"); +STATISTIC(NumStackMove, "Number of stack-move optimizations performed"); namespace { @@ -730,6 +731,23 @@ return true; } + // If this is a load-store pair from a stack slot to a stack slot, we + // might be able to perform the stack-move optimization just as we do for + // memcpys from an alloca to an alloca. + if (auto *DestAlloca = dyn_cast(SI->getPointerOperand())) { + if (auto *SrcAlloca = dyn_cast(LI->getPointerOperand())) { + if (performStackMoveOptzn(LI, SI, DestAlloca, SrcAlloca, + DL.getTypeStoreSize(T))) { + // Avoid invalidating the iterator. + BBI = SI->getNextNonDebugInstruction()->getIterator(); + eraseInstruction(SI); + eraseInstruction(LI); + ++NumMemCpyInstr; + return true; + } + } + } + return false; } @@ -1408,6 +1426,138 @@ return true; } +// TODO: Write doc comments +bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store, + AllocaInst *DestAlloca, + AllocaInst *SrcAlloca, + uint64_t Size) { + LLVM_DEBUG(dbgs() << "Stack Move: Attempting to optimize:\n" + << *Store << "\n"); + + // Make sure the two allocas are in the same address space. + if (SrcAlloca->getAddressSpace() != DestAlloca->getAddressSpace()) { + LLVM_DEBUG(dbgs() << "Stack Move: Address space mismatch\n"); + return false; + } + + // 1. Check that copy is full. Calculate the static size of the allocas to be + // merged, bail out if we can't. + const DataLayout &DL = DestAlloca->getModule()->getDataLayout(); + std::optional SrcSize = SrcAlloca->getAllocationSize(DL); + if (!SrcSize || SrcSize->isScalable() || Size != SrcSize->getFixedValue()) { + LLVM_DEBUG(dbgs() << "Stack Move: Source alloca size mismatch\n"); + return false; + } + std::optional DestSize = DestAlloca->getAllocationSize(DL); + if (!DestSize || DestSize->isScalable() || + Size != DestSize->getFixedValue()) { + LLVM_DEBUG(dbgs() << "Stack Move: Destination alloca size mismatch\n"); + return false; + } + + // 2. Check that src and dest are never captured, unescaped allocas. + if (llvm::PointerMayBeCaptured(SrcAlloca, /* ReturnCaptures=*/true, + /* StoreCaptures= */ true) || + llvm::PointerMayBeCaptured(DestAlloca, /* ReturnCaptures=*/true, + /* StoreCaptures= */ true)) + return false; + + // 3. Check that dest has no Mod, except full size lifetime intrinsics, from + // the alloca to the Store. Collect lifetime markers and first/last users for + // shrink wrap the lifetimes. + SmallVector LifetimeMarkers; + // TODO: non instruction user? -> ConstantExpr. care? + std::optional FirstUser, LastUser; + auto DestLoc = MemoryLocation(DestAlloca, LocationSize::precise(Size)); + bool DestMod = false, DestRef = false; + // TODO: collect noailas metadata inst + for (User *U : DestAlloca->users()) { + if (auto *I = dyn_cast(U)) { + if (!FirstUser || I->comesBefore(*FirstUser)) + FirstUser = I; + if (!LastUser || (*LastUser)->comesBefore(I)) + LastUser = I; + if (auto *II = dyn_cast(I); + II && II->isLifetimeStartOrEnd()) { + int64_t Size = cast(II->getArgOperand(0))->getSExtValue(); + if (Size < 0 || Size == DestSize) { + LifetimeMarkers.push_back(II); + continue; + } + } + ModRefInfo Res = AA->getModRefInfo(I, DestLoc); + if (I->comesBefore(Store) && isModSet(Res)) + return false; + DestMod |= isModSet(Res); + DestRef |= isRefSet(Res); + } + } + + // 3. Check that, from the after the Store to the end of the BB, + // 3-1. if the dest has any Mod, src has no Ref, and + // 3-2. if the dest has any Ref, src has no Mod. + auto SrcLoc = MemoryLocation(SrcAlloca, LocationSize::precise(Size)); + for (User *U : SrcAlloca->users()) { + if (auto *I = dyn_cast(U)) { + if (!FirstUser || I->comesBefore(*FirstUser)) + FirstUser = I; + if (!LastUser || (*LastUser)->comesBefore(I)) + LastUser = I; + if (I->comesBefore(Store) || I == Store) + continue; + if (auto *II = dyn_cast(I); + II && II->isLifetimeStartOrEnd()) { + int64_t Size = cast(II->getArgOperand(0))->getSExtValue(); + if (Size < 0 || Size == SrcSize) { + LifetimeMarkers.push_back(II); + continue; + } + } + ModRefInfo Res = AA->getModRefInfo(I, SrcLoc); + if ((DestMod && isRefSet(Res)) || (DestRef && isModSet(Res))) + return false; + } + } + + // We can do the transformation. First, Remove all existing lifetime markers. + // FIXME: Currently this doesn't erase instructions. why? + for (IntrinsicInst *II : LifetimeMarkers) + eraseInstruction(II); + + // Do "shrink wrap" the lifetimes. + // FIXME: More existing attrs for new lifetime intrinsics + LLVMContext &C = SrcAlloca->getContext(); + IRBuilder<> Builder(C); + + ConstantInt *AllocaSize = + cast(ConstantInt::get(Type::getInt64Ty(C), Size)); + Builder.SetInsertPoint((*FirstUser)->getParent(), + (*FirstUser)->getIterator()); + auto *NewStart = Builder.CreateLifetimeStart(SrcAlloca, AllocaSize); + NewStart->addParamAttr(1, Attribute::NoCapture); + + Builder.SetInsertPoint((*LastUser)->getParent(), + ++(*LastUser)->getIterator()); + auto *NewEnd = Builder.CreateLifetimeEnd(SrcAlloca, AllocaSize); + NewEnd->addParamAttr(1, Attribute::NoCapture); + + // Align the allocas appropriately. + SrcAlloca->setAlignment( + std::max(SrcAlloca->getAlign(), DestAlloca->getAlign())); + + // Merge the two allocas. + DestAlloca->replaceAllUsesWith(SrcAlloca); + + // Drop metadata on the source alloca. + SrcAlloca->dropUnknownNonDebugMetadata(); + + // TODO: remove all noalias metadata from src alloca. + + LLVM_DEBUG(dbgs() << "Stack Move: Performed staack-move optimization\n"); + NumStackMove++; + return false; +} + /// Perform simplification of memcpy's. If we have memcpy A /// which copies X to Y, and memcpy B which copies Y to Z, then we can rewrite /// B to be a memcpy from X to Z (or potentially a memmove, depending on @@ -1488,8 +1638,10 @@ } } } - if (auto *MDep = dyn_cast(MI)) - return processMemCpyMemCpyDependence(M, MDep, BAA); + if (auto *MDep = dyn_cast(MI)) { + if (processMemCpyMemCpyDependence(M, MDep, BAA)) + return true; + } if (auto *MDep = dyn_cast(MI)) { if (performMemCpyToMemSetOptzn(M, MDep, BAA)) { LLVM_DEBUG(dbgs() << "Converted memcpy to memset\n"); @@ -1508,6 +1660,26 @@ } } + // If the transfer is from a stack slot to a stack slot, then we may be able + // to perform the stack-move optimization. See the comments in + // performStackMoveOptzn() for more details. + AllocaInst *DestAlloca = dyn_cast(M->getDest()); + if (DestAlloca == nullptr) + return false; + AllocaInst *SrcAlloca = dyn_cast(M->getSource()); + if (SrcAlloca == nullptr) + return false; + ConstantInt *Len = dyn_cast(M->getLength()); + if (Len == nullptr) + return false; + if (performStackMoveOptzn(M, M, DestAlloca, SrcAlloca, Len->getZExtValue())) { + // Avoid invalidating the iterator. + BBI = M->getNextNonDebugInstruction()->getIterator(); + eraseInstruction(M); + ++NumMemCpyInstr; + return true; + } + return false; } Index: llvm/test/Analysis/ScopedNoAliasAA/alias-scope-merging.ll =================================================================== --- llvm/test/Analysis/ScopedNoAliasAA/alias-scope-merging.ll +++ llvm/test/Analysis/ScopedNoAliasAA/alias-scope-merging.ll @@ -1,11 +1,23 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2 ; RUN: opt < %s -S -passes=memcpyopt | FileCheck --match-full-lines %s ; Alias scopes are merged by taking the intersection of domains, then the union of the scopes within those domains define i8 @test(i8 %input) { +; CHECK-LABEL: define i8 @test +; CHECK-SAME: (i8 [[INPUT:%.*]]) { +; CHECK-NEXT: [[TMP:%.*]] = alloca i8, align 1 +; CHECK-NEXT: [[DST:%.*]] = alloca i8, align 1 +; CHECK-NEXT: [[SRC:%.*]] = alloca i8, align 1 +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr nonnull [[SRC]]), !noalias !0 +; CHECK-NEXT: store i8 [[INPUT]], ptr [[SRC]], align 1 +; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST]], ptr align 8 [[SRC]], i64 1, i1 false), !alias.scope !5 +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 8, ptr nonnull [[SRC]]), !noalias !0 +; CHECK-NEXT: [[RET_VALUE:%.*]] = load i8, ptr [[DST]], align 1 +; CHECK-NEXT: ret i8 [[RET_VALUE]] +; %tmp = alloca i8 %dst = alloca i8 %src = alloca i8 -; CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %dst, ptr align 8 %src, i64 1, i1 false), !alias.scope ![[SCOPE:[0-9]+]] call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %src), !noalias !4 store i8 %input, ptr %src call void @llvm.memcpy.p0.p0.i64(ptr align 8 %tmp, ptr align 8 %src, i64 1, i1 false), !alias.scope !0 @@ -16,9 +28,6 @@ } ; Merged scope contains "callee0: %a" and "callee0 : %b" -; CHECK-DAG: ![[CALLEE0_A:[0-9]+]] = distinct !{!{{[0-9]+}}, !{{[0-9]+}}, !"callee0: %a"} -; CHECK-DAG: ![[CALLEE0_B:[0-9]+]] = distinct !{!{{[0-9]+}}, !{{[0-9]+}}, !"callee0: %b"} -; CHECK-DAG: ![[SCOPE]] = !{![[CALLEE0_A]], ![[CALLEE0_B]]} declare void @llvm.lifetime.start.p0(i64, ptr nocapture) declare void @llvm.lifetime.end.p0(i64, ptr nocapture) Index: llvm/test/Transforms/MemCpyOpt/callslot.ll =================================================================== --- llvm/test/Transforms/MemCpyOpt/callslot.ll +++ llvm/test/Transforms/MemCpyOpt/callslot.ll @@ -58,9 +58,11 @@ ; CHECK-LABEL: @write_src_between_call_and_memcpy( ; CHECK-NEXT: [[DEST:%.*]] = alloca [16 x i8], align 1 ; CHECK-NEXT: [[SRC:%.*]] = alloca [16 x i8], align 1 +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 16, ptr nocapture [[SRC]]) ; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr [[SRC]], i8 0, i64 16, i1 false) ; CHECK-NEXT: store i8 1, ptr [[SRC]], align 1 -; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[DEST]], ptr [[SRC]], i64 16, i1 false) +; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[SRC]], ptr [[SRC]], i64 16, i1 false) +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 16, ptr nocapture [[SRC]]) ; CHECK-NEXT: ret void ; %dest = alloca [16 x i8] @@ -75,7 +77,7 @@ ; CHECK-LABEL: @throw_between_call_and_mempy( ; CHECK-NEXT: [[SRC:%.*]] = alloca [16 x i8], align 1 ; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr [[SRC]], i8 0, i64 16, i1 false) -; CHECK-NEXT: call void @may_throw() #[[ATTR2:[0-9]+]] +; CHECK-NEXT: call void @may_throw() #[[ATTR3:[0-9]+]] ; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr [[DEST_I8:%.*]], i8 0, i64 16, i1 false) ; CHECK-NEXT: ret void ; @@ -91,7 +93,7 @@ ; CHECK-NEXT: [[DEST:%.*]] = alloca [16 x i8], align 1 ; CHECK-NEXT: [[SRC:%.*]] = alloca [8 x i8], align 1 ; CHECK-NEXT: [[DEST_I8:%.*]] = getelementptr [16 x i8], ptr [[DEST]], i64 0, i64 8 -; CHECK-NEXT: call void @accept_ptr(ptr [[DEST_I8]]) #[[ATTR3:[0-9]+]] +; CHECK-NEXT: call void @accept_ptr(ptr [[DEST_I8]]) #[[ATTR4:[0-9]+]] ; CHECK-NEXT: ret void ; %dest = alloca [16 x i8] @@ -123,7 +125,7 @@ ; CHECK-NEXT: [[DEST:%.*]] = alloca [16 x i8], align 1 ; CHECK-NEXT: [[SRC:%.*]] = alloca [8 x i8], align 1 ; CHECK-NEXT: [[DEST_I8:%.*]] = getelementptr [16 x i8], ptr [[DEST]], i64 0, i64 8 -; CHECK-NEXT: call void @accept_ptr(ptr [[DEST_I8]]) #[[ATTR3]] +; CHECK-NEXT: call void @accept_ptr(ptr [[DEST_I8]]) #[[ATTR4]] ; CHECK-NEXT: ret void ; %dest = alloca [16 x i8] @@ -139,7 +141,7 @@ ; CHECK-NEXT: [[DEST:%.*]] = alloca [16 x i8], align 1 ; CHECK-NEXT: [[SRC:%.*]] = alloca [16 x i8], align 1 ; CHECK-NEXT: call void @accept_ptr(ptr [[DEST]]) -; CHECK-NEXT: call void @accept_ptr(ptr nocapture [[DEST]]) #[[ATTR4:[0-9]+]] +; CHECK-NEXT: call void @accept_ptr(ptr nocapture [[DEST]]) #[[ATTR5:[0-9]+]] ; CHECK-NEXT: ret void ; %dest = alloca [16 x i8] @@ -155,7 +157,7 @@ ; CHECK-NEXT: [[DEST:%.*]] = alloca [16 x i8], align 1 ; CHECK-NEXT: [[SRC:%.*]] = alloca [16 x i8], align 1 ; CHECK-NEXT: call void @accept_ptr(ptr [[DEST]]) -; CHECK-NEXT: call void @accept_ptr(ptr nocapture [[DEST]]) #[[ATTR5:[0-9]+]] +; CHECK-NEXT: call void @accept_ptr(ptr nocapture [[DEST]]) #[[ATTR6:[0-9]+]] ; CHECK-NEXT: ret void ; %dest = alloca [16 x i8] @@ -172,7 +174,7 @@ ; CHECK-NEXT: [[DEST:%.*]] = alloca [16 x i8], align 1 ; CHECK-NEXT: [[SRC:%.*]] = alloca [16 x i8], align 1 ; CHECK-NEXT: call void @accept_ptr(ptr [[DEST]]) -; CHECK-NEXT: call void @accept_ptr(ptr nocapture [[DEST]]) #[[ATTR6:[0-9]+]] +; CHECK-NEXT: call void @accept_ptr(ptr nocapture [[DEST]]) #[[ATTR7:[0-9]+]] ; CHECK-NEXT: ret void ; %dest = alloca [16 x i8] @@ -194,7 +196,7 @@ ; CHECK-NEXT: call void @accept_ptr(ptr [[DEST]]) ; CHECK-NEXT: ret void ; CHECK: nocaptures: -; CHECK-NEXT: call void @accept_ptr(ptr [[DEST]]) #[[ATTR3]] +; CHECK-NEXT: call void @accept_ptr(ptr [[DEST]]) #[[ATTR4]] ; CHECK-NEXT: ret void ; %dest = alloca [16 x i8] @@ -214,7 +216,7 @@ define void @source_alignment(ptr noalias dereferenceable(128) %dst) { ; CHECK-LABEL: @source_alignment( ; CHECK-NEXT: [[SRC:%.*]] = alloca [128 x i8], align 4 -; CHECK-NEXT: call void @accept_ptr(ptr nocapture [[DST:%.*]]) #[[ATTR3]] +; CHECK-NEXT: call void @accept_ptr(ptr nocapture [[DST:%.*]]) #[[ATTR4]] ; CHECK-NEXT: ret void ; %src = alloca [128 x i8], align 4 Index: llvm/test/Transforms/MemCpyOpt/callslot_badaa.ll =================================================================== --- llvm/test/Transforms/MemCpyOpt/callslot_badaa.ll +++ llvm/test/Transforms/MemCpyOpt/callslot_badaa.ll @@ -1,3 +1,4 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2 ; RUN: opt < %s -S -passes=memcpyopt | FileCheck --match-full-lines %s ; Make sure callslot optimization merges alias.scope metadata correctly when it merges instructions. @@ -13,11 +14,22 @@ ; !5 = distinct !{!5, !"callee0"} ; Which is incorrect because the lifetime.end of %src will now "noalias" the above memcpy. define i8 @test(i8 %input) { +; CHECK-LABEL: define i8 @test +; CHECK-SAME: (i8 [[INPUT:%.*]]) { +; CHECK-NEXT: [[TMP:%.*]] = alloca i8, align 1 +; CHECK-NEXT: [[DST:%.*]] = alloca i8, align 1 +; CHECK-NEXT: [[SRC:%.*]] = alloca i8, align 1 +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr nonnull [[SRC]]), !noalias !0 +; CHECK-NEXT: store i8 [[INPUT]], ptr [[SRC]], align 1 +; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST]], ptr align 8 [[SRC]], i64 1, i1 false) +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 8, ptr nonnull [[SRC]]), !noalias !0 +; CHECK-NEXT: [[RET_VALUE:%.*]] = load i8, ptr [[DST]], align 1 +; CHECK-NEXT: ret i8 [[RET_VALUE]] +; %tmp = alloca i8 %dst = alloca i8 %src = alloca i8 ; NOTE: we're matching the full line and looking for the lack of !alias.scope here -; CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %dst, ptr align 8 %src, i64 1, i1 false) call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %src), !noalias !3 store i8 %input, ptr %src call void @llvm.memcpy.p0.p0.i64(ptr align 8 %tmp, ptr align 8 %src, i64 1, i1 false), !alias.scope !0 Index: llvm/test/Transforms/MemCpyOpt/memcpy.ll =================================================================== --- llvm/test/Transforms/MemCpyOpt/memcpy.ll +++ llvm/test/Transforms/MemCpyOpt/memcpy.ll @@ -598,8 +598,11 @@ define void @immut_param_enforced_alignment() { ; CHECK-LABEL: @immut_param_enforced_alignment( ; CHECK-NEXT: [[VAL:%.*]] = alloca i8, align 4 +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 1, ptr nocapture [[VAL]]) ; CHECK-NEXT: store i32 42, ptr [[VAL]], align 4 +; CHECK-NEXT: [[VAL1:%.*]] = alloca i8, align 4 ; CHECK-NEXT: call void @f(ptr noalias nocapture readonly [[VAL]]) +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 1, ptr nocapture [[VAL]]) ; CHECK-NEXT: ret void ; %val = alloca i8, align 1 @@ -646,8 +649,11 @@ define void @immut_unescaped_alloca() { ; CHECK-LABEL: @immut_unescaped_alloca( ; CHECK-NEXT: [[VAL:%.*]] = alloca i8, align 4 +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 1, ptr nocapture [[VAL]]) ; CHECK-NEXT: store i32 42, ptr [[VAL]], align 4 +; CHECK-NEXT: [[VAL1:%.*]] = alloca i8, align 4 ; CHECK-NEXT: call void @f_full_readonly(ptr [[VAL]]) +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 1, ptr nocapture [[VAL]]) ; CHECK-NEXT: ret void ; %val = alloca i8, align 4 Index: llvm/test/Transforms/MemCpyOpt/stack-move.ll =================================================================== --- llvm/test/Transforms/MemCpyOpt/stack-move.ll +++ llvm/test/Transforms/MemCpyOpt/stack-move.ll @@ -27,13 +27,13 @@ ; CHECK-NEXT: [[SRC:%.*]] = alloca [[STRUCT_FOO:%.*]], align 4 ; CHECK-NEXT: [[DEST:%.*]] = alloca [[STRUCT_FOO]], align 4 ; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[DEST]]) +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[SRC]], ptr align 4 @constant, i64 12, i1 false) ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]) -; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[DEST]], ptr align 4 [[SRC]], i64 12, i1 false) +; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[SRC]], ptr align 4 [[SRC]], i64 12, i1 false) +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) +; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]) ; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[DEST]]) -; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[DEST]]) ; CHECK-NEXT: ret void ; %src = alloca %struct.Foo, align 4 @@ -56,13 +56,12 @@ ; CHECK-NEXT: [[SRC:%.*]] = alloca [[STRUCT_FOO:%.*]], align 4 ; CHECK-NEXT: [[DEST:%.*]] = alloca [[STRUCT_FOO]], align 4 ; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[DEST]]) +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[SRC]], ptr align 4 @constant, i64 12, i1 false) ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]) -; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[DEST]], ptr align 4 [[SRC]], i64 12, i1 false) ; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[DEST]]) -; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[DEST]]) +; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]) +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) ; CHECK-NEXT: ret void ; %src = alloca %struct.Foo, align 4 @@ -85,14 +84,14 @@ ; CHECK-NEXT: [[SRC:%.*]] = alloca i32, align 4 ; CHECK-NEXT: [[DEST:%.*]] = alloca i32, align 4 ; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 4, ptr nocapture [[SRC]]) -; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 4, ptr nocapture [[DEST]]) +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 4, ptr nocapture [[SRC]]) ; CHECK-NEXT: store i32 42, ptr [[SRC]], align 4 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]) ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[SRC]], align 4 -; CHECK-NEXT: store i32 [[TMP2]], ptr [[DEST]], align 4 +; CHECK-NEXT: store i32 [[TMP2]], ptr [[SRC]], align 4 +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 4, ptr nocapture [[SRC]]) +; CHECK-NEXT: [[TMP3:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]) ; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 4, ptr nocapture [[SRC]]) -; CHECK-NEXT: [[TMP3:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[DEST]]) -; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 4, ptr nocapture [[DEST]]) ; CHECK-NEXT: ret void ; %src = alloca i32, align 4 @@ -117,13 +116,13 @@ ; CHECK-NEXT: [[SRC:%.*]] = alloca [[STRUCT_FOO:%.*]], align 8 ; CHECK-NEXT: [[DEST:%.*]] = alloca [[STRUCT_FOO]], align 4 ; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[DEST]]) +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[SRC]], ptr align 4 @constant, i64 12, i1 false) ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]) -; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[DEST]], ptr align 4 [[SRC]], i64 12, i1 false) +; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[SRC]], ptr align 4 [[SRC]], i64 12, i1 false) +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) +; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]) ; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[DEST]]) -; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[DEST]]) ; CHECK-NEXT: ret void ; %src = alloca %struct.Foo, align 8 @@ -147,18 +146,17 @@ ; CHECK-NEXT: [[SRC:%.*]] = alloca [[STRUCT_FOO:%.*]], align 4 ; CHECK-NEXT: [[DEST:%.*]] = alloca [[STRUCT_FOO]], align 4 ; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[DEST]]) +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[SRC]], ptr align 4 @constant, i64 12, i1 false) ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]) -; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[DEST]], ptr align 4 [[SRC]], i64 12, i1 false) +; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[SRC]], ptr align 4 [[SRC]], i64 12, i1 false) ; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[DEST]]) +; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]) ; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) ; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: [[TMP3:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[DEST]]) +; CHECK-NEXT: [[TMP3:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]) +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) ; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[DEST]]) -; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[DEST]]) ; CHECK-NEXT: ret void ; %src = alloca %struct.Foo, align 4 @@ -179,20 +177,20 @@ ret void } -; TODO: Remove metadata and merge +; TODO: remove metadata ; Tests that we remove scoped noalias metadata from a call. define void @remove_scoped_noalias() { ; CHECK-LABEL: define void @remove_scoped_noalias() { ; CHECK-NEXT: [[SRC:%.*]] = alloca [[STRUCT_FOO:%.*]], align 4 ; CHECK-NEXT: [[DEST:%.*]] = alloca [[STRUCT_FOO]], align 4 ; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[DEST]]) +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[SRC]], ptr align 4 @constant, i64 12, i1 false) ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]), !alias.scope !0 -; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[DEST]], ptr align 4 [[SRC]], i64 12, i1 false) +; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[SRC]], ptr align 4 [[SRC]], i64 12, i1 false) +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) +; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]), !noalias !0 ; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[DEST]]), !noalias !0 -; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[DEST]]) ; CHECK-NEXT: ret void ; %src = alloca %struct.Foo, align 4 @@ -212,16 +210,16 @@ ; Tests that we remove metadata on the merged alloca. define void @remove_alloca_metadata() { ; CHECK-LABEL: define void @remove_alloca_metadata() { -; CHECK-NEXT: [[SRC:%.*]] = alloca [[STRUCT_FOO:%.*]], align 4, !annotation !3 +; CHECK-NEXT: [[SRC:%.*]] = alloca [[STRUCT_FOO:%.*]], align 4 ; CHECK-NEXT: [[DEST:%.*]] = alloca [[STRUCT_FOO]], align 4 ; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[DEST]]) +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr nocapture [[SRC]]) ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[SRC]], ptr align 4 @constant, i64 12, i1 false) ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]), !alias.scope !0 -; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[DEST]], ptr align 4 [[SRC]], i64 12, i1 false) +; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[SRC]], ptr align 4 [[SRC]], i64 12, i1 false) +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) +; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[SRC]]), !noalias !0 ; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[SRC]]) -; CHECK-NEXT: [[TMP2:%.*]] = call i32 @use_nocapture(ptr nocapture noundef [[DEST]]), !noalias !0 -; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 12, ptr nocapture [[DEST]]) ; CHECK-NEXT: ret void ; %src = alloca %struct.Foo, align 4, !annotation !3 Index: llvm/test/Transforms/MemCpyOpt/stackrestore.ll =================================================================== --- llvm/test/Transforms/MemCpyOpt/stackrestore.ll +++ llvm/test/Transforms/MemCpyOpt/stackrestore.ll @@ -53,13 +53,15 @@ ; CHECK-NEXT: [[TMPMEM:%.*]] = alloca [10 x i8], align 4 ; CHECK-NEXT: [[INALLOCA_SAVE:%.*]] = tail call ptr @llvm.stacksave() ; CHECK-NEXT: [[ARGMEM:%.*]] = alloca inalloca [10 x i8], align 4 +; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 10, ptr nocapture [[ARGMEM]]) ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr [[ARGMEM]], ptr align 1 @str, i32 9, i1 false) ; CHECK-NEXT: [[P10:%.*]] = getelementptr inbounds [10 x i8], ptr [[ARGMEM]], i32 0, i32 9 ; CHECK-NEXT: store i8 0, ptr [[P10]], align 1 -; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr [[TMPMEM]], ptr [[ARGMEM]], i32 10, i1 false) +; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr [[ARGMEM]], ptr [[ARGMEM]], i32 10, i1 false) ; CHECK-NEXT: call void @llvm.stackrestore(ptr [[INALLOCA_SAVE]]) ; CHECK-NEXT: [[HEAP:%.*]] = call ptr @malloc(i32 9) -; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr [[HEAP]], ptr [[TMPMEM]], i32 9, i1 false) +; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr [[HEAP]], ptr [[ARGMEM]], i32 9, i1 false) +; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 10, ptr nocapture [[ARGMEM]]) ; CHECK-NEXT: call void @useit(ptr [[HEAP]]) ; CHECK-NEXT: ret i32 0 ;