-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SimplifyCFG] When only one case value is missing, replace default wi…
…th that case (#76669) When the default branch is the last case, we can transform that branch into a concrete branch with an unreachable default branch. ```llvm target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" define i64 @src(i64 %0) { %2 = urem i64 %0, 4 switch i64 %2, label %5 [ i64 1, label %3 i64 2, label %3 i64 3, label %4 ] 3: ; preds = %1, %1 br label %5 4: ; preds = %1 br label %5 5: ; preds = %1, %4, %3 %.0 = phi i64 [ 2, %4 ], [ 1, %3 ], [ 0, %1 ] ret i64 %.0 } define i64 @tgt(i64 %0) { %2 = urem i64 %0, 4 switch i64 %2, label %unreachable [ i64 0, label %5 i64 1, label %3 i64 2, label %3 i64 3, label %4 ] unreachable: ; preds = %1 unreachable 3: ; preds = %1, %1 br label %5 4: ; preds = %1 br label %5 5: ; preds = %1, %4, %3 %.0 = phi i64 [ 2, %4 ], [ 1, %3 ], [ 0, %1 ] ret i64 %.0 } ``` Alive2: https://alive2.llvm.org/ce/z/Y-PGXv After transform to a lookup table, I believe `tgt` is better code. The final instructions are as follows: ```asm src: # @src and edi, 3 lea rax, [rdi - 1] cmp rax, 2 ja .LBB0_1 mov rax, qword ptr [8*rdi + .Lswitch.table.src-8] ret .LBB0_1: xor eax, eax ret tgt: # @tgt and edi, 3 mov rax, qword ptr [8*rdi + .Lswitch.table.tgt] ret .Lswitch.table.src: .quad 1 # 0x1 .quad 1 # 0x1 .quad 2 # 0x2 .Lswitch.table.tgt: .quad 0 # 0x0 .quad 1 # 0x1 .quad 1 # 0x1 .quad 2 # 0x2 ``` Godbolt: https://llvm.godbolt.org/z/borME8znd Closes #73446.
- Loading branch information
Showing
3 changed files
with
142 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
llvm/test/Transforms/SimplifyCFG/switch-dead-default-lookup-table.ll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4 | ||
; RUN: opt %s -S -passes='simplifycfg<switch-to-lookup>' -simplifycfg-require-and-preserve-domtree=1 -switch-range-to-icmp | FileCheck %s | ||
|
||
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" | ||
|
||
define i64 @test_1(i64 %0) { | ||
; CHECK-LABEL: define i64 @test_1( | ||
; CHECK-SAME: i64 [[TMP0:%.*]]) { | ||
; CHECK-NEXT: switch.lookup: | ||
; CHECK-NEXT: [[TMP1:%.*]] = urem i64 [[TMP0]], 4 | ||
; CHECK-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [4 x i64], ptr @switch.table.test_1, i32 0, i64 [[TMP1]] | ||
; CHECK-NEXT: [[SWITCH_LOAD:%.*]] = load i64, ptr [[SWITCH_GEP]], align 8 | ||
; CHECK-NEXT: ret i64 [[SWITCH_LOAD]] | ||
; | ||
%2 = urem i64 %0, 4 | ||
switch i64 %2, label %5 [ | ||
i64 1, label %3 | ||
i64 2, label %3 | ||
i64 3, label %4 | ||
] | ||
|
||
3: | ||
br label %5 | ||
|
||
4: | ||
br label %5 | ||
|
||
5: | ||
%.0 = phi i64 [ 2, %4 ], [ 1, %3 ], [ 0, %1 ] | ||
ret i64 %.0 | ||
} | ||
|
||
|
||
define i64 @test_2(i64 %0) { | ||
; CHECK-LABEL: define i64 @test_2( | ||
; CHECK-SAME: i64 [[TMP0:%.*]]) { | ||
; CHECK-NEXT: switch.lookup: | ||
; CHECK-NEXT: [[TMP1:%.*]] = urem i64 [[TMP0]], 4 | ||
; CHECK-NEXT: ret i64 [[TMP1]] | ||
; | ||
%2 = urem i64 %0, 4 | ||
switch i64 %2, label %6 [ | ||
i64 1, label %3 | ||
i64 2, label %4 | ||
i64 3, label %5 | ||
] | ||
|
||
3: | ||
br label %6 | ||
|
||
4: | ||
br label %6 | ||
|
||
5: | ||
br label %6 | ||
|
||
6: | ||
%.0 = phi i64 [ 0, %1 ], [ 1, %3 ], [ 2, %4 ], [ 3, %5 ] | ||
ret i64 %.0 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters