This is an archive of the discontinued LLVM Phabricator instance.

When canonicalizing gep indices, prefer zext when possible
ClosedPublic

Authored by reames on Jan 29 2015, 8:21 AM.

Details

Summary

We eagerly canonicalize gep indices to the width of a machine register (i64 on x86_64). To do so, we insert sign extensions (sext) to promote smaller types. This change substitutes zero extensions where we know the value being extended is positive. This is motivated by the fact that zero extensions are generally cheaper on x86.

Q for reviewer: Should this be behind a target hook? If so, which and how?

On a broader topic, I don't really get why we're canonicalizing this way at all. Does anyone have a strong reason why this is right approach?

Diff Detail

Repository
rL LLVM

Event Timeline

reames updated this revision to Diff 18929.Jan 29 2015, 8:21 AM
reames retitled this revision from to When canonicalizing gep indices, prefer zext when possible.
reames updated this object.
reames edited the test plan for this revision. (Show Details)
reames added a subscriber: Unknown Object (MLST).
majnemer added inline comments.
lib/Transforms/InstCombine/InstCombineCasts.cpp
1067–1074 ↗(On Diff #18929)

This looks like it can be a separate change.

lib/Transforms/InstCombine/InstructionCombining.cpp
1326–1331 ↗(On Diff #18929)

Isn't this change redundant with the one in InstCombineCasts? It would be nice to have this logic in one place.

Responded to small comments inline. I'm not going to both updating the patch just yet. The high level questions I asked in the original review need to be addressed first. Rephrased:

Is canonicalizing sign extension to zero extension a good idea on all targets? It appears to work out well on x86, but do other targets have concerns? Does this need to be behind a target hook?

lib/Transforms/InstCombine/InstCombineCasts.cpp
1067–1074 ↗(On Diff #18929)

It could yes, but the broader question of whether this transform is a good idea is global to all the locations.

When submitting, I'm happy to split this into a separate patch with it's own test.

lib/Transforms/InstCombine/InstructionCombining.cpp
1326–1331 ↗(On Diff #18929)

Arguably, it is. I'm fine removing this.

majnemer added inline comments.Feb 13 2015, 2:09 PM
lib/Transforms/InstCombine/InstCombineCasts.cpp
1067–1074 ↗(On Diff #18929)

I'm of the opinion that zext should be more canonical than sext in this case because zext's are simpler to analyze. I can't immediately see why this canonicalization wouldn't be preferable except for bugs/deficiencies in other combines.

reames added inline comments.Feb 13 2015, 2:22 PM
lib/Transforms/InstCombine/InstCombineCasts.cpp
1067–1074 ↗(On Diff #18929)

Ok, I'll clean this up and separate the two patches. Do you want another round of review or should I just submit them?

hfinkel added inline comments.
lib/Transforms/InstCombine/InstCombineCasts.cpp
1067–1074 ↗(On Diff #18929)

I agree; I'm in favor of doing this.

reames updated this revision to Diff 19933.Feb 13 2015, 2:33 PM

Here's only the inst combine part

hfinkel accepted this revision.Feb 13 2015, 3:45 PM
hfinkel added a reviewer: hfinkel.

LGTM.

lib/Transforms/InstCombine/InstCombineCasts.cpp
1076 ↗(On Diff #19933)

No need for two blank lines here.

This revision is now accepted and ready to land.Feb 13 2015, 3:45 PM
This revision was automatically updated to reflect the committed changes.