[llvm] 419c534 - [SimplifyCFG] Mark div/rem as not-cheap to sink if we are replacing const denominator
Noah Goldstein via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 17 10:04:58 PDT 2024
Author: Noah Goldstein
Date: 2024-09-17T12:04:34-05:00
New Revision: 419c53477eae62c716ca8f4e18109342f0398d95
URL: https://github.com/llvm/llvm-project/commit/419c53477eae62c716ca8f4e18109342f0398d95
DIFF: https://github.com/llvm/llvm-project/commit/419c53477eae62c716ca8f4e18109342f0398d95.diff
LOG: [SimplifyCFG] Mark div/rem as not-cheap to sink if we are replacing const denominator
Close #109007
Added:
Modified:
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/sink-and-convert-switch.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 1a4820edfaf365..c63618d9dd1297 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1954,6 +1954,9 @@ static bool isLifeTimeMarker(const Instruction *I) {
// into variables.
static bool replacingOperandWithVariableIsCheap(const Instruction *I,
int OpIdx) {
+ // Divide/Remainder by constant is typically much cheaper than by variable.
+ if (I->isIntDivRem())
+ return OpIdx != 1;
return !isa<IntrinsicInst>(I);
}
diff --git a/llvm/test/Transforms/SimplifyCFG/sink-and-convert-switch.ll b/llvm/test/Transforms/SimplifyCFG/sink-and-convert-switch.ll
index bb24fdd04e5741..87d64932ef0935 100644
--- a/llvm/test/Transforms/SimplifyCFG/sink-and-convert-switch.ll
+++ b/llvm/test/Transforms/SimplifyCFG/sink-and-convert-switch.ll
@@ -49,9 +49,28 @@ define i64 @dont_make_div_variable(i64 noundef %x, i64 noundef %i) {
; CHECK-LABEL: define i64 @dont_make_div_variable(
; CHECK-SAME: i64 noundef [[X:%.*]], i64 noundef [[I:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
-; CHECK-NEXT: [[SWITCH_TABLEIDX:%.*]] = sub nsw i64 [[I]], 9
-; CHECK-NEXT: [[SWITCH_OFFSET:%.*]] = add nsw i64 [[SWITCH_TABLEIDX]], 9
-; CHECK-NEXT: [[DIV6:%.*]] = udiv i64 [[X]], [[SWITCH_OFFSET]]
+; CHECK-NEXT: switch i64 [[I]], label %[[SW_DEFAULT:.*]] [
+; CHECK-NEXT: i64 9, label %[[SW_BB:.*]]
+; CHECK-NEXT: i64 10, label %[[SW_BB1:.*]]
+; CHECK-NEXT: i64 11, label %[[SW_BB3:.*]]
+; CHECK-NEXT: i64 12, label %[[SW_BB5:.*]]
+; CHECK-NEXT: ]
+; CHECK: [[SW_BB]]:
+; CHECK-NEXT: [[DIV:%.*]] = udiv i64 [[X]], 9
+; CHECK-NEXT: br label %[[RETURN:.*]]
+; CHECK: [[SW_BB1]]:
+; CHECK-NEXT: [[DIV2:%.*]] = udiv i64 [[X]], 10
+; CHECK-NEXT: br label %[[RETURN]]
+; CHECK: [[SW_BB3]]:
+; CHECK-NEXT: [[DIV4:%.*]] = udiv i64 [[X]], 11
+; CHECK-NEXT: br label %[[RETURN]]
+; CHECK: [[SW_BB5]]:
+; CHECK-NEXT: [[DIV7:%.*]] = udiv i64 [[X]], 12
+; CHECK-NEXT: br label %[[RETURN]]
+; CHECK: [[SW_DEFAULT]]:
+; CHECK-NEXT: unreachable
+; CHECK: [[RETURN]]:
+; CHECK-NEXT: [[DIV6:%.*]] = phi i64 [ [[DIV7]], %[[SW_BB5]] ], [ [[DIV4]], %[[SW_BB3]] ], [ [[DIV2]], %[[SW_BB1]] ], [ [[DIV]], %[[SW_BB]] ]
; CHECK-NEXT: ret i64 [[DIV6]]
;
entry:
More information about the llvm-commits
mailing list