This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombine] Consider SETULT as carry flag
AbandonedPublic

Authored by chfast on Jan 24 2022, 5:07 AM.

Details

Summary

The SETcc/SETULT sets carry flag in the same way as usub, so it can
be considered as a carry flag and pulled into addcarry, subcarry nodes.

Diff Detail

Event Timeline

chfast created this revision.Jan 24 2022, 5:07 AM
chfast requested review of this revision.Jan 24 2022, 5:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 24 2022, 5:07 AM
RKSimon added inline comments.Jan 25 2022, 5:57 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
2772

Do we need any getBooleanContents checks here?

What do you think about this idea in general? Can we assume or check if SETULT actually affects CL flag on given target?

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
2772

I'm not sure. But I can pass it though the getBooleanContents check below.

craig.topper added inline comments.Jan 25 2022, 9:39 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
2773

Use return SDValue() to match the rest of the code.

chfast updated this revision to Diff 402961.Jan 25 2022, 10:29 AM

Address review comments.

chfast edited the summary of this revision. (Show Details)Jan 25 2022, 10:30 AM
chfast updated this revision to Diff 403044.Jan 25 2022, 2:51 PM
chfast marked an inline comment as done.

Use switch. Rebased.

Is ISD::SETULT documented as affecting carry flag?

Is ISD::SETULT documented as affecting carry flag?

I have not found anything about it in code comments.

I'm now investigating what is SETCCCARRY node kind about...

BTW - how come we still have the ISD::ADDE/SUBE nodes? I thought we were intending to get rid of them as the glue nodes made them difficult to work with.

BTW - how come we still have the ISD::ADDE/SUBE nodes? I thought we were intending to get rid of them as the glue nodes made them difficult to work with.

I'm happy to remove these later if you give me hints where to start.

What I try to do in the end is to fix https://github.com/llvm/llvm-project/issues/53432. Maybe this particular change is not good direction, but on the other hand it also helps with the test add_U320_without_i128_add already in repo.

Maybe perform this inside X86ISelLowering?

Maybe perform this inside X86ISelLowering?

I'm totally inexperienced with X86ISelLowering but I'm guessing I'd need to duplicate some/all DAGCombine match patters which take carry flag into account. These tend to be quite complex.

craig.topper added inline comments.Feb 16 2022, 2:05 PM
llvm/test/CodeGen/X86/addcarry.ll
1335

This case looks like an easy X86 backend peephole.

X86ISD::ADC with an unused flag result, 0 for RHS, LHS is a single use ADD. If so fold the ADD operands into a new X86ISD::ADC.

I'm not sure about the @add_U320_without_i128_add case without staring at it for a lot longer.

RKSimon added inline comments.Feb 18 2022, 3:37 AM
llvm/test/CodeGen/X86/addcarry.ll
1335

combineSBB already does something similar, we're just missing this from combineADC

This was intermediate change to solve https://github.com/llvm/llvm-project/issues/53432. There I want to merge sub + cmp into single sbb. Any suggestions how to solve it differently?

chfast added inline comments.Feb 23 2022, 2:03 PM
llvm/test/CodeGen/X86/addcarry.ll
1335

@chfast reverse-ping - any luck with working out how best to deal with add_U320_without_i128_add?

Herald added a project: Restricted Project. · View Herald TranscriptMar 13 2022, 8:24 AM

@chfast reverse-ping - any luck with working out how best to deal with add_U320_without_i128_add?

No. I tried to untangle the graph here for long time but without any meaningful results. But I believe the test can be reduced. This is not my priority any more but I may contribute a reduced tests some day.

On the high level note, using builtin_subc() or builtin_sub_with_overflow() is good enough workaround.

I also think it would be very nice to have generic addc/subc intrinsic on LLVM IR level. Then many of these carry matching could be done in LLVM IR.

Should I try to push this by adding a TTI flag like "CmpULTsetsCarryFlag"?

Please can you rebase to see where we currently are? Also D127115 affects these tests - so maybe see whether this SETULT change is still needed after that patch or whether something else can further improve the codegen?

chfast updated this revision to Diff 453657.Aug 18 2022, 7:17 AM

Rebased.

Please can you rebase to see where we currently are? Also D127115 affects these tests - so maybe see whether this SETULT change is still needed after that patch or whether something else can further improve the codegen?

For what it is worth, when the dag is processed in fully topological order, and you add in D57317 , then you get optimal codegen for these tests.

Please can you rebase to see where we currently are? Also D127115 affects these tests - so maybe see whether this SETULT change is still needed after that patch or whether something else can further improve the codegen?

For what it is worth, when the dag is processed in fully topological order, and you add in D57317 , then you get optimal codegen for these tests.

Yes, this is great. Thanks for reaching out. I will try to be little involved in both then.

I'm not actively working on this one so I will probably focus on regression tests for combining cmp into sbb (as the result of recent changes to InstCombine).

lebedev.ri resigned from this revision.Jan 12 2023, 5:31 PM

This review may be stuck/dead, consider abandoning if no longer relevant.
Removing myself as reviewer in attempt to clean dashboard.

chfast abandoned this revision.Jan 13 2023, 12:59 AM