[SimplifyCFG] 'merge compatible invokes': don't create trivial PHI's with all-identic...
authorRoman Lebedev <lebedev.ri@gmail.com>
Tue, 8 Feb 2022 18:16:39 +0000 (21:16 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Tue, 8 Feb 2022 18:29:38 +0000 (21:29 +0300)
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/X86/merge-compatible-invokes-of-landingpad.ll

index dba67d4..2f67573 100644 (file)
@@ -2438,14 +2438,16 @@ static void MergeCompatibleInvokesImpl(ArrayRef<InvokeInst *> Invokes,
 
   // Form the merged data operands for the merged invoke.
   for (Use &U : MergedInvoke->data_ops()) {
-    Type *Ty = U->getType();
-    if (Ty->isTokenTy())
-      continue; // Keep this arg as-is, we've checked that all the invokes
-                // recieve the *same* token value.
-
-    // Otherwise, simply form a PHI out of all the data ops under this index.
-    PHINode *PN = PHINode::Create(Ty, /*NumReservedValues=*/Invokes.size(), "",
-                                  MergedInvoke);
+    // Don't create trivial PHI's with all-identical incoming values.
+    bool NeedPHI = any_of(Invokes, [&U](InvokeInst *II) {
+      return II->getOperand(U.getOperandNo()) != U.get();
+    });
+    if (!NeedPHI)
+      continue;
+
+    // Form a PHI out of all the data ops under this index.
+    PHINode *PN = PHINode::Create(
+        U->getType(), /*NumReservedValues=*/Invokes.size(), "", MergedInvoke);
     for (InvokeInst *II : Invokes) {
       Use *IVU = II->data_operands_begin() + MergedInvoke->getDataOperandNo(&U);
       PN->addIncoming(IVU->get(), II->getParent());
index 49c2b72..fb44442 100644 (file)
@@ -696,8 +696,7 @@ define void @t12_arguments_are_fine() personality i8* bitcast (i32 (...)* @__gxx
 ; CHECK-NEXT:    [[C1:%.*]] = call i1 @cond()
 ; CHECK-NEXT:    br i1 [[C1]], label [[IF_THEN1_INVOKE]], label [[IF_END:%.*]]
 ; CHECK:       if.then1.invoke:
-; CHECK-NEXT:    [[TMP0:%.*]] = phi i32 [ 42, [[IF_ELSE]] ], [ 42, [[ENTRY:%.*]] ]
-; CHECK-NEXT:    invoke void @simple_throw_taking_argument(i32 [[TMP0]])
+; CHECK-NEXT:    invoke void @simple_throw_taking_argument(i32 42)
 ; CHECK-NEXT:    to label [[IF_THEN1_CONT:%.*]] unwind label [[LPAD:%.*]]
 ; CHECK:       if.then1.cont:
 ; CHECK-NEXT:    unreachable
@@ -1136,8 +1135,7 @@ define void @t19_compatible_operand_bundle() personality i8* bitcast (i32 (...)*
 ; CHECK-NEXT:    [[C1:%.*]] = call i1 @cond()
 ; CHECK-NEXT:    br i1 [[C1]], label [[IF_THEN1_INVOKE]], label [[IF_END:%.*]]
 ; CHECK:       if.then1.invoke:
-; CHECK-NEXT:    [[TMP0:%.*]] = phi i32 [ 42, [[IF_ELSE]] ], [ 42, [[ENTRY:%.*]] ]
-; CHECK-NEXT:    invoke void @simple_throw() [ "abc"(i32 [[TMP0]]) ]
+; CHECK-NEXT:    invoke void @simple_throw() [ "abc"(i32 42) ]
 ; CHECK-NEXT:    to label [[IF_THEN1_CONT:%.*]] unwind label [[LPAD:%.*]]
 ; CHECK:       if.then1.cont:
 ; CHECK-NEXT:    unreachable