Line data Source code
1 : /* Optimization of PHI nodes by converting them into straightline code.
2 : Copyright (C) 2004-2026 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 "backend.h"
24 : #include "insn-codes.h"
25 : #include "rtl.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "cfghooks.h"
29 : #include "tree-pass.h"
30 : #include "ssa.h"
31 : #include "tree-ssa.h"
32 : #include "optabs-tree.h"
33 : #include "insn-config.h"
34 : #include "gimple-pretty-print.h"
35 : #include "fold-const.h"
36 : #include "stor-layout.h"
37 : #include "cfganal.h"
38 : #include "gimple-iterator.h"
39 : #include "tree-cfg.h"
40 : #include "tree-dfa.h"
41 : #include "domwalk.h"
42 : #include "cfgloop.h"
43 : #include "tree-data-ref.h"
44 : #include "tree-scalar-evolution.h"
45 : #include "tree-inline.h"
46 : #include "case-cfn-macros.h"
47 : #include "tree-eh.h"
48 : #include "gimple-fold.h"
49 : #include "internal-fn.h"
50 : #include "gimple-range.h"
51 : #include "gimple-match.h"
52 : #include "dbgcnt.h"
53 : #include "tree-ssa-propagate.h"
54 : #include "tree-ssa-dce.h"
55 : #include "tree-ssa-loop-niter.h"
56 : #include "gimple-predict.h"
57 : #include "alias.h"
58 : #include "tree-ssa-threadedge.h"
59 : #include "gimple-walk.h"
60 :
61 : /* Return the singleton PHI in the SEQ of PHIs for edges E0 and E1. */
62 :
63 : static gphi *
64 3553054 : single_non_singleton_phi_for_edges (gimple_seq seq, edge e0, edge e1)
65 : {
66 3553054 : gimple_stmt_iterator i;
67 3553054 : gphi *phi = NULL;
68 5286517 : for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
69 : {
70 4312513 : gphi *p = as_a <gphi *> (gsi_stmt (i));
71 : /* If the PHI arguments are equal then we can skip this PHI. */
72 4312513 : if (operand_equal_for_phi_arg_p (gimple_phi_arg_def (p, e0->dest_idx),
73 4312513 : gimple_phi_arg_def (p, e1->dest_idx)))
74 255263 : continue;
75 :
76 : /* Punt on virtual phis with different arguments from the edges. */
77 8114500 : if (virtual_operand_p (gimple_phi_result (p)))
78 : return NULL;
79 :
80 : /* If we already have a PHI that has the two edge arguments are
81 : different, then return it is not a singleton for these PHIs. */
82 1746104 : if (phi)
83 : return NULL;
84 :
85 : phi = p;
86 : }
87 : return phi;
88 : }
89 :
90 : /* Move the debug binds of BB, a basic block that is about to be removed,
91 : before the statement at GSI_TO and reset their values, so that the
92 : variables they bind do not keep an earlier value after it. */
93 :
94 : static void
95 88784 : move_reset_debug_binds (basic_block bb, gimple_stmt_iterator *gsi_to)
96 : {
97 88784 : if (!MAY_HAVE_DEBUG_BIND_STMTS)
98 : return;
99 131261 : for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
100 : {
101 70406 : gimple *stmt = gsi_stmt (gsi);
102 70406 : if (gimple_debug_bind_p (stmt))
103 : {
104 55394 : gsi_move_before (&gsi, gsi_to);
105 55394 : gimple_debug_bind_reset_value (stmt);
106 55394 : update_stmt (stmt);
107 : }
108 : else
109 15012 : gsi_next (&gsi);
110 : }
111 : }
112 :
113 : /* Replace PHI node element whose edge is E in block BB with variable NEW.
114 : Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
115 : is known to have two edges, one of which must reach BB). */
116 :
117 : static void
118 98887 : replace_phi_edge_with_variable (basic_block cond_block,
119 : edge e, gphi *phi, tree new_tree,
120 : bitmap dce_ssa_names = nullptr)
121 : {
122 98887 : basic_block bb = gimple_bb (phi);
123 98887 : gimple_stmt_iterator gsi;
124 98887 : tree phi_result = gimple_phi_result (phi);
125 98887 : bool deleteboth = false;
126 :
127 : /* Duplicate range info if they are the only things setting the target PHI.
128 : This is needed as later on, the new_tree will be replacing
129 : The assignment of the PHI.
130 : For an example:
131 : bb1:
132 : _4 = min<a_1, 255>
133 : goto bb2
134 :
135 : # RANGE [-INF, 255]
136 : a_3 = PHI<_4(1)>
137 : bb3:
138 :
139 : use(a_3)
140 : And _4 gets propagated into the use of a_3 and losing the range info.
141 : This can't be done for more than 2 incoming edges as the propagation
142 : won't happen.
143 : The new_tree needs to be defined in the same basic block as the conditional. */
144 98887 : if (TREE_CODE (new_tree) == SSA_NAME
145 98860 : && EDGE_COUNT (gimple_bb (phi)->preds) == 2
146 64418 : && INTEGRAL_TYPE_P (TREE_TYPE (phi_result))
147 59133 : && !SSA_NAME_RANGE_INFO (new_tree)
148 58729 : && SSA_NAME_RANGE_INFO (phi_result)
149 31617 : && gimple_bb (SSA_NAME_DEF_STMT (new_tree)) == cond_block
150 130503 : && dbg_cnt (phiopt_edge_range))
151 31616 : duplicate_ssa_name_range_info (new_tree, phi_result);
152 :
153 : /* Change the PHI argument to new. */
154 98887 : SET_USE (PHI_ARG_DEF_PTR (phi, e->dest_idx), new_tree);
155 :
156 : /* Remove the empty basic block. */
157 98887 : edge edge_to_remove = NULL, keep_edge = NULL;
158 98887 : if (EDGE_SUCC (cond_block, 0)->dest == bb)
159 : {
160 30635 : edge_to_remove = EDGE_SUCC (cond_block, 1);
161 30635 : keep_edge = EDGE_SUCC (cond_block, 0);
162 : }
163 68252 : else if (EDGE_SUCC (cond_block, 1)->dest == bb)
164 : {
165 : edge_to_remove = EDGE_SUCC (cond_block, 0);
166 : keep_edge = EDGE_SUCC (cond_block, 1);
167 : }
168 1387 : else if ((keep_edge = find_edge (cond_block, e->src)))
169 : {
170 1387 : basic_block bb1 = EDGE_SUCC (cond_block, 0)->dest;
171 1387 : basic_block bb2 = EDGE_SUCC (cond_block, 1)->dest;
172 : // If the new phi entry does not refer to either of
173 : // the middle bb, then either middle bbs can be deleted
174 : // as long as the middle bbs only reach the phi bb and only
175 : // reachable from the conditional bb.
176 101661 : if (single_pred_p (bb1) && single_pred_p (bb2)
177 2774 : && single_succ_p (bb1) && single_succ_p (bb2)
178 2774 : && (TREE_CODE (new_tree) != SSA_NAME
179 1387 : || (gimple_bb (SSA_NAME_DEF_STMT (new_tree)) != bb1
180 1387 : && gimple_bb (SSA_NAME_DEF_STMT (new_tree)) != bb2)))
181 : deleteboth = true;
182 : }
183 : else
184 0 : gcc_unreachable ();
185 :
186 : /* If we are removing the cond on a loop exit,
187 : reset number of iteration information of the loop. */
188 98887 : if (loop_exits_from_bb_p (cond_block->loop_father, cond_block))
189 : {
190 0 : auto loop = cond_block->loop_father;
191 0 : free_numbers_of_iterations_estimates (loop);
192 0 : loop->any_upper_bound = false;
193 0 : loop->any_likely_upper_bound = false;
194 : }
195 :
196 98887 : if (edge_to_remove && EDGE_COUNT (edge_to_remove->dest->preds) == 1)
197 : {
198 86010 : e->flags |= EDGE_FALLTHRU;
199 86010 : e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
200 86010 : e->probability = profile_probability::always ();
201 86010 : gsi = gsi_last_bb (cond_block);
202 86010 : move_reset_debug_binds (edge_to_remove->dest, &gsi);
203 86010 : delete_basic_block (edge_to_remove->dest);
204 :
205 : /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
206 86010 : gsi_remove (&gsi, true);
207 : }
208 12877 : else if (deleteboth)
209 : {
210 1387 : basic_block bb1 = EDGE_SUCC (cond_block, 0)->dest;
211 1387 : basic_block bb2 = EDGE_SUCC (cond_block, 1)->dest;
212 1387 : gsi = gsi_last_bb (cond_block);
213 1387 : move_reset_debug_binds (bb1, &gsi);
214 1387 : move_reset_debug_binds (bb2, &gsi);
215 :
216 1387 : edge newedge = redirect_edge_and_branch (keep_edge, bb);
217 :
218 : /* The new edge should be the same. */
219 1387 : gcc_assert (newedge == keep_edge);
220 :
221 1387 : keep_edge->flags |= EDGE_FALLTHRU;
222 1387 : keep_edge->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
223 1387 : keep_edge->probability = profile_probability::always ();
224 :
225 : /* Copy the edge's phi entry from the old one. */
226 1387 : copy_phi_arg_into_existing_phi (e, keep_edge);
227 :
228 : /* Delete the old 2 empty basic blocks */
229 1387 : delete_basic_block (bb1);
230 1387 : delete_basic_block (bb2);
231 :
232 : /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
233 1387 : gsi = gsi_last_bb (cond_block);
234 1387 : gsi_remove (&gsi, true);
235 : }
236 : else
237 : {
238 : /* If there are other edges into the middle block make
239 : CFG cleanup deal with the edge removal to avoid
240 : updating dominators here in a non-trivial way. */
241 22980 : gcond *cond = as_a <gcond *> (*gsi_last_bb (cond_block));
242 11490 : if (keep_edge->flags & EDGE_FALSE_VALUE)
243 7494 : gimple_cond_make_false (cond);
244 3996 : else if (keep_edge->flags & EDGE_TRUE_VALUE)
245 3996 : gimple_cond_make_true (cond);
246 : }
247 :
248 98887 : if (dce_ssa_names)
249 96973 : simple_dce_from_worklist (dce_ssa_names);
250 :
251 98887 : statistics_counter_event (cfun, "Replace PHI with variable", 1);
252 :
253 98887 : if (dump_file && (dump_flags & TDF_DETAILS))
254 30 : fprintf (dump_file,
255 : "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
256 : cond_block->index,
257 : bb->index);
258 98887 : }
259 :
260 : /* Returns true if the OPERANDS (OPCOUNTED) defined from DEF_STMT is profitable to move
261 : to the usage into the basic block MERGE where the new statement
262 : will be located. */
263 : static bool
264 72679 : is_factor_profitable (gimple *def_stmt, basic_block merge, tree *operands, unsigned opcount)
265 : {
266 : /* The defining statement should be conditional. */
267 72679 : if (dominated_by_p (CDI_DOMINATORS, merge,
268 72679 : gimple_bb (def_stmt)))
269 : return false;
270 :
271 : /* We should not increase the live range of arg
272 : across too many statements or calls. */
273 68839 : gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
274 68839 : gsi_next_nondebug (&gsi);
275 :
276 : /* Skip past nops and predicates. */
277 138331 : while (!gsi_end_p (gsi)
278 69492 : && (gimple_code (gsi_stmt (gsi)) == GIMPLE_NOP
279 9874 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
280 653 : gsi_next_nondebug (&gsi);
281 :
282 : /* If the defining statement is at the end of the bb, then it is
283 : always profitable to be to move. */
284 68839 : if (gsi_end_p (gsi))
285 : return true;
286 :
287 : /* If there are a few (non-call/asm) statements between
288 : the old defining statement and end of the bb, then
289 : the live range of operands will increase enough. */
290 9221 : int max_statements = param_phiopt_factor_max_stmts_live;
291 9221 : bool stmts_extending_ok = true;
292 :
293 26924 : while (!gsi_end_p (gsi))
294 : {
295 18868 : gimple *stmt = gsi_stmt (gsi);
296 18868 : auto gcode = gimple_code (stmt);
297 : /* Skip over NOPs and predicts. */
298 18913 : if (gcode == GIMPLE_NOP
299 18868 : || gcode == GIMPLE_PREDICT)
300 : {
301 45 : gsi_next_nondebug (&gsi);
302 45 : continue;
303 : }
304 : /* Non-assigns will extend the live range too much. */
305 18823 : if (gcode != GIMPLE_ASSIGN)
306 : {
307 : stmts_extending_ok = false;
308 : break;
309 : }
310 18546 : max_statements --;
311 18546 : if (max_statements == 0)
312 : {
313 : stmts_extending_ok = false;
314 : break;
315 : }
316 17658 : gsi_next_nondebug (&gsi);
317 : }
318 9221 : if (stmts_extending_ok)
319 : return true;
320 :
321 : /* Loop over all of the operands to see if all are used after anyways. */
322 1690 : for (unsigned i = 0; i < opcount; i++)
323 : {
324 1191 : tree arg = operands[i];
325 : /* If the arg is invariant, then there is
326 : no extending of the live range. */
327 1191 : if (is_gimple_min_invariant (arg))
328 456 : continue;
329 :
330 : /* Otherwise, the arg needs to be a ssa name. */
331 735 : if (TREE_CODE (arg) != SSA_NAME)
332 666 : return false;
333 :
334 : /* Check if the uses of arg is dominated by merge block, this is a quick and
335 : rough estimate if arg is still alive at the merge bb. */
336 : /* FIXME: extend to a more complete live range detection. */
337 735 : use_operand_p use_p;
338 735 : imm_use_iterator iter;
339 735 : bool usedafter = false;
340 2371 : FOR_EACH_IMM_USE_FAST (use_p, iter, arg)
341 : {
342 1705 : gimple *use_stmt = USE_STMT (use_p);
343 1705 : if (is_gimple_debug (use_stmt))
344 312 : continue;
345 1393 : basic_block use_bb = gimple_bb (use_stmt);
346 1393 : if (dominated_by_p (CDI_DOMINATORS, merge, use_bb))
347 : {
348 : usedafter = true;
349 : break;
350 : }
351 735 : }
352 735 : if (!usedafter)
353 : return false;
354 : }
355 : return true;
356 : }
357 :
358 : /* PR66726: Factor operations out of COND_EXPR. If the arguments of the PHI
359 : stmt are Unary operator, factor out the operation and perform the operation
360 : to the result of PHI stmt. COND_STMT is the controlling predicate.
361 : Return true if the operation was factored out; false otherwise. */
362 :
363 : static bool
364 3088561 : factor_out_conditional_operation (edge e0, edge e1, basic_block merge,
365 : gphi *phi, gimple *cond_stmt,
366 : bool early_p)
367 : {
368 3088561 : gimple *arg0_def_stmt = NULL, *arg1_def_stmt = NULL;
369 3088561 : tree temp, result;
370 3088561 : gphi *newphi;
371 3088561 : gimple_stmt_iterator gsi, gsi_for_def;
372 3088561 : location_t locus = gimple_location (phi);
373 3088561 : gimple_match_op arg0_op, arg1_op;
374 :
375 : /* We should only get here if the phi had two arguments. */
376 3088561 : gcc_assert (gimple_phi_num_args (phi) == 2);
377 :
378 : /* Virtual operands are never handled. */
379 6177122 : if (virtual_operand_p (gimple_phi_result (phi)))
380 : return false;
381 :
382 1358221 : tree arg0 = gimple_phi_arg_def (phi, e0->dest_idx);
383 1358221 : tree arg1 = gimple_phi_arg_def (phi, e1->dest_idx);
384 1358221 : location_t narg0_loc = gimple_location (phi);
385 1358221 : location_t narg1_loc = gimple_location (phi);
386 1358221 : if (gimple_phi_arg_location (phi, e0->dest_idx) != UNKNOWN_LOCATION)
387 1132707 : narg0_loc = gimple_phi_arg_location (phi, e0->dest_idx);
388 1358221 : if (gimple_phi_arg_location (phi, e1->dest_idx) != UNKNOWN_LOCATION)
389 954272 : narg1_loc = gimple_phi_arg_location (phi, e1->dest_idx);
390 :
391 1358221 : gcc_assert (arg0 != NULL_TREE && arg1 != NULL_TREE);
392 :
393 : /* Arguments that are the same don't have anything to be
394 : done to them. */
395 1358221 : if (operand_equal_for_phi_arg_p (arg0, arg1))
396 : return false;
397 :
398 : /* First canonicalize to simplify tests. */
399 1358034 : if (TREE_CODE (arg0) != SSA_NAME)
400 : {
401 266371 : std::swap (arg0, arg1);
402 266371 : std::swap (e0, e1);
403 : }
404 :
405 1358034 : if (TREE_CODE (arg0) != SSA_NAME
406 1236002 : || (TREE_CODE (arg1) != SSA_NAME
407 501340 : && TREE_CODE (arg1) != INTEGER_CST))
408 : return false;
409 :
410 : /* Check if arg0 is an SSA_NAME and the stmt which defines arg0 is
411 : an unary operation. */
412 1200392 : arg0_def_stmt = SSA_NAME_DEF_STMT (arg0);
413 1200392 : if (!gimple_extract_op (arg0_def_stmt, &arg0_op))
414 : return false;
415 :
416 : /* Check to make sure none of the operands are in abnormal phis. */
417 627240 : if (arg0_op.operands_occurs_in_abnormal_phi ())
418 : return false;
419 :
420 627240 : tree new_arg0;
421 627240 : tree new_arg1;
422 627240 : int opnum = -1;
423 :
424 : /* If arg0 have > 1 use, then this transformation actually increases
425 : the number of expressions evaluated at runtime. */
426 627240 : if (!has_single_use (arg0))
427 : return false;
428 510178 : if (gimple_has_location (arg0_def_stmt))
429 467894 : narg0_loc = gimple_location (arg0_def_stmt);
430 :
431 510178 : if (TREE_CODE (arg1) == SSA_NAME)
432 : {
433 : /* Check if arg1 is an SSA_NAME. */
434 330083 : arg1_def_stmt = SSA_NAME_DEF_STMT (arg1);
435 330083 : if (!gimple_extract_op (arg1_def_stmt, &arg1_op))
436 321492 : return false;
437 171813 : if (arg1_op.code != arg0_op.code)
438 : return false;
439 48280 : if (arg1_op.num_ops != arg0_op.num_ops)
440 : return false;
441 48264 : if (arg1_op.operands_occurs_in_abnormal_phi ())
442 : return false;
443 :
444 : /* For the complex expression, don't factor
445 : out, that will confuse the uninitializing
446 : warnings. */
447 48264 : if (arg1_op.code == COMPLEX_EXPR)
448 : return false;
449 :
450 : /* If arg1 have > 1 use, then this transformation actually increases
451 : the number of expressions evaluated at runtime. */
452 48149 : if (!has_single_use (arg1))
453 : return false;
454 :
455 35880 : opnum = find_different_opnum (arg0_op, arg1_op, &new_arg0, &new_arg1);
456 35880 : if (opnum == -1)
457 : return false;
458 :
459 : /* Check to make sure extending the lifetimes of all operands is ok. */
460 13791 : if (!is_factor_profitable (arg0_def_stmt, merge,
461 : arg0_op.ops, arg0_op.num_ops))
462 : return false;
463 12731 : if (!is_factor_profitable (arg1_def_stmt, merge,
464 : arg1_op.ops, arg1_op.num_ops))
465 : return false;
466 :
467 10182 : tree args[2] = { new_arg0, new_arg1 };
468 10182 : location_t locs[2];
469 10182 : locs[0] = gimple_location (arg0_def_stmt);
470 10182 : locs[1] = gimple_location (arg1_def_stmt);
471 10182 : if (!factor_operation_ok (arg1_op.code, opnum, args, locs, 2, false, !early_p))
472 : return false;
473 :
474 8591 : if (gimple_has_location (arg1_def_stmt))
475 8287 : narg1_loc = gimple_location (arg1_def_stmt);
476 :
477 : /* Chose the location for the new statement if the phi location is unknown. */
478 8591 : if (locus == UNKNOWN_LOCATION)
479 : {
480 8591 : if (narg0_loc == UNKNOWN_LOCATION
481 8591 : && narg1_loc != UNKNOWN_LOCATION)
482 : locus = narg1_loc;
483 8591 : else if (narg0_loc != UNKNOWN_LOCATION
484 8591 : && narg1_loc == UNKNOWN_LOCATION)
485 97 : locus = narg0_loc;
486 : }
487 : }
488 180095 : else if (arg0_op.num_ops != 1)
489 : return false;
490 : else
491 : {
492 52790 : new_arg0 = arg0_op.ops[0];
493 52790 : opnum = 0;
494 : /* For constants only handle if the phi was the only one. */
495 52790 : if (single_non_singleton_phi_for_edges (phi_nodes (merge), e0, e1) == NULL)
496 : return false;
497 : /* TODO: handle more than just casts and ~/- here. */
498 39375 : if (!gimple_assign_cast_p (arg0_def_stmt)
499 6812 : && arg0_op.code != NEGATE_EXPR
500 45602 : && arg0_op.code != BIT_NOT_EXPR)
501 : return false;
502 34359 : if (!is_factor_profitable (arg0_def_stmt, merge, arg0_op.ops, arg0_op.num_ops))
503 : return false;
504 :
505 : // Heuristics for ~/-, is about being able to being able to handle
506 : // `if (a) t = -1; else t = -(CMP);`
507 : // and the like. - is usually used for a mask to emulate a
508 : // vector.
509 33480 : if (arg0_op.code == NEGATE_EXPR
510 33480 : || arg0_op.code == BIT_NOT_EXPR)
511 : {
512 1762 : if (TREE_CODE (arg1) != INTEGER_CST
513 1762 : || TREE_CODE (new_arg0) != SSA_NAME)
514 : return false;
515 :
516 : // For neg, only allow -1, 0 as those
517 : // the only ones which will reduce.
518 1762 : if (arg0_op.code == NEGATE_EXPR
519 573 : && !integer_zerop (arg1)
520 2145 : && !integer_minus_onep (arg1))
521 : return false;
522 : // For ~, only allow -2 (~1), -1 (~0) as those
523 : // the only ones which will reduce.
524 2812 : if (arg0_op.code == BIT_NOT_EXPR
525 1189 : && !integer_minus_onep (arg1)
526 3378 : && ~wi::to_wide (arg1) != 1)
527 : return false;
528 :
529 : // Look to see if the -/~ comes from a cast from a boolean.
530 624 : gimple *arg0_def_def_stmt = SSA_NAME_DEF_STMT (new_arg0);
531 624 : if (gimple_bb (arg0_def_def_stmt) != gimple_bb (arg0_def_stmt))
532 : return false;
533 404 : if (!gimple_assign_cast_p (arg0_def_def_stmt))
534 : return false;
535 140 : tree_code ass_code = gimple_assign_rhs_code (arg0_def_def_stmt);
536 140 : if (!CONVERT_EXPR_CODE_P (ass_code))
537 : return false;
538 136 : tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (arg0_def_def_stmt));
539 136 : if (!INTEGRAL_TYPE_P (rhs_type)
540 136 : || !TYPE_UNSIGNED (rhs_type)
541 228 : || TYPE_PRECISION (rhs_type) != 1)
542 : return false;
543 : }
544 :
545 : /* If arg1 is an INTEGER_CST, fold it to new type if it fits, or else
546 : if the bits will not be modified during the conversion, except for
547 : boolean types whose precision is not 1 (see int_fits_type_p). */
548 63423 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (new_arg0))
549 63159 : || !(int_fits_type_p (arg1, TREE_TYPE (new_arg0))
550 1843 : || (TYPE_PRECISION (TREE_TYPE (new_arg0))
551 1843 : == TYPE_PRECISION (TREE_TYPE (arg1))
552 724 : && (TREE_CODE (TREE_TYPE (new_arg0)) != BOOLEAN_TYPE
553 0 : || TYPE_PRECISION (TREE_TYPE (new_arg0)) == 1))))
554 : return false;
555 :
556 : /* For the INTEGER_CST case, we are just moving the
557 : conversion from one place to another, which can often
558 : hurt as the conversion moves further away from the
559 : statement that computes the value. So, perform this
560 : only if new_arg0 is an operand of COND_STMT, or
561 : if arg0_def_stmt is the only non-debug stmt in
562 : its basic block, because then it is possible this
563 : could enable further optimizations (minmax replacement
564 : etc.). See PR71016.
565 : Note no-op conversions don't have this issue as
566 : it will not generate any zero/sign extend in that case. */
567 30335 : else if ((TYPE_PRECISION (TREE_TYPE (new_arg0))
568 30335 : != TYPE_PRECISION (TREE_TYPE (arg1)))
569 14665 : && new_arg0 != gimple_cond_lhs (cond_stmt)
570 13964 : && new_arg0 != gimple_cond_rhs (cond_stmt)
571 44293 : && gimple_bb (arg0_def_stmt) == e0->src)
572 : {
573 13958 : gsi = gsi_for_stmt (arg0_def_stmt);
574 13958 : gsi_prev_nondebug (&gsi);
575 : /* Ignore nops, predicates and labels. */
576 27928 : while (!gsi_end_p (gsi)
577 13970 : && (gimple_code (gsi_stmt (gsi)) == GIMPLE_NOP
578 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT
579 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL))
580 12 : gsi_prev_nondebug (&gsi);
581 :
582 13958 : if (!gsi_end_p (gsi))
583 : {
584 10524 : gimple *stmt = gsi_stmt (gsi);
585 10524 : if (gassign *assign = dyn_cast <gassign *> (stmt))
586 : {
587 9889 : tree lhs = gimple_assign_lhs (assign);
588 9889 : tree lhst = TREE_TYPE (lhs);
589 9889 : enum tree_code ass_code
590 9889 : = gimple_assign_rhs_code (assign);
591 9889 : if (ass_code != MAX_EXPR && ass_code != MIN_EXPR
592 : /* Conversions from boolean like types is ok
593 : as `a?1:b` and `a?0:b` will always simplify
594 : to `a & b` or `a | b`.
595 : See PR 116890. */
596 9889 : && !(INTEGRAL_TYPE_P (lhst)
597 9409 : && TYPE_UNSIGNED (lhst)
598 6990 : && TYPE_PRECISION (lhst) == 1))
599 : return false;
600 4738 : if (lhs != gimple_assign_rhs1 (arg0_def_stmt))
601 : return false;
602 4696 : gsi_prev_nondebug (&gsi);
603 4696 : if (!gsi_end_p (gsi))
604 : return false;
605 : }
606 : else
607 : return false;
608 : }
609 : }
610 :
611 : // - is still the opposite of -, likewise for ~.
612 20248 : if (arg0_op.code == NEGATE_EXPR
613 20248 : || arg0_op.code == BIT_NOT_EXPR)
614 91 : new_arg1 = fold_build1 ((tree_code)arg0_op.code,
615 : TREE_TYPE (new_arg0), arg1);
616 : else
617 20157 : new_arg1 = fold_convert (TREE_TYPE (new_arg0), arg1);
618 :
619 : /* Drop the overflow that fold_convert might add. */
620 20248 : if (TREE_OVERFLOW (new_arg1))
621 0 : new_arg1 = drop_tree_overflow (new_arg1);
622 :
623 : /* The locus of the new statement is arg0 defining statement. */
624 20248 : if (gimple_has_location (arg0_def_stmt))
625 19760 : locus = gimple_location (arg0_def_stmt);
626 : }
627 :
628 : /* Create a new PHI stmt. */
629 28839 : result = gimple_phi_result (phi);
630 28839 : temp = make_ssa_name (TREE_TYPE (new_arg0), NULL);
631 :
632 28839 : gimple_match_op new_op = arg0_op;
633 :
634 : /* Create the operation stmt if possible and insert it. */
635 28839 : new_op.ops[opnum] = temp;
636 28839 : gimple_seq seq = NULL;
637 28839 : result = maybe_push_res_to_seq (&new_op, &seq, result);
638 :
639 : /* If we can't create the new statement, release the temp name
640 : and return back. */
641 28839 : if (!result)
642 : {
643 257 : release_ssa_name (temp);
644 257 : return false;
645 : }
646 :
647 28582 : if (locus != UNKNOWN_LOCATION)
648 19857 : annotate_all_with_location (seq, locus);
649 28582 : gsi = gsi_after_labels (gimple_bb (phi));
650 28582 : gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING);
651 :
652 28582 : newphi = create_phi_node (temp, gimple_bb (phi));
653 :
654 28582 : if (dump_file && (dump_flags & TDF_DETAILS))
655 : {
656 38 : fprintf (dump_file, "PHI ");
657 38 : print_generic_expr (dump_file, gimple_phi_result (phi));
658 38 : fprintf (dump_file,
659 : " changed to factor operation out from COND_EXPR.\n");
660 38 : fprintf (dump_file, "New stmt with OPERATION that defines ");
661 38 : print_generic_expr (dump_file, result);
662 38 : fprintf (dump_file, ".\n");
663 : }
664 :
665 : /* Remove the old operation(s) that has single use. */
666 28582 : gsi_for_def = gsi_for_stmt (arg0_def_stmt);
667 28582 : gsi_remove (&gsi_for_def, true);
668 28582 : release_defs (arg0_def_stmt);
669 :
670 28582 : if (arg1_def_stmt)
671 : {
672 8334 : gsi_for_def = gsi_for_stmt (arg1_def_stmt);
673 8334 : gsi_remove (&gsi_for_def, true);
674 8334 : release_defs (arg1_def_stmt);
675 : }
676 :
677 28582 : add_phi_arg (newphi, new_arg0, e0, narg0_loc);
678 28582 : add_phi_arg (newphi, new_arg1, e1, narg1_loc);
679 :
680 : /* Remove the original PHI stmt. */
681 28582 : gsi = gsi_for_stmt (phi);
682 28582 : remove_phi_node (&gsi, false);
683 :
684 28582 : statistics_counter_event (cfun, "factored out operation", 1);
685 :
686 28582 : return true;
687 : }
688 :
689 :
690 : /* Return TRUE if SEQ/OP pair should be allowed during early phiopt.
691 : Currently this is to allow MIN/MAX and ABS/NEGATE and constants. */
692 : static bool
693 182224 : phiopt_early_allow (gimple_seq &seq, gimple_match_op &op)
694 : {
695 : /* Don't allow functions. */
696 182224 : if (!op.code.is_tree_code ())
697 : return false;
698 182145 : tree_code code = (tree_code)op.code;
699 :
700 : /* For non-empty sequence, only allow one statement
701 : a MIN/MAX and an original MIN/MAX. */
702 182145 : if (!gimple_seq_empty_p (seq))
703 : {
704 113593 : if (code == MIN_EXPR || code == MAX_EXPR)
705 : {
706 149118 : if (!gimple_seq_singleton_p (seq))
707 : return false;
708 :
709 13 : gimple *stmt = gimple_seq_first_stmt (seq);
710 : /* Only allow assignments. */
711 13 : if (!is_gimple_assign (stmt))
712 : return false;
713 13 : code = gimple_assign_rhs_code (stmt);
714 13 : return code == MIN_EXPR || code == MAX_EXPR;
715 : }
716 : return false;
717 : }
718 :
719 68552 : switch (code)
720 : {
721 : case MIN_EXPR:
722 : case MAX_EXPR:
723 : case ABS_EXPR:
724 : case ABSU_EXPR:
725 : case NEGATE_EXPR:
726 : case SSA_NAME:
727 : return true;
728 : case INTEGER_CST:
729 : case REAL_CST:
730 : case VECTOR_CST:
731 : case FIXED_CST:
732 : return true;
733 : default:
734 : return false;
735 : }
736 : }
737 :
738 : /* Remove unused statements in SEQ. That are used in OP.
739 : This is needed for the way resimplify/match and simplify
740 : works as it will place some unneeded statements in the sequence
741 : which can get in the way of handling phiopt_early_allow. */
742 :
743 : static void
744 241965 : remove_unused_stmts (gimple_seq *seq, const gimple_match_op &op)
745 : {
746 241965 : if (*seq == nullptr)
747 42876 : return;
748 :
749 199089 : walk_stmt_info wi = {};
750 :
751 : /* Since the sequence of statements are not part of
752 : a basic block yet, have to manually go through the operands
753 : to mark the uses. */
754 199089 : auto_bitmap uses;
755 199089 : wi.info = (bitmap)uses;
756 1117204 : auto fn = [](tree *tp, int *, void *data) -> tree
757 : {
758 918115 : walk_stmt_info *wi = (walk_stmt_info*)data;
759 918115 : if (wi->is_lhs)
760 : return NULL_TREE;
761 696836 : bitmap uses = (bitmap)wi->info;
762 696836 : tree use = *tp;
763 696836 : if (TREE_CODE (use) == SSA_NAME)
764 531118 : bitmap_set_bit (uses, SSA_NAME_VERSION (use));
765 : return NULL_TREE;
766 : };
767 493162 : for (unsigned i = 0; i < op.num_ops; i++)
768 : {
769 294073 : tree use = op.ops[i];
770 294073 : walk_tree (&use, fn, &wi, nullptr);
771 : }
772 199089 : gimple_stmt_iterator gsi = gsi_last (*seq);
773 515440 : while (!gsi_end_p (gsi))
774 : {
775 316351 : gimple *s = *gsi;
776 316351 : tree lhs = gimple_get_lhs (s);
777 316351 : if (bitmap_bit_p (uses, SSA_NAME_VERSION (lhs)))
778 : {
779 221279 : walk_gimple_op (s, fn, &wi);
780 221279 : gsi_prev (&gsi);
781 221279 : continue;
782 : }
783 95072 : gsi_remove (&gsi, true);
784 95072 : release_defs (s);
785 95072 : ggc_free (s);
786 : /* gsi_remove goes to the next stmt so start over. */
787 95072 : if (gsi_end_p (gsi))
788 99782 : gsi = gsi_last (*seq);
789 : }
790 199089 : }
791 :
792 : /* gimple_simplify_phiopt is like gimple_simplify but designed for PHIOPT.
793 : Return NULL if nothing can be simplified or the resulting simplified value
794 : with parts pushed if EARLY_P was true. Also rejects non allowed tree code
795 : if EARLY_P is set.
796 : Takes the comparison from COMP_STMT and two args, ARG0 and ARG1 and tries
797 : to simplify CMP ? ARG0 : ARG1.
798 : Also try to simplify (!CMP) ? ARG1 : ARG0 if the non-inverse failed. */
799 : static tree
800 547258 : gimple_simplify_phiopt (bool early_p, tree type, gimple *comp_stmt,
801 : tree arg0, tree arg1,
802 : gimple_seq *seq)
803 : {
804 547258 : gimple_seq seq1 = NULL;
805 547258 : enum tree_code comp_code = gimple_cond_code (comp_stmt);
806 547258 : location_t loc = gimple_location (comp_stmt);
807 547258 : tree cmp0 = gimple_cond_lhs (comp_stmt);
808 547258 : tree cmp1 = gimple_cond_rhs (comp_stmt);
809 : /* To handle special cases like floating point comparison, it is easier and
810 : less error-prone to build a tree and gimplify it on the fly though it is
811 : less efficient.
812 : Don't use fold_build2 here as that might create (bool)a instead of just
813 : "a != 0". */
814 547258 : tree cond = build2_loc (loc, comp_code, boolean_type_node,
815 : cmp0, cmp1);
816 :
817 547258 : if (dump_file && (dump_flags & TDF_FOLDING))
818 : {
819 1 : fprintf (dump_file, "\nphiopt match-simplify trying:\n\t");
820 1 : print_generic_expr (dump_file, cond);
821 1 : fprintf (dump_file, " ? ");
822 1 : print_generic_expr (dump_file, arg0);
823 1 : fprintf (dump_file, " : ");
824 1 : print_generic_expr (dump_file, arg1);
825 1 : fprintf (dump_file, "\n");
826 : }
827 :
828 547258 : gimple_match_op op (gimple_match_cond::UNCOND,
829 547258 : COND_EXPR, type, cond, arg0, arg1);
830 :
831 547258 : if (op.resimplify (&seq1, follow_all_ssa_edges))
832 : {
833 163735 : remove_unused_stmts (&seq1, op);
834 163735 : bool allowed = !early_p || phiopt_early_allow (seq1, op);
835 163735 : tree result = maybe_push_res_to_seq (&op, &seq1);
836 163735 : if (dump_file && (dump_flags & TDF_FOLDING))
837 : {
838 1 : fprintf (dump_file, "\nphiopt match-simplify back:\n");
839 1 : if (seq1)
840 0 : print_gimple_seq (dump_file, seq1, 0, TDF_VOPS|TDF_MEMSYMS);
841 1 : fprintf (dump_file, "result: ");
842 1 : if (result)
843 1 : print_generic_expr (dump_file, result);
844 : else
845 0 : fprintf (dump_file, " (none)");
846 1 : fprintf (dump_file, "\n");
847 1 : if (!allowed)
848 0 : fprintf (dump_file, "rejected because early\n");
849 : }
850 : /* Early we want only to allow some generated tree codes. */
851 163735 : if (allowed && result)
852 : {
853 88057 : if (loc != UNKNOWN_LOCATION)
854 87569 : annotate_all_with_location (seq1, loc);
855 88057 : gimple_seq_add_seq_without_update (seq, seq1);
856 88057 : return result;
857 : }
858 : }
859 459201 : gimple_seq_discard (seq1);
860 459201 : seq1 = NULL;
861 :
862 : /* Try the inverted comparison, that is !COMP ? ARG1 : ARG0. */
863 459201 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (cmp0));
864 :
865 459201 : if (comp_code == ERROR_MARK)
866 : return NULL;
867 :
868 441742 : cond = build2_loc (loc,
869 : comp_code, boolean_type_node,
870 : cmp0, cmp1);
871 :
872 441742 : if (dump_file && (dump_flags & TDF_FOLDING))
873 : {
874 0 : fprintf (dump_file, "\nphiopt match-simplify trying:\n\t");
875 0 : print_generic_expr (dump_file, cond);
876 0 : fprintf (dump_file, " ? ");
877 0 : print_generic_expr (dump_file, arg1);
878 0 : fprintf (dump_file, " : ");
879 0 : print_generic_expr (dump_file, arg0);
880 0 : fprintf (dump_file, "\n");
881 : }
882 :
883 441742 : gimple_match_op op1 (gimple_match_cond::UNCOND,
884 441742 : COND_EXPR, type, cond, arg1, arg0);
885 :
886 441742 : if (op1.resimplify (&seq1, follow_all_ssa_edges))
887 : {
888 78230 : remove_unused_stmts (&seq1, op1);
889 78230 : bool allowed = !early_p || phiopt_early_allow (seq1, op1);
890 78230 : tree result = maybe_push_res_to_seq (&op1, &seq1);
891 78230 : if (dump_file && (dump_flags & TDF_FOLDING))
892 : {
893 0 : fprintf (dump_file, "\nphiopt match-simplify back:\n");
894 0 : if (seq1)
895 0 : print_gimple_seq (dump_file, seq1, 0, TDF_VOPS|TDF_MEMSYMS);
896 0 : fprintf (dump_file, "result: ");
897 0 : if (result)
898 0 : print_generic_expr (dump_file, result);
899 : else
900 0 : fprintf (dump_file, " (none)");
901 0 : fprintf (dump_file, "\n");
902 0 : if (!allowed)
903 0 : fprintf (dump_file, "rejected because early\n");
904 : }
905 : /* Early we want only to allow some generated tree codes. */
906 78230 : if (allowed && result)
907 : {
908 4771 : if (loc != UNKNOWN_LOCATION)
909 4771 : annotate_all_with_location (seq1, loc);
910 4771 : gimple_seq_add_seq_without_update (seq, seq1);
911 4771 : return result;
912 : }
913 : }
914 436971 : gimple_seq_discard (seq1);
915 :
916 436971 : return NULL;
917 : }
918 :
919 :
920 : /* Returns true if the uses of the sequence SEQ contains only references to
921 : BB0 or BB1 (but not both) or the references are outside of either BB0/BB1. */
922 :
923 : static bool
924 1385 : check_uses (gimple_seq seq, basic_block bb0, basic_block bb1)
925 : {
926 1385 : if (!seq)
927 : return true;
928 932 : gcc_checking_assert (bb0);
929 932 : gcc_checking_assert (bb1);
930 932 : struct bb_info {
931 : basic_block bb0;
932 : basic_block bb1;
933 : bool bb0_referenced;
934 : bool bb1_referenced;
935 : };
936 932 : bb_info info = { bb0, bb1, false, false };
937 :
938 932 : walk_stmt_info wi = {};
939 932 : wi.info = &info;
940 6642 : auto fn = [](tree *tp, int *, void *data) -> tree
941 : {
942 5710 : walk_stmt_info *wi = (walk_stmt_info*)data;
943 5710 : if (wi->is_lhs)
944 : return NULL_TREE;
945 3600 : bb_info *uses = (bb_info*)wi->info;
946 3600 : tree use = *tp;
947 3600 : if (TREE_CODE (use) != SSA_NAME)
948 : return NULL_TREE;
949 2779 : gimple *stmt = SSA_NAME_DEF_STMT (use);
950 2779 : basic_block sbb = gimple_bb (stmt);
951 : // A stmt with a null bb is ok, either it is the sequnce
952 : // or is outside default defined one.
953 2779 : if (sbb == nullptr)
954 : return NULL_TREE;
955 1224 : else if (sbb == uses->bb0)
956 : {
957 11 : if (uses->bb1_referenced)
958 : return use;
959 11 : uses->bb0_referenced = true;
960 : }
961 1213 : else if (sbb == uses->bb1)
962 : {
963 412 : if (uses->bb0_referenced)
964 : return use;
965 412 : uses->bb1_referenced = true;
966 : }
967 : return NULL_TREE;
968 : };
969 :
970 :
971 932 : gimple_stmt_iterator gsi = gsi_last (seq);
972 3974 : for( ; !gsi_end_p (gsi); gsi_prev (&gsi))
973 : {
974 2110 : gimple *s = *gsi;
975 2110 : if (walk_gimple_op (s, fn, &wi))
976 : return false;
977 : }
978 : return true;
979 : }
980 :
981 : /* one_feeding_comparison_into_p returns true if BB has one comparison
982 : statement and it sets STMT to that statement. Note the comparison can
983 : be trapping too. */
984 : static bool
985 1284 : one_feeding_comparison_into_p (basic_block bb,
986 : gimple *phi,
987 : gassign *&assign)
988 : {
989 1284 : assign = nullptr;
990 1284 : gimple *stmt = nullptr;
991 :
992 1284 : if (empty_block_p (bb))
993 : return false;
994 :
995 1266 : if (!single_pred_p (bb))
996 : return false;
997 :
998 1248 : if (!gimple_seq_empty_p (phi_nodes (bb)))
999 : return false;
1000 :
1001 1248 : gimple_stmt_iterator gsi;
1002 1248 : gsi = gsi_start_nondebug_after_labels_bb (bb);
1003 2498 : while (!gsi_end_p (gsi))
1004 : {
1005 2024 : gimple *s = gsi_stmt (gsi);
1006 2024 : gsi_next_nondebug (&gsi);
1007 : /* Skip over Predict and nop statements. */
1008 2024 : if (gimple_code (s) == GIMPLE_PREDICT
1009 2024 : || gimple_code (s) == GIMPLE_NOP)
1010 2 : continue;
1011 : /* If there is more one statement return false. */
1012 2022 : if (stmt)
1013 : return false;
1014 : stmt = s;
1015 : }
1016 :
1017 474 : if (!stmt)
1018 : return false;
1019 :
1020 948 : if (gimple_vuse (stmt))
1021 : return false;
1022 :
1023 468 : gassign *a = dyn_cast<gassign*>(stmt);
1024 468 : if (!a || TREE_CODE_CLASS (gimple_assign_rhs_code (a)) != tcc_comparison)
1025 : return false;
1026 :
1027 468 : tree lhs = gimple_assign_lhs (a);
1028 :
1029 468 : gimple *use_stmt;
1030 468 : use_operand_p use_p;
1031 : /* Allow only a statement which feeds into the other stmt. */
1032 468 : if (!lhs || TREE_CODE (lhs) != SSA_NAME
1033 468 : || !single_imm_use (lhs, &use_p, &use_stmt)
1034 936 : || use_stmt != phi)
1035 : return false;
1036 :
1037 : // Don't handle non-call exceptions
1038 468 : if (stmt_could_throw_p (cfun, a))
1039 : return false;
1040 :
1041 462 : assign = a;
1042 462 : return true;
1043 : }
1044 :
1045 : /* empty_bb_or_one_feeding_into_p returns true if bb was empty basic block
1046 : or it has one cheap preparation statement that feeds into the PHI
1047 : statement and it sets STMT to that statement. */
1048 : static bool
1049 893519 : empty_bb_or_one_feeding_into_p (basic_block bb,
1050 : gimple *phi,
1051 : gimple *&stmt)
1052 : {
1053 893519 : stmt = nullptr;
1054 893519 : gimple *stmt_to_move = nullptr;
1055 893519 : tree lhs;
1056 :
1057 893519 : if (empty_block_p (bb))
1058 : return true;
1059 :
1060 470499 : if (!single_pred_p (bb))
1061 : return false;
1062 :
1063 : /* The middle bb cannot have phi nodes as we don't
1064 : move those assignments yet. */
1065 456970 : if (!gimple_seq_empty_p (phi_nodes (bb)))
1066 : return false;
1067 :
1068 456950 : gimple_stmt_iterator gsi;
1069 :
1070 456950 : gsi = gsi_start_nondebug_after_labels_bb (bb);
1071 917156 : while (!gsi_end_p (gsi))
1072 : {
1073 678239 : gimple *s = gsi_stmt (gsi);
1074 678239 : gsi_next_nondebug (&gsi);
1075 : /* Skip over Predict and nop statements. */
1076 678239 : if (gimple_code (s) == GIMPLE_PREDICT
1077 678239 : || gimple_code (s) == GIMPLE_NOP)
1078 3256 : continue;
1079 : /* If there is more one statement return false. */
1080 674983 : if (stmt_to_move)
1081 : return false;
1082 : stmt_to_move = s;
1083 : }
1084 :
1085 : /* The only statement here was a Predict or a nop statement
1086 : so return true. */
1087 238917 : if (!stmt_to_move)
1088 : return true;
1089 :
1090 477834 : if (gimple_vuse (stmt_to_move))
1091 : return false;
1092 :
1093 167186 : if (gimple_could_trap_p (stmt_to_move)
1094 167186 : || gimple_has_side_effects (stmt_to_move))
1095 : return false;
1096 :
1097 161662 : ssa_op_iter it;
1098 161662 : tree use;
1099 353937 : FOR_EACH_SSA_TREE_OPERAND (use, stmt_to_move, it, SSA_OP_USE)
1100 192943 : if (ssa_name_maybe_undef_p (use))
1101 : return false;
1102 :
1103 : /* Allow assignments but allow some builtin/internal calls.
1104 : As const calls don't match any of the above, yet they could
1105 : still have some side-effects - they could contain
1106 : gimple_could_trap_p statements, like floating point
1107 : exceptions or integer division by zero. See PR70586.
1108 : FIXME: perhaps gimple_has_side_effects or gimple_could_trap_p
1109 : should handle this.
1110 : Allow some known builtin/internal calls that are known not to
1111 : trap: logical functions (e.g. bswap and bit counting). */
1112 160994 : if (!is_gimple_assign (stmt_to_move))
1113 : {
1114 6263 : if (!is_gimple_call (stmt_to_move))
1115 : return false;
1116 6004 : combined_fn cfn = gimple_call_combined_fn (stmt_to_move);
1117 6004 : switch (cfn)
1118 : {
1119 : default:
1120 : return false;
1121 5031 : CASE_CFN_BSWAP:
1122 5031 : CASE_CFN_BITREVERSE:
1123 5031 : CASE_CFN_FFS:
1124 5031 : CASE_CFN_PARITY:
1125 5031 : CASE_CFN_POPCOUNT:
1126 5031 : CASE_CFN_CLZ:
1127 5031 : CASE_CFN_CTZ:
1128 5031 : case CFN_BUILT_IN_CLRSB:
1129 5031 : case CFN_BUILT_IN_CLRSBL:
1130 5031 : case CFN_BUILT_IN_CLRSBLL:
1131 5031 : lhs = gimple_call_lhs (stmt_to_move);
1132 5031 : break;
1133 : }
1134 : }
1135 : else
1136 154731 : lhs = gimple_assign_lhs (stmt_to_move);
1137 :
1138 159762 : gimple *use_stmt;
1139 159762 : use_operand_p use_p;
1140 :
1141 : /* Allow only a statement which feeds into the other stmt. */
1142 159762 : if (!lhs || TREE_CODE (lhs) != SSA_NAME
1143 159762 : || !single_imm_use (lhs, &use_p, &use_stmt)
1144 319519 : || use_stmt != phi)
1145 : return false;
1146 :
1147 159757 : stmt = stmt_to_move;
1148 159757 : return true;
1149 : }
1150 :
1151 : /* Move STMT to before GSI and insert its defining
1152 : name into INSERTED_EXPRS bitmap.
1153 : Also rewrite its if it might be undefined when unconditionalized. */
1154 : static void
1155 193946 : move_stmt (gimple *stmt, gimple_stmt_iterator *gsi, auto_bitmap &inserted_exprs)
1156 : {
1157 193946 : if (!stmt)
1158 187649 : return;
1159 6297 : if (dump_file && (dump_flags & TDF_DETAILS))
1160 : {
1161 9 : fprintf (dump_file, "statement un-sinked:\n");
1162 9 : print_gimple_stmt (dump_file, stmt, 0,
1163 : TDF_VOPS|TDF_MEMSYMS);
1164 : }
1165 :
1166 6297 : tree name = gimple_get_lhs (stmt);
1167 : // Mark the name to be renamed if there is one.
1168 6297 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (name));
1169 6297 : gimple_stmt_iterator gsi1 = gsi_for_stmt (stmt);
1170 6297 : gsi_move_before (&gsi1, gsi, GSI_NEW_STMT);
1171 6297 : reset_flow_sensitive_info (name);
1172 :
1173 : /* Rewrite some code which might be undefined when
1174 : unconditionalized. */
1175 6297 : if (gimple_needing_rewrite_undefined (stmt))
1176 1227 : rewrite_to_defined_unconditional (gsi);
1177 : }
1178 :
1179 : /* RAII style class to temporarily remove flow sensitive
1180 : from ssa names defined by a gimple statement. */
1181 : class auto_flow_sensitive
1182 : {
1183 : public:
1184 : auto_flow_sensitive (gimple *s);
1185 : ~auto_flow_sensitive ();
1186 : private:
1187 : auto_vec<std::pair<tree, flow_sensitive_info_storage>, 2> stack;
1188 : };
1189 :
1190 : /* Constructor for auto_flow_sensitive. Saves
1191 : off the ssa names' flow sensitive information
1192 : that was defined by gimple statement S and
1193 : resets it to be non-flow based ones. */
1194 :
1195 1094516 : auto_flow_sensitive::auto_flow_sensitive (gimple *s)
1196 : {
1197 1094516 : if (!s)
1198 943542 : return;
1199 150974 : ssa_op_iter it;
1200 150974 : tree def;
1201 301948 : FOR_EACH_SSA_TREE_OPERAND (def, s, it, SSA_OP_DEF)
1202 : {
1203 150974 : flow_sensitive_info_storage storage;
1204 150974 : storage.save_and_clear (def);
1205 150974 : stack.safe_push (std::make_pair (def, storage));
1206 : }
1207 : }
1208 :
1209 : /* Deconstructor, restores the flow sensitive information
1210 : for the SSA names that had been saved off. */
1211 :
1212 1094516 : auto_flow_sensitive::~auto_flow_sensitive ()
1213 : {
1214 3434522 : for (auto p : stack)
1215 150974 : p.second.restore (p.first);
1216 1094516 : }
1217 :
1218 : /* Returns true if BB contains an user provided predictor
1219 : (PRED_HOT_LABEL/PRED_COLD_LABEL). */
1220 :
1221 : static bool
1222 7451 : contains_hot_cold_predict (basic_block bb)
1223 : {
1224 7451 : gimple_stmt_iterator gsi;
1225 7451 : gsi = gsi_start_nondebug_after_labels_bb (bb);
1226 10023 : for (; !gsi_end_p (gsi); gsi_next_nondebug (&gsi))
1227 : {
1228 2573 : gimple *s = gsi_stmt (gsi);
1229 2573 : if (gimple_code (s) != GIMPLE_PREDICT)
1230 1 : continue;
1231 2572 : auto predict = gimple_predict_predictor (s);
1232 2572 : if (predict == PRED_HOT_LABEL
1233 2572 : || predict == PRED_COLD_LABEL)
1234 : return true;
1235 : }
1236 : return false;
1237 : }
1238 :
1239 : /* The function match_simplify_replacement does the main work of doing the
1240 : replacement using match and simplify. Return true if the replacement is done.
1241 : Otherwise return false.
1242 : BB is the basic block where the replacement is going to be done on. ARG0
1243 : is argument 0 from PHI. Likewise for ARG1. */
1244 :
1245 : static bool
1246 862666 : match_simplify_replacement (basic_block cond_bb, basic_block middle_bb,
1247 : basic_block middle_bb_alt,
1248 : edge e0, edge e1, gphi *phi,
1249 : tree arg0, tree arg1, bool early_p,
1250 : bool threeway_p)
1251 : {
1252 862666 : gimple *stmt;
1253 862666 : gimple_stmt_iterator gsi;
1254 862666 : edge true_edge, false_edge;
1255 862666 : gimple_seq seq = NULL;
1256 862666 : tree result;
1257 862666 : gimple *stmt_to_move = NULL;
1258 862666 : gimple *stmt_to_move_alt = NULL;
1259 862666 : tree arg_true, arg_false;
1260 :
1261 : /* Special case A ? B : B as this will always simplify to B. */
1262 862666 : if (operand_equal_for_phi_arg_p (arg0, arg1))
1263 : return false;
1264 :
1265 : /* If the basic block only has a cheap preparation statement,
1266 : allow it and move it once the transformation is done. */
1267 862666 : if (!empty_bb_or_one_feeding_into_p (middle_bb, phi, stmt_to_move))
1268 : return false;
1269 :
1270 565650 : if (threeway_p
1271 565650 : && middle_bb != middle_bb_alt
1272 565650 : && !empty_bb_or_one_feeding_into_p (middle_bb_alt, phi,
1273 : stmt_to_move_alt))
1274 : return false;
1275 :
1276 : /* Do not make conditional undefs unconditional. */
1277 551924 : if ((TREE_CODE (arg0) == SSA_NAME
1278 281110 : && ssa_name_maybe_undef_p (arg0))
1279 832685 : || (TREE_CODE (arg1) == SSA_NAME
1280 243617 : && ssa_name_maybe_undef_p (arg1)))
1281 : return false;
1282 :
1283 : /* At this point we know we have a GIMPLE_COND with two successors.
1284 : One successor is BB, the other successor is an empty block which
1285 : falls through into BB.
1286 :
1287 : There is a single PHI node at the join point (BB).
1288 :
1289 : So, given the condition COND, and the two PHI arguments, match and simplify
1290 : can happen on (COND) ? arg0 : arg1. */
1291 :
1292 547258 : stmt = last_nondebug_stmt (cond_bb);
1293 :
1294 : /* We need to know which is the true edge and which is the false
1295 : edge so that we know when to invert the condition below. */
1296 547258 : extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
1297 :
1298 : /* Forward the edges over the middle basic block. */
1299 547258 : if (true_edge->dest == middle_bb)
1300 354884 : true_edge = EDGE_SUCC (true_edge->dest, 0);
1301 547258 : if (false_edge->dest == middle_bb)
1302 192374 : false_edge = EDGE_SUCC (false_edge->dest, 0);
1303 :
1304 : /* When THREEWAY_P then e1 will point to the edge of the final transition
1305 : from middle-bb to end. */
1306 547258 : if (true_edge == e0)
1307 : {
1308 354884 : if (!threeway_p)
1309 338355 : gcc_assert (false_edge == e1);
1310 : arg_true = arg0;
1311 : arg_false = arg1;
1312 : }
1313 : else
1314 : {
1315 192374 : gcc_assert (false_edge == e0);
1316 192374 : if (!threeway_p)
1317 191898 : gcc_assert (true_edge == e1);
1318 : arg_true = arg1;
1319 : arg_false = arg0;
1320 : }
1321 :
1322 547258 : tree type = TREE_TYPE (gimple_phi_result (phi));
1323 547258 : {
1324 547258 : auto_flow_sensitive s1(stmt_to_move);
1325 547258 : auto_flow_sensitive s_alt(stmt_to_move_alt);
1326 :
1327 547258 : result = gimple_simplify_phiopt (early_p, type, stmt,
1328 : arg_true, arg_false,
1329 : &seq);
1330 547258 : }
1331 :
1332 : /* For early phiopt, we don't want to lose user generated predictors
1333 : if the phiopt is converting `if (a)` into `a` as that might
1334 : be jump threaded later on so we want to keep around the
1335 : predictors. */
1336 547258 : if (early_p && result && TREE_CODE (result) == SSA_NAME)
1337 : {
1338 33098 : bool check_it = false;
1339 33098 : tree cmp0 = gimple_cond_lhs (stmt);
1340 33098 : tree cmp1 = gimple_cond_rhs (stmt);
1341 33098 : if (result == cmp0 || result == cmp1)
1342 : check_it = true;
1343 26035 : else if (gimple_seq_singleton_p (seq))
1344 : {
1345 25838 : auto stmt = dyn_cast<gassign *> (gimple_seq_first_stmt (seq));
1346 25838 : if (stmt
1347 25838 : && result == gimple_assign_lhs (stmt)
1348 25838 : && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
1349 : == tcc_comparison)
1350 : check_it = true;
1351 : }
1352 : if (!check_it)
1353 : ;
1354 7063 : else if (contains_hot_cold_predict (middle_bb))
1355 : return false;
1356 7062 : else if (threeway_p
1357 : && middle_bb != middle_bb_alt
1358 7062 : && contains_hot_cold_predict (middle_bb_alt))
1359 : return false;
1360 : }
1361 :
1362 547236 : if (!result)
1363 : {
1364 : /* If we don't get back a MIN/MAX_EXPR still make sure the expression
1365 : stays in a form to be recognized by ISA that map to IEEE x > y ? x : y
1366 : semantics (that's not IEEE max semantics). */
1367 454430 : if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
1368 : return false;
1369 17915 : if (stmt_to_move || stmt_to_move_alt)
1370 : return false;
1371 14948 : tree_code cmp = gimple_cond_code (stmt);
1372 14948 : if (cmp != LT_EXPR && cmp != LE_EXPR
1373 14948 : && cmp != GT_EXPR && cmp != GE_EXPR)
1374 : return false;
1375 7189 : tree lhs = gimple_cond_lhs (stmt);
1376 7189 : tree rhs = gimple_cond_rhs (stmt);
1377 : /* `lhs CMP rhs ? lhs : rhs` or `lhs CMP rhs ? rhs : lhs`
1378 : are only acceptable case here. */
1379 7189 : if ((!operand_equal_for_phi_arg_p (lhs, arg_false)
1380 2732 : || !operand_equal_for_phi_arg_p (rhs, arg_true))
1381 7268 : && (!operand_equal_for_phi_arg_p (rhs, arg_false)
1382 1680 : || !operand_equal_for_phi_arg_p (lhs, arg_true)))
1383 : return false;
1384 4146 : seq = nullptr;
1385 4146 : result = gimple_build (&seq, cmp, boolean_type_node, lhs, rhs);
1386 4146 : result = gimple_build (&seq, COND_EXPR, type, result,
1387 : arg_true, arg_false);
1388 4146 : statistics_counter_event (cfun, "Non-IEEE FP MIN/MAX PHI replacement",
1389 : 1);
1390 : }
1391 :
1392 : // Make sure the sequence does not reference both basic blocks.
1393 96973 : if (threeway_p && middle_bb != middle_bb_alt
1394 96973 : && !check_uses (seq, middle_bb, middle_bb_alt))
1395 : {
1396 0 : if (dump_file && (dump_flags & TDF_FOLDING))
1397 0 : fprintf (dump_file, "rejected the phiopt match-simplify as it uses"
1398 : "both conditional statements.\n");
1399 0 : gimple_seq_discard (seq);
1400 0 : return false;
1401 : }
1402 :
1403 96973 : if (dump_file && (dump_flags & TDF_FOLDING))
1404 1 : fprintf (dump_file, "accepted the phiopt match-simplify.\n");
1405 :
1406 96973 : auto_bitmap exprs_maybe_dce;
1407 :
1408 : /* Mark the cond statements' lhs/rhs as maybe dce. */
1409 96973 : if (TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
1410 96973 : && !SSA_NAME_IS_DEFAULT_DEF (gimple_cond_lhs (stmt)))
1411 91330 : bitmap_set_bit (exprs_maybe_dce,
1412 91330 : SSA_NAME_VERSION (gimple_cond_lhs (stmt)));
1413 96973 : if (TREE_CODE (gimple_cond_rhs (stmt)) == SSA_NAME
1414 96973 : && !SSA_NAME_IS_DEFAULT_DEF (gimple_cond_rhs (stmt)))
1415 28261 : bitmap_set_bit (exprs_maybe_dce,
1416 28261 : SSA_NAME_VERSION (gimple_cond_rhs (stmt)));
1417 96973 : if (TREE_CODE (arg_true) == SSA_NAME
1418 96973 : && !SSA_NAME_IS_DEFAULT_DEF (arg_true))
1419 22512 : bitmap_set_bit (exprs_maybe_dce, SSA_NAME_VERSION (arg_true));
1420 96973 : if (TREE_CODE (arg_false) == SSA_NAME
1421 96973 : && !SSA_NAME_IS_DEFAULT_DEF (arg_false))
1422 35645 : bitmap_set_bit (exprs_maybe_dce, SSA_NAME_VERSION (arg_false));
1423 :
1424 96973 : gsi = gsi_last_bb (cond_bb);
1425 : /* Insert the sequence generated from gimple_simplify_phiopt. */
1426 96973 : if (seq)
1427 : {
1428 : // Mark the lhs of the new statements maybe for dce
1429 87186 : mark_lhs_in_seq_for_dce (exprs_maybe_dce, seq);
1430 87186 : gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING);
1431 : }
1432 :
1433 : /* If there was a statement to move, move it to right before
1434 : the original conditional. */
1435 96973 : move_stmt (stmt_to_move, &gsi, exprs_maybe_dce);
1436 96973 : move_stmt (stmt_to_move_alt, &gsi, exprs_maybe_dce);
1437 :
1438 96973 : replace_phi_edge_with_variable (cond_bb, e1, phi, result, exprs_maybe_dce);
1439 :
1440 : /* Add Statistic here even though replace_phi_edge_with_variable already
1441 : does it as we want to be able to count when match-simplify happens vs
1442 : the others. */
1443 96973 : statistics_counter_event (cfun, "match-simplify PHI replacement", 1);
1444 :
1445 : /* Note that we optimized this PHI. */
1446 96973 : return true;
1447 96973 : }
1448 :
1449 : /* The function comparison_combine tries to handle cases like:
1450 : if (a CMP0 b)
1451 : d = a CMP1 b;
1452 : PHI<d, [0,1]>
1453 : This has to be seperately as `a CMP1 b` might be trapping and
1454 : match_simplify_replacement does not handle trapping statements.
1455 : Returns true if a replacement happens. */
1456 :
1457 : static bool
1458 765693 : comparison_combine (basic_block cond_bb, basic_block middle_bb,
1459 : basic_block middle_bb_alt,
1460 : edge e0, edge e1, gphi *phi,
1461 : tree arg0, tree arg1, bool threeway_p)
1462 : {
1463 765693 : gcond *stmt;
1464 765693 : gimple_stmt_iterator gsi;
1465 765693 : edge true_edge, false_edge;
1466 765693 : tree arg_true, arg_false;
1467 :
1468 765693 : if (!types_compatible_p (boolean_type_node, TREE_TYPE (arg0)))
1469 : return false;
1470 :
1471 : /* Do not make conditional undefs unconditional. */
1472 111189 : if ((TREE_CODE (arg0) == SSA_NAME
1473 78843 : && ssa_name_maybe_undef_p (arg0))
1474 190032 : || (TREE_CODE (arg1) == SSA_NAME
1475 12225 : && ssa_name_maybe_undef_p (arg1)))
1476 : return false;
1477 :
1478 111170 : stmt = as_a<gcond*>(last_nondebug_stmt (cond_bb));
1479 :
1480 : // Handle only floating point types as they only trap.
1481 : // The match and simplify will handle the non-trapping case.
1482 111170 : if (!FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt))))
1483 : return false;
1484 :
1485 : /* Needs to be PHI<[1,0],arg1> PHI<arg0,[1,0]>. */
1486 5162 : if (((!integer_onep (arg0) && !integer_zerop (arg0))
1487 1274 : || TREE_CODE (arg1) != SSA_NAME)
1488 5322 : && ((!integer_onep (arg1) && !integer_zerop (arg1))
1489 2510 : || TREE_CODE (arg0) != SSA_NAME))
1490 : return false;
1491 :
1492 1272 : gassign *other_cmp = nullptr;
1493 1272 : if (!one_feeding_comparison_into_p (middle_bb, phi, other_cmp))
1494 : {
1495 812 : if (!threeway_p || middle_bb == middle_bb_alt)
1496 : return false;
1497 12 : if (!empty_block_p (middle_bb))
1498 : return false;
1499 12 : if (!one_feeding_comparison_into_p (middle_bb_alt, phi, other_cmp))
1500 : return false;
1501 : }
1502 460 : else if (threeway_p
1503 460 : && middle_bb != middle_bb_alt
1504 460 : && !empty_block_p (middle_bb_alt))
1505 : return false;
1506 :
1507 : /* We need to know which is the true edge and which is the false
1508 : edge so that we know when to invert the condition below. */
1509 462 : extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
1510 :
1511 : /* Forward the edges over the middle basic block. */
1512 462 : if (true_edge->dest == middle_bb)
1513 340 : true_edge = EDGE_SUCC (true_edge->dest, 0);
1514 462 : if (false_edge->dest == middle_bb)
1515 122 : false_edge = EDGE_SUCC (false_edge->dest, 0);
1516 : /* When THREEWAY_P then e1 will point to the edge of the final transition
1517 : from middle-bb to end. */
1518 462 : if (true_edge == e0)
1519 : {
1520 340 : if (!threeway_p)
1521 338 : gcc_assert (false_edge == e1);
1522 : arg_true = arg0;
1523 : arg_false = arg1;
1524 : }
1525 : else
1526 : {
1527 122 : gcc_assert (false_edge == e0);
1528 122 : if (!threeway_p)
1529 122 : gcc_assert (true_edge == e1);
1530 : arg_true = arg1;
1531 : arg_false = arg0;
1532 : }
1533 462 : if (TREE_CODE (arg_true) == SSA_NAME
1534 462 : && arg_true != gimple_assign_lhs (other_cmp))
1535 : return false;
1536 462 : if (TREE_CODE (arg_false) == SSA_NAME
1537 462 : && arg_false != gimple_assign_lhs (other_cmp))
1538 : return false;
1539 :
1540 462 : tree larg = gimple_cond_lhs (stmt);
1541 462 : tree rarg = gimple_cond_rhs (stmt);
1542 462 : if (!operand_equal_p (larg, gimple_assign_rhs1 (other_cmp))
1543 785 : || !operand_equal_p (rarg, gimple_assign_rhs2 (other_cmp)))
1544 : return false;
1545 :
1546 171 : tree_code logical;
1547 : // a CMP0 b ? 1 : a CMP1 b -> `a CMP0 b || a CMP1 b`
1548 : // a CMP0 b ? a CMP1 b : 1 -> `!(a CMP0 b) || a CMP1 b`
1549 :
1550 : // a CMP0 b ? a CMP1 b : 0 -> `a CMP0 b && a CMP1 b`
1551 : // a CMP0 b ? 0 : a CMP1 b -> `!(a CMP0 b) && a CMP1 b`
1552 171 : if (integer_onep (arg_true) || integer_onep (arg_false))
1553 : logical = TRUTH_ORIF_EXPR;
1554 : else
1555 : logical = TRUTH_ANDIF_EXPR;
1556 171 : tree_code outer_code = gimple_cond_code (stmt);
1557 171 : tree_code inner_code = gimple_assign_rhs_code (other_cmp);
1558 : // Invert the outter if needed.
1559 171 : if (integer_onep (arg_false) || integer_zerop (arg_true))
1560 : {
1561 0 : outer_code = invert_tree_comparison (outer_code,
1562 : HONOR_NANS (larg));
1563 : // In theory could handle it as !((a CMP0 b) LOGICAL' !(a CMP1 b))
1564 : // Most likely the outer comparison will be EQ/NE which is invertable.
1565 0 : if (outer_code == ERROR_MARK)
1566 : return false;
1567 : }
1568 171 : tree result;
1569 171 : tree_code newcmp_code;
1570 171 : newcmp_code = combine_comparisons (logical, outer_code, inner_code,
1571 : boolean_type_node, boolean_type_node,
1572 : HONOR_NANS (larg), &result);
1573 171 : if (newcmp_code == ERROR_MARK)
1574 : return false;
1575 16 : gimple_seq seq = nullptr;
1576 16 : if (newcmp_code != INTEGER_CST)
1577 16 : result = gimple_build (&seq, newcmp_code, boolean_type_node,
1578 : larg, rarg);
1579 16 : gsi = gsi_last_bb (cond_bb);
1580 16 : gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING);
1581 16 : replace_phi_edge_with_variable (cond_bb, e1, phi, result);
1582 16 : return true;
1583 : }
1584 :
1585 : /* Update *ARG which is defined in STMT so that it contains the
1586 : computed value if that seems profitable. Return true if the
1587 : statement is made dead by that rewriting. */
1588 :
1589 : static bool
1590 339613 : jump_function_from_stmt (tree *arg, gimple *stmt)
1591 : {
1592 339613 : enum tree_code code = gimple_assign_rhs_code (stmt);
1593 339613 : if (code == ADDR_EXPR)
1594 : {
1595 : /* For arg = &p->i transform it to p, if possible. */
1596 4988 : tree rhs1 = gimple_assign_rhs1 (stmt);
1597 4988 : poly_int64 offset;
1598 4988 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs1, 0),
1599 : &offset);
1600 4988 : if (tem
1601 4900 : && TREE_CODE (tem) == MEM_REF
1602 9888 : && known_eq (mem_ref_offset (tem) + offset, 0))
1603 : {
1604 2114 : *arg = TREE_OPERAND (tem, 0);
1605 2114 : return true;
1606 : }
1607 : }
1608 : /* TODO: Much like IPA-CP jump-functions we want to handle constant
1609 : additions symbolically here, and we'd need to update the comparison
1610 : code that compares the arg + cst tuples in our caller. For now the
1611 : code above exactly handles the VEC_BASE pattern from vec.h. */
1612 : return false;
1613 : }
1614 :
1615 : /* RHS is a source argument in a BIT_AND_EXPR or BIT_IOR_EXPR which feeds
1616 : a conditional of the form SSA_NAME NE 0.
1617 :
1618 : If RHS is fed by a simple EQ_EXPR or NE_EXPR comparison of two values,
1619 : see if the two input values of the comparison match arg0 and arg1.
1620 :
1621 : If so update *code and return TRUE. Otherwise return FALSE. */
1622 :
1623 : static bool
1624 169186 : rhs_is_fed_for_value_replacement (const_tree arg0, const_tree arg1,
1625 : enum tree_code *code, const_tree rhs,
1626 : enum tree_code bit_expression_code)
1627 : {
1628 : /* Obviously if RHS is not an SSA_NAME, we can't look at the defining
1629 : statement. */
1630 169186 : if (TREE_CODE (rhs) == SSA_NAME)
1631 : {
1632 132788 : auto *def1 = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (rhs));
1633 :
1634 : /* Verify the defining statement has an EQ_EXPR or NE_EXPR on the RHS. */
1635 122163 : if (def1
1636 122163 : && ((bit_expression_code == BIT_AND_EXPR
1637 82713 : && gimple_assign_rhs_code (def1) == EQ_EXPR)
1638 97195 : || (bit_expression_code == BIT_IOR_EXPR
1639 39450 : && gimple_assign_rhs_code (def1) == NE_EXPR)))
1640 : {
1641 : /* Finally verify the source operands of the EQ_EXPR or NE_EXPR
1642 : are equal to arg0 and arg1. */
1643 30639 : tree op0 = gimple_assign_rhs1 (def1);
1644 30639 : tree op1 = gimple_assign_rhs2 (def1);
1645 30639 : if ((operand_equal_for_phi_arg_p (arg0, op0)
1646 718 : && operand_equal_for_phi_arg_p (arg1, op1))
1647 31254 : || (operand_equal_for_phi_arg_p (arg0, op1)
1648 663 : && operand_equal_for_phi_arg_p (arg1, op0)))
1649 : {
1650 : /* We will perform the optimization. */
1651 534 : *code = gimple_assign_rhs_code (def1);
1652 534 : return true;
1653 : }
1654 : }
1655 : }
1656 : return false;
1657 : }
1658 :
1659 : /* Return TRUE if arg0/arg1 are equal to the rhs/lhs or lhs/rhs of COND.
1660 :
1661 : Also return TRUE if arg0/arg1 are equal to the source arguments of an
1662 : EQ comparison feeding a BIT_AND_EXPR, or NE comparison feeding a
1663 : BIT_IOR_EXPR which feeds COND.
1664 :
1665 : Return FALSE otherwise. */
1666 :
1667 : static bool
1668 687612 : operand_equal_for_value_replacement (const_tree arg0, const_tree arg1,
1669 : enum tree_code *code, gimple *cond)
1670 : {
1671 687612 : tree lhs = gimple_cond_lhs (cond);
1672 687612 : tree rhs = gimple_cond_rhs (cond);
1673 :
1674 687612 : if ((operand_equal_for_phi_arg_p (arg0, lhs)
1675 27895 : && operand_equal_for_phi_arg_p (arg1, rhs))
1676 706090 : || (operand_equal_for_phi_arg_p (arg1, lhs)
1677 48586 : && operand_equal_for_phi_arg_p (arg0, rhs)))
1678 : return true;
1679 :
1680 : /* Now handle more complex case where we have an EQ comparison
1681 : feeding a BIT_AND_EXPR, or a NE comparison feeding a BIT_IOR_EXPR,
1682 : which then feeds into COND.
1683 :
1684 : First verify that COND is of the form SSA_NAME NE 0. */
1685 394802 : if (*code != NE_EXPR || !integer_zerop (rhs)
1686 962353 : || TREE_CODE (lhs) != SSA_NAME)
1687 : return false;
1688 :
1689 : /* Now ensure that SSA_NAME is set by a BIT_AND_EXPR or BIT_OR_EXPR. */
1690 287491 : auto def = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (lhs));
1691 203838 : if (!def
1692 203838 : || (gimple_assign_rhs_code (def) != BIT_AND_EXPR
1693 139824 : && gimple_assign_rhs_code (def) != BIT_IOR_EXPR))
1694 : return false;
1695 :
1696 : /* Now verify arg0/arg1 correspond to the source arguments of an EQ
1697 : comparison feeding the BIT_AND_EXPR or a NE comparison feeding the
1698 : BIT_IOR_EXPR. */
1699 :
1700 84786 : tree tmp = gimple_assign_rhs1 (def);
1701 84786 : if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp,
1702 : gimple_assign_rhs_code (def)))
1703 : return true;
1704 :
1705 84400 : tmp = gimple_assign_rhs2 (def);
1706 84400 : if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp,
1707 : gimple_assign_rhs_code (def)))
1708 : return true;
1709 :
1710 : return false;
1711 : }
1712 :
1713 : /* Returns true if ARG is a neutral element for operation CODE
1714 : on the RIGHT side. */
1715 :
1716 : static bool
1717 1099 : neutral_element_p (tree_code code, tree arg, bool right)
1718 : {
1719 1099 : switch (code)
1720 : {
1721 33 : case PLUS_EXPR:
1722 33 : case BIT_IOR_EXPR:
1723 33 : case BIT_XOR_EXPR:
1724 33 : return integer_zerop (arg);
1725 :
1726 195 : case LROTATE_EXPR:
1727 195 : case RROTATE_EXPR:
1728 195 : case LSHIFT_EXPR:
1729 195 : case RSHIFT_EXPR:
1730 195 : case MINUS_EXPR:
1731 195 : case POINTER_PLUS_EXPR:
1732 195 : return right && integer_zerop (arg);
1733 :
1734 693 : case MULT_EXPR:
1735 693 : return integer_onep (arg);
1736 :
1737 34 : case TRUNC_DIV_EXPR:
1738 34 : case CEIL_DIV_EXPR:
1739 34 : case FLOOR_DIV_EXPR:
1740 34 : case ROUND_DIV_EXPR:
1741 34 : case EXACT_DIV_EXPR:
1742 34 : return right && integer_onep (arg);
1743 :
1744 0 : case BIT_AND_EXPR:
1745 0 : return integer_all_onesp (arg);
1746 :
1747 : default:
1748 : return false;
1749 : }
1750 : }
1751 :
1752 : /* Returns true if ARG is an absorbing element for operation CODE. */
1753 :
1754 : static bool
1755 882 : absorbing_element_p (tree_code code, tree arg, bool right, tree rval)
1756 : {
1757 882 : switch (code)
1758 : {
1759 18 : case BIT_IOR_EXPR:
1760 18 : return integer_all_onesp (arg);
1761 :
1762 47 : case MULT_EXPR:
1763 47 : case BIT_AND_EXPR:
1764 47 : return integer_zerop (arg);
1765 :
1766 3 : case LSHIFT_EXPR:
1767 3 : case RSHIFT_EXPR:
1768 3 : case LROTATE_EXPR:
1769 3 : case RROTATE_EXPR:
1770 3 : return !right && integer_zerop (arg);
1771 :
1772 166 : case TRUNC_DIV_EXPR:
1773 166 : case CEIL_DIV_EXPR:
1774 166 : case FLOOR_DIV_EXPR:
1775 166 : case ROUND_DIV_EXPR:
1776 166 : case EXACT_DIV_EXPR:
1777 166 : case TRUNC_MOD_EXPR:
1778 166 : case CEIL_MOD_EXPR:
1779 166 : case FLOOR_MOD_EXPR:
1780 166 : case ROUND_MOD_EXPR:
1781 166 : return (!right
1782 8 : && integer_zerop (arg)
1783 174 : && tree_single_nonzero_p (rval));
1784 :
1785 : default:
1786 : return false;
1787 : }
1788 : }
1789 :
1790 : /* The function value_replacement does the main work of doing the value
1791 : replacement. Return non-zero if the replacement is done. Otherwise return
1792 : 0. If we remove the middle basic block, return 2.
1793 : BB is the basic block where the replacement is going to be done on. ARG0
1794 : is argument 0 from the PHI. Likewise for ARG1. */
1795 :
1796 : static int
1797 2504449 : value_replacement (basic_block cond_bb, basic_block middle_bb,
1798 : edge e0, edge e1, gphi *phi, tree arg0, tree arg1)
1799 : {
1800 2504449 : gimple_stmt_iterator gsi;
1801 2504449 : edge true_edge, false_edge;
1802 2504449 : enum tree_code code;
1803 2504449 : bool empty_or_with_defined_p = true;
1804 :
1805 : /* Virtual operands don't need to be handled. */
1806 4463243 : if (virtual_operand_p (arg1))
1807 : return 0;
1808 :
1809 : /* Special case A ? B : B as this will always simplify to B. */
1810 1333145 : if (operand_equal_for_phi_arg_p (arg0, arg1))
1811 : return 0;
1812 :
1813 2367994 : gcond *cond = as_a <gcond *> (*gsi_last_bb (cond_bb));
1814 1183997 : code = gimple_cond_code (cond);
1815 :
1816 : /* This transformation is only valid for equality comparisons. */
1817 1183997 : if (code != NE_EXPR && code != EQ_EXPR)
1818 : return 0;
1819 :
1820 : /* Do not make conditional undefs unconditional. */
1821 719359 : if ((TREE_CODE (arg0) == SSA_NAME
1822 553521 : && ssa_name_maybe_undef_p (arg0))
1823 1270384 : || (TREE_CODE (arg1) == SSA_NAME
1824 312172 : && ssa_name_maybe_undef_p (arg1)))
1825 : return false;
1826 :
1827 : /* If the type says honor signed zeros we cannot do this
1828 : optimization. */
1829 701278 : if (HONOR_SIGNED_ZEROS (arg1))
1830 : return 0;
1831 :
1832 : /* If there is a statement in MIDDLE_BB that defines one of the PHI
1833 : arguments, then adjust arg0 or arg1. */
1834 687612 : gsi = gsi_start_nondebug_after_labels_bb (middle_bb);
1835 2216732 : while (!gsi_end_p (gsi))
1836 : {
1837 1529120 : gimple *stmt = gsi_stmt (gsi);
1838 1529120 : tree lhs;
1839 1529120 : gsi_next_nondebug (&gsi);
1840 1529120 : if (!is_gimple_assign (stmt))
1841 : {
1842 211000 : if (gimple_code (stmt) != GIMPLE_PREDICT
1843 211000 : && gimple_code (stmt) != GIMPLE_NOP)
1844 : empty_or_with_defined_p = false;
1845 211000 : continue;
1846 : }
1847 : /* Now try to adjust arg0 or arg1 according to the computation
1848 : in the statement. */
1849 1318120 : lhs = gimple_assign_lhs (stmt);
1850 339613 : if (!(lhs == arg0
1851 339613 : && jump_function_from_stmt (&arg0, stmt))
1852 1320234 : || (lhs == arg1
1853 0 : && jump_function_from_stmt (&arg1, stmt)))
1854 1529120 : empty_or_with_defined_p = false;
1855 : }
1856 :
1857 : /* The middle bb is not empty if there are any phi nodes. */
1858 687612 : if (phi_nodes (middle_bb))
1859 81452 : empty_or_with_defined_p = false;
1860 :
1861 : /* We need to know which is the true edge and which is the false
1862 : edge so that we know if have abs or negative abs. */
1863 687612 : extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
1864 :
1865 : /* At this point we know we have a COND_EXPR with two successors.
1866 : One successor is BB, the other successor is an empty block which
1867 : falls through into BB.
1868 :
1869 : The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
1870 :
1871 : There is a single PHI node at the join point (BB) with two arguments.
1872 :
1873 : We now need to verify that the two arguments in the PHI node match
1874 : the two arguments to the equality comparison. */
1875 :
1876 687612 : bool equal_p = operand_equal_for_value_replacement (arg0, arg1, &code, cond);
1877 687612 : bool maybe_equal_p = false;
1878 687612 : if (!equal_p
1879 687612 : && empty_or_with_defined_p
1880 191769 : && TREE_CODE (gimple_cond_rhs (cond)) == INTEGER_CST
1881 838545 : && (operand_equal_for_phi_arg_p (gimple_cond_lhs (cond), arg0)
1882 150933 : ? TREE_CODE (arg1) == INTEGER_CST
1883 143047 : : (operand_equal_for_phi_arg_p (gimple_cond_lhs (cond), arg1)
1884 23673 : && TREE_CODE (arg0) == INTEGER_CST)))
1885 : maybe_equal_p = true;
1886 667351 : if (equal_p || maybe_equal_p)
1887 : {
1888 33967 : edge e;
1889 33967 : tree arg;
1890 :
1891 : /* For NE_EXPR, we want to build an assignment result = arg where
1892 : arg is the PHI argument associated with the true edge. For
1893 : EQ_EXPR we want the PHI argument associated with the false edge. */
1894 33967 : e = (code == NE_EXPR ? true_edge : false_edge);
1895 :
1896 : /* Unfortunately, E may not reach BB (it may instead have gone to
1897 : OTHER_BLOCK). If that is the case, then we want the single outgoing
1898 : edge from OTHER_BLOCK which reaches BB and represents the desired
1899 : path from COND_BLOCK. */
1900 33967 : if (e->dest == middle_bb)
1901 13721 : e = single_succ_edge (e->dest);
1902 :
1903 : /* Now we know the incoming edge to BB that has the argument for the
1904 : RHS of our new assignment statement. */
1905 33967 : if (e0 == e)
1906 : arg = arg0;
1907 : else
1908 20246 : arg = arg1;
1909 :
1910 : /* If the middle basic block was empty or is defining the
1911 : PHI arguments and this is a single phi where the args are different
1912 : for the edges e0 and e1 then we can remove the middle basic block. */
1913 33967 : if (empty_or_with_defined_p
1914 33967 : && single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)),
1915 : e0, e1) == phi)
1916 : {
1917 20293 : use_operand_p use_p;
1918 20293 : gimple *use_stmt;
1919 :
1920 : /* Even if arg0/arg1 isn't equal to second operand of cond, we
1921 : can optimize away the bb if we can prove it doesn't care whether
1922 : phi result is arg0/arg1 or second operand of cond. Consider:
1923 : <bb 2> [local count: 118111600]:
1924 : if (i_2(D) == 4)
1925 : goto <bb 4>; [97.00%]
1926 : else
1927 : goto <bb 3>; [3.00%]
1928 :
1929 : <bb 3> [local count: 3540129]:
1930 :
1931 : <bb 4> [local count: 118111600]:
1932 : # i_6 = PHI <i_2(D)(3), 6(2)>
1933 : _3 = i_6 != 0;
1934 : Here, carg is 4, oarg is 6, crhs is 0, and because
1935 : (4 != 0) == (6 != 0), we don't care if i_6 is 4 or 6, both
1936 : have the same outcome. So, we can optimize this to:
1937 : _3 = i_2(D) != 0;
1938 : If the single imm use of phi result >, >=, < or <=, similarly
1939 : we can check if both carg and oarg compare the same against
1940 : crhs using ccode. */
1941 20293 : if (maybe_equal_p
1942 18533 : && TREE_CODE (arg) != INTEGER_CST
1943 38811 : && single_imm_use (gimple_phi_result (phi), &use_p, &use_stmt))
1944 : {
1945 10248 : enum tree_code ccode = ERROR_MARK;
1946 10248 : tree clhs = NULL_TREE, crhs = NULL_TREE;
1947 10248 : tree carg = gimple_cond_rhs (cond);
1948 10248 : tree oarg = e0 == e ? arg1 : arg0;
1949 10248 : if (is_gimple_assign (use_stmt)
1950 10248 : && (TREE_CODE_CLASS (gimple_assign_rhs_code (use_stmt))
1951 : == tcc_comparison))
1952 : {
1953 57 : ccode = gimple_assign_rhs_code (use_stmt);
1954 57 : clhs = gimple_assign_rhs1 (use_stmt);
1955 57 : crhs = gimple_assign_rhs2 (use_stmt);
1956 : }
1957 10191 : else if (gimple_code (use_stmt) == GIMPLE_COND)
1958 : {
1959 61 : ccode = gimple_cond_code (use_stmt);
1960 61 : clhs = gimple_cond_lhs (use_stmt);
1961 61 : crhs = gimple_cond_rhs (use_stmt);
1962 : }
1963 118 : if (ccode != ERROR_MARK
1964 118 : && clhs == gimple_phi_result (phi)
1965 207 : && TREE_CODE (crhs) == INTEGER_CST)
1966 45 : switch (ccode)
1967 : {
1968 26 : case EQ_EXPR:
1969 26 : case NE_EXPR:
1970 26 : if (!tree_int_cst_equal (crhs, carg)
1971 26 : && !tree_int_cst_equal (crhs, oarg))
1972 : equal_p = true;
1973 : break;
1974 2 : case GT_EXPR:
1975 4 : if (tree_int_cst_lt (crhs, carg)
1976 2 : == tree_int_cst_lt (crhs, oarg))
1977 : equal_p = true;
1978 : break;
1979 0 : case GE_EXPR:
1980 0 : if (tree_int_cst_le (crhs, carg)
1981 0 : == tree_int_cst_le (crhs, oarg))
1982 : equal_p = true;
1983 : break;
1984 13 : case LT_EXPR:
1985 26 : if (tree_int_cst_lt (carg, crhs)
1986 13 : == tree_int_cst_lt (oarg, crhs))
1987 : equal_p = true;
1988 : break;
1989 4 : case LE_EXPR:
1990 8 : if (tree_int_cst_le (carg, crhs)
1991 4 : == tree_int_cst_le (oarg, crhs))
1992 : equal_p = true;
1993 : break;
1994 : default:
1995 : break;
1996 : }
1997 10232 : if (equal_p)
1998 : {
1999 16 : tree phires = gimple_phi_result (phi);
2000 16 : if (SSA_NAME_RANGE_INFO (phires))
2001 : {
2002 : /* After the optimization PHI result can have value
2003 : which it couldn't have previously. */
2004 15 : value_range r (TREE_TYPE (phires));
2005 15 : if (get_global_range_query ()->range_of_expr (r, phires,
2006 : phi))
2007 : {
2008 15 : value_range tmp (carg, carg);
2009 15 : r.union_ (tmp);
2010 15 : reset_flow_sensitive_info (phires);
2011 15 : set_range_info (phires, r);
2012 15 : }
2013 : else
2014 0 : reset_flow_sensitive_info (phires);
2015 15 : }
2016 : }
2017 16 : if (equal_p && MAY_HAVE_DEBUG_BIND_STMTS)
2018 : {
2019 13 : imm_use_iterator imm_iter;
2020 13 : tree phires = gimple_phi_result (phi);
2021 13 : tree temp = NULL_TREE;
2022 13 : bool reset_p = false;
2023 :
2024 : /* Add # DEBUG D#1 => arg != carg ? arg : oarg. */
2025 42 : FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, phires)
2026 : {
2027 30 : if (!is_gimple_debug (use_stmt))
2028 13 : continue;
2029 17 : if (temp == NULL_TREE)
2030 : {
2031 12 : if (!single_pred_p (middle_bb)
2032 12 : || EDGE_COUNT (gimple_bb (phi)->preds) != 2)
2033 : {
2034 : /* But only if middle_bb has a single
2035 : predecessor and phi bb has two, otherwise
2036 : we could use a SSA_NAME not usable in that
2037 : place or wrong-debug. */
2038 1 : reset_p = true;
2039 1 : break;
2040 : }
2041 11 : gimple_stmt_iterator gsi
2042 11 : = gsi_after_labels (gimple_bb (phi));
2043 11 : tree type = TREE_TYPE (phires);
2044 11 : temp = build_debug_expr_decl (type);
2045 11 : tree t = build2 (NE_EXPR, boolean_type_node,
2046 : arg, carg);
2047 11 : t = build3 (COND_EXPR, type, t, arg, oarg);
2048 11 : gimple *g = gimple_build_debug_bind (temp, t, phi);
2049 11 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2050 : }
2051 32 : FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
2052 16 : replace_exp (use_p, temp);
2053 16 : update_stmt (use_stmt);
2054 13 : }
2055 13 : if (reset_p)
2056 1 : reset_debug_uses (phi);
2057 : }
2058 : }
2059 10061 : if (equal_p)
2060 : {
2061 1776 : replace_phi_edge_with_variable (cond_bb, e1, phi, arg);
2062 : /* Note that we optimized this PHI. */
2063 1776 : return 2;
2064 : }
2065 : }
2066 13674 : else if (equal_p)
2067 : {
2068 11946 : if (!single_pred_p (middle_bb))
2069 : return 0;
2070 11109 : statistics_counter_event (cfun, "Replace PHI with "
2071 : "variable/value_replacement", 1);
2072 :
2073 : /* Replace the PHI arguments with arg. */
2074 11109 : SET_PHI_ARG_DEF (phi, e0->dest_idx, arg);
2075 11109 : SET_PHI_ARG_DEF (phi, e1->dest_idx, arg);
2076 11109 : if (dump_file && (dump_flags & TDF_DETAILS))
2077 : {
2078 0 : fprintf (dump_file, "PHI ");
2079 0 : print_generic_expr (dump_file, gimple_phi_result (phi));
2080 0 : fprintf (dump_file, " reduced for COND_EXPR in block %d to ",
2081 : cond_bb->index);
2082 0 : print_generic_expr (dump_file, arg);
2083 0 : fprintf (dump_file, ".\n");
2084 : }
2085 : return 1;
2086 : }
2087 : }
2088 :
2089 673890 : if (!single_pred_p (middle_bb))
2090 : return 0;
2091 :
2092 : /* Now optimize (x != 0) ? x + y : y to just x + y. */
2093 566962 : gsi = gsi_last_nondebug_bb (middle_bb);
2094 566962 : if (gsi_end_p (gsi))
2095 : return 0;
2096 :
2097 392557 : auto assign = dyn_cast<gassign *> (gsi_stmt (gsi));
2098 336368 : if (!assign
2099 336368 : || (!INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2100 107677 : && !POINTER_TYPE_P (TREE_TYPE (arg0))))
2101 : return 0;
2102 :
2103 333808 : if (gimple_assign_rhs_class (assign) != GIMPLE_BINARY_RHS)
2104 : {
2105 : /* If last stmt of the middle_bb is a conversion, handle it like
2106 : a preparation statement through constant evaluation with
2107 : checking for UB. */
2108 143131 : enum tree_code sc = gimple_assign_rhs_code (assign);
2109 143131 : if (CONVERT_EXPR_CODE_P (sc))
2110 : assign = NULL;
2111 : else
2112 : return 0;
2113 : }
2114 :
2115 : /* Punt if there are (degenerate) PHIs in middle_bb, there should not be. */
2116 214494 : if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
2117 : return 0;
2118 :
2119 : /* Allow up to 2 cheap preparation statements that prepare argument
2120 : for assign, e.g.:
2121 : if (y_4 != 0)
2122 : goto <bb 3>;
2123 : else
2124 : goto <bb 4>;
2125 : <bb 3>:
2126 : _1 = (int) y_4;
2127 : iftmp.0_6 = x_5(D) r<< _1;
2128 : <bb 4>:
2129 : # iftmp.0_2 = PHI <iftmp.0_6(3), x_5(D)(2)>
2130 : or:
2131 : if (y_3(D) == 0)
2132 : goto <bb 4>;
2133 : else
2134 : goto <bb 3>;
2135 : <bb 3>:
2136 : y_4 = y_3(D) & 31;
2137 : _1 = (int) y_4;
2138 : _6 = x_5(D) r<< _1;
2139 : <bb 4>:
2140 : # _2 = PHI <x_5(D)(2), _6(3)> */
2141 214494 : gassign *prep_stmt[2] = { NULL, NULL };
2142 214494 : int prep_cnt;
2143 214494 : for (prep_cnt = 0; ; prep_cnt++)
2144 : {
2145 277716 : if (prep_cnt || assign)
2146 253899 : gsi_prev_nondebug (&gsi);
2147 277716 : if (gsi_end_p (gsi))
2148 : break;
2149 :
2150 212013 : gimple *gs = gsi_stmt (gsi);
2151 212013 : if (gimple_code (gs) == GIMPLE_LABEL)
2152 : break;
2153 :
2154 211655 : auto g = dyn_cast<gassign *> (gsi_stmt (gsi));
2155 211655 : if (prep_cnt == 2 || !g)
2156 148433 : return 0;
2157 :
2158 187024 : tree lhs = gimple_assign_lhs (g);
2159 187024 : tree rhs1 = gimple_assign_rhs1 (g);
2160 187024 : use_operand_p use_p;
2161 187024 : gimple *use_stmt;
2162 187024 : if (TREE_CODE (lhs) != SSA_NAME
2163 179860 : || TREE_CODE (rhs1) != SSA_NAME
2164 115975 : || !INTEGRAL_TYPE_P (TREE_TYPE (lhs))
2165 112475 : || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2166 108555 : || !single_imm_use (lhs, &use_p, &use_stmt)
2167 294393 : || ((prep_cnt || assign)
2168 85053 : && use_stmt != (prep_cnt ? prep_stmt[prep_cnt - 1] : assign)))
2169 : return 0;
2170 99254 : switch (gimple_assign_rhs_code (g))
2171 : {
2172 : CASE_CONVERT:
2173 : break;
2174 21921 : case PLUS_EXPR:
2175 21921 : case BIT_AND_EXPR:
2176 21921 : case BIT_IOR_EXPR:
2177 21921 : case BIT_XOR_EXPR:
2178 43842 : if (TREE_CODE (gimple_assign_rhs2 (g)) != INTEGER_CST)
2179 : return 0;
2180 : break;
2181 : default:
2182 : return 0;
2183 : }
2184 63222 : prep_stmt[prep_cnt] = g;
2185 63222 : }
2186 :
2187 : /* Only transform if it removes the condition. */
2188 66061 : if (!single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)), e0, e1))
2189 : return 0;
2190 :
2191 : /* Size-wise, this is always profitable. */
2192 49708 : if (optimize_bb_for_speed_p (cond_bb)
2193 : /* The special case is useless if it has a low probability. */
2194 46875 : && profile_status_for_fn (cfun) != PROFILE_ABSENT
2195 55675 : && EDGE_PRED (middle_bb, 0)->probability < profile_probability::even ()
2196 : /* If assign is cheap, there is no point avoiding it. */
2197 58527 : && estimate_num_insns_seq (bb_seq (middle_bb), &eni_time_weights)
2198 8819 : >= 3 * estimate_num_insns (cond, &eni_time_weights))
2199 91 : return 0;
2200 :
2201 49617 : tree cond_lhs = gimple_cond_lhs (cond);
2202 49617 : tree cond_rhs = gimple_cond_rhs (cond);
2203 :
2204 : /* Propagate the cond_rhs constant through preparation stmts,
2205 : make sure UB isn't invoked while doing that. */
2206 51253 : for (int i = prep_cnt - 1; i >= 0; --i)
2207 : {
2208 13478 : gassign *g = prep_stmt[i];
2209 13478 : tree grhs1 = gimple_assign_rhs1 (g);
2210 13478 : if (!operand_equal_for_phi_arg_p (cond_lhs, grhs1))
2211 : return 0;
2212 7091 : cond_lhs = gimple_assign_lhs (g);
2213 7091 : cond_rhs = fold_convert (TREE_TYPE (grhs1), cond_rhs);
2214 7091 : if (TREE_CODE (cond_rhs) != INTEGER_CST
2215 7091 : || TREE_OVERFLOW (cond_rhs))
2216 : return 0;
2217 1636 : if (gimple_assign_rhs_class (g) == GIMPLE_BINARY_RHS)
2218 : {
2219 1122 : cond_rhs = int_const_binop (gimple_assign_rhs_code (g), cond_rhs,
2220 561 : gimple_assign_rhs2 (g));
2221 561 : if (TREE_OVERFLOW (cond_rhs))
2222 : return 0;
2223 : }
2224 1636 : cond_rhs = fold_convert (TREE_TYPE (cond_lhs), cond_rhs);
2225 1636 : if (TREE_CODE (cond_rhs) != INTEGER_CST
2226 1636 : || TREE_OVERFLOW (cond_rhs))
2227 : return 0;
2228 : }
2229 :
2230 37775 : tree lhs, rhs1, rhs2;
2231 37775 : enum tree_code code_def;
2232 37775 : if (assign)
2233 : {
2234 37507 : lhs = gimple_assign_lhs (assign);
2235 37507 : rhs1 = gimple_assign_rhs1 (assign);
2236 37507 : rhs2 = gimple_assign_rhs2 (assign);
2237 37507 : code_def = gimple_assign_rhs_code (assign);
2238 : }
2239 : else
2240 : {
2241 268 : gcc_assert (prep_cnt > 0);
2242 : lhs = cond_lhs;
2243 : rhs1 = NULL_TREE;
2244 : rhs2 = NULL_TREE;
2245 : code_def = ERROR_MARK;
2246 : }
2247 :
2248 23799 : if (((code == NE_EXPR && e1 == false_edge)
2249 17158 : || (code == EQ_EXPR && e1 == true_edge))
2250 23426 : && arg0 == lhs
2251 61201 : && ((assign == NULL
2252 267 : && operand_equal_for_phi_arg_p (arg1, cond_rhs))
2253 : || (assign
2254 23159 : && arg1 == rhs1
2255 15151 : && operand_equal_for_phi_arg_p (rhs2, cond_lhs)
2256 403 : && neutral_element_p (code_def, cond_rhs, true))
2257 23091 : || (assign
2258 23091 : && arg1 == rhs2
2259 1463 : && operand_equal_for_phi_arg_p (rhs1, cond_lhs)
2260 696 : && neutral_element_p (code_def, cond_rhs, false))
2261 : || (assign
2262 23091 : && operand_equal_for_phi_arg_p (arg1, cond_rhs)
2263 2848 : && ((operand_equal_for_phi_arg_p (rhs2, cond_lhs)
2264 193 : && absorbing_element_p (code_def, cond_rhs, true, rhs2))
2265 2847 : || (operand_equal_for_phi_arg_p (rhs1, cond_lhs)
2266 689 : && absorbing_element_p (code_def,
2267 : cond_rhs, false, rhs2))))))
2268 : {
2269 109 : gsi = gsi_for_stmt (cond);
2270 : /* Moving ASSIGN might change VR of lhs, e.g. when moving u_6
2271 : def-stmt in:
2272 : if (n_5 != 0)
2273 : goto <bb 3>;
2274 : else
2275 : goto <bb 4>;
2276 :
2277 : <bb 3>:
2278 : # RANGE [0, 4294967294]
2279 : u_6 = n_5 + 4294967295;
2280 :
2281 : <bb 4>:
2282 : # u_3 = PHI <u_6(3), 4294967295(2)> */
2283 109 : reset_flow_sensitive_info (lhs);
2284 109 : gimple_stmt_iterator gsi_from;
2285 297 : for (int i = prep_cnt - 1; i >= 0; --i)
2286 : {
2287 79 : tree plhs = gimple_assign_lhs (prep_stmt[i]);
2288 79 : reset_flow_sensitive_info (plhs);
2289 79 : gsi_from = gsi_for_stmt (prep_stmt[i]);
2290 79 : gsi_move_before (&gsi_from, &gsi);
2291 : }
2292 109 : if (assign)
2293 : {
2294 107 : gsi_from = gsi_for_stmt (assign);
2295 107 : gsi_move_before (&gsi_from, &gsi);
2296 : }
2297 109 : replace_phi_edge_with_variable (cond_bb, e1, phi, lhs);
2298 109 : return 2;
2299 : }
2300 :
2301 : return 0;
2302 : }
2303 :
2304 : /* Attempt to optimize (x <=> y) cmp 0 and similar comparisons.
2305 : For strong ordering <=> try to match something like:
2306 : <bb 2> : // cond3_bb (== cond2_bb)
2307 : if (x_4(D) != y_5(D))
2308 : goto <bb 3>; [INV]
2309 : else
2310 : goto <bb 6>; [INV]
2311 :
2312 : <bb 3> : // cond_bb
2313 : if (x_4(D) < y_5(D))
2314 : goto <bb 6>; [INV]
2315 : else
2316 : goto <bb 4>; [INV]
2317 :
2318 : <bb 4> : // middle_bb
2319 :
2320 : <bb 6> : // phi_bb
2321 : # iftmp.0_2 = PHI <1(4), 0(2), -1(3)>
2322 : _1 = iftmp.0_2 == 0;
2323 :
2324 : and for partial ordering <=> something like:
2325 :
2326 : <bb 2> : // cond3_bb
2327 : if (a_3(D) == b_5(D))
2328 : goto <bb 6>; [50.00%]
2329 : else
2330 : goto <bb 3>; [50.00%]
2331 :
2332 : <bb 3> [local count: 536870913]: // cond2_bb
2333 : if (a_3(D) < b_5(D))
2334 : goto <bb 6>; [50.00%]
2335 : else
2336 : goto <bb 4>; [50.00%]
2337 :
2338 : <bb 4> [local count: 268435456]: // cond_bb
2339 : if (a_3(D) > b_5(D))
2340 : goto <bb 6>; [50.00%]
2341 : else
2342 : goto <bb 5>; [50.00%]
2343 :
2344 : <bb 5> [local count: 134217728]: // middle_bb
2345 :
2346 : <bb 6> [local count: 1073741824]: // phi_bb
2347 : # SR.27_4 = PHI <0(2), -1(3), 1(4), -128(5)>
2348 : _2 = SR.27_4 > 0; */
2349 :
2350 : static bool
2351 651553 : spaceship_replacement (basic_block cond_bb, basic_block middle_bb,
2352 : edge e0, edge e1, gphi *phi,
2353 : tree arg0, tree arg1)
2354 : {
2355 651553 : tree phires = gimple_phi_result (phi);
2356 1296155 : if (!INTEGRAL_TYPE_P (TREE_TYPE (phires))
2357 517771 : || TYPE_UNSIGNED (TREE_TYPE (phires))
2358 262781 : || !tree_fits_shwi_p (arg0)
2359 76022 : || !tree_fits_shwi_p (arg1)
2360 42182 : || (!IN_RANGE (tree_to_shwi (arg0), -1, 1)
2361 19554 : && tree_to_shwi (arg0) != -128)
2362 674674 : || (!IN_RANGE (tree_to_shwi (arg1), -1, 1)
2363 3021 : && tree_to_shwi (arg1) != -128))
2364 : return false;
2365 :
2366 20158 : basic_block phi_bb = gimple_bb (phi);
2367 20158 : gcc_assert (phi_bb == e0->dest && phi_bb == e1->dest);
2368 20158 : if (!IN_RANGE (EDGE_COUNT (phi_bb->preds), 3, 4))
2369 : return false;
2370 :
2371 7901 : use_operand_p use_p;
2372 7901 : gimple *use_stmt;
2373 7901 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (phires))
2374 : return false;
2375 7896 : if (!single_imm_use (phires, &use_p, &use_stmt))
2376 : return false;
2377 7221 : enum tree_code cmp;
2378 7221 : tree lhs, rhs;
2379 7221 : gimple *orig_use_stmt = use_stmt;
2380 7221 : tree orig_use_lhs = NULL_TREE;
2381 7221 : tree temps[2] = { NULL_TREE, NULL_TREE };
2382 :
2383 : /* Handle std::partial_ordering::_M_reverse(), i.e.
2384 : _1 = (unsigned char) phires;
2385 : _2 = -_1;
2386 : _3 = (signed char) _2;
2387 : and uses of _3 in comparison instead of phires. */
2388 7221 : if (gimple_assign_cast_p (use_stmt))
2389 : {
2390 237 : orig_use_lhs = gimple_assign_lhs (use_stmt);
2391 237 : temps[0] = orig_use_lhs;
2392 237 : tree ty1 = TREE_TYPE (gimple_assign_rhs1 (use_stmt));
2393 237 : tree ty2 = TREE_TYPE (orig_use_lhs);
2394 :
2395 237 : if (!TYPE_UNSIGNED (ty2) || !INTEGRAL_TYPE_P (ty2))
2396 : return false;
2397 197 : if (TYPE_PRECISION (ty2) != 8 || TYPE_PRECISION (ty1) < 8)
2398 : return false;
2399 94 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig_use_lhs))
2400 : return false;
2401 94 : if (!single_imm_use (orig_use_lhs, &use_p, &use_stmt))
2402 : return false;
2403 :
2404 93 : if (!is_gimple_assign (use_stmt)
2405 93 : || gimple_assign_rhs_code (use_stmt) != NEGATE_EXPR)
2406 : return false;
2407 :
2408 78 : orig_use_lhs = gimple_assign_lhs (use_stmt);
2409 78 : temps[1] = orig_use_lhs;
2410 78 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig_use_lhs))
2411 : return false;
2412 78 : if (!single_imm_use (orig_use_lhs, &use_p, &use_stmt))
2413 : return false;
2414 :
2415 78 : if (!gimple_assign_cast_p (use_stmt))
2416 : return false;
2417 :
2418 78 : orig_use_lhs = gimple_assign_lhs (use_stmt);
2419 78 : tree ty3 = TREE_TYPE (orig_use_lhs);
2420 :
2421 78 : if (!useless_type_conversion_p (ty3, ty1))
2422 : return false;
2423 78 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig_use_lhs))
2424 : return false;
2425 78 : if (!single_imm_use (orig_use_lhs, &use_p, &use_stmt))
2426 : return false;
2427 : }
2428 7062 : if (gimple_code (use_stmt) == GIMPLE_COND)
2429 : {
2430 2507 : cmp = gimple_cond_code (use_stmt);
2431 2507 : lhs = gimple_cond_lhs (use_stmt);
2432 2507 : rhs = gimple_cond_rhs (use_stmt);
2433 : }
2434 4555 : else if (auto assign = dyn_cast<gassign *> (use_stmt))
2435 : {
2436 2583 : if (gimple_assign_rhs_class (assign) == GIMPLE_BINARY_RHS)
2437 : {
2438 1421 : cmp = gimple_assign_rhs_code (assign);
2439 1421 : lhs = gimple_assign_rhs1 (assign);
2440 1421 : rhs = gimple_assign_rhs2 (assign);
2441 : }
2442 2276 : else if (gimple_assign_rhs_code (assign) == COND_EXPR)
2443 : {
2444 0 : tree cond = gimple_assign_rhs1 (assign);
2445 0 : if (!COMPARISON_CLASS_P (cond))
2446 : return false;
2447 0 : cmp = TREE_CODE (cond);
2448 0 : lhs = TREE_OPERAND (cond, 0);
2449 0 : rhs = TREE_OPERAND (cond, 1);
2450 : }
2451 : else
2452 : return false;
2453 : }
2454 : else
2455 : return false;
2456 3928 : switch (cmp)
2457 : {
2458 3379 : case EQ_EXPR:
2459 3379 : case NE_EXPR:
2460 3379 : case LT_EXPR:
2461 3379 : case GT_EXPR:
2462 3379 : case LE_EXPR:
2463 3379 : case GE_EXPR:
2464 3379 : break;
2465 : default:
2466 : return false;
2467 : }
2468 6680 : if (lhs != (orig_use_lhs ? orig_use_lhs : phires)
2469 3016 : || !tree_fits_shwi_p (rhs)
2470 2748 : || !IN_RANGE (tree_to_shwi (rhs), -1, 1))
2471 : return false;
2472 :
2473 2736 : if (!empty_block_p (middle_bb))
2474 : return false;
2475 :
2476 5472 : gcond *cond1 = as_a <gcond *> (*gsi_last_bb (cond_bb));
2477 2736 : enum tree_code cmp1 = gimple_cond_code (cond1);
2478 2736 : switch (cmp1)
2479 : {
2480 2601 : case LT_EXPR:
2481 2601 : case LE_EXPR:
2482 2601 : case GT_EXPR:
2483 2601 : case GE_EXPR:
2484 2601 : break;
2485 : default:
2486 : return false;
2487 : }
2488 2601 : tree lhs1 = gimple_cond_lhs (cond1);
2489 2601 : tree rhs1 = gimple_cond_rhs (cond1);
2490 2601 : if (TREE_CODE (lhs1) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs1))
2491 : return false;
2492 2601 : if (TREE_CODE (rhs1) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))
2493 : return false;
2494 :
2495 2601 : if (!single_pred_p (cond_bb) || !cond_only_block_p (cond_bb))
2496 : return false;
2497 :
2498 2587 : basic_block cond2_bb = single_pred (cond_bb);
2499 2587 : if (EDGE_COUNT (cond2_bb->succs) != 2)
2500 : return false;
2501 2587 : edge cond2_phi_edge;
2502 2587 : if (EDGE_SUCC (cond2_bb, 0)->dest == cond_bb)
2503 : {
2504 2116 : if (EDGE_SUCC (cond2_bb, 1)->dest != phi_bb)
2505 : return false;
2506 : cond2_phi_edge = EDGE_SUCC (cond2_bb, 1);
2507 : }
2508 471 : else if (EDGE_SUCC (cond2_bb, 0)->dest != phi_bb)
2509 : return false;
2510 : else
2511 : cond2_phi_edge = EDGE_SUCC (cond2_bb, 0);
2512 2587 : tree arg2 = gimple_phi_arg_def (phi, cond2_phi_edge->dest_idx);
2513 2587 : if (!tree_fits_shwi_p (arg2))
2514 : return false;
2515 5174 : gcond *cond2 = safe_dyn_cast <gcond *> (*gsi_last_bb (cond2_bb));
2516 2587 : if (!cond2)
2517 : return false;
2518 2587 : enum tree_code cmp2 = gimple_cond_code (cond2);
2519 2587 : tree lhs2 = gimple_cond_lhs (cond2);
2520 2587 : tree rhs2 = gimple_cond_rhs (cond2);
2521 2587 : if (lhs2 == lhs1)
2522 : {
2523 2585 : if (!operand_equal_p (rhs2, rhs1, 0))
2524 : {
2525 341 : if ((cmp2 == EQ_EXPR || cmp2 == NE_EXPR)
2526 341 : && TREE_CODE (rhs1) == INTEGER_CST
2527 341 : && TREE_CODE (rhs2) == INTEGER_CST)
2528 : {
2529 : /* For integers, we can have cond2 x == 5
2530 : and cond1 x < 5, x <= 4, x <= 5, x < 6,
2531 : x > 5, x >= 6, x >= 5 or x > 4. */
2532 341 : if (tree_int_cst_lt (rhs1, rhs2))
2533 : {
2534 341 : if (wi::ne_p (wi::to_wide (rhs1) + 1, wi::to_wide (rhs2)))
2535 : return false;
2536 341 : if (cmp1 == LE_EXPR)
2537 : cmp1 = LT_EXPR;
2538 0 : else if (cmp1 == GT_EXPR)
2539 : cmp1 = GE_EXPR;
2540 : else
2541 : return false;
2542 : }
2543 : else
2544 : {
2545 0 : gcc_checking_assert (tree_int_cst_lt (rhs2, rhs1));
2546 0 : if (wi::ne_p (wi::to_wide (rhs2) + 1, wi::to_wide (rhs1)))
2547 : return false;
2548 0 : if (cmp1 == LT_EXPR)
2549 : cmp1 = LE_EXPR;
2550 0 : else if (cmp1 == GE_EXPR)
2551 : cmp1 = GT_EXPR;
2552 : else
2553 : return false;
2554 : }
2555 : rhs1 = rhs2;
2556 : }
2557 : else
2558 : return false;
2559 : }
2560 : }
2561 2 : else if (lhs2 == rhs1)
2562 : {
2563 0 : if (rhs2 != lhs1)
2564 : return false;
2565 : }
2566 : else
2567 : return false;
2568 :
2569 2585 : tree arg3 = arg2;
2570 2585 : basic_block cond3_bb = cond2_bb;
2571 2585 : edge cond3_phi_edge = cond2_phi_edge;
2572 2585 : gcond *cond3 = cond2;
2573 2585 : enum tree_code cmp3 = cmp2;
2574 2585 : tree lhs3 = lhs2;
2575 2585 : tree rhs3 = rhs2;
2576 2585 : if (EDGE_COUNT (phi_bb->preds) == 4)
2577 : {
2578 446 : if (absu_hwi (tree_to_shwi (arg2)) != 1)
2579 : return false;
2580 438 : if ((cond2_phi_edge->flags & EDGE_FALSE_VALUE)
2581 438 : && HONOR_NANS (TREE_TYPE (lhs1)))
2582 : return false;
2583 342 : if (e1->flags & EDGE_TRUE_VALUE)
2584 : {
2585 340 : if (tree_to_shwi (arg0) != -128
2586 340 : || absu_hwi (tree_to_shwi (arg1)) != 1
2587 680 : || wi::to_widest (arg1) == wi::to_widest (arg2))
2588 : return false;
2589 : }
2590 2 : else if (tree_to_shwi (arg1) != -128
2591 2 : || absu_hwi (tree_to_shwi (arg0)) != 1
2592 4 : || wi::to_widest (arg0) == wi::to_widest (arg2))
2593 : return false;
2594 342 : switch (cmp2)
2595 : {
2596 342 : case LT_EXPR:
2597 342 : case LE_EXPR:
2598 342 : case GT_EXPR:
2599 342 : case GE_EXPR:
2600 342 : break;
2601 : default:
2602 : return false;
2603 : }
2604 : /* if (x < y) goto phi_bb; else fallthru;
2605 : if (x > y) goto phi_bb; else fallthru;
2606 : bbx:;
2607 : phi_bb:;
2608 : is ok, but if x and y are swapped in one of the comparisons,
2609 : or the comparisons are the same and operands not swapped,
2610 : or the true and false edges are swapped, it is not.
2611 : For HONOR_NANS, the edge flags are irrelevant and the comparisons
2612 : must be different for non-swapped operands and same for swapped
2613 : operands. */
2614 342 : if ((lhs2 == lhs1)
2615 342 : ^ ((cmp2 == LT_EXPR || cmp2 == LE_EXPR)
2616 342 : != (cmp1 == LT_EXPR || cmp1 == LE_EXPR))
2617 342 : ^ ((cond2_phi_edge->flags & EDGE_FALSE_VALUE) != 0))
2618 : return false;
2619 340 : if (!single_pred_p (cond2_bb) || !cond_only_block_p (cond2_bb))
2620 : return false;
2621 340 : cond3_bb = single_pred (cond2_bb);
2622 340 : if (EDGE_COUNT (cond3_bb->succs) != 2)
2623 : return false;
2624 320 : if (EDGE_SUCC (cond3_bb, 0)->dest == cond2_bb)
2625 : {
2626 150 : if (EDGE_SUCC (cond3_bb, 1)->dest != phi_bb)
2627 : return false;
2628 : cond3_phi_edge = EDGE_SUCC (cond3_bb, 1);
2629 : }
2630 170 : else if (EDGE_SUCC (cond3_bb, 0)->dest != phi_bb)
2631 : return false;
2632 : else
2633 : cond3_phi_edge = EDGE_SUCC (cond3_bb, 0);
2634 320 : arg3 = gimple_phi_arg_def (phi, cond3_phi_edge->dest_idx);
2635 640 : cond3 = safe_dyn_cast <gcond *> (*gsi_last_bb (cond3_bb));
2636 320 : if (!cond3)
2637 : return false;
2638 320 : cmp3 = gimple_cond_code (cond3);
2639 320 : lhs3 = gimple_cond_lhs (cond3);
2640 320 : rhs3 = gimple_cond_rhs (cond3);
2641 320 : if (lhs3 == lhs1)
2642 : {
2643 318 : if (!operand_equal_p (rhs3, rhs1, 0))
2644 : return false;
2645 : }
2646 2 : else if (lhs3 == rhs1)
2647 : {
2648 0 : if (rhs3 != lhs1)
2649 : return false;
2650 : }
2651 : else
2652 : return false;
2653 : }
2654 2139 : else if (absu_hwi (tree_to_shwi (arg0)) != 1
2655 2118 : || absu_hwi (tree_to_shwi (arg1)) != 1
2656 4257 : || wi::to_widest (arg0) == wi::to_widest (arg1)
2657 4257 : || HONOR_NANS (TREE_TYPE (lhs1)))
2658 : return false;
2659 :
2660 2436 : if (!integer_zerop (arg3) || (cmp3 != EQ_EXPR && cmp3 != NE_EXPR))
2661 : return false;
2662 2436 : if ((cond3_phi_edge->flags & (cmp3 == EQ_EXPR
2663 2436 : ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE)) == 0)
2664 : return false;
2665 :
2666 : /* lhs1 one_cmp rhs1 results in phires of 1. */
2667 2436 : enum tree_code one_cmp;
2668 4872 : if ((cmp1 == LT_EXPR || cmp1 == LE_EXPR)
2669 2580 : ^ (!integer_onep ((e1->flags & EDGE_TRUE_VALUE) ? arg1 : arg0)))
2670 : one_cmp = LT_EXPR;
2671 : else
2672 1793 : one_cmp = GT_EXPR;
2673 :
2674 2436 : enum tree_code res_cmp;
2675 2436 : bool negate_p = false;
2676 2436 : switch (cmp)
2677 : {
2678 1332 : case EQ_EXPR:
2679 1332 : if (integer_zerop (rhs) && !HONOR_NANS (TREE_TYPE (lhs1)))
2680 : res_cmp = EQ_EXPR;
2681 1248 : else if (integer_minus_onep (rhs))
2682 781 : res_cmp = one_cmp == LT_EXPR ? GT_EXPR : LT_EXPR;
2683 467 : else if (integer_onep (rhs))
2684 : res_cmp = one_cmp;
2685 : else
2686 : return false;
2687 : break;
2688 1014 : case NE_EXPR:
2689 1014 : if (integer_zerop (rhs) && !HONOR_NANS (TREE_TYPE (lhs1)))
2690 : res_cmp = NE_EXPR;
2691 976 : else if (integer_minus_onep (rhs))
2692 492 : res_cmp = one_cmp == LT_EXPR ? LE_EXPR : GE_EXPR;
2693 484 : else if (integer_onep (rhs))
2694 699 : res_cmp = one_cmp == LT_EXPR ? GE_EXPR : LE_EXPR;
2695 : else
2696 : return false;
2697 984 : if (HONOR_NANS (TREE_TYPE (lhs1)))
2698 : negate_p = true;
2699 : break;
2700 28 : case LT_EXPR:
2701 28 : if (integer_onep (rhs))
2702 0 : res_cmp = one_cmp == LT_EXPR ? GE_EXPR : LE_EXPR;
2703 28 : else if (integer_zerop (rhs))
2704 28 : res_cmp = one_cmp == LT_EXPR ? GT_EXPR : LT_EXPR;
2705 : else
2706 : return false;
2707 28 : if (HONOR_NANS (TREE_TYPE (lhs1)))
2708 : negate_p = true;
2709 : break;
2710 5 : case LE_EXPR:
2711 5 : if (integer_zerop (rhs))
2712 5 : res_cmp = one_cmp == LT_EXPR ? GE_EXPR : LE_EXPR;
2713 0 : else if (integer_minus_onep (rhs))
2714 0 : res_cmp = one_cmp == LT_EXPR ? GT_EXPR : LT_EXPR;
2715 : else
2716 : return false;
2717 5 : if (HONOR_NANS (TREE_TYPE (lhs1)))
2718 : negate_p = true;
2719 : break;
2720 0 : case GT_EXPR:
2721 0 : if (integer_minus_onep (rhs))
2722 0 : res_cmp = one_cmp == LT_EXPR ? LE_EXPR : GE_EXPR;
2723 0 : else if (integer_zerop (rhs))
2724 : res_cmp = one_cmp;
2725 : else
2726 : return false;
2727 : break;
2728 57 : case GE_EXPR:
2729 57 : if (integer_zerop (rhs))
2730 57 : res_cmp = one_cmp == LT_EXPR ? LE_EXPR : GE_EXPR;
2731 0 : else if (integer_onep (rhs))
2732 : res_cmp = one_cmp;
2733 : else
2734 : return false;
2735 : break;
2736 : default:
2737 : gcc_unreachable ();
2738 : }
2739 2364 : if (orig_use_lhs)
2740 60 : res_cmp = swap_tree_comparison (res_cmp);
2741 :
2742 2364 : tree clhs1 = lhs1, crhs1 = rhs1;
2743 2364 : if (negate_p)
2744 : {
2745 72 : if (cfun->can_throw_non_call_exceptions)
2746 0 : return false;
2747 72 : res_cmp = invert_tree_comparison (res_cmp, false);
2748 72 : clhs1 = make_ssa_name (boolean_type_node);
2749 72 : gimple *g = gimple_build_assign (clhs1, res_cmp, lhs1, rhs1);
2750 72 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2751 72 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2752 72 : crhs1 = boolean_false_node;
2753 72 : res_cmp = EQ_EXPR;
2754 : }
2755 :
2756 2364 : if (gimple_code (use_stmt) == GIMPLE_COND)
2757 : {
2758 1705 : gcond *use_cond = as_a <gcond *> (use_stmt);
2759 1705 : gimple_cond_set_code (use_cond, res_cmp);
2760 1705 : gimple_cond_set_lhs (use_cond, clhs1);
2761 1705 : gimple_cond_set_rhs (use_cond, crhs1);
2762 : }
2763 659 : else if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
2764 : {
2765 659 : gimple_assign_set_rhs_code (use_stmt, res_cmp);
2766 659 : gimple_assign_set_rhs1 (use_stmt, clhs1);
2767 659 : gimple_assign_set_rhs2 (use_stmt, crhs1);
2768 : }
2769 : else
2770 : {
2771 0 : tree cond = build2 (res_cmp, TREE_TYPE (gimple_assign_rhs1 (use_stmt)),
2772 : clhs1, crhs1);
2773 0 : gimple_assign_set_rhs1 (use_stmt, cond);
2774 : }
2775 2364 : update_stmt (use_stmt);
2776 :
2777 2364 : if (MAY_HAVE_DEBUG_BIND_STMTS)
2778 : {
2779 1966 : use_operand_p use_p;
2780 1966 : imm_use_iterator iter;
2781 1966 : bool has_debug_uses = false;
2782 1966 : bool has_cast1_debug_uses = false;
2783 1966 : bool has_neg_debug_uses = false;
2784 1966 : bool has_cast2_debug_uses = false;
2785 1988 : FOR_EACH_IMM_USE_FAST (use_p, iter, phires)
2786 : {
2787 1988 : gimple *use_stmt = USE_STMT (use_p);
2788 1988 : if (is_gimple_debug (use_stmt))
2789 : {
2790 : has_debug_uses = true;
2791 : break;
2792 : }
2793 1966 : }
2794 1966 : if (orig_use_lhs)
2795 : {
2796 44 : FOR_EACH_IMM_USE_FAST (use_p, iter, temps[0])
2797 : {
2798 38 : gimple *use_stmt = USE_STMT (use_p);
2799 38 : if (is_gimple_debug (use_stmt))
2800 : {
2801 : has_debug_uses = true;
2802 : has_cast1_debug_uses = true;
2803 : break;
2804 : }
2805 22 : }
2806 44 : FOR_EACH_IMM_USE_FAST (use_p, iter, temps[1])
2807 : {
2808 38 : gimple *use_stmt = USE_STMT (use_p);
2809 38 : if (is_gimple_debug (use_stmt))
2810 : {
2811 : has_debug_uses = true;
2812 : has_cast1_debug_uses = true;
2813 : has_neg_debug_uses = true;
2814 : break;
2815 : }
2816 22 : }
2817 22 : FOR_EACH_IMM_USE_FAST (use_p, iter, orig_use_lhs)
2818 : {
2819 16 : gimple *use_stmt = USE_STMT (use_p);
2820 16 : if (is_gimple_debug (use_stmt))
2821 : {
2822 : has_debug_uses = true;
2823 : has_cast1_debug_uses = true;
2824 : has_neg_debug_uses = true;
2825 : has_cast2_debug_uses = true;
2826 : break;
2827 : }
2828 22 : }
2829 22 : if (has_debug_uses)
2830 : {
2831 22 : gimple_stmt_iterator gsi = gsi_for_stmt (orig_use_stmt);
2832 22 : tree zero = build_zero_cst (TREE_TYPE (temps[0]));
2833 22 : gimple_assign_set_rhs_with_ops (&gsi, INTEGER_CST, zero);
2834 22 : update_stmt (orig_use_stmt);
2835 22 : gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (temps[1]));
2836 22 : zero = build_zero_cst (TREE_TYPE (temps[1]));
2837 22 : gimple_assign_set_rhs_with_ops (&gsi, INTEGER_CST, zero);
2838 22 : update_stmt (SSA_NAME_DEF_STMT (temps[1]));
2839 22 : gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (orig_use_lhs));
2840 22 : zero = build_zero_cst (TREE_TYPE (orig_use_lhs));
2841 22 : gimple_assign_set_rhs_with_ops (&gsi, INTEGER_CST, zero);
2842 22 : update_stmt (SSA_NAME_DEF_STMT (orig_use_lhs));
2843 : }
2844 : }
2845 :
2846 1966 : if (has_debug_uses)
2847 : {
2848 : /* If there are debug uses, emit something like:
2849 : # DEBUG D#1 => i_2(D) > j_3(D) ? 1 : -1
2850 : # DEBUG D#2 => i_2(D) == j_3(D) ? 0 : D#1
2851 : where > stands for the comparison that yielded 1
2852 : and replace debug uses of phi result with that D#2.
2853 : Ignore the value of -128 if !HONOR_NANS, because if NaNs
2854 : aren't expected, all floating point numbers should be
2855 : comparable. If HONOR_NANS, emit something like:
2856 : # DEBUG D#1 => i_2(D) < j_3(D) ? -1 : -128
2857 : # DEBUG D#2 => i_2(D) > j_3(D) ? 1 : D#1
2858 : # DEBUG D#3 => i_2(D) == j_3(D) ? 0 : D#2
2859 : instead. */
2860 1966 : gimple_stmt_iterator gsi = gsi_after_labels (gimple_bb (phi));
2861 1966 : tree type = TREE_TYPE (phires);
2862 1966 : tree minus_one = build_int_cst (type, -1);
2863 1966 : if (HONOR_NANS (TREE_TYPE (lhs1)))
2864 : {
2865 102 : tree temp3 = build_debug_expr_decl (type);
2866 204 : tree t = build2 (one_cmp == LT_EXPR ? GT_EXPR : LT_EXPR,
2867 : boolean_type_node, lhs1, rhs2);
2868 102 : t = build3 (COND_EXPR, type, t, minus_one,
2869 : build_int_cst (type, -128));
2870 102 : gimple *g = gimple_build_debug_bind (temp3, t, phi);
2871 102 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2872 102 : minus_one = temp3;
2873 : }
2874 1966 : tree temp1 = build_debug_expr_decl (type);
2875 1966 : tree t = build2 (one_cmp, boolean_type_node, lhs1, rhs2);
2876 1966 : t = build3 (COND_EXPR, type, t, build_one_cst (type),
2877 : minus_one);
2878 1966 : gimple *g = gimple_build_debug_bind (temp1, t, phi);
2879 1966 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2880 1966 : tree temp2 = build_debug_expr_decl (type);
2881 1966 : t = build2 (EQ_EXPR, boolean_type_node, lhs1, rhs2);
2882 1966 : t = build3 (COND_EXPR, type, t, build_zero_cst (type), temp1);
2883 1966 : g = gimple_build_debug_bind (temp2, t, phi);
2884 1966 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2885 1966 : replace_uses_by (phires, temp2);
2886 1966 : if (has_cast1_debug_uses)
2887 : {
2888 16 : tree temp3 = build_debug_expr_decl (TREE_TYPE (temps[0]));
2889 16 : t = fold_convert (TREE_TYPE (temps[0]), temp2);
2890 16 : g = gimple_build_debug_bind (temp3, t, phi);
2891 16 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2892 16 : replace_uses_by (temps[0], temp3);
2893 16 : temp2 = temp3;
2894 : }
2895 1966 : if (has_neg_debug_uses)
2896 : {
2897 16 : tree temp3 = build_debug_expr_decl (TREE_TYPE (temps[1]));
2898 16 : t = fold_build1 (NEGATE_EXPR, TREE_TYPE (temps[1]), temp2);
2899 16 : g = gimple_build_debug_bind (temp3, t, phi);
2900 16 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2901 16 : replace_uses_by (temps[1], temp3);
2902 16 : temp2 = temp3;
2903 : }
2904 1966 : if (has_cast2_debug_uses)
2905 : {
2906 16 : tree temp3 = build_debug_expr_decl (TREE_TYPE (orig_use_lhs));
2907 16 : t = fold_convert (TREE_TYPE (orig_use_lhs), temp2);
2908 16 : g = gimple_build_debug_bind (temp3, t, phi);
2909 16 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2910 16 : replace_uses_by (orig_use_lhs, temp3);
2911 : }
2912 : }
2913 : }
2914 :
2915 2364 : if (orig_use_lhs)
2916 : {
2917 60 : gimple_stmt_iterator gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (orig_use_lhs));
2918 60 : gsi_remove (&gsi, true);
2919 60 : gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (temps[1]));
2920 60 : gsi_remove (&gsi, true);
2921 60 : gsi = gsi_for_stmt (orig_use_stmt);
2922 60 : gsi_remove (&gsi, true);
2923 60 : release_ssa_name (orig_use_lhs);
2924 60 : release_ssa_name (temps[1]);
2925 60 : release_ssa_name (temps[0]);
2926 : }
2927 :
2928 2364 : gimple_stmt_iterator psi = gsi_for_stmt (phi);
2929 2364 : remove_phi_node (&psi, true);
2930 2364 : statistics_counter_event (cfun, "spaceship replacement", 1);
2931 :
2932 2364 : return true;
2933 : }
2934 :
2935 : /* Optimize x ? __builtin_fun (x) : C, where C is __builtin_fun (0).
2936 : Convert
2937 :
2938 : <bb 2>
2939 : if (b_4(D) != 0)
2940 : goto <bb 3>
2941 : else
2942 : goto <bb 4>
2943 :
2944 : <bb 3>
2945 : _2 = (unsigned long) b_4(D);
2946 : _9 = __builtin_popcountl (_2);
2947 : OR
2948 : _9 = __builtin_popcountl (b_4(D));
2949 :
2950 : <bb 4>
2951 : c_12 = PHI <0(2), _9(3)>
2952 :
2953 : Into
2954 : <bb 2>
2955 : _2 = (unsigned long) b_4(D);
2956 : _9 = __builtin_popcountl (_2);
2957 : OR
2958 : _9 = __builtin_popcountl (b_4(D));
2959 :
2960 : <bb 4>
2961 : c_12 = PHI <_9(2)>
2962 :
2963 : Similarly for __builtin_clz or __builtin_ctz if
2964 : C?Z_DEFINED_VALUE_AT_ZERO is 2, optab is present and
2965 : instead of 0 above it uses the value from that macro. */
2966 :
2967 : static bool
2968 448582 : cond_removal_in_builtin_zero_pattern (basic_block cond_bb,
2969 : basic_block middle_bb,
2970 : edge e1, edge e2, gphi *phi,
2971 : tree arg0, tree arg1)
2972 : {
2973 448582 : gimple_stmt_iterator gsi, gsi_from;
2974 448582 : gimple *call;
2975 448582 : gimple *cast = NULL;
2976 448582 : tree lhs, arg;
2977 :
2978 : /* Check that
2979 : _2 = (unsigned long) b_4(D);
2980 : _9 = __builtin_popcountl (_2);
2981 : OR
2982 : _9 = __builtin_popcountl (b_4(D));
2983 : are the only stmts in the middle_bb. */
2984 :
2985 448582 : gsi = gsi_start_nondebug_after_labels_bb (middle_bb);
2986 448582 : if (gsi_end_p (gsi))
2987 : return false;
2988 280805 : cast = gsi_stmt (gsi);
2989 280805 : gsi_next_nondebug (&gsi);
2990 280805 : if (!gsi_end_p (gsi))
2991 : {
2992 142863 : call = gsi_stmt (gsi);
2993 142863 : gsi_next_nondebug (&gsi);
2994 142863 : if (!gsi_end_p (gsi))
2995 : return false;
2996 : }
2997 : else
2998 : {
2999 : call = cast;
3000 : cast = NULL;
3001 : }
3002 :
3003 : /* Check that we have a popcount/clz/ctz builtin. */
3004 186848 : if (!is_gimple_call (call))
3005 : return false;
3006 :
3007 5330 : lhs = gimple_get_lhs (call);
3008 :
3009 5330 : if (lhs == NULL_TREE)
3010 : return false;
3011 :
3012 5304 : combined_fn cfn = gimple_call_combined_fn (call);
3013 5304 : if (gimple_call_num_args (call) != 1
3014 5304 : && (gimple_call_num_args (call) != 2
3015 : || cfn == CFN_CLZ
3016 648 : || cfn == CFN_CTZ))
3017 : return false;
3018 :
3019 3796 : arg = gimple_call_arg (call, 0);
3020 :
3021 3796 : internal_fn ifn = IFN_LAST;
3022 3796 : int val = 0;
3023 3796 : bool any_val = false;
3024 3796 : switch (cfn)
3025 : {
3026 : CASE_CFN_BSWAP:
3027 : CASE_CFN_BITREVERSE:
3028 : CASE_CFN_FFS:
3029 : CASE_CFN_PARITY:
3030 : CASE_CFN_POPCOUNT:
3031 : break;
3032 861 : CASE_CFN_CLZ:
3033 861 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg)))
3034 : {
3035 861 : tree type = TREE_TYPE (arg);
3036 861 : if (BITINT_TYPE_P (type))
3037 : {
3038 12 : if (gimple_call_num_args (call) == 1)
3039 : {
3040 : any_val = true;
3041 : ifn = IFN_CLZ;
3042 : break;
3043 : }
3044 0 : if (!tree_fits_shwi_p (gimple_call_arg (call, 1)))
3045 : return false;
3046 0 : HOST_WIDE_INT at_zero = tree_to_shwi (gimple_call_arg (call, 1));
3047 0 : if ((int) at_zero != at_zero)
3048 : return false;
3049 0 : ifn = IFN_CLZ;
3050 0 : val = at_zero;
3051 0 : break;
3052 : }
3053 849 : if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
3054 1673 : && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
3055 : val) == 2)
3056 : {
3057 : ifn = IFN_CLZ;
3058 : break;
3059 : }
3060 : }
3061 : return false;
3062 402 : CASE_CFN_CTZ:
3063 402 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg)))
3064 : {
3065 402 : tree type = TREE_TYPE (arg);
3066 402 : if (BITINT_TYPE_P (type))
3067 : {
3068 12 : if (gimple_call_num_args (call) == 1)
3069 : {
3070 : any_val = true;
3071 : ifn = IFN_CTZ;
3072 : break;
3073 : }
3074 0 : if (!tree_fits_shwi_p (gimple_call_arg (call, 1)))
3075 : return false;
3076 0 : HOST_WIDE_INT at_zero = tree_to_shwi (gimple_call_arg (call, 1));
3077 0 : if ((int) at_zero != at_zero)
3078 : return false;
3079 0 : ifn = IFN_CTZ;
3080 0 : val = at_zero;
3081 0 : break;
3082 : }
3083 390 : if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
3084 759 : && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
3085 : val) == 2)
3086 : {
3087 : ifn = IFN_CTZ;
3088 : break;
3089 : }
3090 : }
3091 : return false;
3092 3 : case CFN_BUILT_IN_CLRSB:
3093 3 : val = TYPE_PRECISION (integer_type_node) - 1;
3094 3 : break;
3095 3 : case CFN_BUILT_IN_CLRSBL:
3096 3 : val = TYPE_PRECISION (long_integer_type_node) - 1;
3097 3 : break;
3098 3 : case CFN_BUILT_IN_CLRSBLL:
3099 3 : val = TYPE_PRECISION (long_long_integer_type_node) - 1;
3100 3 : break;
3101 : default:
3102 : return false;
3103 : }
3104 :
3105 168 : if (cast)
3106 : {
3107 : /* We have a cast stmt feeding popcount/clz/ctz builtin. */
3108 : /* Check that we have a cast prior to that. */
3109 55 : auto assign = dyn_cast<gassign *> (cast);
3110 55 : if (!assign || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (cast)))
3111 : return false;
3112 : /* Result of the cast stmt is the argument to the builtin. */
3113 25 : if (arg != gimple_assign_lhs (assign))
3114 : return false;
3115 25 : arg = gimple_assign_rhs1 (assign);
3116 : }
3117 :
3118 276 : gcond *cond = dyn_cast <gcond *> (*gsi_last_bb (cond_bb));
3119 :
3120 : /* Cond_bb has a check for b_4 [!=|==] 0 before calling the popcount/clz/ctz
3121 : builtin. */
3122 138 : if (!cond
3123 138 : || (gimple_cond_code (cond) != NE_EXPR
3124 23 : && gimple_cond_code (cond) != EQ_EXPR)
3125 138 : || !integer_zerop (gimple_cond_rhs (cond))
3126 135 : || arg != gimple_cond_lhs (cond))
3127 : return false;
3128 :
3129 57 : edge true_edge, false_edge;
3130 : /* We need to know which is the true edge and which is the false
3131 : edge so that we know when to invert the condition below. */
3132 57 : extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
3133 :
3134 : /* Forward the edges over the middle basic block. */
3135 57 : if (true_edge->dest == middle_bb)
3136 57 : true_edge = EDGE_SUCC (true_edge->dest, 0);
3137 57 : if (false_edge->dest == middle_bb)
3138 0 : false_edge = EDGE_SUCC (false_edge->dest, 0);
3139 :
3140 : /* Canonicalize the args with respect to the edges,
3141 : arg0 is from the true edge and arg1 is from the
3142 : false edge.
3143 : That is `cond ? arg0 : arg1`.*/
3144 57 : if (true_edge == e1)
3145 57 : gcc_assert (false_edge == e2);
3146 : else
3147 : {
3148 0 : gcc_assert (false_edge == e1);
3149 0 : gcc_assert (true_edge == e2);
3150 : std::swap (arg0, arg1);
3151 : }
3152 :
3153 : /* Canonicalize the args such that we get:
3154 : `arg != 0 ? arg0 : arg1`. So swap arg0/arg1
3155 : around if cond was an equals. */
3156 57 : if (gimple_cond_code (cond) == EQ_EXPR)
3157 2 : std::swap (arg0, arg1);
3158 :
3159 : /* Check PHI arguments. */
3160 57 : if (lhs != arg0
3161 55 : || TREE_CODE (arg1) != INTEGER_CST)
3162 : return false;
3163 31 : if (any_val)
3164 : {
3165 0 : if (!tree_fits_shwi_p (arg1))
3166 : return false;
3167 0 : HOST_WIDE_INT at_zero = tree_to_shwi (arg1);
3168 0 : if ((int) at_zero != at_zero)
3169 : return false;
3170 0 : val = at_zero;
3171 : }
3172 31 : else if (wi::to_wide (arg1) != val)
3173 : return false;
3174 :
3175 : /* And insert the popcount/clz/ctz builtin and cast stmt before the
3176 : cond_bb. */
3177 13 : gsi = gsi_last_bb (cond_bb);
3178 13 : if (cast)
3179 : {
3180 13 : gsi_from = gsi_for_stmt (cast);
3181 13 : gsi_move_before (&gsi_from, &gsi);
3182 13 : reset_flow_sensitive_info (gimple_get_lhs (cast));
3183 : }
3184 13 : gsi_from = gsi_for_stmt (call);
3185 13 : if (ifn == IFN_LAST
3186 13 : || (gimple_call_internal_p (call) && gimple_call_num_args (call) == 2))
3187 8 : gsi_move_before (&gsi_from, &gsi);
3188 : else
3189 : {
3190 : /* For __builtin_c[lt]z* force .C[LT]Z ifn, because only
3191 : the latter is well defined at zero. */
3192 5 : call = gimple_build_call_internal (ifn, 2, gimple_call_arg (call, 0),
3193 5 : build_int_cst (integer_type_node, val));
3194 5 : gimple_call_set_lhs (call, lhs);
3195 5 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
3196 5 : gsi_remove (&gsi_from, true);
3197 : }
3198 13 : reset_flow_sensitive_info (lhs);
3199 :
3200 : /* Now update the PHI and remove unneeded bbs. */
3201 13 : replace_phi_edge_with_variable (cond_bb, e2, phi, lhs);
3202 13 : return true;
3203 : }
3204 :
3205 : /* Auxiliary functions to determine the set of memory accesses which
3206 : can't trap because they are preceded by accesses to the same memory
3207 : portion. We do that for MEM_REFs, so we only need to track
3208 : the SSA_NAME of the pointer indirectly referenced. The algorithm
3209 : simply is a walk over all instructions in dominator order. When
3210 : we see an MEM_REF we determine if we've already seen a same
3211 : ref anywhere up to the root of the dominator tree. If we do the
3212 : current access can't trap. If we don't see any dominating access
3213 : the current access might trap, but might also make later accesses
3214 : non-trapping, so we remember it. We need to be careful with loads
3215 : or stores, for instance a load might not trap, while a store would,
3216 : so if we see a dominating read access this doesn't mean that a later
3217 : write access would not trap. Hence we also need to differentiate the
3218 : type of access(es) seen.
3219 :
3220 : ??? We currently are very conservative and assume that a load might
3221 : trap even if a store doesn't (write-only memory). This probably is
3222 : overly conservative.
3223 :
3224 : We currently support a special case that for !TREE_ADDRESSABLE automatic
3225 : variables, it could ignore whether something is a load or store because the
3226 : local stack should be always writable. */
3227 :
3228 : /* A hash-table of references (MEM_REF/ARRAY_REF/COMPONENT_REF), and in which
3229 : basic block an *_REF through it was seen, which would constitute a
3230 : no-trap region for same accesses.
3231 :
3232 : Size is needed to support 2 MEM_REFs of different types, like
3233 : MEM<double>(s_1) and MEM<long>(s_1), which would compare equal with
3234 : OEP_ADDRESS_OF. */
3235 : struct ref_to_bb
3236 : {
3237 : tree exp;
3238 : HOST_WIDE_INT size;
3239 : unsigned int phase;
3240 : basic_block bb;
3241 : };
3242 :
3243 : /* Hashtable helpers. */
3244 :
3245 : struct refs_hasher : free_ptr_hash<ref_to_bb>
3246 : {
3247 : static inline hashval_t hash (const ref_to_bb *);
3248 : static inline bool equal (const ref_to_bb *, const ref_to_bb *);
3249 : };
3250 :
3251 : /* Used for quick clearing of the hash-table when we see calls.
3252 : Hash entries with phase < nt_call_phase are invalid. */
3253 : static unsigned int nt_call_phase;
3254 :
3255 : /* The hash function. */
3256 :
3257 : inline hashval_t
3258 20259482 : refs_hasher::hash (const ref_to_bb *n)
3259 : {
3260 20259482 : inchash::hash hstate;
3261 20259482 : inchash::add_expr (n->exp, hstate, OEP_ADDRESS_OF);
3262 20259482 : hstate.add_hwi (n->size);
3263 20259482 : return hstate.end ();
3264 : }
3265 :
3266 : /* The equality function of *P1 and *P2. */
3267 :
3268 : inline bool
3269 14793211 : refs_hasher::equal (const ref_to_bb *n1, const ref_to_bb *n2)
3270 : {
3271 14793211 : return operand_equal_p (n1->exp, n2->exp, OEP_ADDRESS_OF)
3272 14793211 : && n1->size == n2->size;
3273 : }
3274 :
3275 : class nontrapping_dom_walker : public dom_walker
3276 : {
3277 : public:
3278 1068132 : nontrapping_dom_walker (cdi_direction direction, hash_set<tree> *ps)
3279 1068132 : : dom_walker (direction), m_nontrapping (ps), m_seen_refs (128)
3280 1068132 : {}
3281 :
3282 : edge before_dom_children (basic_block) final override;
3283 : void after_dom_children (basic_block) final override;
3284 :
3285 : private:
3286 :
3287 : /* We see the expression EXP in basic block BB. If it's an interesting
3288 : expression (an MEM_REF through an SSA_NAME) possibly insert the
3289 : expression into the set NONTRAP or the hash table of seen expressions.
3290 : STORE is true if this expression is on the LHS, otherwise it's on
3291 : the RHS. */
3292 : void add_or_mark_expr (basic_block, tree, bool);
3293 :
3294 : hash_set<tree> *m_nontrapping;
3295 :
3296 : /* The hash table for remembering what we've seen. */
3297 : hash_table<refs_hasher> m_seen_refs;
3298 : };
3299 :
3300 : /* Called by walk_dominator_tree, when entering the block BB. */
3301 : edge
3302 11924566 : nontrapping_dom_walker::before_dom_children (basic_block bb)
3303 : {
3304 11924566 : edge e;
3305 11924566 : edge_iterator ei;
3306 11924566 : gimple_stmt_iterator gsi;
3307 :
3308 : /* If we haven't seen all our predecessors, clear the hash-table. */
3309 26196727 : FOR_EACH_EDGE (e, ei, bb->preds)
3310 14984851 : if ((((size_t)e->src->aux) & 2) == 0)
3311 : {
3312 712690 : nt_call_phase++;
3313 712690 : break;
3314 : }
3315 :
3316 : /* Mark this BB as being on the path to dominator root and as visited. */
3317 11924566 : bb->aux = (void*)(1 | 2);
3318 :
3319 : /* And walk the statements in order. */
3320 107398731 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3321 : {
3322 83549599 : gimple *stmt = gsi_stmt (gsi);
3323 :
3324 83549599 : if ((gimple_code (stmt) == GIMPLE_ASM && gimple_vdef (stmt))
3325 83585800 : || (is_gimple_call (stmt)
3326 5720207 : && (!nonfreeing_call_p (stmt) || !nonbarrier_call_p (stmt))))
3327 5133861 : nt_call_phase++;
3328 93934631 : else if (gimple_assign_single_p (stmt) && !gimple_has_volatile_ops (stmt))
3329 : {
3330 13500337 : add_or_mark_expr (bb, gimple_assign_lhs (stmt), true);
3331 13500337 : add_or_mark_expr (bb, gimple_assign_rhs1 (stmt), false);
3332 : }
3333 : }
3334 11924566 : return NULL;
3335 : }
3336 :
3337 : /* Called by walk_dominator_tree, when basic block BB is exited. */
3338 : void
3339 11924566 : nontrapping_dom_walker::after_dom_children (basic_block bb)
3340 : {
3341 : /* This BB isn't on the path to dominator root anymore. */
3342 11924566 : bb->aux = (void*)2;
3343 11924566 : }
3344 :
3345 : /* We see the expression EXP in basic block BB. If it's an interesting
3346 : expression of:
3347 : 1) MEM_REF
3348 : 2) ARRAY_REF
3349 : 3) COMPONENT_REF
3350 : possibly insert the expression into the set NONTRAP or the hash table
3351 : of seen expressions. STORE is true if this expression is on the LHS,
3352 : otherwise it's on the RHS. */
3353 : void
3354 27000674 : nontrapping_dom_walker::add_or_mark_expr (basic_block bb, tree exp, bool store)
3355 : {
3356 27000674 : HOST_WIDE_INT size;
3357 :
3358 27000674 : if ((TREE_CODE (exp) == MEM_REF || TREE_CODE (exp) == ARRAY_REF
3359 22602330 : || TREE_CODE (exp) == COMPONENT_REF)
3360 34263791 : && (size = int_size_in_bytes (TREE_TYPE (exp))) > 0)
3361 : {
3362 11661456 : struct ref_to_bb map;
3363 11661456 : ref_to_bb **slot;
3364 11661456 : struct ref_to_bb *r2bb;
3365 11661456 : basic_block found_bb = 0;
3366 :
3367 11661456 : if (!store)
3368 : {
3369 5337527 : tree base = get_base_address (exp);
3370 : /* Only record a LOAD of a local variable without address-taken, as
3371 : the local stack is always writable. This allows cselim on a STORE
3372 : with a dominating LOAD. */
3373 5337527 : if (!auto_var_p (base) || TREE_ADDRESSABLE (base))
3374 4366196 : return;
3375 : }
3376 :
3377 : /* Try to find the last seen *_REF, which can trap. */
3378 7295260 : map.exp = exp;
3379 7295260 : map.size = size;
3380 7295260 : slot = m_seen_refs.find_slot (&map, INSERT);
3381 7295260 : r2bb = *slot;
3382 7295260 : if (r2bb && r2bb->phase >= nt_call_phase)
3383 291885 : found_bb = r2bb->bb;
3384 :
3385 : /* If we've found a trapping *_REF, _and_ it dominates EXP
3386 : (it's in a basic block on the path from us to the dominator root)
3387 : then we can't trap. */
3388 291885 : if (found_bb && (((size_t)found_bb->aux) & 1) == 1)
3389 : {
3390 76102 : m_nontrapping->add (exp);
3391 : }
3392 : else
3393 : {
3394 : /* EXP might trap, so insert it into the hash table. */
3395 7219158 : if (r2bb)
3396 : {
3397 991677 : r2bb->phase = nt_call_phase;
3398 991677 : r2bb->bb = bb;
3399 : }
3400 : else
3401 : {
3402 6227481 : r2bb = XNEW (struct ref_to_bb);
3403 6227481 : r2bb->phase = nt_call_phase;
3404 6227481 : r2bb->bb = bb;
3405 6227481 : r2bb->exp = exp;
3406 6227481 : r2bb->size = size;
3407 6227481 : *slot = r2bb;
3408 : }
3409 : }
3410 : }
3411 : }
3412 :
3413 : /* This is the entry point of gathering non trapping memory accesses.
3414 : It will do a dominator walk over the whole function, and it will
3415 : make use of the bb->aux pointers. It returns a set of trees
3416 : (the MEM_REFs itself) which can't trap. */
3417 : static hash_set<tree> *
3418 1068132 : get_non_trapping (void)
3419 : {
3420 1068132 : nt_call_phase = 0;
3421 1068132 : hash_set<tree> *nontrap = new hash_set<tree>;
3422 :
3423 2136264 : nontrapping_dom_walker (CDI_DOMINATORS, nontrap)
3424 1068132 : .walk (cfun->cfg->x_entry_block_ptr);
3425 :
3426 1068132 : clear_aux_for_blocks ();
3427 1068132 : return nontrap;
3428 : }
3429 :
3430 : /* Do the main work of conditional store replacement. We already know
3431 : that the recognized pattern looks like so:
3432 :
3433 : split:
3434 : if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
3435 : MIDDLE_BB:
3436 : something
3437 : fallthrough (edge E0)
3438 : JOIN_BB:
3439 : some more
3440 :
3441 : ASSIGN is a store in MIDDLE_BB which is the candidate for cselim. We check
3442 : that MIDDLE_BB contains only one store (i.e., ASSIGN), that that store
3443 : doesn't trap (not via NOTRAP, but via checking if an access to the same
3444 : memory location dominates us, or the store is to a local addressable object)
3445 : and that the store has a "simple" RHS. */
3446 :
3447 : static bool
3448 435111 : cond_store_replacement (basic_block middle_bb, basic_block join_bb, edge e0,
3449 : edge e1, gimple *assign, hash_set<tree> *nontrap)
3450 : {
3451 435111 : tree lhs, rhs, name, name2;
3452 435111 : gphi *newphi;
3453 435111 : gassign *new_stmt;
3454 435111 : gimple_stmt_iterator gsi;
3455 435111 : location_t locus;
3456 :
3457 : /* Check if middle_bb contains of only one store. */
3458 435111 : if (!assign
3459 27380 : || !gimple_assign_single_p (assign)
3460 462491 : || gimple_has_volatile_ops (assign))
3461 : return false;
3462 :
3463 27159 : locus = gimple_location (assign);
3464 27159 : lhs = gimple_assign_lhs (assign);
3465 27159 : rhs = gimple_assign_rhs1 (assign);
3466 27159 : if ((!REFERENCE_CLASS_P (lhs)
3467 27159 : && !DECL_P (lhs))
3468 27159 : || !is_gimple_reg_type (TREE_TYPE (lhs)))
3469 : return false;
3470 :
3471 : /* Make sure all uses (except the rhs) in the single stmt are also available
3472 : where we insert to. */
3473 26693 : ssa_op_iter iter;
3474 26693 : tree use;
3475 51284 : FOR_EACH_SSA_TREE_OPERAND (use, assign, iter, SSA_OP_USE)
3476 : {
3477 29041 : if (use == rhs)
3478 13145 : continue;
3479 :
3480 15896 : gimple *stmt = SSA_NAME_DEF_STMT (use);
3481 15896 : if (stmt && gimple_bb (stmt) == middle_bb)
3482 : return false;
3483 : }
3484 :
3485 : /* Prove that we can move the store down. We could also check
3486 : TREE_THIS_NOTRAP here, but in that case we also could move stores,
3487 : whose value is not available readily, which we want to avoid. */
3488 22243 : if (nontrap->contains (lhs))
3489 : {
3490 : /* For local non-addressable variables, a load in the same bb
3491 : (though before) will cause the lhs to be in the nontrap hashset.
3492 : So need to check if there are no other loads in the middle bb.
3493 : FIXME: this is over conserative, this check could be made to
3494 : allow loads unrelated to lhs. */
3495 424 : tree vuse = gimple_vuse (assign);
3496 424 : imm_use_iterator iter;
3497 424 : gimple *use_stmt;
3498 1228 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vuse)
3499 : {
3500 1025 : if (use_stmt == assign)
3501 403 : continue;
3502 622 : if (gimple_bb (use_stmt) == middle_bb)
3503 221 : return false;
3504 424 : }
3505 : }
3506 : else
3507 : {
3508 : /* If LHS is an access to a local variable without address-taken
3509 : (or when we allow data races) and known not to trap, we could
3510 : always safely move down the store. */
3511 21819 : if (ref_can_have_store_data_races (lhs)
3512 21819 : || lhs_could_trap_p (lhs))
3513 : return false;
3514 : }
3515 :
3516 : /* Now we've checked the constraints, so do the transformation:
3517 : 1) Remove the single store. */
3518 203 : gsi = gsi_for_stmt (assign);
3519 203 : unlink_stmt_vdef (assign);
3520 203 : gsi_remove (&gsi, true);
3521 203 : release_defs (assign);
3522 :
3523 : /* Make both store and load use alias-set zero as we have to
3524 : deal with the case of the store being a conditional change
3525 : of the dynamic type. */
3526 203 : lhs = unshare_expr (lhs);
3527 203 : tree *basep = &lhs;
3528 463 : while (handled_component_p (*basep))
3529 260 : basep = &TREE_OPERAND (*basep, 0);
3530 203 : if (TREE_CODE (*basep) == MEM_REF
3531 203 : || TREE_CODE (*basep) == TARGET_MEM_REF)
3532 94 : TREE_OPERAND (*basep, 1)
3533 188 : = fold_convert (ptr_type_node, TREE_OPERAND (*basep, 1));
3534 : else
3535 109 : *basep = build2 (MEM_REF, TREE_TYPE (*basep),
3536 : build_fold_addr_expr (*basep),
3537 : build_zero_cst (ptr_type_node));
3538 :
3539 : /* 2) Insert a load from the memory of the store to the temporary
3540 : on the edge which did not contain the store. */
3541 203 : gphi *vphi = get_virtual_phi (join_bb);
3542 203 : name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
3543 203 : new_stmt = gimple_build_assign (name, lhs);
3544 203 : gimple_set_location (new_stmt, locus);
3545 : /* Set the vuse for the new load. */
3546 203 : gimple_set_vuse (new_stmt, gimple_phi_arg_def (vphi, e1->dest_idx));
3547 203 : lhs = unshare_expr (lhs);
3548 203 : {
3549 : /* Set the no-warning bit on the rhs of the load to avoid uninit
3550 : warnings. */
3551 203 : tree rhs1 = gimple_assign_rhs1 (new_stmt);
3552 203 : suppress_warning (rhs1, OPT_Wuninitialized);
3553 : }
3554 203 : gsi_insert_on_edge (e1, new_stmt);
3555 :
3556 : /* 3) Create a PHI node at the join block, with one argument
3557 : holding the old RHS, and the other holding the temporary
3558 : where we stored the old memory contents. */
3559 203 : name2 = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
3560 203 : newphi = create_phi_node (name2, join_bb);
3561 203 : add_phi_arg (newphi, rhs, e0, locus);
3562 203 : add_phi_arg (newphi, name, e1, locus);
3563 :
3564 203 : new_stmt = gimple_build_assign (lhs, gimple_phi_result (newphi));
3565 :
3566 : /* Update the vdef for the new store statement. */
3567 203 : tree newvphilhs = make_ssa_name (gimple_vop (cfun));
3568 203 : tree vdef = gimple_phi_result (vphi);
3569 203 : gimple_set_vuse (new_stmt, newvphilhs);
3570 203 : gimple_set_vdef (new_stmt, vdef);
3571 203 : gimple_phi_set_result (vphi, newvphilhs);
3572 203 : SSA_NAME_DEF_STMT (vdef) = new_stmt;
3573 203 : update_stmt (vphi);
3574 :
3575 : /* 4) Insert that PHI node. */
3576 203 : gsi = gsi_after_labels (join_bb);
3577 203 : gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
3578 :
3579 203 : if (dump_file && (dump_flags & TDF_DETAILS))
3580 : {
3581 1 : fprintf (dump_file, "\nConditional store replacement happened!");
3582 1 : fprintf (dump_file, "\nReplaced the store with a load.");
3583 1 : fprintf (dump_file, "\nInserted a new PHI statement in joint block:\n");
3584 1 : print_gimple_stmt (dump_file, new_stmt, 0, TDF_VOPS|TDF_MEMSYMS);
3585 : }
3586 203 : statistics_counter_event (cfun, "conditional store replacement", 1);
3587 :
3588 203 : return true;
3589 : }
3590 :
3591 : /* Do the main work of conditional store replacement. */
3592 :
3593 : static bool
3594 111339 : cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,
3595 : basic_block join_bb, gimple *then_assign,
3596 : gimple *else_assign,
3597 : gphi *vphi)
3598 : {
3599 111339 : tree lhs_base, lhs, then_rhs, else_rhs, name;
3600 111339 : location_t then_locus, else_locus;
3601 111339 : gimple_stmt_iterator gsi;
3602 111339 : gphi *newphi = nullptr;
3603 111339 : gassign *new_stmt;
3604 :
3605 111339 : if (then_assign == NULL
3606 111339 : || !gimple_assign_single_p (then_assign)
3607 111339 : || else_assign == NULL
3608 111339 : || !gimple_assign_single_p (else_assign)
3609 111339 : || stmt_references_abnormal_ssa_name (then_assign)
3610 222664 : || stmt_references_abnormal_ssa_name (else_assign))
3611 : return false;
3612 :
3613 : /* Allow both being clobbers but no other volatile operations. */
3614 111325 : if (gimple_clobber_p (then_assign)
3615 111325 : && gimple_clobber_p (else_assign))
3616 : ;
3617 204733 : else if (gimple_has_volatile_ops (then_assign)
3618 204733 : || gimple_has_volatile_ops (else_assign))
3619 : return false;
3620 :
3621 93787 : lhs = gimple_assign_lhs (then_assign);
3622 93787 : if (!operand_equal_p (lhs, gimple_assign_lhs (else_assign), 0))
3623 : return false;
3624 :
3625 33678 : lhs_base = get_base_address (lhs);
3626 33678 : if (lhs_base == NULL_TREE
3627 33678 : || (!DECL_P (lhs_base) && TREE_CODE (lhs_base) != MEM_REF))
3628 : return false;
3629 :
3630 33633 : then_rhs = gimple_assign_rhs1 (then_assign);
3631 33633 : else_rhs = gimple_assign_rhs1 (else_assign);
3632 33633 : then_locus = gimple_location (then_assign);
3633 33633 : else_locus = gimple_location (else_assign);
3634 :
3635 33633 : if (!is_gimple_reg_type (TREE_TYPE (lhs)))
3636 : {
3637 : /* Handle clobbers separately as operand_equal_p does not check
3638 : the kind of the clobbers being the same. */
3639 7045 : if (TREE_CLOBBER_P (then_rhs) && TREE_CLOBBER_P (else_rhs))
3640 : {
3641 2809 : if (CLOBBER_KIND (then_rhs) != CLOBBER_KIND (else_rhs))
3642 : return false;
3643 : }
3644 4236 : else if (!operand_equal_p (then_rhs, else_rhs))
3645 : return false;
3646 : /* Currently only handle commoning of `= {}`. */
3647 3369 : if (TREE_CODE (then_rhs) != CONSTRUCTOR)
3648 : return false;
3649 : }
3650 :
3651 29533 : if (dump_file && (dump_flags & TDF_DETAILS))
3652 : {
3653 11 : if (TREE_CLOBBER_P (then_rhs))
3654 3 : fprintf(dump_file, "factoring out clobber:\n\tthen:\n");
3655 : else
3656 8 : fprintf(dump_file, "factoring out stores:\n\tthen:\n");
3657 11 : print_gimple_stmt (dump_file, then_assign, 0,
3658 : TDF_VOPS|TDF_MEMSYMS);
3659 11 : fprintf(dump_file, "\telse:\n");
3660 11 : print_gimple_stmt (dump_file, else_assign, 0,
3661 : TDF_VOPS|TDF_MEMSYMS);
3662 11 : fprintf (dump_file, "\n");
3663 : }
3664 :
3665 : /* Now we've checked the constraints, so do the transformation:
3666 : 1) Remove the stores. */
3667 29533 : gsi = gsi_for_stmt (then_assign);
3668 29533 : unlink_stmt_vdef (then_assign);
3669 29533 : gsi_remove (&gsi, true);
3670 29533 : release_defs (then_assign);
3671 :
3672 29533 : gsi = gsi_for_stmt (else_assign);
3673 29533 : unlink_stmt_vdef (else_assign);
3674 29533 : gsi_remove (&gsi, true);
3675 29533 : release_defs (else_assign);
3676 :
3677 : /* 2) Create a PHI node at the join block, with one argument
3678 : holding the old RHS, and the other holding the temporary
3679 : where we stored the old memory contents. */
3680 29533 : if (operand_equal_p (then_rhs, else_rhs))
3681 : name = then_rhs;
3682 : else
3683 : {
3684 24220 : name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
3685 24220 : newphi = create_phi_node (name, join_bb);
3686 24220 : add_phi_arg (newphi, then_rhs, EDGE_SUCC (then_bb, 0), then_locus);
3687 24220 : add_phi_arg (newphi, else_rhs, EDGE_SUCC (else_bb, 0), else_locus);
3688 : }
3689 :
3690 29533 : new_stmt = gimple_build_assign (lhs, name);
3691 : /* Update the vdef for the new store statement. */
3692 29533 : tree newvphilhs = make_ssa_name (gimple_vop (cfun));
3693 29533 : tree vdef = gimple_phi_result (vphi);
3694 29533 : gimple_set_vuse (new_stmt, newvphilhs);
3695 29533 : gimple_set_vdef (new_stmt, vdef);
3696 29533 : gimple_phi_set_result (vphi, newvphilhs);
3697 29533 : SSA_NAME_DEF_STMT (vdef) = new_stmt;
3698 29533 : update_stmt (vphi);
3699 29533 : if (dump_file && (dump_flags & TDF_DETAILS))
3700 : {
3701 11 : if (newphi)
3702 : {
3703 4 : fprintf(dump_file, "to use phi:\n");
3704 4 : print_gimple_stmt (dump_file, newphi, 0,
3705 : TDF_VOPS|TDF_MEMSYMS);
3706 4 : fprintf(dump_file, "\n");
3707 : }
3708 : else
3709 7 : fprintf(dump_file, "to:\n");
3710 11 : print_gimple_stmt (dump_file, new_stmt, 0,
3711 : TDF_VOPS|TDF_MEMSYMS);
3712 11 : fprintf(dump_file, "\n\n");
3713 : }
3714 :
3715 : /* 3) Insert that new store. */
3716 29533 : gsi = gsi_after_labels (join_bb);
3717 29533 : gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
3718 :
3719 29533 : statistics_counter_event (cfun, "if-then-else store replacement", 1);
3720 :
3721 29533 : return true;
3722 : }
3723 :
3724 : /* Return the last store in BB with VDEF or NULL if there are
3725 : loads following the store. VUSE_ONLY is where the only use of the
3726 : vdef should be. If ONLYONESTORE is true, then the store is
3727 : the only store in the BB. */
3728 :
3729 : static gimple *
3730 3441550 : trailing_store_in_bb (basic_block bb, tree vdef, gimple *vuse_only, bool onlyonestore)
3731 : {
3732 3441550 : if (SSA_NAME_IS_DEFAULT_DEF (vdef))
3733 : return NULL;
3734 3411990 : gimple *store = SSA_NAME_DEF_STMT (vdef);
3735 3411990 : if (gimple_bb (store) != bb
3736 3411990 : || gimple_code (store) == GIMPLE_PHI)
3737 : return NULL;
3738 :
3739 : /* Verify there is no other store in this BB if requested. */
3740 3310293 : if (onlyonestore
3741 1653530 : && !SSA_NAME_IS_DEFAULT_DEF (gimple_vuse (store))
3742 1256097 : && gimple_bb (SSA_NAME_DEF_STMT (gimple_vuse (store))) == bb
3743 3795318 : && gimple_code (SSA_NAME_DEF_STMT (gimple_vuse (store))) != GIMPLE_PHI)
3744 : return NULL;
3745 :
3746 : // Needs to be an simple store and not a call.
3747 2825492 : if (!gimple_assign_single_p (store))
3748 : return NULL;
3749 :
3750 : /* Verify there is no load or store after the store, the vdef of the store
3751 : should only be used by the vuse_only. */
3752 1082242 : use_operand_p use_p;
3753 1082242 : gimple *use_stmt;
3754 2164484 : if (!single_imm_use (gimple_vdef (store), &use_p, &use_stmt))
3755 : return NULL;
3756 1057431 : if (use_stmt != vuse_only)
3757 0 : return NULL;
3758 :
3759 : return store;
3760 : }
3761 :
3762 : /* Takes a MEM and changes the aliasing set to be zero on it.
3763 : This handles all variants include decls. */
3764 : static tree
3765 1600 : copy_mem_with_alias_set_zero (tree mem)
3766 : {
3767 1600 : mem = unshare_expr (mem);
3768 1600 : tree *basep = &mem;
3769 3310 : while (handled_component_p (*basep))
3770 1710 : basep = &TREE_OPERAND (*basep, 0);
3771 1600 : if (TREE_CODE (*basep) == MEM_REF
3772 1600 : || TREE_CODE (*basep) == TARGET_MEM_REF)
3773 230 : TREE_OPERAND (*basep, 1)
3774 460 : = fold_convert (ptr_type_node, TREE_OPERAND (*basep, 1));
3775 : else
3776 1370 : *basep = build2 (MEM_REF, TREE_TYPE (*basep),
3777 : build_fold_addr_expr (*basep),
3778 : build_zero_cst (ptr_type_node));
3779 1600 : return mem;
3780 : }
3781 :
3782 : /* Do the main work of a limited conditional store replacement.
3783 : This recognized pattern like so:
3784 :
3785 : COND_BB:
3786 : store = a_1;
3787 : // no loads
3788 : if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
3789 : MIDDLE_BB:
3790 : something // no loads
3791 : store = a_2;
3792 : something // no loads
3793 : fallthrough (edge E0)
3794 : JOIN_BB:
3795 : some more
3796 :
3797 : This is a limited form of the full cond_store_replacement
3798 : to be allowed from use from phiopt and can be done
3799 : without calculating the non-trapping cases. */
3800 : static bool
3801 1470681 : cond_store_replacement_limited (basic_block middle_bb, basic_block join_bb,
3802 : basic_block cond_bb,
3803 : edge e0, edge e1,
3804 : bool caninsert_edge)
3805 : {
3806 1470681 : tree lhs, rhs;
3807 1470681 : location_t locus;
3808 : /* Currently don't handle more than 2 incoming edges
3809 : into the merge bb. */
3810 1470681 : if (EDGE_COUNT (join_bb->preds) > 2)
3811 : return false;
3812 : /* The middle bb needs to have a single predecessor of the cond_bb. */
3813 1470681 : if (!single_pred_p (middle_bb))
3814 : return false;
3815 1470681 : gphi *vphi = get_virtual_phi (join_bb);
3816 1470681 : if (!vphi)
3817 : return false;
3818 1301411 : tree middle_vdef = gimple_phi_arg_def_from_edge (vphi, e0);
3819 : /* Check if middle_bb contains of only one store. */
3820 1301411 : gimple *store_middle;
3821 1301411 : store_middle = trailing_store_in_bb (middle_bb, middle_vdef,
3822 : vphi, true);
3823 :
3824 1301411 : if (!store_middle
3825 142392 : || !gimple_assign_single_p (store_middle)
3826 142392 : || gimple_has_volatile_ops (store_middle)
3827 : // Rejects clobbers too.
3828 1442892 : || gimple_clobber_p (store_middle))
3829 : return false;
3830 :
3831 141481 : locus = gimple_location (store_middle);
3832 141481 : lhs = gimple_assign_lhs (store_middle);
3833 141481 : rhs = gimple_assign_rhs1 (store_middle);
3834 141481 : if ((!REFERENCE_CLASS_P (lhs)
3835 141481 : && !DECL_P (lhs))
3836 141481 : || !is_gimple_reg_type (TREE_TYPE (lhs)))
3837 : return false;
3838 :
3839 : /* Three cases that can be handled:
3840 : 1) the lhs is stored to right before the condition.
3841 : Will remove the store before the condition.
3842 : 2) Or the lhs is loaded from right before the condition.
3843 : 3) Neither of these. (this will insert a load in the other edge)
3844 : For case 2 and 3, check for data races.
3845 : For case 2, the load can either be based on a local variable
3846 : or a known non-trapping decl.
3847 : For case 3, the store needs to known to be non-trapping. */
3848 139210 : tree vuse = gimple_vuse (store_middle);
3849 139210 : gimple *beforestore = nullptr;
3850 139210 : gimple *vdef_before = SSA_NAME_DEF_STMT (vuse);
3851 139210 : tree other_rhs = nullptr;
3852 :
3853 : /* See if there is a store before the condition case. */
3854 139210 : if (gimple_assign_single_p (vdef_before))
3855 : {
3856 35469 : tree beforelhs = gimple_assign_lhs (vdef_before);
3857 : /* Only allow the store to be right before the condition. */
3858 35469 : if (gimple_bb (vdef_before) == cond_bb
3859 : /* This can't be a clobber */
3860 30108 : && !gimple_clobber_p (vdef_before)
3861 : /* An exact match is only supported.
3862 : FIXME: Allow for clique/base mismatch? */
3863 61391 : && operand_equal_p (lhs, beforelhs))
3864 : {
3865 : /* The vuse of the of store in the middle should be also
3866 : the entry in the phi for the other edge. */
3867 5429 : gcc_assert (vuse == gimple_phi_arg_def_from_edge (vphi, e1));
3868 5429 : tree vuse = gimple_vuse (store_middle);
3869 5429 : imm_use_iterator iter;
3870 5429 : gimple *use_stmt;
3871 5429 : bool has_load = false;
3872 : /* If there is a load, then just reuse the value and not
3873 : remove the old store as that might be used by the load. */
3874 15702 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vuse)
3875 : {
3876 10886 : if (use_stmt != store_middle
3877 10886 : && use_stmt != vphi)
3878 : {
3879 : has_load = true;
3880 : break;
3881 : }
3882 5429 : }
3883 5429 : other_rhs = gimple_assign_rhs1 (vdef_before);
3884 : /* If there is no load, then keep the reference to the store stmt. */
3885 5429 : if (!has_load)
3886 4816 : beforestore = vdef_before;
3887 : }
3888 : }
3889 : /*
3890 : case 2:
3891 : a = local_var[n];
3892 : if (b)
3893 : local_var[n] = c;
3894 :
3895 : case 3:
3896 : if (b)
3897 : nontrapping = c;
3898 :
3899 : For case 3, nontrapping needs to satisfy tree_could_trap_p.
3900 : In both cases ref_can_have_store_data_races needs to be satisfy.
3901 : */
3902 :
3903 : /* Maybe the load/local non-escaped variable case. */
3904 5429 : if (!other_rhs)
3905 : {
3906 133781 : tree lhsbase = get_base_address (lhs);
3907 : /* If this store ref can't have data races, a store
3908 : that was conditional can't become unconditional. */
3909 133781 : if (ref_can_have_store_data_races (lhs))
3910 132181 : return false;
3911 8355 : tree vuse = gimple_vuse (store_middle);
3912 8355 : imm_use_iterator iter;
3913 8355 : gimple *use_stmt;
3914 : /* Try to find the load before the store that matches
3915 : if we have a local variable or a non trapping store. */
3916 15877 : if ((auto_var_p (lhsbase) && !TREE_ADDRESSABLE (lhsbase))
3917 8391 : || !lhs_could_trap_p (lhs))
3918 : {
3919 36545 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vuse)
3920 : {
3921 29897 : if (gimple_bb (use_stmt) != cond_bb)
3922 21341 : continue;
3923 : /* Looking for a load only. */
3924 8556 : if (!gimple_assign_load_p (use_stmt))
3925 0 : continue;
3926 8556 : tree rhs = gimple_assign_rhs1 (use_stmt);
3927 8556 : if (!operand_equal_p (rhs, lhs))
3928 7651 : continue;
3929 905 : other_rhs = gimple_assign_lhs (use_stmt);
3930 905 : lhs = copy_mem_with_alias_set_zero (lhs);
3931 905 : break;
3932 7553 : }
3933 : }
3934 8355 : if (!other_rhs)
3935 : {
3936 7450 : gassign *new_stmt;
3937 : /* If not allowing inserting on the edge, then don't. */
3938 7450 : if (!caninsert_edge)
3939 : return false;
3940 : /* If LHS is an access to a local variable without address-taken
3941 : (or when we allow data races) and known not to trap, we could
3942 : always safely move down the store. */
3943 2229 : if (lhs_could_trap_p (lhs))
3944 : return false;
3945 695 : lhs = copy_mem_with_alias_set_zero (lhs);
3946 : /* Insert a load from the memory of the store to the temporary
3947 : on the edge which did not contain the store. */
3948 695 : other_rhs = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
3949 695 : new_stmt = gimple_build_assign (other_rhs, lhs);
3950 695 : gimple_set_location (new_stmt, locus);
3951 : /* Set the vuse for the new load. */
3952 695 : gimple_set_vuse (new_stmt,
3953 695 : gimple_phi_arg_def (vphi, e1->dest_idx));
3954 695 : lhs = unshare_expr (lhs);
3955 695 : {
3956 : /* Set the no-warning bit on the rhs of the load to avoid
3957 : uninit warnings. */
3958 695 : tree rhs1 = gimple_assign_rhs1 (new_stmt);
3959 695 : suppress_warning (rhs1, OPT_Wuninitialized);
3960 : }
3961 695 : gsi_insert_on_edge (e1, new_stmt);
3962 : }
3963 : }
3964 :
3965 7029 : gphi *newphi;
3966 7029 : gassign *new_stmt;
3967 7029 : gimple_stmt_iterator gsi;
3968 : /* Now we've checked the constraints, so do the transformation:
3969 : 1) Remove the store(s). */
3970 7029 : gsi = gsi_for_stmt (store_middle);
3971 7029 : unlink_stmt_vdef (store_middle);
3972 7029 : gsi_remove (&gsi, true);
3973 7029 : release_defs (store_middle);
3974 :
3975 : /* Remove the store before the conditional if possible. */
3976 7029 : if (beforestore)
3977 : {
3978 4816 : gsi = gsi_for_stmt (beforestore);
3979 4816 : unlink_stmt_vdef (beforestore);
3980 4816 : gsi_remove (&gsi, true);
3981 4816 : release_defs (beforestore);
3982 : }
3983 :
3984 :
3985 : /* 2) Create a PHI node at the join block, with one argument
3986 : holding the old RHS, and the other holding the temporary
3987 : where we stored the old memory contents. */
3988 7029 : tree phiname = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
3989 7029 : newphi = create_phi_node (phiname, join_bb);
3990 7029 : add_phi_arg (newphi, rhs, e0, locus);
3991 7029 : add_phi_arg (newphi, other_rhs, e1, locus);
3992 :
3993 : /* 3. Create the new store. */
3994 7029 : new_stmt = gimple_build_assign (lhs, phiname);
3995 :
3996 : /* Update the vdef for the new store statement. */
3997 7029 : tree newvphilhs = make_ssa_name (gimple_vop (cfun));
3998 7029 : tree vdef = gimple_phi_result (vphi);
3999 7029 : gimple_set_vuse (new_stmt, newvphilhs);
4000 7029 : gimple_set_vdef (new_stmt, vdef);
4001 7029 : gimple_phi_set_result (vphi, newvphilhs);
4002 7029 : SSA_NAME_DEF_STMT (vdef) = new_stmt;
4003 7029 : update_stmt (vphi);
4004 :
4005 7029 : gsi = gsi_after_labels (join_bb);
4006 7029 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
4007 :
4008 7029 : if (dump_file && (dump_flags & TDF_DETAILS))
4009 : {
4010 14 : fprintf (dump_file, "\nConditional store replacement happened!");
4011 14 : if (beforestore)
4012 5 : fprintf (dump_file, "\nRemoved the store before the condition.");
4013 14 : fprintf (dump_file, "\nInserted a new PHI statement in joint block:\n");
4014 14 : print_gimple_stmt (dump_file, new_stmt, 0, TDF_VOPS|TDF_MEMSYMS);
4015 : }
4016 7029 : statistics_counter_event (cfun, "conditional store replacement", 1);
4017 7029 : return true;
4018 : }
4019 :
4020 :
4021 : /* Return the only store in MIDDLE_BB as the candidate store for cselim. Return
4022 : NULL if no candidate can be found. */
4023 :
4024 : static gimple *
4025 435111 : cselim_candidate (basic_block middle_bb, basic_block join_bb, edge e0)
4026 : {
4027 435111 : gphi *vphi = get_virtual_phi (join_bb);
4028 435111 : if (!vphi)
4029 : return NULL;
4030 :
4031 265841 : tree middle_vdef = PHI_ARG_DEF_FROM_EDGE (vphi, e0);
4032 265841 : return trailing_store_in_bb (middle_bb, middle_vdef, vphi, true);
4033 : }
4034 :
4035 : /* Limited Conditional store replacement. We already know
4036 : that the recognized pattern looks like so:
4037 :
4038 : split:
4039 : if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
4040 : THEN_BB:
4041 : ...
4042 : STORE = Y;
4043 : ...
4044 : goto JOIN_BB;
4045 : ELSE_BB:
4046 : ...
4047 : STORE = Z;
4048 : ...
4049 : fallthrough (edge E0)
4050 : JOIN_BB:
4051 : some more
4052 :
4053 : Handles only the case with store in THEN_BB and ELSE_BB. That is
4054 : cheap enough due to in phiopt and not worry about heurstics. Moving the store
4055 : out might provide an opportunity for a phiopt to happen.
4056 : At -O1 (!flag_expensive_optimizations), this only handles the only store in
4057 : the BBs. */
4058 :
4059 : static bool
4060 957620 : cond_if_else_store_replacement_limited (basic_block then_bb, basic_block else_bb,
4061 : basic_block join_bb)
4062 : {
4063 957620 : gphi *vphi = get_virtual_phi (join_bb);
4064 957620 : if (!vphi)
4065 : return false;
4066 :
4067 957620 : tree then_vdef = PHI_ARG_DEF_FROM_EDGE (vphi, single_succ_edge (then_bb));
4068 1915240 : gimple *then_assign = trailing_store_in_bb (then_bb, then_vdef, vphi,
4069 957620 : !flag_expensive_optimizations);
4070 957620 : if (!then_assign)
4071 : return false;
4072 :
4073 776738 : tree else_vdef = PHI_ARG_DEF_FROM_EDGE (vphi, single_succ_edge (else_bb));
4074 1553476 : gimple *else_assign = trailing_store_in_bb (else_bb, else_vdef, vphi,
4075 776738 : !flag_expensive_optimizations);
4076 776738 : if (!else_assign)
4077 : return false;
4078 :
4079 99507 : if (!cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
4080 : then_assign, else_assign, vphi))
4081 : {
4082 71563 : gimple *then_n, *else_n;
4083 :
4084 : // Try to see if one "store" can be skipped on either side.
4085 71563 : if (!flag_expensive_optimizations)
4086 : return false;
4087 139940 : then_n = trailing_store_in_bb (then_bb, gimple_vuse (then_assign),
4088 : then_assign, true);
4089 69970 : if (then_n)
4090 : {
4091 7404 : ao_ref then_ref_n;
4092 7404 : ao_ref_init (&then_ref_n, gimple_assign_lhs (then_n));
4093 7404 : if (stmt_may_clobber_ref_p_1 (then_assign, &then_ref_n, false)
4094 7404 : || ref_maybe_used_by_stmt_p (then_assign, &then_ref_n, false))
4095 : then_n = nullptr;
4096 : }
4097 139940 : else_n = trailing_store_in_bb (else_bb, gimple_vuse (else_assign),
4098 : else_assign, true);
4099 69970 : if (else_n)
4100 : {
4101 4010 : ao_ref else_ref_n;
4102 4010 : ao_ref_init (&else_ref_n, gimple_assign_lhs (else_n));
4103 4010 : if (stmt_may_clobber_ref_p_1 (else_assign, &else_ref_n, false)
4104 4010 : || ref_maybe_used_by_stmt_p (else_assign, &else_ref_n, false))
4105 : else_n = nullptr;
4106 : }
4107 69970 : if (then_n
4108 69970 : && cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
4109 : then_n, else_assign, vphi))
4110 : return true;
4111 69650 : if (else_n
4112 69650 : && cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
4113 : then_assign, else_n, vphi))
4114 : return true;
4115 69591 : if (else_n && then_n
4116 69591 : && cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
4117 : then_n, else_n, vphi))
4118 99 : return true;
4119 : return false;
4120 : }
4121 : return true;
4122 : }
4123 :
4124 : /* Conditional store replacement. We already know
4125 : that the recognized pattern looks like so:
4126 :
4127 : split:
4128 : if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
4129 : THEN_BB:
4130 : ...
4131 : X = Y;
4132 : ...
4133 : goto JOIN_BB;
4134 : ELSE_BB:
4135 : ...
4136 : X = Z;
4137 : ...
4138 : fallthrough (edge E0)
4139 : JOIN_BB:
4140 : some more
4141 :
4142 : We check that it is safe to sink the store to JOIN_BB by verifying that
4143 : there are no read-after-write or write-after-write dependencies in
4144 : THEN_BB and ELSE_BB. */
4145 :
4146 : static bool
4147 241537 : cond_if_else_store_replacement (basic_block then_bb, basic_block else_bb,
4148 : basic_block join_bb)
4149 : {
4150 241537 : vec<data_reference_p> then_datarefs, else_datarefs;
4151 241537 : vec<ddr_p> then_ddrs, else_ddrs;
4152 241537 : gimple *then_store, *else_store;
4153 241537 : bool found, ok = false, res;
4154 241537 : tree then_lhs, else_lhs;
4155 241537 : basic_block blocks[3];
4156 241537 : gphi *vphi = get_virtual_phi (join_bb);
4157 241537 : if (!vphi)
4158 : return false;
4159 :
4160 : /* Handle the case with trailing stores in THEN_BB and ELSE_BB. That is
4161 : cheap enough to always handle as it allows us to elide dependence
4162 : checking. */
4163 240273 : while (cond_if_else_store_replacement_limited (then_bb, else_bb, join_bb))
4164 : ;
4165 :
4166 : /* If vectorization is disabled then do not sink any stores. */
4167 225466 : if (param_max_stores_to_sink == 0
4168 225465 : || (!flag_tree_loop_vectorize && !flag_tree_slp_vectorize))
4169 : return false;
4170 :
4171 : /* Find data references. */
4172 219608 : then_datarefs.create (1);
4173 219608 : else_datarefs.create (1);
4174 219608 : if ((find_data_references_in_bb (NULL, then_bb, &then_datarefs)
4175 219608 : == chrec_dont_know)
4176 194209 : || !then_datarefs.length ()
4177 187899 : || (find_data_references_in_bb (NULL, else_bb, &else_datarefs)
4178 187899 : == chrec_dont_know)
4179 235495 : || !else_datarefs.length ())
4180 : {
4181 204892 : free_data_refs (then_datarefs);
4182 204892 : free_data_refs (else_datarefs);
4183 204892 : return false;
4184 : }
4185 :
4186 : /* Clear visited on else stores, we want to make sure to pick each store
4187 : at most once to avoid quadratic behavior. */
4188 67145 : for (auto else_dr : else_datarefs)
4189 : {
4190 37713 : if (DR_IS_READ (else_dr))
4191 19556 : continue;
4192 18157 : gimple_set_visited (DR_STMT (else_dr), false);
4193 : }
4194 :
4195 : /* Find pairs of stores with equal LHS. Work from the end to avoid
4196 : re-ordering stores unnecessarily. */
4197 14716 : auto_vec<std::pair<gimple *, gimple *>, 1> stores_pairs;
4198 14716 : unsigned i;
4199 14716 : data_reference_p then_dr;
4200 62073 : FOR_EACH_VEC_ELT_REVERSE (then_datarefs, i, then_dr)
4201 : {
4202 32641 : if (DR_IS_READ (then_dr))
4203 32641 : continue;
4204 :
4205 19401 : then_store = DR_STMT (then_dr);
4206 19401 : then_lhs = gimple_get_lhs (then_store);
4207 19401 : if (then_lhs == NULL_TREE)
4208 0 : continue;
4209 19401 : found = false;
4210 :
4211 19401 : unsigned j;
4212 19401 : data_reference_p else_dr;
4213 92070 : FOR_EACH_VEC_ELT_REVERSE (else_datarefs, j, else_dr)
4214 : {
4215 56766 : if (DR_IS_READ (else_dr))
4216 20365 : continue;
4217 :
4218 36401 : else_store = DR_STMT (else_dr);
4219 36401 : if (gimple_visited_p (else_store))
4220 3333 : continue;
4221 33068 : else_lhs = gimple_get_lhs (else_store);
4222 33068 : if (else_lhs == NULL_TREE)
4223 0 : continue;
4224 :
4225 33068 : if (operand_equal_p (then_lhs, else_lhs, 0))
4226 : {
4227 : found = true;
4228 : break;
4229 : }
4230 : }
4231 :
4232 19401 : if (!found)
4233 15903 : continue;
4234 :
4235 3498 : gimple_set_visited (else_store, true);
4236 3498 : stores_pairs.safe_push (std::make_pair (then_store, else_store));
4237 : }
4238 :
4239 : /* No pairs of stores found. */
4240 14716 : if (!stores_pairs.length ()
4241 14716 : || stores_pairs.length () > (unsigned) param_max_stores_to_sink)
4242 : {
4243 12725 : free_data_refs (then_datarefs);
4244 12725 : free_data_refs (else_datarefs);
4245 12725 : return false;
4246 : }
4247 :
4248 : /* Compute and check data dependencies in both basic blocks. */
4249 1991 : then_ddrs.create (1);
4250 1991 : else_ddrs.create (1);
4251 1991 : if (!compute_all_dependences (then_datarefs, &then_ddrs,
4252 1991 : vNULL, false)
4253 3982 : || !compute_all_dependences (else_datarefs, &else_ddrs,
4254 1991 : vNULL, false))
4255 : {
4256 0 : free_dependence_relations (then_ddrs);
4257 0 : free_dependence_relations (else_ddrs);
4258 0 : free_data_refs (then_datarefs);
4259 0 : free_data_refs (else_datarefs);
4260 0 : return false;
4261 : }
4262 1991 : blocks[0] = then_bb;
4263 1991 : blocks[1] = else_bb;
4264 1991 : blocks[2] = join_bb;
4265 1991 : renumber_gimple_stmt_uids_in_blocks (blocks, 3);
4266 :
4267 : /* Check that there are no read-after-write or write-after-write dependencies
4268 : in THEN_BB. */
4269 12322 : for (auto ddr : then_ddrs)
4270 : {
4271 7109 : struct data_reference *dra = DDR_A (ddr);
4272 7109 : struct data_reference *drb = DDR_B (ddr);
4273 :
4274 7109 : if (DDR_ARE_DEPENDENT (ddr) != chrec_known
4275 7109 : && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
4276 771 : && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
4277 1531 : || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
4278 495 : && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
4279 1036 : || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
4280 : {
4281 760 : free_dependence_relations (then_ddrs);
4282 760 : free_dependence_relations (else_ddrs);
4283 760 : free_data_refs (then_datarefs);
4284 760 : free_data_refs (else_datarefs);
4285 760 : return false;
4286 : }
4287 : }
4288 :
4289 : /* Check that there are no read-after-write or write-after-write dependencies
4290 : in ELSE_BB. */
4291 8053 : for (auto ddr : else_ddrs)
4292 : {
4293 4524 : struct data_reference *dra = DDR_A (ddr);
4294 4524 : struct data_reference *drb = DDR_B (ddr);
4295 :
4296 4524 : if (DDR_ARE_DEPENDENT (ddr) != chrec_known
4297 4524 : && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
4298 395 : && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
4299 559 : || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
4300 140 : && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
4301 419 : || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
4302 : {
4303 164 : free_dependence_relations (then_ddrs);
4304 164 : free_dependence_relations (else_ddrs);
4305 164 : free_data_refs (then_datarefs);
4306 164 : free_data_refs (else_datarefs);
4307 164 : return false;
4308 : }
4309 : }
4310 :
4311 : /* Sink stores with same LHS. */
4312 4592 : for (auto &store_pair : stores_pairs)
4313 : {
4314 1391 : then_store = store_pair.first;
4315 1391 : else_store = store_pair.second;
4316 1391 : res = cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
4317 : then_store, else_store, vphi);
4318 1391 : ok = ok || res;
4319 : }
4320 :
4321 1067 : free_dependence_relations (then_ddrs);
4322 1067 : free_dependence_relations (else_ddrs);
4323 1067 : free_data_refs (then_datarefs);
4324 1067 : free_data_refs (else_datarefs);
4325 :
4326 1067 : return ok;
4327 14716 : }
4328 :
4329 : /* Returns true when P is based on an induction variable
4330 : inside MERGE's inner most loop. */
4331 : static bool
4332 1312 : induction_based (tree p, basic_block merge)
4333 : {
4334 1312 : if (TREE_CODE (p) != SSA_NAME)
4335 : return false;
4336 594 : tree ev = analyze_scalar_evolution (merge->loop_father, p);
4337 594 : if (chrec_contains_undetermined (ev)
4338 594 : || chrec_contains_symbols_defined_in_loop (ev, merge->loop_father->num))
4339 : return false;
4340 140 : if (tree_does_not_contain_chrecs (ev))
4341 : return false;
4342 : return true;
4343 : }
4344 :
4345 : /* If PHI at MERGE is a "load PHI", PHI <*P, *Q> whose two arguments are
4346 : single-use, non-volatile scalar MEM_REF loads reading the same memory state
4347 : (same VUSE), factor the load out: introduce P' = PHI <P, Q> and a single
4348 : load *P' replacing the PHI. No speculative load is introduced (the load uses
4349 : whichever pointer the taken edge selected).
4350 : E0/E1 are the middle bbs to MERGE edges.
4351 : EARLY_P is set when the first phiopt is run.
4352 : BEFORE_VECT is true if this is before vectorization, where some extra checks
4353 : are needed for profitability.
4354 : Returns true if a load was factored out. */
4355 :
4356 : static bool
4357 998476 : factor_out_conditional_load (edge e0, edge e1, basic_block merge, gphi *phi,
4358 : bool early_p, bool before_vect)
4359 : {
4360 : /* Not a virtual operand. */
4361 1295153 : if (virtual_operand_p (gimple_phi_result (phi))
4362 : /* can only handle the merge bb having 2 predecessors. */
4363 1295153 : || gimple_phi_num_args (phi) != 2)
4364 : return false;
4365 :
4366 296677 : tree arg0 = gimple_phi_arg_def (phi, e0->dest_idx);
4367 296677 : tree arg1 = gimple_phi_arg_def (phi, e1->dest_idx);
4368 : /* The load needs to be only used in the phi. */
4369 252650 : if (TREE_CODE (arg0) != SSA_NAME || TREE_CODE (arg1) != SSA_NAME
4370 532043 : || !has_single_use (arg0) || !has_single_use (arg1))
4371 : return false;
4372 :
4373 : /* Re-derive the loads and pointers validated by the predicate above. */
4374 125276 : gimple *load0 = SSA_NAME_DEF_STMT (arg0);
4375 125276 : gimple *load1 = SSA_NAME_DEF_STMT (arg1);
4376 :
4377 : /* Load have to need to be in the middle bbs. */
4378 125276 : if (gimple_bb (load0) != e0->src
4379 125276 : || gimple_bb (load1) != e1->src)
4380 : return false;
4381 :
4382 : /* The load needs to be a load with NO volatile ops. */
4383 149191 : if (!gimple_assign_load_p (load0) || !gimple_assign_load_p (load1)
4384 168284 : || gimple_has_volatile_ops (load0) || gimple_has_volatile_ops (load1))
4385 : return false;
4386 :
4387 : /* Allow for stores/calls before the load. */
4388 25337 : if (gphi *vphi = get_virtual_phi (merge))
4389 : {
4390 28532 : if (gimple_vuse (load0) != gimple_phi_arg_def (vphi, e0->dest_idx)
4391 24914 : || gimple_vuse (load1) != gimple_phi_arg_def (vphi, e1->dest_idx))
4392 : return false;
4393 : }
4394 : /* Sometimes due to not removing dead statements,
4395 : a virtual phi does not show up going into an infinite loop
4396 : so just reject that case. */
4397 33213 : else if (gimple_vuse (load0) != gimple_vuse (load1))
4398 : return false;
4399 :
4400 21642 : tree ref0 = gimple_assign_rhs1 (load0);
4401 21642 : tree ref1 = gimple_assign_rhs1 (load1);
4402 21642 : tree index = nullptr;
4403 21642 : tree step = nullptr;
4404 21642 : tree index2 = nullptr;
4405 21642 : bool rev_order = false;
4406 :
4407 : /* Both must be *P loads of a compatible value type. The
4408 : TBAA alias-ptr type carried by MEM_REF operand 1 need not match; it is
4409 : merged the way get_alias_type_for_stmts does when the load is built. */
4410 21642 : if (TREE_CODE (ref0) == MEM_REF)
4411 6058 : rev_order = REF_REVERSE_STORAGE_ORDER (ref0);
4412 : else
4413 : {
4414 15584 : if (TREE_CODE (ref0) != TARGET_MEM_REF)
4415 : return false;
4416 372 : index = TMR_INDEX (ref0);
4417 372 : step = TMR_STEP (ref0);
4418 372 : index2 = TMR_INDEX2 (ref0);
4419 : }
4420 6430 : if (TREE_CODE (ref1) == MEM_REF)
4421 : {
4422 5816 : if (index || step || index2)
4423 : return false;
4424 5816 : if (rev_order != REF_REVERSE_STORAGE_ORDER (ref1))
4425 : return false;
4426 : }
4427 : else
4428 : {
4429 614 : if (TREE_CODE (ref1) != TARGET_MEM_REF)
4430 : return false;
4431 382 : if (rev_order)
4432 : return false;
4433 382 : if (!safe_operand_equal_p (index, TMR_INDEX (ref1)))
4434 : return false;
4435 369 : if (!safe_operand_equal_p (step, TMR_STEP (ref1)))
4436 : return false;
4437 369 : if (!safe_operand_equal_p (index2, TMR_INDEX2 (ref1)))
4438 : return false;
4439 : }
4440 :
4441 6170 : if (!types_compatible_p (TREE_TYPE (ref0), TREE_TYPE (ref1)))
4442 : return false;
4443 :
4444 : /* The alignment of the two accesses need to be the same. */
4445 6170 : if (TYPE_ALIGN (TREE_TYPE (ref0)) != TYPE_ALIGN (TREE_TYPE (ref1)))
4446 : return false;
4447 :
4448 5908 : tree p0 = TREE_OPERAND (ref0, 0);
4449 5908 : tree p1 = TREE_OPERAND (ref1, 0);
4450 5908 : if (!is_factor_profitable (load0, merge, &p0, 1))
4451 : return false;
4452 5890 : if (!is_factor_profitable (load1, merge, &p1, 1))
4453 : return false;
4454 :
4455 : /* Merge the two arms' TBAA info as get_alias_type_for_stmts does: keep the
4456 : common alias-ptr type and dependence clique/base when the arms agree,
4457 : otherwise fall back to ptr_type_node (alias-everything) and drop the
4458 : clique/base, so the combined load conservatively conflicts with any store
4459 : either original arm could. */
4460 5890 : unsigned short clique = MR_DEPENDENCE_CLIQUE (ref0);
4461 5890 : unsigned short base = MR_DEPENDENCE_BASE (ref0);
4462 5890 : if (clique != MR_DEPENDENCE_CLIQUE (ref1) || base != MR_DEPENDENCE_BASE (ref1))
4463 : clique = base = 0;
4464 5890 : tree atype = TREE_TYPE (TREE_OPERAND (ref0, 1));
4465 5890 : if (!alias_ptr_types_compatible_p (atype, TREE_TYPE (TREE_OPERAND (ref1, 1))))
4466 : {
4467 625 : atype = ptr_type_node;
4468 625 : clique = base = 0;
4469 : }
4470 :
4471 5890 : tree index0 = TREE_OPERAND (ref0, 1);
4472 5890 : tree index1 = TREE_OPERAND (ref1, 1);
4473 5890 : tree newindex;
4474 5890 : gimple_stmt_iterator gsi;
4475 5890 : gsi = gsi_after_labels (merge);
4476 :
4477 : // factoring of the same pointer should be allowed
4478 : // irrespect to loops.
4479 5890 : if (p0 == p1 && operand_equal_p (index0, index1))
4480 : ;
4481 : // Before inlining, we can't tell if different
4482 : // pointers are going to be induction variable based
4483 : // or not.
4484 4729 : else if (early_p)
4485 : return false;
4486 : // Before vectorization, don't factor out
4487 : // pointers which are based on induction variables.
4488 4184 : else if (before_vect
4489 2433 : && bb_loop_depth (merge) != 0
4490 4892 : && (induction_based (p0, merge)
4491 604 : || induction_based (p1, merge)))
4492 : return false;
4493 :
4494 : /* Try to handle different indices. */
4495 5241 : if (operand_equal_p (index0, index1))
4496 4703 : newindex = fold_convert (atype, index0);
4497 : /* FIXME: right now non ssa names with different indices are not handled. */
4498 538 : else if (TREE_CODE (p0) != SSA_NAME || TREE_CODE (p1) != SSA_NAME)
4499 : return false;
4500 : /* If we have the same base already, just create a phi for the index
4501 : and the pointer plus will be done in the merge. */
4502 84 : else if (p0 == p1)
4503 : {
4504 60 : index0 = fold_convert (sizetype, index0);
4505 60 : index1 = fold_convert (sizetype, index1);
4506 60 : tree index = make_ssa_name (sizetype);
4507 60 : gphi *pphi = create_phi_node (index, merge);
4508 60 : add_phi_arg (pphi, index0, e0, gimple_phi_arg_location (phi, e0->dest_idx));
4509 60 : add_phi_arg (pphi, index1, e1, gimple_phi_arg_location (phi, e1->dest_idx));
4510 60 : p0 = gimple_build (&gsi, true, GSI_SAME_STMT,
4511 : UNKNOWN_LOCATION,
4512 : POINTER_PLUS_EXPR, atype, p0, index);
4513 : /* Since we already have the same pointer for both, just set that way.
4514 : Also the index offset is already 0 because we just did the add. */
4515 60 : p1 = p0;
4516 60 : newindex = build_zero_cst (atype);
4517 60 : if (dump_file && (dump_flags & TDF_DETAILS))
4518 : {
4519 2 : fprintf (dump_file, "new PHI ");
4520 2 : print_generic_expr (dump_file, index);
4521 2 : fprintf (dump_file,
4522 : " was created for the index.\n");
4523 : }
4524 : }
4525 : else
4526 : {
4527 24 : gimple_stmt_iterator gsi_index;
4528 : /* When the indices are different create 2 new pointers on each
4529 : of the middle bb right after the original load.
4530 : Note in the case of 0 index, gimple_build just returns
4531 : the original pointer. */
4532 24 : gsi_index = gsi_for_stmt (load0);
4533 24 : index0 = fold_convert (sizetype, index0);
4534 24 : p0 = gimple_build (&gsi_index, false, GSI_SAME_STMT,
4535 : gimple_location (load0),
4536 : POINTER_PLUS_EXPR, atype, p0, index0);
4537 :
4538 24 : gsi_index = gsi_for_stmt (load1);
4539 24 : index1 = fold_convert (sizetype, index1);
4540 24 : p1 = gimple_build (&gsi_index, false, GSI_SAME_STMT,
4541 : gimple_location (load1),
4542 : POINTER_PLUS_EXPR, atype, p1, index1);
4543 24 : newindex = build_zero_cst (atype);
4544 24 : if (dump_file && (dump_flags & TDF_DETAILS))
4545 : {
4546 1 : fprintf (dump_file, "new ptrs ");
4547 1 : print_generic_expr (dump_file, p0);
4548 1 : fprintf (dump_file, " and ");
4549 1 : print_generic_expr (dump_file, p1);
4550 1 : fprintf (dump_file,
4551 : " was created due to different offsets.\n");
4552 : }
4553 : }
4554 :
4555 4787 : tree newptr;
4556 4787 : if (p0 != p1)
4557 : {
4558 : /* We can't factor out a non-ssa named based load
4559 : as it might cause a variable not taken an
4560 : address to become needing the address taken.
4561 : An example is in go.
4562 : Were we produce:
4563 : _24 = PHI <&crypto/tls.cipherSuitesPreferenceOrder(36), &crypto/tls.cipherSuitesPreferenceOrderNoAES(37)>
4564 : And &crypto/tls.cipherSuitesPreferenceOrder address bit was not set.
4565 : FIXME: Refine to check ADDRESSABLE bit. */
4566 3566 : if (TREE_CODE (p0) != SSA_NAME || TREE_CODE (p1) != SSA_NAME)
4567 : return false;
4568 : // Incompatible address spaces or differnt function pointers could show up here.
4569 427 : if (!types_compatible_p (TREE_TYPE (p0), TREE_TYPE (p1)))
4570 : return false;
4571 : /* Build P' = PHI <P, Q> and the single load result = *P'. */
4572 409 : newptr = make_ssa_name (TREE_TYPE (p0));
4573 409 : gphi *pphi = create_phi_node (newptr, merge);
4574 409 : add_phi_arg (pphi, p0, e0, gimple_phi_arg_location (phi, e0->dest_idx));
4575 409 : add_phi_arg (pphi, p1, e1, gimple_phi_arg_location (phi, e1->dest_idx));
4576 : }
4577 : else
4578 : newptr = p0;
4579 :
4580 : /* Build the combined load RES = *PTR, reusing the PHI result so any range
4581 : info on it is preserved (as factor_out_conditional_operation does). */
4582 1630 : tree nref;
4583 1630 : if (index || step || index2)
4584 28 : nref = build5 (TARGET_MEM_REF, TREE_TYPE (ref0), newptr,
4585 : newindex, index, step, index2);
4586 : else
4587 : {
4588 1602 : nref = build2 (MEM_REF, TREE_TYPE (ref0), newptr, newindex);
4589 1602 : REF_REVERSE_STORAGE_ORDER (nref) = rev_order;
4590 : }
4591 1630 : MR_DEPENDENCE_CLIQUE (nref) = clique;
4592 1630 : MR_DEPENDENCE_BASE (nref) = base;
4593 1630 : tree res = gimple_phi_result (phi);
4594 1630 : gassign *load = gimple_build_assign (res, nref);
4595 1630 : if (gphi *vphi = get_virtual_phi (merge))
4596 1209 : gimple_set_vuse (load, gimple_phi_result (vphi));
4597 : else
4598 842 : gimple_set_vuse (load, gimple_vuse (load0));
4599 1630 : gsi_insert_before (&gsi, load, GSI_SAME_STMT);
4600 :
4601 : /* RES is now defined by the load; drop the original PHI. */
4602 1630 : gsi = gsi_for_stmt (phi);
4603 1630 : remove_phi_node (&gsi, false);
4604 :
4605 : /* The two arm loads are now dead. */
4606 1630 : gsi = gsi_for_stmt (load0);
4607 1630 : gsi_remove (&gsi, true);
4608 1630 : release_defs (load0);
4609 1630 : gsi = gsi_for_stmt (load1);
4610 1630 : gsi_remove (&gsi, true);
4611 1630 : release_defs (load1);
4612 :
4613 1630 : if (dump_file && (dump_flags & TDF_DETAILS))
4614 : {
4615 15 : fprintf (dump_file, "PHI ");
4616 15 : print_generic_expr (dump_file, res);
4617 15 : fprintf (dump_file,
4618 : " changed to factor out load from COND_EXPR.\n");
4619 15 : if (p0 != p1)
4620 : {
4621 13 : fprintf (dump_file, "new PHI ");
4622 13 : print_generic_expr (dump_file, newptr);
4623 13 : fprintf (dump_file,
4624 : " was created for the pointers.\n");
4625 : }
4626 : }
4627 :
4628 1630 : statistics_counter_event (cfun, "factored load out of COND_EXPR", 1);
4629 1630 : return true;
4630 : }
4631 :
4632 : /* Factor out operations and stores from the phi of the MERGE block coming
4633 : in from the edges E1 and E2 if possible. COND_STMT is the conditional
4634 : statement of the origin block. DIAMOND_P says that both E1 and E2 src
4635 : are not the origin block but rather 2 middle BBs. EARLY_P is true if
4636 : this was the early phi-opt.
4637 : Returns true if a factoring happened. */
4638 : static bool
4639 2551262 : factor_out_all (edge e1, edge e2, basic_block merge,
4640 : gcond *cond_stmt, bool diamond_p, bool early_p)
4641 : {
4642 2551262 : bool changed = false;
4643 2551262 : bool do_over;
4644 2551262 : bool before_vect = !fold_before_rtl_expansion_p ();
4645 : // If vectorization is disable, then we are never before the vectorizer.
4646 2551262 : if (!flag_tree_loop_vectorize
4647 195353 : && !merge->loop_father->force_vectorize)
4648 2551262 : before_vect = false;
4649 2551262 : basic_block bb1 = e1->src;
4650 2551262 : basic_block bb2 = e2->src;
4651 2527521 : do
4652 : {
4653 2601096 : do_over = false;
4654 2601096 : if (diamond_p && get_virtual_phi (merge))
4655 : {
4656 717347 : if (cond_if_else_store_replacement_limited (bb1, bb2, merge))
4657 : {
4658 13615 : changed = true;
4659 13615 : do_over = true;
4660 19622 : continue;
4661 : }
4662 : }
4663 2587481 : if (!single_pred_p (bb1))
4664 : break;
4665 1732304 : if (!diamond_p && get_virtual_phi (merge)
4666 3548454 : && cond_store_replacement_limited (bb1, merge, bb2,
4667 : e1, e2, false))
4668 : {
4669 6007 : changed = true;
4670 6007 : do_over = true;
4671 6007 : continue;
4672 : }
4673 2507899 : gphi_iterator gsi;
4674 5567878 : for (gsi = gsi_start_phis (merge); !gsi_end_p (gsi); gsi_next (&gsi))
4675 : {
4676 3090191 : gphi *phi = *gsi;
4677 : /* Conditional load elimination can only be on a diamond. */
4678 3090191 : if ((diamond_p
4679 998476 : && factor_out_conditional_load (e1, e2, merge, phi, early_p,
4680 : before_vect))
4681 4087037 : || factor_out_conditional_operation (e1, e2, merge, phi,
4682 : cond_stmt, early_p))
4683 : {
4684 : changed = true;
4685 : do_over = true;
4686 : break;
4687 : }
4688 : }
4689 : } while (do_over);
4690 2551262 : return changed;
4691 : }
4692 :
4693 : /* Return TRUE if STMT has a VUSE whose corresponding VDEF is in BB. */
4694 :
4695 : static bool
4696 12564 : local_mem_dependence (gimple *stmt, basic_block bb)
4697 : {
4698 25128 : tree vuse = gimple_vuse (stmt);
4699 12564 : gimple *def;
4700 :
4701 12564 : if (!vuse)
4702 : return false;
4703 :
4704 12564 : def = SSA_NAME_DEF_STMT (vuse);
4705 12564 : return (def && gimple_bb (def) == bb);
4706 : }
4707 :
4708 : /* Given a "diamond" control-flow pattern where BB0 tests a condition,
4709 : BB1 and BB2 are "then" and "else" blocks dependent on this test,
4710 : and BB3 rejoins control flow following BB1 and BB2, look for
4711 : opportunities to hoist loads as follows. If BB3 contains a PHI of
4712 : two loads, one each occurring in BB1 and BB2, and the loads are
4713 : provably of adjacent fields in the same structure, then move both
4714 : loads into BB0. Of course this can only be done if there are no
4715 : dependencies preventing such motion.
4716 :
4717 : One of the hoisted loads will always be speculative, so the
4718 : transformation is currently conservative:
4719 :
4720 : - The fields must be strictly adjacent.
4721 : - The two fields must occupy a single memory block that is
4722 : guaranteed to not cross a page boundary.
4723 :
4724 : The last is difficult to prove, as such memory blocks should be
4725 : aligned on the minimum of the stack alignment boundary and the
4726 : alignment guaranteed by heap allocation interfaces. Thus we rely
4727 : on a parameter for the alignment value.
4728 :
4729 : Provided a good value is used for the last case, the first
4730 : restriction could possibly be relaxed. */
4731 :
4732 : static void
4733 601828 : hoist_adjacent_loads (basic_block bb0, basic_block bb1,
4734 : basic_block bb2, basic_block bb3)
4735 : {
4736 601828 : unsigned HOST_WIDE_INT param_align = param_l1_cache_line_size;
4737 601828 : unsigned HOST_WIDE_INT param_align_bits = param_align * BITS_PER_UNIT;
4738 601828 : gphi_iterator gsi;
4739 :
4740 : /* Walk the phis in bb3 looking for an opportunity. We are looking
4741 : for phis of two SSA names, one each of which is defined in bb1 and
4742 : bb2. */
4743 1353065 : for (gsi = gsi_start_phis (bb3); !gsi_end_p (gsi); gsi_next (&gsi))
4744 : {
4745 751237 : gphi *phi_stmt = gsi.phi ();
4746 751237 : gimple *def1, *def2;
4747 751237 : tree arg1, arg2, ref1, ref2, field1, field2;
4748 751237 : tree tree_offset1, tree_offset2, tree_size2, next;
4749 751237 : unsigned HOST_WIDE_INT offset1, offset2, size2, align1;
4750 751237 : gimple_stmt_iterator gsi2;
4751 751237 : basic_block bb_for_def1, bb_for_def2;
4752 :
4753 751237 : if (gimple_phi_num_args (phi_stmt) != 2
4754 1502474 : || virtual_operand_p (gimple_phi_result (phi_stmt)))
4755 744955 : continue;
4756 :
4757 191891 : arg1 = gimple_phi_arg_def (phi_stmt, 0);
4758 191891 : arg2 = gimple_phi_arg_def (phi_stmt, 1);
4759 :
4760 224180 : if (TREE_CODE (arg1) != SSA_NAME
4761 174295 : || TREE_CODE (arg2) != SSA_NAME
4762 160847 : || SSA_NAME_IS_DEFAULT_DEF (arg1)
4763 352407 : || SSA_NAME_IS_DEFAULT_DEF (arg2))
4764 32289 : continue;
4765 :
4766 159602 : def1 = SSA_NAME_DEF_STMT (arg1);
4767 159602 : def2 = SSA_NAME_DEF_STMT (arg2);
4768 :
4769 159602 : if ((gimple_bb (def1) != bb1 || gimple_bb (def2) != bb2)
4770 173858 : && (gimple_bb (def2) != bb1 || gimple_bb (def1) != bb2))
4771 41885 : continue;
4772 :
4773 : /* Check the mode of the arguments to be sure a conditional move
4774 : can be generated for it. */
4775 235434 : if (optab_handler (movcc_optab, TYPE_MODE (TREE_TYPE (arg1)))
4776 : == CODE_FOR_nothing)
4777 4943 : continue;
4778 :
4779 : /* Both statements must be assignments whose RHS is a COMPONENT_REF. */
4780 112774 : if (!gimple_assign_single_p (def1)
4781 52411 : || !gimple_assign_single_p (def2)
4782 69682 : || gimple_has_volatile_ops (def1)
4783 182410 : || gimple_has_volatile_ops (def2))
4784 77956 : continue;
4785 :
4786 34818 : ref1 = gimple_assign_rhs1 (def1);
4787 34818 : ref2 = gimple_assign_rhs1 (def2);
4788 :
4789 34818 : if (TREE_CODE (ref1) != COMPONENT_REF
4790 24099 : || TREE_CODE (ref2) != COMPONENT_REF)
4791 10889 : continue;
4792 :
4793 : /* The zeroth operand of the two component references must be
4794 : identical. It is not sufficient to compare get_base_address of
4795 : the two references, because this could allow for different
4796 : elements of the same array in the two trees. It is not safe to
4797 : assume that the existence of one array element implies the
4798 : existence of a different one. */
4799 23929 : if (!operand_equal_p (TREE_OPERAND (ref1, 0), TREE_OPERAND (ref2, 0), 0))
4800 4424 : continue;
4801 :
4802 19505 : field1 = TREE_OPERAND (ref1, 1);
4803 19505 : field2 = TREE_OPERAND (ref2, 1);
4804 :
4805 : /* Check for field adjacency, and ensure field1 comes first. */
4806 19505 : for (next = DECL_CHAIN (field1);
4807 34156 : next && TREE_CODE (next) != FIELD_DECL;
4808 14651 : next = DECL_CHAIN (next))
4809 : ;
4810 :
4811 19505 : if (next != field2)
4812 : {
4813 15797 : for (next = DECL_CHAIN (field2);
4814 17804 : next && TREE_CODE (next) != FIELD_DECL;
4815 2007 : next = DECL_CHAIN (next))
4816 : ;
4817 :
4818 15797 : if (next != field1)
4819 13223 : continue;
4820 :
4821 : std::swap (field1, field2);
4822 : std::swap (def1, def2);
4823 : }
4824 :
4825 6282 : bb_for_def1 = gimple_bb (def1);
4826 6282 : bb_for_def2 = gimple_bb (def2);
4827 :
4828 : /* Check for proper alignment of the first field. */
4829 6282 : tree_offset1 = bit_position (field1);
4830 6282 : tree_offset2 = bit_position (field2);
4831 6282 : tree_size2 = DECL_SIZE (field2);
4832 :
4833 6282 : if (!tree_fits_uhwi_p (tree_offset1)
4834 6282 : || !tree_fits_uhwi_p (tree_offset2)
4835 6282 : || !tree_fits_uhwi_p (tree_size2))
4836 0 : continue;
4837 :
4838 6282 : offset1 = tree_to_uhwi (tree_offset1);
4839 6282 : offset2 = tree_to_uhwi (tree_offset2);
4840 6282 : size2 = tree_to_uhwi (tree_size2);
4841 6282 : align1 = DECL_ALIGN (field1) % param_align_bits;
4842 :
4843 6282 : if (offset1 % BITS_PER_UNIT != 0)
4844 0 : continue;
4845 :
4846 : /* For profitability, the two field references should fit within
4847 : a single cache line. */
4848 6282 : if (align1 + offset2 - offset1 + size2 > param_align_bits)
4849 0 : continue;
4850 :
4851 : /* The two expressions cannot be dependent upon vdefs defined
4852 : in bb1/bb2. */
4853 6282 : if (local_mem_dependence (def1, bb_for_def1)
4854 6282 : || local_mem_dependence (def2, bb_for_def2))
4855 0 : continue;
4856 :
4857 : /* The conditions are satisfied; hoist the loads from bb1 and bb2 into
4858 : bb0. We hoist the first one first so that a cache miss is handled
4859 : efficiently regardless of hardware cache-fill policy. */
4860 6282 : gsi2 = gsi_for_stmt (def1);
4861 6282 : gsi_move_to_bb_end (&gsi2, bb0);
4862 6282 : gsi2 = gsi_for_stmt (def2);
4863 6282 : gsi_move_to_bb_end (&gsi2, bb0);
4864 : /* Clear range info from the defs we've moved from under the
4865 : condition. */
4866 6282 : reset_flow_sensitive_info (gimple_assign_lhs (def1));
4867 6282 : reset_flow_sensitive_info (gimple_assign_lhs (def2));
4868 6282 : statistics_counter_event (cfun, "hoisted loads", 1);
4869 :
4870 6282 : if (dump_file && (dump_flags & TDF_DETAILS))
4871 : {
4872 0 : fprintf (dump_file,
4873 : "\nHoisting adjacent loads from %d and %d into %d: \n",
4874 : bb_for_def1->index, bb_for_def2->index, bb0->index);
4875 0 : print_gimple_stmt (dump_file, def1, 0, TDF_VOPS|TDF_MEMSYMS);
4876 0 : print_gimple_stmt (dump_file, def2, 0, TDF_VOPS|TDF_MEMSYMS);
4877 : }
4878 : }
4879 601828 : }
4880 :
4881 : /* Determine whether we should attempt to hoist adjacent loads out of
4882 : diamond patterns in pass_phiopt. Always hoist loads if
4883 : -fhoist-adjacent-loads is specified and the target machine has
4884 : both a conditional move instruction and a defined cache line size. */
4885 :
4886 : static bool
4887 3204361 : gate_hoist_loads (void)
4888 : {
4889 3204361 : return (flag_hoist_adjacent_loads == 1
4890 3204361 : && param_l1_cache_line_size
4891 0 : && HAVE_conditional_move);
4892 : }
4893 :
4894 : template <class func_type>
4895 : static void
4896 6796792 : execute_over_cond_phis (func_type func)
4897 : {
4898 : unsigned n, i;
4899 : basic_block *bb_order;
4900 : basic_block bb;
4901 : /* Search every basic block for COND_EXPR we may be able to optimize.
4902 :
4903 : We walk the blocks in order that guarantees that a block with
4904 : a single predecessor is processed before the predecessor.
4905 : This ensures that we collapse inner ifs before visiting the
4906 : outer ones, and also that we do not try to visit a removed
4907 : block. */
4908 6796792 : bb_order = single_pred_before_succ_order ();
4909 6796792 : n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
4910 :
4911 62114199 : for (i = 0; i < n; i++)
4912 : {
4913 : basic_block bb1, bb2;
4914 : edge e1, e2;
4915 55317407 : bool diamond_p = false;
4916 :
4917 55317407 : bb = bb_order[i];
4918 :
4919 : /* Check to see if the last statement is a GIMPLE_COND. */
4920 55317407 : gcond *cond_stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
4921 33504341 : if (!cond_stmt)
4922 55317407 : continue;
4923 :
4924 21813066 : e1 = EDGE_SUCC (bb, 0);
4925 21813066 : bb1 = e1->dest;
4926 21813066 : e2 = EDGE_SUCC (bb, 1);
4927 21813066 : bb2 = e2->dest;
4928 :
4929 : /* We cannot do the optimization on abnormal edges. */
4930 21813066 : if ((e1->flags & EDGE_ABNORMAL) != 0
4931 21813066 : || (e2->flags & EDGE_ABNORMAL) != 0)
4932 0 : continue;
4933 :
4934 : /* If either bb1's succ or bb2 or bb2's succ is non NULL. */
4935 21813066 : if (EDGE_COUNT (bb1->succs) == 0
4936 20456819 : || EDGE_COUNT (bb2->succs) == 0)
4937 4985899 : continue;
4938 :
4939 : /* Find the bb which is the fall through to the other. */
4940 16827167 : if (EDGE_SUCC (bb1, 0)->dest == bb2)
4941 : ;
4942 14296439 : else if (EDGE_SUCC (bb2, 0)->dest == bb1)
4943 : {
4944 : std::swap (bb1, bb2);
4945 : std::swap (e1, e2);
4946 : }
4947 11010175 : else if (EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest
4948 12584678 : && single_succ_p (bb2))
4949 : {
4950 1574503 : diamond_p = true;
4951 1574503 : e2 = EDGE_SUCC (bb2, 0);
4952 : /* Make sure bb2 is just a fall through. */
4953 1574503 : if ((e2->flags & EDGE_FALLTHRU) == 0)
4954 52121 : continue;
4955 : }
4956 : else
4957 11010175 : continue;
4958 :
4959 5764871 : e1 = EDGE_SUCC (bb1, 0);
4960 :
4961 : /* Make sure that bb1 is just a fall through. */
4962 5764871 : if (!single_succ_p (bb1)
4963 5764871 : || (e1->flags & EDGE_FALLTHRU) == 0)
4964 1280814 : continue;
4965 :
4966 4484057 : func (bb, bb1, bb2, e1, e2, diamond_p, cond_stmt);
4967 : }
4968 6796792 : free (bb_order);
4969 6796792 : }
4970 :
4971 : /* This pass tries to replaces an if-then-else block with an
4972 : assignment. We have different kinds of transformations.
4973 : Some of these transformations are also performed by the ifcvt
4974 : RTL optimizer.
4975 :
4976 : PHI-OPT using Match-and-simplify infrastructure
4977 : -----------------------
4978 :
4979 : The PHI-OPT pass will try to use match-and-simplify infrastructure
4980 : (gimple_simplify) to do transformations. This is implemented in
4981 : match_simplify_replacement.
4982 :
4983 : The way it works is it replaces:
4984 : bb0:
4985 : if (cond) goto bb2; else goto bb1;
4986 : bb1:
4987 : bb2:
4988 : x = PHI <a (bb1), b (bb0), ...>;
4989 :
4990 : with a statement if it gets simplified from `cond ? b : a`.
4991 :
4992 : bb0:
4993 : x1 = cond ? b : a;
4994 : bb2:
4995 : x = PHI <a (bb1), x1 (bb0), ...>;
4996 : Bb1 might be removed as it becomes unreachable when doing the replacement.
4997 : Though bb1 does not have to be considered a forwarding basic block from bb0.
4998 :
4999 : Will try to see if `(!cond) ? a : b` gets simplified (iff !cond simplifies);
5000 : this is done not to have an explosion of patterns in match.pd.
5001 : Note bb1 does not need to be completely empty, it can contain
5002 : one statement which is known not to trap.
5003 :
5004 : It also can handle the case where we have two forwarding bbs (diamond):
5005 : bb0:
5006 : if (cond) goto bb2; else goto bb1;
5007 : bb1: goto bb3;
5008 : bb2: goto bb3;
5009 : bb3:
5010 : x = PHI <a (bb1), b (bb2), ...>;
5011 : And that is replaced with a statement if it is simplified
5012 : from `cond ? b : a`.
5013 : Again bb1 and bb2 does not have to be completely empty but
5014 : each can contain one statement which is known not to trap.
5015 : But in this case bb1/bb2 can only be forwarding basic blocks.
5016 :
5017 : This fully replaces the old "Conditional Replacement",
5018 : "ABS Replacement" and "MIN/MAX Replacement" transformations as they are now
5019 : implemented in match.pd.
5020 :
5021 : Value Replacement
5022 : -----------------
5023 :
5024 : This transformation, implemented in value_replacement, replaces
5025 :
5026 : bb0:
5027 : if (a != b) goto bb2; else goto bb1;
5028 : bb1:
5029 : bb2:
5030 : x = PHI <a (bb1), b (bb0), ...>;
5031 :
5032 : with
5033 :
5034 : bb0:
5035 : bb2:
5036 : x = PHI <b (bb0), ...>;
5037 :
5038 : This opportunity can sometimes occur as a result of other
5039 : optimizations.
5040 :
5041 :
5042 : Another case caught by value replacement looks like this:
5043 :
5044 : bb0:
5045 : t1 = a == CONST;
5046 : t2 = b > c;
5047 : t3 = t1 & t2;
5048 : if (t3 != 0) goto bb1; else goto bb2;
5049 : bb1:
5050 : bb2:
5051 : x = PHI (CONST, a)
5052 :
5053 : Gets replaced with:
5054 : bb0:
5055 : bb2:
5056 : t1 = a == CONST;
5057 : t2 = b > c;
5058 : t3 = t1 & t2;
5059 : x = a;
5060 :
5061 :
5062 : This pass also performs a fifth transformation of a slightly different
5063 : flavor.
5064 :
5065 : Factor operations in COND_EXPR
5066 : ------------------------------
5067 :
5068 : This transformation factors the unary operations out of COND_EXPR with
5069 : factor_out_conditional_operation.
5070 :
5071 : For example:
5072 : if (a <= CST) goto <bb 3>; else goto <bb 4>;
5073 : <bb 3>:
5074 : tmp = (int) a;
5075 : <bb 4>:
5076 : tmp = PHI <tmp, CST>
5077 :
5078 : Into:
5079 : if (a <= CST) goto <bb 3>; else goto <bb 4>;
5080 : <bb 3>:
5081 : <bb 4>:
5082 : a = PHI <a, CST>
5083 : tmp = (int) a;
5084 :
5085 : Adjacent Load Hoisting
5086 : ----------------------
5087 :
5088 : This transformation replaces
5089 :
5090 : bb0:
5091 : if (...) goto bb2; else goto bb1;
5092 : bb1:
5093 : x1 = (<expr>).field1;
5094 : goto bb3;
5095 : bb2:
5096 : x2 = (<expr>).field2;
5097 : bb3:
5098 : # x = PHI <x1, x2>;
5099 :
5100 : with
5101 :
5102 : bb0:
5103 : x1 = (<expr>).field1;
5104 : x2 = (<expr>).field2;
5105 : if (...) goto bb2; else goto bb1;
5106 : bb1:
5107 : goto bb3;
5108 : bb2:
5109 : bb3:
5110 : # x = PHI <x1, x2>;
5111 :
5112 : The purpose of this transformation is to enable generation of conditional
5113 : move instructions such as Intel CMOVE or PowerPC ISEL. Because one of
5114 : the loads is speculative, the transformation is restricted to very
5115 : specific cases to avoid introducing a page fault. We are looking for
5116 : the common idiom:
5117 :
5118 : if (...)
5119 : x = y->left;
5120 : else
5121 : x = y->right;
5122 :
5123 : where left and right are typically adjacent pointers in a tree structure. */
5124 :
5125 : /* Replicate the join block at E's destination into E's source. The copy's
5126 : PHIs degenerate to their argument on E, so the copied conditional tests the
5127 : predecessor's own value. The net effect after cleanups, for edge (3, 5)
5128 : would be:
5129 :
5130 : <bb 3>: <bb 3>:
5131 : t_9 = a < b; t_9 = a < b;
5132 : goto <bb 5>; if (t_9 != 0)
5133 :
5134 : <bb 4>: -> <bb 4>:
5135 : t_6 = c < d; t_6 = c < d;
5136 : goto <bb 5>; goto <bb 5>;
5137 :
5138 : <bb 5>: <bb 5>:
5139 : # t_1 = PHI <t_9(3), t_6(4)> # t_1 = PHI <t_6(4)>
5140 : if (t_1 != 0) if (t_1 != 0)
5141 :
5142 : Return TRUE if the replication was performed. */
5143 :
5144 : static bool
5145 50982 : replicate_cond_into_pred (edge e)
5146 : {
5147 50982 : basic_block bb = e->dest;
5148 :
5149 50982 : if (!can_duplicate_block_on_edge_p (e))
5150 : return false;
5151 :
5152 50982 : if (dump_file && (dump_flags & TDF_DETAILS))
5153 2 : fprintf (dump_file,
5154 : "replicating conditional in bb%d into predecessor bb%d\n",
5155 2 : bb->index, e->src->index);
5156 :
5157 50982 : basic_block copy = duplicate_block (bb, e, e->src);
5158 50982 : flush_pending_stmts (e);
5159 50982 : add_phi_args_after_copy (©, 1, NULL);
5160 50982 : return true;
5161 : }
5162 :
5163 : /* Replicate conditionals over a PHI of comparisons into the
5164 : qualifying predecessors of every join block in the function, so
5165 : each arm branches directly on its own comparison instead of merging
5166 : into a boolean that is tested again:
5167 :
5168 : <bb 3>: <bb 3>:
5169 : t_9 = a < b; t_9 = a < b;
5170 : goto <bb 5>; if (t_9 != 0)
5171 :
5172 : <bb 4>: -> <bb 4>:
5173 : t_6 = c < d; t_6 = c < d;
5174 : goto <bb 5>; if (t_6 != 0)
5175 :
5176 : <bb 5>:
5177 : # t_1 = PHI <t_9(3), t_6(4)>
5178 : if (t_1 != 0)
5179 : */
5180 :
5181 : static bool
5182 5728660 : replicate_conds_over_phis (void)
5183 : {
5184 5728660 : bool cfgchanged = false;
5185 5728660 : basic_block bb;
5186 :
5187 5728660 : initialize_original_copy_tables ();
5188 50101027 : FOR_EACH_BB_FN (bb, cfun)
5189 : {
5190 44372367 : gcond *cond;
5191 44372367 : gphi *phi;
5192 : /* Never duplicate loop headers. */
5193 88509357 : if (bb->loop_father->header == bb
5194 41609617 : || EDGE_COUNT (bb->preds) < 2
5195 52103659 : || !cond_on_phi_p (bb, &cond, &phi))
5196 44136990 : continue;
5197 :
5198 235377 : edge e;
5199 235377 : edge_iterator ei = ei_start (bb->preds);
5200 766504 : while ((e = ei_safe_edge (ei)))
5201 : {
5202 531127 : if (phi_arg_from_cmp_p (phi, e)
5203 531127 : && replicate_cond_into_pred (e))
5204 : cfgchanged = true;
5205 : else
5206 480145 : ei_next (&ei);
5207 : }
5208 : }
5209 5728660 : free_original_copy_tables ();
5210 :
5211 : /* Removing an entry of an irreducible region can make it reducible, creating
5212 : a new loop. */
5213 5728660 : if (cfgchanged)
5214 25431 : loops_state_set (LOOPS_NEED_FIXUP);
5215 :
5216 5728660 : return cfgchanged;
5217 : }
5218 :
5219 : namespace {
5220 :
5221 : const pass_data pass_data_phiopt =
5222 : {
5223 : GIMPLE_PASS, /* type */
5224 : "phiopt", /* name */
5225 : OPTGROUP_NONE, /* optinfo_flags */
5226 : TV_TREE_PHIOPT, /* tv_id */
5227 : ( PROP_cfg | PROP_ssa ), /* properties_required */
5228 : 0, /* properties_provided */
5229 : 0, /* properties_destroyed */
5230 : 0, /* todo_flags_start */
5231 : 0, /* todo_flags_finish */
5232 : };
5233 :
5234 : class pass_phiopt : public gimple_opt_pass
5235 : {
5236 : public:
5237 1211940 : pass_phiopt (gcc::context *ctxt)
5238 2423880 : : gimple_opt_pass (pass_data_phiopt, ctxt), early_p (false)
5239 : {}
5240 :
5241 : /* opt_pass methods: */
5242 908955 : opt_pass * clone () final override { return new pass_phiopt (m_ctxt); }
5243 1211940 : void set_pass_param (unsigned n, bool param) final override
5244 : {
5245 1211940 : gcc_assert (n == 0);
5246 1211940 : early_p = param;
5247 1211940 : }
5248 5732135 : bool gate (function *) final override { return flag_ssa_phiopt; }
5249 : unsigned int execute (function *) final override;
5250 :
5251 : private:
5252 : bool early_p;
5253 : }; // class pass_phiopt
5254 :
5255 : } // anon namespace
5256 :
5257 : gimple_opt_pass *
5258 302985 : make_pass_phiopt (gcc::context *ctxt)
5259 : {
5260 302985 : return new pass_phiopt (ctxt);
5261 : }
5262 :
5263 : unsigned int
5264 5728660 : pass_phiopt::execute (function *)
5265 : {
5266 5728660 : bool do_hoist_loads = !early_p ? gate_hoist_loads () : false;
5267 5728660 : bool cfgchanged = false;
5268 5728660 : bool need_loop_finalize = false;
5269 :
5270 5728660 : if (!early_p
5271 3204361 : && !fold_before_rtl_expansion_p ()
5272 2136704 : && (flag_tree_loop_vectorize
5273 276664 : || cfun->has_force_vectorize_loops)
5274 9456952 : && number_of_loops (cfun) > 1)
5275 : {
5276 432330 : loop_optimizer_init (LOOPS_NORMAL);
5277 432330 : scev_initialize ();
5278 432330 : need_loop_finalize = true;
5279 : }
5280 :
5281 5728660 : calculate_dominance_info (CDI_DOMINATORS);
5282 5728660 : mark_ssa_maybe_undefs ();
5283 :
5284 9274209 : auto phiopt_exec = [&] (basic_block bb, basic_block bb1,
5285 : basic_block bb2, edge e1, edge e2,
5286 : bool diamond_p, gcond *cond_stmt)
5287 : {
5288 3545549 : if (diamond_p)
5289 : {
5290 1127047 : basic_block bb3 = e1->dest;
5291 :
5292 1127047 : if (!single_pred_p (bb1)
5293 2193498 : || !single_pred_p (bb2))
5294 3545549 : return;
5295 :
5296 1008774 : if (do_hoist_loads
5297 792715 : && !FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (cond_stmt)))
5298 784471 : && EDGE_COUNT (bb->succs) == 2
5299 784471 : && EDGE_COUNT (bb3->preds) == 2
5300 : /* If one edge or the other is dominant, a conditional move
5301 : is likely to perform worse than the well-predicted branch. */
5302 606438 : && !predictable_edge_p (EDGE_SUCC (bb, 0))
5303 1610602 : && !predictable_edge_p (EDGE_SUCC (bb, 1)))
5304 601828 : hoist_adjacent_loads (bb, bb1, bb2, bb3);
5305 : }
5306 :
5307 1008774 : gimple_stmt_iterator gsi;
5308 :
5309 : /* Check that we're looking for nested phis. */
5310 1008774 : basic_block merge = diamond_p ? EDGE_SUCC (bb2, 0)->dest : bb2;
5311 :
5312 : /* Factor out operations from the phi if possible. */
5313 3427276 : if (EDGE_COUNT (merge->preds) == 2
5314 3427276 : && !optimize_debug && factor_out_all (e1, e2, merge, cond_stmt, diamond_p, early_p))
5315 42947 : cfgchanged = true;
5316 :
5317 3427276 : gimple_seq phis = phi_nodes (merge);
5318 :
5319 3427276 : if (gimple_seq_empty_p (phis))
5320 : return;
5321 :
5322 : /* Value replacement can work with more than one PHI
5323 : so try that first. */
5324 3412999 : if (!early_p && !diamond_p)
5325 4288611 : for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
5326 : {
5327 2504449 : gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
5328 2504449 : tree arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
5329 2504449 : tree arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
5330 2504449 : if (value_replacement (bb, bb1, e1, e2, phi, arg0, arg1) == 2)
5331 : {
5332 1885 : cfgchanged = true;
5333 1885 : return;
5334 : }
5335 : }
5336 :
5337 3411114 : gphi *phi = single_non_singleton_phi_for_edges (phis, e1, e2);
5338 3411114 : if (!phi)
5339 : return;
5340 :
5341 862666 : tree arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
5342 862666 : tree arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
5343 :
5344 : /* Something is wrong if we cannot find the arguments in the PHI
5345 : node. */
5346 862666 : gcc_assert (arg0 != NULL_TREE && arg1 != NULL_TREE);
5347 :
5348 :
5349 : /* Do the replacement of conditional if it can be done. */
5350 862666 : if (match_simplify_replacement (bb, bb1, bb2, e1, e2, phi,
5351 : arg0, arg1, early_p, diamond_p))
5352 96973 : cfgchanged = true;
5353 765693 : else if (comparison_combine (bb, bb1, bb2, e1, e2, phi,
5354 : arg0, arg1, diamond_p))
5355 16 : cfgchanged = true;
5356 765677 : else if (!early_p
5357 502664 : && !diamond_p
5358 469233 : && single_pred_p (bb1)
5359 1214259 : && cond_removal_in_builtin_zero_pattern (bb, bb1, e1, e2,
5360 : phi, arg0, arg1))
5361 13 : cfgchanged = true;
5362 765664 : else if (single_pred_p (bb1)
5363 714636 : && !diamond_p
5364 1417217 : && spaceship_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
5365 2364 : cfgchanged = true;
5366 5728660 : };
5367 :
5368 5728660 : execute_over_cond_phis (phiopt_exec);
5369 :
5370 5728660 : if (need_loop_finalize)
5371 : {
5372 432330 : loop_optimizer_finalize ();
5373 432330 : scev_finalize ();
5374 : }
5375 5728660 : if (replicate_conds_over_phis ())
5376 : {
5377 25431 : free_dominance_info (CDI_DOMINATORS);
5378 25431 : return TODO_cleanup_cfg | TODO_update_ssa;
5379 : }
5380 :
5381 5703229 : if (cfgchanged)
5382 85684 : return TODO_cleanup_cfg;
5383 : return 0;
5384 : }
5385 :
5386 : /* This pass tries to transform conditional stores into unconditional
5387 : ones, enabling further simplifications with the simpler then and else
5388 : blocks. In particular it replaces this:
5389 :
5390 : bb0:
5391 : if (cond) goto bb2; else goto bb1;
5392 : bb1:
5393 : *p = RHS;
5394 : bb2:
5395 :
5396 : with
5397 :
5398 : bb0:
5399 : if (cond) goto bb1; else goto bb2;
5400 : bb1:
5401 : condtmp' = *p;
5402 : bb2:
5403 : condtmp = PHI <RHS, condtmp'>
5404 : *p = condtmp;
5405 :
5406 : This transformation can only be done under several constraints,
5407 : documented below. It also replaces:
5408 :
5409 : bb0:
5410 : if (cond) goto bb2; else goto bb1;
5411 : bb1:
5412 : *p = RHS1;
5413 : goto bb3;
5414 : bb2:
5415 : *p = RHS2;
5416 : bb3:
5417 :
5418 : with
5419 :
5420 : bb0:
5421 : if (cond) goto bb3; else goto bb1;
5422 : bb1:
5423 : bb3:
5424 : condtmp = PHI <RHS1, RHS2>
5425 : *p = condtmp; */
5426 :
5427 : namespace {
5428 :
5429 : const pass_data pass_data_cselim =
5430 : {
5431 : GIMPLE_PASS, /* type */
5432 : "cselim", /* name */
5433 : OPTGROUP_NONE, /* optinfo_flags */
5434 : TV_TREE_PHIOPT, /* tv_id */
5435 : ( PROP_cfg | PROP_ssa ), /* properties_required */
5436 : 0, /* properties_provided */
5437 : 0, /* properties_destroyed */
5438 : 0, /* todo_flags_start */
5439 : 0, /* todo_flags_finish */
5440 : };
5441 :
5442 : class pass_cselim : public gimple_opt_pass
5443 : {
5444 : public:
5445 302985 : pass_cselim (gcc::context *ctxt)
5446 605970 : : gimple_opt_pass (pass_data_cselim, ctxt)
5447 : {}
5448 :
5449 : /* opt_pass methods: */
5450 1068301 : bool gate (function *) final override { return flag_tree_cselim; }
5451 : unsigned int execute (function *) final override;
5452 :
5453 : }; // class pass_cselim
5454 :
5455 : } // anon namespace
5456 :
5457 : gimple_opt_pass *
5458 302985 : make_pass_cselim (gcc::context *ctxt)
5459 : {
5460 302985 : return new pass_cselim (ctxt);
5461 : }
5462 :
5463 : unsigned int
5464 1068132 : pass_cselim::execute (function *)
5465 : {
5466 1068132 : bool cfgchanged = false;
5467 1068132 : hash_set<tree> *nontrap = 0;
5468 1068132 : unsigned todo = 0;
5469 :
5470 : /* ??? We are not interested in loop related info, but the following
5471 : will create it, ICEing as we didn't init loops with pre-headers.
5472 : An interfacing issue of find_data_references_in_bb. */
5473 1068132 : loop_optimizer_init (LOOPS_NORMAL);
5474 1068132 : scev_initialize ();
5475 :
5476 1068132 : calculate_dominance_info (CDI_DOMINATORS);
5477 :
5478 : /* Calculate the set of non-trapping memory accesses. */
5479 1068132 : nontrap = get_non_trapping ();
5480 :
5481 2006640 : auto cselim_exec = [&] (basic_block bb, basic_block bb1,
5482 : basic_block bb2, edge e1, edge e2,
5483 : bool diamond_p, gcond *)
5484 : {
5485 938508 : if (diamond_p)
5486 : {
5487 326141 : basic_block bb3 = e1->dest;
5488 :
5489 : /* Only handle sinking of store from 2 bbs only,
5490 : The middle bbs don't need to come from the
5491 : if always since we are sinking rather than
5492 : hoisting. */
5493 326141 : if (EDGE_COUNT (bb3->preds) != 2)
5494 : return;
5495 241537 : if (cond_if_else_store_replacement (bb1, bb2, bb3))
5496 789 : cfgchanged = true;
5497 : return;
5498 : }
5499 :
5500 : /* Also make sure that bb1 only have one predecessor and that it
5501 : is bb. */
5502 612367 : if (!single_pred_p (bb1)
5503 1176594 : || single_pred (bb1) != bb)
5504 : return;
5505 :
5506 : /* bb1 is the middle block, bb2 the join block, bb the split block,
5507 : e1 the fallthrough edge from bb1 to bb2. We can't do the
5508 : optimization if the join block has more than two predecessors. */
5509 564227 : if (EDGE_COUNT (bb2->preds) > 2)
5510 : return;
5511 :
5512 436133 : if (cond_store_replacement_limited (bb1, bb2, bb, e1, e2, true))
5513 : {
5514 1022 : cfgchanged = true;
5515 1022 : return;
5516 : }
5517 435111 : gimple *assign = cselim_candidate (bb1, bb2, e1);
5518 435111 : if (cond_store_replacement (bb1, bb2, e1, e2, assign, nontrap))
5519 203 : cfgchanged = true;
5520 1068132 : };
5521 :
5522 1068132 : execute_over_cond_phis (cselim_exec);
5523 :
5524 2136264 : delete nontrap;
5525 : /* If the CFG has changed, we should cleanup the CFG. */
5526 1068132 : if (cfgchanged)
5527 : {
5528 1430 : gsi_commit_edge_inserts ();
5529 1430 : todo = TODO_cleanup_cfg;
5530 : }
5531 1068132 : scev_finalize ();
5532 1068132 : loop_optimizer_finalize ();
5533 1068132 : return todo;
5534 : }
|