This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombine] Don't fold a trunc if it feeds an anyext
ClosedPublic

Authored by mkuper on Aug 25 2016, 3:13 PM.

Details

Summary

Legalization tends to create anyext(trunc) patterns. This should always be combined - into either a single trunc, a single ext, or nothing if the types match exactly. But if we happen to combine the trunc first, we may pull the trunc away from the anyext or make it implicit (e.g. the truncate(extract) -> extract(bitcast) fold). To prevent this, we can avoid doing the fold, similarly to how we already handle fpround(fpextend).

This is somewhat ugly, but the alternative is to try to undo this later when we combine the anyext, and that gets painful.

Diff Detail

Repository
rL LLVM

Event Timeline

mkuper updated this revision to Diff 69270.Aug 25 2016, 3:13 PM
mkuper retitled this revision from to [DAGCombine] Don't fold a trunc if it feeds an anyext.
mkuper updated this object.
mkuper added reviewers: RKSimon, arsenm.
mkuper added a subscriber: llvm-commits.
RKSimon edited edge metadata.Aug 26 2016, 2:28 AM

Are the mem-intrin-base-reg.ll changes relevant?

lib/CodeGen/SelectionDAG/DAGCombiner.cpp
7139 ↗(On Diff #69270)

Should we limit this to AfterLegalizeTypes or later?

Are the mem-intrin-base-reg.ll changes relevant?

Surprisingly, yes.

The test was documented to use a vector pattern to create an "aligned temporary" from the vector compare and extract.
We used to generate:

pcmpgtd %xmm1, %xmm0
movdqa  %xmm0, 32(%esp)
movzbl  32(%esp), %eax
andl    $1, %eax

With this patch, we no longer have the temporary - we now get:

pcmpgtd %xmm1, %xmm0
movd    %xmm0, %eax
andl    $1, %eax

This is clearly better - but the temporary at 32(%esp) is gone.
The alloca forces the stack back into alignment.

lib/CodeGen/SelectionDAG/DAGCombiner.cpp
7139 ↗(On Diff #69270)

My motivation for this was a situation that occurs post-legalization, but I don't see anything specific to being post-legalization that makes this a good (or bad) idea.
Why do you think we should limit this?

RKSimon accepted this revision.Aug 31 2016, 6:14 AM
RKSimon edited edge metadata.

LGTM

lib/CodeGen/SelectionDAG/DAGCombiner.cpp
7139 ↗(On Diff #69270)

I was trying to think if there were any cases where it should be reduced, but you're right it will be from legalization anyhow.

test/CodeGen/X86/2011-10-21-widen-cmp.ll
18 ↗(On Diff #69270)

This code still makes me shudder, but its a job for another day.....

This revision is now accepted and ready to land.Aug 31 2016, 6:14 AM
This revision was automatically updated to reflect the committed changes.