This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner] Fold (zext (and/or/xor (shl/shr (load x), cst), cst))
ClosedPublic

Authored by Carrot on Mar 12 2018, 2:30 PM.

Details

Summary

In our real world application, we found the following optimization is missed in DAGCombiner

(zext (and/or/xor (shl/shr (load x), cst), cst)) -> (and/or/xor (shl/shr (zextload x), (zext cst)), (zext cst))

If the user of original zext is an add, it may enable further lea optimization on x86.

This patch add a new function CombineZExtLogicopShiftLoad to do this optimization.

Diff Detail

Repository
rL LLVM

Event Timeline

Carrot created this revision.Mar 12 2018, 2:30 PM
samparker added inline comments.
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
7463 ↗(On Diff #138092)

Assert that N is a zext?

test/CodeGen/X86/zext-logicop-shift-load.ll
1 ↗(On Diff #138092)

You're missing test cases for SRL and OR, as well as non constant operand to the logic op. You could also test indexed and sext loads.

Carrot updated this revision to Diff 138258.Mar 13 2018, 2:09 PM
Carrot marked 2 inline comments as done.
samparker accepted this revision.Mar 15 2018, 2:06 AM

Thanks, LGTM.

This revision is now accepted and ready to land.Mar 15 2018, 2:06 AM
This revision was automatically updated to reflect the committed changes.

This change caused widespread breakage when compiling a number of different projects, see PR36873. I'll revert this in the meantime.

Carrot reopened this revision.Mar 23 2018, 12:51 PM

Reopened to fix pr36873.

This revision is now accepted and ready to land.Mar 23 2018, 12:51 PM
Carrot updated this revision to Diff 139641.Mar 23 2018, 12:56 PM

The previous revision caused PR36873. The problem is for some targets, the VT of shift constant may be larger than the other operand. So for the extended shift, we can't simply use the result VT as the constant VT, instead we should use the max of result VT and the original constant VT.

samparker added inline comments.Mar 26 2018, 8:01 AM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
7527 ↗(On Diff #139641)

I would of thought that the actual problem was that previously you didn't use the VT from the shift at all, so I'm not convinced that you need to perform this check and select the max. In my mind, ShiftVT should be fine.

Carrot updated this revision to Diff 139823.Mar 26 2018, 11:20 AM
Carrot marked an inline comment as done.

Since both original ShiftVT and shift value are used, we can simply use the original shift constant node.

Sorry for the delay, LGTM. Thanks.

This revision was automatically updated to reflect the committed changes.