19529597cSSjoerd Meijer; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2cee313d2SEric Christopher; RUN: opt -loop-vectorize -force-vector-width=4 -S < %s | FileCheck %s
3cee313d2SEric Christopher
4cee313d2SEric Christopher; This is the test case from PR26314.
5cee313d2SEric Christopher; When we were retrying dependence checking with memchecks only,
6cee313d2SEric Christopher; the loop-invariant access in the inner loop was incorrectly determined to be wrapping
7cee313d2SEric Christopher; because it was not strided in the inner loop.
8cee313d2SEric Christopher; Improved wrapping detection allows vectorization in the following case.
9cee313d2SEric Christopher
10cee313d2SEric Christopher; #define Z 32
11cee313d2SEric Christopher; typedef struct s {
12cee313d2SEric Christopher;       int v1[Z];
13cee313d2SEric Christopher;       int v2[Z];
14cee313d2SEric Christopher;       int v3[Z][Z];
15cee313d2SEric Christopher; } s;
16cee313d2SEric Christopher;
17cee313d2SEric Christopher; void slow_function (s* const obj, int z) {
18cee313d2SEric Christopher;    for (int j=0; j<Z; j++) {
19cee313d2SEric Christopher;        for (int k=0; k<z; k++) {
20cee313d2SEric Christopher;            int x = obj->v1[k] + obj->v2[j];
21cee313d2SEric Christopher;            obj->v3[j][k] += x;
22cee313d2SEric Christopher;        }
23cee313d2SEric Christopher;    }
24cee313d2SEric Christopher; }
25cee313d2SEric Christopher
26cee313d2SEric Christophertarget datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
27cee313d2SEric Christopher
28cee313d2SEric Christopher%struct.s = type { [32 x i32], [32 x i32], [32 x [32 x i32]] }
29cee313d2SEric Christopher
30cee313d2SEric Christopherdefine void @Test(%struct.s* nocapture %obj, i64 %z) #0 {
319529597cSSjoerd Meijer; CHECK-LABEL: @Test(
329529597cSSjoerd Meijer; CHECK-NEXT:    [[OBJ4:%.*]] = bitcast %struct.s* [[OBJ:%.*]] to i8*
339529597cSSjoerd Meijer; CHECK-NEXT:    [[SCEVGEP5:%.*]] = getelementptr [[STRUCT_S:%.*]], %struct.s* [[OBJ]], i64 0, i32 0, i64 [[Z:%.*]]
349529597cSSjoerd Meijer; CHECK-NEXT:    [[SCEVGEP56:%.*]] = bitcast i32* [[SCEVGEP5]] to i8*
359529597cSSjoerd Meijer; CHECK-NEXT:    br label [[DOTOUTER_PREHEADER:%.*]]
369529597cSSjoerd Meijer; CHECK:       .outer.preheader:
379529597cSSjoerd Meijer; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, [[TMP0:%.*]] ], [ [[I_NEXT:%.*]], [[DOTOUTER:%.*]] ]
389529597cSSjoerd Meijer; CHECK-NEXT:    [[SCEVGEP:%.*]] = getelementptr [[STRUCT_S]], %struct.s* [[OBJ]], i64 0, i32 2, i64 [[I]], i64 0
399529597cSSjoerd Meijer; CHECK-NEXT:    [[SCEVGEP1:%.*]] = bitcast i32* [[SCEVGEP]] to i8*
409529597cSSjoerd Meijer; CHECK-NEXT:    [[SCEVGEP2:%.*]] = getelementptr [[STRUCT_S]], %struct.s* [[OBJ]], i64 0, i32 2, i64 [[I]], i64 [[Z]]
419529597cSSjoerd Meijer; CHECK-NEXT:    [[SCEVGEP23:%.*]] = bitcast i32* [[SCEVGEP2]] to i8*
429529597cSSjoerd Meijer; CHECK-NEXT:    [[SCEVGEP7:%.*]] = getelementptr [[STRUCT_S]], %struct.s* [[OBJ]], i64 0, i32 1, i64 [[I]]
439529597cSSjoerd Meijer; CHECK-NEXT:    [[SCEVGEP78:%.*]] = bitcast i32* [[SCEVGEP7]] to i8*
44e908e063SMindong Chen; CHECK-NEXT:    [[TMP1:%.*]] = add i64 [[I]], 1
45e908e063SMindong Chen; CHECK-NEXT:    [[SCEVGEP9:%.*]] = getelementptr [[STRUCT_S]], %struct.s* [[OBJ]], i64 0, i32 1, i64 [[TMP1]]
46e908e063SMindong Chen; CHECK-NEXT:    [[SCEVGEP910:%.*]] = bitcast i32* [[SCEVGEP9]] to i8*
47e908e063SMindong Chen; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds [[STRUCT_S]], %struct.s* [[OBJ]], i64 0, i32 1, i64 [[I]]
489529597cSSjoerd Meijer; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[Z]], 4
499529597cSSjoerd Meijer; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]]
509529597cSSjoerd Meijer; CHECK:       vector.memcheck:
519529597cSSjoerd Meijer; CHECK-NEXT:    [[BOUND0:%.*]] = icmp ult i8* [[SCEVGEP1]], [[SCEVGEP56]]
529529597cSSjoerd Meijer; CHECK-NEXT:    [[BOUND1:%.*]] = icmp ult i8* [[OBJ4]], [[SCEVGEP23]]
539529597cSSjoerd Meijer; CHECK-NEXT:    [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
54e908e063SMindong Chen; CHECK-NEXT:    [[BOUND011:%.*]] = icmp ult i8* [[SCEVGEP1]], [[SCEVGEP910]]
55e908e063SMindong Chen; CHECK-NEXT:    [[BOUND112:%.*]] = icmp ult i8* [[SCEVGEP78]], [[SCEVGEP23]]
56e908e063SMindong Chen; CHECK-NEXT:    [[FOUND_CONFLICT13:%.*]] = and i1 [[BOUND011]], [[BOUND112]]
57e908e063SMindong Chen; CHECK-NEXT:    [[CONFLICT_RDX:%.*]] = or i1 [[FOUND_CONFLICT]], [[FOUND_CONFLICT13]]
58*e844f053SFlorian Hahn; CHECK-NEXT:    br i1 [[CONFLICT_RDX]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
599529597cSSjoerd Meijer; CHECK:       vector.ph:
609529597cSSjoerd Meijer; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[Z]], 4
619529597cSSjoerd Meijer; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[Z]], [[N_MOD_VF]]
629529597cSSjoerd Meijer; CHECK-NEXT:    br label [[VECTOR_BODY:%.*]]
639529597cSSjoerd Meijer; CHECK:       vector.body:
649529597cSSjoerd Meijer; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
65e908e063SMindong Chen; CHECK-NEXT:    [[TMP3:%.*]] = add i64 [[INDEX]], 0
66e908e063SMindong Chen; CHECK-NEXT:    [[TMP4:%.*]] = getelementptr inbounds [[STRUCT_S]], %struct.s* [[OBJ]], i64 0, i32 0, i64 [[TMP3]]
67e908e063SMindong Chen; CHECK-NEXT:    [[TMP5:%.*]] = getelementptr inbounds i32, i32* [[TMP4]], i32 0
68e908e063SMindong Chen; CHECK-NEXT:    [[TMP6:%.*]] = bitcast i32* [[TMP5]] to <4 x i32>*
69e908e063SMindong Chen; CHECK-NEXT:    [[WIDE_LOAD:%.*]] = load <4 x i32>, <4 x i32>* [[TMP6]], align 4, !alias.scope !0
70e908e063SMindong Chen; CHECK-NEXT:    [[TMP7:%.*]] = load i32, i32* [[TMP2]], align 4, !alias.scope !3
71e908e063SMindong Chen; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[TMP7]], i32 0
72278aa65cSJuneyoung Lee; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
73e908e063SMindong Chen; CHECK-NEXT:    [[TMP8:%.*]] = add nsw <4 x i32> [[BROADCAST_SPLAT]], [[WIDE_LOAD]]
74e908e063SMindong Chen; CHECK-NEXT:    [[TMP9:%.*]] = getelementptr inbounds [[STRUCT_S]], %struct.s* [[OBJ]], i64 0, i32 2, i64 [[I]], i64 [[TMP3]]
75e908e063SMindong Chen; CHECK-NEXT:    [[TMP10:%.*]] = getelementptr inbounds i32, i32* [[TMP9]], i32 0
76e908e063SMindong Chen; CHECK-NEXT:    [[TMP11:%.*]] = bitcast i32* [[TMP10]] to <4 x i32>*
77e908e063SMindong Chen; CHECK-NEXT:    [[WIDE_LOAD14:%.*]] = load <4 x i32>, <4 x i32>* [[TMP11]], align 4, !alias.scope !5, !noalias !7
78e908e063SMindong Chen; CHECK-NEXT:    [[TMP12:%.*]] = add nsw <4 x i32> [[TMP8]], [[WIDE_LOAD14]]
79e908e063SMindong Chen; CHECK-NEXT:    [[TMP13:%.*]] = bitcast i32* [[TMP10]] to <4 x i32>*
80e908e063SMindong Chen; CHECK-NEXT:    store <4 x i32> [[TMP12]], <4 x i32>* [[TMP13]], align 4, !alias.scope !5, !noalias !7
8123c2f2e6SFlorian Hahn; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
82e908e063SMindong Chen; CHECK-NEXT:    [[TMP14:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
83e908e063SMindong Chen; CHECK-NEXT:    br i1 [[TMP14]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP8:![0-9]+]]
849529597cSSjoerd Meijer; CHECK:       middle.block:
859529597cSSjoerd Meijer; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[Z]], [[N_VEC]]
869529597cSSjoerd Meijer; CHECK-NEXT:    br i1 [[CMP_N]], label [[DOTOUTER]], label [[SCALAR_PH]]
879529597cSSjoerd Meijer; CHECK:       scalar.ph:
889529597cSSjoerd Meijer; CHECK-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[DOTOUTER_PREHEADER]] ], [ 0, [[VECTOR_MEMCHECK]] ]
899529597cSSjoerd Meijer; CHECK-NEXT:    br label [[DOTINNER:%.*]]
909529597cSSjoerd Meijer; CHECK:       .exit:
919529597cSSjoerd Meijer; CHECK-NEXT:    ret void
929529597cSSjoerd Meijer; CHECK:       .outer:
939529597cSSjoerd Meijer; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
949529597cSSjoerd Meijer; CHECK-NEXT:    [[EXITCOND_OUTER:%.*]] = icmp eq i64 [[I_NEXT]], 32
959529597cSSjoerd Meijer; CHECK-NEXT:    br i1 [[EXITCOND_OUTER]], label [[DOTEXIT:%.*]], label [[DOTOUTER_PREHEADER]]
969529597cSSjoerd Meijer; CHECK:       .inner:
979529597cSSjoerd Meijer; CHECK-NEXT:    [[J:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[J_NEXT:%.*]], [[DOTINNER]] ]
98e908e063SMindong Chen; CHECK-NEXT:    [[TMP15:%.*]] = getelementptr inbounds [[STRUCT_S]], %struct.s* [[OBJ]], i64 0, i32 0, i64 [[J]]
99e908e063SMindong Chen; CHECK-NEXT:    [[TMP16:%.*]] = load i32, i32* [[TMP15]], align 4
100e908e063SMindong Chen; CHECK-NEXT:    [[TMP17:%.*]] = load i32, i32* [[TMP2]], align 4
101e908e063SMindong Chen; CHECK-NEXT:    [[TMP18:%.*]] = add nsw i32 [[TMP17]], [[TMP16]]
102e908e063SMindong Chen; CHECK-NEXT:    [[TMP19:%.*]] = getelementptr inbounds [[STRUCT_S]], %struct.s* [[OBJ]], i64 0, i32 2, i64 [[I]], i64 [[J]]
103e908e063SMindong Chen; CHECK-NEXT:    [[TMP20:%.*]] = load i32, i32* [[TMP19]], align 4
104e908e063SMindong Chen; CHECK-NEXT:    [[TMP21:%.*]] = add nsw i32 [[TMP18]], [[TMP20]]
105e908e063SMindong Chen; CHECK-NEXT:    store i32 [[TMP21]], i32* [[TMP19]], align 4
1069529597cSSjoerd Meijer; CHECK-NEXT:    [[J_NEXT]] = add nuw nsw i64 [[J]], 1
1079529597cSSjoerd Meijer; CHECK-NEXT:    [[EXITCOND_INNER:%.*]] = icmp eq i64 [[J_NEXT]], [[Z]]
108f3814ed3SMindong Chen; CHECK-NEXT:    br i1 [[EXITCOND_INNER]], label [[DOTOUTER]], label [[DOTINNER]], !llvm.loop [[LOOP10:![0-9]+]]
1099529597cSSjoerd Meijer;
110cee313d2SEric Christopher  br label %.outer.preheader
111cee313d2SEric Christopher
112cee313d2SEric Christopher
113cee313d2SEric Christopher.outer.preheader:
114cee313d2SEric Christopher  %i = phi i64 [ 0, %0 ], [ %i.next, %.outer ]
115cee313d2SEric Christopher  %1 = getelementptr inbounds %struct.s, %struct.s* %obj, i64 0, i32 1, i64 %i
116cee313d2SEric Christopher  br label %.inner
117cee313d2SEric Christopher
118cee313d2SEric Christopher.exit:
119cee313d2SEric Christopher  ret void
120cee313d2SEric Christopher
121cee313d2SEric Christopher.outer:
122cee313d2SEric Christopher  %i.next = add nuw nsw i64 %i, 1
123cee313d2SEric Christopher  %exitcond.outer = icmp eq i64 %i.next, 32
124cee313d2SEric Christopher  br i1 %exitcond.outer, label %.exit, label %.outer.preheader
125cee313d2SEric Christopher
126cee313d2SEric Christopher.inner:
127cee313d2SEric Christopher  %j = phi i64 [ 0, %.outer.preheader ], [ %j.next, %.inner ]
128cee313d2SEric Christopher  %2 = getelementptr inbounds %struct.s, %struct.s* %obj, i64 0, i32 0, i64 %j
129cee313d2SEric Christopher  %3 = load i32, i32* %2
130cee313d2SEric Christopher  %4 = load i32, i32* %1
131cee313d2SEric Christopher  %5 = add nsw i32 %4, %3
132cee313d2SEric Christopher  %6 = getelementptr inbounds %struct.s, %struct.s* %obj, i64 0, i32 2, i64 %i, i64 %j
133cee313d2SEric Christopher  %7 = load i32, i32* %6
134cee313d2SEric Christopher  %8 = add nsw i32 %5, %7
135cee313d2SEric Christopher  store i32 %8, i32* %6
136cee313d2SEric Christopher  %j.next = add nuw nsw i64 %j, 1
137cee313d2SEric Christopher  %exitcond.inner = icmp eq i64 %j.next, %z
138cee313d2SEric Christopher  br i1 %exitcond.inner, label %.outer, label %.inner
139cee313d2SEric Christopher}
140