Files
clang-p2996/llvm/test/Transforms/SROA/phi-catchswitch.ll
Roman Lebedev 4f7e5d2206 [SROA] For non-speculatable loads of selects -- split block, insert then/else blocks, form two-entry PHI node, take 2
Currently, SROA is CFG-preserving.
Not doing so does not affect any pipeline test. (???)
Internally, SROA requires Dominator Tree, and uses it solely for the final `-mem2reg` call.

By design, we can't really SROA alloca if their address escapes somehow,
but we have logic to deal with `load` of `select`/`PHI`,
where at least one of the possible addresses prevents promotion,
by speculating the `load`s and `select`ing between loaded values.

As one would expect, that requires ensuring that the speculation is actually legal.
Even ignoring complexity bailouts, that logic does not deal with everything,
e.g. `isSafeToLoadUnconditionally()` does not recurse into hands of `select`.
There can also be cases where the load is genuinely non-speculate.

So if we can't prove that the load can be speculated,
unfold the select, produce two-entry phi node, and perform predicated load.

Now, that transformation must obviously update Dominator Tree,
since we require it later on. Doing so is trivial.
Additionally, we don't want to do this for the final SROA invocation (D136806).

In the end, this ends up having negative (!) compile-time cost:
https://llvm-compile-time-tracker.com/compare.php?from=c6d7e80ec4c17a415673b1cfd25924f98ac83608&to=ddf9600365093ea50d7e278696cbfa01641c959d&stat=instructions:u

Though indeed, this only deals with `select`s, `PHI`s are still using speculation.

Should we update some more analysis?

Reviewed By: arsenm

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

This reverts commit 739611870d,
and recommits 03e6d9d9d1
with a fixed assertion - we should check that DTU is there,
not just assert false...
2022-12-08 20:19:55 +03:00

70 lines
2.8 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes='sroa<preserve-cfg>' -S | FileCheck %s --check-prefixes=CHECK,CHECK-PRESERVE-CFG
; RUN: opt < %s -passes='sroa<modify-cfg>' -S | FileCheck %s --check-prefixes=CHECK,CHECK-MODIFY-CFG
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
target triple = "wasm32-unknown-unknown"
%struct.foo = type { i32 }
declare i32 @__gxx_wasm_personality_v0(...)
declare void @foo()
; Tests if the SROA pass correctly bails out on rewriting PHIs in a catchswitch
; BB.
define void @test_phi_catchswitch() personality ptr @__gxx_wasm_personality_v0 {
; CHECK-LABEL: @test_phi_catchswitch(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP:%.*]] = alloca [[STRUCT_FOO:%.*]], align 4
; CHECK-NEXT: invoke void @foo()
; CHECK-NEXT: to label [[BB3:%.*]] unwind label [[BB10:%.*]]
; CHECK: bb3:
; CHECK-NEXT: invoke void @foo()
; CHECK-NEXT: to label [[BB9:%.*]] unwind label [[CATCH_DISPATCH:%.*]]
; CHECK: catch.dispatch:
; CHECK-NEXT: [[TMP5:%.*]] = phi ptr [ [[TMP]], [[BB3]] ]
; CHECK-NEXT: [[TMP6:%.*]] = catchswitch within none [label %catch.start] unwind label [[BB10]]
; CHECK: catch.start:
; CHECK-NEXT: [[TMP8:%.*]] = catchpad within [[TMP6]] [ptr null]
; CHECK-NEXT: unreachable
; CHECK: bb9:
; CHECK-NEXT: unreachable
; CHECK: bb10:
; CHECK-NEXT: [[TMP11:%.*]] = phi ptr [ [[TMP]], [[ENTRY:%.*]] ], [ [[TMP5]], [[CATCH_DISPATCH]] ]
; CHECK-NEXT: [[TMP12:%.*]] = cleanuppad within none []
; CHECK-NEXT: store i32 0, ptr [[TMP11]], align 4
; CHECK-NEXT: unreachable
;
entry:
%tmp = alloca %struct.foo, align 4
invoke void @foo()
to label %bb3 unwind label %bb10
bb3: ; preds = %entry
invoke void @foo()
to label %bb9 unwind label %catch.dispatch
catch.dispatch: ; preds = %bb3
; While rewriting the alloca in the entry BB, the SROA pass tries to insert a
; non-PHI instruction in this BB by calling getFirstInsertionPt(), which is
; not possible in a catchswitch BB. This test checks if we correctly bail out
; on these cases.
%tmp5 = phi ptr [ %tmp, %bb3 ]
%tmp6 = catchswitch within none [label %catch.start] unwind label %bb10
catch.start: ; preds = %catch.dispatch
%tmp8 = catchpad within %tmp6 [ptr null]
unreachable
bb9: ; preds = %bb3
unreachable
bb10: ; preds = %catch.dispatch, %entry
%tmp11 = phi ptr [ %tmp, %entry ], [ %tmp5, %catch.dispatch ]
%tmp12 = cleanuppad within none []
store i32 0, ptr %tmp11, align 4
unreachable
}
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
; CHECK-MODIFY-CFG: {{.*}}
; CHECK-PRESERVE-CFG: {{.*}}