1;  RUN: opt -basic-aa -loop-idiom -use-lir-code-size-heurs=true < %s -S | FileCheck %s
2
3; When compiling for codesize we avoid idiom recognition for a
4; multi-block loop unless it is one of
5; - a loop_memset idiom, or
6; - a memset/memcpy idiom in a nested loop.
7
8declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i1)
9@APPLES = common global i32 0, align 4
10@ORANGES = common global i32 0, align 4
11
12; LIR allowed: loop_memset idiom in multi-block loop.
13; ===================================================
14; CHECK-LABEL: @LoopMemset
15; CHECK: for.body.preheader:
16; CHECK: call void @llvm.memset
17; CHECK: for.body:
18;
19define i32 @LoopMemset([2048 x i8]* noalias nocapture %DST, i32 %SIZE) local_unnamed_addr optsize {
20entry:
21  %cmp12 = icmp sgt i32 %SIZE, 0
22  br i1 %cmp12, label %for.body.preheader, label %for.end
23
24for.body.preheader:                               ; preds = %entry
25  br label %for.body
26
27for.body:                                         ; preds = %for.body.preheader, %for.inc
28  %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.inc ]
29  %BASKET.013 = phi i32 [ %BASKET.1, %for.inc ], [ 0, %for.body.preheader ]
30  %arraydecay = getelementptr inbounds [2048 x i8], [2048 x i8]* %DST, i64 %indvars.iv, i64 0
31  tail call void @llvm.memset.p0i8.i64(i8* %arraydecay, i8 -1, i64 2048, i1 false)
32  %0 = trunc i64 %indvars.iv to i32
33  %rem11 = and i32 %0, 1
34  %cmp1 = icmp eq i32 %rem11, 0
35  %1 = load i32, i32* @ORANGES, align 4
36  %2 = load i32, i32* @APPLES, align 4
37  br i1 %cmp1, label %if.then, label %if.else
38
39if.else:                                          ; preds = %for.body
40  %dec3 = add nsw i32 %2, -1
41  store i32 %dec3, i32* @APPLES, align 4
42  br label %for.inc
43
44if.then:                                          ; preds = %for.body
45  %dec = add nsw i32 %1, -1
46  store i32 %dec, i32* @ORANGES, align 4
47  br label %for.inc
48
49for.inc:                                          ; preds = %if.then, %if.else
50  %.pn = phi i32 [ %2, %if.then ], [ %1, %if.else ]
51  %BASKET.1 = add nsw i32 %.pn, %BASKET.013
52  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
53  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
54  %exitcond = icmp ne i32 %lftr.wideiv, %SIZE
55  br i1 %exitcond, label %for.body, label %for.end.loopexit
56
57for.end.loopexit:                                 ; preds = %for.inc
58  %BASKET.1.lcssa = phi i32 [ %BASKET.1, %for.inc ]
59  br label %for.end
60
61for.end:                                          ; preds = %for.end.loopexit, %entry
62  %BASKET.0.lcssa = phi i32 [ 0, %entry ], [ %BASKET.1.lcssa, %for.end.loopexit ]
63  ret i32 %BASKET.0.lcssa
64}
65
66; LIR allowed: memset idiom in multi-block nested loop,
67; which is recognized as a loop_memset in its turn.
68; =====================================================
69; CHECK-LABEL: @NestedMemset_LoopMemset
70; CHECK: for.cond1.preheader.preheader:
71; CHECK: call void @llvm.memset
72; CHECK: for.cond1.preheader:
73;
74define i32 @NestedMemset_LoopMemset([2046 x i8]* noalias nocapture %DST, i32 %SIZE) local_unnamed_addr optsize {
75entry:
76  %cmp25 = icmp sgt i32 %SIZE, 0
77  br i1 %cmp25, label %for.cond1.preheader.preheader, label %for.end11
78
79for.cond1.preheader.preheader:                    ; preds = %entry
80  br label %for.cond1.preheader
81
82for.cond1.preheader:                              ; preds = %for.cond1.preheader.preheader, %for.inc9
83  %i.027 = phi i32 [ %inc10, %for.inc9 ], [ 0, %for.cond1.preheader.preheader ]
84  %BASKET.026 = phi i32 [ %BASKET.2.lcssa, %for.inc9 ], [ 0, %for.cond1.preheader.preheader ]
85  %idxprom4 = sext i32 %i.027 to i64
86  %rem22 = and i32 %i.027, 1
87  %cmp6 = icmp eq i32 %rem22, 0
88  br label %for.body3
89
90for.body3:                                        ; preds = %for.cond1.preheader, %for.inc
91  %indvars.iv = phi i64 [ 0, %for.cond1.preheader ], [ %indvars.iv.next, %for.inc ]
92  %BASKET.123 = phi i32 [ %BASKET.026, %for.cond1.preheader ], [ %BASKET.2, %for.inc ]
93  %arrayidx5 = getelementptr inbounds [2046 x i8], [2046 x i8]* %DST, i64 %idxprom4, i64 %indvars.iv
94  store i8 -1, i8* %arrayidx5, align 1
95  %0 = load i32, i32* @APPLES, align 4
96  %1 = load i32, i32* @ORANGES, align 4
97  br i1 %cmp6, label %if.then, label %if.else
98
99if.else:                                          ; preds = %for.body3
100  %dec8 = add nsw i32 %0, -1
101  store i32 %dec8, i32* @APPLES, align 4
102  br label %for.inc
103
104if.then:                                          ; preds = %for.body3
105  %dec = add nsw i32 %1, -1
106  store i32 %dec, i32* @ORANGES, align 4
107  br label %for.inc
108
109for.inc:                                          ; preds = %if.then, %if.else
110  %.pn = phi i32 [ %0, %if.then ], [ %1, %if.else ]
111  %BASKET.2 = add nsw i32 %.pn, %BASKET.123
112  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
113  %exitcond = icmp ne i64 %indvars.iv.next, 2046
114  br i1 %exitcond, label %for.body3, label %for.inc9
115
116for.inc9:                                         ; preds = %for.inc
117  %BASKET.2.lcssa = phi i32 [ %BASKET.2, %for.inc ]
118  %inc10 = add nsw i32 %i.027, 1
119  %cmp = icmp slt i32 %inc10, %SIZE
120  br i1 %cmp, label %for.cond1.preheader, label %for.end11.loopexit
121
122for.end11.loopexit:                               ; preds = %for.inc9
123  %BASKET.2.lcssa.lcssa = phi i32 [ %BASKET.2.lcssa, %for.inc9 ]
124  br label %for.end11
125
126for.end11:                                        ; preds = %for.end11.loopexit, %entry
127  %BASKET.0.lcssa = phi i32 [ 0, %entry ], [ %BASKET.2.lcssa.lcssa, %for.end11.loopexit ]
128  ret i32 %BASKET.0.lcssa
129}
130
131; LIR avoided: memset idiom in multi-block top-level loop.
132; ========================================================
133; CHECK-LABEL: @Non_NestedMemset
134; CHECK-NOT: call void @llvm.memset
135;
136define i32 @Non_NestedMemset(i8* noalias nocapture %DST, i32 %SIZE) local_unnamed_addr optsize {
137entry:
138  %cmp12 = icmp sgt i32 %SIZE, 0
139  br i1 %cmp12, label %for.body.preheader, label %for.end
140
141for.body.preheader:                               ; preds = %entry
142  br label %for.body
143
144for.body:                                         ; preds = %for.body.preheader, %for.inc
145  %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.inc ]
146  %BASKET.013 = phi i32 [ %BASKET.1, %for.inc ], [ 0, %for.body.preheader ]
147  %arrayidx = getelementptr inbounds i8, i8* %DST, i64 %indvars.iv
148  store i8 -1, i8* %arrayidx, align 1
149  %0 = trunc i64 %indvars.iv to i32
150  %rem11 = and i32 %0, 1
151  %cmp1 = icmp eq i32 %rem11, 0
152  %1 = load i32, i32* @ORANGES, align 4
153  %2 = load i32, i32* @APPLES, align 4
154  br i1 %cmp1, label %if.then, label %if.else
155
156if.else:                                          ; preds = %for.body
157  %dec3 = add nsw i32 %2, -1
158  store i32 %dec3, i32* @APPLES, align 4
159  br label %for.inc
160
161if.then:                                          ; preds = %for.body
162  %dec = add nsw i32 %1, -1
163  store i32 %dec, i32* @ORANGES, align 4
164  br label %for.inc
165
166for.inc:                                          ; preds = %if.then, %if.else
167  %.pn = phi i32 [ %2, %if.then ], [ %1, %if.else ]
168  %BASKET.1 = add nsw i32 %.pn, %BASKET.013
169  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
170  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
171  %exitcond = icmp ne i32 %lftr.wideiv, %SIZE
172  br i1 %exitcond, label %for.body, label %for.end.loopexit
173
174for.end.loopexit:                                 ; preds = %for.inc
175  %BASKET.1.lcssa = phi i32 [ %BASKET.1, %for.inc ]
176  br label %for.end
177
178for.end:                                          ; preds = %for.end.loopexit, %entry
179  %BASKET.0.lcssa = phi i32 [ 0, %entry ], [ %BASKET.1.lcssa, %for.end.loopexit ]
180  ret i32 %BASKET.0.lcssa
181}
182
183