]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/sese.c
Preserve the original program while using graphite.
[thirdparty/gcc.git] / gcc / sese.c
1 /* Single entry single exit control flow regions.
2 Copyright (C) 2008-2015 Free Software Foundation, Inc.
3 Contributed by Jan Sjodin <jan.sjodin@amd.com> and
4 Sebastian Pop <sebastian.pop@amd.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License 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 "cfganal.h"
29 #include "cfghooks.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "tree-pretty-print.h"
33 #include "fold-const.h"
34 #include "gimple-fold.h"
35 #include "tree-eh.h"
36 #include "gimplify.h"
37 #include "gimple-iterator.h"
38 #include "gimple-pretty-print.h"
39 #include "gimplify-me.h"
40 #include "tree-cfg.h"
41 #include "tree-ssa-loop.h"
42 #include "tree-into-ssa.h"
43 #include "cfgloop.h"
44 #include "tree-data-ref.h"
45 #include "tree-scalar-evolution.h"
46 #include "value-prof.h"
47 #include "sese.h"
48 #include "tree-ssa-propagate.h"
49
50 /* Record LOOP as occurring in REGION. */
51
52 static void
53 sese_record_loop (sese_info_p region, loop_p loop)
54 {
55 if (sese_contains_loop (region, loop))
56 return;
57
58 bitmap_set_bit (region->loops, loop->num);
59 region->loop_nest.safe_push (loop);
60 }
61
62 /* Build the loop nests contained in REGION. Returns true when the
63 operation was successful. */
64
65 void
66 build_sese_loop_nests (sese_info_p region)
67 {
68 unsigned i;
69 basic_block bb;
70 struct loop *loop0, *loop1;
71
72 FOR_EACH_BB_FN (bb, cfun)
73 if (bb_in_sese_p (bb, region->region))
74 {
75 struct loop *loop = bb->loop_father;
76
77 /* Only add loops if they are completely contained in the SCoP. */
78 if (loop->header == bb
79 && bb_in_sese_p (loop->latch, region->region))
80 sese_record_loop (region, loop);
81 }
82
83 /* Make sure that the loops in the SESE_LOOP_NEST are ordered. It
84 can be the case that an inner loop is inserted before an outer
85 loop. To avoid this, semi-sort once. */
86 FOR_EACH_VEC_ELT (region->loop_nest, i, loop0)
87 {
88 if (region->loop_nest.length () == i + 1)
89 break;
90
91 loop1 = region->loop_nest[i + 1];
92 if (loop0->num > loop1->num)
93 {
94 region->loop_nest[i] = loop1;
95 region->loop_nest[i + 1] = loop0;
96 }
97 }
98 }
99
100 /* For a USE in BB, if BB is outside REGION, mark the USE in the
101 LIVEOUTS set. */
102
103 static void
104 sese_build_liveouts_use (sese_info_p region, bitmap liveouts, basic_block bb,
105 tree use)
106 {
107 gcc_assert (!bb_in_sese_p (bb, region->region));
108 if (TREE_CODE (use) != SSA_NAME)
109 return;
110
111 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
112
113 if (!def_bb || !bb_in_sese_p (def_bb, region->region))
114 return;
115
116 unsigned ver = SSA_NAME_VERSION (use);
117 bitmap_set_bit (liveouts, ver);
118 }
119
120 /* Marks for rewrite all the SSA_NAMES defined in REGION and that are
121 used in BB that is outside of the REGION. */
122
123 static void
124 sese_build_liveouts_bb (sese_info_p region, bitmap liveouts, basic_block bb)
125 {
126 edge e;
127 edge_iterator ei;
128 ssa_op_iter iter;
129 use_operand_p use_p;
130
131 FOR_EACH_EDGE (e, ei, bb->succs)
132 for (gphi_iterator bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi);
133 gsi_next (&bsi))
134 sese_build_liveouts_use (region, liveouts, bb,
135 PHI_ARG_DEF_FROM_EDGE (bsi.phi (), e));
136
137 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
138 gsi_next (&bsi))
139 {
140 gimple *stmt = gsi_stmt (bsi);
141
142 if (is_gimple_debug (stmt))
143 continue;
144
145 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
146 sese_build_liveouts_use (region, liveouts, bb, USE_FROM_PTR (use_p));
147 }
148 }
149
150 /* For a USE in BB, return true if BB is outside REGION and it's not
151 in the LIVEOUTS set. */
152
153 static bool
154 sese_bad_liveouts_use (sese_info_p region, bitmap liveouts, basic_block bb,
155 tree use)
156 {
157 gcc_assert (!bb_in_sese_p (bb, region->region));
158
159 if (TREE_CODE (use) != SSA_NAME)
160 return false;
161
162 unsigned ver = SSA_NAME_VERSION (use);
163
164 /* If it's in liveouts, the variable will get a new PHI node, and
165 the debug use will be properly adjusted. */
166 if (bitmap_bit_p (liveouts, ver))
167 return false;
168
169 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
170
171 if (!def_bb || !bb_in_sese_p (def_bb, region->region))
172 return false;
173
174 return true;
175 }
176
177 /* Reset debug stmts that reference SSA_NAMES defined in REGION that
178 are not marked as liveouts. */
179
180 static void
181 sese_reset_debug_liveouts_bb (sese_info_p region, bitmap liveouts, basic_block bb)
182 {
183 gimple_stmt_iterator bsi;
184 ssa_op_iter iter;
185 use_operand_p use_p;
186
187 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
188 {
189 gimple *stmt = gsi_stmt (bsi);
190
191 if (!is_gimple_debug (stmt))
192 continue;
193
194 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
195 if (sese_bad_liveouts_use (region, liveouts, bb,
196 USE_FROM_PTR (use_p)))
197 {
198 gimple_debug_bind_reset_value (stmt);
199 update_stmt (stmt);
200 break;
201 }
202 }
203 }
204
205 /* Build the LIVEOUTS of REGION: the set of variables defined inside
206 and used outside the REGION. */
207
208 static void
209 sese_build_liveouts (sese_info_p region, bitmap liveouts)
210 {
211 basic_block bb;
212
213 /* FIXME: We could start iterating form the successor of sese. */
214 FOR_EACH_BB_FN (bb, cfun)
215 if (!bb_in_sese_p (bb, region->region))
216 sese_build_liveouts_bb (region, liveouts, bb);
217
218 /* FIXME: We could start iterating form the successor of sese. */
219 if (MAY_HAVE_DEBUG_STMTS)
220 FOR_EACH_BB_FN (bb, cfun)
221 if (!bb_in_sese_p (bb, region->region))
222 sese_reset_debug_liveouts_bb (region, liveouts, bb);
223 }
224
225 /* Builds a new SESE region from edges ENTRY and EXIT. */
226
227 sese_info_p
228 new_sese_info (edge entry, edge exit)
229 {
230 sese_info_p region = XNEW (struct sese_info_t);
231
232 region->region.entry = entry;
233 region->region.exit = exit;
234 region->loops = BITMAP_ALLOC (NULL);
235 region->loop_nest.create (3);
236 region->params.create (3);
237 region->rename_map = new rename_map_t;
238 region->copied_bb_map = new bb_map_t;
239 region->bbs.create (3);
240 region->incomplete_phis.create (3);
241
242 return region;
243 }
244
245 /* Deletes REGION. */
246
247 void
248 free_sese_info (sese_info_p region)
249 {
250 if (region->loops)
251 region->loops = BITMAP_ALLOC (NULL);
252
253 region->params.release ();
254 region->loop_nest.release ();
255
256 for (rename_map_t::iterator it = region->rename_map->begin ();
257 it != region->rename_map->begin (); ++it)
258 (*it).second.release ();
259
260 for (bb_map_t::iterator it = region->copied_bb_map->begin ();
261 it != region->copied_bb_map->begin (); ++it)
262 (*it).second.release ();
263
264 delete region->rename_map;
265 delete region->copied_bb_map;
266
267 region->rename_map = NULL;
268 region->copied_bb_map = NULL;
269
270 region->bbs.release ();
271 region->incomplete_phis.release ();
272
273 XDELETE (region);
274 }
275
276 /* Add exit phis for USE on EXIT. */
277
278 static void
279 sese_add_exit_phis_edge (basic_block exit, tree use, edge false_e, edge true_e)
280 {
281 gphi *phi = create_phi_node (NULL_TREE, exit);
282 create_new_def_for (use, phi, gimple_phi_result_ptr (phi));
283 add_phi_arg (phi, use, false_e, UNKNOWN_LOCATION);
284 add_phi_arg (phi, use, true_e, UNKNOWN_LOCATION);
285 update_stmt (phi);
286 }
287
288 /* Insert in the block BB phi nodes for variables defined in REGION
289 and used outside the REGION. The code generation moves REGION in
290 the else clause of an "if (1)" and generates code in the then
291 clause that is at this point empty:
292
293 | if (1)
294 | empty;
295 | else
296 | REGION;
297 */
298
299 void
300 sese_insert_phis_for_liveouts (sese_info_p region, basic_block bb,
301 edge false_e, edge true_e)
302 {
303 unsigned i;
304 bitmap_iterator bi;
305 bitmap liveouts = BITMAP_ALLOC (NULL);
306
307 update_ssa (TODO_update_ssa);
308
309 sese_build_liveouts (region, liveouts);
310
311 EXECUTE_IF_SET_IN_BITMAP (liveouts, 0, i, bi)
312 if (!virtual_operand_p (ssa_name (i)))
313 sese_add_exit_phis_edge (bb, ssa_name (i), false_e, true_e);
314
315 BITMAP_FREE (liveouts);
316
317 update_ssa (TODO_update_ssa);
318 }
319
320 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set. */
321
322 edge
323 get_true_edge_from_guard_bb (basic_block bb)
324 {
325 edge e;
326 edge_iterator ei;
327
328 FOR_EACH_EDGE (e, ei, bb->succs)
329 if (e->flags & EDGE_TRUE_VALUE)
330 return e;
331
332 gcc_unreachable ();
333 return NULL;
334 }
335
336 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared. */
337
338 edge
339 get_false_edge_from_guard_bb (basic_block bb)
340 {
341 edge e;
342 edge_iterator ei;
343
344 FOR_EACH_EDGE (e, ei, bb->succs)
345 if (!(e->flags & EDGE_TRUE_VALUE))
346 return e;
347
348 gcc_unreachable ();
349 return NULL;
350 }
351
352 /* Check if USE is defined in a basic block from where the definition of USE can
353 propagate from all the paths. */
354
355 static bool
356 is_loop_closed_ssa_use (basic_block bb, tree use)
357 {
358 if (TREE_CODE (use) != SSA_NAME)
359 return true;
360
361 /* We should not have a rename for virtual operands. */
362 gcc_assert (!virtual_operand_p (use));
363
364 /* For close-phi nodes def always comes from a loop which has a back-edge. */
365 if (bb_contains_loop_close_phi_nodes (bb))
366 return true;
367
368 gimple *def = SSA_NAME_DEF_STMT (use);
369 basic_block def_bb = gimple_bb (def);
370 return (!def_bb
371 || flow_bb_inside_loop_p (def_bb->loop_father, bb));
372 }
373
374 /* Return the number of phi nodes in BB. */
375
376 static int
377 number_of_phi_nodes (basic_block bb)
378 {
379 int num_phis = 0;
380 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
381 gsi_next (&psi))
382 num_phis++;
383 return num_phis;
384 }
385
386 /* Return true when BB contains loop close phi nodes. */
387
388 bool
389 bb_contains_loop_close_phi_nodes (basic_block bb)
390 {
391 return single_pred_p (bb)
392 && bb->loop_father != single_pred_edge (bb)->src->loop_father;
393 }
394
395 /* Return true when BB contains loop phi nodes. */
396
397 bool
398 bb_contains_loop_phi_nodes (basic_block bb)
399 {
400 gcc_assert (EDGE_COUNT (bb->preds) <= 2);
401
402 if (bb->preds->length () == 1)
403 return false;
404
405 unsigned depth = loop_depth (bb->loop_father);
406
407 edge preds[2] = { (*bb->preds)[0], (*bb->preds)[1] };
408
409 if (depth > loop_depth (preds[0]->src->loop_father)
410 || depth > loop_depth (preds[1]->src->loop_father))
411 return true;
412
413 /* When one of the edges correspond to the same loop father and other
414 doesn't. */
415 if (bb->loop_father != preds[0]->src->loop_father
416 && bb->loop_father == preds[1]->src->loop_father)
417 return true;
418
419 if (bb->loop_father != preds[1]->src->loop_father
420 && bb->loop_father == preds[0]->src->loop_father)
421 return true;
422
423 return false;
424 }
425
426 /* Returns true if BB uses name in one of its PHIs. */
427
428 static bool
429 phi_uses_name (basic_block bb, tree name)
430 {
431 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
432 gsi_next (&psi))
433 {
434 gphi *phi = psi.phi ();
435 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
436 {
437 tree use_arg = gimple_phi_arg_def (phi, i);
438 if (use_arg == name)
439 return true;
440 }
441 }
442 return false;
443 }
444
445 /* Return true if RENAME (defined in BB) is a valid use in NEW_BB. The
446 definition should flow into use, and the use should respect the loop-closed SSA
447 form. */
448
449 static bool
450 is_valid_rename (tree rename, basic_block def_bb,
451 basic_block use_bb, bool loop_phi,
452 tree old_name, basic_block old_bb)
453 {
454 /* The def of the rename must either dominate the uses or come from a
455 back-edge. Also the def must respect the loop closed ssa form. */
456 if (!is_loop_closed_ssa_use (use_bb, rename))
457 {
458 if (dump_file)
459 {
460 fprintf (dump_file, "\n[codegen] rename not in loop closed ssa:");
461 print_generic_expr (dump_file, rename, 0);
462 }
463 return false;
464 }
465
466 if (dominated_by_p (CDI_DOMINATORS, use_bb, def_bb))
467 return true;
468
469 if (bb_contains_loop_phi_nodes (use_bb) && loop_phi)
470 {
471 /* The loop-header dominates the loop-body. */
472 if (!dominated_by_p (CDI_DOMINATORS, def_bb, use_bb))
473 return false;
474
475 /* RENAME would be used in loop-phi. */
476 gcc_assert (number_of_phi_nodes (use_bb));
477
478 /* For definitions coming from back edges, we should check that
479 old_name is used in a loop PHI node. */
480 if (phi_uses_name (old_bb, old_name))
481 return true;
482 }
483 return false;
484 }
485
486 /* Returns the expression associated to OLD_NAME (which is used in OLD_BB), in
487 NEW_BB from RENAME_MAP. LOOP_PHI is true when we want to rename OLD_NAME
488 within a loop PHI instruction. */
489
490 static tree
491 get_rename (rename_map_t *rename_map, basic_block new_bb, tree old_name,
492 basic_block old_bb, bool loop_phi)
493 {
494 gcc_assert (TREE_CODE (old_name) == SSA_NAME);
495 vec <tree> *renames = rename_map->get (old_name);
496
497 if (!renames || renames->is_empty ())
498 return NULL_TREE;
499
500 if (1 == renames->length ())
501 {
502 tree rename = (*renames)[0];
503 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (rename));
504 if (is_valid_rename (rename, bb, new_bb, loop_phi, old_name, old_bb))
505 return rename;
506 return NULL_TREE;
507 }
508
509 /* More than one renames corresponding to the old_name. Find the rename for
510 which the definition flows into usage at new_bb. */
511 int i;
512 tree t1 = NULL_TREE, t2;
513 basic_block t1_bb = NULL;
514 FOR_EACH_VEC_ELT (*renames, i, t2)
515 {
516 basic_block t2_bb = gimple_bb (SSA_NAME_DEF_STMT (t2));
517
518 /* Defined in the same basic block as used. */
519 if (t2_bb == new_bb)
520 return t2;
521
522 /* NEW_BB and T2_BB are in two unrelated if-clauses. */
523 if (!dominated_by_p (CDI_DOMINATORS, new_bb, t2_bb))
524 continue;
525
526 /* Compute the nearest dominator. */
527 if (!t1 || dominated_by_p (CDI_DOMINATORS, t2_bb, t1_bb))
528 {
529 t1_bb = t2_bb;
530 t1 = t2;
531 }
532 //if (is_valid_rename (rename, bb, new_bb, loop_phi, old_name, old_bb))
533 //return rename;
534 }
535
536 return t1;
537 }
538
539 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
540 When OLD_NAME and EXPR are the same we assert. */
541
542 static void
543 set_rename (tree old_name, tree expr, sese_info_p region)
544 {
545 if (dump_file)
546 {
547 fprintf (dump_file, "\n[codegen] setting rename: old_name = ");
548 print_generic_expr (dump_file, old_name, 0);
549 fprintf (dump_file, ", new_name = ");
550 print_generic_expr (dump_file, expr, 0);
551 }
552
553 if (old_name == expr)
554 return;
555
556 vec <tree> *renames = region->rename_map->get (old_name);
557
558 if (renames)
559 renames->safe_push (expr);
560 else
561 {
562 vec<tree> r;
563 r.create (2);
564 r.safe_push (expr);
565 region->rename_map->put (old_name, r);
566 }
567 }
568
569 /* Return an iterator to the instructions comes
570 last in the execution order. Either GSI1 and GSI2 should belong
571 to the same basic block or one of their respective basic blocks
572 should dominate the other. */
573
574 gimple_stmt_iterator
575 later_of_the_two (gimple_stmt_iterator gsi1, gimple_stmt_iterator gsi2)
576 {
577 basic_block bb1 = gsi_bb (gsi1);
578 basic_block bb2 = gsi_bb (gsi2);
579
580 /* Find the iterator which is the latest. */
581 if (bb1 == bb2)
582 {
583 /* For empty basic blocks gsis point to the end of the sequence. Since
584 there is no operator== defined for gimple_stmt_iterator and for gsis
585 not pointing to a valid statement gsi_next would assert. */
586 gimple_stmt_iterator gsi = gsi1;
587 do {
588 if (gsi_stmt (gsi) == gsi_stmt (gsi2))
589 return gsi2;
590 gsi_next (&gsi);
591 } while (!gsi_end_p (gsi));
592
593 return gsi1;
594 }
595
596 /* Find the basic block closest to the basic block which defines stmt. */
597 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
598 return gsi1;
599
600 gcc_assert (dominated_by_p (CDI_DOMINATORS, bb2, bb1));
601 return gsi2;
602 }
603
604 /* Insert each statement from SEQ at its earliest insertion p. */
605
606 static void
607 gsi_insert_earliest (gimple_seq seq, sese_info_p region)
608 {
609 update_modified_stmts (seq);
610 sese_l &codegen_region = region->if_region->true_region->region;
611 basic_block begin_bb = get_entry_bb (codegen_region);
612
613 /* Inserting the gimple statements in a vector because gimple_seq behave
614 in strage ways when inserting the stmts from it into different basic
615 blocks one at a time. */
616 auto_vec<gimple *, 3> stmts;
617 for (gimple_stmt_iterator gsi = gsi_start (seq); !gsi_end_p (gsi);
618 gsi_next (&gsi))
619 stmts.safe_push (gsi_stmt (gsi));
620
621 int i;
622 gimple *use_stmt;
623 FOR_EACH_VEC_ELT (stmts, i, use_stmt)
624 {
625 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
626 gimple_stmt_iterator gsi_def_stmt = gsi_start_bb_nondebug (begin_bb);
627
628 use_operand_p use_p;
629 ssa_op_iter op_iter;
630 FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, op_iter, SSA_OP_USE)
631 {
632 /* Iterator to the current def of use_p. For function parameters or
633 anything where def is not found, insert at the beginning of the
634 generated region. */
635 gimple_stmt_iterator gsi_stmt = gsi_def_stmt;
636
637 tree op = USE_FROM_PTR (use_p);
638 gimple *stmt = SSA_NAME_DEF_STMT (op);
639 if (stmt && (gimple_code (stmt) != GIMPLE_NOP))
640 gsi_stmt = gsi_for_stmt (stmt);
641
642 /* For region parameters, insert at the beginning of the generated
643 region. */
644 if (!bb_in_sese_p (gsi_bb (gsi_stmt), codegen_region))
645 {
646 /* The parameter should have been inserted in the parameter
647 map or it must have a scev. */
648 gsi_stmt = gsi_def_stmt;
649 }
650
651 gsi_def_stmt = later_of_the_two (gsi_stmt, gsi_def_stmt);
652 }
653
654 if (!gsi_stmt (gsi_def_stmt))
655 {
656 gimple_stmt_iterator gsi = gsi_after_labels (gsi_bb (gsi_def_stmt));
657 gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT);
658 }
659 else if (gimple_code (gsi_stmt (gsi_def_stmt)) == GIMPLE_PHI)
660 {
661 gimple_stmt_iterator bsi
662 = gsi_start_bb_nondebug (gsi_bb (gsi_def_stmt));
663 /* Insert right after the PHI statements. */
664 gsi_insert_before (&bsi, use_stmt, GSI_NEW_STMT);
665 }
666 else
667 gsi_insert_after (&gsi_def_stmt, use_stmt, GSI_NEW_STMT);
668
669 if (dump_file)
670 {
671 fprintf (dump_file, "\n[codegen] inserting statement: ");
672 print_gimple_stmt (dump_file, use_stmt, 0, TDF_VOPS | TDF_MEMSYMS);
673 print_loops_bb (dump_file, gimple_bb (use_stmt), 0, 3);
674 }
675 }
676 }
677
678 /* Collect all the operands of NEW_EXPR by recursively visiting each
679 operand. */
680
681 static void
682 collect_all_ssa_names (tree new_expr, vec<tree> *vec_ssa, sese_info_p region)
683 {
684
685 /* Rename all uses in new_expr. */
686 if (TREE_CODE (new_expr) == SSA_NAME)
687 {
688 vec_ssa->safe_push (new_expr);
689 return;
690 }
691
692 /* Iterate over SSA_NAMES in NEW_EXPR. */
693 for (int i = 0; i < (TREE_CODE_LENGTH (TREE_CODE (new_expr))); i++)
694 {
695 tree op = TREE_OPERAND (new_expr, i);
696 collect_all_ssa_names (op, vec_ssa, region);
697 }
698 }
699
700 static tree
701 substitute_ssa_name (tree exp, tree f, tree r)
702 {
703 enum tree_code code = TREE_CODE (exp);
704 tree op0, op1, op2, op3;
705 tree new_tree;
706
707 /* We handle TREE_LIST and COMPONENT_REF separately. */
708 if (code == TREE_LIST)
709 {
710 op0 = substitute_ssa_name (TREE_CHAIN (exp), f, r);
711 op1 = substitute_ssa_name (TREE_VALUE (exp), f, r);
712 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
713 return exp;
714
715 return tree_cons (TREE_PURPOSE (exp), op1, op0);
716 }
717 else if (code == COMPONENT_REF)
718 {
719 tree inner;
720
721 /* If this expression is getting a value from a PLACEHOLDER_EXPR
722 and it is the right field, replace it with R. */
723 for (inner = TREE_OPERAND (exp, 0);
724 REFERENCE_CLASS_P (inner);
725 inner = TREE_OPERAND (inner, 0))
726 ;
727
728 /* The field. */
729 op1 = TREE_OPERAND (exp, 1);
730
731 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
732 return r;
733
734 /* If this expression hasn't been completed let, leave it alone. */
735 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
736 return exp;
737
738 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
739 if (op0 == TREE_OPERAND (exp, 0))
740 return exp;
741
742 new_tree
743 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
744 }
745 else
746 switch (TREE_CODE_CLASS (code))
747 {
748 case tcc_constant:
749 return exp;
750
751 case tcc_declaration:
752 if (exp == f)
753 return r;
754 else
755 return exp;
756
757 case tcc_expression:
758 if (exp == f)
759 return r;
760
761 /* Fall through... */
762
763 case tcc_exceptional:
764 case tcc_unary:
765 case tcc_binary:
766 case tcc_comparison:
767 case tcc_reference:
768 switch (TREE_CODE_LENGTH (code))
769 {
770 case 0:
771 if (exp == f)
772 return r;
773 return exp;
774
775 case 1:
776 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
777 if (op0 == TREE_OPERAND (exp, 0))
778 return exp;
779
780 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
781 break;
782
783 case 2:
784 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
785 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
786
787 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
788 return exp;
789
790 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
791 break;
792
793 case 3:
794 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
795 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
796 op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r);
797
798 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
799 && op2 == TREE_OPERAND (exp, 2))
800 return exp;
801
802 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
803 break;
804
805 case 4:
806 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
807 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
808 op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r);
809 op3 = substitute_ssa_name (TREE_OPERAND (exp, 3), f, r);
810
811 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
812 && op2 == TREE_OPERAND (exp, 2)
813 && op3 == TREE_OPERAND (exp, 3))
814 return exp;
815
816 new_tree
817 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
818 break;
819
820 default:
821 gcc_unreachable ();
822 }
823 break;
824
825 case tcc_vl_exp:
826 default:
827 gcc_unreachable ();
828 }
829
830 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
831
832 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
833 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
834
835 return new_tree;
836 }
837
838 /* Rename all the operands of NEW_EXPR by recursively visiting each operand. */
839
840 static tree
841 rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb,
842 sese_info_p region)
843 {
844 vec<tree> ssa_names;
845 ssa_names.create (2);
846 collect_all_ssa_names (new_expr, &ssa_names, region);
847 tree t;
848 int i;
849 FOR_EACH_VEC_ELT (ssa_names, i, t)
850 {
851 if (tree r = get_rename (region->rename_map, new_bb, t, old_bb, false))
852 new_expr = substitute_ssa_name (new_expr, t, r);
853 /* else
854 return NULL_TREE;*/
855 }
856
857 return new_expr;
858 }
859
860 static tree
861 get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
862 basic_block new_bb, basic_block old_bb,
863 vec<tree> iv_map, sese_info_p region, bool *gloog_error)
864 {
865 tree scev = scalar_evolution_in_region (region->region, loop, old_name);
866
867 /* At this point we should know the exact scev for each
868 scalar SSA_NAME used in the scop: all the other scalar
869 SSA_NAMEs should have been translated out of SSA using
870 arrays with one element. */
871 tree new_expr;
872 if (chrec_contains_undetermined (scev))
873 {
874 *gloog_error = true;
875 return build_zero_cst (TREE_TYPE (old_name));
876 }
877
878 new_expr = chrec_apply_map (scev, iv_map);
879
880 /* The apply should produce an expression tree containing
881 the uses of the new induction variables. We should be
882 able to use new_expr instead of the old_name in the newly
883 generated loop nest. */
884 if (chrec_contains_undetermined (new_expr)
885 || tree_contains_chrecs (new_expr, NULL))
886 {
887 *gloog_error = true;
888 return build_zero_cst (TREE_TYPE (old_name));
889 }
890
891 new_expr = rename_all_uses (new_expr, new_bb, old_bb, region);
892
893 /* Replace the old_name with the new_expr. */
894 return force_gimple_operand (unshare_expr (new_expr), stmts,
895 true, NULL_TREE);
896 }
897
898 /* Renames the scalar uses of the statement COPY, using the
899 substitution map RENAME_MAP, inserting the gimplification code at
900 GSI_TGT, for the translation REGION, with the original copied
901 statement in LOOP, and using the induction variable renaming map
902 IV_MAP. Returns true when something has been renamed. GLOOG_ERROR
903 is set when the code generation cannot continue. */
904
905 static bool
906 rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt,
907 basic_block old_bb, sese_info_p region,
908 loop_p loop, vec<tree> iv_map, bool *gloog_error)
909 {
910 bool changed = false;
911
912 if (is_gimple_debug (copy))
913 {
914 if (gimple_debug_bind_p (copy))
915 gimple_debug_bind_reset_value (copy);
916 else if (gimple_debug_source_bind_p (copy))
917 return false;
918 else
919 gcc_unreachable ();
920
921 return false;
922 }
923
924 if (dump_file)
925 {
926 fprintf (dump_file, "\n[codegen] renaming uses of stmt: ");
927 print_gimple_stmt (dump_file, copy, 0, 0);
928 }
929
930 use_operand_p use_p;
931 ssa_op_iter op_iter;
932 FOR_EACH_SSA_USE_OPERAND (use_p, copy, op_iter, SSA_OP_USE)
933 {
934 tree old_name = USE_FROM_PTR (use_p);
935
936 if (dump_file)
937 {
938 fprintf (dump_file, "\n[codegen] renaming old_name = ");
939 print_generic_expr (dump_file, old_name, 0);
940 }
941
942 if (TREE_CODE (old_name) != SSA_NAME
943 || SSA_NAME_IS_DEFAULT_DEF (old_name))
944 continue;
945
946 changed = true;
947 tree new_expr = get_rename (region->rename_map, gsi_tgt->bb, old_name,
948 old_bb, false);
949
950 if (new_expr)
951 {
952 tree type_old_name = TREE_TYPE (old_name);
953 tree type_new_expr = TREE_TYPE (new_expr);
954
955 if (dump_file)
956 {
957 fprintf (dump_file, "\n[codegen] from rename_map: new_name = ");
958 print_generic_expr (dump_file, new_expr, 0);
959 }
960
961 if (type_old_name != type_new_expr
962 || TREE_CODE (new_expr) != SSA_NAME)
963 {
964 tree var = create_tmp_var (type_old_name, "var");
965
966 if (!useless_type_conversion_p (type_old_name, type_new_expr))
967 new_expr = fold_convert (type_old_name, new_expr);
968
969 gimple_seq stmts;
970 new_expr = force_gimple_operand (new_expr, &stmts, true, var);
971 gsi_insert_earliest (stmts, region);
972 }
973
974 replace_exp (use_p, new_expr);
975 continue;
976 }
977
978 gimple_seq stmts;
979 new_expr = get_rename_from_scev (old_name, &stmts, loop, gimple_bb (copy),
980 old_bb, iv_map, region, gloog_error);
981 if (!new_expr || *gloog_error)
982 return false;
983
984 if (dump_file)
985 {
986 fprintf (dump_file, "\n[codegen] not in rename map, scev: ");
987 print_generic_expr (dump_file, new_expr, 0);
988 }
989
990 gsi_insert_earliest (stmts, region);
991 replace_exp (use_p, new_expr);
992
993 if (TREE_CODE (new_expr) == INTEGER_CST
994 && is_gimple_assign (copy))
995 {
996 tree rhs = gimple_assign_rhs1 (copy);
997
998 if (TREE_CODE (rhs) == ADDR_EXPR)
999 recompute_tree_invariant_for_addr_expr (rhs);
1000 }
1001
1002 set_rename (old_name, new_expr, region);
1003 }
1004
1005 return changed;
1006 }
1007
1008 /* Returns a basic block that could correspond to where a constant was defined
1009 in the original code. In the original code OLD_BB had the definition, we
1010 need to find which basic block out of the copies of old_bb, in the new
1011 region, should a definition correspond to if it has to reach BB. */
1012
1013 static basic_block
1014 get_def_bb_for_const (sese_info_p region, basic_block bb, basic_block old_bb)
1015 {
1016 vec <basic_block> *bbs = region->copied_bb_map->get (old_bb);
1017
1018 if (!bbs || bbs->is_empty ())
1019 return NULL;
1020
1021 if (1 == bbs->length ())
1022 return (*bbs)[0];
1023
1024 int i;
1025 basic_block b1 = NULL, b2;
1026 FOR_EACH_VEC_ELT (*bbs, i, b2)
1027 {
1028 if (b2 == bb)
1029 return bb;
1030
1031 /* BB and B2 are in two unrelated if-clauses. */
1032 if (!dominated_by_p (CDI_DOMINATORS, bb, b2))
1033 continue;
1034
1035 /* Compute the nearest dominator. */
1036 if (!b1 || dominated_by_p (CDI_DOMINATORS, b2, b1))
1037 b1 = b2;
1038 }
1039
1040 gcc_assert (b1);
1041 return b1;
1042 }
1043
1044 /* LOOP_PHI is true when we want to rename an OP within a loop PHI
1045 instruction. */
1046
1047 static tree
1048 get_new_name (sese_info_p region, basic_block new_bb, tree op,
1049 basic_block old_bb, bool loop_phi)
1050 {
1051 if (TREE_CODE (op) == INTEGER_CST
1052 || TREE_CODE (op) == REAL_CST
1053 || TREE_CODE (op) == COMPLEX_CST
1054 || TREE_CODE (op) == VECTOR_CST)
1055 return op;
1056
1057 return get_rename (region->rename_map, new_bb, op, old_bb, loop_phi);
1058 }
1059
1060 /* Return a debug location for OP. */
1061
1062 static location_t
1063 get_loc (tree op)
1064 {
1065 location_t loc = UNKNOWN_LOCATION;
1066
1067 if (TREE_CODE (op) == SSA_NAME)
1068 loc = gimple_location (SSA_NAME_DEF_STMT (op));
1069 return loc;
1070 }
1071
1072 /* Returns the incoming edges of basic_block BB in the pair. The first edge is
1073 the init edge (from outside the loop) and the second one is the back edge
1074 from the same loop. */
1075
1076 std::pair<edge, edge>
1077 get_edges (basic_block bb)
1078 {
1079 std::pair<edge, edge> edges;
1080 edge e;
1081 edge_iterator ei;
1082 FOR_EACH_EDGE (e, ei, bb->preds)
1083 if (bb->loop_father != e->src->loop_father)
1084 edges.first = e;
1085 else
1086 edges.second = e;
1087 return edges;
1088 }
1089
1090 /* Copy the PHI arguments from OLD_PHI to the NEW_PHI. The arguments to NEW_PHI
1091 must be found unless they can be POSTPONEd for later. */
1092
1093 void
1094 copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
1095 gphi *new_phi, init_back_edge_pair_t &ibp_new_bb,
1096 sese_info_p region, bool postpone)
1097 {
1098 gcc_assert (gimple_phi_num_args (old_phi) == gimple_phi_num_args (new_phi));
1099
1100 basic_block new_bb = gimple_bb (new_phi);
1101 for (unsigned i = 0; i < gimple_phi_num_args (old_phi); i++)
1102 {
1103 edge e;
1104 if (gimple_phi_arg_edge (old_phi, i) == ibp_old_bb.first)
1105 e = ibp_new_bb.first;
1106 else
1107 e = ibp_new_bb.second;
1108
1109 tree old_name = gimple_phi_arg_def (old_phi, i);
1110 tree new_name = get_new_name (region, new_bb, old_name,
1111 gimple_bb (old_phi), true);
1112 if (new_name)
1113 {
1114 add_phi_arg (new_phi, new_name, e, get_loc (old_name));
1115 continue;
1116 }
1117
1118 gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
1119 if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
1120 /* If the phi arg was a function arg, or wasn't defined, just use the old
1121 name. */
1122 add_phi_arg (new_phi, old_name, e, get_loc (old_name));
1123 else if (postpone)
1124 {
1125 /* Postpone code gen for later for those back-edges we don't have the
1126 names yet. */
1127 region->incomplete_phis.safe_push (std::make_pair (old_phi, new_phi));
1128 if (dump_file)
1129 fprintf (dump_file, "\n[codegen] postpone loop phi nodes: ");
1130 }
1131 else
1132 /* Either we should add the arg to phi or, we should postpone. */
1133 gcc_unreachable ();
1134 }
1135 }
1136
1137 /* Copy loop phi nodes from BB to NEW_BB. */
1138
1139 static bool
1140 copy_loop_phi_nodes (basic_block bb, basic_block new_bb, sese_info_p region)
1141 {
1142 if (dump_file)
1143 fprintf (dump_file, "\n[codegen] copying loop phi nodes in bb_%d.",
1144 new_bb->index);
1145
1146 /* Loop phi nodes should have only two arguments. */
1147 gcc_assert (2 == EDGE_COUNT (bb->preds));
1148
1149 /* First edge is the init edge and second is the back edge. */
1150 init_back_edge_pair_t ibp_old_bb = get_edges (bb);
1151
1152 /* First edge is the init edge and second is the back edge. */
1153 init_back_edge_pair_t ibp_new_bb = get_edges (new_bb);
1154
1155 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1156 gsi_next (&psi))
1157 {
1158 gphi *phi = psi.phi ();
1159 tree res = gimple_phi_result (phi);
1160 if (virtual_operand_p (res))
1161 continue;
1162 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
1163 continue;
1164
1165 gphi *new_phi = create_phi_node (SSA_NAME_VAR (res), new_bb);
1166 tree new_res = create_new_def_for (res, new_phi,
1167 gimple_phi_result_ptr (new_phi));
1168 set_rename (res, new_res, region);
1169 copy_loop_phi_args (phi, ibp_old_bb, new_phi, ibp_new_bb, region, true);
1170 update_stmt (new_phi);
1171 }
1172
1173 return true;
1174 }
1175
1176 /* Return the init value of PHI, the value coming from outside the loop. */
1177
1178 static tree
1179 get_loop_init_value (gphi *phi)
1180 {
1181
1182 loop_p loop = gimple_bb (phi)->loop_father;
1183
1184 edge e;
1185 edge_iterator ei;
1186 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
1187 if (e->src->loop_father != loop)
1188 return gimple_phi_arg_def (phi, e->dest_idx);
1189
1190 return NULL_TREE;
1191 }
1192
1193 /* Find the init value (the value which comes from outside the loop), of one of
1194 the operands of DEF which is defined by a loop phi. */
1195
1196 static tree
1197 find_init_value (gimple *def)
1198 {
1199 if (gimple_code (def) == GIMPLE_PHI)
1200 return get_loop_init_value (as_a <gphi*> (def));
1201
1202 if (gimple_vuse (def))
1203 return NULL_TREE;
1204
1205 ssa_op_iter iter;
1206 use_operand_p use_p;
1207 FOR_EACH_SSA_USE_OPERAND (use_p, def, iter, SSA_OP_USE)
1208 {
1209 tree use = USE_FROM_PTR (use_p);
1210 if (TREE_CODE (use) == SSA_NAME)
1211 {
1212 if (tree res = find_init_value (SSA_NAME_DEF_STMT (use)))
1213 return res;
1214 }
1215 }
1216
1217 return NULL_TREE;
1218 }
1219
1220 /* Return the init value, the value coming from outside the loop. */
1221
1222 static tree
1223 find_init_value_close_phi (gphi *phi)
1224 {
1225 gcc_assert (gimple_phi_num_args (phi) == 1);
1226 tree use_arg = gimple_phi_arg_def (phi, 0);
1227 gimple *def = SSA_NAME_DEF_STMT (use_arg);
1228 return find_init_value (def);
1229 }
1230
1231 /* Copy all the loop-close phi args from BB to NEW_BB. */
1232
1233 bool
1234 copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb,
1235 sese_info_p region, bool postpone)
1236 {
1237 /* The successor of bb having close phi should be a merge of the diamond
1238 inserted to guard the loop during codegen. */
1239 basic_block close_phi_merge_bb = single_succ (new_bb);
1240
1241 for (gphi_iterator psi = gsi_start_phis (old_bb); !gsi_end_p (psi);
1242 gsi_next (&psi))
1243 {
1244 gphi *phi = psi.phi ();
1245 tree res = gimple_phi_result (phi);
1246 if (virtual_operand_p (res))
1247 continue;
1248
1249 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
1250 /* Loop close phi nodes should not be scev_analyzable_p. */
1251 gcc_unreachable ();
1252
1253 gphi *new_phi = create_phi_node (SSA_NAME_VAR (res), new_bb);
1254 tree new_res = create_new_def_for (res, new_phi,
1255 gimple_phi_result_ptr (new_phi));
1256 set_rename (res, new_res, region);
1257
1258 tree old_name = gimple_phi_arg_def (phi, 0);
1259 tree new_name = get_new_name (region, new_bb, old_name, old_bb, false);
1260
1261 /* Predecessor basic blocks of a loop close phi should have been code
1262 generated before. FIXME: This is fixable by merging PHIs from inner
1263 loops as well. When we are looking at close-phi of an outer loop, and
1264 arguments flowing out of inner loop as not been collected by the
1265 outer-loop close phi, we will hit this situation. For now we just bail
1266 out. See: gfortran.dg/graphite/interchange-3.f90. */
1267 if (!new_name)
1268 return false;
1269
1270 add_phi_arg (new_phi, new_name, single_pred_edge (new_bb),
1271 get_loc (old_name));
1272 if (dump_file)
1273 {
1274 fprintf (dump_file, "\n[codegen] Adding loop-closed phi: ");
1275 print_gimple_stmt (dump_file, new_phi, 0, 0);
1276 }
1277
1278 update_stmt (new_phi);
1279
1280 /* When there is no loop guard around this codegenerated loop, there is no
1281 need to collect the close-phi arg. */
1282 if (2 != EDGE_COUNT (close_phi_merge_bb->preds))
1283 continue;
1284
1285 /* Add a PHI in the close_phi_merge_bb for each close phi of the loop. */
1286 tree init = find_init_value_close_phi (new_phi);
1287
1288 /* A close phi must come from a loop-phi having an init value. */
1289 if (!init)
1290 {
1291 gcc_assert (postpone);
1292 region->incomplete_phis.safe_push (std::make_pair (phi, new_phi));
1293 if (dump_file)
1294 {
1295 fprintf (dump_file, "\n[codegen] postpone close phi nodes: ");
1296 print_gimple_stmt (dump_file, new_phi, 0, 0);
1297 }
1298 continue;
1299 }
1300
1301 gphi *merge_phi = create_phi_node (SSA_NAME_VAR (res),
1302 close_phi_merge_bb);
1303 tree merge_res = create_new_def_for (res, merge_phi,
1304 gimple_phi_result_ptr (merge_phi));
1305 set_rename (res, merge_res, region);
1306
1307 edge from_loop = single_succ_edge (new_bb);
1308 add_phi_arg (merge_phi, new_res, from_loop, get_loc (old_name));
1309
1310 /* The edge coming from loop guard. */
1311 edge other = from_loop == (*close_phi_merge_bb->preds)[0]
1312 ? (*close_phi_merge_bb->preds)[1] : (*close_phi_merge_bb->preds)[0];
1313
1314 add_phi_arg (merge_phi, init, other, get_loc (old_name));
1315 if (dump_file)
1316 {
1317 fprintf (dump_file, "\n[codegen] Adding guard-phi: ");
1318 print_gimple_stmt (dump_file, merge_phi, 0, 0);
1319 }
1320
1321 update_stmt (new_phi);
1322 }
1323
1324 return true;
1325 }
1326
1327 /* Copy loop close phi nodes from BB to NEW_BB. */
1328
1329 static bool
1330 copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb,
1331 sese_info_p region)
1332 {
1333 if (dump_file)
1334 fprintf (dump_file, "\n[codegen] copying loop closed phi nodes in bb_%d.",
1335 new_bb->index);
1336 /* Loop close phi nodes should have only one argument. */
1337 gcc_assert (1 == EDGE_COUNT (old_bb->preds));
1338
1339 return copy_loop_close_phi_args (old_bb, new_bb, region, true);
1340 }
1341
1342
1343 /* Add NEW_NAME as the ARGNUM-th arg of NEW_PHI which is in NEW_BB.
1344 DOMINATING_PRED is the predecessor basic block of OLD_BB which dominates the
1345 other pred of OLD_BB as well. If no such basic block exists then it is NULL.
1346 NON_DOMINATING_PRED is a pred which does not dominate OLD_BB, it cannot be
1347 NULL.
1348
1349 Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice versa.
1350 In this case DOMINATING_PRED = NULL.
1351
1352 Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2.
1353
1354 Returns true on successful copy of the args, false otherwise. */
1355
1356 static bool
1357 add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
1358 edge old_bb_dominating_edge,
1359 edge old_bb_non_dominating_edge,
1360 gphi *phi, gphi *new_phi,
1361 basic_block new_bb, sese_info_p region)
1362 {
1363 basic_block def_pred[2];
1364 int not_found_bb_index = -1;
1365 for (int i = 0; i < 2; i++)
1366 {
1367 /* If the corresponding def_bb could not be found the entry will be
1368 NULL. */
1369 if (TREE_CODE (old_phi_args[i]) == INTEGER_CST)
1370 def_pred[i] = get_def_bb_for_const (region, new_bb,
1371 gimple_phi_arg_edge (phi, i)->src);
1372 else
1373 def_pred[i] = gimple_bb (SSA_NAME_DEF_STMT (new_phi_args[i]));
1374 if (!def_pred[i])
1375 {
1376 gcc_assert (not_found_bb_index == -1);
1377 not_found_bb_index = i;
1378 }
1379 }
1380
1381 /* Here we are pattern matching on the structure of CFG w.r.t. old one. */
1382 if (old_bb_dominating_edge)
1383 {
1384 return false;
1385 basic_block new_pred1 = (*new_bb->preds)[0]->src;
1386 basic_block new_pred2 = (*new_bb->preds)[1]->src;
1387 vec <basic_block> *bbs
1388 = region->copied_bb_map->get (old_bb_non_dominating_edge->src);
1389 gcc_assert (bbs);
1390 basic_block new_pred = NULL;
1391 basic_block b;
1392 int i;
1393 FOR_EACH_VEC_ELT (*bbs, i, b)
1394 if (new_pred1 == b || new_pred2 == b)
1395 {
1396 gcc_assert (!new_pred);
1397 new_pred = b;
1398 }
1399
1400 gcc_assert (new_pred);
1401
1402 edge new_non_dominating_edge = find_edge (new_pred, new_bb);
1403 /* By the process of elimination we first insert insert phi-edge for
1404 non-dominating pred which is computed above and then we insert the
1405 remaining one. */
1406 int inserted_edge = 0;
1407 for (; inserted_edge < 2; inserted_edge++)
1408 {
1409 edge new_bb_pred_edge = gimple_phi_arg_edge (phi, inserted_edge);
1410 if (new_non_dominating_edge == new_bb_pred_edge)
1411 {
1412 add_phi_arg (new_phi, new_phi_args[inserted_edge],
1413 new_non_dominating_edge,
1414 get_loc (old_phi_args[inserted_edge]));
1415 break;
1416 }
1417 }
1418
1419 int edge_dominating = 0;
1420 if (inserted_edge == 0)
1421 edge_dominating = 1;
1422
1423 edge new_dominating_edge = NULL;
1424 for (int i; i < 2; i++)
1425 {
1426 edge e = gimple_phi_arg_edge (new_phi, i);
1427 if (e != new_non_dominating_edge)
1428 new_dominating_edge = e;
1429 }
1430
1431 add_phi_arg (new_phi, new_phi_args[edge_dominating], new_dominating_edge,
1432 get_loc (old_phi_args[inserted_edge]));
1433 }
1434 else
1435 {
1436 /* Classic diamond structure: both edges are non-dominating. We need to
1437 find one unique edge then the other can be found be elimination. If
1438 any definition (def_pred) dominates both the preds of new_bb then we
1439 bail out. Entries of def_pred maybe NULL, in that case we must
1440 uniquely find pred with help of only one entry. */
1441 edge new_e[2] = { NULL, NULL };
1442 for (int i = 0; i < 2; i++)
1443 {
1444 edge e;
1445 edge_iterator ei;
1446 FOR_EACH_EDGE (e, ei, new_bb->preds)
1447 if (def_pred[i]
1448 && dominated_by_p (CDI_DOMINATORS, e->src, def_pred[i]))
1449 {
1450 if (new_e[i])
1451 /* We do not know how to handle the case when def_pred
1452 dominates more than a predecessor. */
1453 return false;
1454 new_e[i] = e;
1455 }
1456 }
1457
1458 gcc_assert (new_e[0] || new_e[1]);
1459
1460 /* Find the other edge by process of elimination. */
1461 if (not_found_bb_index != -1)
1462 {
1463 gcc_assert (!new_e[not_found_bb_index]);
1464 int found_bb_index = not_found_bb_index == 1 ? 0 : 1;
1465 edge e;
1466 edge_iterator ei;
1467 FOR_EACH_EDGE (e, ei, new_bb->preds)
1468 {
1469 if (new_e[found_bb_index] == e)
1470 continue;
1471 new_e[not_found_bb_index] = e;
1472 }
1473 }
1474
1475 /* Add edges to phi args. */
1476 for (int i = 0; i < 2; i++)
1477 add_phi_arg (new_phi, new_phi_args[i], new_e[i],
1478 get_loc (old_phi_args[i]));
1479 }
1480
1481 return true;
1482 }
1483
1484 /* Copy the arguments of cond-phi node PHI, to NEW_PHI in the codegenerated
1485 region. If postpone is true and it isn't possible to copy any arg of PHI,
1486 the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated
1487 later. Returns false if the copying was unsuccessful. */
1488
1489 bool
1490 copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map,
1491 sese_info_p region, bool postpone)
1492 {
1493 if (dump_file)
1494 fprintf (dump_file, "\n[codegen] copying cond phi args: ");
1495 gcc_assert (2 == gimple_phi_num_args (phi));
1496
1497 basic_block new_bb = gimple_bb (new_phi);
1498 loop_p loop = gimple_bb (phi)->loop_father;
1499
1500 basic_block old_bb = gimple_bb (phi);
1501 edge old_bb_non_dominating_edge = NULL, old_bb_dominating_edge = NULL;
1502
1503 edge e;
1504 edge_iterator ei;
1505 FOR_EACH_EDGE (e, ei, old_bb->preds)
1506 if (!dominated_by_p (CDI_DOMINATORS, old_bb, e->src))
1507 old_bb_non_dominating_edge = e;
1508 else
1509 old_bb_dominating_edge = e;
1510
1511 gcc_assert (!dominated_by_p (CDI_DOMINATORS, old_bb,
1512 old_bb_non_dominating_edge->src));
1513
1514 tree new_phi_args[2];
1515 tree old_phi_args[2];
1516
1517 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
1518 {
1519 tree old_name = gimple_phi_arg_def (phi, i);
1520 tree new_name = get_new_name (region, new_bb, old_name, old_bb, false);
1521 old_phi_args[i] = old_name;
1522 if (new_name)
1523 {
1524 new_phi_args [i] = new_name;
1525 continue;
1526 }
1527
1528 if (vec_find (region->params, old_name))
1529 {
1530 new_phi_args [i] = old_name;
1531 if (dump_file)
1532 {
1533 fprintf (dump_file,
1534 "\n[codegen] parameter argument to phi, new_expr: ");
1535 print_gimple_stmt (dump_file, new_phi, 0, 0);
1536 }
1537 continue;
1538 }
1539
1540 /* If the phi-arg is scev-analyzeable but only in the first stage. */
1541 if (postpone && is_gimple_reg (old_name)
1542 && scev_analyzable_p (old_name, region->region))
1543 {
1544 gimple_seq stmts;
1545 bool gloog_error = false;
1546 tree new_expr
1547 = get_rename_from_scev (old_name, &stmts, loop, new_bb,
1548 old_bb, iv_map, region, &gloog_error);
1549 if (gloog_error)
1550 return false;
1551
1552 gcc_assert (new_expr);
1553 if (dump_file)
1554 {
1555 fprintf (dump_file, "\n[codegen] scev analyzeable, new_expr: ");
1556 print_generic_expr (dump_file, new_expr, 0);
1557 }
1558 gsi_insert_earliest (stmts, region);
1559 new_phi_args [i] = new_name;
1560 continue;
1561 }
1562
1563 gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
1564 if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
1565 /* If the phi arg was a function arg, or wasn't defined, just use the
1566 old name. */
1567 gcc_unreachable ();
1568 else if (postpone)
1569 {
1570 /* Postpone code gen for later for back-edges. */
1571 region->incomplete_phis.safe_push (std::make_pair (phi, new_phi));
1572
1573 if (dump_file)
1574 {
1575 fprintf (dump_file, "\n[codegen] postpone cond phi nodes: ");
1576 print_gimple_stmt (dump_file, new_phi, 0, 0);
1577 }
1578
1579 new_phi_args [i] = NULL_TREE;
1580 continue;
1581 }
1582 else
1583 gcc_unreachable ();
1584 }
1585
1586 return add_phi_arg_for_new_expr (old_phi_args, new_phi_args,
1587 old_bb_dominating_edge,
1588 old_bb_non_dominating_edge,
1589 phi, new_phi, new_bb, region);
1590 }
1591
1592 /* Copy cond phi nodes from BB to NEW_BB. */
1593
1594 static bool
1595 copy_cond_phi_nodes (basic_block bb, basic_block new_bb, vec<tree> iv_map,
1596 sese_info_p region)
1597 {
1598
1599 gcc_assert (!bb_contains_loop_close_phi_nodes (bb));
1600
1601 if (dump_file)
1602 fprintf (dump_file, "\n[codegen] copying cond phi nodes in bb_%d:",
1603 new_bb->index);
1604
1605 /* Cond phi nodes should have exactly two arguments. */
1606 gcc_assert (2 == EDGE_COUNT (bb->preds));
1607
1608 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1609 gsi_next (&psi))
1610 {
1611 gphi *phi = psi.phi ();
1612 tree res = gimple_phi_result (phi);
1613 if (virtual_operand_p (res))
1614 continue;
1615 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
1616 /* Cond phi nodes should not be scev_analyzable_p. */
1617 gcc_unreachable ();
1618
1619 gphi *new_phi = create_phi_node (SSA_NAME_VAR (res), new_bb);
1620 tree new_res = create_new_def_for (res, new_phi,
1621 gimple_phi_result_ptr (new_phi));
1622 set_rename (res, new_res, region);
1623
1624 if (!copy_cond_phi_args (phi, new_phi, iv_map, region, true))
1625 return false;
1626
1627 update_stmt (new_phi);
1628 }
1629
1630 return true;
1631 }
1632
1633 /* Return true if STMT should be copied from region to the
1634 new code-generated region. LABELs, CONDITIONS, induction-variables
1635 and region parameters need not be copied. */
1636
1637 static bool
1638 should_copy_to_new_region (gimple *stmt, sese_info_p region)
1639 {
1640 /* Do not copy labels or conditions. */
1641 if (gimple_code (stmt) == GIMPLE_LABEL
1642 || gimple_code (stmt) == GIMPLE_COND)
1643 return false;
1644
1645 tree lhs;
1646 /* Do not copy induction variables. */
1647 if (is_gimple_assign (stmt)
1648 && (lhs = gimple_assign_lhs (stmt))
1649 && TREE_CODE (lhs) == SSA_NAME
1650 && is_gimple_reg (lhs)
1651 && scev_analyzable_p (lhs, region->region))
1652 return false;
1653
1654 return true;
1655 }
1656
1657 /* Create new names for all the definitions created by COPY and
1658 add replacement mappings for each new name. */
1659
1660 static void
1661 set_rename_for_each_def (gimple *stmt, sese_info_p region)
1662 {
1663 def_operand_p def_p;
1664 ssa_op_iter op_iter;
1665 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_ALL_DEFS)
1666 {
1667 tree old_name = DEF_FROM_PTR (def_p);
1668 tree new_name = create_new_def_for (old_name, stmt, def_p);
1669 set_rename (old_name, new_name, region);
1670 }
1671 }
1672
1673 /* Duplicates the statements of basic block BB into basic block NEW_BB
1674 and compute the new induction variables according to the IV_MAP.
1675 GLOOG_ERROR is set when the code generation cannot continue. */
1676 static bool
1677 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
1678 vec<tree> iv_map, sese_info_p region,
1679 bool *gloog_error)
1680 {
1681 /* Iterator poining to the place where new statement (s) will be inserted. */
1682 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
1683
1684 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1685 gsi_next (&gsi))
1686 {
1687 gimple *stmt = gsi_stmt (gsi);
1688 if (!should_copy_to_new_region (stmt, region))
1689 continue;
1690
1691 /* Create a new copy of STMT and duplicate STMT's virtual
1692 operands. */
1693 gimple *copy = gimple_copy (stmt);
1694 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
1695
1696 if (dump_file)
1697 {
1698 fprintf (dump_file, "\n[codegen] inserting statement: ");
1699 print_gimple_stmt (dump_file, copy, 0, 0);
1700 }
1701
1702 maybe_duplicate_eh_stmt (copy, stmt);
1703 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
1704
1705 /* Crete new names for each def in the copied stmt. */
1706 set_rename_for_each_def (copy, region);
1707
1708 loop_p loop = bb->loop_father;
1709 if (rename_uses (copy, &gsi_tgt, bb, region, loop, iv_map, gloog_error))
1710 {
1711 fold_stmt_inplace (&gsi_tgt);
1712 gcc_assert (gsi_stmt (gsi_tgt) == copy);
1713 }
1714
1715 if (*gloog_error)
1716 return false;
1717
1718 update_stmt (copy);
1719 }
1720
1721 return true;
1722 }
1723
1724 /* Copies BB and includes in the copied BB all the statements that can
1725 be reached following the use-def chains from the memory accesses,
1726 and returns the next edge following this new block. GLOOG_ERROR is
1727 set when the code generation cannot continue. */
1728
1729 edge
1730 copy_bb_and_scalar_dependences (basic_block bb, sese_info_p region,
1731 edge next_e, vec<tree> iv_map,
1732 bool *codegen_err)
1733 {
1734 int num_phis = number_of_phi_nodes (bb);
1735
1736 if (region->copied_bb_map->get (bb))
1737 {
1738 /* FIXME: We do not handle inner loop unrolling when the inner loop has
1739 phi-nodes. In that case inner loop will be copied multiple times
1740 outside the region. */
1741 if (num_phis)
1742 {
1743 *codegen_err = true;
1744 return NULL;
1745 }
1746 }
1747
1748 basic_block new_bb = split_edge (next_e);
1749 if (num_phis > 0 && bb_contains_loop_phi_nodes (bb))
1750 {
1751 basic_block phi_bb = next_e->dest->loop_father->header;
1752
1753 /* At this point we are unable to codegenerate by still preserving the SSA
1754 structure because maybe the loop is completely unrolled and the PHIs
1755 and cross-bb scalar dependencies are untrackable w.r.t. the original
1756 code. See gfortran.dg/graphite/pr29832.f90. */
1757 if (EDGE_COUNT (bb->preds) != EDGE_COUNT (phi_bb->preds))
1758 {
1759 *codegen_err = true;
1760 return NULL;
1761 }
1762
1763 if (dump_file)
1764 fprintf (dump_file, "\n[codegen] bb_%d contains loop phi nodes",
1765 bb->index);
1766 if (!copy_loop_phi_nodes (bb, phi_bb, region))
1767 {
1768 *codegen_err = true;
1769 return NULL;
1770 }
1771 }
1772 else if (bb_contains_loop_close_phi_nodes (bb))
1773 {
1774 if (dump_file)
1775 fprintf (dump_file, "\n[codegen] bb_%d contains close phi nodes",
1776 bb->index);
1777
1778 /* Make sure that NEW_BB is the loop->exit->dest. */
1779 edge e = single_pred_edge (new_bb);
1780 basic_block phi_bb = new_bb;
1781 if (e->src->loop_father == e->dest->loop_father)
1782 {
1783 /* This is one of the places which shows preserving original structure
1784 is not always possible, as we may need to insert close PHI for a
1785 loop where the latch does not have any mapping, or the mapping is
1786 ambiguous. */
1787 basic_block old_loop_bb = single_pred_edge (bb)->src;
1788 vec <basic_block> *bbs = region->copied_bb_map->get (old_loop_bb);
1789 if (!bbs || bbs->length () != 1)
1790 {
1791 *codegen_err = true;
1792 return NULL;
1793 }
1794
1795 basic_block new_loop_bb = (*bbs)[0];
1796 loop_p new_loop = new_loop_bb->loop_father;
1797 phi_bb = single_exit (new_loop)->dest;
1798 e = single_pred_edge (phi_bb);
1799 }
1800
1801 gcc_assert (e->src->loop_father != e->dest->loop_father);
1802
1803 if (!copy_loop_close_phi_nodes (bb, phi_bb, region))
1804 {
1805 *codegen_err = true;
1806 return NULL;
1807 }
1808 }
1809 else if (num_phis > 0)
1810 {
1811 if (dump_file)
1812 fprintf (dump_file, "\n[codegen] bb_%d contains cond phi nodes",
1813 bb->index);
1814
1815 basic_block phi_bb = single_pred (new_bb);
1816 loop_p loop_father = new_bb->loop_father;
1817
1818 /* Move back until we find the block with two predecessors. */
1819 while (single_pred_p (phi_bb))
1820 phi_bb = single_pred_edge (phi_bb)->src;
1821
1822 /* If a corresponding merge-point was not found, then abort codegen. */
1823 if (phi_bb->loop_father != loop_father
1824 || !copy_cond_phi_nodes (bb, phi_bb, iv_map, region))
1825 {
1826 *codegen_err = true;
1827 return NULL;
1828 }
1829 }
1830
1831 if (dump_file)
1832 fprintf (dump_file, "\n[codegen] copying from bb_%d to bb_%d",
1833 bb->index, new_bb->index);
1834
1835 vec <basic_block> *copied_bbs = region->copied_bb_map->get (bb);
1836 if (copied_bbs)
1837 copied_bbs->safe_push (new_bb);
1838 else
1839 {
1840 vec<basic_block> bbs;
1841 bbs.create (2);
1842 bbs.safe_push (new_bb);
1843 region->copied_bb_map->put (bb, bbs);
1844 }
1845
1846 if (!graphite_copy_stmts_from_block (bb, new_bb, iv_map, region, codegen_err))
1847 {
1848 *codegen_err = true;
1849 return NULL;
1850 }
1851
1852 return single_succ_edge (new_bb);
1853 }
1854
1855 /* Returns the outermost loop in SCOP that contains BB. */
1856
1857 struct loop *
1858 outermost_loop_in_sese_1 (sese_l &region, basic_block bb)
1859 {
1860 struct loop *nest;
1861
1862 nest = bb->loop_father;
1863 while (loop_outer (nest)
1864 && loop_in_sese_p (loop_outer (nest), region))
1865 nest = loop_outer (nest);
1866
1867 return nest;
1868 }
1869
1870 /* Same as outermost_loop_in_sese_1, returns the outermost loop
1871 containing BB in REGION, but makes sure that the returned loop
1872 belongs to the REGION, and so this returns the first loop in the
1873 REGION when the loop containing BB does not belong to REGION. */
1874
1875 loop_p
1876 outermost_loop_in_sese (sese_l &region, basic_block bb)
1877 {
1878 loop_p nest = outermost_loop_in_sese_1 (region, bb);
1879
1880 if (loop_in_sese_p (nest, region))
1881 return nest;
1882
1883 /* When the basic block BB does not belong to a loop in the region,
1884 return the first loop in the region. */
1885 nest = nest->inner;
1886 while (nest)
1887 if (loop_in_sese_p (nest, region))
1888 break;
1889 else
1890 nest = nest->next;
1891
1892 gcc_assert (nest);
1893 return nest;
1894 }
1895
1896 /* Sets the false region of an IF_REGION to REGION. */
1897
1898 void
1899 if_region_set_false_region (ifsese if_region, sese_info_p region)
1900 {
1901 basic_block condition = if_region_get_condition_block (if_region);
1902 edge false_edge = get_false_edge_from_guard_bb (condition);
1903 basic_block dummy = false_edge->dest;
1904 edge entry_region = region->region.entry;
1905 edge exit_region = region->region.exit;
1906 basic_block before_region = entry_region->src;
1907 basic_block last_in_region = exit_region->src;
1908 hashval_t hash = htab_hash_pointer (exit_region);
1909 loop_exit **slot
1910 = current_loops->exits->find_slot_with_hash (exit_region, hash, NO_INSERT);
1911
1912 entry_region->flags = false_edge->flags;
1913 false_edge->flags = exit_region->flags;
1914
1915 redirect_edge_pred (entry_region, condition);
1916 redirect_edge_pred (exit_region, before_region);
1917 redirect_edge_pred (false_edge, last_in_region);
1918 redirect_edge_succ (false_edge, single_succ (dummy));
1919 delete_basic_block (dummy);
1920
1921 exit_region->flags = EDGE_FALLTHRU;
1922 recompute_all_dominators ();
1923
1924 region->region.exit = false_edge;
1925
1926 free (if_region->false_region);
1927 if_region->false_region = region;
1928
1929 if (slot)
1930 {
1931 struct loop_exit *loop_exit = ggc_cleared_alloc<struct loop_exit> ();
1932
1933 memcpy (loop_exit, *((struct loop_exit **) slot), sizeof (struct loop_exit));
1934 current_loops->exits->clear_slot (slot);
1935
1936 hashval_t hash = htab_hash_pointer (false_edge);
1937 slot = current_loops->exits->find_slot_with_hash (false_edge, hash,
1938 INSERT);
1939 loop_exit->e = false_edge;
1940 *slot = loop_exit;
1941 false_edge->src->loop_father->exits->next = loop_exit;
1942 }
1943 }
1944
1945 /* Creates an IFSESE with CONDITION on edge ENTRY. */
1946
1947 static ifsese
1948 create_if_region_on_edge (edge entry, tree condition)
1949 {
1950 edge e;
1951 edge_iterator ei;
1952 sese_info_p sese_region = XNEW (struct sese_info_t);
1953 sese_info_p true_region = XNEW (struct sese_info_t);
1954 sese_info_p false_region = XNEW (struct sese_info_t);
1955 ifsese if_region = XNEW (struct ifsese_s);
1956 edge exit = create_empty_if_region_on_edge (entry, condition);
1957
1958 if_region->region = sese_region;
1959 if_region->region->region.entry = entry;
1960 if_region->region->region.exit = exit;
1961
1962 FOR_EACH_EDGE (e, ei, entry->dest->succs)
1963 {
1964 if (e->flags & EDGE_TRUE_VALUE)
1965 {
1966 true_region->region.entry = e;
1967 true_region->region.exit = single_succ_edge (e->dest);
1968 if_region->true_region = true_region;
1969 }
1970 else if (e->flags & EDGE_FALSE_VALUE)
1971 {
1972 false_region->region.entry = e;
1973 false_region->region.exit = single_succ_edge (e->dest);
1974 if_region->false_region = false_region;
1975 }
1976 }
1977
1978 return if_region;
1979 }
1980
1981 /* Moves REGION in a condition expression:
1982 | if (1)
1983 | ;
1984 | else
1985 | REGION;
1986 */
1987
1988 ifsese
1989 move_sese_in_condition (sese_info_p region)
1990 {
1991 basic_block pred_block = split_edge (region->region.entry);
1992 ifsese if_region;
1993
1994 region->region.entry = single_succ_edge (pred_block);
1995 if_region = create_if_region_on_edge (single_pred_edge (pred_block),
1996 integer_one_node);
1997 if_region_set_false_region (if_region, region);
1998
1999 return if_region;
2000 }
2001
2002 /* Replaces the condition of the IF_REGION with CONDITION:
2003 | if (CONDITION)
2004 | true_region;
2005 | else
2006 | false_region;
2007 */
2008
2009 void
2010 set_ifsese_condition (ifsese if_region, tree condition)
2011 {
2012 sese_info_p region = if_region->region;
2013 edge entry = region->region.entry;
2014 basic_block bb = entry->dest;
2015 gimple *last = last_stmt (bb);
2016 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2017 gcond *cond_stmt;
2018
2019 gcc_assert (gimple_code (last) == GIMPLE_COND);
2020
2021 gsi_remove (&gsi, true);
2022 gsi = gsi_last_bb (bb);
2023 condition = force_gimple_operand_gsi (&gsi, condition, true, NULL,
2024 false, GSI_NEW_STMT);
2025 cond_stmt = gimple_build_cond_from_tree (condition, NULL_TREE, NULL_TREE);
2026 gsi = gsi_last_bb (bb);
2027 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
2028 }
2029
2030 /* Return true when T is defined outside REGION or when no definitions are
2031 variant in REGION. When HAS_VDEFS is a valid pointer, sets HAS_VDEFS to true
2032 when T depends on memory that may change in REGION. */
2033
2034 bool
2035 invariant_in_sese_p_rec (tree t, sese_l &region, bool *has_vdefs)
2036 {
2037 if (!defined_in_sese_p (t, region))
2038 return true;
2039
2040 gimple *stmt = SSA_NAME_DEF_STMT (t);
2041
2042 if (gimple_code (stmt) == GIMPLE_PHI
2043 || gimple_code (stmt) == GIMPLE_CALL)
2044 return false;
2045
2046 /* VDEF is variant when it is in the region. */
2047 if (gimple_vdef (stmt))
2048 {
2049 if (has_vdefs)
2050 *has_vdefs = true;
2051 return false;
2052 }
2053
2054 /* A VUSE may or may not be variant following the VDEFs. */
2055 if (tree vuse = gimple_vuse (stmt))
2056 return invariant_in_sese_p_rec (vuse, region, has_vdefs);
2057
2058 ssa_op_iter iter;
2059 use_operand_p use_p;
2060 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
2061 {
2062 tree use = USE_FROM_PTR (use_p);
2063
2064 if (!defined_in_sese_p (use, region))
2065 continue;
2066
2067 if (!invariant_in_sese_p_rec (use, region, has_vdefs))
2068 return false;
2069 }
2070
2071 return true;
2072 }
2073
2074 /* Returns the scalar evolution of T in REGION. Every variable that
2075 is not defined in the REGION is considered a parameter. */
2076
2077 tree
2078 scalar_evolution_in_region (sese_l &region, loop_p loop, tree t)
2079 {
2080 gimple *def;
2081 struct loop *def_loop;
2082 basic_block before = region.entry->src;
2083
2084 /* SCOP parameters. */
2085 if (TREE_CODE (t) == SSA_NAME
2086 && !defined_in_sese_p (t, region))
2087 return t;
2088
2089 if (TREE_CODE (t) != SSA_NAME
2090 || loop_in_sese_p (loop, region))
2091 return instantiate_scev (before, loop,
2092 analyze_scalar_evolution (loop, t));
2093
2094 def = SSA_NAME_DEF_STMT (t);
2095 def_loop = loop_containing_stmt (def);
2096
2097 if (loop_in_sese_p (def_loop, region))
2098 {
2099 t = analyze_scalar_evolution (def_loop, t);
2100 def_loop = superloop_at_depth (def_loop, loop_depth (loop) + 1);
2101 t = compute_overall_effect_of_inner_loop (def_loop, t);
2102 return t;
2103 }
2104
2105 bool has_vdefs = false;
2106 if (invariant_in_sese_p_rec (t, region, &has_vdefs))
2107 return t;
2108
2109 /* T variates in REGION. */
2110 if (has_vdefs)
2111 return chrec_dont_know;
2112
2113 return instantiate_scev (before, loop, t);
2114 }