[llvm] 51fecd1 - [InstCombine] Don't combine PHI before catchswitch

Heejin Ahn via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 2 12:11:20 PDT 2021


Author: Heejin Ahn
Date: 2021-07-02T12:10:24-07:00
New Revision: 51fecd17bbe04ccf1551132b5fffac43139c7af8

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

LOG: [InstCombine] Don't combine PHI before catchswitch

This tries to bail out if the PHI is in a `catchswitch` BB in
InstCombine. A PHI cannot be combined into a non-PHI instruction if it
is in a `catchswitch` BB, because `catchswitch` BB cannot have any
non-PHI instruction other than `catchswitch` itself.

The given test case started crashing after D98058.

Reviewed By: lebedev.ri, rnk

Differential Revision: https://reviews.llvm.org/D105309

Added: 
    llvm/test/Transforms/InstCombine/catchswitch-phi.ll

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
index 6c000b8bb2450..6c6351c70e3a6 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
@@ -1317,8 +1317,9 @@ Instruction *InstCombinerImpl::visitPHINode(PHINode &PN) {
       return Result;
 
   // If the incoming values are pointer casts of the same original value,
-  // replace the phi with a single cast.
-  if (PN.getType()->isPointerTy()) {
+  // replace the phi with a single cast iff we can insert a non-PHI instruction.
+  if (PN.getType()->isPointerTy() &&
+      PN.getParent()->getFirstInsertionPt() != PN.getParent()->end()) {
     Value *IV0 = PN.getIncomingValue(0);
     Value *IV0Stripped = IV0->stripPointerCasts();
     // Set to keep track of values known to be equal to IV0Stripped after

diff  --git a/llvm/test/Transforms/InstCombine/catchswitch-phi.ll b/llvm/test/Transforms/InstCombine/catchswitch-phi.ll
new file mode 100644
index 0000000000000..28695704097b2
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/catchswitch-phi.ll
@@ -0,0 +1,53 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128-ni:1"
+target triple = "wasm32-unknown-unknown"
+
+%struct.quux = type { i32 }
+%struct.blam = type <{ %struct.quux }>
+
+declare void @foo()
+declare void @bar(%struct.quux*)
+declare i32 @__gxx_wasm_personality_v0(...)
+
+define void @test() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
+bb:
+  %tmp0 = alloca %struct.blam, align 4
+  br i1 undef, label %bb1, label %bb2
+
+bb1:                                              ; preds = %bb
+  %tmp1 = getelementptr inbounds %struct.blam, %struct.blam* %tmp0, i32 0, i32 0
+  invoke void @foo()
+          to label %bb3 unwind label %bb4
+
+bb2:                                              ; preds = %bb
+  %tmp2 = getelementptr inbounds %struct.blam, %struct.blam* %tmp0, i32 0, i32 0
+  invoke void @foo()
+          to label %bb3 unwind label %bb4
+
+bb3:                                              ; preds = %bb2, %bb1
+  unreachable
+
+bb4:                                              ; preds = %bb2, %bb1
+  ; This PHI should not be combined into a non-PHI instruction, because
+  ; catchswitch BB cannot have any non-PHI instruction other than catchswitch
+  ; itself.
+  ; CHECK: bb4:
+  ; CHECK-NEXT: phi
+  ; CHECK-NEXT: catchswitch
+  %tmp3 = phi %struct.quux* [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
+  %tmp4 = catchswitch within none [label %bb5] unwind label %bb7
+
+bb5:                                              ; preds = %bb4
+  %tmp5 = catchpad within %tmp4 [i8* null]
+  invoke void @foo() [ "funclet"(token %tmp5) ]
+          to label %bb6 unwind label %bb7
+
+bb6:                                              ; preds = %bb5
+  unreachable
+
+bb7:                                              ; preds = %bb5, %bb4
+  %tmp6 = cleanuppad within none []
+  call void @bar(%struct.quux* %tmp3) [ "funclet"(token %tmp6) ]
+  unreachable
+}


        


More information about the llvm-commits mailing list