[SimplifyCFG] Teach FoldCondBranchOnPHI() to preserve DomTree
authorRoman Lebedev <lebedev.ri@gmail.com>
Tue, 29 Dec 2020 19:58:28 +0000 (22:58 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Tue, 29 Dec 2020 21:48:11 +0000 (00:48 +0300)
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/LoopVectorize/if-pred-stores.ll
llvm/test/Transforms/SimplifyCFG/2004-12-10-SimplifyCFGCrash.ll
llvm/test/Transforms/SimplifyCFG/X86/critedge-assume.ll
llvm/test/Transforms/SimplifyCFG/branch-phi-thread.ll
llvm/test/Transforms/SimplifyCFG/debug-info-thread-phi.ll
llvm/test/Transforms/SimplifyCFG/pr46638.ll
llvm/test/Transforms/SimplifyCFG/unprofitable-pr.ll

index 2fcfc557393a9ce6df3d562a8d0d00cfe9c14270..95a5de09c18f473c4ef536c4e7cb0ef7644bab5f 100644 (file)
@@ -2344,8 +2344,8 @@ static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
 /// If we have a conditional branch on a PHI node value that is defined in the
 /// same block as the branch and if any PHI entries are constants, thread edges
 /// corresponding to that entry to be branches to their ultimate destination.
-static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL,
-                                AssumptionCache *AC) {
+static bool FoldCondBranchOnPHI(BranchInst *BI, DomTreeUpdater *DTU,
+                                const DataLayout &DL, AssumptionCache *AC) {
   BasicBlock *BB = BI->getParent();
   PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
   // NOTE: we currently cannot transform this case if the PHI node is used
@@ -2381,6 +2381,8 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL,
     if (isa<IndirectBrInst>(PredBB->getTerminator()))
       continue;
 
+    SmallVector<DominatorTree::UpdateType, 3> Updates;
+
     // The dest block might have PHI nodes, other predecessors and other
     // difficult cases.  Instead of being smart about this, just insert a new
     // block that jumps to the destination block, effectively splitting
@@ -2389,6 +2391,7 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL,
         BasicBlock::Create(BB->getContext(), RealDest->getName() + ".critedge",
                            RealDest->getParent(), RealDest);
     BranchInst *CritEdgeBranch = BranchInst::Create(RealDest, EdgeBB);
+    Updates.push_back({DominatorTree::Insert, EdgeBB, RealDest});
     CritEdgeBranch->setDebugLoc(BI->getDebugLoc());
 
     // Update PHI nodes.
@@ -2447,8 +2450,14 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL,
         PredBBTI->setSuccessor(i, EdgeBB);
       }
 
+    Updates.push_back({DominatorTree::Delete, PredBB, BB});
+    Updates.push_back({DominatorTree::Insert, PredBB, EdgeBB});
+
+    if (DTU)
+      DTU->applyUpdatesPermissive(Updates);
+
     // Recurse, simplifying any other constants.
-    return FoldCondBranchOnPHI(BI, DL, AC) || true;
+    return FoldCondBranchOnPHI(BI, DTU, DL, AC) || true;
   }
 
   return false;
@@ -6331,7 +6340,7 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
   // through this block if any PHI node entries are constants.
   if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
     if (PN->getParent() == BI->getParent())
-      if (FoldCondBranchOnPHI(BI, DL, Options.AC))
+      if (FoldCondBranchOnPHI(BI, DTU, DL, Options.AC))
         return requestResimplify();
 
   // Scan predecessor blocks for conditional branches.
index 795df0caedd63646713632c136eb8d488736d455..3c8d07356632b5e5137989e3800b7e54524963be 100644 (file)
@@ -1,7 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -vectorize-num-stores-pred=1 -force-vector-width=1 -force-vector-interleave=2 -loop-vectorize -verify-loop-info -simplifycfg < %s | FileCheck %s --check-prefix=UNROLL
+; RUN: opt -S -vectorize-num-stores-pred=1 -force-vector-width=1 -force-vector-interleave=2 -loop-vectorize -verify-loop-info -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck %s --check-prefix=UNROLL
 ; RUN: opt -S -vectorize-num-stores-pred=1 -force-vector-width=1 -force-vector-interleave=2 -loop-vectorize -verify-loop-info < %s | FileCheck %s --check-prefix=UNROLL-NOSIMPLIFY
-; RUN: opt -S -vectorize-num-stores-pred=1 -force-vector-width=2 -force-vector-interleave=1 -loop-vectorize -verify-loop-info -simplifycfg < %s | FileCheck %s --check-prefix=VEC
+; RUN: opt -S -vectorize-num-stores-pred=1 -force-vector-width=2 -force-vector-interleave=1 -loop-vectorize -verify-loop-info -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck %s --check-prefix=VEC
 
 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
 
index fafe73b2b4efeefa42603ebdc9fe6d4fdc26290c..69803400a683c044bb9b6c72250cc2d99225a0f1 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -disable-output
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -disable-output
 
 define void @symhash_add() {
 entry:
index 42ce5a561cc664f0286e9bcdedacf7cc0070ff90..22e01782e50192737c30a01c36356f83e9159dab 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: opt -o %t %s -instcombine -simplifycfg -thinlto-bc -verify-assumption-cache
+; RUN: opt -o %t %s -instcombine -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -thinlto-bc -verify-assumption-cache
 ; RUN: llvm-dis -o - %t | FileCheck %s
 
 ; Test that the simplifycfg pass correctly updates the assumption cache
index 4c1b7e68e25f38e9b8e9cd083b2f3c9875352cb6..366c2fb44b8c9be38c3ac9b5ebfd3fea3093af26 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -adce -S | \
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -adce -S | \
 ; RUN:   not grep "call void @f1"
 ; END.
 
index c5eb43642bf8a69c5676d0e621676ed94fe2f7e7..83a303c30fd9132222b0d84780109f43c59075b2 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: opt %s -debugify -simplifycfg -S | FileCheck %s
+; RUN: opt %s -debugify -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
 ; Tests Bug 37966
 
 define void @bar(i32 %aa) {
index ba7ce88cf6ad2117bf38ac53864ef966e58dae09..bdc2848239a3231eee19f1b74f538712b819603b 100644 (file)
@@ -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 void @pr46638(i1 %c, i32 %x) {
 ; CHECK-LABEL: @pr46638(
index 31ed7e203c45e021c944328d5b695107a563fe05..283922f015ed4e416ca13396389ad8be811ec361 100644 (file)
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -simplifycfg -simplifycfg-max-small-block-size=10 -S < %s | FileCheck %s
+; RUN: opt -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -simplifycfg-max-small-block-size=10 -S < %s | FileCheck %s
 ; RUN: opt -passes=simplify-cfg -simplifycfg-max-small-block-size=10 -S < %s | FileCheck %s
 
 target datalayout = "e-p:64:64-p5:32:32-A5"