From e22e7bcb2033cdff84b63466588c02679f69a2d1 Mon Sep 17 00:00:00 2001 From: zoecarver Date: Sun, 19 Apr 2020 12:36:35 -0700 Subject: [PATCH] [LVA] Support access instructions in DCE. Simple check for the following pattern: x = begin_access end_access x --- .../Transforms/DeadCodeElimination.cpp | 9 +++++++++ test/SILOptimizer/dead_code_elimination.sil | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/SILOptimizer/Transforms/DeadCodeElimination.cpp b/lib/SILOptimizer/Transforms/DeadCodeElimination.cpp index 263af770e1443..0eca4b14568be 100644 --- a/lib/SILOptimizer/Transforms/DeadCodeElimination.cpp +++ b/lib/SILOptimizer/Transforms/DeadCodeElimination.cpp @@ -40,6 +40,11 @@ namespace { // FIXME: Reconcile the similarities between this and // isInstructionTriviallyDead. static bool seemsUseful(SILInstruction *I) { + // begin_access is defined to have side effects, but this is not relevant for + // DCE. + if (isa(I)) + return false; + if (I->mayHaveSideEffects()) return true; @@ -258,6 +263,10 @@ void DCE::markLive(SILFunction &F) { } continue; } + if (auto *endAccess = dyn_cast(&I)) { + addReverseDependency(endAccess->getBeginAccess(), &I); + continue; + } if (seemsUseful(&I)) markValueLive(&I); } diff --git a/test/SILOptimizer/dead_code_elimination.sil b/test/SILOptimizer/dead_code_elimination.sil index 07db0a890b3cf..3e93dbc4e2986 100644 --- a/test/SILOptimizer/dead_code_elimination.sil +++ b/test/SILOptimizer/dead_code_elimination.sil @@ -259,3 +259,21 @@ bb2: bb3: br bb1 } + +// Check that DCE eliminates dead access instructions. +// CHECK-LABEL: sil @dead_access +// CHECK: bb0 +// CHECK-NEXT: tuple +// CHECK-NEXT: return +// CHECK-LABEL: end sil function 'dead_access' +sil @dead_access : $@convention(thin) (@in Container) -> () { +bb0(%0 : $*Container): + %1 = begin_access [modify] [dynamic] %0 : $*Container + end_access %1 : $*Container + + %3 = begin_access [read] [static] %0 : $*Container + end_access %3 : $*Container + + %999 = tuple () + return %999 : $() +}