[llvm] 9986d60 - [SimplifyCFG] 'merge compatible invokes': support normal destination w/ PHIs but no uses

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 8 06:49:51 PST 2022


Author: Roman Lebedev
Date: 2022-02-08T17:49:38+03:00
New Revision: 9986d602241ddb22a8ec92f4ef4cf7071804df64

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

LOG: [SimplifyCFG] 'merge compatible invokes': support normal destination w/ PHIs but no uses

As long as the incoming values for all the invokes in the set
are identical, we can merge the invokes.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/test/Transforms/SimplifyCFG/X86/merge-compatible-invokes-of-landingpad.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index f039f87963bc..15995ffd6c34 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2307,9 +2307,10 @@ bool CompatibleSets::shouldBelongToSameSet(ArrayRef<InvokeInst *> Invokes) {
         return false;
     }
 
-    // In the normal destination, there must be no PHI nodes.
-    // FIXME: just check that the incoming values are compatible?
-    if (!empty(NormalBB->phis()))
+    // In the normal destination, the incoming values for these two `invoke`s
+    // must be compatible.
+    if (!IncomingValuesAreCompatible(
+            NormalBB, {Invokes[0]->getParent(), Invokes[1]->getParent()}))
       return false;
 
     // For now, simply don't deal with `invoke`s that have uses.
@@ -2333,7 +2334,7 @@ bool CompatibleSets::shouldBelongToSameSet(ArrayRef<InvokeInst *> Invokes) {
 #endif
 
   // In the unwind destination, the incoming values for these two `invoke`s
-  // must be compatible .
+  // must be compatible.
   if (!IncomingValuesAreCompatible(
           Invokes.front()->getUnwindDest(),
           {Invokes[0]->getParent(), Invokes[1]->getParent()}))
@@ -2445,12 +2446,12 @@ static void MergeCompatibleInvokesImpl(ArrayRef<InvokeInst *> Invokes,
     U.set(PN);
   }
 
-  // We've ensured that each PHI node in the `landingpad` has compatible
-  // (identical) incoming values when coming from each of the `invoke`s
-  // in the current merge set, so update the PHI nodes accordingly.
-  AddPredecessorToBlock(/*Succ=*/MergedInvoke->getUnwindDest(),
-                        /*NewPred=*/MergedInvoke->getParent(),
-                        /*ExistPred=*/Invokes.front()->getParent());
+  // We've ensured that each PHI node has compatible (identical) incoming values
+  // when coming from each of the `invoke`s in the current merge set,
+  // so update the PHI nodes accordingly.
+  for (BasicBlock *Succ : successors(MergedInvoke))
+    AddPredecessorToBlock(Succ, /*NewPred=*/MergedInvoke->getParent(),
+                          /*ExistPred=*/Invokes.front()->getParent());
 
   // And finally, replace the original `invoke`s with an unconditional branch
   // to the block with the merged `invoke`. Also, give that merged `invoke`

diff  --git a/llvm/test/Transforms/SimplifyCFG/X86/merge-compatible-invokes-of-landingpad.ll b/llvm/test/Transforms/SimplifyCFG/X86/merge-compatible-invokes-of-landingpad.ll
index 5fee3d8bb8d7..77d1afcbc746 100644
--- a/llvm/test/Transforms/SimplifyCFG/X86/merge-compatible-invokes-of-landingpad.ll
+++ b/llvm/test/Transforms/SimplifyCFG/X86/merge-compatible-invokes-of-landingpad.ll
@@ -1567,12 +1567,9 @@ define void @t26_phi_in_normal_dest_compatible_incoming_values() personality i8*
 ; CHECK-LABEL: @t26_phi_in_normal_dest_compatible_incoming_values(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[C0:%.*]] = call i1 @cond()
-; CHECK-NEXT:    br i1 [[C0]], label [[IF_THEN0:%.*]], label [[IF_ELSE0:%.*]]
-; CHECK:       if.then0:
-; CHECK-NEXT:    invoke void @maybe_throw()
-; CHECK-NEXT:    to label [[INVOKE_CONT:%.*]] unwind label [[LPAD:%.*]]
+; CHECK-NEXT:    br i1 [[C0]], label [[IF_THEN1_INVOKE:%.*]], label [[IF_ELSE0:%.*]]
 ; CHECK:       invoke.cont:
-; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 0, [[IF_THEN0]] ], [ 0, [[IF_THEN1:%.*]] ], [ -1, [[IF_THEN2:%.*]] ]
+; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ -1, [[IF_THEN2:%.*]] ], [ 0, [[IF_THEN1_INVOKE]] ]
 ; CHECK-NEXT:    call void @consume(i32 [[PHI]])
 ; CHECK-NEXT:    call void @sideeffect()
 ; CHECK-NEXT:    unreachable
@@ -1583,10 +1580,10 @@ define void @t26_phi_in_normal_dest_compatible_incoming_values() personality i8*
 ; CHECK-NEXT:    resume { i8*, i32 } [[EH]]
 ; CHECK:       if.else0:
 ; CHECK-NEXT:    [[C1:%.*]] = call i1 @cond()
-; CHECK-NEXT:    br i1 [[C1]], label [[IF_THEN1]], label [[IF_ELSE1:%.*]]
-; CHECK:       if.then1:
+; CHECK-NEXT:    br i1 [[C1]], label [[IF_THEN1_INVOKE]], label [[IF_ELSE1:%.*]]
+; CHECK:       if.then1.invoke:
 ; CHECK-NEXT:    invoke void @maybe_throw()
-; CHECK-NEXT:    to label [[INVOKE_CONT]] unwind label [[LPAD]]
+; CHECK-NEXT:    to label [[INVOKE_CONT:%.*]] unwind label [[LPAD:%.*]]
 ; CHECK:       if.else1:
 ; CHECK-NEXT:    [[C2:%.*]] = call i1 @cond()
 ; CHECK-NEXT:    br i1 [[C2]], label [[IF_THEN2]], label [[IF_END:%.*]]


        


More information about the llvm-commits mailing list