[llvm] [AMDGPU] Omit umin on ctlz/cttz if operand is non-zero. (PR #79127)

Leon Clark via llvm-commits llvm-commits at lists.llvm.org
Fri May 17 01:02:08 PDT 2024


PeddleSpam wrote:

> > > Instead of doing this during the lowering, should the combine on CTLZ/CTTZ transform the non-undef version into the undef version if the input is known non-zero? I thought it was already doing that (it is https://github.com/llvm/llvm-project/blob/9731b77e80261c627d79980f8c275700bdaf6591/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp#L11005C6-L11005C7)
> > 
> > 
> > It must be missing some cases. Otherwise the tests wouldn't change.
> 
> Yes, so should debug why that happened. We shouldn't need to reinvent optimizations during the lowering

At the point where `DAGCombiner` is called we only have a `CTLZ`/`CTTZ` op of a load, so `SelectionDAG::isKnownNeverZero` correctly returns `false`. However, during legalisation we introduce an `OR` of the loaded value with a constant.

```
Legalizing: t18: i16 = cttz t45
Trying to promote node
Creating new node: t51: i32 = any_extend t45
Creating constant: t52: i32 = Constant<65536>
Creating new node: t53: i32 = or t51, Constant:i32<65536>
Creating new node: t54: i32 = cttz t53
Creating new node: t55: i16 = truncate t54
Successfully promoted node
 ... replacing: t18: i16 = cttz t45
     with:      t55: i16 = truncate t54
```

Custom legalisation happens after this so we're able to determine that the operand is non-zero. It's an optimisation that can only happen after promotion.

https://github.com/llvm/llvm-project/pull/79127


More information about the llvm-commits mailing list