[RISCV] Add DAG combine for CTTZ/CTLZ in the case of input 0
authorDjordje Todorovic <djolertrk@gmail.com>
Wed, 19 Jul 2023 09:25:51 +0000 (11:25 +0200)
committerDjordje Todorovic <djordje.todorovic@syrmia.com>
Wed, 19 Jul 2023 14:22:04 +0000 (16:22 +0200)
commit80e20c8a8d074cd4053d9dbfd675025a28240c85
tree5f4350df710e0b00388120d3354c55558dcb0a1b
parentbe6e9df11f880ab128aef6550c6911d9f091e7d7
[RISCV] Add DAG combine for CTTZ/CTLZ in the case of input 0

Within the AggressiveInstCombine Pass we have
an analysis/optimization that matches that
pattern of the Table Based CTZ. Some Targets do
not support/define ctz(0), but since the
AggressiveInstCombine is just an extension of
InstCombine, it should be a target-independent
canonicalization Pass, and therefore, we decided
to introduce several instructions, such as select
and compare that produce canonical IR, even if
the input is 0. The task for the Targets that do
support that input is to handle such a case and
to produce an optimal assembly.

This patch optimizes the CTTZ/CTLZ instructions
if the input is 0 by performing the`DAG combine`,
by generating the cttz(x) & 0x1f pattern (the
same goes for ctlz as well).

Differential Revision: https://reviews.llvm.org/D151449
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
llvm/test/CodeGen/RISCV/ctz_zero_return_test.ll [new file with mode: 0644]