This is an archive of the discontinued LLVM Phabricator instance.

[PatternMatch] Switch to using ValueTracking::matchSelectPattern for min/max matching
ClosedPublic

Authored by jmolloy on Sep 21 2015, 12:39 PM.

Details

Reviewers
majnemer
hfinkel
Summary

Instead of rolling our own min/max matching code (which is notoriously hard to get completely right), use ValueTracking's instead.

MFCI (minimal functional change intended)

Diff Detail

Repository
rL LLVM

Event Timeline

jmolloy updated this revision to Diff 35292.Sep 21 2015, 12:39 PM
jmolloy retitled this revision from to [PatternMatch] Switch to using ValueTracking::matchSelectPattern for min/max matching.
jmolloy updated this object.
jmolloy added a reviewer: majnemer.
jmolloy set the repository for this revision to rL LLVM.
jmolloy added a subscriber: llvm-commits.
hfinkel added inline comments.
include/llvm/IR/PatternMatch.h
966

I think this makes sense, but I wonder if we should now swap the order of the checks here to check L.match and R.match first. The current ordering certainly made sense because the predicate checks done before calling those functions were cheap. Calling matchSelectPattern, however, calls isKnownNonZero (at least for integer comparisons), and that can get expensive. I'd recommend that we not call matchSelectPattern until the end. My suggestion is that we:

  1. Check that V is a SelectInst (and maybe that SI->getCondition() is a CmpInst)
  2. Check L.match(LHS) && R.match(RHS)
  3. Call matchSelectPattern and check Pred_t::match(SPR)

Hi Hal,

Thanks for the review.

include/llvm/IR/PatternMatch.h
966

That makes sense. However we can't do step 2 first, because we need to bind LHS and RHS via matchSelectPattern before we can bind L and R to them with match().

jmolloy updated this revision to Diff 35986.Sep 29 2015, 9:10 AM

Updated diff following Hal's comments.

majnemer added inline comments.Oct 12 2015, 11:48 AM
include/llvm/IR/PatternMatch.h
959–962

Unless I'm mistaken, isn't this what llvm::matchSelectPattern already does? I'd be surprised if the extra call frame matters here.

hfinkel accepted this revision.Oct 27 2015, 5:27 PM
hfinkel added a reviewer: hfinkel.

Remove the redundant check as noted, and this LGTM.

include/llvm/IR/PatternMatch.h
959–962

Yea, he half-implemented my earlier suggestion by putting this check here. But, as James pointed out, he needs to call matchSelectPattern first anyway to bind LHS and RHS. Thus, as you point out, this is not needed.

This revision is now accepted and ready to land.Oct 27 2015, 5:27 PM
jmolloy closed this revision.Nov 2 2015, 1:56 AM

Committed with required changes in r25178.