This is an archive of the discontinued LLVM Phabricator instance.

[SelectionDAG] Fold more offsets into GlobalAddresses
ClosedPublic

Authored by sunfish on Jan 11 2016, 2:20 PM.

Details

Summary

SelectionDAG currently misses opportunities to fold constants into GlobalAddresses in several areas. For example, given (add (add GA, c1), y), it often reassociates to (add (add GA, y), c1), missing the opportunity to create (add GA+c, y). This isn't often visible on targets such as X86 which effectively reassociate adds in their complex address-mode folding logic, however it is currently visible on WebAssembly since it currently has very simple address mode folding code that doesn't reassociate anything.

The attached patch fixes this by making SelectionDAG fold offsets into GlobalAddresses at the same times that it folds constants together, so that it doesn't miss any opportunities to perform such folding.

Diff Detail

Repository
rL LLVM

Event Timeline

sunfish updated this revision to Diff 44557.Jan 11 2016, 2:20 PM
sunfish retitled this revision from to [SelectionDAG] Fold more offsets into GlobalAddresses.
sunfish updated this object.
sunfish set the repository for this revision to rL LLVM.
sunfish added a subscriber: llvm-commits.
arsenm added a subscriber: arsenm.Jan 19 2016, 9:51 PM
arsenm added inline comments.
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
397 ↗(On Diff #44557)

Why does this need to be added here? I don't see a new use of this

782–783 ↗(On Diff #44557)

The order of these checks should be swapped

lib/CodeGen/SelectionDAG/SelectionDAG.cpp
3278–3281 ↗(On Diff #44557)

Ditto

3285 ↗(On Diff #44557)

This should be int64_t, since you use getSExtValue and negate it below

sunfish marked 4 inline comments as done.Jan 19 2016, 10:20 PM
sunfish added inline comments.
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
397 ↗(On Diff #44557)

It calls isConstantIntBuildVectorOrConstantInt, which is also being made a member function, so that we don't have to pass the TLI around. Or would it be better to just explicitly pass the TLI around so that these don't need to be member functions?

782–783 ↗(On Diff #44557)

Fixed.

lib/CodeGen/SelectionDAG/SelectionDAG.cpp
3278–3281 ↗(On Diff #44557)

Fixed.

3285 ↗(On Diff #44557)

We need to do the negate and the add as unsigned to avoid undefined behavior. But I can make the variable signed and just cast it at its uses. Fixed.

sunfish updated this revision to Diff 45350.Jan 19 2016, 10:21 PM
sunfish marked 4 inline comments as done.
  • address review comments
  • rebase on trunk
arsenm added inline comments.Jan 19 2016, 10:34 PM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
397 ↗(On Diff #45350)

I think it would be better to move this to either SelectionDAG (or maybe TLI). It should be generally useful to targets

arsenm added inline comments.Jan 19 2016, 10:48 PM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
397 ↗(On Diff #45350)

By this I mean isConstantIntBuildVectorOrConstantInt, not MatchRotateHalf

sunfish updated this revision to Diff 45353.Jan 19 2016, 10:59 PM
  • Move isConstantIntBuildVectorOrConstantInt to SelectionDAG.
sunfish marked 2 inline comments as done.Jan 19 2016, 11:00 PM
sunfish added inline comments.
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
397 ↗(On Diff #45353)

Done.

arsenm accepted this revision.Jan 19 2016, 11:00 PM
arsenm added a reviewer: arsenm.

LGTM

This revision is now accepted and ready to land.Jan 19 2016, 11:00 PM
sunfish marked an inline comment as done.Jan 19 2016, 11:07 PM

Thanks!

This revision was automatically updated to reflect the committed changes.