This is motivated by the troubles in D143624 where a change in inlining
order resulted in instcombine seeing more code and then breaking the
smin idiom. The problem is that code like this:
%shr = ashr i32 %mul, 15 %cmp4.i = icmp sgt i32 %shr, 32767 %switch.i = icmp ult i1 %cmp4.i, true %spec.select.i = select i1 %switch.i, i32 %shr, i32 32767
has the %cmp4.i being modified so that the idiom doesn't match. This patch
extends some of the attempts to detect a potential idiom by looking for the
"not" idiom of the %switch.i icmp in between the first cmp and the select.
This however isn't quite enough. After we prevent the first breakage, another
combine mutates the constant operand and condition of %cmp4.i, and unfortunately
that again prevents the idiom-preserving bail out code from working. To
get around this we re-use some of the canonicalization code that undoes that
transform to again prevent breakage.
I don't feel good about any of this.