]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-phiopt.c
2015-06-04 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / tree-ssa-phiopt.c
CommitLineData
4ee9c684 1/* Optimization of PHI nodes by converting them into straightline code.
d353bf18 2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
4ee9c684 3
4This file is part of GCC.
20e5647c 5
4ee9c684 6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8c4c00c1 8Free Software Foundation; either version 3, or (at your option) any
4ee9c684 9later version.
20e5647c 10
4ee9c684 11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
20e5647c 15
4ee9c684 16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
4ee9c684 19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
d9dd21a8 23#include "hash-table.h"
4ee9c684 24#include "tm.h"
b20a8bb4 25#include "hash-set.h"
b20a8bb4 26#include "vec.h"
b20a8bb4 27#include "input.h"
28#include "alias.h"
29#include "symtab.h"
b20a8bb4 30#include "inchash.h"
4ee9c684 31#include "tree.h"
b20a8bb4 32#include "fold-const.h"
9ed99284 33#include "stor-layout.h"
0beac6fc 34#include "flags.h"
4ee9c684 35#include "tm_p.h"
94ea8568 36#include "predict.h"
94ea8568 37#include "hard-reg-set.h"
94ea8568 38#include "function.h"
39#include "dominance.h"
40#include "cfg.h"
41#include "cfganal.h"
42#include "basic-block.h"
bc61cadb 43#include "tree-ssa-alias.h"
44#include "internal-fn.h"
45#include "gimple-expr.h"
46#include "is-a.h"
073c1fd5 47#include "gimple.h"
a8783bee 48#include "gimplify.h"
dcf1a1ec 49#include "gimple-iterator.h"
e795d6e1 50#include "gimplify-me.h"
073c1fd5 51#include "gimple-ssa.h"
52#include "tree-cfg.h"
53#include "tree-phinodes.h"
54#include "ssa-iterators.h"
9ed99284 55#include "stringpool.h"
073c1fd5 56#include "tree-ssanames.h"
d53441c8 57#include "hashtab.h"
58#include "rtl.h"
59#include "statistics.h"
d53441c8 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"
9ed99284 68#include "expr.h"
073c1fd5 69#include "tree-dfa.h"
4ee9c684 70#include "tree-pass.h"
4ee9c684 71#include "langhooks.h"
e6d0e152 72#include "domwalk.h"
ec611e12 73#include "cfgloop.h"
74#include "tree-data-ref.h"
239e9670 75#include "gimple-pretty-print.h"
34517c64 76#include "insn-codes.h"
239e9670 77#include "optabs.h"
f6568ea4 78#include "tree-scalar-evolution.h"
b6814ca0 79#include "tree-inline.h"
239e9670 80
239e9670 81static unsigned int tree_ssa_phiopt_worker (bool, bool);
a4844041 82static bool conditional_replacement (basic_block, basic_block,
1a91d914 83 edge, edge, gphi *, tree, tree);
fb9912ea 84static int value_replacement (basic_block, basic_block,
85 edge, edge, gimple, tree, tree);
a4844041 86static bool minmax_replacement (basic_block, basic_block,
75a70cf9 87 edge, edge, gimple, tree, tree);
a4844041 88static bool abs_replacement (basic_block, basic_block,
75a70cf9 89 edge, edge, gimple, tree, tree);
e6d0e152 90static bool cond_store_replacement (basic_block, basic_block, edge, edge,
431205b7 91 hash_set<tree> *);
91cf53d5 92static bool cond_if_else_store_replacement (basic_block, basic_block, basic_block);
431205b7 93static hash_set<tree> * get_non_trapping ();
75a70cf9 94static void replace_phi_edge_with_variable (basic_block, edge, gimple, tree);
239e9670 95static void hoist_adjacent_loads (basic_block, basic_block,
96 basic_block, basic_block);
97static bool gate_hoist_loads (void);
902929aa 98
e6d0e152 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:
91cf53d5 106 *p = RHS;
e6d0e152 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'>
91cf53d5 117 *p = condtmp;
e6d0e152 118
119 This transformation can only be done under several constraints,
91cf53d5 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; */
e6d0e152 139
140static unsigned int
141tree_ssa_cs_elim (void)
142{
f6568ea4 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;
e6d0e152 153}
154
c3597b05 155/* Return the singleton PHI in the SEQ of PHIs for edges E0 and E1. */
156
1a91d914 157static gphi *
c3597b05 158single_non_singleton_phi_for_edges (gimple_seq seq, edge e0, edge e1)
159{
160 gimple_stmt_iterator i;
1a91d914 161 gphi *phi = NULL;
c3597b05 162 if (gimple_seq_singleton_p (seq))
1a91d914 163 return as_a <gphi *> (gsi_stmt (gsi_start (seq)));
c3597b05 164 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
165 {
1a91d914 166 gphi *p = as_a <gphi *> (gsi_stmt (i));
c3597b05 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
e6d0e152 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
239e9670 185 when we want to do conditional store replacement, false otherwise.
f32420fb 186 DO_HOIST_LOADS is true when we want to hoist adjacent loads out
239e9670 187 of diamond control flow patterns, false otherwise. */
e6d0e152 188static unsigned int
239e9670 189tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads)
4ee9c684 190{
191 basic_block bb;
194899bf 192 basic_block *bb_order;
193 unsigned n, i;
1e4b21e3 194 bool cfgchanged = false;
431205b7 195 hash_set<tree> *nontrap = 0;
e6d0e152 196
197 if (do_store_elim)
03d37e4e 198 /* Calculate the set of non-trapping memory accesses. */
199 nontrap = get_non_trapping ();
194899bf 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. */
ba4d2b2f 208 bb_order = single_pred_before_succ_order ();
a28770e1 209 n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
4ee9c684 210
48e1416a 211 for (i = 0; i < n; i++)
4ee9c684 212 {
1a91d914 213 gimple cond_stmt;
214 gphi *phi;
33784d89 215 basic_block bb1, bb2;
216 edge e1, e2;
194899bf 217 tree arg0, arg1;
218
219 bb = bb_order[i];
20e5647c 220
75a70cf9 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)
33784d89 225 continue;
20e5647c 226
33784d89 227 e1 = EDGE_SUCC (bb, 0);
228 bb1 = e1->dest;
229 e2 = EDGE_SUCC (bb, 1);
230 bb2 = e2->dest;
20e5647c 231
33784d89 232 /* We cannot do the optimization on abnormal edges. */
233 if ((e1->flags & EDGE_ABNORMAL) != 0
234 || (e2->flags & EDGE_ABNORMAL) != 0)
235 continue;
20e5647c 236
33784d89 237 /* If either bb1's succ or bb2 or bb2's succ is non NULL. */
ea091dfd 238 if (EDGE_COUNT (bb1->succs) == 0
33784d89 239 || bb2 == NULL
ea091dfd 240 || EDGE_COUNT (bb2->succs) == 0)
33784d89 241 continue;
20e5647c 242
33784d89 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 }
91cf53d5 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 }
239e9670 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 }
33784d89 289 else
f32420fb 290 continue;
20e5647c 291
33784d89 292 e1 = EDGE_SUCC (bb1, 0);
20e5647c 293
33784d89 294 /* Make sure that bb1 is just a fall through. */
db5ba14c 295 if (!single_succ_p (bb1)
33784d89 296 || (e1->flags & EDGE_FALLTHRU) == 0)
297 continue;
20e5647c 298
3472707f 299 /* Also make sure that bb1 only have one predecessor and that it
300 is bb. */
ea091dfd 301 if (!single_pred_p (bb1)
302 || single_pred (bb1) != bb)
33784d89 303 continue;
20e5647c 304
e6d0e152 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 {
75a70cf9 317 gimple_seq phis = phi_nodes (bb2);
2109076a 318 gimple_stmt_iterator gsi;
fb9912ea 319 bool candorest = true;
c3597b05 320
fb9912ea 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 {
1a91d914 325 phi = as_a <gphi *> (gsi_stmt (gsi));
fb9912ea 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 }
e6d0e152 335
fb9912ea 336 if (!candorest)
337 continue;
f32420fb 338
c3597b05 339 phi = single_non_singleton_phi_for_edges (phis, e1, e2);
2109076a 340 if (!phi)
e6d0e152 341 continue;
342
75a70cf9 343 arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
344 arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
e6d0e152 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;
e6d0e152 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 }
194899bf 358 }
359
360 free (bb_order);
48e1416a 361
e6d0e152 362 if (do_store_elim)
431205b7 363 delete nontrap;
e6d0e152 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). */
75a70cf9 369 gsi_commit_edge_inserts ();
e6d0e152 370 return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
371 }
372 else if (cfgchanged)
373 return TODO_cleanup_cfg;
374 return 0;
194899bf 375}
376
fccee353 377/* Replace PHI node element whose edge is E in block BB with variable NEW.
33784d89 378 Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
902929aa 379 is known to have two edges, one of which must reach BB). */
380
381static void
a4844041 382replace_phi_edge_with_variable (basic_block cond_block,
75a70cf9 383 edge e, gimple phi, tree new_tree)
902929aa 384{
75a70cf9 385 basic_block bb = gimple_bb (phi);
0e1a77e1 386 basic_block block_to_remove;
75a70cf9 387 gimple_stmt_iterator gsi;
33784d89 388
20e5647c 389 /* Change the PHI argument to new. */
f0d6e81c 390 SET_USE (PHI_ARG_DEF_PTR (phi, e->dest_idx), new_tree);
0e1a77e1 391
0e1a77e1 392 /* Remove the empty basic block. */
cd665a06 393 if (EDGE_SUCC (cond_block, 0)->dest == bb)
902929aa 394 {
cd665a06 395 EDGE_SUCC (cond_block, 0)->flags |= EDGE_FALLTHRU;
396 EDGE_SUCC (cond_block, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
81c5be57 397 EDGE_SUCC (cond_block, 0)->probability = REG_BR_PROB_BASE;
398 EDGE_SUCC (cond_block, 0)->count += EDGE_SUCC (cond_block, 1)->count;
0e1a77e1 399
cd665a06 400 block_to_remove = EDGE_SUCC (cond_block, 1)->dest;
902929aa 401 }
402 else
403 {
cd665a06 404 EDGE_SUCC (cond_block, 1)->flags |= EDGE_FALLTHRU;
405 EDGE_SUCC (cond_block, 1)->flags
902929aa 406 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
81c5be57 407 EDGE_SUCC (cond_block, 1)->probability = REG_BR_PROB_BASE;
408 EDGE_SUCC (cond_block, 1)->count += EDGE_SUCC (cond_block, 0)->count;
0e1a77e1 409
cd665a06 410 block_to_remove = EDGE_SUCC (cond_block, 0)->dest;
902929aa 411 }
0e1a77e1 412 delete_basic_block (block_to_remove);
20e5647c 413
902929aa 414 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
75a70cf9 415 gsi = gsi_last_bb (cond_block);
416 gsi_remove (&gsi, true);
20e5647c 417
902929aa 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
dac49aa5 429 is argument 0 from PHI. Likewise for ARG1. */
902929aa 430
431static bool
33784d89 432conditional_replacement (basic_block cond_bb, basic_block middle_bb,
1a91d914 433 edge e0, edge e1, gphi *phi,
33784d89 434 tree arg0, tree arg1)
902929aa 435{
436 tree result;
1a91d914 437 gimple stmt;
438 gassign *new_stmt;
75a70cf9 439 tree cond;
440 gimple_stmt_iterator gsi;
902929aa 441 edge true_edge, false_edge;
75a70cf9 442 tree new_var, new_var2;
678919fd 443 bool neg;
902929aa 444
435e1a75 445 /* FIXME: Gimplification of complex type is too hard for now. */
47b88316 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))))
435e1a75 452 return false;
453
678919fd 454 /* The PHI arguments have the constants 0 and 1, or 0 and -1, then
455 convert it to the conditional. */
902929aa 456 if ((integer_zerop (arg0) && integer_onep (arg1))
457 || (integer_zerop (arg1) && integer_onep (arg0)))
678919fd 458 neg = false;
459 else if ((integer_zerop (arg0) && integer_all_onesp (arg1))
460 || (integer_zerop (arg1) && integer_all_onesp (arg0)))
461 neg = true;
902929aa 462 else
463 return false;
20e5647c 464
33784d89 465 if (!empty_block_p (middle_bb))
902929aa 466 return false;
20e5647c 467
75a70cf9 468 /* At this point we know we have a GIMPLE_COND with two successors.
2ab0a163 469 One successor is BB, the other successor is an empty block which
470 falls through into BB.
20e5647c 471
2ab0a163 472 There is a single PHI node at the join point (BB) and its arguments
678919fd 473 are constants (0, 1) or (0, -1).
20e5647c 474
2ab0a163 475 So, given the condition COND, and the two PHI arguments, we can
20e5647c 476 rewrite this PHI into non-branching code:
477
2ab0a163 478 dest = (COND) or dest = COND'
20e5647c 479
2ab0a163 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
75a70cf9 483 the same since only one of the outgoing edges from the GIMPLE_COND
2ab0a163 484 will directly reach BB and thus be associated with an argument. */
ae5a4794 485
75a70cf9 486 stmt = last_stmt (cond_bb);
487 result = PHI_RESULT (phi);
b2a02a0e 488
75a70cf9 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. */
6f9714b3 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));
4ee9c684 495
75a70cf9 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))
678919fd 500 || (e0 == false_edge && !integer_zerop (arg0))
75a70cf9 501 || (e1 == true_edge && integer_zerop (arg1))
678919fd 502 || (e1 == false_edge && !integer_zerop (arg1)))
6f9714b3 503 cond = fold_build1_loc (gimple_location (stmt),
678919fd 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 }
75a70cf9 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 {
efbcb6de 522 source_location locus_0, locus_1;
523
f9e245b2 524 new_var2 = make_ssa_name (TREE_TYPE (result));
e9cf809e 525 new_stmt = gimple_build_assign (new_var2, CONVERT_EXPR, new_var);
75a70cf9 526 gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
527 new_var = new_var2;
efbcb6de 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);
4ee9c684 535 }
20e5647c 536
75a70cf9 537 replace_phi_edge_with_variable (cond_bb, e1, phi, new_var);
902929aa 538
4ee9c684 539 /* Note that we optimized this PHI. */
540 return true;
541}
542
17b9476e 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
547static bool
548jump_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
796b6678 560 && (mem_ref_offset (tem) + offset) == 0)
17b9476e 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
f32420fb 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
581static bool
582rhs_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
619static bool
620operand_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
b6814ca0 660/* Returns true if ARG is a neutral element for operation CODE
661 on the RIGHT side. */
662
663static bool
664neutral_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
701static bool
702absorbing_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
0beac6fc 718/* The function value_replacement does the main work of doing the value
fb9912ea 719 replacement. Return non-zero if the replacement is done. Otherwise return
720 0. If we remove the middle basic block, return 2.
0beac6fc 721 BB is the basic block where the replacement is going to be done on. ARG0
dac49aa5 722 is argument 0 from the PHI. Likewise for ARG1. */
0beac6fc 723
fb9912ea 724static int
33784d89 725value_replacement (basic_block cond_bb, basic_block middle_bb,
75a70cf9 726 edge e0, edge e1, gimple phi,
33784d89 727 tree arg0, tree arg1)
0beac6fc 728{
17b9476e 729 gimple_stmt_iterator gsi;
75a70cf9 730 gimple cond;
0beac6fc 731 edge true_edge, false_edge;
75a70cf9 732 enum tree_code code;
fb9912ea 733 bool emtpy_or_with_defined_p = true;
0beac6fc 734
735 /* If the type says honor signed zeros we cannot do this
dac49aa5 736 optimization. */
fe994837 737 if (HONOR_SIGNED_ZEROS (arg1))
fb9912ea 738 return 0;
0beac6fc 739
fb9912ea 740 /* If there is a statement in MIDDLE_BB that defines one of the PHI
741 arguments, then adjust arg0 or arg1. */
b6814ca0 742 gsi = gsi_start_nondebug_after_labels_bb (middle_bb);
fb9912ea 743 while (!gsi_end_p (gsi))
17b9476e 744 {
fb9912ea 745 gimple stmt = gsi_stmt (gsi);
746 tree lhs;
747 gsi_next_nondebug (&gsi);
748 if (!is_gimple_assign (stmt))
17b9476e 749 {
fb9912ea 750 emtpy_or_with_defined_p = false;
751 continue;
17b9476e 752 }
fb9912ea 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;
17b9476e 761 }
0beac6fc 762
75a70cf9 763 cond = last_stmt (cond_bb);
764 code = gimple_cond_code (cond);
0beac6fc 765
766 /* This transformation is only valid for equality comparisons. */
75a70cf9 767 if (code != NE_EXPR && code != EQ_EXPR)
fb9912ea 768 return 0;
0beac6fc 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. */
33784d89 772 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
0beac6fc 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. */
20e5647c 784
f32420fb 785 if (operand_equal_for_value_replacement (arg0, arg1, &code, cond))
0beac6fc 786 {
787 edge e;
788 tree arg;
789
50737d20 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. */
75a70cf9 793 e = (code == NE_EXPR ? true_edge : false_edge);
50737d20 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. */
33784d89 799 if (e->dest == middle_bb)
ea091dfd 800 e = single_succ_edge (e->dest);
50737d20 801
802 /* Now we know the incoming edge to BB that has the argument for the
803 RHS of our new assignment statement. */
33784d89 804 if (e0 == e)
0beac6fc 805 arg = arg0;
806 else
807 arg = arg1;
808
fb9912ea 809 /* If the middle basic block was empty or is defining the
c3597b05 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. */
fb9912ea 812 if (emtpy_or_with_defined_p
c3597b05 813 && single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)),
ce75c7c2 814 e0, e1) == phi)
fb9912ea 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);
c3597b05 829 fprintf (dump_file, " reduced for COND_EXPR in block %d to ",
830 cond_bb->index);
fb9912ea 831 print_generic_expr (dump_file, arg, 0);
832 fprintf (dump_file, ".\n");
833 }
834 return 1;
835 }
0beac6fc 836
0beac6fc 837 }
b6814ca0 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
6c96fe34 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
43fbec4a 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
b6814ca0 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);
18c06fb8 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 }
b6814ca0 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
fb9912ea 920 return 0;
0beac6fc 921}
922
194899bf 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
929static bool
930minmax_replacement (basic_block cond_bb, basic_block middle_bb,
75a70cf9 931 edge e0, edge e1, gimple phi,
194899bf 932 tree arg0, tree arg1)
933{
934 tree result, type;
1a91d914 935 gcond *cond;
936 gassign *new_stmt;
194899bf 937 edge true_edge, false_edge;
938 enum tree_code cmp, minmax, ass_code;
939 tree smaller, larger, arg_true, arg_false;
75a70cf9 940 gimple_stmt_iterator gsi, gsi_from;
194899bf 941
942 type = TREE_TYPE (PHI_RESULT (phi));
943
944 /* The optimization may be unsafe due to NaNs. */
93633022 945 if (HONOR_NANS (type))
194899bf 946 return false;
947
1a91d914 948 cond = as_a <gcond *> (last_stmt (cond_bb));
75a70cf9 949 cmp = gimple_cond_code (cond);
194899bf 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 {
75a70cf9 955 smaller = gimple_cond_lhs (cond);
956 larger = gimple_cond_rhs (cond);
194899bf 957 }
958 else if (cmp == GT_EXPR || cmp == GE_EXPR)
959 {
75a70cf9 960 smaller = gimple_cond_rhs (cond);
961 larger = gimple_cond_lhs (cond);
194899bf 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
48e1416a 996
194899bf 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
75a70cf9 1022 gimple assign = last_and_only_stmt (middle_bb);
1023 tree lhs, op0, op1, bound;
194899bf 1024
1025 if (!assign
75a70cf9 1026 || gimple_code (assign) != GIMPLE_ASSIGN)
194899bf 1027 return false;
1028
75a70cf9 1029 lhs = gimple_assign_lhs (assign);
1030 ass_code = gimple_assign_rhs_code (assign);
194899bf 1031 if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
1032 return false;
75a70cf9 1033 op0 = gimple_assign_rhs1 (assign);
1034 op1 = gimple_assign_rhs2 (assign);
194899bf 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. */
49d00087 1063 if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
1064 bound, larger)))
194899bf 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. */
49d00087 1088 if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
1089 bound, smaller)))
194899bf 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. */
49d00087 1122 if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
1123 bound, larger)))
194899bf 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. */
49d00087 1147 if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
1148 bound, smaller)))
194899bf 1149 return false;
1150 }
1151 else
1152 return false;
1153 }
1154
1155 /* Move the statement from the middle block. */
75a70cf9 1156 gsi = gsi_last_bb (cond_bb);
445a6ba5 1157 gsi_from = gsi_last_nondebug_bb (middle_bb);
75a70cf9 1158 gsi_move_before (&gsi_from, &gsi);
194899bf 1159 }
1160
1161 /* Emit the statement to compute min/max. */
1162 result = duplicate_ssa_name (PHI_RESULT (phi), NULL);
e9cf809e 1163 new_stmt = gimple_build_assign (result, minmax, arg0, arg1);
75a70cf9 1164 gsi = gsi_last_bb (cond_bb);
1165 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
194899bf 1166
a4844041 1167 replace_phi_edge_with_variable (cond_bb, e1, phi, result);
194899bf 1168 return true;
1169}
1170
70512b93 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
f7f07c95 1175 is argument 0 from the phi. Likewise for arg1. */
33784d89 1176
70512b93 1177static bool
33784d89 1178abs_replacement (basic_block cond_bb, basic_block middle_bb,
a4844041 1179 edge e0 ATTRIBUTE_UNUSED, edge e1,
75a70cf9 1180 gimple phi, tree arg0, tree arg1)
70512b93 1181{
1182 tree result;
1a91d914 1183 gassign *new_stmt;
1184 gimple cond;
75a70cf9 1185 gimple_stmt_iterator gsi;
70512b93 1186 edge true_edge, false_edge;
75a70cf9 1187 gimple assign;
70512b93 1188 edge e;
194899bf 1189 tree rhs, lhs;
70512b93 1190 bool negate;
1191 enum tree_code cond_code;
1192
1193 /* If the type says honor signed zeros we cannot do this
dac49aa5 1194 optimization. */
fe994837 1195 if (HONOR_SIGNED_ZEROS (arg1))
70512b93 1196 return false;
1197
70512b93 1198 /* OTHER_BLOCK must have only one executable statement which must have the
1199 form arg0 = -arg1 or arg1 = -arg0. */
70512b93 1200
194899bf 1201 assign = last_and_only_stmt (middle_bb);
70512b93 1202 /* If we did not find the proper negation assignment, then we can not
1203 optimize. */
1204 if (assign == NULL)
1205 return false;
48e1416a 1206
194899bf 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. */
75a70cf9 1210 if (gimple_code (assign) != GIMPLE_ASSIGN)
194899bf 1211 return false;
1212
75a70cf9 1213 lhs = gimple_assign_lhs (assign);
194899bf 1214
75a70cf9 1215 if (gimple_assign_rhs_code (assign) != NEGATE_EXPR)
194899bf 1216 return false;
1217
75a70cf9 1218 rhs = gimple_assign_rhs1 (assign);
48e1416a 1219
194899bf 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;
70512b93 1224
75a70cf9 1225 cond = last_stmt (cond_bb);
70512b93 1226 result = PHI_RESULT (phi);
1227
1228 /* Only relationals comparing arg[01] against zero are interesting. */
75a70cf9 1229 cond_code = gimple_cond_code (cond);
70512b93 1230 if (cond_code != GT_EXPR && cond_code != GE_EXPR
1231 && cond_code != LT_EXPR && cond_code != LE_EXPR)
1232 return false;
1233
dac49aa5 1234 /* Make sure the conditional is arg[01] OP y. */
75a70cf9 1235 if (gimple_cond_lhs (cond) != rhs)
70512b93 1236 return false;
1237
75a70cf9 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)))
70512b93 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. */
33784d89 1247 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
70512b93 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;
20e5647c 1256
33784d89 1257 if (e->dest == middle_bb)
70512b93 1258 negate = true;
1259 else
1260 negate = false;
20e5647c 1261
33784d89 1262 result = duplicate_ssa_name (result, NULL);
20e5647c 1263
70512b93 1264 if (negate)
f9e245b2 1265 lhs = make_ssa_name (TREE_TYPE (result));
70512b93 1266 else
1267 lhs = result;
1268
dac49aa5 1269 /* Build the modify expression with abs expression. */
e9cf809e 1270 new_stmt = gimple_build_assign (lhs, ABS_EXPR, rhs);
70512b93 1271
75a70cf9 1272 gsi = gsi_last_bb (cond_bb);
1273 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
70512b93 1274
1275 if (negate)
1276 {
75a70cf9 1277 /* Get the right GSI. We want to insert after the recently
70512b93 1278 added ABS_EXPR statement (which we know is the first statement
1279 in the block. */
e9cf809e 1280 new_stmt = gimple_build_assign (result, NEGATE_EXPR, lhs);
70512b93 1281
75a70cf9 1282 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
70512b93 1283 }
20e5647c 1284
a4844041 1285 replace_phi_edge_with_variable (cond_bb, e1, phi, result);
70512b93 1286
1287 /* Note that we optimized this PHI. */
1288 return true;
1289}
1290
e6d0e152 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
182cf5a9 1293 portion. We do that for MEM_REFs, so we only need to track
e6d0e152 1294 the SSA_NAME of the pointer indirectly referenced. The algorithm
1295 simply is a walk over all instructions in dominator order. When
182cf5a9 1296 we see an MEM_REF we determine if we've already seen a same
e6d0e152 1297 ref anywhere up to the root of the dominator tree. If we do the
af4f74fa 1298 current access can't trap. If we don't see any dominating access
e6d0e152 1299 the current access might trap, but might also make later accesses
af4f74fa 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. */
e6d0e152 1309
182cf5a9 1310/* A hash-table of SSA_NAMEs, and in which basic block an MEM_REF
e6d0e152 1311 through it was seen, which would constitute a no-trap region for
1312 same accesses. */
1313struct name_to_bb
1314{
963aee26 1315 unsigned int ssa_name_ver;
42540642 1316 unsigned int phase;
963aee26 1317 bool store;
1318 HOST_WIDE_INT offset, size;
e6d0e152 1319 basic_block bb;
1320};
1321
d9dd21a8 1322/* Hashtable helpers. */
1323
1324struct ssa_names_hasher : typed_free_remove <name_to_bb>
1325{
9969c043 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 *);
d9dd21a8 1330};
e6d0e152 1331
42540642 1332/* Used for quick clearing of the hash-table when we see calls.
1333 Hash entries with phase < nt_call_phase are invalid. */
1334static unsigned int nt_call_phase;
1335
963aee26 1336/* The hash function. */
d9dd21a8 1337
1338inline hashval_t
9969c043 1339ssa_names_hasher::hash (const name_to_bb *n)
e6d0e152 1340{
963aee26 1341 return n->ssa_name_ver ^ (((hashval_t) n->store) << 31)
1342 ^ (n->offset << 6) ^ (n->size << 3);
e6d0e152 1343}
1344
963aee26 1345/* The equality function of *P1 and *P2. */
e6d0e152 1346
d9dd21a8 1347inline bool
9969c043 1348ssa_names_hasher::equal (const name_to_bb *n1, const name_to_bb *n2)
d9dd21a8 1349{
963aee26 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;
e6d0e152 1354}
1355
c1f445d2 1356class nontrapping_dom_walker : public dom_walker
1357{
1358public:
431205b7 1359 nontrapping_dom_walker (cdi_direction direction, hash_set<tree> *ps)
c1f445d2 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
1365private:
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
431205b7 1374 hash_set<tree> *m_nontrapping;
c1f445d2 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. */
1381void
1382nontrapping_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. */
1415void
1416nontrapping_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}
d9dd21a8 1421
f0b5f617 1422/* We see the expression EXP in basic block BB. If it's an interesting
182cf5a9 1423 expression (an MEM_REF through an SSA_NAME) possibly insert the
af4f74fa 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. */
c1f445d2 1427void
1428nontrapping_dom_walker::add_or_mark_expr (basic_block bb, tree exp, bool store)
e6d0e152 1429{
963aee26 1430 HOST_WIDE_INT size;
1431
182cf5a9 1432 if (TREE_CODE (exp) == MEM_REF
963aee26 1433 && TREE_CODE (TREE_OPERAND (exp, 0)) == SSA_NAME
e913b5cd 1434 && tree_fits_shwi_p (TREE_OPERAND (exp, 1))
963aee26 1435 && (size = int_size_in_bytes (TREE_TYPE (exp))) > 0)
e6d0e152 1436 {
1437 tree name = TREE_OPERAND (exp, 0);
1438 struct name_to_bb map;
d9dd21a8 1439 name_to_bb **slot;
af4f74fa 1440 struct name_to_bb *n2bb;
e6d0e152 1441 basic_block found_bb = 0;
1442
182cf5a9 1443 /* Try to find the last seen MEM_REF through the same
e6d0e152 1444 SSA_NAME, which can trap. */
963aee26 1445 map.ssa_name_ver = SSA_NAME_VERSION (name);
42540642 1446 map.phase = 0;
e6d0e152 1447 map.bb = 0;
af4f74fa 1448 map.store = store;
e913b5cd 1449 map.offset = tree_to_shwi (TREE_OPERAND (exp, 1));
963aee26 1450 map.size = size;
1451
c1f445d2 1452 slot = m_seen_ssa_names.find_slot (&map, INSERT);
d9dd21a8 1453 n2bb = *slot;
42540642 1454 if (n2bb && n2bb->phase >= nt_call_phase)
af4f74fa 1455 found_bb = n2bb->bb;
e6d0e152 1456
182cf5a9 1457 /* If we've found a trapping MEM_REF, _and_ it dominates EXP
e6d0e152 1458 (it's in a basic block on the path from us to the dominator root)
1459 then we can't trap. */
42540642 1460 if (found_bb && (((size_t)found_bb->aux) & 1) == 1)
e6d0e152 1461 {
431205b7 1462 m_nontrapping->add (exp);
e6d0e152 1463 }
1464 else
1465 {
1466 /* EXP might trap, so insert it into the hash table. */
af4f74fa 1467 if (n2bb)
e6d0e152 1468 {
42540642 1469 n2bb->phase = nt_call_phase;
af4f74fa 1470 n2bb->bb = bb;
e6d0e152 1471 }
1472 else
1473 {
af4f74fa 1474 n2bb = XNEW (struct name_to_bb);
963aee26 1475 n2bb->ssa_name_ver = SSA_NAME_VERSION (name);
42540642 1476 n2bb->phase = nt_call_phase;
af4f74fa 1477 n2bb->bb = bb;
1478 n2bb->store = store;
963aee26 1479 n2bb->offset = map.offset;
1480 n2bb->size = size;
af4f74fa 1481 *slot = n2bb;
e6d0e152 1482 }
1483 }
1484 }
1485}
1486
e6d0e152 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
182cf5a9 1490 (the MEM_REFs itself) which can't trap. */
431205b7 1491static hash_set<tree> *
e6d0e152 1492get_non_trapping (void)
1493{
42540642 1494 nt_call_phase = 0;
431205b7 1495 hash_set<tree> *nontrap = new hash_set<tree>;
e6d0e152 1496 /* We're going to do a dominator walk, so ensure that we have
1497 dominance information. */
1498 calculate_dominance_info (CDI_DOMINATORS);
1499
54c91640 1500 nontrapping_dom_walker (CDI_DOMINATORS, nontrap)
1501 .walk (cfun->cfg->x_entry_block_ptr);
1502
42540642 1503 clear_aux_for_blocks ();
e6d0e152 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
1522static bool
1523cond_store_replacement (basic_block middle_bb, basic_block join_bb,
431205b7 1524 edge e0, edge e1, hash_set<tree> *nontrap)
e6d0e152 1525{
75a70cf9 1526 gimple assign = last_and_only_stmt (middle_bb);
03d37e4e 1527 tree lhs, rhs, name, name2;
1a91d914 1528 gphi *newphi;
1529 gassign *new_stmt;
75a70cf9 1530 gimple_stmt_iterator gsi;
efbcb6de 1531 source_location locus;
e6d0e152 1532
1533 /* Check if middle_bb contains of only one store. */
1534 if (!assign
6cc085b6 1535 || !gimple_assign_single_p (assign)
1536 || gimple_has_volatile_ops (assign))
e6d0e152 1537 return false;
1538
efbcb6de 1539 locus = gimple_location (assign);
75a70cf9 1540 lhs = gimple_assign_lhs (assign);
1541 rhs = gimple_assign_rhs1 (assign);
182cf5a9 1542 if (TREE_CODE (lhs) != MEM_REF
91cf53d5 1543 || TREE_CODE (TREE_OPERAND (lhs, 0)) != SSA_NAME
3211fa0a 1544 || !is_gimple_reg_type (TREE_TYPE (lhs)))
e6d0e152 1545 return false;
91cf53d5 1546
e6d0e152 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. */
431205b7 1550 if (!nontrap->contains (lhs))
e6d0e152 1551 return false;
1552
1553 /* Now we've checked the constraints, so do the transformation:
1554 1) Remove the single store. */
75a70cf9 1555 gsi = gsi_for_stmt (assign);
3211fa0a 1556 unlink_stmt_vdef (assign);
75a70cf9 1557 gsi_remove (&gsi, true);
91cf53d5 1558 release_defs (assign);
e6d0e152 1559
03d37e4e 1560 /* 2) Insert a load from the memory of the store to the temporary
e6d0e152 1561 on the edge which did not contain the store. */
1562 lhs = unshare_expr (lhs);
03d37e4e 1563 name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
1564 new_stmt = gimple_build_assign (name, lhs);
efbcb6de 1565 gimple_set_location (new_stmt, locus);
75a70cf9 1566 gsi_insert_on_edge (e1, new_stmt);
e6d0e152 1567
03d37e4e 1568 /* 3) Create a PHI node at the join block, with one argument
e6d0e152 1569 holding the old RHS, and the other holding the temporary
1570 where we stored the old memory contents. */
03d37e4e 1571 name2 = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
1572 newphi = create_phi_node (name2, join_bb);
60d535d2 1573 add_phi_arg (newphi, rhs, e0, locus);
1574 add_phi_arg (newphi, name, e1, locus);
e6d0e152 1575
1576 lhs = unshare_expr (lhs);
75a70cf9 1577 new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
e6d0e152 1578
03d37e4e 1579 /* 4) Insert that PHI node. */
75a70cf9 1580 gsi = gsi_after_labels (join_bb);
1581 if (gsi_end_p (gsi))
e6d0e152 1582 {
75a70cf9 1583 gsi = gsi_last_bb (join_bb);
1584 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
e6d0e152 1585 }
1586 else
75a70cf9 1587 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
e6d0e152 1588
1589 return true;
1590}
4ee9c684 1591
ec611e12 1592/* Do the main work of conditional store replacement. */
91cf53d5 1593
1594static bool
ec611e12 1595cond_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)
91cf53d5 1598{
03d37e4e 1599 tree lhs_base, lhs, then_rhs, else_rhs, name;
91cf53d5 1600 source_location then_locus, else_locus;
1601 gimple_stmt_iterator gsi;
1a91d914 1602 gphi *newphi;
1603 gassign *new_stmt;
91cf53d5 1604
91cf53d5 1605 if (then_assign == NULL
1606 || !gimple_assign_single_p (then_assign)
3c25489e 1607 || gimple_clobber_p (then_assign)
6cc085b6 1608 || gimple_has_volatile_ops (then_assign)
91cf53d5 1609 || else_assign == NULL
3c25489e 1610 || !gimple_assign_single_p (else_assign)
6cc085b6 1611 || gimple_clobber_p (else_assign)
1612 || gimple_has_volatile_ops (else_assign))
91cf53d5 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
03d37e4e 1642 /* 2) Create a PHI node at the join block, with one argument
91cf53d5 1643 holding the old RHS, and the other holding the temporary
1644 where we stored the old memory contents. */
03d37e4e 1645 name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
1646 newphi = create_phi_node (name, join_bb);
60d535d2 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);
91cf53d5 1649
1650 new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
1651
03d37e4e 1652 /* 3) Insert that PHI node. */
91cf53d5 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
ec611e12 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
1687static bool
1688cond_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);
f1f41a6c 1693 vec<data_reference_p> then_datarefs, else_datarefs;
1694 vec<ddr_p> then_ddrs, else_ddrs;
ec611e12 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;
ec611e12 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. */
f1f41a6c 1712 then_datarefs.create (1);
1713 else_datarefs.create (1);
ec611e12 1714 if ((find_data_references_in_bb (NULL, then_bb, &then_datarefs)
1715 == chrec_dont_know)
f1f41a6c 1716 || !then_datarefs.length ()
ec611e12 1717 || (find_data_references_in_bb (NULL, else_bb, &else_datarefs)
c71d3c24 1718 == chrec_dont_know)
f1f41a6c 1719 || !else_datarefs.length ())
ec611e12 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. */
4997014d 1727 auto_vec<gimple, 1> then_stores, else_stores;
f1f41a6c 1728 FOR_EACH_VEC_ELT (then_datarefs, i, then_dr)
ec611e12 1729 {
1730 if (DR_IS_READ (then_dr))
1731 continue;
1732
1733 then_store = DR_STMT (then_dr);
728dcc71 1734 then_lhs = gimple_get_lhs (then_store);
c71d3c24 1735 if (then_lhs == NULL_TREE)
1736 continue;
ec611e12 1737 found = false;
1738
f1f41a6c 1739 FOR_EACH_VEC_ELT (else_datarefs, j, else_dr)
ec611e12 1740 {
1741 if (DR_IS_READ (else_dr))
1742 continue;
1743
1744 else_store = DR_STMT (else_dr);
728dcc71 1745 else_lhs = gimple_get_lhs (else_store);
c71d3c24 1746 if (else_lhs == NULL_TREE)
1747 continue;
ec611e12 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
f1f41a6c 1759 then_stores.safe_push (then_store);
1760 else_stores.safe_push (else_store);
ec611e12 1761 }
1762
1763 /* No pairs of stores found. */
f1f41a6c 1764 if (!then_stores.length ()
1765 || then_stores.length () > (unsigned) MAX_STORES_TO_SINK)
ec611e12 1766 {
1767 free_data_refs (then_datarefs);
1768 free_data_refs (else_datarefs);
ec611e12 1769 return false;
1770 }
1771
1772 /* Compute and check data dependencies in both basic blocks. */
f1f41a6c 1773 then_ddrs.create (1);
1774 else_ddrs.create (1);
1775 if (!compute_all_dependences (then_datarefs, &then_ddrs,
1e094109 1776 vNULL, false)
f1f41a6c 1777 || !compute_all_dependences (else_datarefs, &else_ddrs,
1e094109 1778 vNULL, false))
8b3fb720 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);
8b3fb720 1784 return false;
1785 }
ec611e12 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. */
f1f41a6c 1793 FOR_EACH_VEC_ELT (then_ddrs, i, ddr)
ec611e12 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);
2473bfb7 1807 free_data_refs (then_datarefs);
1808 free_data_refs (else_datarefs);
ec611e12 1809 return false;
1810 }
1811 }
1812
1813 /* Check that there are no read-after-write or write-after-write dependencies
1814 in ELSE_BB. */
f1f41a6c 1815 FOR_EACH_VEC_ELT (else_ddrs, i, ddr)
ec611e12 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);
2473bfb7 1829 free_data_refs (then_datarefs);
1830 free_data_refs (else_datarefs);
ec611e12 1831 return false;
1832 }
1833 }
1834
1835 /* Sink stores with same LHS. */
f1f41a6c 1836 FOR_EACH_VEC_ELT (then_stores, i, then_store)
ec611e12 1837 {
f1f41a6c 1838 else_store = else_stores[i];
ec611e12 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);
2473bfb7 1846 free_data_refs (then_datarefs);
1847 free_data_refs (else_datarefs);
ec611e12 1848
1849 return ok;
1850}
1851
239e9670 1852/* Return TRUE if STMT has a VUSE whose corresponding VDEF is in BB. */
1853
1854static bool
1855local_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,
f32420fb 1869 and BB3 rejoins control flow following BB1 and BB2, look for
239e9670 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
1891static void
1892hoist_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);
1a91d914 1897 gphi_iterator gsi;
239e9670 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 {
1a91d914 1904 gphi *phi_stmt = gsi.phi ();
dfcf26a5 1905 gimple def1, def2;
1906 tree arg1, arg2, ref1, ref2, field1, field2;
239e9670 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
7c782c9b 1913 if (gimple_phi_num_args (phi_stmt) != 2
1914 || virtual_operand_p (gimple_phi_result (phi_stmt)))
239e9670 1915 continue;
1916
1917 arg1 = gimple_phi_arg_def (phi_stmt, 0);
1918 arg2 = gimple_phi_arg_def (phi_stmt, 1);
f32420fb 1919
239e9670 1920 if (TREE_CODE (arg1) != SSA_NAME
1921 || TREE_CODE (arg2) != SSA_NAME
1922 || SSA_NAME_IS_DEFAULT_DEF (arg1)
7c782c9b 1923 || SSA_NAME_IS_DEFAULT_DEF (arg2))
239e9670 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. */
935611bc 1935 if (optab_handler (movcc_optab, TYPE_MODE (TREE_TYPE (arg1)))
1936 == CODE_FOR_nothing)
239e9670 1937 continue;
1938
1939 /* Both statements must be assignments whose RHS is a COMPONENT_REF. */
1940 if (!gimple_assign_single_p (def1)
6cc085b6 1941 || !gimple_assign_single_p (def2)
1942 || gimple_has_volatile_ops (def1)
1943 || gimple_has_volatile_ops (def2))
239e9670 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
dfcf26a5 1981 std::swap (field1, field2);
1982 std::swap (def1, def2);
239e9670 1983 }
1984
7c74ee50 1985 bb_for_def1 = gimple_bb (def1);
1986 bb_for_def2 = gimple_bb (def2);
1987
239e9670 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
e913b5cd 1993 if (!tree_fits_uhwi_p (tree_offset1)
1994 || !tree_fits_uhwi_p (tree_offset2)
1995 || !tree_fits_uhwi_p (tree_size2))
239e9670 1996 continue;
1997
e913b5cd 1998 offset1 = tree_to_uhwi (tree_offset1);
1999 offset2 = tree_to_uhwi (tree_offset2);
2000 size2 = tree_to_uhwi (tree_size2);
239e9670 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
6f0ddab1 2039 both a conditional move instruction and a defined cache line size. */
239e9670 2040
2041static bool
2042gate_hoist_loads (void)
2043{
6f0ddab1 2044 return (flag_hoist_adjacent_loads == 1
2045 && PARAM_VALUE (PARAM_L1_CACHE_LINE_SIZE)
2046 && HAVE_conditional_move);
239e9670 2047}
2048
65b0537f 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. */
20e5647c 2198
cbe8bda8 2199namespace {
2200
2201const pass_data pass_data_phiopt =
4ee9c684 2202{
cbe8bda8 2203 GIMPLE_PASS, /* type */
2204 "phiopt", /* name */
2205 OPTGROUP_NONE, /* optinfo_flags */
cbe8bda8 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 */
8b88439e 2211 0, /* todo_flags_finish */
4ee9c684 2212};
e6d0e152 2213
cbe8bda8 2214class pass_phiopt : public gimple_opt_pass
2215{
2216public:
9af5ce0c 2217 pass_phiopt (gcc::context *ctxt)
2218 : gimple_opt_pass (pass_data_phiopt, ctxt)
cbe8bda8 2219 {}
2220
2221 /* opt_pass methods: */
ae84f584 2222 opt_pass * clone () { return new pass_phiopt (m_ctxt); }
3dac50cc 2223 virtual bool gate (function *) { return flag_ssa_phiopt; }
65b0537f 2224 virtual unsigned int execute (function *)
2225 {
2226 return tree_ssa_phiopt_worker (false, gate_hoist_loads ());
2227 }
cbe8bda8 2228
2229}; // class pass_phiopt
2230
2231} // anon namespace
2232
2233gimple_opt_pass *
2234make_pass_phiopt (gcc::context *ctxt)
2235{
2236 return new pass_phiopt (ctxt);
2237}
2238
cbe8bda8 2239namespace {
2240
2241const pass_data pass_data_cselim =
e6d0e152 2242{
cbe8bda8 2243 GIMPLE_PASS, /* type */
2244 "cselim", /* name */
2245 OPTGROUP_NONE, /* optinfo_flags */
cbe8bda8 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 */
8b88439e 2251 0, /* todo_flags_finish */
e6d0e152 2252};
cbe8bda8 2253
2254class pass_cselim : public gimple_opt_pass
2255{
2256public:
9af5ce0c 2257 pass_cselim (gcc::context *ctxt)
2258 : gimple_opt_pass (pass_data_cselim, ctxt)
cbe8bda8 2259 {}
2260
2261 /* opt_pass methods: */
31315c24 2262 virtual bool gate (function *) { return flag_tree_cselim; }
65b0537f 2263 virtual unsigned int execute (function *) { return tree_ssa_cs_elim (); }
cbe8bda8 2264
2265}; // class pass_cselim
2266
2267} // anon namespace
2268
2269gimple_opt_pass *
2270make_pass_cselim (gcc::context *ctxt)
2271{
2272 return new pass_cselim (ctxt);
2273}