This is an archive of the discontinued LLVM Phabricator instance.

[KnownBits] Define and use intersectWith and unionWith
ClosedPublic

Authored by foad on May 12 2023, 5:46 AM.

Details

Summary

Define intersectWith and unionWith as two complementary ways of
combining KnownBits. The names are chosen for consistency with
ConstantRange.

Deprecate commonBits as a synonym for intersectWith.

Diff Detail

Event Timeline

foad created this revision.May 12 2023, 5:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 12 2023, 5:46 AM
foad requested review of this revision.May 12 2023, 5:46 AM
goldstein.w.n added inline comments.May 12 2023, 8:21 AM
llvm/include/llvm/Support/KnownBits.h
297

Personally the text after the 'i.e' seems more useful for understanding the code (and I think matches the motivation for the code better) so I would swap the order.

'Returns the information that is known to be true for both this (aka LHS) and RHS. Another way to look at it is the greatest lower bound of LHS and RHS.'

Likewise below.

310

Since we already have KnownBits::commonBits maybe instead of adding the new essentially equivilent meet API, we could just add a non-static version of commonBits (so you could do Known.commonBits(Known2)). Seems like an all around simpler change and probably more familiar for people who have used the interface.

foad updated this revision to Diff 521659.May 12 2023, 8:36 AM

Reword descriptions.

foad marked an inline comment as done.May 12 2023, 8:38 AM
foad added inline comments.
llvm/include/llvm/Support/KnownBits.h
310

Personally I think having good and complementary names for the two operations is more important than maintaining the old name, even if it is familiar to some people.

goldstein.w.n added inline comments.May 12 2023, 1:55 PM
llvm/include/llvm/Support/KnownBits.h
310

Fair enough.

This revision is now accepted and ready to land.May 12 2023, 3:01 PM
nikic added a comment.May 13 2023, 7:25 AM

Two pieces of feedback on the API:

  • Can we call this union and intersect instead of join and meet? I think this will be more obvious to most readers.
  • The fact that the instance methods modify the value in place rather than returning a new one is rather surprising -- isn't the usual convention for KnownBits methods to return the result?
foad added a comment.May 13 2023, 9:57 AM
  • Can we call this union and intersect instead of join and meet? I think this will be more obvious to most readers.

No because union is a keyword :-( Apart from that I think it is a good candidate. I did think seriously about union/intersect, as well as other options like and/or.

  • The fact that the instance methods modify the value in place rather than returning a new one is rather surprising -- isn't the usual convention for KnownBits methods to return the result?

Maybe but I don't think there is much consistency. I chose to do it this way for a couple of reasons:

  1. It is based on the way we implement binary operators like &. The work is done in the assignment form of the operator, &=, and the various non-assignment forms are implemented in terms of that. (And this was based on me googling for best practice for overloading operators for value-like classes.)
  2. The meet-assignment form seems to be very useful in practice.

But I don't mind changing it if there is consensus that having A.meet(B) modify A is surprising.

barannikov88 added a subscriber: barannikov88.EditedMay 13 2023, 10:16 AM

meet and join sound like advanced stuff from something called lattice theory.
Why don't just use overloaded operators?

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
387

Known = LHS & RHS;
?

foad added a comment.May 13 2023, 10:57 AM

Why don't just use overloaded operators?

Because operator& already does something different. It returns the known bits of the expression A&B, given the known bits of A and the known bits of B. That was probably my idea. I thought it would be nice if simple operators like A&B, A+B, A<<B etc all worked consistently in that way. But I guess we could abandon that plan and change the meaning of operator&.

Why don't just use overloaded operators?

Because operator& already does something different. It returns the known bits of the expression A&B, given the known bits of A and the known bits of B.

This may be a bit surprising, but in a way it makes sense.

nikic added a comment.May 13 2023, 2:15 PM
  • Can we call this union and intersect instead of join and meet? I think this will be more obvious to most readers.

No because union is a keyword :-( Apart from that I think it is a good candidate. I did think seriously about union/intersect, as well as other options like and/or.

Oh, so that's why the corresponding ConstantRange methods are called unionWith and intersectWith...

foad updated this revision to Diff 522108.May 15 2023, 3:24 AM

Change names.

foad retitled this revision from [KnownBits] Define and use meet and join operations to [KnownBits] Define and use intersectWith and unionWith.May 15 2023, 3:25 AM
foad edited the summary of this revision. (Show Details)
  • Can we call this union and intersect instead of join and meet? I think this will be more obvious to most readers.

No because union is a keyword :-( Apart from that I think it is a good candidate. I did think seriously about union/intersect, as well as other options like and/or.

Oh, so that's why the corresponding ConstantRange methods are called unionWith and intersectWith...

Thanks, I did not know about them. It seems best to copy that design.

nikic accepted this revision.May 15 2023, 1:08 PM

LGTM

llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
241

Side note: Should use isUnknown() here.

foad added inline comments.May 16 2023, 1:22 AM
llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
241
This revision was landed with ongoing or failed builds.May 16 2023, 1:27 AM
This revision was automatically updated to reflect the committed changes.