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