Skip to content

Commit ea7ae17

Browse files
author
git apple-llvm automerger
committed
Merge commit '54766969b28b' from llvm.org/release/21.x into stable/21.x
2 parents c875877 + 5476696 commit ea7ae17

File tree

4 files changed

+42
-41
lines changed

4 files changed

+42
-41
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class VectorCombine {
7474
const DataLayout *DL, TTI::TargetCostKind CostKind,
7575
bool TryEarlyFoldsOnly)
7676
: F(F), Builder(F.getContext(), InstSimplifyFolder(*DL)), TTI(TTI),
77-
DT(DT), AA(AA), AC(AC), DL(DL), CostKind(CostKind),
77+
DT(DT), AA(AA), AC(AC), DL(DL), CostKind(CostKind), SQ(*DL),
7878
TryEarlyFoldsOnly(TryEarlyFoldsOnly) {}
7979

8080
bool run();
@@ -88,6 +88,7 @@ class VectorCombine {
8888
AssumptionCache ∾
8989
const DataLayout *DL;
9090
TTI::TargetCostKind CostKind;
91+
const SimplifyQuery SQ;
9192

9293
/// If true, only perform beneficial early IR transforms. Do not introduce new
9394
/// vector operations.
@@ -1185,24 +1186,26 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
11851186
// Fold the vector constants in the original vectors into a new base vector to
11861187
// get more accurate cost modelling.
11871188
Value *NewVecC = nullptr;
1188-
TargetFolder Folder(*DL);
11891189
if (CI)
1190-
NewVecC = Folder.FoldCmp(CI->getPredicate(), VecCs[0], VecCs[1]);
1190+
NewVecC = simplifyCmpInst(CI->getPredicate(), VecCs[0], VecCs[1], SQ);
11911191
else if (UO)
11921192
NewVecC =
1193-
Folder.FoldUnOpFMF(UO->getOpcode(), VecCs[0], UO->getFastMathFlags());
1193+
simplifyUnOp(UO->getOpcode(), VecCs[0], UO->getFastMathFlags(), SQ);
11941194
else if (BO)
1195-
NewVecC = Folder.FoldBinOp(BO->getOpcode(), VecCs[0], VecCs[1]);
1196-
else if (II->arg_size() == 2)
1197-
NewVecC = Folder.FoldBinaryIntrinsic(II->getIntrinsicID(), VecCs[0],
1198-
VecCs[1], II->getType(), &I);
1195+
NewVecC = simplifyBinOp(BO->getOpcode(), VecCs[0], VecCs[1], SQ);
1196+
else if (II)
1197+
NewVecC = simplifyCall(II, II->getCalledOperand(), VecCs, SQ);
1198+
1199+
if (!NewVecC)
1200+
return false;
11991201

12001202
// Get cost estimate for the insert element. This cost will factor into
12011203
// both sequences.
12021204
InstructionCost OldCost = VectorOpCost;
12031205
InstructionCost NewCost =
12041206
ScalarOpCost + TTI.getVectorInstrCost(Instruction::InsertElement, VecTy,
12051207
CostKind, *Index, NewVecC);
1208+
12061209
for (auto [Idx, Op, VecC, Scalar] : enumerate(Ops, VecCs, ScalarOps)) {
12071210
if (!Scalar || (II && isVectorIntrinsicWithScalarOpAtArg(
12081211
II->getIntrinsicID(), Idx, &TTI)))
@@ -1247,15 +1250,6 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
12471250
if (auto *ScalarInst = dyn_cast<Instruction>(Scalar))
12481251
ScalarInst->copyIRFlags(&I);
12491252

1250-
// Create a new base vector if the constant folding failed.
1251-
if (!NewVecC) {
1252-
if (CI)
1253-
NewVecC = Builder.CreateCmp(CI->getPredicate(), VecCs[0], VecCs[1]);
1254-
else if (UO || BO)
1255-
NewVecC = Builder.CreateNAryOp(Opcode, VecCs);
1256-
else
1257-
NewVecC = Builder.CreateIntrinsic(VecTy, II->getIntrinsicID(), VecCs);
1258-
}
12591253
Value *Insert = Builder.CreateInsertElement(NewVecC, Scalar, *Index);
12601254
replaceValue(I, *Insert);
12611255
return true;

llvm/test/Transforms/VectorCombine/X86/intrinsic-scalarize.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ define <2 x float> @maxnum(float %x, float %y) {
1313
; AVX2-LABEL: define <2 x float> @maxnum(
1414
; AVX2-SAME: float [[X:%.*]], float [[Y:%.*]]) #[[ATTR0:[0-9]+]] {
1515
; AVX2-NEXT: [[V_SCALAR:%.*]] = call float @llvm.maxnum.f32(float [[X]], float [[Y]])
16-
; AVX2-NEXT: [[TMP1:%.*]] = call <2 x float> @llvm.maxnum.v2f32(<2 x float> poison, <2 x float> poison)
17-
; AVX2-NEXT: [[V:%.*]] = insertelement <2 x float> [[TMP1]], float [[V_SCALAR]], i64 0
16+
; AVX2-NEXT: [[V:%.*]] = insertelement <2 x float> poison, float [[V_SCALAR]], i64 0
1817
; AVX2-NEXT: ret <2 x float> [[V]]
1918
;
2019
%x.insert = insertelement <2 x float> poison, float %x, i32 0

llvm/test/Transforms/VectorCombine/binop-scalarize.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,22 @@ define <4 x i8> @udiv_ub(i8 %x, i8 %y) {
2020
%v = udiv <4 x i8> %x.insert, %y.insert
2121
ret <4 x i8> %v
2222
}
23+
24+
25+
; Unfoldable constant expression may cause infinite loop between
26+
; scalarizing insertelement and folding binop(insert(x,a,idx),insert(y,b,idx))
27+
@val = external hidden global ptr, align 8
28+
29+
define <2 x i64> @pr153012(i64 %idx) #0 {
30+
; CHECK-LABEL: define <2 x i64> @pr153012(
31+
; CHECK-SAME: i64 [[IDX:%.*]]) {
32+
; CHECK-NEXT: [[ENTRY:.*:]]
33+
; CHECK-NEXT: [[A:%.*]] = insertelement <2 x i64> <i64 5, i64 ptrtoint (ptr @val to i64)>, i64 [[IDX]], i32 0
34+
; CHECK-NEXT: [[B:%.*]] = or disjoint <2 x i64> splat (i64 2), [[A]]
35+
; CHECK-NEXT: ret <2 x i64> [[B]]
36+
;
37+
entry:
38+
%a = insertelement <2 x i64> <i64 5, i64 ptrtoint (ptr @val to i64)>, i64 %idx, i32 0
39+
%b = or disjoint <2 x i64> splat (i64 2), %a
40+
ret <2 x i64> %b
41+
}

llvm/test/Transforms/VectorCombine/intrinsic-scalarize.ll

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ define <4 x i32> @umax_fixed(i32 %x, i32 %y) {
55
; CHECK-LABEL: define <4 x i32> @umax_fixed(
66
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
77
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
8-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x i32> @llvm.umax.v4i32(<4 x i32> poison, <4 x i32> poison)
9-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
8+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> poison, i32 [[V_SCALAR]], i64 0
109
; CHECK-NEXT: ret <4 x i32> [[V]]
1110
;
1211
%x.insert = insertelement <4 x i32> poison, i32 %x, i32 0
@@ -19,8 +18,7 @@ define <vscale x 4 x i32> @umax_scalable(i32 %x, i32 %y) {
1918
; CHECK-LABEL: define <vscale x 4 x i32> @umax_scalable(
2019
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
2120
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
22-
; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 4 x i32> @llvm.umax.nxv4i32(<vscale x 4 x i32> poison, <vscale x 4 x i32> poison)
23-
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
21+
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[V_SCALAR]], i64 0
2422
; CHECK-NEXT: ret <vscale x 4 x i32> [[V]]
2523
;
2624
%x.insert = insertelement <vscale x 4 x i32> poison, i32 %x, i32 0
@@ -33,8 +31,7 @@ define <4 x i32> @umax_fixed_lhs_const(i32 %x) {
3331
; CHECK-LABEL: define <4 x i32> @umax_fixed_lhs_const(
3432
; CHECK-SAME: i32 [[X:%.*]]) {
3533
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 1, i32 [[X]])
36-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x i32> @llvm.umax.v4i32(<4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32> poison)
37-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
34+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> poison, i32 [[V_SCALAR]], i64 0
3835
; CHECK-NEXT: ret <4 x i32> [[V]]
3936
;
4037
%x.insert = insertelement <4 x i32> poison, i32 %x, i32 0
@@ -46,8 +43,7 @@ define <4 x i32> @umax_fixed_rhs_const(i32 %x) {
4643
; CHECK-LABEL: define <4 x i32> @umax_fixed_rhs_const(
4744
; CHECK-SAME: i32 [[X:%.*]]) {
4845
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 1)
49-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x i32> @llvm.umax.v4i32(<4 x i32> poison, <4 x i32> <i32 1, i32 2, i32 3, i32 4>)
50-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
46+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> poison, i32 [[V_SCALAR]], i64 0
5147
; CHECK-NEXT: ret <4 x i32> [[V]]
5248
;
5349
%x.insert = insertelement <4 x i32> poison, i32 %x, i32 0
@@ -59,8 +55,7 @@ define <vscale x 4 x i32> @umax_scalable_lhs_const(i32 %x) {
5955
; CHECK-LABEL: define <vscale x 4 x i32> @umax_scalable_lhs_const(
6056
; CHECK-SAME: i32 [[X:%.*]]) {
6157
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 42, i32 [[X]])
62-
; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 4 x i32> @llvm.umax.nxv4i32(<vscale x 4 x i32> splat (i32 42), <vscale x 4 x i32> poison)
63-
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
58+
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[V_SCALAR]], i64 0
6459
; CHECK-NEXT: ret <vscale x 4 x i32> [[V]]
6560
;
6661
%x.insert = insertelement <vscale x 4 x i32> poison, i32 %x, i32 0
@@ -72,8 +67,7 @@ define <vscale x 4 x i32> @umax_scalable_rhs_const(i32 %x) {
7267
; CHECK-LABEL: define <vscale x 4 x i32> @umax_scalable_rhs_const(
7368
; CHECK-SAME: i32 [[X:%.*]]) {
7469
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 42)
75-
; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 4 x i32> @llvm.umax.nxv4i32(<vscale x 4 x i32> poison, <vscale x 4 x i32> splat (i32 42))
76-
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
70+
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[V_SCALAR]], i64 0
7771
; CHECK-NEXT: ret <vscale x 4 x i32> [[V]]
7872
;
7973
%x.insert = insertelement <vscale x 4 x i32> poison, i32 %x, i32 0
@@ -100,8 +94,7 @@ define <4 x float> @fabs_fixed(float %x) {
10094
; CHECK-LABEL: define <4 x float> @fabs_fixed(
10195
; CHECK-SAME: float [[X:%.*]]) {
10296
; CHECK-NEXT: [[V_SCALAR:%.*]] = call float @llvm.fabs.f32(float [[X]])
103-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x float> @llvm.fabs.v4f32(<4 x float> poison)
104-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> [[TMP1]], float [[V_SCALAR]], i64 0
97+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> poison, float [[V_SCALAR]], i64 0
10598
; CHECK-NEXT: ret <4 x float> [[V]]
10699
;
107100
%x.insert = insertelement <4 x float> poison, float %x, i32 0
@@ -113,8 +106,7 @@ define <vscale x 4 x float> @fabs_scalable(float %x) {
113106
; CHECK-LABEL: define <vscale x 4 x float> @fabs_scalable(
114107
; CHECK-SAME: float [[X:%.*]]) {
115108
; CHECK-NEXT: [[V_SCALAR:%.*]] = call float @llvm.fabs.f32(float [[X]])
116-
; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 4 x float> @llvm.fabs.nxv4f32(<vscale x 4 x float> poison)
117-
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x float> [[TMP1]], float [[V_SCALAR]], i64 0
109+
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x float> poison, float [[V_SCALAR]], i64 0
118110
; CHECK-NEXT: ret <vscale x 4 x float> [[V]]
119111
;
120112
%x.insert = insertelement <vscale x 4 x float> poison, float %x, i32 0
@@ -126,8 +118,7 @@ define <4 x float> @fma_fixed(float %x, float %y, float %z) {
126118
; CHECK-LABEL: define <4 x float> @fma_fixed(
127119
; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]], float [[Z:%.*]]) {
128120
; CHECK-NEXT: [[V_SCALAR:%.*]] = call float @llvm.fma.f32(float [[X]], float [[Y]], float [[Z]])
129-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x float> @llvm.fma.v4f32(<4 x float> poison, <4 x float> poison, <4 x float> poison)
130-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> [[TMP1]], float [[V_SCALAR]], i64 0
121+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> poison, float [[V_SCALAR]], i64 0
131122
; CHECK-NEXT: ret <4 x float> [[V]]
132123
;
133124
%x.insert = insertelement <4 x float> poison, float %x, i32 0
@@ -141,8 +132,7 @@ define <vscale x 4 x float> @fma_scalable(float %x, float %y, float %z) {
141132
; CHECK-LABEL: define <vscale x 4 x float> @fma_scalable(
142133
; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]], float [[Z:%.*]]) {
143134
; CHECK-NEXT: [[V_SCALAR:%.*]] = call float @llvm.fma.f32(float [[X]], float [[Y]], float [[Z]])
144-
; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 4 x float> @llvm.fma.nxv4f32(<vscale x 4 x float> poison, <vscale x 4 x float> poison, <vscale x 4 x float> poison)
145-
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x float> [[TMP1]], float [[V_SCALAR]], i64 0
135+
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x float> poison, float [[V_SCALAR]], i64 0
146136
; CHECK-NEXT: ret <vscale x 4 x float> [[V]]
147137
;
148138
%x.insert = insertelement <vscale x 4 x float> poison, float %x, i32 0
@@ -156,8 +146,7 @@ define <4 x float> @scalar_argument(float %x) {
156146
; CHECK-LABEL: define <4 x float> @scalar_argument(
157147
; CHECK-SAME: float [[X:%.*]]) {
158148
; CHECK-NEXT: [[V_SCALAR:%.*]] = call float @llvm.powi.f32.i32(float [[X]], i32 42)
159-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x float> @llvm.powi.v4f32.i32(<4 x float> poison, i32 42)
160-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> [[TMP1]], float [[V_SCALAR]], i64 0
149+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> poison, float [[V_SCALAR]], i64 0
161150
; CHECK-NEXT: ret <4 x float> [[V]]
162151
;
163152
%x.insert = insertelement <4 x float> poison, float %x, i32 0

0 commit comments

Comments
 (0)