This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Support BitTests in ThreeWayComparison. Trivial case
AbandonedPublic

Authored by mkazantsev on Apr 19 2018, 6:27 PM.

Details

Summary

Three-way comparison matcher which is used in some of InstCombine's transforms
is intended to detect the following pattern:

%c1 = icmp eq %a, %b
%c2 = icmp slt %a, %b
%select1 = select %c2, %Less, %Greater
%select2 = select %c1, %Equal, %select2

It works fine if %a and %b are some values about which we know nothing. However
if %b is a constant zero, the situation is different. If we look closer at the example:

%c1 = icmp eq %a, 0
%c2 = icmp slt %a, 0
%select1 = select %c2, %Less, %Greater
%select2 = select %c1, %Equal, %select2

Then we can see that the pattern of %c2 and %select1 is actually a bit test of the
highest bit of %a. There are rules in InstCombine that detect this couple before we
go down to detection of the three-way comparison pattern which is only seen when we
process select2. These transforms may turn %c2, %select1 into something different,
and when we process %select2 we may not be able to recognize the three-way comparison
pattern.

This patch adds support of the following transform:

%c2 = icmp slt %a, 0
%select1 = select %c2, -1, %Greater

to

%shift = ashr %a, 31
%or = or %shift, %Greater

This is a part of fix for bug 37147.

Diff Detail

Event Timeline

mkazantsev created this revision.Apr 19 2018, 6:27 PM
mkazantsev edited the summary of this revision. (Show Details)
mkazantsev edited the summary of this revision. (Show Details)Apr 19 2018, 6:30 PM
mkazantsev edited the summary of this revision. (Show Details)

Please upload all patches with the full context (-U99999).

test/Transforms/InstCombine/three-way-comparison.ll
2

Please use utils/update_test_checks.py (the file's header should say it was used), and don't adjust the ; CHECK lines afterwards manually.

Added context.

mkazantsev marked an inline comment as done.

Updated tests using script.

mkazantsev planned changes to this revision.Apr 23 2018, 10:27 PM

We will try another approach to deal with the problem, see discussion in review of D45862.

mkazantsev requested review of this revision.Apr 27 2018, 7:43 PM
mkazantsev abandoned this revision.May 2 2018, 6:45 PM

Abandoning in favour of D46086