Skip to content

[Reassociate] Use disjoint flag to convert Or to Add. #72772

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
merged 1 commit into from
Dec 6, 2023
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
7 changes: 4 additions & 3 deletions llvm/lib/Transforms/Scalar/Reassociate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2256,9 +2256,10 @@ void ReassociatePass::OptimizeInst(Instruction *I) {
// with no common bits set, convert it to X+Y.
if (I->getOpcode() == Instruction::Or &&
shouldConvertOrWithNoCommonBitsToAdd(I) && !isLoadCombineCandidate(I) &&
haveNoCommonBitsSet(I->getOperand(0), I->getOperand(1),
SimplifyQuery(I->getModule()->getDataLayout(),
/*DT=*/nullptr, /*AC=*/nullptr, I))) {
(I->isDisjoint() ||
haveNoCommonBitsSet(I->getOperand(0), I->getOperand(1),
SimplifyQuery(I->getModule()->getDataLayout(),
/*DT=*/nullptr, /*AC=*/nullptr, I)))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should instead infer disjoint in InstCombine and then replace all existing haveNoCommonBitsSet() checks with only a isDisjoint() check, not both.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I looked at this some more. When we reassociate a set of Ors we lose the flag, but then we revisit them to try more reassociation that may want to treat them as Adds. But we've lost the flag. So I think we need to call haveNoCommonBitsSet again somewhere to recover it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, in that case keeping the haveNoCommonBitsSet calls makes sense.

Instruction *NI = convertOrWithNoCommonBitsToAdd(I);
RedoInsts.insert(I);
MadeChange = true;
Expand Down
12 changes: 12 additions & 0 deletions llvm/test/Transforms/Reassociate/add-like-or.ll
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ define i32 @test3(i32 %x, i32 %bit) {
ret i32 %res
}

; Test that disjoint allow reassociation.
define i32 @test4(i32 %a, i32 %b) {
; CHECK-LABEL: @test4(
; CHECK-NEXT: [[C:%.*]] = add i32 [[A:%.*]], 1
; CHECK-NEXT: [[C_PLUS_ONE:%.*]] = add i32 [[C]], [[B:%.*]]
; CHECK-NEXT: ret i32 [[C_PLUS_ONE]]
;
%c = or disjoint i32 %a, %b
%c.plus.one = add i32 %c, 1
ret i32 %c.plus.one
}

declare i32 @llvm.ctlz.i32(i32, i1 immarg) #2

!0 = !{i32 0, i32 33}