This is an archive of the discontinued LLVM Phabricator instance.

[GVN] Replace an inverted comparison with a logical not
Needs ReviewPublic

Authored by majnemer on Apr 20 2016, 9:29 PM.

Details

Summary

If we have two comparisons:

%iszero = icmp eq i32 %A, 0
%isnotzero = icmp ne i32 %A, 0

we should be able to turn one into the logical not of the other:

%iszero = icmp eq i32 %A, 0
%isnotzero = xor i1 %iszero, true

This fixes PR27431.

Diff Detail

Event Timeline

majnemer updated this revision to Diff 54454.Apr 20 2016, 9:29 PM
majnemer retitled this revision from to [GVN] Replace an inverted comparison with a logical not.
majnemer updated this object.
majnemer added reviewers: nlewycky, dberlin.
majnemer added a subscriber: llvm-commits.

Do you think this would be worth adding to EarlyCSE as well since it could lead to simplifications that could effect inlining decisions?

This looks fine to me.

reames added a subscriber: reames.Apr 21 2016, 8:19 AM

As a canonicalization, this makes sense, but I'm worried it will lead to bad code generation. Having to materialize a flag into a register to negate it seems potentially problematic. Do we already have code in the backend (CGP?) which undoes this transformation? If not, I think we're likely to need it.

Also, how does this impact the dominating condition optimizations in SimplifyCFG and JumpThreading? Those optimizations expect the dominated conditional branch to be fed by a compare rather than a not of the dominating compare.

@reames I believe CodeGenPrepare::SinkCmpExpressions already takes care of this.

@mcrosier I would guess that any cond branches that have not(c) as their condition would get simplified to use just 'c' and swap the branch targets, so I don't think this would impact the optimizations you mentioned.

dberlin edited edge metadata.Apr 21 2016, 9:58 PM
dberlin added a subscriber: dberlin.

My suggestion, given the comments on this thread, would be for someone to
make a decision (or build a consensus) on what the canonical form of this
kind of thing should look like, and then we just go and make everything do
that :P, whether it's the result david has produced, or compare over
compare, or whatever.