This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Try not to demand low order bits for Add
ClosedPublic

Authored by foad on Jul 19 2022, 4:06 AM.

Details

Summary

Don't demand low order bits from the LHS of an Add if:

  • they are not demanded in the result, and
  • they are known to be zero in the RHS, so they can't possibly overflow and affect higher bit positions

This is intended to avoid a regression from a future patch to change
the order of canonicalization of ADD and AND.

Diff Detail

Event Timeline

foad created this revision.Jul 19 2022, 4:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 19 2022, 4:06 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
foad requested review of this revision.Jul 19 2022, 4:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 19 2022, 4:06 AM
foad added a comment.Jul 19 2022, 4:08 AM

This is still WIP because I need to write a better test for it.

Related to D129844.

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
521–523

This is the important change. The rest is just refactoring.

foad updated this revision to Diff 445786.Jul 19 2022, 5:07 AM

Simpler test diffs

fhahn added a subscriber: fhahn.Jul 20 2022, 3:33 AM
fhahn added inline comments.
llvm/test/Transforms/LoopVectorize/X86/float-induction-x86.ll
302

Is dropping exact here intentional?

It looks like we are missing a dedicated instcombine test for this scenario?

foad added inline comments.Jul 20 2022, 3:43 AM
llvm/test/Transforms/LoopVectorize/X86/float-induction-x86.ll
302

Is dropping exact here intentional?

Yes, because the value we are shifting is now based on SMAX (an arbitrary i64 value) instead of N_VEC which is SMAX & 0x7ffffffffffffff0 (i.e. known to have 0 in the low bits).

foad updated this revision to Diff 447736.Jul 26 2022, 9:08 AM

Add a test case.

foad added a comment.Aug 1 2022, 5:43 AM

Ping! The immediate motivation for this is to avoid D130080 (already accepted) causing this regression:

diff --git a/llvm/test/Transforms/InstCombine/cast.ll b/llvm/test/Transforms/InstCombine/cast.ll
index 3fafbd69e032..ec17b24f4c2a 100644
--- a/llvm/test/Transforms/InstCombine/cast.ll
+++ b/llvm/test/Transforms/InstCombine/cast.ll
@@ -680,18 +680,19 @@ define i64 @test49(i64 %A) {
   %C = or i32 %B, 1
   %D = sext i32 %C to i64
   ret i64 %D
 }
 
 define i64 @test50(i64 %x) {
 ; ALL-LABEL: @test50(
 ; ALL-NEXT:    [[TMP1:%.*]] = shl i64 [[X:%.*]], 30
-; ALL-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], -4294967296
-; ALL-NEXT:    [[E:%.*]] = ashr i64 [[TMP2]], 32
+; ALL-NEXT:    [[D:%.*]] = and i64 [[TMP1]], -4294967296
+; ALL-NEXT:    [[SEXT:%.*]] = add i64 [[D]], -4294967296
+; ALL-NEXT:    [[E:%.*]] = ashr exact i64 [[SEXT]], 32
 ; ALL-NEXT:    ret i64 [[E]]
 ;
   %a = lshr i64 %x, 2
   %B = trunc i64 %a to i32
   %D = add i32 %B, -1
   %E = sext i32 %D to i64
   ret i64 %E
 }

The current patch fixes it by noticing that the "and" is redundant because it only affects bits that are not demanded by the "add".

Sorry, I didn't notice the dependency between D130080 and this one.

But I'm not seeing test coverage for the change here - the instcombine test is already folded without this patch:
https://alive2.llvm.org/ce/z/RtXYQq

I thought this patch would have an effect like this:
https://alive2.llvm.org/ce/z/XwHTxg
...but I don't see that change.

Should there be at least 2 tests (one each with add and sub) that are improved with this change?

foad updated this revision to Diff 454515.Aug 22 2022, 8:42 AM

Rebase.

foad added a comment.Aug 22 2022, 8:44 AM

But I'm not seeing test coverage for the change here - the instcombine test is already folded without this patch:
https://alive2.llvm.org/ce/z/RtXYQq

Yeah, sorry, that was confusing because the test was already being folded by the very transform that I'm working towards removing in D130080. I've added a couple of new tests that should better demonstrate the effect of the current patch.

spatel accepted this revision.Aug 22 2022, 11:03 AM

LGTM

This revision is now accepted and ready to land.Aug 22 2022, 11:03 AM
This revision was landed with ongoing or failed builds.Aug 22 2022, 12:04 PM
This revision was automatically updated to reflect the committed changes.