[InstCombine] allow more narrowing of casted select
authorSanjay Patel <spatel@rotateright.com>
Mon, 27 Jan 2020 21:33:13 +0000 (16:33 -0500)
committerSanjay Patel <spatel@rotateright.com>
Mon, 27 Jan 2020 21:35:50 +0000 (16:35 -0500)
commit747242af8dd03916ab46a16c1e38e716550cb60b
tree30fd46ee3eca53fb4012e6f8d631ec1199f65940
parent74df89f67f17f1e95c249831ce2d9c9d9830e496
[InstCombine] allow more narrowing of casted select

D47163 created a rule that we should not change the casted
type of a select when we have matching types in its compare condition.
That was intended to help vector codegen, but it also could create
situations where we miss subsequent folds as shown in PR44545:
https://bugs.llvm.org/show_bug.cgi?id=44545

By using shouldChangeType(), we can continue to get the vector folds
(because we always return false for vector types). But we also solve
the motivating bug because it's ok to narrow the scalar select in that
example.

Our canonicalization rules around select are a mess, but AFAICT, this
will not induce any infinite looping from the reverse transform (but
we'll need to watch for that possibility if committed).

Side note: there's a similar use of shouldChangeType() for phi ops
just below this diff, and the source and destination types appear to
be reversed.

Differential Revision: https://reviews.llvm.org/D72733
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
llvm/test/Transforms/InstCombine/cast-select.ll
llvm/test/Transforms/InstCombine/select-imm-canon.ll
llvm/test/Transforms/InstCombine/trunc.ll