This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Canonicalize min/max expressions correctly.
ClosedPublic

Authored by jmolloy on May 13 2015, 7:36 AM.

Details

Reviewers
reames
majnemer
Summary

This patch introduces a canonical form for min/max idioms where one operand
is extended or truncated. This often happens when the other operand is a
constant. For example:

%1 = icmp slt i32 %a, i32 0
%2 = sext i32 %a to i64
%3 = select i1 %1, i64 %2, i64 0

Would now be canonicalized into:

%1 = icmp slt i32 %a, i32 0
%2 = select i1 %1, i32 %a, i32 0
%3 = sext i32 %2 to i64

This builds upon a patch posted by David Majenemer
(https://www.marc.info/?l=llvm-commits&m=143008038714141&w=2). That pass
passively stopped instcombine from ruining canonical patterns. This
patch additionally actively makes instcombine canonicalize too.

Diff Detail

Repository
rL LLVM

Event Timeline

jmolloy updated this revision to Diff 25686.May 13 2015, 7:36 AM
jmolloy retitled this revision from to [InstCombine] Canonicalize min/max expressions correctly..
jmolloy updated this object.
jmolloy edited the test plan for this revision. (Show Details)
jmolloy added reviewers: majnemer, reames.
jmolloy set the repository for this revision to rL LLVM.
jmolloy added a subscriber: Unknown Object (MLST).
majnemer added inline comments.May 13 2015, 11:49 PM
lib/Transforms/InstCombine/InstCombineSelect.cpp
1169–1176

This looks the same as getICmpPredicateForMinMax.

1177–1182

I'd expect you to use the IRBuilder to construct each instruction up until the last which I'd expect you'd feed into ReplaceInstUsesWith.

jmolloy updated this revision to Diff 25763.May 14 2015, 2:58 AM

Hi David,

Thanks - updated patch attached.

Cheers,

James

majnemer added inline comments.May 14 2015, 3:43 PM
lib/Transforms/InstCombine/InstCombineSelect.cpp
1170

Don't we already have a builder called Builder ?

jmolloy updated this revision to Diff 25851.May 15 2015, 2:53 AM

Yes. Yes we do.

Patch updated.

majnemer accepted this revision.May 15 2015, 8:58 AM
majnemer edited edge metadata.

LGTM

This revision is now accepted and ready to land.May 15 2015, 8:58 AM
jmolloy closed this revision.Jul 17 2015, 2:21 AM