This is an archive of the discontinued LLVM Phabricator instance.

[ARM] Extract shifts out of multiply-by-constant
ClosedPublic

Authored by john.brawn on Aug 20 2015, 8:39 AM.

Details

Summary

Turning (op x (mul y k)) into (op x (lsl (mul y k>>n) n)) is beneficial when we can do the lsl as a shifted operand and the resulting multiply constant is simpler to generate.

Do this by doing the transformation when trying to select a shifted operand, as that ensures that it actually turns out better (the alternative would be to do it in PreprocessISelDAG, but we don't know for sure there if extracting the shift would allow a shifted operand to be used).

Diff Detail

Repository
rL LLVM

Event Timeline

john.brawn updated this revision to Diff 32699.Aug 20 2015, 8:39 AM
john.brawn retitled this revision from to [ARM] Extract shifts out of multiply-by-constant.
john.brawn updated this object.
john.brawn added reviewers: t.p.northover, rengolin.
john.brawn set the repository for this revision to rL LLVM.
john.brawn added a subscriber: llvm-commits.
rengolin edited edge metadata.Sep 3 2015, 2:13 AM

Hi John,

Sorry for the delay, I was on holidays.

cheers,
-renato

lib/Target/ARM/ARMISelDAGToDAG.cpp
523 ↗(On Diff #32699)

I think this should just return true/false, and let the caller change the constant at the same time as applying the shift.

My worry is if you use this in an if() with other statements, like:

if (ExtractShiftFromMul() && SomeOtherTest())
  return false;

and "SomeOtherTest()" fails, returning with the changed constant but not applying the shift.

538 ↗(On Diff #32699)

You already test this in ExtractShiftFromMul, no?

540 ↗(On Diff #32699)

If my comment above is pertinent, maybe this should be called "canExtract..." and also change N, not only PowerOfTwo, so this block remains the same, but it's more future proof when someone adds another condition after the call to "canExtract...".

735 ↗(On Diff #32699)

Same comments as above.

1404 ↗(On Diff #32699)

... :)

john.brawn added inline comments.Sep 7 2015, 5:13 AM
lib/Target/ARM/ARMISelDAGToDAG.cpp
538 ↗(On Diff #32699)

ExtractShiftFromMul checks if the constant that is being multiplied by has more than one use, but here it's checking if the multiply has more than one use. Moving this check into ExtractShiftFromMul makes sense though, so I'll do that.

john.brawn updated this revision to Diff 34163.Sep 7 2015, 9:04 AM
john.brawn edited edge metadata.

Turn ExtractShiftFromMul into canExtractShiftFromMul and move the hasOneUse check on the multiply into it.

rengolin accepted this revision.Sep 14 2015, 7:33 AM
rengolin edited edge metadata.

Sorry for the delay, LGTM. Thanks!

This revision is now accepted and ready to land.Sep 14 2015, 7:33 AM
This revision was automatically updated to reflect the committed changes.