]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-phiopt.c
gcc/ada/
[thirdparty/gcc.git] / gcc / tree-ssa-phiopt.c
1 /* Optimization of PHI nodes by converting them into straightline code.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "hash-table.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "tree.h"
27 #include "stor-layout.h"
28 #include "flags.h"
29 #include "tm_p.h"
30 #include "basic-block.h"
31 #include "gimple.h"
32 #include "gimplify.h"
33 #include "gimple-iterator.h"
34 #include "gimplify-me.h"
35 #include "gimple-ssa.h"
36 #include "tree-cfg.h"
37 #include "tree-phinodes.h"
38 #include "ssa-iterators.h"
39 #include "stringpool.h"
40 #include "tree-ssanames.h"
41 #include "expr.h"
42 #include "tree-dfa.h"
43 #include "tree-pass.h"
44 #include "langhooks.h"
45 #include "pointer-set.h"
46 #include "domwalk.h"
47 #include "cfgloop.h"
48 #include "tree-data-ref.h"
49 #include "gimple-pretty-print.h"
50 #include "insn-config.h"
51 #include "expr.h"
52 #include "optabs.h"
53 #include "tree-scalar-evolution.h"
54
55 #ifndef HAVE_conditional_move
56 #define HAVE_conditional_move (0)
57 #endif
58
59 static unsigned int tree_ssa_phiopt (void);
60 static unsigned int tree_ssa_phiopt_worker (bool, bool);
61 static bool conditional_replacement (basic_block, basic_block,
62 edge, edge, gimple, tree, tree);
63 static int value_replacement (basic_block, basic_block,
64 edge, edge, gimple, tree, tree);
65 static bool minmax_replacement (basic_block, basic_block,
66 edge, edge, gimple, tree, tree);
67 static bool abs_replacement (basic_block, basic_block,
68 edge, edge, gimple, tree, tree);
69 static bool cond_store_replacement (basic_block, basic_block, edge, edge,
70 struct pointer_set_t *);
71 static bool cond_if_else_store_replacement (basic_block, basic_block, basic_block);
72 static struct pointer_set_t * get_non_trapping (void);
73 static void replace_phi_edge_with_variable (basic_block, edge, gimple, tree);
74 static void hoist_adjacent_loads (basic_block, basic_block,
75 basic_block, basic_block);
76 static bool gate_hoist_loads (void);
77
78 /* This pass tries to replaces an if-then-else block with an
79 assignment. We have four kinds of transformations. Some of these
80 transformations are also performed by the ifcvt RTL optimizer.
81
82 Conditional Replacement
83 -----------------------
84
85 This transformation, implemented in conditional_replacement,
86 replaces
87
88 bb0:
89 if (cond) goto bb2; else goto bb1;
90 bb1:
91 bb2:
92 x = PHI <0 (bb1), 1 (bb0), ...>;
93
94 with
95
96 bb0:
97 x' = cond;
98 goto bb2;
99 bb2:
100 x = PHI <x' (bb0), ...>;
101
102 We remove bb1 as it becomes unreachable. This occurs often due to
103 gimplification of conditionals.
104
105 Value Replacement
106 -----------------
107
108 This transformation, implemented in value_replacement, replaces
109
110 bb0:
111 if (a != b) goto bb2; else goto bb1;
112 bb1:
113 bb2:
114 x = PHI <a (bb1), b (bb0), ...>;
115
116 with
117
118 bb0:
119 bb2:
120 x = PHI <b (bb0), ...>;
121
122 This opportunity can sometimes occur as a result of other
123 optimizations.
124
125
126 Another case caught by value replacement looks like this:
127
128 bb0:
129 t1 = a == CONST;
130 t2 = b > c;
131 t3 = t1 & t2;
132 if (t3 != 0) goto bb1; else goto bb2;
133 bb1:
134 bb2:
135 x = PHI (CONST, a)
136
137 Gets replaced with:
138 bb0:
139 bb2:
140 t1 = a == CONST;
141 t2 = b > c;
142 t3 = t1 & t2;
143 x = a;
144
145 ABS Replacement
146 ---------------
147
148 This transformation, implemented in abs_replacement, replaces
149
150 bb0:
151 if (a >= 0) goto bb2; else goto bb1;
152 bb1:
153 x = -a;
154 bb2:
155 x = PHI <x (bb1), a (bb0), ...>;
156
157 with
158
159 bb0:
160 x' = ABS_EXPR< a >;
161 bb2:
162 x = PHI <x' (bb0), ...>;
163
164 MIN/MAX Replacement
165 -------------------
166
167 This transformation, minmax_replacement replaces
168
169 bb0:
170 if (a <= b) goto bb2; else goto bb1;
171 bb1:
172 bb2:
173 x = PHI <b (bb1), a (bb0), ...>;
174
175 with
176
177 bb0:
178 x' = MIN_EXPR (a, b)
179 bb2:
180 x = PHI <x' (bb0), ...>;
181
182 A similar transformation is done for MAX_EXPR.
183
184
185 This pass also performs a fifth transformation of a slightly different
186 flavor.
187
188 Adjacent Load Hoisting
189 ----------------------
190
191 This transformation replaces
192
193 bb0:
194 if (...) goto bb2; else goto bb1;
195 bb1:
196 x1 = (<expr>).field1;
197 goto bb3;
198 bb2:
199 x2 = (<expr>).field2;
200 bb3:
201 # x = PHI <x1, x2>;
202
203 with
204
205 bb0:
206 x1 = (<expr>).field1;
207 x2 = (<expr>).field2;
208 if (...) goto bb2; else goto bb1;
209 bb1:
210 goto bb3;
211 bb2:
212 bb3:
213 # x = PHI <x1, x2>;
214
215 The purpose of this transformation is to enable generation of conditional
216 move instructions such as Intel CMOVE or PowerPC ISEL. Because one of
217 the loads is speculative, the transformation is restricted to very
218 specific cases to avoid introducing a page fault. We are looking for
219 the common idiom:
220
221 if (...)
222 x = y->left;
223 else
224 x = y->right;
225
226 where left and right are typically adjacent pointers in a tree structure. */
227
228 static unsigned int
229 tree_ssa_phiopt (void)
230 {
231 return tree_ssa_phiopt_worker (false, gate_hoist_loads ());
232 }
233
234 /* This pass tries to transform conditional stores into unconditional
235 ones, enabling further simplifications with the simpler then and else
236 blocks. In particular it replaces this:
237
238 bb0:
239 if (cond) goto bb2; else goto bb1;
240 bb1:
241 *p = RHS;
242 bb2:
243
244 with
245
246 bb0:
247 if (cond) goto bb1; else goto bb2;
248 bb1:
249 condtmp' = *p;
250 bb2:
251 condtmp = PHI <RHS, condtmp'>
252 *p = condtmp;
253
254 This transformation can only be done under several constraints,
255 documented below. It also replaces:
256
257 bb0:
258 if (cond) goto bb2; else goto bb1;
259 bb1:
260 *p = RHS1;
261 goto bb3;
262 bb2:
263 *p = RHS2;
264 bb3:
265
266 with
267
268 bb0:
269 if (cond) goto bb3; else goto bb1;
270 bb1:
271 bb3:
272 condtmp = PHI <RHS1, RHS2>
273 *p = condtmp; */
274
275 static unsigned int
276 tree_ssa_cs_elim (void)
277 {
278 unsigned todo;
279 /* ??? We are not interested in loop related info, but the following
280 will create it, ICEing as we didn't init loops with pre-headers.
281 An interfacing issue of find_data_references_in_bb. */
282 loop_optimizer_init (LOOPS_NORMAL);
283 scev_initialize ();
284 todo = tree_ssa_phiopt_worker (true, false);
285 scev_finalize ();
286 loop_optimizer_finalize ();
287 return todo;
288 }
289
290 /* Return the singleton PHI in the SEQ of PHIs for edges E0 and E1. */
291
292 static gimple
293 single_non_singleton_phi_for_edges (gimple_seq seq, edge e0, edge e1)
294 {
295 gimple_stmt_iterator i;
296 gimple phi = NULL;
297 if (gimple_seq_singleton_p (seq))
298 return gsi_stmt (gsi_start (seq));
299 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
300 {
301 gimple p = gsi_stmt (i);
302 /* If the PHI arguments are equal then we can skip this PHI. */
303 if (operand_equal_for_phi_arg_p (gimple_phi_arg_def (p, e0->dest_idx),
304 gimple_phi_arg_def (p, e1->dest_idx)))
305 continue;
306
307 /* If we already have a PHI that has the two edge arguments are
308 different, then return it is not a singleton for these PHIs. */
309 if (phi)
310 return NULL;
311
312 phi = p;
313 }
314 return phi;
315 }
316
317 /* The core routine of conditional store replacement and normal
318 phi optimizations. Both share much of the infrastructure in how
319 to match applicable basic block patterns. DO_STORE_ELIM is true
320 when we want to do conditional store replacement, false otherwise.
321 DO_HOIST_LOADS is true when we want to hoist adjacent loads out
322 of diamond control flow patterns, false otherwise. */
323 static unsigned int
324 tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads)
325 {
326 basic_block bb;
327 basic_block *bb_order;
328 unsigned n, i;
329 bool cfgchanged = false;
330 struct pointer_set_t *nontrap = 0;
331
332 if (do_store_elim)
333 /* Calculate the set of non-trapping memory accesses. */
334 nontrap = get_non_trapping ();
335
336 /* Search every basic block for COND_EXPR we may be able to optimize.
337
338 We walk the blocks in order that guarantees that a block with
339 a single predecessor is processed before the predecessor.
340 This ensures that we collapse inner ifs before visiting the
341 outer ones, and also that we do not try to visit a removed
342 block. */
343 bb_order = single_pred_before_succ_order ();
344 n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
345
346 for (i = 0; i < n; i++)
347 {
348 gimple cond_stmt, phi;
349 basic_block bb1, bb2;
350 edge e1, e2;
351 tree arg0, arg1;
352
353 bb = bb_order[i];
354
355 cond_stmt = last_stmt (bb);
356 /* Check to see if the last statement is a GIMPLE_COND. */
357 if (!cond_stmt
358 || gimple_code (cond_stmt) != GIMPLE_COND)
359 continue;
360
361 e1 = EDGE_SUCC (bb, 0);
362 bb1 = e1->dest;
363 e2 = EDGE_SUCC (bb, 1);
364 bb2 = e2->dest;
365
366 /* We cannot do the optimization on abnormal edges. */
367 if ((e1->flags & EDGE_ABNORMAL) != 0
368 || (e2->flags & EDGE_ABNORMAL) != 0)
369 continue;
370
371 /* If either bb1's succ or bb2 or bb2's succ is non NULL. */
372 if (EDGE_COUNT (bb1->succs) == 0
373 || bb2 == NULL
374 || EDGE_COUNT (bb2->succs) == 0)
375 continue;
376
377 /* Find the bb which is the fall through to the other. */
378 if (EDGE_SUCC (bb1, 0)->dest == bb2)
379 ;
380 else if (EDGE_SUCC (bb2, 0)->dest == bb1)
381 {
382 basic_block bb_tmp = bb1;
383 edge e_tmp = e1;
384 bb1 = bb2;
385 bb2 = bb_tmp;
386 e1 = e2;
387 e2 = e_tmp;
388 }
389 else if (do_store_elim
390 && EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest)
391 {
392 basic_block bb3 = EDGE_SUCC (bb1, 0)->dest;
393
394 if (!single_succ_p (bb1)
395 || (EDGE_SUCC (bb1, 0)->flags & EDGE_FALLTHRU) == 0
396 || !single_succ_p (bb2)
397 || (EDGE_SUCC (bb2, 0)->flags & EDGE_FALLTHRU) == 0
398 || EDGE_COUNT (bb3->preds) != 2)
399 continue;
400 if (cond_if_else_store_replacement (bb1, bb2, bb3))
401 cfgchanged = true;
402 continue;
403 }
404 else if (do_hoist_loads
405 && EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest)
406 {
407 basic_block bb3 = EDGE_SUCC (bb1, 0)->dest;
408
409 if (!FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (cond_stmt)))
410 && single_succ_p (bb1)
411 && single_succ_p (bb2)
412 && single_pred_p (bb1)
413 && single_pred_p (bb2)
414 && EDGE_COUNT (bb->succs) == 2
415 && EDGE_COUNT (bb3->preds) == 2
416 /* If one edge or the other is dominant, a conditional move
417 is likely to perform worse than the well-predicted branch. */
418 && !predictable_edge_p (EDGE_SUCC (bb, 0))
419 && !predictable_edge_p (EDGE_SUCC (bb, 1)))
420 hoist_adjacent_loads (bb, bb1, bb2, bb3);
421 continue;
422 }
423 else
424 continue;
425
426 e1 = EDGE_SUCC (bb1, 0);
427
428 /* Make sure that bb1 is just a fall through. */
429 if (!single_succ_p (bb1)
430 || (e1->flags & EDGE_FALLTHRU) == 0)
431 continue;
432
433 /* Also make sure that bb1 only have one predecessor and that it
434 is bb. */
435 if (!single_pred_p (bb1)
436 || single_pred (bb1) != bb)
437 continue;
438
439 if (do_store_elim)
440 {
441 /* bb1 is the middle block, bb2 the join block, bb the split block,
442 e1 the fallthrough edge from bb1 to bb2. We can't do the
443 optimization if the join block has more than two predecessors. */
444 if (EDGE_COUNT (bb2->preds) > 2)
445 continue;
446 if (cond_store_replacement (bb1, bb2, e1, e2, nontrap))
447 cfgchanged = true;
448 }
449 else
450 {
451 gimple_seq phis = phi_nodes (bb2);
452 gimple_stmt_iterator gsi;
453 bool candorest = true;
454
455 /* Value replacement can work with more than one PHI
456 so try that first. */
457 for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
458 {
459 phi = gsi_stmt (gsi);
460 arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
461 arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
462 if (value_replacement (bb, bb1, e1, e2, phi, arg0, arg1) == 2)
463 {
464 candorest = false;
465 cfgchanged = true;
466 break;
467 }
468 }
469
470 if (!candorest)
471 continue;
472
473 phi = single_non_singleton_phi_for_edges (phis, e1, e2);
474 if (!phi)
475 continue;
476
477 arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
478 arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
479
480 /* Something is wrong if we cannot find the arguments in the PHI
481 node. */
482 gcc_assert (arg0 != NULL && arg1 != NULL);
483
484 /* Do the replacement of conditional if it can be done. */
485 if (conditional_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
486 cfgchanged = true;
487 else if (abs_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
488 cfgchanged = true;
489 else if (minmax_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
490 cfgchanged = true;
491 }
492 }
493
494 free (bb_order);
495
496 if (do_store_elim)
497 pointer_set_destroy (nontrap);
498 /* If the CFG has changed, we should cleanup the CFG. */
499 if (cfgchanged && do_store_elim)
500 {
501 /* In cond-store replacement we have added some loads on edges
502 and new VOPS (as we moved the store, and created a load). */
503 gsi_commit_edge_inserts ();
504 return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
505 }
506 else if (cfgchanged)
507 return TODO_cleanup_cfg;
508 return 0;
509 }
510
511 /* Replace PHI node element whose edge is E in block BB with variable NEW.
512 Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
513 is known to have two edges, one of which must reach BB). */
514
515 static void
516 replace_phi_edge_with_variable (basic_block cond_block,
517 edge e, gimple phi, tree new_tree)
518 {
519 basic_block bb = gimple_bb (phi);
520 basic_block block_to_remove;
521 gimple_stmt_iterator gsi;
522
523 /* Change the PHI argument to new. */
524 SET_USE (PHI_ARG_DEF_PTR (phi, e->dest_idx), new_tree);
525
526 /* Remove the empty basic block. */
527 if (EDGE_SUCC (cond_block, 0)->dest == bb)
528 {
529 EDGE_SUCC (cond_block, 0)->flags |= EDGE_FALLTHRU;
530 EDGE_SUCC (cond_block, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
531 EDGE_SUCC (cond_block, 0)->probability = REG_BR_PROB_BASE;
532 EDGE_SUCC (cond_block, 0)->count += EDGE_SUCC (cond_block, 1)->count;
533
534 block_to_remove = EDGE_SUCC (cond_block, 1)->dest;
535 }
536 else
537 {
538 EDGE_SUCC (cond_block, 1)->flags |= EDGE_FALLTHRU;
539 EDGE_SUCC (cond_block, 1)->flags
540 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
541 EDGE_SUCC (cond_block, 1)->probability = REG_BR_PROB_BASE;
542 EDGE_SUCC (cond_block, 1)->count += EDGE_SUCC (cond_block, 0)->count;
543
544 block_to_remove = EDGE_SUCC (cond_block, 0)->dest;
545 }
546 delete_basic_block (block_to_remove);
547
548 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
549 gsi = gsi_last_bb (cond_block);
550 gsi_remove (&gsi, true);
551
552 if (dump_file && (dump_flags & TDF_DETAILS))
553 fprintf (dump_file,
554 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
555 cond_block->index,
556 bb->index);
557 }
558
559 /* The function conditional_replacement does the main work of doing the
560 conditional replacement. Return true if the replacement is done.
561 Otherwise return false.
562 BB is the basic block where the replacement is going to be done on. ARG0
563 is argument 0 from PHI. Likewise for ARG1. */
564
565 static bool
566 conditional_replacement (basic_block cond_bb, basic_block middle_bb,
567 edge e0, edge e1, gimple phi,
568 tree arg0, tree arg1)
569 {
570 tree result;
571 gimple stmt, new_stmt;
572 tree cond;
573 gimple_stmt_iterator gsi;
574 edge true_edge, false_edge;
575 tree new_var, new_var2;
576 bool neg;
577
578 /* FIXME: Gimplification of complex type is too hard for now. */
579 /* We aren't prepared to handle vectors either (and it is a question
580 if it would be worthwhile anyway). */
581 if (!(INTEGRAL_TYPE_P (TREE_TYPE (arg0))
582 || POINTER_TYPE_P (TREE_TYPE (arg0)))
583 || !(INTEGRAL_TYPE_P (TREE_TYPE (arg1))
584 || POINTER_TYPE_P (TREE_TYPE (arg1))))
585 return false;
586
587 /* The PHI arguments have the constants 0 and 1, or 0 and -1, then
588 convert it to the conditional. */
589 if ((integer_zerop (arg0) && integer_onep (arg1))
590 || (integer_zerop (arg1) && integer_onep (arg0)))
591 neg = false;
592 else if ((integer_zerop (arg0) && integer_all_onesp (arg1))
593 || (integer_zerop (arg1) && integer_all_onesp (arg0)))
594 neg = true;
595 else
596 return false;
597
598 if (!empty_block_p (middle_bb))
599 return false;
600
601 /* At this point we know we have a GIMPLE_COND with two successors.
602 One successor is BB, the other successor is an empty block which
603 falls through into BB.
604
605 There is a single PHI node at the join point (BB) and its arguments
606 are constants (0, 1) or (0, -1).
607
608 So, given the condition COND, and the two PHI arguments, we can
609 rewrite this PHI into non-branching code:
610
611 dest = (COND) or dest = COND'
612
613 We use the condition as-is if the argument associated with the
614 true edge has the value one or the argument associated with the
615 false edge as the value zero. Note that those conditions are not
616 the same since only one of the outgoing edges from the GIMPLE_COND
617 will directly reach BB and thus be associated with an argument. */
618
619 stmt = last_stmt (cond_bb);
620 result = PHI_RESULT (phi);
621
622 /* To handle special cases like floating point comparison, it is easier and
623 less error-prone to build a tree and gimplify it on the fly though it is
624 less efficient. */
625 cond = fold_build2_loc (gimple_location (stmt),
626 gimple_cond_code (stmt), boolean_type_node,
627 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
628
629 /* We need to know which is the true edge and which is the false
630 edge so that we know when to invert the condition below. */
631 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
632 if ((e0 == true_edge && integer_zerop (arg0))
633 || (e0 == false_edge && !integer_zerop (arg0))
634 || (e1 == true_edge && integer_zerop (arg1))
635 || (e1 == false_edge && !integer_zerop (arg1)))
636 cond = fold_build1_loc (gimple_location (stmt),
637 TRUTH_NOT_EXPR, TREE_TYPE (cond), cond);
638
639 if (neg)
640 {
641 cond = fold_convert_loc (gimple_location (stmt),
642 TREE_TYPE (result), cond);
643 cond = fold_build1_loc (gimple_location (stmt),
644 NEGATE_EXPR, TREE_TYPE (cond), cond);
645 }
646
647 /* Insert our new statements at the end of conditional block before the
648 COND_STMT. */
649 gsi = gsi_for_stmt (stmt);
650 new_var = force_gimple_operand_gsi (&gsi, cond, true, NULL, true,
651 GSI_SAME_STMT);
652
653 if (!useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (new_var)))
654 {
655 source_location locus_0, locus_1;
656
657 new_var2 = make_ssa_name (TREE_TYPE (result), NULL);
658 new_stmt = gimple_build_assign_with_ops (CONVERT_EXPR, new_var2,
659 new_var, NULL);
660 gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
661 new_var = new_var2;
662
663 /* Set the locus to the first argument, unless is doesn't have one. */
664 locus_0 = gimple_phi_arg_location (phi, 0);
665 locus_1 = gimple_phi_arg_location (phi, 1);
666 if (locus_0 == UNKNOWN_LOCATION)
667 locus_0 = locus_1;
668 gimple_set_location (new_stmt, locus_0);
669 }
670
671 replace_phi_edge_with_variable (cond_bb, e1, phi, new_var);
672
673 /* Note that we optimized this PHI. */
674 return true;
675 }
676
677 /* Update *ARG which is defined in STMT so that it contains the
678 computed value if that seems profitable. Return true if the
679 statement is made dead by that rewriting. */
680
681 static bool
682 jump_function_from_stmt (tree *arg, gimple stmt)
683 {
684 enum tree_code code = gimple_assign_rhs_code (stmt);
685 if (code == ADDR_EXPR)
686 {
687 /* For arg = &p->i transform it to p, if possible. */
688 tree rhs1 = gimple_assign_rhs1 (stmt);
689 HOST_WIDE_INT offset;
690 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs1, 0),
691 &offset);
692 if (tem
693 && TREE_CODE (tem) == MEM_REF
694 && (mem_ref_offset (tem) + double_int::from_shwi (offset)).is_zero ())
695 {
696 *arg = TREE_OPERAND (tem, 0);
697 return true;
698 }
699 }
700 /* TODO: Much like IPA-CP jump-functions we want to handle constant
701 additions symbolically here, and we'd need to update the comparison
702 code that compares the arg + cst tuples in our caller. For now the
703 code above exactly handles the VEC_BASE pattern from vec.h. */
704 return false;
705 }
706
707 /* RHS is a source argument in a BIT_AND_EXPR which feeds a conditional
708 of the form SSA_NAME NE 0.
709
710 If RHS is fed by a simple EQ_EXPR comparison of two values, see if
711 the two input values of the EQ_EXPR match arg0 and arg1.
712
713 If so update *code and return TRUE. Otherwise return FALSE. */
714
715 static bool
716 rhs_is_fed_for_value_replacement (const_tree arg0, const_tree arg1,
717 enum tree_code *code, const_tree rhs)
718 {
719 /* Obviously if RHS is not an SSA_NAME, we can't look at the defining
720 statement. */
721 if (TREE_CODE (rhs) == SSA_NAME)
722 {
723 gimple def1 = SSA_NAME_DEF_STMT (rhs);
724
725 /* Verify the defining statement has an EQ_EXPR on the RHS. */
726 if (is_gimple_assign (def1) && gimple_assign_rhs_code (def1) == EQ_EXPR)
727 {
728 /* Finally verify the source operands of the EQ_EXPR are equal
729 to arg0 and arg1. */
730 tree op0 = gimple_assign_rhs1 (def1);
731 tree op1 = gimple_assign_rhs2 (def1);
732 if ((operand_equal_for_phi_arg_p (arg0, op0)
733 && operand_equal_for_phi_arg_p (arg1, op1))
734 || (operand_equal_for_phi_arg_p (arg0, op1)
735 && operand_equal_for_phi_arg_p (arg1, op0)))
736 {
737 /* We will perform the optimization. */
738 *code = gimple_assign_rhs_code (def1);
739 return true;
740 }
741 }
742 }
743 return false;
744 }
745
746 /* Return TRUE if arg0/arg1 are equal to the rhs/lhs or lhs/rhs of COND.
747
748 Also return TRUE if arg0/arg1 are equal to the source arguments of a
749 an EQ comparison feeding a BIT_AND_EXPR which feeds COND.
750
751 Return FALSE otherwise. */
752
753 static bool
754 operand_equal_for_value_replacement (const_tree arg0, const_tree arg1,
755 enum tree_code *code, gimple cond)
756 {
757 gimple def;
758 tree lhs = gimple_cond_lhs (cond);
759 tree rhs = gimple_cond_rhs (cond);
760
761 if ((operand_equal_for_phi_arg_p (arg0, lhs)
762 && operand_equal_for_phi_arg_p (arg1, rhs))
763 || (operand_equal_for_phi_arg_p (arg1, lhs)
764 && operand_equal_for_phi_arg_p (arg0, rhs)))
765 return true;
766
767 /* Now handle more complex case where we have an EQ comparison
768 which feeds a BIT_AND_EXPR which feeds COND.
769
770 First verify that COND is of the form SSA_NAME NE 0. */
771 if (*code != NE_EXPR || !integer_zerop (rhs)
772 || TREE_CODE (lhs) != SSA_NAME)
773 return false;
774
775 /* Now ensure that SSA_NAME is set by a BIT_AND_EXPR. */
776 def = SSA_NAME_DEF_STMT (lhs);
777 if (!is_gimple_assign (def) || gimple_assign_rhs_code (def) != BIT_AND_EXPR)
778 return false;
779
780 /* Now verify arg0/arg1 correspond to the source arguments of an
781 EQ comparison feeding the BIT_AND_EXPR. */
782
783 tree tmp = gimple_assign_rhs1 (def);
784 if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp))
785 return true;
786
787 tmp = gimple_assign_rhs2 (def);
788 if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp))
789 return true;
790
791 return false;
792 }
793
794 /* The function value_replacement does the main work of doing the value
795 replacement. Return non-zero if the replacement is done. Otherwise return
796 0. If we remove the middle basic block, return 2.
797 BB is the basic block where the replacement is going to be done on. ARG0
798 is argument 0 from the PHI. Likewise for ARG1. */
799
800 static int
801 value_replacement (basic_block cond_bb, basic_block middle_bb,
802 edge e0, edge e1, gimple phi,
803 tree arg0, tree arg1)
804 {
805 gimple_stmt_iterator gsi;
806 gimple cond;
807 edge true_edge, false_edge;
808 enum tree_code code;
809 bool emtpy_or_with_defined_p = true;
810
811 /* If the type says honor signed zeros we cannot do this
812 optimization. */
813 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
814 return 0;
815
816 /* If there is a statement in MIDDLE_BB that defines one of the PHI
817 arguments, then adjust arg0 or arg1. */
818 gsi = gsi_after_labels (middle_bb);
819 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
820 gsi_next_nondebug (&gsi);
821 while (!gsi_end_p (gsi))
822 {
823 gimple stmt = gsi_stmt (gsi);
824 tree lhs;
825 gsi_next_nondebug (&gsi);
826 if (!is_gimple_assign (stmt))
827 {
828 emtpy_or_with_defined_p = false;
829 continue;
830 }
831 /* Now try to adjust arg0 or arg1 according to the computation
832 in the statement. */
833 lhs = gimple_assign_lhs (stmt);
834 if (!(lhs == arg0
835 && jump_function_from_stmt (&arg0, stmt))
836 || (lhs == arg1
837 && jump_function_from_stmt (&arg1, stmt)))
838 emtpy_or_with_defined_p = false;
839 }
840
841 cond = last_stmt (cond_bb);
842 code = gimple_cond_code (cond);
843
844 /* This transformation is only valid for equality comparisons. */
845 if (code != NE_EXPR && code != EQ_EXPR)
846 return 0;
847
848 /* We need to know which is the true edge and which is the false
849 edge so that we know if have abs or negative abs. */
850 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
851
852 /* At this point we know we have a COND_EXPR with two successors.
853 One successor is BB, the other successor is an empty block which
854 falls through into BB.
855
856 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
857
858 There is a single PHI node at the join point (BB) with two arguments.
859
860 We now need to verify that the two arguments in the PHI node match
861 the two arguments to the equality comparison. */
862
863 if (operand_equal_for_value_replacement (arg0, arg1, &code, cond))
864 {
865 edge e;
866 tree arg;
867
868 /* For NE_EXPR, we want to build an assignment result = arg where
869 arg is the PHI argument associated with the true edge. For
870 EQ_EXPR we want the PHI argument associated with the false edge. */
871 e = (code == NE_EXPR ? true_edge : false_edge);
872
873 /* Unfortunately, E may not reach BB (it may instead have gone to
874 OTHER_BLOCK). If that is the case, then we want the single outgoing
875 edge from OTHER_BLOCK which reaches BB and represents the desired
876 path from COND_BLOCK. */
877 if (e->dest == middle_bb)
878 e = single_succ_edge (e->dest);
879
880 /* Now we know the incoming edge to BB that has the argument for the
881 RHS of our new assignment statement. */
882 if (e0 == e)
883 arg = arg0;
884 else
885 arg = arg1;
886
887 /* If the middle basic block was empty or is defining the
888 PHI arguments and this is a single phi where the args are different
889 for the edges e0 and e1 then we can remove the middle basic block. */
890 if (emtpy_or_with_defined_p
891 && single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)),
892 e0, e1))
893 {
894 replace_phi_edge_with_variable (cond_bb, e1, phi, arg);
895 /* Note that we optimized this PHI. */
896 return 2;
897 }
898 else
899 {
900 /* Replace the PHI arguments with arg. */
901 SET_PHI_ARG_DEF (phi, e0->dest_idx, arg);
902 SET_PHI_ARG_DEF (phi, e1->dest_idx, arg);
903 if (dump_file && (dump_flags & TDF_DETAILS))
904 {
905 fprintf (dump_file, "PHI ");
906 print_generic_expr (dump_file, gimple_phi_result (phi), 0);
907 fprintf (dump_file, " reduced for COND_EXPR in block %d to ",
908 cond_bb->index);
909 print_generic_expr (dump_file, arg, 0);
910 fprintf (dump_file, ".\n");
911 }
912 return 1;
913 }
914
915 }
916 return 0;
917 }
918
919 /* The function minmax_replacement does the main work of doing the minmax
920 replacement. Return true if the replacement is done. Otherwise return
921 false.
922 BB is the basic block where the replacement is going to be done on. ARG0
923 is argument 0 from the PHI. Likewise for ARG1. */
924
925 static bool
926 minmax_replacement (basic_block cond_bb, basic_block middle_bb,
927 edge e0, edge e1, gimple phi,
928 tree arg0, tree arg1)
929 {
930 tree result, type;
931 gimple cond, new_stmt;
932 edge true_edge, false_edge;
933 enum tree_code cmp, minmax, ass_code;
934 tree smaller, larger, arg_true, arg_false;
935 gimple_stmt_iterator gsi, gsi_from;
936
937 type = TREE_TYPE (PHI_RESULT (phi));
938
939 /* The optimization may be unsafe due to NaNs. */
940 if (HONOR_NANS (TYPE_MODE (type)))
941 return false;
942
943 cond = last_stmt (cond_bb);
944 cmp = gimple_cond_code (cond);
945
946 /* This transformation is only valid for order comparisons. Record which
947 operand is smaller/larger if the result of the comparison is true. */
948 if (cmp == LT_EXPR || cmp == LE_EXPR)
949 {
950 smaller = gimple_cond_lhs (cond);
951 larger = gimple_cond_rhs (cond);
952 }
953 else if (cmp == GT_EXPR || cmp == GE_EXPR)
954 {
955 smaller = gimple_cond_rhs (cond);
956 larger = gimple_cond_lhs (cond);
957 }
958 else
959 return false;
960
961 /* We need to know which is the true edge and which is the false
962 edge so that we know if have abs or negative abs. */
963 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
964
965 /* Forward the edges over the middle basic block. */
966 if (true_edge->dest == middle_bb)
967 true_edge = EDGE_SUCC (true_edge->dest, 0);
968 if (false_edge->dest == middle_bb)
969 false_edge = EDGE_SUCC (false_edge->dest, 0);
970
971 if (true_edge == e0)
972 {
973 gcc_assert (false_edge == e1);
974 arg_true = arg0;
975 arg_false = arg1;
976 }
977 else
978 {
979 gcc_assert (false_edge == e0);
980 gcc_assert (true_edge == e1);
981 arg_true = arg1;
982 arg_false = arg0;
983 }
984
985 if (empty_block_p (middle_bb))
986 {
987 if (operand_equal_for_phi_arg_p (arg_true, smaller)
988 && operand_equal_for_phi_arg_p (arg_false, larger))
989 {
990 /* Case
991
992 if (smaller < larger)
993 rslt = smaller;
994 else
995 rslt = larger; */
996 minmax = MIN_EXPR;
997 }
998 else if (operand_equal_for_phi_arg_p (arg_false, smaller)
999 && operand_equal_for_phi_arg_p (arg_true, larger))
1000 minmax = MAX_EXPR;
1001 else
1002 return false;
1003 }
1004 else
1005 {
1006 /* Recognize the following case, assuming d <= u:
1007
1008 if (a <= u)
1009 b = MAX (a, d);
1010 x = PHI <b, u>
1011
1012 This is equivalent to
1013
1014 b = MAX (a, d);
1015 x = MIN (b, u); */
1016
1017 gimple assign = last_and_only_stmt (middle_bb);
1018 tree lhs, op0, op1, bound;
1019
1020 if (!assign
1021 || gimple_code (assign) != GIMPLE_ASSIGN)
1022 return false;
1023
1024 lhs = gimple_assign_lhs (assign);
1025 ass_code = gimple_assign_rhs_code (assign);
1026 if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
1027 return false;
1028 op0 = gimple_assign_rhs1 (assign);
1029 op1 = gimple_assign_rhs2 (assign);
1030
1031 if (true_edge->src == middle_bb)
1032 {
1033 /* We got here if the condition is true, i.e., SMALLER < LARGER. */
1034 if (!operand_equal_for_phi_arg_p (lhs, arg_true))
1035 return false;
1036
1037 if (operand_equal_for_phi_arg_p (arg_false, larger))
1038 {
1039 /* Case
1040
1041 if (smaller < larger)
1042 {
1043 r' = MAX_EXPR (smaller, bound)
1044 }
1045 r = PHI <r', larger> --> to be turned to MIN_EXPR. */
1046 if (ass_code != MAX_EXPR)
1047 return false;
1048
1049 minmax = MIN_EXPR;
1050 if (operand_equal_for_phi_arg_p (op0, smaller))
1051 bound = op1;
1052 else if (operand_equal_for_phi_arg_p (op1, smaller))
1053 bound = op0;
1054 else
1055 return false;
1056
1057 /* We need BOUND <= LARGER. */
1058 if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
1059 bound, larger)))
1060 return false;
1061 }
1062 else if (operand_equal_for_phi_arg_p (arg_false, smaller))
1063 {
1064 /* Case
1065
1066 if (smaller < larger)
1067 {
1068 r' = MIN_EXPR (larger, bound)
1069 }
1070 r = PHI <r', smaller> --> to be turned to MAX_EXPR. */
1071 if (ass_code != MIN_EXPR)
1072 return false;
1073
1074 minmax = MAX_EXPR;
1075 if (operand_equal_for_phi_arg_p (op0, larger))
1076 bound = op1;
1077 else if (operand_equal_for_phi_arg_p (op1, larger))
1078 bound = op0;
1079 else
1080 return false;
1081
1082 /* We need BOUND >= SMALLER. */
1083 if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
1084 bound, smaller)))
1085 return false;
1086 }
1087 else
1088 return false;
1089 }
1090 else
1091 {
1092 /* We got here if the condition is false, i.e., SMALLER > LARGER. */
1093 if (!operand_equal_for_phi_arg_p (lhs, arg_false))
1094 return false;
1095
1096 if (operand_equal_for_phi_arg_p (arg_true, larger))
1097 {
1098 /* Case
1099
1100 if (smaller > larger)
1101 {
1102 r' = MIN_EXPR (smaller, bound)
1103 }
1104 r = PHI <r', larger> --> to be turned to MAX_EXPR. */
1105 if (ass_code != MIN_EXPR)
1106 return false;
1107
1108 minmax = MAX_EXPR;
1109 if (operand_equal_for_phi_arg_p (op0, smaller))
1110 bound = op1;
1111 else if (operand_equal_for_phi_arg_p (op1, smaller))
1112 bound = op0;
1113 else
1114 return false;
1115
1116 /* We need BOUND >= LARGER. */
1117 if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
1118 bound, larger)))
1119 return false;
1120 }
1121 else if (operand_equal_for_phi_arg_p (arg_true, smaller))
1122 {
1123 /* Case
1124
1125 if (smaller > larger)
1126 {
1127 r' = MAX_EXPR (larger, bound)
1128 }
1129 r = PHI <r', smaller> --> to be turned to MIN_EXPR. */
1130 if (ass_code != MAX_EXPR)
1131 return false;
1132
1133 minmax = MIN_EXPR;
1134 if (operand_equal_for_phi_arg_p (op0, larger))
1135 bound = op1;
1136 else if (operand_equal_for_phi_arg_p (op1, larger))
1137 bound = op0;
1138 else
1139 return false;
1140
1141 /* We need BOUND <= SMALLER. */
1142 if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
1143 bound, smaller)))
1144 return false;
1145 }
1146 else
1147 return false;
1148 }
1149
1150 /* Move the statement from the middle block. */
1151 gsi = gsi_last_bb (cond_bb);
1152 gsi_from = gsi_last_nondebug_bb (middle_bb);
1153 gsi_move_before (&gsi_from, &gsi);
1154 }
1155
1156 /* Emit the statement to compute min/max. */
1157 result = duplicate_ssa_name (PHI_RESULT (phi), NULL);
1158 new_stmt = gimple_build_assign_with_ops (minmax, result, arg0, arg1);
1159 gsi = gsi_last_bb (cond_bb);
1160 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1161
1162 replace_phi_edge_with_variable (cond_bb, e1, phi, result);
1163 return true;
1164 }
1165
1166 /* The function absolute_replacement does the main work of doing the absolute
1167 replacement. Return true if the replacement is done. Otherwise return
1168 false.
1169 bb is the basic block where the replacement is going to be done on. arg0
1170 is argument 0 from the phi. Likewise for arg1. */
1171
1172 static bool
1173 abs_replacement (basic_block cond_bb, basic_block middle_bb,
1174 edge e0 ATTRIBUTE_UNUSED, edge e1,
1175 gimple phi, tree arg0, tree arg1)
1176 {
1177 tree result;
1178 gimple new_stmt, cond;
1179 gimple_stmt_iterator gsi;
1180 edge true_edge, false_edge;
1181 gimple assign;
1182 edge e;
1183 tree rhs, lhs;
1184 bool negate;
1185 enum tree_code cond_code;
1186
1187 /* If the type says honor signed zeros we cannot do this
1188 optimization. */
1189 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
1190 return false;
1191
1192 /* OTHER_BLOCK must have only one executable statement which must have the
1193 form arg0 = -arg1 or arg1 = -arg0. */
1194
1195 assign = last_and_only_stmt (middle_bb);
1196 /* If we did not find the proper negation assignment, then we can not
1197 optimize. */
1198 if (assign == NULL)
1199 return false;
1200
1201 /* If we got here, then we have found the only executable statement
1202 in OTHER_BLOCK. If it is anything other than arg = -arg1 or
1203 arg1 = -arg0, then we can not optimize. */
1204 if (gimple_code (assign) != GIMPLE_ASSIGN)
1205 return false;
1206
1207 lhs = gimple_assign_lhs (assign);
1208
1209 if (gimple_assign_rhs_code (assign) != NEGATE_EXPR)
1210 return false;
1211
1212 rhs = gimple_assign_rhs1 (assign);
1213
1214 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
1215 if (!(lhs == arg0 && rhs == arg1)
1216 && !(lhs == arg1 && rhs == arg0))
1217 return false;
1218
1219 cond = last_stmt (cond_bb);
1220 result = PHI_RESULT (phi);
1221
1222 /* Only relationals comparing arg[01] against zero are interesting. */
1223 cond_code = gimple_cond_code (cond);
1224 if (cond_code != GT_EXPR && cond_code != GE_EXPR
1225 && cond_code != LT_EXPR && cond_code != LE_EXPR)
1226 return false;
1227
1228 /* Make sure the conditional is arg[01] OP y. */
1229 if (gimple_cond_lhs (cond) != rhs)
1230 return false;
1231
1232 if (FLOAT_TYPE_P (TREE_TYPE (gimple_cond_rhs (cond)))
1233 ? real_zerop (gimple_cond_rhs (cond))
1234 : integer_zerop (gimple_cond_rhs (cond)))
1235 ;
1236 else
1237 return false;
1238
1239 /* We need to know which is the true edge and which is the false
1240 edge so that we know if have abs or negative abs. */
1241 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
1242
1243 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
1244 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
1245 the false edge goes to OTHER_BLOCK. */
1246 if (cond_code == GT_EXPR || cond_code == GE_EXPR)
1247 e = true_edge;
1248 else
1249 e = false_edge;
1250
1251 if (e->dest == middle_bb)
1252 negate = true;
1253 else
1254 negate = false;
1255
1256 result = duplicate_ssa_name (result, NULL);
1257
1258 if (negate)
1259 lhs = make_ssa_name (TREE_TYPE (result), NULL);
1260 else
1261 lhs = result;
1262
1263 /* Build the modify expression with abs expression. */
1264 new_stmt = gimple_build_assign_with_ops (ABS_EXPR, lhs, rhs, NULL);
1265
1266 gsi = gsi_last_bb (cond_bb);
1267 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1268
1269 if (negate)
1270 {
1271 /* Get the right GSI. We want to insert after the recently
1272 added ABS_EXPR statement (which we know is the first statement
1273 in the block. */
1274 new_stmt = gimple_build_assign_with_ops (NEGATE_EXPR, result, lhs, NULL);
1275
1276 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1277 }
1278
1279 replace_phi_edge_with_variable (cond_bb, e1, phi, result);
1280
1281 /* Note that we optimized this PHI. */
1282 return true;
1283 }
1284
1285 /* Auxiliary functions to determine the set of memory accesses which
1286 can't trap because they are preceded by accesses to the same memory
1287 portion. We do that for MEM_REFs, so we only need to track
1288 the SSA_NAME of the pointer indirectly referenced. The algorithm
1289 simply is a walk over all instructions in dominator order. When
1290 we see an MEM_REF we determine if we've already seen a same
1291 ref anywhere up to the root of the dominator tree. If we do the
1292 current access can't trap. If we don't see any dominating access
1293 the current access might trap, but might also make later accesses
1294 non-trapping, so we remember it. We need to be careful with loads
1295 or stores, for instance a load might not trap, while a store would,
1296 so if we see a dominating read access this doesn't mean that a later
1297 write access would not trap. Hence we also need to differentiate the
1298 type of access(es) seen.
1299
1300 ??? We currently are very conservative and assume that a load might
1301 trap even if a store doesn't (write-only memory). This probably is
1302 overly conservative. */
1303
1304 /* A hash-table of SSA_NAMEs, and in which basic block an MEM_REF
1305 through it was seen, which would constitute a no-trap region for
1306 same accesses. */
1307 struct name_to_bb
1308 {
1309 unsigned int ssa_name_ver;
1310 unsigned int phase;
1311 bool store;
1312 HOST_WIDE_INT offset, size;
1313 basic_block bb;
1314 };
1315
1316 /* Hashtable helpers. */
1317
1318 struct ssa_names_hasher : typed_free_remove <name_to_bb>
1319 {
1320 typedef name_to_bb value_type;
1321 typedef name_to_bb compare_type;
1322 static inline hashval_t hash (const value_type *);
1323 static inline bool equal (const value_type *, const compare_type *);
1324 };
1325
1326 /* Used for quick clearing of the hash-table when we see calls.
1327 Hash entries with phase < nt_call_phase are invalid. */
1328 static unsigned int nt_call_phase;
1329
1330 /* The hash function. */
1331
1332 inline hashval_t
1333 ssa_names_hasher::hash (const value_type *n)
1334 {
1335 return n->ssa_name_ver ^ (((hashval_t) n->store) << 31)
1336 ^ (n->offset << 6) ^ (n->size << 3);
1337 }
1338
1339 /* The equality function of *P1 and *P2. */
1340
1341 inline bool
1342 ssa_names_hasher::equal (const value_type *n1, const compare_type *n2)
1343 {
1344 return n1->ssa_name_ver == n2->ssa_name_ver
1345 && n1->store == n2->store
1346 && n1->offset == n2->offset
1347 && n1->size == n2->size;
1348 }
1349
1350 /* The hash table for remembering what we've seen. */
1351 static hash_table <ssa_names_hasher> seen_ssa_names;
1352
1353 /* We see the expression EXP in basic block BB. If it's an interesting
1354 expression (an MEM_REF through an SSA_NAME) possibly insert the
1355 expression into the set NONTRAP or the hash table of seen expressions.
1356 STORE is true if this expression is on the LHS, otherwise it's on
1357 the RHS. */
1358 static void
1359 add_or_mark_expr (basic_block bb, tree exp,
1360 struct pointer_set_t *nontrap, bool store)
1361 {
1362 HOST_WIDE_INT size;
1363
1364 if (TREE_CODE (exp) == MEM_REF
1365 && TREE_CODE (TREE_OPERAND (exp, 0)) == SSA_NAME
1366 && tree_fits_shwi_p (TREE_OPERAND (exp, 1))
1367 && (size = int_size_in_bytes (TREE_TYPE (exp))) > 0)
1368 {
1369 tree name = TREE_OPERAND (exp, 0);
1370 struct name_to_bb map;
1371 name_to_bb **slot;
1372 struct name_to_bb *n2bb;
1373 basic_block found_bb = 0;
1374
1375 /* Try to find the last seen MEM_REF through the same
1376 SSA_NAME, which can trap. */
1377 map.ssa_name_ver = SSA_NAME_VERSION (name);
1378 map.phase = 0;
1379 map.bb = 0;
1380 map.store = store;
1381 map.offset = tree_to_shwi (TREE_OPERAND (exp, 1));
1382 map.size = size;
1383
1384 slot = seen_ssa_names.find_slot (&map, INSERT);
1385 n2bb = *slot;
1386 if (n2bb && n2bb->phase >= nt_call_phase)
1387 found_bb = n2bb->bb;
1388
1389 /* If we've found a trapping MEM_REF, _and_ it dominates EXP
1390 (it's in a basic block on the path from us to the dominator root)
1391 then we can't trap. */
1392 if (found_bb && (((size_t)found_bb->aux) & 1) == 1)
1393 {
1394 pointer_set_insert (nontrap, exp);
1395 }
1396 else
1397 {
1398 /* EXP might trap, so insert it into the hash table. */
1399 if (n2bb)
1400 {
1401 n2bb->phase = nt_call_phase;
1402 n2bb->bb = bb;
1403 }
1404 else
1405 {
1406 n2bb = XNEW (struct name_to_bb);
1407 n2bb->ssa_name_ver = SSA_NAME_VERSION (name);
1408 n2bb->phase = nt_call_phase;
1409 n2bb->bb = bb;
1410 n2bb->store = store;
1411 n2bb->offset = map.offset;
1412 n2bb->size = size;
1413 *slot = n2bb;
1414 }
1415 }
1416 }
1417 }
1418
1419 class nontrapping_dom_walker : public dom_walker
1420 {
1421 public:
1422 nontrapping_dom_walker (cdi_direction direction, pointer_set_t *ps)
1423 : dom_walker (direction), m_nontrapping (ps) {}
1424
1425 virtual void before_dom_children (basic_block);
1426 virtual void after_dom_children (basic_block);
1427
1428 private:
1429 pointer_set_t *m_nontrapping;
1430 };
1431
1432 /* Called by walk_dominator_tree, when entering the block BB. */
1433 void
1434 nontrapping_dom_walker::before_dom_children (basic_block bb)
1435 {
1436 edge e;
1437 edge_iterator ei;
1438 gimple_stmt_iterator gsi;
1439
1440 /* If we haven't seen all our predecessors, clear the hash-table. */
1441 FOR_EACH_EDGE (e, ei, bb->preds)
1442 if ((((size_t)e->src->aux) & 2) == 0)
1443 {
1444 nt_call_phase++;
1445 break;
1446 }
1447
1448 /* Mark this BB as being on the path to dominator root and as visited. */
1449 bb->aux = (void*)(1 | 2);
1450
1451 /* And walk the statements in order. */
1452 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1453 {
1454 gimple stmt = gsi_stmt (gsi);
1455
1456 if (is_gimple_call (stmt) && !nonfreeing_call_p (stmt))
1457 nt_call_phase++;
1458 else if (gimple_assign_single_p (stmt) && !gimple_has_volatile_ops (stmt))
1459 {
1460 add_or_mark_expr (bb, gimple_assign_lhs (stmt), m_nontrapping, true);
1461 add_or_mark_expr (bb, gimple_assign_rhs1 (stmt), m_nontrapping, false);
1462 }
1463 }
1464 }
1465
1466 /* Called by walk_dominator_tree, when basic block BB is exited. */
1467 void
1468 nontrapping_dom_walker::after_dom_children (basic_block bb)
1469 {
1470 /* This BB isn't on the path to dominator root anymore. */
1471 bb->aux = (void*)2;
1472 }
1473
1474 /* This is the entry point of gathering non trapping memory accesses.
1475 It will do a dominator walk over the whole function, and it will
1476 make use of the bb->aux pointers. It returns a set of trees
1477 (the MEM_REFs itself) which can't trap. */
1478 static struct pointer_set_t *
1479 get_non_trapping (void)
1480 {
1481 nt_call_phase = 0;
1482 pointer_set_t *nontrap = pointer_set_create ();
1483 seen_ssa_names.create (128);
1484 /* We're going to do a dominator walk, so ensure that we have
1485 dominance information. */
1486 calculate_dominance_info (CDI_DOMINATORS);
1487
1488 nontrapping_dom_walker (CDI_DOMINATORS, nontrap)
1489 .walk (cfun->cfg->x_entry_block_ptr);
1490
1491 seen_ssa_names.dispose ();
1492
1493 clear_aux_for_blocks ();
1494 return nontrap;
1495 }
1496
1497 /* Do the main work of conditional store replacement. We already know
1498 that the recognized pattern looks like so:
1499
1500 split:
1501 if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
1502 MIDDLE_BB:
1503 something
1504 fallthrough (edge E0)
1505 JOIN_BB:
1506 some more
1507
1508 We check that MIDDLE_BB contains only one store, that that store
1509 doesn't trap (not via NOTRAP, but via checking if an access to the same
1510 memory location dominates us) and that the store has a "simple" RHS. */
1511
1512 static bool
1513 cond_store_replacement (basic_block middle_bb, basic_block join_bb,
1514 edge e0, edge e1, struct pointer_set_t *nontrap)
1515 {
1516 gimple assign = last_and_only_stmt (middle_bb);
1517 tree lhs, rhs, name, name2;
1518 gimple newphi, new_stmt;
1519 gimple_stmt_iterator gsi;
1520 source_location locus;
1521
1522 /* Check if middle_bb contains of only one store. */
1523 if (!assign
1524 || !gimple_assign_single_p (assign)
1525 || gimple_has_volatile_ops (assign))
1526 return false;
1527
1528 locus = gimple_location (assign);
1529 lhs = gimple_assign_lhs (assign);
1530 rhs = gimple_assign_rhs1 (assign);
1531 if (TREE_CODE (lhs) != MEM_REF
1532 || TREE_CODE (TREE_OPERAND (lhs, 0)) != SSA_NAME
1533 || !is_gimple_reg_type (TREE_TYPE (lhs)))
1534 return false;
1535
1536 /* Prove that we can move the store down. We could also check
1537 TREE_THIS_NOTRAP here, but in that case we also could move stores,
1538 whose value is not available readily, which we want to avoid. */
1539 if (!pointer_set_contains (nontrap, lhs))
1540 return false;
1541
1542 /* Now we've checked the constraints, so do the transformation:
1543 1) Remove the single store. */
1544 gsi = gsi_for_stmt (assign);
1545 unlink_stmt_vdef (assign);
1546 gsi_remove (&gsi, true);
1547 release_defs (assign);
1548
1549 /* 2) Insert a load from the memory of the store to the temporary
1550 on the edge which did not contain the store. */
1551 lhs = unshare_expr (lhs);
1552 name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
1553 new_stmt = gimple_build_assign (name, lhs);
1554 gimple_set_location (new_stmt, locus);
1555 gsi_insert_on_edge (e1, new_stmt);
1556
1557 /* 3) Create a PHI node at the join block, with one argument
1558 holding the old RHS, and the other holding the temporary
1559 where we stored the old memory contents. */
1560 name2 = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
1561 newphi = create_phi_node (name2, join_bb);
1562 add_phi_arg (newphi, rhs, e0, locus);
1563 add_phi_arg (newphi, name, e1, locus);
1564
1565 lhs = unshare_expr (lhs);
1566 new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
1567
1568 /* 4) Insert that PHI node. */
1569 gsi = gsi_after_labels (join_bb);
1570 if (gsi_end_p (gsi))
1571 {
1572 gsi = gsi_last_bb (join_bb);
1573 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1574 }
1575 else
1576 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1577
1578 return true;
1579 }
1580
1581 /* Do the main work of conditional store replacement. */
1582
1583 static bool
1584 cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,
1585 basic_block join_bb, gimple then_assign,
1586 gimple else_assign)
1587 {
1588 tree lhs_base, lhs, then_rhs, else_rhs, name;
1589 source_location then_locus, else_locus;
1590 gimple_stmt_iterator gsi;
1591 gimple newphi, new_stmt;
1592
1593 if (then_assign == NULL
1594 || !gimple_assign_single_p (then_assign)
1595 || gimple_clobber_p (then_assign)
1596 || gimple_has_volatile_ops (then_assign)
1597 || else_assign == NULL
1598 || !gimple_assign_single_p (else_assign)
1599 || gimple_clobber_p (else_assign)
1600 || gimple_has_volatile_ops (else_assign))
1601 return false;
1602
1603 lhs = gimple_assign_lhs (then_assign);
1604 if (!is_gimple_reg_type (TREE_TYPE (lhs))
1605 || !operand_equal_p (lhs, gimple_assign_lhs (else_assign), 0))
1606 return false;
1607
1608 lhs_base = get_base_address (lhs);
1609 if (lhs_base == NULL_TREE
1610 || (!DECL_P (lhs_base) && TREE_CODE (lhs_base) != MEM_REF))
1611 return false;
1612
1613 then_rhs = gimple_assign_rhs1 (then_assign);
1614 else_rhs = gimple_assign_rhs1 (else_assign);
1615 then_locus = gimple_location (then_assign);
1616 else_locus = gimple_location (else_assign);
1617
1618 /* Now we've checked the constraints, so do the transformation:
1619 1) Remove the stores. */
1620 gsi = gsi_for_stmt (then_assign);
1621 unlink_stmt_vdef (then_assign);
1622 gsi_remove (&gsi, true);
1623 release_defs (then_assign);
1624
1625 gsi = gsi_for_stmt (else_assign);
1626 unlink_stmt_vdef (else_assign);
1627 gsi_remove (&gsi, true);
1628 release_defs (else_assign);
1629
1630 /* 2) Create a PHI node at the join block, with one argument
1631 holding the old RHS, and the other holding the temporary
1632 where we stored the old memory contents. */
1633 name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
1634 newphi = create_phi_node (name, join_bb);
1635 add_phi_arg (newphi, then_rhs, EDGE_SUCC (then_bb, 0), then_locus);
1636 add_phi_arg (newphi, else_rhs, EDGE_SUCC (else_bb, 0), else_locus);
1637
1638 new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
1639
1640 /* 3) Insert that PHI node. */
1641 gsi = gsi_after_labels (join_bb);
1642 if (gsi_end_p (gsi))
1643 {
1644 gsi = gsi_last_bb (join_bb);
1645 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1646 }
1647 else
1648 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1649
1650 return true;
1651 }
1652
1653 /* Conditional store replacement. We already know
1654 that the recognized pattern looks like so:
1655
1656 split:
1657 if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
1658 THEN_BB:
1659 ...
1660 X = Y;
1661 ...
1662 goto JOIN_BB;
1663 ELSE_BB:
1664 ...
1665 X = Z;
1666 ...
1667 fallthrough (edge E0)
1668 JOIN_BB:
1669 some more
1670
1671 We check that it is safe to sink the store to JOIN_BB by verifying that
1672 there are no read-after-write or write-after-write dependencies in
1673 THEN_BB and ELSE_BB. */
1674
1675 static bool
1676 cond_if_else_store_replacement (basic_block then_bb, basic_block else_bb,
1677 basic_block join_bb)
1678 {
1679 gimple then_assign = last_and_only_stmt (then_bb);
1680 gimple else_assign = last_and_only_stmt (else_bb);
1681 vec<data_reference_p> then_datarefs, else_datarefs;
1682 vec<ddr_p> then_ddrs, else_ddrs;
1683 gimple then_store, else_store;
1684 bool found, ok = false, res;
1685 struct data_dependence_relation *ddr;
1686 data_reference_p then_dr, else_dr;
1687 int i, j;
1688 tree then_lhs, else_lhs;
1689 basic_block blocks[3];
1690
1691 if (MAX_STORES_TO_SINK == 0)
1692 return false;
1693
1694 /* Handle the case with single statement in THEN_BB and ELSE_BB. */
1695 if (then_assign && else_assign)
1696 return cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
1697 then_assign, else_assign);
1698
1699 /* Find data references. */
1700 then_datarefs.create (1);
1701 else_datarefs.create (1);
1702 if ((find_data_references_in_bb (NULL, then_bb, &then_datarefs)
1703 == chrec_dont_know)
1704 || !then_datarefs.length ()
1705 || (find_data_references_in_bb (NULL, else_bb, &else_datarefs)
1706 == chrec_dont_know)
1707 || !else_datarefs.length ())
1708 {
1709 free_data_refs (then_datarefs);
1710 free_data_refs (else_datarefs);
1711 return false;
1712 }
1713
1714 /* Find pairs of stores with equal LHS. */
1715 stack_vec<gimple, 1> then_stores, else_stores;
1716 FOR_EACH_VEC_ELT (then_datarefs, i, then_dr)
1717 {
1718 if (DR_IS_READ (then_dr))
1719 continue;
1720
1721 then_store = DR_STMT (then_dr);
1722 then_lhs = gimple_get_lhs (then_store);
1723 found = false;
1724
1725 FOR_EACH_VEC_ELT (else_datarefs, j, else_dr)
1726 {
1727 if (DR_IS_READ (else_dr))
1728 continue;
1729
1730 else_store = DR_STMT (else_dr);
1731 else_lhs = gimple_get_lhs (else_store);
1732
1733 if (operand_equal_p (then_lhs, else_lhs, 0))
1734 {
1735 found = true;
1736 break;
1737 }
1738 }
1739
1740 if (!found)
1741 continue;
1742
1743 then_stores.safe_push (then_store);
1744 else_stores.safe_push (else_store);
1745 }
1746
1747 /* No pairs of stores found. */
1748 if (!then_stores.length ()
1749 || then_stores.length () > (unsigned) MAX_STORES_TO_SINK)
1750 {
1751 free_data_refs (then_datarefs);
1752 free_data_refs (else_datarefs);
1753 return false;
1754 }
1755
1756 /* Compute and check data dependencies in both basic blocks. */
1757 then_ddrs.create (1);
1758 else_ddrs.create (1);
1759 if (!compute_all_dependences (then_datarefs, &then_ddrs,
1760 vNULL, false)
1761 || !compute_all_dependences (else_datarefs, &else_ddrs,
1762 vNULL, false))
1763 {
1764 free_dependence_relations (then_ddrs);
1765 free_dependence_relations (else_ddrs);
1766 free_data_refs (then_datarefs);
1767 free_data_refs (else_datarefs);
1768 return false;
1769 }
1770 blocks[0] = then_bb;
1771 blocks[1] = else_bb;
1772 blocks[2] = join_bb;
1773 renumber_gimple_stmt_uids_in_blocks (blocks, 3);
1774
1775 /* Check that there are no read-after-write or write-after-write dependencies
1776 in THEN_BB. */
1777 FOR_EACH_VEC_ELT (then_ddrs, i, ddr)
1778 {
1779 struct data_reference *dra = DDR_A (ddr);
1780 struct data_reference *drb = DDR_B (ddr);
1781
1782 if (DDR_ARE_DEPENDENT (ddr) != chrec_known
1783 && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
1784 && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
1785 || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
1786 && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
1787 || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
1788 {
1789 free_dependence_relations (then_ddrs);
1790 free_dependence_relations (else_ddrs);
1791 free_data_refs (then_datarefs);
1792 free_data_refs (else_datarefs);
1793 return false;
1794 }
1795 }
1796
1797 /* Check that there are no read-after-write or write-after-write dependencies
1798 in ELSE_BB. */
1799 FOR_EACH_VEC_ELT (else_ddrs, i, ddr)
1800 {
1801 struct data_reference *dra = DDR_A (ddr);
1802 struct data_reference *drb = DDR_B (ddr);
1803
1804 if (DDR_ARE_DEPENDENT (ddr) != chrec_known
1805 && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
1806 && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
1807 || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
1808 && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
1809 || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
1810 {
1811 free_dependence_relations (then_ddrs);
1812 free_dependence_relations (else_ddrs);
1813 free_data_refs (then_datarefs);
1814 free_data_refs (else_datarefs);
1815 return false;
1816 }
1817 }
1818
1819 /* Sink stores with same LHS. */
1820 FOR_EACH_VEC_ELT (then_stores, i, then_store)
1821 {
1822 else_store = else_stores[i];
1823 res = cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
1824 then_store, else_store);
1825 ok = ok || res;
1826 }
1827
1828 free_dependence_relations (then_ddrs);
1829 free_dependence_relations (else_ddrs);
1830 free_data_refs (then_datarefs);
1831 free_data_refs (else_datarefs);
1832
1833 return ok;
1834 }
1835
1836 /* Return TRUE if STMT has a VUSE whose corresponding VDEF is in BB. */
1837
1838 static bool
1839 local_mem_dependence (gimple stmt, basic_block bb)
1840 {
1841 tree vuse = gimple_vuse (stmt);
1842 gimple def;
1843
1844 if (!vuse)
1845 return false;
1846
1847 def = SSA_NAME_DEF_STMT (vuse);
1848 return (def && gimple_bb (def) == bb);
1849 }
1850
1851 /* Given a "diamond" control-flow pattern where BB0 tests a condition,
1852 BB1 and BB2 are "then" and "else" blocks dependent on this test,
1853 and BB3 rejoins control flow following BB1 and BB2, look for
1854 opportunities to hoist loads as follows. If BB3 contains a PHI of
1855 two loads, one each occurring in BB1 and BB2, and the loads are
1856 provably of adjacent fields in the same structure, then move both
1857 loads into BB0. Of course this can only be done if there are no
1858 dependencies preventing such motion.
1859
1860 One of the hoisted loads will always be speculative, so the
1861 transformation is currently conservative:
1862
1863 - The fields must be strictly adjacent.
1864 - The two fields must occupy a single memory block that is
1865 guaranteed to not cross a page boundary.
1866
1867 The last is difficult to prove, as such memory blocks should be
1868 aligned on the minimum of the stack alignment boundary and the
1869 alignment guaranteed by heap allocation interfaces. Thus we rely
1870 on a parameter for the alignment value.
1871
1872 Provided a good value is used for the last case, the first
1873 restriction could possibly be relaxed. */
1874
1875 static void
1876 hoist_adjacent_loads (basic_block bb0, basic_block bb1,
1877 basic_block bb2, basic_block bb3)
1878 {
1879 int param_align = PARAM_VALUE (PARAM_L1_CACHE_LINE_SIZE);
1880 unsigned param_align_bits = (unsigned) (param_align * BITS_PER_UNIT);
1881 gimple_stmt_iterator gsi;
1882
1883 /* Walk the phis in bb3 looking for an opportunity. We are looking
1884 for phis of two SSA names, one each of which is defined in bb1 and
1885 bb2. */
1886 for (gsi = gsi_start_phis (bb3); !gsi_end_p (gsi); gsi_next (&gsi))
1887 {
1888 gimple phi_stmt = gsi_stmt (gsi);
1889 gimple def1, def2, defswap;
1890 tree arg1, arg2, ref1, ref2, field1, field2, fieldswap;
1891 tree tree_offset1, tree_offset2, tree_size2, next;
1892 int offset1, offset2, size2;
1893 unsigned align1;
1894 gimple_stmt_iterator gsi2;
1895 basic_block bb_for_def1, bb_for_def2;
1896
1897 if (gimple_phi_num_args (phi_stmt) != 2
1898 || virtual_operand_p (gimple_phi_result (phi_stmt)))
1899 continue;
1900
1901 arg1 = gimple_phi_arg_def (phi_stmt, 0);
1902 arg2 = gimple_phi_arg_def (phi_stmt, 1);
1903
1904 if (TREE_CODE (arg1) != SSA_NAME
1905 || TREE_CODE (arg2) != SSA_NAME
1906 || SSA_NAME_IS_DEFAULT_DEF (arg1)
1907 || SSA_NAME_IS_DEFAULT_DEF (arg2))
1908 continue;
1909
1910 def1 = SSA_NAME_DEF_STMT (arg1);
1911 def2 = SSA_NAME_DEF_STMT (arg2);
1912
1913 if ((gimple_bb (def1) != bb1 || gimple_bb (def2) != bb2)
1914 && (gimple_bb (def2) != bb1 || gimple_bb (def1) != bb2))
1915 continue;
1916
1917 /* Check the mode of the arguments to be sure a conditional move
1918 can be generated for it. */
1919 if (optab_handler (movcc_optab, TYPE_MODE (TREE_TYPE (arg1)))
1920 == CODE_FOR_nothing)
1921 continue;
1922
1923 /* Both statements must be assignments whose RHS is a COMPONENT_REF. */
1924 if (!gimple_assign_single_p (def1)
1925 || !gimple_assign_single_p (def2)
1926 || gimple_has_volatile_ops (def1)
1927 || gimple_has_volatile_ops (def2))
1928 continue;
1929
1930 ref1 = gimple_assign_rhs1 (def1);
1931 ref2 = gimple_assign_rhs1 (def2);
1932
1933 if (TREE_CODE (ref1) != COMPONENT_REF
1934 || TREE_CODE (ref2) != COMPONENT_REF)
1935 continue;
1936
1937 /* The zeroth operand of the two component references must be
1938 identical. It is not sufficient to compare get_base_address of
1939 the two references, because this could allow for different
1940 elements of the same array in the two trees. It is not safe to
1941 assume that the existence of one array element implies the
1942 existence of a different one. */
1943 if (!operand_equal_p (TREE_OPERAND (ref1, 0), TREE_OPERAND (ref2, 0), 0))
1944 continue;
1945
1946 field1 = TREE_OPERAND (ref1, 1);
1947 field2 = TREE_OPERAND (ref2, 1);
1948
1949 /* Check for field adjacency, and ensure field1 comes first. */
1950 for (next = DECL_CHAIN (field1);
1951 next && TREE_CODE (next) != FIELD_DECL;
1952 next = DECL_CHAIN (next))
1953 ;
1954
1955 if (next != field2)
1956 {
1957 for (next = DECL_CHAIN (field2);
1958 next && TREE_CODE (next) != FIELD_DECL;
1959 next = DECL_CHAIN (next))
1960 ;
1961
1962 if (next != field1)
1963 continue;
1964
1965 fieldswap = field1;
1966 field1 = field2;
1967 field2 = fieldswap;
1968 defswap = def1;
1969 def1 = def2;
1970 def2 = defswap;
1971 }
1972
1973 bb_for_def1 = gimple_bb (def1);
1974 bb_for_def2 = gimple_bb (def2);
1975
1976 /* Check for proper alignment of the first field. */
1977 tree_offset1 = bit_position (field1);
1978 tree_offset2 = bit_position (field2);
1979 tree_size2 = DECL_SIZE (field2);
1980
1981 if (!tree_fits_uhwi_p (tree_offset1)
1982 || !tree_fits_uhwi_p (tree_offset2)
1983 || !tree_fits_uhwi_p (tree_size2))
1984 continue;
1985
1986 offset1 = tree_to_uhwi (tree_offset1);
1987 offset2 = tree_to_uhwi (tree_offset2);
1988 size2 = tree_to_uhwi (tree_size2);
1989 align1 = DECL_ALIGN (field1) % param_align_bits;
1990
1991 if (offset1 % BITS_PER_UNIT != 0)
1992 continue;
1993
1994 /* For profitability, the two field references should fit within
1995 a single cache line. */
1996 if (align1 + offset2 - offset1 + size2 > param_align_bits)
1997 continue;
1998
1999 /* The two expressions cannot be dependent upon vdefs defined
2000 in bb1/bb2. */
2001 if (local_mem_dependence (def1, bb_for_def1)
2002 || local_mem_dependence (def2, bb_for_def2))
2003 continue;
2004
2005 /* The conditions are satisfied; hoist the loads from bb1 and bb2 into
2006 bb0. We hoist the first one first so that a cache miss is handled
2007 efficiently regardless of hardware cache-fill policy. */
2008 gsi2 = gsi_for_stmt (def1);
2009 gsi_move_to_bb_end (&gsi2, bb0);
2010 gsi2 = gsi_for_stmt (def2);
2011 gsi_move_to_bb_end (&gsi2, bb0);
2012
2013 if (dump_file && (dump_flags & TDF_DETAILS))
2014 {
2015 fprintf (dump_file,
2016 "\nHoisting adjacent loads from %d and %d into %d: \n",
2017 bb_for_def1->index, bb_for_def2->index, bb0->index);
2018 print_gimple_stmt (dump_file, def1, 0, TDF_VOPS|TDF_MEMSYMS);
2019 print_gimple_stmt (dump_file, def2, 0, TDF_VOPS|TDF_MEMSYMS);
2020 }
2021 }
2022 }
2023
2024 /* Determine whether we should attempt to hoist adjacent loads out of
2025 diamond patterns in pass_phiopt. Always hoist loads if
2026 -fhoist-adjacent-loads is specified and the target machine has
2027 both a conditional move instruction and a defined cache line size. */
2028
2029 static bool
2030 gate_hoist_loads (void)
2031 {
2032 return (flag_hoist_adjacent_loads == 1
2033 && PARAM_VALUE (PARAM_L1_CACHE_LINE_SIZE)
2034 && HAVE_conditional_move);
2035 }
2036
2037 /* Always do these optimizations if we have SSA
2038 trees to work on. */
2039 static bool
2040 gate_phiopt (void)
2041 {
2042 return 1;
2043 }
2044
2045 namespace {
2046
2047 const pass_data pass_data_phiopt =
2048 {
2049 GIMPLE_PASS, /* type */
2050 "phiopt", /* name */
2051 OPTGROUP_NONE, /* optinfo_flags */
2052 true, /* has_gate */
2053 true, /* has_execute */
2054 TV_TREE_PHIOPT, /* tv_id */
2055 ( PROP_cfg | PROP_ssa ), /* properties_required */
2056 0, /* properties_provided */
2057 0, /* properties_destroyed */
2058 0, /* todo_flags_start */
2059 ( TODO_verify_ssa | TODO_verify_flow
2060 | TODO_verify_stmts ), /* todo_flags_finish */
2061 };
2062
2063 class pass_phiopt : public gimple_opt_pass
2064 {
2065 public:
2066 pass_phiopt (gcc::context *ctxt)
2067 : gimple_opt_pass (pass_data_phiopt, ctxt)
2068 {}
2069
2070 /* opt_pass methods: */
2071 opt_pass * clone () { return new pass_phiopt (m_ctxt); }
2072 bool gate () { return gate_phiopt (); }
2073 unsigned int execute () { return tree_ssa_phiopt (); }
2074
2075 }; // class pass_phiopt
2076
2077 } // anon namespace
2078
2079 gimple_opt_pass *
2080 make_pass_phiopt (gcc::context *ctxt)
2081 {
2082 return new pass_phiopt (ctxt);
2083 }
2084
2085 static bool
2086 gate_cselim (void)
2087 {
2088 return flag_tree_cselim;
2089 }
2090
2091 namespace {
2092
2093 const pass_data pass_data_cselim =
2094 {
2095 GIMPLE_PASS, /* type */
2096 "cselim", /* name */
2097 OPTGROUP_NONE, /* optinfo_flags */
2098 true, /* has_gate */
2099 true, /* has_execute */
2100 TV_TREE_PHIOPT, /* tv_id */
2101 ( PROP_cfg | PROP_ssa ), /* properties_required */
2102 0, /* properties_provided */
2103 0, /* properties_destroyed */
2104 0, /* todo_flags_start */
2105 ( TODO_verify_ssa | TODO_verify_flow
2106 | TODO_verify_stmts ), /* todo_flags_finish */
2107 };
2108
2109 class pass_cselim : public gimple_opt_pass
2110 {
2111 public:
2112 pass_cselim (gcc::context *ctxt)
2113 : gimple_opt_pass (pass_data_cselim, ctxt)
2114 {}
2115
2116 /* opt_pass methods: */
2117 bool gate () { return gate_cselim (); }
2118 unsigned int execute () { return tree_ssa_cs_elim (); }
2119
2120 }; // class pass_cselim
2121
2122 } // anon namespace
2123
2124 gimple_opt_pass *
2125 make_pass_cselim (gcc::context *ctxt)
2126 {
2127 return new pass_cselim (ctxt);
2128 }