Skip to content

Commit 419c534

Browse files
committed
[SimplifyCFG] Mark div/rem as not-cheap to sink if we are replacing const denominator
Close #109007
1 parent ae8d020 commit 419c534

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,9 @@ static bool isLifeTimeMarker(const Instruction *I) {
19541954
// into variables.
19551955
static bool replacingOperandWithVariableIsCheap(const Instruction *I,
19561956
int OpIdx) {
1957+
// Divide/Remainder by constant is typically much cheaper than by variable.
1958+
if (I->isIntDivRem())
1959+
return OpIdx != 1;
19571960
return !isa<IntrinsicInst>(I);
19581961
}
19591962

llvm/test/Transforms/SimplifyCFG/sink-and-convert-switch.ll

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,28 @@ define i64 @dont_make_div_variable(i64 noundef %x, i64 noundef %i) {
4949
; CHECK-LABEL: define i64 @dont_make_div_variable(
5050
; CHECK-SAME: i64 noundef [[X:%.*]], i64 noundef [[I:%.*]]) {
5151
; CHECK-NEXT: [[ENTRY:.*:]]
52-
; CHECK-NEXT: [[SWITCH_TABLEIDX:%.*]] = sub nsw i64 [[I]], 9
53-
; CHECK-NEXT: [[SWITCH_OFFSET:%.*]] = add nsw i64 [[SWITCH_TABLEIDX]], 9
54-
; CHECK-NEXT: [[DIV6:%.*]] = udiv i64 [[X]], [[SWITCH_OFFSET]]
52+
; CHECK-NEXT: switch i64 [[I]], label %[[SW_DEFAULT:.*]] [
53+
; CHECK-NEXT: i64 9, label %[[SW_BB:.*]]
54+
; CHECK-NEXT: i64 10, label %[[SW_BB1:.*]]
55+
; CHECK-NEXT: i64 11, label %[[SW_BB3:.*]]
56+
; CHECK-NEXT: i64 12, label %[[SW_BB5:.*]]
57+
; CHECK-NEXT: ]
58+
; CHECK: [[SW_BB]]:
59+
; CHECK-NEXT: [[DIV:%.*]] = udiv i64 [[X]], 9
60+
; CHECK-NEXT: br label %[[RETURN:.*]]
61+
; CHECK: [[SW_BB1]]:
62+
; CHECK-NEXT: [[DIV2:%.*]] = udiv i64 [[X]], 10
63+
; CHECK-NEXT: br label %[[RETURN]]
64+
; CHECK: [[SW_BB3]]:
65+
; CHECK-NEXT: [[DIV4:%.*]] = udiv i64 [[X]], 11
66+
; CHECK-NEXT: br label %[[RETURN]]
67+
; CHECK: [[SW_BB5]]:
68+
; CHECK-NEXT: [[DIV7:%.*]] = udiv i64 [[X]], 12
69+
; CHECK-NEXT: br label %[[RETURN]]
70+
; CHECK: [[SW_DEFAULT]]:
71+
; CHECK-NEXT: unreachable
72+
; CHECK: [[RETURN]]:
73+
; CHECK-NEXT: [[DIV6:%.*]] = phi i64 [ [[DIV7]], %[[SW_BB5]] ], [ [[DIV4]], %[[SW_BB3]] ], [ [[DIV2]], %[[SW_BB1]] ], [ [[DIV]], %[[SW_BB]] ]
5574
; CHECK-NEXT: ret i64 [[DIV6]]
5675
;
5776
entry:

0 commit comments

Comments
 (0)