1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -S -passes='default<O1>,function(loop-versioning-licm,loop-mssa(licm))' -debug-only=loop-versioning-licm 2>&1 | FileCheck %s
5 ; Test to confirm loop is a candidate for LoopVersioningLICM.
6 ; It also confirms invariant moved out of loop.
8 ; CHECK: Loop: Loop at depth 2 containing: %for.body3<header><latch><exiting>
9 ; CHECK-NEXT: Loop Versioning found to be beneficial
11 define i32 @foo(ptr nocapture %var1, ptr nocapture readnone %var2, ptr nocapture %var3, i32 %itr) #0 {
14 ; CHECK-NEXT: [[CMP14:%.*]] = icmp eq i32 [[ITR:%.*]], 0
15 ; CHECK-NEXT: br i1 [[CMP14]], label [[FOR_END13:%.*]], label [[FOR_COND1_PREHEADER_PREHEADER:%.*]]
16 ; CHECK: for.cond1.preheader.preheader:
17 ; CHECK-NEXT: [[UGLYGEP1:%.*]] = getelementptr i8, ptr [[VAR1:%.*]], i64 4
18 ; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[ITR]], -1
19 ; CHECK-NEXT: br label [[FOR_COND1_PREHEADER:%.*]]
20 ; CHECK: for.cond1.preheader:
21 ; CHECK-NEXT: [[INDVAR:%.*]] = phi i64 [ 0, [[FOR_COND1_PREHEADER_PREHEADER]] ], [ [[INDVAR_NEXT:%.*]], [[FOR_INC11:%.*]] ]
22 ; CHECK-NEXT: [[J_016:%.*]] = phi i32 [ [[J_1_LCSSA:%.*]], [[FOR_INC11]] ], [ 0, [[FOR_COND1_PREHEADER_PREHEADER]] ]
23 ; CHECK-NEXT: [[I_015:%.*]] = phi i32 [ [[INC12:%.*]], [[FOR_INC11]] ], [ 0, [[FOR_COND1_PREHEADER_PREHEADER]] ]
24 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[INDVAR]], 2
25 ; CHECK-NEXT: [[UGLYGEP3:%.*]] = getelementptr i8, ptr [[VAR3:%.*]], i64 [[TMP1]]
26 ; CHECK-NEXT: [[TMP2:%.*]] = add i64 [[TMP1]], 4
27 ; CHECK-NEXT: [[UGLYGEP4:%.*]] = getelementptr i8, ptr [[VAR3]], i64 [[TMP2]]
28 ; CHECK-NEXT: [[CMP212:%.*]] = icmp ult i32 [[J_016]], [[ITR]]
29 ; CHECK-NEXT: br i1 [[CMP212]], label [[FOR_BODY3_LVER_CHECK:%.*]], label [[FOR_INC11]]
30 ; CHECK: for.body3.lver.check:
31 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[I_015]], [[ITR]]
32 ; CHECK-NEXT: [[IDXPROM6:%.*]] = zext i32 [[I_015]] to i64
33 ; CHECK-NEXT: [[ARRAYIDX7:%.*]] = getelementptr inbounds i32, ptr [[VAR3]], i64 [[IDXPROM6]]
34 ; CHECK-NEXT: [[TMP3:%.*]] = zext i32 [[J_016]] to i64
35 ; CHECK-NEXT: [[TMP4:%.*]] = shl nuw nsw i64 [[TMP3]], 2
36 ; CHECK-NEXT: [[UGLYGEP:%.*]] = getelementptr i8, ptr [[VAR1]], i64 [[TMP4]]
37 ; CHECK-NEXT: [[TMP5:%.*]] = sub i32 [[TMP0]], [[J_016]]
38 ; CHECK-NEXT: [[TMP6:%.*]] = zext i32 [[TMP5]] to i64
39 ; CHECK-NEXT: [[TMP7:%.*]] = shl nuw nsw i64 [[TMP6]], 2
40 ; CHECK-NEXT: [[TMP8:%.*]] = add i64 [[TMP4]], [[TMP7]]
41 ; CHECK-NEXT: [[UGLYGEP2:%.*]] = getelementptr i8, ptr [[UGLYGEP1]], i64 [[TMP8]]
42 ; CHECK-NEXT: [[BOUND0:%.*]] = icmp ult ptr [[UGLYGEP]], [[UGLYGEP4]]
43 ; CHECK-NEXT: [[BOUND1:%.*]] = icmp ult ptr [[UGLYGEP3]], [[UGLYGEP2]]
44 ; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
45 ; CHECK-NEXT: br i1 [[FOUND_CONFLICT]], label [[FOR_BODY3_PH_LVER_ORIG:%.*]], label [[FOR_BODY3_PH:%.*]]
46 ; CHECK: for.body3.ph.lver.orig:
47 ; CHECK-NEXT: br label [[FOR_BODY3_LVER_ORIG:%.*]]
48 ; CHECK: for.body3.lver.orig:
49 ; CHECK-NEXT: [[J_113_LVER_ORIG:%.*]] = phi i32 [ [[J_016]], [[FOR_BODY3_PH_LVER_ORIG]] ], [ [[INC_LVER_ORIG:%.*]], [[FOR_BODY3_LVER_ORIG]] ]
50 ; CHECK-NEXT: [[IDXPROM_LVER_ORIG:%.*]] = zext i32 [[J_113_LVER_ORIG]] to i64
51 ; CHECK-NEXT: [[ARRAYIDX_LVER_ORIG:%.*]] = getelementptr inbounds i32, ptr [[VAR1]], i64 [[IDXPROM_LVER_ORIG]]
52 ; CHECK-NEXT: store i32 [[ADD]], ptr [[ARRAYIDX_LVER_ORIG]], align 4
53 ; CHECK-NEXT: [[TMP9:%.*]] = load i32, ptr [[ARRAYIDX7]], align 4
54 ; CHECK-NEXT: [[ADD8_LVER_ORIG:%.*]] = add nsw i32 [[TMP9]], [[ADD]]
55 ; CHECK-NEXT: store i32 [[ADD8_LVER_ORIG]], ptr [[ARRAYIDX7]], align 4
56 ; CHECK-NEXT: [[INC_LVER_ORIG]] = add nuw i32 [[J_113_LVER_ORIG]], 1
57 ; CHECK-NEXT: [[CMP2_LVER_ORIG:%.*]] = icmp ult i32 [[INC_LVER_ORIG]], [[ITR]]
58 ; CHECK-NEXT: br i1 [[CMP2_LVER_ORIG]], label [[FOR_BODY3_LVER_ORIG]], label [[FOR_INC11_LOOPEXIT_LOOPEXIT:%.*]], !llvm.loop [[LOOP0:![0-9]+]]
59 ; CHECK: for.body3.ph:
60 ; CHECK-NEXT: [[ARRAYIDX7_PROMOTED:%.*]] = load i32, ptr [[ARRAYIDX7]], align 4, !alias.scope !2, !noalias !2
61 ; CHECK-NEXT: br label [[FOR_BODY3:%.*]]
63 ; CHECK-NEXT: [[ADD86:%.*]] = phi i32 [ [[ARRAYIDX7_PROMOTED]], [[FOR_BODY3_PH]] ], [ [[ADD8:%.*]], [[FOR_BODY3]] ]
64 ; CHECK-NEXT: [[J_113:%.*]] = phi i32 [ [[J_016]], [[FOR_BODY3_PH]] ], [ [[INC:%.*]], [[FOR_BODY3]] ]
65 ; CHECK-NEXT: [[IDXPROM:%.*]] = zext i32 [[J_113]] to i64
66 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[VAR1]], i64 [[IDXPROM]]
67 ; CHECK-NEXT: store i32 [[ADD]], ptr [[ARRAYIDX]], align 4, !alias.scope !2, !noalias !2
68 ; CHECK-NEXT: [[ADD8]] = add nsw i32 [[ADD86]], [[ADD]]
69 ; CHECK-NEXT: [[INC]] = add nuw i32 [[J_113]], 1
70 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i32 [[INC]], [[ITR]]
71 ; CHECK-NEXT: br i1 [[CMP2]], label [[FOR_BODY3]], label [[FOR_INC11_LOOPEXIT_LOOPEXIT5:%.*]], !llvm.loop [[LOOP5:![0-9]+]]
72 ; CHECK: for.inc11.loopexit.loopexit:
73 ; CHECK-NEXT: br label [[FOR_INC11_LOOPEXIT:%.*]]
74 ; CHECK: for.inc11.loopexit.loopexit5:
75 ; CHECK-NEXT: [[ADD8_LCSSA:%.*]] = phi i32 [ [[ADD8]], [[FOR_BODY3]] ]
76 ; CHECK-NEXT: store i32 [[ADD8_LCSSA]], ptr [[ARRAYIDX7]], align 4, !alias.scope !2, !noalias !2
77 ; CHECK-NEXT: br label [[FOR_INC11_LOOPEXIT]]
78 ; CHECK: for.inc11.loopexit:
79 ; CHECK-NEXT: br label [[FOR_INC11]]
81 ; CHECK-NEXT: [[J_1_LCSSA]] = phi i32 [ [[J_016]], [[FOR_COND1_PREHEADER]] ], [ [[ITR]], [[FOR_INC11_LOOPEXIT]] ]
82 ; CHECK-NEXT: [[INC12]] = add nuw i32 [[I_015]], 1
83 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[INC12]], [[ITR]]
84 ; CHECK-NEXT: [[INDVAR_NEXT]] = add i64 [[INDVAR]], 1
85 ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_COND1_PREHEADER]], label [[FOR_END13_LOOPEXIT:%.*]]
86 ; CHECK: for.end13.loopexit:
87 ; CHECK-NEXT: br label [[FOR_END13]]
89 ; CHECK-NEXT: ret i32 0
92 %cmp14 = icmp eq i32 %itr, 0
93 br i1 %cmp14, label %for.end13, label %for.cond1.preheader.preheader
95 for.cond1.preheader.preheader: ; preds = %entry
96 br label %for.cond1.preheader
98 for.cond1.preheader: ; preds = %for.cond1.preheader.preheader, %for.inc11
99 %j.016 = phi i32 [ %j.1.lcssa, %for.inc11 ], [ 0, %for.cond1.preheader.preheader ]
100 %i.015 = phi i32 [ %inc12, %for.inc11 ], [ 0, %for.cond1.preheader.preheader ]
101 %cmp212 = icmp ult i32 %j.016, %itr
102 br i1 %cmp212, label %for.body3.lr.ph, label %for.inc11
104 for.body3.lr.ph: ; preds = %for.cond1.preheader
105 %add = add i32 %i.015, %itr
106 %idxprom6 = zext i32 %i.015 to i64
107 %arrayidx7 = getelementptr inbounds i32, ptr %var3, i64 %idxprom6
110 for.body3: ; preds = %for.body3.lr.ph, %for.body3
111 %j.113 = phi i32 [ %j.016, %for.body3.lr.ph ], [ %inc, %for.body3 ]
112 %idxprom = zext i32 %j.113 to i64
113 %arrayidx = getelementptr inbounds i32, ptr %var1, i64 %idxprom
114 store i32 %add, ptr %arrayidx, align 4
115 %0 = load i32, ptr %arrayidx7, align 4
116 %add8 = add nsw i32 %0, %add
117 store i32 %add8, ptr %arrayidx7, align 4
118 %inc = add nuw i32 %j.113, 1
119 %cmp2 = icmp ult i32 %inc, %itr
120 br i1 %cmp2, label %for.body3, label %for.inc11.loopexit
122 for.inc11.loopexit: ; preds = %for.body3
125 for.inc11: ; preds = %for.inc11.loopexit, %for.cond1.preheader
126 %j.1.lcssa = phi i32 [ %j.016, %for.cond1.preheader ], [ %itr, %for.inc11.loopexit ]
127 %inc12 = add nuw i32 %i.015, 1
128 %cmp = icmp ult i32 %inc12, %itr
129 br i1 %cmp, label %for.cond1.preheader, label %for.end13.loopexit
131 for.end13.loopexit: ; preds = %for.inc11
134 for.end13: ; preds = %for.end13.loopexit, %entry