[InstCombine] simplify icmps with same operands based on dominating cmp
authorSanjay Patel <spatel@rotateright.com>
Wed, 5 Dec 2018 15:04:00 +0000 (15:04 +0000)
committerSanjay Patel <spatel@rotateright.com>
Wed, 5 Dec 2018 15:04:00 +0000 (15:04 +0000)
commitbaffae91b258d09ff97138d8c742d2e3b55c50e0
tree81443cf75c03203cc326c7aa96ba35136586ecd2
parent32483668d7104dab7a5e10b247fe047d6002d7fb
[InstCombine] simplify icmps with same operands based on dominating cmp

The tests here are based on the motivating cases from D54827.

More background:
1. We don't get these cases in general with SimplifyCFG because the root
   of the pattern match is an icmp, not a branch. I'm not sure how often
   we encounter this pattern vs. the seemingly more likely case with
   branches, but I don't see evidence to leave the minimal pattern
   unoptimized.

2. This has a chance of increasing compile-time because we're using a
   ValueTracking call to handle the match. The motivating cases could be
   handled with a simpler pair of calls to isImpliedTrueByMatchingCmp/
   isImpliedFalseByMatchingCmp, but I saw that we have a more
   comprehensive wrapper around those, so we might as well use it here
   unless there's evidence that it's significantly slower.

3. Ideally, we'd handle the fold to constants in InstSimplify, but as
   with the existing code here, we could extend this to handle cases
   where the result is not a constant, but a new combined predicate.
   That would mean splitting the logic across the 2 passes and possibly
   duplicating the pattern-matching cost.

4. As mentioned in D54827, this seems like the kind of thing that should
   be handled in Correlated Value Propagation, but that pass is currently
   limited to dealing with instructions with constant operands, so extending
   this bit of InstCombine is the smallest/easiest way to get these patterns
   optimized.

llvm-svn: 348367
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp-dom.ll