This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner][X86][SystemZ] Canonicalize SSUBO with immediate RHS to SADDO by negating the immediate.
ClosedPublic

Authored by craig.topper on Mar 29 2019, 4:47 PM.

Details

Summary

This lines up with what we do for regular subtract and it matches up better with X86 assumptions in isel patterns that add with immediate is more canonical than sub with immediate.

Diff Detail

Event Timeline

craig.topper created this revision.Mar 29 2019, 4:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2019, 4:47 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
craig.topper retitled this revision from [DAGCombiner][X86] Canonicalize SSUBO with immediate RHS to SADDO to [DAGCombiner][X86] Canonicalize SSUBO with immediate RHS to SADDO by negating the immediate..Mar 29 2019, 4:47 PM
craig.topper marked an inline comment as done.
craig.topper added inline comments.
llvm/test/CodeGen/X86/sub-with-overflow.ll
86–87

This is somewhat a regression, but we now generate the code we would get from sadd.with.overflow.i32 with an immediate of -1. So we probably need a more complete canonicalization to icmp when the data result isn't used.

RKSimon added inline comments.Mar 31 2019, 12:55 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
3046

(subo x, c)

3049

Might be wrong here, but what happens with (subo x, INT_MIN) ? That can't be expressed as a +ve integer and we're dealing with overflows here....

cc @lebedev.ri @nikic @dlrobertson - do we have a patch proposal for this transform in IR? (I'm not finding it now.)

craig.topper marked an inline comment as done.Mar 31 2019, 10:48 AM
craig.topper added inline comments.
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
3049

I think you're right. This generates the opposite value for the overflow flag for INT_MIN.

craig.topper planned changes to this revision.Mar 31 2019, 10:48 AM
nikic added a comment.Mar 31 2019, 2:04 PM

cc @lebedev.ri @nikic @dlrobertson - do we have a patch proposal for this transform in IR? (I'm not finding it now.)

Don't think we have a patch open for this yet, but @dlrobertson added some tests for this transform in D59653 already, so probably the instcombine patch will come as well...

cc @lebedev.ri @nikic @dlrobertson - do we have a patch proposal for this transform in IR? (I'm not finding it now.)

Don't think we have a patch open for this yet, but @dlrobertson added some tests for this transform in D59653 already, so probably the instcombine patch will come as well...

Sorry for the delay. Was planning on posting a patch for the IR transform in the next day or so.

spatel added a comment.Apr 1 2019, 6:07 AM

D60061 is the sibling patch for IR. Does that make the backend patch unnecessary? If not, then let's make sure the behavior is consistent between these patches.

-Add a check for INT_MIN.
-Update a SystemZ test I missed cause I only had X86 building previously. I think this one of these cases is a regression, but that's probably also going to happen with the InstCombine version of this patch too. So probably needs to be fixed regardless.

craig.topper retitled this revision from [DAGCombiner][X86] Canonicalize SSUBO with immediate RHS to SADDO by negating the immediate. to [DAGCombiner][X86][SystemZ] Canonicalize SSUBO with immediate RHS to SADDO by negating the immediate..Apr 2 2019, 3:41 PM
craig.topper added a reviewer: uweigand.
dlrobertson added inline comments.Apr 2 2019, 6:30 PM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
3047

I don't think this path would be hit post-D60061 right? Would this be for the case that opt is run without instcombine?

Also D60061 uses Constant and isNotMinSignedValue. Is there a reason not to use N1C->getConstantIntValue()->isNotMinSignedValue here too? Are there pros/cons to either one?

craig.topper marked an inline comment as done.Apr 2 2019, 6:55 PM
craig.topper added inline comments.
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
3047

Not sure if this can come up in real code after D60061, but we have lit tests here that change. Should we update those to post D60061 code instead?

IsNotMinSignedValue does dyn_cast checks for ConstantInt, ConstantFP, and vectors. Seems like a bit of wasted work if we already know we're looking at an integer.

nikic added inline comments.Apr 3 2019, 7:55 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
3047

ssubo nodes may be created during ssubsat legalization, so it's possible for nodes of this form to occur during codegen of canonical IR, even after D60061. With that in mind, I think it's okay to have this as a DAG transform as well.

isNotMinSignedValue() isn't applicable here as it operates on IR constants. We could replicate the same effect using matchUnaryPredicate() here, if we're interested in parity.

-Update a SystemZ test I missed cause I only had X86 building previously. I think this one of these cases is a regression, but that's probably also going to happen with the InstCombine version of this patch too. So probably needs to be fixed regardless.

Indeed. In fact, we see the suboptimal code today when compiling a sadd.with.overflow with the negated constant, so this should be fixed anyway. I've checked the fix in as rev 357597.

If you rebase against that revision, you should no longer see the regression.

This revision is now accepted and ready to land.Apr 8 2019, 11:23 PM
This revision was automatically updated to reflect the committed changes.