This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Try to narrow expr if trunc cannot be removed.
ClosedPublic

Authored by fhahn on Jul 1 2020, 8:51 AM.

Details

Summary

Narrowing an input expression of a truncate to a type larger than the
result of the truncate won't allow removing the truncate, but it may
enable further optimizations, e.g. allowing for larger vectorization
factors.

For now this is intentionally limited to integer types only, to avoid
producing new vector ops that might not be suitable for the target.

If we know that the only user is a trunc, we can also be allow more
cases, e.g. also shortening expressions with some additional shifts.

I would appreciate feedback on the best place to do such a narrowing.

This fixes PR43580.

Diff Detail

Event Timeline

fhahn created this revision.Jul 1 2020, 8:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 1 2020, 8:51 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
nikic added a subscriber: nikic.Jul 1 2020, 9:31 AM

vector test cases? I realize for PR43580 we can get this done before vectorization

spatel added a comment.Jul 1 2020, 9:53 AM

vector test cases? I realize for PR43580 we can get this done before vectorization

We could add tests, but I don't think we can see changes as long as we are using shouldChangeType() to guard the transform:

bool InstCombiner::shouldChangeType(Type *From, Type *To) const {
  // TODO: This could be extended to allow vectors. Datalayout changes might be
  // needed to properly support that.
  if (!From->isIntegerTy() || !To->isIntegerTy())
    return false;
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
725

DestTy should be NewDestTy here?

fhahn updated this revision to Diff 275362.Jul 3 2020, 4:19 AM
fhahn marked an inline comment as done.

Use NewDestTy instead DestTy

vector test cases? I realize for PR43580 we can get this done before vectorization

We could add tests, but I don't think we can see changes as long as we are using shouldChangeType() to guard the transform:

Thanks, I added a vector test, but it won't be transformed currently, as I don't think there's an easy way to check if the new vector would be legal (as noted in shouldChangeType. Also, limiting to integer types allows using getExtendedType.

lebedev.ri added inline comments.Jul 3 2020, 5:17 AM
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
725

Still reads as DestTy to me

fhahn updated this revision to Diff 275372.Jul 3 2020, 5:24 AM

Actually upload patch to use NewDestTy instead of DestTy........

This revision is now accepted and ready to land.Jul 3 2020, 5:52 AM
This revision was automatically updated to reflect the committed changes.