This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Move xori creation for scalar setccs to lowering.
ClosedPublic

Authored by craig.topper on Aug 11 2022, 3:03 PM.

Details

Summary

This patch enables expansion or custom lowering for some integer
condition codes so that any xori that is needed is created before
the last DAG combine to enable optimization.

I've seen cases where we end up with
(or (xori (setcc), 1), (xori (setcc), 1)) which we would ideally
convert to (xori (and (setcc), (setcc)), 1). This patch doesn't
accomplish that yet, but it should allow us to add DAG
combines as follow ups. Example https://godbolt.org/z/Y4qnvsq1b

Diff Detail

Event Timeline

craig.topper created this revision.Aug 11 2022, 3:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2022, 3:03 PM
craig.topper requested review of this revision.Aug 11 2022, 3:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2022, 3:03 PM
craig.topper added inline comments.Aug 11 2022, 3:12 PM
llvm/test/CodeGen/RISCV/rv32zbt.ll
656

The two xoris were the true and false values of the first cmov. The later two cmovs use the first cmov as a condition. We are now able to pull the xoris through the first cmov and fold it into the condition of the other two by swapping their true and false values.

llvm/test/CodeGen/RISCV/urem-seteq-illegal-types.ll
510

This appears to be a scheduling change only

llvm/test/CodeGen/RISCV/xaluo.ll
788–789

This is a special case related to the constant value -2. Originally we had setugt x, -2, which was converted to seteq x, -1 since that's the the only value unsigned greater than -2. We now convert it to the setult -2, x and the DAG combine misses the seteq opportunity since the constant is on the left hand side. This allows use to use the -2 for both the sub and the sltu.

reames added inline comments.Aug 15 2022, 9:41 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
3637

I'm not following something here. You're increment the rhs of a GT or UGT in the manner I'd expect to apply to a GE or UGE, but also not changing the condition code?

This looks like a dag combine btw, why does this need to be done during lowering at all?

3642

Why is swapping the operands correct here? I don't follow?

8351

I think the setcc here is only providing the 0 or 1 fact for N1, is there any way we can generalize? (e.g. known bits?) If so, we can probably separate and test separately.

craig.topper added inline comments.Aug 15 2022, 10:01 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
3637

The condition code was swapped on line 3623.

3642

Line 3623 swapped the condition code so it is now setlt or setltu

craig.topper added inline comments.Aug 15 2022, 10:34 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
3637

Prior to LegalizeDAG it would probably be reversed by DAGCombiner. I think there's an setcc+xor combine that only checks CondCodeLegal after LegalOperations. I could write an opposite DAGCombine that only runs after lowering?

reames added inline comments.Aug 15 2022, 12:35 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
3637

I'm still not following.

Original:
GT LHS, RHS

New:
LTE LHS, RHS+1

That doesn't look correct? Unless maybe there's some implicit restriction on LHS and RHS I'm missing?

3642

Agreed.

craig.topper added inline comments.Aug 15 2022, 12:46 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
3637

We started with GT LHS, RHS. Swapped the condition code to LT, and emitted (xor (LT LHS, RHS+1), 1).

Add more comments to the setcc lowering code. Swap the operands when we swap
the condition code. Use isInt<12> for the range check after guarding against
signed integer overflow.

Fix a place that still used RHS when it needs to be LHS after the swap.

Use LHS instead of RHS in another place. Maybe I shouldn't have use std::swap...

-Refactor after an offline conversation with Philip.
-Remove one use check on the constant. It's too error prone at this level. We can't tell if the other use of the constant will be folded.

reames accepted this revision.Aug 17 2022, 8:04 AM

LGTM

Thanks for explaining this offline, the new structure makes a lot more sense to me.

This revision is now accepted and ready to land.Aug 17 2022, 8:04 AM