Skip to content

Commit 05527b6

Browse files
committed
[InstCombine] fold more shuffles with FP<->Int cast operands
shuffle (cast X), (cast Y), Mask --> cast (shuffle X, Y, Mask) This extends the transform added with 0353c2c. If the shuffle reduces vector length, the transform reduces the width of the cast, so that should be a win for most codegen (if not, it can be inverted).
1 parent 6edbdf8 commit 05527b6

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,8 +2302,9 @@ static Instruction *foldCastShuffle(ShuffleVectorInst &Shuf,
23022302
VectorType *ShufOpTy = cast<VectorType>(Shuf.getOperand(0)->getType());
23032303
VectorType *CastSrcTy = cast<VectorType>(Cast0->getSrcTy());
23042304

2305-
// TODO: Allow length-changing shuffles?
2306-
if (ShufTy != ShufOpTy)
2305+
// TODO: Allow length-increasing shuffles?
2306+
if (ShufTy->getElementCount().getKnownMinValue() >
2307+
ShufOpTy->getElementCount().getKnownMinValue())
23072308
return nullptr;
23082309

23092310
// TODO: Allow element-size-decreasing casts (ex: fptosi float to i8)?

llvm/test/Transforms/InstCombine/vec_shuffle.ll

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,8 +2075,6 @@ define <3 x i16> @fptoui_shuf_different_source_types(<3 x float> %x, <3 x half>
20752075
ret <3 x i16> %r
20762076
}
20772077

2078-
; negative test - must have same size elements
2079-
20802078
define <4 x i32> @fptoui_shuf_widen_elts(<4 x half> %x, <4 x half> %y) {
20812079
; CHECK-LABEL: @fptoui_shuf_widen_elts(
20822080
; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <4 x half> [[X:%.*]], <4 x half> [[Y:%.*]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
@@ -2089,7 +2087,7 @@ define <4 x i32> @fptoui_shuf_widen_elts(<4 x half> %x, <4 x half> %y) {
20892087
ret <4 x i32> %r
20902088
}
20912089

2092-
; negative test - must have same size elements
2090+
; negative test - must have same or smaller size source elements
20932091

20942092
define <4 x float> @sitofp_shuf_narrow_elts(<4 x i64> %x, <4 x i64> %y) {
20952093
; CHECK-LABEL: @sitofp_shuf_narrow_elts(
@@ -2172,7 +2170,7 @@ define <4 x i32> @fptoi_shuf(<4 x float> %x, <4 x float> %y) {
21722170
ret <4 x i32> %r
21732171
}
21742172

2175-
; negative test - length-changing shuffle
2173+
; negative test - length-increasing shuffle
21762174

21772175
define <4 x float> @sitofp_shuf_widen(<2 x i32> %x, <2 x i32> %y) {
21782176
; CHECK-LABEL: @sitofp_shuf_widen(
@@ -2187,13 +2185,10 @@ define <4 x float> @sitofp_shuf_widen(<2 x i32> %x, <2 x i32> %y) {
21872185
ret <4 x float> %r
21882186
}
21892187

2190-
; negative test - length-changing shuffle
2191-
21922188
define <2 x float> @uitofp_shuf_narrow(<4 x i32> %x, <4 x i32> %y) {
21932189
; CHECK-LABEL: @uitofp_shuf_narrow(
2194-
; CHECK-NEXT: [[NX:%.*]] = uitofp <4 x i32> [[X:%.*]] to <4 x float>
2195-
; CHECK-NEXT: [[NY:%.*]] = uitofp <4 x i32> [[Y:%.*]] to <4 x float>
2196-
; CHECK-NEXT: [[R:%.*]] = shufflevector <4 x float> [[NX]], <4 x float> [[NY]], <2 x i32> <i32 3, i32 5>
2190+
; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> [[Y:%.*]], <2 x i32> <i32 3, i32 5>
2191+
; CHECK-NEXT: [[R:%.*]] = uitofp <2 x i32> [[TMP1]] to <2 x float>
21972192
; CHECK-NEXT: ret <2 x float> [[R]]
21982193
;
21992194
%nx = uitofp <4 x i32> %x to <4 x float>

0 commit comments

Comments
 (0)