[PATCH] D138641: [reg2mem] Add special handling to CatchSwitchInst

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 8 05:42:35 PST 2022


nikic added a comment.

Can you please upload the patch with full context (`-U99999`)?



================
Comment at: llvm/lib/Transforms/Utils/DemoteRegToStack.cpp:100
+    if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(InsertPt)) {
+      for (BasicBlock *Handler : CSI->handlers()) {
+        new StoreInst(&I, Slot, &*Handler->getFirstInsertionPt());
----------------
You can probably combine handlers and unwind dest by iterating over `successors(CSI)`.


================
Comment at: llvm/lib/Transforms/Utils/DemoteRegToStack.cpp:158
+    // We need a separate load before each actual use of the PHI
+    SmallVector<Instruction *, 4> users;
+    for (User *U : P->users()) {
----------------
Why does this insert a load before uses, rather than doing the same as in the previous function (insert load in each successor)?


================
Comment at: llvm/test/Transforms/Reg2Mem/catchswitch-crach2.ll:1
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=reg2mem -S < %s | FileCheck %s
----------------
Typo in file name: crach -> crash?


================
Comment at: llvm/test/Transforms/Reg2Mem/catchswitch-crach2.ll:8
+%"type5" = type { i32 (...)** }
+%"type6" = type { i8*, i8 }
+
----------------
Can you please convert the test to use opaque pointers? This will remove these types.

I'd also replace the invoke undef with an invoke of a real function, to avoid UB. Something like this:

```
declare i32 @__CxxFrameHandler3(...)
declare i64 @fn() 

define void @testreg2mem(ptr %_Val) personality ptr @__CxxFrameHandler3 {
entry:
  %call.i166167 = invoke i64 @fn()
  to label %if.end56 unwind label %catch.dispatch

if.end56:                                         ; preds = %entry
  invoke void @"extfunc1"()
  to label %invoke.cont75 unwind label %catch.dispatch

invoke.cont75:                                    ; preds = %if.end56
  unreachable

catch.dispatch:                                   ; preds = %if.end56, %entry
  %_State.3 = phi i32 [ undef, %if.end56 ], [ 0, %entry ]
  %0 = catchswitch within none [label %catch] unwind label %ehcleanup105

catch:                                            ; preds = %catch.dispatch
  %1 = catchpad within %0 [ptr null, i32 64, ptr null]
  invoke void @"extfunc2"() [ "funclet"(token %1) ]
  to label %invoke.cont98 unwind label %ehcleanup105

invoke.cont98:                                    ; preds = %catch
  catchret from %1 to label %if.end99

if.end99:                                         ; preds = %invoke.cont98
  %or.i = or i32 undef, %_State.3
  unreachable

ehcleanup105:                                     ; preds = %catch, %catch.dispatch
  %2 = cleanuppad within none []
  cleanupret from %2 unwind to caller
}

declare void @"extfunc1"()

declare void @"extfunc2"()

```


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138641/new/

https://reviews.llvm.org/D138641



More information about the llvm-commits mailing list