]>
Commit | Line | Data |
---|---|---|
b8698a0f | 1 | /* Vectorizer Specific Loop Manipulations |
23a5b65a | 2 | Copyright (C) 2003-2014 Free Software Foundation, Inc. |
b8698a0f | 3 | Contributed by Dorit Naishlos <dorit@il.ibm.com> |
ebfd146a IR |
4 | and Ira Rosen <irar@il.ibm.com> |
5 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
10 | Software Foundation; either version 3, or (at your option) any later | |
11 | version. | |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
78c60e3d | 25 | #include "dumpfile.h" |
ebfd146a | 26 | #include "tm.h" |
ebfd146a | 27 | #include "tree.h" |
60393bbc AM |
28 | #include "predict.h" |
29 | #include "vec.h" | |
30 | #include "hashtab.h" | |
31 | #include "hash-set.h" | |
32 | #include "machmode.h" | |
33 | #include "hard-reg-set.h" | |
34 | #include "input.h" | |
35 | #include "function.h" | |
36 | #include "dominance.h" | |
37 | #include "cfg.h" | |
38 | #include "cfganal.h" | |
ebfd146a | 39 | #include "basic-block.h" |
cf835838 | 40 | #include "gimple-pretty-print.h" |
2fb9a547 AM |
41 | #include "tree-ssa-alias.h" |
42 | #include "internal-fn.h" | |
43 | #include "gimple-expr.h" | |
44 | #include "is-a.h" | |
18f429e2 | 45 | #include "gimple.h" |
45b0be94 | 46 | #include "gimplify.h" |
5be5c238 | 47 | #include "gimple-iterator.h" |
18f429e2 | 48 | #include "gimplify-me.h" |
442b4905 AM |
49 | #include "gimple-ssa.h" |
50 | #include "tree-cfg.h" | |
51 | #include "tree-phinodes.h" | |
52 | #include "ssa-iterators.h" | |
d8a2d370 | 53 | #include "stringpool.h" |
442b4905 | 54 | #include "tree-ssanames.h" |
e28030cf | 55 | #include "tree-ssa-loop-manip.h" |
442b4905 | 56 | #include "tree-into-ssa.h" |
7a300452 | 57 | #include "tree-ssa.h" |
7ee2468b | 58 | #include "tree-pass.h" |
ebfd146a | 59 | #include "cfgloop.h" |
718f9c0f | 60 | #include "diagnostic-core.h" |
ebfd146a IR |
61 | #include "tree-scalar-evolution.h" |
62 | #include "tree-vectorizer.h" | |
63 | #include "langhooks.h" | |
64 | ||
65 | /************************************************************************* | |
66 | Simple Loop Peeling Utilities | |
67 | ||
68 | Utilities to support loop peeling for vectorization purposes. | |
69 | *************************************************************************/ | |
70 | ||
71 | ||
72 | /* Renames the use *OP_P. */ | |
73 | ||
74 | static void | |
75 | rename_use_op (use_operand_p op_p) | |
76 | { | |
77 | tree new_name; | |
78 | ||
79 | if (TREE_CODE (USE_FROM_PTR (op_p)) != SSA_NAME) | |
80 | return; | |
81 | ||
82 | new_name = get_current_def (USE_FROM_PTR (op_p)); | |
83 | ||
84 | /* Something defined outside of the loop. */ | |
85 | if (!new_name) | |
86 | return; | |
87 | ||
88 | /* An ordinary ssa name defined in the loop. */ | |
89 | ||
90 | SET_USE (op_p, new_name); | |
91 | } | |
92 | ||
93 | ||
94 | /* Renames the variables in basic block BB. */ | |
95 | ||
2cfc56b9 | 96 | static void |
ebfd146a IR |
97 | rename_variables_in_bb (basic_block bb) |
98 | { | |
ebfd146a IR |
99 | gimple stmt; |
100 | use_operand_p use_p; | |
101 | ssa_op_iter iter; | |
102 | edge e; | |
103 | edge_iterator ei; | |
104 | struct loop *loop = bb->loop_father; | |
105 | ||
538dd0b7 DM |
106 | for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); |
107 | gsi_next (&gsi)) | |
ebfd146a IR |
108 | { |
109 | stmt = gsi_stmt (gsi); | |
110 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES) | |
111 | rename_use_op (use_p); | |
112 | } | |
113 | ||
2cfc56b9 | 114 | FOR_EACH_EDGE (e, ei, bb->preds) |
ebfd146a | 115 | { |
2cfc56b9 | 116 | if (!flow_bb_inside_loop_p (loop, e->src)) |
ebfd146a | 117 | continue; |
538dd0b7 DM |
118 | for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); |
119 | gsi_next (&gsi)) | |
120 | rename_use_op (PHI_ARG_DEF_PTR_FROM_EDGE (gsi.phi (), e)); | |
ebfd146a IR |
121 | } |
122 | } | |
123 | ||
124 | ||
684f25f4 AO |
125 | typedef struct |
126 | { | |
127 | tree from, to; | |
128 | basic_block bb; | |
129 | } adjust_info; | |
130 | ||
684f25f4 AO |
131 | /* A stack of values to be adjusted in debug stmts. We have to |
132 | process them LIFO, so that the closest substitution applies. If we | |
133 | processed them FIFO, without the stack, we might substitute uses | |
134 | with a PHI DEF that would soon become non-dominant, and when we got | |
135 | to the suitable one, it wouldn't have anything to substitute any | |
136 | more. */ | |
ff4c81cc | 137 | static vec<adjust_info, va_heap> adjust_vec; |
684f25f4 AO |
138 | |
139 | /* Adjust any debug stmts that referenced AI->from values to use the | |
140 | loop-closed AI->to, if the references are dominated by AI->bb and | |
141 | not by the definition of AI->from. */ | |
142 | ||
143 | static void | |
144 | adjust_debug_stmts_now (adjust_info *ai) | |
145 | { | |
146 | basic_block bbphi = ai->bb; | |
147 | tree orig_def = ai->from; | |
148 | tree new_def = ai->to; | |
149 | imm_use_iterator imm_iter; | |
150 | gimple stmt; | |
151 | basic_block bbdef = gimple_bb (SSA_NAME_DEF_STMT (orig_def)); | |
152 | ||
153 | gcc_assert (dom_info_available_p (CDI_DOMINATORS)); | |
154 | ||
155 | /* Adjust any debug stmts that held onto non-loop-closed | |
156 | references. */ | |
157 | FOR_EACH_IMM_USE_STMT (stmt, imm_iter, orig_def) | |
158 | { | |
159 | use_operand_p use_p; | |
160 | basic_block bbuse; | |
161 | ||
162 | if (!is_gimple_debug (stmt)) | |
163 | continue; | |
164 | ||
165 | gcc_assert (gimple_debug_bind_p (stmt)); | |
166 | ||
167 | bbuse = gimple_bb (stmt); | |
168 | ||
169 | if ((bbuse == bbphi | |
170 | || dominated_by_p (CDI_DOMINATORS, bbuse, bbphi)) | |
171 | && !(bbuse == bbdef | |
172 | || dominated_by_p (CDI_DOMINATORS, bbuse, bbdef))) | |
173 | { | |
174 | if (new_def) | |
175 | FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter) | |
176 | SET_USE (use_p, new_def); | |
177 | else | |
178 | { | |
179 | gimple_debug_bind_reset_value (stmt); | |
180 | update_stmt (stmt); | |
181 | } | |
182 | } | |
183 | } | |
184 | } | |
185 | ||
186 | /* Adjust debug stmts as scheduled before. */ | |
187 | ||
188 | static void | |
189 | adjust_vec_debug_stmts (void) | |
190 | { | |
191 | if (!MAY_HAVE_DEBUG_STMTS) | |
192 | return; | |
193 | ||
9771b263 | 194 | gcc_assert (adjust_vec.exists ()); |
684f25f4 | 195 | |
9771b263 | 196 | while (!adjust_vec.is_empty ()) |
684f25f4 | 197 | { |
9771b263 DN |
198 | adjust_debug_stmts_now (&adjust_vec.last ()); |
199 | adjust_vec.pop (); | |
684f25f4 AO |
200 | } |
201 | ||
9771b263 | 202 | adjust_vec.release (); |
684f25f4 AO |
203 | } |
204 | ||
205 | /* Adjust any debug stmts that referenced FROM values to use the | |
206 | loop-closed TO, if the references are dominated by BB and not by | |
207 | the definition of FROM. If adjust_vec is non-NULL, adjustments | |
208 | will be postponed until adjust_vec_debug_stmts is called. */ | |
209 | ||
210 | static void | |
211 | adjust_debug_stmts (tree from, tree to, basic_block bb) | |
212 | { | |
213 | adjust_info ai; | |
214 | ||
a471762f RG |
215 | if (MAY_HAVE_DEBUG_STMTS |
216 | && TREE_CODE (from) == SSA_NAME | |
a52ca739 | 217 | && ! SSA_NAME_IS_DEFAULT_DEF (from) |
a471762f | 218 | && ! virtual_operand_p (from)) |
684f25f4 AO |
219 | { |
220 | ai.from = from; | |
221 | ai.to = to; | |
222 | ai.bb = bb; | |
223 | ||
9771b263 DN |
224 | if (adjust_vec.exists ()) |
225 | adjust_vec.safe_push (ai); | |
684f25f4 AO |
226 | else |
227 | adjust_debug_stmts_now (&ai); | |
228 | } | |
229 | } | |
230 | ||
231 | /* Change E's phi arg in UPDATE_PHI to NEW_DEF, and record information | |
232 | to adjust any debug stmts that referenced the old phi arg, | |
233 | presumably non-loop-closed references left over from other | |
234 | transformations. */ | |
235 | ||
236 | static void | |
237 | adjust_phi_and_debug_stmts (gimple update_phi, edge e, tree new_def) | |
238 | { | |
239 | tree orig_def = PHI_ARG_DEF_FROM_EDGE (update_phi, e); | |
240 | ||
241 | SET_PHI_ARG_DEF (update_phi, e->dest_idx, new_def); | |
242 | ||
243 | if (MAY_HAVE_DEBUG_STMTS) | |
244 | adjust_debug_stmts (orig_def, PHI_RESULT (update_phi), | |
245 | gimple_bb (update_phi)); | |
246 | } | |
247 | ||
ebfd146a | 248 | |
ebfd146a IR |
249 | /* Update PHI nodes for a guard of the LOOP. |
250 | ||
251 | Input: | |
252 | - LOOP, GUARD_EDGE: LOOP is a loop for which we added guard code that | |
253 | controls whether LOOP is to be executed. GUARD_EDGE is the edge that | |
254 | originates from the guard-bb, skips LOOP and reaches the (unique) exit | |
255 | bb of LOOP. This loop-exit-bb is an empty bb with one successor. | |
256 | We denote this bb NEW_MERGE_BB because before the guard code was added | |
257 | it had a single predecessor (the LOOP header), and now it became a merge | |
258 | point of two paths - the path that ends with the LOOP exit-edge, and | |
259 | the path that ends with GUARD_EDGE. | |
260 | - NEW_EXIT_BB: New basic block that is added by this function between LOOP | |
261 | and NEW_MERGE_BB. It is used to place loop-closed-ssa-form exit-phis. | |
262 | ||
263 | ===> The CFG before the guard-code was added: | |
264 | LOOP_header_bb: | |
265 | loop_body | |
266 | if (exit_loop) goto update_bb | |
267 | else goto LOOP_header_bb | |
268 | update_bb: | |
269 | ||
270 | ==> The CFG after the guard-code was added: | |
271 | guard_bb: | |
272 | if (LOOP_guard_condition) goto new_merge_bb | |
273 | else goto LOOP_header_bb | |
274 | LOOP_header_bb: | |
275 | loop_body | |
276 | if (exit_loop_condition) goto new_merge_bb | |
277 | else goto LOOP_header_bb | |
278 | new_merge_bb: | |
279 | goto update_bb | |
280 | update_bb: | |
281 | ||
282 | ==> The CFG after this function: | |
283 | guard_bb: | |
284 | if (LOOP_guard_condition) goto new_merge_bb | |
285 | else goto LOOP_header_bb | |
286 | LOOP_header_bb: | |
287 | loop_body | |
288 | if (exit_loop_condition) goto new_exit_bb | |
289 | else goto LOOP_header_bb | |
290 | new_exit_bb: | |
291 | new_merge_bb: | |
292 | goto update_bb | |
293 | update_bb: | |
294 | ||
295 | This function: | |
296 | 1. creates and updates the relevant phi nodes to account for the new | |
297 | incoming edge (GUARD_EDGE) into NEW_MERGE_BB. This involves: | |
298 | 1.1. Create phi nodes at NEW_MERGE_BB. | |
299 | 1.2. Update the phi nodes at the successor of NEW_MERGE_BB (denoted | |
300 | UPDATE_BB). UPDATE_BB was the exit-bb of LOOP before NEW_MERGE_BB | |
301 | 2. preserves loop-closed-ssa-form by creating the required phi nodes | |
302 | at the exit of LOOP (i.e, in NEW_EXIT_BB). | |
303 | ||
304 | There are two flavors to this function: | |
305 | ||
306 | slpeel_update_phi_nodes_for_guard1: | |
307 | Here the guard controls whether we enter or skip LOOP, where LOOP is a | |
308 | prolog_loop (loop1 below), and the new phis created in NEW_MERGE_BB are | |
309 | for variables that have phis in the loop header. | |
310 | ||
311 | slpeel_update_phi_nodes_for_guard2: | |
312 | Here the guard controls whether we enter or skip LOOP, where LOOP is an | |
313 | epilog_loop (loop2 below), and the new phis created in NEW_MERGE_BB are | |
314 | for variables that have phis in the loop exit. | |
315 | ||
316 | I.E., the overall structure is: | |
317 | ||
318 | loop1_preheader_bb: | |
319 | guard1 (goto loop1/merge1_bb) | |
320 | loop1 | |
321 | loop1_exit_bb: | |
322 | guard2 (goto merge1_bb/merge2_bb) | |
323 | merge1_bb | |
324 | loop2 | |
325 | loop2_exit_bb | |
326 | merge2_bb | |
327 | next_bb | |
328 | ||
329 | slpeel_update_phi_nodes_for_guard1 takes care of creating phis in | |
330 | loop1_exit_bb and merge1_bb. These are entry phis (phis for the vars | |
331 | that have phis in loop1->header). | |
332 | ||
333 | slpeel_update_phi_nodes_for_guard2 takes care of creating phis in | |
334 | loop2_exit_bb and merge2_bb. These are exit phis (phis for the vars | |
335 | that have phis in next_bb). It also adds some of these phis to | |
336 | loop1_exit_bb. | |
337 | ||
338 | slpeel_update_phi_nodes_for_guard1 is always called before | |
339 | slpeel_update_phi_nodes_for_guard2. They are both needed in order | |
340 | to create correct data-flow and loop-closed-ssa-form. | |
341 | ||
342 | Generally slpeel_update_phi_nodes_for_guard1 creates phis for variables | |
343 | that change between iterations of a loop (and therefore have a phi-node | |
344 | at the loop entry), whereas slpeel_update_phi_nodes_for_guard2 creates | |
b8698a0f L |
345 | phis for variables that are used out of the loop (and therefore have |
346 | loop-closed exit phis). Some variables may be both updated between | |
ebfd146a IR |
347 | iterations and used after the loop. This is why in loop1_exit_bb we |
348 | may need both entry_phis (created by slpeel_update_phi_nodes_for_guard1) | |
349 | and exit phis (created by slpeel_update_phi_nodes_for_guard2). | |
350 | ||
351 | - IS_NEW_LOOP: if IS_NEW_LOOP is true, then LOOP is a newly created copy of | |
352 | an original loop. i.e., we have: | |
353 | ||
354 | orig_loop | |
355 | guard_bb (goto LOOP/new_merge) | |
356 | new_loop <-- LOOP | |
357 | new_exit | |
358 | new_merge | |
359 | next_bb | |
360 | ||
361 | If IS_NEW_LOOP is false, then LOOP is an original loop, in which case we | |
362 | have: | |
363 | ||
364 | new_loop | |
365 | guard_bb (goto LOOP/new_merge) | |
366 | orig_loop <-- LOOP | |
367 | new_exit | |
368 | new_merge | |
369 | next_bb | |
370 | ||
371 | The SSA names defined in the original loop have a current | |
372 | reaching definition that that records the corresponding new | |
373 | ssa-name used in the new duplicated loop copy. | |
374 | */ | |
375 | ||
376 | /* Function slpeel_update_phi_nodes_for_guard1 | |
b8698a0f | 377 | |
ebfd146a IR |
378 | Input: |
379 | - GUARD_EDGE, LOOP, IS_NEW_LOOP, NEW_EXIT_BB - as explained above. | |
380 | - DEFS - a bitmap of ssa names to mark new names for which we recorded | |
b8698a0f L |
381 | information. |
382 | ||
ebfd146a IR |
383 | In the context of the overall structure, we have: |
384 | ||
b8698a0f | 385 | loop1_preheader_bb: |
ebfd146a IR |
386 | guard1 (goto loop1/merge1_bb) |
387 | LOOP-> loop1 | |
388 | loop1_exit_bb: | |
389 | guard2 (goto merge1_bb/merge2_bb) | |
390 | merge1_bb | |
391 | loop2 | |
392 | loop2_exit_bb | |
393 | merge2_bb | |
394 | next_bb | |
395 | ||
396 | For each name updated between loop iterations (i.e - for each name that has | |
397 | an entry (loop-header) phi in LOOP) we create a new phi in: | |
398 | 1. merge1_bb (to account for the edge from guard1) | |
399 | 2. loop1_exit_bb (an exit-phi to keep LOOP in loop-closed form) | |
400 | */ | |
401 | ||
402 | static void | |
403 | slpeel_update_phi_nodes_for_guard1 (edge guard_edge, struct loop *loop, | |
c334023f | 404 | bool is_new_loop, basic_block *new_exit_bb) |
ebfd146a | 405 | { |
538dd0b7 DM |
406 | gphi *orig_phi, *new_phi; |
407 | gphi *update_phi, *update_phi2; | |
ebfd146a IR |
408 | tree guard_arg, loop_arg; |
409 | basic_block new_merge_bb = guard_edge->dest; | |
410 | edge e = EDGE_SUCC (new_merge_bb, 0); | |
411 | basic_block update_bb = e->dest; | |
412 | basic_block orig_bb = loop->header; | |
413 | edge new_exit_e; | |
414 | tree current_new_name; | |
538dd0b7 | 415 | gphi_iterator gsi_orig, gsi_update; |
ebfd146a IR |
416 | |
417 | /* Create new bb between loop and new_merge_bb. */ | |
418 | *new_exit_bb = split_edge (single_exit (loop)); | |
419 | ||
420 | new_exit_e = EDGE_SUCC (*new_exit_bb, 0); | |
421 | ||
422 | for (gsi_orig = gsi_start_phis (orig_bb), | |
423 | gsi_update = gsi_start_phis (update_bb); | |
424 | !gsi_end_p (gsi_orig) && !gsi_end_p (gsi_update); | |
425 | gsi_next (&gsi_orig), gsi_next (&gsi_update)) | |
426 | { | |
e20f6b4b | 427 | source_location loop_locus, guard_locus; |
070ecdfd | 428 | tree new_res; |
538dd0b7 DM |
429 | orig_phi = gsi_orig.phi (); |
430 | update_phi = gsi_update.phi (); | |
ebfd146a | 431 | |
ebfd146a IR |
432 | /** 1. Handle new-merge-point phis **/ |
433 | ||
434 | /* 1.1. Generate new phi node in NEW_MERGE_BB: */ | |
070ecdfd RG |
435 | new_res = copy_ssa_name (PHI_RESULT (orig_phi), NULL); |
436 | new_phi = create_phi_node (new_res, new_merge_bb); | |
ebfd146a IR |
437 | |
438 | /* 1.2. NEW_MERGE_BB has two incoming edges: GUARD_EDGE and the exit-edge | |
439 | of LOOP. Set the two phi args in NEW_PHI for these edges: */ | |
440 | loop_arg = PHI_ARG_DEF_FROM_EDGE (orig_phi, EDGE_SUCC (loop->latch, 0)); | |
b8698a0f L |
441 | loop_locus = gimple_phi_arg_location_from_edge (orig_phi, |
442 | EDGE_SUCC (loop->latch, | |
f5045c96 | 443 | 0)); |
ebfd146a | 444 | guard_arg = PHI_ARG_DEF_FROM_EDGE (orig_phi, loop_preheader_edge (loop)); |
b8698a0f L |
445 | guard_locus |
446 | = gimple_phi_arg_location_from_edge (orig_phi, | |
f5045c96 | 447 | loop_preheader_edge (loop)); |
ebfd146a | 448 | |
9e227d60 DC |
449 | add_phi_arg (new_phi, loop_arg, new_exit_e, loop_locus); |
450 | add_phi_arg (new_phi, guard_arg, guard_edge, guard_locus); | |
ebfd146a IR |
451 | |
452 | /* 1.3. Update phi in successor block. */ | |
453 | gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi, e) == loop_arg | |
454 | || PHI_ARG_DEF_FROM_EDGE (update_phi, e) == guard_arg); | |
684f25f4 | 455 | adjust_phi_and_debug_stmts (update_phi, e, PHI_RESULT (new_phi)); |
ebfd146a IR |
456 | update_phi2 = new_phi; |
457 | ||
458 | ||
459 | /** 2. Handle loop-closed-ssa-form phis **/ | |
460 | ||
ea057359 | 461 | if (virtual_operand_p (PHI_RESULT (orig_phi))) |
ebfd146a IR |
462 | continue; |
463 | ||
464 | /* 2.1. Generate new phi node in NEW_EXIT_BB: */ | |
070ecdfd RG |
465 | new_res = copy_ssa_name (PHI_RESULT (orig_phi), NULL); |
466 | new_phi = create_phi_node (new_res, *new_exit_bb); | |
ebfd146a IR |
467 | |
468 | /* 2.2. NEW_EXIT_BB has one incoming edge: the exit-edge of the loop. */ | |
9e227d60 | 469 | add_phi_arg (new_phi, loop_arg, single_exit (loop), loop_locus); |
ebfd146a IR |
470 | |
471 | /* 2.3. Update phi in successor of NEW_EXIT_BB: */ | |
472 | gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi2, new_exit_e) == loop_arg); | |
684f25f4 AO |
473 | adjust_phi_and_debug_stmts (update_phi2, new_exit_e, |
474 | PHI_RESULT (new_phi)); | |
ebfd146a IR |
475 | |
476 | /* 2.4. Record the newly created name with set_current_def. | |
477 | We want to find a name such that | |
478 | name = get_current_def (orig_loop_name) | |
479 | and to set its current definition as follows: | |
480 | set_current_def (name, new_phi_name) | |
481 | ||
482 | If LOOP is a new loop then loop_arg is already the name we're | |
483 | looking for. If LOOP is the original loop, then loop_arg is | |
484 | the orig_loop_name and the relevant name is recorded in its | |
485 | current reaching definition. */ | |
486 | if (is_new_loop) | |
487 | current_new_name = loop_arg; | |
488 | else | |
489 | { | |
490 | current_new_name = get_current_def (loop_arg); | |
491 | /* current_def is not available only if the variable does not | |
492 | change inside the loop, in which case we also don't care | |
493 | about recording a current_def for it because we won't be | |
494 | trying to create loop-exit-phis for it. */ | |
495 | if (!current_new_name) | |
496 | continue; | |
497 | } | |
39719c84 JJ |
498 | tree new_name = get_current_def (current_new_name); |
499 | /* Because of peeled_chrec optimization it is possible that we have | |
500 | set this earlier. Verify the PHI has the same value. */ | |
501 | if (new_name) | |
502 | { | |
503 | gimple phi = SSA_NAME_DEF_STMT (new_name); | |
504 | gcc_assert (gimple_code (phi) == GIMPLE_PHI | |
505 | && gimple_bb (phi) == *new_exit_bb | |
506 | && (PHI_ARG_DEF_FROM_EDGE (phi, single_exit (loop)) | |
507 | == loop_arg)); | |
508 | continue; | |
509 | } | |
ebfd146a IR |
510 | |
511 | set_current_def (current_new_name, PHI_RESULT (new_phi)); | |
ebfd146a IR |
512 | } |
513 | } | |
514 | ||
515 | ||
516 | /* Function slpeel_update_phi_nodes_for_guard2 | |
517 | ||
518 | Input: | |
519 | - GUARD_EDGE, LOOP, IS_NEW_LOOP, NEW_EXIT_BB - as explained above. | |
520 | ||
521 | In the context of the overall structure, we have: | |
522 | ||
b8698a0f | 523 | loop1_preheader_bb: |
ebfd146a IR |
524 | guard1 (goto loop1/merge1_bb) |
525 | loop1 | |
b8698a0f | 526 | loop1_exit_bb: |
ebfd146a IR |
527 | guard2 (goto merge1_bb/merge2_bb) |
528 | merge1_bb | |
529 | LOOP-> loop2 | |
530 | loop2_exit_bb | |
531 | merge2_bb | |
532 | next_bb | |
533 | ||
534 | For each name used out side the loop (i.e - for each name that has an exit | |
535 | phi in next_bb) we create a new phi in: | |
b8698a0f | 536 | 1. merge2_bb (to account for the edge from guard_bb) |
ebfd146a IR |
537 | 2. loop2_exit_bb (an exit-phi to keep LOOP in loop-closed form) |
538 | 3. guard2 bb (an exit phi to keep the preceding loop in loop-closed form), | |
539 | if needed (if it wasn't handled by slpeel_update_phis_nodes_for_phi1). | |
540 | */ | |
541 | ||
542 | static void | |
543 | slpeel_update_phi_nodes_for_guard2 (edge guard_edge, struct loop *loop, | |
544 | bool is_new_loop, basic_block *new_exit_bb) | |
545 | { | |
538dd0b7 DM |
546 | gphi *orig_phi, *new_phi; |
547 | gphi *update_phi, *update_phi2; | |
ebfd146a IR |
548 | tree guard_arg, loop_arg; |
549 | basic_block new_merge_bb = guard_edge->dest; | |
550 | edge e = EDGE_SUCC (new_merge_bb, 0); | |
551 | basic_block update_bb = e->dest; | |
552 | edge new_exit_e; | |
553 | tree orig_def, orig_def_new_name; | |
554 | tree new_name, new_name2; | |
555 | tree arg; | |
538dd0b7 | 556 | gphi_iterator gsi; |
ebfd146a IR |
557 | |
558 | /* Create new bb between loop and new_merge_bb. */ | |
559 | *new_exit_bb = split_edge (single_exit (loop)); | |
560 | ||
561 | new_exit_e = EDGE_SUCC (*new_exit_bb, 0); | |
562 | ||
563 | for (gsi = gsi_start_phis (update_bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
564 | { | |
070ecdfd | 565 | tree new_res; |
538dd0b7 | 566 | update_phi = gsi.phi (); |
ebfd146a IR |
567 | orig_phi = update_phi; |
568 | orig_def = PHI_ARG_DEF_FROM_EDGE (orig_phi, e); | |
569 | /* This loop-closed-phi actually doesn't represent a use | |
b8698a0f | 570 | out of the loop - the phi arg is a constant. */ |
ebfd146a IR |
571 | if (TREE_CODE (orig_def) != SSA_NAME) |
572 | continue; | |
573 | orig_def_new_name = get_current_def (orig_def); | |
574 | arg = NULL_TREE; | |
575 | ||
576 | /** 1. Handle new-merge-point phis **/ | |
577 | ||
578 | /* 1.1. Generate new phi node in NEW_MERGE_BB: */ | |
070ecdfd RG |
579 | new_res = copy_ssa_name (PHI_RESULT (orig_phi), NULL); |
580 | new_phi = create_phi_node (new_res, new_merge_bb); | |
ebfd146a IR |
581 | |
582 | /* 1.2. NEW_MERGE_BB has two incoming edges: GUARD_EDGE and the exit-edge | |
583 | of LOOP. Set the two PHI args in NEW_PHI for these edges: */ | |
584 | new_name = orig_def; | |
585 | new_name2 = NULL_TREE; | |
586 | if (orig_def_new_name) | |
587 | { | |
588 | new_name = orig_def_new_name; | |
589 | /* Some variables have both loop-entry-phis and loop-exit-phis. | |
590 | Such variables were given yet newer names by phis placed in | |
591 | guard_bb by slpeel_update_phi_nodes_for_guard1. I.e: | |
592 | new_name2 = get_current_def (get_current_def (orig_name)). */ | |
593 | new_name2 = get_current_def (new_name); | |
594 | } | |
b8698a0f | 595 | |
ebfd146a IR |
596 | if (is_new_loop) |
597 | { | |
598 | guard_arg = orig_def; | |
599 | loop_arg = new_name; | |
600 | } | |
601 | else | |
602 | { | |
603 | guard_arg = new_name; | |
604 | loop_arg = orig_def; | |
605 | } | |
606 | if (new_name2) | |
607 | guard_arg = new_name2; | |
b8698a0f | 608 | |
9e227d60 DC |
609 | add_phi_arg (new_phi, loop_arg, new_exit_e, UNKNOWN_LOCATION); |
610 | add_phi_arg (new_phi, guard_arg, guard_edge, UNKNOWN_LOCATION); | |
ebfd146a IR |
611 | |
612 | /* 1.3. Update phi in successor block. */ | |
613 | gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi, e) == orig_def); | |
684f25f4 | 614 | adjust_phi_and_debug_stmts (update_phi, e, PHI_RESULT (new_phi)); |
ebfd146a IR |
615 | update_phi2 = new_phi; |
616 | ||
617 | ||
618 | /** 2. Handle loop-closed-ssa-form phis **/ | |
619 | ||
620 | /* 2.1. Generate new phi node in NEW_EXIT_BB: */ | |
070ecdfd RG |
621 | new_res = copy_ssa_name (PHI_RESULT (orig_phi), NULL); |
622 | new_phi = create_phi_node (new_res, *new_exit_bb); | |
ebfd146a IR |
623 | |
624 | /* 2.2. NEW_EXIT_BB has one incoming edge: the exit-edge of the loop. */ | |
9e227d60 | 625 | add_phi_arg (new_phi, loop_arg, single_exit (loop), UNKNOWN_LOCATION); |
ebfd146a IR |
626 | |
627 | /* 2.3. Update phi in successor of NEW_EXIT_BB: */ | |
628 | gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi2, new_exit_e) == loop_arg); | |
684f25f4 AO |
629 | adjust_phi_and_debug_stmts (update_phi2, new_exit_e, |
630 | PHI_RESULT (new_phi)); | |
ebfd146a IR |
631 | |
632 | ||
633 | /** 3. Handle loop-closed-ssa-form phis for first loop **/ | |
634 | ||
635 | /* 3.1. Find the relevant names that need an exit-phi in | |
636 | GUARD_BB, i.e. names for which | |
637 | slpeel_update_phi_nodes_for_guard1 had not already created a | |
638 | phi node. This is the case for names that are used outside | |
639 | the loop (and therefore need an exit phi) but are not updated | |
640 | across loop iterations (and therefore don't have a | |
641 | loop-header-phi). | |
642 | ||
643 | slpeel_update_phi_nodes_for_guard1 is responsible for | |
644 | creating loop-exit phis in GUARD_BB for names that have a | |
645 | loop-header-phi. When such a phi is created we also record | |
646 | the new name in its current definition. If this new name | |
647 | exists, then guard_arg was set to this new name (see 1.2 | |
648 | above). Therefore, if guard_arg is not this new name, this | |
649 | is an indication that an exit-phi in GUARD_BB was not yet | |
650 | created, so we take care of it here. */ | |
651 | if (guard_arg == new_name2) | |
652 | continue; | |
653 | arg = guard_arg; | |
654 | ||
655 | /* 3.2. Generate new phi node in GUARD_BB: */ | |
070ecdfd RG |
656 | new_res = copy_ssa_name (PHI_RESULT (orig_phi), NULL); |
657 | new_phi = create_phi_node (new_res, guard_edge->src); | |
ebfd146a IR |
658 | |
659 | /* 3.3. GUARD_BB has one incoming edge: */ | |
660 | gcc_assert (EDGE_COUNT (guard_edge->src->preds) == 1); | |
b8698a0f | 661 | add_phi_arg (new_phi, arg, EDGE_PRED (guard_edge->src, 0), |
9e227d60 | 662 | UNKNOWN_LOCATION); |
ebfd146a IR |
663 | |
664 | /* 3.4. Update phi in successor of GUARD_BB: */ | |
665 | gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi2, guard_edge) | |
666 | == guard_arg); | |
684f25f4 AO |
667 | adjust_phi_and_debug_stmts (update_phi2, guard_edge, |
668 | PHI_RESULT (new_phi)); | |
ebfd146a IR |
669 | } |
670 | } | |
671 | ||
672 | ||
673 | /* Make the LOOP iterate NITERS times. This is done by adding a new IV | |
674 | that starts at zero, increases by one and its limit is NITERS. | |
675 | ||
676 | Assumption: the exit-condition of LOOP is the last stmt in the loop. */ | |
677 | ||
678 | void | |
679 | slpeel_make_loop_iterate_ntimes (struct loop *loop, tree niters) | |
680 | { | |
681 | tree indx_before_incr, indx_after_incr; | |
538dd0b7 DM |
682 | gcond *cond_stmt; |
683 | gcond *orig_cond; | |
ebfd146a IR |
684 | edge exit_edge = single_exit (loop); |
685 | gimple_stmt_iterator loop_cond_gsi; | |
686 | gimple_stmt_iterator incr_gsi; | |
687 | bool insert_after; | |
688 | tree init = build_int_cst (TREE_TYPE (niters), 0); | |
689 | tree step = build_int_cst (TREE_TYPE (niters), 1); | |
b05e0233 | 690 | source_location loop_loc; |
ebfd146a IR |
691 | enum tree_code code; |
692 | ||
693 | orig_cond = get_loop_exit_condition (loop); | |
694 | gcc_assert (orig_cond); | |
695 | loop_cond_gsi = gsi_for_stmt (orig_cond); | |
696 | ||
697 | standard_iv_increment_position (loop, &incr_gsi, &insert_after); | |
698 | create_iv (init, step, NULL_TREE, loop, | |
699 | &incr_gsi, insert_after, &indx_before_incr, &indx_after_incr); | |
700 | ||
701 | indx_after_incr = force_gimple_operand_gsi (&loop_cond_gsi, indx_after_incr, | |
702 | true, NULL_TREE, true, | |
703 | GSI_SAME_STMT); | |
704 | niters = force_gimple_operand_gsi (&loop_cond_gsi, niters, true, NULL_TREE, | |
705 | true, GSI_SAME_STMT); | |
706 | ||
707 | code = (exit_edge->flags & EDGE_TRUE_VALUE) ? GE_EXPR : LT_EXPR; | |
708 | cond_stmt = gimple_build_cond (code, indx_after_incr, niters, NULL_TREE, | |
709 | NULL_TREE); | |
710 | ||
711 | gsi_insert_before (&loop_cond_gsi, cond_stmt, GSI_SAME_STMT); | |
712 | ||
713 | /* Remove old loop exit test: */ | |
714 | gsi_remove (&loop_cond_gsi, true); | |
6f723d33 | 715 | free_stmt_vec_info (orig_cond); |
ebfd146a IR |
716 | |
717 | loop_loc = find_loop_location (loop); | |
73fbfcad | 718 | if (dump_enabled_p ()) |
ebfd146a | 719 | { |
b05e0233 RB |
720 | if (LOCATION_LOCUS (loop_loc) != UNKNOWN_LOCATION) |
721 | dump_printf (MSG_NOTE, "\nloop at %s:%d: ", LOCATION_FILE (loop_loc), | |
722 | LOCATION_LINE (loop_loc)); | |
78c60e3d | 723 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, cond_stmt, 0); |
e645e942 | 724 | dump_printf (MSG_NOTE, "\n"); |
ebfd146a | 725 | } |
ebfd146a IR |
726 | loop->nb_iterations = niters; |
727 | } | |
728 | ||
5ce9450f JJ |
729 | /* Helper routine of slpeel_tree_duplicate_loop_to_edge_cfg. |
730 | For all PHI arguments in FROM->dest and TO->dest from those | |
731 | edges ensure that TO->dest PHI arguments have current_def | |
732 | to that in from. */ | |
733 | ||
734 | static void | |
735 | slpeel_duplicate_current_defs_from_edges (edge from, edge to) | |
736 | { | |
737 | gimple_stmt_iterator gsi_from, gsi_to; | |
738 | ||
739 | for (gsi_from = gsi_start_phis (from->dest), | |
740 | gsi_to = gsi_start_phis (to->dest); | |
741 | !gsi_end_p (gsi_from) && !gsi_end_p (gsi_to); | |
742 | gsi_next (&gsi_from), gsi_next (&gsi_to)) | |
743 | { | |
744 | gimple from_phi = gsi_stmt (gsi_from); | |
745 | gimple to_phi = gsi_stmt (gsi_to); | |
746 | tree from_arg = PHI_ARG_DEF_FROM_EDGE (from_phi, from); | |
747 | tree to_arg = PHI_ARG_DEF_FROM_EDGE (to_phi, to); | |
748 | if (TREE_CODE (from_arg) == SSA_NAME | |
749 | && TREE_CODE (to_arg) == SSA_NAME | |
750 | && get_current_def (to_arg) == NULL_TREE) | |
751 | set_current_def (to_arg, get_current_def (from_arg)); | |
752 | } | |
753 | } | |
754 | ||
ebfd146a | 755 | |
b8698a0f | 756 | /* Given LOOP this function generates a new copy of it and puts it |
5ce9450f JJ |
757 | on E which is either the entry or exit of LOOP. If SCALAR_LOOP is |
758 | non-NULL, assume LOOP and SCALAR_LOOP are equivalent and copy the | |
759 | basic blocks from SCALAR_LOOP instead of LOOP, but to either the | |
760 | entry or exit of LOOP. */ | |
ebfd146a IR |
761 | |
762 | struct loop * | |
5ce9450f JJ |
763 | slpeel_tree_duplicate_loop_to_edge_cfg (struct loop *loop, |
764 | struct loop *scalar_loop, edge e) | |
ebfd146a IR |
765 | { |
766 | struct loop *new_loop; | |
767 | basic_block *new_bbs, *bbs; | |
768 | bool at_exit; | |
769 | bool was_imm_dom; | |
b8698a0f | 770 | basic_block exit_dest; |
ebfd146a | 771 | edge exit, new_exit; |
ebfd146a | 772 | |
2cfc56b9 RB |
773 | exit = single_exit (loop); |
774 | at_exit = (e == exit); | |
ebfd146a IR |
775 | if (!at_exit && e != loop_preheader_edge (loop)) |
776 | return NULL; | |
777 | ||
5ce9450f JJ |
778 | if (scalar_loop == NULL) |
779 | scalar_loop = loop; | |
780 | ||
781 | bbs = XNEWVEC (basic_block, scalar_loop->num_nodes + 1); | |
782 | get_loop_body_with_size (scalar_loop, bbs, scalar_loop->num_nodes); | |
ebfd146a IR |
783 | |
784 | /* Check whether duplication is possible. */ | |
5ce9450f | 785 | if (!can_copy_bbs_p (bbs, scalar_loop->num_nodes)) |
ebfd146a IR |
786 | { |
787 | free (bbs); | |
788 | return NULL; | |
789 | } | |
790 | ||
791 | /* Generate new loop structure. */ | |
5ce9450f JJ |
792 | new_loop = duplicate_loop (scalar_loop, loop_outer (scalar_loop)); |
793 | duplicate_subloops (scalar_loop, new_loop); | |
ebfd146a | 794 | |
2cfc56b9 | 795 | exit_dest = exit->dest; |
b8698a0f L |
796 | was_imm_dom = (get_immediate_dominator (CDI_DOMINATORS, |
797 | exit_dest) == loop->header ? | |
ebfd146a IR |
798 | true : false); |
799 | ||
2cfc56b9 RB |
800 | /* Also copy the pre-header, this avoids jumping through hoops to |
801 | duplicate the loop entry PHI arguments. Create an empty | |
802 | pre-header unconditionally for this. */ | |
5ce9450f | 803 | basic_block preheader = split_edge (loop_preheader_edge (scalar_loop)); |
2cfc56b9 | 804 | edge entry_e = single_pred_edge (preheader); |
5ce9450f JJ |
805 | bbs[scalar_loop->num_nodes] = preheader; |
806 | new_bbs = XNEWVEC (basic_block, scalar_loop->num_nodes + 1); | |
ebfd146a | 807 | |
5ce9450f JJ |
808 | exit = single_exit (scalar_loop); |
809 | copy_bbs (bbs, scalar_loop->num_nodes + 1, new_bbs, | |
ebfd146a | 810 | &exit, 1, &new_exit, NULL, |
f14540b6 | 811 | e->src, true); |
5ce9450f JJ |
812 | exit = single_exit (loop); |
813 | basic_block new_preheader = new_bbs[scalar_loop->num_nodes]; | |
ebfd146a | 814 | |
5ce9450f JJ |
815 | add_phi_args_after_copy (new_bbs, scalar_loop->num_nodes + 1, NULL); |
816 | ||
817 | if (scalar_loop != loop) | |
818 | { | |
819 | /* If we copied from SCALAR_LOOP rather than LOOP, SSA_NAMEs from | |
820 | SCALAR_LOOP will have current_def set to SSA_NAMEs in the new_loop, | |
821 | but LOOP will not. slpeel_update_phi_nodes_for_guard{1,2} expects | |
822 | the LOOP SSA_NAMEs (on the exit edge and edge from latch to | |
823 | header) to have current_def set, so copy them over. */ | |
824 | slpeel_duplicate_current_defs_from_edges (single_exit (scalar_loop), | |
825 | exit); | |
826 | slpeel_duplicate_current_defs_from_edges (EDGE_SUCC (scalar_loop->latch, | |
827 | 0), | |
828 | EDGE_SUCC (loop->latch, 0)); | |
829 | } | |
b8698a0f | 830 | |
ebfd146a IR |
831 | if (at_exit) /* Add the loop copy at exit. */ |
832 | { | |
5ce9450f JJ |
833 | if (scalar_loop != loop) |
834 | { | |
538dd0b7 | 835 | gphi_iterator gsi; |
5ce9450f JJ |
836 | new_exit = redirect_edge_and_branch (new_exit, exit_dest); |
837 | ||
838 | for (gsi = gsi_start_phis (exit_dest); !gsi_end_p (gsi); | |
839 | gsi_next (&gsi)) | |
840 | { | |
538dd0b7 | 841 | gphi *phi = gsi.phi (); |
5ce9450f JJ |
842 | tree orig_arg = PHI_ARG_DEF_FROM_EDGE (phi, e); |
843 | location_t orig_locus | |
844 | = gimple_phi_arg_location_from_edge (phi, e); | |
845 | ||
846 | add_phi_arg (phi, orig_arg, new_exit, orig_locus); | |
847 | } | |
848 | } | |
2cfc56b9 RB |
849 | redirect_edge_and_branch_force (e, new_preheader); |
850 | flush_pending_stmts (e); | |
851 | set_immediate_dominator (CDI_DOMINATORS, new_preheader, e->src); | |
ebfd146a | 852 | if (was_imm_dom) |
5ce9450f | 853 | set_immediate_dominator (CDI_DOMINATORS, exit_dest, new_exit->src); |
2cfc56b9 RB |
854 | |
855 | /* And remove the non-necessary forwarder again. Keep the other | |
856 | one so we have a proper pre-header for the loop at the exit edge. */ | |
5ce9450f JJ |
857 | redirect_edge_pred (single_succ_edge (preheader), |
858 | single_pred (preheader)); | |
2cfc56b9 | 859 | delete_basic_block (preheader); |
5ce9450f JJ |
860 | set_immediate_dominator (CDI_DOMINATORS, scalar_loop->header, |
861 | loop_preheader_edge (scalar_loop)->src); | |
ebfd146a IR |
862 | } |
863 | else /* Add the copy at entry. */ | |
864 | { | |
5ce9450f JJ |
865 | if (scalar_loop != loop) |
866 | { | |
867 | /* Remove the non-necessary forwarder of scalar_loop again. */ | |
868 | redirect_edge_pred (single_succ_edge (preheader), | |
869 | single_pred (preheader)); | |
870 | delete_basic_block (preheader); | |
871 | set_immediate_dominator (CDI_DOMINATORS, scalar_loop->header, | |
872 | loop_preheader_edge (scalar_loop)->src); | |
873 | preheader = split_edge (loop_preheader_edge (loop)); | |
874 | entry_e = single_pred_edge (preheader); | |
875 | } | |
876 | ||
2cfc56b9 RB |
877 | redirect_edge_and_branch_force (entry_e, new_preheader); |
878 | flush_pending_stmts (entry_e); | |
879 | set_immediate_dominator (CDI_DOMINATORS, new_preheader, entry_e->src); | |
880 | ||
881 | redirect_edge_and_branch_force (new_exit, preheader); | |
882 | flush_pending_stmts (new_exit); | |
883 | set_immediate_dominator (CDI_DOMINATORS, preheader, new_exit->src); | |
884 | ||
885 | /* And remove the non-necessary forwarder again. Keep the other | |
886 | one so we have a proper pre-header for the loop at the exit edge. */ | |
5ce9450f JJ |
887 | redirect_edge_pred (single_succ_edge (new_preheader), |
888 | single_pred (new_preheader)); | |
2cfc56b9 RB |
889 | delete_basic_block (new_preheader); |
890 | set_immediate_dominator (CDI_DOMINATORS, new_loop->header, | |
891 | loop_preheader_edge (new_loop)->src); | |
ebfd146a IR |
892 | } |
893 | ||
5ce9450f | 894 | for (unsigned i = 0; i < scalar_loop->num_nodes + 1; i++) |
2cfc56b9 RB |
895 | rename_variables_in_bb (new_bbs[i]); |
896 | ||
5ce9450f JJ |
897 | if (scalar_loop != loop) |
898 | { | |
899 | /* Update new_loop->header PHIs, so that on the preheader | |
900 | edge they are the ones from loop rather than scalar_loop. */ | |
538dd0b7 | 901 | gphi_iterator gsi_orig, gsi_new; |
5ce9450f JJ |
902 | edge orig_e = loop_preheader_edge (loop); |
903 | edge new_e = loop_preheader_edge (new_loop); | |
904 | ||
905 | for (gsi_orig = gsi_start_phis (loop->header), | |
906 | gsi_new = gsi_start_phis (new_loop->header); | |
907 | !gsi_end_p (gsi_orig) && !gsi_end_p (gsi_new); | |
908 | gsi_next (&gsi_orig), gsi_next (&gsi_new)) | |
909 | { | |
538dd0b7 DM |
910 | gphi *orig_phi = gsi_orig.phi (); |
911 | gphi *new_phi = gsi_new.phi (); | |
5ce9450f JJ |
912 | tree orig_arg = PHI_ARG_DEF_FROM_EDGE (orig_phi, orig_e); |
913 | location_t orig_locus | |
914 | = gimple_phi_arg_location_from_edge (orig_phi, orig_e); | |
915 | ||
916 | add_phi_arg (new_phi, orig_arg, new_e, orig_locus); | |
917 | } | |
918 | } | |
919 | ||
ebfd146a IR |
920 | free (new_bbs); |
921 | free (bbs); | |
922 | ||
2cfc56b9 RB |
923 | #ifdef ENABLE_CHECKING |
924 | verify_dominators (CDI_DOMINATORS); | |
925 | #endif | |
926 | ||
ebfd146a IR |
927 | return new_loop; |
928 | } | |
929 | ||
930 | ||
931 | /* Given the condition statement COND, put it as the last statement | |
932 | of GUARD_BB; EXIT_BB is the basic block to skip the loop; | |
b8698a0f | 933 | Assumes that this is the single exit of the guarded loop. |
86290011 | 934 | Returns the skip edge, inserts new stmts on the COND_EXPR_STMT_LIST. */ |
ebfd146a IR |
935 | |
936 | static edge | |
86290011 RG |
937 | slpeel_add_loop_guard (basic_block guard_bb, tree cond, |
938 | gimple_seq cond_expr_stmt_list, | |
e78410bf JH |
939 | basic_block exit_bb, basic_block dom_bb, |
940 | int probability) | |
ebfd146a IR |
941 | { |
942 | gimple_stmt_iterator gsi; | |
943 | edge new_e, enter_e; | |
538dd0b7 | 944 | gcond *cond_stmt; |
ebfd146a IR |
945 | gimple_seq gimplify_stmt_list = NULL; |
946 | ||
947 | enter_e = EDGE_SUCC (guard_bb, 0); | |
948 | enter_e->flags &= ~EDGE_FALLTHRU; | |
949 | enter_e->flags |= EDGE_FALSE_VALUE; | |
950 | gsi = gsi_last_bb (guard_bb); | |
951 | ||
f7a06a98 RG |
952 | cond = force_gimple_operand_1 (cond, &gimplify_stmt_list, is_gimple_condexpr, |
953 | NULL_TREE); | |
86290011 RG |
954 | if (gimplify_stmt_list) |
955 | gimple_seq_add_seq (&cond_expr_stmt_list, gimplify_stmt_list); | |
f7a06a98 | 956 | cond_stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE); |
86290011 RG |
957 | if (cond_expr_stmt_list) |
958 | gsi_insert_seq_after (&gsi, cond_expr_stmt_list, GSI_NEW_STMT); | |
ebfd146a IR |
959 | |
960 | gsi = gsi_last_bb (guard_bb); | |
961 | gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT); | |
962 | ||
963 | /* Add new edge to connect guard block to the merge/loop-exit block. */ | |
964 | new_e = make_edge (guard_bb, exit_bb, EDGE_TRUE_VALUE); | |
e78410bf JH |
965 | |
966 | new_e->count = guard_bb->count; | |
967 | new_e->probability = probability; | |
968 | new_e->count = apply_probability (enter_e->count, probability); | |
969 | enter_e->count -= new_e->count; | |
970 | enter_e->probability = inverse_probability (probability); | |
ebfd146a IR |
971 | set_immediate_dominator (CDI_DOMINATORS, exit_bb, dom_bb); |
972 | return new_e; | |
973 | } | |
974 | ||
975 | ||
976 | /* This function verifies that the following restrictions apply to LOOP: | |
977 | (1) it is innermost | |
978 | (2) it consists of exactly 2 basic blocks - header, and an empty latch. | |
979 | (3) it is single entry, single exit | |
980 | (4) its exit condition is the last stmt in the header | |
981 | (5) E is the entry/exit edge of LOOP. | |
982 | */ | |
983 | ||
984 | bool | |
985 | slpeel_can_duplicate_loop_p (const struct loop *loop, const_edge e) | |
986 | { | |
987 | edge exit_e = single_exit (loop); | |
988 | edge entry_e = loop_preheader_edge (loop); | |
538dd0b7 | 989 | gcond *orig_cond = get_loop_exit_condition (loop); |
ebfd146a IR |
990 | gimple_stmt_iterator loop_exit_gsi = gsi_last_bb (exit_e->src); |
991 | ||
ebfd146a IR |
992 | if (loop->inner |
993 | /* All loops have an outer scope; the only case loop->outer is NULL is for | |
994 | the function itself. */ | |
995 | || !loop_outer (loop) | |
996 | || loop->num_nodes != 2 | |
997 | || !empty_block_p (loop->latch) | |
998 | || !single_exit (loop) | |
999 | /* Verify that new loop exit condition can be trivially modified. */ | |
1000 | || (!orig_cond || orig_cond != gsi_stmt (loop_exit_gsi)) | |
1001 | || (e != exit_e && e != entry_e)) | |
1002 | return false; | |
1003 | ||
1004 | return true; | |
1005 | } | |
1006 | ||
1007 | #ifdef ENABLE_CHECKING | |
1008 | static void | |
1009 | slpeel_verify_cfg_after_peeling (struct loop *first_loop, | |
1010 | struct loop *second_loop) | |
1011 | { | |
1012 | basic_block loop1_exit_bb = single_exit (first_loop)->dest; | |
1013 | basic_block loop2_entry_bb = loop_preheader_edge (second_loop)->src; | |
1014 | basic_block loop1_entry_bb = loop_preheader_edge (first_loop)->src; | |
1015 | ||
1016 | /* A guard that controls whether the second_loop is to be executed or skipped | |
1017 | is placed in first_loop->exit. first_loop->exit therefore has two | |
1018 | successors - one is the preheader of second_loop, and the other is a bb | |
1019 | after second_loop. | |
1020 | */ | |
1021 | gcc_assert (EDGE_COUNT (loop1_exit_bb->succs) == 2); | |
b8698a0f | 1022 | |
ebfd146a IR |
1023 | /* 1. Verify that one of the successors of first_loop->exit is the preheader |
1024 | of second_loop. */ | |
b8698a0f | 1025 | |
ebfd146a IR |
1026 | /* The preheader of new_loop is expected to have two predecessors: |
1027 | first_loop->exit and the block that precedes first_loop. */ | |
1028 | ||
b8698a0f | 1029 | gcc_assert (EDGE_COUNT (loop2_entry_bb->preds) == 2 |
ebfd146a IR |
1030 | && ((EDGE_PRED (loop2_entry_bb, 0)->src == loop1_exit_bb |
1031 | && EDGE_PRED (loop2_entry_bb, 1)->src == loop1_entry_bb) | |
1032 | || (EDGE_PRED (loop2_entry_bb, 1)->src == loop1_exit_bb | |
1033 | && EDGE_PRED (loop2_entry_bb, 0)->src == loop1_entry_bb))); | |
b8698a0f | 1034 | |
ebfd146a IR |
1035 | /* Verify that the other successor of first_loop->exit is after the |
1036 | second_loop. */ | |
1037 | /* TODO */ | |
1038 | } | |
1039 | #endif | |
1040 | ||
1041 | /* If the run time cost model check determines that vectorization is | |
1042 | not profitable and hence scalar loop should be generated then set | |
1043 | FIRST_NITERS to prologue peeled iterations. This will allow all the | |
1044 | iterations to be executed in the prologue peeled scalar loop. */ | |
1045 | ||
1046 | static void | |
1047 | set_prologue_iterations (basic_block bb_before_first_loop, | |
5d2eb24b | 1048 | tree *first_niters, |
ebfd146a | 1049 | struct loop *loop, |
e78410bf JH |
1050 | unsigned int th, |
1051 | int probability) | |
ebfd146a IR |
1052 | { |
1053 | edge e; | |
1054 | basic_block cond_bb, then_bb; | |
1055 | tree var, prologue_after_cost_adjust_name; | |
1056 | gimple_stmt_iterator gsi; | |
538dd0b7 | 1057 | gphi *newphi; |
ebfd146a | 1058 | edge e_true, e_false, e_fallthru; |
538dd0b7 | 1059 | gcond *cond_stmt; |
f7a06a98 | 1060 | gimple_seq stmts = NULL; |
ebfd146a | 1061 | tree cost_pre_condition = NULL_TREE; |
b8698a0f | 1062 | tree scalar_loop_iters = |
ebfd146a IR |
1063 | unshare_expr (LOOP_VINFO_NITERS_UNCHANGED (loop_vec_info_for_loop (loop))); |
1064 | ||
1065 | e = single_pred_edge (bb_before_first_loop); | |
c3284718 | 1066 | cond_bb = split_edge (e); |
ebfd146a IR |
1067 | |
1068 | e = single_pred_edge (bb_before_first_loop); | |
c3284718 | 1069 | then_bb = split_edge (e); |
ebfd146a IR |
1070 | set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb); |
1071 | ||
1072 | e_false = make_single_succ_edge (cond_bb, bb_before_first_loop, | |
1073 | EDGE_FALSE_VALUE); | |
1074 | set_immediate_dominator (CDI_DOMINATORS, bb_before_first_loop, cond_bb); | |
1075 | ||
1076 | e_true = EDGE_PRED (then_bb, 0); | |
1077 | e_true->flags &= ~EDGE_FALLTHRU; | |
1078 | e_true->flags |= EDGE_TRUE_VALUE; | |
1079 | ||
e78410bf JH |
1080 | e_true->probability = probability; |
1081 | e_false->probability = inverse_probability (probability); | |
1082 | e_true->count = apply_probability (cond_bb->count, probability); | |
1083 | e_false->count = cond_bb->count - e_true->count; | |
1084 | then_bb->frequency = EDGE_FREQUENCY (e_true); | |
1085 | then_bb->count = e_true->count; | |
1086 | ||
ebfd146a | 1087 | e_fallthru = EDGE_SUCC (then_bb, 0); |
e78410bf | 1088 | e_fallthru->count = then_bb->count; |
ebfd146a | 1089 | |
f7a06a98 | 1090 | gsi = gsi_last_bb (cond_bb); |
ebfd146a | 1091 | cost_pre_condition = |
b8698a0f | 1092 | fold_build2 (LE_EXPR, boolean_type_node, scalar_loop_iters, |
ebfd146a IR |
1093 | build_int_cst (TREE_TYPE (scalar_loop_iters), th)); |
1094 | cost_pre_condition = | |
f7a06a98 RG |
1095 | force_gimple_operand_gsi_1 (&gsi, cost_pre_condition, is_gimple_condexpr, |
1096 | NULL_TREE, false, GSI_CONTINUE_LINKING); | |
1097 | cond_stmt = gimple_build_cond_from_tree (cost_pre_condition, | |
1098 | NULL_TREE, NULL_TREE); | |
ebfd146a | 1099 | gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT); |
b8698a0f | 1100 | |
ebfd146a IR |
1101 | var = create_tmp_var (TREE_TYPE (scalar_loop_iters), |
1102 | "prologue_after_cost_adjust"); | |
b8698a0f | 1103 | prologue_after_cost_adjust_name = |
ebfd146a IR |
1104 | force_gimple_operand (scalar_loop_iters, &stmts, false, var); |
1105 | ||
1106 | gsi = gsi_last_bb (then_bb); | |
1107 | if (stmts) | |
1108 | gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT); | |
1109 | ||
1110 | newphi = create_phi_node (var, bb_before_first_loop); | |
b8698a0f | 1111 | add_phi_arg (newphi, prologue_after_cost_adjust_name, e_fallthru, |
9e227d60 DC |
1112 | UNKNOWN_LOCATION); |
1113 | add_phi_arg (newphi, *first_niters, e_false, UNKNOWN_LOCATION); | |
ebfd146a | 1114 | |
5d2eb24b | 1115 | *first_niters = PHI_RESULT (newphi); |
ebfd146a IR |
1116 | } |
1117 | ||
ebfd146a IR |
1118 | /* Function slpeel_tree_peel_loop_to_edge. |
1119 | ||
1120 | Peel the first (last) iterations of LOOP into a new prolog (epilog) loop | |
1121 | that is placed on the entry (exit) edge E of LOOP. After this transformation | |
1122 | we have two loops one after the other - first-loop iterates FIRST_NITERS | |
1123 | times, and second-loop iterates the remainder NITERS - FIRST_NITERS times. | |
b8698a0f | 1124 | If the cost model indicates that it is profitable to emit a scalar |
ebfd146a IR |
1125 | loop instead of the vector one, then the prolog (epilog) loop will iterate |
1126 | for the entire unchanged scalar iterations of the loop. | |
1127 | ||
1128 | Input: | |
1129 | - LOOP: the loop to be peeled. | |
5ce9450f JJ |
1130 | - SCALAR_LOOP: if non-NULL, the alternate loop from which basic blocks |
1131 | should be copied. | |
ebfd146a IR |
1132 | - E: the exit or entry edge of LOOP. |
1133 | If it is the entry edge, we peel the first iterations of LOOP. In this | |
1134 | case first-loop is LOOP, and second-loop is the newly created loop. | |
1135 | If it is the exit edge, we peel the last iterations of LOOP. In this | |
1136 | case, first-loop is the newly created loop, and second-loop is LOOP. | |
1137 | - NITERS: the number of iterations that LOOP iterates. | |
1138 | - FIRST_NITERS: the number of iterations that the first-loop should iterate. | |
1139 | - UPDATE_FIRST_LOOP_COUNT: specified whether this function is responsible | |
1140 | for updating the loop bound of the first-loop to FIRST_NITERS. If it | |
1141 | is false, the caller of this function may want to take care of this | |
1142 | (this can be useful if we don't want new stmts added to first-loop). | |
1143 | - TH: cost model profitability threshold of iterations for vectorization. | |
1144 | - CHECK_PROFITABILITY: specify whether cost model check has not occurred | |
1145 | during versioning and hence needs to occur during | |
b8698a0f | 1146 | prologue generation or whether cost model check |
ebfd146a IR |
1147 | has not occurred during prologue generation and hence |
1148 | needs to occur during epilogue generation. | |
e78410bf JH |
1149 | - BOUND1 is the upper bound on number of iterations of the first loop (if known) |
1150 | - BOUND2 is the upper bound on number of iterations of the second loop (if known) | |
b8698a0f | 1151 | |
ebfd146a IR |
1152 | |
1153 | Output: | |
1154 | The function returns a pointer to the new loop-copy, or NULL if it failed | |
1155 | to perform the transformation. | |
1156 | ||
1157 | The function generates two if-then-else guards: one before the first loop, | |
1158 | and the other before the second loop: | |
1159 | The first guard is: | |
1160 | if (FIRST_NITERS == 0) then skip the first loop, | |
1161 | and go directly to the second loop. | |
1162 | The second guard is: | |
1163 | if (FIRST_NITERS == NITERS) then skip the second loop. | |
1164 | ||
86290011 RG |
1165 | If the optional COND_EXPR and COND_EXPR_STMT_LIST arguments are given |
1166 | then the generated condition is combined with COND_EXPR and the | |
1167 | statements in COND_EXPR_STMT_LIST are emitted together with it. | |
1168 | ||
ebfd146a IR |
1169 | FORNOW only simple loops are supported (see slpeel_can_duplicate_loop_p). |
1170 | FORNOW the resulting code will not be in loop-closed-ssa form. | |
1171 | */ | |
1172 | ||
5ce9450f JJ |
1173 | static struct loop * |
1174 | slpeel_tree_peel_loop_to_edge (struct loop *loop, struct loop *scalar_loop, | |
5d2eb24b | 1175 | edge e, tree *first_niters, |
ebfd146a | 1176 | tree niters, bool update_first_loop_count, |
86290011 | 1177 | unsigned int th, bool check_profitability, |
e78410bf JH |
1178 | tree cond_expr, gimple_seq cond_expr_stmt_list, |
1179 | int bound1, int bound2) | |
ebfd146a IR |
1180 | { |
1181 | struct loop *new_loop = NULL, *first_loop, *second_loop; | |
1182 | edge skip_e; | |
1183 | tree pre_condition = NULL_TREE; | |
ebfd146a IR |
1184 | basic_block bb_before_second_loop, bb_after_second_loop; |
1185 | basic_block bb_before_first_loop; | |
1186 | basic_block bb_between_loops; | |
1187 | basic_block new_exit_bb; | |
538dd0b7 | 1188 | gphi_iterator gsi; |
ebfd146a | 1189 | edge exit_e = single_exit (loop); |
b05e0233 | 1190 | source_location loop_loc; |
e78410bf JH |
1191 | /* There are many aspects to how likely the first loop is going to be executed. |
1192 | Without histogram we can't really do good job. Simply set it to | |
1193 | 2/3, so the first loop is not reordered to the end of function and | |
1194 | the hot path through stays short. */ | |
1195 | int first_guard_probability = 2 * REG_BR_PROB_BASE / 3; | |
1196 | int second_guard_probability = 2 * REG_BR_PROB_BASE / 3; | |
1197 | int probability_of_second_loop; | |
b8698a0f | 1198 | |
ebfd146a IR |
1199 | if (!slpeel_can_duplicate_loop_p (loop, e)) |
1200 | return NULL; | |
b8698a0f | 1201 | |
141310ef RB |
1202 | /* We might have a queued need to update virtual SSA form. As we |
1203 | delete the update SSA machinery below after doing a regular | |
1204 | incremental SSA update during loop copying make sure we don't | |
1205 | lose that fact. | |
1206 | ??? Needing to update virtual SSA form by renaming is unfortunate | |
1207 | but not all of the vectorizer code inserting new loads / stores | |
1208 | properly assigns virtual operands to those statements. */ | |
1209 | update_ssa (TODO_update_ssa_only_virtuals); | |
1210 | ||
e20f6b4b JJ |
1211 | /* If the loop has a virtual PHI, but exit bb doesn't, create a virtual PHI |
1212 | in the exit bb and rename all the uses after the loop. This simplifies | |
1213 | the *guard[12] routines, which assume loop closed SSA form for all PHIs | |
1214 | (but normally loop closed SSA form doesn't require virtual PHIs to be | |
1215 | in the same form). Doing this early simplifies the checking what | |
1216 | uses should be renamed. */ | |
1217 | for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi)) | |
ea057359 | 1218 | if (virtual_operand_p (gimple_phi_result (gsi_stmt (gsi)))) |
e20f6b4b | 1219 | { |
538dd0b7 | 1220 | gphi *phi = gsi.phi (); |
e20f6b4b JJ |
1221 | for (gsi = gsi_start_phis (exit_e->dest); |
1222 | !gsi_end_p (gsi); gsi_next (&gsi)) | |
ea057359 | 1223 | if (virtual_operand_p (gimple_phi_result (gsi_stmt (gsi)))) |
e20f6b4b JJ |
1224 | break; |
1225 | if (gsi_end_p (gsi)) | |
1226 | { | |
070ecdfd | 1227 | tree new_vop = copy_ssa_name (PHI_RESULT (phi), NULL); |
538dd0b7 | 1228 | gphi *new_phi = create_phi_node (new_vop, exit_e->dest); |
e20f6b4b JJ |
1229 | tree vop = PHI_ARG_DEF_FROM_EDGE (phi, EDGE_SUCC (loop->latch, 0)); |
1230 | imm_use_iterator imm_iter; | |
1231 | gimple stmt; | |
e20f6b4b JJ |
1232 | use_operand_p use_p; |
1233 | ||
9e227d60 | 1234 | add_phi_arg (new_phi, vop, exit_e, UNKNOWN_LOCATION); |
e20f6b4b JJ |
1235 | gimple_phi_set_result (new_phi, new_vop); |
1236 | FOR_EACH_IMM_USE_STMT (stmt, imm_iter, vop) | |
1237 | if (stmt != new_phi && gimple_bb (stmt) != loop->header) | |
1238 | FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter) | |
1239 | SET_USE (use_p, new_vop); | |
1240 | } | |
1241 | break; | |
1242 | } | |
ebfd146a IR |
1243 | |
1244 | /* 1. Generate a copy of LOOP and put it on E (E is the entry/exit of LOOP). | |
1245 | Resulting CFG would be: | |
1246 | ||
1247 | first_loop: | |
1248 | do { | |
1249 | } while ... | |
1250 | ||
1251 | second_loop: | |
1252 | do { | |
1253 | } while ... | |
1254 | ||
1255 | orig_exit_bb: | |
1256 | */ | |
b8698a0f | 1257 | |
5ce9450f JJ |
1258 | if (!(new_loop = slpeel_tree_duplicate_loop_to_edge_cfg (loop, scalar_loop, |
1259 | e))) | |
ebfd146a IR |
1260 | { |
1261 | loop_loc = find_loop_location (loop); | |
78c60e3d SS |
1262 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, loop_loc, |
1263 | "tree_duplicate_loop_to_edge_cfg failed.\n"); | |
ebfd146a IR |
1264 | return NULL; |
1265 | } | |
b8698a0f | 1266 | |
684f25f4 AO |
1267 | if (MAY_HAVE_DEBUG_STMTS) |
1268 | { | |
9771b263 | 1269 | gcc_assert (!adjust_vec.exists ()); |
ff4c81cc | 1270 | adjust_vec.create (32); |
684f25f4 AO |
1271 | } |
1272 | ||
ebfd146a IR |
1273 | if (e == exit_e) |
1274 | { | |
1275 | /* NEW_LOOP was placed after LOOP. */ | |
1276 | first_loop = loop; | |
1277 | second_loop = new_loop; | |
1278 | } | |
1279 | else | |
1280 | { | |
1281 | /* NEW_LOOP was placed before LOOP. */ | |
1282 | first_loop = new_loop; | |
1283 | second_loop = loop; | |
1284 | } | |
1285 | ||
ebfd146a IR |
1286 | /* 2. Add the guard code in one of the following ways: |
1287 | ||
1288 | 2.a Add the guard that controls whether the first loop is executed. | |
1289 | This occurs when this function is invoked for prologue or epilogue | |
1290 | generation and when the cost model check can be done at compile time. | |
1291 | ||
1292 | Resulting CFG would be: | |
1293 | ||
1294 | bb_before_first_loop: | |
1295 | if (FIRST_NITERS == 0) GOTO bb_before_second_loop | |
1296 | GOTO first-loop | |
1297 | ||
1298 | first_loop: | |
1299 | do { | |
1300 | } while ... | |
1301 | ||
1302 | bb_before_second_loop: | |
1303 | ||
1304 | second_loop: | |
1305 | do { | |
1306 | } while ... | |
1307 | ||
1308 | orig_exit_bb: | |
1309 | ||
1310 | 2.b Add the cost model check that allows the prologue | |
1311 | to iterate for the entire unchanged scalar | |
1312 | iterations of the loop in the event that the cost | |
1313 | model indicates that the scalar loop is more | |
1314 | profitable than the vector one. This occurs when | |
1315 | this function is invoked for prologue generation | |
1316 | and the cost model check needs to be done at run | |
1317 | time. | |
1318 | ||
1319 | Resulting CFG after prologue peeling would be: | |
1320 | ||
1321 | if (scalar_loop_iterations <= th) | |
1322 | FIRST_NITERS = scalar_loop_iterations | |
1323 | ||
1324 | bb_before_first_loop: | |
1325 | if (FIRST_NITERS == 0) GOTO bb_before_second_loop | |
1326 | GOTO first-loop | |
1327 | ||
1328 | first_loop: | |
1329 | do { | |
1330 | } while ... | |
1331 | ||
1332 | bb_before_second_loop: | |
1333 | ||
1334 | second_loop: | |
1335 | do { | |
1336 | } while ... | |
1337 | ||
1338 | orig_exit_bb: | |
1339 | ||
1340 | 2.c Add the cost model check that allows the epilogue | |
1341 | to iterate for the entire unchanged scalar | |
1342 | iterations of the loop in the event that the cost | |
1343 | model indicates that the scalar loop is more | |
1344 | profitable than the vector one. This occurs when | |
1345 | this function is invoked for epilogue generation | |
1346 | and the cost model check needs to be done at run | |
86290011 RG |
1347 | time. This check is combined with any pre-existing |
1348 | check in COND_EXPR to avoid versioning. | |
ebfd146a IR |
1349 | |
1350 | Resulting CFG after prologue peeling would be: | |
1351 | ||
1352 | bb_before_first_loop: | |
1353 | if ((scalar_loop_iterations <= th) | |
1354 | || | |
1355 | FIRST_NITERS == 0) GOTO bb_before_second_loop | |
1356 | GOTO first-loop | |
1357 | ||
1358 | first_loop: | |
1359 | do { | |
1360 | } while ... | |
1361 | ||
1362 | bb_before_second_loop: | |
1363 | ||
1364 | second_loop: | |
1365 | do { | |
1366 | } while ... | |
1367 | ||
1368 | orig_exit_bb: | |
1369 | */ | |
1370 | ||
1371 | bb_before_first_loop = split_edge (loop_preheader_edge (first_loop)); | |
2cfc56b9 RB |
1372 | /* Loop copying insterted a forwarder block for us here. */ |
1373 | bb_before_second_loop = single_exit (first_loop)->dest; | |
ebfd146a | 1374 | |
e78410bf JH |
1375 | probability_of_second_loop = (inverse_probability (first_guard_probability) |
1376 | + combine_probabilities (second_guard_probability, | |
1377 | first_guard_probability)); | |
1378 | /* Theoretically preheader edge of first loop and exit edge should have | |
1379 | same frequencies. Loop exit probablities are however easy to get wrong. | |
1380 | It is safer to copy value from original loop entry. */ | |
1381 | bb_before_second_loop->frequency | |
8ddb5a29 TJ |
1382 | = combine_probabilities (bb_before_first_loop->frequency, |
1383 | probability_of_second_loop); | |
e78410bf JH |
1384 | bb_before_second_loop->count |
1385 | = apply_probability (bb_before_first_loop->count, | |
1386 | probability_of_second_loop); | |
1387 | single_succ_edge (bb_before_second_loop)->count | |
1388 | = bb_before_second_loop->count; | |
1389 | ||
ebfd146a IR |
1390 | /* Epilogue peeling. */ |
1391 | if (!update_first_loop_count) | |
1392 | { | |
95b3eff3 RB |
1393 | loop_vec_info loop_vinfo = loop_vec_info_for_loop (loop); |
1394 | tree scalar_loop_iters = LOOP_VINFO_NITERSM1 (loop_vinfo); | |
1395 | unsigned limit = LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1; | |
1396 | if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo)) | |
1397 | limit = limit + 1; | |
1398 | if (check_profitability | |
1399 | && th > limit) | |
1400 | limit = th; | |
ebfd146a | 1401 | pre_condition = |
95b3eff3 RB |
1402 | fold_build2 (LT_EXPR, boolean_type_node, scalar_loop_iters, |
1403 | build_int_cst (TREE_TYPE (scalar_loop_iters), limit)); | |
86290011 RG |
1404 | if (cond_expr) |
1405 | { | |
1406 | pre_condition = | |
1407 | fold_build2 (TRUTH_OR_EXPR, boolean_type_node, | |
1408 | pre_condition, | |
1409 | fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, | |
1410 | cond_expr)); | |
1411 | } | |
ebfd146a IR |
1412 | } |
1413 | ||
b8698a0f | 1414 | /* Prologue peeling. */ |
ebfd146a IR |
1415 | else |
1416 | { | |
1417 | if (check_profitability) | |
1418 | set_prologue_iterations (bb_before_first_loop, first_niters, | |
e78410bf | 1419 | loop, th, first_guard_probability); |
ebfd146a IR |
1420 | |
1421 | pre_condition = | |
5d2eb24b IR |
1422 | fold_build2 (LE_EXPR, boolean_type_node, *first_niters, |
1423 | build_int_cst (TREE_TYPE (*first_niters), 0)); | |
ebfd146a IR |
1424 | } |
1425 | ||
1426 | skip_e = slpeel_add_loop_guard (bb_before_first_loop, pre_condition, | |
86290011 | 1427 | cond_expr_stmt_list, |
e78410bf JH |
1428 | bb_before_second_loop, bb_before_first_loop, |
1429 | inverse_probability (first_guard_probability)); | |
1430 | scale_loop_profile (first_loop, first_guard_probability, | |
1431 | check_profitability && (int)th > bound1 ? th : bound1); | |
ebfd146a IR |
1432 | slpeel_update_phi_nodes_for_guard1 (skip_e, first_loop, |
1433 | first_loop == new_loop, | |
c334023f | 1434 | &new_exit_bb); |
ebfd146a IR |
1435 | |
1436 | ||
1437 | /* 3. Add the guard that controls whether the second loop is executed. | |
1438 | Resulting CFG would be: | |
1439 | ||
1440 | bb_before_first_loop: | |
1441 | if (FIRST_NITERS == 0) GOTO bb_before_second_loop (skip first loop) | |
1442 | GOTO first-loop | |
1443 | ||
1444 | first_loop: | |
1445 | do { | |
1446 | } while ... | |
1447 | ||
1448 | bb_between_loops: | |
1449 | if (FIRST_NITERS == NITERS) GOTO bb_after_second_loop (skip second loop) | |
1450 | GOTO bb_before_second_loop | |
1451 | ||
1452 | bb_before_second_loop: | |
1453 | ||
1454 | second_loop: | |
1455 | do { | |
1456 | } while ... | |
1457 | ||
1458 | bb_after_second_loop: | |
1459 | ||
1460 | orig_exit_bb: | |
1461 | */ | |
1462 | ||
1463 | bb_between_loops = new_exit_bb; | |
1464 | bb_after_second_loop = split_edge (single_exit (second_loop)); | |
1465 | ||
b8698a0f | 1466 | pre_condition = |
5d2eb24b | 1467 | fold_build2 (EQ_EXPR, boolean_type_node, *first_niters, niters); |
86290011 | 1468 | skip_e = slpeel_add_loop_guard (bb_between_loops, pre_condition, NULL, |
e78410bf JH |
1469 | bb_after_second_loop, bb_before_first_loop, |
1470 | inverse_probability (second_guard_probability)); | |
1471 | scale_loop_profile (second_loop, probability_of_second_loop, bound2); | |
ebfd146a IR |
1472 | slpeel_update_phi_nodes_for_guard2 (skip_e, second_loop, |
1473 | second_loop == new_loop, &new_exit_bb); | |
1474 | ||
1475 | /* 4. Make first-loop iterate FIRST_NITERS times, if requested. | |
1476 | */ | |
1477 | if (update_first_loop_count) | |
5d2eb24b | 1478 | slpeel_make_loop_iterate_ntimes (first_loop, *first_niters); |
ebfd146a | 1479 | |
040d39ee RG |
1480 | delete_update_ssa (); |
1481 | ||
684f25f4 AO |
1482 | adjust_vec_debug_stmts (); |
1483 | ||
ebfd146a IR |
1484 | return new_loop; |
1485 | } | |
1486 | ||
1487 | /* Function vect_get_loop_location. | |
1488 | ||
1489 | Extract the location of the loop in the source code. | |
1490 | If the loop is not well formed for vectorization, an estimated | |
1491 | location is calculated. | |
1492 | Return the loop location if succeed and NULL if not. */ | |
1493 | ||
b05e0233 | 1494 | source_location |
ebfd146a IR |
1495 | find_loop_location (struct loop *loop) |
1496 | { | |
1497 | gimple stmt = NULL; | |
1498 | basic_block bb; | |
1499 | gimple_stmt_iterator si; | |
1500 | ||
1501 | if (!loop) | |
b05e0233 | 1502 | return UNKNOWN_LOCATION; |
ebfd146a IR |
1503 | |
1504 | stmt = get_loop_exit_condition (loop); | |
1505 | ||
502498d5 JJ |
1506 | if (stmt |
1507 | && LOCATION_LOCUS (gimple_location (stmt)) > BUILTINS_LOCATION) | |
ebfd146a IR |
1508 | return gimple_location (stmt); |
1509 | ||
1510 | /* If we got here the loop is probably not "well formed", | |
1511 | try to estimate the loop location */ | |
1512 | ||
1513 | if (!loop->header) | |
b05e0233 | 1514 | return UNKNOWN_LOCATION; |
ebfd146a IR |
1515 | |
1516 | bb = loop->header; | |
1517 | ||
1518 | for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) | |
1519 | { | |
1520 | stmt = gsi_stmt (si); | |
502498d5 | 1521 | if (LOCATION_LOCUS (gimple_location (stmt)) > BUILTINS_LOCATION) |
ebfd146a IR |
1522 | return gimple_location (stmt); |
1523 | } | |
1524 | ||
b05e0233 | 1525 | return UNKNOWN_LOCATION; |
ebfd146a IR |
1526 | } |
1527 | ||
1528 | ||
ebfd146a IR |
1529 | /* Function vect_can_advance_ivs_p |
1530 | ||
b8698a0f L |
1531 | In case the number of iterations that LOOP iterates is unknown at compile |
1532 | time, an epilog loop will be generated, and the loop induction variables | |
1533 | (IVs) will be "advanced" to the value they are supposed to take just before | |
ebfd146a IR |
1534 | the epilog loop. Here we check that the access function of the loop IVs |
1535 | and the expression that represents the loop bound are simple enough. | |
1536 | These restrictions will be relaxed in the future. */ | |
1537 | ||
b8698a0f | 1538 | bool |
ebfd146a IR |
1539 | vect_can_advance_ivs_p (loop_vec_info loop_vinfo) |
1540 | { | |
1541 | struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo); | |
1542 | basic_block bb = loop->header; | |
1543 | gimple phi; | |
538dd0b7 | 1544 | gphi_iterator gsi; |
ebfd146a IR |
1545 | |
1546 | /* Analyze phi functions of the loop header. */ | |
1547 | ||
73fbfcad | 1548 | if (dump_enabled_p ()) |
e645e942 | 1549 | dump_printf_loc (MSG_NOTE, vect_location, "vect_can_advance_ivs_p:\n"); |
ebfd146a IR |
1550 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
1551 | { | |
ebfd146a IR |
1552 | tree evolution_part; |
1553 | ||
538dd0b7 | 1554 | phi = gsi.phi (); |
73fbfcad | 1555 | if (dump_enabled_p ()) |
ebfd146a | 1556 | { |
78c60e3d SS |
1557 | dump_printf_loc (MSG_NOTE, vect_location, "Analyze phi: "); |
1558 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0); | |
e645e942 | 1559 | dump_printf (MSG_NOTE, "\n"); |
ebfd146a IR |
1560 | } |
1561 | ||
1562 | /* Skip virtual phi's. The data dependences that are associated with | |
1563 | virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */ | |
1564 | ||
ea057359 | 1565 | if (virtual_operand_p (PHI_RESULT (phi))) |
ebfd146a | 1566 | { |
73fbfcad | 1567 | if (dump_enabled_p ()) |
78c60e3d | 1568 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
e645e942 | 1569 | "virtual phi. skip.\n"); |
ebfd146a IR |
1570 | continue; |
1571 | } | |
1572 | ||
1573 | /* Skip reduction phis. */ | |
1574 | ||
1575 | if (STMT_VINFO_DEF_TYPE (vinfo_for_stmt (phi)) == vect_reduction_def) | |
1576 | { | |
73fbfcad | 1577 | if (dump_enabled_p ()) |
78c60e3d | 1578 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
e645e942 | 1579 | "reduc phi. skip.\n"); |
ebfd146a IR |
1580 | continue; |
1581 | } | |
1582 | ||
1583 | /* Analyze the evolution function. */ | |
1584 | ||
afb119be RB |
1585 | evolution_part |
1586 | = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (vinfo_for_stmt (phi)); | |
ebfd146a IR |
1587 | if (evolution_part == NULL_TREE) |
1588 | { | |
73fbfcad | 1589 | if (dump_enabled_p ()) |
afb119be | 1590 | dump_printf (MSG_MISSED_OPTIMIZATION, |
e645e942 | 1591 | "No access function or evolution.\n"); |
ebfd146a IR |
1592 | return false; |
1593 | } | |
b8698a0f L |
1594 | |
1595 | /* FORNOW: We do not transform initial conditions of IVs | |
ebfd146a IR |
1596 | which evolution functions are a polynomial of degree >= 2. */ |
1597 | ||
1598 | if (tree_is_chrec (evolution_part)) | |
b8698a0f | 1599 | return false; |
ebfd146a IR |
1600 | } |
1601 | ||
1602 | return true; | |
1603 | } | |
1604 | ||
1605 | ||
1606 | /* Function vect_update_ivs_after_vectorizer. | |
1607 | ||
1608 | "Advance" the induction variables of LOOP to the value they should take | |
1609 | after the execution of LOOP. This is currently necessary because the | |
1610 | vectorizer does not handle induction variables that are used after the | |
1611 | loop. Such a situation occurs when the last iterations of LOOP are | |
1612 | peeled, because: | |
1613 | 1. We introduced new uses after LOOP for IVs that were not originally used | |
1614 | after LOOP: the IVs of LOOP are now used by an epilog loop. | |
1615 | 2. LOOP is going to be vectorized; this means that it will iterate N/VF | |
1616 | times, whereas the loop IVs should be bumped N times. | |
1617 | ||
1618 | Input: | |
1619 | - LOOP - a loop that is going to be vectorized. The last few iterations | |
1620 | of LOOP were peeled. | |
1621 | - NITERS - the number of iterations that LOOP executes (before it is | |
1622 | vectorized). i.e, the number of times the ivs should be bumped. | |
1623 | - UPDATE_E - a successor edge of LOOP->exit that is on the (only) path | |
1624 | coming out from LOOP on which there are uses of the LOOP ivs | |
1625 | (this is the path from LOOP->exit to epilog_loop->preheader). | |
1626 | ||
1627 | The new definitions of the ivs are placed in LOOP->exit. | |
1628 | The phi args associated with the edge UPDATE_E in the bb | |
1629 | UPDATE_E->dest are updated accordingly. | |
1630 | ||
1631 | Assumption 1: Like the rest of the vectorizer, this function assumes | |
1632 | a single loop exit that has a single predecessor. | |
1633 | ||
1634 | Assumption 2: The phi nodes in the LOOP header and in update_bb are | |
1635 | organized in the same order. | |
1636 | ||
1637 | Assumption 3: The access function of the ivs is simple enough (see | |
1638 | vect_can_advance_ivs_p). This assumption will be relaxed in the future. | |
1639 | ||
1640 | Assumption 4: Exactly one of the successors of LOOP exit-bb is on a path | |
b8698a0f | 1641 | coming out of LOOP on which the ivs of LOOP are used (this is the path |
ebfd146a IR |
1642 | that leads to the epilog loop; other paths skip the epilog loop). This |
1643 | path starts with the edge UPDATE_E, and its destination (denoted update_bb) | |
1644 | needs to have its phis updated. | |
1645 | */ | |
1646 | ||
1647 | static void | |
b8698a0f | 1648 | vect_update_ivs_after_vectorizer (loop_vec_info loop_vinfo, tree niters, |
ebfd146a IR |
1649 | edge update_e) |
1650 | { | |
1651 | struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo); | |
1652 | basic_block exit_bb = single_exit (loop)->dest; | |
538dd0b7 DM |
1653 | gphi *phi, *phi1; |
1654 | gphi_iterator gsi, gsi1; | |
ebfd146a IR |
1655 | basic_block update_bb = update_e->dest; |
1656 | ||
47c32082 | 1657 | gcc_checking_assert (vect_can_advance_ivs_p (loop_vinfo)); |
ebfd146a IR |
1658 | |
1659 | /* Make sure there exists a single-predecessor exit bb: */ | |
1660 | gcc_assert (single_pred_p (exit_bb)); | |
1661 | ||
1662 | for (gsi = gsi_start_phis (loop->header), gsi1 = gsi_start_phis (update_bb); | |
1663 | !gsi_end_p (gsi) && !gsi_end_p (gsi1); | |
1664 | gsi_next (&gsi), gsi_next (&gsi1)) | |
1665 | { | |
ebfd146a | 1666 | tree init_expr; |
550918ca RG |
1667 | tree step_expr, off; |
1668 | tree type; | |
ebfd146a IR |
1669 | tree var, ni, ni_name; |
1670 | gimple_stmt_iterator last_gsi; | |
0ac168a1 | 1671 | stmt_vec_info stmt_info; |
ebfd146a | 1672 | |
538dd0b7 DM |
1673 | phi = gsi.phi (); |
1674 | phi1 = gsi1.phi (); | |
73fbfcad | 1675 | if (dump_enabled_p ()) |
ebfd146a | 1676 | { |
78c60e3d SS |
1677 | dump_printf_loc (MSG_NOTE, vect_location, |
1678 | "vect_update_ivs_after_vectorizer: phi: "); | |
1679 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0); | |
e645e942 | 1680 | dump_printf (MSG_NOTE, "\n"); |
ebfd146a IR |
1681 | } |
1682 | ||
1683 | /* Skip virtual phi's. */ | |
ea057359 | 1684 | if (virtual_operand_p (PHI_RESULT (phi))) |
ebfd146a | 1685 | { |
73fbfcad | 1686 | if (dump_enabled_p ()) |
78c60e3d | 1687 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
e645e942 | 1688 | "virtual phi. skip.\n"); |
ebfd146a IR |
1689 | continue; |
1690 | } | |
1691 | ||
1692 | /* Skip reduction phis. */ | |
0ac168a1 RG |
1693 | stmt_info = vinfo_for_stmt (phi); |
1694 | if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def) | |
b8698a0f | 1695 | { |
73fbfcad | 1696 | if (dump_enabled_p ()) |
78c60e3d | 1697 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
e645e942 | 1698 | "reduc phi. skip.\n"); |
ebfd146a | 1699 | continue; |
b8698a0f | 1700 | } |
ebfd146a | 1701 | |
0ac168a1 RG |
1702 | type = TREE_TYPE (gimple_phi_result (phi)); |
1703 | step_expr = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_info); | |
1704 | step_expr = unshare_expr (step_expr); | |
b8698a0f | 1705 | |
ebfd146a IR |
1706 | /* FORNOW: We do not support IVs whose evolution function is a polynomial |
1707 | of degree >= 2 or exponential. */ | |
0ac168a1 | 1708 | gcc_assert (!tree_is_chrec (step_expr)); |
ebfd146a | 1709 | |
0ac168a1 | 1710 | init_expr = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop)); |
ebfd146a | 1711 | |
550918ca RG |
1712 | off = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr), |
1713 | fold_convert (TREE_TYPE (step_expr), niters), | |
1714 | step_expr); | |
0ac168a1 | 1715 | if (POINTER_TYPE_P (type)) |
5d49b6a7 | 1716 | ni = fold_build_pointer_plus (init_expr, off); |
ebfd146a | 1717 | else |
0ac168a1 RG |
1718 | ni = fold_build2 (PLUS_EXPR, type, |
1719 | init_expr, fold_convert (type, off)); | |
ebfd146a | 1720 | |
0ac168a1 | 1721 | var = create_tmp_var (type, "tmp"); |
ebfd146a IR |
1722 | |
1723 | last_gsi = gsi_last_bb (exit_bb); | |
1724 | ni_name = force_gimple_operand_gsi (&last_gsi, ni, false, var, | |
1725 | true, GSI_SAME_STMT); | |
b8698a0f | 1726 | |
ebfd146a | 1727 | /* Fix phi expressions in the successor bb. */ |
684f25f4 | 1728 | adjust_phi_and_debug_stmts (phi1, update_e, ni_name); |
ebfd146a IR |
1729 | } |
1730 | } | |
1731 | ||
ebfd146a IR |
1732 | /* Function vect_do_peeling_for_loop_bound |
1733 | ||
1734 | Peel the last iterations of the loop represented by LOOP_VINFO. | |
b8698a0f | 1735 | The peeled iterations form a new epilog loop. Given that the loop now |
ebfd146a IR |
1736 | iterates NITERS times, the new epilog loop iterates |
1737 | NITERS % VECTORIZATION_FACTOR times. | |
b8698a0f L |
1738 | |
1739 | The original loop will later be made to iterate | |
86290011 RG |
1740 | NITERS / VECTORIZATION_FACTOR times (this value is placed into RATIO). |
1741 | ||
1742 | COND_EXPR and COND_EXPR_STMT_LIST are combined with a new generated | |
1743 | test. */ | |
ebfd146a | 1744 | |
b8698a0f | 1745 | void |
f3c92486 RB |
1746 | vect_do_peeling_for_loop_bound (loop_vec_info loop_vinfo, |
1747 | tree ni_name, tree ratio_mult_vf_name, | |
368117e8 | 1748 | unsigned int th, bool check_profitability) |
ebfd146a | 1749 | { |
ebfd146a | 1750 | struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo); |
5ce9450f | 1751 | struct loop *scalar_loop = LOOP_VINFO_SCALAR_LOOP (loop_vinfo); |
ebfd146a IR |
1752 | struct loop *new_loop; |
1753 | edge update_e; | |
1754 | basic_block preheader; | |
1755 | int loop_num; | |
d68d56b5 | 1756 | int max_iter; |
368117e8 RG |
1757 | tree cond_expr = NULL_TREE; |
1758 | gimple_seq cond_expr_stmt_list = NULL; | |
ebfd146a | 1759 | |
73fbfcad | 1760 | if (dump_enabled_p ()) |
ccb3ad87 | 1761 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 1762 | "=== vect_do_peeling_for_loop_bound ===\n"); |
ebfd146a IR |
1763 | |
1764 | initialize_original_copy_tables (); | |
1765 | ||
b8698a0f | 1766 | loop_num = loop->num; |
ebfd146a | 1767 | |
5ce9450f JJ |
1768 | new_loop |
1769 | = slpeel_tree_peel_loop_to_edge (loop, scalar_loop, single_exit (loop), | |
1770 | &ratio_mult_vf_name, ni_name, false, | |
1771 | th, check_profitability, | |
1772 | cond_expr, cond_expr_stmt_list, | |
1773 | 0, LOOP_VINFO_VECT_FACTOR (loop_vinfo)); | |
ebfd146a IR |
1774 | gcc_assert (new_loop); |
1775 | gcc_assert (loop_num == loop->num); | |
1776 | #ifdef ENABLE_CHECKING | |
1777 | slpeel_verify_cfg_after_peeling (loop, new_loop); | |
1778 | #endif | |
1779 | ||
1780 | /* A guard that controls whether the new_loop is to be executed or skipped | |
1781 | is placed in LOOP->exit. LOOP->exit therefore has two successors - one | |
1782 | is the preheader of NEW_LOOP, where the IVs from LOOP are used. The other | |
1783 | is a bb after NEW_LOOP, where these IVs are not used. Find the edge that | |
1784 | is on the path where the LOOP IVs are used and need to be updated. */ | |
1785 | ||
1786 | preheader = loop_preheader_edge (new_loop)->src; | |
1787 | if (EDGE_PRED (preheader, 0)->src == single_exit (loop)->dest) | |
1788 | update_e = EDGE_PRED (preheader, 0); | |
1789 | else | |
1790 | update_e = EDGE_PRED (preheader, 1); | |
1791 | ||
b8698a0f | 1792 | /* Update IVs of original loop as if they were advanced |
ebfd146a | 1793 | by ratio_mult_vf_name steps. */ |
b8698a0f | 1794 | vect_update_ivs_after_vectorizer (loop_vinfo, ratio_mult_vf_name, update_e); |
ebfd146a | 1795 | |
22458c5a JH |
1796 | /* For vectorization factor N, we need to copy last N-1 values in epilogue |
1797 | and this means N-2 loopback edge executions. | |
1798 | ||
1799 | PEELING_FOR_GAPS works by subtracting last iteration and thus the epilogue | |
1800 | will execute at least LOOP_VINFO_VECT_FACTOR times. */ | |
1801 | max_iter = (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) | |
1802 | ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) * 2 | |
1803 | : LOOP_VINFO_VECT_FACTOR (loop_vinfo)) - 2; | |
368117e8 | 1804 | if (check_profitability) |
22458c5a | 1805 | max_iter = MAX (max_iter, (int) th - 1); |
807e902e | 1806 | record_niter_bound (new_loop, max_iter, false, true); |
ccb3ad87 | 1807 | dump_printf (MSG_NOTE, |
78c60e3d SS |
1808 | "Setting upper bound of nb iterations for epilogue " |
1809 | "loop to %d\n", max_iter); | |
7d5a99f4 | 1810 | |
ebfd146a IR |
1811 | /* After peeling we have to reset scalar evolution analyzer. */ |
1812 | scev_reset (); | |
1813 | ||
1814 | free_original_copy_tables (); | |
1815 | } | |
1816 | ||
1817 | ||
1818 | /* Function vect_gen_niters_for_prolog_loop | |
1819 | ||
1820 | Set the number of iterations for the loop represented by LOOP_VINFO | |
1821 | to the minimum between LOOP_NITERS (the original iteration count of the loop) | |
1822 | and the misalignment of DR - the data reference recorded in | |
b8698a0f | 1823 | LOOP_VINFO_UNALIGNED_DR (LOOP_VINFO). As a result, after the execution of |
ebfd146a IR |
1824 | this loop, the data reference DR will refer to an aligned location. |
1825 | ||
1826 | The following computation is generated: | |
1827 | ||
1828 | If the misalignment of DR is known at compile time: | |
1829 | addr_mis = int mis = DR_MISALIGNMENT (dr); | |
1830 | Else, compute address misalignment in bytes: | |
5aea1e76 | 1831 | addr_mis = addr & (vectype_align - 1) |
ebfd146a IR |
1832 | |
1833 | prolog_niters = min (LOOP_NITERS, ((VF - addr_mis/elem_size)&(VF-1))/step) | |
1834 | ||
1835 | (elem_size = element type size; an element is the scalar element whose type | |
1836 | is the inner type of the vectype) | |
1837 | ||
1838 | When the step of the data-ref in the loop is not 1 (as in interleaved data | |
1839 | and SLP), the number of iterations of the prolog must be divided by the step | |
1840 | (which is equal to the size of interleaved group). | |
1841 | ||
1842 | The above formulas assume that VF == number of elements in the vector. This | |
1843 | may not hold when there are multiple-types in the loop. | |
1844 | In this case, for some data-references in the loop the VF does not represent | |
1845 | the number of elements that fit in the vector. Therefore, instead of VF we | |
1846 | use TYPE_VECTOR_SUBPARTS. */ | |
1847 | ||
b8698a0f | 1848 | static tree |
e78410bf | 1849 | vect_gen_niters_for_prolog_loop (loop_vec_info loop_vinfo, tree loop_niters, int *bound) |
ebfd146a IR |
1850 | { |
1851 | struct data_reference *dr = LOOP_VINFO_UNALIGNED_DR (loop_vinfo); | |
1852 | struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo); | |
1853 | tree var; | |
1854 | gimple_seq stmts; | |
1855 | tree iters, iters_name; | |
1856 | edge pe; | |
1857 | basic_block new_bb; | |
1858 | gimple dr_stmt = DR_STMT (dr); | |
1859 | stmt_vec_info stmt_info = vinfo_for_stmt (dr_stmt); | |
1860 | tree vectype = STMT_VINFO_VECTYPE (stmt_info); | |
1861 | int vectype_align = TYPE_ALIGN (vectype) / BITS_PER_UNIT; | |
1862 | tree niters_type = TREE_TYPE (loop_niters); | |
ebfd146a IR |
1863 | int nelements = TYPE_VECTOR_SUBPARTS (vectype); |
1864 | ||
b8698a0f | 1865 | pe = loop_preheader_edge (loop); |
ebfd146a | 1866 | |
15e693cc | 1867 | if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) > 0) |
ebfd146a | 1868 | { |
15e693cc | 1869 | int npeel = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo); |
ebfd146a | 1870 | |
73fbfcad | 1871 | if (dump_enabled_p ()) |
ccb3ad87 | 1872 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 1873 | "known peeling = %d.\n", npeel); |
ebfd146a | 1874 | |
720f5239 | 1875 | iters = build_int_cst (niters_type, npeel); |
15e693cc | 1876 | *bound = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo); |
ebfd146a IR |
1877 | } |
1878 | else | |
1879 | { | |
1880 | gimple_seq new_stmts = NULL; | |
d8ba5b19 RG |
1881 | bool negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0; |
1882 | tree offset = negative | |
1883 | ? size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1) : NULL_TREE; | |
b8698a0f | 1884 | tree start_addr = vect_create_addr_base_for_vector_ref (dr_stmt, |
d8ba5b19 | 1885 | &new_stmts, offset, loop); |
96f9265a | 1886 | tree type = unsigned_type_for (TREE_TYPE (start_addr)); |
5aea1e76 UW |
1887 | tree vectype_align_minus_1 = build_int_cst (type, vectype_align - 1); |
1888 | HOST_WIDE_INT elem_size = | |
1889 | int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype))); | |
1890 | tree elem_size_log = build_int_cst (type, exact_log2 (elem_size)); | |
ebfd146a IR |
1891 | tree nelements_minus_1 = build_int_cst (type, nelements - 1); |
1892 | tree nelements_tree = build_int_cst (type, nelements); | |
1893 | tree byte_misalign; | |
1894 | tree elem_misalign; | |
1895 | ||
1896 | new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmts); | |
1897 | gcc_assert (!new_bb); | |
b8698a0f | 1898 | |
5aea1e76 | 1899 | /* Create: byte_misalign = addr & (vectype_align - 1) */ |
b8698a0f | 1900 | byte_misalign = |
720f5239 | 1901 | fold_build2 (BIT_AND_EXPR, type, fold_convert (type, start_addr), |
5aea1e76 | 1902 | vectype_align_minus_1); |
b8698a0f | 1903 | |
ebfd146a IR |
1904 | /* Create: elem_misalign = byte_misalign / element_size */ |
1905 | elem_misalign = | |
1906 | fold_build2 (RSHIFT_EXPR, type, byte_misalign, elem_size_log); | |
1907 | ||
1908 | /* Create: (niters_type) (nelements - elem_misalign)&(nelements - 1) */ | |
d8ba5b19 RG |
1909 | if (negative) |
1910 | iters = fold_build2 (MINUS_EXPR, type, elem_misalign, nelements_tree); | |
1911 | else | |
1912 | iters = fold_build2 (MINUS_EXPR, type, nelements_tree, elem_misalign); | |
ebfd146a IR |
1913 | iters = fold_build2 (BIT_AND_EXPR, type, iters, nelements_minus_1); |
1914 | iters = fold_convert (niters_type, iters); | |
e78410bf | 1915 | *bound = nelements; |
ebfd146a IR |
1916 | } |
1917 | ||
1918 | /* Create: prolog_loop_niters = min (iters, loop_niters) */ | |
1919 | /* If the loop bound is known at compile time we already verified that it is | |
1920 | greater than vf; since the misalignment ('iters') is at most vf, there's | |
1921 | no need to generate the MIN_EXPR in this case. */ | |
1922 | if (TREE_CODE (loop_niters) != INTEGER_CST) | |
1923 | iters = fold_build2 (MIN_EXPR, niters_type, iters, loop_niters); | |
1924 | ||
73fbfcad | 1925 | if (dump_enabled_p ()) |
ebfd146a | 1926 | { |
ccb3ad87 | 1927 | dump_printf_loc (MSG_NOTE, vect_location, |
78c60e3d | 1928 | "niters for prolog loop: "); |
ccb3ad87 | 1929 | dump_generic_expr (MSG_NOTE, TDF_SLIM, iters); |
e645e942 | 1930 | dump_printf (MSG_NOTE, "\n"); |
ebfd146a IR |
1931 | } |
1932 | ||
1933 | var = create_tmp_var (niters_type, "prolog_loop_niters"); | |
ebfd146a IR |
1934 | stmts = NULL; |
1935 | iters_name = force_gimple_operand (iters, &stmts, false, var); | |
1936 | ||
1937 | /* Insert stmt on loop preheader edge. */ | |
1938 | if (stmts) | |
1939 | { | |
1940 | basic_block new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts); | |
1941 | gcc_assert (!new_bb); | |
1942 | } | |
1943 | ||
b8698a0f | 1944 | return iters_name; |
ebfd146a IR |
1945 | } |
1946 | ||
1947 | ||
1948 | /* Function vect_update_init_of_dr | |
1949 | ||
1950 | NITERS iterations were peeled from LOOP. DR represents a data reference | |
1951 | in LOOP. This function updates the information recorded in DR to | |
b8698a0f | 1952 | account for the fact that the first NITERS iterations had already been |
ebfd146a IR |
1953 | executed. Specifically, it updates the OFFSET field of DR. */ |
1954 | ||
1955 | static void | |
1956 | vect_update_init_of_dr (struct data_reference *dr, tree niters) | |
1957 | { | |
1958 | tree offset = DR_OFFSET (dr); | |
b8698a0f | 1959 | |
ebfd146a IR |
1960 | niters = fold_build2 (MULT_EXPR, sizetype, |
1961 | fold_convert (sizetype, niters), | |
1962 | fold_convert (sizetype, DR_STEP (dr))); | |
587aa063 RG |
1963 | offset = fold_build2 (PLUS_EXPR, sizetype, |
1964 | fold_convert (sizetype, offset), niters); | |
ebfd146a IR |
1965 | DR_OFFSET (dr) = offset; |
1966 | } | |
1967 | ||
1968 | ||
1969 | /* Function vect_update_inits_of_drs | |
1970 | ||
b8698a0f L |
1971 | NITERS iterations were peeled from the loop represented by LOOP_VINFO. |
1972 | This function updates the information recorded for the data references in | |
1973 | the loop to account for the fact that the first NITERS iterations had | |
ebfd146a IR |
1974 | already been executed. Specifically, it updates the initial_condition of |
1975 | the access_function of all the data_references in the loop. */ | |
1976 | ||
1977 | static void | |
1978 | vect_update_inits_of_drs (loop_vec_info loop_vinfo, tree niters) | |
1979 | { | |
1980 | unsigned int i; | |
9771b263 | 1981 | vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo); |
ebfd146a | 1982 | struct data_reference *dr; |
78c60e3d | 1983 | |
73fbfcad | 1984 | if (dump_enabled_p ()) |
ccb3ad87 | 1985 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 1986 | "=== vect_update_inits_of_dr ===\n"); |
ebfd146a | 1987 | |
9771b263 | 1988 | FOR_EACH_VEC_ELT (datarefs, i, dr) |
ebfd146a IR |
1989 | vect_update_init_of_dr (dr, niters); |
1990 | } | |
1991 | ||
1992 | ||
1993 | /* Function vect_do_peeling_for_alignment | |
1994 | ||
1995 | Peel the first 'niters' iterations of the loop represented by LOOP_VINFO. | |
1996 | 'niters' is set to the misalignment of one of the data references in the | |
1997 | loop, thereby forcing it to refer to an aligned location at the beginning | |
1998 | of the execution of this loop. The data reference for which we are | |
1999 | peeling is recorded in LOOP_VINFO_UNALIGNED_DR. */ | |
2000 | ||
2001 | void | |
f3c92486 | 2002 | vect_do_peeling_for_alignment (loop_vec_info loop_vinfo, tree ni_name, |
368117e8 | 2003 | unsigned int th, bool check_profitability) |
ebfd146a IR |
2004 | { |
2005 | struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo); | |
5ce9450f | 2006 | struct loop *scalar_loop = LOOP_VINFO_SCALAR_LOOP (loop_vinfo); |
f3c92486 | 2007 | tree niters_of_prolog_loop; |
b61b1f17 | 2008 | tree wide_prolog_niters; |
ebfd146a | 2009 | struct loop *new_loop; |
03fd03d5 | 2010 | int max_iter; |
e78410bf | 2011 | int bound = 0; |
ebfd146a | 2012 | |
73fbfcad | 2013 | if (dump_enabled_p ()) |
9cc1fb4b XDL |
2014 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location, |
2015 | "loop peeled for vectorization to enhance" | |
2016 | " alignment\n"); | |
ebfd146a IR |
2017 | |
2018 | initialize_original_copy_tables (); | |
2019 | ||
f3c92486 RB |
2020 | gimple_seq stmts = NULL; |
2021 | gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts); | |
5d2eb24b | 2022 | niters_of_prolog_loop = vect_gen_niters_for_prolog_loop (loop_vinfo, |
e78410bf JH |
2023 | ni_name, |
2024 | &bound); | |
ebfd146a | 2025 | |
ebfd146a IR |
2026 | /* Peel the prolog loop and iterate it niters_of_prolog_loop. */ |
2027 | new_loop = | |
5ce9450f JJ |
2028 | slpeel_tree_peel_loop_to_edge (loop, scalar_loop, |
2029 | loop_preheader_edge (loop), | |
5d2eb24b | 2030 | &niters_of_prolog_loop, ni_name, true, |
e78410bf | 2031 | th, check_profitability, NULL_TREE, NULL, |
5ce9450f | 2032 | bound, 0); |
ebfd146a IR |
2033 | |
2034 | gcc_assert (new_loop); | |
2035 | #ifdef ENABLE_CHECKING | |
2036 | slpeel_verify_cfg_after_peeling (new_loop, loop); | |
2037 | #endif | |
22458c5a JH |
2038 | /* For vectorization factor N, we need to copy at most N-1 values |
2039 | for alignment and this means N-2 loopback edge executions. */ | |
2040 | max_iter = LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 2; | |
368117e8 | 2041 | if (check_profitability) |
22458c5a | 2042 | max_iter = MAX (max_iter, (int) th - 1); |
807e902e | 2043 | record_niter_bound (new_loop, max_iter, false, true); |
ccb3ad87 | 2044 | dump_printf (MSG_NOTE, |
78c60e3d SS |
2045 | "Setting upper bound of nb iterations for prologue " |
2046 | "loop to %d\n", max_iter); | |
ebfd146a IR |
2047 | |
2048 | /* Update number of times loop executes. */ | |
ebfd146a | 2049 | LOOP_VINFO_NITERS (loop_vinfo) = fold_build2 (MINUS_EXPR, |
15e693cc | 2050 | TREE_TYPE (ni_name), ni_name, niters_of_prolog_loop); |
95b3eff3 RB |
2051 | LOOP_VINFO_NITERSM1 (loop_vinfo) = fold_build2 (MINUS_EXPR, |
2052 | TREE_TYPE (ni_name), | |
2053 | LOOP_VINFO_NITERSM1 (loop_vinfo), niters_of_prolog_loop); | |
ebfd146a | 2054 | |
5d2eb24b IR |
2055 | if (types_compatible_p (sizetype, TREE_TYPE (niters_of_prolog_loop))) |
2056 | wide_prolog_niters = niters_of_prolog_loop; | |
2057 | else | |
2058 | { | |
2059 | gimple_seq seq = NULL; | |
2060 | edge pe = loop_preheader_edge (loop); | |
2061 | tree wide_iters = fold_convert (sizetype, niters_of_prolog_loop); | |
2062 | tree var = create_tmp_var (sizetype, "prolog_loop_adjusted_niters"); | |
5d2eb24b IR |
2063 | wide_prolog_niters = force_gimple_operand (wide_iters, &seq, false, |
2064 | var); | |
2065 | if (seq) | |
2066 | { | |
2067 | /* Insert stmt on loop preheader edge. */ | |
2068 | basic_block new_bb = gsi_insert_seq_on_edge_immediate (pe, seq); | |
2069 | gcc_assert (!new_bb); | |
2070 | } | |
2071 | } | |
2072 | ||
ebfd146a | 2073 | /* Update the init conditions of the access functions of all data refs. */ |
b61b1f17 | 2074 | vect_update_inits_of_drs (loop_vinfo, wide_prolog_niters); |
ebfd146a IR |
2075 | |
2076 | /* After peeling we have to reset scalar evolution analyzer. */ | |
2077 | scev_reset (); | |
2078 | ||
2079 | free_original_copy_tables (); | |
2080 | } | |
2081 | ||
2082 | ||
2083 | /* Function vect_create_cond_for_align_checks. | |
2084 | ||
2085 | Create a conditional expression that represents the alignment checks for | |
2086 | all of data references (array element references) whose alignment must be | |
2087 | checked at runtime. | |
2088 | ||
2089 | Input: | |
2090 | COND_EXPR - input conditional expression. New conditions will be chained | |
2091 | with logical AND operation. | |
2092 | LOOP_VINFO - two fields of the loop information are used. | |
2093 | LOOP_VINFO_PTR_MASK is the mask used to check the alignment. | |
2094 | LOOP_VINFO_MAY_MISALIGN_STMTS contains the refs to be checked. | |
2095 | ||
2096 | Output: | |
2097 | COND_EXPR_STMT_LIST - statements needed to construct the conditional | |
2098 | expression. | |
2099 | The returned value is the conditional expression to be used in the if | |
2100 | statement that controls which version of the loop gets executed at runtime. | |
2101 | ||
2102 | The algorithm makes two assumptions: | |
2103 | 1) The number of bytes "n" in a vector is a power of 2. | |
2104 | 2) An address "a" is aligned if a%n is zero and that this | |
2105 | test can be done as a&(n-1) == 0. For example, for 16 | |
2106 | byte vectors the test is a&0xf == 0. */ | |
2107 | ||
2108 | static void | |
2109 | vect_create_cond_for_align_checks (loop_vec_info loop_vinfo, | |
2110 | tree *cond_expr, | |
2111 | gimple_seq *cond_expr_stmt_list) | |
2112 | { | |
2113 | struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo); | |
9771b263 | 2114 | vec<gimple> may_misalign_stmts |
ebfd146a IR |
2115 | = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo); |
2116 | gimple ref_stmt; | |
2117 | int mask = LOOP_VINFO_PTR_MASK (loop_vinfo); | |
2118 | tree mask_cst; | |
2119 | unsigned int i; | |
ebfd146a IR |
2120 | tree int_ptrsize_type; |
2121 | char tmp_name[20]; | |
2122 | tree or_tmp_name = NULL_TREE; | |
83d5977e | 2123 | tree and_tmp_name; |
ebfd146a IR |
2124 | gimple and_stmt; |
2125 | tree ptrsize_zero; | |
2126 | tree part_cond_expr; | |
2127 | ||
2128 | /* Check that mask is one less than a power of 2, i.e., mask is | |
2129 | all zeros followed by all ones. */ | |
2130 | gcc_assert ((mask != 0) && ((mask & (mask+1)) == 0)); | |
2131 | ||
96f9265a | 2132 | int_ptrsize_type = signed_type_for (ptr_type_node); |
ebfd146a IR |
2133 | |
2134 | /* Create expression (mask & (dr_1 || ... || dr_n)) where dr_i is the address | |
2135 | of the first vector of the i'th data reference. */ | |
2136 | ||
9771b263 | 2137 | FOR_EACH_VEC_ELT (may_misalign_stmts, i, ref_stmt) |
ebfd146a IR |
2138 | { |
2139 | gimple_seq new_stmt_list = NULL; | |
2140 | tree addr_base; | |
83d5977e RG |
2141 | tree addr_tmp_name; |
2142 | tree new_or_tmp_name; | |
ebfd146a | 2143 | gimple addr_stmt, or_stmt; |
d8ba5b19 RG |
2144 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (ref_stmt); |
2145 | tree vectype = STMT_VINFO_VECTYPE (stmt_vinfo); | |
2146 | bool negative = tree_int_cst_compare | |
2147 | (DR_STEP (STMT_VINFO_DATA_REF (stmt_vinfo)), size_zero_node) < 0; | |
2148 | tree offset = negative | |
2149 | ? size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1) : NULL_TREE; | |
ebfd146a IR |
2150 | |
2151 | /* create: addr_tmp = (int)(address_of_first_vector) */ | |
2152 | addr_base = | |
2153 | vect_create_addr_base_for_vector_ref (ref_stmt, &new_stmt_list, | |
d8ba5b19 | 2154 | offset, loop); |
ebfd146a IR |
2155 | if (new_stmt_list != NULL) |
2156 | gimple_seq_add_seq (cond_expr_stmt_list, new_stmt_list); | |
2157 | ||
83d5977e RG |
2158 | sprintf (tmp_name, "addr2int%d", i); |
2159 | addr_tmp_name = make_temp_ssa_name (int_ptrsize_type, NULL, tmp_name); | |
ebfd146a | 2160 | addr_stmt = gimple_build_assign_with_ops (NOP_EXPR, addr_tmp_name, |
00d66391 | 2161 | addr_base); |
ebfd146a IR |
2162 | gimple_seq_add_stmt (cond_expr_stmt_list, addr_stmt); |
2163 | ||
2164 | /* The addresses are OR together. */ | |
2165 | ||
2166 | if (or_tmp_name != NULL_TREE) | |
2167 | { | |
2168 | /* create: or_tmp = or_tmp | addr_tmp */ | |
83d5977e RG |
2169 | sprintf (tmp_name, "orptrs%d", i); |
2170 | new_or_tmp_name = make_temp_ssa_name (int_ptrsize_type, NULL, tmp_name); | |
ebfd146a IR |
2171 | or_stmt = gimple_build_assign_with_ops (BIT_IOR_EXPR, |
2172 | new_or_tmp_name, | |
2173 | or_tmp_name, addr_tmp_name); | |
ebfd146a IR |
2174 | gimple_seq_add_stmt (cond_expr_stmt_list, or_stmt); |
2175 | or_tmp_name = new_or_tmp_name; | |
2176 | } | |
2177 | else | |
2178 | or_tmp_name = addr_tmp_name; | |
2179 | ||
2180 | } /* end for i */ | |
2181 | ||
2182 | mask_cst = build_int_cst (int_ptrsize_type, mask); | |
2183 | ||
2184 | /* create: and_tmp = or_tmp & mask */ | |
83d5977e | 2185 | and_tmp_name = make_temp_ssa_name (int_ptrsize_type, NULL, "andmask"); |
ebfd146a IR |
2186 | |
2187 | and_stmt = gimple_build_assign_with_ops (BIT_AND_EXPR, and_tmp_name, | |
2188 | or_tmp_name, mask_cst); | |
ebfd146a IR |
2189 | gimple_seq_add_stmt (cond_expr_stmt_list, and_stmt); |
2190 | ||
2191 | /* Make and_tmp the left operand of the conditional test against zero. | |
2192 | if and_tmp has a nonzero bit then some address is unaligned. */ | |
2193 | ptrsize_zero = build_int_cst (int_ptrsize_type, 0); | |
2194 | part_cond_expr = fold_build2 (EQ_EXPR, boolean_type_node, | |
2195 | and_tmp_name, ptrsize_zero); | |
2196 | if (*cond_expr) | |
2197 | *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, | |
2198 | *cond_expr, part_cond_expr); | |
2199 | else | |
2200 | *cond_expr = part_cond_expr; | |
2201 | } | |
2202 | ||
ebfd146a IR |
2203 | /* Function vect_create_cond_for_alias_checks. |
2204 | ||
2205 | Create a conditional expression that represents the run-time checks for | |
2206 | overlapping of address ranges represented by a list of data references | |
2207 | relations passed as input. | |
2208 | ||
2209 | Input: | |
2210 | COND_EXPR - input conditional expression. New conditions will be chained | |
a05a89fa CH |
2211 | with logical AND operation. If it is NULL, then the function |
2212 | is used to return the number of alias checks. | |
ebfd146a IR |
2213 | LOOP_VINFO - field LOOP_VINFO_MAY_ALIAS_STMTS contains the list of ddrs |
2214 | to be checked. | |
2215 | ||
2216 | Output: | |
2217 | COND_EXPR - conditional expression. | |
ebfd146a | 2218 | |
a05a89fa | 2219 | The returned COND_EXPR is the conditional expression to be used in the if |
ebfd146a IR |
2220 | statement that controls which version of the loop gets executed at runtime. |
2221 | */ | |
2222 | ||
a05a89fa | 2223 | void |
4bdd44c4 | 2224 | vect_create_cond_for_alias_checks (loop_vec_info loop_vinfo, tree * cond_expr) |
ebfd146a | 2225 | { |
93bdc3ed | 2226 | vec<dr_with_seg_len_pair_t> comp_alias_ddrs = |
a05a89fa CH |
2227 | LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo); |
2228 | tree part_cond_expr; | |
ebfd146a IR |
2229 | |
2230 | /* Create expression | |
36fc3799 RS |
2231 | ((store_ptr_0 + store_segment_length_0) <= load_ptr_0) |
2232 | || (load_ptr_0 + load_segment_length_0) <= store_ptr_0)) | |
b8698a0f | 2233 | && |
ebfd146a IR |
2234 | ... |
2235 | && | |
36fc3799 RS |
2236 | ((store_ptr_n + store_segment_length_n) <= load_ptr_n) |
2237 | || (load_ptr_n + load_segment_length_n) <= store_ptr_n)) */ | |
ebfd146a | 2238 | |
a05a89fa | 2239 | if (comp_alias_ddrs.is_empty ()) |
ebfd146a IR |
2240 | return; |
2241 | ||
a05a89fa | 2242 | for (size_t i = 0, s = comp_alias_ddrs.length (); i < s; ++i) |
ebfd146a | 2243 | { |
93bdc3ed CH |
2244 | const dr_with_seg_len& dr_a = comp_alias_ddrs[i].first; |
2245 | const dr_with_seg_len& dr_b = comp_alias_ddrs[i].second; | |
a05a89fa CH |
2246 | tree segment_length_a = dr_a.seg_len; |
2247 | tree segment_length_b = dr_b.seg_len; | |
ebfd146a | 2248 | |
a05a89fa | 2249 | tree addr_base_a |
93bdc3ed | 2250 | = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_a.dr), dr_a.offset); |
a05a89fa | 2251 | tree addr_base_b |
93bdc3ed | 2252 | = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_b.dr), dr_b.offset); |
ebfd146a | 2253 | |
73fbfcad | 2254 | if (dump_enabled_p ()) |
ebfd146a | 2255 | { |
ccb3ad87 | 2256 | dump_printf_loc (MSG_NOTE, vect_location, |
a05a89fa CH |
2257 | "create runtime check for data references "); |
2258 | dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr_a.dr)); | |
ccb3ad87 | 2259 | dump_printf (MSG_NOTE, " and "); |
a05a89fa CH |
2260 | dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr_b.dr)); |
2261 | dump_printf (MSG_NOTE, "\n"); | |
ebfd146a IR |
2262 | } |
2263 | ||
a05a89fa CH |
2264 | tree seg_a_min = addr_base_a; |
2265 | tree seg_a_max = fold_build_pointer_plus (addr_base_a, segment_length_a); | |
82d89471 BM |
2266 | /* For negative step, we need to adjust address range by TYPE_SIZE_UNIT |
2267 | bytes, e.g., int a[3] -> a[1] range is [a+4, a+16) instead of | |
2268 | [a, a+12) */ | |
a05a89fa | 2269 | if (tree_int_cst_compare (DR_STEP (dr_a.dr), size_zero_node) < 0) |
82d89471 BM |
2270 | { |
2271 | tree unit_size = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr_a.dr))); | |
2272 | seg_a_min = fold_build_pointer_plus (seg_a_max, unit_size); | |
2273 | seg_a_max = fold_build_pointer_plus (addr_base_a, unit_size); | |
2274 | } | |
d8ba5b19 | 2275 | |
a05a89fa CH |
2276 | tree seg_b_min = addr_base_b; |
2277 | tree seg_b_max = fold_build_pointer_plus (addr_base_b, segment_length_b); | |
2278 | if (tree_int_cst_compare (DR_STEP (dr_b.dr), size_zero_node) < 0) | |
82d89471 BM |
2279 | { |
2280 | tree unit_size = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr_b.dr))); | |
2281 | seg_b_min = fold_build_pointer_plus (seg_b_max, unit_size); | |
2282 | seg_b_max = fold_build_pointer_plus (addr_base_b, unit_size); | |
2283 | } | |
ebfd146a | 2284 | |
b8698a0f | 2285 | part_cond_expr = |
ebfd146a | 2286 | fold_build2 (TRUTH_OR_EXPR, boolean_type_node, |
36fc3799 RS |
2287 | fold_build2 (LE_EXPR, boolean_type_node, seg_a_max, seg_b_min), |
2288 | fold_build2 (LE_EXPR, boolean_type_node, seg_b_max, seg_a_min)); | |
b8698a0f | 2289 | |
ebfd146a IR |
2290 | if (*cond_expr) |
2291 | *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, | |
2292 | *cond_expr, part_cond_expr); | |
2293 | else | |
2294 | *cond_expr = part_cond_expr; | |
2295 | } | |
ebfd146a | 2296 | |
73fbfcad | 2297 | if (dump_enabled_p ()) |
ccb3ad87 | 2298 | dump_printf_loc (MSG_NOTE, vect_location, |
78c60e3d | 2299 | "created %u versioning for alias checks.\n", |
a05a89fa CH |
2300 | comp_alias_ddrs.length ()); |
2301 | ||
2302 | comp_alias_ddrs.release (); | |
ebfd146a IR |
2303 | } |
2304 | ||
2305 | ||
2306 | /* Function vect_loop_versioning. | |
b8698a0f | 2307 | |
ebfd146a IR |
2308 | If the loop has data references that may or may not be aligned or/and |
2309 | has data reference relations whose independence was not proven then | |
2310 | two versions of the loop need to be generated, one which is vectorized | |
2311 | and one which isn't. A test is then generated to control which of the | |
2312 | loops is executed. The test checks for the alignment of all of the | |
2313 | data references that may or may not be aligned. An additional | |
2314 | sequence of runtime tests is generated for each pairs of DDRs whose | |
b8698a0f L |
2315 | independence was not proven. The vectorized version of loop is |
2316 | executed only if both alias and alignment tests are passed. | |
2317 | ||
ebfd146a | 2318 | The test generated to check which version of loop is executed |
b8698a0f | 2319 | is modified to also check for profitability as indicated by the |
86290011 RG |
2320 | cost model initially. |
2321 | ||
2322 | The versioning precondition(s) are placed in *COND_EXPR and | |
d68d56b5 | 2323 | *COND_EXPR_STMT_LIST. */ |
ebfd146a IR |
2324 | |
2325 | void | |
368117e8 RG |
2326 | vect_loop_versioning (loop_vec_info loop_vinfo, |
2327 | unsigned int th, bool check_profitability) | |
ebfd146a IR |
2328 | { |
2329 | struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo); | |
5ce9450f | 2330 | struct loop *scalar_loop = LOOP_VINFO_SCALAR_LOOP (loop_vinfo); |
ebfd146a | 2331 | basic_block condition_bb; |
538dd0b7 DM |
2332 | gphi_iterator gsi; |
2333 | gimple_stmt_iterator cond_exp_gsi; | |
ebfd146a IR |
2334 | basic_block merge_bb; |
2335 | basic_block new_exit_bb; | |
2336 | edge new_exit_e, e; | |
538dd0b7 | 2337 | gphi *orig_phi, *new_phi; |
368117e8 | 2338 | tree cond_expr = NULL_TREE; |
d68d56b5 | 2339 | gimple_seq cond_expr_stmt_list = NULL; |
ebfd146a IR |
2340 | tree arg; |
2341 | unsigned prob = 4 * REG_BR_PROB_BASE / 5; | |
2342 | gimple_seq gimplify_stmt_list = NULL; | |
2343 | tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo); | |
9cc1fb4b XDL |
2344 | bool version_align = LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo); |
2345 | bool version_alias = LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo); | |
ebfd146a | 2346 | |
368117e8 RG |
2347 | if (check_profitability) |
2348 | { | |
2349 | cond_expr = fold_build2 (GT_EXPR, boolean_type_node, scalar_loop_iters, | |
2350 | build_int_cst (TREE_TYPE (scalar_loop_iters), th)); | |
2351 | cond_expr = force_gimple_operand_1 (cond_expr, &cond_expr_stmt_list, | |
2352 | is_gimple_condexpr, NULL_TREE); | |
2353 | } | |
ebfd146a | 2354 | |
9cc1fb4b | 2355 | if (version_align) |
d68d56b5 RG |
2356 | vect_create_cond_for_align_checks (loop_vinfo, &cond_expr, |
2357 | &cond_expr_stmt_list); | |
ebfd146a | 2358 | |
9cc1fb4b | 2359 | if (version_alias) |
4bdd44c4 | 2360 | vect_create_cond_for_alias_checks (loop_vinfo, &cond_expr); |
86290011 | 2361 | |
d68d56b5 RG |
2362 | cond_expr = force_gimple_operand_1 (cond_expr, &gimplify_stmt_list, |
2363 | is_gimple_condexpr, NULL_TREE); | |
2364 | gimple_seq_add_seq (&cond_expr_stmt_list, gimplify_stmt_list); | |
ebfd146a IR |
2365 | |
2366 | initialize_original_copy_tables (); | |
5ce9450f JJ |
2367 | if (scalar_loop) |
2368 | { | |
2369 | edge scalar_e; | |
2370 | basic_block preheader, scalar_preheader; | |
2371 | ||
2372 | /* We don't want to scale SCALAR_LOOP's frequencies, we need to | |
2373 | scale LOOP's frequencies instead. */ | |
2374 | loop_version (scalar_loop, cond_expr, &condition_bb, | |
2375 | prob, REG_BR_PROB_BASE, REG_BR_PROB_BASE - prob, true); | |
2376 | scale_loop_frequencies (loop, prob, REG_BR_PROB_BASE); | |
2377 | /* CONDITION_BB was created above SCALAR_LOOP's preheader, | |
2378 | while we need to move it above LOOP's preheader. */ | |
2379 | e = loop_preheader_edge (loop); | |
2380 | scalar_e = loop_preheader_edge (scalar_loop); | |
2381 | gcc_assert (empty_block_p (e->src) | |
2382 | && single_pred_p (e->src)); | |
2383 | gcc_assert (empty_block_p (scalar_e->src) | |
2384 | && single_pred_p (scalar_e->src)); | |
2385 | gcc_assert (single_pred_p (condition_bb)); | |
2386 | preheader = e->src; | |
2387 | scalar_preheader = scalar_e->src; | |
2388 | scalar_e = find_edge (condition_bb, scalar_preheader); | |
2389 | e = single_pred_edge (preheader); | |
2390 | redirect_edge_and_branch_force (single_pred_edge (condition_bb), | |
2391 | scalar_preheader); | |
2392 | redirect_edge_and_branch_force (scalar_e, preheader); | |
2393 | redirect_edge_and_branch_force (e, condition_bb); | |
2394 | set_immediate_dominator (CDI_DOMINATORS, condition_bb, | |
2395 | single_pred (condition_bb)); | |
2396 | set_immediate_dominator (CDI_DOMINATORS, scalar_preheader, | |
2397 | single_pred (scalar_preheader)); | |
2398 | set_immediate_dominator (CDI_DOMINATORS, preheader, | |
2399 | condition_bb); | |
2400 | } | |
2401 | else | |
2402 | loop_version (loop, cond_expr, &condition_bb, | |
2403 | prob, prob, REG_BR_PROB_BASE - prob, true); | |
9cc1fb4b | 2404 | |
b05e0233 | 2405 | if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION |
9cc1fb4b XDL |
2406 | && dump_enabled_p ()) |
2407 | { | |
2408 | if (version_alias) | |
2409 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location, | |
2410 | "loop versioned for vectorization because of " | |
2411 | "possible aliasing\n"); | |
2412 | if (version_align) | |
2413 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location, | |
2414 | "loop versioned for vectorization to enhance " | |
2415 | "alignment\n"); | |
2416 | ||
2417 | } | |
c3284718 | 2418 | free_original_copy_tables (); |
ebfd146a | 2419 | |
b8698a0f | 2420 | /* Loop versioning violates an assumption we try to maintain during |
ebfd146a IR |
2421 | vectorization - that the loop exit block has a single predecessor. |
2422 | After versioning, the exit block of both loop versions is the same | |
2423 | basic block (i.e. it has two predecessors). Just in order to simplify | |
2424 | following transformations in the vectorizer, we fix this situation | |
2425 | here by adding a new (empty) block on the exit-edge of the loop, | |
5ce9450f JJ |
2426 | with the proper loop-exit phis to maintain loop-closed-form. |
2427 | If loop versioning wasn't done from loop, but scalar_loop instead, | |
2428 | merge_bb will have already just a single successor. */ | |
b8698a0f | 2429 | |
ebfd146a | 2430 | merge_bb = single_exit (loop)->dest; |
5ce9450f | 2431 | if (scalar_loop == NULL || EDGE_COUNT (merge_bb->preds) >= 2) |
ebfd146a | 2432 | { |
5ce9450f JJ |
2433 | gcc_assert (EDGE_COUNT (merge_bb->preds) >= 2); |
2434 | new_exit_bb = split_edge (single_exit (loop)); | |
2435 | new_exit_e = single_exit (loop); | |
2436 | e = EDGE_SUCC (new_exit_bb, 0); | |
2437 | ||
2438 | for (gsi = gsi_start_phis (merge_bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
2439 | { | |
2440 | tree new_res; | |
538dd0b7 | 2441 | orig_phi = gsi.phi (); |
5ce9450f JJ |
2442 | new_res = copy_ssa_name (PHI_RESULT (orig_phi), NULL); |
2443 | new_phi = create_phi_node (new_res, new_exit_bb); | |
2444 | arg = PHI_ARG_DEF_FROM_EDGE (orig_phi, e); | |
2445 | add_phi_arg (new_phi, arg, new_exit_e, | |
2446 | gimple_phi_arg_location_from_edge (orig_phi, e)); | |
2447 | adjust_phi_and_debug_stmts (orig_phi, e, PHI_RESULT (new_phi)); | |
2448 | } | |
b8698a0f | 2449 | } |
ebfd146a IR |
2450 | |
2451 | /* End loop-exit-fixes after versioning. */ | |
2452 | ||
d68d56b5 | 2453 | if (cond_expr_stmt_list) |
ebfd146a IR |
2454 | { |
2455 | cond_exp_gsi = gsi_last_bb (condition_bb); | |
d68d56b5 | 2456 | gsi_insert_seq_before (&cond_exp_gsi, cond_expr_stmt_list, |
86290011 | 2457 | GSI_SAME_STMT); |
ebfd146a | 2458 | } |
90eb75f2 | 2459 | update_ssa (TODO_update_ssa); |
ebfd146a | 2460 | } |