This is an archive of the discontinued LLVM Phabricator instance.

Add DAG combine for shl + add of constants.
ClosedPublic

Authored by arsenm on Aug 2 2014, 10:18 PM.

Details

Reviewers
andreadb
resistor
Summary

Do

(shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2)

This is already done for multiplies, but since multiplies
by powers of two are turned into shifts, we also need
to handle it here. This not happening is interfering with folding
some constant offsets into an addressing mode.

This might want checks for isLegalAddImmediate to avoid
transforming an add of a legal immediate with one that isn't.

Diff Detail

Event Timeline

arsenm updated this revision to Diff 12143.Aug 2 2014, 10:18 PM
arsenm retitled this revision from to Add DAG combine for shl + add of constants. .
arsenm updated this object.
arsenm edited the test plan for this revision. (Show Details)
arsenm added a subscriber: Unknown Object (MLST).

Hi Matt,

I have a couple of comments about your patch (see below).

lib/CodeGen/SelectionDAG/DAGCombiner.cpp
4146–4157

If I understand correctly, your rule doesn't try to fold the case where C2 is a build_vector of all ConstantSDNode or Undef. Is this intentional?
If so, then why at line 4151 you check for 'isConstantSplatVector'? I am asking this because you specifically check for N1C; however, (unless I misread the code) N1C is not null only if N1 is a ConstantSDNode.

Also, I noticed that the dag combiner already implements a similar (but less poweful) rule in 'visitADD' at around line 1658 (see also function 'combineShlAddConstant'); the rule is:
// fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )

I think that your patch would make that rule redundant. I would do an experiment and see if that is true; in case, I suggest to get rid of it (and also the static function 'combineShlAddConstant').

I hope this helps.
Andrea

arsenm added inline comments.Sep 10 2014, 9:00 PM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
4146–4157

It's sort of hard to follow, but N1C is replaced with the constant splat value under the VT.isVector() at the beginning of the function. I've added a test for vectors in the new version to make sure that works.

The combineShlAddConstant transform actually made the opposite change way back in r33361 / 2007 with the weak justification that it "fixes test/CodeGen/ARM/smul.ll" so I assume there isn't actually a real reason to avoid doing this. I've also removed it in the new version.

arsenm updated this revision to Diff 13577.Sep 10 2014, 9:01 PM

Remove redundant transform in visitADD, add more tests

andreadb accepted this revision.Sep 11 2014, 2:34 AM
andreadb edited edge metadata.

Hi Matt,
Thanks for the explanation.
The updated patch LGTM. Thanks!

-Andrea

This revision is now accepted and ready to land.Sep 11 2014, 2:34 AM
arsenm closed this revision.Sep 11 2014, 10:44 AM

r217610