OSDN Git Service

2009-11-03 Sebastian Pop <sebastian.pop@amd.com>
[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 stmt = SSA_NAME_DEF_STMT (name);
411   basic_block def_bb = gimple_bb (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 (stmt))
422     {
423     case GIMPLE_PHI:
424       for (i = 0; i < gimple_phi_num_args (stmt); i++)
425         {
426           tree arg = gimple_phi_arg_def (stmt, i);
427           tree res;
428
429           if (gimple_bb (SSA_NAME_DEF_STMT (arg))
430               && def_bb->index == gimple_bb (SSA_NAME_DEF_STMT (arg))->index)
431             continue;
432
433           res = get_vdef_before_sese (region, arg, visited);
434           if (res)
435             return res;
436         }
437       return NULL_TREE;
438
439     case GIMPLE_ASSIGN:
440     case GIMPLE_CALL:
441       {
442         use_operand_p use_p = gimple_vuse_op (stmt);
443         tree use = USE_FROM_PTR (use_p);
444
445         if (def_bb->index == gimple_bb (SSA_NAME_DEF_STMT (use))->index)
446           RESET_BIT (visited, def_bb->index);
447
448         return get_vdef_before_sese (region, use, visited);
449       }
450
451     default:
452       return NULL_TREE;
453     }
454 }
455
456 /* Adjust a virtual phi node PHI that is placed at the end of the
457    generated code for SCOP:
458
459    | if (1)
460    |   generated code from REGION;
461    | else
462    |   REGION;
463
464    The FALSE_E edge comes from the original code, TRUE_E edge comes
465    from the code generated for the SCOP.  */
466
467 static void
468 sese_adjust_vphi (sese region, gimple phi, edge true_e)
469 {
470   unsigned i;
471
472   gcc_assert (gimple_phi_num_args (phi) == 2);
473
474   for (i = 0; i < gimple_phi_num_args (phi); i++)
475     if (gimple_phi_arg_edge (phi, i) == true_e)
476       {
477         tree true_arg, false_arg, before_scop_arg;
478         sbitmap visited;
479
480         true_arg = gimple_phi_arg_def (phi, i);
481         if (!SSA_NAME_IS_DEFAULT_DEF (true_arg))
482           return;
483
484         false_arg = gimple_phi_arg_def (phi, i == 0 ? 1 : 0);
485         if (SSA_NAME_IS_DEFAULT_DEF (false_arg))
486           return;
487
488         visited = sbitmap_alloc (last_basic_block);
489         sbitmap_zero (visited);
490         before_scop_arg = get_vdef_before_sese (region, false_arg, visited);
491         gcc_assert (before_scop_arg != NULL_TREE);
492         SET_PHI_ARG_DEF (phi, i, before_scop_arg);
493         sbitmap_free (visited);
494       }
495 }
496
497 /* Returns the name associated to OLD_NAME in MAP.  */
498
499 static tree
500 get_rename (htab_t map, tree old_name)
501 {
502   struct rename_map_elt_s tmp;
503   PTR *slot;
504
505   tmp.old_name = old_name;
506   slot = htab_find_slot (map, &tmp, NO_INSERT);
507
508   if (slot && *slot)
509     return ((rename_map_elt) *slot)->expr;
510
511   return old_name;
512 }
513
514 /* Register in MAP the rename tuple (old_name, expr).  */
515
516 void
517 set_rename (htab_t map, tree old_name, tree expr)
518 {
519   struct rename_map_elt_s tmp;
520   PTR *slot;
521
522   if (old_name == expr)
523     return;
524
525   tmp.old_name = old_name;
526   slot = htab_find_slot (map, &tmp, INSERT);
527
528   if (!slot)
529     return;
530
531   if (*slot)
532     free (*slot);
533
534   *slot = new_rename_map_elt (old_name, expr);
535 }
536
537 /* Adjusts the phi nodes in the block BB for variables defined in
538    SCOP_REGION and used outside the SCOP_REGION.  The code generation
539    moves SCOP_REGION in the else clause of an "if (1)" and generates
540    code in the then clause:
541
542    | if (1)
543    |   generated code from REGION;
544    | else
545    |   REGION;
546
547    To adjust the phi nodes after the condition, the RENAME_MAP is
548    used.  */
549
550 void
551 sese_adjust_liveout_phis (sese region, htab_t rename_map, basic_block bb,
552                           edge false_e, edge true_e)
553 {
554   gimple_stmt_iterator si;
555
556   for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
557     {
558       unsigned i;
559       unsigned false_i = 0;
560       gimple phi = gsi_stmt (si);
561
562       if (!is_gimple_reg (PHI_RESULT (phi)))
563         {
564           sese_adjust_vphi (region, phi, true_e);
565           continue;
566         }
567
568       for (i = 0; i < gimple_phi_num_args (phi); i++)
569         if (gimple_phi_arg_edge (phi, i) == false_e)
570           {
571             false_i = i;
572             break;
573           }
574
575       for (i = 0; i < gimple_phi_num_args (phi); i++)
576         if (gimple_phi_arg_edge (phi, i) == true_e)
577           {
578             tree old_name = gimple_phi_arg_def (phi, false_i);
579             tree expr = get_rename (rename_map, old_name);
580             gimple_seq stmts;
581
582             gcc_assert (old_name != expr);
583
584             if (TREE_CODE (expr) != SSA_NAME
585                 && is_gimple_reg (old_name))
586               {
587                 tree type = TREE_TYPE (old_name);
588                 tree var = create_tmp_var (type, "var");
589
590                 expr = build2 (MODIFY_EXPR, type, var, expr);
591                 expr = force_gimple_operand (expr, &stmts, true, NULL);
592                 gsi_insert_seq_on_edge_immediate (true_e, stmts);
593               }
594
595             SET_PHI_ARG_DEF (phi, i, expr);
596           }
597     }
598 }
599
600 /* Rename the SSA_NAMEs used in STMT and that appear in MAP.  */
601
602 static void 
603 rename_variables_in_stmt (gimple stmt, htab_t map, gimple_stmt_iterator *insert_gsi)
604 {
605   ssa_op_iter iter;
606   use_operand_p use_p;
607
608   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
609     {
610       tree use = USE_FROM_PTR (use_p);
611       tree expr = get_rename (map, use);
612       tree type_use = TREE_TYPE (use);
613       tree type_expr = TREE_TYPE (expr);
614       gimple_seq stmts;
615
616       if (use == expr)
617         continue;
618
619       if (type_use != type_expr
620           || (TREE_CODE (expr) != SSA_NAME
621               && is_gimple_reg (use)))
622         {
623           tree var;
624
625           if (is_gimple_debug (stmt))
626             {
627               if (gimple_debug_bind_p (stmt))
628                 gimple_debug_bind_reset_value (stmt);
629               else
630                 gcc_unreachable ();
631
632               break;
633             }
634
635           var = create_tmp_var (type_use, "var");
636
637           if (type_use != type_expr)
638             expr = fold_convert (type_use, expr);
639
640           expr = build2 (MODIFY_EXPR, type_use, var, expr);
641           expr = force_gimple_operand (expr, &stmts, true, NULL);
642           gsi_insert_seq_before (insert_gsi, stmts, GSI_SAME_STMT);
643         }
644
645       replace_exp (use_p, expr);
646     }
647
648   update_stmt (stmt);
649 }
650
651 /* Returns true if NAME is a parameter of SESE.  */
652
653 static bool
654 is_parameter (sese region, tree name)
655 {
656   int i;
657   tree p;
658
659   for (i = 0; VEC_iterate (tree, SESE_PARAMS (region), i, p); i++)
660     if (p == name)
661       return true;
662
663   return false;
664 }
665
666 /* Returns true if NAME is an induction variable.  */
667
668 static bool
669 is_iv (tree name)
670 {
671   return gimple_code (SSA_NAME_DEF_STMT (name)) == GIMPLE_PHI;
672 }
673
674 static void expand_scalar_variables_stmt (gimple, basic_block, sese,
675                                           htab_t, gimple_stmt_iterator *);
676 static tree
677 expand_scalar_variables_expr (tree, tree, enum tree_code, tree, basic_block,
678                               sese, htab_t, gimple_stmt_iterator *);
679
680 static tree
681 expand_scalar_variables_call (gimple stmt, basic_block bb, sese region,
682                               htab_t map, gimple_stmt_iterator *gsi)
683 {
684   int i, nargs = gimple_call_num_args (stmt);
685   VEC (tree, gc) *args = VEC_alloc (tree, gc, nargs);
686   tree fn_type = TREE_TYPE (gimple_call_fn (stmt));
687   tree fn = gimple_call_fndecl (stmt);
688   tree call_expr, var, lhs;
689   gimple call;
690
691   for (i = 0; i < nargs; i++)
692     {
693       tree arg = gimple_call_arg (stmt, i);
694       tree t = TREE_TYPE (arg);
695
696       var = create_tmp_var (t, "var");
697       arg = expand_scalar_variables_expr (t, arg, TREE_CODE (arg), NULL,
698                                           bb, region, map, gsi);
699       arg = build2 (MODIFY_EXPR, t, var, arg);
700       arg = force_gimple_operand_gsi (gsi, arg, true, NULL,
701                                       true, GSI_SAME_STMT);
702       VEC_quick_push (tree, args, arg);
703     }
704
705   lhs = gimple_call_lhs (stmt);
706   var = create_tmp_var (TREE_TYPE (lhs), "var");
707   call_expr = build_call_vec (fn_type, fn, args);
708   call = gimple_build_call_from_tree (call_expr);
709   var = make_ssa_name (var, call);
710   gimple_call_set_lhs (call, var);
711   gsi_insert_before (gsi, call, GSI_SAME_STMT);
712
713   return var;
714 }
715
716 /* Copies at GSI all the scalar computations on which the ssa_name OP0
717    depends on in the SESE: these are all the scalar variables used in
718    the definition of OP0, that are defined outside BB and still in the
719    SESE, i.e. not a parameter of the SESE.  The expression that is
720    returned contains only induction variables from the generated code:
721    MAP contains the induction variables renaming mapping, and is used
722    to translate the names of induction variables.  */
723
724 static tree
725 expand_scalar_variables_ssa_name (tree op0, basic_block bb,
726                                   sese region, htab_t map, 
727                                   gimple_stmt_iterator *gsi)
728 {
729   gimple def_stmt;
730   tree new_op;
731       
732   if (is_parameter (region, op0)
733       || is_iv (op0))
734     return get_rename (map, op0);
735       
736   def_stmt = SSA_NAME_DEF_STMT (op0);
737
738   /* Check whether we already have a rename for OP0.  */
739   new_op = get_rename (map, op0);
740
741   if (new_op != op0
742       && gimple_bb (SSA_NAME_DEF_STMT (new_op)) == bb)
743     return new_op;
744       
745   if (gimple_bb (def_stmt) == bb)
746     {
747       /* If the defining statement is in the basic block already
748          we do not need to create a new expression for it, we
749          only need to ensure its operands are expanded.  */
750       expand_scalar_variables_stmt (def_stmt, bb, region, map, gsi);
751       return new_op;
752     }
753   else
754     {
755       if (!gimple_bb (def_stmt)
756           || !bb_in_sese_p (gimple_bb (def_stmt), region))
757         return new_op;
758
759       switch (gimple_code (def_stmt))
760         {
761         case GIMPLE_ASSIGN:
762           {
763             tree var0 = gimple_assign_rhs1 (def_stmt);
764             enum tree_code subcode = gimple_assign_rhs_code (def_stmt);
765             tree var1 = gimple_assign_rhs2 (def_stmt);
766             tree type = gimple_expr_type (def_stmt);
767
768             return expand_scalar_variables_expr (type, var0, subcode, var1, bb,
769                                                  region, map, gsi);
770           }
771
772         case GIMPLE_CALL:
773           return expand_scalar_variables_call (def_stmt, bb, region, map, gsi);
774
775         default:
776           gcc_unreachable ();
777           return new_op;
778         }
779     }
780 }
781
782 /* Copies at GSI all the scalar computations on which the expression
783    OP0 CODE OP1 depends on in the SESE: these are all the scalar
784    variables used in OP0 and OP1, defined outside BB and still defined
785    in the SESE, i.e. not a parameter of the SESE.  The expression that
786    is returned contains only induction variables from the generated
787    code: MAP contains the induction variables renaming mapping, and is
788    used to translate the names of induction variables.  */
789
790 static tree
791 expand_scalar_variables_expr (tree type, tree op0, enum tree_code code, 
792                               tree op1, basic_block bb, sese region, 
793                               htab_t map, gimple_stmt_iterator *gsi)
794 {
795   if (TREE_CODE_CLASS (code) == tcc_constant
796       || TREE_CODE_CLASS (code) == tcc_declaration)
797     return op0;
798
799   /* For data references we have to duplicate also its memory
800      indexing.  */
801   if (TREE_CODE_CLASS (code) == tcc_reference)
802     {
803       switch (code)
804         {
805         case REALPART_EXPR:
806         case IMAGPART_EXPR:
807           {
808             tree op = TREE_OPERAND (op0, 0);
809             tree res = expand_scalar_variables_expr
810               (type, op, TREE_CODE (op), NULL, bb, region, map, gsi);
811             return build1 (code, type, res);
812           }
813
814         case INDIRECT_REF:
815           {
816             tree old_name = TREE_OPERAND (op0, 0);
817             tree expr = expand_scalar_variables_ssa_name
818               (old_name, bb, region, map, gsi);
819
820             if (TREE_CODE (expr) != SSA_NAME
821                 && is_gimple_reg (old_name))
822               {
823                 tree type = TREE_TYPE (old_name);
824                 tree var = create_tmp_var (type, "var");
825
826                 expr = build2 (MODIFY_EXPR, type, var, expr);
827                 expr = force_gimple_operand_gsi (gsi, expr, true, NULL,
828                                                  true, GSI_SAME_STMT);
829               }
830
831             return fold_build1 (code, type, expr);
832           }
833
834         case ARRAY_REF:
835           {
836             tree op00 = TREE_OPERAND (op0, 0);
837             tree op01 = TREE_OPERAND (op0, 1);
838             tree op02 = TREE_OPERAND (op0, 2);
839             tree op03 = TREE_OPERAND (op0, 3);
840             tree base = expand_scalar_variables_expr
841               (TREE_TYPE (op00), op00, TREE_CODE (op00), NULL, bb, region,
842                map, gsi);
843             tree subscript = expand_scalar_variables_expr
844               (TREE_TYPE (op01), op01, TREE_CODE (op01), NULL, bb, region,
845                map, gsi);
846
847             return build4 (ARRAY_REF, type, base, subscript, op02, op03);
848           }
849
850         default:
851           /* The above cases should catch everything.  */
852           gcc_unreachable ();
853         }
854     }
855
856   if (TREE_CODE_CLASS (code) == tcc_unary)
857     {
858       tree op0_type = TREE_TYPE (op0);
859       enum tree_code op0_code = TREE_CODE (op0);
860       tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
861                                                     NULL, bb, region, map, gsi);
862   
863       return fold_build1 (code, type, op0_expr);
864     }
865
866   if (TREE_CODE_CLASS (code) == tcc_binary
867       || TREE_CODE_CLASS (code) == tcc_comparison)
868     {
869       tree op0_type = TREE_TYPE (op0);
870       enum tree_code op0_code = TREE_CODE (op0);
871       tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
872                                                     NULL, bb, region, map, gsi);
873       tree op1_type = TREE_TYPE (op1);
874       enum tree_code op1_code = TREE_CODE (op1);
875       tree op1_expr = expand_scalar_variables_expr (op1_type, op1, op1_code,
876                                                     NULL, bb, region, map, gsi);
877
878       return fold_build2 (code, type, op0_expr, op1_expr);
879     }
880
881   if (code == SSA_NAME)
882     return expand_scalar_variables_ssa_name (op0, bb, region, map, gsi);
883
884   if (code == ADDR_EXPR)
885     return op0;
886
887   gcc_unreachable ();
888   return NULL;
889 }
890
891 /* Copies at the beginning of BB all the scalar computations on which
892    STMT depends on in the SESE: these are all the scalar variables used
893    in STMT, defined outside BB and still defined in the SESE, i.e. not a
894    parameter of the SESE.  The expression that is returned contains
895    only induction variables from the generated code: MAP contains the
896    induction variables renaming mapping, and is used to translate the
897    names of induction variables.  */
898  
899 static void
900 expand_scalar_variables_stmt (gimple stmt, basic_block bb, sese region,
901                               htab_t map, gimple_stmt_iterator *gsi)
902 {
903   ssa_op_iter iter;
904   use_operand_p use_p;
905
906   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
907     {
908       tree use = USE_FROM_PTR (use_p);
909       tree type = TREE_TYPE (use);
910       enum tree_code code = TREE_CODE (use);
911       tree use_expr;
912
913       if (!is_gimple_reg (use))
914         continue;
915
916       /* Don't expand USE if we already have a rename for it.  */
917       use_expr = get_rename (map, use);
918       if (use_expr != use)
919         continue;
920
921       use_expr = expand_scalar_variables_expr (type, use, code, NULL, bb,
922                                                region, map, gsi);
923       use_expr = fold_convert (type, use_expr);
924
925       if (use_expr == use)
926         continue;
927
928       if (is_gimple_debug (stmt))
929         {
930           if (gimple_debug_bind_p (stmt))
931             gimple_debug_bind_reset_value (stmt);
932           else
933             gcc_unreachable ();
934
935           break;
936         }
937
938       if (TREE_CODE (use_expr) != SSA_NAME)
939         {
940           tree var = create_tmp_var (type, "var");
941
942           use_expr = build2 (MODIFY_EXPR, type, var, use_expr);
943           use_expr = force_gimple_operand_gsi (gsi, use_expr, true, NULL,
944                                                true, GSI_SAME_STMT);
945         }
946
947       replace_exp (use_p, use_expr);
948     }
949
950   update_stmt (stmt);
951 }
952
953 /* Copies at the beginning of BB all the scalar computations on which
954    BB depends on in the SESE: these are all the scalar variables used
955    in BB, defined outside BB and still defined in the SESE, i.e. not a
956    parameter of the SESE.  The expression that is returned contains
957    only induction variables from the generated code: MAP contains the
958    induction variables renaming mapping, and is used to translate the
959    names of induction variables.  */
960
961 static void 
962 expand_scalar_variables (basic_block bb, sese region, htab_t map)
963 {
964   gimple_stmt_iterator gsi;
965   
966   for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
967     {
968       gimple stmt = gsi_stmt (gsi);
969       expand_scalar_variables_stmt (stmt, bb, region, map, &gsi);
970       gsi_next (&gsi);
971     }
972 }
973
974 /* Rename all the SSA_NAMEs from block BB according to the MAP.  */
975
976 static void 
977 rename_variables (basic_block bb, htab_t map)
978 {
979   gimple_stmt_iterator gsi;
980   gimple_stmt_iterator insert_gsi = gsi_start_bb (bb);
981   
982   for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
983     rename_variables_in_stmt (gsi_stmt (gsi), map, &insert_gsi);
984 }
985
986 /* Remove condition from BB.  */
987
988 static void
989 remove_condition (basic_block bb)
990 {
991   gimple last = last_stmt (bb);
992
993   if (last && gimple_code (last) == GIMPLE_COND)
994     {
995       gimple_stmt_iterator gsi = gsi_last_bb (bb);
996       gsi_remove (&gsi, true);
997     }
998 }
999
1000 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set.  */
1001
1002 edge
1003 get_true_edge_from_guard_bb (basic_block bb)
1004 {
1005   edge e;
1006   edge_iterator ei;
1007
1008   FOR_EACH_EDGE (e, ei, bb->succs)
1009     if (e->flags & EDGE_TRUE_VALUE) 
1010       return e;
1011
1012   gcc_unreachable ();
1013   return NULL;
1014 }
1015
1016 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared.  */
1017
1018 edge
1019 get_false_edge_from_guard_bb (basic_block bb)
1020 {
1021   edge e;
1022   edge_iterator ei;
1023
1024   FOR_EACH_EDGE (e, ei, bb->succs)
1025     if (!(e->flags & EDGE_TRUE_VALUE)) 
1026       return e;
1027
1028   gcc_unreachable ();
1029   return NULL;
1030 }
1031
1032 /* Returns true when NAME is defined in LOOP.  */
1033
1034 static bool
1035 defined_in_loop_p (tree name, loop_p loop)
1036 {
1037   gimple stmt = SSA_NAME_DEF_STMT (name);
1038
1039   return (gimple_bb (stmt)->loop_father == loop);
1040 }
1041
1042 /* Returns the gimple statement that uses NAME outside the loop it is
1043    defined in, returns NULL if there is no such loop close phi node.
1044    An invariant of the loop closed SSA form is that the only use of a
1045    variable, outside the loop it is defined in, is in the loop close
1046    phi node that just follows the loop.  */
1047
1048 static gimple
1049 alive_after_loop (tree name)
1050 {
1051   use_operand_p use_p;
1052   imm_use_iterator imm_iter;
1053   loop_p loop = gimple_bb (SSA_NAME_DEF_STMT (name))->loop_father;
1054
1055   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, name)
1056     {
1057       gimple stmt = USE_STMT (use_p);
1058
1059       if (gimple_code (stmt) == GIMPLE_PHI
1060           && gimple_bb (stmt)->loop_father != loop)
1061         return stmt;
1062     }
1063
1064   return NULL;
1065 }
1066
1067 /* Return true if a close phi has not yet been inserted for the use of
1068    variable NAME on the single exit of LOOP.  */
1069
1070 static bool
1071 close_phi_not_yet_inserted_p (loop_p loop, tree name)
1072 {
1073   gimple_stmt_iterator psi;
1074   basic_block bb = single_exit (loop)->dest;
1075
1076   for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1077     if (gimple_phi_arg_def (gsi_stmt (psi), 0) == name)
1078       return false;
1079
1080   return true;
1081 }
1082
1083 /* A structure for passing parameters to add_loop_exit_phis.  */
1084
1085 typedef struct alep {
1086   loop_p loop;
1087   VEC (rename_map_elt, heap) *new_renames;
1088 } *alep_p;
1089
1090 /* Helper function for htab_traverse in insert_loop_close_phis.  */
1091
1092 static int
1093 add_loop_exit_phis (void **slot, void *data)
1094 {
1095   struct rename_map_elt_s *entry;
1096   alep_p a;
1097   loop_p loop;
1098   tree expr, new_name;
1099   bool def_in_loop_p, used_outside_p, need_close_phi_p;
1100   gimple old_close_phi;
1101
1102   if (!slot || !data)
1103     return 1;
1104
1105   entry = (struct rename_map_elt_s *) *slot;
1106   a = (alep_p) data;
1107   loop = a->loop;
1108   expr = entry->expr;
1109
1110   if (TREE_CODE (expr) != SSA_NAME)
1111     return 1;
1112
1113   new_name = expr;
1114   def_in_loop_p = defined_in_loop_p (new_name, loop);
1115   old_close_phi = alive_after_loop (entry->old_name);
1116   used_outside_p = (old_close_phi != NULL);
1117   need_close_phi_p = (def_in_loop_p && used_outside_p
1118                       && close_phi_not_yet_inserted_p (loop, new_name));
1119
1120   /* Insert a loop close phi node.  */
1121   if (need_close_phi_p)
1122     {
1123       basic_block bb = single_exit (loop)->dest;
1124       gimple phi = create_phi_node (new_name, bb);
1125       tree new_res = create_new_def_for (gimple_phi_result (phi), phi,
1126                                          gimple_phi_result_ptr (phi));
1127
1128       add_phi_arg (phi, new_name, single_pred_edge (bb), UNKNOWN_LOCATION);
1129       VEC_safe_push (rename_map_elt, heap, a->new_renames,
1130                      new_rename_map_elt (gimple_phi_result (old_close_phi),
1131                                          new_res));
1132     }
1133
1134   /* Remove the old rename from the map.  */
1135   if (def_in_loop_p && *slot)
1136     {
1137       free (*slot);
1138       *slot = NULL;
1139     }
1140
1141   return 1;
1142 }
1143
1144 /* Traverses MAP and removes from it all the tuples (OLD, NEW) where
1145    NEW is defined in LOOP.  Inserts on the exit of LOOP the close phi
1146    node "RES = phi (NEW)" corresponding to "OLD_RES = phi (OLD)" in
1147    the original code.  Inserts in MAP the tuple (OLD_RES, RES).  */
1148
1149 void
1150 insert_loop_close_phis (htab_t map, loop_p loop)
1151 {
1152   int i;
1153   struct alep a;
1154   rename_map_elt elt;
1155
1156   a.loop = loop;
1157   a.new_renames = VEC_alloc (rename_map_elt, heap, 3);
1158   update_ssa (TODO_update_ssa);
1159   htab_traverse (map, add_loop_exit_phis, &a);
1160   update_ssa (TODO_update_ssa);
1161
1162   for (i = 0; VEC_iterate (rename_map_elt, a.new_renames, i, elt); i++)
1163     {
1164       set_rename (map, elt->old_name, elt->expr);
1165       free (elt);
1166     }
1167
1168   VEC_free (rename_map_elt, heap, a.new_renames);
1169 }
1170
1171 /* Helper structure for htab_traverse in insert_guard_phis.  */
1172
1173 struct igp {
1174   basic_block bb;
1175   edge true_edge, false_edge;
1176   htab_t before_guard;
1177 };
1178
1179 /* Return the default name that is before the guard.  */
1180
1181 static tree
1182 default_before_guard (htab_t before_guard, tree old_name)
1183 {
1184   tree res = get_rename (before_guard, old_name);
1185
1186   if (res == old_name)
1187     {
1188       if (is_gimple_reg (res))
1189         return fold_convert (TREE_TYPE (res), integer_zero_node);
1190       return gimple_default_def (cfun, SSA_NAME_VAR (res));
1191     }
1192
1193   return res;
1194 }
1195
1196 /* Prepares EXPR to be a good phi argument when the phi result is
1197    RES.  Insert needed statements on edge E.  */
1198
1199 static tree
1200 convert_for_phi_arg (tree expr, tree res, edge e)
1201 {
1202   tree type = TREE_TYPE (res);
1203
1204   if (TREE_TYPE (expr) != type)
1205     expr = fold_convert (type, expr);
1206
1207   if (TREE_CODE (expr) != SSA_NAME
1208       && !is_gimple_min_invariant (expr))
1209     {
1210       tree var = create_tmp_var (type, "var");
1211       gimple_seq stmts;
1212
1213       expr = build2 (MODIFY_EXPR, type, var, expr);
1214       expr = force_gimple_operand (expr, &stmts, true, NULL);
1215       gsi_insert_seq_on_edge_immediate (e, stmts);
1216     }
1217
1218   return expr;
1219 }
1220
1221 /* Helper function for htab_traverse in insert_guard_phis.  */
1222
1223 static int
1224 add_guard_exit_phis (void **slot, void *s)
1225 {
1226   struct rename_map_elt_s *entry = (struct rename_map_elt_s *) *slot;
1227   struct igp *i = (struct igp *) s;
1228   basic_block bb = i->bb;
1229   edge true_edge = i->true_edge;
1230   edge false_edge = i->false_edge;
1231   tree res = entry->old_name;
1232   tree name1 = entry->expr;
1233   tree name2 = default_before_guard (i->before_guard, res);
1234   gimple phi;
1235
1236   /* Nothing to be merged if the name before the guard is the same as
1237      the one after.  */
1238   if (name1 == name2)
1239     return 1;
1240
1241   name1 = convert_for_phi_arg (name1, res, true_edge);
1242   name2 = convert_for_phi_arg (name2, res, false_edge);
1243
1244   phi = create_phi_node (res, bb);
1245   res = create_new_def_for (gimple_phi_result (phi), phi,
1246                             gimple_phi_result_ptr (phi));
1247
1248   add_phi_arg (phi, name1, true_edge, UNKNOWN_LOCATION);
1249   add_phi_arg (phi, name2, false_edge, UNKNOWN_LOCATION);
1250
1251   entry->expr = res;
1252   *slot = entry;
1253   return 1;
1254 }
1255
1256 /* Iterate over RENAME_MAP and get tuples of the form (OLD, NAME1).
1257    If there is a correspondent tuple (OLD, NAME2) in BEFORE_GUARD,
1258    with NAME1 different than NAME2, then insert in BB the phi node:
1259
1260    | RES = phi (NAME1 (on TRUE_EDGE), NAME2 (on FALSE_EDGE))"
1261
1262    if there is no tuple for OLD in BEFORE_GUARD, insert
1263
1264    | RES = phi (NAME1 (on TRUE_EDGE),
1265    |            DEFAULT_DEFINITION of NAME1 (on FALSE_EDGE))".
1266
1267    Finally register in RENAME_MAP the tuple (OLD, RES).  */
1268
1269 void
1270 insert_guard_phis (basic_block bb, edge true_edge, edge false_edge,
1271                    htab_t before_guard, htab_t rename_map)
1272 {
1273   struct igp i;
1274   i.bb = bb;
1275   i.true_edge = true_edge;
1276   i.false_edge = false_edge;
1277   i.before_guard = before_guard;
1278
1279   update_ssa (TODO_update_ssa);
1280   htab_traverse (rename_map, add_guard_exit_phis, &i);
1281   update_ssa (TODO_update_ssa);
1282 }
1283
1284 /* Create a duplicate of the basic block BB.  NOTE: This does not
1285    preserve SSA form.  */
1286
1287 static void
1288 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb, htab_t map)
1289 {
1290   gimple_stmt_iterator gsi, gsi_tgt;
1291
1292   gsi_tgt = gsi_start_bb (new_bb);
1293   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1294     {
1295       def_operand_p def_p;
1296       ssa_op_iter op_iter;
1297       gimple stmt = gsi_stmt (gsi);
1298       gimple copy;
1299
1300       if (gimple_code (stmt) == GIMPLE_LABEL)
1301         continue;
1302
1303       /* Create a new copy of STMT and duplicate STMT's virtual
1304          operands.  */
1305       copy = gimple_copy (stmt);
1306       gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
1307       mark_sym_for_renaming (gimple_vop (cfun));
1308
1309       maybe_duplicate_eh_stmt (copy, stmt);
1310       gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
1311
1312       /* Create new names for all the definitions created by COPY and
1313          add replacement mappings for each new name.  */
1314       FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
1315         {
1316           tree old_name = DEF_FROM_PTR (def_p);
1317           tree new_name = create_new_def_for (old_name, copy, def_p);
1318           set_rename (map, old_name, new_name);
1319         }
1320     }
1321 }
1322
1323 /* Copies BB and includes in the copied BB all the statements that can
1324    be reached following the use-def chains from the memory accesses,
1325    and returns the next edge following this new block.  */
1326  
1327 edge
1328 copy_bb_and_scalar_dependences (basic_block bb, sese region,
1329                                 edge next_e, htab_t map)
1330 {
1331   basic_block new_bb = split_edge (next_e);
1332
1333   next_e = single_succ_edge (new_bb);
1334   graphite_copy_stmts_from_block (bb, new_bb, map);
1335   remove_condition (new_bb);
1336   remove_phi_nodes (new_bb);
1337   expand_scalar_variables (new_bb, region, map);
1338   rename_variables (new_bb, map);
1339
1340   return next_e;
1341 }
1342
1343 /* Returns the outermost loop in SCOP that contains BB.  */
1344
1345 struct loop *
1346 outermost_loop_in_sese (sese region, basic_block bb)
1347 {
1348   struct loop *nest;
1349
1350   nest = bb->loop_father;
1351   while (loop_outer (nest)
1352          && loop_in_sese_p (loop_outer (nest), region))
1353     nest = loop_outer (nest);
1354
1355   return nest;
1356 }
1357
1358 /* Sets the false region of an IF_REGION to REGION.  */
1359
1360 void
1361 if_region_set_false_region (ifsese if_region, sese region)
1362 {
1363   basic_block condition = if_region_get_condition_block (if_region);
1364   edge false_edge = get_false_edge_from_guard_bb (condition);
1365   basic_block dummy = false_edge->dest;
1366   edge entry_region = SESE_ENTRY (region);
1367   edge exit_region = SESE_EXIT (region);
1368   basic_block before_region = entry_region->src;
1369   basic_block last_in_region = exit_region->src;
1370   void **slot = htab_find_slot_with_hash (current_loops->exits, exit_region,
1371                                           htab_hash_pointer (exit_region),
1372                                           NO_INSERT);
1373
1374   entry_region->flags = false_edge->flags;
1375   false_edge->flags = exit_region->flags;
1376
1377   redirect_edge_pred (entry_region, condition);
1378   redirect_edge_pred (exit_region, before_region);
1379   redirect_edge_pred (false_edge, last_in_region);
1380   redirect_edge_succ (false_edge, single_succ (dummy));
1381   delete_basic_block (dummy);
1382
1383   exit_region->flags = EDGE_FALLTHRU;
1384   recompute_all_dominators ();
1385
1386   SESE_EXIT (region) = false_edge;
1387
1388   if (if_region->false_region)
1389     free (if_region->false_region);
1390   if_region->false_region = region;
1391
1392   if (slot)
1393     {
1394       struct loop_exit *loop_exit = GGC_CNEW (struct loop_exit);
1395
1396       memcpy (loop_exit, *((struct loop_exit **) slot), sizeof (struct loop_exit));
1397       htab_clear_slot (current_loops->exits, slot);
1398
1399       slot = htab_find_slot_with_hash (current_loops->exits, false_edge,
1400                                        htab_hash_pointer (false_edge),
1401                                        INSERT);
1402       loop_exit->e = false_edge;
1403       *slot = loop_exit;
1404       false_edge->src->loop_father->exits->next = loop_exit;
1405     }
1406 }
1407
1408 /* Creates an IFSESE with CONDITION on edge ENTRY.  */
1409
1410 ifsese
1411 create_if_region_on_edge (edge entry, tree condition)
1412 {
1413   edge e;
1414   edge_iterator ei;
1415   sese sese_region = XNEW (struct sese_s);
1416   sese true_region = XNEW (struct sese_s);
1417   sese false_region = XNEW (struct sese_s);
1418   ifsese if_region = XNEW (struct ifsese_s);
1419   edge exit = create_empty_if_region_on_edge (entry, condition);
1420
1421   if_region->region = sese_region;
1422   if_region->region->entry = entry;
1423   if_region->region->exit = exit;
1424
1425   FOR_EACH_EDGE (e, ei, entry->dest->succs)
1426     {
1427       if (e->flags & EDGE_TRUE_VALUE)
1428         {
1429           true_region->entry = e;
1430           true_region->exit = single_succ_edge (e->dest);
1431           if_region->true_region = true_region;
1432         }
1433       else if (e->flags & EDGE_FALSE_VALUE)
1434         {
1435           false_region->entry = e;
1436           false_region->exit = single_succ_edge (e->dest);
1437           if_region->false_region = false_region;
1438         }
1439     }
1440
1441   return if_region;
1442 }
1443
1444 /* Moves REGION in a condition expression:
1445    | if (1)
1446    |   ;
1447    | else
1448    |   REGION;
1449 */
1450
1451 ifsese
1452 move_sese_in_condition (sese region)
1453 {
1454   basic_block pred_block = split_edge (SESE_ENTRY (region));
1455   ifsese if_region;
1456
1457   SESE_ENTRY (region) = single_succ_edge (pred_block);
1458   if_region = create_if_region_on_edge (single_pred_edge (pred_block), integer_one_node);
1459   if_region_set_false_region (if_region, region);
1460
1461   return if_region;
1462 }
1463
1464 /* Returns the scalar evolution of T in REGION.  Every variable that
1465    is not defined in the REGION is considered a parameter.  */
1466
1467 tree
1468 scalar_evolution_in_region (sese region, loop_p loop, tree t)
1469 {
1470   gimple def;
1471   struct loop *def_loop;
1472   basic_block before = block_before_sese (region);
1473
1474   if (TREE_CODE (t) != SSA_NAME
1475       || loop_in_sese_p (loop, region))
1476     return instantiate_scev (before, loop,
1477                              analyze_scalar_evolution (loop, t));
1478
1479   if (!defined_in_sese_p (t, region))
1480     return t;
1481
1482   def = SSA_NAME_DEF_STMT (t);
1483   def_loop = loop_containing_stmt (def);
1484
1485   if (loop_in_sese_p (def_loop, region))
1486     {
1487       t = analyze_scalar_evolution (def_loop, t);
1488       def_loop = superloop_at_depth (def_loop, loop_depth (loop) + 1);
1489       t = compute_overall_effect_of_inner_loop (def_loop, t);
1490       return t;
1491     }
1492   else
1493     return instantiate_scev (before, loop, t);
1494 }