[RISCV] Teach RISCVCodeGenPrepare to optimize (zext (abs(i32 X, i1 1))).
authorCraig Topper <craig.topper@sifive.com>
Mon, 25 Jul 2022 16:13:21 +0000 (09:13 -0700)
committerCraig Topper <craig.topper@sifive.com>
Mon, 25 Jul 2022 16:36:41 +0000 (09:36 -0700)
commit1db6d6dcd888540574e5ada6dadc09029e5e79ec
treeac7541b23fe9ba8c35816ee6e61d7e19be79e68f
parent4cf30c5157a6ff7d22c7f663ed48d079877cb71b
[RISCV] Teach RISCVCodeGenPrepare to optimize (zext (abs(i32 X, i1 1))).

(abs(i32 X, i1 1) always produces a positive result. The 'i1 1'
means INT_MIN input produces poison. If the result is sign extended,
InstCombine will convert it to zext. This does not produce ideal
code for RISCV.

This patch reverses the zext back to sext which can be folded
into a subw or negw. Ideally we'd do this in SelectionDAG, but
we lose the INT_MIN poison flag when llvm.abs becomes ISD::ABS.

Reviewed By: reames

Differential Revision: https://reviews.llvm.org/D130412
llvm/lib/Target/RISCV/RISCVCodeGenPrepare.cpp
llvm/test/CodeGen/RISCV/iabs.ll