[reg2mem] Skip non-sized Instructions (PR58890)
authorHanSheng Zhang <admin@mayuyu.io>
Mon, 14 Nov 2022 11:45:23 +0000 (12:45 +0100)
committerNikita Popov <npopov@redhat.com>
Mon, 14 Nov 2022 11:47:39 +0000 (12:47 +0100)
We can only convert sized values into alloca/load/store, skip
instructions returning other types.

Fixes https://github.com/llvm/llvm-project/issues/58890.

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

llvm/lib/Transforms/Scalar/Reg2Mem.cpp
llvm/test/Transforms/Reg2Mem/catchswitch-crash.ll [new file with mode: 0644]
llvm/test/Transforms/Reg2Mem/non-token-test.ll [new file with mode: 0644]

index 9dc6449..db7a1f2 100644 (file)
@@ -40,6 +40,9 @@ STATISTIC(NumRegsDemoted, "Number of registers demoted");
 STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
 
 static bool valueEscapes(const Instruction &Inst) {
+  if (!Inst.getType()->isSized())
+    return false;
+
   const BasicBlock *BB = Inst.getParent();
   for (const User *U : Inst.users()) {
     const Instruction *UI = cast<Instruction>(U);
diff --git a/llvm/test/Transforms/Reg2Mem/catchswitch-crash.ll b/llvm/test/Transforms/Reg2Mem/catchswitch-crash.ll
new file mode 100644 (file)
index 0000000..9d2347d
--- /dev/null
@@ -0,0 +1,35 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=reg2mem -S < %s | FileCheck %s
+
+declare void @"read_mem"()
+
+define void @"memcpy_seh"() personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*) {
+; CHECK-LABEL: @memcpy_seh(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    %"reg2mem alloca point" = bitcast i32 0 to i32
+; CHECK-NEXT:    invoke void @read_mem()
+; CHECK-NEXT:    to label [[CLEANUP:%.*]] unwind label [[CATCH_DISPATCH:%.*]]
+; CHECK:       catch.dispatch:
+; CHECK-NEXT:    [[TMP0:%.*]] = catchswitch within none [label %__except] unwind to caller
+; CHECK:       __except:
+; CHECK-NEXT:    [[TMP1:%.*]] = catchpad within [[TMP0]] [i8* null]
+; CHECK-NEXT:    unreachable
+; CHECK:       cleanup:
+; CHECK-NEXT:    ret void
+;
+entry:
+  invoke void @"read_mem"()
+  to label %cleanup unwind label %catch.dispatch
+
+catch.dispatch:                                   ; preds = %entry
+  %0 = catchswitch within none [label %__except] unwind to caller
+
+__except:                                         ; preds = %catch.dispatch
+  %1 = catchpad within %0 [i8* null]
+  unreachable
+
+cleanup:                                          ; preds = %entry
+  ret void
+}
+
+declare i32 @__C_specific_handler(...)
diff --git a/llvm/test/Transforms/Reg2Mem/non-token-test.ll b/llvm/test/Transforms/Reg2Mem/non-token-test.ll
new file mode 100644 (file)
index 0000000..e30f37a
--- /dev/null
@@ -0,0 +1,24 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=reg2mem -S < %s | FileCheck %s
+
+%opaque = type opaque
+
+declare %opaque @ret_opaque()
+declare void @pass_opaque(%opaque)
+
+define void @test() {
+; CHECK-LABEL: @test(
+; CHECK-NEXT:    %"reg2mem alloca point" = bitcast i32 0 to i32
+; CHECK-NEXT:    [[X:%.*]] = call [[OPAQUE:%.*]] @ret_opaque()
+; CHECK-NEXT:    br label [[NEXT:%.*]]
+; CHECK:       next:
+; CHECK-NEXT:    call void @pass_opaque([[OPAQUE]] [[X]])
+; CHECK-NEXT:    ret void
+;
+  %x = call %opaque @ret_opaque()
+  br label %next
+
+next:
+  call void @pass_opaque(%opaque %x)
+  ret void
+}