diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp --- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -1461,6 +1461,8 @@ DenseMap IOLs; + Function *ExitUseFn; + DSEState(Function &F, AliasAnalysis &AA, MemorySSA &MSSA, DominatorTree &DT, PostDominatorTree &PDT, const TargetLibraryInfo &TLI) : F(F), AA(AA), MSSA(MSSA), DT(DT), PDT(PDT), TLI(TLI) {} @@ -1469,6 +1471,13 @@ DominatorTree &DT, PostDominatorTree &PDT, const TargetLibraryInfo &TLI) { DSEState State(F, AA, MSSA, DT, PDT, TLI); + Module *M = F.getParent(); + LLVMContext &Ctx = M->getContext(); + FunctionType *FnTy = FunctionType::get(Type::getVoidTy(Ctx), {}, false); + State.ExitUseFn = + cast(M->getOrInsertFunction("____foobar", FnTy).getCallee()); + State.ExitUseFn->addFnAttr(Attribute::ReadOnly); + // Collect blocks with throwing instructions not modeled in MemroySSA and // alloc-like objects. unsigned PO = 0; @@ -1490,6 +1499,15 @@ (isAllocLikeFn(&I, &TLI) && !PointerMayBeCaptured(&I, false, true))) State.InvisibleToCaller.insert(&I); } + if (isa(BB->getTerminator()) && DT.isReachableFromEntry(BB)) { + MemorySSAUpdater MSSAU(&MSSA); + CallInst *CI = + CallInst::Create(State.ExitUseFn->getFunctionType(), + State.ExitUseFn, {}, BB->getTerminator()); + MemoryAccess *NewMemAcc = MSSAU.createMemoryAccessInBB( + CI, nullptr, CI->getParent(), MemorySSA::End); + MSSAU.insertUse(cast(NewMemAcc), false); + } } // Treat byval or inalloca arguments the same as Allocas, stores to them are @@ -1526,7 +1544,7 @@ } // Returns true if \p Use may write to \p DefLoc. - bool isWriteClobber(MemoryLocation DefLoc, Instruction *UseInst) const { + bool isMustWriteClobber(MemoryLocation DefLoc, Instruction *UseInst) const { if (!UseInst->mayWriteToMemory()) return false; @@ -1535,11 +1553,7 @@ return false; ModRefInfo MR = AA.getModRefInfo(UseInst, DefLoc); - // If necessary, perform additional analysis. - if (isModSet(MR)) - MR = AA.callCapturesBefore(UseInst, DefLoc, &DT); - MR = clearMust(MR); - return isModSet(MR); + return isModSet(MR) && isMustSet(MR); } // Returns true if \p Use may read from \p DefLoc. @@ -1547,10 +1561,19 @@ if (!UseInst->mayReadFromMemory()) return false; - if (auto CS = CallSite(UseInst)) + if (auto CS = CallSite(UseInst)) { if (CS.onlyAccessesInaccessibleMemory()) return false; + if (CS.getCalledFunction() == ExitUseFn) { + DataLayout DL = F.getParent()->getDataLayout(); + const Value *UO = GetUnderlyingObject(DefLoc.Ptr, DL); + /// Maybe pre-compute + return !UO || InvisibleToCaller.find(UO) == InvisibleToCaller.end() || + (!isa(UO) && PointerMayBeCaptured(UO, true, false)); + } + } + ModRefInfo MR = AA.getModRefInfo(UseInst, DefLoc); // If necessary, perform additional analysis. if (isRefSet(MR)) @@ -1663,10 +1686,18 @@ // (The Use points to the *first* Def it may alias) // 3 = Def(1) ; <---- Current (3, 2) = NoAlias, (3,1) = MayAlias, stores [0,1] if (MemoryDef *UseDef = dyn_cast(UseAccess)) { - PushMemUses(UseDef); - if (isWriteClobber(DefLoc, UseInst)) { - LLVM_DEBUG(dbgs() << " ... may-write clobber\n"); - return None; + int64_t InstWriteOffset, DepWriteOffset; + auto CC = getLocForWriteEx(UseInst); + InstOverlapIntervalsTy IOL; + + const DataLayout &DL = F.getParent()->getDataLayout(); + + if (!isMustWriteClobber(DefLoc, UseInst) || + (CC && + isOverwrite(DefLoc, *CC, DL, TLI, DepWriteOffset, InstWriteOffset, + UseInst, IOL, AA, &F) != OW_Complete)) { + LLVM_DEBUG(dbgs() << " ... found non-aliasing MemoryDef\n"); + PushMemUses(UseDef); } } } @@ -1831,14 +1862,6 @@ continue; } - // We must post-dom the instructions to safely remove it. Continue - // checking from NextDef - if (!PDT.dominates(SI->getParent(), NextDef->getBlock())) { - ToCheck.insert(NextDef->getDefiningAccess()); - LLVM_DEBUG(dbgs() << " skip, not post-dominating!\n"); - continue; - } - // Before we try to remove anything, check for any extra throwing // instructions that block us from DSEing if (State.mayThrowBetween(SI, NI, SILocUnd)) { @@ -1873,6 +1896,17 @@ for (auto &KV : State.IOLs) MadeChange |= removePartiallyOverlappedStores(&AA, DL, KV.second); + MemorySSAUpdater MSSAU(&MSSA); + for (auto &BB : F) { + if (!DT.isReachableFromEntry(&BB)) + continue; + if (isa(BB.getTerminator())) { + Instruction *C = &*std::prev(BB.getTerminator()->getIterator()); + MSSAU.removeMemoryAccess(C); + C->eraseFromParent(); + } + } + return MadeChange; } } // end anonymous namespace diff --git a/llvm/test/Transforms/DeadStoreElimination/MSSA/memset-missing-debugloc.ll b/llvm/test/Transforms/DeadStoreElimination/MSSA/memset-missing-debugloc.ll --- a/llvm/test/Transforms/DeadStoreElimination/MSSA/memset-missing-debugloc.ll +++ b/llvm/test/Transforms/DeadStoreElimination/MSSA/memset-missing-debugloc.ll @@ -21,10 +21,9 @@ ; } -define dso_local i32 @_Z1av() !dbg !7 { +define dso_local i32 @_Z1av([5 x i64]* %b) !dbg !7 { entry: %retval = alloca i32, align 4 - %b = alloca [5 x i64], align 16 call void @llvm.dbg.declare(metadata [5 x i64]* %b, metadata !11, metadata !DIExpression()), !dbg !16 %0 = bitcast [5 x i64]* %b to i8*, !dbg !16 call void @llvm.memset.p0i8.i64(i8* align 16 %0, i8 0, i64 40, i1 false), !dbg !16 @@ -37,7 +36,7 @@ store i64 2, i64* %4, align 16, !dbg !16 %5 = getelementptr inbounds [5 x i64], [5 x i64]* %1, i32 0, i32 3, !dbg !16 store i64 2, i64* %5, align 8, !dbg !16 - %call = call i32 @_Z1av(), !dbg !17 + %call = call i32 @_Z1av([5 x i64]* %b), !dbg !17 %tobool = icmp ne i32 %call, 0, !dbg !17 br i1 %tobool, label %if.then, label %if.end, !dbg !19 diff --git a/llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-malloc-free.ll b/llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-malloc-free.ll --- a/llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-malloc-free.ll +++ b/llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-malloc-free.ll @@ -1,8 +1,5 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py -; XFAIL: * -; TODO: Handling of free not implemented yet. - ; RUN: opt < %s -basicaa -dse -enable-dse-memoryssa -S | FileCheck %s target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" diff --git a/llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-multipath.ll b/llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-multipath.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-multipath.ll @@ -0,0 +1,74 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -basicaa -dse -enable-dse-memoryssa -S | FileCheck %s + +target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" + +declare void @use(i32 *) + +define void @test4(i32* noalias %P, i1 %c1) { +; CHECK-LABEL: @test4( +; CHECK-NEXT: br i1 [[C1:%.*]], label [[BB1:%.*]], label [[BB2:%.*]] +; CHECK: bb1: +; CHECK-NEXT: store i32 0, i32* [[P:%.*]] +; CHECK-NEXT: br label [[BB5:%.*]] +; CHECK: bb2: +; CHECK-NEXT: store i32 3, i32* [[P]] +; CHECK-NEXT: br label [[BB5]] +; CHECK: bb5: +; CHECK-NEXT: call void @use(i32* [[P]]) +; CHECK-NEXT: ret void +; + store i32 1, i32* %P + br i1 %c1, label %bb1, label %bb2 + +bb1: + store i32 0, i32* %P + br label %bb5 +bb2: + store i32 3, i32* %P + br label %bb5 + +bb5: + call void @use(i32* %P) + ret void +} + +define void @test5(i32* noalias %P) { +; CHECK-LABEL: @test5( +; CHECK-NEXT: br i1 true, label [[BB1:%.*]], label [[BB2:%.*]] +; CHECK: bb1: +; CHECK-NEXT: store i32 0, i32* [[P:%.*]] +; CHECK-NEXT: br label [[BB5:%.*]] +; CHECK: bb2: +; CHECK-NEXT: br i1 undef, label [[BB3:%.*]], label [[BB4:%.*]] +; CHECK: bb3: +; CHECK-NEXT: store i32 3, i32* [[P]] +; CHECK-NEXT: br label [[BB5]] +; CHECK: bb4: +; CHECK-NEXT: store i32 5, i32* [[P]] +; CHECK-NEXT: br label [[BB5]] +; CHECK: bb5: +; CHECK-NEXT: call void @use(i32* [[P]]) +; CHECK-NEXT: ret void +; + store i32 1, i32* %P + br i1 true, label %bb1, label %bb2 +bb1: + store i32 0, i32* %P + br label %bb5 + +bb2: + br i1 undef, label %bb3, label %bb4 + +bb3: + store i32 3, i32* %P + br label %bb5 + +bb4: + store i32 5, i32* %P + br label %bb5 + +bb5: + call void @use(i32* %P) + ret void +} diff --git a/llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-simple.ll b/llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-simple.ll --- a/llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-simple.ll +++ b/llvm/test/Transforms/DeadStoreElimination/MSSA/multiblock-simple.ll @@ -141,7 +141,6 @@ define void @test11() { ; CHECK-LABEL: @test11( ; CHECK-NEXT: [[P:%.*]] = alloca i32 -; CHECK-NEXT: store i32 0, i32* [[P]] ; CHECK-NEXT: br i1 true, label [[BB1:%.*]], label [[BB2:%.*]] ; CHECK: bb1: ; CHECK-NEXT: store i32 0, i32* [[P]] @@ -166,10 +165,9 @@ define void @test12(i32* %P) { ; CHECK-LABEL: @test12( -; CHECK-NEXT: store i32 0, i32* [[P:%.*]] ; CHECK-NEXT: br i1 true, label [[BB1:%.*]], label [[BB2:%.*]] ; CHECK: bb1: -; CHECK-NEXT: store i32 1, i32* [[P]] +; CHECK-NEXT: store i32 1, i32* [[P:%.*]] ; CHECK-NEXT: br label [[BB3:%.*]] ; CHECK: bb2: ; CHECK-NEXT: store i32 1, i32* [[P]] @@ -192,10 +190,9 @@ define void @test13(i32* %P) { ; CHECK-LABEL: @test13( -; CHECK-NEXT: store i32 0, i32* [[P:%.*]] ; CHECK-NEXT: br i1 true, label [[BB1:%.*]], label [[BB2:%.*]] ; CHECK: bb1: -; CHECK-NEXT: store i32 1, i32* [[P]] +; CHECK-NEXT: store i32 1, i32* [[P:%.*]] ; CHECK-NEXT: br label [[BB3:%.*]] ; CHECK: bb2: ; CHECK-NEXT: store i32 1, i32* [[P]] diff --git a/llvm/test/Transforms/DeadStoreElimination/MSSA/simple.ll b/llvm/test/Transforms/DeadStoreElimination/MSSA/simple.ll --- a/llvm/test/Transforms/DeadStoreElimination/MSSA/simple.ll +++ b/llvm/test/Transforms/DeadStoreElimination/MSSA/simple.ll @@ -114,7 +114,7 @@ ; CHECK-NEXT: [[P_4:%.*]] = getelementptr i8, i8* [[P:%.*]], i64 4 ; CHECK-NEXT: [[TMP:%.*]] = load i8, i8* [[P_4]], align 1 ; CHECK-NEXT: store i8 0, i8* [[P_4]], align 1 -; CHECK-NEXT: [[Q:%.*]] = call i8* @strdup(i8* [[P]]) #6 +; CHECK-NEXT: [[Q:%.*]] = call i8* @strdup(i8* [[P]]) #7 ; CHECK-NEXT: store i8 [[TMP]], i8* [[P_4]], align 1 ; CHECK-NEXT: ret i8* [[Q]] ;