[llvm-branch-commits] [llvm] a14c36f - [SimplifyCFG] switchToSelect(): don't forget to insert DomTree edge iff needed

Roman Lebedev via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Jan 15 12:41:47 PST 2021


Author: Roman Lebedev
Date: 2021-01-15T23:35:57+03:00
New Revision: a14c36fe27f5c36de44049237011d140a7277774

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

LOG: [SimplifyCFG] switchToSelect(): don't forget to insert DomTree edge iff needed

DestBB might or might not already be a successor of SelectBB,
and it wasn't we need to ensure that we record the fact in DomTree.

The testcase used to crash in lazy domtree updater mode + non-per-function
domtree validity checks disabled.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 4c0427e103f7..559830ed9a17 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -5394,20 +5394,25 @@ static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI,
                                               Value *SelectValue,
                                               IRBuilder<> &Builder,
                                               DomTreeUpdater *DTU) {
+  std::vector<DominatorTree::UpdateType> Updates;
+
   BasicBlock *SelectBB = SI->getParent();
+  BasicBlock *DestBB = PHI->getParent();
+
+  if (!is_contained(predecessors(DestBB), SelectBB))
+    Updates.push_back({DominatorTree::Insert, SelectBB, DestBB});
+  Builder.CreateBr(DestBB);
+
+  // Remove the switch.
+
   while (PHI->getBasicBlockIndex(SelectBB) >= 0)
     PHI->removeIncomingValue(SelectBB);
   PHI->addIncoming(SelectValue, SelectBB);
 
-  Builder.CreateBr(PHI->getParent());
-
-  std::vector<DominatorTree::UpdateType> Updates;
-
-  // Remove the switch.
   for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
     BasicBlock *Succ = SI->getSuccessor(i);
 
-    if (Succ == PHI->getParent())
+    if (Succ == DestBB)
       continue;
     Succ->removePredecessor(SelectBB);
     Updates.push_back({DominatorTree::Delete, SelectBB, Succ});

diff  --git a/llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll b/llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll
index 7f3428ba8c7b..72e887502694 100644
--- a/llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=0 -sink-common-insts=1 < %s | FileCheck %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -sink-common-insts=1 < %s | FileCheck %s
 
 declare void @widget(i8)
 


        


More information about the llvm-branch-commits mailing list