OSDN Git Service

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