1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -basic-aa -slp-vectorizer -dce -S -mtriple=i386-apple-macosx10.8.0 -mcpu=corei7-avx | FileCheck %s
3
4target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32-S128"
5target triple = "i386-apple-macosx10.8.0"
6
7; int foo(double *A, int n, int m) {
8;   double sum = 0, v1 = 2, v0 = 3;
9;   for (int i=0; i < n; ++i)
10;     sum += 7*A[i*2] + 7*A[i*2+1];
11;   return sum;
12; }
13
14define i32 @reduce(double* nocapture %A, i32 %n, i32 %m) {
15; CHECK-LABEL: @reduce(
16; CHECK-NEXT:  entry:
17; CHECK-NEXT:    [[CMP13:%.*]] = icmp sgt i32 [[N:%.*]], 0
18; CHECK-NEXT:    br i1 [[CMP13]], label [[FOR_BODY:%.*]], label [[FOR_END:%.*]]
19; CHECK:       for.body:
20; CHECK-NEXT:    [[I_015:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY:%.*]] ]
21; CHECK-NEXT:    [[SUM_014:%.*]] = phi double [ [[ADD6:%.*]], [[FOR_BODY]] ], [ 0.000000e+00, [[ENTRY]] ]
22; CHECK-NEXT:    [[MUL:%.*]] = shl nsw i32 [[I_015]], 1
23; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds double, double* [[A:%.*]], i32 [[MUL]]
24; CHECK-NEXT:    [[TMP0:%.*]] = bitcast double* [[ARRAYIDX]] to <2 x double>*
25; CHECK-NEXT:    [[TMP1:%.*]] = load <2 x double>, <2 x double>* [[TMP0]], align 4
26; CHECK-NEXT:    [[TMP2:%.*]] = fmul <2 x double> [[TMP1]], <double 7.000000e+00, double 7.000000e+00>
27; CHECK-NEXT:    [[TMP3:%.*]] = extractelement <2 x double> [[TMP2]], i32 0
28; CHECK-NEXT:    [[TMP4:%.*]] = extractelement <2 x double> [[TMP2]], i32 1
29; CHECK-NEXT:    [[ADD5:%.*]] = fadd double [[TMP3]], [[TMP4]]
30; CHECK-NEXT:    [[ADD6]] = fadd double [[SUM_014]], [[ADD5]]
31; CHECK-NEXT:    [[INC]] = add nsw i32 [[I_015]], 1
32; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp eq i32 [[INC]], [[N]]
33; CHECK-NEXT:    br i1 [[EXITCOND]], label [[FOR_COND_FOR_END_CRIT_EDGE:%.*]], label [[FOR_BODY]]
34; CHECK:       for.cond.for.end_crit_edge:
35; CHECK-NEXT:    [[PHITMP:%.*]] = fptosi double [[ADD6]] to i32
36; CHECK-NEXT:    br label [[FOR_END]]
37; CHECK:       for.end:
38; CHECK-NEXT:    [[SUM_0_LCSSA:%.*]] = phi i32 [ [[PHITMP]], [[FOR_COND_FOR_END_CRIT_EDGE]] ], [ 0, [[ENTRY]] ]
39; CHECK-NEXT:    ret i32 [[SUM_0_LCSSA]]
40;
41entry:
42  %cmp13 = icmp sgt i32 %n, 0
43  br i1 %cmp13, label %for.body, label %for.end
44
45for.body:                                         ; preds = %entry, %for.body
46  %i.015 = phi i32 [ %inc, %for.body ], [ 0, %entry ]
47  %sum.014 = phi double [ %add6, %for.body ], [ 0.000000e+00, %entry ]
48  %mul = shl nsw i32 %i.015, 1
49  %arrayidx = getelementptr inbounds double, double* %A, i32 %mul
50  %0 = load double, double* %arrayidx, align 4
51  %mul1 = fmul double %0, 7.000000e+00
52  %add12 = or i32 %mul, 1
53  %arrayidx3 = getelementptr inbounds double, double* %A, i32 %add12
54  %1 = load double, double* %arrayidx3, align 4
55  %mul4 = fmul double %1, 7.000000e+00
56  %add5 = fadd double %mul1, %mul4
57  %add6 = fadd double %sum.014, %add5
58  %inc = add nsw i32 %i.015, 1
59  %exitcond = icmp eq i32 %inc, %n
60  br i1 %exitcond, label %for.cond.for.end_crit_edge, label %for.body
61
62for.cond.for.end_crit_edge:                       ; preds = %for.body
63  %phitmp = fptosi double %add6 to i32
64  br label %for.end
65
66for.end:                                          ; preds = %for.cond.for.end_crit_edge, %entry
67  %sum.0.lcssa = phi i32 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
68  ret i32 %sum.0.lcssa
69}
70
71; PR43948 - https://bugs.llvm.org/show_bug.cgi?id=43948
72; The extra use of a non-vectorized element of a reduction must not be killed.
73
74define i32 @horiz_max_multiple_uses([32 x i32]* %x, i32* %p) {
75; CHECK-LABEL: @horiz_max_multiple_uses(
76; CHECK-NEXT:    [[X0:%.*]] = getelementptr [32 x i32], [32 x i32]* [[X:%.*]], i64 0, i64 0
77; CHECK-NEXT:    [[X4:%.*]] = getelementptr [32 x i32], [32 x i32]* [[X]], i64 0, i64 4
78; CHECK-NEXT:    [[X5:%.*]] = getelementptr [32 x i32], [32 x i32]* [[X]], i64 0, i64 5
79; CHECK-NEXT:    [[TMP1:%.*]] = bitcast i32* [[X0]] to <4 x i32>*
80; CHECK-NEXT:    [[TMP2:%.*]] = load <4 x i32>, <4 x i32>* [[TMP1]], align 4
81; CHECK-NEXT:    [[T4:%.*]] = load i32, i32* [[X4]], align 4
82; CHECK-NEXT:    [[T5:%.*]] = load i32, i32* [[X5]], align 4
83; CHECK-NEXT:    [[TMP3:%.*]] = call i32 @llvm.vector.reduce.smax.v4i32(<4 x i32> [[TMP2]])
84; CHECK-NEXT:    [[MAX_ROOT_CMP:%.*]] = icmp sgt i32 [[TMP3]], [[T4]]
85; CHECK-NEXT:    [[MAX_ROOT_SEL:%.*]] = select i1 [[MAX_ROOT_CMP]], i32 [[TMP3]], i32 [[T4]]
86; CHECK-NEXT:    [[C012345:%.*]] = icmp sgt i32 [[MAX_ROOT_SEL]], [[T5]]
87; CHECK-NEXT:    [[T17:%.*]] = select i1 [[C012345]], i32 [[MAX_ROOT_SEL]], i32 [[T5]]
88; CHECK-NEXT:    [[THREE_OR_FOUR:%.*]] = select i1 [[MAX_ROOT_CMP]], i32 3, i32 4
89; CHECK-NEXT:    store i32 [[THREE_OR_FOUR]], i32* [[P:%.*]], align 8
90; CHECK-NEXT:    ret i32 [[T17]]
91;
92  %x0 = getelementptr [32 x i32], [32 x i32]* %x, i64 0, i64 0
93  %x1 = getelementptr [32 x i32], [32 x i32]* %x, i64 0, i64 1
94  %x2 = getelementptr [32 x i32], [32 x i32]* %x, i64 0, i64 2
95  %x3 = getelementptr [32 x i32], [32 x i32]* %x, i64 0, i64 3
96  %x4 = getelementptr [32 x i32], [32 x i32]* %x, i64 0, i64 4
97  %x5 = getelementptr [32 x i32], [32 x i32]* %x, i64 0, i64 5
98
99  %t0 = load i32, i32* %x0
100  %t1 = load i32, i32* %x1
101  %t2 = load i32, i32* %x2
102  %t3 = load i32, i32* %x3
103  %t4 = load i32, i32* %x4
104  %t5 = load i32, i32* %x5
105
106  %c01 = icmp sgt i32 %t0, %t1
107  %s5 = select i1 %c01, i32 %t0, i32 %t1
108  %c012 = icmp sgt i32 %s5, %t2
109  %t8 = select i1 %c012, i32 %s5, i32 %t2
110  %c0123 = icmp sgt i32 %t8, %t3
111  %rdx4 = select i1 %c0123, i32 %t8, i32 %t3
112  %MAX_ROOT_CMP = icmp sgt i32 %rdx4, %t4
113  %MAX_ROOT_SEL = select i1 %MAX_ROOT_CMP, i32 %rdx4, i32 %t4
114  %c012345 = icmp sgt i32 %MAX_ROOT_SEL, %t5
115  %t17 = select i1 %c012345, i32 %MAX_ROOT_SEL, i32 %t5
116  %three_or_four = select i1 %MAX_ROOT_CMP, i32 3, i32 4
117  store i32 %three_or_four, i32* %p, align 8
118  ret i32 %t17
119}
120
121; This is a miscompile (see the undef operand) and/or test for invalid IR.
122
123define i1 @bad_insertpoint_rdx([8 x i32]* %p) #0 {
124; CHECK-LABEL: @bad_insertpoint_rdx(
125; CHECK-NEXT:    [[ARRAYIDX22:%.*]] = getelementptr inbounds [8 x i32], [8 x i32]* [[P:%.*]], i64 0, i64 0
126; CHECK-NEXT:    [[T0:%.*]] = load i32, i32* [[ARRAYIDX22]], align 16
127; CHECK-NEXT:    [[CMP23:%.*]] = icmp sgt i32 [[T0]], 0
128; CHECK-NEXT:    [[SPEC_SELECT:%.*]] = select i1 [[CMP23]], i32 [[T0]], i32 0
129; CHECK-NEXT:    [[ARRAYIDX22_1:%.*]] = getelementptr inbounds [8 x i32], [8 x i32]* [[P]], i64 0, i64 1
130; CHECK-NEXT:    [[T1:%.*]] = load i32, i32* [[ARRAYIDX22_1]], align 4
131; CHECK-NEXT:    [[CMP23_1:%.*]] = icmp sgt i32 [[T1]], [[SPEC_SELECT]]
132; CHECK-NEXT:    [[SPEC_STORE_SELECT87:%.*]] = zext i1 [[CMP23_1]] to i32
133; CHECK-NEXT:    [[SPEC_SELECT88:%.*]] = select i1 [[CMP23_1]], i32 [[T1]], i32 [[SPEC_SELECT]]
134; CHECK-NEXT:    [[CMP23_2:%.*]] = icmp sgt i32 [[SPEC_STORE_SELECT87]], [[SPEC_SELECT88]]
135; CHECK-NEXT:    ret i1 [[CMP23_2]]
136;
137  %arrayidx22 = getelementptr inbounds [8 x i32], [8 x i32]* %p, i64 0, i64 0
138  %t0 = load i32, i32* %arrayidx22, align 16
139  %cmp23 = icmp sgt i32 %t0, 0
140  %spec.select = select i1 %cmp23, i32 %t0, i32 0
141  %arrayidx22.1 = getelementptr inbounds [8 x i32], [8 x i32]* %p, i64 0, i64 1
142  %t1 = load i32, i32* %arrayidx22.1, align 4
143  %cmp23.1 = icmp sgt i32 %t1, %spec.select
144  %spec.store.select87 = zext i1 %cmp23.1 to i32
145  %spec.select88 = select i1 %cmp23.1, i32 %t1, i32 %spec.select
146  %cmp23.2 = icmp sgt i32 %spec.store.select87, %spec.select88
147  ret i1 %cmp23.2
148}
149