1; REQUIRES: asserts
2; RUN: opt < %s -loop-vectorize -disable-output -debug-only=loop-vectorize 2>&1 | FileCheck %s --check-prefix=COST
3; RUN: opt < %s -loop-vectorize -force-vector-width=2 -instcombine -simplifycfg -S | FileCheck %s
4
5target datalayout = "e-m:e-i64:64-i128:128-n32:64-S128"
6target triple = "aarch64--linux-gnu"
7
8; This test checks that we correctly compute the scalarized operands for a
9; user-specified vectorization factor when interleaving is disabled. We use the
10; "optsize" attribute to disable all interleaving calculations.  A cost of 4
11; for %tmp4 indicates that we would scalarize it's operand (%tmp3), giving
12; %tmp4 a lower scalarization overhead.
13;
14; COST-LABEL:  predicated_udiv_scalarized_operand
15; COST:        LV: Found an estimated cost of 4 for VF 2 For instruction: %tmp4 = udiv i64 %tmp2, %tmp3
16;
17; CHECK-LABEL: @predicated_udiv_scalarized_operand(
18; CHECK:       vector.body:
19; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %entry ], [ [[INDEX_NEXT:%.*]], %[[PRED_UDIV_CONTINUE2:.*]] ]
20; CHECK-NEXT:    [[VEC_PHI:%.*]] = phi <2 x i64> [ zeroinitializer, %entry ], [ [[TMP17:%.*]], %[[PRED_UDIV_CONTINUE2]] ]
21; CHECK-NEXT:    [[TMP0:%.*]] = getelementptr inbounds i64, i64* %a, i64 [[INDEX]]
22; CHECK-NEXT:    [[TMP1:%.*]] = bitcast i64* [[TMP0]] to <2 x i64>*
23; CHECK-NEXT:    [[WIDE_LOAD:%.*]] = load <2 x i64>, <2 x i64>* [[TMP1]], align 4
24; CHECK-NEXT:    [[TMP2:%.*]] = icmp sgt <2 x i64> [[WIDE_LOAD]], zeroinitializer
25; CHECK-NEXT:    [[TMP3:%.*]] = extractelement <2 x i1> [[TMP2]], i32 0
26; CHECK-NEXT:    br i1 [[TMP3]], label %[[PRED_UDIV_IF:.*]], label %[[PRED_UDIV_CONTINUE:.*]]
27; CHECK:       [[PRED_UDIV_IF]]:
28; CHECK-NEXT:    [[TMP4:%.*]] = extractelement <2 x i64> [[WIDE_LOAD]], i32 0
29; CHECK-NEXT:    [[TMP5:%.*]] = add nsw i64 [[TMP4]], %x
30; CHECK-NEXT:    [[TMP6:%.*]] = extractelement <2 x i64> [[WIDE_LOAD]], i32 0
31; CHECK-NEXT:    [[TMP7:%.*]] = udiv i64 [[TMP6]], [[TMP5]]
32; CHECK-NEXT:    [[TMP8:%.*]] = insertelement <2 x i64> undef, i64 [[TMP7]], i32 0
33; CHECK-NEXT:    br label %[[PRED_UDIV_CONTINUE]]
34; CHECK:       [[PRED_UDIV_CONTINUE]]:
35; CHECK-NEXT:    [[TMP9:%.*]] = phi <2 x i64> [ undef, %vector.body ], [ [[TMP8]], %[[PRED_UDIV_IF]] ]
36; CHECK-NEXT:    [[TMP10:%.*]] = extractelement <2 x i1> [[TMP2]], i32 1
37; CHECK-NEXT:    br i1 [[TMP10]], label %[[PRED_UDIV_IF1:.*]], label %[[PRED_UDIV_CONTINUE2]]
38; CHECK:       [[PRED_UDIV_IF1]]:
39; CHECK-NEXT:    [[TMP11:%.*]] = extractelement <2 x i64> [[WIDE_LOAD]], i32 1
40; CHECK-NEXT:    [[TMP12:%.*]] = add nsw i64 [[TMP11]], %x
41; CHECK-NEXT:    [[TMP13:%.*]] = extractelement <2 x i64> [[WIDE_LOAD]], i32 1
42; CHECK-NEXT:    [[TMP14:%.*]] = udiv i64 [[TMP13]], [[TMP12]]
43; CHECK-NEXT:    [[TMP15:%.*]] = insertelement <2 x i64> [[TMP9]], i64 [[TMP14]], i32 1
44; CHECK-NEXT:    br label %[[PRED_UDIV_CONTINUE2]]
45; CHECK:       [[PRED_UDIV_CONTINUE2]]:
46; CHECK-NEXT:    [[TMP16:%.*]] = phi <2 x i64> [ [[TMP9]], %[[PRED_UDIV_CONTINUE]] ], [ [[TMP15]], %[[PRED_UDIV_IF1]] ]
47; CHECK-NEXT:    [[PREDPHI:%.*]] = select <2 x i1> [[TMP2]], <2 x i64> [[TMP16]], <2 x i64> [[WIDE_LOAD]]
48; CHECK-NEXT:    [[TMP17]] = add <2 x i64> [[VEC_PHI]], [[PREDPHI]]
49; CHECK-NEXT:    [[INDEX_NEXT]] = add i64 [[INDEX]], 2
50; CHECK:         br i1 {{.*}}, label %middle.block, label %vector.body
51;
52define i64 @predicated_udiv_scalarized_operand(i64* %a, i64 %x) optsize {
53entry:
54  br label %for.body
55
56for.body:
57  %i = phi i64 [ 0, %entry ], [ %i.next, %for.inc ]
58  %r = phi i64 [ 0, %entry ], [ %tmp6, %for.inc ]
59  %tmp0 = getelementptr inbounds i64, i64* %a, i64 %i
60  %tmp2 = load i64, i64* %tmp0, align 4
61  %cond0 = icmp sgt i64 %tmp2, 0
62  br i1 %cond0, label %if.then, label %for.inc
63
64if.then:
65  %tmp3 = add nsw i64 %tmp2, %x
66  %tmp4 = udiv i64 %tmp2, %tmp3
67  br label %for.inc
68
69for.inc:
70  %tmp5 = phi i64 [ %tmp2, %for.body ], [ %tmp4, %if.then]
71  %tmp6 = add i64 %r, %tmp5
72  %i.next = add nuw nsw i64 %i, 1
73  %cond1 = icmp slt i64 %i.next, 100
74  br i1 %cond1, label %for.body, label %for.end
75
76for.end:
77  %tmp7 = phi i64 [ %tmp6, %for.inc ]
78  ret i64 %tmp7
79}
80