From b4429f3cdd1a8511bd014877b22d9679c4a74dbd Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Fri, 1 Jan 2021 21:58:12 +0300 Subject: [PATCH] [SimplifyCFG] Teach removeUndefIntroducingPredecessor to preserve DomTree --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 13 +++++++++---- .../misaligned_double_vector_store_not_fast.ll | 2 +- llvm/test/CodeGen/Hexagon/swp-epilog-phi10.ll | 2 +- llvm/test/CodeGen/Hexagon/swp-order-deps3.ll | 2 +- llvm/test/CodeGen/Hexagon/swp-reuse-phi-4.ll | 2 +- llvm/test/CodeGen/Thumb2/2009-08-04-CoalescerBug.ll | 2 +- 6 files changed, 14 insertions(+), 9 deletions(-) diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 37e594dbd1a4..fa12d8b99644 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -6563,14 +6563,16 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) { /// If BB has an incoming value that will always trigger undefined behavior /// (eg. null pointer dereference), remove the branch leading here. -static bool removeUndefIntroducingPredecessor(BasicBlock *BB) { +static bool removeUndefIntroducingPredecessor(BasicBlock *BB, + DomTreeUpdater *DTU) { for (PHINode &PHI : BB->phis()) for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) if (passingValueIsAlwaysUndefined(PHI.getIncomingValue(i), &PHI)) { - Instruction *T = PHI.getIncomingBlock(i)->getTerminator(); + BasicBlock *Predecessor = PHI.getIncomingBlock(i); + Instruction *T = Predecessor->getTerminator(); IRBuilder<> Builder(T); if (BranchInst *BI = dyn_cast(T)) { - BB->removePredecessor(PHI.getIncomingBlock(i)); + BB->removePredecessor(Predecessor); // Turn uncoditional branches into unreachables and remove the dead // destination from conditional branches. if (BI->isUnconditional()) @@ -6579,6 +6581,9 @@ static bool removeUndefIntroducingPredecessor(BasicBlock *BB) { Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1) : BI->getSuccessor(0)); BI->eraseFromParent(); + if (DTU) + DTU->applyUpdatesPermissive( + {{DominatorTree::Delete, Predecessor, BB}}); return true; } // TODO: SwitchInst. @@ -6611,7 +6616,7 @@ bool SimplifyCFGOpt::simplifyOnceImpl(BasicBlock *BB) { Changed |= EliminateDuplicatePHINodes(BB); // Check for and remove branches that will always cause undefined behavior. - Changed |= removeUndefIntroducingPredecessor(BB); + Changed |= removeUndefIntroducingPredecessor(BB, DTU); // Merge basic blocks into their predecessor if there is only one distinct // pred, and if there is only one distinct successor of the predecessor, and diff --git a/llvm/test/CodeGen/Hexagon/misaligned_double_vector_store_not_fast.ll b/llvm/test/CodeGen/Hexagon/misaligned_double_vector_store_not_fast.ll index ba867e68a95e..0945e17531be 100644 --- a/llvm/test/CodeGen/Hexagon/misaligned_double_vector_store_not_fast.ll +++ b/llvm/test/CodeGen/Hexagon/misaligned_double_vector_store_not_fast.ll @@ -1,4 +1,4 @@ -; RUN: llc -march=hexagon -O3 -debug-only=isel 2>&1 -simplifycfg-require-and-preserve-domtree=0 < %s | FileCheck %s +; RUN: llc -march=hexagon -O3 -debug-only=isel 2>&1 -simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck %s ; REQUIRES: asserts ; DAGCombiner converts the two vector stores to a double vector store, diff --git a/llvm/test/CodeGen/Hexagon/swp-epilog-phi10.ll b/llvm/test/CodeGen/Hexagon/swp-epilog-phi10.ll index 192f1356fe42..5b5b45d13210 100644 --- a/llvm/test/CodeGen/Hexagon/swp-epilog-phi10.ll +++ b/llvm/test/CodeGen/Hexagon/swp-epilog-phi10.ll @@ -1,4 +1,4 @@ -; RUN: llc -march=hexagon -mcpu=hexagonv5 -simplifycfg-require-and-preserve-domtree=0 < %s +; RUN: llc -march=hexagon -mcpu=hexagonv5 -simplifycfg-require-and-preserve-domtree=1 < %s ; REQUIRES: asserts define void @test(i8* noalias nocapture readonly %src, i32 %srcStride) local_unnamed_addr #0 { diff --git a/llvm/test/CodeGen/Hexagon/swp-order-deps3.ll b/llvm/test/CodeGen/Hexagon/swp-order-deps3.ll index 6f5245348187..e85d35549385 100644 --- a/llvm/test/CodeGen/Hexagon/swp-order-deps3.ll +++ b/llvm/test/CodeGen/Hexagon/swp-order-deps3.ll @@ -1,4 +1,4 @@ -; RUN: llc -march=hexagon -O2 -simplifycfg-require-and-preserve-domtree=0 < %s +; RUN: llc -march=hexagon -O2 -simplifycfg-require-and-preserve-domtree=1 < %s ; REQUIRES: asserts ; Function Attrs: noinline nounwind ssp diff --git a/llvm/test/CodeGen/Hexagon/swp-reuse-phi-4.ll b/llvm/test/CodeGen/Hexagon/swp-reuse-phi-4.ll index 197e02cfceb4..4a309d5e31e1 100644 --- a/llvm/test/CodeGen/Hexagon/swp-reuse-phi-4.ll +++ b/llvm/test/CodeGen/Hexagon/swp-reuse-phi-4.ll @@ -1,4 +1,4 @@ -; RUN: llc -march=hexagon -O2 -simplifycfg-require-and-preserve-domtree=0 < %s +; RUN: llc -march=hexagon -O2 -simplifycfg-require-and-preserve-domtree=1 < %s ; REQUIRES: asserts ; Test that we generate the correct Phi names in the epilog when we need diff --git a/llvm/test/CodeGen/Thumb2/2009-08-04-CoalescerBug.ll b/llvm/test/CodeGen/Thumb2/2009-08-04-CoalescerBug.ll index 93fcf745469d..20da9ba595b7 100644 --- a/llvm/test/CodeGen/Thumb2/2009-08-04-CoalescerBug.ll +++ b/llvm/test/CodeGen/Thumb2/2009-08-04-CoalescerBug.ll @@ -1,4 +1,4 @@ -; RUN: llc -simplifycfg-require-and-preserve-domtree=0 < %s -mtriple=thumbv7-apple-darwin -mcpu=cortex-a8 -relocation-model=pic -frame-pointer=all +; RUN: llc -simplifycfg-require-and-preserve-domtree=1 < %s -mtriple=thumbv7-apple-darwin -mcpu=cortex-a8 -relocation-model=pic -frame-pointer=all %0 = type { %struct.GAP } ; type %0 %1 = type { i16, i8, i8 } ; type %1