OSDN Git Service

PR debug/41888
[pf3gnuchains/gcc-fork.git] / gcc / sese.c
1 /* Single entry single exit control flow regions.
2    Copyright (C) 2008, 2009  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 "tm.h"
26 #include "ggc.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "toplev.h"
33 #include "tree-dump.h"
34 #include "timevar.h"
35 #include "cfgloop.h"
36 #include "tree-chrec.h"
37 #include "tree-data-ref.h"
38 #include "tree-scalar-evolution.h"
39 #include "tree-pass.h"
40 #include "domwalk.h"
41 #include "value-prof.h"
42 #include "pointer-set.h"
43 #include "gimple.h"
44 #include "sese.h"
45
46 /* Print to stderr the element ELT.  */
47
48 static void
49 debug_rename_elt (rename_map_elt elt)
50 {
51   fprintf (stderr, "(");
52   print_generic_expr (stderr, elt->old_name, 0);
53   fprintf (stderr, ", ");
54   print_generic_expr (stderr, elt->expr, 0);
55   fprintf (stderr, ")\n");
56 }
57
58 /* Helper function for debug_rename_map.  */
59
60 static int
61 debug_rename_map_1 (void **slot, void *s ATTRIBUTE_UNUSED)
62 {
63   struct rename_map_elt_s *entry = (struct rename_map_elt_s *) *slot;
64   debug_rename_elt (entry);
65   return 1;
66 }
67
68 /* Print to stderr all the elements of MAP.  */
69
70 void
71 debug_rename_map (htab_t map)
72 {
73   htab_traverse (map, debug_rename_map_1, NULL);
74 }
75
76 /* Computes a hash function for database element ELT.  */
77
78 hashval_t
79 rename_map_elt_info (const void *elt)
80 {
81   return htab_hash_pointer (((const struct rename_map_elt_s *) elt)->old_name);
82 }
83
84 /* Compares database elements E1 and E2.  */
85
86 int
87 eq_rename_map_elts (const void *e1, const void *e2)
88 {
89   const struct rename_map_elt_s *elt1 = (const struct rename_map_elt_s *) e1;
90   const struct rename_map_elt_s *elt2 = (const struct rename_map_elt_s *) e2;
91
92   return (elt1->old_name == elt2->old_name);
93 }
94
95 \f
96
97 /* Print to stderr the element ELT.  */
98
99 static void
100 debug_ivtype_elt (ivtype_map_elt elt)
101 {
102   fprintf (stderr, "(%s, ", elt->cloog_iv);
103   print_generic_expr (stderr, elt->type, 0);
104   fprintf (stderr, ")\n");
105 }
106
107 /* Helper function for debug_ivtype_map.  */
108
109 static int
110 debug_ivtype_map_1 (void **slot, void *s ATTRIBUTE_UNUSED)
111 {
112   struct ivtype_map_elt_s *entry = (struct ivtype_map_elt_s *) *slot;
113   debug_ivtype_elt (entry);
114   return 1;
115 }
116
117 /* Print to stderr all the elements of MAP.  */
118
119 void
120 debug_ivtype_map (htab_t map)
121 {
122   htab_traverse (map, debug_ivtype_map_1, NULL);
123 }
124
125 /* Computes a hash function for database element ELT.  */
126
127 hashval_t
128 ivtype_map_elt_info (const void *elt)
129 {
130   return htab_hash_pointer (((const struct ivtype_map_elt_s *) elt)->cloog_iv);
131 }
132
133 /* Compares database elements E1 and E2.  */
134
135 int
136 eq_ivtype_map_elts (const void *e1, const void *e2)
137 {
138   const struct ivtype_map_elt_s *elt1 = (const struct ivtype_map_elt_s *) e1;
139   const struct ivtype_map_elt_s *elt2 = (const struct ivtype_map_elt_s *) e2;
140
141   return (elt1->cloog_iv == elt2->cloog_iv);
142 }
143
144 \f
145
146 /* Record LOOP as occuring in REGION.  */
147
148 static void
149 sese_record_loop (sese region, loop_p loop)
150 {
151   if (sese_contains_loop (region, loop))
152     return;
153
154   bitmap_set_bit (SESE_LOOPS (region), loop->num);
155   VEC_safe_push (loop_p, heap, SESE_LOOP_NEST (region), loop);
156 }
157
158 /* Build the loop nests contained in REGION.  Returns true when the
159    operation was successful.  */
160
161 void
162 build_sese_loop_nests (sese region)
163 {
164   unsigned i;
165   basic_block bb;
166   struct loop *loop0, *loop1;
167
168   FOR_EACH_BB (bb)
169     if (bb_in_sese_p (bb, region))
170       {
171         struct loop *loop = bb->loop_father;
172
173         /* Only add loops if they are completely contained in the SCoP.  */
174         if (loop->header == bb
175             && bb_in_sese_p (loop->latch, region))
176           sese_record_loop (region, loop);
177       }
178
179   /* Make sure that the loops in the SESE_LOOP_NEST are ordered.  It
180      can be the case that an inner loop is inserted before an outer
181      loop.  To avoid this, semi-sort once.  */
182   for (i = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), i, loop0); i++)
183     {
184       if (VEC_length (loop_p, SESE_LOOP_NEST (region)) == i + 1)
185         break;
186
187       loop1 = VEC_index (loop_p, SESE_LOOP_NEST (region), i + 1);
188       if (loop0->num > loop1->num)
189         {
190           VEC_replace (loop_p, SESE_LOOP_NEST (region), i, loop1);
191           VEC_replace (loop_p, SESE_LOOP_NEST (region), i + 1, loop0);
192         }
193     }
194 }
195
196 /* For a USE in BB, if BB is outside REGION, mark the USE in the
197    LIVEOUTS set.  */
198
199 static void
200 sese_build_liveouts_use (sese region, bitmap liveouts, basic_block bb,
201                          tree use)
202 {
203   unsigned ver;
204   basic_block def_bb;
205
206   if (TREE_CODE (use) != SSA_NAME)
207     return;
208
209   ver = SSA_NAME_VERSION (use);
210   def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
211
212   if (!def_bb
213       || !bb_in_sese_p (def_bb, region)
214       || bb_in_sese_p (bb, region))
215     return;
216
217   bitmap_set_bit (liveouts, ver);
218 }
219
220 /* Marks for rewrite all the SSA_NAMES defined in REGION and that are
221    used in BB that is outside of the REGION.  */
222
223 static void
224 sese_build_liveouts_bb (sese region, bitmap liveouts, basic_block bb)
225 {
226   gimple_stmt_iterator bsi;
227   edge e;
228   edge_iterator ei;
229   ssa_op_iter iter;
230   use_operand_p use_p;
231
232   FOR_EACH_EDGE (e, ei, bb->succs)
233     for (bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi); gsi_next (&bsi))
234       sese_build_liveouts_use (region, liveouts, bb,
235                                PHI_ARG_DEF_FROM_EDGE (gsi_stmt (bsi), e));
236
237   for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
238     {
239       gimple stmt = gsi_stmt (bsi);
240
241       if (is_gimple_debug (stmt))
242         continue;
243
244       FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
245         sese_build_liveouts_use (region, liveouts, bb, USE_FROM_PTR (use_p));
246     }
247 }
248
249 /* For a USE in BB, return true if BB is outside REGION and it's not
250    in the LIVEOUTS set.  */
251
252 static bool
253 sese_bad_liveouts_use (sese region, bitmap liveouts, basic_block bb,
254                        tree use)
255 {
256   unsigned ver;
257   basic_block def_bb;
258
259   if (TREE_CODE (use) != SSA_NAME)
260     return false;
261
262   ver = SSA_NAME_VERSION (use);
263
264   /* If it's in liveouts, the variable will get a new PHI node, and
265      the debug use will be properly adjusted.  */
266   if (bitmap_bit_p (liveouts, ver))
267     return false;
268
269   def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
270
271   if (!def_bb
272       || !bb_in_sese_p (def_bb, region)
273       || bb_in_sese_p (bb, region))
274     return false;
275
276   return true;
277 }
278
279 /* Reset debug stmts that reference SSA_NAMES defined in REGION that
280    are not marked as liveouts.  */
281
282 static void
283 sese_reset_debug_liveouts_bb (sese region, bitmap liveouts, basic_block bb)
284 {
285   gimple_stmt_iterator bsi;
286   ssa_op_iter iter;
287   use_operand_p use_p;
288
289   for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
290     {
291       gimple stmt = gsi_stmt (bsi);
292
293       if (!is_gimple_debug (stmt))
294         continue;
295
296       FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
297         if (sese_bad_liveouts_use (region, liveouts, bb,
298                                    USE_FROM_PTR (use_p)))
299           {
300             gimple_debug_bind_reset_value (stmt);
301             update_stmt (stmt);
302             break;
303           }
304     }
305 }
306
307 /* Build the LIVEOUTS of REGION: the set of variables defined inside
308    and used outside the REGION.  */
309
310 static void
311 sese_build_liveouts (sese region, bitmap liveouts)
312 {
313   basic_block bb;
314
315   FOR_EACH_BB (bb)
316     sese_build_liveouts_bb (region, liveouts, bb);
317   if (MAY_HAVE_DEBUG_INSNS)
318     FOR_EACH_BB (bb)
319       sese_reset_debug_liveouts_bb (region, liveouts, bb);
320 }
321
322 /* Builds a new SESE region from edges ENTRY and EXIT.  */
323
324 sese
325 new_sese (edge entry, edge exit)
326 {
327   sese region = XNEW (struct sese_s);
328
329   SESE_ENTRY (region) = entry;
330   SESE_EXIT (region) = exit;
331   SESE_LOOPS (region) = BITMAP_ALLOC (NULL);
332   SESE_LOOP_NEST (region) = VEC_alloc (loop_p, heap, 3);
333   SESE_ADD_PARAMS (region) = true;
334   SESE_PARAMS (region) = VEC_alloc (tree, heap, 3);
335   SESE_PARAMS_INDEX (region) = htab_create (10, clast_name_index_elt_info,
336                                             eq_clast_name_indexes, free);
337   SESE_PARAMS_NAMES (region) = XNEWVEC (char *, num_ssa_names);
338
339   return region;
340 }
341
342 /* Deletes REGION.  */
343
344 void
345 free_sese (sese region)
346 {
347   if (SESE_LOOPS (region))
348     SESE_LOOPS (region) = BITMAP_ALLOC (NULL);
349
350   VEC_free (tree, heap, SESE_PARAMS (region));
351   VEC_free (loop_p, heap, SESE_LOOP_NEST (region)); 
352
353   if (SESE_PARAMS_INDEX (region))
354     htab_delete (SESE_PARAMS_INDEX (region));
355
356   /* Do not free SESE_PARAMS_NAMES: CLooG does that.  */
357
358   XDELETE (region);
359 }
360
361 /* Add exit phis for USE on EXIT.  */
362
363 static void
364 sese_add_exit_phis_edge (basic_block exit, tree use, edge false_e, edge true_e)
365 {
366   gimple phi = create_phi_node (use, exit);
367
368   create_new_def_for (gimple_phi_result (phi), phi,
369                       gimple_phi_result_ptr (phi));
370   add_phi_arg (phi, use, false_e, UNKNOWN_LOCATION);
371   add_phi_arg (phi, use, true_e, UNKNOWN_LOCATION);
372 }
373
374 /* Insert in the block BB phi nodes for variables defined in REGION
375    and used outside the REGION.  The code generation moves REGION in
376    the else clause of an "if (1)" and generates code in the then
377    clause that is at this point empty:
378
379    | if (1)
380    |   empty;
381    | else
382    |   REGION;
383 */
384
385 void
386 sese_insert_phis_for_liveouts (sese region, basic_block bb,
387                                edge false_e, edge true_e)
388 {
389   unsigned i;
390   bitmap_iterator bi;
391   bitmap liveouts = BITMAP_ALLOC (NULL);
392
393   update_ssa (TODO_update_ssa);
394
395   sese_build_liveouts (region, liveouts);
396   EXECUTE_IF_SET_IN_BITMAP (liveouts, 0, i, bi)
397     sese_add_exit_phis_edge (bb, ssa_name (i), false_e, true_e);
398   BITMAP_FREE (liveouts);
399
400   update_ssa (TODO_update_ssa);
401 }
402
403 /* Get the definition of NAME before the SESE.  Keep track of the
404    basic blocks that have been VISITED in a bitmap.  */
405
406 static tree
407 get_vdef_before_sese (sese region, tree name, sbitmap visited)
408 {
409   unsigned i;
410   gimple def_stmt = SSA_NAME_DEF_STMT (name);
411   basic_block def_bb = gimple_bb (def_stmt);
412
413   if (!def_bb || !bb_in_sese_p (def_bb, region))
414     return name;
415
416   if (TEST_BIT (visited, def_bb->index))
417     return NULL_TREE;
418
419   SET_BIT (visited, def_bb->index);
420
421   switch (gimple_code (def_stmt))
422     {
423     case GIMPLE_PHI:
424       for (i = 0; i < gimple_phi_num_args (def_stmt); i++)
425         {
426           tree arg = gimple_phi_arg_def (def_stmt, i);
427           tree res = get_vdef_before_sese (region, arg, visited);
428           if (res)
429             return res;
430         }
431       return NULL_TREE;
432
433     default:
434       return NULL_TREE;
435     }
436 }
437
438 /* Adjust a virtual phi node PHI that is placed at the end of the
439    generated code for SCOP:
440
441    | if (1)
442    |   generated code from REGION;
443    | else
444    |   REGION;
445
446    The FALSE_E edge comes from the original code, TRUE_E edge comes
447    from the code generated for the SCOP.  */
448
449 static void
450 sese_adjust_vphi (sese region, gimple phi, edge true_e)
451 {
452   unsigned i;
453
454   gcc_assert (gimple_phi_num_args (phi) == 2);
455
456   for (i = 0; i < gimple_phi_num_args (phi); i++)
457     if (gimple_phi_arg_edge (phi, i) == true_e)
458       {
459         tree true_arg, false_arg, before_scop_arg;
460         sbitmap visited;
461
462         true_arg = gimple_phi_arg_def (phi, i);
463         if (!SSA_NAME_IS_DEFAULT_DEF (true_arg))
464           return;
465
466         false_arg = gimple_phi_arg_def (phi, i == 0 ? 1 : 0);
467         if (SSA_NAME_IS_DEFAULT_DEF (false_arg))
468           return;
469
470         visited = sbitmap_alloc (last_basic_block);
471         sbitmap_zero (visited);
472         before_scop_arg = get_vdef_before_sese (region, false_arg, visited);
473         gcc_assert (before_scop_arg != NULL_TREE);
474         SET_PHI_ARG_DEF (phi, i, before_scop_arg);
475         sbitmap_free (visited);
476       }
477 }
478
479 /* Returns the name associated to OLD_NAME in MAP.  */
480
481 static tree
482 get_rename (htab_t map, tree old_name)
483 {
484   struct rename_map_elt_s tmp;
485   PTR *slot;
486
487   tmp.old_name = old_name;
488   slot = htab_find_slot (map, &tmp, NO_INSERT);
489
490   if (slot && *slot)
491     return ((rename_map_elt) *slot)->expr;
492
493   return old_name;
494 }
495
496 /* Register in MAP the rename tuple (old_name, expr).  */
497
498 void
499 set_rename (htab_t map, tree old_name, tree expr)
500 {
501   struct rename_map_elt_s tmp;
502   PTR *slot;
503
504   if (old_name == expr)
505     return;
506
507   tmp.old_name = old_name;
508   slot = htab_find_slot (map, &tmp, INSERT);
509
510   if (!slot)
511     return;
512
513   if (*slot)
514     free (*slot);
515
516   *slot = new_rename_map_elt (old_name, expr);
517 }
518
519 /* Adjusts the phi nodes in the block BB for variables defined in
520    SCOP_REGION and used outside the SCOP_REGION.  The code generation
521    moves SCOP_REGION in the else clause of an "if (1)" and generates
522    code in the then clause:
523
524    | if (1)
525    |   generated code from REGION;
526    | else
527    |   REGION;
528
529    To adjust the phi nodes after the condition, the RENAME_MAP is
530    used.  */
531
532 void
533 sese_adjust_liveout_phis (sese region, htab_t rename_map, basic_block bb,
534                           edge false_e, edge true_e)
535 {
536   gimple_stmt_iterator si;
537
538   for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
539     {
540       unsigned i;
541       unsigned false_i = 0;
542       gimple phi = gsi_stmt (si);
543
544       if (!is_gimple_reg (PHI_RESULT (phi)))
545         {
546           sese_adjust_vphi (region, phi, true_e);
547           continue;
548         }
549
550       for (i = 0; i < gimple_phi_num_args (phi); i++)
551         if (gimple_phi_arg_edge (phi, i) == false_e)
552           {
553             false_i = i;
554             break;
555           }
556
557       for (i = 0; i < gimple_phi_num_args (phi); i++)
558         if (gimple_phi_arg_edge (phi, i) == true_e)
559           {
560             tree old_name = gimple_phi_arg_def (phi, false_i);
561             tree expr = get_rename (rename_map, old_name);
562             gimple_seq stmts;
563
564             gcc_assert (old_name != expr);
565
566             if (TREE_CODE (expr) != SSA_NAME
567                 && is_gimple_reg (old_name))
568               {
569                 tree type = TREE_TYPE (old_name);
570                 tree var = create_tmp_var (type, "var");
571
572                 expr = build2 (MODIFY_EXPR, type, var, expr);
573                 expr = force_gimple_operand (expr, &stmts, true, NULL);
574                 gsi_insert_seq_on_edge_immediate (true_e, stmts);
575               }
576
577             SET_PHI_ARG_DEF (phi, i, expr);
578           }
579     }
580 }
581
582 /* Rename the SSA_NAMEs used in STMT and that appear in MAP.  */
583
584 static void 
585 rename_variables_in_stmt (gimple stmt, htab_t map, gimple_stmt_iterator *insert_gsi)
586 {
587   ssa_op_iter iter;
588   use_operand_p use_p;
589
590   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
591     {
592       tree use = USE_FROM_PTR (use_p);
593       tree expr = get_rename (map, use);
594       tree type_use = TREE_TYPE (use);
595       tree type_expr = TREE_TYPE (expr);
596       gimple_seq stmts;
597
598       if (use == expr)
599         continue;
600
601       if (type_use != type_expr
602           || (TREE_CODE (expr) != SSA_NAME
603               && is_gimple_reg (use)))
604         {
605           tree var;
606
607           if (is_gimple_debug (stmt))
608             {
609               if (gimple_debug_bind_p (stmt))
610                 gimple_debug_bind_reset_value (stmt);
611               else
612                 gcc_unreachable ();
613
614               break;
615             }
616
617           var = create_tmp_var (type_use, "var");
618
619           if (type_use != type_expr)
620             expr = fold_convert (type_use, expr);
621
622           expr = build2 (MODIFY_EXPR, type_use, var, expr);
623           expr = force_gimple_operand (expr, &stmts, true, NULL);
624           gsi_insert_seq_before (insert_gsi, stmts, GSI_SAME_STMT);
625         }
626
627       replace_exp (use_p, expr);
628     }
629
630   update_stmt (stmt);
631 }
632
633 /* Returns true if NAME is a parameter of SESE.  */
634
635 static bool
636 is_parameter (sese region, tree name)
637 {
638   int i;
639   tree p;
640
641   for (i = 0; VEC_iterate (tree, SESE_PARAMS (region), i, p); i++)
642     if (p == name)
643       return true;
644
645   return false;
646 }
647
648 /* Returns true if NAME is an induction variable.  */
649
650 static bool
651 is_iv (tree name)
652 {
653   return gimple_code (SSA_NAME_DEF_STMT (name)) == GIMPLE_PHI;
654 }
655
656 static void expand_scalar_variables_stmt (gimple, basic_block, sese,
657                                           htab_t, gimple_stmt_iterator *);
658 static tree
659 expand_scalar_variables_expr (tree, tree, enum tree_code, tree, basic_block,
660                               sese, htab_t, gimple_stmt_iterator *);
661
662 static tree
663 expand_scalar_variables_call (gimple stmt, basic_block bb, sese region,
664                               htab_t map, gimple_stmt_iterator *gsi)
665 {
666   int i, nargs = gimple_call_num_args (stmt);
667   VEC (tree, gc) *args = VEC_alloc (tree, gc, nargs);
668   tree fn_type = TREE_TYPE (gimple_call_fn (stmt));
669   tree fn = gimple_call_fndecl (stmt);
670   tree call_expr, var, lhs;
671   gimple call;
672
673   for (i = 0; i < nargs; i++)
674     {
675       tree arg = gimple_call_arg (stmt, i);
676       tree t = TREE_TYPE (arg);
677
678       var = create_tmp_var (t, "var");
679       arg = expand_scalar_variables_expr (t, arg, TREE_CODE (arg), NULL,
680                                           bb, region, map, gsi);
681       arg = build2 (MODIFY_EXPR, t, var, arg);
682       arg = force_gimple_operand_gsi (gsi, arg, true, NULL,
683                                       true, GSI_SAME_STMT);
684       VEC_quick_push (tree, args, arg);
685     }
686
687   lhs = gimple_call_lhs (stmt);
688   var = create_tmp_var (TREE_TYPE (lhs), "var");
689   call_expr = build_call_vec (fn_type, fn, args);
690   call = gimple_build_call_from_tree (call_expr);
691   var = make_ssa_name (var, call);
692   gimple_call_set_lhs (call, var);
693   gsi_insert_before (gsi, call, GSI_SAME_STMT);
694
695   return var;
696 }
697
698 /* Copies at GSI all the scalar computations on which the ssa_name OP0
699    depends on in the SESE: these are all the scalar variables used in
700    the definition of OP0, that are defined outside BB and still in the
701    SESE, i.e. not a parameter of the SESE.  The expression that is
702    returned contains only induction variables from the generated code:
703    MAP contains the induction variables renaming mapping, and is used
704    to translate the names of induction variables.  */
705
706 static tree
707 expand_scalar_variables_ssa_name (tree op0, basic_block bb,
708                                   sese region, htab_t map, 
709                                   gimple_stmt_iterator *gsi)
710 {
711   gimple def_stmt;
712   tree new_op;
713       
714   if (is_parameter (region, op0)
715       || is_iv (op0))
716     return get_rename (map, op0);
717       
718   def_stmt = SSA_NAME_DEF_STMT (op0);
719
720   /* Check whether we already have a rename for OP0.  */
721   new_op = get_rename (map, op0);
722
723   if (new_op != op0
724       && gimple_bb (SSA_NAME_DEF_STMT (new_op)) == bb)
725     return new_op;
726       
727   if (gimple_bb (def_stmt) == bb)
728     {
729       /* If the defining statement is in the basic block already
730          we do not need to create a new expression for it, we
731          only need to ensure its operands are expanded.  */
732       expand_scalar_variables_stmt (def_stmt, bb, region, map, gsi);
733       return new_op;
734     }
735   else
736     {
737       if (!gimple_bb (def_stmt)
738           || !bb_in_sese_p (gimple_bb (def_stmt), region))
739         return new_op;
740
741       switch (gimple_code (def_stmt))
742         {
743         case GIMPLE_ASSIGN:
744           {
745             tree var0 = gimple_assign_rhs1 (def_stmt);
746             enum tree_code subcode = gimple_assign_rhs_code (def_stmt);
747             tree var1 = gimple_assign_rhs2 (def_stmt);
748             tree type = gimple_expr_type (def_stmt);
749
750             return expand_scalar_variables_expr (type, var0, subcode, var1, bb,
751                                                  region, map, gsi);
752           }
753
754         case GIMPLE_CALL:
755           return expand_scalar_variables_call (def_stmt, bb, region, map, gsi);
756
757         default:
758           gcc_unreachable ();
759           return new_op;
760         }
761     }
762 }
763
764 /* Copies at GSI all the scalar computations on which the expression
765    OP0 CODE OP1 depends on in the SESE: these are all the scalar
766    variables used in OP0 and OP1, defined outside BB and still defined
767    in the SESE, i.e. not a parameter of the SESE.  The expression that
768    is returned contains only induction variables from the generated
769    code: MAP contains the induction variables renaming mapping, and is
770    used to translate the names of induction variables.  */
771
772 static tree
773 expand_scalar_variables_expr (tree type, tree op0, enum tree_code code, 
774                               tree op1, basic_block bb, sese region, 
775                               htab_t map, gimple_stmt_iterator *gsi)
776 {
777   if (TREE_CODE_CLASS (code) == tcc_constant
778       || TREE_CODE_CLASS (code) == tcc_declaration)
779     return op0;
780
781   /* For data references we have to duplicate also its memory
782      indexing.  */
783   if (TREE_CODE_CLASS (code) == tcc_reference)
784     {
785       switch (code)
786         {
787         case REALPART_EXPR:
788         case IMAGPART_EXPR:
789           {
790             tree op = TREE_OPERAND (op0, 0);
791             tree res = expand_scalar_variables_expr
792               (type, op, TREE_CODE (op), NULL, bb, region, map, gsi);
793             return build1 (code, type, res);
794           }
795
796         case INDIRECT_REF:
797           {
798             tree old_name = TREE_OPERAND (op0, 0);
799             tree expr = expand_scalar_variables_ssa_name
800               (old_name, bb, region, map, gsi);
801
802             if (TREE_CODE (expr) != SSA_NAME
803                 && is_gimple_reg (old_name))
804               {
805                 tree type = TREE_TYPE (old_name);
806                 tree var = create_tmp_var (type, "var");
807
808                 expr = build2 (MODIFY_EXPR, type, var, expr);
809                 expr = force_gimple_operand_gsi (gsi, expr, true, NULL,
810                                                  true, GSI_SAME_STMT);
811               }
812
813             return fold_build1 (code, type, expr);
814           }
815
816         case ARRAY_REF:
817           {
818             tree op00 = TREE_OPERAND (op0, 0);
819             tree op01 = TREE_OPERAND (op0, 1);
820             tree op02 = TREE_OPERAND (op0, 2);
821             tree op03 = TREE_OPERAND (op0, 3);
822             tree base = expand_scalar_variables_expr
823               (TREE_TYPE (op00), op00, TREE_CODE (op00), NULL, bb, region,
824                map, gsi);
825             tree subscript = expand_scalar_variables_expr
826               (TREE_TYPE (op01), op01, TREE_CODE (op01), NULL, bb, region,
827                map, gsi);
828
829             return build4 (ARRAY_REF, type, base, subscript, op02, op03);
830           }
831
832         default:
833           /* The above cases should catch everything.  */
834           gcc_unreachable ();
835         }
836     }
837
838   if (TREE_CODE_CLASS (code) == tcc_unary)
839     {
840       tree op0_type = TREE_TYPE (op0);
841       enum tree_code op0_code = TREE_CODE (op0);
842       tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
843                                                     NULL, bb, region, map, gsi);
844   
845       return fold_build1 (code, type, op0_expr);
846     }
847
848   if (TREE_CODE_CLASS (code) == tcc_binary
849       || TREE_CODE_CLASS (code) == tcc_comparison)
850     {
851       tree op0_type = TREE_TYPE (op0);
852       enum tree_code op0_code = TREE_CODE (op0);
853       tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
854                                                     NULL, bb, region, map, gsi);
855       tree op1_type = TREE_TYPE (op1);
856       enum tree_code op1_code = TREE_CODE (op1);
857       tree op1_expr = expand_scalar_variables_expr (op1_type, op1, op1_code,
858                                                     NULL, bb, region, map, gsi);
859
860       return fold_build2 (code, type, op0_expr, op1_expr);
861     }
862
863   if (code == SSA_NAME)
864     return expand_scalar_variables_ssa_name (op0, bb, region, map, gsi);
865
866   if (code == ADDR_EXPR)
867     return op0;
868
869   gcc_unreachable ();
870   return NULL;
871 }
872
873 /* Copies at the beginning of BB all the scalar computations on which
874    STMT depends on in the SESE: these are all the scalar variables used
875    in STMT, defined outside BB and still defined in the SESE, i.e. not a
876    parameter of the SESE.  The expression that is returned contains
877    only induction variables from the generated code: MAP contains the
878    induction variables renaming mapping, and is used to translate the
879    names of induction variables.  */
880  
881 static void
882 expand_scalar_variables_stmt (gimple stmt, basic_block bb, sese region,
883                               htab_t map, gimple_stmt_iterator *gsi)
884 {
885   ssa_op_iter iter;
886   use_operand_p use_p;
887
888   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
889     {
890       tree use = USE_FROM_PTR (use_p);
891       tree type = TREE_TYPE (use);
892       enum tree_code code = TREE_CODE (use);
893       tree use_expr;
894
895       if (!is_gimple_reg (use))
896         continue;
897
898       /* Don't expand USE if we already have a rename for it.  */
899       use_expr = get_rename (map, use);
900       if (use_expr != use)
901         continue;
902
903       use_expr = expand_scalar_variables_expr (type, use, code, NULL, bb,
904                                                region, map, gsi);
905       use_expr = fold_convert (type, use_expr);
906
907       if (use_expr == use)
908         continue;
909
910       if (is_gimple_debug (stmt))
911         {
912           if (gimple_debug_bind_p (stmt))
913             gimple_debug_bind_reset_value (stmt);
914           else
915             gcc_unreachable ();
916
917           break;
918         }
919
920       if (TREE_CODE (use_expr) != SSA_NAME)
921         {
922           tree var = create_tmp_var (type, "var");
923
924           use_expr = build2 (MODIFY_EXPR, type, var, use_expr);
925           use_expr = force_gimple_operand_gsi (gsi, use_expr, true, NULL,
926                                                true, GSI_SAME_STMT);
927         }
928
929       replace_exp (use_p, use_expr);
930     }
931
932   update_stmt (stmt);
933 }
934
935 /* Copies at the beginning of BB all the scalar computations on which
936    BB depends on in the SESE: these are all the scalar variables used
937    in BB, defined outside BB and still defined in the SESE, i.e. not a
938    parameter of the SESE.  The expression that is returned contains
939    only induction variables from the generated code: MAP contains the
940    induction variables renaming mapping, and is used to translate the
941    names of induction variables.  */
942
943 static void 
944 expand_scalar_variables (basic_block bb, sese region, htab_t map)
945 {
946   gimple_stmt_iterator gsi;
947   
948   for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
949     {
950       gimple stmt = gsi_stmt (gsi);
951       expand_scalar_variables_stmt (stmt, bb, region, map, &gsi);
952       gsi_next (&gsi);
953     }
954 }
955
956 /* Rename all the SSA_NAMEs from block BB according to the MAP.  */
957
958 static void 
959 rename_variables (basic_block bb, htab_t map)
960 {
961   gimple_stmt_iterator gsi;
962   gimple_stmt_iterator insert_gsi = gsi_start_bb (bb);
963   
964   for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
965     rename_variables_in_stmt (gsi_stmt (gsi), map, &insert_gsi);
966 }
967
968 /* Remove condition from BB.  */
969
970 static void
971 remove_condition (basic_block bb)
972 {
973   gimple last = last_stmt (bb);
974
975   if (last && gimple_code (last) == GIMPLE_COND)
976     {
977       gimple_stmt_iterator gsi = gsi_last_bb (bb);
978       gsi_remove (&gsi, true);
979     }
980 }
981
982 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set.  */
983
984 edge
985 get_true_edge_from_guard_bb (basic_block bb)
986 {
987   edge e;
988   edge_iterator ei;
989
990   FOR_EACH_EDGE (e, ei, bb->succs)
991     if (e->flags & EDGE_TRUE_VALUE) 
992       return e;
993
994   gcc_unreachable ();
995   return NULL;
996 }
997
998 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared.  */
999
1000 edge
1001 get_false_edge_from_guard_bb (basic_block bb)
1002 {
1003   edge e;
1004   edge_iterator ei;
1005
1006   FOR_EACH_EDGE (e, ei, bb->succs)
1007     if (!(e->flags & EDGE_TRUE_VALUE)) 
1008       return e;
1009
1010   gcc_unreachable ();
1011   return NULL;
1012 }
1013
1014 /* Returns true when NAME is defined in LOOP.  */
1015
1016 static bool
1017 defined_in_loop_p (tree name, loop_p loop)
1018 {
1019   gimple stmt = SSA_NAME_DEF_STMT (name);
1020
1021   return (gimple_bb (stmt)->loop_father == loop);
1022 }
1023
1024 /* Returns the gimple statement that uses NAME outside the loop it is
1025    defined in, returns NULL if there is no such loop close phi node.
1026    An invariant of the loop closed SSA form is that the only use of a
1027    variable, outside the loop it is defined in, is in the loop close
1028    phi node that just follows the loop.  */
1029
1030 static gimple
1031 alive_after_loop (tree name)
1032 {
1033   use_operand_p use_p;
1034   imm_use_iterator imm_iter;
1035   loop_p loop = gimple_bb (SSA_NAME_DEF_STMT (name))->loop_father;
1036
1037   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, name)
1038     {
1039       gimple stmt = USE_STMT (use_p);
1040
1041       if (gimple_code (stmt) == GIMPLE_PHI
1042           && gimple_bb (stmt)->loop_father != loop)
1043         return stmt;
1044     }
1045
1046   return NULL;
1047 }
1048
1049 /* Return true if a close phi has not yet been inserted for the use of
1050    variable NAME on the single exit of LOOP.  */
1051
1052 static bool
1053 close_phi_not_yet_inserted_p (loop_p loop, tree name)
1054 {
1055   gimple_stmt_iterator psi;
1056   basic_block bb = single_exit (loop)->dest;
1057
1058   for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1059     if (gimple_phi_arg_def (gsi_stmt (psi), 0) == name)
1060       return false;
1061
1062   return true;
1063 }
1064
1065 /* A structure for passing parameters to add_loop_exit_phis.  */
1066
1067 typedef struct alep {
1068   loop_p loop;
1069   VEC (rename_map_elt, heap) *new_renames;
1070 } *alep_p;
1071
1072 /* Helper function for htab_traverse in insert_loop_close_phis.  */
1073
1074 static int
1075 add_loop_exit_phis (void **slot, void *data)
1076 {
1077   struct rename_map_elt_s *entry;
1078   alep_p a;
1079   loop_p loop;
1080   tree expr, new_name;
1081   bool def_in_loop_p, used_outside_p, need_close_phi_p;
1082   gimple old_close_phi;
1083
1084   if (!slot || !data)
1085     return 1;
1086
1087   entry = (struct rename_map_elt_s *) *slot;
1088   a = (alep_p) data;
1089   loop = a->loop;
1090   expr = entry->expr;
1091
1092   if (TREE_CODE (expr) != SSA_NAME)
1093     return 1;
1094
1095   new_name = expr;
1096   def_in_loop_p = defined_in_loop_p (new_name, loop);
1097   old_close_phi = alive_after_loop (entry->old_name);
1098   used_outside_p = (old_close_phi != NULL);
1099   need_close_phi_p = (def_in_loop_p && used_outside_p
1100                       && close_phi_not_yet_inserted_p (loop, new_name));
1101
1102   /* Insert a loop close phi node.  */
1103   if (need_close_phi_p)
1104     {
1105       basic_block bb = single_exit (loop)->dest;
1106       gimple phi = create_phi_node (new_name, bb);
1107       tree new_res = create_new_def_for (gimple_phi_result (phi), phi,
1108                                          gimple_phi_result_ptr (phi));
1109
1110       add_phi_arg (phi, new_name, single_pred_edge (bb), UNKNOWN_LOCATION);
1111       VEC_safe_push (rename_map_elt, heap, a->new_renames,
1112                      new_rename_map_elt (gimple_phi_result (old_close_phi),
1113                                          new_res));
1114     }
1115
1116   /* Remove the old rename from the map.  */
1117   if (def_in_loop_p && *slot)
1118     {
1119       free (*slot);
1120       *slot = NULL;
1121     }
1122
1123   return 1;
1124 }
1125
1126 /* Traverses MAP and removes from it all the tuples (OLD, NEW) where
1127    NEW is defined in LOOP.  Inserts on the exit of LOOP the close phi
1128    node "RES = phi (NEW)" corresponding to "OLD_RES = phi (OLD)" in
1129    the original code.  Inserts in MAP the tuple (OLD_RES, RES).  */
1130
1131 void
1132 insert_loop_close_phis (htab_t map, loop_p loop)
1133 {
1134   int i;
1135   struct alep a;
1136   rename_map_elt elt;
1137
1138   a.loop = loop;
1139   a.new_renames = VEC_alloc (rename_map_elt, heap, 3);
1140   update_ssa (TODO_update_ssa);
1141   htab_traverse (map, add_loop_exit_phis, &a);
1142   update_ssa (TODO_update_ssa);
1143
1144   for (i = 0; VEC_iterate (rename_map_elt, a.new_renames, i, elt); i++)
1145     {
1146       set_rename (map, elt->old_name, elt->expr);
1147       free (elt);
1148     }
1149
1150   VEC_free (rename_map_elt, heap, a.new_renames);
1151 }
1152
1153 /* Helper structure for htab_traverse in insert_guard_phis.  */
1154
1155 struct igp {
1156   basic_block bb;
1157   edge true_edge, false_edge;
1158   htab_t before_guard;
1159 };
1160
1161 /* Return the default name that is before the guard.  */
1162
1163 static tree
1164 default_before_guard (htab_t before_guard, tree old_name)
1165 {
1166   tree res = get_rename (before_guard, old_name);
1167
1168   if (res == old_name)
1169     {
1170       if (is_gimple_reg (res))
1171         return fold_convert (TREE_TYPE (res), integer_zero_node);
1172       return gimple_default_def (cfun, SSA_NAME_VAR (res));
1173     }
1174
1175   return res;
1176 }
1177
1178 /* Prepares EXPR to be a good phi argument when the phi result is
1179    RES.  Insert needed statements on edge E.  */
1180
1181 static tree
1182 convert_for_phi_arg (tree expr, tree res, edge e)
1183 {
1184   tree type = TREE_TYPE (res);
1185
1186   if (TREE_TYPE (expr) != type)
1187     expr = fold_convert (type, expr);
1188
1189   if (TREE_CODE (expr) != SSA_NAME
1190       && !is_gimple_min_invariant (expr))
1191     {
1192       tree var = create_tmp_var (type, "var");
1193       gimple_seq stmts;
1194
1195       expr = build2 (MODIFY_EXPR, type, var, expr);
1196       expr = force_gimple_operand (expr, &stmts, true, NULL);
1197       gsi_insert_seq_on_edge_immediate (e, stmts);
1198     }
1199
1200   return expr;
1201 }
1202
1203 /* Helper function for htab_traverse in insert_guard_phis.  */
1204
1205 static int
1206 add_guard_exit_phis (void **slot, void *s)
1207 {
1208   struct rename_map_elt_s *entry = (struct rename_map_elt_s *) *slot;
1209   struct igp *i = (struct igp *) s;
1210   basic_block bb = i->bb;
1211   edge true_edge = i->true_edge;
1212   edge false_edge = i->false_edge;
1213   tree res = entry->old_name;
1214   tree name1 = entry->expr;
1215   tree name2 = default_before_guard (i->before_guard, res);
1216   gimple phi;
1217
1218   /* Nothing to be merged if the name before the guard is the same as
1219      the one after.  */
1220   if (name1 == name2)
1221     return 1;
1222
1223   name1 = convert_for_phi_arg (name1, res, true_edge);
1224   name2 = convert_for_phi_arg (name2, res, false_edge);
1225
1226   phi = create_phi_node (res, bb);
1227   res = create_new_def_for (gimple_phi_result (phi), phi,
1228                             gimple_phi_result_ptr (phi));
1229
1230   add_phi_arg (phi, name1, true_edge, UNKNOWN_LOCATION);
1231   add_phi_arg (phi, name2, false_edge, UNKNOWN_LOCATION);
1232
1233   entry->expr = res;
1234   *slot = entry;
1235   return 1;
1236 }
1237
1238 /* Iterate over RENAME_MAP and get tuples of the form (OLD, NAME1).
1239    If there is a correspondent tuple (OLD, NAME2) in BEFORE_GUARD,
1240    with NAME1 different than NAME2, then insert in BB the phi node:
1241
1242    | RES = phi (NAME1 (on TRUE_EDGE), NAME2 (on FALSE_EDGE))"
1243
1244    if there is no tuple for OLD in BEFORE_GUARD, insert
1245
1246    | RES = phi (NAME1 (on TRUE_EDGE),
1247    |            DEFAULT_DEFINITION of NAME1 (on FALSE_EDGE))".
1248
1249    Finally register in RENAME_MAP the tuple (OLD, RES).  */
1250
1251 void
1252 insert_guard_phis (basic_block bb, edge true_edge, edge false_edge,
1253                    htab_t before_guard, htab_t rename_map)
1254 {
1255   struct igp i;
1256   i.bb = bb;
1257   i.true_edge = true_edge;
1258   i.false_edge = false_edge;
1259   i.before_guard = before_guard;
1260
1261   update_ssa (TODO_update_ssa);
1262   htab_traverse (rename_map, add_guard_exit_phis, &i);
1263   update_ssa (TODO_update_ssa);
1264 }
1265
1266 /* Create a duplicate of the basic block BB.  NOTE: This does not
1267    preserve SSA form.  */
1268
1269 static void
1270 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb, htab_t map)
1271 {
1272   gimple_stmt_iterator gsi, gsi_tgt;
1273
1274   gsi_tgt = gsi_start_bb (new_bb);
1275   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1276     {
1277       def_operand_p def_p;
1278       ssa_op_iter op_iter;
1279       gimple stmt = gsi_stmt (gsi);
1280       gimple copy;
1281
1282       if (gimple_code (stmt) == GIMPLE_LABEL)
1283         continue;
1284
1285       /* Create a new copy of STMT and duplicate STMT's virtual
1286          operands.  */
1287       copy = gimple_copy (stmt);
1288       gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
1289       mark_sym_for_renaming (gimple_vop (cfun));
1290
1291       maybe_duplicate_eh_stmt (copy, stmt);
1292       gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
1293
1294       /* Create new names for all the definitions created by COPY and
1295          add replacement mappings for each new name.  */
1296       FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
1297         {
1298           tree old_name = DEF_FROM_PTR (def_p);
1299           tree new_name = create_new_def_for (old_name, copy, def_p);
1300           set_rename (map, old_name, new_name);
1301         }
1302     }
1303 }
1304
1305 /* Copies BB and includes in the copied BB all the statements that can
1306    be reached following the use-def chains from the memory accesses,
1307    and returns the next edge following this new block.  */
1308  
1309 edge
1310 copy_bb_and_scalar_dependences (basic_block bb, sese region,
1311                                 edge next_e, htab_t map)
1312 {
1313   basic_block new_bb = split_edge (next_e);
1314
1315   next_e = single_succ_edge (new_bb);
1316   graphite_copy_stmts_from_block (bb, new_bb, map);
1317   remove_condition (new_bb);
1318   remove_phi_nodes (new_bb);
1319   expand_scalar_variables (new_bb, region, map);
1320   rename_variables (new_bb, map);
1321
1322   return next_e;
1323 }
1324
1325 /* Returns the outermost loop in SCOP that contains BB.  */
1326
1327 struct loop *
1328 outermost_loop_in_sese (sese region, basic_block bb)
1329 {
1330   struct loop *nest;
1331
1332   nest = bb->loop_father;
1333   while (loop_outer (nest)
1334          && loop_in_sese_p (loop_outer (nest), region))
1335     nest = loop_outer (nest);
1336
1337   return nest;
1338 }
1339
1340 /* Sets the false region of an IF_REGION to REGION.  */
1341
1342 void
1343 if_region_set_false_region (ifsese if_region, sese region)
1344 {
1345   basic_block condition = if_region_get_condition_block (if_region);
1346   edge false_edge = get_false_edge_from_guard_bb (condition);
1347   basic_block dummy = false_edge->dest;
1348   edge entry_region = SESE_ENTRY (region);
1349   edge exit_region = SESE_EXIT (region);
1350   basic_block before_region = entry_region->src;
1351   basic_block last_in_region = exit_region->src;
1352   void **slot = htab_find_slot_with_hash (current_loops->exits, exit_region,
1353                                           htab_hash_pointer (exit_region),
1354                                           NO_INSERT);
1355
1356   entry_region->flags = false_edge->flags;
1357   false_edge->flags = exit_region->flags;
1358
1359   redirect_edge_pred (entry_region, condition);
1360   redirect_edge_pred (exit_region, before_region);
1361   redirect_edge_pred (false_edge, last_in_region);
1362   redirect_edge_succ (false_edge, single_succ (dummy));
1363   delete_basic_block (dummy);
1364
1365   exit_region->flags = EDGE_FALLTHRU;
1366   recompute_all_dominators ();
1367
1368   SESE_EXIT (region) = false_edge;
1369   if_region->false_region = region;
1370
1371   if (slot)
1372     {
1373       struct loop_exit *loop_exit = GGC_CNEW (struct loop_exit);
1374
1375       memcpy (loop_exit, *((struct loop_exit **) slot), sizeof (struct loop_exit));
1376       htab_clear_slot (current_loops->exits, slot);
1377
1378       slot = htab_find_slot_with_hash (current_loops->exits, false_edge,
1379                                        htab_hash_pointer (false_edge),
1380                                        INSERT);
1381       loop_exit->e = false_edge;
1382       *slot = loop_exit;
1383       false_edge->src->loop_father->exits->next = loop_exit;
1384     }
1385 }
1386
1387 /* Creates an IFSESE with CONDITION on edge ENTRY.  */
1388
1389 ifsese
1390 create_if_region_on_edge (edge entry, tree condition)
1391 {
1392   edge e;
1393   edge_iterator ei;
1394   sese sese_region = GGC_NEW (struct sese_s);
1395   sese true_region = GGC_NEW (struct sese_s);
1396   sese false_region = GGC_NEW (struct sese_s);
1397   ifsese if_region = GGC_NEW (struct ifsese_s);
1398   edge exit = create_empty_if_region_on_edge (entry, condition);
1399
1400   if_region->region = sese_region;
1401   if_region->region->entry = entry;
1402   if_region->region->exit = exit;
1403
1404   FOR_EACH_EDGE (e, ei, entry->dest->succs)
1405     {
1406       if (e->flags & EDGE_TRUE_VALUE)
1407         {
1408           true_region->entry = e;
1409           true_region->exit = single_succ_edge (e->dest);
1410           if_region->true_region = true_region;
1411         }
1412       else if (e->flags & EDGE_FALSE_VALUE)
1413         {
1414           false_region->entry = e;
1415           false_region->exit = single_succ_edge (e->dest);
1416           if_region->false_region = false_region;
1417         }
1418     }
1419
1420   return if_region;
1421 }
1422
1423 /* Moves REGION in a condition expression:
1424    | if (1)
1425    |   ;
1426    | else
1427    |   REGION;
1428 */
1429
1430 ifsese
1431 move_sese_in_condition (sese region)
1432 {
1433   basic_block pred_block = split_edge (SESE_ENTRY (region));
1434   ifsese if_region = NULL;
1435
1436   SESE_ENTRY (region) = single_succ_edge (pred_block);
1437   if_region = create_if_region_on_edge (single_pred_edge (pred_block), integer_one_node);
1438   if_region_set_false_region (if_region, region);
1439
1440   return if_region;
1441 }
1442
1443 /* Reset the loop->aux pointer for all loops in REGION.  */
1444
1445 void
1446 sese_reset_aux_in_loops (sese region)
1447 {
1448   int i;
1449   loop_p loop;
1450
1451   for (i = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), i, loop); i++)
1452     loop->aux = NULL;
1453 }
1454
1455 /* Returns the scalar evolution of T in REGION.  Every variable that
1456    is not defined in the REGION is considered a parameter.  */
1457
1458 tree
1459 scalar_evolution_in_region (sese region, loop_p loop, tree t)
1460 {
1461   gimple def;
1462   struct loop *def_loop;
1463   basic_block before = block_before_sese (region);
1464
1465   if (TREE_CODE (t) != SSA_NAME
1466       || loop_in_sese_p (loop, region))
1467     return instantiate_scev (before, loop,
1468                              analyze_scalar_evolution (loop, t));
1469
1470   if (!defined_in_sese_p (t, region))
1471     return t;
1472
1473   def = SSA_NAME_DEF_STMT (t);
1474   def_loop = loop_containing_stmt (def);
1475
1476   if (loop_in_sese_p (def_loop, region))
1477     {
1478       t = analyze_scalar_evolution (def_loop, t);
1479       def_loop = superloop_at_depth (def_loop, loop_depth (loop) + 1);
1480       t = compute_overall_effect_of_inner_loop (def_loop, t);
1481       return t;
1482     }
1483   else
1484     return instantiate_scev (before, loop, t);
1485 }