This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] Perform known-bits analysis in SimplifyAndInst
Needs ReviewPublic

Authored by uweigand on Aug 1 2017, 1:31 PM.

Details

Summary

When simplifying an And operation, it can be useful to take
known bits of the LHS and RHS into consideration. The current
code already does that to some extent:

  • Code at the end of SimplifyInstruction detects that *all* bits of the result of the And are known. However, this check only happens at the top level; it is not done for sub-expressions that are checked during simplification e.g. via ExpandBinOp.
  • A very special case of known-bits analysis was added via https://reviews.llvm.org/D33221. This only handles the case where some bits on the LHS are known zero since the LHS is a shift operation, and the rest of the bits are known one on the RHS since it is a constant mask.

We can instead implement a more general known-bits analysis, to
handle three cases:

  • If for each bit position, either LHS or RHS is known to be zero, the result must be 0.
  • If for each bit position, either the LHS is known to be zero, or the RHS is known to be one, the result equals the LHS.
  • And likewise with LHS and RHS swapped.

(The only other useful case seems to be if both LHS and RHS are
known to be all one bits. But that is already caught earlier
since in this case LHS == RHS.)

The shift-related special case is fully covered by the generic
case and can now be removed (all associated tests still pass).

One interesting scenario that is now simplified and wasn't
before is the case where sub-words are merged into a full word,
and subsequently a sub-word is extracted again. This is now
simplified to the original sub-word. (See new tests.)

Diff Detail

Event Timeline

uweigand created this revision.Aug 1 2017, 1:31 PM
craig.topper edited edge metadata.Aug 1 2017, 1:48 PM

Can something similar be done for visitOrInst? We have this tendency to add a transform for one and forget that the other is very similar.

Sure, it would be possible to add the corresponding transforms to SimplifyOrInst. However, I have been unable to trigger this at all, with any workload I've tried. Should I still add the transforms anyway?

Do you have a real workload that benefits from this. Surely InstCombine would have caught it eventually?

Yes, this benefits a real workload. This happens when the improved SimplifyAndInst now simplifies a partial result, which then allows *further* simplification in InstSimplify, in my case in ThreadBinOpOverPHI. This latter simplification doesn't seem to be done anywhere else.