Line data Source code
1 : /* Vectorizer Specific Loop Manipulations
2 : Copyright (C) 2003-2026 Free Software Foundation, Inc.
3 : Contributed by Dorit Naishlos <dorit@il.ibm.com>
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"
25 : #include "backend.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "cfghooks.h"
29 : #include "tree-pass.h"
30 : #include "ssa.h"
31 : #include "fold-const.h"
32 : #include "cfganal.h"
33 : #include "gimplify.h"
34 : #include "gimple-iterator.h"
35 : #include "gimplify-me.h"
36 : #include "tree-cfg.h"
37 : #include "tree-ssa-loop-manip.h"
38 : #include "tree-into-ssa.h"
39 : #include "tree-ssa.h"
40 : #include "cfgloop.h"
41 : #include "tree-scalar-evolution.h"
42 : #include "tree-vectorizer.h"
43 : #include "tree-ssa-loop-ivopts.h"
44 : #include "gimple-fold.h"
45 : #include "tree-ssa-loop-niter.h"
46 : #include "internal-fn.h"
47 : #include "stor-layout.h"
48 : #include "optabs-query.h"
49 : #include "vec-perm-indices.h"
50 : #include "insn-config.h"
51 : #include "rtl.h"
52 : #include "recog.h"
53 : #include "langhooks.h"
54 : #include "tree-vector-builder.h"
55 : #include "optabs-tree.h"
56 : #include "hierarchical_discriminator.h"
57 :
58 :
59 : /*************************************************************************
60 : Simple Loop Peeling Utilities
61 :
62 : Utilities to support loop peeling for vectorization purposes.
63 : *************************************************************************/
64 :
65 :
66 : /* Renames the use *OP_P. */
67 :
68 : static void
69 904815 : rename_use_op (use_operand_p op_p)
70 : {
71 904815 : tree new_name;
72 :
73 904815 : if (TREE_CODE (USE_FROM_PTR (op_p)) != SSA_NAME)
74 : return;
75 :
76 901366 : new_name = get_current_def (USE_FROM_PTR (op_p));
77 :
78 : /* Something defined outside of the loop. */
79 901366 : if (!new_name)
80 : return;
81 :
82 : /* An ordinary ssa name defined in the loop. */
83 :
84 778131 : SET_USE (op_p, new_name);
85 : }
86 :
87 :
88 : /* Renames the variables in basic block BB. Allow renaming of PHI arguments
89 : on edges incoming from outer-block header if RENAME_FROM_OUTER_LOOP is
90 : true. */
91 :
92 : static void
93 111029 : rename_variables_in_bb (basic_block bb, bool rename_from_outer_loop)
94 : {
95 111029 : gimple *stmt;
96 111029 : use_operand_p use_p;
97 111029 : ssa_op_iter iter;
98 111029 : edge e;
99 111029 : edge_iterator ei;
100 111029 : class loop *loop = bb->loop_father;
101 111029 : class loop *outer_loop = NULL;
102 :
103 111029 : if (rename_from_outer_loop)
104 : {
105 935 : gcc_assert (loop);
106 935 : outer_loop = loop_outer (loop);
107 : }
108 :
109 737845 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
110 515787 : gsi_next (&gsi))
111 : {
112 515787 : stmt = gsi_stmt (gsi);
113 1267737 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
114 751950 : rename_use_op (use_p);
115 : }
116 :
117 227150 : FOR_EACH_EDGE (e, ei, bb->preds)
118 : {
119 116121 : if (!flow_bb_inside_loop_p (loop, e->src))
120 : {
121 34748 : if (!rename_from_outer_loop)
122 34462 : continue;
123 286 : if (e->src != outer_loop->header)
124 : {
125 177 : if (outer_loop->inner->next)
126 : {
127 : /* If outer_loop has 2 inner loops, allow there to
128 : be an extra basic block which decides which of the
129 : two loops to use using LOOP_VECTORIZED. */
130 174 : if (!single_pred_p (e->src)
131 42 : || single_pred (e->src) != outer_loop->header)
132 132 : continue;
133 : }
134 : }
135 : }
136 185919 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
137 104392 : gsi_next (&gsi))
138 104392 : rename_use_op (PHI_ARG_DEF_PTR_FROM_EDGE (gsi.phi (), e));
139 : }
140 111029 : }
141 :
142 :
143 : struct adjust_info
144 : {
145 : tree from, to;
146 : basic_block bb;
147 : };
148 :
149 : /* A stack of values to be adjusted in debug stmts. We have to
150 : process them LIFO, so that the closest substitution applies. If we
151 : processed them FIFO, without the stack, we might substitute uses
152 : with a PHI DEF that would soon become non-dominant, and when we got
153 : to the suitable one, it wouldn't have anything to substitute any
154 : more. */
155 : static vec<adjust_info, va_heap> adjust_vec;
156 :
157 : /* Adjust any debug stmts that referenced AI->from values to use the
158 : loop-closed AI->to, if the references are dominated by AI->bb and
159 : not by the definition of AI->from. */
160 :
161 : static void
162 78939 : adjust_debug_stmts_now (adjust_info *ai)
163 : {
164 78939 : basic_block bbphi = ai->bb;
165 78939 : tree orig_def = ai->from;
166 78939 : tree new_def = ai->to;
167 78939 : imm_use_iterator imm_iter;
168 78939 : gimple *stmt;
169 78939 : basic_block bbdef = gimple_bb (SSA_NAME_DEF_STMT (orig_def));
170 :
171 78939 : gcc_assert (dom_info_available_p (CDI_DOMINATORS));
172 :
173 : /* Adjust any debug stmts that held onto non-loop-closed
174 : references. */
175 392388 : FOR_EACH_IMM_USE_STMT (stmt, imm_iter, orig_def)
176 : {
177 234510 : use_operand_p use_p;
178 234510 : basic_block bbuse;
179 :
180 234510 : if (!is_gimple_debug (stmt))
181 176766 : continue;
182 :
183 57744 : gcc_assert (gimple_debug_bind_p (stmt));
184 :
185 57744 : bbuse = gimple_bb (stmt);
186 :
187 57744 : if ((bbuse == bbphi
188 57744 : || dominated_by_p (CDI_DOMINATORS, bbuse, bbphi))
189 59620 : && !(bbuse == bbdef
190 938 : || dominated_by_p (CDI_DOMINATORS, bbuse, bbdef)))
191 : {
192 0 : if (new_def)
193 0 : FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
194 0 : SET_USE (use_p, new_def);
195 : else
196 : {
197 0 : gimple_debug_bind_reset_value (stmt);
198 0 : update_stmt (stmt);
199 : }
200 : }
201 78939 : }
202 78939 : }
203 :
204 : /* Adjust debug stmts as scheduled before. */
205 :
206 : static void
207 33425 : adjust_vec_debug_stmts (void)
208 : {
209 33425 : if (!MAY_HAVE_DEBUG_BIND_STMTS)
210 : return;
211 :
212 12572 : gcc_assert (adjust_vec.exists ());
213 :
214 91403 : while (!adjust_vec.is_empty ())
215 : {
216 78831 : adjust_debug_stmts_now (&adjust_vec.last ());
217 78831 : adjust_vec.pop ();
218 : }
219 : }
220 :
221 : /* Adjust any debug stmts that referenced FROM values to use the
222 : loop-closed TO, if the references are dominated by BB and not by
223 : the definition of FROM. If adjust_vec is non-NULL, adjustments
224 : will be postponed until adjust_vec_debug_stmts is called. */
225 :
226 : static void
227 107545 : adjust_debug_stmts (tree from, tree to, basic_block bb)
228 : {
229 107545 : adjust_info ai;
230 :
231 107545 : if (MAY_HAVE_DEBUG_BIND_STMTS
232 107545 : && TREE_CODE (from) == SSA_NAME
233 97871 : && ! SSA_NAME_IS_DEFAULT_DEF (from)
234 204055 : && ! virtual_operand_p (from))
235 : {
236 78939 : ai.from = from;
237 78939 : ai.to = to;
238 78939 : ai.bb = bb;
239 :
240 78939 : if (adjust_vec.exists ())
241 78831 : adjust_vec.safe_push (ai);
242 : else
243 108 : adjust_debug_stmts_now (&ai);
244 : }
245 107545 : }
246 :
247 : /* Change E's phi arg in UPDATE_PHI to NEW_DEF, and record information
248 : to adjust any debug stmts that referenced the old phi arg,
249 : presumably non-loop-closed references left over from other
250 : transformations. */
251 :
252 : static void
253 216344 : adjust_phi_and_debug_stmts (gimple *update_phi, edge e, tree new_def)
254 : {
255 216344 : tree orig_def = PHI_ARG_DEF_FROM_EDGE (update_phi, e);
256 :
257 216344 : gcc_assert (TREE_CODE (orig_def) != SSA_NAME
258 : || orig_def != new_def);
259 :
260 216344 : SET_PHI_ARG_DEF (update_phi, e->dest_idx, new_def);
261 :
262 216344 : if (MAY_HAVE_DEBUG_BIND_STMTS)
263 84359 : adjust_debug_stmts (orig_def, PHI_RESULT (update_phi),
264 : gimple_bb (update_phi));
265 216344 : }
266 :
267 : /* Define one loop rgroup control CTRL from loop LOOP. INIT_CTRL is the value
268 : that the control should have during the first iteration and NEXT_CTRL is the
269 : value that it should have on subsequent iterations. */
270 :
271 : static void
272 86 : vect_set_loop_control (class loop *loop, tree ctrl, tree init_ctrl,
273 : tree next_ctrl)
274 : {
275 86 : gphi *phi = create_phi_node (ctrl, loop->header);
276 86 : add_phi_arg (phi, init_ctrl, loop_preheader_edge (loop), UNKNOWN_LOCATION);
277 86 : add_phi_arg (phi, next_ctrl, loop_latch_edge (loop), UNKNOWN_LOCATION);
278 86 : }
279 :
280 : /* Add SEQ to the end of LOOP's preheader block. */
281 :
282 : static void
283 18 : add_preheader_seq (class loop *loop, gimple_seq seq)
284 : {
285 18 : if (seq)
286 : {
287 15 : edge pe = loop_preheader_edge (loop);
288 15 : basic_block new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
289 15 : gcc_assert (!new_bb);
290 : }
291 18 : }
292 :
293 : /* Add SEQ to the beginning of LOOP's header block. */
294 :
295 : static void
296 0 : add_header_seq (class loop *loop, gimple_seq seq)
297 : {
298 0 : if (seq)
299 : {
300 0 : gimple_stmt_iterator gsi = gsi_after_labels (loop->header);
301 0 : gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
302 : }
303 0 : }
304 :
305 : /* Return true if the target can interleave elements of two vectors.
306 : OFFSET is 0 if the first half of the vectors should be interleaved
307 : or 1 if the second half should. When returning true, store the
308 : associated permutation in INDICES. */
309 :
310 : static bool
311 0 : interleave_supported_p (vec_perm_indices *indices, tree vectype,
312 : unsigned int offset)
313 : {
314 0 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (vectype);
315 0 : poly_uint64 base = exact_div (nelts, 2) * offset;
316 0 : vec_perm_builder sel (nelts, 2, 3);
317 0 : for (unsigned int i = 0; i < 3; ++i)
318 : {
319 0 : sel.quick_push (base + i);
320 0 : sel.quick_push (base + i + nelts);
321 : }
322 0 : indices->new_vector (sel, 2, nelts);
323 0 : return can_vec_perm_const_p (TYPE_MODE (vectype), TYPE_MODE (vectype),
324 0 : *indices);
325 0 : }
326 :
327 : /* Try to use permutes to define the masks in DEST_RGM using the masks
328 : in SRC_RGM, given that the former has twice as many masks as the
329 : latter. Return true on success, adding any new statements to SEQ. */
330 :
331 : static bool
332 0 : vect_maybe_permute_loop_masks (gimple_seq *seq, rgroup_controls *dest_rgm,
333 : rgroup_controls *src_rgm)
334 : {
335 0 : tree src_masktype = src_rgm->type;
336 0 : tree dest_masktype = dest_rgm->type;
337 0 : machine_mode src_mode = TYPE_MODE (src_masktype);
338 0 : insn_code icode1, icode2;
339 0 : if (dest_rgm->max_nscalars_per_iter <= src_rgm->max_nscalars_per_iter
340 0 : && (icode1 = optab_handler (vec_unpacku_hi_optab,
341 : src_mode)) != CODE_FOR_nothing
342 0 : && (icode2 = optab_handler (vec_unpacku_lo_optab,
343 : src_mode)) != CODE_FOR_nothing)
344 : {
345 : /* Unpacking the source masks gives at least as many mask bits as
346 : we need. We can then VIEW_CONVERT any excess bits away. */
347 0 : machine_mode dest_mode = insn_data[icode1].operand[0].mode;
348 0 : gcc_assert (dest_mode == insn_data[icode2].operand[0].mode);
349 0 : tree unpack_masktype = vect_halve_mask_nunits (src_masktype, dest_mode);
350 0 : for (unsigned int i = 0; i < dest_rgm->controls.length (); ++i)
351 : {
352 0 : tree src = src_rgm->controls[i / 2];
353 0 : tree dest = dest_rgm->controls[i];
354 0 : tree_code code = ((i & 1) == (BYTES_BIG_ENDIAN ? 0 : 1)
355 0 : ? VEC_UNPACK_HI_EXPR
356 : : VEC_UNPACK_LO_EXPR);
357 0 : gassign *stmt;
358 0 : if (dest_masktype == unpack_masktype)
359 0 : stmt = gimple_build_assign (dest, code, src);
360 : else
361 : {
362 0 : tree temp = make_ssa_name (unpack_masktype);
363 0 : stmt = gimple_build_assign (temp, code, src);
364 0 : gimple_seq_add_stmt (seq, stmt);
365 0 : stmt = gimple_build_assign (dest, VIEW_CONVERT_EXPR,
366 : build1 (VIEW_CONVERT_EXPR,
367 : dest_masktype, temp));
368 : }
369 0 : gimple_seq_add_stmt (seq, stmt);
370 : }
371 : return true;
372 : }
373 0 : vec_perm_indices indices[2];
374 0 : if (dest_masktype == src_masktype
375 0 : && interleave_supported_p (&indices[0], src_masktype, 0)
376 0 : && interleave_supported_p (&indices[1], src_masktype, 1))
377 : {
378 : /* The destination requires twice as many mask bits as the source, so
379 : we can use interleaving permutes to double up the number of bits. */
380 : tree masks[2];
381 0 : for (unsigned int i = 0; i < 2; ++i)
382 0 : masks[i] = vect_gen_perm_mask_checked (src_masktype, indices[i]);
383 0 : for (unsigned int i = 0; i < dest_rgm->controls.length (); ++i)
384 : {
385 0 : tree src = src_rgm->controls[i / 2];
386 0 : tree dest = dest_rgm->controls[i];
387 0 : gimple *stmt = gimple_build_assign (dest, VEC_PERM_EXPR,
388 0 : src, src, masks[i & 1]);
389 0 : gimple_seq_add_stmt (seq, stmt);
390 : }
391 0 : return true;
392 : }
393 : return false;
394 0 : }
395 :
396 : /* Populate DEST_RGM->controls, given that they should add up to STEP.
397 :
398 : STEP = MIN_EXPR <ivtmp_34, VF>;
399 :
400 : First length (MIN (X, VF/N)):
401 : loop_len_15 = MIN_EXPR <STEP, VF/N>;
402 :
403 : Second length:
404 : tmp = STEP - loop_len_15;
405 : loop_len_16 = MIN (tmp, VF/N);
406 :
407 : Third length:
408 : tmp2 = tmp - loop_len_16;
409 : loop_len_17 = MIN (tmp2, VF/N);
410 :
411 : Last length:
412 : loop_len_18 = tmp2 - loop_len_17;
413 : */
414 :
415 : static void
416 0 : vect_adjust_loop_lens_control (tree iv_type, gimple_seq *seq,
417 : rgroup_controls *dest_rgm, tree step)
418 : {
419 0 : tree ctrl_type = dest_rgm->type;
420 0 : poly_uint64 nitems_per_ctrl
421 0 : = TYPE_VECTOR_SUBPARTS (ctrl_type) * dest_rgm->factor;
422 0 : tree length_limit = build_int_cst (iv_type, nitems_per_ctrl);
423 :
424 0 : for (unsigned int i = 0; i < dest_rgm->controls.length (); ++i)
425 : {
426 0 : tree ctrl = dest_rgm->controls[i];
427 0 : if (i == 0)
428 : {
429 : /* First iteration: MIN (X, VF/N) capped to the range [0, VF/N]. */
430 0 : gassign *assign
431 0 : = gimple_build_assign (ctrl, MIN_EXPR, step, length_limit);
432 0 : gimple_seq_add_stmt (seq, assign);
433 : }
434 0 : else if (i == dest_rgm->controls.length () - 1)
435 : {
436 : /* Last iteration: Remain capped to the range [0, VF/N]. */
437 0 : gassign *assign = gimple_build_assign (ctrl, MINUS_EXPR, step,
438 0 : dest_rgm->controls[i - 1]);
439 0 : gimple_seq_add_stmt (seq, assign);
440 : }
441 : else
442 : {
443 : /* (MIN (remain, VF*I/N)) capped to the range [0, VF/N]. */
444 0 : step = gimple_build (seq, MINUS_EXPR, iv_type, step,
445 0 : dest_rgm->controls[i - 1]);
446 0 : gassign *assign
447 0 : = gimple_build_assign (ctrl, MIN_EXPR, step, length_limit);
448 0 : gimple_seq_add_stmt (seq, assign);
449 : }
450 : }
451 0 : }
452 :
453 : /* Stores the standard position for induction variable increment in belonging to
454 : LOOP_EXIT (just before the exit condition of the given exit to BSI.
455 : INSERT_AFTER is set to true if the increment should be inserted after
456 : *BSI. */
457 :
458 : void
459 63298 : vect_iv_increment_position (edge loop_exit, gimple_stmt_iterator *bsi,
460 : bool *insert_after)
461 : {
462 63298 : basic_block bb = loop_exit->src;
463 63298 : *bsi = gsi_last_bb (bb);
464 63298 : *insert_after = false;
465 63298 : }
466 :
467 : /* Helper for vect_set_loop_condition_partial_vectors. Generate definitions
468 : for all the rgroup controls in RGC and return a control that is nonzero
469 : when the loop needs to iterate. Add any new preheader statements to
470 : PREHEADER_SEQ. Use LOOP_COND_GSI to insert code before the exit gcond.
471 :
472 : RGC belongs to loop LOOP. The loop originally iterated NITERS
473 : times and has been vectorized according to LOOP_VINFO.
474 :
475 : If NITERS_SKIP is nonnull, the first iteration of the vectorized loop
476 : starts with NITERS_SKIP dummy iterations of the scalar loop before
477 : the real work starts. The mask elements for these dummy iterations
478 : must be 0, to ensure that the extra iterations do not have an effect.
479 :
480 : It is known that:
481 :
482 : NITERS * RGC->max_nscalars_per_iter * RGC->factor
483 :
484 : does not overflow. However, MIGHT_WRAP_P says whether an induction
485 : variable that starts at 0 and has step:
486 :
487 : VF * RGC->max_nscalars_per_iter * RGC->factor
488 :
489 : might overflow before hitting a value above:
490 :
491 : (NITERS + NITERS_SKIP) * RGC->max_nscalars_per_iter * RGC->factor
492 :
493 : This means that we cannot guarantee that such an induction variable
494 : would ever hit a value that produces a set of all-false masks or zero
495 : lengths for RGC.
496 :
497 : Note: the cost of the code generated by this function is modeled
498 : by vect_estimate_min_profitable_iters, so changes here may need
499 : corresponding changes there. */
500 :
501 : static tree
502 0 : vect_set_loop_controls_directly (class loop *loop, loop_vec_info loop_vinfo,
503 : gimple_seq *preheader_seq,
504 : gimple_seq *header_seq,
505 : gimple_stmt_iterator loop_cond_gsi,
506 : rgroup_controls *rgc, tree niters,
507 : tree niters_skip, bool might_wrap_p,
508 : tree *iv_step, tree *compare_step)
509 : {
510 0 : tree compare_type = LOOP_VINFO_RGROUP_COMPARE_TYPE (loop_vinfo);
511 0 : tree iv_type = LOOP_VINFO_RGROUP_IV_TYPE (loop_vinfo);
512 0 : bool use_masks_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
513 :
514 0 : tree ctrl_type = rgc->type;
515 0 : unsigned int nitems_per_iter = rgc->max_nscalars_per_iter * rgc->factor;
516 0 : poly_uint64 nitems_per_ctrl = TYPE_VECTOR_SUBPARTS (ctrl_type) * rgc->factor;
517 0 : tree length_limit = NULL_TREE;
518 : /* For length, we need length_limit to ensure length in range. */
519 0 : if (!use_masks_p)
520 0 : length_limit = build_int_cst (compare_type, nitems_per_ctrl);
521 :
522 : /* Calculate the maximum number of item values that the rgroup
523 : handles in total, the number that it handles for each iteration
524 : of the vector loop, and the number that it should skip during the
525 : first iteration of the vector loop. */
526 0 : tree nitems_total = niters;
527 0 : tree nitems_vf
528 0 : = build_int_cst (iv_type, LOOP_VINFO_VECT_FACTOR (loop_vinfo));
529 0 : tree nitems_step
530 0 : = LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo)
531 0 : ? gimple_convert (preheader_seq, iv_type,
532 : LOOP_VINFO_IV_INCREMENT (loop_vinfo))
533 0 : : gimple_convert (&loop_cond_gsi, true, GSI_SAME_STMT, UNKNOWN_LOCATION,
534 : iv_type, LOOP_VINFO_IV_INCREMENT (loop_vinfo));
535 :
536 0 : tree nitems_skip = niters_skip;
537 0 : if (nitems_per_iter != 1)
538 : {
539 : /* We checked before setting LOOP_VINFO_USING_PARTIAL_VECTORS_P that
540 : these multiplications don't overflow. */
541 0 : tree compare_factor = build_int_cst (compare_type, nitems_per_iter);
542 0 : tree iv_factor = build_int_cst (iv_type, nitems_per_iter);
543 0 : nitems_total = gimple_build (preheader_seq, MULT_EXPR, compare_type,
544 : nitems_total, compare_factor);
545 0 : nitems_vf = gimple_build (preheader_seq, MULT_EXPR, iv_type,
546 : nitems_vf, iv_factor);
547 0 : nitems_step = LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo)
548 0 : ? gimple_build (preheader_seq, MULT_EXPR, iv_type,
549 : nitems_step, iv_factor)
550 0 : : gimple_build (&loop_cond_gsi, true, GSI_SAME_STMT,
551 : UNKNOWN_LOCATION, MULT_EXPR, iv_type,
552 : nitems_step, iv_factor);
553 0 : if (nitems_skip)
554 0 : nitems_skip = gimple_build (preheader_seq, MULT_EXPR, compare_type,
555 : nitems_skip, compare_factor);
556 : }
557 :
558 : /* Create an induction variable that counts the number of items
559 : processed. */
560 0 : tree index_before_incr, index_after_incr;
561 0 : gimple_stmt_iterator incr_gsi;
562 0 : bool insert_after;
563 0 : edge exit_e = LOOP_VINFO_MAIN_EXIT (loop_vinfo);
564 0 : vect_iv_increment_position (exit_e, &incr_gsi, &insert_after);
565 0 : if (LOOP_VINFO_USING_DECREMENTING_IV_P (loop_vinfo))
566 : {
567 : /* Create an IV that counts down from niters_total and whose step
568 : is the (variable) amount processed in the current iteration:
569 : ...
570 : _10 = (unsigned long) count_12(D);
571 : ...
572 : # ivtmp_9 = PHI <ivtmp_35(6), _10(5)>
573 : _36 = (MIN_EXPR | SELECT_VL) <ivtmp_9, POLY_INT_CST [4, 4]>;
574 : ...
575 : vect__4.8_28 = .LEN_LOAD (_17, 32B, _36, 0);
576 : ...
577 : ivtmp_35 = ivtmp_9 - POLY_INT_CST [4, 4];
578 : ...
579 : if (ivtmp_9 > POLY_INT_CST [4, 4])
580 : goto <bb 4>; [83.33%]
581 : else
582 : goto <bb 5>; [16.67%]
583 : */
584 0 : nitems_total = gimple_convert (preheader_seq, iv_type, nitems_total);
585 0 : tree step = rgc->controls.length () == 1 ? rgc->controls[0]
586 0 : : make_ssa_name (iv_type);
587 : /* Create decrement IV. */
588 0 : if (LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
589 : {
590 0 : create_iv (nitems_total, MINUS_EXPR, step, NULL_TREE, loop, &incr_gsi,
591 : insert_after, &index_before_incr, &index_after_incr);
592 0 : tree vectype = build_zero_cst (rgc->type);
593 0 : tree len = gimple_build (header_seq, IFN_SELECT_VL, iv_type,
594 : index_before_incr, nitems_vf,
595 : vectype);
596 0 : gimple_seq_add_stmt (header_seq, gimple_build_assign (step, len));
597 0 : len = gimple_convert (header_seq, sizetype, len);
598 :
599 : /* Remove the previous initialization of IV_INCREMENT to VARYING. */
600 0 : gimple *varying_def
601 0 : = SSA_NAME_DEF_STMT (LOOP_VINFO_IV_INCREMENT (loop_vinfo));
602 0 : auto def_gsi = gsi_for_stmt (varying_def);
603 0 : gsi_remove (&def_gsi, true);
604 :
605 : /* Set the LOOP_VINFO_IV_INCREMENT to be len. */
606 0 : gassign* assign_iv_increment
607 0 : = gimple_build_assign (LOOP_VINFO_IV_INCREMENT (loop_vinfo), len);
608 0 : gimple_seq_add_stmt (header_seq, assign_iv_increment);
609 : }
610 : else
611 : {
612 0 : create_iv (nitems_total, MINUS_EXPR, nitems_step, NULL_TREE, loop,
613 : &incr_gsi, insert_after, &index_before_incr,
614 : &index_after_incr);
615 0 : gimple_seq_add_stmt (header_seq,
616 0 : gimple_build_assign (step, MIN_EXPR,
617 : index_before_incr,
618 : nitems_step));
619 : }
620 0 : *iv_step = step;
621 0 : *compare_step = nitems_vf;
622 0 : return LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo) ? index_after_incr
623 0 : : index_before_incr;
624 : }
625 :
626 : /* Create increment IV. */
627 0 : create_iv (build_int_cst (iv_type, 0), PLUS_EXPR, nitems_step, NULL_TREE,
628 : loop, &incr_gsi, insert_after, &index_before_incr,
629 : &index_after_incr,
630 0 : LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo));
631 :
632 0 : tree zero_index = build_int_cst (compare_type, 0);
633 0 : tree test_index, test_limit, first_limit;
634 0 : gimple_stmt_iterator *test_gsi;
635 0 : if (might_wrap_p)
636 : {
637 : /* In principle the loop should stop iterating once the incremented
638 : IV reaches a value greater than or equal to:
639 :
640 : NITEMS_TOTAL +[infinite-prec] NITEMS_SKIP
641 :
642 : However, there's no guarantee that this addition doesn't overflow
643 : the comparison type, or that the IV hits a value above it before
644 : wrapping around. We therefore adjust the limit down by one
645 : IV step:
646 :
647 : (NITEMS_TOTAL +[infinite-prec] NITEMS_SKIP)
648 : -[infinite-prec] NITEMS_STEP
649 :
650 : and compare the IV against this limit _before_ incrementing it.
651 : Since the comparison type is unsigned, we actually want the
652 : subtraction to saturate at zero:
653 :
654 : (NITEMS_TOTAL +[infinite-prec] NITEMS_SKIP)
655 : -[sat] NITEMS_STEP
656 :
657 : And since NITEMS_SKIP < NITEMS_STEP, we can reassociate this as:
658 :
659 : NITEMS_TOTAL -[sat] (NITEMS_STEP - NITEMS_SKIP)
660 :
661 : where the rightmost subtraction can be done directly in
662 : COMPARE_TYPE. */
663 0 : test_index = index_before_incr;
664 0 : tree adjust = gimple_convert (preheader_seq, compare_type,
665 : nitems_vf);
666 0 : if (nitems_skip)
667 0 : adjust = gimple_build (preheader_seq, MINUS_EXPR, compare_type,
668 : adjust, nitems_skip);
669 0 : test_limit = gimple_build (preheader_seq, MAX_EXPR, compare_type,
670 : nitems_total, adjust);
671 0 : test_limit = gimple_build (preheader_seq, MINUS_EXPR, compare_type,
672 : test_limit, adjust);
673 0 : test_gsi = &incr_gsi;
674 :
675 : /* Get a safe limit for the first iteration. */
676 0 : if (nitems_skip)
677 : {
678 : /* The first vector iteration can handle at most NITEMS_STEP
679 : items. NITEMS_STEP <= CONST_LIMIT, and adding
680 : NITEMS_SKIP to that cannot overflow. */
681 0 : tree const_limit = build_int_cst (compare_type,
682 0 : LOOP_VINFO_VECT_FACTOR (loop_vinfo)
683 0 : * nitems_per_iter);
684 0 : first_limit = gimple_build (preheader_seq, MIN_EXPR, compare_type,
685 : nitems_total, const_limit);
686 0 : first_limit = gimple_build (preheader_seq, PLUS_EXPR, compare_type,
687 : first_limit, nitems_skip);
688 : }
689 : else
690 : /* For the first iteration it doesn't matter whether the IV hits
691 : a value above NITEMS_TOTAL. That only matters for the latch
692 : condition. */
693 : first_limit = nitems_total;
694 : }
695 : else
696 : {
697 : /* Test the incremented IV, which will always hit a value above
698 : the bound before wrapping. */
699 0 : test_index = index_after_incr;
700 0 : test_limit = nitems_total;
701 0 : if (nitems_skip)
702 0 : test_limit = gimple_build (preheader_seq, PLUS_EXPR, compare_type,
703 : test_limit, nitems_skip);
704 : test_gsi = &loop_cond_gsi;
705 :
706 : first_limit = test_limit;
707 : }
708 :
709 : /* Convert the IV value to the comparison type (either a no-op or
710 : a demotion). */
711 0 : gimple_seq test_seq = NULL;
712 0 : test_index = gimple_convert (&test_seq, compare_type, test_index);
713 0 : gsi_insert_seq_before (test_gsi, test_seq, GSI_SAME_STMT);
714 :
715 : /* Provide a definition of each control in the group. */
716 0 : tree next_ctrl = NULL_TREE;
717 0 : tree ctrl;
718 0 : unsigned int i;
719 0 : FOR_EACH_VEC_ELT_REVERSE (rgc->controls, i, ctrl)
720 : {
721 : /* Previous controls will cover BIAS items. This control covers the
722 : next batch. */
723 0 : poly_uint64 bias = nitems_per_ctrl * i;
724 0 : tree bias_tree = build_int_cst (compare_type, bias);
725 :
726 : /* See whether the first iteration of the vector loop is known
727 : to have a full control. */
728 0 : poly_uint64 const_limit;
729 0 : bool first_iteration_full
730 0 : = (poly_int_tree_p (first_limit, &const_limit)
731 0 : && known_ge (const_limit, (i + 1) * nitems_per_ctrl));
732 :
733 : /* Rather than have a new IV that starts at BIAS and goes up to
734 : TEST_LIMIT, prefer to use the same 0-based IV for each control
735 : and adjust the bound down by BIAS. */
736 0 : tree this_test_limit = test_limit;
737 0 : if (i != 0)
738 : {
739 0 : this_test_limit = gimple_build (preheader_seq, MAX_EXPR,
740 : compare_type, this_test_limit,
741 : bias_tree);
742 0 : this_test_limit = gimple_build (preheader_seq, MINUS_EXPR,
743 : compare_type, this_test_limit,
744 : bias_tree);
745 : }
746 :
747 : /* Create the initial control. First include all items that
748 : are within the loop limit. */
749 0 : tree init_ctrl = NULL_TREE;
750 0 : if (!first_iteration_full)
751 : {
752 0 : tree start, end;
753 0 : if (first_limit == test_limit)
754 : {
755 : /* Use a natural test between zero (the initial IV value)
756 : and the loop limit. The "else" block would be valid too,
757 : but this choice can avoid the need to load BIAS_TREE into
758 : a register. */
759 : start = zero_index;
760 : end = this_test_limit;
761 : }
762 : else
763 : {
764 : /* FIRST_LIMIT is the maximum number of items handled by the
765 : first iteration of the vector loop. Test the portion
766 : associated with this control. */
767 0 : start = bias_tree;
768 0 : end = first_limit;
769 : }
770 :
771 0 : if (use_masks_p)
772 0 : init_ctrl = vect_gen_while (preheader_seq, ctrl_type,
773 : start, end, "max_mask");
774 : else
775 : {
776 0 : init_ctrl = make_temp_ssa_name (compare_type, NULL, "max_len");
777 0 : gimple_seq seq = vect_gen_len (init_ctrl, start,
778 : end, length_limit);
779 0 : gimple_seq_add_seq (preheader_seq, seq);
780 : }
781 : }
782 :
783 : /* Now AND out the bits that are within the number of skipped
784 : items. */
785 0 : poly_uint64 const_skip;
786 0 : if (nitems_skip
787 0 : && !(poly_int_tree_p (nitems_skip, &const_skip)
788 0 : && known_le (const_skip, bias)))
789 : {
790 0 : gcc_assert (use_masks_p);
791 0 : tree unskipped_mask = vect_gen_while_not (preheader_seq, ctrl_type,
792 : bias_tree, nitems_skip);
793 0 : if (init_ctrl)
794 0 : init_ctrl = gimple_build (preheader_seq, BIT_AND_EXPR, ctrl_type,
795 : init_ctrl, unskipped_mask);
796 : else
797 : init_ctrl = unskipped_mask;
798 : }
799 :
800 0 : if (!init_ctrl)
801 : {
802 : /* First iteration is full. */
803 0 : if (use_masks_p)
804 0 : init_ctrl = build_minus_one_cst (ctrl_type);
805 : else
806 : init_ctrl = length_limit;
807 : }
808 :
809 : /* Get the control value for the next iteration of the loop. */
810 0 : if (use_masks_p)
811 : {
812 0 : gimple_seq stmts = NULL;
813 0 : next_ctrl = vect_gen_while (&stmts, ctrl_type, test_index,
814 : this_test_limit, "next_mask");
815 0 : gsi_insert_seq_before (test_gsi, stmts, GSI_SAME_STMT);
816 : }
817 : else
818 : {
819 0 : next_ctrl = make_temp_ssa_name (compare_type, NULL, "next_len");
820 0 : gimple_seq seq = vect_gen_len (next_ctrl, test_index, this_test_limit,
821 : length_limit);
822 0 : gsi_insert_seq_before (test_gsi, seq, GSI_SAME_STMT);
823 : }
824 :
825 0 : vect_set_loop_control (loop, ctrl, init_ctrl, next_ctrl);
826 : }
827 :
828 0 : int partial_load_bias = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
829 0 : if (partial_load_bias != 0)
830 : {
831 0 : tree adjusted_len = rgc->bias_adjusted_ctrl;
832 0 : gassign *minus = gimple_build_assign (adjusted_len, PLUS_EXPR,
833 0 : rgc->controls[0],
834 : build_int_cst
835 0 : (TREE_TYPE (rgc->controls[0]),
836 0 : partial_load_bias));
837 0 : gimple_seq_add_stmt (header_seq, minus);
838 : }
839 :
840 : return next_ctrl;
841 : }
842 :
843 : /* Set up the iteration condition and rgroup controls for LOOP, given
844 : that LOOP_VINFO_USING_PARTIAL_VECTORS_P is true for the vectorized
845 : loop. LOOP_VINFO describes the vectorization of LOOP. NITERS is
846 : the number of iterations of the original scalar loop that should be
847 : handled by the vector loop. NITERS_MAYBE_ZERO and FINAL_IV are as
848 : for vect_set_loop_condition.
849 :
850 : Insert the branch-back condition before LOOP_COND_GSI and return the
851 : final gcond. */
852 :
853 : static gcond *
854 0 : vect_set_loop_condition_partial_vectors (class loop *loop, edge exit_edge,
855 : loop_vec_info loop_vinfo, tree niters,
856 : tree final_iv, bool niters_maybe_zero,
857 : gimple_stmt_iterator loop_cond_gsi)
858 : {
859 0 : gimple_seq preheader_seq = NULL;
860 0 : gimple_seq header_seq = NULL;
861 :
862 0 : bool use_masks_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
863 0 : tree compare_type = LOOP_VINFO_RGROUP_COMPARE_TYPE (loop_vinfo);
864 0 : unsigned int compare_precision = TYPE_PRECISION (compare_type);
865 0 : tree orig_niters = niters;
866 :
867 : /* Type of the initial value of NITERS. */
868 0 : tree ni_actual_type = TREE_TYPE (niters);
869 0 : unsigned int ni_actual_precision = TYPE_PRECISION (ni_actual_type);
870 0 : tree niters_skip = LOOP_VINFO_MASK_SKIP_NITERS (loop_vinfo);
871 0 : if (niters_skip)
872 0 : niters_skip = gimple_convert (&preheader_seq, compare_type, niters_skip);
873 :
874 : /* Convert NITERS to the same size as the compare. */
875 0 : if (compare_precision > ni_actual_precision
876 0 : && niters_maybe_zero)
877 : {
878 : /* We know that there is always at least one iteration, so if the
879 : count is zero then it must have wrapped. Cope with this by
880 : subtracting 1 before the conversion and adding 1 to the result. */
881 0 : gcc_assert (TYPE_UNSIGNED (ni_actual_type));
882 0 : niters = gimple_build (&preheader_seq, PLUS_EXPR, ni_actual_type,
883 : niters, build_minus_one_cst (ni_actual_type));
884 0 : niters = gimple_convert (&preheader_seq, compare_type, niters);
885 0 : niters = gimple_build (&preheader_seq, PLUS_EXPR, compare_type,
886 : niters, build_one_cst (compare_type));
887 : }
888 : else
889 0 : niters = gimple_convert (&preheader_seq, compare_type, niters);
890 :
891 : /* Iterate over all the rgroups and fill in their controls. We could use
892 : the first control from any rgroup for the loop condition; here we
893 : arbitrarily pick the last. */
894 0 : tree test_ctrl = NULL_TREE;
895 0 : tree iv_step = NULL_TREE;
896 0 : tree compare_step = NULL_TREE;
897 0 : rgroup_controls *rgc;
898 0 : rgroup_controls *iv_rgc = nullptr;
899 0 : unsigned int i;
900 0 : auto_vec<rgroup_controls> *controls = use_masks_p
901 0 : ? &LOOP_VINFO_MASKS (loop_vinfo).rgc_vec
902 : : &LOOP_VINFO_LENS (loop_vinfo);
903 0 : FOR_EACH_VEC_ELT (*controls, i, rgc)
904 0 : if (!rgc->controls.is_empty ())
905 : {
906 : /* First try using permutes. This adds a single vector
907 : instruction to the loop for each mask, but needs no extra
908 : loop invariants or IVs. */
909 0 : unsigned int nmasks = i + 1;
910 0 : if (use_masks_p && (nmasks & 1) == 0)
911 : {
912 0 : rgroup_controls *half_rgc = &(*controls)[nmasks / 2 - 1];
913 0 : if (!half_rgc->controls.is_empty ()
914 0 : && vect_maybe_permute_loop_masks (&header_seq, rgc, half_rgc))
915 0 : continue;
916 : }
917 :
918 0 : if (!LOOP_VINFO_USING_DECREMENTING_IV_P (loop_vinfo)
919 0 : || !iv_rgc
920 0 : || (iv_rgc->max_nscalars_per_iter * iv_rgc->factor
921 0 : != rgc->max_nscalars_per_iter * rgc->factor))
922 : {
923 : /* See whether zero-based IV would ever generate all-false masks
924 : or zero length before wrapping around. */
925 0 : bool might_wrap_p = vect_rgroup_iv_might_wrap_p (loop_vinfo, rgc);
926 :
927 : /* Set up all controls for this group. */
928 0 : test_ctrl
929 0 : = vect_set_loop_controls_directly (loop, loop_vinfo,
930 : &preheader_seq, &header_seq,
931 : loop_cond_gsi, rgc, niters,
932 : niters_skip, might_wrap_p,
933 : &iv_step, &compare_step);
934 :
935 0 : iv_rgc = rgc;
936 : }
937 :
938 0 : if (LOOP_VINFO_USING_DECREMENTING_IV_P (loop_vinfo)
939 0 : && rgc->controls.length () > 1)
940 : {
941 : /* vect_set_loop_controls_directly creates an IV whose step
942 : is equal to the expected sum of RGC->controls. Use that
943 : information to populate RGC->controls. */
944 0 : tree iv_type = LOOP_VINFO_RGROUP_IV_TYPE (loop_vinfo);
945 0 : gcc_assert (iv_step);
946 0 : vect_adjust_loop_lens_control (iv_type, &header_seq, rgc, iv_step);
947 : }
948 : }
949 :
950 : /* Emit all accumulated statements. */
951 0 : add_preheader_seq (loop, preheader_seq);
952 0 : add_header_seq (loop, header_seq);
953 :
954 : /* Get a boolean result that tells us whether to iterate. */
955 0 : gcond *cond_stmt;
956 0 : if (LOOP_VINFO_USING_DECREMENTING_IV_P (loop_vinfo)
957 0 : && !LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
958 : {
959 0 : gcc_assert (compare_step);
960 0 : tree_code code = (exit_edge->flags & EDGE_TRUE_VALUE) ? LE_EXPR : GT_EXPR;
961 0 : cond_stmt = gimple_build_cond (code, test_ctrl, compare_step, NULL_TREE,
962 : NULL_TREE);
963 0 : }
964 : else
965 : {
966 0 : tree_code code = (exit_edge->flags & EDGE_TRUE_VALUE) ? EQ_EXPR : NE_EXPR;
967 0 : tree zero_ctrl = build_zero_cst (TREE_TYPE (test_ctrl));
968 0 : cond_stmt
969 0 : = gimple_build_cond (code, test_ctrl, zero_ctrl, NULL_TREE, NULL_TREE);
970 : }
971 0 : gsi_insert_before (&loop_cond_gsi, cond_stmt, GSI_SAME_STMT);
972 :
973 : /* The loop iterates (NITERS - 1) / VF + 1 times.
974 : Subtract one from this to get the latch count. */
975 0 : tree step = build_int_cst (compare_type,
976 0 : LOOP_VINFO_VECT_FACTOR (loop_vinfo));
977 0 : tree niters_minus_one = fold_build2 (PLUS_EXPR, compare_type, niters,
978 : build_minus_one_cst (compare_type));
979 0 : loop->nb_iterations = fold_build2 (TRUNC_DIV_EXPR, compare_type,
980 : niters_minus_one, step);
981 :
982 0 : if (final_iv)
983 : {
984 0 : gassign *assign;
985 : /* If vectorizing an inverted early break loop we have to restart the
986 : scalar loop at niters - vf. This matches what we do in
987 : vect_gen_vector_loop_niters_mult_vf for non-masked loops. */
988 0 : if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
989 : {
990 0 : tree ftype = TREE_TYPE (orig_niters);
991 0 : tree vf = build_int_cst (ftype, LOOP_VINFO_VECT_FACTOR (loop_vinfo));
992 0 : assign = gimple_build_assign (final_iv, MINUS_EXPR, orig_niters, vf);
993 : }
994 : else
995 0 : assign = gimple_build_assign (final_iv, orig_niters);
996 0 : gsi_insert_on_edge_immediate (exit_edge, assign);
997 : }
998 :
999 0 : return cond_stmt;
1000 : }
1001 :
1002 : /* Set up the iteration condition and rgroup controls for LOOP in AVX512
1003 : style, given that LOOP_VINFO_USING_PARTIAL_VECTORS_P is true for the
1004 : vectorized loop. LOOP_VINFO describes the vectorization of LOOP. NITERS is
1005 : the number of iterations of the original scalar loop that should be
1006 : handled by the vector loop. NITERS_MAYBE_ZERO and FINAL_IV are as
1007 : for vect_set_loop_condition.
1008 :
1009 : Insert the branch-back condition before LOOP_COND_GSI and return the
1010 : final gcond. */
1011 :
1012 : static gcond *
1013 18 : vect_set_loop_condition_partial_vectors_avx512 (class loop *loop,
1014 : edge exit_edge,
1015 : loop_vec_info loop_vinfo, tree niters,
1016 : tree final_iv,
1017 : bool niters_maybe_zero,
1018 : gimple_stmt_iterator loop_cond_gsi)
1019 : {
1020 18 : tree niters_skip = LOOP_VINFO_MASK_SKIP_NITERS (loop_vinfo);
1021 18 : tree iv_type = LOOP_VINFO_RGROUP_IV_TYPE (loop_vinfo);
1022 18 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1023 18 : tree orig_niters = niters;
1024 18 : gimple_seq preheader_seq = NULL;
1025 :
1026 : /* Create an IV that counts down from niters and whose step
1027 : is the number of iterations processed in the current iteration.
1028 : Produce the controls with compares like the following.
1029 :
1030 : # iv_2 = PHI <niters, iv_3>
1031 : rem_4 = MIN <iv_2, VF>;
1032 : remv_6 = { rem_4, rem_4, rem_4, ... }
1033 : mask_5 = { 0, 0, 1, 1, 2, 2, ... } < remv6;
1034 : iv_3 = iv_2 - VF;
1035 : if (iv_2 > VF)
1036 : continue;
1037 :
1038 : Where the constant is built with elements at most VF - 1 and
1039 : repetitions according to max_nscalars_per_iter which is guaranteed
1040 : to be the same within a group. */
1041 :
1042 : /* Convert NITERS to the determined IV type. */
1043 18 : if (TYPE_PRECISION (iv_type) > TYPE_PRECISION (TREE_TYPE (niters))
1044 18 : && niters_maybe_zero)
1045 : {
1046 : /* We know that there is always at least one iteration, so if the
1047 : count is zero then it must have wrapped. Cope with this by
1048 : subtracting 1 before the conversion and adding 1 to the result. */
1049 0 : gcc_assert (TYPE_UNSIGNED (TREE_TYPE (niters)));
1050 0 : niters = gimple_build (&preheader_seq, PLUS_EXPR, TREE_TYPE (niters),
1051 0 : niters, build_minus_one_cst (TREE_TYPE (niters)));
1052 0 : niters = gimple_convert (&preheader_seq, iv_type, niters);
1053 0 : niters = gimple_build (&preheader_seq, PLUS_EXPR, iv_type,
1054 : niters, build_one_cst (iv_type));
1055 : }
1056 : else
1057 18 : niters = gimple_convert (&preheader_seq, iv_type, niters);
1058 :
1059 : /* Bias the initial value of the IV in case we need to skip iterations
1060 : at the beginning. */
1061 18 : tree niters_adj = niters;
1062 18 : if (niters_skip)
1063 : {
1064 1 : tree skip = gimple_convert (&preheader_seq, iv_type, niters_skip);
1065 1 : niters_adj = gimple_build (&preheader_seq, PLUS_EXPR,
1066 : iv_type, niters, skip);
1067 : }
1068 :
1069 18 : gimple_stmt_iterator incr_gsi;
1070 18 : tree index_before_incr, index_after_incr;
1071 18 : bool insert_after;
1072 18 : vect_iv_increment_position (exit_edge, &incr_gsi, &insert_after);
1073 :
1074 : /* The iteration step is the vectorization factor. */
1075 18 : tree iv_step = gimple_convert (&preheader_seq, iv_type,
1076 : LOOP_VINFO_IV_INCREMENT (loop_vinfo));
1077 :
1078 : /* Create the decrement IV. */
1079 18 : create_iv (niters_adj, MINUS_EXPR, iv_step, NULL_TREE, loop,
1080 : &incr_gsi, insert_after, &index_before_incr,
1081 : &index_after_incr);
1082 :
1083 : /* Iterate over all the rgroups and fill in their controls. */
1084 80 : for (auto &rgc : LOOP_VINFO_MASKS (loop_vinfo).rgc_vec)
1085 : {
1086 26 : if (rgc.controls.is_empty ())
1087 6 : continue;
1088 :
1089 20 : tree ctrl_type = rgc.type;
1090 20 : poly_uint64 nitems_per_ctrl = TYPE_VECTOR_SUBPARTS (ctrl_type);
1091 :
1092 20 : tree vectype = rgc.compare_type;
1093 :
1094 : /* index_after_incr is the IV specifying the remaining iterations in
1095 : the next iteration. */
1096 20 : tree rem = index_after_incr;
1097 : /* When the data type for the compare to produce the mask is
1098 : smaller than the IV type we need to saturate. Saturate to
1099 : the smallest possible value (IV_TYPE) so we only have to
1100 : saturate once (CSE will catch redundant ones we add). */
1101 20 : if (TYPE_PRECISION (TREE_TYPE (vectype)) < TYPE_PRECISION (iv_type))
1102 9 : rem = gimple_build (&incr_gsi, false, GSI_CONTINUE_LINKING,
1103 : UNKNOWN_LOCATION,
1104 9 : MIN_EXPR, TREE_TYPE (rem), rem, iv_step);
1105 20 : rem = gimple_convert (&incr_gsi, false, GSI_CONTINUE_LINKING,
1106 20 : UNKNOWN_LOCATION, TREE_TYPE (vectype), rem);
1107 :
1108 : /* Build a data vector composed of the remaining iterations. */
1109 20 : rem = gimple_build_vector_from_val (&incr_gsi, false, GSI_CONTINUE_LINKING,
1110 : UNKNOWN_LOCATION, vectype, rem);
1111 :
1112 : /* Provide a definition of each vector in the control group. */
1113 20 : tree next_ctrl = NULL_TREE;
1114 20 : tree first_rem = NULL_TREE;
1115 20 : tree ctrl;
1116 20 : unsigned int i;
1117 238 : FOR_EACH_VEC_ELT_REVERSE (rgc.controls, i, ctrl)
1118 : {
1119 : /* Previous controls will cover BIAS items. This control covers the
1120 : next batch. */
1121 86 : poly_uint64 bias = nitems_per_ctrl * i;
1122 :
1123 : /* Build the constant to compare the remaining iters against,
1124 : this is sth like { 0, 0, 1, 1, 2, 2, 3, 3, ... } appropriately
1125 : split into pieces. */
1126 86 : unsigned n = TYPE_VECTOR_SUBPARTS (ctrl_type).to_constant ();
1127 86 : tree_vector_builder builder (vectype, n, 1);
1128 1266 : for (unsigned i = 0; i < n; ++i)
1129 : {
1130 1180 : unsigned HOST_WIDE_INT val
1131 1180 : = (i + bias.to_constant ()) / rgc.max_nscalars_per_iter;
1132 1180 : gcc_assert (val < vf.to_constant ());
1133 1180 : builder.quick_push (build_int_cst (TREE_TYPE (vectype), val));
1134 : }
1135 86 : tree cmp_series = builder.build ();
1136 :
1137 : /* Create the initial control. First include all items that
1138 : are within the loop limit. */
1139 86 : tree init_ctrl = NULL_TREE;
1140 86 : poly_uint64 const_limit;
1141 : /* See whether the first iteration of the vector loop is known
1142 : to have a full control. */
1143 86 : if (poly_int_tree_p (niters, &const_limit)
1144 86 : && known_ge (const_limit, (i + 1) * nitems_per_ctrl))
1145 1 : init_ctrl = build_minus_one_cst (ctrl_type);
1146 : else
1147 : {
1148 : /* The remaining work items initially are niters. Saturate,
1149 : splat and compare. */
1150 85 : if (!first_rem)
1151 : {
1152 19 : first_rem = niters;
1153 19 : if (TYPE_PRECISION (TREE_TYPE (vectype))
1154 19 : < TYPE_PRECISION (iv_type))
1155 9 : first_rem = gimple_build (&preheader_seq,
1156 9 : MIN_EXPR, TREE_TYPE (first_rem),
1157 : first_rem, iv_step);
1158 19 : first_rem = gimple_convert (&preheader_seq, TREE_TYPE (vectype),
1159 : first_rem);
1160 19 : first_rem = gimple_build_vector_from_val (&preheader_seq,
1161 : vectype, first_rem);
1162 : }
1163 85 : init_ctrl = gimple_build (&preheader_seq, LT_EXPR, ctrl_type,
1164 : cmp_series, first_rem);
1165 : }
1166 :
1167 : /* Now AND out the bits that are within the number of skipped
1168 : items. */
1169 86 : poly_uint64 const_skip;
1170 86 : if (niters_skip
1171 86 : && !(poly_int_tree_p (niters_skip, &const_skip)
1172 1 : && known_le (const_skip, bias)))
1173 : {
1174 : /* For integer mode masks it's cheaper to shift out the bits
1175 : since that avoids loading a constant. */
1176 1 : gcc_assert (GET_MODE_CLASS (TYPE_MODE (ctrl_type)) == MODE_INT);
1177 1 : init_ctrl = gimple_build (&preheader_seq, VIEW_CONVERT_EXPR,
1178 1 : lang_hooks.types.type_for_mode
1179 1 : (TYPE_MODE (ctrl_type), 1),
1180 : init_ctrl);
1181 : /* ??? But when the shift amount isn't constant this requires
1182 : a round-trip to GRPs. We could apply the bias to either
1183 : side of the compare instead. */
1184 2 : tree shift = gimple_build (&preheader_seq, MINUS_EXPR,
1185 1 : TREE_TYPE (niters_skip), niters_skip,
1186 1 : build_int_cst (TREE_TYPE (niters_skip),
1187 1 : bias));
1188 2 : shift = gimple_build (&preheader_seq, MULT_EXPR,
1189 1 : TREE_TYPE (niters_skip), shift,
1190 1 : build_int_cst (TREE_TYPE (niters_skip),
1191 1 : rgc.max_nscalars_per_iter));
1192 1 : init_ctrl = gimple_build (&preheader_seq, LSHIFT_EXPR,
1193 1 : TREE_TYPE (init_ctrl),
1194 : init_ctrl, shift);
1195 1 : init_ctrl = gimple_build (&preheader_seq, VIEW_CONVERT_EXPR,
1196 : ctrl_type, init_ctrl);
1197 : }
1198 :
1199 : /* Get the control value for the next iteration of the loop. */
1200 86 : next_ctrl = gimple_build (&incr_gsi, false, GSI_CONTINUE_LINKING,
1201 : UNKNOWN_LOCATION,
1202 : LT_EXPR, ctrl_type, cmp_series, rem);
1203 :
1204 86 : vect_set_loop_control (loop, ctrl, init_ctrl, next_ctrl);
1205 86 : }
1206 : }
1207 :
1208 : /* Emit all accumulated statements. */
1209 18 : add_preheader_seq (loop, preheader_seq);
1210 :
1211 : /* Adjust the exit test using the decrementing IV. */
1212 18 : tree_code code = (exit_edge->flags & EDGE_TRUE_VALUE) ? LE_EXPR : GT_EXPR;
1213 : /* When we peel for alignment with niter_skip != 0 this can
1214 : cause niter + niter_skip to wrap and since we are comparing the
1215 : value before the decrement here we get a false early exit.
1216 : We can't compare the value after decrement either because that
1217 : decrement could wrap as well as we're not doing a saturating
1218 : decrement. To avoid this situation we force a larger
1219 : iv_type. */
1220 18 : gcond *cond_stmt = gimple_build_cond (code, index_before_incr, iv_step,
1221 : NULL_TREE, NULL_TREE);
1222 18 : gsi_insert_before (&loop_cond_gsi, cond_stmt, GSI_SAME_STMT);
1223 :
1224 : /* The loop iterates (NITERS - 1 + NITERS_SKIP) / VF + 1 times.
1225 : Subtract one from this to get the latch count. */
1226 18 : tree niters_minus_one
1227 18 : = fold_build2 (PLUS_EXPR, TREE_TYPE (orig_niters), orig_niters,
1228 : build_minus_one_cst (TREE_TYPE (orig_niters)));
1229 18 : tree niters_adj2 = fold_convert (iv_type, niters_minus_one);
1230 18 : if (niters_skip)
1231 1 : niters_adj2 = fold_build2 (PLUS_EXPR, iv_type, niters_minus_one,
1232 : fold_convert (iv_type, niters_skip));
1233 18 : loop->nb_iterations = fold_build2 (TRUNC_DIV_EXPR, iv_type,
1234 : niters_adj2, iv_step);
1235 :
1236 18 : if (final_iv)
1237 : {
1238 0 : gassign *assign;
1239 : /* If vectorizing an inverted early break loop we have to restart the
1240 : scalar loop at niters - vf. This matches what we do in
1241 : vect_gen_vector_loop_niters_mult_vf for non-masked loops. */
1242 0 : if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
1243 : {
1244 0 : tree ftype = TREE_TYPE (orig_niters);
1245 0 : tree vf = build_int_cst (ftype, LOOP_VINFO_VECT_FACTOR (loop_vinfo));
1246 0 : assign = gimple_build_assign (final_iv, MINUS_EXPR, orig_niters, vf);
1247 : }
1248 : else
1249 0 : assign = gimple_build_assign (final_iv, orig_niters);
1250 0 : gsi_insert_on_edge_immediate (exit_edge, assign);
1251 : }
1252 :
1253 18 : return cond_stmt;
1254 : }
1255 :
1256 :
1257 : /* Like vect_set_loop_condition, but handle the case in which the vector
1258 : loop handles exactly VF scalars per iteration. */
1259 :
1260 : static gcond *
1261 62351 : vect_set_loop_condition_normal (loop_vec_info loop_vinfo, edge exit_edge,
1262 : class loop *loop, tree niters, tree step,
1263 : tree final_iv, bool niters_maybe_zero,
1264 : gimple_stmt_iterator loop_cond_gsi)
1265 : {
1266 62351 : tree indx_before_incr, indx_after_incr;
1267 62351 : gcond *cond_stmt;
1268 62351 : gcond *orig_cond;
1269 62351 : edge pe = loop_preheader_edge (loop);
1270 62351 : gimple_stmt_iterator incr_gsi;
1271 62351 : bool insert_after;
1272 62351 : enum tree_code code;
1273 62351 : tree niters_type = TREE_TYPE (niters);
1274 :
1275 62351 : orig_cond = get_loop_exit_condition (exit_edge);
1276 62351 : gcc_assert (orig_cond);
1277 62351 : loop_cond_gsi = gsi_for_stmt (orig_cond);
1278 :
1279 62351 : tree init, limit;
1280 62351 : if (!niters_maybe_zero && integer_onep (step))
1281 : {
1282 : /* In this case we can use a simple 0-based IV:
1283 :
1284 : A:
1285 : x = 0;
1286 : do
1287 : {
1288 : ...
1289 : x += 1;
1290 : }
1291 : while (x < NITERS); */
1292 62351 : code = (exit_edge->flags & EDGE_TRUE_VALUE) ? GE_EXPR : LT_EXPR;
1293 62351 : init = build_zero_cst (niters_type);
1294 62351 : limit = niters;
1295 : }
1296 : else
1297 : {
1298 : /* The following works for all values of NITERS except 0:
1299 :
1300 : B:
1301 : x = 0;
1302 : do
1303 : {
1304 : ...
1305 : x += STEP;
1306 : }
1307 : while (x <= NITERS - STEP);
1308 :
1309 : so that the loop continues to iterate if x + STEP - 1 < NITERS
1310 : but stops if x + STEP - 1 >= NITERS.
1311 :
1312 : However, if NITERS is zero, x never hits a value above NITERS - STEP
1313 : before wrapping around. There are two obvious ways of dealing with
1314 : this:
1315 :
1316 : - start at STEP - 1 and compare x before incrementing it
1317 : - start at -1 and compare x after incrementing it
1318 :
1319 : The latter is simpler and is what we use. The loop in this case
1320 : looks like:
1321 :
1322 : C:
1323 : x = -1;
1324 : do
1325 : {
1326 : ...
1327 : x += STEP;
1328 : }
1329 : while (x < NITERS - STEP);
1330 :
1331 : In both cases the loop limit is NITERS - STEP. */
1332 0 : gimple_seq seq = NULL;
1333 0 : limit = force_gimple_operand (niters, &seq, true, NULL_TREE);
1334 0 : limit = gimple_build (&seq, MINUS_EXPR, TREE_TYPE (limit), limit, step);
1335 0 : if (seq)
1336 : {
1337 0 : basic_block new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
1338 0 : gcc_assert (!new_bb);
1339 : }
1340 0 : if (niters_maybe_zero)
1341 : {
1342 : /* Case C. */
1343 0 : code = (exit_edge->flags & EDGE_TRUE_VALUE) ? GE_EXPR : LT_EXPR;
1344 0 : init = build_all_ones_cst (niters_type);
1345 : }
1346 : else
1347 : {
1348 : /* Case B. */
1349 0 : code = (exit_edge->flags & EDGE_TRUE_VALUE) ? GT_EXPR : LE_EXPR;
1350 0 : init = build_zero_cst (niters_type);
1351 : }
1352 : }
1353 :
1354 62351 : vect_iv_increment_position (exit_edge, &incr_gsi, &insert_after);
1355 62351 : create_iv (init, PLUS_EXPR, step, NULL_TREE, loop,
1356 : &incr_gsi, insert_after,
1357 : &indx_before_incr, &indx_after_incr,
1358 62351 : !loop_vinfo || LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo));
1359 :
1360 62351 : indx_after_incr = force_gimple_operand_gsi (&loop_cond_gsi, indx_after_incr,
1361 : true, NULL_TREE, true,
1362 : GSI_SAME_STMT);
1363 62351 : limit = force_gimple_operand_gsi (&loop_cond_gsi, limit, true, NULL_TREE,
1364 : true, GSI_SAME_STMT);
1365 :
1366 62351 : cond_stmt = gimple_build_cond (code, indx_after_incr, limit, NULL_TREE,
1367 : NULL_TREE);
1368 :
1369 62351 : gsi_insert_before (&loop_cond_gsi, cond_stmt, GSI_SAME_STMT);
1370 :
1371 : /* Record the number of latch iterations. */
1372 62351 : if (limit == niters)
1373 : /* Case A: the loop iterates NITERS times. Subtract one to get the
1374 : latch count. */
1375 62351 : loop->nb_iterations = fold_build2 (MINUS_EXPR, niters_type, niters,
1376 : build_int_cst (niters_type, 1));
1377 : else
1378 : /* Case B or C: the loop iterates (NITERS - STEP) / STEP + 1 times.
1379 : Subtract one from this to get the latch count. */
1380 0 : loop->nb_iterations = fold_build2 (TRUNC_DIV_EXPR, niters_type,
1381 : limit, step);
1382 :
1383 62351 : if (final_iv)
1384 : {
1385 0 : gassign *assign;
1386 0 : gcc_assert (single_pred_p (exit_edge->dest));
1387 0 : tree phi_dest
1388 0 : = integer_zerop (init) ? final_iv : copy_ssa_name (indx_after_incr);
1389 : /* Make sure to maintain LC SSA form here and elide the subtraction
1390 : if the value is zero. */
1391 0 : gphi *phi = create_phi_node (phi_dest, exit_edge->dest);
1392 0 : add_phi_arg (phi, indx_after_incr, exit_edge, UNKNOWN_LOCATION);
1393 0 : if (!integer_zerop (init))
1394 : {
1395 0 : assign = gimple_build_assign (final_iv, MINUS_EXPR,
1396 : phi_dest, init);
1397 0 : gimple_stmt_iterator gsi = gsi_after_labels (exit_edge->dest);
1398 0 : gsi_insert_before (&gsi, assign, GSI_SAME_STMT);
1399 : }
1400 : }
1401 :
1402 62351 : return cond_stmt;
1403 : }
1404 :
1405 : /* If we're using fully-masked loops, make LOOP iterate:
1406 :
1407 : N == (NITERS - 1) / STEP + 1
1408 :
1409 : times. When NITERS is zero, this is equivalent to making the loop
1410 : execute (1 << M) / STEP times, where M is the precision of NITERS.
1411 : NITERS_MAYBE_ZERO is true if this last case might occur.
1412 :
1413 : If we're not using fully-masked loops, make LOOP iterate:
1414 :
1415 : N == (NITERS - STEP) / STEP + 1
1416 :
1417 : times, where NITERS is known to be outside the range [1, STEP - 1].
1418 : This is equivalent to making the loop execute NITERS / STEP times
1419 : when NITERS is nonzero and (1 << M) / STEP times otherwise.
1420 : NITERS_MAYBE_ZERO again indicates whether this last case might occur.
1421 :
1422 : If FINAL_IV is nonnull, it is an SSA name that should be set to
1423 : N * STEP on exit from the loop.
1424 :
1425 : Assumption: the exit-condition of LOOP is the last stmt in the loop. */
1426 :
1427 : void
1428 62369 : vect_set_loop_condition (class loop *loop, edge loop_e, loop_vec_info loop_vinfo,
1429 : tree niters, tree step, tree final_iv,
1430 : bool niters_maybe_zero)
1431 : {
1432 62369 : gcond *cond_stmt;
1433 62369 : gcond *orig_cond = get_loop_exit_condition (loop_e);
1434 62369 : gimple_stmt_iterator loop_cond_gsi = gsi_for_stmt (orig_cond);
1435 :
1436 : /* Check to see whether we will be replacing final_IV below. Because of the
1437 : various replacement strategies (assign vs PHI) just remove it now and
1438 : leave the SSA name to be rebuild below. */
1439 62369 : if (final_iv && TREE_CODE (final_iv) == SSA_NAME)
1440 : {
1441 0 : gimple *def = SSA_NAME_DEF_STMT (final_iv);
1442 0 : if (gimple_call_internal_p (def, IFN_VARYING))
1443 : {
1444 0 : gimple_stmt_iterator gsi = gsi_for_stmt (def);
1445 0 : gsi_remove (&gsi, true);
1446 : }
1447 : }
1448 :
1449 62369 : if (loop_vinfo && LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo))
1450 : {
1451 18 : if (LOOP_VINFO_PARTIAL_VECTORS_STYLE (loop_vinfo) == vect_partial_vectors_avx512)
1452 18 : cond_stmt = vect_set_loop_condition_partial_vectors_avx512 (loop, loop_e,
1453 : loop_vinfo,
1454 : niters, final_iv,
1455 : niters_maybe_zero,
1456 : loop_cond_gsi);
1457 : else
1458 0 : cond_stmt = vect_set_loop_condition_partial_vectors (loop, loop_e,
1459 : loop_vinfo,
1460 : niters, final_iv,
1461 : niters_maybe_zero,
1462 : loop_cond_gsi);
1463 : }
1464 : else
1465 62351 : cond_stmt = vect_set_loop_condition_normal (loop_vinfo, loop_e, loop,
1466 : niters,
1467 : step, final_iv,
1468 : niters_maybe_zero,
1469 : loop_cond_gsi);
1470 :
1471 : /* Remove old loop exit test. */
1472 62369 : stmt_vec_info orig_cond_info;
1473 62369 : if (loop_vinfo
1474 62369 : && (orig_cond_info = loop_vinfo->lookup_stmt (orig_cond)))
1475 61936 : loop_vinfo->remove_stmt (orig_cond_info);
1476 : else
1477 433 : gsi_remove (&loop_cond_gsi, true);
1478 :
1479 62369 : if (dump_enabled_p ())
1480 11271 : dump_printf_loc (MSG_NOTE, vect_location, "New loop exit condition: %G",
1481 : (gimple *) cond_stmt);
1482 62369 : }
1483 :
1484 : /* Get the virtual operand live on E. The precondition on this is valid
1485 : immediate dominators and an actual virtual definition dominating E. */
1486 : /* ??? Costly band-aid. For the use in question we can populate a
1487 : live-on-exit/end-of-BB virtual operand when copying stmts. */
1488 :
1489 : static tree
1490 10 : get_live_virtual_operand_on_edge (edge e)
1491 : {
1492 10 : basic_block bb = e->src;
1493 22 : do
1494 : {
1495 61 : for (auto gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
1496 : {
1497 37 : gimple *stmt = gsi_stmt (gsi);
1498 55 : if (gimple_vdef (stmt))
1499 10 : return gimple_vdef (stmt);
1500 39 : if (gimple_vuse (stmt))
1501 : return gimple_vuse (stmt);
1502 : }
1503 8 : if (gphi *vphi = get_virtual_phi (bb))
1504 2 : return gimple_phi_result (vphi);
1505 6 : bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1506 6 : }
1507 : while (1);
1508 : }
1509 :
1510 : /* Given LOOP this function generates a new copy of it and puts it
1511 : on E which is either the entry or exit of LOOP. If SCALAR_LOOP is
1512 : non-NULL, assume LOOP and SCALAR_LOOP are equivalent and copy the
1513 : basic blocks from SCALAR_LOOP instead of LOOP, but to either the
1514 : entry or exit of LOOP. If FLOW_LOOPS then connect LOOP to SCALAR_LOOP as a
1515 : continuation. This is correct for cases where one loop continues from the
1516 : other like in the vectorizer, but not true for uses in e.g. loop distribution
1517 : where the contents of the loop body are split but the iteration space of both
1518 : copies remains the same.
1519 :
1520 : If UPDATED_DOMS is not NULL it is update with the list of basic blocks whose
1521 : dominators were updated during the peeling. When doing early break vectorization
1522 : then LOOP_VINFO needs to be provided and is used to keep track of any newly created
1523 : memory references that need to be updated should we decide to vectorize. */
1524 :
1525 : class loop *
1526 34594 : slpeel_tree_duplicate_loop_to_edge_cfg (class loop *loop, edge loop_exit,
1527 : class loop *scalar_loop,
1528 : edge scalar_exit, edge e, edge *new_e,
1529 : bool flow_loops,
1530 : vec<basic_block> *updated_doms,
1531 : bool uncounted_p, bool create_main_e,
1532 : bool redirect_exits)
1533 : {
1534 34594 : class loop *new_loop;
1535 34594 : basic_block *new_bbs, *bbs, *pbbs;
1536 34594 : bool at_exit;
1537 34594 : bool was_imm_dom;
1538 34594 : basic_block exit_dest;
1539 34594 : edge exit, new_exit;
1540 34594 : bool duplicate_outer_loop = false;
1541 :
1542 34594 : exit = loop_exit;
1543 34594 : at_exit = (e == exit);
1544 34594 : if (!at_exit && e != loop_preheader_edge (loop))
1545 : return NULL;
1546 :
1547 34594 : if (scalar_loop == NULL)
1548 : {
1549 32058 : scalar_loop = loop;
1550 32058 : scalar_exit = loop_exit;
1551 : }
1552 2536 : else if (scalar_loop == loop)
1553 0 : scalar_exit = loop_exit;
1554 : else
1555 : {
1556 : /* Loop has been version, match exits up using the aux index. */
1557 7608 : for (edge exit : get_loop_exit_edges (scalar_loop))
1558 2536 : if (exit->aux == loop_exit->aux)
1559 : {
1560 2536 : scalar_exit = exit;
1561 2536 : break;
1562 2536 : }
1563 :
1564 2536 : gcc_assert (scalar_exit);
1565 : }
1566 :
1567 34594 : bbs = XNEWVEC (basic_block, scalar_loop->num_nodes + 1);
1568 34594 : pbbs = bbs + 1;
1569 34594 : get_loop_body_with_size (scalar_loop, pbbs, scalar_loop->num_nodes);
1570 : /* Allow duplication of outer loops. */
1571 34594 : if (scalar_loop->inner)
1572 132 : duplicate_outer_loop = true;
1573 :
1574 : /* Generate new loop structure. */
1575 34594 : new_loop = duplicate_loop (scalar_loop, loop_outer (scalar_loop));
1576 34594 : duplicate_subloops (scalar_loop, new_loop);
1577 :
1578 34594 : exit_dest = exit->dest;
1579 34594 : was_imm_dom = (get_immediate_dominator (CDI_DOMINATORS,
1580 34594 : exit_dest) == exit->src ?
1581 : true : false);
1582 :
1583 : /* Also copy the pre-header, this avoids jumping through hoops to
1584 : duplicate the loop entry PHI arguments. Create an empty
1585 : pre-header unconditionally for this. */
1586 34594 : basic_block preheader = split_edge (loop_preheader_edge (scalar_loop));
1587 34594 : edge entry_e = single_pred_edge (preheader);
1588 34594 : bbs[0] = preheader;
1589 34594 : new_bbs = XNEWVEC (basic_block, scalar_loop->num_nodes + 1);
1590 :
1591 34594 : copy_bbs (bbs, scalar_loop->num_nodes + 1, new_bbs,
1592 : &scalar_exit, 1, &new_exit, NULL,
1593 : at_exit ? loop->latch : e->src, true);
1594 34594 : exit = loop_exit;
1595 34594 : basic_block new_preheader = new_bbs[0];
1596 :
1597 34594 : gcc_assert (new_exit);
1598 :
1599 : /* Record the new loop exit information. new_loop doesn't have SCEV data and
1600 : so we must initialize the exit information. */
1601 34594 : if (new_e)
1602 33425 : *new_e = new_exit;
1603 :
1604 : /* Before installing PHI arguments make sure that the edges
1605 : into them match that of the scalar loop we analyzed. This
1606 : makes sure the SLP tree matches up between the main vectorized
1607 : loop and the epilogue vectorized copies. */
1608 34594 : if (single_succ_edge (preheader)->dest_idx
1609 34594 : != single_succ_edge (new_bbs[0])->dest_idx)
1610 : {
1611 29567 : basic_block swap_bb = new_bbs[1];
1612 29567 : gcc_assert (EDGE_COUNT (swap_bb->preds) == 2);
1613 29567 : std::swap (EDGE_PRED (swap_bb, 0), EDGE_PRED (swap_bb, 1));
1614 29567 : EDGE_PRED (swap_bb, 0)->dest_idx = 0;
1615 29567 : EDGE_PRED (swap_bb, 1)->dest_idx = 1;
1616 : }
1617 34594 : if (duplicate_outer_loop)
1618 : {
1619 132 : class loop *new_inner_loop = get_loop_copy (scalar_loop->inner);
1620 132 : if (loop_preheader_edge (scalar_loop)->dest_idx
1621 132 : != loop_preheader_edge (new_inner_loop)->dest_idx)
1622 : {
1623 99 : basic_block swap_bb = new_inner_loop->header;
1624 99 : gcc_assert (EDGE_COUNT (swap_bb->preds) == 2);
1625 99 : std::swap (EDGE_PRED (swap_bb, 0), EDGE_PRED (swap_bb, 1));
1626 99 : EDGE_PRED (swap_bb, 0)->dest_idx = 0;
1627 99 : EDGE_PRED (swap_bb, 1)->dest_idx = 1;
1628 : }
1629 : }
1630 :
1631 34594 : add_phi_args_after_copy (new_bbs, scalar_loop->num_nodes + 1, NULL);
1632 :
1633 : /* Skip new preheader since it's deleted if copy loop is added at entry. */
1634 180217 : for (unsigned i = (at_exit ? 0 : 1); i < scalar_loop->num_nodes + 1; i++)
1635 111029 : rename_variables_in_bb (new_bbs[i], duplicate_outer_loop);
1636 :
1637 : /* Rename the exit uses. */
1638 139554 : for (edge exit : get_loop_exit_edges (new_loop))
1639 35772 : for (auto gsi = gsi_start_phis (exit->dest);
1640 84245 : !gsi_end_p (gsi); gsi_next (&gsi))
1641 : {
1642 48473 : tree orig_def = PHI_ARG_DEF_FROM_EDGE (gsi.phi (), exit);
1643 48473 : rename_use_op (PHI_ARG_DEF_PTR_FROM_EDGE (gsi.phi (), exit));
1644 48473 : if (MAY_HAVE_DEBUG_BIND_STMTS)
1645 23186 : adjust_debug_stmts (orig_def, PHI_RESULT (gsi.phi ()), exit->dest);
1646 34594 : }
1647 :
1648 34594 : auto loop_exits = get_loop_exit_edges (loop);
1649 34594 : bool has_multiple_exits_p = loop_exits.length () > 1;
1650 :
1651 : /* If REDIRECT_EXITS is false we leave the alternative exits untouched and
1652 : treat the duplication as if the loop only had the main exit. */
1653 34594 : bool redirect_multiple_exits_p = redirect_exits && has_multiple_exits_p;
1654 34594 : auto_vec<basic_block> doms;
1655 :
1656 34594 : if (at_exit) /* Add the loop copy at exit. */
1657 : {
1658 32992 : if (scalar_loop != loop && new_exit->dest != exit_dest)
1659 : {
1660 2532 : new_exit = redirect_edge_and_branch (new_exit, exit_dest);
1661 2532 : flush_pending_stmts (new_exit);
1662 : }
1663 :
1664 32992 : bool need_virtual_phi = get_virtual_phi (loop->header);
1665 :
1666 : /* For the main loop exit preserve the LC PHI nodes. For vectorization
1667 : we need them to continue or finalize reductions. Since we do not
1668 : copy the loop exit blocks we have to materialize PHIs at the
1669 : new destination before redirecting edges. */
1670 32992 : for (auto gsi_from = gsi_start_phis (loop_exit->dest);
1671 78643 : !gsi_end_p (gsi_from); gsi_next (&gsi_from))
1672 : {
1673 45651 : tree res = gimple_phi_result (*gsi_from);
1674 45651 : create_phi_node (copy_ssa_name (res), new_preheader);
1675 : }
1676 32992 : edge e = redirect_edge_and_branch (loop_exit, new_preheader);
1677 32992 : gcc_assert (e == loop_exit);
1678 32992 : flush_pending_stmts (loop_exit);
1679 32992 : set_immediate_dominator (CDI_DOMINATORS, new_preheader, loop_exit->src);
1680 :
1681 : /* If we ended up choosing an exit leading to a path not using memory
1682 : we can end up without a virtual LC PHI. Create it when it is
1683 : needed because of the epilog loop continuation. */
1684 32992 : if (need_virtual_phi && !get_virtual_phi (loop_exit->dest))
1685 : {
1686 8 : tree header_def = gimple_phi_result (get_virtual_phi (loop->header));
1687 8 : gphi *vphi = create_phi_node (copy_ssa_name (header_def),
1688 : new_preheader);
1689 8 : add_phi_arg (vphi, get_live_virtual_operand_on_edge (loop_exit),
1690 : loop_exit, UNKNOWN_LOCATION);
1691 : }
1692 :
1693 32992 : basic_block main_loop_exit_block = new_preheader;
1694 32992 : basic_block alt_loop_exit_block = new_preheader;
1695 : /* When we redirect the other exits create the CFG
1696 : below to funnel everything through the merge block:
1697 : | loop_exit | alt1 | altN
1698 : v v ... v
1699 : main_loop_exit_block: alt_loop_exit_block:
1700 : | /
1701 : v v
1702 : new_preheader:
1703 : where in the new preheader we need merge PHIs for
1704 : the continuation values into the epilogue header.
1705 : Do not bother with exit PHIs for the early exits but
1706 : their live virtual operand. We'll fix up things below. */
1707 32992 : if (redirect_multiple_exits_p || uncounted_p)
1708 : {
1709 659 : edge loop_e = single_succ_edge (new_preheader);
1710 659 : new_preheader = split_edge (loop_e);
1711 :
1712 659 : if (redirect_exits)
1713 : {
1714 653 : gphi *vphi = NULL;
1715 653 : alt_loop_exit_block = new_preheader;
1716 3387 : for (auto exit : loop_exits)
1717 1428 : if (exit != loop_exit)
1718 : {
1719 775 : tree vphi_def = NULL_TREE;
1720 775 : if (gphi *evphi = get_virtual_phi (exit->dest))
1721 466 : vphi_def = gimple_phi_arg_def_from_edge (evphi, exit);
1722 775 : edge res
1723 775 : = redirect_edge_and_branch (exit, alt_loop_exit_block);
1724 775 : gcc_assert (res == exit);
1725 775 : redirect_edge_var_map_clear (exit);
1726 :
1727 775 : if (alt_loop_exit_block == new_preheader)
1728 630 : alt_loop_exit_block = split_edge (exit);
1729 775 : if (!need_virtual_phi)
1730 317 : continue;
1731 :
1732 : /* When the edge has no virtual LC PHI get at the live
1733 : virtual operand by other means. */
1734 458 : if (!vphi_def)
1735 2 : vphi_def = get_live_virtual_operand_on_edge (exit);
1736 :
1737 458 : if (!vphi)
1738 424 : vphi = create_phi_node (copy_ssa_name (vphi_def),
1739 : alt_loop_exit_block);
1740 : else
1741 : /* Edge redirection might re-allocate the PHI node
1742 : so we have to rediscover it. */
1743 34 : vphi = get_virtual_phi (alt_loop_exit_block);
1744 458 : add_phi_arg (vphi, vphi_def, exit, UNKNOWN_LOCATION);
1745 : }
1746 : }
1747 :
1748 659 : set_immediate_dominator (CDI_DOMINATORS, new_preheader,
1749 : loop->header);
1750 :
1751 : /* Fix up the profile counts of the new exit blocks.
1752 : main_loop_exit_block was created by duplicating the
1753 : preheader, so needs its count scaling according to the main
1754 : exit edge's probability. The remaining count from the
1755 : preheader goes to the alt_loop_exit_block, since all
1756 : alternative exits have been redirected there. */
1757 659 : main_loop_exit_block->count = loop_exit->count ();
1758 659 : alt_loop_exit_block->count
1759 659 : = preheader->count - main_loop_exit_block->count;
1760 : }
1761 :
1762 : /* Adjust the epilog loop PHI entry values to continue iteration.
1763 : This adds remaining necessary LC PHI nodes to the main exit
1764 : and creates merge PHIs when we have multiple exits with
1765 : their appropriate continuation. */
1766 32992 : if (flow_loops)
1767 : {
1768 32992 : edge loop_entry = single_succ_edge (new_preheader);
1769 32992 : bool peeled_iters = (uncounted_p
1770 32992 : || single_pred (loop->latch) != loop_exit->src);
1771 :
1772 : /* Record the new SSA names in the cache so that we can skip
1773 : materializing them again when we fill in the rest of the LC SSA
1774 : variables. */
1775 32992 : hash_map <tree, tree> new_phi_args;
1776 32992 : for (auto psi = gsi_start_phis (main_loop_exit_block);
1777 78651 : !gsi_end_p (psi); gsi_next (&psi))
1778 : {
1779 45659 : gphi *phi = *psi;
1780 45659 : tree new_arg = gimple_phi_arg_def_from_edge (phi, loop_exit);
1781 45659 : if (TREE_CODE (new_arg) != SSA_NAME)
1782 313 : continue;
1783 :
1784 : /* If the loop doesn't have a virtual def then only possibly keep
1785 : the epilog LC PHI for it and avoid creating new defs. */
1786 45436 : if (virtual_operand_p (new_arg) && !need_virtual_phi)
1787 : {
1788 90 : auto gsi = gsi_for_stmt (phi);
1789 90 : remove_phi_node (&gsi, true);
1790 90 : continue;
1791 90 : }
1792 :
1793 : /* If we decided not to remove the PHI node we should also not
1794 : rematerialize it later on. */
1795 45346 : new_phi_args.put (new_arg, gimple_phi_result (phi));
1796 : }
1797 :
1798 : /* Create the merge PHI nodes in new_preheader and populate the
1799 : arguments for the exits. */
1800 32992 : if (redirect_multiple_exits_p)
1801 : {
1802 630 : for (auto gsi_from = gsi_start_phis (loop->header),
1803 630 : gsi_to = gsi_start_phis (new_loop->header);
1804 2294 : !gsi_end_p (gsi_from) && !gsi_end_p (gsi_to);
1805 1664 : gsi_next (&gsi_from), gsi_next (&gsi_to))
1806 : {
1807 1664 : gimple *from_phi = gsi_stmt (gsi_from);
1808 1664 : gimple *to_phi = gsi_stmt (gsi_to);
1809 :
1810 : /* When the vector loop is peeled then we need to use the
1811 : value at start of the loop, otherwise the main loop exit
1812 : should use the final iter value. */
1813 1664 : tree new_arg;
1814 1664 : if (peeled_iters)
1815 71 : new_arg = gimple_phi_result (from_phi);
1816 : else
1817 1593 : new_arg = PHI_ARG_DEF_FROM_EDGE (from_phi,
1818 : loop_latch_edge (loop));
1819 :
1820 : /* Check if we've already created a new phi node during edge
1821 : redirection and re-use it if so. Otherwise create a
1822 : LC PHI node to feed the merge PHI. */
1823 1664 : tree *res;
1824 3328 : if (virtual_operand_p (new_arg))
1825 : {
1826 : /* Use the existing virtual LC SSA from exit block. */
1827 424 : gphi *vphi = get_virtual_phi (main_loop_exit_block);
1828 424 : new_arg = gimple_phi_result (vphi);
1829 : }
1830 1240 : else if ((res = new_phi_args.get (new_arg)))
1831 105 : new_arg = *res;
1832 : else
1833 : {
1834 : /* Create the LC PHI node for the exit. */
1835 1135 : tree new_def = copy_ssa_name (new_arg);
1836 1135 : gphi *lc_phi
1837 1135 : = create_phi_node (new_def, main_loop_exit_block);
1838 1135 : SET_PHI_ARG_DEF (lc_phi, 0, new_arg);
1839 1135 : new_arg = new_def;
1840 : }
1841 :
1842 : /* Create the PHI node in the merge block merging the
1843 : main and early exit values. */
1844 1664 : tree new_res = copy_ssa_name (gimple_phi_result (from_phi));
1845 1664 : gphi *lcssa_phi = create_phi_node (new_res, new_preheader);
1846 1664 : edge main_e = single_succ_edge (main_loop_exit_block);
1847 1664 : SET_PHI_ARG_DEF_ON_EDGE (lcssa_phi, main_e, new_arg);
1848 :
1849 : /* And adjust the epilog entry value. */
1850 1664 : adjust_phi_and_debug_stmts (to_phi, loop_entry, new_res);
1851 : }
1852 : }
1853 :
1854 32992 : if (redirect_multiple_exits_p)
1855 : {
1856 : /* After creating the merge PHIs handle the early exits those
1857 : should use the values at the start of the loop. */
1858 630 : for (auto gsi_from = gsi_start_phis (loop->header),
1859 630 : gsi_to = gsi_start_phis (new_preheader);
1860 2294 : !gsi_end_p (gsi_from) && !gsi_end_p (gsi_to);
1861 1664 : gsi_next (&gsi_from), gsi_next (&gsi_to))
1862 : {
1863 1664 : gimple *from_phi = gsi_stmt (gsi_from);
1864 1664 : gimple *to_phi = gsi_stmt (gsi_to);
1865 :
1866 : /* Now update the virtual PHI nodes with the right value. */
1867 1664 : tree alt_arg = gimple_phi_result (from_phi);
1868 3328 : if (virtual_operand_p (alt_arg))
1869 : {
1870 424 : gphi *vphi = get_virtual_phi (alt_loop_exit_block);
1871 424 : alt_arg = gimple_phi_result (vphi);
1872 : }
1873 : /* For other live args we didn't create LC PHI nodes.
1874 : Do so here. */
1875 : else
1876 : {
1877 1240 : tree alt_def = copy_ssa_name (alt_arg);
1878 1240 : gphi *lc_phi
1879 1240 : = create_phi_node (alt_def, alt_loop_exit_block);
1880 2769 : for (unsigned i = 0; i < gimple_phi_num_args (lc_phi);
1881 : ++i)
1882 1529 : SET_PHI_ARG_DEF (lc_phi, i, alt_arg);
1883 : alt_arg = alt_def;
1884 : }
1885 :
1886 : /* The merge PHIs live in NEW_PREHEADER; their
1887 : alternative argument always comes from the
1888 : successor edge of ALT_LOOP_EXIT_BLOCK. */
1889 1664 : edge alt_e = single_succ_edge (alt_loop_exit_block);
1890 1664 : SET_PHI_ARG_DEF_ON_EDGE (to_phi, alt_e, alt_arg);
1891 : }
1892 : }
1893 :
1894 : /* For the single exit case only create the missing LC PHI nodes
1895 : for the continuation of the loop IVs that are not also already
1896 : reductions and thus had LC PHI nodes on the exit already. When
1897 : we are not redirecting the alternative exits the layout is:
1898 :
1899 : loop_exit ---> new_preheader ---> epilog
1900 : alt_exit ---------------> original dest
1901 : */
1902 630 : if (!redirect_multiple_exits_p)
1903 : {
1904 32362 : for (auto gsi_from = gsi_start_phis (loop->header),
1905 32362 : gsi_to = gsi_start_phis (new_loop->header);
1906 123375 : !gsi_end_p (gsi_from) && !gsi_end_p (gsi_to);
1907 91013 : gsi_next (&gsi_from), gsi_next (&gsi_to))
1908 : {
1909 91013 : gimple *from_phi = gsi_stmt (gsi_from);
1910 91013 : gimple *to_phi = gsi_stmt (gsi_to);
1911 91013 : tree new_arg;
1912 :
1913 : /* Use the value on the exiting path. When the exit is from
1914 : the latch edge we want the post-iteration value on that
1915 : edge; when the exit is from the loop header (before the
1916 : latch ever executes) we must use the current header value,
1917 : otherwise we pick up a name that was never defined. */
1918 91013 : if (!has_multiple_exits_p && !uncounted_p)
1919 90732 : new_arg = PHI_ARG_DEF_FROM_EDGE (from_phi,
1920 : loop_latch_edge (loop));
1921 : else
1922 281 : new_arg = gimple_phi_result (from_phi);
1923 :
1924 : /* Re-use the virtual LC PHI we already built when we are not
1925 : redirecting the other exits to avoid creating duplicate
1926 : virtual SSA names. */
1927 182026 : if (virtual_operand_p (new_arg))
1928 : {
1929 24571 : if (gphi *vphi = get_virtual_phi (main_loop_exit_block))
1930 : {
1931 24571 : adjust_phi_and_debug_stmts (to_phi, loop_entry,
1932 : gimple_phi_result (vphi));
1933 42641 : continue;
1934 : }
1935 : }
1936 :
1937 : /* Check if we've already created a new phi node during edge
1938 : redirection. If we have, only propagate the value
1939 : downwards. */
1940 66442 : if (tree *res = new_phi_args.get (new_arg))
1941 : {
1942 : /* Check if the new dest block already contains a use. */
1943 18070 : gimple *stmt = SSA_NAME_DEF_STMT (*res);
1944 :
1945 : /* If the value already exist, just update the destination
1946 : and if it doesn't we want a new node. */
1947 18070 : if (gimple_bb (stmt) == main_loop_exit_block)
1948 : {
1949 18070 : adjust_phi_and_debug_stmts (to_phi, loop_entry, *res);
1950 18070 : continue;
1951 : }
1952 : else
1953 0 : new_arg = *res;
1954 : }
1955 :
1956 48372 : tree new_res = copy_ssa_name (gimple_phi_result (from_phi));
1957 48372 : gphi *lcssa_phi = create_phi_node (new_res, main_loop_exit_block);
1958 48372 : SET_PHI_ARG_DEF (lcssa_phi, loop_exit->dest_idx, new_arg);
1959 48372 : adjust_phi_and_debug_stmts (to_phi, loop_entry, new_res);
1960 : }
1961 : }
1962 32992 : }
1963 :
1964 32992 : if (was_imm_dom || duplicate_outer_loop)
1965 32718 : set_immediate_dominator (CDI_DOMINATORS, exit_dest, new_exit->src);
1966 :
1967 : /* And remove the non-necessary forwarder again. Keep the other
1968 : one so we have a proper pre-header for the loop at the exit edge. */
1969 32992 : redirect_edge_pred (single_succ_edge (preheader),
1970 : single_pred (preheader));
1971 32992 : delete_basic_block (preheader);
1972 32992 : set_immediate_dominator (CDI_DOMINATORS, scalar_loop->header,
1973 32992 : loop_preheader_edge (scalar_loop)->src);
1974 :
1975 : /* Finally after wiring the new epilogue we need to update its main exit
1976 : to the original function exit we recorded. Other exits are already
1977 : correct. */
1978 32992 : if (has_multiple_exits_p || uncounted_p)
1979 : {
1980 842 : class loop *update_loop = new_loop;
1981 842 : doms = get_all_dominated_blocks (CDI_DOMINATORS, loop->header);
1982 19507 : for (unsigned i = 0; i < doms.length (); ++i)
1983 18665 : if (flow_bb_inside_loop_p (loop, doms[i]))
1984 2630 : doms.unordered_remove (i);
1985 :
1986 4326 : for (edge e : get_loop_exit_edges (update_loop))
1987 : {
1988 1800 : edge ex;
1989 1800 : edge_iterator ei;
1990 3643 : FOR_EACH_EDGE (ex, ei, e->dest->succs)
1991 : {
1992 : /* Find the first non-fallthrough block as fall-throughs can't
1993 : dominate other blocks. */
1994 1843 : if (single_succ_p (ex->dest))
1995 : {
1996 994 : doms.safe_push (ex->dest);
1997 994 : ex = single_succ_edge (ex->dest);
1998 : }
1999 1843 : doms.safe_push (ex->dest);
2000 : }
2001 1800 : doms.safe_push (e->dest);
2002 842 : }
2003 :
2004 842 : iterate_fix_dominators (CDI_DOMINATORS, doms, false);
2005 842 : if (updated_doms)
2006 842 : updated_doms->safe_splice (doms);
2007 : }
2008 : }
2009 : else /* Add the copy at entry. */
2010 : {
2011 : /* Copy the current loop LC PHI nodes between the original loop exit
2012 : block and the new loop header. This allows us to later split the
2013 : preheader block and still find the right LC nodes. */
2014 1602 : if (flow_loops)
2015 433 : for (auto gsi_from = gsi_start_phis (new_loop->header),
2016 433 : gsi_to = gsi_start_phis (loop->header);
2017 1357 : !gsi_end_p (gsi_from) && !gsi_end_p (gsi_to);
2018 924 : gsi_next (&gsi_from), gsi_next (&gsi_to))
2019 : {
2020 924 : gimple *from_phi = gsi_stmt (gsi_from);
2021 924 : gimple *to_phi = gsi_stmt (gsi_to);
2022 924 : tree new_arg = PHI_ARG_DEF_FROM_EDGE (from_phi,
2023 : loop_latch_edge (new_loop));
2024 924 : adjust_phi_and_debug_stmts (to_phi, loop_preheader_edge (loop),
2025 : new_arg);
2026 : }
2027 :
2028 1602 : if (scalar_loop != loop)
2029 : {
2030 : /* Remove the non-necessary forwarder of scalar_loop again. */
2031 4 : redirect_edge_pred (single_succ_edge (preheader),
2032 : single_pred (preheader));
2033 4 : delete_basic_block (preheader);
2034 4 : set_immediate_dominator (CDI_DOMINATORS, scalar_loop->header,
2035 4 : loop_preheader_edge (scalar_loop)->src);
2036 4 : preheader = split_edge (loop_preheader_edge (loop));
2037 4 : entry_e = single_pred_edge (preheader);
2038 : }
2039 :
2040 1602 : redirect_edge_and_branch_force (entry_e, new_preheader);
2041 1602 : flush_pending_stmts (entry_e);
2042 1602 : set_immediate_dominator (CDI_DOMINATORS, new_preheader, entry_e->src);
2043 :
2044 :
2045 : /* `vect_set_loop_condition' replaces the condition in the main exit of
2046 : loop. For counted loops, this is the IV counting exit, so in the case
2047 : of the prolog loop, we are replacing the old IV counting exit limit of
2048 : total loop niters for the new limit of the prolog niters, as desired.
2049 : For uncounted loops, we don't have an IV-counting exit to replace, so
2050 : we add a dummy exit to be consumed by `vect_set_loop_condition' later
2051 : on. */
2052 1602 : if (create_main_e)
2053 : {
2054 31 : edge to_latch_e = single_pred_edge (new_loop->latch);
2055 31 : bool latch_is_false = to_latch_e->flags & EDGE_FALSE_VALUE ? true
2056 : : false;
2057 :
2058 : /* Add new bb for duplicate exit. */
2059 31 : basic_block bbcond = split_edge (to_latch_e);
2060 31 : gimple_stmt_iterator a = gsi_last_bb (bbcond);
2061 :
2062 : /* Fix flags for the edge leading to the latch. */
2063 31 : to_latch_e = find_edge (bbcond, new_loop->latch);
2064 31 : to_latch_e->flags &= ~EDGE_FALLTHRU;
2065 31 : to_latch_e->flags |= latch_is_false ? EDGE_FALSE_VALUE
2066 : : EDGE_TRUE_VALUE;
2067 :
2068 : /* Build the condition. */
2069 31 : tree cone = build_int_cst (sizetype, 1);
2070 31 : tree czero = build_int_cst (sizetype, 0);
2071 31 : gcond *cond_copy = gimple_build_cond (NE_EXPR, cone, czero, NULL_TREE,
2072 : NULL_TREE);
2073 :
2074 31 : gsi_insert_after (&a, cond_copy, GSI_NEW_STMT);
2075 :
2076 : /* Add edge for exiting the loop via new condition. */
2077 38 : edge dup_exit = make_edge (bbcond, new_exit->dest, latch_is_false
2078 : ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE);
2079 :
2080 31 : profile_probability probability = profile_probability::even ();
2081 31 : to_latch_e->probability = dup_exit->probability = probability;
2082 :
2083 31 : set_immediate_dominator (CDI_DOMINATORS, dup_exit->src,
2084 : new_exit->src);
2085 31 : new_exit = dup_exit;
2086 31 : *new_e = new_exit;
2087 : }
2088 :
2089 1602 : redirect_edge_and_branch_force (new_exit, preheader);
2090 1602 : flush_pending_stmts (new_exit);
2091 1602 : set_immediate_dominator (CDI_DOMINATORS, preheader, new_exit->src);
2092 :
2093 : /* And remove the non-necessary forwarder again. Keep the other
2094 : one so we have a proper pre-header for the loop at the exit edge. */
2095 1602 : redirect_edge_pred (single_succ_edge (new_preheader),
2096 : single_pred (new_preheader));
2097 1602 : delete_basic_block (new_preheader);
2098 1602 : set_immediate_dominator (CDI_DOMINATORS, new_loop->header,
2099 1602 : loop_preheader_edge (new_loop)->src);
2100 :
2101 : /* Update dominators for multiple exits. */
2102 1602 : if (has_multiple_exits_p || create_main_e)
2103 : {
2104 1128 : for (edge alt_e : loop_exits)
2105 : {
2106 447 : if ((alt_e == loop_exit) && !create_main_e)
2107 196 : continue;
2108 251 : basic_block old_dom
2109 251 : = get_immediate_dominator (CDI_DOMINATORS, alt_e->dest);
2110 251 : if (flow_bb_inside_loop_p (loop, old_dom))
2111 : {
2112 104 : auto_vec<basic_block, 8> queue;
2113 104 : for (auto son = first_dom_son (CDI_DOMINATORS, old_dom);
2114 340 : son; son = next_dom_son (CDI_DOMINATORS, son))
2115 236 : if (!flow_bb_inside_loop_p (loop, son))
2116 132 : queue.safe_push (son);
2117 444 : for (auto son : queue)
2118 132 : set_immediate_dominator (CDI_DOMINATORS,
2119 : son, get_bb_copy (old_dom));
2120 104 : }
2121 : }
2122 : }
2123 :
2124 : /* When loop_exit != scalar_exit due to if-conversion loop versioning,
2125 : the `scalar_exit' now has two incoming edges, one from the if-converted
2126 : and one from the peeled prolog loop. It is therefore dominated by a
2127 : common block between these. Update its dominator accordingly. */
2128 227 : if (create_main_e && loop_exit != scalar_exit)
2129 0 : set_immediate_dominator (CDI_DOMINATORS, scalar_exit->dest,
2130 : recompute_dominator (CDI_DOMINATORS,
2131 : scalar_exit->dest));
2132 : }
2133 :
2134 34594 : free (new_bbs);
2135 34594 : free (bbs);
2136 :
2137 34594 : checking_verify_dominators (CDI_DOMINATORS);
2138 :
2139 34594 : return new_loop;
2140 34594 : }
2141 :
2142 :
2143 : /* Given the condition expression COND, put it as the last statement of
2144 : GUARD_BB; set both edges' probability; set dominator of GUARD_TO to
2145 : DOM_BB; return the skip edge. GUARD_TO is the target basic block to
2146 : skip the loop. PROBABILITY is the skip edge's probability. Mark the
2147 : new edge as irreducible if IRREDUCIBLE_P is true. */
2148 :
2149 : static edge
2150 50777 : slpeel_add_loop_guard (basic_block guard_bb, tree cond,
2151 : basic_block guard_to, basic_block dom_bb,
2152 : profile_probability probability, bool irreducible_p)
2153 : {
2154 50777 : gimple_stmt_iterator gsi;
2155 50777 : edge new_e, enter_e;
2156 50777 : gcond *cond_stmt;
2157 50777 : gimple_seq gimplify_stmt_list = NULL;
2158 :
2159 50777 : enter_e = EDGE_SUCC (guard_bb, 0);
2160 50777 : enter_e->flags &= ~EDGE_FALLTHRU;
2161 50777 : enter_e->flags |= EDGE_FALSE_VALUE;
2162 50777 : gsi = gsi_last_bb (guard_bb);
2163 :
2164 50777 : cond = force_gimple_operand_1 (cond, &gimplify_stmt_list,
2165 : is_gimple_condexpr_for_cond, NULL_TREE);
2166 50777 : if (gimplify_stmt_list)
2167 22921 : gsi_insert_seq_after (&gsi, gimplify_stmt_list, GSI_NEW_STMT);
2168 :
2169 50777 : cond_stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
2170 50777 : gsi = gsi_last_bb (guard_bb);
2171 50777 : gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
2172 :
2173 : /* Add new edge to connect guard block to the merge/loop-exit block. */
2174 50777 : new_e = make_edge (guard_bb, guard_to, EDGE_TRUE_VALUE);
2175 :
2176 50777 : new_e->probability = probability;
2177 50777 : if (irreducible_p)
2178 16 : new_e->flags |= EDGE_IRREDUCIBLE_LOOP;
2179 :
2180 50777 : enter_e->probability = probability.invert ();
2181 50777 : set_immediate_dominator (CDI_DOMINATORS, guard_to, dom_bb);
2182 :
2183 : /* Split enter_e to preserve LOOPS_HAVE_PREHEADERS. */
2184 50777 : if (enter_e->dest->loop_father->header == enter_e->dest)
2185 479 : split_edge (enter_e);
2186 :
2187 50777 : return new_e;
2188 : }
2189 :
2190 :
2191 : /* This function verifies that the following restrictions apply to LOOP:
2192 : (1) it consists of exactly 2 basic blocks - header, and an empty latch
2193 : for innermost loop and 5 basic blocks for outer-loop.
2194 : (2) it is single entry, single exit
2195 : (3) its exit condition is the last stmt in the header
2196 : (4) E is the entry/exit edge of LOOP.
2197 : */
2198 :
2199 : bool
2200 506605 : slpeel_can_duplicate_loop_p (const class loop *loop, const_edge exit_e,
2201 : const_edge e)
2202 : {
2203 506605 : edge entry_e = loop_preheader_edge (loop);
2204 506605 : gcond *orig_cond = get_loop_exit_condition (exit_e);
2205 506605 : gimple_stmt_iterator loop_exit_gsi = gsi_last_bb (exit_e->src);
2206 :
2207 : /* All loops have an outer scope; the only case loop->outer is NULL is for
2208 : the function itself. */
2209 506605 : if (!loop_outer (loop)
2210 506605 : || !empty_block_p (loop->latch)
2211 : || !exit_e
2212 : /* Verify that new loop exit condition can be trivially modified. */
2213 506605 : || (!orig_cond || orig_cond != gsi_stmt (loop_exit_gsi))
2214 1013210 : || (e != exit_e && e != entry_e))
2215 0 : return false;
2216 :
2217 506605 : basic_block *bbs = XNEWVEC (basic_block, loop->num_nodes);
2218 506605 : get_loop_body_with_size (loop, bbs, loop->num_nodes);
2219 506605 : bool ret = can_copy_bbs_p (bbs, loop->num_nodes);
2220 506605 : free (bbs);
2221 506605 : return ret;
2222 : }
2223 :
2224 : /* Function find_loop_location.
2225 :
2226 : Extract the location of the loop in the source code.
2227 : If the loop is not well formed for vectorization, an estimated
2228 : location is calculated.
2229 : Return the loop location if succeed and NULL if not. */
2230 :
2231 : dump_user_location_t
2232 3604516 : find_loop_location (class loop *loop)
2233 : {
2234 3604516 : gimple *stmt = NULL;
2235 3604516 : basic_block bb;
2236 3604516 : gimple_stmt_iterator si;
2237 :
2238 3604516 : if (!loop)
2239 0 : return dump_user_location_t ();
2240 :
2241 : /* For the root of the loop tree return the function location. */
2242 3604516 : if (!loop_outer (loop))
2243 0 : return dump_user_location_t::from_function_decl (cfun->decl);
2244 :
2245 3604516 : if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
2246 : {
2247 : /* We only care about the loop location, so use any exit with location
2248 : information. */
2249 11300375 : for (edge e : get_loop_exit_edges (loop))
2250 : {
2251 3699808 : stmt = get_loop_exit_condition (e);
2252 :
2253 3699808 : if (stmt
2254 3699808 : && LOCATION_LOCUS (gimple_location (stmt)) > BUILTINS_LOCATION)
2255 3153319 : return stmt;
2256 3604516 : }
2257 : }
2258 :
2259 : /* If we got here the loop is probably not "well formed",
2260 : try to estimate the loop location */
2261 :
2262 451197 : if (!loop->header)
2263 0 : return dump_user_location_t ();
2264 :
2265 451197 : bb = loop->header;
2266 :
2267 1602357 : for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2268 : {
2269 1031789 : stmt = gsi_stmt (si);
2270 1031789 : if (LOCATION_LOCUS (gimple_location (stmt)) > BUILTINS_LOCATION)
2271 331826 : return stmt;
2272 : }
2273 :
2274 119371 : return dump_user_location_t ();
2275 : }
2276 :
2277 : /* Return true if the phi described by STMT_INFO defines an IV of the
2278 : loop to be vectorized. */
2279 :
2280 : static bool
2281 1358849 : iv_phi_p (stmt_vec_info stmt_info)
2282 : {
2283 1358849 : gphi *phi = as_a <gphi *> (stmt_info->stmt);
2284 2717698 : if (virtual_operand_p (PHI_RESULT (phi)))
2285 : return false;
2286 :
2287 1080810 : if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def
2288 1080810 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_double_reduction_def)
2289 157531 : return false;
2290 :
2291 : return true;
2292 : }
2293 :
2294 : /* Return true if vectorizer can peel for nonlinear iv. */
2295 : static bool
2296 7910 : vect_can_peel_nonlinear_iv_p (loop_vec_info loop_vinfo,
2297 : stmt_vec_info stmt_info)
2298 : {
2299 7910 : enum vect_induction_op_type induction_type
2300 : = STMT_VINFO_LOOP_PHI_EVOLUTION_TYPE (stmt_info);
2301 7910 : tree niters_skip;
2302 : /* Init_expr will be update by vect_update_ivs_after_vectorizer,
2303 : if niters or vf is unknown:
2304 : For shift, when shift mount >= precision, there would be UD.
2305 : For mult, don't known how to generate
2306 : init_expr * pow (step, niters) for variable niters.
2307 : For neg unknown niters are ok, since niters of vectorized main loop
2308 : will always be multiple of 2.
2309 : See also PR113163, PR114196 and PR114485. */
2310 7910 : if (!LOOP_VINFO_VECT_FACTOR (loop_vinfo).is_constant ()
2311 7910 : || LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo)
2312 7910 : || (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
2313 3252 : && induction_type != vect_step_op_neg))
2314 : {
2315 3068 : if (dump_enabled_p ())
2316 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2317 : "Peeling for epilogue is not supported"
2318 : " for this nonlinear induction"
2319 : " when iteration count is unknown or"
2320 : " when using partial vectorization.\n");
2321 3068 : return false;
2322 : }
2323 :
2324 4842 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
2325 266 : && induction_type == vect_step_op_mul)
2326 : {
2327 24 : if (dump_enabled_p ())
2328 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2329 : "Peeling for is not supported for nonlinear mult"
2330 : " induction using partial vectorization.\n");
2331 24 : return false;
2332 : }
2333 :
2334 : /* Avoid compile time hog on vect_peel_nonlinear_iv_init. */
2335 4576 : if (induction_type == vect_step_op_mul)
2336 : {
2337 401 : tree step_expr = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_info);
2338 401 : tree type = TREE_TYPE (step_expr);
2339 :
2340 796 : if (wi::exact_log2 (wi::to_wide (step_expr)) == -1
2341 401 : && LOOP_VINFO_INT_NITERS(loop_vinfo) >= TYPE_PRECISION (type))
2342 : {
2343 6 : if (dump_enabled_p ())
2344 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2345 : "Avoid compile time hog on"
2346 : " vect_peel_nonlinear_iv_init"
2347 : " for nonlinear induction vec_step_op_mul"
2348 : " when iteration count is too big.\n");
2349 6 : return false;
2350 : }
2351 : }
2352 :
2353 : /* Also doesn't support peel for neg when niter is variable.
2354 : ??? generate something like niter_expr & 1 ? init_expr : -init_expr? */
2355 4812 : niters_skip = LOOP_VINFO_MASK_SKIP_NITERS (loop_vinfo);
2356 4812 : if ((niters_skip != NULL_TREE
2357 0 : && (TREE_CODE (niters_skip) != INTEGER_CST
2358 0 : || (HOST_WIDE_INT) TREE_INT_CST_LOW (niters_skip) < 0))
2359 4812 : || (!vect_use_loop_mask_for_alignment_p (loop_vinfo)
2360 4812 : && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) < 0))
2361 : {
2362 4 : if (dump_enabled_p ())
2363 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2364 : "Peeling for alignment is not supported"
2365 : " for nonlinear induction when niters_skip"
2366 : " is not constant.\n");
2367 4 : return false;
2368 : }
2369 :
2370 : return true;
2371 : }
2372 :
2373 : /* Function vect_can_advance_ivs_p
2374 :
2375 : In case the number of iterations that LOOP iterates is unknown at compile
2376 : time, an epilog loop will be generated, and the loop induction variables
2377 : (IVs) will be "advanced" to the value they are supposed to take just before
2378 : the epilog loop. Here we check that the access function of the loop IVs
2379 : and the expression that represents the loop bound are simple enough.
2380 : These restrictions will be relaxed in the future. */
2381 :
2382 : bool
2383 510275 : vect_can_advance_ivs_p (loop_vec_info loop_vinfo)
2384 : {
2385 510275 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2386 510275 : basic_block bb = loop->header;
2387 510275 : gphi_iterator gsi;
2388 :
2389 : /* Analyze phi functions of the loop header. */
2390 :
2391 510275 : if (dump_enabled_p ())
2392 25218 : dump_printf_loc (MSG_NOTE, vect_location, "vect_can_advance_ivs_p:\n");
2393 1770637 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2394 : {
2395 1264508 : tree evolution_part;
2396 1264508 : enum vect_induction_op_type induction_type;
2397 :
2398 1264508 : gphi *phi = gsi.phi ();
2399 1264508 : stmt_vec_info phi_info = loop_vinfo->lookup_stmt (phi);
2400 1264508 : if (dump_enabled_p ())
2401 72314 : dump_printf_loc (MSG_NOTE, vect_location, "Analyze phi: %G",
2402 : phi_info->stmt);
2403 :
2404 : /* Skip virtual phi's. The data dependences that are associated with
2405 : virtual defs/uses (i.e., memory accesses) are analyzed elsewhere.
2406 :
2407 : Skip reduction phis. */
2408 1264508 : if (!iv_phi_p (phi_info))
2409 : {
2410 392401 : if (dump_enabled_p ())
2411 25681 : dump_printf_loc (MSG_NOTE, vect_location,
2412 : "reduc or virtual phi. skip.\n");
2413 392401 : continue;
2414 : }
2415 :
2416 872107 : induction_type = STMT_VINFO_LOOP_PHI_EVOLUTION_TYPE (phi_info);
2417 872107 : if (induction_type != vect_step_op_add)
2418 : {
2419 7910 : if (!vect_can_peel_nonlinear_iv_p (loop_vinfo, phi_info))
2420 : return false;
2421 :
2422 4808 : continue;
2423 : }
2424 :
2425 : /* Analyze the evolution function. */
2426 :
2427 864197 : evolution_part = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (phi_info);
2428 864197 : if (evolution_part == NULL_TREE)
2429 : {
2430 1012 : if (dump_enabled_p ())
2431 81 : dump_printf (MSG_MISSED_OPTIMIZATION,
2432 : "No access function or evolution.\n");
2433 1012 : return false;
2434 : }
2435 :
2436 : /* FORNOW: We do not transform initial conditions of IVs
2437 : which evolution functions are not invariants in the loop. */
2438 :
2439 863185 : if (!expr_invariant_in_loop_p (loop, evolution_part))
2440 : {
2441 32 : if (dump_enabled_p ())
2442 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2443 : "evolution not invariant in loop.\n");
2444 32 : return false;
2445 : }
2446 :
2447 : /* FORNOW: We do not transform initial conditions of IVs
2448 : which evolution functions are a polynomial of degree >= 2. */
2449 :
2450 2123515 : if (tree_is_chrec (evolution_part))
2451 : {
2452 0 : if (dump_enabled_p ())
2453 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2454 : "evolution is chrec.\n");
2455 0 : return false;
2456 : }
2457 : }
2458 :
2459 : return true;
2460 : }
2461 :
2462 :
2463 : /* Function vect_update_ivs_after_vectorizer.
2464 :
2465 : "Advance" the induction variables of LOOP to the value they should take
2466 : after the execution of LOOP. This is currently necessary because the
2467 : vectorizer does not handle induction variables that are used after the
2468 : loop. Such a situation occurs when the last iterations of LOOP are
2469 : peeled, because:
2470 : 1. We introduced new uses after LOOP for IVs that were not originally used
2471 : after LOOP: the IVs of LOOP are now used by an epilog loop.
2472 : 2. LOOP is going to be vectorized; this means that it will iterate N/VF
2473 : times, whereas the loop IVs should be bumped N times.
2474 :
2475 : Input:
2476 : - LOOP - a loop that is going to be vectorized. The last few iterations
2477 : of LOOP were peeled.
2478 : - NITERS - the number of iterations that LOOP executes (before it is
2479 : vectorized). i.e, the number of times the ivs should be bumped.
2480 : - UPDATE_E - a successor edge of LOOP->exit that is on the (only) path
2481 : coming out from LOOP on which there are uses of the LOOP ivs
2482 : (this is the path from LOOP->exit to epilog_loop->preheader).
2483 :
2484 : The new definitions of the ivs are placed in LOOP->exit.
2485 : The phi args associated with the edge UPDATE_E in the bb
2486 : UPDATE_E->dest are updated accordingly.
2487 :
2488 : - EARLY_EXIT_P - Indicates whether the exit is an early exit rather than
2489 : the main latch exit.
2490 :
2491 : Assumption 1: Like the rest of the vectorizer, this function assumes
2492 : a single loop exit that has a single predecessor.
2493 :
2494 : Assumption 2: The phi nodes in the LOOP header and in update_bb are
2495 : organized in the same order.
2496 :
2497 : Assumption 3: The access function of the ivs is simple enough (see
2498 : vect_can_advance_ivs_p). This assumption will be relaxed in the future.
2499 :
2500 : Assumption 4: Exactly one of the successors of LOOP exit-bb is on a path
2501 : coming out of LOOP on which the ivs of LOOP are used (this is the path
2502 : that leads to the epilog loop; other paths skip the epilog loop). This
2503 : path starts with the edge UPDATE_E, and its destination (denoted update_bb)
2504 : needs to have its phis updated.
2505 : */
2506 :
2507 : static void
2508 33622 : vect_update_ivs_after_vectorizer (loop_vec_info loop_vinfo,
2509 : tree niters, edge update_e,
2510 : bool early_exit_p)
2511 : {
2512 33622 : gphi_iterator gsi, gsi1;
2513 33622 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2514 33622 : basic_block update_bb = update_e->dest;
2515 33622 : basic_block exit_bb = update_e->src;
2516 : /* Check to see if this is an empty loop pre-header block. If it exists
2517 : we need to use the edge from that block -> loop header for updates but
2518 : must use the original exit_bb to add any new adjustment because there
2519 : can be a skip_epilog edge bypassing the epilog and so the loop pre-header
2520 : too. */
2521 33697 : if (empty_block_p (update_bb) && single_succ_p (update_bb))
2522 : {
2523 75 : update_e = single_succ_edge (update_bb);
2524 75 : update_bb = update_e->dest;
2525 : }
2526 33622 : gimple_stmt_iterator last_gsi = gsi_last_bb (exit_bb);
2527 :
2528 33622 : for (gsi = gsi_start_phis (loop->header), gsi1 = gsi_start_phis (update_bb);
2529 127963 : !gsi_end_p (gsi) && !gsi_end_p (gsi1);
2530 94341 : gsi_next (&gsi), gsi_next (&gsi1))
2531 : {
2532 94341 : tree init_expr;
2533 94341 : tree step_expr, off;
2534 94341 : tree type;
2535 94341 : tree var, ni, ni_name;
2536 :
2537 94341 : gphi *phi = gsi.phi ();
2538 94341 : gphi *phi1 = gsi1.phi ();
2539 94341 : stmt_vec_info phi_info = loop_vinfo->lookup_stmt (phi);
2540 94341 : if (dump_enabled_p ())
2541 12243 : dump_printf_loc (MSG_NOTE, vect_location,
2542 : "vect_update_ivs_after_vectorizer: phi: %G",
2543 : (gimple *) phi);
2544 :
2545 : /* Skip reduction and virtual phis. */
2546 94341 : if (!iv_phi_p (phi_info))
2547 : {
2548 43169 : if (dump_enabled_p ())
2549 4763 : dump_printf_loc (MSG_NOTE, vect_location,
2550 : "reduc or virtual phi. skip.\n");
2551 43169 : continue;
2552 : }
2553 :
2554 51172 : type = TREE_TYPE (gimple_phi_result (phi));
2555 51172 : step_expr = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (phi_info);
2556 51172 : step_expr = unshare_expr (step_expr);
2557 :
2558 : /* FORNOW: We do not support IVs whose evolution function is a polynomial
2559 : of degree >= 2 or exponential. */
2560 51172 : gcc_assert (!tree_is_chrec (step_expr));
2561 :
2562 51172 : init_expr = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
2563 51172 : gimple_seq stmts = NULL;
2564 51172 : enum vect_induction_op_type induction_type
2565 : = STMT_VINFO_LOOP_PHI_EVOLUTION_TYPE (phi_info);
2566 :
2567 51172 : if (induction_type == vect_step_op_add)
2568 : {
2569 51044 : tree stype = TREE_TYPE (step_expr);
2570 51044 : off = fold_build2 (MULT_EXPR, stype,
2571 : fold_convert (stype, niters), step_expr);
2572 :
2573 51044 : if (POINTER_TYPE_P (type))
2574 3633 : ni = fold_build_pointer_plus (init_expr, off);
2575 : else
2576 47411 : ni = fold_convert (type,
2577 : fold_build2 (PLUS_EXPR, stype,
2578 : fold_convert (stype, init_expr),
2579 : off));
2580 : }
2581 : /* Don't bother call vect_peel_nonlinear_iv_init. */
2582 128 : else if (induction_type == vect_step_op_neg)
2583 : ni = init_expr;
2584 : else
2585 84 : ni = vect_peel_nonlinear_iv_init (&stmts, init_expr,
2586 : niters, step_expr,
2587 : induction_type, early_exit_p);
2588 :
2589 51172 : var = create_tmp_var (type, "tmp");
2590 :
2591 51172 : gimple_seq new_stmts = NULL;
2592 51172 : ni_name = force_gimple_operand (ni, &new_stmts, false, var);
2593 :
2594 : /* Exit_bb shouldn't be empty, but we also can't insert after a ctrl
2595 : statements. */
2596 51172 : if (!gsi_end_p (last_gsi) && !is_ctrl_stmt (gsi_stmt (last_gsi)))
2597 : {
2598 276 : gsi_insert_seq_after (&last_gsi, stmts, GSI_SAME_STMT);
2599 276 : gsi_insert_seq_after (&last_gsi, new_stmts, GSI_SAME_STMT);
2600 : }
2601 : else
2602 : {
2603 50896 : gsi_insert_seq_before (&last_gsi, stmts, GSI_SAME_STMT);
2604 50896 : gsi_insert_seq_before (&last_gsi, new_stmts, GSI_SAME_STMT);
2605 : }
2606 :
2607 : /* Update the PHI argument on the requested edge. */
2608 51172 : adjust_phi_and_debug_stmts (phi1, update_e, ni_name);
2609 : }
2610 33622 : }
2611 :
2612 : /* Return a gimple value containing the misalignment (measured in vector
2613 : elements) for the loop described by LOOP_VINFO, i.e. how many elements
2614 : it is away from a perfectly aligned address. Add any new statements
2615 : to SEQ. */
2616 :
2617 : static tree
2618 205 : get_misalign_in_elems (gimple **seq, loop_vec_info loop_vinfo)
2619 : {
2620 205 : dr_vec_info *dr_info = LOOP_VINFO_UNALIGNED_DR (loop_vinfo);
2621 205 : stmt_vec_info stmt_info = dr_info->stmt;
2622 205 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2623 :
2624 205 : poly_uint64 target_align = DR_TARGET_ALIGNMENT (dr_info);
2625 205 : unsigned HOST_WIDE_INT target_align_c;
2626 205 : tree target_align_minus_1;
2627 :
2628 205 : bool negative = tree_int_cst_compare (DR_STEP (dr_info->dr),
2629 205 : size_zero_node) < 0;
2630 205 : tree offset = (negative
2631 205 : ? size_int ((-TYPE_VECTOR_SUBPARTS (vectype) + 1)
2632 : * TREE_INT_CST_LOW
2633 : (TYPE_SIZE_UNIT (TREE_TYPE (vectype))))
2634 202 : : size_zero_node);
2635 205 : tree start_addr = vect_create_addr_base_for_vector_ref (loop_vinfo,
2636 : stmt_info, seq,
2637 : offset);
2638 205 : tree type = unsigned_type_for (TREE_TYPE (start_addr));
2639 205 : if (target_align.is_constant (&target_align_c))
2640 205 : target_align_minus_1 = build_int_cst (type, target_align_c - 1);
2641 : else
2642 : {
2643 : tree vla = build_int_cst (type, target_align);
2644 : target_align_minus_1 = fold_build2 (MINUS_EXPR, type, vla,
2645 : build_int_cst (type, 1));
2646 : }
2647 :
2648 205 : HOST_WIDE_INT elem_size
2649 205 : = int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
2650 410 : tree elem_size_log = build_int_cst (type, exact_log2 (elem_size));
2651 :
2652 : /* Create: misalign_in_bytes = addr & (target_align - 1). */
2653 205 : tree int_start_addr = fold_convert (type, start_addr);
2654 205 : tree misalign_in_bytes = fold_build2 (BIT_AND_EXPR, type, int_start_addr,
2655 : target_align_minus_1);
2656 :
2657 : /* Create: misalign_in_elems = misalign_in_bytes / element_size. */
2658 205 : tree misalign_in_elems = fold_build2 (RSHIFT_EXPR, type, misalign_in_bytes,
2659 : elem_size_log);
2660 :
2661 205 : return misalign_in_elems;
2662 : }
2663 :
2664 : /* Function vect_gen_prolog_loop_niters
2665 :
2666 : Generate the number of iterations which should be peeled as prolog for the
2667 : loop represented by LOOP_VINFO. It is calculated as the misalignment of
2668 : DR - the data reference recorded in LOOP_VINFO_UNALIGNED_DR (LOOP_VINFO).
2669 : As a result, after the execution of this loop, the data reference DR will
2670 : refer to an aligned location. The following computation is generated:
2671 :
2672 : If the misalignment of DR is known at compile time:
2673 : addr_mis = int mis = DR_MISALIGNMENT (dr);
2674 : Else, compute address misalignment in bytes:
2675 : addr_mis = addr & (target_align - 1)
2676 :
2677 : prolog_niters = ((VF - addr_mis/elem_size)&(VF-1))/step
2678 :
2679 : (elem_size = element type size; an element is the scalar element whose type
2680 : is the inner type of the vectype)
2681 :
2682 : The computations will be emitted at the end of BB. We also compute and
2683 : store upper bound (included) of the result in BOUND.
2684 :
2685 : When the step of the data-ref in the loop is not 1 (as in interleaved data
2686 : and SLP), the number of iterations of the prolog must be divided by the step
2687 : (which is equal to the size of interleaved group).
2688 :
2689 : The above formulas assume that VF == number of elements in the vector. This
2690 : may not hold when there are multiple-types in the loop.
2691 : In this case, for some data-references in the loop the VF does not represent
2692 : the number of elements that fit in the vector. Therefore, instead of VF we
2693 : use TYPE_VECTOR_SUBPARTS. */
2694 :
2695 : static tree
2696 433 : vect_gen_prolog_loop_niters (loop_vec_info loop_vinfo,
2697 : basic_block bb, poly_int64 *bound)
2698 : {
2699 433 : dr_vec_info *dr_info = LOOP_VINFO_UNALIGNED_DR (loop_vinfo);
2700 433 : tree var;
2701 433 : tree niters_type
2702 433 : = LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo) ? sizetype
2703 433 : : TREE_TYPE (LOOP_VINFO_NITERS
2704 : (loop_vinfo));
2705 433 : gimple_seq stmts = NULL, new_stmts = NULL;
2706 433 : tree iters, iters_name;
2707 433 : stmt_vec_info stmt_info = dr_info->stmt;
2708 433 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2709 433 : poly_uint64 target_align = DR_TARGET_ALIGNMENT (dr_info);
2710 :
2711 433 : if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) > 0)
2712 : {
2713 228 : int npeel = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
2714 :
2715 228 : if (dump_enabled_p ())
2716 217 : dump_printf_loc (MSG_NOTE, vect_location,
2717 : "known peeling = %d.\n", npeel);
2718 :
2719 228 : iters = build_int_cst (niters_type, npeel);
2720 228 : *bound = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
2721 : }
2722 : else
2723 : {
2724 205 : tree misalign_in_elems = get_misalign_in_elems (&stmts, loop_vinfo);
2725 205 : tree type = TREE_TYPE (misalign_in_elems);
2726 205 : HOST_WIDE_INT elem_size
2727 205 : = int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
2728 : /* We only do prolog peeling if the target alignment is known at compile
2729 : time. */
2730 205 : poly_uint64 align_in_elems =
2731 205 : exact_div (target_align, elem_size);
2732 205 : tree align_in_elems_minus_1 =
2733 205 : build_int_cst (type, align_in_elems - 1);
2734 205 : tree align_in_elems_tree = build_int_cst (type, align_in_elems);
2735 :
2736 : /* Create: (niters_type) ((align_in_elems - misalign_in_elems)
2737 : & (align_in_elems - 1)). */
2738 205 : bool negative = tree_int_cst_compare (DR_STEP (dr_info->dr),
2739 205 : size_zero_node) < 0;
2740 205 : if (negative)
2741 3 : iters = fold_build2 (MINUS_EXPR, type, misalign_in_elems,
2742 : align_in_elems_tree);
2743 : else
2744 202 : iters = fold_build2 (MINUS_EXPR, type, align_in_elems_tree,
2745 : misalign_in_elems);
2746 205 : iters = fold_build2 (BIT_AND_EXPR, type, iters, align_in_elems_minus_1);
2747 205 : iters = fold_convert (niters_type, iters);
2748 205 : *bound = align_in_elems;
2749 : }
2750 :
2751 433 : if (dump_enabled_p ())
2752 279 : dump_printf_loc (MSG_NOTE, vect_location,
2753 : "niters for prolog loop: %T\n", iters);
2754 :
2755 433 : var = create_tmp_var (niters_type, "prolog_loop_niters");
2756 433 : iters_name = force_gimple_operand (iters, &new_stmts, false, var);
2757 :
2758 433 : if (new_stmts)
2759 205 : gimple_seq_add_seq (&stmts, new_stmts);
2760 433 : if (stmts)
2761 : {
2762 205 : gcc_assert (single_succ_p (bb));
2763 205 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
2764 205 : if (gsi_end_p (gsi))
2765 42 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
2766 : else
2767 163 : gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
2768 : }
2769 433 : return iters_name;
2770 : }
2771 :
2772 :
2773 : /* Function vect_update_init_of_dr
2774 :
2775 : If CODE is PLUS, the vector loop starts NITERS iterations after the
2776 : scalar one, otherwise CODE is MINUS and the vector loop starts NITERS
2777 : iterations before the scalar one (using masking to skip inactive
2778 : elements). This function updates the information recorded in DR to
2779 : account for the difference. Specifically, it updates the OFFSET
2780 : field of DR_INFO. */
2781 :
2782 : static void
2783 24401 : vect_update_init_of_dr (dr_vec_info *dr_info, tree niters, tree_code code)
2784 : {
2785 24401 : struct data_reference *dr = dr_info->dr;
2786 24401 : tree offset = dr_info->offset;
2787 24401 : if (!offset)
2788 24401 : offset = build_zero_cst (sizetype);
2789 :
2790 24401 : niters = fold_build2 (MULT_EXPR, sizetype,
2791 : fold_convert (sizetype, niters),
2792 : fold_convert (sizetype, DR_STEP (dr)));
2793 24401 : offset = fold_build2 (code, sizetype,
2794 : fold_convert (sizetype, offset), niters);
2795 24401 : dr_info->offset = offset;
2796 24401 : }
2797 :
2798 :
2799 : /* Function vect_update_inits_of_drs
2800 :
2801 : Apply vect_update_inits_of_dr to all accesses in LOOP_VINFO.
2802 : CODE and NITERS are as for vect_update_inits_of_dr. */
2803 :
2804 : void
2805 7289 : vect_update_inits_of_drs (loop_vec_info loop_vinfo, tree niters,
2806 : tree_code code)
2807 : {
2808 7289 : unsigned int i;
2809 7289 : vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2810 7289 : struct data_reference *dr;
2811 :
2812 7289 : DUMP_VECT_SCOPE ("vect_update_inits_of_dr");
2813 :
2814 : /* Adjust niters to sizetype. We used to insert the stmts on loop preheader
2815 : here, but since we might use these niters to update the epilogues niters
2816 : and data references we can't insert them here as this definition might not
2817 : always dominate its uses. */
2818 7289 : if (!types_compatible_p (sizetype, TREE_TYPE (niters)))
2819 4600 : niters = fold_convert (sizetype, niters);
2820 :
2821 39284 : FOR_EACH_VEC_ELT (datarefs, i, dr)
2822 : {
2823 24826 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2824 24826 : if (!STMT_VINFO_GATHER_SCATTER_P (dr_info->stmt)
2825 24401 : && !STMT_VINFO_SIMD_LANE_ACCESS_P (dr_info->stmt))
2826 24401 : vect_update_init_of_dr (dr_info, niters, code);
2827 : }
2828 7289 : }
2829 :
2830 : /* For the information recorded in LOOP_VINFO prepare the loop for peeling
2831 : by masking. This involves calculating the number of iterations to
2832 : be peeled and then aligning all memory references appropriately. */
2833 :
2834 : void
2835 1 : vect_prepare_for_masked_peels (loop_vec_info loop_vinfo)
2836 : {
2837 1 : tree misalign_in_elems;
2838 1 : tree type = TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo));
2839 :
2840 1 : gcc_assert (vect_use_loop_mask_for_alignment_p (loop_vinfo));
2841 :
2842 : /* From the information recorded in LOOP_VINFO get the number of iterations
2843 : that need to be skipped via masking. */
2844 1 : if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) > 0)
2845 : {
2846 1 : poly_int64 misalign = (LOOP_VINFO_VECT_FACTOR (loop_vinfo)
2847 1 : - LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo));
2848 1 : misalign_in_elems = build_int_cst (type, misalign);
2849 : }
2850 : else
2851 : {
2852 0 : gimple_seq seq1 = NULL, seq2 = NULL;
2853 0 : misalign_in_elems = get_misalign_in_elems (&seq1, loop_vinfo);
2854 0 : misalign_in_elems = fold_convert (type, misalign_in_elems);
2855 0 : misalign_in_elems = force_gimple_operand (misalign_in_elems,
2856 : &seq2, true, NULL_TREE);
2857 0 : gimple_seq_add_seq (&seq1, seq2);
2858 0 : if (seq1)
2859 : {
2860 0 : edge pe = loop_preheader_edge (LOOP_VINFO_LOOP (loop_vinfo));
2861 0 : basic_block new_bb = gsi_insert_seq_on_edge_immediate (pe, seq1);
2862 0 : gcc_assert (!new_bb);
2863 : }
2864 : }
2865 :
2866 1 : if (dump_enabled_p ())
2867 1 : dump_printf_loc (MSG_NOTE, vect_location,
2868 : "misalignment for fully-masked loop: %T\n",
2869 : misalign_in_elems);
2870 :
2871 1 : LOOP_VINFO_MASK_SKIP_NITERS (loop_vinfo) = misalign_in_elems;
2872 :
2873 1 : vect_update_inits_of_drs (loop_vinfo, misalign_in_elems, MINUS_EXPR);
2874 1 : }
2875 :
2876 : /* This function builds ni_name = number of iterations. Statements
2877 : are emitted on the loop preheader edge. If NEW_VAR_P is not NULL, set
2878 : it to TRUE if new ssa_var is generated. */
2879 :
2880 : tree
2881 62412 : vect_build_loop_niters (loop_vec_info loop_vinfo, bool *new_var_p)
2882 : {
2883 62412 : if (LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo))
2884 : return NULL_TREE;
2885 62338 : tree ni = unshare_expr (LOOP_VINFO_NITERS (loop_vinfo));
2886 62338 : if (TREE_CODE (ni) == INTEGER_CST)
2887 : return ni;
2888 : else
2889 : {
2890 26031 : tree ni_name, var;
2891 26031 : gimple_seq stmts = NULL;
2892 26031 : edge pe = loop_preheader_edge (LOOP_VINFO_LOOP (loop_vinfo));
2893 :
2894 26031 : var = create_tmp_var (TREE_TYPE (ni), "niters");
2895 26031 : ni_name = force_gimple_operand (ni, &stmts, false, var);
2896 26031 : if (stmts)
2897 : {
2898 24973 : gsi_insert_seq_on_edge_immediate (pe, stmts);
2899 24973 : if (new_var_p != NULL)
2900 215 : *new_var_p = true;
2901 : }
2902 :
2903 26031 : return ni_name;
2904 : }
2905 : }
2906 :
2907 : /* Calculate the number of iterations above which vectorized loop will be
2908 : preferred than scalar loop. NITERS_PROLOG is the number of iterations
2909 : of prolog loop. If it's integer const, the integer number is also passed
2910 : in INT_NITERS_PROLOG. BOUND_PROLOG is the upper bound (inclusive) of the
2911 : number of iterations of the prolog loop. BOUND_EPILOG is the corresponding
2912 : value for the epilog loop. If CHECK_PROFITABILITY is true, TH is the
2913 : threshold below which the scalar (rather than vectorized) loop will be
2914 : executed. This function stores the upper bound (inclusive) of the result
2915 : in BOUND_SCALAR. */
2916 :
2917 : static tree
2918 25013 : vect_gen_scalar_loop_niters (tree niters_prolog, int int_niters_prolog,
2919 : poly_int64 bound_prolog, poly_int64 bound_epilog,
2920 : int th, poly_uint64 *bound_scalar,
2921 : bool check_profitability)
2922 : {
2923 25013 : tree type = TREE_TYPE (niters_prolog);
2924 25013 : tree niters = fold_build2 (PLUS_EXPR, type, niters_prolog,
2925 : build_int_cst (type, bound_epilog));
2926 :
2927 25013 : *bound_scalar = bound_prolog + bound_epilog;
2928 25013 : if (check_profitability)
2929 : {
2930 : /* TH indicates the minimum niters of vectorized loop, while we
2931 : compute the maximum niters of scalar loop. */
2932 16084 : th--;
2933 : /* Peeling for constant times. */
2934 16084 : if (int_niters_prolog >= 0)
2935 : {
2936 16045 : *bound_scalar = upper_bound (int_niters_prolog + bound_epilog, th);
2937 16045 : return build_int_cst (type, *bound_scalar);
2938 : }
2939 : /* Peeling an unknown number of times. Note that both BOUND_PROLOG
2940 : and BOUND_EPILOG are inclusive upper bounds. */
2941 39 : if (known_ge (th, bound_prolog + bound_epilog))
2942 : {
2943 0 : *bound_scalar = th;
2944 0 : return build_int_cst (type, th);
2945 : }
2946 : /* Need to do runtime comparison. */
2947 39 : else if (maybe_gt (th, bound_epilog))
2948 : {
2949 39 : *bound_scalar = upper_bound (*bound_scalar, th);
2950 39 : return fold_build2 (MAX_EXPR, type,
2951 : build_int_cst (type, th), niters);
2952 : }
2953 : }
2954 : return niters;
2955 : }
2956 :
2957 : /* NITERS is the number of times that the original scalar loop executes
2958 : after peeling. Work out the maximum number of iterations N that can
2959 : be handled by the vectorized form of the loop and then either:
2960 :
2961 : a) set *STEP_VECTOR_PTR to the vectorization factor and generate:
2962 :
2963 : niters_vector = N
2964 :
2965 : b) set *STEP_VECTOR_PTR to one and generate:
2966 :
2967 : niters_vector = N / vf
2968 :
2969 : In both cases, store niters_vector in *NITERS_VECTOR_PTR and add
2970 : any new statements on the loop preheader edge. NITERS_NO_OVERFLOW
2971 : is true if NITERS doesn't overflow (i.e. if NITERS is always nonzero).
2972 :
2973 : Case (a) is used for LOOP_VINFO_USING_PARTIAL_VECTORS_P or if VF is
2974 : variable. As stated above, NITERS_VECTOR then equals the number
2975 : of scalar iterations and vect_set_loop_condition will handle the
2976 : step. As opposed to (b) we don't know anything about NITER_VECTOR's
2977 : range here.
2978 : */
2979 :
2980 : void
2981 33742 : vect_gen_vector_loop_niters (loop_vec_info loop_vinfo, tree niters,
2982 : tree *niters_vector_ptr, tree *step_vector_ptr,
2983 : bool niters_no_overflow)
2984 : {
2985 33742 : tree ni_minus_gap, var;
2986 33742 : tree niters_vector, step_vector;
2987 33742 : tree type = niters ? TREE_TYPE (niters) : sizetype;
2988 33742 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2989 33742 : edge pe = loop_preheader_edge (LOOP_VINFO_LOOP (loop_vinfo));
2990 :
2991 : /* If epilogue loop is required because of data accesses with gaps, we
2992 : subtract one iteration from the total number of iterations here for
2993 : correct calculation of RATIO. */
2994 33742 : if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo))
2995 : {
2996 388 : ni_minus_gap = fold_build2 (MINUS_EXPR, type, niters,
2997 : build_one_cst (type));
2998 388 : if (!is_gimple_val (ni_minus_gap))
2999 : {
3000 186 : var = create_tmp_var (type, "ni_gap");
3001 186 : gimple *stmts = NULL;
3002 186 : ni_minus_gap = force_gimple_operand (ni_minus_gap, &stmts,
3003 : true, var);
3004 186 : gsi_insert_seq_on_edge_immediate (pe, stmts);
3005 : }
3006 : }
3007 : else
3008 : ni_minus_gap = niters;
3009 :
3010 : /* To silence some unexpected warnings, simply initialize to 0. */
3011 33742 : unsigned HOST_WIDE_INT const_vf = 0;
3012 33742 : if (vf.is_constant (&const_vf)
3013 33742 : && !LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo)
3014 33724 : && LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo))
3015 : {
3016 : /* Create: niters / vf, which is equivalent to niters >> log2(vf) when
3017 : vf is a power of two, and when not we approximate using a
3018 : truncating division. */
3019 : /* If it's known that niters == number of latch executions + 1 doesn't
3020 : overflow, we can generate niters / vf; otherwise we generate
3021 : (niters - vf) / vf + 1 by using the fact that we know ratio
3022 : will be at least one. */
3023 33724 : tree var_vf = build_int_cst (type, const_vf);
3024 33724 : if (niters_no_overflow)
3025 33547 : niters_vector = fold_build2 (TRUNC_DIV_EXPR, type, ni_minus_gap,
3026 : var_vf);
3027 : else
3028 177 : niters_vector
3029 177 : = fold_build2 (PLUS_EXPR, type,
3030 : fold_build2 (TRUNC_DIV_EXPR, type,
3031 : fold_build2 (MINUS_EXPR, type,
3032 : ni_minus_gap,
3033 : var_vf),
3034 : var_vf),
3035 : build_int_cst (type, 1));
3036 33724 : step_vector = build_one_cst (type);
3037 : }
3038 : else
3039 : {
3040 18 : niters_vector = ni_minus_gap;
3041 18 : step_vector = build_int_cst (type, vf);
3042 : }
3043 :
3044 33742 : if (!is_gimple_val (niters_vector))
3045 : {
3046 25808 : var = create_tmp_var (type, "bnd");
3047 25808 : gimple_seq stmts = NULL;
3048 25808 : niters_vector = force_gimple_operand (niters_vector, &stmts, true, var);
3049 25808 : gsi_insert_seq_on_edge_immediate (pe, stmts);
3050 : /* Peeling algorithm guarantees that vector loop bound is at least ONE,
3051 : we set range information to make niters analyzer's life easier.
3052 : Note the number of latch iteration value can be TYPE_MAX_VALUE so
3053 : we have to represent the vector niter TYPE_MAX_VALUE + 1 / vf. */
3054 25808 : if (stmts != NULL
3055 25808 : && integer_onep (step_vector))
3056 : {
3057 25793 : if (niters_no_overflow)
3058 : {
3059 25634 : int_range<1> vr (type,
3060 51268 : wi::one (TYPE_PRECISION (type)),
3061 51268 : wi::div_trunc (wi::max_value
3062 25634 : (TYPE_PRECISION (type),
3063 25634 : TYPE_SIGN (type)),
3064 : const_vf,
3065 51268 : TYPE_SIGN (type)));
3066 25634 : set_range_info (niters_vector, vr);
3067 25634 : }
3068 : /* For VF == 1 the vector IV might also overflow so we cannot
3069 : assert a minimum value of 1. */
3070 159 : else if (const_vf > 1)
3071 : {
3072 123 : int_range<1> vr (type,
3073 246 : wi::one (TYPE_PRECISION (type)),
3074 369 : wi::rshift (wi::max_value (TYPE_PRECISION (type),
3075 123 : TYPE_SIGN (type))
3076 246 : - (const_vf - 1),
3077 246 : exact_log2 (const_vf), TYPE_SIGN (type))
3078 492 : + 1);
3079 123 : set_range_info (niters_vector, vr);
3080 123 : }
3081 : }
3082 : }
3083 33742 : *niters_vector_ptr = niters_vector;
3084 33742 : *step_vector_ptr = step_vector;
3085 :
3086 33742 : return;
3087 : }
3088 :
3089 : /* Finds the amount IV's should be incremented by each iteration.
3090 : Stored in LOOP_VINFO_IV_INCREMENT. */
3091 :
3092 : tree
3093 61979 : vect_get_loop_iv_increment (loop_vec_info loop_vinfo)
3094 : {
3095 61979 : if (LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
3096 : {
3097 : /* For now, set this as varying.
3098 : Fill this in later when building the loop controls. */
3099 0 : tree iv_increment = make_temp_ssa_name (sizetype, NULL, "iv_increment");
3100 :
3101 0 : gcall *varying_call = gimple_build_call_internal (IFN_VARYING, 0);
3102 0 : gimple_call_set_lhs (varying_call, iv_increment);
3103 :
3104 0 : gimple_stmt_iterator gsi = gsi_after_labels (loop_vinfo->loop->header);
3105 0 : gsi_insert_before (&gsi, varying_call, GSI_NEW_STMT);
3106 :
3107 0 : return iv_increment;
3108 : }
3109 : else
3110 61979 : return build_int_cst (sizetype, LOOP_VINFO_VECT_FACTOR (loop_vinfo));
3111 : }
3112 :
3113 : /* Given NITERS_VECTOR which is the number of iterations for vectorized
3114 : loop specified by LOOP_VINFO after vectorization, compute the number
3115 : of iterations before vectorization (niters_vector * vf) and store it
3116 : to NITERS_VECTOR_MULT_VF_PTR. */
3117 :
3118 : static void
3119 32949 : vect_gen_vector_loop_niters_mult_vf (loop_vec_info loop_vinfo,
3120 : tree niters_vector,
3121 : tree *niters_vector_mult_vf_ptr)
3122 : {
3123 : /* We should be using a step_vector of VF if VF is variable. */
3124 32949 : int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo).to_constant ();
3125 32949 : tree type = TREE_TYPE (niters_vector);
3126 32949 : tree tree_vf = build_int_cst (type, vf);
3127 32949 : basic_block exit_bb = LOOP_VINFO_MAIN_EXIT (loop_vinfo)->dest;
3128 :
3129 32949 : gcc_assert (niters_vector_mult_vf_ptr != NULL);
3130 32949 : tree niters_vector_mult_vf = fold_build2 (MULT_EXPR, type,
3131 : niters_vector, tree_vf);
3132 :
3133 : /* If we've peeled a vector iteration then subtract one full vector
3134 : iteration. */
3135 32949 : if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
3136 19 : niters_vector_mult_vf = fold_build2 (MINUS_EXPR, type,
3137 : niters_vector_mult_vf, tree_vf);
3138 :
3139 32949 : if (!is_gimple_val (niters_vector_mult_vf))
3140 : {
3141 25038 : tree var = create_tmp_var (type, "niters_vector_mult_vf");
3142 25038 : gimple_seq stmts = NULL;
3143 25038 : niters_vector_mult_vf = force_gimple_operand (niters_vector_mult_vf,
3144 : &stmts, true, var);
3145 25038 : gimple_stmt_iterator gsi = gsi_start_bb (exit_bb);
3146 25038 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
3147 : }
3148 32949 : *niters_vector_mult_vf_ptr = niters_vector_mult_vf;
3149 32949 : }
3150 :
3151 : /* Function slpeel_add_loop_guard adds guard skipping from the beginning
3152 : of SKIP_LOOP to the beginning of UPDATE_LOOP. GUARD_EDGE and MERGE_EDGE
3153 : are two pred edges of the merge point before UPDATE_LOOP. The two loops
3154 : appear like below:
3155 :
3156 : guard_bb:
3157 : if (cond)
3158 : goto merge_bb;
3159 : else
3160 : goto skip_loop;
3161 :
3162 : skip_loop:
3163 : header_a:
3164 : i_1 = PHI<i_0, i_2>;
3165 : ...
3166 : i_2 = i_1 + 1;
3167 : if (cond_a)
3168 : goto latch_a;
3169 : else
3170 : goto exit_a;
3171 : latch_a:
3172 : goto header_a;
3173 :
3174 : exit_a:
3175 : i_5 = PHI<i_2>;
3176 :
3177 : merge_bb:
3178 : ;; PHI (i_x = PHI<i_0, i_5>) to be created at merge point.
3179 :
3180 : update_loop:
3181 : header_b:
3182 : i_3 = PHI<i_5, i_4>; ;; Use of i_5 to be replaced with i_x.
3183 : ...
3184 : i_4 = i_3 + 1;
3185 : if (cond_b)
3186 : goto latch_b;
3187 : else
3188 : goto exit_bb;
3189 : latch_b:
3190 : goto header_b;
3191 :
3192 : exit_bb:
3193 :
3194 : This function creates PHI nodes at merge_bb and replaces the use of i_5
3195 : in the update_loop's PHI node with the result of new PHI result. */
3196 :
3197 : static void
3198 25446 : slpeel_update_phi_nodes_for_guard1 (class loop *skip_loop,
3199 : class loop *update_loop,
3200 : edge guard_edge, edge merge_edge)
3201 : {
3202 25446 : location_t merge_loc, guard_loc;
3203 25446 : edge orig_e = loop_preheader_edge (skip_loop);
3204 25446 : edge update_e = loop_preheader_edge (update_loop);
3205 25446 : gphi_iterator gsi_orig, gsi_update;
3206 :
3207 25446 : for ((gsi_orig = gsi_start_phis (skip_loop->header),
3208 25446 : gsi_update = gsi_start_phis (update_loop->header));
3209 93090 : !gsi_end_p (gsi_orig) && !gsi_end_p (gsi_update);
3210 67644 : gsi_next (&gsi_orig), gsi_next (&gsi_update))
3211 : {
3212 67644 : gphi *orig_phi = gsi_orig.phi ();
3213 67644 : gphi *update_phi = gsi_update.phi ();
3214 :
3215 : /* Generate new phi node at merge bb of the guard. */
3216 67644 : tree new_res = copy_ssa_name (PHI_RESULT (orig_phi));
3217 67644 : gphi *new_phi = create_phi_node (new_res, guard_edge->dest);
3218 :
3219 : /* Merge bb has two incoming edges: GUARD_EDGE and MERGE_EDGE. Set the
3220 : args in NEW_PHI for these edges. */
3221 67644 : tree merge_arg = PHI_ARG_DEF_FROM_EDGE (update_phi, update_e);
3222 67644 : tree guard_arg = PHI_ARG_DEF_FROM_EDGE (orig_phi, orig_e);
3223 67644 : merge_loc = gimple_phi_arg_location_from_edge (update_phi, update_e);
3224 67644 : guard_loc = gimple_phi_arg_location_from_edge (orig_phi, orig_e);
3225 67644 : add_phi_arg (new_phi, merge_arg, merge_edge, merge_loc);
3226 67644 : add_phi_arg (new_phi, guard_arg, guard_edge, guard_loc);
3227 :
3228 : /* Update phi in UPDATE_PHI. */
3229 67644 : adjust_phi_and_debug_stmts (update_phi, update_e, new_res);
3230 : }
3231 25446 : }
3232 :
3233 : /* LOOP_VINFO is an epilogue loop whose corresponding main loop can be skipped.
3234 : Return a value that equals:
3235 :
3236 : - MAIN_LOOP_VALUE when LOOP_VINFO is entered from the main loop and
3237 : - SKIP_VALUE when the main loop is skipped. */
3238 :
3239 : tree
3240 3892 : vect_get_main_loop_result (loop_vec_info loop_vinfo, tree main_loop_value,
3241 : tree skip_value)
3242 : {
3243 3892 : gcc_assert (loop_vinfo->main_loop_edge);
3244 :
3245 3892 : tree phi_result = make_ssa_name (TREE_TYPE (main_loop_value));
3246 3892 : basic_block bb = loop_vinfo->main_loop_edge->dest;
3247 3892 : gphi *new_phi = create_phi_node (phi_result, bb);
3248 3892 : add_phi_arg (new_phi, main_loop_value, loop_vinfo->main_loop_edge,
3249 : UNKNOWN_LOCATION);
3250 3892 : add_phi_arg (new_phi, skip_value,
3251 : loop_vinfo->skip_main_loop_edge, UNKNOWN_LOCATION);
3252 3892 : return phi_result;
3253 : }
3254 :
3255 : /* Function vect_do_peeling.
3256 :
3257 : Input:
3258 : - LOOP_VINFO: Represent a loop to be vectorized, which looks like:
3259 :
3260 : preheader:
3261 : LOOP:
3262 : header_bb:
3263 : loop_body
3264 : if (exit_loop_cond) goto exit_bb
3265 : else goto header_bb
3266 : exit_bb:
3267 :
3268 : - NITERS: The number of iterations of the loop.
3269 : - NITERSM1: The number of iterations of the loop's latch.
3270 : - NITERS_NO_OVERFLOW: No overflow in computing NITERS.
3271 : - TH, CHECK_PROFITABILITY: Threshold of niters to vectorize loop if
3272 : CHECK_PROFITABILITY is true.
3273 : Output:
3274 : - *NITERS_VECTOR and *STEP_VECTOR describe how the main loop should
3275 : iterate after vectorization; see vect_set_loop_condition for details.
3276 : - *NITERS_VECTOR_MULT_VF_VAR is either null or an SSA name that
3277 : should be set to the number of scalar iterations handled by the
3278 : vector loop. The SSA name is only used on exit from the loop.
3279 :
3280 : This function peels prolog and epilog from the loop, adds guards skipping
3281 : PROLOG and EPILOG for various conditions. As a result, the changed CFG
3282 : would look like:
3283 :
3284 : guard_bb_1:
3285 : if (prefer_scalar_loop) goto merge_bb_1
3286 : else goto guard_bb_2
3287 :
3288 : guard_bb_2:
3289 : if (skip_prolog) goto merge_bb_2
3290 : else goto prolog_preheader
3291 :
3292 : prolog_preheader:
3293 : PROLOG:
3294 : prolog_header_bb:
3295 : prolog_body
3296 : if (exit_prolog_cond) goto prolog_exit_bb
3297 : else goto prolog_header_bb
3298 : prolog_exit_bb:
3299 :
3300 : merge_bb_2:
3301 :
3302 : vector_preheader:
3303 : VECTOR LOOP:
3304 : vector_header_bb:
3305 : vector_body
3306 : if (exit_vector_cond) goto vector_exit_bb
3307 : else goto vector_header_bb
3308 : vector_exit_bb:
3309 :
3310 : guard_bb_3:
3311 : if (skip_epilog) goto merge_bb_3
3312 : else goto epilog_preheader
3313 :
3314 : merge_bb_1:
3315 :
3316 : epilog_preheader:
3317 : EPILOG:
3318 : epilog_header_bb:
3319 : epilog_body
3320 : if (exit_epilog_cond) goto merge_bb_3
3321 : else goto epilog_header_bb
3322 :
3323 : merge_bb_3:
3324 :
3325 : Note this function peels prolog and epilog only if it's necessary,
3326 : as well as guards.
3327 : This function returns the epilogue loop if a decision was made to vectorize
3328 : it, otherwise NULL.
3329 :
3330 : The analysis resulting in this epilogue loop's loop_vec_info was performed
3331 : in the same vect_analyze_loop call as the main loop's. At that time
3332 : vect_analyze_loop constructs a list of accepted loop_vec_info's for lower
3333 : vectorization factors than the main loop. This list is chained in the
3334 : loop's loop_vec_info in the 'epilogue_vinfo' member. When we decide to
3335 : vectorize the epilogue loop for a lower vectorization factor, the
3336 : loop_vec_info in epilogue_vinfo is updated and linked to the epilogue loop.
3337 : This is later used to vectorize the epilogue.
3338 : The reason the loop_vec_info needs updating is that it was
3339 : constructed based on the original main loop, and the epilogue loop is a
3340 : copy of this loop, so all links pointing to statements in the original loop
3341 : need updating. Furthermore, these loop_vec_infos share the
3342 : data_reference's records, which will also need to be updated.
3343 :
3344 : TODO: Guard for prefer_scalar_loop should be emitted along with
3345 : versioning conditions if loop versioning is needed. */
3346 :
3347 :
3348 : class loop *
3349 61979 : vect_do_peeling (loop_vec_info loop_vinfo, tree niters, tree nitersm1,
3350 : tree *niters_vector, tree *step_vector,
3351 : tree *niters_vector_mult_vf_var, int th,
3352 : bool check_profitability, bool niters_no_overflow,
3353 : tree *advance)
3354 : {
3355 61979 : edge e, guard_e;
3356 61979 : tree type = niters ? TREE_TYPE (niters) : sizetype;
3357 61979 : tree guard_cond;
3358 61979 : basic_block guard_bb, guard_to;
3359 61979 : profile_probability prob_prolog, prob_vector, prob_epilog;
3360 61979 : int estimated_vf;
3361 61979 : int prolog_peeling = 0;
3362 61979 : bool vect_epilogues = loop_vinfo->epilogue_vinfo != NULL;
3363 61979 : bool uncounted_p = LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo);
3364 :
3365 61979 : if (!vect_use_loop_mask_for_alignment_p (loop_vinfo))
3366 61978 : prolog_peeling = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
3367 :
3368 61979 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3369 61979 : poly_uint64 bound_epilog = 0;
3370 61979 : if (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo)
3371 61961 : && LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo))
3372 32623 : bound_epilog += vf - 1;
3373 61979 : if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo))
3374 388 : bound_epilog += 1;
3375 :
3376 : /* For early breaks the scalar loop needs to execute at most VF times
3377 : to find the element that caused the break. */
3378 61979 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
3379 61979 : && (LOOP_VINFO_EARLY_BRK_NEEDS_EPILOG (loop_vinfo)
3380 777 : || LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo)))
3381 : bound_epilog = vf;
3382 :
3383 61979 : bool epilog_peeling = maybe_ne (bound_epilog, 0U);
3384 61979 : poly_uint64 bound_scalar = bound_epilog;
3385 :
3386 61979 : if (!LOOP_VINFO_EARLY_BRK_NEEDS_EPILOG (loop_vinfo) && dump_enabled_p ())
3387 8160 : dump_printf_loc (MSG_NOTE, vect_location,
3388 : "early break does not require epilog.\n");
3389 :
3390 61979 : if (!prolog_peeling && !epilog_peeling)
3391 : return NULL;
3392 :
3393 : /* Before doing any peeling make sure to reset debug binds outside of
3394 : the loop referring to defs not in LC SSA. */
3395 33029 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3396 100376 : for (unsigned i = 0; i < loop->num_nodes; ++i)
3397 : {
3398 67347 : basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
3399 67347 : imm_use_iterator ui;
3400 67347 : gimple *use_stmt;
3401 160676 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
3402 93329 : gsi_next (&gsi))
3403 : {
3404 388712 : FOR_EACH_IMM_USE_STMT (use_stmt, ui, gimple_phi_result (gsi.phi ()))
3405 222235 : if (gimple_debug_bind_p (use_stmt)
3406 20181 : && loop != gimple_bb (use_stmt)->loop_father
3407 20197 : && !flow_loop_nested_p (loop,
3408 16 : gimple_bb (use_stmt)->loop_father))
3409 : {
3410 2 : gimple_debug_bind_reset_value (use_stmt);
3411 2 : update_stmt (use_stmt);
3412 93329 : }
3413 : }
3414 630473 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
3415 495779 : gsi_next (&gsi))
3416 : {
3417 495779 : ssa_op_iter op_iter;
3418 495779 : def_operand_p def_p;
3419 802426 : FOR_EACH_SSA_DEF_OPERAND (def_p, gsi_stmt (gsi), op_iter, SSA_OP_DEF)
3420 1074081 : FOR_EACH_IMM_USE_STMT (use_stmt, ui, DEF_FROM_PTR (def_p))
3421 497578 : if (gimple_debug_bind_p (use_stmt)
3422 36791 : && loop != gimple_bb (use_stmt)->loop_father
3423 36818 : && !flow_loop_nested_p (loop,
3424 27 : gimple_bb (use_stmt)->loop_father))
3425 : {
3426 0 : gimple_debug_bind_reset_value (use_stmt);
3427 0 : update_stmt (use_stmt);
3428 306647 : }
3429 : }
3430 : }
3431 :
3432 33029 : prob_vector = profile_probability::guessed_always ().apply_scale (9, 10);
3433 33029 : estimated_vf = vect_vf_for_cost (loop_vinfo);
3434 33029 : if (estimated_vf == 2)
3435 6874 : estimated_vf = 3;
3436 33029 : prob_prolog = prob_epilog = profile_probability::guessed_always ()
3437 33029 : .apply_scale (estimated_vf - 1, estimated_vf);
3438 :
3439 33029 : class loop *prolog = NULL, *epilog = NULL;
3440 33029 : class loop *first_loop = loop;
3441 33029 : bool irred_flag = loop_preheader_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP;
3442 :
3443 : /* SSA form needs to be up-to-date since we are going to manually
3444 : update SSA form in slpeel_tree_duplicate_loop_to_edge_cfg and delete all
3445 : update SSA state after that, so we have to make sure to not lose any
3446 : pending update needs. */
3447 33029 : gcc_assert (!need_ssa_update_p (cfun));
3448 :
3449 : /* If we're vectorizing an epilogue loop, we have ensured that the
3450 : virtual operand is in SSA form throughout the vectorized main loop.
3451 : Normally it is possible to trace the updated
3452 : vector-stmt vdefs back to scalar-stmt vdefs and vector-stmt vuses
3453 : back to scalar-stmt vuses, meaning that the effect of the SSA update
3454 : remains local to the main loop. However, there are rare cases in
3455 : which the vectorized loop should have vdefs even when the original scalar
3456 : loop didn't. For example, vectorizing a load with IFN_LOAD_LANES
3457 : introduces clobbers of the temporary vector array, which in turn
3458 : needs new vdefs. If the scalar loop doesn't write to memory, these
3459 : new vdefs will be the only ones in the vector loop.
3460 : We are currently deferring updating virtual SSA form and creating
3461 : of a virtual PHI for this case so we do not have to make sure the
3462 : newly introduced virtual def is in LCSSA form. */
3463 :
3464 33029 : if (MAY_HAVE_DEBUG_BIND_STMTS)
3465 : {
3466 12505 : gcc_assert (!adjust_vec.exists ());
3467 12505 : adjust_vec.create (32);
3468 : }
3469 33029 : initialize_original_copy_tables ();
3470 :
3471 : /* Record the anchor bb at which the guard should be placed if the scalar
3472 : loop might be preferred. */
3473 33029 : basic_block anchor = loop_preheader_edge (loop)->src;
3474 :
3475 : /* Generate the number of iterations for the prolog loop. We do this here
3476 : so that we can also get the upper bound on the number of iterations. */
3477 33029 : tree niters_prolog;
3478 33029 : poly_int64 bound_prolog = 0;
3479 33029 : if (prolog_peeling)
3480 : {
3481 433 : niters_prolog = vect_gen_prolog_loop_niters (loop_vinfo, anchor,
3482 : &bound_prolog);
3483 : /* If algonment peeling is known, we will always execute prolog. */
3484 433 : if (TREE_CODE (niters_prolog) == INTEGER_CST)
3485 228 : prob_prolog = profile_probability::always ();
3486 : }
3487 : else
3488 32596 : niters_prolog = build_int_cst (type, 0);
3489 :
3490 33029 : loop_vec_info epilogue_vinfo = loop_vinfo->epilogue_vinfo;
3491 33029 : tree niters_vector_mult_vf = NULL_TREE;
3492 : /* Saving NITERs before the loop, as this may be changed by prologue. */
3493 33029 : tree before_loop_niters = LOOP_VINFO_NITERS (loop_vinfo);
3494 33029 : edge update_e = NULL, skip_e = NULL;
3495 33029 : unsigned int lowest_vf = constant_lower_bound (vf);
3496 : /* Prolog loop may be skipped. */
3497 33029 : bool skip_prolog = (prolog_peeling != 0);
3498 : /* Skip this loop to epilog when there are not enough iterations to enter this
3499 : vectorized loop. If true we should perform runtime checks on the NITERS
3500 : to check whether we should skip the current vectorized loop. If we know
3501 : the number of scalar iterations we may choose to add a runtime check if
3502 : this number "maybe" smaller than the number of iterations required
3503 : when we know the number of scalar iterations may potentially
3504 : be smaller than the number of iterations required to enter this loop, for
3505 : this we use the upper bounds on the prolog and epilog peeling. When we
3506 : don't know the number of iterations and don't require versioning it is
3507 : because we have asserted that there are enough scalar iterations to enter
3508 : the main loop, so this skip is not necessary. When we are versioning then
3509 : we only add such a skip if we have chosen to vectorize the epilogue. */
3510 33029 : bool skip_vector = false;
3511 33029 : if (!uncounted_p)
3512 7958 : skip_vector = (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
3513 40944 : ? maybe_lt (LOOP_VINFO_INT_NITERS (loop_vinfo),
3514 7958 : bound_prolog + bound_epilog)
3515 25053 : : (!LOOP_VINFO_USE_VERSIONING_WITHOUT_PEELING (loop_vinfo)
3516 40 : || vect_epilogues));
3517 :
3518 : /* Epilog loop must be executed if the number of iterations for epilog
3519 : loop is known at compile time, otherwise we need to add a check at
3520 : the end of vector loop and skip to the end of epilog loop. */
3521 33029 : bool skip_epilog = (prolog_peeling < 0
3522 32824 : || !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
3523 33029 : || !vf.is_constant ());
3524 : /* PEELING_FOR_GAPS and peeling for early breaks are special because epilog
3525 : loop must be executed. */
3526 33029 : if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo)
3527 32641 : || LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
3528 1236 : skip_epilog = false;
3529 :
3530 33029 : class loop *scalar_loop = LOOP_VINFO_SCALAR_LOOP (loop_vinfo);
3531 33029 : auto_vec<profile_count> original_counts;
3532 33029 : basic_block *original_bbs = NULL;
3533 :
3534 33029 : if (skip_vector)
3535 : {
3536 25013 : split_edge (loop_preheader_edge (loop));
3537 :
3538 25013 : if (epilog_peeling && (vect_epilogues || scalar_loop == NULL))
3539 : {
3540 22788 : original_bbs = get_loop_body (loop);
3541 68780 : for (unsigned int i = 0; i < loop->num_nodes; i++)
3542 45992 : original_counts.safe_push(original_bbs[i]->count);
3543 : }
3544 :
3545 : /* Due to the order in which we peel prolog and epilog, we first
3546 : propagate probability to the whole loop. The purpose is to
3547 : avoid adjusting probabilities of both prolog and vector loops
3548 : separately. Note in this case, the probability of epilog loop
3549 : needs to be scaled back later. */
3550 25013 : basic_block bb_before_loop = loop_preheader_edge (loop)->src;
3551 25013 : if (prob_vector.initialized_p ())
3552 : {
3553 25013 : scale_bbs_frequencies (&bb_before_loop, 1, prob_vector);
3554 25013 : scale_loop_profile (loop, prob_vector, -1);
3555 : }
3556 : }
3557 :
3558 33029 : if (vect_epilogues)
3559 : {
3560 : /* Make sure to set the epilogue's epilogue scalar loop, such that we can
3561 : use the original scalar loop as remaining epilogue if necessary. */
3562 6855 : LOOP_VINFO_SCALAR_LOOP (epilogue_vinfo)
3563 6855 : = LOOP_VINFO_SCALAR_LOOP (loop_vinfo);
3564 6855 : LOOP_VINFO_SCALAR_MAIN_EXIT (epilogue_vinfo)
3565 6855 : = LOOP_VINFO_SCALAR_MAIN_EXIT (loop_vinfo);
3566 : }
3567 :
3568 33029 : if (prolog_peeling)
3569 : {
3570 433 : e = loop_preheader_edge (loop);
3571 433 : edge exit_e = LOOP_VINFO_MAIN_EXIT (loop_vinfo);
3572 433 : gcc_checking_assert (slpeel_can_duplicate_loop_p (loop, exit_e, e)
3573 : && (!LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo)
3574 : || uncounted_p));
3575 :
3576 : /* Peel prolog and put it on preheader edge of loop. */
3577 433 : edge scalar_e = LOOP_VINFO_SCALAR_MAIN_EXIT (loop_vinfo);
3578 433 : edge prolog_e = NULL;
3579 433 : prolog = slpeel_tree_duplicate_loop_to_edge_cfg (loop, exit_e,
3580 : scalar_loop, scalar_e,
3581 : e, &prolog_e, true, NULL,
3582 : uncounted_p, uncounted_p,
3583 : false);
3584 :
3585 433 : gcc_assert (prolog);
3586 433 : prolog->force_vectorize = false;
3587 :
3588 : /* Assign hierarchical discriminators to distinguish prolog loop. */
3589 433 : gimple *prolog_last = last_nondebug_stmt (prolog->header);
3590 433 : location_t prolog_loc
3591 433 : = prolog_last ? gimple_location (prolog_last) : UNKNOWN_LOCATION;
3592 433 : if (prolog_loc != UNKNOWN_LOCATION)
3593 : {
3594 431 : unsigned int prolog_copyid = allocate_copyid_base (prolog_loc, 1);
3595 431 : assign_discriminators_to_loop (prolog, 0, prolog_copyid);
3596 : }
3597 433 : first_loop = prolog;
3598 433 : reset_original_copy_tables ();
3599 :
3600 : /* Update the number of iterations for prolog loop. */
3601 433 : tree step_prolog = build_one_cst (TREE_TYPE (niters_prolog));
3602 433 : vect_set_loop_condition (prolog, prolog_e, NULL, niters_prolog,
3603 : step_prolog, NULL_TREE, false);
3604 :
3605 : /* Skip the prolog loop. */
3606 433 : if (skip_prolog)
3607 : {
3608 433 : guard_cond = fold_build2 (EQ_EXPR, boolean_type_node,
3609 : niters_prolog, build_int_cst (type, 0));
3610 433 : guard_bb = loop_preheader_edge (prolog)->src;
3611 433 : basic_block bb_after_prolog = loop_preheader_edge (loop)->src;
3612 433 : guard_to = split_edge (loop_preheader_edge (loop));
3613 433 : guard_e = slpeel_add_loop_guard (guard_bb, guard_cond,
3614 : guard_to, guard_bb,
3615 : prob_prolog.invert (),
3616 : irred_flag);
3617 1983 : for (edge alt_e : get_loop_exit_edges (prolog))
3618 : {
3619 684 : if (alt_e == prolog_e)
3620 433 : continue;
3621 251 : basic_block old_dom
3622 251 : = get_immediate_dominator (CDI_DOMINATORS, alt_e->dest);
3623 251 : if (flow_bb_inside_loop_p (prolog, old_dom))
3624 : {
3625 104 : auto_vec<basic_block, 8> queue;
3626 104 : for (auto son = first_dom_son (CDI_DOMINATORS, old_dom);
3627 340 : son; son = next_dom_son (CDI_DOMINATORS, son))
3628 236 : if (!flow_bb_inside_loop_p (prolog, son))
3629 132 : queue.safe_push (son);
3630 444 : for (auto son : queue)
3631 132 : set_immediate_dominator (CDI_DOMINATORS, son, guard_bb);
3632 104 : }
3633 433 : }
3634 :
3635 433 : e = EDGE_PRED (guard_to, 0);
3636 433 : e = (e != guard_e ? e : EDGE_PRED (guard_to, 1));
3637 433 : slpeel_update_phi_nodes_for_guard1 (prolog, loop, guard_e, e);
3638 :
3639 433 : scale_bbs_frequencies (&bb_after_prolog, 1, prob_prolog);
3640 433 : scale_loop_profile (prolog, prob_prolog,
3641 433 : estimated_poly_value (bound_prolog) - 1);
3642 : }
3643 :
3644 : /* Update init address of DRs. */
3645 433 : vect_update_inits_of_drs (loop_vinfo, niters_prolog, PLUS_EXPR);
3646 433 : if (!uncounted_p)
3647 : {
3648 : /* Update niters for vector loop. */
3649 402 : LOOP_VINFO_NITERS (loop_vinfo)
3650 402 : = fold_build2 (MINUS_EXPR, type, niters, niters_prolog);
3651 402 : LOOP_VINFO_NITERSM1 (loop_vinfo)
3652 402 : = fold_build2 (MINUS_EXPR, type,
3653 : LOOP_VINFO_NITERSM1 (loop_vinfo), niters_prolog);
3654 : }
3655 433 : bool new_var_p = false;
3656 433 : niters = vect_build_loop_niters (loop_vinfo, &new_var_p);
3657 : /* It's guaranteed that vector loop bound before vectorization is at
3658 : least VF, so set range information for newly generated var. */
3659 433 : if (new_var_p)
3660 : {
3661 215 : int_range<1> vr (type,
3662 430 : wi::to_wide (build_int_cst (type, lowest_vf)),
3663 430 : wi::to_wide (TYPE_MAX_VALUE (type)));
3664 215 : set_range_info (niters, vr);
3665 215 : }
3666 :
3667 : /* Prolog iterates at most bound_prolog times, latch iterates at
3668 : most bound_prolog - 1 times. */
3669 433 : if (bound_prolog.is_constant ())
3670 433 : record_niter_bound (prolog, bound_prolog.to_constant () - 1, false,
3671 : true);
3672 433 : delete_update_ssa ();
3673 433 : adjust_vec_debug_stmts ();
3674 433 : scev_reset ();
3675 : }
3676 33029 : basic_block bb_before_epilog = NULL;
3677 :
3678 33029 : if (epilog_peeling)
3679 : {
3680 32992 : e = LOOP_VINFO_MAIN_EXIT (loop_vinfo);
3681 32992 : gcc_checking_assert (slpeel_can_duplicate_loop_p (loop, e, e));
3682 :
3683 : /* Peel epilog and put it on exit edge of loop. If we are vectorizing
3684 : said epilog then we should use a copy of the main loop as a starting
3685 : point. This loop may have already had some preliminary transformations
3686 : to allow for more optimal vectorization, for example if-conversion.
3687 : If we are not vectorizing the epilog then we should use the scalar loop
3688 : as the transformations mentioned above make less or no sense when not
3689 : vectorizing. */
3690 32992 : edge scalar_e = LOOP_VINFO_SCALAR_MAIN_EXIT (loop_vinfo);
3691 32992 : epilog = vect_epilogues ? get_loop_copy (loop) : scalar_loop;
3692 6855 : edge epilog_e = vect_epilogues ? e : scalar_e;
3693 32992 : edge new_epilog_e = NULL;
3694 32992 : auto_vec<basic_block> doms;
3695 32992 : bool early_break_peel_p = LOOP_VINFO_EARLY_BRK_NEEDS_EPILOG (loop_vinfo);
3696 32992 : epilog
3697 32992 : = slpeel_tree_duplicate_loop_to_edge_cfg (loop, e, epilog, epilog_e, e,
3698 : &new_epilog_e, true, &doms,
3699 : uncounted_p, false,
3700 : early_break_peel_p);
3701 :
3702 32992 : LOOP_VINFO_EPILOGUE_MAIN_EXIT (loop_vinfo) = new_epilog_e;
3703 32992 : gcc_assert (epilog);
3704 32992 : gcc_assert (new_epilog_e);
3705 32992 : epilog->force_vectorize = false;
3706 32992 : bb_before_epilog = loop_preheader_edge (epilog)->src;
3707 :
3708 : /* Assign hierarchical discriminators to distinguish epilog loop.
3709 : Only assign if it's a scalar epilog. If it will be vectorized
3710 : (vect_epilogues), discriminators will be assigned.
3711 : Use dynamic copy_id allocation instead of hardcoded constants. */
3712 32992 : if (!vect_epilogues)
3713 : {
3714 26137 : gimple *epilog_last = last_nondebug_stmt (epilog->header);
3715 26137 : location_t epilog_loc
3716 26137 : = epilog_last ? gimple_location (epilog_last) : UNKNOWN_LOCATION;
3717 26108 : if (epilog_loc != UNKNOWN_LOCATION)
3718 : {
3719 21296 : unsigned int epilog_copyid = allocate_copyid_base (epilog_loc, 1);
3720 21296 : assign_discriminators_to_loop (epilog, 0, epilog_copyid);
3721 : }
3722 : }
3723 :
3724 : /* Scalar version loop may be preferred. In this case, add guard
3725 : and skip to epilog. Note this only happens when the number of
3726 : iterations of loop is unknown at compile time, otherwise this
3727 : won't be vectorized. */
3728 32992 : if (skip_vector)
3729 : {
3730 : /* Additional epilogue iteration is peeled if gap exists. */
3731 25013 : tree t = vect_gen_scalar_loop_niters (niters_prolog, prolog_peeling,
3732 25013 : bound_prolog, bound_epilog,
3733 : th, &bound_scalar,
3734 : check_profitability);
3735 : /* Build guard against NITERSM1 since NITERS may overflow. */
3736 25013 : guard_cond = fold_build2 (LT_EXPR, boolean_type_node, nitersm1, t);
3737 25013 : guard_bb = anchor;
3738 25013 : guard_to = split_edge (loop_preheader_edge (epilog));
3739 25013 : guard_e = slpeel_add_loop_guard (guard_bb, guard_cond,
3740 : guard_to, guard_bb,
3741 : prob_vector.invert (),
3742 : irred_flag);
3743 25013 : skip_e = guard_e;
3744 25013 : e = EDGE_PRED (guard_to, 0);
3745 25013 : e = (e != guard_e ? e : EDGE_PRED (guard_to, 1));
3746 :
3747 : /* Handle any remaining dominator updates needed after
3748 : inserting the loop skip edge above. */
3749 25013 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
3750 279 : && prolog_peeling)
3751 : {
3752 : /* Adding a skip edge to skip a loop with multiple exits
3753 : means the dominator of the join blocks for all exits shifts
3754 : from the prolog skip guard to the loop skip guard. */
3755 156 : auto prolog_skip_bb
3756 156 : = single_pred (loop_preheader_edge (prolog)->src);
3757 156 : auto needs_update
3758 156 : = get_dominated_by (CDI_DOMINATORS, prolog_skip_bb);
3759 :
3760 : /* Update everything except for the immediate children of
3761 : the prolog skip block (the prolog and vector preheaders).
3762 : Those should remain dominated by the prolog skip block itself,
3763 : since the loop guard edge goes to the epilogue. */
3764 840 : for (auto bb : needs_update)
3765 372 : if (bb != EDGE_SUCC (prolog_skip_bb, 0)->dest
3766 372 : && bb != EDGE_SUCC (prolog_skip_bb, 1)->dest)
3767 60 : set_immediate_dominator (CDI_DOMINATORS, bb, guard_bb);
3768 156 : }
3769 :
3770 25013 : slpeel_update_phi_nodes_for_guard1 (first_loop, epilog, guard_e, e);
3771 :
3772 : /* Simply propagate profile info from guard_bb to guard_to which is
3773 : a merge point of control flow. */
3774 25013 : profile_count old_count = guard_to->count;
3775 25013 : guard_to->count = guard_bb->count;
3776 :
3777 : /* Restore the counts of the epilog loop if we didn't use the scalar loop. */
3778 25013 : if (vect_epilogues || scalar_loop == NULL)
3779 : {
3780 22788 : gcc_assert(epilog->num_nodes == loop->num_nodes);
3781 22788 : basic_block *bbs = get_loop_body (epilog);
3782 68780 : for (unsigned int i = 0; i < epilog->num_nodes; i++)
3783 : {
3784 45992 : gcc_assert(get_bb_original (bbs[i]) == original_bbs[i]);
3785 45992 : bbs[i]->count = original_counts[i];
3786 : }
3787 22788 : free (bbs);
3788 22788 : free (original_bbs);
3789 : }
3790 2225 : else if (old_count.nonzero_p ())
3791 2225 : scale_loop_profile (epilog, guard_to->count.probability_in (old_count), -1);
3792 :
3793 : /* Only need to handle basic block before epilog loop if it's not
3794 : the guard_bb, which is the case when skip_vector is true. */
3795 25013 : if (guard_bb != bb_before_epilog && single_pred_p (bb_before_epilog))
3796 24874 : bb_before_epilog->count = single_pred_edge (bb_before_epilog)->count ();
3797 25013 : bb_before_epilog = loop_preheader_edge (epilog)->src;
3798 : }
3799 :
3800 32992 : if (!uncounted_p)
3801 : {
3802 : /* If loop is peeled for non-zero constant times, now niters refers to
3803 : orig_niters - prolog_peeling, it won't overflow even the
3804 : orig_niters overflows. */
3805 32949 : niters_no_overflow |= (prolog_peeling > 0);
3806 32949 : vect_gen_vector_loop_niters (loop_vinfo, niters,
3807 : niters_vector, step_vector,
3808 : niters_no_overflow);
3809 32949 : if (!integer_onep (*step_vector))
3810 : {
3811 : /* On exit from the loop we will have an easy way of calculating
3812 : NITERS_VECTOR / STEP * STEP. Install a dummy definition
3813 : until then. */
3814 0 : niters_vector_mult_vf
3815 0 : = make_ssa_name (TREE_TYPE (*niters_vector));
3816 0 : edge exit_e = LOOP_VINFO_MAIN_EXIT (loop_vinfo);
3817 0 : gimple_stmt_iterator loop_cond_gsi
3818 0 : = gsi_after_labels (exit_e->dest);
3819 :
3820 0 : gcall *tmp = gimple_build_call_internal (IFN_VARYING, 0);
3821 0 : gimple_call_set_lhs (tmp, niters_vector_mult_vf);
3822 0 : gsi_insert_before (&loop_cond_gsi, tmp, GSI_SAME_STMT);
3823 0 : *niters_vector_mult_vf_var = niters_vector_mult_vf;
3824 : }
3825 : else
3826 32949 : vect_gen_vector_loop_niters_mult_vf (loop_vinfo, *niters_vector,
3827 : &niters_vector_mult_vf);
3828 : /* Update IVs of original loop as if they were advanced by
3829 : niters_vector_mult_vf steps. */
3830 32949 : gcc_checking_assert (vect_can_advance_ivs_p (loop_vinfo));
3831 32949 : update_e = skip_vector ? e : loop_preheader_edge (epilog);
3832 : }
3833 32992 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
3834 842 : update_e = single_succ_edge (LOOP_VINFO_MAIN_EXIT (loop_vinfo)->dest);
3835 :
3836 : /* If we have a peeled vector iteration we will never skip the epilog loop
3837 : and we can simplify the cfg a lot by not doing the edge split. */
3838 32992 : if (skip_epilog
3839 32992 : || (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
3840 842 : && !LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo)))
3841 : {
3842 25331 : guard_cond = fold_build2 (EQ_EXPR, boolean_type_node,
3843 : niters, niters_vector_mult_vf);
3844 :
3845 25331 : guard_bb = LOOP_VINFO_MAIN_EXIT (loop_vinfo)->dest;
3846 25331 : edge epilog_e = LOOP_VINFO_EPILOGUE_MAIN_EXIT (loop_vinfo);
3847 25331 : guard_to = epilog_e->dest;
3848 25845 : guard_e = slpeel_add_loop_guard (guard_bb, guard_cond, guard_to,
3849 : skip_vector ? anchor : guard_bb,
3850 : prob_epilog.invert (),
3851 : irred_flag);
3852 :
3853 25331 : doms.safe_push (guard_to);
3854 25331 : if (vect_epilogues)
3855 5514 : epilogue_vinfo->skip_this_loop_edge = guard_e;
3856 25331 : edge main_iv = LOOP_VINFO_MAIN_EXIT (loop_vinfo);
3857 25331 : gphi_iterator gsi2 = gsi_start_phis (main_iv->dest);
3858 25331 : for (gphi_iterator gsi = gsi_start_phis (guard_to);
3859 61914 : !gsi_end_p (gsi); gsi_next (&gsi))
3860 : {
3861 : /* We are expecting all of the PHIs we have on epilog_e
3862 : to be also on the main loop exit. But sometimes
3863 : a stray virtual definition can appear at epilog_e
3864 : which we can then take as the same on all exits,
3865 : we've removed the LC SSA PHI on the main exit before
3866 : so we wouldn't need to create a loop PHI for it. */
3867 36583 : if (virtual_operand_p (gimple_phi_result (*gsi))
3868 36583 : && (gsi_end_p (gsi2)
3869 36702 : || !virtual_operand_p (gimple_phi_result (*gsi2))))
3870 172 : add_phi_arg (*gsi,
3871 86 : gimple_phi_arg_def_from_edge (*gsi, epilog_e),
3872 : guard_e, UNKNOWN_LOCATION);
3873 : else
3874 : {
3875 36497 : add_phi_arg (*gsi, gimple_phi_result (*gsi2), guard_e,
3876 : UNKNOWN_LOCATION);
3877 36497 : gsi_next (&gsi2);
3878 : }
3879 : }
3880 :
3881 : /* Only need to handle basic block before epilog loop if it's not
3882 : the guard_bb, which is the case when skip_vector is true. */
3883 25331 : if (guard_bb != bb_before_epilog)
3884 : {
3885 25285 : prob_epilog = prob_vector * prob_epilog + prob_vector.invert ();
3886 :
3887 25285 : scale_bbs_frequencies (&bb_before_epilog, 1, prob_epilog);
3888 : }
3889 25331 : scale_loop_profile (epilog, prob_epilog, -1);
3890 : }
3891 :
3892 : /* If we have a peeled vector iteration, all exits are the same, leave it
3893 : and so the main exit needs to be treated the same as the alternative
3894 : exits in that we leave their updates to vectorizable_live_operations.
3895 : */
3896 32992 : tree vector_iters_vf = niters_vector_mult_vf;
3897 32992 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
3898 : {
3899 842 : tree tmp_niters_vf
3900 842 : = make_ssa_name (LOOP_VINFO_EARLY_BRK_IV_TYPE (loop_vinfo));
3901 842 : gcall *tmp_call = gimple_build_call_internal (IFN_VARYING, 0);
3902 842 : gimple_call_set_lhs (tmp_call, tmp_niters_vf);
3903 842 : auto header_gsi = gsi_after_labels (loop->header);
3904 842 : gsi_insert_after (&header_gsi, tmp_call, GSI_SAME_STMT);
3905 :
3906 43 : if (!(LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo)
3907 928 : && get_loop_exit_edges (loop).length () == 1)
3908 856 : && LOOP_VINFO_EARLY_BRK_NEEDS_EPILOG (loop_vinfo))
3909 : {
3910 630 : basic_block exit_bb = NULL;
3911 630 : edge update_e = NULL;
3912 :
3913 : /* Identify the early exit merge block. I wish we had stored
3914 : this. */
3915 1890 : for (auto e : get_loop_exit_edges (loop))
3916 630 : if (e != LOOP_VINFO_MAIN_EXIT (loop_vinfo))
3917 : {
3918 630 : exit_bb = e->dest;
3919 630 : update_e = single_succ_edge (exit_bb);
3920 630 : break;
3921 630 : }
3922 630 : vect_update_ivs_after_vectorizer (loop_vinfo, tmp_niters_vf,
3923 : update_e, true);
3924 : }
3925 842 : if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
3926 : vector_iters_vf = tmp_niters_vf;
3927 :
3928 842 : LOOP_VINFO_EARLY_BRK_NITERS_VAR (loop_vinfo) = tmp_niters_vf;
3929 : }
3930 :
3931 32992 : bool recalculate_peel_niters_init
3932 32992 : = LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo);
3933 32992 : vect_update_ivs_after_vectorizer (loop_vinfo, vector_iters_vf,
3934 : update_e,
3935 : recalculate_peel_niters_init);
3936 :
3937 : /* Recalculate the dominators after adding the guard edge. */
3938 32992 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
3939 842 : iterate_fix_dominators (CDI_DOMINATORS, doms, false);
3940 :
3941 : /* When we do not have a loop-around edge to the epilog we know
3942 : the vector loop covered at least VF scalar iterations unless
3943 : we have early breaks.
3944 : Update any known upper bound with this knowledge. */
3945 32992 : if (! skip_vector
3946 7979 : && ! LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
3947 : {
3948 7416 : if (epilog->any_upper_bound)
3949 7416 : epilog->nb_iterations_upper_bound -= lowest_vf;
3950 7416 : if (epilog->any_likely_upper_bound)
3951 7416 : epilog->nb_iterations_likely_upper_bound -= lowest_vf;
3952 7416 : if (epilog->any_estimate)
3953 7414 : epilog->nb_iterations_estimate -= lowest_vf;
3954 : }
3955 :
3956 32992 : unsigned HOST_WIDE_INT bound;
3957 32992 : if (bound_scalar.is_constant (&bound))
3958 : {
3959 32992 : gcc_assert (bound != 0);
3960 : /* Adjust the upper bound by the extra peeled vector iteration if we
3961 : are an epilogue of an peeled vect loop and not VLA. For VLA the
3962 : loop bounds are unknown. */
3963 65965 : if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo)
3964 32992 : && vf.is_constant ())
3965 62 : bound += vf.to_constant ();
3966 : /* -1 to convert loop iterations to latch iterations. */
3967 32992 : record_niter_bound (epilog, bound - 1, false, true);
3968 32992 : scale_loop_profile (epilog, profile_probability::always (),
3969 : bound - 1);
3970 : }
3971 :
3972 32992 : delete_update_ssa ();
3973 32992 : adjust_vec_debug_stmts ();
3974 32992 : scev_reset ();
3975 32992 : }
3976 :
3977 33029 : if (vect_epilogues)
3978 : {
3979 6855 : epilog->aux = epilogue_vinfo;
3980 6855 : LOOP_VINFO_LOOP (epilogue_vinfo) = epilog;
3981 6855 : LOOP_VINFO_MAIN_EXIT (epilogue_vinfo)
3982 6855 : = LOOP_VINFO_EPILOGUE_MAIN_EXIT (loop_vinfo);
3983 :
3984 6855 : loop_constraint_clear (epilog, LOOP_C_INFINITE);
3985 :
3986 : /* We now must calculate the number of NITERS performed by the previous
3987 : loop and EPILOGUE_NITERS to be performed by the epilogue. */
3988 6855 : tree niters = fold_build2 (PLUS_EXPR, TREE_TYPE (niters_vector_mult_vf),
3989 : niters_prolog, niters_vector_mult_vf);
3990 :
3991 : /* If skip_vector we may skip the previous loop, we insert a phi-node to
3992 : determine whether we are coming from the previous vectorized loop
3993 : using the update_e edge or the skip_vector basic block using the
3994 : skip_e edge. */
3995 6855 : if (skip_vector)
3996 : {
3997 5584 : gcc_assert (update_e != NULL && skip_e != NULL);
3998 5584 : gphi *new_phi = create_phi_node (make_ssa_name (TREE_TYPE (niters)),
3999 : update_e->dest);
4000 5584 : tree new_ssa = make_ssa_name (TREE_TYPE (niters));
4001 5584 : gimple *stmt = gimple_build_assign (new_ssa, niters);
4002 5584 : gimple_stmt_iterator gsi;
4003 5584 : if (TREE_CODE (niters_vector_mult_vf) == SSA_NAME
4004 5584 : && SSA_NAME_DEF_STMT (niters_vector_mult_vf)->bb != NULL)
4005 : {
4006 5584 : gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (niters_vector_mult_vf));
4007 5584 : gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
4008 : }
4009 : else
4010 : {
4011 0 : gsi = gsi_last_bb (update_e->src);
4012 0 : gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
4013 : }
4014 :
4015 5584 : niters = new_ssa;
4016 5584 : add_phi_arg (new_phi, niters, update_e, UNKNOWN_LOCATION);
4017 5584 : add_phi_arg (new_phi, build_zero_cst (TREE_TYPE (niters)), skip_e,
4018 : UNKNOWN_LOCATION);
4019 5584 : niters = PHI_RESULT (new_phi);
4020 5584 : epilogue_vinfo->main_loop_edge = update_e;
4021 5584 : epilogue_vinfo->skip_main_loop_edge = skip_e;
4022 : }
4023 :
4024 : /* Set ADVANCE to the number of iterations performed by the previous
4025 : loop and its prologue. */
4026 6855 : *advance = niters;
4027 :
4028 : /* Subtract the number of iterations performed by the vectorized loop
4029 : from the number of total iterations. */
4030 6855 : tree epilogue_niters = fold_build2 (MINUS_EXPR, TREE_TYPE (niters),
4031 : before_loop_niters,
4032 : niters);
4033 :
4034 6855 : LOOP_VINFO_NITERS (epilogue_vinfo) = epilogue_niters;
4035 6855 : LOOP_VINFO_NITERSM1 (epilogue_vinfo)
4036 6855 : = fold_build2 (MINUS_EXPR, TREE_TYPE (epilogue_niters),
4037 : epilogue_niters,
4038 : build_one_cst (TREE_TYPE (epilogue_niters)));
4039 : }
4040 :
4041 33029 : adjust_vec.release ();
4042 33029 : free_original_copy_tables ();
4043 :
4044 33029 : return vect_epilogues ? epilog : NULL;
4045 33029 : }
4046 :
4047 : /* Function vect_create_cond_for_niters_checks.
4048 :
4049 : Create a conditional expression that represents the run-time checks for
4050 : loop's niter. The loop is guaranteed to terminate if the run-time
4051 : checks hold.
4052 :
4053 : Input:
4054 : COND_EXPR - input conditional expression. New conditions will be chained
4055 : with logical AND operation. If it is NULL, then the function
4056 : is used to return the number of alias checks.
4057 : LOOP_VINFO - field LOOP_VINFO_MAY_ALIAS_STMTS contains the list of ddrs
4058 : to be checked.
4059 :
4060 : Output:
4061 : COND_EXPR - conditional expression.
4062 :
4063 : The returned COND_EXPR is the conditional expression to be used in the
4064 : if statement that controls which version of the loop gets executed at
4065 : runtime. */
4066 :
4067 : static void
4068 373 : vect_create_cond_for_niters_checks (loop_vec_info loop_vinfo, tree *cond_expr)
4069 : {
4070 373 : tree part_cond_expr = LOOP_VINFO_NITERS_ASSUMPTIONS (loop_vinfo);
4071 :
4072 373 : if (*cond_expr)
4073 373 : *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
4074 : *cond_expr, part_cond_expr);
4075 : else
4076 0 : *cond_expr = part_cond_expr;
4077 373 : }
4078 :
4079 : /* Set *COND_EXPR to a tree that is true when both the original *COND_EXPR
4080 : and PART_COND_EXPR are true. Treat a null *COND_EXPR as "true". */
4081 :
4082 : static void
4083 287 : chain_cond_expr (tree *cond_expr, tree part_cond_expr)
4084 : {
4085 287 : if (*cond_expr)
4086 283 : *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
4087 : *cond_expr, part_cond_expr);
4088 : else
4089 4 : *cond_expr = part_cond_expr;
4090 287 : }
4091 :
4092 : /* Function vect_create_cond_for_align_checks.
4093 :
4094 : Create a conditional expression that represents the alignment checks for
4095 : all of data references (array element references) whose alignment must be
4096 : checked at runtime.
4097 :
4098 : Input:
4099 : COND_EXPR - input conditional expression. New conditions will be chained
4100 : with logical AND operation.
4101 : LOOP_VINFO - three fields of the loop information are used.
4102 : LOOP_VINFO_PTR_MASK is the mask used to check the alignment.
4103 : LOOP_VINFO_MAY_MISALIGN_STMTS contains the refs to be checked.
4104 : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT indicates which check applies.
4105 :
4106 : Output:
4107 : COND_EXPR_STMT_LIST - statements needed to construct the conditional
4108 : expression.
4109 : The returned value is the conditional expression to be used in the if
4110 : statement that controls which version of the loop gets executed at runtime.
4111 :
4112 : Based on the boolean value of LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT, we decide
4113 : which type of check should be applied and create two different expressions
4114 : accordingly.
4115 : 1) When LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT is false, we see if all data refs
4116 : to be checked are already aligned to an alignment boundary. We create
4117 : an expression of "(a_1 | a_2 | a_3 | ... | a_n) & mask", where "a_i" is
4118 : the address of i'th data reference.
4119 : 2) When LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT is true, we see if all data refs
4120 : can be aligned to a boundary after a certain amount of peeling, in other
4121 : words, their addresses have the same bottom bits according to the mask.
4122 : We create "((a_1 ^ a_2) | (a_2 ^ a_3) | ... | (a_n-1 ^ a_n)) & mask",
4123 : where "a_i" is the address of i'th data reference.
4124 :
4125 : Both algorithms make two assumptions:
4126 : 1) The number of bytes "n" in a vector is a power of 2.
4127 : 2) An address "a" is aligned if a%n is zero and that this
4128 : test can be done as a&(n-1) == 0. For example, for 16
4129 : byte vectors the test is a&0xf == 0. */
4130 :
4131 : static void
4132 45 : vect_create_cond_for_align_checks (loop_vec_info loop_vinfo,
4133 : tree *cond_expr,
4134 : gimple_seq *cond_expr_stmt_list)
4135 : {
4136 45 : const vec<stmt_vec_info> &may_misalign_stmts
4137 : = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
4138 45 : stmt_vec_info stmt_info;
4139 45 : poly_uint64 mask = LOOP_VINFO_PTR_MASK (loop_vinfo);
4140 45 : tree mask_cst;
4141 45 : unsigned int i;
4142 45 : tree int_ptrsize_type;
4143 45 : char tmp_name[30];
4144 45 : tree or_tmp_name = NULL_TREE;
4145 45 : tree prev_addr_tmp_name = NULL_TREE;
4146 45 : tree and_tmp_name;
4147 45 : gimple *and_stmt;
4148 45 : tree ptrsize_zero;
4149 45 : tree part_cond_expr;
4150 :
4151 45 : gcc_assert (known_ne (mask, 0U));
4152 :
4153 45 : int_ptrsize_type = signed_type_for (ptr_type_node);
4154 :
4155 : /* If LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT is true, we should have at least two
4156 : datarefs to check the mutual alignment. */
4157 45 : gcc_assert (may_misalign_stmts.length () > 1
4158 : || !LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT (loop_vinfo));
4159 :
4160 109 : FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt_info)
4161 : {
4162 64 : gimple_seq new_stmt_list = NULL;
4163 64 : tree addr_base;
4164 64 : tree addr_tmp_name;
4165 64 : tree xor_tmp_name;
4166 64 : tree new_or_tmp_name;
4167 64 : gimple *addr_stmt, *or_stmt, *xor_stmt;
4168 64 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4169 64 : bool negative = tree_int_cst_compare
4170 64 : (DR_STEP (STMT_VINFO_DATA_REF (stmt_info)), size_zero_node) < 0;
4171 64 : tree offset = negative
4172 64 : ? size_int ((-TYPE_VECTOR_SUBPARTS (vectype) + 1)
4173 : * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))))
4174 64 : : size_zero_node;
4175 :
4176 : /* create: addr_tmp = (int)(address_of_first_vector) */
4177 64 : addr_base =
4178 64 : vect_create_addr_base_for_vector_ref (loop_vinfo,
4179 : stmt_info, &new_stmt_list,
4180 : offset);
4181 64 : if (new_stmt_list != NULL)
4182 14 : gimple_seq_add_seq (cond_expr_stmt_list, new_stmt_list);
4183 :
4184 64 : sprintf (tmp_name, "addr2int%d", i);
4185 64 : addr_tmp_name = make_temp_ssa_name (int_ptrsize_type, NULL, tmp_name);
4186 64 : addr_stmt = gimple_build_assign (addr_tmp_name, NOP_EXPR, addr_base);
4187 64 : gimple_seq_add_stmt (cond_expr_stmt_list, addr_stmt);
4188 :
4189 64 : if (LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT (loop_vinfo))
4190 : {
4191 : /* Create "((a_1 ^ a_2) | (a_2 ^ a_3) | ... | (a_n-1 ^ a_n)) & mask"
4192 : to check mutual alignment. */
4193 36 : if (prev_addr_tmp_name != NULL_TREE)
4194 : {
4195 18 : sprintf (tmp_name, "xorptrs%d_%d", i - 1, i);
4196 18 : xor_tmp_name = make_temp_ssa_name (int_ptrsize_type, NULL,
4197 : tmp_name);
4198 18 : xor_stmt = gimple_build_assign (xor_tmp_name, BIT_XOR_EXPR,
4199 : prev_addr_tmp_name,
4200 : addr_tmp_name);
4201 18 : gimple_seq_add_stmt (cond_expr_stmt_list, xor_stmt);
4202 18 : if (or_tmp_name == NULL_TREE)
4203 : {
4204 : /* Create the 1st XOR when the 2nd data ref is seen. */
4205 : or_tmp_name = xor_tmp_name;
4206 : }
4207 : else
4208 : {
4209 : /* Create: or_tmp = or_tmp | new_xor_tmp. */
4210 0 : sprintf (tmp_name, "orxors%d", i - 1);
4211 0 : new_or_tmp_name = make_temp_ssa_name (int_ptrsize_type, NULL,
4212 : tmp_name);
4213 0 : or_stmt = gimple_build_assign (new_or_tmp_name, BIT_IOR_EXPR,
4214 : or_tmp_name, xor_tmp_name);
4215 0 : gimple_seq_add_stmt (cond_expr_stmt_list, or_stmt);
4216 0 : or_tmp_name = new_or_tmp_name;
4217 : }
4218 : }
4219 : prev_addr_tmp_name = addr_tmp_name;
4220 : }
4221 : else
4222 : {
4223 : /* Create: "(a_1 | a_2 | a_3 | ... | a_n) & mask" to check if all
4224 : addresses are already aligned. */
4225 28 : if (or_tmp_name != NULL_TREE)
4226 : {
4227 : /* Create: or_tmp = or_tmp | addr_tmp. */
4228 1 : sprintf (tmp_name, "orptrs%d", i);
4229 1 : new_or_tmp_name = make_temp_ssa_name (int_ptrsize_type, NULL,
4230 : tmp_name);
4231 1 : or_stmt = gimple_build_assign (new_or_tmp_name, BIT_IOR_EXPR,
4232 : or_tmp_name, addr_tmp_name);
4233 1 : gimple_seq_add_stmt (cond_expr_stmt_list, or_stmt);
4234 1 : or_tmp_name = new_or_tmp_name;
4235 : }
4236 : else
4237 : or_tmp_name = addr_tmp_name;
4238 : }
4239 :
4240 : } /* end for i */
4241 :
4242 45 : mask_cst = build_int_cst (int_ptrsize_type, mask);
4243 :
4244 : /* create: and_tmp = or_tmp & mask */
4245 45 : and_tmp_name = make_temp_ssa_name (int_ptrsize_type, NULL, "andmask");
4246 :
4247 45 : and_stmt = gimple_build_assign (and_tmp_name, BIT_AND_EXPR,
4248 : or_tmp_name, mask_cst);
4249 45 : gimple_seq_add_stmt (cond_expr_stmt_list, and_stmt);
4250 :
4251 : /* Make and_tmp the left operand of the conditional test against zero.
4252 : if and_tmp has a nonzero bit then some address is unaligned. */
4253 45 : ptrsize_zero = build_int_cst (int_ptrsize_type, 0);
4254 45 : part_cond_expr = fold_build2 (EQ_EXPR, boolean_type_node,
4255 : and_tmp_name, ptrsize_zero);
4256 45 : chain_cond_expr (cond_expr, part_cond_expr);
4257 45 : }
4258 :
4259 : /* Function vect_create_cond_for_vla_spec_read.
4260 :
4261 : Create a conditional expression that represents the run-time checks with
4262 : max speculative read amount in VLA modes. We check two things:
4263 : 1) if the max speculative read amount exceeds the min page size
4264 : 2) if the VF is power-of-2 - done by checking the max read amount instead
4265 :
4266 : Input:
4267 : COND_EXPR - input conditional expression. New conditions will be chained
4268 : with logical AND operation.
4269 : LOOP_VINFO - field LOOP_VINFO_MAX_SPEC_READ_AMOUNT contains the max
4270 : possible speculative read amount in VLA modes.
4271 :
4272 : Output:
4273 : COND_EXPR - conditional expression.
4274 :
4275 : The returned COND_EXPR is the conditional expression to be used in the
4276 : if statement that controls which version of the loop gets executed at
4277 : runtime. */
4278 :
4279 : static void
4280 0 : vect_create_cond_for_vla_spec_read (loop_vec_info loop_vinfo, tree *cond_expr)
4281 : {
4282 0 : poly_uint64 read_amount_poly = LOOP_VINFO_MAX_SPEC_READ_AMOUNT (loop_vinfo);
4283 0 : tree amount = build_int_cst (long_unsigned_type_node, read_amount_poly);
4284 :
4285 : /* Both the read amount and the VF must be variants, and the read amount must
4286 : be a constant power-of-2 multiple of the VF. */
4287 0 : unsigned HOST_WIDE_INT multiple;
4288 0 : gcc_assert (!read_amount_poly.is_constant ()
4289 : && !LOOP_VINFO_VECT_FACTOR (loop_vinfo).is_constant ()
4290 : && constant_multiple_p (read_amount_poly,
4291 : LOOP_VINFO_VECT_FACTOR (loop_vinfo),
4292 : &multiple)
4293 : && pow2p_hwi (multiple));
4294 :
4295 : tree cst_ul_zero = build_int_cstu (long_unsigned_type_node, 0U);
4296 : tree cst_ul_one = build_int_cstu (long_unsigned_type_node, 1U);
4297 : tree cst_ul_pagesize = build_int_cstu (long_unsigned_type_node,
4298 : (unsigned long) param_min_pagesize);
4299 :
4300 : /* Create an expression of "amount & (amount - 1) == 0". */
4301 : tree amount_m1 = fold_build2 (MINUS_EXPR, long_unsigned_type_node,
4302 : amount, cst_ul_one);
4303 : tree amount_and_expr = fold_build2 (BIT_AND_EXPR, long_unsigned_type_node,
4304 : amount, amount_m1);
4305 : tree powof2_cond_expr = fold_build2 (EQ_EXPR, boolean_type_node,
4306 : amount_and_expr, cst_ul_zero);
4307 : chain_cond_expr (cond_expr, powof2_cond_expr);
4308 :
4309 : /* Create an expression of "amount <= cst_ul_pagesize". */
4310 : tree pagesize_cond_expr = fold_build2 (LE_EXPR, boolean_type_node,
4311 : amount, cst_ul_pagesize);
4312 : chain_cond_expr (cond_expr, pagesize_cond_expr);
4313 : }
4314 :
4315 : /* If LOOP_VINFO_CHECK_UNEQUAL_ADDRS contains <A1, B1>, ..., <An, Bn>,
4316 : create a tree representation of: (&A1 != &B1) && ... && (&An != &Bn).
4317 : Set *COND_EXPR to a tree that is true when both the original *COND_EXPR
4318 : and this new condition are true. Treat a null *COND_EXPR as "true". */
4319 :
4320 : static void
4321 3311 : vect_create_cond_for_unequal_addrs (loop_vec_info loop_vinfo, tree *cond_expr)
4322 : {
4323 3311 : const vec<vec_object_pair> &pairs
4324 : = LOOP_VINFO_CHECK_UNEQUAL_ADDRS (loop_vinfo);
4325 3311 : unsigned int i;
4326 3311 : vec_object_pair *pair;
4327 3323 : FOR_EACH_VEC_ELT (pairs, i, pair)
4328 : {
4329 12 : tree addr1 = build_fold_addr_expr (pair->first);
4330 12 : tree addr2 = build_fold_addr_expr (pair->second);
4331 12 : tree part_cond_expr = fold_build2 (NE_EXPR, boolean_type_node,
4332 : addr1, addr2);
4333 12 : chain_cond_expr (cond_expr, part_cond_expr);
4334 : }
4335 3311 : }
4336 :
4337 : /* Create an expression that is true when all lower-bound conditions for
4338 : the vectorized loop are met. Chain this condition with *COND_EXPR. */
4339 :
4340 : static void
4341 3311 : vect_create_cond_for_lower_bounds (loop_vec_info loop_vinfo, tree *cond_expr)
4342 : {
4343 3311 : const vec<vec_lower_bound> &lower_bounds
4344 : = LOOP_VINFO_LOWER_BOUNDS (loop_vinfo);
4345 3541 : for (unsigned int i = 0; i < lower_bounds.length (); ++i)
4346 : {
4347 230 : tree expr = lower_bounds[i].expr;
4348 230 : tree type = unsigned_type_for (TREE_TYPE (expr));
4349 230 : expr = fold_convert (type, expr);
4350 230 : poly_uint64 bound = lower_bounds[i].min_value;
4351 230 : if (!lower_bounds[i].unsigned_p)
4352 : {
4353 72 : expr = fold_build2 (PLUS_EXPR, type, expr,
4354 : build_int_cstu (type, bound - 1));
4355 72 : bound += bound - 1;
4356 : }
4357 230 : tree part_cond_expr = fold_build2 (GE_EXPR, boolean_type_node, expr,
4358 : build_int_cstu (type, bound));
4359 230 : chain_cond_expr (cond_expr, part_cond_expr);
4360 : }
4361 3311 : }
4362 :
4363 : /* Function vect_create_cond_for_alias_checks.
4364 :
4365 : Create a conditional expression that represents the run-time checks for
4366 : overlapping of address ranges represented by a list of data references
4367 : relations passed as input.
4368 :
4369 : Input:
4370 : COND_EXPR - input conditional expression. New conditions will be chained
4371 : with logical AND operation. If it is NULL, then the function
4372 : is used to return the number of alias checks.
4373 : LOOP_VINFO - field LOOP_VINFO_MAY_ALIAS_STMTS contains the list of ddrs
4374 : to be checked.
4375 :
4376 : Output:
4377 : COND_EXPR - conditional expression.
4378 :
4379 : The returned COND_EXPR is the conditional expression to be used in the if
4380 : statement that controls which version of the loop gets executed at runtime.
4381 : */
4382 :
4383 : void
4384 3311 : vect_create_cond_for_alias_checks (loop_vec_info loop_vinfo, tree * cond_expr)
4385 : {
4386 3311 : const vec<dr_with_seg_len_pair_t> &comp_alias_ddrs =
4387 : LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
4388 :
4389 3311 : if (comp_alias_ddrs.is_empty ())
4390 : return;
4391 :
4392 3200 : create_runtime_alias_checks (LOOP_VINFO_LOOP (loop_vinfo),
4393 : &comp_alias_ddrs, cond_expr);
4394 3200 : if (dump_enabled_p ())
4395 1292 : dump_printf_loc (MSG_NOTE, vect_location,
4396 : "created %u versioning for alias checks.\n",
4397 : comp_alias_ddrs.length ());
4398 : }
4399 :
4400 :
4401 : /* Function vect_loop_versioning.
4402 :
4403 : If the loop has data references that may or may not be aligned or/and
4404 : has data reference relations whose independence was not proven then
4405 : two versions of the loop need to be generated, one which is vectorized
4406 : and one which isn't. A test is then generated to control which of the
4407 : loops is executed. The test checks for the alignment of all of the
4408 : data references that may or may not be aligned. An additional
4409 : sequence of runtime tests is generated for each pairs of DDRs whose
4410 : independence was not proven. The vectorized version of loop is
4411 : executed only if both alias and alignment tests are passed.
4412 :
4413 : The test generated to check which version of loop is executed
4414 : is modified to also check for profitability as indicated by the
4415 : cost model threshold TH.
4416 :
4417 : The versioning precondition(s) are placed in *COND_EXPR and
4418 : *COND_EXPR_STMT_LIST. */
4419 :
4420 : class loop *
4421 3766 : vect_loop_versioning (loop_vec_info loop_vinfo,
4422 : gimple *loop_vectorized_call)
4423 : {
4424 3766 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo), *nloop;
4425 3766 : class loop *scalar_loop = LOOP_VINFO_SCALAR_LOOP (loop_vinfo);
4426 3766 : basic_block condition_bb;
4427 3766 : gphi_iterator gsi;
4428 3766 : gimple_stmt_iterator cond_exp_gsi;
4429 3766 : basic_block merge_bb;
4430 3766 : basic_block new_exit_bb;
4431 3766 : edge new_exit_e, e;
4432 3766 : gphi *orig_phi, *new_phi;
4433 3766 : tree cond_expr = NULL_TREE;
4434 3766 : gimple_seq cond_expr_stmt_list = NULL;
4435 3766 : tree arg;
4436 3766 : profile_probability prob = profile_probability::likely ();
4437 3766 : gimple_seq gimplify_stmt_list = NULL;
4438 3766 : tree scalar_loop_iters = LOOP_VINFO_NITERSM1 (loop_vinfo);
4439 3766 : bool version_align = LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo);
4440 3766 : bool version_spec_read = LOOP_REQUIRES_VERSIONING_FOR_SPEC_READ (loop_vinfo);
4441 3766 : bool version_alias = LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo);
4442 3766 : bool version_niter = LOOP_REQUIRES_VERSIONING_FOR_NITERS (loop_vinfo);
4443 3766 : poly_uint64 versioning_threshold
4444 : = LOOP_VINFO_VERSIONING_THRESHOLD (loop_vinfo);
4445 3766 : tree version_simd_if_cond
4446 : = LOOP_REQUIRES_VERSIONING_FOR_SIMD_IF_COND (loop_vinfo);
4447 3766 : unsigned th = LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo);
4448 3766 : bool uncounted_p = LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo);
4449 :
4450 3766 : if (!uncounted_p && vect_apply_runtime_profitability_check_p (loop_vinfo)
4451 : && !ordered_p (th, versioning_threshold))
4452 : cond_expr = fold_build2 (GE_EXPR, boolean_type_node, scalar_loop_iters,
4453 : build_int_cst (TREE_TYPE (scalar_loop_iters),
4454 : th - 1));
4455 3766 : if (!uncounted_p && maybe_ne (versioning_threshold, 0U))
4456 : {
4457 3753 : tree expr = fold_build2 (GE_EXPR, boolean_type_node, scalar_loop_iters,
4458 : build_int_cst (TREE_TYPE (scalar_loop_iters),
4459 : versioning_threshold - 1));
4460 3753 : if (cond_expr)
4461 0 : cond_expr = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4462 : expr, cond_expr);
4463 : else
4464 3753 : cond_expr = expr;
4465 : }
4466 :
4467 3766 : tree cost_name = NULL_TREE;
4468 3766 : profile_probability prob2 = profile_probability::always ();
4469 3766 : if (cond_expr
4470 3753 : && EXPR_P (cond_expr)
4471 2614 : && (version_niter
4472 2614 : || version_align
4473 : || version_alias
4474 2281 : || version_simd_if_cond))
4475 : {
4476 2614 : cost_name = cond_expr = force_gimple_operand_1 (unshare_expr (cond_expr),
4477 : &cond_expr_stmt_list,
4478 : is_gimple_val, NULL_TREE);
4479 : /* Split prob () into two so that the overall probability of passing
4480 : both the cost-model and versioning checks is the orig prob. */
4481 2614 : prob2 = prob = prob.sqrt ();
4482 : }
4483 :
4484 3766 : if (version_niter)
4485 373 : vect_create_cond_for_niters_checks (loop_vinfo, &cond_expr);
4486 :
4487 3766 : if (cond_expr)
4488 : {
4489 3753 : gimple_seq tem = NULL;
4490 3753 : cond_expr = force_gimple_operand_1 (unshare_expr (cond_expr),
4491 : &tem, is_gimple_condexpr_for_cond,
4492 : NULL_TREE);
4493 3753 : gimple_seq_add_seq (&cond_expr_stmt_list, tem);
4494 : }
4495 :
4496 3766 : if (version_align)
4497 45 : vect_create_cond_for_align_checks (loop_vinfo, &cond_expr,
4498 : &cond_expr_stmt_list);
4499 :
4500 3766 : if (version_spec_read)
4501 0 : vect_create_cond_for_vla_spec_read (loop_vinfo, &cond_expr);
4502 :
4503 3766 : if (version_alias)
4504 : {
4505 3311 : vect_create_cond_for_unequal_addrs (loop_vinfo, &cond_expr);
4506 3311 : vect_create_cond_for_lower_bounds (loop_vinfo, &cond_expr);
4507 3311 : vect_create_cond_for_alias_checks (loop_vinfo, &cond_expr);
4508 : }
4509 :
4510 3766 : if (version_simd_if_cond)
4511 : {
4512 58 : gcc_assert (dom_info_available_p (CDI_DOMINATORS));
4513 58 : if (flag_checking)
4514 58 : if (basic_block bb
4515 58 : = gimple_bb (SSA_NAME_DEF_STMT (version_simd_if_cond)))
4516 58 : gcc_assert (bb != loop->header
4517 : && dominated_by_p (CDI_DOMINATORS, loop->header, bb)
4518 : && (scalar_loop == NULL
4519 : || (bb != scalar_loop->header
4520 : && dominated_by_p (CDI_DOMINATORS,
4521 : scalar_loop->header, bb))));
4522 58 : tree zero = build_zero_cst (TREE_TYPE (version_simd_if_cond));
4523 58 : tree c = fold_build2 (NE_EXPR, boolean_type_node,
4524 : version_simd_if_cond, zero);
4525 58 : if (cond_expr)
4526 58 : cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
4527 : c, cond_expr);
4528 : else
4529 0 : cond_expr = c;
4530 58 : if (dump_enabled_p ())
4531 5 : dump_printf_loc (MSG_NOTE, vect_location,
4532 : "created versioning for simd if condition check.\n");
4533 : }
4534 :
4535 3766 : cond_expr = force_gimple_operand_1 (unshare_expr (cond_expr),
4536 : &gimplify_stmt_list,
4537 : is_gimple_condexpr_for_cond, NULL_TREE);
4538 3766 : gimple_seq_add_seq (&cond_expr_stmt_list, gimplify_stmt_list);
4539 :
4540 : /* Compute the outermost loop cond_expr and cond_expr_stmt_list are
4541 : invariant in. */
4542 3766 : class loop *outermost = outermost_invariant_loop_for_expr (loop, cond_expr);
4543 3766 : for (gimple_stmt_iterator gsi = gsi_start (cond_expr_stmt_list);
4544 68352 : !gsi_end_p (gsi); gsi_next (&gsi))
4545 : {
4546 64586 : gimple *stmt = gsi_stmt (gsi);
4547 64586 : update_stmt (stmt);
4548 64586 : ssa_op_iter iter;
4549 64586 : use_operand_p use_p;
4550 64586 : basic_block def_bb;
4551 148528 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
4552 83942 : if ((def_bb = gimple_bb (SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p))))
4553 83942 : && flow_bb_inside_loop_p (outermost, def_bb))
4554 2519 : outermost = superloop_at_depth (loop, bb_loop_depth (def_bb) + 1);
4555 : }
4556 :
4557 : /* Search for the outermost loop we can version. Avoid versioning of
4558 : non-perfect nests but allow if-conversion versioned loops inside. */
4559 3766 : class loop *loop_to_version = loop;
4560 3766 : if (flow_loop_nested_p (outermost, loop))
4561 : {
4562 1446 : if (dump_enabled_p ())
4563 679 : dump_printf_loc (MSG_NOTE, vect_location,
4564 : "trying to apply versioning to outer loop %d\n",
4565 : outermost->num);
4566 1446 : if (outermost->num == 0)
4567 1368 : outermost = superloop_at_depth (loop, 1);
4568 : /* And avoid applying versioning on non-perfect nests. */
4569 : while (loop_to_version != outermost
4570 100 : && (e = single_exit (loop_outer (loop_to_version)))
4571 81 : && !(e->flags & EDGE_COMPLEX)
4572 80 : && (!loop_outer (loop_to_version)->inner->next
4573 52 : || vect_loop_vectorized_call (loop_to_version))
4574 32 : && (!loop_outer (loop_to_version)->inner->next
4575 4 : || !loop_outer (loop_to_version)->inner->next->next)
4576 1508 : && can_duplicate_loop_p (loop_outer (loop_to_version)))
4577 30 : loop_to_version = loop_outer (loop_to_version);
4578 : }
4579 :
4580 : /* Apply versioning. If there is already a scalar version created by
4581 : if-conversion re-use that. Note we cannot re-use the copy of
4582 : an if-converted outer-loop when vectorizing the inner loop only. */
4583 3766 : gcond *cond;
4584 3766 : if ((!loop_to_version->inner || loop == loop_to_version)
4585 3740 : && loop_vectorized_call)
4586 : {
4587 90 : gcc_assert (scalar_loop);
4588 90 : condition_bb = gimple_bb (loop_vectorized_call);
4589 180 : cond = as_a <gcond *> (*gsi_last_bb (condition_bb));
4590 90 : gimple_cond_set_condition_from_tree (cond, cond_expr);
4591 90 : update_stmt (cond);
4592 :
4593 90 : if (cond_expr_stmt_list)
4594 : {
4595 90 : cond_exp_gsi = gsi_for_stmt (loop_vectorized_call);
4596 90 : gsi_insert_seq_before (&cond_exp_gsi, cond_expr_stmt_list,
4597 : GSI_SAME_STMT);
4598 : }
4599 :
4600 : /* if-conversion uses profile_probability::always () for both paths,
4601 : reset the paths probabilities appropriately. */
4602 90 : edge te, fe;
4603 90 : extract_true_false_edges_from_block (condition_bb, &te, &fe);
4604 90 : te->probability = prob;
4605 90 : fe->probability = prob.invert ();
4606 : /* We can scale loops counts immediately but have to postpone
4607 : scaling the scalar loop because we re-use it during peeling.
4608 :
4609 : Ifcvt duplicates loop preheader, loop body and produces an basic
4610 : block after loop exit. We need to scale all that. */
4611 90 : basic_block preheader = loop_preheader_edge (loop_to_version)->src;
4612 90 : preheader->count = preheader->count.apply_probability (prob * prob2);
4613 90 : scale_loop_frequencies (loop_to_version, prob * prob2);
4614 : /* When the loop has multiple exits then we can only version itself.
4615 : This is denoted by loop_to_version == loop. In this case we can
4616 : do the versioning by selecting the exit edge the vectorizer is
4617 : currently using. */
4618 90 : edge exit_edge;
4619 90 : if (loop_to_version == loop)
4620 90 : exit_edge = LOOP_VINFO_MAIN_EXIT (loop_vinfo);
4621 : else
4622 0 : exit_edge = single_exit (loop_to_version);
4623 90 : exit_edge->dest->count = preheader->count;
4624 90 : LOOP_VINFO_SCALAR_LOOP_SCALING (loop_vinfo) = (prob * prob2).invert ();
4625 :
4626 90 : nloop = scalar_loop;
4627 90 : if (dump_enabled_p ())
4628 90 : dump_printf_loc (MSG_NOTE, vect_location,
4629 : "reusing %sloop version created by if conversion\n",
4630 : loop_to_version != loop ? "outer " : "");
4631 90 : }
4632 : else
4633 : {
4634 3676 : if (loop_to_version != loop
4635 3676 : && dump_enabled_p ())
4636 13 : dump_printf_loc (MSG_NOTE, vect_location,
4637 : "applying loop versioning to outer loop %d\n",
4638 : loop_to_version->num);
4639 :
4640 3676 : unsigned orig_pe_idx = loop_preheader_edge (loop)->dest_idx;
4641 :
4642 3676 : initialize_original_copy_tables ();
4643 7352 : nloop = loop_version (loop_to_version, cond_expr, &condition_bb,
4644 3676 : prob * prob2, (prob * prob2).invert (),
4645 3676 : prob * prob2, (prob * prob2).invert (),
4646 : true);
4647 :
4648 : /* If the PHI nodes in the loop header were reallocated, we need to fix up
4649 : our internally stashed copies of those. */
4650 3676 : if (loop_to_version == loop)
4651 3650 : for (auto gsi = gsi_start_phis (loop->header);
4652 14569 : !gsi_end_p (gsi); gsi_next (&gsi))
4653 10919 : loop_vinfo->resync_stmt_addr (gsi.phi ());
4654 :
4655 : /* We will later insert second conditional so overall outcome of
4656 : both is prob * prob2. */
4657 3676 : edge true_e, false_e;
4658 3676 : extract_true_false_edges_from_block (condition_bb, &true_e, &false_e);
4659 3676 : true_e->probability = prob;
4660 3676 : false_e->probability = prob.invert ();
4661 3676 : gcc_assert (nloop);
4662 3676 : nloop = get_loop_copy (loop);
4663 :
4664 : /* Assign hierarchical discriminators to distinguish loop versions.
4665 : Only assign to the scalar version here; the vectorized version will
4666 : get discriminators later during transformation/peeling.
4667 : Use dynamic copy_id allocation instead of hardcoded constants. */
4668 3676 : gimple *nloop_last = last_nondebug_stmt (nloop->header);
4669 3676 : location_t nloop_loc
4670 3676 : = nloop_last ? gimple_location (nloop_last) : UNKNOWN_LOCATION;
4671 3676 : if (nloop_loc != UNKNOWN_LOCATION)
4672 : {
4673 3212 : unsigned int nloop_copyid = allocate_copyid_base (nloop_loc, 1);
4674 3212 : assign_discriminators_to_loop (nloop, 0, nloop_copyid);
4675 : }
4676 : /* For cycle vectorization with SLP we rely on the PHI arguments
4677 : appearing in the same order as the SLP node operands which for the
4678 : loop PHI nodes means the preheader edge dest index needs to remain
4679 : the same for the analyzed loop which also becomes the vectorized one.
4680 : Make it so in case the state after versioning differs by redirecting
4681 : the first edge into the header to the same destination which moves
4682 : it last. */
4683 3676 : if (loop_preheader_edge (loop)->dest_idx != orig_pe_idx)
4684 : {
4685 302 : edge e = EDGE_PRED (loop->header, 0);
4686 302 : ssa_redirect_edge (e, e->dest);
4687 302 : flush_pending_stmts (e);
4688 : }
4689 3676 : gcc_assert (loop_preheader_edge (loop)->dest_idx == orig_pe_idx);
4690 :
4691 : /* Kill off IFN_LOOP_VECTORIZED_CALL in the copy, nobody will
4692 : reap those otherwise; they also refer to the original
4693 : loops. */
4694 : class loop *l = loop;
4695 3680 : while (gimple *call = vect_loop_vectorized_call (l))
4696 : {
4697 4 : call = SSA_NAME_DEF_STMT (get_current_def (gimple_call_lhs (call)));
4698 4 : fold_loop_internal_call (call, boolean_false_node);
4699 4 : l = loop_outer (l);
4700 4 : }
4701 3676 : free_original_copy_tables ();
4702 :
4703 3676 : if (cond_expr_stmt_list)
4704 : {
4705 3595 : cond_exp_gsi = gsi_last_bb (condition_bb);
4706 3595 : gsi_insert_seq_before (&cond_exp_gsi, cond_expr_stmt_list,
4707 : GSI_SAME_STMT);
4708 : }
4709 :
4710 : /* Loop versioning violates an assumption we try to maintain during
4711 : vectorization - that the loop exit block has a single predecessor.
4712 : After versioning, the exit block of both loop versions is the same
4713 : basic block (i.e. it has two predecessors). Just in order to simplify
4714 : following transformations in the vectorizer, we fix this situation
4715 : here by adding a new (empty) block on the exit-edge of the loop,
4716 : with the proper loop-exit phis to maintain loop-closed-form.
4717 : If loop versioning wasn't done from loop, but scalar_loop instead,
4718 : merge_bb will have already just a single successor. */
4719 :
4720 : /* When the loop has multiple exits then we can only version itself.
4721 : This is denoted by loop_to_version == loop. In this case we can
4722 : do the versioning by selecting the exit edge the vectorizer is
4723 : currently using. */
4724 3676 : edge exit_edge;
4725 3676 : if (loop_to_version == loop)
4726 3650 : exit_edge = LOOP_VINFO_MAIN_EXIT (loop_vinfo);
4727 : else
4728 26 : exit_edge = single_exit (loop_to_version);
4729 :
4730 3676 : gcc_assert (exit_edge);
4731 3676 : merge_bb = exit_edge->dest;
4732 3676 : if (EDGE_COUNT (merge_bb->preds) >= 2)
4733 : {
4734 3676 : gcc_assert (EDGE_COUNT (merge_bb->preds) >= 2);
4735 3676 : new_exit_bb = split_edge (exit_edge);
4736 3676 : new_exit_e = exit_edge;
4737 3676 : e = EDGE_SUCC (new_exit_bb, 0);
4738 :
4739 7603 : for (gsi = gsi_start_phis (merge_bb); !gsi_end_p (gsi);
4740 3927 : gsi_next (&gsi))
4741 : {
4742 3927 : tree new_res;
4743 3927 : orig_phi = gsi.phi ();
4744 3927 : new_res = copy_ssa_name (PHI_RESULT (orig_phi));
4745 3927 : new_phi = create_phi_node (new_res, new_exit_bb);
4746 3927 : arg = PHI_ARG_DEF_FROM_EDGE (orig_phi, e);
4747 3927 : add_phi_arg (new_phi, arg, new_exit_e,
4748 : gimple_phi_arg_location_from_edge (orig_phi, e));
4749 3927 : adjust_phi_and_debug_stmts (orig_phi, e, PHI_RESULT (new_phi));
4750 : }
4751 : }
4752 :
4753 3676 : update_ssa (TODO_update_ssa_no_phi);
4754 : }
4755 :
4756 : /* Split the cost model check off to a separate BB. Costing assumes
4757 : this is the only thing we perform when we enter the scalar loop
4758 : from a failed cost decision. */
4759 3766 : if (cost_name && TREE_CODE (cost_name) == SSA_NAME)
4760 : {
4761 2614 : gimple *def = SSA_NAME_DEF_STMT (cost_name);
4762 2614 : gcc_assert (gimple_bb (def) == condition_bb);
4763 : /* All uses of the cost check are 'true' after the check we
4764 : are going to insert. */
4765 2614 : replace_uses_by (cost_name, boolean_true_node);
4766 : /* And we're going to build the new single use of it. */
4767 2614 : gcond *cond = gimple_build_cond (NE_EXPR, cost_name, boolean_false_node,
4768 : NULL_TREE, NULL_TREE);
4769 2614 : edge e = split_block (gimple_bb (def), def);
4770 2614 : gimple_stmt_iterator gsi = gsi_for_stmt (def);
4771 2614 : gsi_insert_after (&gsi, cond, GSI_NEW_STMT);
4772 2614 : edge true_e, false_e;
4773 2614 : extract_true_false_edges_from_block (e->dest, &true_e, &false_e);
4774 2614 : e->flags &= ~EDGE_FALLTHRU;
4775 2614 : e->flags |= EDGE_TRUE_VALUE;
4776 2614 : edge e2 = make_edge (e->src, false_e->dest, EDGE_FALSE_VALUE);
4777 2614 : e->probability = prob2;
4778 2614 : e2->probability = prob2.invert ();
4779 2614 : e->dest->count = e->count ();
4780 2614 : set_immediate_dominator (CDI_DOMINATORS, false_e->dest, e->src);
4781 2614 : auto_vec<basic_block, 3> adj;
4782 2614 : for (basic_block son = first_dom_son (CDI_DOMINATORS, e->dest);
4783 7745 : son;
4784 5131 : son = next_dom_son (CDI_DOMINATORS, son))
4785 7648 : if (EDGE_COUNT (son->preds) > 1)
4786 2517 : adj.safe_push (son);
4787 10359 : for (auto son : adj)
4788 2517 : set_immediate_dominator (CDI_DOMINATORS, son, e->src);
4789 : //debug_bb (condition_bb);
4790 : //debug_bb (e->src);
4791 2614 : }
4792 :
4793 3766 : if (version_niter)
4794 : {
4795 : /* The versioned loop could be infinite, we need to clear existing
4796 : niter information which is copied from the original loop. */
4797 373 : gcc_assert (loop_constraint_set_p (loop, LOOP_C_FINITE));
4798 373 : vect_free_loop_info_assumptions (nloop);
4799 : }
4800 :
4801 3766 : if (LOCATION_LOCUS (vect_location.get_location_t ()) != UNKNOWN_LOCATION
4802 3766 : && dump_enabled_p ())
4803 : {
4804 821 : if (version_alias)
4805 748 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS | MSG_PRIORITY_USER_FACING,
4806 : vect_location,
4807 : "loop versioned for vectorization because of "
4808 : "possible aliasing\n");
4809 821 : if (version_align)
4810 30 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS | MSG_PRIORITY_USER_FACING,
4811 : vect_location,
4812 : "loop versioned for vectorization to enhance "
4813 : "alignment\n");
4814 :
4815 : }
4816 :
4817 3766 : return nloop;
4818 : }
|