[llvm] [DAGCombine] Propagate truncate to operands (PR #98666)
Justin Fargnoli via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 31 20:06:12 PDT 2024
================
@@ -70,8 +70,9 @@ define i16 @add1_i16(ptr addrspace(1) nocapture %arg, ptr addrspace(1) nocapture
; GFX9-NEXT: global_load_dword v0, v[0:1], off
; GFX9-NEXT: v_bfe_u32 v1, v31, 10, 10
; GFX9-NEXT: v_cmp_gt_u32_e32 vcc, v2, v1
+; GFX9-NEXT: v_cndmask_b32_e64 v1, 0, 1, vcc
; GFX9-NEXT: s_waitcnt vmcnt(0)
-; GFX9-NEXT: v_addc_co_u32_e32 v0, vcc, 0, v0, vcc
+; GFX9-NEXT: v_add_u16_e32 v0, v0, v1
----------------
justinfargnoli wrote:
At the DAG level this optimization transforms:
```
...
...
t20: i32,ch = load<(load (s32) from %ir.gep, addrspace 1)> # D:1 t0, t17, undef:i64
t13: i32 = llvm.amdgcn.workitem.id.y # D:1 TargetConstant:i64<3150>
t22: i1 = setcc # D:1 t11, t13, setugt:ch
t23: i32 = zero_extend # D:1 t22
t24: i32 = add # D:1 t20, t23
t25: i16 = truncate # D:1 t24
...
```
into
```
...
...
t20: i32,ch = load<(load (s32) from %ir.gep, addrspace 1)> # D:1 t0, t17, undef:i64
t29: i16 = truncate # D:1 t20
t13: i32 = llvm.amdgcn.workitem.id.y # D:1 TargetConstant:i64<3150>
t22: i1 = setcc # D:1 t11, t13, setugt:ch
t30: i16 = zero_extend # D:1 t22
t31: i16 = add # D:1 t29, t30
...
```
On its own, this transformation seems profitable.
However, it disables `performAddCombine()` ([source](https://github.com/llvm/llvm-project/blob/d1b311d7d2258531decaea9556d2e96ce2433817/llvm/lib/Target/AMDGPU/SIISelLowering.cpp#L14295-L14320)) from transforming:
```
...
...
t22: i1 = setcc # D:1 t45, t43, setugt:ch
t23: i32 = zero_extend # D:1 t22
t24: i32 = add # D:1 t20, t23
...
```
into
```
...
...
t22: i1 = setcc # D:1 t45, t43, setugt:ch
t50: i32,i1 = uaddo_carry # D:1 t20, Constant:i32<0>, t22
...
```
This results in the suboptimal code generation.
@arsenm, do you think enabling `add i16 x, zext i16 (setcc i1) => uaddo_carry (zext i32 x), 0, setcc` is the right fix in this case?
https://github.com/llvm/llvm-project/pull/98666
More information about the llvm-commits
mailing list