1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -slp-vectorizer -slp-vectorize-hor -S -mtriple=x86_64-unknown-linux-gnu -mcpu=bdver2 -debug < %s 2>&1 | FileCheck %s
3; RUN: opt -slp-vectorizer -slp-vectorize-hor -S -mtriple=x86_64-unknown-linux-gnu -mcpu=core2 -debug < %s 2>&1 | FileCheck --check-prefix=SSE2 %s
4; REQUIRES: asserts
5
6; int test(unsigned int *p) {
7;   int sum = 0;
8;   for (int i = 0; i < 8; i++)
9;     sum += p[i];
10;   return sum;
11; }
12
13; Vector cost is 5, Scalar cost is 7
14; CHECK: Adding cost -2 for reduction that starts with   %7 = load i32, i32* %arrayidx.7, align 4 (It is a splitting reduction)
15; Vector cost is 11, Scalar cost is 7
16; SSE2:  Adding cost 4 for reduction that starts with   %7 = load i32, i32* %arrayidx.7, align 4 (It is a splitting reduction)
17define i32 @test(i32* nocapture readonly %p) {
18; CHECK-LABEL: @test(
19; CHECK:         [[BC:%.*]] = bitcast i32* %p to <8 x i32>*
20; CHECK-NEXT:    [[LD:%.*]] = load <8 x i32>, <8 x i32>* [[BC]], align 4
21; CHECK:         [[RDX_SHUF:%.*]] = shufflevector <8 x i32> [[LD]], <8 x i32> undef, <8 x i32> <i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef>
22; CHECK-NEXT:    [[BIN_RDX:%.*]] = add <8 x i32> [[LD]], [[RDX_SHUF]]
23; CHECK-NEXT:    [[RDX_SHUF1:%.*]] = shufflevector <8 x i32> [[BIN_RDX]], <8 x i32> undef, <8 x i32> <i32 2, i32 3, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
24; CHECK-NEXT:    [[BIN_RDX2:%.*]] = add <8 x i32> [[BIN_RDX]], [[RDX_SHUF1]]
25; CHECK-NEXT:    [[RDX_SHUF3:%.*]] = shufflevector <8 x i32> [[BIN_RDX2]], <8 x i32> undef, <8 x i32> <i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
26; CHECK-NEXT:    [[BIN_RDX4:%.*]] = add <8 x i32> [[BIN_RDX2]], [[RDX_SHUF3]]
27; CHECK-NEXT:    [[TMP2:%.*]] = extractelement <8 x i32> [[BIN_RDX4]], i32 0
28; CHECK:         ret i32 [[TMP2]]
29;
30; SSE2-LABEL: @test(
31; SSE2:         [[BC:%.*]] = bitcast i32* %p to <8 x i32>*
32; SSE2-NEXT:    [[LD:%.*]] = load <8 x i32>, <8 x i32>* [[BC]], align 4
33; SSE2:         [[RDX_SHUF:%.*]] = shufflevector <8 x i32> [[LD]], <8 x i32> undef, <8 x i32> <i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef>
34; SSE2-NEXT:    [[BIN_RDX:%.*]] = add <8 x i32> [[LD]], [[RDX_SHUF]]
35; SSE2-NEXT:    [[RDX_SHUF1:%.*]] = shufflevector <8 x i32> [[BIN_RDX]], <8 x i32> undef, <8 x i32> <i32 2, i32 3, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
36; SSE2-NEXT:    [[BIN_RDX2:%.*]] = add <8 x i32> [[BIN_RDX]], [[RDX_SHUF1]]
37; SSE2-NEXT:    [[RDX_SHUF3:%.*]] = shufflevector <8 x i32> [[BIN_RDX2]], <8 x i32> undef, <8 x i32> <i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
38; SSE2-NEXT:    [[BIN_RDX4:%.*]] = add <8 x i32> [[BIN_RDX2]], [[RDX_SHUF3]]
39; SSE2-NEXT:    [[TMP2:%.*]] = extractelement <8 x i32> [[BIN_RDX4]], i32 0
40; SSE2:         ret i32 [[TMP2]]
41;
42entry:
43  %0 = load i32, i32* %p, align 4
44  %arrayidx.1 = getelementptr inbounds i32, i32* %p, i64 1
45  %1 = load i32, i32* %arrayidx.1, align 4
46  %mul.18 = add i32 %1, %0
47  %arrayidx.2 = getelementptr inbounds i32, i32* %p, i64 2
48  %2 = load i32, i32* %arrayidx.2, align 4
49  %mul.29 = add i32 %2, %mul.18
50  %arrayidx.3 = getelementptr inbounds i32, i32* %p, i64 3
51  %3 = load i32, i32* %arrayidx.3, align 4
52  %mul.310 = add i32 %3, %mul.29
53  %arrayidx.4 = getelementptr inbounds i32, i32* %p, i64 4
54  %4 = load i32, i32* %arrayidx.4, align 4
55  %mul.411 = add i32 %4, %mul.310
56  %arrayidx.5 = getelementptr inbounds i32, i32* %p, i64 5
57  %5 = load i32, i32* %arrayidx.5, align 4
58  %mul.512 = add i32 %5, %mul.411
59  %arrayidx.6 = getelementptr inbounds i32, i32* %p, i64 6
60  %6 = load i32, i32* %arrayidx.6, align 4
61  %mul.613 = add i32 %6, %mul.512
62  %arrayidx.7 = getelementptr inbounds i32, i32* %p, i64 7
63  %7 = load i32, i32* %arrayidx.7, align 4
64  %mul.714 = add i32 %7, %mul.613
65  ret i32 %mul.714
66}
67