This is an archive of the discontinued LLVM Phabricator instance.

[GVN] Fix handling of sub-byte types in big-endian mode
ClosedPublic

Authored by uweigand on Mar 31 2016, 12:02 PM.

Details

Summary

When GVN wants to re-interpret an already available value in a smaller
type, it needs to right-shift the value on big-endian systems to ensure
the correct bytes are accessed. The shift value is the difference of
the sizes of the two types.

This is correct as long as both types occupy multiples of full bytes.
However, when one of them is a sub-byte type like i1, this no longer
holds true: we still need to shift, but only to access the correct
*byte*. Accessing bits within the byte requires no shift in either
endianness; e.g. an i1 resides in the least-significant bit of its
containing byte on both big- and little-endian systems.

Therefore, the appropriate shift value to be used is the difference of
the *storage* sizes of the two types. This is already handled correctly
in one place where such a shift takes place (GetStoreValueForLoad), but
is incorrect in two other places: GetLoadValueForLoad and
CoerceAvailableValueToLoadType.

This patch changes both places to use the storage size as well.

Diff Detail

Repository
rL LLVM

Event Timeline

uweigand updated this revision to Diff 52251.Mar 31 2016, 12:02 PM
uweigand retitled this revision from to [GVN] Fix handling of sub-byte types in big-endian mode.
uweigand updated this object.
uweigand added a subscriber: llvm-commits.
chandlerc accepted this revision.Apr 7 2016, 1:22 AM
chandlerc edited edge metadata.

Very nice fix. Minor complaints below about variable naming, feel free to address and commit as you see fit.

lib/Transforms/Scalar/GVN.cpp
778 ↗(On Diff #52251)

You know, "StoreSize" and "LoadSize" are really bad variable names since they aren't actually the type store size... Which makes much more sense out of how this bug got in there in the first place.

Maybe rename them (in a separate patch) to something more clearly to do with value sizes rather than store and load sizes?

1061 ↗(On Diff #52251)

Here, we see the reverse confusion IMO: SrcValSize would be better named SrcValStoreSize.

This revision is now accepted and ready to land.Apr 7 2016, 1:22 AM
This revision was automatically updated to reflect the committed changes.

Very nice fix. Minor complaints below about variable naming, feel free to address and commit as you see fit.

Thanks for the review! I've now checked in the original patch, as well as a separate commit to rename those variables.