[PATCH] D126650: [InstCombine] Fix const folding of switched with default case
Danila Malyutin via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue May 31 05:14:29 PDT 2022
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG4fb3fd7d8206: [InstCombine] Fix const folding of switches with default case (authored by danilaml).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D126650/new/
https://reviews.llvm.org/D126650
Files:
llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
llvm/test/Transforms/InstCombine/simple_phi_condition.ll
Index: llvm/test/Transforms/InstCombine/simple_phi_condition.ll
===================================================================
--- llvm/test/Transforms/InstCombine/simple_phi_condition.ll
+++ llvm/test/Transforms/InstCombine/simple_phi_condition.ll
@@ -585,3 +585,69 @@
%ret = phi i8 [ 1, %sw.1 ], [ 7, %sw.7 ], [ 19, %sw.19 ], [ 42, %entry ]
ret i8 %ret
}
+
+define i8 @test_switch_default_edge_direct(i8 %cond) {
+; CHECK-LABEL: @test_switch_default_edge_direct(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: switch i8 [[COND:%.*]], label [[MERGE:%.*]] [
+; CHECK-NEXT: i8 1, label [[SW_1:%.*]]
+; CHECK-NEXT: i8 7, label [[SW_7:%.*]]
+; CHECK-NEXT: i8 19, label [[MERGE]]
+; CHECK-NEXT: ]
+; CHECK: sw.1:
+; CHECK-NEXT: br label [[MERGE]]
+; CHECK: sw.7:
+; CHECK-NEXT: br label [[MERGE]]
+; CHECK: merge:
+; CHECK-NEXT: [[RET:%.*]] = phi i8 [ 1, [[SW_1]] ], [ 7, [[SW_7]] ], [ 19, [[ENTRY:%.*]] ], [ 19, [[ENTRY]] ]
+; CHECK-NEXT: ret i8 [[RET]]
+;
+entry:
+ switch i8 %cond, label %merge [
+ i8 1, label %sw.1
+ i8 7, label %sw.7
+ i8 19, label %merge
+ ]
+sw.1:
+ br label %merge
+sw.7:
+ br label %merge
+merge:
+ %ret = phi i8 [ 1, %sw.1 ], [ 7, %sw.7 ], [ 19, %entry ], [ 19, %entry ]
+ ret i8 %ret
+}
+
+define i8 @test_switch_default_edge_duplicate(i8 %cond) {
+; CHECK-LABEL: @test_switch_default_edge_duplicate(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: switch i8 [[COND:%.*]], label [[SW_19:%.*]] [
+; CHECK-NEXT: i8 1, label [[SW_1:%.*]]
+; CHECK-NEXT: i8 7, label [[SW_7:%.*]]
+; CHECK-NEXT: i8 19, label [[SW_19]]
+; CHECK-NEXT: ]
+; CHECK: sw.1:
+; CHECK-NEXT: br label [[MERGE:%.*]]
+; CHECK: sw.7:
+; CHECK-NEXT: br label [[MERGE]]
+; CHECK: sw.19:
+; CHECK-NEXT: br label [[MERGE]]
+; CHECK: merge:
+; CHECK-NEXT: [[RET:%.*]] = phi i8 [ 1, [[SW_1]] ], [ 7, [[SW_7]] ], [ 19, [[SW_19]] ]
+; CHECK-NEXT: ret i8 [[RET]]
+;
+entry:
+ switch i8 %cond, label %sw.19 [
+ i8 1, label %sw.1
+ i8 7, label %sw.7
+ i8 19, label %sw.19
+ ]
+sw.1:
+ br label %merge
+sw.7:
+ br label %merge
+sw.19:
+ br label %merge
+merge:
+ %ret = phi i8 [ 1, %sw.1 ], [ 7, %sw.7 ], [ 19, %sw.19 ]
+ ret i8 %ret
+}
Index: llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
@@ -1269,6 +1269,12 @@
// ... ...
// \ /
// phi [true] [false]
+ // and
+ // switch (cond)
+ // case v1: / \ case v2:
+ // ... ...
+ // \ /
+ // phi [v1] [v2]
// Make sure all inputs are constants.
if (!all_of(PN.operands(), [](Value *V) { return isa<ConstantInt>(V); }))
return nullptr;
@@ -1297,6 +1303,7 @@
AddSucc(ConstantInt::getFalse(Context), BI->getSuccessor(1));
} else if (auto *SI = dyn_cast<SwitchInst>(IDom->getTerminator())) {
Cond = SI->getCondition();
+ ++SuccCount[SI->getDefaultDest()];
for (auto Case : SI->cases())
AddSucc(Case.getCaseValue(), Case.getCaseSuccessor());
} else {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126650.433057.patch
Type: text/x-patch
Size: 3191 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220531/864e773f/attachment.bin>
More information about the llvm-commits
mailing list