OSDN Git Service

PR c/36507
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-loop-im.c
1 /* Loop invariant motion.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Free Software
3    Foundation, Inc.
4    
5 This file is part of GCC.
6    
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
11    
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16    
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
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 #include "real.h"
41 #include "hashtab.h"
42 #include "tree-affine.h"
43 #include "pointer-set.h"
44 #include "tree-ssa-propagate.h"
45
46 /* TODO:  Support for predicated code motion.  I.e.
47
48    while (1)
49      {
50        if (cond)
51          {
52            a = inv;
53            something;
54          }
55      }
56
57    Where COND and INV are is invariants, but evaluating INV may trap or be
58    invalid from some other reason if !COND.  This may be transformed to
59
60    if (cond)
61      a = inv;
62    while (1)
63      {
64        if (cond)
65          something;
66      }  */
67
68 /* A type for the list of statements that have to be moved in order to be able
69    to hoist an invariant computation.  */
70
71 struct depend
72 {
73   tree stmt;
74   struct depend *next;
75 };
76
77 /* The auxiliary data kept for each statement.  */
78
79 struct lim_aux_data
80 {
81   struct loop *max_loop;        /* The outermost loop in that the statement
82                                    is invariant.  */
83
84   struct loop *tgt_loop;        /* The loop out of that we want to move the
85                                    invariant.  */
86
87   struct loop *always_executed_in;
88                                 /* The outermost loop for that we are sure
89                                    the statement is executed if the loop
90                                    is entered.  */
91
92   unsigned cost;                /* Cost of the computation performed by the
93                                    statement.  */
94
95   struct depend *depends;       /* List of statements that must be also hoisted
96                                    out of the loop when this statement is
97                                    hoisted; i.e. those that define the operands
98                                    of the statement and are inside of the
99                                    MAX_LOOP loop.  */
100 };
101
102 #define LIM_DATA(STMT) (TREE_CODE (STMT) == PHI_NODE \
103                         ? NULL \
104                         : (struct lim_aux_data *) (stmt_ann (STMT)->common.aux))
105
106 /* Description of a memory reference location.  */
107
108 typedef struct mem_ref_loc
109 {
110   tree *ref;                    /* The reference itself.  */
111   tree stmt;                    /* The statement in that it occurs.  */
112 } *mem_ref_loc_p;
113
114 DEF_VEC_P(mem_ref_loc_p);
115 DEF_VEC_ALLOC_P(mem_ref_loc_p, heap);
116
117 /* The list of memory reference locations in a loop.  */
118
119 typedef struct mem_ref_locs
120 {
121   VEC (mem_ref_loc_p, heap) *locs;
122 } *mem_ref_locs_p;
123
124 DEF_VEC_P(mem_ref_locs_p);
125 DEF_VEC_ALLOC_P(mem_ref_locs_p, heap);
126
127 /* Description of a memory reference.  */
128
129 typedef struct mem_ref
130 {
131   tree mem;                     /* The memory itself.  */
132   unsigned id;                  /* ID assigned to the memory reference
133                                    (its index in memory_accesses.refs_list)  */
134   hashval_t hash;               /* Its hash value.  */
135   bitmap stored;                /* The set of loops in that this memory location
136                                    is stored to.  */
137   VEC (mem_ref_locs_p, heap) *accesses_in_loop;
138                                 /* The locations of the accesses.  Vector
139                                    indexed by the loop number.  */
140   bitmap vops;                  /* Vops corresponding to this memory
141                                    location.  */
142
143   /* The following sets are computed on demand.  We keep both set and
144      its complement, so that we know whether the information was
145      already computed or not.  */
146   bitmap indep_loop;            /* The set of loops in that the memory
147                                    reference is independent, meaning:
148                                    If it is stored in the loop, this store
149                                      is independent on all other loads and
150                                      stores.
151                                    If it is only loaded, then it is independent
152                                      on all stores in the loop.  */
153   bitmap dep_loop;              /* The complement of INDEP_LOOP.  */
154
155   bitmap indep_ref;             /* The set of memory references on that
156                                    this reference is independent.  */
157   bitmap dep_ref;               /* The complement of DEP_REF.  */
158 } *mem_ref_p;
159
160 DEF_VEC_P(mem_ref_p);
161 DEF_VEC_ALLOC_P(mem_ref_p, heap);
162
163 DEF_VEC_P(bitmap);
164 DEF_VEC_ALLOC_P(bitmap, heap);
165
166 DEF_VEC_P(htab_t);
167 DEF_VEC_ALLOC_P(htab_t, heap);
168
169 /* Description of memory accesses in loops.  */
170
171 static struct
172 {
173   /* The hash table of memory references accessed in loops.  */
174   htab_t refs;
175
176   /* The list of memory references.  */
177   VEC (mem_ref_p, heap) *refs_list;
178
179   /* The set of memory references accessed in each loop.  */
180   VEC (bitmap, heap) *refs_in_loop;
181
182   /* The set of memory references accessed in each loop, including
183      subloops.  */
184   VEC (bitmap, heap) *all_refs_in_loop;
185
186   /* The set of virtual operands clobbered in a given loop.  */
187   VEC (bitmap, heap) *clobbered_vops;
188
189   /* Map from the pair (loop, virtual operand) to the set of refs that
190      touch the virtual operand in the loop.  */
191   VEC (htab_t, heap) *vop_ref_map;
192
193   /* Cache for expanding memory addresses.  */
194   struct pointer_map_t *ttae_cache;
195 } memory_accesses;
196
197 static bool ref_indep_loop_p (struct loop *, mem_ref_p);
198
199 /* Minimum cost of an expensive expression.  */
200 #define LIM_EXPENSIVE ((unsigned) PARAM_VALUE (PARAM_LIM_EXPENSIVE))
201
202 /* The outermost loop for that execution of the header guarantees that the
203    block will be executed.  */
204 #define ALWAYS_EXECUTED_IN(BB) ((struct loop *) (BB)->aux)
205
206 /* Calls CBCK for each index in memory reference ADDR_P.  There are two
207    kinds situations handled; in each of these cases, the memory reference
208    and DATA are passed to the callback:
209    
210    Access to an array: ARRAY_{RANGE_}REF (base, index).  In this case we also
211    pass the pointer to the index to the callback.
212
213    Pointer dereference: INDIRECT_REF (addr).  In this case we also pass the
214    pointer to addr to the callback.
215    
216    If the callback returns false, the whole search stops and false is returned.
217    Otherwise the function returns true after traversing through the whole
218    reference *ADDR_P.  */
219
220 bool
221 for_each_index (tree *addr_p, bool (*cbck) (tree, tree *, void *), void *data)
222 {
223   tree *nxt, *idx;
224
225   for (; ; addr_p = nxt)
226     {
227       switch (TREE_CODE (*addr_p))
228         {
229         case SSA_NAME:
230           return cbck (*addr_p, addr_p, data);
231
232         case MISALIGNED_INDIRECT_REF:
233         case ALIGN_INDIRECT_REF:
234         case INDIRECT_REF:
235           nxt = &TREE_OPERAND (*addr_p, 0);
236           return cbck (*addr_p, nxt, data);
237
238         case BIT_FIELD_REF:
239         case VIEW_CONVERT_EXPR:
240         case REALPART_EXPR:
241         case IMAGPART_EXPR:
242           nxt = &TREE_OPERAND (*addr_p, 0);
243           break;
244
245         case COMPONENT_REF:
246           /* If the component has varying offset, it behaves like index
247              as well.  */
248           idx = &TREE_OPERAND (*addr_p, 2);
249           if (*idx
250               && !cbck (*addr_p, idx, data))
251             return false;
252
253           nxt = &TREE_OPERAND (*addr_p, 0);
254           break;
255
256         case ARRAY_REF:
257         case ARRAY_RANGE_REF:
258           nxt = &TREE_OPERAND (*addr_p, 0);
259           if (!cbck (*addr_p, &TREE_OPERAND (*addr_p, 1), data))
260             return false;
261           break;
262
263         case VAR_DECL:
264         case PARM_DECL:
265         case STRING_CST:
266         case RESULT_DECL:
267         case VECTOR_CST:
268         case COMPLEX_CST:
269         case INTEGER_CST:
270         case REAL_CST:
271         case FIXED_CST:
272         case CONSTRUCTOR:
273           return true;
274
275         case ADDR_EXPR:
276           gcc_assert (is_gimple_min_invariant (*addr_p));
277           return true;
278
279         case TARGET_MEM_REF:
280           idx = &TMR_BASE (*addr_p);
281           if (*idx
282               && !cbck (*addr_p, idx, data))
283             return false;
284           idx = &TMR_INDEX (*addr_p);
285           if (*idx
286               && !cbck (*addr_p, idx, data))
287             return false;
288           return true;
289
290         default:
291           gcc_unreachable ();
292         }
293     }
294 }
295
296 /* If it is possible to hoist the statement STMT unconditionally,
297    returns MOVE_POSSIBLE.
298    If it is possible to hoist the statement STMT, but we must avoid making
299    it executed if it would not be executed in the original program (e.g.
300    because it may trap), return MOVE_PRESERVE_EXECUTION.
301    Otherwise return MOVE_IMPOSSIBLE.  */
302
303 enum move_pos
304 movement_possibility (tree stmt)
305 {
306   tree lhs, rhs;
307
308   if (flag_unswitch_loops
309       && TREE_CODE (stmt) == COND_EXPR)
310     {
311       /* If we perform unswitching, force the operands of the invariant
312          condition to be moved out of the loop.  */
313       return MOVE_POSSIBLE;
314     }
315
316   if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
317     return MOVE_IMPOSSIBLE;
318
319   if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
320     return MOVE_IMPOSSIBLE;
321
322   if (stmt_ends_bb_p (stmt))
323     return MOVE_IMPOSSIBLE;
324
325   if (stmt_ann (stmt)->has_volatile_ops)
326     return MOVE_IMPOSSIBLE;
327
328   lhs = GIMPLE_STMT_OPERAND (stmt, 0);
329   if (TREE_CODE (lhs) == SSA_NAME
330       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
331     return MOVE_IMPOSSIBLE;
332
333   rhs = GIMPLE_STMT_OPERAND (stmt, 1);
334
335   if (TREE_SIDE_EFFECTS (rhs)
336       || tree_could_throw_p (rhs))
337     return MOVE_IMPOSSIBLE;
338
339   if (TREE_CODE (lhs) != SSA_NAME
340       || tree_could_trap_p (rhs))
341     return MOVE_PRESERVE_EXECUTION;
342
343   if (get_call_expr_in (stmt))
344     {
345       /* While pure or const call is guaranteed to have no side effects, we
346          cannot move it arbitrarily.  Consider code like
347
348          char *s = something ();
349
350          while (1)
351            {
352              if (s)
353                t = strlen (s);
354              else
355                t = 0;
356            }
357
358          Here the strlen call cannot be moved out of the loop, even though
359          s is invariant.  In addition to possibly creating a call with
360          invalid arguments, moving out a function call that is not executed
361          may cause performance regressions in case the call is costly and
362          not executed at all.  */
363       return MOVE_PRESERVE_EXECUTION;
364     }
365   return MOVE_POSSIBLE;
366 }
367
368 /* Suppose that operand DEF is used inside the LOOP.  Returns the outermost
369    loop to that we could move the expression using DEF if it did not have
370    other operands, i.e. the outermost loop enclosing LOOP in that the value
371    of DEF is invariant.  */
372
373 static struct loop *
374 outermost_invariant_loop (tree def, struct loop *loop)
375 {
376   tree def_stmt;
377   basic_block def_bb;
378   struct loop *max_loop;
379
380   if (TREE_CODE (def) != SSA_NAME)
381     return superloop_at_depth (loop, 1);
382
383   def_stmt = SSA_NAME_DEF_STMT (def);
384   def_bb = bb_for_stmt (def_stmt);
385   if (!def_bb)
386     return superloop_at_depth (loop, 1);
387
388   max_loop = find_common_loop (loop, def_bb->loop_father);
389
390   if (LIM_DATA (def_stmt) && LIM_DATA (def_stmt)->max_loop)
391     max_loop = find_common_loop (max_loop,
392                                  loop_outer (LIM_DATA (def_stmt)->max_loop));
393   if (max_loop == loop)
394     return NULL;
395   max_loop = superloop_at_depth (loop, loop_depth (max_loop) + 1);
396
397   return max_loop;
398 }
399
400 /* Returns the outermost superloop of LOOP in that the expression EXPR is
401    invariant.  */
402
403 static struct loop *
404 outermost_invariant_loop_expr (tree expr, struct loop *loop)
405 {
406   enum tree_code_class codeclass = TREE_CODE_CLASS (TREE_CODE (expr));
407   unsigned i, nops;
408   struct loop *max_loop = superloop_at_depth (loop, 1), *aloop;
409
410   if (TREE_CODE (expr) == SSA_NAME
411       || TREE_CODE (expr) == INTEGER_CST
412       || is_gimple_min_invariant (expr))
413     return outermost_invariant_loop (expr, loop);
414
415   if (codeclass != tcc_unary
416       && codeclass != tcc_binary
417       && codeclass != tcc_expression
418       && codeclass != tcc_vl_exp
419       && codeclass != tcc_comparison)
420     return NULL;
421
422   nops = TREE_OPERAND_LENGTH (expr);
423   for (i = 0; i < nops; i++)
424     {
425       aloop = outermost_invariant_loop_expr (TREE_OPERAND (expr, i), loop);
426       if (!aloop)
427         return NULL;
428
429       if (flow_loop_nested_p (max_loop, aloop))
430         max_loop = aloop;
431     }
432
433   return max_loop;
434 }
435
436 /* DATA is a structure containing information associated with a statement
437    inside LOOP.  DEF is one of the operands of this statement.
438    
439    Find the outermost loop enclosing LOOP in that value of DEF is invariant
440    and record this in DATA->max_loop field.  If DEF itself is defined inside
441    this loop as well (i.e. we need to hoist it out of the loop if we want
442    to hoist the statement represented by DATA), record the statement in that
443    DEF is defined to the DATA->depends list.  Additionally if ADD_COST is true,
444    add the cost of the computation of DEF to the DATA->cost.
445    
446    If DEF is not invariant in LOOP, return false.  Otherwise return TRUE.  */
447
448 static bool
449 add_dependency (tree def, struct lim_aux_data *data, struct loop *loop,
450                 bool add_cost)
451 {
452   tree def_stmt = SSA_NAME_DEF_STMT (def);
453   basic_block def_bb = bb_for_stmt (def_stmt);
454   struct loop *max_loop;
455   struct depend *dep;
456
457   if (!def_bb)
458     return true;
459
460   max_loop = outermost_invariant_loop (def, loop);
461   if (!max_loop)
462     return false;
463
464   if (flow_loop_nested_p (data->max_loop, max_loop))
465     data->max_loop = max_loop;
466
467   if (!LIM_DATA (def_stmt))
468     return true;
469
470   if (add_cost
471       /* Only add the cost if the statement defining DEF is inside LOOP,
472          i.e. if it is likely that by moving the invariants dependent
473          on it, we will be able to avoid creating a new register for
474          it (since it will be only used in these dependent invariants).  */
475       && def_bb->loop_father == loop)
476     data->cost += LIM_DATA (def_stmt)->cost;
477
478   dep = XNEW (struct depend);
479   dep->stmt = def_stmt;
480   dep->next = data->depends;
481   data->depends = dep;
482
483   return true;
484 }
485
486 /* Returns an estimate for a cost of statement STMT.  TODO -- the values here
487    are just ad-hoc constants.  The estimates should be based on target-specific
488    values.  */
489
490 static unsigned
491 stmt_cost (tree stmt)
492 {
493   tree rhs;
494   unsigned cost = 1;
495
496   /* Always try to create possibilities for unswitching.  */
497   if (TREE_CODE (stmt) == COND_EXPR)
498     return LIM_EXPENSIVE;
499
500   rhs = GENERIC_TREE_OPERAND (stmt, 1);
501
502   /* Hoisting memory references out should almost surely be a win.  */
503   if (stmt_references_memory_p (stmt))
504     cost += 20;
505
506   switch (TREE_CODE (rhs))
507     {
508     case CALL_EXPR:
509       /* We should be hoisting calls if possible.  */
510
511       /* Unless the call is a builtin_constant_p; this always folds to a
512          constant, so moving it is useless.  */
513       rhs = get_callee_fndecl (rhs);
514       if (DECL_BUILT_IN_CLASS (rhs) == BUILT_IN_NORMAL
515           && DECL_FUNCTION_CODE (rhs) == BUILT_IN_CONSTANT_P)
516         return 0;
517
518       cost += 20;
519       break;
520
521     case MULT_EXPR:
522     case TRUNC_DIV_EXPR:
523     case CEIL_DIV_EXPR:
524     case FLOOR_DIV_EXPR:
525     case ROUND_DIV_EXPR:
526     case EXACT_DIV_EXPR:
527     case CEIL_MOD_EXPR:
528     case FLOOR_MOD_EXPR:
529     case ROUND_MOD_EXPR:
530     case TRUNC_MOD_EXPR:
531     case RDIV_EXPR:
532       /* Division and multiplication are usually expensive.  */
533       cost += 20;
534       break;
535
536     case LSHIFT_EXPR:
537     case RSHIFT_EXPR:
538       cost += 20;
539       break;
540
541     default:
542       break;
543     }
544
545   return cost;
546 }
547
548 /* Finds the outermost loop between OUTER and LOOP in that the memory reference
549    REF is independent.  If REF is not independent in LOOP, NULL is returned
550    instead.  */
551
552 static struct loop *
553 outermost_indep_loop (struct loop *outer, struct loop *loop, mem_ref_p ref)
554 {
555   struct loop *aloop;
556
557   if (bitmap_bit_p (ref->stored, loop->num))
558     return NULL;
559
560   for (aloop = outer;
561        aloop != loop;
562        aloop = superloop_at_depth (loop, loop_depth (aloop) + 1))
563     if (!bitmap_bit_p (ref->stored, aloop->num)
564         && ref_indep_loop_p (aloop, ref))
565       return aloop;
566
567   if (ref_indep_loop_p (loop, ref))
568     return loop;
569   else
570     return NULL;
571 }
572
573 /* If there is a simple load or store to a memory reference in STMT, returns
574    the location of the memory reference, and sets IS_STORE according to whether
575    it is a store or load.  Otherwise, returns NULL.  */
576
577 static tree *
578 simple_mem_ref_in_stmt (tree stmt, bool *is_store)
579 {
580   tree *lhs, *rhs;
581
582   /* Recognize MEM = (SSA_NAME | invariant) and SSA_NAME = MEM patterns.  */
583   if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
584     return NULL;
585
586   lhs = &GIMPLE_STMT_OPERAND (stmt, 0);
587   rhs = &GIMPLE_STMT_OPERAND (stmt, 1);
588
589   if (TREE_CODE (*lhs) == SSA_NAME)
590     {
591       if (!is_gimple_addressable (*rhs))
592         return NULL;
593
594       *is_store = false;
595       return rhs;
596     }
597   else if (TREE_CODE (*rhs) == SSA_NAME
598            || is_gimple_min_invariant (*rhs))
599     {
600       *is_store = true;
601       return lhs;
602     }
603   else
604     return NULL;
605 }
606
607 /* Returns the memory reference contained in STMT.  */
608
609 static mem_ref_p
610 mem_ref_in_stmt (tree stmt)
611 {
612   bool store;
613   tree *mem = simple_mem_ref_in_stmt (stmt, &store);
614   hashval_t hash;
615   mem_ref_p ref;
616
617   if (!mem)
618     return NULL;
619   gcc_assert (!store);
620
621   hash = iterative_hash_expr (*mem, 0);
622   ref = htab_find_with_hash (memory_accesses.refs, *mem, hash);
623
624   gcc_assert (ref != NULL);
625   return ref;
626 }
627
628 /* Determine the outermost loop to that it is possible to hoist a statement
629    STMT and store it to LIM_DATA (STMT)->max_loop.  To do this we determine
630    the outermost loop in that the value computed by STMT is invariant.
631    If MUST_PRESERVE_EXEC is true, additionally choose such a loop that
632    we preserve the fact whether STMT is executed.  It also fills other related
633    information to LIM_DATA (STMT).
634    
635    The function returns false if STMT cannot be hoisted outside of the loop it
636    is defined in, and true otherwise.  */
637
638 static bool
639 determine_max_movement (tree stmt, bool must_preserve_exec)
640 {
641   basic_block bb = bb_for_stmt (stmt);
642   struct loop *loop = bb->loop_father;
643   struct loop *level;
644   struct lim_aux_data *lim_data = LIM_DATA (stmt);
645   tree val;
646   ssa_op_iter iter;
647   
648   if (must_preserve_exec)
649     level = ALWAYS_EXECUTED_IN (bb);
650   else
651     level = superloop_at_depth (loop, 1);
652   lim_data->max_loop = level;
653
654   FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_USE)
655     if (!add_dependency (val, lim_data, loop, true))
656       return false;
657
658   if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_USES))
659     {
660       mem_ref_p ref = mem_ref_in_stmt (stmt);
661
662       if (ref)
663         {
664           lim_data->max_loop
665                   = outermost_indep_loop (lim_data->max_loop, loop, ref);
666           if (!lim_data->max_loop)
667             return false;
668         }
669       else
670         {
671           FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_VIRTUAL_USES)
672             {
673               if (!add_dependency (val, lim_data, loop, false))
674                 return false;
675             }
676         }
677     }
678
679   lim_data->cost += stmt_cost (stmt);
680
681   return true;
682 }
683
684 /* Suppose that some statement in ORIG_LOOP is hoisted to the loop LEVEL,
685    and that one of the operands of this statement is computed by STMT.
686    Ensure that STMT (together with all the statements that define its
687    operands) is hoisted at least out of the loop LEVEL.  */
688
689 static void
690 set_level (tree stmt, struct loop *orig_loop, struct loop *level)
691 {
692   struct loop *stmt_loop = bb_for_stmt (stmt)->loop_father;
693   struct depend *dep;
694
695   stmt_loop = find_common_loop (orig_loop, stmt_loop);
696   if (LIM_DATA (stmt) && LIM_DATA (stmt)->tgt_loop)
697     stmt_loop = find_common_loop (stmt_loop,
698                                   loop_outer (LIM_DATA (stmt)->tgt_loop));
699   if (flow_loop_nested_p (stmt_loop, level))
700     return;
701
702   gcc_assert (LIM_DATA (stmt));
703   gcc_assert (level == LIM_DATA (stmt)->max_loop
704               || flow_loop_nested_p (LIM_DATA (stmt)->max_loop, level));
705
706   LIM_DATA (stmt)->tgt_loop = level;
707   for (dep = LIM_DATA (stmt)->depends; dep; dep = dep->next)
708     set_level (dep->stmt, orig_loop, level);
709 }
710
711 /* Determines an outermost loop from that we want to hoist the statement STMT.
712    For now we chose the outermost possible loop.  TODO -- use profiling
713    information to set it more sanely.  */
714
715 static void
716 set_profitable_level (tree stmt)
717 {
718   set_level (stmt, bb_for_stmt (stmt)->loop_father, LIM_DATA (stmt)->max_loop);
719 }
720
721 /* Returns true if STMT is not a pure call.  */
722
723 static bool
724 nonpure_call_p (tree stmt)
725 {
726   tree call = get_call_expr_in (stmt);
727
728   if (!call)
729     return false;
730
731   return TREE_SIDE_EFFECTS (call) != 0;
732 }
733
734 /* Releases the memory occupied by DATA.  */
735
736 static void
737 free_lim_aux_data (struct lim_aux_data *data)
738 {
739   struct depend *dep, *next;
740
741   for (dep = data->depends; dep; dep = next)
742     {
743       next = dep->next;
744       free (dep);
745     }
746   free (data);
747 }
748
749 /* Rewrite a/b to a*(1/b).  Return the invariant stmt to process.  */
750
751 static tree
752 rewrite_reciprocal (block_stmt_iterator *bsi)
753 {
754   tree stmt, lhs, rhs, stmt1, stmt2, var, name, tmp;
755
756   stmt = bsi_stmt (*bsi);
757   lhs = GENERIC_TREE_OPERAND (stmt, 0);
758   rhs = GENERIC_TREE_OPERAND (stmt, 1);
759
760   /* stmt must be GIMPLE_MODIFY_STMT.  */
761   var = create_tmp_var (TREE_TYPE (rhs), "reciptmp");
762   add_referenced_var (var);
763
764   tmp = build2 (RDIV_EXPR, TREE_TYPE (rhs),
765                 build_real (TREE_TYPE (rhs), dconst1),
766                 TREE_OPERAND (rhs, 1));
767   stmt1 = build_gimple_modify_stmt (var, tmp);
768   name = make_ssa_name (var, stmt1);
769   GIMPLE_STMT_OPERAND (stmt1, 0) = name;
770   tmp = build2 (MULT_EXPR, TREE_TYPE (rhs),
771                 name, TREE_OPERAND (rhs, 0));
772   stmt2 = build_gimple_modify_stmt (lhs, tmp);
773
774   /* Replace division stmt with reciprocal and multiply stmts.
775      The multiply stmt is not invariant, so update iterator
776      and avoid rescanning.  */
777   bsi_replace (bsi, stmt1, true);
778   bsi_insert_after (bsi, stmt2, BSI_NEW_STMT);
779   SSA_NAME_DEF_STMT (lhs) = stmt2;
780
781   /* Continue processing with invariant reciprocal statement.  */
782   return stmt1;
783 }
784
785 /* Check if the pattern at *BSI is a bittest of the form
786    (A >> B) & 1 != 0 and in this case rewrite it to A & (1 << B) != 0.  */
787
788 static tree
789 rewrite_bittest (block_stmt_iterator *bsi)
790 {
791   tree stmt, lhs, rhs, var, name, use_stmt, stmt1, stmt2, t;
792   use_operand_p use;
793
794   stmt = bsi_stmt (*bsi);
795   lhs = GENERIC_TREE_OPERAND (stmt, 0);
796   rhs = GENERIC_TREE_OPERAND (stmt, 1);
797
798   /* Verify that the single use of lhs is a comparison against zero.  */
799   if (TREE_CODE (lhs) != SSA_NAME
800       || !single_imm_use (lhs, &use, &use_stmt)
801       || TREE_CODE (use_stmt) != COND_EXPR)
802     return stmt;
803   t = COND_EXPR_COND (use_stmt);
804   if (TREE_OPERAND (t, 0) != lhs
805       || (TREE_CODE (t) != NE_EXPR
806           && TREE_CODE (t) != EQ_EXPR)
807       || !integer_zerop (TREE_OPERAND (t, 1)))
808     return stmt;
809
810   /* Get at the operands of the shift.  The rhs is TMP1 & 1.  */
811   stmt1 = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
812   if (TREE_CODE (stmt1) != GIMPLE_MODIFY_STMT)
813     return stmt;
814
815   /* There is a conversion in between possibly inserted by fold.  */
816   t = GIMPLE_STMT_OPERAND (stmt1, 1);
817   if (CONVERT_EXPR_P (t))
818     {
819       t = TREE_OPERAND (t, 0);
820       if (TREE_CODE (t) != SSA_NAME
821           || !has_single_use (t))
822         return stmt;
823       stmt1 = SSA_NAME_DEF_STMT (t);
824       if (TREE_CODE (stmt1) != GIMPLE_MODIFY_STMT)
825         return stmt;
826       t = GIMPLE_STMT_OPERAND (stmt1, 1);
827     }
828
829   /* Verify that B is loop invariant but A is not.  Verify that with
830      all the stmt walking we are still in the same loop.  */
831   if (TREE_CODE (t) == RSHIFT_EXPR
832       && loop_containing_stmt (stmt1) == loop_containing_stmt (stmt)
833       && outermost_invariant_loop_expr (TREE_OPERAND (t, 1),
834                                         loop_containing_stmt (stmt1)) != NULL
835       && outermost_invariant_loop_expr (TREE_OPERAND (t, 0),
836                                         loop_containing_stmt (stmt1)) == NULL)
837     {
838       tree a = TREE_OPERAND (t, 0);
839       tree b = TREE_OPERAND (t, 1);
840
841       /* 1 << B */
842       var = create_tmp_var (TREE_TYPE (a), "shifttmp");
843       add_referenced_var (var);
844       t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (a),
845                        build_int_cst (TREE_TYPE (a), 1), b);
846       stmt1 = build_gimple_modify_stmt (var, t);
847       name = make_ssa_name (var, stmt1);
848       GIMPLE_STMT_OPERAND (stmt1, 0) = name;
849
850       /* A & (1 << B) */
851       t = fold_build2 (BIT_AND_EXPR, TREE_TYPE (a), a, name);
852       stmt2 = build_gimple_modify_stmt (var, t);
853       name = make_ssa_name (var, stmt2);
854       GIMPLE_STMT_OPERAND (stmt2, 0) = name;
855
856       /* Replace the SSA_NAME we compare against zero.  Adjust
857          the type of zero accordingly.  */
858       SET_USE (use, name);
859       TREE_OPERAND (COND_EXPR_COND (use_stmt), 1)
860         = build_int_cst_type (TREE_TYPE (name), 0);
861
862       bsi_insert_before (bsi, stmt1, BSI_SAME_STMT);
863       bsi_replace (bsi, stmt2, true);
864
865       return stmt1;
866     }
867
868   return stmt;
869 }
870
871
872 /* Determine the outermost loops in that statements in basic block BB are
873    invariant, and record them to the LIM_DATA associated with the statements.
874    Callback for walk_dominator_tree.  */
875
876 static void
877 determine_invariantness_stmt (struct dom_walk_data *dw_data ATTRIBUTE_UNUSED,
878                               basic_block bb)
879 {
880   enum move_pos pos;
881   block_stmt_iterator bsi;
882   tree stmt, rhs;
883   bool maybe_never = ALWAYS_EXECUTED_IN (bb) == NULL;
884   struct loop *outermost = ALWAYS_EXECUTED_IN (bb);
885
886   if (!loop_outer (bb->loop_father))
887     return;
888
889   if (dump_file && (dump_flags & TDF_DETAILS))
890     fprintf (dump_file, "Basic block %d (loop %d -- depth %d):\n\n",
891              bb->index, bb->loop_father->num, loop_depth (bb->loop_father));
892
893   for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
894     {
895       stmt = bsi_stmt (bsi);
896
897       pos = movement_possibility (stmt);
898       if (pos == MOVE_IMPOSSIBLE)
899         {
900           if (nonpure_call_p (stmt))
901             {
902               maybe_never = true;
903               outermost = NULL;
904             }
905           /* Make sure to note always_executed_in for stores to make
906              store-motion work.  */
907           else if (stmt_makes_single_store (stmt))
908             {
909               stmt_ann (stmt)->common.aux
910                 = xcalloc (1, sizeof (struct lim_aux_data));
911               LIM_DATA (stmt)->always_executed_in = outermost;
912             }
913           continue;
914         }
915
916       if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
917         {
918           rhs = GIMPLE_STMT_OPERAND (stmt, 1);
919
920           /* If divisor is invariant, convert a/b to a*(1/b), allowing reciprocal
921              to be hoisted out of loop, saving expensive divide.  */
922           if (pos == MOVE_POSSIBLE
923               && TREE_CODE (rhs) == RDIV_EXPR
924               && flag_unsafe_math_optimizations
925               && !flag_trapping_math
926               && outermost_invariant_loop_expr (TREE_OPERAND (rhs, 1),
927                                                 loop_containing_stmt (stmt)) != NULL
928               && outermost_invariant_loop_expr (rhs,
929                                                 loop_containing_stmt (stmt)) == NULL)
930             stmt = rewrite_reciprocal (&bsi);
931
932           /* If the shift count is invariant, convert (A >> B) & 1 to
933              A & (1 << B) allowing the bit mask to be hoisted out of the loop
934              saving an expensive shift.  */
935           if (pos == MOVE_POSSIBLE
936               && TREE_CODE (rhs) == BIT_AND_EXPR
937               && integer_onep (TREE_OPERAND (rhs, 1))
938               && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
939               && has_single_use (TREE_OPERAND (rhs, 0)))
940             stmt = rewrite_bittest (&bsi);
941         }
942
943       stmt_ann (stmt)->common.aux = xcalloc (1, sizeof (struct lim_aux_data));
944       LIM_DATA (stmt)->always_executed_in = outermost;
945
946       if (maybe_never && pos == MOVE_PRESERVE_EXECUTION)
947         continue;
948
949       if (!determine_max_movement (stmt, pos == MOVE_PRESERVE_EXECUTION))
950         {
951           LIM_DATA (stmt)->max_loop = NULL;
952           continue;
953         }
954
955       if (dump_file && (dump_flags & TDF_DETAILS))
956         {
957           print_generic_stmt_indented (dump_file, stmt, 0, 2);
958           fprintf (dump_file, "  invariant up to level %d, cost %d.\n\n",
959                    loop_depth (LIM_DATA (stmt)->max_loop),
960                    LIM_DATA (stmt)->cost);
961         }
962
963       if (LIM_DATA (stmt)->cost >= LIM_EXPENSIVE)
964         set_profitable_level (stmt);
965     }
966 }
967
968 /* For each statement determines the outermost loop in that it is invariant,
969    statements on whose motion it depends and the cost of the computation.
970    This information is stored to the LIM_DATA structure associated with
971    each statement.  */
972
973 static void
974 determine_invariantness (void)
975 {
976   struct dom_walk_data walk_data;
977
978   memset (&walk_data, 0, sizeof (struct dom_walk_data));
979   walk_data.dom_direction = CDI_DOMINATORS;
980   walk_data.before_dom_children_before_stmts = determine_invariantness_stmt;
981
982   init_walk_dominator_tree (&walk_data);
983   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
984   fini_walk_dominator_tree (&walk_data);
985 }
986
987 /* Hoist the statements in basic block BB out of the loops prescribed by
988    data stored in LIM_DATA structures associated with each statement.  Callback
989    for walk_dominator_tree.  */
990
991 static void
992 move_computations_stmt (struct dom_walk_data *dw_data ATTRIBUTE_UNUSED,
993                         basic_block bb)
994 {
995   struct loop *level;
996   block_stmt_iterator bsi;
997   tree stmt;
998   unsigned cost = 0;
999
1000   if (!loop_outer (bb->loop_father))
1001     return;
1002
1003   for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
1004     {
1005       stmt = bsi_stmt (bsi);
1006
1007       if (!LIM_DATA (stmt))
1008         {
1009           bsi_next (&bsi);
1010           continue;
1011         }
1012
1013       cost = LIM_DATA (stmt)->cost;
1014       level = LIM_DATA (stmt)->tgt_loop;
1015       free_lim_aux_data (LIM_DATA (stmt));
1016       stmt_ann (stmt)->common.aux = NULL;
1017
1018       if (!level)
1019         {
1020           bsi_next (&bsi);
1021           continue;
1022         }
1023
1024       /* We do not really want to move conditionals out of the loop; we just
1025          placed it here to force its operands to be moved if necessary.  */
1026       if (TREE_CODE (stmt) == COND_EXPR)
1027         continue;
1028
1029       if (dump_file && (dump_flags & TDF_DETAILS))
1030         {
1031           fprintf (dump_file, "Moving statement\n");
1032           print_generic_stmt (dump_file, stmt, 0);
1033           fprintf (dump_file, "(cost %u) out of loop %d.\n\n",
1034                    cost, level->num);
1035         }
1036
1037       mark_virtual_ops_for_renaming (stmt);
1038       bsi_insert_on_edge (loop_preheader_edge (level), stmt);
1039       bsi_remove (&bsi, false);
1040     }
1041 }
1042
1043 /* Hoist the statements out of the loops prescribed by data stored in
1044    LIM_DATA structures associated with each statement.*/
1045
1046 static void
1047 move_computations (void)
1048 {
1049   struct dom_walk_data walk_data;
1050
1051   memset (&walk_data, 0, sizeof (struct dom_walk_data));
1052   walk_data.dom_direction = CDI_DOMINATORS;
1053   walk_data.before_dom_children_before_stmts = move_computations_stmt;
1054
1055   init_walk_dominator_tree (&walk_data);
1056   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1057   fini_walk_dominator_tree (&walk_data);
1058
1059   bsi_commit_edge_inserts ();
1060   if (need_ssa_update_p ())
1061     rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1062 }
1063
1064 /* Checks whether the statement defining variable *INDEX can be hoisted
1065    out of the loop passed in DATA.  Callback for for_each_index.  */
1066
1067 static bool
1068 may_move_till (tree ref, tree *index, void *data)
1069 {
1070   struct loop *loop = (struct loop*) data, *max_loop;
1071
1072   /* If REF is an array reference, check also that the step and the lower
1073      bound is invariant in LOOP.  */
1074   if (TREE_CODE (ref) == ARRAY_REF)
1075     {
1076       tree step = array_ref_element_size (ref);
1077       tree lbound = array_ref_low_bound (ref);
1078
1079       max_loop = outermost_invariant_loop_expr (step, loop);
1080       if (!max_loop)
1081         return false;
1082
1083       max_loop = outermost_invariant_loop_expr (lbound, loop);
1084       if (!max_loop)
1085         return false;
1086     }
1087
1088   max_loop = outermost_invariant_loop (*index, loop);
1089   if (!max_loop)
1090     return false;
1091
1092   return true;
1093 }
1094
1095 /* Forces statements defining (invariant) SSA names in expression EXPR to be
1096    moved out of the LOOP.  ORIG_LOOP is the loop in that EXPR is used.  */
1097
1098 static void
1099 force_move_till_expr (tree expr, struct loop *orig_loop, struct loop *loop)
1100 {
1101   enum tree_code_class codeclass = TREE_CODE_CLASS (TREE_CODE (expr));
1102   unsigned i, nops;
1103
1104   if (TREE_CODE (expr) == SSA_NAME)
1105     {
1106       tree stmt = SSA_NAME_DEF_STMT (expr);
1107       if (IS_EMPTY_STMT (stmt))
1108         return;
1109
1110       set_level (stmt, orig_loop, loop);
1111       return;
1112     }
1113
1114   if (codeclass != tcc_unary
1115       && codeclass != tcc_binary
1116       && codeclass != tcc_expression
1117       && codeclass != tcc_vl_exp
1118       && codeclass != tcc_comparison)
1119     return;
1120
1121   nops = TREE_OPERAND_LENGTH (expr);
1122   for (i = 0; i < nops; i++)
1123     force_move_till_expr (TREE_OPERAND (expr, i), orig_loop, loop);
1124 }
1125
1126 /* Forces statement defining invariants in REF (and *INDEX) to be moved out of
1127    the LOOP.  The reference REF is used in the loop ORIG_LOOP.  Callback for
1128    for_each_index.  */
1129
1130 struct fmt_data
1131 {
1132   struct loop *loop;
1133   struct loop *orig_loop;
1134 };
1135
1136 static bool
1137 force_move_till (tree ref, tree *index, void *data)
1138 {
1139   tree stmt;
1140   struct fmt_data *fmt_data = (struct fmt_data *) data;
1141
1142   if (TREE_CODE (ref) == ARRAY_REF)
1143     {
1144       tree step = array_ref_element_size (ref);
1145       tree lbound = array_ref_low_bound (ref);
1146
1147       force_move_till_expr (step, fmt_data->orig_loop, fmt_data->loop);
1148       force_move_till_expr (lbound, fmt_data->orig_loop, fmt_data->loop);
1149     }
1150
1151   if (TREE_CODE (*index) != SSA_NAME)
1152     return true;
1153
1154   stmt = SSA_NAME_DEF_STMT (*index);
1155   if (IS_EMPTY_STMT (stmt))
1156     return true;
1157
1158   set_level (stmt, fmt_data->orig_loop, fmt_data->loop);
1159
1160   return true;
1161 }
1162
1163 /* A hash function for struct mem_ref object OBJ.  */
1164
1165 static hashval_t
1166 memref_hash (const void *obj)
1167 {
1168   const struct mem_ref *mem = obj;
1169
1170   return mem->hash;
1171 }
1172
1173 /* An equality function for struct mem_ref object OBJ1 with
1174    memory reference OBJ2.  */
1175
1176 static int
1177 memref_eq (const void *obj1, const void *obj2)
1178 {
1179   const struct mem_ref *mem1 = obj1;
1180
1181   return operand_equal_p (mem1->mem, (tree) obj2, 0);
1182 }
1183
1184 /* Releases list of memory reference locations ACCS.  */
1185
1186 static void
1187 free_mem_ref_locs (mem_ref_locs_p accs)
1188 {
1189   unsigned i;
1190   mem_ref_loc_p loc;
1191
1192   if (!accs)
1193     return;
1194
1195   for (i = 0; VEC_iterate (mem_ref_loc_p, accs->locs, i, loc); i++)
1196     free (loc);
1197   VEC_free (mem_ref_loc_p, heap, accs->locs);
1198   free (accs);
1199 }
1200
1201 /* A function to free the mem_ref object OBJ.  */
1202
1203 static void
1204 memref_free (void *obj)
1205 {
1206   struct mem_ref *mem = obj;
1207   unsigned i;
1208   mem_ref_locs_p accs;
1209
1210   BITMAP_FREE (mem->stored);
1211   BITMAP_FREE (mem->indep_loop);
1212   BITMAP_FREE (mem->dep_loop);
1213   BITMAP_FREE (mem->indep_ref);
1214   BITMAP_FREE (mem->dep_ref);
1215
1216   for (i = 0; VEC_iterate (mem_ref_locs_p, mem->accesses_in_loop, i, accs); i++)
1217     free_mem_ref_locs (accs);
1218   VEC_free (mem_ref_locs_p, heap, mem->accesses_in_loop);
1219
1220   BITMAP_FREE (mem->vops);
1221   free (mem);
1222 }
1223
1224 /* Allocates and returns a memory reference description for MEM whose hash
1225    value is HASH and id is ID.  */
1226
1227 static mem_ref_p
1228 mem_ref_alloc (tree mem, unsigned hash, unsigned id)
1229 {
1230   mem_ref_p ref = XNEW (struct mem_ref);
1231   ref->mem = mem;
1232   ref->id = id;
1233   ref->hash = hash;
1234   ref->stored = BITMAP_ALLOC (NULL);
1235   ref->indep_loop = BITMAP_ALLOC (NULL);
1236   ref->dep_loop = BITMAP_ALLOC (NULL);
1237   ref->indep_ref = BITMAP_ALLOC (NULL);
1238   ref->dep_ref = BITMAP_ALLOC (NULL);
1239   ref->accesses_in_loop = NULL;
1240   ref->vops = BITMAP_ALLOC (NULL);
1241
1242   return ref;
1243 }
1244
1245 /* Allocates and returns the new list of locations.  */
1246
1247 static mem_ref_locs_p
1248 mem_ref_locs_alloc (void)
1249 {
1250   mem_ref_locs_p accs = XNEW (struct mem_ref_locs);
1251   accs->locs = NULL;
1252   return accs;
1253 }
1254
1255 /* Records memory reference location *LOC in LOOP to the memory reference
1256    description REF.  The reference occurs in statement STMT.  */
1257
1258 static void
1259 record_mem_ref_loc (mem_ref_p ref, struct loop *loop, tree stmt, tree *loc)
1260 {
1261   mem_ref_loc_p aref = XNEW (struct mem_ref_loc);
1262   mem_ref_locs_p accs;
1263   bitmap ril = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1264
1265   if (VEC_length (mem_ref_locs_p, ref->accesses_in_loop)
1266       <= (unsigned) loop->num)
1267     VEC_safe_grow_cleared (mem_ref_locs_p, heap, ref->accesses_in_loop,
1268                            loop->num + 1);
1269   accs = VEC_index (mem_ref_locs_p, ref->accesses_in_loop, loop->num);
1270   if (!accs)
1271     {
1272       accs = mem_ref_locs_alloc ();
1273       VEC_replace (mem_ref_locs_p, ref->accesses_in_loop, loop->num, accs);
1274     }
1275
1276   aref->stmt = stmt;
1277   aref->ref = loc;
1278
1279   VEC_safe_push (mem_ref_loc_p, heap, accs->locs, aref);
1280   bitmap_set_bit (ril, ref->id);
1281 }
1282
1283 /* Marks reference REF as stored in LOOP.  */
1284
1285 static void
1286 mark_ref_stored (mem_ref_p ref, struct loop *loop)
1287 {
1288   for (;
1289        loop != current_loops->tree_root
1290        && !bitmap_bit_p (ref->stored, loop->num);
1291        loop = loop_outer (loop))
1292     bitmap_set_bit (ref->stored, loop->num);
1293 }
1294
1295 /* Gathers memory references in statement STMT in LOOP, storing the
1296    information about them in the memory_accesses structure.  Marks
1297    the vops accessed through unrecognized statements there as
1298    well.  */
1299
1300 static void
1301 gather_mem_refs_stmt (struct loop *loop, tree stmt)
1302 {
1303   tree *mem = NULL;
1304   hashval_t hash;
1305   PTR *slot;
1306   mem_ref_p ref;
1307   ssa_op_iter oi;
1308   tree vname;
1309   bool is_stored;
1310   bitmap clvops;
1311   unsigned id;
1312
1313   if (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
1314     return;
1315
1316   mem = simple_mem_ref_in_stmt (stmt, &is_stored);
1317   if (!mem)
1318     goto fail;
1319
1320   hash = iterative_hash_expr (*mem, 0);
1321   slot = htab_find_slot_with_hash (memory_accesses.refs, *mem, hash, INSERT);
1322
1323   if (*slot)
1324     {
1325       ref = *slot;
1326       id = ref->id;
1327     }
1328   else
1329     {
1330       id = VEC_length (mem_ref_p, memory_accesses.refs_list);
1331       ref = mem_ref_alloc (*mem, hash, id);
1332       VEC_safe_push (mem_ref_p, heap, memory_accesses.refs_list, ref);
1333       *slot = ref;
1334
1335       if (dump_file && (dump_flags & TDF_DETAILS))
1336         {
1337           fprintf (dump_file, "Memory reference %u: ", id);
1338           print_generic_expr (dump_file, ref->mem, TDF_SLIM);
1339           fprintf (dump_file, "\n");
1340         }
1341     }
1342   if (is_stored)
1343     mark_ref_stored (ref, loop);
1344
1345   FOR_EACH_SSA_TREE_OPERAND (vname, stmt, oi, SSA_OP_VIRTUAL_USES)
1346     bitmap_set_bit (ref->vops, DECL_UID (SSA_NAME_VAR (vname)));
1347   record_mem_ref_loc (ref, loop, stmt, mem);
1348   return;
1349
1350 fail:
1351   clvops = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
1352   FOR_EACH_SSA_TREE_OPERAND (vname, stmt, oi, SSA_OP_VIRTUAL_USES)
1353     bitmap_set_bit (clvops, DECL_UID (SSA_NAME_VAR (vname)));
1354 }
1355
1356 /* Gathers memory references in loops.  */
1357
1358 static void
1359 gather_mem_refs_in_loops (void)
1360 {
1361   block_stmt_iterator bsi;
1362   basic_block bb;
1363   struct loop *loop;
1364   loop_iterator li;
1365   bitmap clvo, clvi;
1366   bitmap lrefs, alrefs, alrefso;
1367
1368   FOR_EACH_BB (bb)
1369     {
1370       loop = bb->loop_father;
1371       if (loop == current_loops->tree_root)
1372         continue;
1373
1374       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1375         gather_mem_refs_stmt (loop, bsi_stmt (bsi));
1376     }
1377
1378   /* Propagate the information about clobbered vops and accessed memory
1379      references up the loop hierarchy.  */
1380   FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
1381     {
1382       lrefs = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1383       alrefs = VEC_index (bitmap, memory_accesses.all_refs_in_loop, loop->num);
1384       bitmap_ior_into (alrefs, lrefs);
1385
1386       if (loop_outer (loop) == current_loops->tree_root)
1387         continue;
1388
1389       clvi = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
1390       clvo = VEC_index (bitmap, memory_accesses.clobbered_vops,
1391                         loop_outer (loop)->num);
1392       bitmap_ior_into (clvo, clvi);
1393
1394       alrefso = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
1395                            loop_outer (loop)->num);
1396       bitmap_ior_into (alrefso, alrefs);
1397     }
1398 }
1399
1400 /* Element of the hash table that maps vops to memory references.  */
1401
1402 struct vop_to_refs_elt
1403 {
1404   /* DECL_UID of the vop.  */
1405   unsigned uid;
1406
1407   /* List of the all references.  */
1408   bitmap refs_all;
1409
1410   /* List of stored references.  */
1411   bitmap refs_stored;
1412 };
1413
1414 /* A hash function for struct vop_to_refs_elt object OBJ.  */
1415
1416 static hashval_t
1417 vtoe_hash (const void *obj)
1418 {
1419   const struct vop_to_refs_elt *vtoe = obj;
1420
1421   return vtoe->uid;
1422 }
1423
1424 /* An equality function for struct vop_to_refs_elt object OBJ1 with
1425    uid of a vop OBJ2.  */
1426
1427 static int
1428 vtoe_eq (const void *obj1, const void *obj2)
1429 {
1430   const struct vop_to_refs_elt *vtoe = obj1;
1431   const unsigned *uid = obj2;
1432
1433   return vtoe->uid == *uid;
1434 }
1435
1436 /* A function to free the struct vop_to_refs_elt object.  */
1437
1438 static void
1439 vtoe_free (void *obj)
1440 {
1441   struct vop_to_refs_elt *vtoe = obj;
1442
1443   BITMAP_FREE (vtoe->refs_all);
1444   BITMAP_FREE (vtoe->refs_stored);
1445   free (vtoe);
1446 }
1447
1448 /* Records REF to hashtable VOP_TO_REFS for the index VOP.  STORED is true
1449    if the reference REF is stored.  */
1450
1451 static void
1452 record_vop_access (htab_t vop_to_refs, unsigned vop, unsigned ref, bool stored)
1453 {
1454   void **slot = htab_find_slot_with_hash (vop_to_refs, &vop, vop, INSERT);
1455   struct vop_to_refs_elt *vtoe;
1456
1457   if (!*slot)
1458     {
1459       vtoe = XNEW (struct vop_to_refs_elt);
1460       vtoe->uid = vop;
1461       vtoe->refs_all = BITMAP_ALLOC (NULL);
1462       vtoe->refs_stored = BITMAP_ALLOC (NULL);
1463       *slot = vtoe;
1464     }
1465   else
1466     vtoe = *slot;
1467
1468   bitmap_set_bit (vtoe->refs_all, ref);
1469   if (stored)
1470     bitmap_set_bit (vtoe->refs_stored, ref);
1471 }
1472
1473 /* Returns the set of references that access VOP according to the table
1474    VOP_TO_REFS.  */
1475
1476 static bitmap
1477 get_vop_accesses (htab_t vop_to_refs, unsigned vop)
1478 {
1479   struct vop_to_refs_elt *vtoe = htab_find_with_hash (vop_to_refs, &vop, vop);
1480   return vtoe->refs_all;
1481 }
1482
1483 /* Returns the set of stores that access VOP according to the table
1484    VOP_TO_REFS.  */
1485
1486 static bitmap
1487 get_vop_stores (htab_t vop_to_refs, unsigned vop)
1488 {
1489   struct vop_to_refs_elt *vtoe = htab_find_with_hash (vop_to_refs, &vop, vop);
1490   return vtoe->refs_stored;
1491 }
1492
1493 /* Adds REF to mapping from virtual operands to references in LOOP.  */
1494
1495 static void
1496 add_vop_ref_mapping (struct loop *loop, mem_ref_p ref)
1497 {
1498   htab_t map = VEC_index (htab_t, memory_accesses.vop_ref_map, loop->num);
1499   bool stored = bitmap_bit_p (ref->stored, loop->num);
1500   bitmap clobbers = VEC_index (bitmap, memory_accesses.clobbered_vops,
1501                                loop->num);
1502   bitmap_iterator bi;
1503   unsigned vop;
1504
1505   EXECUTE_IF_AND_COMPL_IN_BITMAP (ref->vops, clobbers, 0, vop, bi)
1506     {
1507       record_vop_access (map, vop, ref->id, stored);
1508     }
1509 }
1510
1511 /* Create a mapping from virtual operands to references that touch them
1512    in LOOP.  */
1513
1514 static void
1515 create_vop_ref_mapping_loop (struct loop *loop)
1516 {
1517   bitmap refs = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1518   struct loop *sloop;
1519   bitmap_iterator bi;
1520   unsigned i;
1521   mem_ref_p ref;
1522
1523   EXECUTE_IF_SET_IN_BITMAP (refs, 0, i, bi)
1524     {
1525       ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
1526       for (sloop = loop; sloop != current_loops->tree_root; sloop = loop_outer (sloop))
1527         add_vop_ref_mapping (sloop, ref);
1528     }
1529 }
1530
1531 /* For each non-clobbered virtual operand and each loop, record the memory
1532    references in this loop that touch the operand.  */
1533
1534 static void
1535 create_vop_ref_mapping (void)
1536 {
1537   loop_iterator li;
1538   struct loop *loop;
1539
1540   FOR_EACH_LOOP (li, loop, 0)
1541     {
1542       create_vop_ref_mapping_loop (loop);
1543     }
1544 }
1545
1546 /* Gathers information about memory accesses in the loops.  */
1547
1548 static void
1549 analyze_memory_references (void)
1550 {
1551   unsigned i;
1552   bitmap empty;
1553   htab_t hempty;
1554
1555   memory_accesses.refs
1556           = htab_create (100, memref_hash, memref_eq, memref_free);
1557   memory_accesses.refs_list = NULL;
1558   memory_accesses.refs_in_loop = VEC_alloc (bitmap, heap,
1559                                             number_of_loops ());
1560   memory_accesses.all_refs_in_loop = VEC_alloc (bitmap, heap,
1561                                                 number_of_loops ());
1562   memory_accesses.clobbered_vops = VEC_alloc (bitmap, heap,
1563                                               number_of_loops ());
1564   memory_accesses.vop_ref_map = VEC_alloc (htab_t, heap,
1565                                            number_of_loops ());
1566
1567   for (i = 0; i < number_of_loops (); i++)
1568     {
1569       empty = BITMAP_ALLOC (NULL);
1570       VEC_quick_push (bitmap, memory_accesses.refs_in_loop, empty);
1571       empty = BITMAP_ALLOC (NULL);
1572       VEC_quick_push (bitmap, memory_accesses.all_refs_in_loop, empty);
1573       empty = BITMAP_ALLOC (NULL);
1574       VEC_quick_push (bitmap, memory_accesses.clobbered_vops, empty);
1575       hempty = htab_create (10, vtoe_hash, vtoe_eq, vtoe_free);
1576       VEC_quick_push (htab_t, memory_accesses.vop_ref_map, hempty);
1577     }
1578
1579   memory_accesses.ttae_cache = NULL;
1580
1581   gather_mem_refs_in_loops ();
1582   create_vop_ref_mapping ();
1583 }
1584
1585 /* Returns true if a region of size SIZE1 at position 0 and a region of
1586    size SIZE2 at position DIFF cannot overlap.  */
1587
1588 static bool
1589 cannot_overlap_p (aff_tree *diff, double_int size1, double_int size2)
1590 {
1591   double_int d, bound;
1592
1593   /* Unless the difference is a constant, we fail.  */
1594   if (diff->n != 0)
1595     return false;
1596
1597   d = diff->offset;
1598   if (double_int_negative_p (d))
1599     {
1600       /* The second object is before the first one, we succeed if the last
1601          element of the second object is before the start of the first one.  */
1602       bound = double_int_add (d, double_int_add (size2, double_int_minus_one));
1603       return double_int_negative_p (bound);
1604     }
1605   else
1606     {
1607       /* We succeed if the second object starts after the first one ends.  */
1608       return double_int_scmp (size1, d) <= 0;
1609     }
1610 }
1611
1612 /* Returns true if MEM1 and MEM2 may alias.  TTAE_CACHE is used as a cache in
1613    tree_to_aff_combination_expand.  */
1614
1615 static bool
1616 mem_refs_may_alias_p (tree mem1, tree mem2, struct pointer_map_t **ttae_cache)
1617 {
1618   /* Perform BASE + OFFSET analysis -- if MEM1 and MEM2 are based on the same
1619      object and their offset differ in such a way that the locations cannot
1620      overlap, then they cannot alias.  */
1621   double_int size1, size2;
1622   aff_tree off1, off2;
1623
1624   /* Perform basic offset and type-based disambiguation.  */
1625   if (!refs_may_alias_p (mem1, mem2))
1626     return false;
1627
1628   /* The expansion of addresses may be a bit expensive, thus we only do
1629      the check at -O2 and higher optimization levels.  */
1630   if (optimize < 2)
1631     return true;
1632
1633   get_inner_reference_aff (mem1, &off1, &size1);
1634   get_inner_reference_aff (mem2, &off2, &size2);
1635   aff_combination_expand (&off1, ttae_cache);
1636   aff_combination_expand (&off2, ttae_cache);
1637   aff_combination_scale (&off1, double_int_minus_one);
1638   aff_combination_add (&off2, &off1);
1639
1640   if (cannot_overlap_p (&off2, size1, size2))
1641     return false;
1642
1643   return true;
1644 }
1645
1646 /* Rewrites location LOC by TMP_VAR.  */
1647
1648 static void
1649 rewrite_mem_ref_loc (mem_ref_loc_p loc, tree tmp_var)
1650 {
1651   mark_virtual_ops_for_renaming (loc->stmt);
1652   *loc->ref = tmp_var;
1653   update_stmt (loc->stmt);
1654 }
1655
1656 /* Adds all locations of REF in LOOP and its subloops to LOCS.  */
1657
1658 static void
1659 get_all_locs_in_loop (struct loop *loop, mem_ref_p ref,
1660                       VEC (mem_ref_loc_p, heap) **locs)
1661 {
1662   mem_ref_locs_p accs;
1663   unsigned i;
1664   mem_ref_loc_p loc;
1665   bitmap refs = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
1666                            loop->num);
1667   struct loop *subloop;
1668
1669   if (!bitmap_bit_p (refs, ref->id))
1670     return;
1671
1672   if (VEC_length (mem_ref_locs_p, ref->accesses_in_loop)
1673       > (unsigned) loop->num)
1674     {
1675       accs = VEC_index (mem_ref_locs_p, ref->accesses_in_loop, loop->num);
1676       if (accs)
1677         {
1678           for (i = 0; VEC_iterate (mem_ref_loc_p, accs->locs, i, loc); i++)
1679             VEC_safe_push (mem_ref_loc_p, heap, *locs, loc);
1680         }
1681     }
1682
1683   for (subloop = loop->inner; subloop != NULL; subloop = subloop->next)
1684     get_all_locs_in_loop (subloop, ref, locs);
1685 }
1686
1687 /* Rewrites all references to REF in LOOP by variable TMP_VAR.  */
1688
1689 static void
1690 rewrite_mem_refs (struct loop *loop, mem_ref_p ref, tree tmp_var)
1691 {
1692   unsigned i;
1693   mem_ref_loc_p loc;
1694   VEC (mem_ref_loc_p, heap) *locs = NULL;
1695
1696   get_all_locs_in_loop (loop, ref, &locs);
1697   for (i = 0; VEC_iterate (mem_ref_loc_p, locs, i, loc); i++)
1698     rewrite_mem_ref_loc (loc, tmp_var);
1699   VEC_free (mem_ref_loc_p, heap, locs);
1700 }
1701
1702 /* The name and the length of the currently generated variable
1703    for lsm.  */
1704 #define MAX_LSM_NAME_LENGTH 40
1705 static char lsm_tmp_name[MAX_LSM_NAME_LENGTH + 1];
1706 static int lsm_tmp_name_length;
1707
1708 /* Adds S to lsm_tmp_name.  */
1709
1710 static void
1711 lsm_tmp_name_add (const char *s)
1712 {
1713   int l = strlen (s) + lsm_tmp_name_length;
1714   if (l > MAX_LSM_NAME_LENGTH)
1715     return;
1716
1717   strcpy (lsm_tmp_name + lsm_tmp_name_length, s);
1718   lsm_tmp_name_length = l;
1719 }
1720
1721 /* Stores the name for temporary variable that replaces REF to
1722    lsm_tmp_name.  */
1723
1724 static void
1725 gen_lsm_tmp_name (tree ref)
1726 {
1727   const char *name;
1728
1729   switch (TREE_CODE (ref))
1730     {
1731     case MISALIGNED_INDIRECT_REF:
1732     case ALIGN_INDIRECT_REF:
1733     case INDIRECT_REF:
1734       gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1735       lsm_tmp_name_add ("_");
1736       break;
1737
1738     case BIT_FIELD_REF:
1739     case VIEW_CONVERT_EXPR:
1740     case ARRAY_RANGE_REF:
1741       gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1742       break;
1743
1744     case REALPART_EXPR:
1745       gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1746       lsm_tmp_name_add ("_RE");
1747       break;
1748       
1749     case IMAGPART_EXPR:
1750       gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1751       lsm_tmp_name_add ("_IM");
1752       break;
1753
1754     case COMPONENT_REF:
1755       gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1756       lsm_tmp_name_add ("_");
1757       name = get_name (TREE_OPERAND (ref, 1));
1758       if (!name)
1759         name = "F";
1760       lsm_tmp_name_add ("_");
1761       lsm_tmp_name_add (name);
1762
1763     case ARRAY_REF:
1764       gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1765       lsm_tmp_name_add ("_I");
1766       break;
1767
1768     case SSA_NAME:
1769       ref = SSA_NAME_VAR (ref);
1770       /* Fallthru.  */
1771
1772     case VAR_DECL:
1773     case PARM_DECL:
1774       name = get_name (ref);
1775       if (!name)
1776         name = "D";
1777       lsm_tmp_name_add (name);
1778       break;
1779
1780     case STRING_CST:
1781       lsm_tmp_name_add ("S");
1782       break;
1783
1784     case RESULT_DECL:
1785       lsm_tmp_name_add ("R");
1786       break;
1787
1788     default:
1789       gcc_unreachable ();
1790     }
1791 }
1792
1793 /* Determines name for temporary variable that replaces REF.
1794    The name is accumulated into the lsm_tmp_name variable.
1795    N is added to the name of the temporary.  */
1796
1797 char *
1798 get_lsm_tmp_name (tree ref, unsigned n)
1799 {
1800   char ns[2];
1801
1802   lsm_tmp_name_length = 0;
1803   gen_lsm_tmp_name (ref);
1804   lsm_tmp_name_add ("_lsm");
1805   if (n < 10)
1806     {
1807       ns[0] = '0' + n;
1808       ns[1] = 0;
1809       lsm_tmp_name_add (ns);
1810     }
1811   return lsm_tmp_name;
1812 }
1813
1814 /* Executes store motion of memory reference REF from LOOP.
1815    Exits from the LOOP are stored in EXITS.  The initialization of the
1816    temporary variable is put to the preheader of the loop, and assignments
1817    to the reference from the temporary variable are emitted to exits.  */
1818
1819 static void
1820 execute_sm (struct loop *loop, VEC (edge, heap) *exits, mem_ref_p ref)
1821 {
1822   tree tmp_var;
1823   unsigned i;
1824   tree load, store;
1825   struct fmt_data fmt_data;
1826   edge ex;
1827
1828   if (dump_file && (dump_flags & TDF_DETAILS))
1829     {
1830       fprintf (dump_file, "Executing store motion of ");
1831       print_generic_expr (dump_file, ref->mem, 0);
1832       fprintf (dump_file, " from loop %d\n", loop->num);
1833     }
1834
1835   tmp_var = make_rename_temp (TREE_TYPE (ref->mem),
1836                               get_lsm_tmp_name (ref->mem, ~0));
1837
1838   fmt_data.loop = loop;
1839   fmt_data.orig_loop = loop;
1840   for_each_index (&ref->mem, force_move_till, &fmt_data);
1841
1842   rewrite_mem_refs (loop, ref, tmp_var);
1843
1844   /* Emit the load & stores.  */
1845   load = build_gimple_modify_stmt (tmp_var, unshare_expr (ref->mem));
1846   get_stmt_ann (load)->common.aux = xcalloc (1, sizeof (struct lim_aux_data));
1847   LIM_DATA (load)->max_loop = loop;
1848   LIM_DATA (load)->tgt_loop = loop;
1849
1850   /* Put this into the latch, so that we are sure it will be processed after
1851      all dependencies.  */
1852   bsi_insert_on_edge (loop_latch_edge (loop), load);
1853
1854   for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
1855     {
1856       store = build_gimple_modify_stmt (unshare_expr (ref->mem), tmp_var);
1857       bsi_insert_on_edge (ex, store);
1858     }
1859 }
1860
1861 /* Hoists memory references MEM_REFS out of LOOP.  EXITS is the list of exit
1862    edges of the LOOP.  */
1863
1864 static void
1865 hoist_memory_references (struct loop *loop, bitmap mem_refs,
1866                          VEC (edge, heap) *exits)
1867 {
1868   mem_ref_p ref;
1869   unsigned  i;
1870   bitmap_iterator bi;
1871
1872   EXECUTE_IF_SET_IN_BITMAP (mem_refs, 0, i, bi)
1873     {
1874       ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
1875       execute_sm (loop, exits, ref);
1876     }
1877 }
1878
1879 /* Returns true if REF is always accessed in LOOP.  */
1880
1881 static bool
1882 ref_always_accessed_p (struct loop *loop, mem_ref_p ref)
1883 {
1884   VEC (mem_ref_loc_p, heap) *locs = NULL;
1885   unsigned i;
1886   mem_ref_loc_p loc;
1887   bool ret = false;
1888   struct loop *must_exec;
1889
1890   get_all_locs_in_loop (loop, ref, &locs);
1891   for (i = 0; VEC_iterate (mem_ref_loc_p, locs, i, loc); i++)
1892     {
1893       if (!LIM_DATA (loc->stmt))
1894         continue;
1895
1896       must_exec = LIM_DATA (loc->stmt)->always_executed_in;
1897       if (!must_exec)
1898         continue;
1899
1900       if (must_exec == loop
1901           || flow_loop_nested_p (must_exec, loop))
1902         {
1903           ret = true;
1904           break;
1905         }
1906     }
1907   VEC_free (mem_ref_loc_p, heap, locs);
1908
1909   return ret;
1910 }
1911
1912 /* Returns true if REF1 and REF2 are independent.  */
1913
1914 static bool
1915 refs_independent_p (mem_ref_p ref1, mem_ref_p ref2)
1916 {
1917   if (ref1 == ref2
1918       || bitmap_bit_p (ref1->indep_ref, ref2->id))
1919     return true;
1920   if (bitmap_bit_p (ref1->dep_ref, ref2->id))
1921     return false;
1922
1923   if (dump_file && (dump_flags & TDF_DETAILS))
1924     fprintf (dump_file, "Querying dependency of refs %u and %u: ",
1925              ref1->id, ref2->id);
1926
1927   if (mem_refs_may_alias_p (ref1->mem, ref2->mem,
1928                             &memory_accesses.ttae_cache))
1929     {
1930       bitmap_set_bit (ref1->dep_ref, ref2->id);
1931       bitmap_set_bit (ref2->dep_ref, ref1->id);
1932       if (dump_file && (dump_flags & TDF_DETAILS))
1933         fprintf (dump_file, "dependent.\n");
1934       return false;
1935     }
1936   else
1937     {
1938       bitmap_set_bit (ref1->indep_ref, ref2->id);
1939       bitmap_set_bit (ref2->indep_ref, ref1->id);
1940       if (dump_file && (dump_flags & TDF_DETAILS))
1941         fprintf (dump_file, "independent.\n");
1942       return true;
1943     }
1944 }
1945
1946 /* Records the information whether REF is independent in LOOP (according
1947    to INDEP).  */
1948
1949 static void
1950 record_indep_loop (struct loop *loop, mem_ref_p ref, bool indep)
1951 {
1952   if (indep)
1953     bitmap_set_bit (ref->indep_loop, loop->num);
1954   else
1955     bitmap_set_bit (ref->dep_loop, loop->num);
1956 }
1957
1958 /* Returns true if REF is independent on all other memory references in
1959    LOOP.  */
1960
1961 static bool
1962 ref_indep_loop_p_1 (struct loop *loop, mem_ref_p ref)
1963 {
1964   bitmap clobbers, refs_to_check, refs;
1965   unsigned i;
1966   bitmap_iterator bi;
1967   bool ret = true, stored = bitmap_bit_p (ref->stored, loop->num);
1968   htab_t map;
1969   mem_ref_p aref;
1970
1971   /* If the reference is clobbered, it is not independent.  */
1972   clobbers = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
1973   if (bitmap_intersect_p (ref->vops, clobbers))
1974     return false;
1975
1976   refs_to_check = BITMAP_ALLOC (NULL);
1977
1978   map = VEC_index (htab_t, memory_accesses.vop_ref_map, loop->num);
1979   EXECUTE_IF_AND_COMPL_IN_BITMAP (ref->vops, clobbers, 0, i, bi)
1980     {
1981       if (stored)
1982         refs = get_vop_accesses (map, i);
1983       else
1984         refs = get_vop_stores (map, i);
1985
1986       bitmap_ior_into (refs_to_check, refs);
1987     }
1988
1989   EXECUTE_IF_SET_IN_BITMAP (refs_to_check, 0, i, bi)
1990     {
1991       aref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
1992       if (!refs_independent_p (ref, aref))
1993         {
1994           ret = false;
1995           record_indep_loop (loop, aref, false);
1996           break;
1997         }
1998     }
1999
2000   BITMAP_FREE (refs_to_check);
2001   return ret;
2002 }
2003
2004 /* Returns true if REF is independent on all other memory references in
2005    LOOP.  Wrapper over ref_indep_loop_p_1, caching its results.  */
2006
2007 static bool
2008 ref_indep_loop_p (struct loop *loop, mem_ref_p ref)
2009 {
2010   bool ret;
2011
2012   if (bitmap_bit_p (ref->indep_loop, loop->num))
2013     return true;
2014   if (bitmap_bit_p (ref->dep_loop, loop->num))
2015     return false;
2016
2017   ret = ref_indep_loop_p_1 (loop, ref);
2018
2019   if (dump_file && (dump_flags & TDF_DETAILS))
2020     fprintf (dump_file, "Querying dependencies of ref %u in loop %d: %s\n",
2021              ref->id, loop->num, ret ? "independent" : "dependent");
2022
2023   record_indep_loop (loop, ref, ret);
2024
2025   return ret;
2026 }
2027
2028 /* Returns true if we can perform store motion of REF from LOOP.  */
2029
2030 static bool
2031 can_sm_ref_p (struct loop *loop, mem_ref_p ref)
2032 {
2033   /* Unless the reference is stored in the loop, there is nothing to do.  */
2034   if (!bitmap_bit_p (ref->stored, loop->num))
2035     return false;
2036
2037   /* It should be movable.  */
2038   if (!is_gimple_reg_type (TREE_TYPE (ref->mem))
2039       || TREE_THIS_VOLATILE (ref->mem)
2040       || !for_each_index (&ref->mem, may_move_till, loop))
2041     return false;
2042
2043   /* If it can trap, it must be always executed in LOOP.  */
2044   if (tree_could_trap_p (ref->mem)
2045       && !ref_always_accessed_p (loop, ref))
2046     return false;
2047
2048   /* And it must be independent on all other memory references
2049      in LOOP.  */
2050   if (!ref_indep_loop_p (loop, ref))
2051     return false;
2052
2053   return true;
2054 }
2055
2056 /* Marks the references in LOOP for that store motion should be performed
2057    in REFS_TO_SM.  SM_EXECUTED is the set of references for that store
2058    motion was performed in one of the outer loops.  */
2059
2060 static void
2061 find_refs_for_sm (struct loop *loop, bitmap sm_executed, bitmap refs_to_sm)
2062 {
2063   bitmap refs = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
2064                            loop->num);
2065   unsigned i;
2066   bitmap_iterator bi;
2067   mem_ref_p ref;
2068
2069   EXECUTE_IF_AND_COMPL_IN_BITMAP (refs, sm_executed, 0, i, bi)
2070     {
2071       ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
2072       if (can_sm_ref_p (loop, ref))
2073         bitmap_set_bit (refs_to_sm, i);
2074     }
2075 }
2076
2077 /* Checks whether LOOP (with exits stored in EXITS array) is suitable
2078    for a store motion optimization (i.e. whether we can insert statement
2079    on its exits).  */
2080
2081 static bool
2082 loop_suitable_for_sm (struct loop *loop ATTRIBUTE_UNUSED,
2083                       VEC (edge, heap) *exits)
2084 {
2085   unsigned i;
2086   edge ex;
2087
2088   for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
2089     if (ex->flags & EDGE_ABNORMAL)
2090       return false;
2091
2092   return true;
2093 }
2094
2095 /* Try to perform store motion for all memory references modified inside
2096    LOOP.  SM_EXECUTED is the bitmap of the memory references for that
2097    store motion was executed in one of the outer loops.  */
2098
2099 static void
2100 store_motion_loop (struct loop *loop, bitmap sm_executed)
2101 {
2102   VEC (edge, heap) *exits = get_loop_exit_edges (loop);
2103   struct loop *subloop;
2104   bitmap sm_in_loop = BITMAP_ALLOC (NULL);
2105
2106   if (loop_suitable_for_sm (loop, exits))
2107     {
2108       find_refs_for_sm (loop, sm_executed, sm_in_loop);
2109       hoist_memory_references (loop, sm_in_loop, exits);
2110     }
2111   VEC_free (edge, heap, exits);
2112
2113   bitmap_ior_into (sm_executed, sm_in_loop);
2114   for (subloop = loop->inner; subloop != NULL; subloop = subloop->next)
2115     store_motion_loop (subloop, sm_executed);
2116   bitmap_and_compl_into (sm_executed, sm_in_loop);
2117   BITMAP_FREE (sm_in_loop);
2118 }
2119
2120 /* Try to perform store motion for all memory references modified inside
2121    loops.  */
2122
2123 static void
2124 store_motion (void)
2125 {
2126   struct loop *loop;
2127   bitmap sm_executed = BITMAP_ALLOC (NULL);
2128
2129   for (loop = current_loops->tree_root->inner; loop != NULL; loop = loop->next)
2130     store_motion_loop (loop, sm_executed);
2131
2132   BITMAP_FREE (sm_executed);
2133   bsi_commit_edge_inserts ();
2134 }
2135
2136 /* Fills ALWAYS_EXECUTED_IN information for basic blocks of LOOP, i.e.
2137    for each such basic block bb records the outermost loop for that execution
2138    of its header implies execution of bb.  CONTAINS_CALL is the bitmap of
2139    blocks that contain a nonpure call.  */
2140
2141 static void
2142 fill_always_executed_in (struct loop *loop, sbitmap contains_call)
2143 {
2144   basic_block bb = NULL, *bbs, last = NULL;
2145   unsigned i;
2146   edge e;
2147   struct loop *inn_loop = loop;
2148
2149   if (!loop->header->aux)
2150     {
2151       bbs = get_loop_body_in_dom_order (loop);
2152
2153       for (i = 0; i < loop->num_nodes; i++)
2154         {
2155           edge_iterator ei;
2156           bb = bbs[i];
2157
2158           if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
2159             last = bb;
2160
2161           if (TEST_BIT (contains_call, bb->index))
2162             break;
2163
2164           FOR_EACH_EDGE (e, ei, bb->succs)
2165             if (!flow_bb_inside_loop_p (loop, e->dest))
2166               break;
2167           if (e)
2168             break;
2169
2170           /* A loop might be infinite (TODO use simple loop analysis
2171              to disprove this if possible).  */
2172           if (bb->flags & BB_IRREDUCIBLE_LOOP)
2173             break;
2174
2175           if (!flow_bb_inside_loop_p (inn_loop, bb))
2176             break;
2177
2178           if (bb->loop_father->header == bb)
2179             {
2180               if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
2181                 break;
2182
2183               /* In a loop that is always entered we may proceed anyway.
2184                  But record that we entered it and stop once we leave it.  */
2185               inn_loop = bb->loop_father;
2186             }
2187         }
2188
2189       while (1)
2190         {
2191           last->aux = loop;
2192           if (last == loop->header)
2193             break;
2194           last = get_immediate_dominator (CDI_DOMINATORS, last);
2195         }
2196
2197       free (bbs);
2198     }
2199
2200   for (loop = loop->inner; loop; loop = loop->next)
2201     fill_always_executed_in (loop, contains_call);
2202 }
2203
2204 /* Compute the global information needed by the loop invariant motion pass.  */
2205
2206 static void
2207 tree_ssa_lim_initialize (void)
2208 {
2209   sbitmap contains_call = sbitmap_alloc (last_basic_block);
2210   block_stmt_iterator bsi;
2211   struct loop *loop;
2212   basic_block bb;
2213
2214   sbitmap_zero (contains_call);
2215   FOR_EACH_BB (bb)
2216     {
2217       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2218         {
2219           if (nonpure_call_p (bsi_stmt (bsi)))
2220             break;
2221         }
2222
2223       if (!bsi_end_p (bsi))
2224         SET_BIT (contains_call, bb->index);
2225     }
2226
2227   for (loop = current_loops->tree_root->inner; loop; loop = loop->next)
2228     fill_always_executed_in (loop, contains_call);
2229
2230   sbitmap_free (contains_call);
2231 }
2232
2233 /* Cleans up after the invariant motion pass.  */
2234
2235 static void
2236 tree_ssa_lim_finalize (void)
2237 {
2238   basic_block bb;
2239   unsigned i;
2240   bitmap b;
2241   htab_t h;
2242
2243   FOR_EACH_BB (bb)
2244     {
2245       bb->aux = NULL;
2246     }
2247
2248   VEC_free (mem_ref_p, heap, memory_accesses.refs_list);
2249   htab_delete (memory_accesses.refs);
2250
2251   for (i = 0; VEC_iterate (bitmap, memory_accesses.refs_in_loop, i, b); i++)
2252     BITMAP_FREE (b);
2253   VEC_free (bitmap, heap, memory_accesses.refs_in_loop);
2254
2255   for (i = 0; VEC_iterate (bitmap, memory_accesses.all_refs_in_loop, i, b); i++)
2256     BITMAP_FREE (b);
2257   VEC_free (bitmap, heap, memory_accesses.all_refs_in_loop);
2258
2259   for (i = 0; VEC_iterate (bitmap, memory_accesses.clobbered_vops, i, b); i++)
2260     BITMAP_FREE (b);
2261   VEC_free (bitmap, heap, memory_accesses.clobbered_vops);
2262
2263   for (i = 0; VEC_iterate (htab_t, memory_accesses.vop_ref_map, i, h); i++)
2264     htab_delete (h);
2265   VEC_free (htab_t, heap, memory_accesses.vop_ref_map);
2266
2267   if (memory_accesses.ttae_cache)
2268     pointer_map_destroy (memory_accesses.ttae_cache);
2269 }
2270
2271 /* Moves invariants from loops.  Only "expensive" invariants are moved out --
2272    i.e. those that are likely to be win regardless of the register pressure.  */
2273
2274 void
2275 tree_ssa_lim (void)
2276 {
2277   tree_ssa_lim_initialize ();
2278
2279   /* Gathers information about memory accesses in the loops.  */
2280   analyze_memory_references ();
2281
2282   /* For each statement determine the outermost loop in that it is
2283      invariant and cost for computing the invariant.  */
2284   determine_invariantness ();
2285
2286   /* Execute store motion.  Force the necessary invariants to be moved
2287      out of the loops as well.  */
2288   store_motion ();
2289
2290   /* Move the expressions that are expensive enough.  */
2291   move_computations ();
2292
2293   tree_ssa_lim_finalize ();
2294 }