Create startup files from the GCC sources and drop our versions.
[dragonfly.git] / contrib / gcc-4.0 / gcc / tree-ssa-loop-im.c
1 /* Loop invariant motion.
2    Copyright (C) 2003, 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 "domwalk.h"
37 #include "params.h"
38 #include "tree-pass.h"
39 #include "flags.h"
40
41 /* TODO:  Support for predicated code motion.  I.e.
42
43    while (1)
44      {
45        if (cond)
46          {
47            a = inv;
48            something;
49          }
50      }
51
52    Where COND and INV are is invariants, but evaluating INV may trap or be
53    invalid from some other reason if !COND.  This may be transformed to
54
55    if (cond)
56      a = inv;
57    while (1)
58      {
59        if (cond)
60          something;
61      }  */
62
63 /* A type for the list of statements that have to be moved in order to be able
64    to hoist an invariant computation.  */
65
66 struct depend
67 {
68   tree stmt;
69   struct depend *next;
70 };
71
72 /* The auxiliary data kept for each statement.  */
73
74 struct lim_aux_data
75 {
76   struct loop *max_loop;        /* The outermost loop in that the statement
77                                    is invariant.  */
78
79   struct loop *tgt_loop;        /* The loop out of that we want to move the
80                                    invariant.  */
81
82   struct loop *always_executed_in;
83                                 /* The outermost loop for that we are sure
84                                    the statement is executed if the loop
85                                    is entered.  */
86
87   bool sm_done;                 /* True iff the store motion for a memory
88                                    reference in the statement has already
89                                    been executed.  */
90
91   unsigned cost;                /* Cost of the computation performed by the
92                                    statement.  */
93
94   struct depend *depends;       /* List of statements that must be also hoisted
95                                    out of the loop when this statement is
96                                    hoisted; i.e. those that define the operands
97                                    of the statement and are inside of the
98                                    MAX_LOOP loop.  */
99 };
100
101 #define LIM_DATA(STMT) (TREE_CODE (STMT) == PHI_NODE \
102                         ? NULL \
103                         : (struct lim_aux_data *) (stmt_ann (STMT)->common.aux))
104
105 /* Description of a memory reference for store motion.  */
106
107 struct mem_ref
108 {
109   tree *ref;                    /* The reference itself.  */
110   tree stmt;                    /* The statement in that it occurs.  */
111   struct mem_ref *next;         /* Next use in the chain.  */
112 };
113
114 /* Minimum cost of an expensive expression.  */
115 #define LIM_EXPENSIVE ((unsigned) PARAM_VALUE (PARAM_LIM_EXPENSIVE))
116
117 /* The outermost loop for that execution of the header guarantees that the
118    block will be executed.  */
119 #define ALWAYS_EXECUTED_IN(BB) ((struct loop *) (BB)->aux)
120
121 static unsigned max_stmt_uid;   /* Maximal uid of a statement.  Uids to phi
122                                    nodes are assigned using the versions of
123                                    ssa names they define.  */
124
125 /* Returns uid of statement STMT.  */
126
127 static unsigned
128 get_stmt_uid (tree stmt)
129 {
130   if (TREE_CODE (stmt) == PHI_NODE)
131     return SSA_NAME_VERSION (PHI_RESULT (stmt)) + max_stmt_uid;
132
133   return stmt_ann (stmt)->uid;
134 }
135
136 /* Calls CBCK for each index in memory reference ADDR_P.  There are two
137    kinds situations handled; in each of these cases, the memory reference
138    and DATA are passed to the callback:
139    
140    Access to an array: ARRAY_{RANGE_}REF (base, index).  In this case we also
141    pass the pointer to the index to the callback.
142
143    Pointer dereference: INDIRECT_REF (addr).  In this case we also pass the
144    pointer to addr to the callback.
145    
146    If the callback returns false, the whole search stops and false is returned.
147    Otherwise the function returns true after traversing through the whole
148    reference *ADDR_P.  */
149
150 bool
151 for_each_index (tree *addr_p, bool (*cbck) (tree, tree *, void *), void *data)
152 {
153   tree *nxt, *idx;
154
155   for (; ; addr_p = nxt)
156     {
157       switch (TREE_CODE (*addr_p))
158         {
159         case SSA_NAME:
160           return cbck (*addr_p, addr_p, data);
161
162         case MISALIGNED_INDIRECT_REF:
163         case ALIGN_INDIRECT_REF:
164         case INDIRECT_REF:
165           nxt = &TREE_OPERAND (*addr_p, 0);
166           return cbck (*addr_p, nxt, data);
167
168         case BIT_FIELD_REF:
169         case VIEW_CONVERT_EXPR:
170         case ARRAY_RANGE_REF:
171         case REALPART_EXPR:
172         case IMAGPART_EXPR:
173           nxt = &TREE_OPERAND (*addr_p, 0);
174           break;
175
176         case COMPONENT_REF:
177           /* If the component has varying offset, it behaves like index
178              as well.  */
179           idx = &TREE_OPERAND (*addr_p, 2);
180           if (*idx
181               && !cbck (*addr_p, idx, data))
182             return false;
183
184           nxt = &TREE_OPERAND (*addr_p, 0);
185           break;
186
187         case ARRAY_REF:
188           nxt = &TREE_OPERAND (*addr_p, 0);
189           if (!cbck (*addr_p, &TREE_OPERAND (*addr_p, 1), data))
190             return false;
191           break;
192
193         case VAR_DECL:
194         case PARM_DECL:
195         case STRING_CST:
196         case RESULT_DECL:
197         case VECTOR_CST:
198           return true;
199
200         default:
201           gcc_unreachable ();
202         }
203     }
204 }
205
206 /* If it is possible to hoist the statement STMT unconditionally,
207    returns MOVE_POSSIBLE.
208    If it is possible to hoist the statement STMT, but we must avoid making
209    it executed if it would not be executed in the original program (e.g.
210    because it may trap), return MOVE_PRESERVE_EXECUTION.
211    Otherwise return MOVE_IMPOSSIBLE.  */
212
213 enum move_pos
214 movement_possibility (tree stmt)
215 {
216   tree lhs, rhs;
217
218   if (flag_unswitch_loops
219       && TREE_CODE (stmt) == COND_EXPR)
220     {
221       /* If we perform unswitching, force the operands of the invariant
222          condition to be moved out of the loop.  */
223       get_stmt_operands (stmt);
224
225       return MOVE_POSSIBLE;
226     }
227
228   if (TREE_CODE (stmt) != MODIFY_EXPR)
229     return MOVE_IMPOSSIBLE;
230
231   if (stmt_ends_bb_p (stmt))
232     return MOVE_IMPOSSIBLE;
233
234   get_stmt_operands (stmt);
235
236   if (stmt_ann (stmt)->has_volatile_ops)
237     return MOVE_IMPOSSIBLE;
238
239   lhs = TREE_OPERAND (stmt, 0);
240   if (TREE_CODE (lhs) == SSA_NAME
241       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
242     return MOVE_IMPOSSIBLE;
243
244   rhs = TREE_OPERAND (stmt, 1);
245
246   if (TREE_SIDE_EFFECTS (rhs))
247     return MOVE_IMPOSSIBLE;
248
249   if (TREE_CODE (lhs) != SSA_NAME
250       || tree_could_trap_p (rhs))
251     return MOVE_PRESERVE_EXECUTION;
252
253   if (get_call_expr_in (stmt))
254     {
255       /* While pure or const call is guaranteed to have no side effects, we
256          cannot move it arbitrarily.  Consider code like
257
258          char *s = something ();
259
260          while (1)
261            {
262              if (s)
263                t = strlen (s);
264              else
265                t = 0;
266            }
267
268          Here the strlen call cannot be moved out of the loop, even though
269          s is invariant.  In addition to possibly creating a call with
270          invalid arguments, moving out a function call that is not executed
271          may cause performance regressions in case the call is costly and
272          not executed at all.  */
273       return MOVE_PRESERVE_EXECUTION;
274     }
275   return MOVE_POSSIBLE;
276 }
277
278 /* Suppose that operand DEF is used inside the LOOP.  Returns the outermost
279    loop to that we could move the expression using DEF if it did not have
280    other operands, i.e. the outermost loop enclosing LOOP in that the value
281    of DEF is invariant.  */
282
283 static struct loop *
284 outermost_invariant_loop (tree def, struct loop *loop)
285 {
286   tree def_stmt;
287   basic_block def_bb;
288   struct loop *max_loop;
289
290   if (TREE_CODE (def) != SSA_NAME)
291     return superloop_at_depth (loop, 1);
292
293   def_stmt = SSA_NAME_DEF_STMT (def);
294   def_bb = bb_for_stmt (def_stmt);
295   if (!def_bb)
296     return superloop_at_depth (loop, 1);
297
298   max_loop = find_common_loop (loop, def_bb->loop_father);
299
300   if (LIM_DATA (def_stmt) && LIM_DATA (def_stmt)->max_loop)
301     max_loop = find_common_loop (max_loop,
302                                  LIM_DATA (def_stmt)->max_loop->outer);
303   if (max_loop == loop)
304     return NULL;
305   max_loop = superloop_at_depth (loop, max_loop->depth + 1);
306
307   return max_loop;
308 }
309
310 /* Returns the outermost superloop of LOOP in that the expression EXPR is
311    invariant.  */
312
313 static struct loop *
314 outermost_invariant_loop_expr (tree expr, struct loop *loop)
315 {
316   enum tree_code_class class = TREE_CODE_CLASS (TREE_CODE (expr));
317   unsigned i, nops;
318   struct loop *max_loop = superloop_at_depth (loop, 1), *aloop;
319
320   if (TREE_CODE (expr) == SSA_NAME
321       || TREE_CODE (expr) == INTEGER_CST
322       || is_gimple_min_invariant (expr))
323     return outermost_invariant_loop (expr, loop);
324
325   if (class != tcc_unary
326       && class != tcc_binary
327       && class != tcc_expression
328       && class != tcc_comparison)
329     return NULL;
330
331   nops = TREE_CODE_LENGTH (TREE_CODE (expr));
332   for (i = 0; i < nops; i++)
333     {
334       aloop = outermost_invariant_loop_expr (TREE_OPERAND (expr, i), loop);
335       if (!aloop)
336         return NULL;
337
338       if (flow_loop_nested_p (max_loop, aloop))
339         max_loop = aloop;
340     }
341
342   return max_loop;
343 }
344
345 /* DATA is a structure containing information associated with a statement
346    inside LOOP.  DEF is one of the operands of this statement.
347    
348    Find the outermost loop enclosing LOOP in that value of DEF is invariant
349    and record this in DATA->max_loop field.  If DEF itself is defined inside
350    this loop as well (i.e. we need to hoist it out of the loop if we want
351    to hoist the statement represented by DATA), record the statement in that
352    DEF is defined to the DATA->depends list.  Additionally if ADD_COST is true,
353    add the cost of the computation of DEF to the DATA->cost.
354    
355    If DEF is not invariant in LOOP, return false.  Otherwise return TRUE.  */
356
357 static bool
358 add_dependency (tree def, struct lim_aux_data *data, struct loop *loop,
359                 bool add_cost)
360 {
361   tree def_stmt = SSA_NAME_DEF_STMT (def);
362   basic_block def_bb = bb_for_stmt (def_stmt);
363   struct loop *max_loop;
364   struct depend *dep;
365
366   if (!def_bb)
367     return true;
368
369   max_loop = outermost_invariant_loop (def, loop);
370   if (!max_loop)
371     return false;
372
373   if (flow_loop_nested_p (data->max_loop, max_loop))
374     data->max_loop = max_loop;
375
376   if (!LIM_DATA (def_stmt))
377     return true;
378
379   if (add_cost
380       /* Only add the cost if the statement defining DEF is inside LOOP,
381          i.e. if it is likely that by moving the invariants dependent
382          on it, we will be able to avoid creating a new register for
383          it (since it will be only used in these dependent invariants).  */
384       && def_bb->loop_father == loop)
385     data->cost += LIM_DATA (def_stmt)->cost;
386
387   dep = xmalloc (sizeof (struct depend));
388   dep->stmt = def_stmt;
389   dep->next = data->depends;
390   data->depends = dep;
391
392   return true;
393 }
394
395 /* Returns an estimate for a cost of statement STMT.  TODO -- the values here
396    are just ad-hoc constants.  The estimates should be based on target-specific
397    values.  */
398
399 static unsigned
400 stmt_cost (tree stmt)
401 {
402   tree lhs, rhs;
403   unsigned cost = 1;
404
405   /* Always try to create possibilities for unswitching.  */
406   if (TREE_CODE (stmt) == COND_EXPR)
407     return LIM_EXPENSIVE;
408
409   lhs = TREE_OPERAND (stmt, 0);
410   rhs = TREE_OPERAND (stmt, 1);
411
412   /* Hoisting memory references out should almost surely be a win.  */
413   if (stmt_references_memory_p (stmt))
414     cost += 20;
415
416   switch (TREE_CODE (rhs))
417     {
418     case CALL_EXPR:
419       /* We should be hoisting calls if possible.  */
420
421       /* Unless the call is a builtin_constant_p; this always folds to a
422          constant, so moving it is useless.  */
423       rhs = get_callee_fndecl (rhs);
424       if (DECL_BUILT_IN_CLASS (rhs) == BUILT_IN_NORMAL
425           && DECL_FUNCTION_CODE (rhs) == BUILT_IN_CONSTANT_P)
426         return 0;
427
428       cost += 20;
429       break;
430
431     case MULT_EXPR:
432     case TRUNC_DIV_EXPR:
433     case CEIL_DIV_EXPR:
434     case FLOOR_DIV_EXPR:
435     case ROUND_DIV_EXPR:
436     case EXACT_DIV_EXPR:
437     case CEIL_MOD_EXPR:
438     case FLOOR_MOD_EXPR:
439     case ROUND_MOD_EXPR:
440     case TRUNC_MOD_EXPR:
441       /* Division and multiplication are usually expensive.  */
442       cost += 20;
443       break;
444
445     default:
446       break;
447     }
448
449   return cost;
450 }
451
452 /* Determine the outermost loop to that it is possible to hoist a statement
453    STMT and store it to LIM_DATA (STMT)->max_loop.  To do this we determine
454    the outermost loop in that the value computed by STMT is invariant.
455    If MUST_PRESERVE_EXEC is true, additionally choose such a loop that
456    we preserve the fact whether STMT is executed.  It also fills other related
457    information to LIM_DATA (STMT).
458    
459    The function returns false if STMT cannot be hoisted outside of the loop it
460    is defined in, and true otherwise.  */
461
462 static bool
463 determine_max_movement (tree stmt, bool must_preserve_exec)
464 {
465   basic_block bb = bb_for_stmt (stmt);
466   struct loop *loop = bb->loop_father;
467   struct loop *level;
468   struct lim_aux_data *lim_data = LIM_DATA (stmt);
469   tree val;
470   ssa_op_iter iter;
471   
472   if (must_preserve_exec)
473     level = ALWAYS_EXECUTED_IN (bb);
474   else
475     level = superloop_at_depth (loop, 1);
476   lim_data->max_loop = level;
477
478   FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_USE)
479     if (!add_dependency (val, lim_data, loop, true))
480       return false;
481
482   FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_KILLS)
483     if (!add_dependency (val, lim_data, loop, false))
484       return false;
485
486   lim_data->cost += stmt_cost (stmt);
487
488   return true;
489 }
490
491 /* Suppose that some statement in ORIG_LOOP is hoisted to the loop LEVEL,
492    and that one of the operands of this statement is computed by STMT.
493    Ensure that STMT (together with all the statements that define its
494    operands) is hoisted at least out of the loop LEVEL.  */
495
496 static void
497 set_level (tree stmt, struct loop *orig_loop, struct loop *level)
498 {
499   struct loop *stmt_loop = bb_for_stmt (stmt)->loop_father;
500   struct depend *dep;
501
502   stmt_loop = find_common_loop (orig_loop, stmt_loop);
503   if (LIM_DATA (stmt) && LIM_DATA (stmt)->tgt_loop)
504     stmt_loop = find_common_loop (stmt_loop,
505                                   LIM_DATA (stmt)->tgt_loop->outer);
506   if (flow_loop_nested_p (stmt_loop, level))
507     return;
508
509   gcc_assert (LIM_DATA (stmt));
510   gcc_assert (level == LIM_DATA (stmt)->max_loop
511               || flow_loop_nested_p (LIM_DATA (stmt)->max_loop, level));
512
513   LIM_DATA (stmt)->tgt_loop = level;
514   for (dep = LIM_DATA (stmt)->depends; dep; dep = dep->next)
515     set_level (dep->stmt, orig_loop, level);
516 }
517
518 /* Determines an outermost loop from that we want to hoist the statement STMT.
519    For now we chose the outermost possible loop.  TODO -- use profiling
520    information to set it more sanely.  */
521
522 static void
523 set_profitable_level (tree stmt)
524 {
525   set_level (stmt, bb_for_stmt (stmt)->loop_father, LIM_DATA (stmt)->max_loop);
526 }
527
528 /* Returns true if STMT is not a pure call.  */
529
530 static bool
531 nonpure_call_p (tree stmt)
532 {
533   tree call = get_call_expr_in (stmt);
534
535   if (!call)
536     return false;
537
538   return TREE_SIDE_EFFECTS (call) != 0;
539 }
540
541 /* Releases the memory occupied by DATA.  */
542
543 static void
544 free_lim_aux_data (struct lim_aux_data *data)
545 {
546   struct depend *dep, *next;
547
548   for (dep = data->depends; dep; dep = next)
549     {
550       next = dep->next;
551       free (dep);
552     }
553   free (data);
554 }
555
556 /* Determine the outermost loops in that statements in basic block BB are
557    invariant, and record them to the LIM_DATA associated with the statements.
558    Callback for walk_dominator_tree.  */
559
560 static void
561 determine_invariantness_stmt (struct dom_walk_data *dw_data ATTRIBUTE_UNUSED,
562                               basic_block bb)
563 {
564   enum move_pos pos;
565   block_stmt_iterator bsi;
566   tree stmt;
567   bool maybe_never = ALWAYS_EXECUTED_IN (bb) == NULL;
568   struct loop *outermost = ALWAYS_EXECUTED_IN (bb);
569
570   if (!bb->loop_father->outer)
571     return;
572
573   if (dump_file && (dump_flags & TDF_DETAILS))
574     fprintf (dump_file, "Basic block %d (loop %d -- depth %d):\n\n",
575              bb->index, bb->loop_father->num, bb->loop_father->depth);
576
577   for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
578     {
579       stmt = bsi_stmt (bsi);
580
581       pos = movement_possibility (stmt);
582       if (pos == MOVE_IMPOSSIBLE)
583         {
584           if (nonpure_call_p (stmt))
585             {
586               maybe_never = true;
587               outermost = NULL;
588             }
589           continue;
590         }
591
592       stmt_ann (stmt)->common.aux = xcalloc (1, sizeof (struct lim_aux_data));
593       LIM_DATA (stmt)->always_executed_in = outermost;
594
595       if (maybe_never && pos == MOVE_PRESERVE_EXECUTION)
596         continue;
597
598       if (!determine_max_movement (stmt, pos == MOVE_PRESERVE_EXECUTION))
599         {
600           LIM_DATA (stmt)->max_loop = NULL;
601           continue;
602         }
603
604       if (dump_file && (dump_flags & TDF_DETAILS))
605         {
606           print_generic_stmt_indented (dump_file, stmt, 0, 2);
607           fprintf (dump_file, "  invariant up to level %d, cost %d.\n\n",
608                    LIM_DATA (stmt)->max_loop->depth,
609                    LIM_DATA (stmt)->cost);
610         }
611
612       if (LIM_DATA (stmt)->cost >= LIM_EXPENSIVE)
613         set_profitable_level (stmt);
614     }
615 }
616
617 /* For each statement determines the outermost loop in that it is invariant,
618    statements on whose motion it depends and the cost of the computation.
619    This information is stored to the LIM_DATA structure associated with
620    each statement.  */
621
622 static void
623 determine_invariantness (void)
624 {
625   struct dom_walk_data walk_data;
626
627   memset (&walk_data, 0, sizeof (struct dom_walk_data));
628   walk_data.before_dom_children_before_stmts = determine_invariantness_stmt;
629
630   init_walk_dominator_tree (&walk_data);
631   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
632   fini_walk_dominator_tree (&walk_data);
633 }
634
635 /* Commits edge insertions and updates loop structures.  */
636
637 void
638 loop_commit_inserts (void)
639 {
640   unsigned old_last_basic_block, i;
641   basic_block bb;
642
643   old_last_basic_block = last_basic_block;
644   bsi_commit_edge_inserts ();
645   for (i = old_last_basic_block; i < (unsigned) last_basic_block; i++)
646     {
647       bb = BASIC_BLOCK (i);
648       add_bb_to_loop (bb,
649                       find_common_loop (EDGE_SUCC (bb, 0)->dest->loop_father,
650                                         EDGE_PRED (bb, 0)->src->loop_father));
651     }
652 }
653
654 /* Hoist the statements in basic block BB out of the loops prescribed by
655    data stored in LIM_DATA structures associated with each statement.  Callback
656    for walk_dominator_tree.  */
657
658 static void
659 move_computations_stmt (struct dom_walk_data *dw_data ATTRIBUTE_UNUSED,
660                         basic_block bb)
661 {
662   struct loop *level;
663   block_stmt_iterator bsi;
664   tree stmt;
665   unsigned cost = 0;
666
667   if (!bb->loop_father->outer)
668     return;
669
670   for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
671     {
672       stmt = bsi_stmt (bsi);
673
674       if (!LIM_DATA (stmt))
675         {
676           bsi_next (&bsi);
677           continue;
678         }
679
680       cost = LIM_DATA (stmt)->cost;
681       level = LIM_DATA (stmt)->tgt_loop;
682       free_lim_aux_data (LIM_DATA (stmt));
683       stmt_ann (stmt)->common.aux = NULL;
684
685       if (!level)
686         {
687           bsi_next (&bsi);
688           continue;
689         }
690
691       /* We do not really want to move conditionals out of the loop; we just
692          placed it here to force its operands to be moved if necessary.  */
693       if (TREE_CODE (stmt) == COND_EXPR)
694         continue;
695
696       if (dump_file && (dump_flags & TDF_DETAILS))
697         {
698           fprintf (dump_file, "Moving statement\n");
699           print_generic_stmt (dump_file, stmt, 0);
700           fprintf (dump_file, "(cost %u) out of loop %d.\n\n",
701                    cost, level->num);
702         }
703       bsi_insert_on_edge (loop_preheader_edge (level), stmt);
704       bsi_remove (&bsi);
705     }
706 }
707
708 /* Hoist the statements out of the loops prescribed by data stored in
709    LIM_DATA structures associated with each statement.*/
710
711 static void
712 move_computations (void)
713 {
714   struct dom_walk_data walk_data;
715
716   memset (&walk_data, 0, sizeof (struct dom_walk_data));
717   walk_data.before_dom_children_before_stmts = move_computations_stmt;
718
719   init_walk_dominator_tree (&walk_data);
720   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
721   fini_walk_dominator_tree (&walk_data);
722
723   loop_commit_inserts ();
724   rewrite_into_ssa (false);
725   if (!bitmap_empty_p (vars_to_rename))
726     {
727       /* The rewrite of ssa names may cause violation of loop closed ssa
728          form invariants.  TODO -- avoid these rewrites completely.
729          Information in virtual phi nodes is sufficient for it.  */
730       rewrite_into_loop_closed_ssa ();
731     }
732   bitmap_clear (vars_to_rename);
733 }
734
735 /* Checks whether the statement defining variable *INDEX can be hoisted
736    out of the loop passed in DATA.  Callback for for_each_index.  */
737
738 static bool
739 may_move_till (tree ref, tree *index, void *data)
740 {
741   struct loop *loop = data, *max_loop;
742
743   /* If REF is an array reference, check also that the step and the lower
744      bound is invariant in LOOP.  */
745   if (TREE_CODE (ref) == ARRAY_REF)
746     {
747       tree step = array_ref_element_size (ref);
748       tree lbound = array_ref_low_bound (ref);
749
750       max_loop = outermost_invariant_loop_expr (step, loop);
751       if (!max_loop)
752         return false;
753
754       max_loop = outermost_invariant_loop_expr (lbound, loop);
755       if (!max_loop)
756         return false;
757     }
758
759   max_loop = outermost_invariant_loop (*index, loop);
760   if (!max_loop)
761     return false;
762
763   return true;
764 }
765
766 /* Forces statements defining (invariant) SSA names in expression EXPR to be
767    moved out of the LOOP.  ORIG_LOOP is the loop in that EXPR is used.  */
768
769 static void
770 force_move_till_expr (tree expr, struct loop *orig_loop, struct loop *loop)
771 {
772   enum tree_code_class class = TREE_CODE_CLASS (TREE_CODE (expr));
773   unsigned i, nops;
774
775   if (TREE_CODE (expr) == SSA_NAME)
776     {
777       tree stmt = SSA_NAME_DEF_STMT (expr);
778       if (IS_EMPTY_STMT (stmt))
779         return;
780
781       set_level (stmt, orig_loop, loop);
782       return;
783     }
784
785   if (class != tcc_unary
786       && class != tcc_binary
787       && class != tcc_expression
788       && class != tcc_comparison)
789     return;
790
791   nops = TREE_CODE_LENGTH (TREE_CODE (expr));
792   for (i = 0; i < nops; i++)
793     force_move_till_expr (TREE_OPERAND (expr, i), orig_loop, loop);
794 }
795
796 /* Forces statement defining invariants in REF (and *INDEX) to be moved out of
797    the LOOP.  The reference REF is used in the loop ORIG_LOOP.  Callback for
798    for_each_index.  */
799
800 struct fmt_data
801 {
802   struct loop *loop;
803   struct loop *orig_loop;
804 };
805
806 static bool
807 force_move_till (tree ref, tree *index, void *data)
808 {
809   tree stmt;
810   struct fmt_data *fmt_data = data;
811
812   if (TREE_CODE (ref) == ARRAY_REF)
813     {
814       tree step = array_ref_element_size (ref);
815       tree lbound = array_ref_low_bound (ref);
816
817       force_move_till_expr (step, fmt_data->orig_loop, fmt_data->loop);
818       force_move_till_expr (lbound, fmt_data->orig_loop, fmt_data->loop);
819     }
820
821   if (TREE_CODE (*index) != SSA_NAME)
822     return true;
823
824   stmt = SSA_NAME_DEF_STMT (*index);
825   if (IS_EMPTY_STMT (stmt))
826     return true;
827
828   set_level (stmt, fmt_data->orig_loop, fmt_data->loop);
829
830   return true;
831 }
832
833 /* Records memory reference *REF (that occurs in statement STMT)
834    to the list MEM_REFS.  */
835
836 static void
837 record_mem_ref (struct mem_ref **mem_refs, tree stmt, tree *ref)
838 {
839   struct mem_ref *aref = xmalloc (sizeof (struct mem_ref));
840
841   aref->stmt = stmt;
842   aref->ref = ref;
843
844   aref->next = *mem_refs;
845   *mem_refs = aref;
846 }
847
848 /* Releases list of memory references MEM_REFS.  */
849
850 static void
851 free_mem_refs (struct mem_ref *mem_refs)
852 {
853   struct mem_ref *act;
854
855   while (mem_refs)
856     {
857       act = mem_refs;
858       mem_refs = mem_refs->next;
859       free (act);
860     }
861 }
862
863 /* If VAR is defined in LOOP and the statement it is defined in does not belong
864    to the set SEEN, add the statement to QUEUE of length IN_QUEUE and
865    to the set SEEN.  */
866
867 static void
868 maybe_queue_var (tree var, struct loop *loop,
869                  sbitmap seen, tree *queue, unsigned *in_queue)
870 {
871   tree stmt = SSA_NAME_DEF_STMT (var);
872   basic_block def_bb = bb_for_stmt (stmt);
873               
874   if (!def_bb
875       || !flow_bb_inside_loop_p (loop, def_bb)
876       || TEST_BIT (seen, get_stmt_uid (stmt)))
877     return;
878           
879   SET_BIT (seen, get_stmt_uid (stmt));
880   queue[(*in_queue)++] = stmt;
881 }
882
883 /* If COMMON_REF is NULL, set COMMON_REF to *OP and return true.
884    Otherwise return true if the memory reference *OP is equal to COMMON_REF.
885    Record the reference OP to list MEM_REFS.  STMT is the statement in that
886    the reference occurs.  */
887
888 struct sra_data
889 {
890   struct mem_ref **mem_refs;
891   tree common_ref;
892   tree stmt;
893 };
894
895 static bool
896 fem_single_reachable_address (tree *op, void *data)
897 {
898   struct sra_data *sra_data = data;
899
900   if (sra_data->common_ref
901       && !operand_equal_p (*op, sra_data->common_ref, 0))
902     return false;
903   sra_data->common_ref = *op;
904
905   record_mem_ref (sra_data->mem_refs, sra_data->stmt, op);
906   return true;
907 }
908
909 /* Runs CALLBACK for each operand of STMT that is a memory reference.  DATA
910    is passed to the CALLBACK as well.  The traversal stops if CALLBACK
911    returns false, for_each_memref then returns false as well.  Otherwise
912    for_each_memref returns true.  */
913
914 static bool
915 for_each_memref (tree stmt, bool (*callback)(tree *, void *), void *data)
916 {
917   tree *op;
918
919   if (TREE_CODE (stmt) == RETURN_EXPR)
920     stmt = TREE_OPERAND (stmt, 1);
921
922   if (TREE_CODE (stmt) == MODIFY_EXPR)
923     {
924       op = &TREE_OPERAND (stmt, 0);
925       if (TREE_CODE (*op) != SSA_NAME
926           && !callback (op, data))
927         return false;
928
929       op = &TREE_OPERAND (stmt, 1);
930       if (TREE_CODE (*op) != SSA_NAME
931           && is_gimple_lvalue (*op)
932           && !callback (op, data))
933         return false;
934
935       stmt = TREE_OPERAND (stmt, 1);
936     }
937
938   if (TREE_CODE (stmt) == WITH_SIZE_EXPR)
939     stmt = TREE_OPERAND (stmt, 0);
940
941   if (TREE_CODE (stmt) == CALL_EXPR)
942     {
943       tree args;
944
945       for (args = TREE_OPERAND (stmt, 1); args; args = TREE_CHAIN (args))
946         {
947           op = &TREE_VALUE (args);
948
949           if (TREE_CODE (*op) != SSA_NAME
950               && is_gimple_lvalue (*op)
951               && !callback (op, data))
952             return false;
953         }
954     }
955
956   return true;
957 }
958
959 /* Determine whether all memory references inside the LOOP that correspond
960    to virtual ssa names defined in statement STMT are equal.
961    If so, store the list of the references to MEM_REFS, and return one
962    of them.  Otherwise store NULL to MEM_REFS and return NULL_TREE.
963    *SEEN_CALL_STMT is set to true if the virtual operands suggest
964    that the reference might be clobbered by a call inside the LOOP.  */
965
966 static tree
967 single_reachable_address (struct loop *loop, tree stmt,
968                           struct mem_ref **mem_refs,
969                           bool *seen_call_stmt)
970 {
971   unsigned max_uid = max_stmt_uid + num_ssa_names;
972   tree *queue = xmalloc (sizeof (tree) * max_uid);
973   sbitmap seen = sbitmap_alloc (max_uid);
974   unsigned in_queue = 1;
975   dataflow_t df;
976   unsigned i, n;
977   struct sra_data sra_data;
978   tree call;
979   tree val;
980   ssa_op_iter iter;
981
982   sbitmap_zero (seen);
983
984   *mem_refs = NULL;
985   sra_data.mem_refs = mem_refs;
986   sra_data.common_ref = NULL_TREE;
987
988   queue[0] = stmt;
989   SET_BIT (seen, get_stmt_uid (stmt));
990   *seen_call_stmt = false;
991
992   while (in_queue)
993     {
994       stmt = queue[--in_queue];
995       sra_data.stmt = stmt;
996
997       if (LIM_DATA (stmt)
998           && LIM_DATA (stmt)->sm_done)
999         goto fail;
1000
1001       switch (TREE_CODE (stmt))
1002         {
1003         case MODIFY_EXPR:
1004         case CALL_EXPR:
1005         case RETURN_EXPR:
1006           if (!for_each_memref (stmt, fem_single_reachable_address,
1007                                 &sra_data))
1008             goto fail;
1009
1010           /* If this is a function that may depend on the memory location,
1011              record the fact.  We cannot directly refuse call clobbered
1012              operands here, since sra_data.common_ref did not have
1013              to be set yet.  */
1014           call = get_call_expr_in (stmt);
1015           if (call
1016               && !(call_expr_flags (call) & ECF_CONST))
1017             *seen_call_stmt = true;
1018
1019           /* Traverse also definitions of the VUSES (there may be other
1020              distinct from the one we used to get to this statement).  */
1021           FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_VIRTUAL_USES)
1022             maybe_queue_var (val, loop, seen, queue, &in_queue);
1023
1024           break;
1025
1026         case PHI_NODE:
1027           for (i = 0; i < (unsigned) PHI_NUM_ARGS (stmt); i++)
1028             if (TREE_CODE (PHI_ARG_DEF (stmt, i)) == SSA_NAME)
1029               maybe_queue_var (PHI_ARG_DEF (stmt, i), loop,
1030                                seen, queue, &in_queue);
1031           break;
1032
1033         default:
1034           goto fail;
1035         }
1036
1037       /* Find uses of virtual names.  */
1038       df = get_immediate_uses (stmt);
1039       n = num_immediate_uses (df);
1040
1041       for (i = 0; i < n; i++)
1042         {
1043           stmt = immediate_use (df, i);
1044
1045           if (!flow_bb_inside_loop_p (loop, bb_for_stmt (stmt)))
1046             continue;
1047
1048           if (TEST_BIT (seen, get_stmt_uid (stmt)))
1049             continue;
1050           SET_BIT (seen, get_stmt_uid (stmt));
1051
1052           queue[in_queue++] = stmt;
1053         }
1054     }
1055
1056   free (queue);
1057   sbitmap_free (seen);
1058
1059   return sra_data.common_ref;
1060
1061 fail:
1062   free_mem_refs (*mem_refs);
1063   *mem_refs = NULL;
1064   free (queue);
1065   sbitmap_free (seen);
1066
1067   return NULL;
1068 }
1069
1070 /* Rewrites memory references in list MEM_REFS by variable TMP_VAR.  */
1071
1072 static void
1073 rewrite_mem_refs (tree tmp_var, struct mem_ref *mem_refs)
1074 {
1075   tree var;
1076   ssa_op_iter iter;
1077
1078   for (; mem_refs; mem_refs = mem_refs->next)
1079     {
1080       FOR_EACH_SSA_TREE_OPERAND (var, mem_refs->stmt, iter, SSA_OP_ALL_VIRTUALS)
1081         {
1082           var = SSA_NAME_VAR (var);
1083           bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1084         }
1085
1086       *mem_refs->ref = tmp_var;
1087       modify_stmt (mem_refs->stmt);
1088     }
1089 }
1090
1091 /* Records request for store motion of memory reference REF from LOOP.
1092    MEM_REFS is the list of occurrences of the reference REF inside LOOP;
1093    these references are rewritten by a new temporary variable.
1094    Exits from the LOOP are stored in EXITS, there are N_EXITS of them.
1095    The initialization of the temporary variable is put to the preheader
1096    of the loop, and assignments to the reference from the temporary variable
1097    are emitted to exits.  */
1098
1099 static void
1100 schedule_sm (struct loop *loop, edge *exits, unsigned n_exits, tree ref,
1101              struct mem_ref *mem_refs)
1102 {
1103   struct mem_ref *aref;
1104   tree tmp_var;
1105   unsigned i;
1106   tree load, store;
1107   struct fmt_data fmt_data;
1108
1109   if (dump_file && (dump_flags & TDF_DETAILS))
1110     {
1111       fprintf (dump_file, "Executing store motion of ");
1112       print_generic_expr (dump_file, ref, 0);
1113       fprintf (dump_file, " from loop %d\n", loop->num);
1114     }
1115
1116   tmp_var = make_rename_temp (TREE_TYPE (ref), "lsm_tmp");
1117
1118   fmt_data.loop = loop;
1119   fmt_data.orig_loop = loop;
1120   for_each_index (&ref, force_move_till, &fmt_data);
1121
1122   rewrite_mem_refs (tmp_var, mem_refs);
1123   for (aref = mem_refs; aref; aref = aref->next)
1124     if (LIM_DATA (aref->stmt))
1125       LIM_DATA (aref->stmt)->sm_done = true;
1126
1127   /* Emit the load & stores.  */
1128   load = build (MODIFY_EXPR, void_type_node, tmp_var, ref);
1129   get_stmt_ann (load)->common.aux = xcalloc (1, sizeof (struct lim_aux_data));
1130   LIM_DATA (load)->max_loop = loop;
1131   LIM_DATA (load)->tgt_loop = loop;
1132
1133   /* Put this into the latch, so that we are sure it will be processed after
1134      all dependencies.  */
1135   bsi_insert_on_edge (loop_latch_edge (loop), load);
1136
1137   for (i = 0; i < n_exits; i++)
1138     {
1139       store = build (MODIFY_EXPR, void_type_node,
1140                      unshare_expr (ref), tmp_var);
1141       bsi_insert_on_edge (exits[i], store);
1142     }
1143 }
1144
1145 /* Returns true if REF may be clobbered by calls.  */
1146
1147 static bool
1148 is_call_clobbered_ref (tree ref)
1149 {
1150   tree base;
1151
1152   base = get_base_address (ref);
1153   if (!base)
1154     return true;
1155
1156   if (DECL_P (base))
1157     return is_call_clobbered (base);
1158
1159   if (INDIRECT_REF_P (base))
1160     {
1161       /* Check whether the alias tags associated with the pointer
1162          are call clobbered.  */
1163       tree ptr = TREE_OPERAND (base, 0);
1164       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1165       tree nmt = (pi) ? pi->name_mem_tag : NULL_TREE;
1166       tree tmt = var_ann (SSA_NAME_VAR (ptr))->type_mem_tag;
1167
1168       if ((nmt && is_call_clobbered (nmt))
1169           || (tmt && is_call_clobbered (tmt)))
1170         return true;
1171
1172       return false;
1173     }
1174
1175   gcc_unreachable ();
1176 }
1177
1178 /* Determine whether all memory references inside LOOP corresponding to the
1179    virtual ssa name REG are equal to each other, and whether the address of
1180    this common reference can be hoisted outside of the loop.  If this is true,
1181    prepare the statements that load the value of the memory reference to a
1182    temporary variable in the loop preheader, store it back on the loop exits,
1183    and replace all the references inside LOOP by this temporary variable.
1184    LOOP has N_EXITS stored in EXITS.  */
1185
1186 static void
1187 determine_lsm_reg (struct loop *loop, edge *exits, unsigned n_exits, tree reg)
1188 {
1189   tree ref;
1190   struct mem_ref *mem_refs, *aref;
1191   struct loop *must_exec;
1192   bool sees_call;
1193   
1194   if (is_gimple_reg (reg))
1195     return;
1196   
1197   ref = single_reachable_address (loop, SSA_NAME_DEF_STMT (reg), &mem_refs,
1198                                   &sees_call);
1199   if (!ref)
1200     return;
1201
1202   /* If we cannot create a ssa name for the result, give up.  */
1203   if (!is_gimple_reg_type (TREE_TYPE (ref))
1204       || TREE_THIS_VOLATILE (ref))
1205     goto fail;
1206
1207   /* If there is a call that may use the location, give up as well.  */
1208   if (sees_call
1209       && is_call_clobbered_ref (ref))
1210     goto fail;
1211
1212   if (!for_each_index (&ref, may_move_till, loop))
1213     goto fail;
1214
1215   if (tree_could_trap_p (ref))
1216     {
1217       /* If the memory access is unsafe (i.e. it might trap), ensure that some
1218          of the statements in that it occurs is always executed when the loop
1219          is entered.  This way we know that by moving the load from the
1220          reference out of the loop we will not cause the error that would not
1221          occur otherwise.
1222
1223          TODO -- in fact we would like to check for anticipability of the
1224          reference, i.e. that on each path from loop entry to loop exit at
1225          least one of the statements containing the memory reference is
1226          executed.  */
1227
1228       for (aref = mem_refs; aref; aref = aref->next)
1229         {
1230           if (!LIM_DATA (aref->stmt))
1231             continue;
1232
1233           must_exec = LIM_DATA (aref->stmt)->always_executed_in;
1234           if (!must_exec)
1235             continue;
1236
1237           if (must_exec == loop
1238               || flow_loop_nested_p (must_exec, loop))
1239             break;
1240         }
1241
1242       if (!aref)
1243         goto fail;
1244     }
1245
1246   schedule_sm (loop, exits, n_exits, ref, mem_refs);
1247
1248 fail: ;
1249   free_mem_refs (mem_refs);
1250 }
1251
1252 /* Checks whether LOOP (with N_EXITS exits stored in EXITS array) is suitable
1253    for a store motion optimization (i.e. whether we can insert statement
1254    on its exits).  */
1255
1256 static bool
1257 loop_suitable_for_sm (struct loop *loop ATTRIBUTE_UNUSED, edge *exits,
1258                       unsigned n_exits)
1259 {
1260   unsigned i;
1261
1262   for (i = 0; i < n_exits; i++)
1263     if (exits[i]->flags & EDGE_ABNORMAL)
1264       return false;
1265
1266   return true;
1267 }
1268
1269 /* Try to perform store motion for all memory references modified inside
1270    LOOP.  */
1271
1272 static void
1273 determine_lsm_loop (struct loop *loop)
1274 {
1275   tree phi;
1276   unsigned n_exits;
1277   edge *exits = get_loop_exit_edges (loop, &n_exits);
1278
1279   if (!loop_suitable_for_sm (loop, exits, n_exits))
1280     {
1281       free (exits);
1282       return;
1283     }
1284
1285   for (phi = phi_nodes (loop->header); phi; phi = PHI_CHAIN (phi))
1286     determine_lsm_reg (loop, exits, n_exits, PHI_RESULT (phi));
1287
1288   free (exits);
1289 }
1290
1291 /* Try to perform store motion for all memory references modified inside
1292    any of LOOPS.  */
1293
1294 static void
1295 determine_lsm (struct loops *loops)
1296 {
1297   struct loop *loop;
1298   basic_block bb;
1299
1300   if (!loops->tree_root->inner)
1301     return;
1302
1303   /* Create a UID for each statement in the function.  Ordering of the
1304      UIDs is not important for this pass.  */
1305   max_stmt_uid = 0;
1306   FOR_EACH_BB (bb)
1307     {
1308       block_stmt_iterator bsi;
1309
1310       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1311         stmt_ann (bsi_stmt (bsi))->uid = max_stmt_uid++;
1312     }
1313
1314   compute_immediate_uses (TDFA_USE_VOPS, NULL);
1315
1316   /* Pass the loops from the outermost.  For each virtual operand loop phi node
1317      check whether all the references inside the loop correspond to a single
1318      address, and if so, move them.  */
1319
1320   loop = loops->tree_root->inner;
1321   while (1)
1322     {
1323       determine_lsm_loop (loop);
1324
1325       if (loop->inner)
1326         {
1327           loop = loop->inner;
1328           continue;
1329         }
1330       while (!loop->next)
1331         {
1332           loop = loop->outer;
1333           if (loop == loops->tree_root)
1334             {
1335               free_df ();
1336               loop_commit_inserts ();
1337               return;
1338             }
1339         }
1340       loop = loop->next;
1341     }
1342 }
1343
1344 /* Fills ALWAYS_EXECUTED_IN information for basic blocks of LOOP, i.e.
1345    for each such basic block bb records the outermost loop for that execution
1346    of its header implies execution of bb.  CONTAINS_CALL is the bitmap of
1347    blocks that contain a nonpure call.  */
1348
1349 static void
1350 fill_always_executed_in (struct loop *loop, sbitmap contains_call)
1351 {
1352   basic_block bb = NULL, *bbs, last = NULL;
1353   unsigned i;
1354   edge e;
1355   struct loop *inn_loop = loop;
1356
1357   if (!loop->header->aux)
1358     {
1359       bbs = get_loop_body_in_dom_order (loop);
1360
1361       for (i = 0; i < loop->num_nodes; i++)
1362         {
1363           edge_iterator ei;
1364           bb = bbs[i];
1365
1366           if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
1367             last = bb;
1368
1369           if (TEST_BIT (contains_call, bb->index))
1370             break;
1371
1372           FOR_EACH_EDGE (e, ei, bb->succs)
1373             if (!flow_bb_inside_loop_p (loop, e->dest))
1374               break;
1375           if (e)
1376             break;
1377
1378           /* A loop might be infinite (TODO use simple loop analysis
1379              to disprove this if possible).  */
1380           if (bb->flags & BB_IRREDUCIBLE_LOOP)
1381             break;
1382
1383           if (!flow_bb_inside_loop_p (inn_loop, bb))
1384             break;
1385
1386           if (bb->loop_father->header == bb)
1387             {
1388               if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
1389                 break;
1390
1391               /* In a loop that is always entered we may proceed anyway.
1392                  But record that we entered it and stop once we leave it.  */
1393               inn_loop = bb->loop_father;
1394             }
1395         }
1396
1397       while (1)
1398         {
1399           last->aux = loop;
1400           if (last == loop->header)
1401             break;
1402           last = get_immediate_dominator (CDI_DOMINATORS, last);
1403         }
1404
1405       free (bbs);
1406     }
1407
1408   for (loop = loop->inner; loop; loop = loop->next)
1409     fill_always_executed_in (loop, contains_call);
1410 }
1411
1412 /* Compute the global information needed by the loop invariant motion pass.
1413    LOOPS is the loop tree.  */
1414
1415 static void
1416 tree_ssa_lim_initialize (struct loops *loops)
1417 {
1418   sbitmap contains_call = sbitmap_alloc (last_basic_block);
1419   block_stmt_iterator bsi;
1420   struct loop *loop;
1421   basic_block bb;
1422
1423   sbitmap_zero (contains_call);
1424   FOR_EACH_BB (bb)
1425     {
1426       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1427         {
1428           if (nonpure_call_p (bsi_stmt (bsi)))
1429             break;
1430         }
1431
1432       if (!bsi_end_p (bsi))
1433         SET_BIT (contains_call, bb->index);
1434     }
1435
1436   for (loop = loops->tree_root->inner; loop; loop = loop->next)
1437     fill_always_executed_in (loop, contains_call);
1438
1439   sbitmap_free (contains_call);
1440 }
1441
1442 /* Cleans up after the invariant motion pass.  */
1443
1444 static void
1445 tree_ssa_lim_finalize (void)
1446 {
1447   basic_block bb;
1448
1449   FOR_EACH_BB (bb)
1450     {
1451       bb->aux = NULL;
1452     }
1453 }
1454
1455 /* Moves invariants from LOOPS.  Only "expensive" invariants are moved out --
1456    i.e. those that are likely to be win regardless of the register pressure.  */
1457
1458 void
1459 tree_ssa_lim (struct loops *loops)
1460 {
1461   tree_ssa_lim_initialize (loops);
1462
1463   /* For each statement determine the outermost loop in that it is
1464      invariant and cost for computing the invariant.  */
1465   determine_invariantness ();
1466
1467   /* For each memory reference determine whether it is possible to hoist it
1468      out of the loop.  Force the necessary invariants to be moved out of the
1469      loops as well.  */
1470   determine_lsm (loops);
1471
1472   /* Move the expressions that are expensive enough.  */
1473   move_computations ();
1474
1475   tree_ssa_lim_finalize ();
1476 }