Skip to content

[SimplifyCFG] Relax cttz cost check in simplifySwitchOfPowersOfTwo #145159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7198,8 +7198,10 @@ static bool reduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder,
/// will be transformed to:
/// switch (count_trailing_zeros(C)) { case 0: case 1: case 6: case 7: }
///
/// This transformation allows better lowering and could allow transforming into
/// a lookup table.
/// This transformation allows better lowering and may transform the switch
/// instruction into a sequence of bit manipulation and a smaller
/// log2(C)-indexed value table (instead of traditionally emitting a load of the
/// address of the jump target, and indirectly jump to it).
static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder,
const DataLayout &DL,
const TargetTransformInfo &TTI) {
Expand All @@ -7211,17 +7213,15 @@ static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder,
!DL.fitsInLegalInteger(CondTy->getIntegerBitWidth()))
return false;

const auto CttzIntrinsicCost = TTI.getIntrinsicInstrCost(
IntrinsicCostAttributes(Intrinsic::cttz, CondTy,
{Condition, ConstantInt::getTrue(Context)}),
TTI::TCK_SizeAndLatency);

if (CttzIntrinsicCost > TTI::TCC_Basic)
// Inserting intrinsic is too expensive.
// Ensure trailing zeroes count intrinsic emission is not too expensive.
IntrinsicCostAttributes Attrs(Intrinsic::cttz, CondTy,
{Condition, ConstantInt::getTrue(Context)});
if (TTI.getIntrinsicInstrCost(Attrs, TTI::TCK_SizeAndLatency) >
TTI::TCC_Basic * 2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there not a cost for the alternative codegen to compare against? Using hardcoded TTI::TCC_* cost magic numbers tends to cause problems at some point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there not a cost for the alternative codegen to compare against?

Without this transform, the switch will be lowered into an icmp chain/indirect jump table. Thus, the transform is always profitable if ctlz is natively supported. The default cost for native ctlz is TCC_Basic. Can we increase the threshold to < TCC_Expensive as it is also used in other places?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we increase the threshold to < TCC_Expensive as it is also used in other places?

I think keeping the threshold under or equal to 2 should make sense here (< TCC_Expensive may be already too permissive).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - but I'd really like to get rid of TargetCostConstants entirely at some point :/

return false;

// Only bother with this optimization if there are more than 3 switch cases.
// SDAG will only bother creating jump tables for 4 or more cases.
// SDAG will start emitting jump tables for 4 or more cases.
if (SI->getNumCases() < 4)
return false;

Expand Down
35 changes: 35 additions & 0 deletions llvm/test/Transforms/SimplifyCFG/X86/switch-of-powers-of-two.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -passes='simplifycfg<switch-to-lookup>' -simplifycfg-require-and-preserve-domtree=1 -S < %s | FileCheck %s

target triple = "x86_64-unknown-linux-gnu"

define i32 @switch_of_powers_two(i32 %arg) {
; CHECK-LABEL: define i32 @switch_of_powers_two(
; CHECK-SAME: i32 [[ARG:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[ARG]], i1 true)
; CHECK-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [7 x i32], ptr @switch.table.switch_of_powers_two, i32 0, i32 [[TMP0]]
; CHECK-NEXT: [[SWITCH_LOAD:%.*]] = load i32, ptr [[SWITCH_GEP]], align 4
; CHECK-NEXT: ret i32 [[SWITCH_LOAD]]
;
entry:
switch i32 %arg, label %default_case [
i32 1, label %bb1
i32 8, label %bb2
i32 16, label %bb3
i32 32, label %bb4
i32 64, label %bb5
]


default_case: unreachable
bb1: br label %return
bb2: br label %return
bb3: br label %return
bb4: br label %return
bb5: br label %return

return:
%phi = phi i32 [ 3, %bb1 ], [ 2, %bb2 ], [ 1, %bb3 ], [ 0, %bb4 ], [ 42, %bb5 ]
ret i32 %phi
}
Loading