OSDN Git Service

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