This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] change bitwise logic type to eliminate bitcasts
ClosedPublic

Authored by spatel on Nov 14 2016, 3:27 PM.

Details

Summary

In PR27925:
https://llvm.org/bugs/show_bug.cgi?id=27925

...we proposed adding this fold to eliminate a bitcast. In D20774, there was some concern about changing the type of a bitwise op as well as creating bitcasts that might not be free for a target. However, if we're strictly eliminating an instruction (by limiting this to one-use ops), then we should be able to do this in InstCombine.

I purposely chose vector+scalar types in a couple of the tests to show the possibility of changing the logic op from a scalar to vector or vice-versa. But again, I think we're safe because we can assume that the source and destination types are DataLayout-legal because InstCombine shouldn't be creating strange types. Ie, I would typically only expect this pattern to emerge for vector-vector bitcasts like in the PR27925 test.

Diff Detail

Repository
rL LLVM

Event Timeline

spatel updated this revision to Diff 77892.Nov 14 2016, 3:27 PM
spatel retitled this revision from to [InstCombine] change bitwise logic type to eliminate bitcasts.
spatel updated this object.
spatel added reviewers: efriedma, majnemer.
spatel added a subscriber: llvm-commits.

Note - the re-motivation to look at this pattern came up because there's a similar problem in the "ideal" example in PR6137:
https://llvm.org/bugs/show_bug.cgi?id=6137#c6

efriedma edited edge metadata.Nov 21 2016, 4:47 PM

I'm a little skeptical this is a good idea in all cases... it would be terrible to transform a vector xor into an i128 xor (and therefore force the operation into integer registers) just because SROA happened to produce an i128, or some calling convention has weird requirements. The transform looks good for your testcase in PR27925 in particular because "xor <2 x i64>" and "xor <4 x i32>" lower to the same instruction on x86.

lib/Transforms/InstCombine/InstCombineCasts.cpp
1796 ↗(On Diff #77892)

Don't you need some sort of check that DestTy is an integer type?

I'm a little skeptical this is a good idea in all cases... it would be terrible to transform a vector xor into an i128 xor (and therefore force the operation into integer registers) just because SROA happened to produce an i128, or some calling convention has weird requirements. The transform looks good for your testcase in PR27925 in particular because "xor <2 x i64>" and "xor <4 x i32>" lower to the same instruction on x86.

I see 3 possible ways to solve/work-around this:

  1. Don't do this transform in InstCombine (but then we're missing a potential canonicalization).
  2. Limit this to vector-vector transforms in InstCombine (but then we're still missing a potential canonicalization for some types).
  3. Fix it up in DAGCombine by using TLI to transform away from illegal operations.

I think #3 is the answer, but please let me know if I've missed any possibilities.

lib/Transforms/InstCombine/InstCombineCasts.cpp
1796 ↗(On Diff #77892)

Oops - yes, that was an oversight.

3 would be nice... but I'm not sure you can actually implement it properly without global isel because the operands/users could be defined in different blocks.

3 would be nice... but I'm not sure you can actually implement it properly without global isel because the operands/users could be defined in different blocks.

Hmm..good point. Could add to the CGP pile instead? But I'll propose that we use #2 with a FIXME comment for this patch, so we can make partial progress (I've only seen vector code with this problem so far).

Not particularly excited about 2, but I don't see a better solution.

spatel updated this revision to Diff 78932.Nov 22 2016, 1:32 PM
spatel edited edge metadata.

Patch updated:

  1. Added a check on the destination type to be an integer element type (can't do a bitwise op otherwise).
  2. Added a test for that case.
  3. Restricted the transform to vector types (FIXME comment in the code) pending a backend fix-up to make sure we're not creating a mess with an illegal logic op for the destination type.
efriedma accepted this revision.Nov 22 2016, 1:40 PM
efriedma edited edge metadata.

LGTM.

This revision is now accepted and ready to land.Nov 22 2016, 1:40 PM
This revision was automatically updated to reflect the committed changes.