[Test] Add more sophisticated tests for switch UB opt

Optimizer is being too smart with existing tests, and the transform
gets concealed by following transforms.
This commit is contained in:
Max Kazantsev 2021-09-13 15:27:33 +07:00
parent f29336104d
commit 7e337d8ba2
1 changed files with 130 additions and 0 deletions

View File

@ -2,6 +2,10 @@
; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s ; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s ; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
declare void @foo_01()
declare void @foo_02()
declare void @foo_03()
; TODO: Basing on fact that load(null) is UB, we can remove edge pred->bb. ; TODO: Basing on fact that load(null) is UB, we can remove edge pred->bb.
define i32 @test_01(i32* %p, i32 %x, i1 %cond) { define i32 @test_01(i32* %p, i32 %x, i1 %cond) {
; CHECK-LABEL: @test_01( ; CHECK-LABEL: @test_01(
@ -73,3 +77,129 @@ bb:
other_succ: other_succ:
ret i32 0 ret i32 0
} }
; TODO: Basing on fact that load(null) is UB, we can remove edge pred->bb.
define i32 @test_03(i32* %p, i32 %x, i1 %cond) {
; CHECK-LABEL: @test_03(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[BB:%.*]], label [[PRED:%.*]]
; CHECK: pred:
; CHECK-NEXT: switch i32 [[X:%.*]], label [[BB]] [
; CHECK-NEXT: i32 42, label [[COMMON_RET:%.*]]
; CHECK-NEXT: i32 123456, label [[COMMON_RET]]
; CHECK-NEXT: i32 -654321, label [[COMMON_RET]]
; CHECK-NEXT: i32 1, label [[DO_1:%.*]]
; CHECK-NEXT: i32 2, label [[DO_2:%.*]]
; CHECK-NEXT: i32 3, label [[DO_3:%.*]]
; CHECK-NEXT: ]
; CHECK: common.ret:
; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ [[R:%.*]], [[BB]] ], [ 1, [[DO_1]] ], [ 1, [[DO_2]] ], [ 1, [[DO_3]] ], [ 0, [[PRED]] ], [ 0, [[PRED]] ], [ 0, [[PRED]] ]
; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
; CHECK: bb:
; CHECK-NEXT: [[PHI:%.*]] = phi i32* [ null, [[PRED]] ], [ [[P:%.*]], [[ENTRY:%.*]] ]
; CHECK-NEXT: [[R]] = load i32, i32* [[PHI]], align 4
; CHECK-NEXT: br label [[COMMON_RET]]
; CHECK: do_1:
; CHECK-NEXT: call void @foo_01()
; CHECK-NEXT: br label [[COMMON_RET]]
; CHECK: do_2:
; CHECK-NEXT: call void @foo_02()
; CHECK-NEXT: br label [[COMMON_RET]]
; CHECK: do_3:
; CHECK-NEXT: call void @foo_03()
; CHECK-NEXT: br label [[COMMON_RET]]
;
entry:
br i1 %cond, label %bb, label %pred
pred:
switch i32 %x, label %bb [i32 42, label %other_succ
i32 123456, label %other_succ
i32 -654321, label %other_succ
i32 1, label %do_1
i32 2, label %do_2
i32 3, label %do_3]
bb:
%phi = phi i32* [null, %pred], [%p, %entry]
%r = load i32, i32* %phi
ret i32 %r
do_1:
call void @foo_01()
ret i32 1
do_2:
call void @foo_02()
ret i32 1
do_3:
call void @foo_03()
ret i32 1
other_succ:
ret i32 0
}
; TODO: Basing on fact that load(null) is UB, we can remove edge pred->bb.
define i32 @test_04(i32* %p, i32 %x, i1 %cond) {
; CHECK-LABEL: @test_04(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[BB:%.*]], label [[PRED:%.*]]
; CHECK: pred:
; CHECK-NEXT: switch i32 [[X:%.*]], label [[COMMON_RET:%.*]] [
; CHECK-NEXT: i32 42, label [[BB]]
; CHECK-NEXT: i32 123456, label [[BB]]
; CHECK-NEXT: i32 -654321, label [[BB]]
; CHECK-NEXT: i32 1, label [[DO_1:%.*]]
; CHECK-NEXT: i32 2, label [[DO_2:%.*]]
; CHECK-NEXT: i32 3, label [[DO_3:%.*]]
; CHECK-NEXT: ]
; CHECK: common.ret:
; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ [[R:%.*]], [[BB]] ], [ 1, [[DO_1]] ], [ 1, [[DO_2]] ], [ 1, [[DO_3]] ], [ 0, [[PRED]] ]
; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
; CHECK: bb:
; CHECK-NEXT: [[PHI:%.*]] = phi i32* [ null, [[PRED]] ], [ null, [[PRED]] ], [ null, [[PRED]] ], [ [[P:%.*]], [[ENTRY:%.*]] ]
; CHECK-NEXT: [[R]] = load i32, i32* [[PHI]], align 4
; CHECK-NEXT: br label [[COMMON_RET]]
; CHECK: do_1:
; CHECK-NEXT: call void @foo_01()
; CHECK-NEXT: br label [[COMMON_RET]]
; CHECK: do_2:
; CHECK-NEXT: call void @foo_02()
; CHECK-NEXT: br label [[COMMON_RET]]
; CHECK: do_3:
; CHECK-NEXT: call void @foo_03()
; CHECK-NEXT: br label [[COMMON_RET]]
;
entry:
br i1 %cond, label %bb, label %pred
pred:
switch i32 %x, label %other_succ [i32 42, label %bb
i32 123456, label %bb
i32 -654321, label %bb
i32 1, label %do_1
i32 2, label %do_2
i32 3, label %do_3]
bb:
%phi = phi i32* [null, %pred], [null, %pred], [null, %pred], [%p, %entry]
%r = load i32, i32* %phi
ret i32 %r
do_1:
call void @foo_01()
ret i32 1
do_2:
call void @foo_02()
ret i32 1
do_3:
call void @foo_03()
ret i32 1
other_succ:
ret i32 0
}