1; RUN: opt -loop-vectorize -scalable-vectorization=off -force-vector-width=4 -prefer-predicate-over-epilogue=predicate-dont-vectorize -S < %s | FileCheck %s
2
3; NOTE: These tests aren't really target-specific, but it's convenient to target AArch64
4; so that TTI.isLegalMaskedLoad can return true.
5
6target triple = "aarch64-linux-gnu"
7
8; The original loop had an unconditional uniform load. Let's make sure
9; we don't artificially create new predicated blocks for the load.
10define void @uniform_load(i32* noalias %dst, i32* noalias readonly %src, i64 %n) #0 {
11; CHECK-LABEL: @uniform_load(
12; CHECK:       vector.body:
13; CHECK-NEXT:    [[IDX:%.*]] = phi i64 [ 0, %vector.ph ], [ [[IDX_NEXT:%.*]], %vector.body ]
14; CHECK-NEXT:    [[TMP3:%.*]] = add i64 [[IDX]], 0
15; CHECK-NEXT:    [[LOOP_PRED:%.*]] = call <4 x i1> @llvm.get.active.lane.mask.v4i1.i64(i64 [[TMP3]], i64 %n)
16; CHECK-NEXT:    [[LOAD_VAL:%.*]] = load i32, i32* %src, align 4
17; CHECK-NOT:     load i32, i32* %src, align 4
18; CHECK-NEXT:    [[TMP4:%.*]] = insertelement <4 x i32> poison, i32 [[LOAD_VAL]], i32 0
19; CHECK-NEXT:    [[TMP5:%.*]] = shufflevector <4 x i32> [[TMP4]], <4 x i32> poison, <4 x i32> zeroinitializer
20; CHECK-NEXT:    [[TMP6:%.*]] = getelementptr inbounds i32, i32* %dst, i64 [[TMP3]]
21; CHECK-NEXT:    [[TMP7:%.*]] = getelementptr inbounds i32, i32* [[TMP6]], i32 0
22; CHECK-NEXT:    [[STORE_PTR:%.*]] = bitcast i32* [[TMP7]] to <4 x i32>*
23; CHECK-NEXT:    call void @llvm.masked.store.v4i32.p0v4i32(<4 x i32> [[TMP5]], <4 x i32>* [[STORE_PTR]], i32 4, <4 x i1> [[LOOP_PRED]])
24; CHECK-NEXT:    [[IDX_NEXT]] = add i64 [[IDX]], 4
25; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[IDX_NEXT]], %n.vec
26; CHECK-NEXT:    br i1 [[CMP]], label %middle.block, label %vector.body
27
28entry:
29  br label %for.body
30
31for.body:                                         ; preds = %entry, %for.body
32  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
33  %val = load i32, i32* %src, align 4
34  %arrayidx = getelementptr inbounds i32, i32* %dst, i64 %indvars.iv
35  store i32 %val, i32* %arrayidx, align 4
36  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
37  %exitcond.not = icmp eq i64 %indvars.iv.next, %n
38  br i1 %exitcond.not, label %for.end, label %for.body
39
40for.end:                                          ; preds = %for.body, %entry
41  ret void
42}
43
44; The original loop had a conditional uniform load. In this case we actually
45; do need to perform conditional loads and so we end up using a gather instead.
46; However, we at least ensure the mask is the overlap of the loop predicate
47; and the original condition.
48define void @cond_uniform_load(i32* nocapture %dst, i32* nocapture readonly %src, i32* nocapture readonly %cond, i64 %n) #0 {
49; CHECK-LABEL: @cond_uniform_load(
50; CHECK:       vector.ph:
51; CHECK:         [[TMP1:%.*]] = insertelement <4 x i32*> poison, i32* %src, i32 0
52; CHECK-NEXT:    [[SRC_SPLAT:%.*]] = shufflevector <4 x i32*> [[TMP1]], <4 x i32*> poison, <4 x i32> zeroinitializer
53; CHECK:       vector.body:
54; CHECK-NEXT:    [[IDX:%.*]] = phi i64 [ 0, %vector.ph ], [ [[IDX_NEXT:%.*]], %vector.body ]
55; CHECK-NEXT:    [[TMP3:%.*]] = add i64 [[IDX]], 0
56; CHECK-NEXT:    [[LOOP_PRED:%.*]] = call <4 x i1> @llvm.get.active.lane.mask.v4i1.i64(i64 [[TMP3]], i64 %n)
57; CHECK:         [[COND_LOAD:%.*]] = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* {{%.*}}, i32 4, <4 x i1> [[LOOP_PRED]], <4 x i32> poison)
58; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq <4 x i32> [[COND_LOAD]], zeroinitializer
59; CHECK-NEXT:    [[TMP5:%.*]] = xor <4 x i1> [[TMP4]], <i1 true, i1 true, i1 true, i1 true>
60; CHECK-NEXT:    [[MASK:%.*]] = select <4 x i1> [[LOOP_PRED]], <4 x i1> [[TMP5]], <4 x i1> zeroinitializer
61; CHECK-NEXT:    call <4 x i32> @llvm.masked.gather.v4i32.v4p0i32(<4 x i32*> [[SRC_SPLAT]], i32 4, <4 x i1> [[MASK]], <4 x i32> undef)
62entry:
63  br label %for.body
64
65for.body:                                         ; preds = %entry, %if.end
66  %index = phi i64 [ %index.next, %if.end ], [ 0, %entry ]
67  %arrayidx = getelementptr inbounds i32, i32* %cond, i64 %index
68  %0 = load i32, i32* %arrayidx, align 4
69  %tobool.not = icmp eq i32 %0, 0
70  br i1 %tobool.not, label %if.end, label %if.then
71
72if.then:                                          ; preds = %for.body
73  %1 = load i32, i32* %src, align 4
74  br label %if.end
75
76if.end:                                           ; preds = %if.then, %for.body
77  %val.0 = phi i32 [ %1, %if.then ], [ 0, %for.body ]
78  %arrayidx1 = getelementptr inbounds i32, i32* %dst, i64 %index
79  store i32 %val.0, i32* %arrayidx1, align 4
80  %index.next = add nuw i64 %index, 1
81  %exitcond.not = icmp eq i64 %index.next, %n
82  br i1 %exitcond.not, label %for.end, label %for.body
83
84for.end:                                          ; preds = %for.inc, %entry
85  ret void
86}
87
88attributes #0 = { "target-features"="+neon,+sve,+v8.1a" vscale_range(2, 0) }
89