Skip to content

Commit f459819

Browse files
authored
DAG: Fold bitcast of scalar_to_vector to anyext (#122660)
scalar_to_vector is difficult to make appear and test, but I found one case where this makes an observable difference. It fires more often than this in the test suite, but most of them have no net result in the final code. This helps reduce regressions in a future commit.
1 parent e9a5577 commit f459819

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16012,6 +16012,14 @@ SDValue DAGCombiner::visitBITCAST(SDNode *N) {
1601216012
if (SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT))
1601316013
return CombineLD;
1601416014

16015+
// int_vt (bitcast (vec_vt (scalar_to_vector elt_vt:x)))
16016+
// => int_vt (any_extend elt_vt:x)
16017+
if (N0.getOpcode() == ISD::SCALAR_TO_VECTOR && VT.isScalarInteger()) {
16018+
SDValue SrcScalar = N0.getOperand(0);
16019+
if (SrcScalar.getValueType().isScalarInteger())
16020+
return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SrcScalar);
16021+
}
16022+
1601516023
// Remove double bitcasts from shuffles - this is often a legacy of
1601616024
// XformToShuffleWithZero being used to combine bitmaskings (of
1601716025
// float vectors bitcast to integer vectors) into shuffles.

llvm/test/CodeGen/AMDGPU/scalar_to_vector.ll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,33 @@ define amdgpu_kernel void @scalar_to_vector_test6(ptr addrspace(1) %out, i8 zero
332332
store <2 x half> %bc, ptr addrspace(1) %out
333333
ret void
334334
}
335+
336+
; bitcast (scalar_to_vector x) -> any_extend x
337+
define i64 @bitcast_combine_scalar_to_vector_v4i16(i16 %arg) {
338+
; SI-LABEL: bitcast_combine_scalar_to_vector_v4i16:
339+
; SI: ; %bb.0:
340+
; SI-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
341+
; SI-NEXT: v_and_b32_e32 v1, 0xffff, v0
342+
; SI-NEXT: v_and_b32_e32 v2, 0xff00, v0
343+
; SI-NEXT: v_bfe_u32 v0, v0, 8, 8
344+
; SI-NEXT: v_or_b32_e32 v2, v0, v2
345+
; SI-NEXT: v_lshlrev_b32_e32 v3, 16, v2
346+
; SI-NEXT: v_or_b32_e32 v0, v1, v3
347+
; SI-NEXT: v_or_b32_e32 v1, v2, v3
348+
; SI-NEXT: s_setpc_b64 s[30:31]
349+
;
350+
; GFX89-LABEL: bitcast_combine_scalar_to_vector_v4i16:
351+
; GFX89: ; %bb.0:
352+
; GFX89-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
353+
; GFX89-NEXT: v_and_b32_e32 v1, 0xffffff00, v0
354+
; GFX89-NEXT: v_or_b32_sdwa v1, v0, v1 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_1 src1_sel:DWORD
355+
; GFX89-NEXT: v_lshlrev_b32_e32 v2, 16, v1
356+
; GFX89-NEXT: v_or_b32_sdwa v1, v1, v2 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:DWORD
357+
; GFX89-NEXT: v_or_b32_sdwa v0, v0, v2 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:DWORD
358+
; GFX89-NEXT: s_setpc_b64 s[30:31]
359+
%arg.cast = bitcast i16 %arg to <2 x i8>
360+
%tmp1 = shufflevector <2 x i8> %arg.cast, <2 x i8> poison, <8 x i32> <i32 0, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>
361+
%tmp2 = shufflevector <8 x i8> %tmp1, <8 x i8> poison, <8 x i32> <i32 0, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
362+
%cast = bitcast <8 x i8> %tmp2 to i64
363+
ret i64 %cast
364+
}

0 commit comments

Comments
 (0)