@@ -3351,6 +3351,129 @@ for.end: ; preds = %for.body, %entry
3351
3351
ret i32 %x.0.lcssa
3352
3352
}
3353
3353
3354
+ ; Test that bundling recipes that share an operand into an expression works.
3355
+ ; In this case the two extends are the recipes that share an operand.
3356
+ define i64 @reduction_expression_same_operands (ptr nocapture readonly %x , ptr nocapture readonly %y , i32 %n ) {
3357
+ ; CHECK-LABEL: define i64 @reduction_expression_same_operands(
3358
+ ; CHECK-SAME: ptr readonly captures(none) [[X:%.*]], ptr readonly captures(none) [[Y:%.*]], i32 [[N:%.*]]) {
3359
+ ; CHECK-NEXT: [[ENTRY:.*]]:
3360
+ ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[N]], 4
3361
+ ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
3362
+ ; CHECK: [[VECTOR_PH]]:
3363
+ ; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[N]], 4
3364
+ ; CHECK-NEXT: [[N_VEC:%.*]] = sub i32 [[N]], [[N_MOD_VF]]
3365
+ ; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
3366
+ ; CHECK: [[VECTOR_BODY]]:
3367
+ ; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
3368
+ ; CHECK-NEXT: [[VEC_PHI:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[TMP6:%.*]], %[[VECTOR_BODY]] ]
3369
+ ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i16, ptr [[X]], i32 [[INDEX]]
3370
+ ; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i16>, ptr [[TMP1]], align 4
3371
+ ; CHECK-NEXT: [[TMP3:%.*]] = sext <4 x i16> [[WIDE_LOAD]] to <4 x i64>
3372
+ ; CHECK-NEXT: [[TMP4:%.*]] = mul nsw <4 x i64> [[TMP3]], [[TMP3]]
3373
+ ; CHECK-NEXT: [[TMP5:%.*]] = call i64 @llvm.vector.reduce.add.v4i64(<4 x i64> [[TMP4]])
3374
+ ; CHECK-NEXT: [[TMP6]] = add i64 [[VEC_PHI]], [[TMP5]]
3375
+ ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
3376
+ ; CHECK-NEXT: [[TMP7:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
3377
+ ; CHECK-NEXT: br i1 [[TMP7]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP33:![0-9]+]]
3378
+ ; CHECK: [[MIDDLE_BLOCK]]:
3379
+ ; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[N]], [[N_VEC]]
3380
+ ; CHECK-NEXT: br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
3381
+ ; CHECK: [[SCALAR_PH]]:
3382
+ ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
3383
+ ; CHECK-NEXT: [[BC_MERGE_RDX:%.*]] = phi i64 [ [[TMP6]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
3384
+ ; CHECK-NEXT: br label %[[LOOP:.*]]
3385
+ ; CHECK: [[LOOP]]:
3386
+ ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_NEXT:%.*]], %[[LOOP]] ], [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ]
3387
+ ; CHECK-NEXT: [[RDX:%.*]] = phi i64 [ [[RDX_NEXT:%.*]], %[[LOOP]] ], [ [[BC_MERGE_RDX]], %[[SCALAR_PH]] ]
3388
+ ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i16, ptr [[X]], i32 [[IV]]
3389
+ ; CHECK-NEXT: [[LOAD0:%.*]] = load i16, ptr [[ARRAYIDX]], align 4
3390
+ ; CHECK-NEXT: [[CONV0:%.*]] = sext i16 [[LOAD0]] to i32
3391
+ ; CHECK-NEXT: [[CONV1:%.*]] = sext i16 [[LOAD0]] to i32
3392
+ ; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i32 [[CONV0]], [[CONV1]]
3393
+ ; CHECK-NEXT: [[MUL:%.*]] = sext i32 [[MUL1]] to i64
3394
+ ; CHECK-NEXT: [[RDX_NEXT]] = add nsw i64 [[RDX]], [[MUL]]
3395
+ ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
3396
+ ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[IV_NEXT]], [[N]]
3397
+ ; CHECK-NEXT: br i1 [[EXITCOND]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP34:![0-9]+]]
3398
+ ; CHECK: [[EXIT]]:
3399
+ ; CHECK-NEXT: [[R_0_LCSSA:%.*]] = phi i64 [ [[RDX_NEXT]], %[[LOOP]] ], [ [[TMP6]], %[[MIDDLE_BLOCK]] ]
3400
+ ; CHECK-NEXT: ret i64 [[R_0_LCSSA]]
3401
+ ;
3402
+ ; CHECK-INTERLEAVED-LABEL: define i64 @reduction_expression_same_operands(
3403
+ ; CHECK-INTERLEAVED-SAME: ptr readonly captures(none) [[X:%.*]], ptr readonly captures(none) [[Y:%.*]], i32 [[N:%.*]]) {
3404
+ ; CHECK-INTERLEAVED-NEXT: [[ENTRY:.*]]:
3405
+ ; CHECK-INTERLEAVED-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[N]], 8
3406
+ ; CHECK-INTERLEAVED-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
3407
+ ; CHECK-INTERLEAVED: [[VECTOR_PH]]:
3408
+ ; CHECK-INTERLEAVED-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[N]], 8
3409
+ ; CHECK-INTERLEAVED-NEXT: [[N_VEC:%.*]] = sub i32 [[N]], [[N_MOD_VF]]
3410
+ ; CHECK-INTERLEAVED-NEXT: br label %[[VECTOR_BODY:.*]]
3411
+ ; CHECK-INTERLEAVED: [[VECTOR_BODY]]:
3412
+ ; CHECK-INTERLEAVED-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
3413
+ ; CHECK-INTERLEAVED-NEXT: [[VEC_PHI:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[TMP7:%.*]], %[[VECTOR_BODY]] ]
3414
+ ; CHECK-INTERLEAVED-NEXT: [[VEC_PHI1:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[TMP12:%.*]], %[[VECTOR_BODY]] ]
3415
+ ; CHECK-INTERLEAVED-NEXT: [[TMP1:%.*]] = getelementptr inbounds i16, ptr [[X]], i32 [[INDEX]]
3416
+ ; CHECK-INTERLEAVED-NEXT: [[TMP2:%.*]] = getelementptr inbounds i16, ptr [[TMP1]], i32 4
3417
+ ; CHECK-INTERLEAVED-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i16>, ptr [[TMP1]], align 4
3418
+ ; CHECK-INTERLEAVED-NEXT: [[WIDE_LOAD2:%.*]] = load <4 x i16>, ptr [[TMP2]], align 4
3419
+ ; CHECK-INTERLEAVED-NEXT: [[TMP4:%.*]] = sext <4 x i16> [[WIDE_LOAD]] to <4 x i64>
3420
+ ; CHECK-INTERLEAVED-NEXT: [[TMP5:%.*]] = mul nsw <4 x i64> [[TMP4]], [[TMP4]]
3421
+ ; CHECK-INTERLEAVED-NEXT: [[TMP6:%.*]] = call i64 @llvm.vector.reduce.add.v4i64(<4 x i64> [[TMP5]])
3422
+ ; CHECK-INTERLEAVED-NEXT: [[TMP7]] = add i64 [[VEC_PHI]], [[TMP6]]
3423
+ ; CHECK-INTERLEAVED-NEXT: [[TMP9:%.*]] = sext <4 x i16> [[WIDE_LOAD2]] to <4 x i64>
3424
+ ; CHECK-INTERLEAVED-NEXT: [[TMP10:%.*]] = mul nsw <4 x i64> [[TMP9]], [[TMP9]]
3425
+ ; CHECK-INTERLEAVED-NEXT: [[TMP11:%.*]] = call i64 @llvm.vector.reduce.add.v4i64(<4 x i64> [[TMP10]])
3426
+ ; CHECK-INTERLEAVED-NEXT: [[TMP12]] = add i64 [[VEC_PHI1]], [[TMP11]]
3427
+ ; CHECK-INTERLEAVED-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 8
3428
+ ; CHECK-INTERLEAVED-NEXT: [[TMP13:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
3429
+ ; CHECK-INTERLEAVED-NEXT: br i1 [[TMP13]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP33:![0-9]+]]
3430
+ ; CHECK-INTERLEAVED: [[MIDDLE_BLOCK]]:
3431
+ ; CHECK-INTERLEAVED-NEXT: [[BIN_RDX:%.*]] = add i64 [[TMP12]], [[TMP7]]
3432
+ ; CHECK-INTERLEAVED-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[N]], [[N_VEC]]
3433
+ ; CHECK-INTERLEAVED-NEXT: br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
3434
+ ; CHECK-INTERLEAVED: [[SCALAR_PH]]:
3435
+ ; CHECK-INTERLEAVED-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
3436
+ ; CHECK-INTERLEAVED-NEXT: [[BC_MERGE_RDX:%.*]] = phi i64 [ [[BIN_RDX]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
3437
+ ; CHECK-INTERLEAVED-NEXT: br label %[[LOOP:.*]]
3438
+ ; CHECK-INTERLEAVED: [[LOOP]]:
3439
+ ; CHECK-INTERLEAVED-NEXT: [[IV:%.*]] = phi i32 [ [[IV_NEXT:%.*]], %[[LOOP]] ], [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ]
3440
+ ; CHECK-INTERLEAVED-NEXT: [[RDX:%.*]] = phi i64 [ [[RDX_NEXT:%.*]], %[[LOOP]] ], [ [[BC_MERGE_RDX]], %[[SCALAR_PH]] ]
3441
+ ; CHECK-INTERLEAVED-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i16, ptr [[X]], i32 [[IV]]
3442
+ ; CHECK-INTERLEAVED-NEXT: [[LOAD0:%.*]] = load i16, ptr [[ARRAYIDX]], align 4
3443
+ ; CHECK-INTERLEAVED-NEXT: [[CONV0:%.*]] = sext i16 [[LOAD0]] to i32
3444
+ ; CHECK-INTERLEAVED-NEXT: [[CONV1:%.*]] = sext i16 [[LOAD0]] to i32
3445
+ ; CHECK-INTERLEAVED-NEXT: [[MUL1:%.*]] = mul nsw i32 [[CONV0]], [[CONV1]]
3446
+ ; CHECK-INTERLEAVED-NEXT: [[MUL:%.*]] = sext i32 [[MUL1]] to i64
3447
+ ; CHECK-INTERLEAVED-NEXT: [[RDX_NEXT]] = add nsw i64 [[RDX]], [[MUL]]
3448
+ ; CHECK-INTERLEAVED-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
3449
+ ; CHECK-INTERLEAVED-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[IV_NEXT]], [[N]]
3450
+ ; CHECK-INTERLEAVED-NEXT: br i1 [[EXITCOND]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP34:![0-9]+]]
3451
+ ; CHECK-INTERLEAVED: [[EXIT]]:
3452
+ ; CHECK-INTERLEAVED-NEXT: [[R_0_LCSSA:%.*]] = phi i64 [ [[RDX_NEXT]], %[[LOOP]] ], [ [[BIN_RDX]], %[[MIDDLE_BLOCK]] ]
3453
+ ; CHECK-INTERLEAVED-NEXT: ret i64 [[R_0_LCSSA]]
3454
+ ;
3455
+ entry:
3456
+ br label %loop
3457
+
3458
+ loop:
3459
+ %iv = phi i32 [ %iv.next , %loop ], [ 0 , %entry ]
3460
+ %rdx = phi i64 [ %rdx.next , %loop ], [ 0 , %entry ]
3461
+ %arrayidx = getelementptr inbounds i16 , ptr %x , i32 %iv
3462
+ %load0 = load i16 , ptr %arrayidx , align 4
3463
+ %conv0 = sext i16 %load0 to i32
3464
+ %conv1 = sext i16 %load0 to i32
3465
+ %mul = mul nsw i32 %conv0 , %conv1
3466
+ %conv = sext i32 %mul to i64
3467
+ %rdx.next = add nsw i64 %rdx , %conv
3468
+ %iv.next = add nuw nsw i32 %iv , 1
3469
+ %exitcond = icmp eq i32 %iv.next , %n
3470
+ br i1 %exitcond , label %exit , label %loop
3471
+
3472
+ exit:
3473
+ %r.0.lcssa = phi i64 [ %rdx.next , %loop ]
3474
+ ret i64 %r.0.lcssa
3475
+ }
3476
+
3354
3477
declare float @llvm.fmuladd.f32 (float , float , float )
3355
3478
3356
3479
!6 = distinct !{!6 , !7 , !8 }
0 commit comments