Index: llvm/test/Transforms/SimplifyCFG/branch-fold.ll =================================================================== --- llvm/test/Transforms/SimplifyCFG/branch-fold.ll +++ llvm/test/Transforms/SimplifyCFG/branch-fold.ll @@ -114,3 +114,37 @@ call void @foo() ret void } + +define i8 @common_pred(i8 noundef %arg, i1 %c1, i1 %c2) { +; CHECK-LABEL: @common_pred( +; CHECK-NEXT: Pred: +; CHECK-NEXT: call void @dummy() +; CHECK-NEXT: br i1 [[C1:%.*]], label [[COMMONPRED:%.*]], label [[BB:%.*]] +; CHECK: CommonPred: +; CHECK-NEXT: call void @dummy() +; CHECK-NEXT: br i1 [[C2:%.*]], label [[SUCC:%.*]], label [[BB]] +; CHECK: BB: +; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ 0, [[PRED:%.*]] ], [ 1, [[COMMONPRED]] ] +; CHECK-NEXT: br label [[SUCC]] +; CHECK: Succ: +; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[PHI1]], [[BB]] ], [ 4, [[COMMONPRED]] ] +; CHECK-NEXT: ret i8 [[PHI2]] +; +Pred: +call void @dummy() + br i1 %c1, label %CommonPred, label %BB + +CommonPred: +call void @dummy() + br i1 %c2, label %Succ, label %BB + +BB: + %phi1 = phi i8 [ 0, %Pred ], [1, %CommonPred] + br label %Succ + +Succ: + %phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ] + ret i8 %phi2 +} + +declare void @dummy() Index: llvm/test/Transforms/SimplifyCFG/merge-phis-in-switch.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/SimplifyCFG/merge-phis-in-switch.ll @@ -0,0 +1,335 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3 +; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s + +; Test a bunch of cases where the combination of phis in switch should be merged into one phi + +declare void @use(i8) + +define i8 @phis_of_switch_minimal(i8 noundef %arg) { +; CHECK-LABEL: define i8 @phis_of_switch_minimal +; CHECK-SAME: (i8 noundef [[ARG:%.*]]) { +; CHECK-NEXT: start: +; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [ +; CHECK-NEXT: i8 0, label [[CASE01:%.*]] +; CHECK-NEXT: i8 1, label [[CASE1:%.*]] +; CHECK-NEXT: i8 2, label [[END:%.*]] +; CHECK-NEXT: ] +; CHECK: unreachable: +; CHECK-NEXT: unreachable +; CHECK: case1: +; CHECK-NEXT: br label [[CASE01]] +; CHECK: case01: +; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ 2, [[CASE1]] ], [ 1, [[START:%.*]] ] +; CHECK-NEXT: br label [[END]] +; CHECK: end: +; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[PHI1]], [[CASE01]] ], [ 3, [[START]] ] +; CHECK-NEXT: ret i8 [[PHI2]] +; +start: + switch i8 %arg, label %unreachable [ + i8 0, label %case01 + i8 1, label %case1 + i8 2, label %end + ] +unreachable: + unreachable +case1: + br label %case01 + +case01: + %phi1 = phi i8 [ 2, %case1 ], [1, %start] + br label %end + +end: + %phi2 = phi i8 [ %phi1, %case01 ], [ 3, %start ] + ret i8 %phi2 +} + +define i8 @phis_of_switch(i8 noundef %arg) { +; CHECK-LABEL: define i8 @phis_of_switch +; CHECK-SAME: (i8 noundef [[ARG:%.*]]) { +; CHECK-NEXT: start: +; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [ +; CHECK-NEXT: i8 0, label [[CASE012:%.*]] +; CHECK-NEXT: i8 1, label [[CASE1:%.*]] +; CHECK-NEXT: i8 2, label [[CASE2:%.*]] +; CHECK-NEXT: i8 3, label [[END:%.*]] +; CHECK-NEXT: ] +; CHECK: unreachable: +; CHECK-NEXT: unreachable +; CHECK: case1: +; CHECK-NEXT: br label [[CASE012]] +; CHECK: case2: +; CHECK-NEXT: br label [[CASE012]] +; CHECK: case012: +; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ 3, [[CASE2]] ], [ 2, [[CASE1]] ], [ 1, [[START:%.*]] ] +; CHECK-NEXT: br label [[END]] +; CHECK: end: +; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[PHI1]], [[CASE012]] ], [ 4, [[START]] ] +; CHECK-NEXT: ret i8 [[PHI2]] +; +start: + switch i8 %arg, label %unreachable [ + i8 0, label %case012 + i8 1, label %case1 + i8 2, label %case2 + i8 3, label %end + ] +unreachable: + unreachable +case1: + br label %case012 + +case2: + br label %case012 + +case012: + %phi1 = phi i8 [ 3, %case2 ], [ 2, %case1 ], [1, %start] + br label %end + +end: + %phi2 = phi i8 [ %phi1, %case012 ], [ 4, %start ] + ret i8 %phi2 +} + +define i8 @multiple_phis_of_switch(i8 noundef %arg) { +; CHECK-LABEL: define i8 @multiple_phis_of_switch +; CHECK-SAME: (i8 noundef [[ARG:%.*]]) { +; CHECK-NEXT: start: +; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [ +; CHECK-NEXT: i8 0, label [[CASE012:%.*]] +; CHECK-NEXT: i8 1, label [[CASE1:%.*]] +; CHECK-NEXT: i8 2, label [[CASE2:%.*]] +; CHECK-NEXT: i8 3, label [[END:%.*]] +; CHECK-NEXT: ] +; CHECK: unreachable: +; CHECK-NEXT: unreachable +; CHECK: case1: +; CHECK-NEXT: br label [[CASE012]] +; CHECK: case2: +; CHECK-NEXT: br label [[CASE012]] +; CHECK: case012: +; CHECK-NEXT: [[PHI1_1:%.*]] = phi i8 [ 3, [[CASE2]] ], [ 2, [[CASE1]] ], [ 1, [[START:%.*]] ] +; CHECK-NEXT: [[PHI1_2:%.*]] = phi i8 [ 6, [[CASE2]] ], [ 5, [[CASE1]] ], [ 4, [[START]] ] +; CHECK-NEXT: br label [[END]] +; CHECK: end: +; CHECK-NEXT: [[PHI2_1:%.*]] = phi i8 [ [[PHI1_1]], [[CASE012]] ], [ 4, [[START]] ] +; CHECK-NEXT: [[PHI2_2:%.*]] = phi i8 [ [[PHI1_1]], [[CASE012]] ], [ 5, [[START]] ] +; CHECK-NEXT: [[PHI2_3:%.*]] = phi i8 [ [[PHI1_2]], [[CASE012]] ], [ 3, [[START]] ] +; CHECK-NEXT: call void @use(i8 [[PHI2_1]]) +; CHECK-NEXT: call void @use(i8 [[PHI2_2]]) +; CHECK-NEXT: call void @use(i8 [[PHI2_3]]) +; CHECK-NEXT: ret i8 [[PHI2_1]] +; +start: + switch i8 %arg, label %unreachable [ + i8 0, label %case012 + i8 1, label %case1 + i8 2, label %case2 + i8 3, label %end + ] +unreachable: + unreachable +case1: + br label %case012 + +case2: + br label %case012 + +case012: + %phi1_1 = phi i8 [ 3, %case2 ], [ 2, %case1 ], [1, %start] + %phi1_2 = phi i8 [ 6, %case2 ], [ 5, %case1 ], [4, %start] + br label %end + +end: + %phi2_1 = phi i8 [ %phi1_1, %case012 ], [ 4, %start ] + %phi2_2 = phi i8 [ %phi1_1, %case012 ], [ 5, %start ] + %phi2_3 = phi i8 [ %phi1_2, %case012 ], [ 3, %start ] + call void @use(i8 %phi2_1) + call void @use(i8 %phi2_2) + call void @use(i8 %phi2_3) + ret i8 %phi2_1 +} + +define i8 @phis_of_switch_multiple_stage0(i8 noundef %arg) { +; CHECK-LABEL: define i8 @phis_of_switch_multiple_stage0 +; CHECK-SAME: (i8 noundef [[ARG:%.*]]) { +; CHECK-NEXT: start: +; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [ +; CHECK-NEXT: i8 0, label [[CASE0:%.*]] +; CHECK-NEXT: i8 1, label [[CASE1:%.*]] +; CHECK-NEXT: i8 2, label [[CASE2:%.*]] +; CHECK-NEXT: i8 3, label [[CASE0123:%.*]] +; CHECK-NEXT: i8 4, label [[CASE01234:%.*]] +; CHECK-NEXT: i8 5, label [[END:%.*]] +; CHECK-NEXT: ] +; CHECK: unreachable: +; CHECK-NEXT: unreachable +; CHECK: case0: +; CHECK-NEXT: br label [[CASE0123]] +; CHECK: case1: +; CHECK-NEXT: br label [[CASE0123]] +; CHECK: case2: +; CHECK-NEXT: br label [[CASE0123]] +; CHECK: case0123: +; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ 4, [[START:%.*]] ], [ 3, [[CASE2]] ], [ 2, [[CASE1]] ], [ 1, [[CASE0]] ] +; CHECK-NEXT: br label [[CASE01234]] +; CHECK: case01234: +; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[PHI1]], [[CASE0123]] ], [ 5, [[START]] ] +; CHECK-NEXT: br label [[END]] +; CHECK: end: +; CHECK-NEXT: [[PHI3:%.*]] = phi i8 [ [[PHI2]], [[CASE01234]] ], [ 6, [[START]] ] +; CHECK-NEXT: ret i8 [[PHI3]] +; +start: + switch i8 %arg, label %unreachable [ + i8 0, label %case0 + i8 1, label %case1 + i8 2, label %case2 + i8 3, label %case0123 + i8 4, label %case01234 + i8 5, label %end + ] +unreachable: + unreachable + +case0: + br label %case0123 + +case1: + br label %case0123 + +case2: + br label %case0123 + +case0123: + %phi1 = phi i8 [4, %start], [ 3, %case2 ], [ 2, %case1 ], [ 1, %case0 ] + br label %case01234 + +case01234: + %phi2 = phi i8 [ %phi1, %case0123 ], [ 5, %start ] + br label %end + +end: + %phi3 = phi i8 [ %phi2, %case01234 ], [6, %start] + ret i8 %phi3 +} + +define i8 @phis_of_switch_multiple_stage1(i8 noundef %arg) { +; CHECK-LABEL: define i8 @phis_of_switch_multiple_stage1 +; CHECK-SAME: (i8 noundef [[ARG:%.*]]) { +; CHECK-NEXT: start: +; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [ +; CHECK-NEXT: i8 0, label [[CASE0:%.*]] +; CHECK-NEXT: i8 1, label [[CASE1:%.*]] +; CHECK-NEXT: i8 2, label [[CASE012:%.*]] +; CHECK-NEXT: i8 3, label [[CASE3:%.*]] +; CHECK-NEXT: i8 4, label [[CASE4:%.*]] +; CHECK-NEXT: i8 5, label [[CASE345:%.*]] +; CHECK-NEXT: i8 6, label [[CASE0123456:%.*]] +; CHECK-NEXT: ] +; CHECK: unreachable: +; CHECK-NEXT: unreachable +; CHECK: case0: +; CHECK-NEXT: br label [[CASE012]] +; CHECK: case1: +; CHECK-NEXT: br label [[CASE012]] +; CHECK: case012: +; CHECK-NEXT: [[PHI123:%.*]] = phi i8 [ 3, [[START:%.*]] ], [ 2, [[CASE1]] ], [ 1, [[CASE0]] ] +; CHECK-NEXT: br label [[CASE0123456]] +; CHECK: case3: +; CHECK-NEXT: br label [[CASE345]] +; CHECK: case4: +; CHECK-NEXT: br label [[CASE345]] +; CHECK: case345: +; CHECK-NEXT: [[PHI456:%.*]] = phi i8 [ 6, [[START]] ], [ 5, [[CASE4]] ], [ 4, [[CASE3]] ] +; CHECK-NEXT: br label [[CASE0123456]] +; CHECK: case0123456: +; CHECK-NEXT: [[PHI1234567:%.*]] = phi i8 [ 7, [[START]] ], [ [[PHI456]], [[CASE345]] ], [ [[PHI123]], [[CASE012]] ] +; CHECK-NEXT: ret i8 [[PHI1234567]] +; +start: + switch i8 %arg, label %unreachable [ + i8 0, label %case0 + i8 1, label %case1 + i8 2, label %case012 + i8 3, label %case3 + i8 4, label %case4 + i8 5, label %case345 + i8 6, label %case0123456 + ] +unreachable: + unreachable +case0: + br label %case012 + +case1: + br label %case012 + +case012: + %phi123 = phi i8 [3, %start], [ 2, %case1 ], [ 1, %case0 ] + br label %case0123456 + +case3: + br label %case345 + +case4: + br label %case345 + +case345: + %phi456 = phi i8 [6, %start], [ 5, %case4 ], [ 4, %case3 ] + br label %case0123456 + +case0123456: + %phi1234567 = phi i8 [7, %start], [ %phi456, %case345 ], [ %phi123, %case012 ] + ret i8 %phi1234567 + +} + +define i8 @phis_of_switch_extra_use_fail(i8 noundef %arg) { +; CHECK-LABEL: define i8 @phis_of_switch_extra_use_fail +; CHECK-SAME: (i8 noundef [[ARG:%.*]]) { +; CHECK-NEXT: start: +; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [ +; CHECK-NEXT: i8 0, label [[CASE012:%.*]] +; CHECK-NEXT: i8 1, label [[CASE1:%.*]] +; CHECK-NEXT: i8 2, label [[CASE2:%.*]] +; CHECK-NEXT: i8 3, label [[END:%.*]] +; CHECK-NEXT: ] +; CHECK: unreachable: +; CHECK-NEXT: unreachable +; CHECK: case1: +; CHECK-NEXT: br label [[CASE012]] +; CHECK: case2: +; CHECK-NEXT: br label [[CASE012]] +; CHECK: case012: +; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ 3, [[CASE2]] ], [ 2, [[CASE1]] ], [ 1, [[START:%.*]] ] +; CHECK-NEXT: call void @use(i8 [[PHI1]]) +; CHECK-NEXT: br label [[END]] +; CHECK: end: +; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[PHI1]], [[CASE012]] ], [ 4, [[START]] ] +; CHECK-NEXT: ret i8 [[PHI2]] +; +start: + switch i8 %arg, label %unreachable [ + i8 0, label %case012 + i8 1, label %case1 + i8 2, label %case2 i8 3, label %end + ] +unreachable: + unreachable +case1: + br label %case012 + +case2: + br label %case012 + +case012: + %phi1 = phi i8 [ 3, %case2 ], [ 2, %case1 ], [1, %start] + call void @use(i8 %phi1) + br label %end + +end: + %phi2 = phi i8 [ %phi1, %case012 ], [ 4, %start ] + ret i8 %phi2 +} Index: llvm/test/Transforms/SimplifyCFG/multiple-phis.ll =================================================================== --- llvm/test/Transforms/SimplifyCFG/multiple-phis.ll +++ llvm/test/Transforms/SimplifyCFG/multiple-phis.ll @@ -1,4 +1,5 @@ -; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2 + ; RUN: opt -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -keep-loops=false -S < %s | FileCheck %s ; RUN: opt -passes='simplifycfg' -S < %s | FileCheck %s @@ -7,11 +8,12 @@ ; SimplifyCFG if-converts one of the phis, it should do both. define i32 @upper_bound(ptr %r, i32 %high, i32 %k) nounwind { -; CHECK-LABEL: @upper_bound( +; CHECK-LABEL: define i32 @upper_bound +; CHECK-SAME: (ptr [[R:%.*]], i32 [[HIGH:%.*]], i32 [[K:%.*]]) #[[ATTR0:[0-9]+]] { ; CHECK-NEXT: entry: ; CHECK-NEXT: br label [[WHILE_COND:%.*]] ; CHECK: while.cond: -; CHECK-NEXT: [[HIGH_ADDR_0:%.*]] = phi i32 [ [[HIGH:%.*]], [[ENTRY:%.*]] ], [ [[SPEC_SELECT:%.*]], [[WHILE_BODY:%.*]] ] +; CHECK-NEXT: [[HIGH_ADDR_0:%.*]] = phi i32 [ [[HIGH]], [[ENTRY:%.*]] ], [ [[SPEC_SELECT:%.*]], [[WHILE_BODY:%.*]] ] ; CHECK-NEXT: [[LOW_0:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[SPEC_SELECT1:%.*]], [[WHILE_BODY]] ] ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[LOW_0]], [[HIGH_ADDR_0]] ; CHECK-NEXT: br i1 [[CMP]], label [[WHILE_BODY]], label [[WHILE_END:%.*]] @@ -19,9 +21,9 @@ ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[LOW_0]], [[HIGH_ADDR_0]] ; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[ADD]], 2 ; CHECK-NEXT: [[IDXPROM:%.*]] = zext i32 [[DIV]] to i64 -; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[R:%.*]], i64 [[IDXPROM]] +; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[R]], i64 [[IDXPROM]] ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4 -; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i32 [[K:%.*]], [[TMP0]] +; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i32 [[K]], [[TMP0]] ; CHECK-NEXT: [[ADD2:%.*]] = add i32 [[DIV]], 1 ; CHECK-NEXT: [[SPEC_SELECT]] = select i1 [[CMP1]], i32 [[DIV]], i32 [[HIGH_ADDR_0]] ; CHECK-NEXT: [[SPEC_SELECT1]] = select i1 [[CMP1]], i32 [[LOW_0]], i32 [[ADD2]] @@ -57,3 +59,285 @@ while.end: ; preds = %while.cond ret i32 %low.0 } + +define i32 @merge0(i1 %c1, i1 %c2, i1 %c3) { +; CHECK-LABEL: define i32 @merge0 +; CHECK-SAME: (i1 [[C1:%.*]], i1 [[C2:%.*]], i1 [[C3:%.*]]) { +; CHECK-NEXT: br i1 [[C1]], label [[IF1:%.*]], label [[ELSE1:%.*]] +; CHECK: if1: +; CHECK-NEXT: [[DOT:%.*]] = select i1 [[C2]], i32 0, i32 1 +; CHECK-NEXT: br label [[J1:%.*]] +; CHECK: else1: +; CHECK-NEXT: br i1 [[C3]], label [[J1]], label [[J2:%.*]] +; CHECK: j1: +; CHECK-NEXT: [[PHI1:%.*]] = phi i32 [ 2, [[ELSE1]] ], [ [[DOT]], [[IF1]] ] +; CHECK-NEXT: br label [[J2]] +; CHECK: j2: +; CHECK-NEXT: [[PHI2:%.*]] = phi i32 [ [[PHI1]], [[J1]] ], [ 3, [[ELSE1]] ] +; CHECK-NEXT: ret i32 [[PHI2]] +; + br i1 %c1, label %if1, label %else1 + +if1: + br i1 %c2, label %if2, label %else2 + +if2: + br label %j1 else2: + br label %j1 + +else1: + br i1 %c3, label %j1, label %j2 + +j1: + %phi1 = phi i32 [ 0, %if2 ], [ 1, %else2 ], [ 2, %else1 ] + br label %j2 + +j2: + %phi2 = phi i32 [ %phi1, %j1 ], [ 3, %else1 ] + ret i32 %phi2 +} + +define i8 @merge1(i8 noundef %arg, i1 %c1, i1 %c2) { +; CHECK-LABEL: define i8 @merge1 +; CHECK-SAME: (i8 noundef [[ARG:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [ +; CHECK-NEXT: i8 -123, label [[CASE0:%.*]] +; CHECK-NEXT: i8 66, label [[BB:%.*]] +; CHECK-NEXT: i8 123, label [[CASE2:%.*]] +; CHECK-NEXT: ] +; CHECK: unreachable: +; CHECK-NEXT: unreachable +; CHECK: case0: +; CHECK-NEXT: [[C1_NOT:%.*]] = xor i1 [[C1]], true +; CHECK-NEXT: [[C2_NOT:%.*]] = xor i1 [[C2]], true +; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[C1_NOT]], i1 true, i1 [[C2_NOT]] +; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[C1_NOT]], i8 0, i8 3 +; CHECK-NEXT: br i1 [[BRMERGE]], label [[BB]], label [[SUCC:%.*]] +; CHECK: case2: +; CHECK-NEXT: br label [[BB]] +; CHECK: BB: +; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ [[DOTMUX]], [[CASE0]] ], [ 2, [[CASE2]] ], [ 1, [[ENTRY:%.*]] ] +; CHECK-NEXT: br label [[SUCC]] +; CHECK: Succ: +; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[PHI1]], [[BB]] ], [ 4, [[CASE0]] ] +; CHECK-NEXT: ret i8 [[PHI2]] +; +entry: + switch i8 %arg, label %unreachable [ + i8 -123, label %case0 + i8 66, label %case1 + i8 123, label %case2 + ] + +unreachable: + unreachable + +case0: + br i1 %c1, label %CommonPred, label %BB + +case1: + br label %BB + +case2: + br label %BB + +CommonPred: + br i1 %c2, label %Succ, label %BB + +BB: + %phi1 = phi i8 [ 0, %case0 ], [1, %case1],[2, %case2],[3,%CommonPred] + br label %Succ + +Succ: + %phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ] + ret i8 %phi2 +} + +define i8 @merge1_unfoldable_one_block(i8 noundef %arg, i1 %c1, i1 %c2) { +; CHECK-LABEL: define i8 @merge1_unfoldable_one_block +; CHECK-SAME: (i8 noundef [[ARG:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [ +; CHECK-NEXT: i8 -123, label [[CASE0:%.*]] +; CHECK-NEXT: i8 66, label [[BB:%.*]] +; CHECK-NEXT: i8 123, label [[CASE2:%.*]] +; CHECK-NEXT: ] +; CHECK: unreachable: +; CHECK-NEXT: unreachable +; CHECK: case0: +; CHECK-NEXT: call void @dummy() +; CHECK-NEXT: [[C1_NOT:%.*]] = xor i1 [[C1]], true +; CHECK-NEXT: [[C2_NOT:%.*]] = xor i1 [[C2]], true +; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[C1_NOT]], i1 true, i1 [[C2_NOT]] +; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[C1_NOT]], i8 0, i8 3 +; CHECK-NEXT: br i1 [[BRMERGE]], label [[BB]], label [[SUCC:%.*]] +; CHECK: case2: +; CHECK-NEXT: br label [[BB]] +; CHECK: BB: +; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ [[DOTMUX]], [[CASE0]] ], [ 2, [[CASE2]] ], [ 1, [[ENTRY:%.*]] ] +; CHECK-NEXT: br label [[SUCC]] +; CHECK: Succ: +; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[PHI1]], [[BB]] ], [ 4, [[CASE0]] ] +; CHECK-NEXT: ret i8 [[PHI2]] +; +entry: + switch i8 %arg, label %unreachable [ + i8 -123, label %case0 + i8 66, label %case1 + i8 123, label %case2 + ] + +unreachable: + unreachable + +case0: + call void @dummy() + br i1 %c1, label %CommonPred, label %BB + +case1: + br label %BB + +case2: + br label %BB + +CommonPred: + br i1 %c2, label %Succ, label %BB + +BB: + %phi1 = phi i8 [ 0, %case0 ], [1, %case1],[2, %case2],[3,%CommonPred] + br label %Succ + +Succ: + %phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ] + ret i8 %phi2 +} + +define i8 @merge1_unfoldable_two_block(i8 noundef %arg, i1 %c1, i1 %c2) { +; CHECK-LABEL: define i8 @merge1_unfoldable_two_block +; CHECK-SAME: (i8 noundef [[ARG:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [ +; CHECK-NEXT: i8 -123, label [[CASE0:%.*]] +; CHECK-NEXT: i8 66, label [[CASE1:%.*]] +; CHECK-NEXT: i8 123, label [[BB:%.*]] +; CHECK-NEXT: ] +; CHECK: unreachable: +; CHECK-NEXT: unreachable +; CHECK: case0: +; CHECK-NEXT: call void @dummy() +; CHECK-NEXT: [[C1_NOT:%.*]] = xor i1 [[C1]], true +; CHECK-NEXT: [[C2_NOT:%.*]] = xor i1 [[C2]], true +; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[C1_NOT]], i1 true, i1 [[C2_NOT]] +; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[C1_NOT]], i8 0, i8 3 +; CHECK-NEXT: br i1 [[BRMERGE]], label [[BB]], label [[SUCC:%.*]] +; CHECK: case1: +; CHECK-NEXT: call void @dummy() +; CHECK-NEXT: br label [[BB]] +; CHECK: BB: +; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ [[DOTMUX]], [[CASE0]] ], [ 1, [[CASE1]] ], [ 2, [[ENTRY:%.*]] ] +; CHECK-NEXT: br label [[SUCC]] +; CHECK: Succ: +; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[PHI1]], [[BB]] ], [ 4, [[CASE0]] ] +; CHECK-NEXT: ret i8 [[PHI2]] +; +entry: + switch i8 %arg, label %unreachable [ + i8 -123, label %case0 + i8 66, label %case1 + i8 123, label %case2 + ] + +unreachable: + unreachable + +case0: +call void @dummy() + br i1 %c1, label %CommonPred, label %BB + +case1: +call void @dummy() + br label %BB + +case2: + br label %BB + +CommonPred: + br i1 %c2, label %Succ, label %BB + +BB: + %phi1 = phi i8 [ 0, %case0 ], [1, %case1],[2, %case2],[3,%CommonPred] + br label %Succ + +Succ: + %phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ] + ret i8 %phi2 +} + +define i8 @merge1_unfoldable_all_block(i8 noundef %arg, i1 %c1, i1 %c2) { +; CHECK-LABEL: define i8 @merge1_unfoldable_all_block +; CHECK-SAME: (i8 noundef [[ARG:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [ +; CHECK-NEXT: i8 -123, label [[CASE0:%.*]] +; CHECK-NEXT: i8 66, label [[CASE1:%.*]] +; CHECK-NEXT: i8 123, label [[CASE2:%.*]] +; CHECK-NEXT: ] +; CHECK: unreachable: +; CHECK-NEXT: unreachable +; CHECK: case0: +; CHECK-NEXT: call void @dummy() +; CHECK-NEXT: br i1 [[C1]], label [[COMMONPRED:%.*]], label [[BB:%.*]] +; CHECK: case1: +; CHECK-NEXT: call void @dummy() +; CHECK-NEXT: br label [[BB]] +; CHECK: case2: +; CHECK-NEXT: call void @dummy() +; CHECK-NEXT: br label [[BB]] +; CHECK: CommonPred: +; CHECK-NEXT: call void @dummy() +; CHECK-NEXT: br i1 [[C2]], label [[SUCC:%.*]], label [[BB]] +; CHECK: BB: +; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ 0, [[CASE0]] ], [ 1, [[CASE1]] ], [ 2, [[CASE2]] ], [ 3, [[COMMONPRED]] ] +; CHECK-NEXT: br label [[SUCC]] +; CHECK: Succ: +; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[PHI1]], [[BB]] ], [ 4, [[COMMONPRED]] ] +; CHECK-NEXT: ret i8 [[PHI2]] +; +entry: + switch i8 %arg, label %unreachable [ + i8 -123, label %case0 + i8 66, label %case1 + i8 123, label %case2 + ] + +unreachable: + unreachable + +case0: +call void @dummy() + br i1 %c1, label %CommonPred, label %BB + +case1: +call void @dummy() + br label %BB + +case2: +call void @dummy() + br label %BB + +CommonPred: +call void @dummy() + br i1 %c2, label %Succ, label %BB + +BB: + %phi1 = phi i8 [ 0, %case0 ], [1, %case1],[2, %case2],[3,%CommonPred] + br label %Succ + +Succ: + %phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ] + ret i8 %phi2 +} + +declare void @dummy() +