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