[llvm-branch-commits] [llvm] a815b6b - [SimplifyCFG] Teach eliminateDeadSwitchCases() to preserve DomTree, part 1

Roman Lebedev via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Dec 31 16:30:37 PST 2020


Author: Roman Lebedev
Date: 2021-01-01T03:25:24+03:00
New Revision: a815b6b2b2cc483cfe9d2b520790322a39f8d37b

URL: https://github.com/llvm/llvm-project/commit/a815b6b2b2cc483cfe9d2b520790322a39f8d37b
DIFF: https://github.com/llvm/llvm-project/commit/a815b6b2b2cc483cfe9d2b520790322a39f8d37b.diff

LOG: [SimplifyCFG] Teach eliminateDeadSwitchCases() to preserve DomTree, part 1

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/test/Transforms/SimplifyCFG/switch-masked-bits.ll
    llvm/test/Transforms/SimplifyCFG/switch-simplify-crash.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 402b98efadad..2ff576b17751 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4908,7 +4908,8 @@ bool SimplifyCFGOpt::TurnSwitchRangeIntoICmp(SwitchInst *SI,
 
 /// Compute masked bits for the condition of a switch
 /// and use it to remove dead cases.
-static bool eliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC,
+static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
+                                     AssumptionCache *AC,
                                      const DataLayout &DL) {
   Value *Cond = SI->getCondition();
   unsigned Bits = Cond->getType()->getIntegerBitWidth();
@@ -4922,11 +4923,15 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC,
 
   // Gather dead cases.
   SmallVector<ConstantInt *, 8> DeadCases;
+  SmallMapVector<BasicBlock *, int, 8> NumPerSuccessorCases;
   for (auto &Case : SI->cases()) {
+    auto *Successor = Case.getCaseSuccessor();
+    ++NumPerSuccessorCases[Successor];
     const APInt &CaseVal = Case.getCaseValue()->getValue();
     if (Known.Zero.intersects(CaseVal) || !Known.One.isSubsetOf(CaseVal) ||
         (CaseVal.getMinSignedBits() > MaxSignificantBitsInCond)) {
       DeadCases.push_back(Case.getCaseValue());
+      --NumPerSuccessorCases[Successor];
       LLVM_DEBUG(dbgs() << "SimplifyCFG: switch case " << CaseVal
                         << " is dead.\n");
     }
@@ -4961,6 +4966,13 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC,
     SIW.removeCase(CaseI);
   }
 
+  std::vector<DominatorTree::UpdateType> Updates;
+  for (const std::pair<BasicBlock *, int> &I : NumPerSuccessorCases)
+    if (I.second == 0)
+      Updates.push_back({DominatorTree::Delete, SI->getParent(), I.first});
+  if (DTU)
+    DTU->applyUpdatesPermissive(Updates);
+
   return true;
 }
 
@@ -6132,7 +6144,7 @@ bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
     return requestResimplify();
 
   // Remove unreachable cases.
-  if (eliminateDeadSwitchCases(SI, Options.AC, DL))
+  if (eliminateDeadSwitchCases(SI, DTU, Options.AC, DL))
     return requestResimplify();
 
   if (switchToSelect(SI, Builder, DTU, DL, TTI))

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch-masked-bits.ll b/llvm/test/Transforms/SimplifyCFG/switch-masked-bits.ll
index 21cecc5c942b..1b8d6e70b7dd 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-masked-bits.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-masked-bits.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -simplifycfg < %s | FileCheck %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck %s
 
 define i32 @test1(i32 %x) nounwind {
 ; CHECK-LABEL: @test1(

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch-simplify-crash.ll b/llvm/test/Transforms/SimplifyCFG/switch-simplify-crash.ll
index bbc0bd78da7c..755ad0a16338 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-simplify-crash.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-simplify-crash.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -disable-output
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -disable-output
 
 define void @NewExtractNames() {
 entry:


        


More information about the llvm-branch-commits mailing list