This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Relax preconditions for ashr+and+icmp fold (PR44754)
ClosedPublic

Authored by nikic on Feb 9 2020, 1:06 AM.

Details

Summary

Fix for https://bugs.llvm.org/show_bug.cgi?id=44754. We already have a fold that converts icmp (and (ashr X, C3), C2), C1 into icmp (and C2'), C1', but it imposed overly strict requirements on the transform.

Relax this by checking that both C2 and C1 don't shift out bits (in a signed sense) when forming the new constants. Alive proofs (https://rise4fun.com/Alive/PTz0):

Name: ashr_legal
Pre: ((C2 << C3) >> C3) == C2 && ((C1 << C3) >> C3) == C1
%a = ashr i16 %x, C3
%b = and i16 %a, C2
%c = icmp i16 %b, C1
=>
%d = and i16 %x, C2 << C3
%c = icmp i16 %d, C1 << C3

Name: ashr_shiftout_eq
Pre: ((C2 << C3) >> C3) == C2 && ((C1 << C3) >> C3) != C1
%a = ashr i16 %x, C3
%b = and i16 %a, C2
%c = icmp eq i16 %b, C1
=>
%c = false

Note that >> corresponds to ashr here. The case of an equality comparison has some special handling in this transform, because it will form to a true/false result if the condition on the comparison constant it violated.

Diff Detail

Event Timeline

nikic created this revision.Feb 9 2020, 1:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 9 2020, 1:06 AM
spatel accepted this revision.Feb 17 2020, 1:49 PM

LGTM

llvm/test/Transforms/InstCombine/icmp.ll
1929–1936

Add a 'ne' sibling for this test if we don't have it already?

This revision is now accepted and ready to land.Feb 17 2020, 1:49 PM
This revision was automatically updated to reflect the committed changes.
nikic marked an inline comment as done.