OSDN Git Service

* interface.c: Fix a comment typo.
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-loop-manip.c
1 /* High-level loop manipulation functions.
2    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3    
4 This file is part of GCC.
5    
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10    
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15    
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "output.h"
31 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-dump.h"
34 #include "timevar.h"
35 #include "cfgloop.h"
36 #include "tree-pass.h"
37 #include "cfglayout.h"
38 #include "tree-scalar-evolution.h"
39
40 /* Creates an induction variable with value BASE + STEP * iteration in LOOP.
41    It is expected that neither BASE nor STEP are shared with other expressions
42    (unless the sharing rules allow this).  Use VAR as a base var_decl for it
43    (if NULL, a new temporary will be created).  The increment will occur at
44    INCR_POS (after it if AFTER is true, before it otherwise).  INCR_POS and 
45    AFTER can be computed using standard_iv_increment_position.  The ssa versions
46    of the variable before and after increment will be stored in VAR_BEFORE and
47    VAR_AFTER (unless they are NULL).  */
48
49 void
50 create_iv (tree base, tree step, tree var, struct loop *loop,
51            block_stmt_iterator *incr_pos, bool after,
52            tree *var_before, tree *var_after)
53 {
54   tree stmt, initial, step1, stmts;
55   tree vb, va;
56   enum tree_code incr_op = PLUS_EXPR;
57
58   if (!var)
59     {
60       var = create_tmp_var (TREE_TYPE (base), "ivtmp");
61       add_referenced_tmp_var (var);
62     }
63
64   vb = make_ssa_name (var, NULL_TREE);
65   if (var_before)
66     *var_before = vb;
67   va = make_ssa_name (var, NULL_TREE);
68   if (var_after)
69     *var_after = va;
70
71   /* For easier readability of the created code, produce MINUS_EXPRs
72      when suitable.  */
73   if (TREE_CODE (step) == INTEGER_CST)
74     {
75       if (TYPE_UNSIGNED (TREE_TYPE (step)))
76         {
77           step1 = fold (build1 (NEGATE_EXPR, TREE_TYPE (step), step));
78           if (tree_int_cst_lt (step1, step))
79             {
80               incr_op = MINUS_EXPR;
81               step = step1;
82             }
83         }
84       else
85         {
86           if (!tree_expr_nonnegative_p (step)
87               && may_negate_without_overflow_p (step))
88             {
89               incr_op = MINUS_EXPR;
90               step = fold (build1 (NEGATE_EXPR, TREE_TYPE (step), step));
91             }
92         }
93     }
94
95   stmt = build2 (MODIFY_EXPR, void_type_node, va,
96                  build2 (incr_op, TREE_TYPE (base),
97                          vb, step));
98   SSA_NAME_DEF_STMT (va) = stmt;
99   if (after)
100     bsi_insert_after (incr_pos, stmt, BSI_NEW_STMT);
101   else
102     bsi_insert_before (incr_pos, stmt, BSI_NEW_STMT);
103
104   initial = force_gimple_operand (base, &stmts, true, var);
105   if (stmts)
106     {
107       edge pe = loop_preheader_edge (loop);
108
109       bsi_insert_on_edge_immediate_loop (pe, stmts);
110     }
111
112   stmt = create_phi_node (vb, loop->header);
113   SSA_NAME_DEF_STMT (vb) = stmt;
114   add_phi_arg (stmt, initial, loop_preheader_edge (loop));
115   add_phi_arg (stmt, va, loop_latch_edge (loop));
116 }
117
118 /* Add exit phis for the USE on EXIT.  */
119
120 static void
121 add_exit_phis_edge (basic_block exit, tree use)
122 {
123   tree phi, def_stmt = SSA_NAME_DEF_STMT (use);
124   basic_block def_bb = bb_for_stmt (def_stmt);
125   struct loop *def_loop;
126   edge e;
127   edge_iterator ei;
128
129   /* Check that some of the edges entering the EXIT block exits a loop in
130      that USE is defined.  */
131   FOR_EACH_EDGE (e, ei, exit->preds)
132     {
133       def_loop = find_common_loop (def_bb->loop_father, e->src->loop_father);
134       if (!flow_bb_inside_loop_p (def_loop, e->dest))
135         break;
136     }
137
138   if (!e)
139     return;
140
141   phi = create_phi_node (use, exit);
142
143   FOR_EACH_EDGE (e, ei, exit->preds)
144     add_phi_arg (phi, use, e);
145
146   SSA_NAME_DEF_STMT (use) = def_stmt;
147 }
148
149 /* Add exit phis for VAR that is used in LIVEIN.
150    Exits of the loops are stored in EXITS.  */
151
152 static void
153 add_exit_phis_var (tree var, bitmap livein, bitmap exits)
154 {
155   bitmap def;
156   unsigned index;
157   basic_block def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
158   bitmap_iterator bi;
159
160   if (is_gimple_reg (var))
161     bitmap_clear_bit (livein, def_bb->index);
162   else
163     bitmap_set_bit (livein, def_bb->index);
164
165   def = BITMAP_ALLOC (NULL);
166   bitmap_set_bit (def, def_bb->index);
167   compute_global_livein (livein, def);
168   BITMAP_FREE (def);
169
170   EXECUTE_IF_AND_IN_BITMAP (exits, livein, 0, index, bi)
171     {
172       add_exit_phis_edge (BASIC_BLOCK (index), var);
173     }
174 }
175
176 /* Add exit phis for the names marked in NAMES_TO_RENAME.
177    Exits of the loops are stored in EXITS.  Sets of blocks where the ssa
178    names are used are stored in USE_BLOCKS.  */
179
180 static void
181 add_exit_phis (bitmap names_to_rename, bitmap *use_blocks, bitmap loop_exits)
182 {
183   unsigned i;
184   bitmap_iterator bi;
185
186   EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi)
187     {
188       add_exit_phis_var (ssa_name (i), use_blocks[i], loop_exits);
189     }
190 }
191
192 /* Returns a bitmap of all loop exit edge targets.  */
193
194 static bitmap
195 get_loops_exits (void)
196 {
197   bitmap exits = BITMAP_ALLOC (NULL);
198   basic_block bb;
199   edge e;
200   edge_iterator ei;
201
202   FOR_EACH_BB (bb)
203     {
204       FOR_EACH_EDGE (e, ei, bb->preds)
205         if (e->src != ENTRY_BLOCK_PTR
206             && !flow_bb_inside_loop_p (e->src->loop_father, bb))
207           {
208             bitmap_set_bit (exits, bb->index);
209             break;
210           }
211     }
212
213   return exits;
214 }
215
216 /* For USE in BB, if it is used outside of the loop it is defined in,
217    mark it for rewrite.  Record basic block BB where it is used
218    to USE_BLOCKS.  */
219
220 static void
221 find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks)
222 {
223   unsigned ver;
224   basic_block def_bb;
225   struct loop *def_loop;
226
227   if (TREE_CODE (use) != SSA_NAME)
228     return;
229
230   ver = SSA_NAME_VERSION (use);
231   def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (use));
232   if (!def_bb)
233     return;
234   def_loop = def_bb->loop_father;
235
236   /* If the definition is not inside loop, it is not interesting.  */
237   if (!def_loop->outer)
238     return;
239
240   if (!use_blocks[ver])
241     use_blocks[ver] = BITMAP_ALLOC (NULL);
242   bitmap_set_bit (use_blocks[ver], bb->index);
243
244   if (!flow_bb_inside_loop_p (def_loop, bb))
245     mark_for_rewrite (use);
246 }
247
248 /* For uses in STMT, mark names that are used outside of the loop they are
249    defined to rewrite.  Record the set of blocks in that the ssa
250    names are defined to USE_BLOCKS.  */
251
252 static void
253 find_uses_to_rename_stmt (tree stmt, bitmap *use_blocks)
254 {
255   ssa_op_iter iter;
256   tree var;
257   basic_block bb = bb_for_stmt (stmt);
258
259   get_stmt_operands (stmt);
260
261   FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
262     find_uses_to_rename_use (bb, var, use_blocks);
263 }
264
265 /* Marks names that are used in BB and outside of the loop they are
266    defined in for rewrite.  Records the set of blocks in that the ssa
267    names are defined to USE_BLOCKS.  */
268
269 static void
270 find_uses_to_rename_bb (basic_block bb, bitmap *use_blocks)
271 {
272   block_stmt_iterator bsi;
273   edge e;
274   edge_iterator ei;
275   tree phi;
276
277   FOR_EACH_EDGE (e, ei, bb->succs)
278     for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
279       find_uses_to_rename_use (bb, PHI_ARG_DEF_FROM_EDGE (phi, e),
280                                use_blocks);
281  
282   for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
283     find_uses_to_rename_stmt (bsi_stmt (bsi), use_blocks);
284 }
285      
286 /* Marks names that are used outside of the loop they are defined in
287    for rewrite.  Records the set of blocks in that the ssa
288    names are defined to USE_BLOCKS.  If CHANGED_BBS is not NULL,
289    scan only blocks in this set.  */
290
291 static void
292 find_uses_to_rename (bitmap changed_bbs, bitmap *use_blocks)
293 {
294   basic_block bb;
295   unsigned index;
296   bitmap_iterator bi;
297
298   if (changed_bbs)
299     {
300       EXECUTE_IF_SET_IN_BITMAP (changed_bbs, 0, index, bi)
301         {
302           find_uses_to_rename_bb (BASIC_BLOCK (index), use_blocks);
303         }
304     }
305   else
306     {
307       FOR_EACH_BB (bb)
308         {
309           find_uses_to_rename_bb (bb, use_blocks);
310         }
311     }
312 }
313
314 /* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
315    phi nodes to ensure that no variable is used outside the loop it is
316    defined in.
317
318    This strengthening of the basic ssa form has several advantages:
319
320    1) Updating it during unrolling/peeling/versioning is trivial, since
321       we do not need to care about the uses outside of the loop.
322    2) The behavior of all uses of an induction variable is the same.
323       Without this, you need to distinguish the case when the variable
324       is used outside of the loop it is defined in, for example
325
326       for (i = 0; i < 100; i++)
327         {
328           for (j = 0; j < 100; j++)
329             {
330               k = i + j;
331               use1 (k);
332             }
333           use2 (k);
334         }
335
336       Looking from the outer loop with the normal SSA form, the first use of k
337       is not well-behaved, while the second one is an induction variable with
338       base 99 and step 1.
339       
340       If CHANGED_BBS is not NULL, we look for uses outside loops only in
341       the basic blocks in this set.  */
342
343 void
344 rewrite_into_loop_closed_ssa (bitmap changed_bbs)
345 {
346   bitmap loop_exits = get_loops_exits ();
347   bitmap *use_blocks;
348   unsigned i;
349   bitmap names_to_rename;
350
351   gcc_assert (!any_marked_for_rewrite_p ());
352
353   use_blocks = xcalloc (num_ssa_names, sizeof (bitmap));
354
355   /* Find the uses outside loops.  */
356   find_uses_to_rename (changed_bbs, use_blocks);
357
358   if (!any_marked_for_rewrite_p ())
359     {
360       free (use_blocks);
361       BITMAP_FREE (loop_exits);
362       return;
363     }
364
365   /* Add the phi nodes on exits of the loops for the names we need to
366      rewrite.  */
367   names_to_rename = marked_ssa_names ();
368   add_exit_phis (names_to_rename, use_blocks, loop_exits);
369
370   for (i = 0; i < num_ssa_names; i++)
371     BITMAP_FREE (use_blocks[i]);
372   free (use_blocks);
373   BITMAP_FREE (loop_exits);
374   BITMAP_FREE (names_to_rename);
375
376   /* Do the rewriting.  */
377   rewrite_ssa_into_ssa ();
378 }
379
380 /* Check invariants of the loop closed ssa form for the USE in BB.  */
381
382 static void
383 check_loop_closed_ssa_use (basic_block bb, tree use)
384 {
385   tree def;
386   basic_block def_bb;
387   
388   if (TREE_CODE (use) != SSA_NAME)
389     return;
390
391   def = SSA_NAME_DEF_STMT (use);
392   def_bb = bb_for_stmt (def);
393   gcc_assert (!def_bb
394               || flow_bb_inside_loop_p (def_bb->loop_father, bb));
395 }
396
397 /* Checks invariants of loop closed ssa form in statement STMT in BB.  */
398
399 static void
400 check_loop_closed_ssa_stmt (basic_block bb, tree stmt)
401 {
402   ssa_op_iter iter;
403   tree var;
404
405   get_stmt_operands (stmt);
406
407   FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES)
408     check_loop_closed_ssa_use (bb, var);
409 }
410
411 /* Checks that invariants of the loop closed ssa form are preserved.  */
412
413 void
414 verify_loop_closed_ssa (void)
415 {
416   basic_block bb;
417   block_stmt_iterator bsi;
418   tree phi;
419   unsigned i;
420
421   verify_ssa (false);
422
423   FOR_EACH_BB (bb)
424     {
425       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
426         for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
427           check_loop_closed_ssa_use (PHI_ARG_EDGE (phi, i)->src,
428                                      PHI_ARG_DEF (phi, i));
429
430       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
431         check_loop_closed_ssa_stmt (bb, bsi_stmt (bsi));
432     }
433 }
434
435 /* Split loop exit edge EXIT.  The things are a bit complicated by a need to
436    preserve the loop closed ssa form.  */
437
438 void
439 split_loop_exit_edge (edge exit)
440 {
441   basic_block dest = exit->dest;
442   basic_block bb = loop_split_edge_with (exit, NULL);
443   tree phi, new_phi, new_name, name;
444   use_operand_p op_p;
445
446   for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
447     {
448       op_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (bb));
449
450       name = USE_FROM_PTR (op_p);
451
452       /* If the argument of the phi node is a constant, we do not need
453          to keep it inside loop.  */
454       if (TREE_CODE (name) != SSA_NAME)
455         continue;
456
457       /* Otherwise create an auxiliary phi node that will copy the value
458          of the ssa name out of the loop.  */
459       new_name = duplicate_ssa_name (name, NULL);
460       new_phi = create_phi_node (new_name, bb);
461       SSA_NAME_DEF_STMT (new_name) = new_phi;
462       add_phi_arg (new_phi, name, exit);
463       SET_USE (op_p, new_name);
464     }
465 }
466
467 /* Insert statement STMT to the edge E and update the loop structures.
468    Returns the newly created block (if any).  */
469
470 basic_block
471 bsi_insert_on_edge_immediate_loop (edge e, tree stmt)
472 {
473   basic_block src, dest, new_bb;
474   struct loop *loop_c;
475
476   src = e->src;
477   dest = e->dest;
478
479   loop_c = find_common_loop (src->loop_father, dest->loop_father);
480
481   new_bb = bsi_insert_on_edge_immediate (e, stmt);
482
483   if (!new_bb)
484     return NULL;
485
486   add_bb_to_loop (new_bb, loop_c);
487   if (dest->loop_father->latch == src)
488     dest->loop_father->latch = new_bb;
489
490   return new_bb;
491 }
492
493 /* Returns the basic block in that statements should be emitted for induction
494    variables incremented at the end of the LOOP.  */
495
496 basic_block
497 ip_end_pos (struct loop *loop)
498 {
499   return loop->latch;
500 }
501
502 /* Returns the basic block in that statements should be emitted for induction
503    variables incremented just before exit condition of a LOOP.  */
504
505 basic_block
506 ip_normal_pos (struct loop *loop)
507 {
508   tree last;
509   basic_block bb;
510   edge exit;
511
512   if (!single_pred_p (loop->latch))
513     return NULL;
514
515   bb = single_pred (loop->latch);
516   last = last_stmt (bb);
517   if (TREE_CODE (last) != COND_EXPR)
518     return NULL;
519
520   exit = EDGE_SUCC (bb, 0);
521   if (exit->dest == loop->latch)
522     exit = EDGE_SUCC (bb, 1);
523
524   if (flow_bb_inside_loop_p (loop, exit->dest))
525     return NULL;
526
527   return bb;
528 }
529
530 /* Stores the standard position for induction variable increment in LOOP
531    (just before the exit condition if it is available and latch block is empty,
532    end of the latch block otherwise) to BSI.  INSERT_AFTER is set to true if
533    the increment should be inserted after *BSI.  */
534
535 void
536 standard_iv_increment_position (struct loop *loop, block_stmt_iterator *bsi,
537                                 bool *insert_after)
538 {
539   basic_block bb = ip_normal_pos (loop), latch = ip_end_pos (loop);
540   tree last = last_stmt (latch);
541
542   if (!bb
543       || (last && TREE_CODE (last) != LABEL_EXPR))
544     {
545       *bsi = bsi_last (latch);
546       *insert_after = true;
547     }
548   else
549     {
550       *bsi = bsi_last (bb);
551       *insert_after = false;
552     }
553 }
554
555 /* Copies phi node arguments for duplicated blocks.  The index of the first
556    duplicated block is FIRST_NEW_BLOCK.  */
557
558 static void
559 copy_phi_node_args (unsigned first_new_block)
560 {
561   unsigned i;
562
563   for (i = first_new_block; i < (unsigned) last_basic_block; i++)
564     BASIC_BLOCK (i)->rbi->duplicated = 1;
565
566   for (i = first_new_block; i < (unsigned) last_basic_block; i++)
567     add_phi_args_after_copy_bb (BASIC_BLOCK (i));
568
569   for (i = first_new_block; i < (unsigned) last_basic_block; i++)
570     BASIC_BLOCK (i)->rbi->duplicated = 0;
571 }
572
573 /* Renames variables in the area copied by tree_duplicate_loop_to_header_edge.
574    FIRST_NEW_BLOCK is the first block in the copied area.   DEFINITIONS is
575    a bitmap of all ssa names defined inside the loop.  */
576
577 static void
578 rename_variables (unsigned first_new_block, bitmap definitions)
579 {
580   unsigned i, copy_number = 0;
581   basic_block bb;
582   htab_t ssa_name_map = NULL;
583
584   for (i = first_new_block; i < (unsigned) last_basic_block; i++)
585     {
586       bb = BASIC_BLOCK (i);
587
588       /* We assume that first come all blocks from the first copy, then all
589          blocks from the second copy, etc.  */
590       if (copy_number != (unsigned) bb->rbi->copy_number)
591         {
592           allocate_ssa_names (definitions, &ssa_name_map);
593           copy_number = bb->rbi->copy_number;
594         }
595
596       rewrite_to_new_ssa_names_bb (bb, ssa_name_map);
597     }
598
599   htab_delete (ssa_name_map);
600 }
601
602 /* Sets SSA_NAME_DEF_STMT for results of all phi nodes in BB.  */
603
604 static void
605 set_phi_def_stmts (basic_block bb)
606 {
607   tree phi;
608
609   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
610     SSA_NAME_DEF_STMT (PHI_RESULT (phi)) = phi;
611 }
612
613 /* The same as cfgloopmanip.c:duplicate_loop_to_header_edge, but also updates
614    ssa.  In order to achieve this, only loops whose exits all lead to the same
615    location are handled.
616    
617    FIXME: we create some degenerate phi nodes that could be avoided by copy
618    propagating them instead.  Unfortunately this is not completely
619    straightforward due to problems with constant folding.  */
620
621 bool
622 tree_duplicate_loop_to_header_edge (struct loop *loop, edge e,
623                                     struct loops *loops,
624                                     unsigned int ndupl, sbitmap wont_exit,
625                                     edge orig, edge *to_remove,
626                                     unsigned int *n_to_remove, int flags)
627 {
628   unsigned first_new_block;
629   basic_block bb;
630   unsigned i;
631   bitmap definitions;
632
633   if (!(loops->state & LOOPS_HAVE_SIMPLE_LATCHES))
634     return false;
635   if (!(loops->state & LOOPS_HAVE_PREHEADERS))
636     return false;
637
638 #ifdef ENABLE_CHECKING
639   verify_loop_closed_ssa ();
640 #endif
641
642   gcc_assert (!any_marked_for_rewrite_p ());
643
644   first_new_block = last_basic_block;
645   if (!duplicate_loop_to_header_edge (loop, e, loops, ndupl, wont_exit,
646                                       orig, to_remove, n_to_remove, flags))
647     return false;
648
649   /* Readd the removed phi args for e.  */
650   flush_pending_stmts (e);
651
652   /* Copy the phi node arguments.  */
653   copy_phi_node_args (first_new_block);
654
655   /* Rename the variables.  */
656   definitions = marked_ssa_names ();
657   rename_variables (first_new_block, definitions);
658   unmark_all_for_rewrite ();
659   BITMAP_FREE (definitions);
660
661   /* For some time we have the identical ssa names as results in multiple phi
662      nodes.  When phi node is resized, it sets SSA_NAME_DEF_STMT of its result
663      to the new copy.  This means that we cannot easily ensure that the ssa
664      names defined in those phis are pointing to the right one -- so just
665      recompute SSA_NAME_DEF_STMT for them.  */ 
666
667   for (i = first_new_block; i < (unsigned) last_basic_block; i++)
668     {
669       bb = BASIC_BLOCK (i);
670       set_phi_def_stmts (bb);
671       if (bb->rbi->copy_number == 1)
672         set_phi_def_stmts (bb->rbi->original);
673     }
674
675   scev_reset ();
676 #ifdef ENABLE_CHECKING
677   verify_loop_closed_ssa ();
678 #endif
679
680   return true;
681 }
682