1 ; RUN: opt < %s -mattr=+sve2 -passes=loop-vectorize,instcombine -enable-histogram-loop-vectorization -sve-gather-overhead=2 -sve-scatter-overhead=2 -force-vector-interleave=1 -debug-only=loop-vectorize -S 2>&1 | FileCheck %s
4 target triple = "aarch64-unknown-linux-gnu"
6 ;; Based on the following C code:
8 ;; void simple_histogram(int *buckets, unsigned *indices, int N) {
9 ;; for (int i = 0; i < N; ++i)
10 ;; buckets[indices[i]]++;
13 ;; Check that the scalar plan contains the original instructions.
14 ; CHECK: VPlan 'Initial VPlan for VF={1},UF>=1' {
15 ; CHECK-NEXT: Live-in [[VFxUF:.*]] = VF * UF
16 ; CHECK-NEXT: Live-in [[VTC:.*]] = vector-trip-count
17 ; CHECK-NEXT: Live-in [[OTC:.*]] = original trip-count
19 ; CHECK-NEXT: vector.ph:
20 ; CHECK-NEXT: Successor(s): vector loop
22 ; CHECK-NEXT: <x1> vector loop: {
23 ; CHECK-NEXT: vector.body:
24 ; CHECK-NEXT: EMIT [[IV:.*]] = CANONICAL-INDUCTION ir<0>, [[IV_NEXT:.*]]
25 ; CHECK-NEXT: [[STEPS:vp.*]] = SCALAR-STEPS [[IV]], ir<1>
26 ; CHECK-NEXT: CLONE [[GEP_IDX:.*]] = getelementptr inbounds ir<%indices>, [[STEPS]]
27 ; CHECK-NEXT: CLONE [[IDX:.*]] = load [[GEP_IDX]]
28 ; CHECK-NEXT: CLONE [[EXT_IDX:.*]] = zext [[IDX]]
29 ; CHECK-NEXT: CLONE [[GEP_BUCKET:.*]] = getelementptr inbounds ir<%buckets>, [[EXT_IDX]]
30 ; CHECK-NEXT: CLONE [[HISTVAL:.*]] = load [[GEP_BUCKET]]
31 ; CHECK-NEXT: CLONE [[UPDATE:.*]] = add nsw [[HISTVAL]], ir<1>
32 ; CHECK-NEXT: CLONE store [[UPDATE]], [[GEP_BUCKET]]
33 ; CHECK-NEXT: EMIT [[IV_NEXT]] = add nuw [[IV]], [[VFxUF]]
34 ; CHECK-NEXT: EMIT branch-on-count [[IV_NEXT]], [[VTC]]
35 ; CHECK-NEXT: No successors
37 ; CHECK-NEXT: Successor(s): middle.block
39 ; CHECK-NEXT: middle.block:
40 ; CHECK-NEXT: EMIT [[TC_CHECK:.*]] = icmp eq [[OTC:.*]], [[VTC]]
41 ; CHECK-NEXT: EMIT branch-on-cond [[TC_CHECK]]
42 ; CHECK-NEXT: Successor(s): ir-bb<for.exit>, scalar.ph
44 ; CHECK-NEXT: scalar.ph:
45 ; CHECK-NEXT: Successor(s): ir-bb<for.body>
47 ; CHECK-NEXT: ir-bb<for.body>:
48 ; CHECK-NEXT: IR %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
49 ; CHECK: IR %exitcond = icmp eq i64 %iv.next, %N
50 ; CHECK-NEXT: No successors
52 ; CHECK-NEXT: ir-bb<for.exit>:
53 ; CHECK-NEXT: No successors
56 ;; Check that the vectorized plan contains a histogram recipe instead.
57 ; CHECK: VPlan 'Initial VPlan for VF={vscale x 2,vscale x 4},UF>=1' {
58 ; CHECK-NEXT: Live-in [[VFxUF:.*]] = VF * UF
59 ; CHECK-NEXT: Live-in [[VTC:.*]] = vector-trip-count
60 ; CHECK-NEXT: Live-in [[OTC:.*]] = original trip-count
62 ; CHECK-NEXT: vector.ph:
63 ; CHECK-NEXT: Successor(s): vector loop
65 ; CHECK-NEXT: <x1> vector loop: {
66 ; CHECK-NEXT: vector.body:
67 ; CHECK-NEXT: EMIT [[IV:.*]] = CANONICAL-INDUCTION ir<0>, [[IV_NEXT:.*]]
68 ; CHECK-NEXT: [[STEPS:vp.*]] = SCALAR-STEPS [[IV]], ir<1>
69 ; CHECK-NEXT: CLONE [[GEP_IDX:.*]] = getelementptr inbounds ir<%indices>, [[STEPS]]
70 ; CHECK-NEXT: [[VECP_IDX:vp.*]] = vector-pointer [[GEP_IDX]]
71 ; CHECK-NEXT: WIDEN [[IDX:.*]] = load [[VECP_IDX]]
72 ; CHECK-NEXT: WIDEN-CAST [[EXT_IDX:.*]] = zext [[IDX]] to i64
73 ; CHECK-NEXT: WIDEN-GEP Inv[Var] [[GEP_BUCKET:.*]] = getelementptr inbounds ir<%buckets>, [[EXT_IDX]]
74 ; CHECK-NEXT: WIDEN-HISTOGRAM buckets: [[GEP_BUCKET]], inc: ir<1>
75 ; CHECK-NEXT: EMIT [[IV_NEXT]] = add nuw [[IV]], [[VFxUF]]
76 ; CHECK-NEXT: EMIT branch-on-count [[IV_NEXT]], [[VTC]]
77 ; CHECK-NEXT: No successors
79 ; CHECK-NEXT: Successor(s): middle.block
81 ; CHECK-NEXT: middle.block:
82 ; CHECK-NEXT: EMIT [[TC_CHECK:.*]] = icmp eq [[OTC]], [[VTC]]
83 ; CHECK-NEXT: EMIT branch-on-cond [[TC_CHECK]]
84 ; CHECK-NEXT: Successor(s): ir-bb<for.exit>, scalar.ph
86 ; CHECK-NEXT: scalar.ph:
87 ; CHECK-NEXT: Successor(s): ir-bb<for.body>
89 ; CHECK-NEXT: ir-bb<for.body>:
90 ; CHECK-NEXT: IR %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
91 ; CHECK: IR %exitcond = icmp eq i64 %iv.next, %N
92 ; CHECK-NEXT: No successors
94 ; CHECK-NEXT: ir-bb<for.exit>:
95 ; CHECK-NEXT: No successors
98 define void @simple_histogram(ptr noalias %buckets, ptr readonly %indices, i64 %N) {
103 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
104 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv
105 %l.idx = load i32, ptr %gep.indices, align 4
106 %idxprom1 = zext i32 %l.idx to i64
107 %gep.bucket = getelementptr inbounds i32, ptr %buckets, i64 %idxprom1
108 %l.bucket = load i32, ptr %gep.bucket, align 4
109 %inc = add nsw i32 %l.bucket, 1
110 store i32 %inc, ptr %gep.bucket, align 4
111 %iv.next = add nuw nsw i64 %iv, 1
112 %exitcond = icmp eq i64 %iv.next, %N
113 br i1 %exitcond, label %for.exit, label %for.body