Index: llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp =================================================================== --- llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -1910,8 +1910,18 @@ return true; }; + auto IsEntryBlockGEP = [](GEPOperator *GEP) { + if (auto *I = dyn_cast(GEP)) { + return I->getParent() == &I->getFunction()->getEntryBlock(); + } + return false; + }; + Ptr = Ptr->stripPointerCasts(); if (auto *GEP = dyn_cast(Ptr)) { + if (IsEntryBlockGEP(GEP)) + return true; + return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) && GEP->hasAllConstantIndices(); } Index: llvm/test/Transforms/DeadStoreElimination/MSSA/loop-invariant-entry-block.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/DeadStoreElimination/MSSA/loop-invariant-entry-block.ll @@ -0,0 +1,42 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -dse -S | FileCheck %s + +@BUFFER = external dso_local local_unnamed_addr global [0 x i8], align 1 + +define void @MissedDSEOpportunity(i64 %idx, i1* noalias %cc) { +; CHECK-LABEL: @MissedDSEOpportunity( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [0 x i8], [0 x i8]* @BUFFER, i64 0, i64 [[IDX:%.*]] +; CHECK-NEXT: [[BC:%.*]] = bitcast i8* [[GEP]] to i64* +; CHECK-NEXT: [[CC0:%.*]] = load volatile i1, i1* [[CC:%.*]], align 1 +; CHECK-NEXT: br i1 [[CC0]], label [[HEAD:%.*]], label [[EXIT:%.*]] +; CHECK: head: +; CHECK-NEXT: [[CC1:%.*]] = load volatile i1, i1* [[CC]], align 1 +; CHECK-NEXT: br i1 [[CC1]], label [[HEAD]], label [[EXIT_LOOPEXIT:%.*]] +; CHECK: exit.loopexit: +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: store i64 0, i64* [[BC]], align 4 +; CHECK-NEXT: ret void +; +entry: + ; The entry block cannot have predecessors or be part of a loop + %gep = getelementptr inbounds [0 x i8], [0 x i8]* @BUFFER, i64 0, i64 %idx + %bc = bitcast i8* %gep to i64* + %cc0 = load volatile i1, i1* %cc, align 1 + br i1 %cc0, label %head, label %exit + +head: ; preds = %entry, %head + %cc1 = load volatile i1, i1* %cc, align 1 + br i1 %cc1, label %head, label %exit.loopexit + +exit.loopexit: ; preds = %head + ; Dead store + store i64 2, i64* %bc, align 4 + br label %exit + +exit: ; preds = %exit.loopexit, %entry + ; Killer store + store i64 0, i64* %bc, align 4 + ret void +}