[InstCombine] Always try to invert non-canonical predicate of an icmp
authorRoman Lebedev <lebedev.ri@gmail.com>
Sat, 4 Jul 2020 14:39:48 +0000 (17:39 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Sat, 4 Jul 2020 15:12:04 +0000 (18:12 +0300)
commitc3b8bd1eea5b74b6cd4a89f3c221bc2dfa891248
treec6205f8584f1f2a1ca5ec3c7fa35c8c717cf2855
parentef70cc9d1ac69125f38910d4c66388536f49f4dc
[InstCombine] Always try to invert non-canonical predicate of an icmp

Summary:
The actual transform i was going after was:
https://rise4fun.com/Alive/Tp9H
```
Name: zz
Pre: isPowerOf2(C0) && isPowerOf2(C1) && C1 == C0
%t0 = and i8 %x, C0
%r = icmp eq i8 %t0, C1
  =>
%t = icmp eq i8 %t0, 0
%r = xor i1 %t, -1

Name: zz
Pre: isPowerOf2(C0)
%t0 = and i8 %x, C0
%r = icmp ne i8 %t0, 0
  =>
%t = icmp eq i8 %t0, 0
%r = xor i1 %t, -1
```
but as it can be seen from the current tests, we already canonicalize most of it,
and we are only missing handling multi-use non-canonical icmp predicates.

If we have both `!=0` and `==0`, even though we can CSE them,
we end up being stuck with them. We should canonicalize to the `==0`.

I believe this is one of the cleanup steps i'll need after `-scalarizer`
if i end up proceeding with my WIP alloca promotion helper pass.

Reviewers: spatel, jdoerfert, nikic

Reviewed By: nikic

Subscribers: zzheng, hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D83139
13 files changed:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/ThinLTO/X86/cfi-devirt.ll
llvm/test/Transforms/InstCombine/canonicalize-selects-icmp-condition-bittest.ll
llvm/test/Transforms/InstCombine/icmp-mul-zext.ll
llvm/test/Transforms/InstCombine/logical-select.ll
llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll
llvm/test/Transforms/InstCombine/shift.ll
llvm/test/Transforms/LoopUnroll/runtime-loop-multiple-exits.ll
llvm/test/Transforms/PGOProfile/chr.ll
llvm/test/Transforms/SimplifyCFG/merge-cond-stores.ll