Branch data Line data Source code
1 : : /* Optimization of PHI nodes by converting them into straightline code.
2 : : Copyright (C) 2004-2025 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 "gimplify.h"
39 : : #include "gimple-iterator.h"
40 : : #include "gimplify-me.h"
41 : : #include "tree-cfg.h"
42 : : #include "tree-dfa.h"
43 : : #include "domwalk.h"
44 : : #include "cfgloop.h"
45 : : #include "tree-data-ref.h"
46 : : #include "tree-scalar-evolution.h"
47 : : #include "tree-inline.h"
48 : : #include "case-cfn-macros.h"
49 : : #include "tree-eh.h"
50 : : #include "gimple-fold.h"
51 : : #include "internal-fn.h"
52 : : #include "gimple-range.h"
53 : : #include "gimple-match.h"
54 : : #include "dbgcnt.h"
55 : : #include "tree-ssa-propagate.h"
56 : : #include "tree-ssa-dce.h"
57 : : #include "tree-ssa-loop-niter.h"
58 : :
59 : : /* Return the singleton PHI in the SEQ of PHIs for edges E0 and E1. */
60 : :
61 : : static gphi *
62 : 3401609 : single_non_singleton_phi_for_edges (gimple_seq seq, edge e0, edge e1)
63 : : {
64 : 3401609 : gimple_stmt_iterator i;
65 : 3401609 : gphi *phi = NULL;
66 : 5089033 : for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
67 : : {
68 : 4138404 : gphi *p = as_a <gphi *> (gsi_stmt (i));
69 : : /* If the PHI arguments are equal then we can skip this PHI. */
70 : 4138404 : if (operand_equal_for_phi_arg_p (gimple_phi_arg_def (p, e0->dest_idx),
71 : 4138404 : gimple_phi_arg_def (p, e1->dest_idx)))
72 : 271624 : continue;
73 : :
74 : : /* Punt on virtual phis with different arguments from the edges. */
75 : 7733560 : if (virtual_operand_p (gimple_phi_result (p)))
76 : : return NULL;
77 : :
78 : : /* If we already have a PHI that has the two edge arguments are
79 : : different, then return it is not a singleton for these PHIs. */
80 : 1656863 : if (phi)
81 : : return NULL;
82 : :
83 : : phi = p;
84 : : }
85 : : return phi;
86 : : }
87 : :
88 : : /* Replace PHI node element whose edge is E in block BB with variable NEW.
89 : : Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
90 : : is known to have two edges, one of which must reach BB). */
91 : :
92 : : static void
93 : 89572 : replace_phi_edge_with_variable (basic_block cond_block,
94 : : edge e, gphi *phi, tree new_tree,
95 : : bitmap dce_ssa_names = nullptr)
96 : : {
97 : 89572 : basic_block bb = gimple_bb (phi);
98 : 89572 : gimple_stmt_iterator gsi;
99 : 89572 : tree phi_result = gimple_phi_result (phi);
100 : 89572 : bool deleteboth = false;
101 : :
102 : : /* Duplicate range info if they are the only things setting the target PHI.
103 : : This is needed as later on, the new_tree will be replacing
104 : : The assignement of the PHI.
105 : : For an example:
106 : : bb1:
107 : : _4 = min<a_1, 255>
108 : : goto bb2
109 : :
110 : : # RANGE [-INF, 255]
111 : : a_3 = PHI<_4(1)>
112 : : bb3:
113 : :
114 : : use(a_3)
115 : : And _4 gets propagated into the use of a_3 and losing the range info.
116 : : This can't be done for more than 2 incoming edges as the propagation
117 : : won't happen.
118 : : The new_tree needs to be defined in the same basic block as the conditional. */
119 : 89572 : if (TREE_CODE (new_tree) == SSA_NAME
120 : 89536 : && EDGE_COUNT (gimple_bb (phi)->preds) == 2
121 : 60172 : && INTEGRAL_TYPE_P (TREE_TYPE (phi_result))
122 : 55461 : && !SSA_NAME_RANGE_INFO (new_tree)
123 : 55371 : && SSA_NAME_RANGE_INFO (phi_result)
124 : 32583 : && gimple_bb (SSA_NAME_DEF_STMT (new_tree)) == cond_block
125 : 122155 : && dbg_cnt (phiopt_edge_range))
126 : 32583 : duplicate_ssa_name_range_info (new_tree, phi_result);
127 : :
128 : : /* Change the PHI argument to new. */
129 : 89572 : SET_USE (PHI_ARG_DEF_PTR (phi, e->dest_idx), new_tree);
130 : :
131 : : /* Remove the empty basic block. */
132 : 89572 : edge edge_to_remove = NULL, keep_edge = NULL;
133 : 89572 : if (EDGE_SUCC (cond_block, 0)->dest == bb)
134 : : {
135 : 34738 : edge_to_remove = EDGE_SUCC (cond_block, 1);
136 : 34738 : keep_edge = EDGE_SUCC (cond_block, 0);
137 : : }
138 : 54834 : else if (EDGE_SUCC (cond_block, 1)->dest == bb)
139 : : {
140 : : edge_to_remove = EDGE_SUCC (cond_block, 0);
141 : : keep_edge = EDGE_SUCC (cond_block, 1);
142 : : }
143 : 806 : else if ((keep_edge = find_edge (cond_block, e->src)))
144 : : {
145 : 806 : basic_block bb1 = EDGE_SUCC (cond_block, 0)->dest;
146 : 806 : basic_block bb2 = EDGE_SUCC (cond_block, 1)->dest;
147 : 1612 : if (single_pred_p (bb1) && single_pred_p (bb2)
148 : 91184 : && single_succ_p (bb1) && single_succ_p (bb2)
149 : 1612 : && empty_block_p (bb1) && empty_block_p (bb2))
150 : : deleteboth = true;
151 : : }
152 : : else
153 : 0 : gcc_unreachable ();
154 : :
155 : : /* If we are removing the cond on a loop exit,
156 : : reset number of iteration information of the loop. */
157 : 89572 : if (loop_exits_from_bb_p (cond_block->loop_father, cond_block))
158 : : {
159 : 10 : auto loop = cond_block->loop_father;
160 : 10 : free_numbers_of_iterations_estimates (loop);
161 : 10 : loop->any_upper_bound = false;
162 : 10 : loop->any_likely_upper_bound = false;
163 : : }
164 : :
165 : 89572 : if (edge_to_remove && EDGE_COUNT (edge_to_remove->dest->preds) == 1)
166 : : {
167 : 77974 : e->flags |= EDGE_FALLTHRU;
168 : 77974 : e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
169 : 77974 : e->probability = profile_probability::always ();
170 : 77974 : delete_basic_block (edge_to_remove->dest);
171 : :
172 : : /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
173 : 77974 : gsi = gsi_last_bb (cond_block);
174 : 77974 : gsi_remove (&gsi, true);
175 : : }
176 : 11598 : else if (deleteboth)
177 : : {
178 : 806 : basic_block bb1 = EDGE_SUCC (cond_block, 0)->dest;
179 : 806 : basic_block bb2 = EDGE_SUCC (cond_block, 1)->dest;
180 : :
181 : 806 : edge newedge = redirect_edge_and_branch (keep_edge, bb);
182 : :
183 : : /* The new edge should be the same. */
184 : 806 : gcc_assert (newedge == keep_edge);
185 : :
186 : 806 : keep_edge->flags |= EDGE_FALLTHRU;
187 : 806 : keep_edge->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
188 : 806 : keep_edge->probability = profile_probability::always ();
189 : :
190 : : /* Copy the edge's phi entry from the old one. */
191 : 806 : copy_phi_arg_into_existing_phi (e, keep_edge);
192 : :
193 : : /* Delete the old 2 empty basic blocks */
194 : 806 : delete_basic_block (bb1);
195 : 806 : delete_basic_block (bb2);
196 : :
197 : : /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
198 : 806 : gsi = gsi_last_bb (cond_block);
199 : 806 : gsi_remove (&gsi, true);
200 : : }
201 : : else
202 : : {
203 : : /* If there are other edges into the middle block make
204 : : CFG cleanup deal with the edge removal to avoid
205 : : updating dominators here in a non-trivial way. */
206 : 21584 : gcond *cond = as_a <gcond *> (*gsi_last_bb (cond_block));
207 : 10792 : if (keep_edge->flags & EDGE_FALSE_VALUE)
208 : 7381 : gimple_cond_make_false (cond);
209 : 3411 : else if (keep_edge->flags & EDGE_TRUE_VALUE)
210 : 3411 : gimple_cond_make_true (cond);
211 : : }
212 : :
213 : 89572 : if (dce_ssa_names)
214 : 84241 : simple_dce_from_worklist (dce_ssa_names);
215 : :
216 : 89572 : statistics_counter_event (cfun, "Replace PHI with variable", 1);
217 : :
218 : 89572 : if (dump_file && (dump_flags & TDF_DETAILS))
219 : 13 : fprintf (dump_file,
220 : : "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
221 : : cond_block->index,
222 : : bb->index);
223 : 89572 : }
224 : :
225 : : /* Returns true if the ARG used from DEF_STMT is profitable to move
226 : : to a PHI node of the basic block MERGE where the new statement
227 : : will be located. */
228 : : static bool
229 : 108222 : is_factor_profitable (gimple *def_stmt, basic_block merge, tree arg)
230 : : {
231 : : /* The defining statement should be conditional. */
232 : 108222 : if (dominated_by_p (CDI_DOMINATORS, merge,
233 : 108222 : gimple_bb (def_stmt)))
234 : : return false;
235 : :
236 : : /* If the arg is invariant, then there is
237 : : no extending of the live range. */
238 : 106318 : if (is_gimple_min_invariant (arg))
239 : : return true;
240 : :
241 : : /* Otherwise, the arg needs to be a ssa name. */
242 : 102688 : if (TREE_CODE (arg) != SSA_NAME)
243 : : return false;
244 : :
245 : : /* We should not increase the live range of arg
246 : : across too many statements or calls. */
247 : 102688 : gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
248 : 102688 : gsi_next_nondebug (&gsi);
249 : :
250 : : /* Skip past nops and predicates. */
251 : 205935 : while (!gsi_end_p (gsi)
252 : 103247 : && (gimple_code (gsi_stmt (gsi)) == GIMPLE_NOP
253 : 12654 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
254 : 559 : gsi_next_nondebug (&gsi);
255 : :
256 : : /* If the defining statement is at the end of the bb, then it is
257 : : always profitable to be to move. */
258 : 102688 : if (gsi_end_p (gsi))
259 : : return true;
260 : :
261 : : /* Check if the uses of arg is dominated by merge block, this is a quick and
262 : : rough estimate if arg is still alive at the merge bb. */
263 : : /* FIXME: extend to a more complete live range detection. */
264 : 12095 : use_operand_p use_p;
265 : 12095 : imm_use_iterator iter;
266 : 28501 : FOR_EACH_IMM_USE_FAST (use_p, iter, arg)
267 : : {
268 : 18477 : gimple *use_stmt = USE_STMT (use_p);
269 : 18477 : basic_block use_bb = gimple_bb (use_stmt);
270 : 18477 : if (dominated_by_p (CDI_DOMINATORS, merge, use_bb))
271 : : return true;
272 : : }
273 : :
274 : : /* If there are a few (non-call/asm) statements between
275 : : the old defining statement and end of the bb, then
276 : : the live range of new arg does not increase enough. */
277 : 10024 : int max_statements = param_phiopt_factor_max_stmts_live;
278 : :
279 : 29160 : while (!gsi_end_p (gsi))
280 : : {
281 : 20821 : gimple *stmt = gsi_stmt (gsi);
282 : 20821 : auto gcode = gimple_code (stmt);
283 : : /* Skip over NOPs and predicts. */
284 : 20825 : if (gcode == GIMPLE_NOP
285 : 20821 : || gcode == GIMPLE_PREDICT)
286 : : {
287 : 4 : gsi_next_nondebug (&gsi);
288 : 4 : continue;
289 : : }
290 : : /* Non-assigns will extend the live range too much. */
291 : 20817 : if (gcode != GIMPLE_ASSIGN)
292 : : return false;
293 : 20617 : max_statements --;
294 : 20617 : if (max_statements == 0)
295 : : return false;
296 : 19132 : gsi_next_nondebug (&gsi);
297 : : }
298 : : return true;
299 : : }
300 : :
301 : : /* PR66726: Factor operations out of COND_EXPR. If the arguments of the PHI
302 : : stmt are Unary operator, factor out the operation and perform the operation
303 : : to the result of PHI stmt. COND_STMT is the controlling predicate.
304 : : Return true if the operation was factored out; false otherwise. */
305 : :
306 : : static bool
307 : 2980220 : factor_out_conditional_operation (edge e0, edge e1, basic_block merge,
308 : : gphi *phi, gimple *cond_stmt)
309 : : {
310 : 2980220 : gimple *arg0_def_stmt = NULL, *arg1_def_stmt = NULL;
311 : 2980220 : tree temp, result;
312 : 2980220 : gphi *newphi;
313 : 2980220 : gimple_stmt_iterator gsi, gsi_for_def;
314 : 2980220 : location_t locus = gimple_location (phi);
315 : 2980220 : gimple_match_op arg0_op, arg1_op;
316 : :
317 : : /* We should only get here if the phi had two arguments. */
318 : 2980220 : gcc_assert (gimple_phi_num_args (phi) == 2);
319 : :
320 : : /* Virtual operands are never handled. */
321 : 5960440 : if (virtual_operand_p (gimple_phi_result (phi)))
322 : : return false;
323 : :
324 : 1275689 : tree arg0 = gimple_phi_arg_def (phi, e0->dest_idx);
325 : 1275689 : tree arg1 = gimple_phi_arg_def (phi, e1->dest_idx);
326 : 1275689 : location_t narg0_loc = gimple_location (phi);
327 : 1275689 : location_t narg1_loc = gimple_location (phi);
328 : 1275689 : if (gimple_phi_arg_location (phi, e0->dest_idx) != UNKNOWN_LOCATION)
329 : 1068129 : narg0_loc = gimple_phi_arg_location (phi, e0->dest_idx);
330 : 1275689 : if (gimple_phi_arg_location (phi, e1->dest_idx) != UNKNOWN_LOCATION)
331 : 901971 : narg1_loc = gimple_phi_arg_location (phi, e1->dest_idx);
332 : :
333 : 1275689 : gcc_assert (arg0 != NULL_TREE && arg1 != NULL_TREE);
334 : :
335 : : /* Arugments that are the same don't have anything to be
336 : : done to them. */
337 : 1275689 : if (operand_equal_for_phi_arg_p (arg0, arg1))
338 : : return false;
339 : :
340 : : /* First canonicalize to simplify tests. */
341 : 1262745 : if (TREE_CODE (arg0) != SSA_NAME)
342 : : {
343 : 251277 : std::swap (arg0, arg1);
344 : 251277 : std::swap (e0, e1);
345 : : }
346 : :
347 : 1262745 : if (TREE_CODE (arg0) != SSA_NAME
348 : 1137588 : || (TREE_CODE (arg1) != SSA_NAME
349 : 485846 : && TREE_CODE (arg1) != INTEGER_CST))
350 : : return false;
351 : :
352 : : /* Check if arg0 is an SSA_NAME and the stmt which defines arg0 is
353 : : an unary operation. */
354 : 1105512 : arg0_def_stmt = SSA_NAME_DEF_STMT (arg0);
355 : 1105512 : if (!gimple_extract_op (arg0_def_stmt, &arg0_op))
356 : : return false;
357 : :
358 : : /* Check to make sure none of the operands are in abnormal phis. */
359 : 560158 : if (arg0_op.operands_occurs_in_abnormal_phi ())
360 : : return false;
361 : :
362 : : /* Currently just support one operand expressions. */
363 : 560158 : if (arg0_op.num_ops != 1)
364 : : return false;
365 : :
366 : 124843 : tree new_arg0 = arg0_op.ops[0];
367 : 124843 : tree new_arg1;
368 : :
369 : : /* If arg0 have > 1 use, then this transformation actually increases
370 : : the number of expressions evaluated at runtime. */
371 : 124843 : if (!has_single_use (arg0))
372 : : return false;
373 : 102543 : if (!is_factor_profitable (arg0_def_stmt, merge, new_arg0))
374 : : return false;
375 : 99465 : if (gimple_has_location (arg0_def_stmt))
376 : 97092 : narg0_loc = gimple_location (arg0_def_stmt);
377 : :
378 : 99465 : if (TREE_CODE (arg1) == SSA_NAME)
379 : : {
380 : : /* Check if arg1 is an SSA_NAME. */
381 : 50793 : arg1_def_stmt = SSA_NAME_DEF_STMT (arg1);
382 : 50793 : if (!gimple_extract_op (arg1_def_stmt, &arg1_op))
383 : : return false;
384 : 33892 : if (arg1_op.code != arg0_op.code)
385 : : return false;
386 : 8481 : if (arg1_op.num_ops != arg0_op.num_ops)
387 : : return false;
388 : 8481 : if (arg1_op.operands_occurs_in_abnormal_phi ())
389 : : return false;
390 : :
391 : : /* If arg1 have > 1 use, then this transformation actually increases
392 : : the number of expressions evaluated at runtime. */
393 : 8481 : if (!has_single_use (arg1))
394 : : return false;
395 : :
396 : 5679 : new_arg1 = arg1_op.ops[0];
397 : 5679 : if (!is_factor_profitable (arg1_def_stmt, merge, new_arg1))
398 : : return false;
399 : 5168 : if (gimple_has_location (arg1_def_stmt))
400 : 5070 : narg1_loc = gimple_location (arg1_def_stmt);
401 : :
402 : : /* Chose the location for the new statement if the phi location is unknown. */
403 : 5168 : if (locus == UNKNOWN_LOCATION)
404 : : {
405 : 5168 : if (narg0_loc == UNKNOWN_LOCATION
406 : 5168 : && narg1_loc != UNKNOWN_LOCATION)
407 : : locus = narg1_loc;
408 : 5168 : else if (narg0_loc != UNKNOWN_LOCATION
409 : 5168 : && narg1_loc == UNKNOWN_LOCATION)
410 : 46 : locus = narg0_loc;
411 : : }
412 : : }
413 : : else
414 : : {
415 : : /* For constants only handle if the phi was the only one. */
416 : 48672 : if (single_non_singleton_phi_for_edges (phi_nodes (merge), e0, e1) == NULL)
417 : : return false;
418 : : /* TODO: handle more than just casts here. */
419 : 38315 : if (!gimple_assign_cast_p (arg0_def_stmt))
420 : : return false;
421 : :
422 : : /* arg0_def_stmt should be conditional. */
423 : 30979 : if (dominated_by_p (CDI_DOMINATORS, gimple_bb (phi), gimple_bb (arg0_def_stmt)))
424 : : return false;
425 : :
426 : : /* If arg1 is an INTEGER_CST, fold it to new type if it fits, or else
427 : : if the bits will not be modified during the conversion, except for
428 : : boolean types whose precision is not 1 (see int_fits_type_p). */
429 : 61948 : if (!INTEGRAL_TYPE_P (TREE_TYPE (new_arg0))
430 : 61672 : || !(int_fits_type_p (arg1, TREE_TYPE (new_arg0))
431 : 2036 : || (TYPE_PRECISION (TREE_TYPE (new_arg0))
432 : 2036 : == TYPE_PRECISION (TREE_TYPE (arg1))
433 : 1508 : && (TREE_CODE (TREE_TYPE (new_arg0)) != BOOLEAN_TYPE
434 : 0 : || TYPE_PRECISION (TREE_TYPE (new_arg0)) == 1))))
435 : : return false;
436 : :
437 : : /* For the INTEGER_CST case, we are just moving the
438 : : conversion from one place to another, which can often
439 : : hurt as the conversion moves further away from the
440 : : statement that computes the value. So, perform this
441 : : only if new_arg0 is an operand of COND_STMT, or
442 : : if arg0_def_stmt is the only non-debug stmt in
443 : : its basic block, because then it is possible this
444 : : could enable further optimizations (minmax replacement
445 : : etc.). See PR71016.
446 : : Note no-op conversions don't have this issue as
447 : : it will not generate any zero/sign extend in that case. */
448 : 30175 : if ((TYPE_PRECISION (TREE_TYPE (new_arg0))
449 : 30175 : != TYPE_PRECISION (TREE_TYPE (arg1)))
450 : 13808 : && new_arg0 != gimple_cond_lhs (cond_stmt)
451 : 12893 : && new_arg0 != gimple_cond_rhs (cond_stmt)
452 : 43062 : && gimple_bb (arg0_def_stmt) == e0->src)
453 : : {
454 : 12887 : gsi = gsi_for_stmt (arg0_def_stmt);
455 : 12887 : gsi_prev_nondebug (&gsi);
456 : : /* Ignore nops, predicates and labels. */
457 : 25794 : while (!gsi_end_p (gsi)
458 : 12907 : && (gimple_code (gsi_stmt (gsi)) == GIMPLE_NOP
459 : : || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT
460 : : || gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL))
461 : 20 : gsi_prev_nondebug (&gsi);
462 : :
463 : 12887 : if (!gsi_end_p (gsi))
464 : : {
465 : 8759 : gimple *stmt = gsi_stmt (gsi);
466 : 2962407 : if (gassign *assign = dyn_cast <gassign *> (stmt))
467 : : {
468 : 8376 : tree lhs = gimple_assign_lhs (assign);
469 : 8376 : tree lhst = TREE_TYPE (lhs);
470 : 8376 : enum tree_code ass_code
471 : 8376 : = gimple_assign_rhs_code (assign);
472 : 8376 : if (ass_code != MAX_EXPR && ass_code != MIN_EXPR
473 : : /* Conversions from boolean like types is ok
474 : : as `a?1:b` and `a?0:b` will always simplify
475 : : to `a & b` or `a | b`.
476 : : See PR 116890. */
477 : 8376 : && !(INTEGRAL_TYPE_P (lhst)
478 : 7960 : && TYPE_UNSIGNED (lhst)
479 : 5278 : && TYPE_PRECISION (lhst) == 1))
480 : : return false;
481 : 4582 : if (lhs != gimple_assign_rhs1 (arg0_def_stmt))
482 : : return false;
483 : 4523 : gsi_prev_nondebug (&gsi);
484 : 4523 : if (!gsi_end_p (gsi))
485 : : return false;
486 : : }
487 : : else
488 : : return false;
489 : : }
490 : : }
491 : 21718 : new_arg1 = fold_convert (TREE_TYPE (new_arg0), arg1);
492 : :
493 : : /* Drop the overlow that fold_convert might add. */
494 : 21718 : if (TREE_OVERFLOW (new_arg1))
495 : 0 : new_arg1 = drop_tree_overflow (new_arg1);
496 : :
497 : : /* The locus of the new statement is arg0 defining statement. */
498 : 21718 : if (gimple_has_location (arg0_def_stmt))
499 : 21305 : locus = gimple_location (arg0_def_stmt);
500 : : }
501 : :
502 : : /* If types of new_arg0 and new_arg1 are different bailout. */
503 : 26886 : if (!types_compatible_p (TREE_TYPE (new_arg0), TREE_TYPE (new_arg1)))
504 : : return false;
505 : :
506 : : /* Create a new PHI stmt. */
507 : 26322 : result = gimple_phi_result (phi);
508 : 26322 : temp = make_ssa_name (TREE_TYPE (new_arg0), NULL);
509 : :
510 : 26322 : gimple_match_op new_op = arg0_op;
511 : :
512 : : /* Create the operation stmt if possible and insert it. */
513 : 26322 : new_op.ops[0] = temp;
514 : 26322 : gimple_seq seq = NULL;
515 : 26322 : result = maybe_push_res_to_seq (&new_op, &seq, result);
516 : :
517 : : /* If we can't create the new statement, release the temp name
518 : : and return back. */
519 : 26322 : if (!result)
520 : : {
521 : 133 : release_ssa_name (temp);
522 : 133 : return false;
523 : : }
524 : :
525 : 26189 : if (locus != UNKNOWN_LOCATION)
526 : 21347 : annotate_all_with_location (seq, locus);
527 : 26189 : gsi = gsi_after_labels (gimple_bb (phi));
528 : 26189 : gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING);
529 : :
530 : 26189 : newphi = create_phi_node (temp, gimple_bb (phi));
531 : :
532 : 26189 : if (dump_file && (dump_flags & TDF_DETAILS))
533 : : {
534 : 14 : fprintf (dump_file, "PHI ");
535 : 14 : print_generic_expr (dump_file, gimple_phi_result (phi));
536 : 14 : fprintf (dump_file,
537 : : " changed to factor operation out from COND_EXPR.\n");
538 : 14 : fprintf (dump_file, "New stmt with OPERATION that defines ");
539 : 14 : print_generic_expr (dump_file, result);
540 : 14 : fprintf (dump_file, ".\n");
541 : : }
542 : :
543 : : /* Remove the old operation(s) that has single use. */
544 : 26189 : gsi_for_def = gsi_for_stmt (arg0_def_stmt);
545 : 26189 : gsi_remove (&gsi_for_def, true);
546 : 26189 : release_defs (arg0_def_stmt);
547 : :
548 : 26189 : if (arg1_def_stmt)
549 : : {
550 : 4471 : gsi_for_def = gsi_for_stmt (arg1_def_stmt);
551 : 4471 : gsi_remove (&gsi_for_def, true);
552 : 4471 : release_defs (arg1_def_stmt);
553 : : }
554 : :
555 : 26189 : add_phi_arg (newphi, new_arg0, e0, narg0_loc);
556 : 26189 : add_phi_arg (newphi, new_arg1, e1, narg1_loc);
557 : :
558 : : /* Remove the original PHI stmt. */
559 : 26189 : gsi = gsi_for_stmt (phi);
560 : 26189 : gsi_remove (&gsi, true);
561 : :
562 : 26189 : statistics_counter_event (cfun, "factored out operation", 1);
563 : :
564 : 26189 : return true;
565 : : }
566 : :
567 : :
568 : : /* Return TRUE if SEQ/OP pair should be allowed during early phiopt.
569 : : Currently this is to allow MIN/MAX and ABS/NEGATE and constants. */
570 : : static bool
571 : 163211 : phiopt_early_allow (gimple_seq &seq, gimple_match_op &op)
572 : : {
573 : : /* Don't allow functions. */
574 : 163211 : if (!op.code.is_tree_code ())
575 : : return false;
576 : 163186 : tree_code code = (tree_code)op.code;
577 : :
578 : : /* For non-empty sequence, only allow one statement
579 : : a MIN/MAX and an original MIN/MAX. */
580 : 163186 : if (!gimple_seq_empty_p (seq))
581 : : {
582 : 136429 : if (code == MIN_EXPR || code == MAX_EXPR)
583 : : {
584 : 140612 : if (!gimple_seq_singleton_p (seq))
585 : : return false;
586 : :
587 : 11 : gimple *stmt = gimple_seq_first_stmt (seq);
588 : : /* Only allow assignments. */
589 : 11 : if (!is_gimple_assign (stmt))
590 : : return false;
591 : 11 : code = gimple_assign_rhs_code (stmt);
592 : 11 : return code == MIN_EXPR || code == MAX_EXPR;
593 : : }
594 : : return false;
595 : : }
596 : :
597 : 26757 : switch (code)
598 : : {
599 : : case MIN_EXPR:
600 : : case MAX_EXPR:
601 : : case ABS_EXPR:
602 : : case ABSU_EXPR:
603 : : case NEGATE_EXPR:
604 : : case SSA_NAME:
605 : : return true;
606 : : case INTEGER_CST:
607 : : case REAL_CST:
608 : : case VECTOR_CST:
609 : : case FIXED_CST:
610 : : return true;
611 : : default:
612 : : return false;
613 : : }
614 : : }
615 : :
616 : : /* gimple_simplify_phiopt is like gimple_simplify but designed for PHIOPT.
617 : : Return NULL if nothing can be simplified or the resulting simplified value
618 : : with parts pushed if EARLY_P was true. Also rejects non allowed tree code
619 : : if EARLY_P is set.
620 : : Takes the comparison from COMP_STMT and two args, ARG0 and ARG1 and tries
621 : : to simplify CMP ? ARG0 : ARG1.
622 : : Also try to simplify (!CMP) ? ARG1 : ARG0 if the non-inverse failed. */
623 : : static tree
624 : 525854 : gimple_simplify_phiopt (bool early_p, tree type, gimple *comp_stmt,
625 : : tree arg0, tree arg1,
626 : : gimple_seq *seq)
627 : : {
628 : 525854 : gimple_seq seq1 = NULL;
629 : 525854 : enum tree_code comp_code = gimple_cond_code (comp_stmt);
630 : 525854 : location_t loc = gimple_location (comp_stmt);
631 : 525854 : tree cmp0 = gimple_cond_lhs (comp_stmt);
632 : 525854 : tree cmp1 = gimple_cond_rhs (comp_stmt);
633 : : /* To handle special cases like floating point comparison, it is easier and
634 : : less error-prone to build a tree and gimplify it on the fly though it is
635 : : less efficient.
636 : : Don't use fold_build2 here as that might create (bool)a instead of just
637 : : "a != 0". */
638 : 525854 : tree cond = build2_loc (loc, comp_code, boolean_type_node,
639 : : cmp0, cmp1);
640 : :
641 : 525854 : if (dump_file && (dump_flags & TDF_FOLDING))
642 : : {
643 : 1 : fprintf (dump_file, "\nphiopt match-simplify trying:\n\t");
644 : 1 : print_generic_expr (dump_file, cond);
645 : 1 : fprintf (dump_file, " ? ");
646 : 1 : print_generic_expr (dump_file, arg0);
647 : 1 : fprintf (dump_file, " : ");
648 : 1 : print_generic_expr (dump_file, arg1);
649 : 1 : fprintf (dump_file, "\n");
650 : : }
651 : :
652 : 525854 : gimple_match_op op (gimple_match_cond::UNCOND,
653 : 525854 : COND_EXPR, type, cond, arg0, arg1);
654 : :
655 : 525854 : if (op.resimplify (&seq1, follow_all_ssa_edges))
656 : : {
657 : 152559 : bool allowed = !early_p || phiopt_early_allow (seq1, op);
658 : 152559 : tree result = maybe_push_res_to_seq (&op, &seq1);
659 : 152559 : if (dump_file && (dump_flags & TDF_FOLDING))
660 : : {
661 : 1 : fprintf (dump_file, "\nphiopt match-simplify back:\n");
662 : 1 : if (seq1)
663 : 0 : print_gimple_seq (dump_file, seq1, 0, TDF_VOPS|TDF_MEMSYMS);
664 : 1 : fprintf (dump_file, "result: ");
665 : 1 : if (result)
666 : 1 : print_generic_expr (dump_file, result);
667 : : else
668 : 0 : fprintf (dump_file, " (none)");
669 : 1 : fprintf (dump_file, "\n");
670 : 1 : if (!allowed)
671 : 0 : fprintf (dump_file, "rejected because early\n");
672 : : }
673 : : /* Early we want only to allow some generated tree codes. */
674 : 152559 : if (allowed && result)
675 : : {
676 : 81277 : if (loc != UNKNOWN_LOCATION)
677 : 80808 : annotate_all_with_location (seq1, loc);
678 : 81277 : gimple_seq_add_seq_without_update (seq, seq1);
679 : 81277 : return result;
680 : : }
681 : : }
682 : 444577 : gimple_seq_discard (seq1);
683 : 444577 : seq1 = NULL;
684 : :
685 : : /* Try the inverted comparison, that is !COMP ? ARG1 : ARG0. */
686 : 444577 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (cmp0));
687 : :
688 : 444577 : if (comp_code == ERROR_MARK)
689 : : return NULL;
690 : :
691 : 430974 : cond = build2_loc (loc,
692 : : comp_code, boolean_type_node,
693 : : cmp0, cmp1);
694 : :
695 : 430974 : if (dump_file && (dump_flags & TDF_FOLDING))
696 : : {
697 : 0 : fprintf (dump_file, "\nphiopt match-simplify trying:\n\t");
698 : 0 : print_generic_expr (dump_file, cond);
699 : 0 : fprintf (dump_file, " ? ");
700 : 0 : print_generic_expr (dump_file, arg1);
701 : 0 : fprintf (dump_file, " : ");
702 : 0 : print_generic_expr (dump_file, arg0);
703 : 0 : fprintf (dump_file, "\n");
704 : : }
705 : :
706 : 430974 : gimple_match_op op1 (gimple_match_cond::UNCOND,
707 : 430974 : COND_EXPR, type, cond, arg1, arg0);
708 : :
709 : 430974 : if (op1.resimplify (&seq1, follow_all_ssa_edges))
710 : : {
711 : 72325 : bool allowed = !early_p || phiopt_early_allow (seq1, op1);
712 : 72325 : tree result = maybe_push_res_to_seq (&op1, &seq1);
713 : 72325 : if (dump_file && (dump_flags & TDF_FOLDING))
714 : : {
715 : 0 : fprintf (dump_file, "\nphiopt match-simplify back:\n");
716 : 0 : if (seq1)
717 : 0 : print_gimple_seq (dump_file, seq1, 0, TDF_VOPS|TDF_MEMSYMS);
718 : 0 : fprintf (dump_file, "result: ");
719 : 0 : if (result)
720 : 0 : print_generic_expr (dump_file, result);
721 : : else
722 : 0 : fprintf (dump_file, " (none)");
723 : 0 : fprintf (dump_file, "\n");
724 : 0 : if (!allowed)
725 : 0 : fprintf (dump_file, "rejected because early\n");
726 : : }
727 : : /* Early we want only to allow some generated tree codes. */
728 : 72325 : if (allowed && result)
729 : : {
730 : 2964 : if (loc != UNKNOWN_LOCATION)
731 : 2964 : annotate_all_with_location (seq1, loc);
732 : 2964 : gimple_seq_add_seq_without_update (seq, seq1);
733 : 2964 : return result;
734 : : }
735 : : }
736 : 428010 : gimple_seq_discard (seq1);
737 : :
738 : 428010 : return NULL;
739 : : }
740 : :
741 : : /* empty_bb_or_one_feeding_into_p returns true if bb was empty basic block
742 : : or it has one cheap preparation statement that feeds into the PHI
743 : : statement and it sets STMT to that statement. */
744 : : static bool
745 : 865781 : empty_bb_or_one_feeding_into_p (basic_block bb,
746 : : gimple *phi,
747 : : gimple *&stmt)
748 : : {
749 : 865781 : stmt = nullptr;
750 : 865781 : gimple *stmt_to_move = nullptr;
751 : 865781 : tree lhs;
752 : :
753 : 865781 : if (empty_block_p (bb))
754 : : return true;
755 : :
756 : 746222 : if (!single_pred_p (bb))
757 : : return false;
758 : :
759 : : /* The middle bb cannot have phi nodes as we don't
760 : : move those assignments yet. */
761 : 438562 : if (!gimple_seq_empty_p (phi_nodes (bb)))
762 : : return false;
763 : :
764 : 438536 : gimple_stmt_iterator gsi;
765 : :
766 : 438536 : gsi = gsi_start_nondebug_after_labels_bb (bb);
767 : 880203 : while (!gsi_end_p (gsi))
768 : : {
769 : 655054 : gimple *s = gsi_stmt (gsi);
770 : 655054 : gsi_next_nondebug (&gsi);
771 : : /* Skip over Predict and nop statements. */
772 : 655054 : if (gimple_code (s) == GIMPLE_PREDICT
773 : 655054 : || gimple_code (s) == GIMPLE_NOP)
774 : 3131 : continue;
775 : : /* If there is more one statement return false. */
776 : 651923 : if (stmt_to_move)
777 : : return false;
778 : : stmt_to_move = s;
779 : : }
780 : :
781 : : /* The only statement here was a Predict or a nop statement
782 : : so return true. */
783 : 225149 : if (!stmt_to_move)
784 : : return true;
785 : :
786 : 450298 : if (gimple_vuse (stmt_to_move))
787 : : return false;
788 : :
789 : 149147 : if (gimple_could_trap_p (stmt_to_move)
790 : 149147 : || gimple_has_side_effects (stmt_to_move))
791 : 4874 : return false;
792 : :
793 : 144273 : ssa_op_iter it;
794 : 144273 : tree use;
795 : 321600 : FOR_EACH_SSA_TREE_OPERAND (use, stmt_to_move, it, SSA_OP_USE)
796 : 177971 : if (ssa_name_maybe_undef_p (use))
797 : : return false;
798 : :
799 : : /* Allow assignments but allow some builtin/internal calls.
800 : : As const calls don't match any of the above, yet they could
801 : : still have some side-effects - they could contain
802 : : gimple_could_trap_p statements, like floating point
803 : : exceptions or integer division by zero. See PR70586.
804 : : FIXME: perhaps gimple_has_side_effects or gimple_could_trap_p
805 : : should handle this.
806 : : Allow some known builtin/internal calls that are known not to
807 : : trap: logical functions (e.g. bswap and bit counting). */
808 : 143629 : if (!is_gimple_assign (stmt_to_move))
809 : : {
810 : 6308 : if (!is_gimple_call (stmt_to_move))
811 : : return false;
812 : 6057 : combined_fn cfn = gimple_call_combined_fn (stmt_to_move);
813 : 6057 : switch (cfn)
814 : : {
815 : : default:
816 : : return false;
817 : 5077 : case CFN_BUILT_IN_BSWAP16:
818 : 5077 : case CFN_BUILT_IN_BSWAP32:
819 : 5077 : case CFN_BUILT_IN_BSWAP64:
820 : 5077 : case CFN_BUILT_IN_BSWAP128:
821 : 5077 : CASE_CFN_FFS:
822 : 5077 : CASE_CFN_PARITY:
823 : 5077 : CASE_CFN_POPCOUNT:
824 : 5077 : CASE_CFN_CLZ:
825 : 5077 : CASE_CFN_CTZ:
826 : 5077 : case CFN_BUILT_IN_CLRSB:
827 : 5077 : case CFN_BUILT_IN_CLRSBL:
828 : 5077 : case CFN_BUILT_IN_CLRSBLL:
829 : 5077 : lhs = gimple_call_lhs (stmt_to_move);
830 : 5077 : break;
831 : : }
832 : : }
833 : : else
834 : 137321 : lhs = gimple_assign_lhs (stmt_to_move);
835 : :
836 : 142398 : gimple *use_stmt;
837 : 142398 : use_operand_p use_p;
838 : :
839 : : /* Allow only a statement which feeds into the other stmt. */
840 : 142398 : if (!lhs || TREE_CODE (lhs) != SSA_NAME
841 : 142398 : || !single_imm_use (lhs, &use_p, &use_stmt)
842 : 284791 : || use_stmt != phi)
843 : 5 : return false;
844 : :
845 : 142393 : stmt = stmt_to_move;
846 : 142393 : return true;
847 : : }
848 : :
849 : : /* Move STMT to before GSI and insert its defining
850 : : name into INSERTED_EXPRS bitmap.
851 : : Also rewrite its if it might be undefined when unconditionalized. */
852 : : static void
853 : 168482 : move_stmt (gimple *stmt, gimple_stmt_iterator *gsi, auto_bitmap &inserted_exprs)
854 : : {
855 : 168482 : if (!stmt)
856 : 162960 : return;
857 : 5522 : if (dump_file && (dump_flags & TDF_DETAILS))
858 : : {
859 : 7 : fprintf (dump_file, "statement un-sinked:\n");
860 : 7 : print_gimple_stmt (dump_file, stmt, 0,
861 : : TDF_VOPS|TDF_MEMSYMS);
862 : : }
863 : :
864 : 5522 : tree name = gimple_get_lhs (stmt);
865 : : // Mark the name to be renamed if there is one.
866 : 5522 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (name));
867 : 5522 : gimple_stmt_iterator gsi1 = gsi_for_stmt (stmt);
868 : 5522 : gsi_move_before (&gsi1, gsi, GSI_NEW_STMT);
869 : 5522 : reset_flow_sensitive_info (name);
870 : :
871 : : /* Rewrite some code which might be undefined when
872 : : unconditionalized. */
873 : 5522 : if (gimple_needing_rewrite_undefined (stmt))
874 : 1051 : rewrite_to_defined_unconditional (gsi);
875 : : }
876 : :
877 : : /* RAII style class to temporarily remove flow sensitive
878 : : from ssa names defined by a gimple statement. */
879 : : class auto_flow_sensitive
880 : : {
881 : : public:
882 : : auto_flow_sensitive (gimple *s);
883 : : ~auto_flow_sensitive ();
884 : : private:
885 : : auto_vec<std::pair<tree, flow_sensitive_info_storage>, 2> stack;
886 : : };
887 : :
888 : : /* Constructor for auto_flow_sensitive. Saves
889 : : off the ssa names' flow sensitive information
890 : : that was defined by gimple statement S and
891 : : resets it to be non-flow based ones. */
892 : :
893 : 1051708 : auto_flow_sensitive::auto_flow_sensitive (gimple *s)
894 : : {
895 : 1051708 : if (!s)
896 : 916263 : return;
897 : 135445 : ssa_op_iter it;
898 : 135445 : tree def;
899 : 270890 : FOR_EACH_SSA_TREE_OPERAND (def, s, it, SSA_OP_DEF)
900 : : {
901 : 135445 : flow_sensitive_info_storage storage;
902 : 135445 : storage.save_and_clear (def);
903 : 135445 : stack.safe_push (std::make_pair (def, storage));
904 : : }
905 : : }
906 : :
907 : : /* Deconstructor, restores the flow sensitive information
908 : : for the SSA names that had been saved off. */
909 : :
910 : 1051708 : auto_flow_sensitive::~auto_flow_sensitive ()
911 : : {
912 : 3290569 : for (auto p : stack)
913 : 135445 : p.second.restore (p.first);
914 : 1051708 : }
915 : :
916 : : /* The function match_simplify_replacement does the main work of doing the
917 : : replacement using match and simplify. Return true if the replacement is done.
918 : : Otherwise return false.
919 : : BB is the basic block where the replacement is going to be done on. ARG0
920 : : is argument 0 from PHI. Likewise for ARG1. */
921 : :
922 : : static bool
923 : 838022 : match_simplify_replacement (basic_block cond_bb, basic_block middle_bb,
924 : : basic_block middle_bb_alt,
925 : : edge e0, edge e1, gphi *phi,
926 : : tree arg0, tree arg1, bool early_p,
927 : : bool threeway_p)
928 : : {
929 : 838022 : gimple *stmt;
930 : 838022 : gimple_stmt_iterator gsi;
931 : 838022 : edge true_edge, false_edge;
932 : 838022 : gimple_seq seq = NULL;
933 : 838022 : tree result;
934 : 838022 : gimple *stmt_to_move = NULL;
935 : 838022 : gimple *stmt_to_move_alt = NULL;
936 : 838022 : tree arg_true, arg_false;
937 : :
938 : : /* Special case A ? B : B as this will always simplify to B. */
939 : 838022 : if (operand_equal_for_phi_arg_p (arg0, arg1))
940 : : return false;
941 : :
942 : : /* If the basic block only has a cheap preparation statement,
943 : : allow it and move it once the transformation is done. */
944 : 838022 : if (!empty_bb_or_one_feeding_into_p (middle_bb, phi, stmt_to_move))
945 : : return false;
946 : :
947 : 541943 : if (threeway_p
948 : 541943 : && middle_bb != middle_bb_alt
949 : 541943 : && !empty_bb_or_one_feeding_into_p (middle_bb_alt, phi,
950 : : stmt_to_move_alt))
951 : : return false;
952 : :
953 : : /* Do not make conditional undefs unconditional. */
954 : 530362 : if ((TREE_CODE (arg0) == SSA_NAME
955 : 269945 : && ssa_name_maybe_undef_p (arg0))
956 : 799992 : || (TREE_CODE (arg1) == SSA_NAME
957 : 222994 : && ssa_name_maybe_undef_p (arg1)))
958 : : return false;
959 : :
960 : : /* At this point we know we have a GIMPLE_COND with two successors.
961 : : One successor is BB, the other successor is an empty block which
962 : : falls through into BB.
963 : :
964 : : There is a single PHI node at the join point (BB).
965 : :
966 : : So, given the condition COND, and the two PHI arguments, match and simplify
967 : : can happen on (COND) ? arg0 : arg1. */
968 : :
969 : 525854 : stmt = last_nondebug_stmt (cond_bb);
970 : :
971 : : /* We need to know which is the true edge and which is the false
972 : : edge so that we know when to invert the condition below. */
973 : 525854 : extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
974 : :
975 : : /* Forward the edges over the middle basic block. */
976 : 525854 : if (true_edge->dest == middle_bb)
977 : 330527 : true_edge = EDGE_SUCC (true_edge->dest, 0);
978 : 525854 : if (false_edge->dest == middle_bb)
979 : 195327 : false_edge = EDGE_SUCC (false_edge->dest, 0);
980 : :
981 : : /* When THREEWAY_P then e1 will point to the edge of the final transition
982 : : from middle-bb to end. */
983 : 525854 : if (true_edge == e0)
984 : : {
985 : 330527 : if (!threeway_p)
986 : 314726 : gcc_assert (false_edge == e1);
987 : : arg_true = arg0;
988 : : arg_false = arg1;
989 : : }
990 : : else
991 : : {
992 : 195327 : gcc_assert (false_edge == e0);
993 : 195327 : if (!threeway_p)
994 : 195071 : gcc_assert (true_edge == e1);
995 : : arg_true = arg1;
996 : : arg_false = arg0;
997 : : }
998 : :
999 : 525854 : tree type = TREE_TYPE (gimple_phi_result (phi));
1000 : 525854 : {
1001 : 525854 : auto_flow_sensitive s1(stmt_to_move);
1002 : 525854 : auto_flow_sensitive s_alt(stmt_to_move_alt);
1003 : :
1004 : 525854 : result = gimple_simplify_phiopt (early_p, type, stmt,
1005 : : arg_true, arg_false,
1006 : : &seq);
1007 : 525854 : }
1008 : :
1009 : 525854 : if (!result)
1010 : : return false;
1011 : 84241 : if (dump_file && (dump_flags & TDF_FOLDING))
1012 : 1 : fprintf (dump_file, "accepted the phiopt match-simplify.\n");
1013 : :
1014 : 84241 : auto_bitmap exprs_maybe_dce;
1015 : :
1016 : : /* Mark the cond statements' lhs/rhs as maybe dce. */
1017 : 84241 : if (TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
1018 : 84241 : && !SSA_NAME_IS_DEFAULT_DEF (gimple_cond_lhs (stmt)))
1019 : 79578 : bitmap_set_bit (exprs_maybe_dce,
1020 : 79578 : SSA_NAME_VERSION (gimple_cond_lhs (stmt)));
1021 : 84241 : if (TREE_CODE (gimple_cond_rhs (stmt)) == SSA_NAME
1022 : 84241 : && !SSA_NAME_IS_DEFAULT_DEF (gimple_cond_rhs (stmt)))
1023 : 24837 : bitmap_set_bit (exprs_maybe_dce,
1024 : 24837 : SSA_NAME_VERSION (gimple_cond_rhs (stmt)));
1025 : :
1026 : 84241 : gsi = gsi_last_bb (cond_bb);
1027 : : /* Insert the sequence generated from gimple_simplify_phiopt. */
1028 : 84241 : if (seq)
1029 : : {
1030 : : // Mark the lhs of the new statements maybe for dce
1031 : 76452 : mark_lhs_in_seq_for_dce (exprs_maybe_dce, seq);
1032 : 76452 : gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING);
1033 : : }
1034 : :
1035 : : /* If there was a statement to move, move it to right before
1036 : : the original conditional. */
1037 : 84241 : move_stmt (stmt_to_move, &gsi, exprs_maybe_dce);
1038 : 84241 : move_stmt (stmt_to_move_alt, &gsi, exprs_maybe_dce);
1039 : :
1040 : 84241 : replace_phi_edge_with_variable (cond_bb, e1, phi, result, exprs_maybe_dce);
1041 : :
1042 : : /* Add Statistic here even though replace_phi_edge_with_variable already
1043 : : does it as we want to be able to count when match-simplify happens vs
1044 : : the others. */
1045 : 84241 : statistics_counter_event (cfun, "match-simplify PHI replacement", 1);
1046 : :
1047 : : /* Note that we optimized this PHI. */
1048 : 84241 : return true;
1049 : 84241 : }
1050 : :
1051 : : /* Update *ARG which is defined in STMT so that it contains the
1052 : : computed value if that seems profitable. Return true if the
1053 : : statement is made dead by that rewriting. */
1054 : :
1055 : : static bool
1056 : 339071 : jump_function_from_stmt (tree *arg, gimple *stmt)
1057 : : {
1058 : 339071 : enum tree_code code = gimple_assign_rhs_code (stmt);
1059 : 339071 : if (code == ADDR_EXPR)
1060 : : {
1061 : : /* For arg = &p->i transform it to p, if possible. */
1062 : 4378 : tree rhs1 = gimple_assign_rhs1 (stmt);
1063 : 4378 : poly_int64 offset;
1064 : 4378 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs1, 0),
1065 : : &offset);
1066 : 4378 : if (tem
1067 : 4293 : && TREE_CODE (tem) == MEM_REF
1068 : 8671 : && known_eq (mem_ref_offset (tem) + offset, 0))
1069 : : {
1070 : 1804 : *arg = TREE_OPERAND (tem, 0);
1071 : 1804 : return true;
1072 : : }
1073 : : }
1074 : : /* TODO: Much like IPA-CP jump-functions we want to handle constant
1075 : : additions symbolically here, and we'd need to update the comparison
1076 : : code that compares the arg + cst tuples in our caller. For now the
1077 : : code above exactly handles the VEC_BASE pattern from vec.h. */
1078 : : return false;
1079 : : }
1080 : :
1081 : : /* RHS is a source argument in a BIT_AND_EXPR or BIT_IOR_EXPR which feeds
1082 : : a conditional of the form SSA_NAME NE 0.
1083 : :
1084 : : If RHS is fed by a simple EQ_EXPR or NE_EXPR comparison of two values,
1085 : : see if the two input values of the comparison match arg0 and arg1.
1086 : :
1087 : : If so update *code and return TRUE. Otherwise return FALSE. */
1088 : :
1089 : : static bool
1090 : 183354 : rhs_is_fed_for_value_replacement (const_tree arg0, const_tree arg1,
1091 : : enum tree_code *code, const_tree rhs,
1092 : : enum tree_code bit_expression_code)
1093 : : {
1094 : : /* Obviously if RHS is not an SSA_NAME, we can't look at the defining
1095 : : statement. */
1096 : 183354 : if (TREE_CODE (rhs) == SSA_NAME)
1097 : : {
1098 : 138034 : gimple *def1 = SSA_NAME_DEF_STMT (rhs);
1099 : :
1100 : : /* Verify the defining statement has an EQ_EXPR or NE_EXPR on the RHS. */
1101 : 138034 : if (is_gimple_assign (def1)
1102 : 138034 : && ((bit_expression_code == BIT_AND_EXPR
1103 : 89220 : && gimple_assign_rhs_code (def1) == EQ_EXPR)
1104 : 110612 : || (bit_expression_code == BIT_IOR_EXPR
1105 : 39799 : && gimple_assign_rhs_code (def1) == NE_EXPR)))
1106 : : {
1107 : : /* Finally verify the source operands of the EQ_EXPR or NE_EXPR
1108 : : are equal to arg0 and arg1. */
1109 : 23599 : tree op0 = gimple_assign_rhs1 (def1);
1110 : 23599 : tree op1 = gimple_assign_rhs2 (def1);
1111 : 23599 : if ((operand_equal_for_phi_arg_p (arg0, op0)
1112 : 582 : && operand_equal_for_phi_arg_p (arg1, op1))
1113 : 24129 : || (operand_equal_for_phi_arg_p (arg0, op1)
1114 : 641 : && operand_equal_for_phi_arg_p (arg1, op0)))
1115 : : {
1116 : : /* We will perform the optimization. */
1117 : 450 : *code = gimple_assign_rhs_code (def1);
1118 : 450 : return true;
1119 : : }
1120 : : }
1121 : : }
1122 : : return false;
1123 : : }
1124 : :
1125 : : /* Return TRUE if arg0/arg1 are equal to the rhs/lhs or lhs/rhs of COND.
1126 : :
1127 : : Also return TRUE if arg0/arg1 are equal to the source arguments of an
1128 : : EQ comparison feeding a BIT_AND_EXPR, or NE comparison feeding a
1129 : : BIT_IOR_EXPR which feeds COND.
1130 : :
1131 : : Return FALSE otherwise. */
1132 : :
1133 : : static bool
1134 : 697904 : operand_equal_for_value_replacement (const_tree arg0, const_tree arg1,
1135 : : enum tree_code *code, gimple *cond)
1136 : : {
1137 : 697904 : gimple *def;
1138 : 697904 : tree lhs = gimple_cond_lhs (cond);
1139 : 697904 : tree rhs = gimple_cond_rhs (cond);
1140 : :
1141 : 697904 : if ((operand_equal_for_phi_arg_p (arg0, lhs)
1142 : 28280 : && operand_equal_for_phi_arg_p (arg1, rhs))
1143 : 718704 : || (operand_equal_for_phi_arg_p (arg1, lhs)
1144 : 46030 : && operand_equal_for_phi_arg_p (arg0, rhs)))
1145 : 14105 : return true;
1146 : :
1147 : : /* Now handle more complex case where we have an EQ comparison
1148 : : feeding a BIT_AND_EXPR, or a NE comparison feeding a BIT_IOR_EXPR,
1149 : : which then feeds into COND.
1150 : :
1151 : : First verify that COND is of the form SSA_NAME NE 0. */
1152 : 392465 : if (*code != NE_EXPR || !integer_zerop (rhs)
1153 : 963921 : || TREE_CODE (lhs) != SSA_NAME)
1154 : 403788 : return false;
1155 : :
1156 : : /* Now ensure that SSA_NAME is set by a BIT_AND_EXPR or BIT_OR_EXPR. */
1157 : 280011 : def = SSA_NAME_DEF_STMT (lhs);
1158 : 280011 : if (!is_gimple_assign (def)
1159 : 280011 : || (gimple_assign_rhs_code (def) != BIT_AND_EXPR
1160 : 122715 : && gimple_assign_rhs_code (def) != BIT_IOR_EXPR))
1161 : : return false;
1162 : :
1163 : : /* Now verify arg0/arg1 correspond to the source arguments of an EQ
1164 : : comparison feeding the BIT_AND_EXPR or a NE comparison feeding the
1165 : : BIT_IOR_EXPR. */
1166 : :
1167 : 91840 : tree tmp = gimple_assign_rhs1 (def);
1168 : 91840 : if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp,
1169 : : gimple_assign_rhs_code (def)))
1170 : : return true;
1171 : :
1172 : 91514 : tmp = gimple_assign_rhs2 (def);
1173 : 91514 : if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp,
1174 : : gimple_assign_rhs_code (def)))
1175 : : return true;
1176 : :
1177 : : return false;
1178 : : }
1179 : :
1180 : : /* Returns true if ARG is a neutral element for operation CODE
1181 : : on the RIGHT side. */
1182 : :
1183 : : static bool
1184 : 864 : neutral_element_p (tree_code code, tree arg, bool right)
1185 : : {
1186 : 864 : switch (code)
1187 : : {
1188 : 41 : case PLUS_EXPR:
1189 : 41 : case BIT_IOR_EXPR:
1190 : 41 : case BIT_XOR_EXPR:
1191 : 41 : return integer_zerop (arg);
1192 : :
1193 : 190 : case LROTATE_EXPR:
1194 : 190 : case RROTATE_EXPR:
1195 : 190 : case LSHIFT_EXPR:
1196 : 190 : case RSHIFT_EXPR:
1197 : 190 : case MINUS_EXPR:
1198 : 190 : case POINTER_PLUS_EXPR:
1199 : 190 : return right && integer_zerop (arg);
1200 : :
1201 : 483 : case MULT_EXPR:
1202 : 483 : return integer_onep (arg);
1203 : :
1204 : 31 : case TRUNC_DIV_EXPR:
1205 : 31 : case CEIL_DIV_EXPR:
1206 : 31 : case FLOOR_DIV_EXPR:
1207 : 31 : case ROUND_DIV_EXPR:
1208 : 31 : case EXACT_DIV_EXPR:
1209 : 31 : return right && integer_onep (arg);
1210 : :
1211 : 0 : case BIT_AND_EXPR:
1212 : 0 : return integer_all_onesp (arg);
1213 : :
1214 : : default:
1215 : : return false;
1216 : : }
1217 : : }
1218 : :
1219 : : /* Returns true if ARG is an absorbing element for operation CODE. */
1220 : :
1221 : : static bool
1222 : 680 : absorbing_element_p (tree_code code, tree arg, bool right, tree rval)
1223 : : {
1224 : 680 : switch (code)
1225 : : {
1226 : 18 : case BIT_IOR_EXPR:
1227 : 18 : return integer_all_onesp (arg);
1228 : :
1229 : 42 : case MULT_EXPR:
1230 : 42 : case BIT_AND_EXPR:
1231 : 42 : return integer_zerop (arg);
1232 : :
1233 : 2 : case LSHIFT_EXPR:
1234 : 2 : case RSHIFT_EXPR:
1235 : 2 : case LROTATE_EXPR:
1236 : 2 : case RROTATE_EXPR:
1237 : 2 : return !right && integer_zerop (arg);
1238 : :
1239 : 170 : case TRUNC_DIV_EXPR:
1240 : 170 : case CEIL_DIV_EXPR:
1241 : 170 : case FLOOR_DIV_EXPR:
1242 : 170 : case ROUND_DIV_EXPR:
1243 : 170 : case EXACT_DIV_EXPR:
1244 : 170 : case TRUNC_MOD_EXPR:
1245 : 170 : case CEIL_MOD_EXPR:
1246 : 170 : case FLOOR_MOD_EXPR:
1247 : 170 : case ROUND_MOD_EXPR:
1248 : 170 : return (!right
1249 : 12 : && integer_zerop (arg)
1250 : 181 : && tree_single_nonzero_warnv_p (rval, NULL));
1251 : :
1252 : : default:
1253 : : return false;
1254 : : }
1255 : : }
1256 : :
1257 : : /* The function value_replacement does the main work of doing the value
1258 : : replacement. Return non-zero if the replacement is done. Otherwise return
1259 : : 0. If we remove the middle basic block, return 2.
1260 : : BB is the basic block where the replacement is going to be done on. ARG0
1261 : : is argument 0 from the PHI. Likewise for ARG1. */
1262 : :
1263 : : static int
1264 : 2444288 : value_replacement (basic_block cond_bb, basic_block middle_bb,
1265 : : edge e0, edge e1, gphi *phi, tree arg0, tree arg1)
1266 : : {
1267 : 2444288 : gimple_stmt_iterator gsi;
1268 : 2444288 : edge true_edge, false_edge;
1269 : 2444288 : enum tree_code code;
1270 : 2444288 : bool empty_or_with_defined_p = true;
1271 : :
1272 : : /* Virtual operands don't need to be handled. */
1273 : 4327816 : if (virtual_operand_p (arg1))
1274 : : return 0;
1275 : :
1276 : : /* Special case A ? B : B as this will always simplify to B. */
1277 : 1301993 : if (operand_equal_for_phi_arg_p (arg0, arg1))
1278 : : return 0;
1279 : :
1280 : 2282148 : gcond *cond = as_a <gcond *> (*gsi_last_bb (cond_bb));
1281 : 1141074 : code = gimple_cond_code (cond);
1282 : :
1283 : : /* This transformation is only valid for equality comparisons. */
1284 : 1141074 : if (code != NE_EXPR && code != EQ_EXPR)
1285 : : return 0;
1286 : :
1287 : : /* Do not make conditional undefs unconditional. */
1288 : 725998 : if ((TREE_CODE (arg0) == SSA_NAME
1289 : 550373 : && ssa_name_maybe_undef_p (arg0))
1290 : 1274600 : || (TREE_CODE (arg1) == SSA_NAME
1291 : 311958 : && ssa_name_maybe_undef_p (arg1)))
1292 : : return false;
1293 : :
1294 : : /* If the type says honor signed zeros we cannot do this
1295 : : optimization. */
1296 : 710297 : if (HONOR_SIGNED_ZEROS (arg1))
1297 : : return 0;
1298 : :
1299 : : /* If there is a statement in MIDDLE_BB that defines one of the PHI
1300 : : arguments, then adjust arg0 or arg1. */
1301 : 697904 : gsi = gsi_start_nondebug_after_labels_bb (middle_bb);
1302 : 2098671 : while (!gsi_end_p (gsi))
1303 : : {
1304 : 1400767 : gimple *stmt = gsi_stmt (gsi);
1305 : 1400767 : tree lhs;
1306 : 1400767 : gsi_next_nondebug (&gsi);
1307 : 1400767 : if (!is_gimple_assign (stmt))
1308 : : {
1309 : 210328 : if (gimple_code (stmt) != GIMPLE_PREDICT
1310 : 210328 : && gimple_code (stmt) != GIMPLE_NOP)
1311 : : empty_or_with_defined_p = false;
1312 : 210328 : continue;
1313 : : }
1314 : : /* Now try to adjust arg0 or arg1 according to the computation
1315 : : in the statement. */
1316 : 1190439 : lhs = gimple_assign_lhs (stmt);
1317 : 339071 : if (!(lhs == arg0
1318 : 339071 : && jump_function_from_stmt (&arg0, stmt))
1319 : 1192243 : || (lhs == arg1
1320 : 0 : && jump_function_from_stmt (&arg1, stmt)))
1321 : : empty_or_with_defined_p = false;
1322 : : }
1323 : :
1324 : : /* The middle bb is not empty if there are any phi nodes. */
1325 : 697904 : if (phi_nodes (middle_bb))
1326 : 98570 : empty_or_with_defined_p = false;
1327 : :
1328 : : /* We need to know which is the true edge and which is the false
1329 : : edge so that we know if have abs or negative abs. */
1330 : 697904 : extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
1331 : :
1332 : : /* At this point we know we have a COND_EXPR with two successors.
1333 : : One successor is BB, the other successor is an empty block which
1334 : : falls through into BB.
1335 : :
1336 : : The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
1337 : :
1338 : : There is a single PHI node at the join point (BB) with two arguments.
1339 : :
1340 : : We now need to verify that the two arguments in the PHI node match
1341 : : the two arguments to the equality comparison. */
1342 : :
1343 : 697904 : bool equal_p = operand_equal_for_value_replacement (arg0, arg1, &code, cond);
1344 : 697904 : bool maybe_equal_p = false;
1345 : 697904 : if (!equal_p
1346 : 697904 : && empty_or_with_defined_p
1347 : 204423 : && TREE_CODE (gimple_cond_rhs (cond)) == INTEGER_CST
1348 : 862989 : && (operand_equal_for_phi_arg_p (gimple_cond_lhs (cond), arg0)
1349 : 165085 : ? TREE_CODE (arg1) == INTEGER_CST
1350 : 154510 : : (operand_equal_for_phi_arg_p (gimple_cond_lhs (cond), arg1)
1351 : 21941 : && TREE_CODE (arg0) == INTEGER_CST)))
1352 : : maybe_equal_p = true;
1353 : 678371 : if (equal_p || maybe_equal_p)
1354 : : {
1355 : 34088 : edge e;
1356 : 34088 : tree arg;
1357 : :
1358 : : /* For NE_EXPR, we want to build an assignment result = arg where
1359 : : arg is the PHI argument associated with the true edge. For
1360 : : EQ_EXPR we want the PHI argument associated with the false edge. */
1361 : 34088 : e = (code == NE_EXPR ? true_edge : false_edge);
1362 : :
1363 : : /* Unfortunately, E may not reach BB (it may instead have gone to
1364 : : OTHER_BLOCK). If that is the case, then we want the single outgoing
1365 : : edge from OTHER_BLOCK which reaches BB and represents the desired
1366 : : path from COND_BLOCK. */
1367 : 34088 : if (e->dest == middle_bb)
1368 : 14123 : e = single_succ_edge (e->dest);
1369 : :
1370 : : /* Now we know the incoming edge to BB that has the argument for the
1371 : : RHS of our new assignment statement. */
1372 : 34088 : if (e0 == e)
1373 : : arg = arg0;
1374 : : else
1375 : 19965 : arg = arg1;
1376 : :
1377 : : /* If the middle basic block was empty or is defining the
1378 : : PHI arguments and this is a single phi where the args are different
1379 : : for the edges e0 and e1 then we can remove the middle basic block. */
1380 : 34088 : if (empty_or_with_defined_p
1381 : 34088 : && single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)),
1382 : : e0, e1) == phi)
1383 : : {
1384 : 19304 : use_operand_p use_p;
1385 : 19304 : gimple *use_stmt;
1386 : :
1387 : : /* Even if arg0/arg1 isn't equal to second operand of cond, we
1388 : : can optimize away the bb if we can prove it doesn't care whether
1389 : : phi result is arg0/arg1 or second operand of cond. Consider:
1390 : : <bb 2> [local count: 118111600]:
1391 : : if (i_2(D) == 4)
1392 : : goto <bb 4>; [97.00%]
1393 : : else
1394 : : goto <bb 3>; [3.00%]
1395 : :
1396 : : <bb 3> [local count: 3540129]:
1397 : :
1398 : : <bb 4> [local count: 118111600]:
1399 : : # i_6 = PHI <i_2(D)(3), 6(2)>
1400 : : _3 = i_6 != 0;
1401 : : Here, carg is 4, oarg is 6, crhs is 0, and because
1402 : : (4 != 0) == (6 != 0), we don't care if i_6 is 4 or 6, both
1403 : : have the same outcome. So, we can optimize this to:
1404 : : _3 = i_2(D) != 0;
1405 : : If the single imm use of phi result >, >=, < or <=, similarly
1406 : : we can check if both carg and oarg compare the same against
1407 : : crhs using ccode. */
1408 : 19304 : if (maybe_equal_p
1409 : 17983 : && TREE_CODE (arg) != INTEGER_CST
1410 : 37272 : && single_imm_use (gimple_phi_result (phi), &use_p, &use_stmt))
1411 : : {
1412 : 11520 : enum tree_code ccode = ERROR_MARK;
1413 : 11520 : tree clhs = NULL_TREE, crhs = NULL_TREE;
1414 : 11520 : tree carg = gimple_cond_rhs (cond);
1415 : 11520 : tree oarg = e0 == e ? arg1 : arg0;
1416 : 11520 : if (is_gimple_assign (use_stmt)
1417 : 11520 : && (TREE_CODE_CLASS (gimple_assign_rhs_code (use_stmt))
1418 : : == tcc_comparison))
1419 : : {
1420 : 55 : ccode = gimple_assign_rhs_code (use_stmt);
1421 : 55 : clhs = gimple_assign_rhs1 (use_stmt);
1422 : 55 : crhs = gimple_assign_rhs2 (use_stmt);
1423 : : }
1424 : 11465 : else if (gimple_code (use_stmt) == GIMPLE_COND)
1425 : : {
1426 : 64 : ccode = gimple_cond_code (use_stmt);
1427 : 64 : clhs = gimple_cond_lhs (use_stmt);
1428 : 64 : crhs = gimple_cond_rhs (use_stmt);
1429 : : }
1430 : 119 : if (ccode != ERROR_MARK
1431 : 119 : && clhs == gimple_phi_result (phi)
1432 : 201 : && TREE_CODE (crhs) == INTEGER_CST)
1433 : 58 : switch (ccode)
1434 : : {
1435 : 41 : case EQ_EXPR:
1436 : 41 : case NE_EXPR:
1437 : 41 : if (!tree_int_cst_equal (crhs, carg)
1438 : 41 : && !tree_int_cst_equal (crhs, oarg))
1439 : : equal_p = true;
1440 : : break;
1441 : 2 : case GT_EXPR:
1442 : 4 : if (tree_int_cst_lt (crhs, carg)
1443 : 2 : == tree_int_cst_lt (crhs, oarg))
1444 : : equal_p = true;
1445 : : break;
1446 : 0 : case GE_EXPR:
1447 : 0 : if (tree_int_cst_le (crhs, carg)
1448 : 0 : == tree_int_cst_le (crhs, oarg))
1449 : : equal_p = true;
1450 : : break;
1451 : 11 : case LT_EXPR:
1452 : 22 : if (tree_int_cst_lt (carg, crhs)
1453 : 11 : == tree_int_cst_lt (oarg, crhs))
1454 : : equal_p = true;
1455 : : break;
1456 : 4 : case LE_EXPR:
1457 : 8 : if (tree_int_cst_le (carg, crhs)
1458 : 4 : == tree_int_cst_le (oarg, crhs))
1459 : : equal_p = true;
1460 : : break;
1461 : : default:
1462 : : break;
1463 : : }
1464 : 11504 : if (equal_p)
1465 : : {
1466 : 16 : tree phires = gimple_phi_result (phi);
1467 : 16 : if (SSA_NAME_RANGE_INFO (phires))
1468 : : {
1469 : : /* After the optimization PHI result can have value
1470 : : which it couldn't have previously. */
1471 : 15 : value_range r (TREE_TYPE (phires));
1472 : 15 : if (get_global_range_query ()->range_of_expr (r, phires,
1473 : : phi))
1474 : : {
1475 : 15 : value_range tmp (carg, carg);
1476 : 15 : r.union_ (tmp);
1477 : 15 : reset_flow_sensitive_info (phires);
1478 : 15 : set_range_info (phires, r);
1479 : 15 : }
1480 : : else
1481 : 0 : reset_flow_sensitive_info (phires);
1482 : 15 : }
1483 : : }
1484 : 16 : if (equal_p && MAY_HAVE_DEBUG_BIND_STMTS)
1485 : : {
1486 : 13 : imm_use_iterator imm_iter;
1487 : 13 : tree phires = gimple_phi_result (phi);
1488 : 13 : tree temp = NULL_TREE;
1489 : 13 : bool reset_p = false;
1490 : :
1491 : : /* Add # DEBUG D#1 => arg != carg ? arg : oarg. */
1492 : 42 : FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, phires)
1493 : : {
1494 : 30 : if (!is_gimple_debug (use_stmt))
1495 : 13 : continue;
1496 : 17 : if (temp == NULL_TREE)
1497 : : {
1498 : 12 : if (!single_pred_p (middle_bb)
1499 : 12 : || EDGE_COUNT (gimple_bb (phi)->preds) != 2)
1500 : : {
1501 : : /* But only if middle_bb has a single
1502 : : predecessor and phi bb has two, otherwise
1503 : : we could use a SSA_NAME not usable in that
1504 : : place or wrong-debug. */
1505 : 1 : reset_p = true;
1506 : 1 : break;
1507 : : }
1508 : 11 : gimple_stmt_iterator gsi
1509 : 11 : = gsi_after_labels (gimple_bb (phi));
1510 : 11 : tree type = TREE_TYPE (phires);
1511 : 11 : temp = build_debug_expr_decl (type);
1512 : 11 : tree t = build2 (NE_EXPR, boolean_type_node,
1513 : : arg, carg);
1514 : 11 : t = build3 (COND_EXPR, type, t, arg, oarg);
1515 : 11 : gimple *g = gimple_build_debug_bind (temp, t, phi);
1516 : 11 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1517 : : }
1518 : 32 : FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
1519 : 16 : replace_exp (use_p, temp);
1520 : 16 : update_stmt (use_stmt);
1521 : 13 : }
1522 : 13 : if (reset_p)
1523 : 1 : reset_debug_uses (phi);
1524 : : }
1525 : : }
1526 : 7800 : if (equal_p)
1527 : : {
1528 : 1337 : replace_phi_edge_with_variable (cond_bb, e1, phi, arg);
1529 : : /* Note that we optimized this PHI. */
1530 : 1337 : return 2;
1531 : : }
1532 : : }
1533 : 14784 : else if (equal_p)
1534 : : {
1535 : 13234 : if (!single_pred_p (middle_bb))
1536 : : return 0;
1537 : 11821 : statistics_counter_event (cfun, "Replace PHI with "
1538 : : "variable/value_replacement", 1);
1539 : :
1540 : : /* Replace the PHI arguments with arg. */
1541 : 11821 : SET_PHI_ARG_DEF (phi, e0->dest_idx, arg);
1542 : 11821 : SET_PHI_ARG_DEF (phi, e1->dest_idx, arg);
1543 : 11821 : if (dump_file && (dump_flags & TDF_DETAILS))
1544 : : {
1545 : 0 : fprintf (dump_file, "PHI ");
1546 : 0 : print_generic_expr (dump_file, gimple_phi_result (phi));
1547 : 0 : fprintf (dump_file, " reduced for COND_EXPR in block %d to ",
1548 : : cond_bb->index);
1549 : 0 : print_generic_expr (dump_file, arg);
1550 : 0 : fprintf (dump_file, ".\n");
1551 : : }
1552 : 11821 : return 1;
1553 : : }
1554 : : }
1555 : :
1556 : 2992570 : if (!single_pred_p (middle_bb))
1557 : : return 0;
1558 : :
1559 : : /* Now optimize (x != 0) ? x + y : y to just x + y. */
1560 : 561545 : gsi = gsi_last_nondebug_bb (middle_bb);
1561 : 561545 : if (gsi_end_p (gsi))
1562 : : return 0;
1563 : :
1564 : 373799 : gimple *assign = gsi_stmt (gsi);
1565 : 373799 : if (!is_gimple_assign (assign)
1566 : 373799 : || (!INTEGRAL_TYPE_P (TREE_TYPE (arg0))
1567 : 97684 : && !POINTER_TYPE_P (TREE_TYPE (arg0))))
1568 : : return 0;
1569 : :
1570 : 318977 : if (gimple_assign_rhs_class (assign) != GIMPLE_BINARY_RHS)
1571 : : {
1572 : : /* If last stmt of the middle_bb is a conversion, handle it like
1573 : : a preparation statement through constant evaluation with
1574 : : checking for UB. */
1575 : 138001 : enum tree_code sc = gimple_assign_rhs_code (assign);
1576 : 138001 : if (CONVERT_EXPR_CODE_P (sc))
1577 : : assign = NULL;
1578 : : else
1579 : : return 0;
1580 : : }
1581 : :
1582 : : /* Punt if there are (degenerate) PHIs in middle_bb, there should not be. */
1583 : 204562 : if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
1584 : : return 0;
1585 : :
1586 : : /* Allow up to 2 cheap preparation statements that prepare argument
1587 : : for assign, e.g.:
1588 : : if (y_4 != 0)
1589 : : goto <bb 3>;
1590 : : else
1591 : : goto <bb 4>;
1592 : : <bb 3>:
1593 : : _1 = (int) y_4;
1594 : : iftmp.0_6 = x_5(D) r<< _1;
1595 : : <bb 4>:
1596 : : # iftmp.0_2 = PHI <iftmp.0_6(3), x_5(D)(2)>
1597 : : or:
1598 : : if (y_3(D) == 0)
1599 : : goto <bb 4>;
1600 : : else
1601 : : goto <bb 3>;
1602 : : <bb 3>:
1603 : : y_4 = y_3(D) & 31;
1604 : : _1 = (int) y_4;
1605 : : _6 = x_5(D) r<< _1;
1606 : : <bb 4>:
1607 : : # _2 = PHI <x_5(D)(2), _6(3)> */
1608 : 204562 : gimple *prep_stmt[2] = { NULL, NULL };
1609 : 204562 : int prep_cnt;
1610 : 204562 : for (prep_cnt = 0; ; prep_cnt++)
1611 : : {
1612 : 266514 : if (prep_cnt || assign)
1613 : 242928 : gsi_prev_nondebug (&gsi);
1614 : 266514 : if (gsi_end_p (gsi))
1615 : : break;
1616 : :
1617 : 201050 : gimple *g = gsi_stmt (gsi);
1618 : 201050 : if (gimple_code (g) == GIMPLE_LABEL)
1619 : : break;
1620 : :
1621 : 200704 : if (prep_cnt == 2 || !is_gimple_assign (g))
1622 : 138752 : return 0;
1623 : :
1624 : 177967 : tree lhs = gimple_assign_lhs (g);
1625 : 177967 : tree rhs1 = gimple_assign_rhs1 (g);
1626 : 177967 : use_operand_p use_p;
1627 : 177967 : gimple *use_stmt;
1628 : 177967 : if (TREE_CODE (lhs) != SSA_NAME
1629 : 170893 : || TREE_CODE (rhs1) != SSA_NAME
1630 : 110961 : || !INTEGRAL_TYPE_P (TREE_TYPE (lhs))
1631 : 107539 : || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1632 : 102295 : || !single_imm_use (lhs, &use_p, &use_stmt)
1633 : 279460 : || ((prep_cnt || assign)
1634 : 79438 : && use_stmt != (prep_cnt ? prep_stmt[prep_cnt - 1] : assign)))
1635 : 84032 : return 0;
1636 : 93935 : switch (gimple_assign_rhs_code (g))
1637 : : {
1638 : : CASE_CONVERT:
1639 : : break;
1640 : 19436 : case PLUS_EXPR:
1641 : 19436 : case BIT_AND_EXPR:
1642 : 19436 : case BIT_IOR_EXPR:
1643 : 19436 : case BIT_XOR_EXPR:
1644 : 19436 : if (TREE_CODE (gimple_assign_rhs2 (g)) != INTEGER_CST)
1645 : : return 0;
1646 : : break;
1647 : : default:
1648 : : return 0;
1649 : : }
1650 : 61952 : prep_stmt[prep_cnt] = g;
1651 : 61952 : }
1652 : :
1653 : : /* Only transform if it removes the condition. */
1654 : 65810 : if (!single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)), e0, e1))
1655 : : return 0;
1656 : :
1657 : : /* Size-wise, this is always profitable. */
1658 : 53274 : if (optimize_bb_for_speed_p (cond_bb)
1659 : : /* The special case is useless if it has a low probability. */
1660 : 50709 : && profile_status_for_fn (cfun) != PROFILE_ABSENT
1661 : 63052 : && EDGE_PRED (middle_bb, 0)->probability < profile_probability::even ()
1662 : : /* If assign is cheap, there is no point avoiding it. */
1663 : 65637 : && estimate_num_insns_seq (bb_seq (middle_bb), &eni_time_weights)
1664 : 12363 : >= 3 * estimate_num_insns (cond, &eni_time_weights))
1665 : 78 : return 0;
1666 : :
1667 : 53196 : tree cond_lhs = gimple_cond_lhs (cond);
1668 : 53196 : tree cond_rhs = gimple_cond_rhs (cond);
1669 : :
1670 : : /* Propagate the cond_rhs constant through preparation stmts,
1671 : : make sure UB isn't invoked while doing that. */
1672 : 55232 : for (int i = prep_cnt - 1; i >= 0; --i)
1673 : : {
1674 : 13772 : gimple *g = prep_stmt[i];
1675 : 13772 : tree grhs1 = gimple_assign_rhs1 (g);
1676 : 13772 : if (!operand_equal_for_phi_arg_p (cond_lhs, grhs1))
1677 : : return 0;
1678 : 7629 : cond_lhs = gimple_assign_lhs (g);
1679 : 7629 : cond_rhs = fold_convert (TREE_TYPE (grhs1), cond_rhs);
1680 : 7629 : if (TREE_CODE (cond_rhs) != INTEGER_CST
1681 : 7629 : || TREE_OVERFLOW (cond_rhs))
1682 : : return 0;
1683 : 2036 : if (gimple_assign_rhs_class (g) == GIMPLE_BINARY_RHS)
1684 : : {
1685 : 900 : cond_rhs = int_const_binop (gimple_assign_rhs_code (g), cond_rhs,
1686 : 900 : gimple_assign_rhs2 (g));
1687 : 900 : if (TREE_OVERFLOW (cond_rhs))
1688 : : return 0;
1689 : : }
1690 : 2036 : cond_rhs = fold_convert (TREE_TYPE (cond_lhs), cond_rhs);
1691 : 2036 : if (TREE_CODE (cond_rhs) != INTEGER_CST
1692 : 2036 : || TREE_OVERFLOW (cond_rhs))
1693 : : return 0;
1694 : : }
1695 : :
1696 : 41460 : tree lhs, rhs1, rhs2;
1697 : 41460 : enum tree_code code_def;
1698 : 41460 : if (assign)
1699 : : {
1700 : 41259 : lhs = gimple_assign_lhs (assign);
1701 : 41259 : rhs1 = gimple_assign_rhs1 (assign);
1702 : 41259 : rhs2 = gimple_assign_rhs2 (assign);
1703 : 41259 : code_def = gimple_assign_rhs_code (assign);
1704 : : }
1705 : : else
1706 : : {
1707 : 201 : gcc_assert (prep_cnt > 0);
1708 : : lhs = cond_lhs;
1709 : : rhs1 = NULL_TREE;
1710 : : rhs2 = NULL_TREE;
1711 : : code_def = ERROR_MARK;
1712 : : }
1713 : :
1714 : 23284 : if (((code == NE_EXPR && e1 == false_edge)
1715 : 20993 : || (code == EQ_EXPR && e1 == true_edge))
1716 : 23167 : && arg0 == lhs
1717 : 64627 : && ((assign == NULL
1718 : 200 : && operand_equal_for_phi_arg_p (arg1, cond_rhs))
1719 : : || (assign
1720 : 22967 : && arg1 == rhs1
1721 : 14229 : && operand_equal_for_phi_arg_p (rhs2, cond_lhs)
1722 : 403 : && neutral_element_p (code_def, cond_rhs, true))
1723 : 22909 : || (assign
1724 : 22909 : && arg1 == rhs2
1725 : 1136 : && operand_equal_for_phi_arg_p (rhs1, cond_lhs)
1726 : 461 : && neutral_element_p (code_def, cond_rhs, false))
1727 : : || (assign
1728 : 22898 : && operand_equal_for_phi_arg_p (arg1, cond_rhs)
1729 : 2702 : && ((operand_equal_for_phi_arg_p (rhs2, cond_lhs)
1730 : 234 : && absorbing_element_p (code_def, cond_rhs, true, rhs2))
1731 : 2701 : || (operand_equal_for_phi_arg_p (rhs1, cond_lhs)
1732 : 446 : && absorbing_element_p (code_def,
1733 : : cond_rhs, false, rhs2))))))
1734 : : {
1735 : 105 : gsi = gsi_for_stmt (cond);
1736 : : /* Moving ASSIGN might change VR of lhs, e.g. when moving u_6
1737 : : def-stmt in:
1738 : : if (n_5 != 0)
1739 : : goto <bb 3>;
1740 : : else
1741 : : goto <bb 4>;
1742 : :
1743 : : <bb 3>:
1744 : : # RANGE [0, 4294967294]
1745 : : u_6 = n_5 + 4294967295;
1746 : :
1747 : : <bb 4>:
1748 : : # u_3 = PHI <u_6(3), 4294967295(2)> */
1749 : 105 : reset_flow_sensitive_info (lhs);
1750 : 105 : gimple_stmt_iterator gsi_from;
1751 : 174 : for (int i = prep_cnt - 1; i >= 0; --i)
1752 : : {
1753 : 69 : tree plhs = gimple_assign_lhs (prep_stmt[i]);
1754 : 69 : reset_flow_sensitive_info (plhs);
1755 : 69 : gsi_from = gsi_for_stmt (prep_stmt[i]);
1756 : 69 : gsi_move_before (&gsi_from, &gsi);
1757 : : }
1758 : 105 : if (assign)
1759 : : {
1760 : 105 : gsi_from = gsi_for_stmt (assign);
1761 : 105 : gsi_move_before (&gsi_from, &gsi);
1762 : : }
1763 : 105 : replace_phi_edge_with_variable (cond_bb, e1, phi, lhs);
1764 : 105 : return 2;
1765 : : }
1766 : :
1767 : : return 0;
1768 : : }
1769 : :
1770 : : /* If VAR is an SSA_NAME that points to a BIT_NOT_EXPR then return the TREE for
1771 : : the value being inverted. */
1772 : :
1773 : : static tree
1774 : 0 : strip_bit_not (tree var)
1775 : : {
1776 : 0 : if (TREE_CODE (var) != SSA_NAME)
1777 : : return NULL_TREE;
1778 : :
1779 : 0 : gimple *assign = SSA_NAME_DEF_STMT (var);
1780 : 0 : if (gimple_code (assign) != GIMPLE_ASSIGN)
1781 : : return NULL_TREE;
1782 : :
1783 : 0 : if (gimple_assign_rhs_code (assign) != BIT_NOT_EXPR)
1784 : : return NULL_TREE;
1785 : :
1786 : 0 : return gimple_assign_rhs1 (assign);
1787 : : }
1788 : :
1789 : : /* Invert a MIN to a MAX or a MAX to a MIN expression CODE. */
1790 : :
1791 : : enum tree_code
1792 : 0 : invert_minmax_code (enum tree_code code)
1793 : : {
1794 : 0 : switch (code) {
1795 : : case MIN_EXPR:
1796 : : return MAX_EXPR;
1797 : 0 : case MAX_EXPR:
1798 : 0 : return MIN_EXPR;
1799 : 0 : default:
1800 : 0 : gcc_unreachable ();
1801 : : }
1802 : : }
1803 : :
1804 : : /* The function minmax_replacement does the main work of doing the minmax
1805 : : replacement. Return true if the replacement is done. Otherwise return
1806 : : false.
1807 : : BB is the basic block where the replacement is going to be done on. ARG0
1808 : : is argument 0 from the PHI. Likewise for ARG1.
1809 : :
1810 : : If THREEWAY_P then expect the BB to be laid out in diamond shape with each
1811 : : BB containing only a MIN or MAX expression. */
1812 : :
1813 : : static bool
1814 : 753768 : minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_middle_bb,
1815 : : edge e0, edge e1, gphi *phi, tree arg0, tree arg1, bool threeway_p)
1816 : : {
1817 : 753768 : tree result;
1818 : 753768 : edge true_edge, false_edge;
1819 : 753768 : enum tree_code minmax, ass_code;
1820 : 753768 : tree smaller, larger, arg_true, arg_false;
1821 : 753768 : gimple_stmt_iterator gsi, gsi_from;
1822 : :
1823 : 753768 : tree type = TREE_TYPE (gimple_phi_result (phi));
1824 : :
1825 : 1507536 : gcond *cond = as_a <gcond *> (*gsi_last_bb (cond_bb));
1826 : 753768 : enum tree_code cmp = gimple_cond_code (cond);
1827 : 753768 : tree rhs = gimple_cond_rhs (cond);
1828 : :
1829 : : /* Turn EQ/NE of extreme values to order comparisons. */
1830 : 753768 : if ((cmp == NE_EXPR || cmp == EQ_EXPR)
1831 : 561083 : && TREE_CODE (rhs) == INTEGER_CST
1832 : 1173953 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs)))
1833 : : {
1834 : 346635 : if (wi::eq_p (wi::to_wide (rhs), wi::min_value (TREE_TYPE (rhs))))
1835 : : {
1836 : 145096 : cmp = (cmp == EQ_EXPR) ? LT_EXPR : GE_EXPR;
1837 : 145096 : rhs = wide_int_to_tree (TREE_TYPE (rhs),
1838 : 290192 : wi::min_value (TREE_TYPE (rhs)) + 1);
1839 : : }
1840 : 201539 : else if (wi::eq_p (wi::to_wide (rhs), wi::max_value (TREE_TYPE (rhs))))
1841 : : {
1842 : 12692 : cmp = (cmp == EQ_EXPR) ? GT_EXPR : LE_EXPR;
1843 : 12692 : rhs = wide_int_to_tree (TREE_TYPE (rhs),
1844 : 25384 : wi::max_value (TREE_TYPE (rhs)) - 1);
1845 : : }
1846 : : }
1847 : :
1848 : : /* This transformation is only valid for order comparisons. Record which
1849 : : operand is smaller/larger if the result of the comparison is true. */
1850 : 753768 : tree alt_smaller = NULL_TREE;
1851 : 753768 : tree alt_larger = NULL_TREE;
1852 : 753768 : if (cmp == LT_EXPR || cmp == LE_EXPR)
1853 : : {
1854 : 135808 : smaller = gimple_cond_lhs (cond);
1855 : 135808 : larger = rhs;
1856 : : /* If we have smaller < CST it is equivalent to smaller <= CST-1.
1857 : : Likewise smaller <= CST is equivalent to smaller < CST+1. */
1858 : 135808 : if (TREE_CODE (larger) == INTEGER_CST
1859 : 135808 : && INTEGRAL_TYPE_P (TREE_TYPE (larger)))
1860 : : {
1861 : 87520 : if (cmp == LT_EXPR)
1862 : : {
1863 : 48415 : wi::overflow_type overflow;
1864 : 145245 : wide_int alt = wi::sub (wi::to_wide (larger), 1,
1865 : 48415 : TYPE_SIGN (TREE_TYPE (larger)),
1866 : 48415 : &overflow);
1867 : 48415 : if (! overflow)
1868 : 48415 : alt_larger = wide_int_to_tree (TREE_TYPE (larger), alt);
1869 : 48415 : }
1870 : : else
1871 : : {
1872 : 39105 : wi::overflow_type overflow;
1873 : 117315 : wide_int alt = wi::add (wi::to_wide (larger), 1,
1874 : 39105 : TYPE_SIGN (TREE_TYPE (larger)),
1875 : 39105 : &overflow);
1876 : 39105 : if (! overflow)
1877 : 39067 : alt_larger = wide_int_to_tree (TREE_TYPE (larger), alt);
1878 : 39105 : }
1879 : : }
1880 : : }
1881 : 617960 : else if (cmp == GT_EXPR || cmp == GE_EXPR)
1882 : : {
1883 : 211622 : smaller = rhs;
1884 : 211622 : larger = gimple_cond_lhs (cond);
1885 : : /* If we have larger > CST it is equivalent to larger >= CST+1.
1886 : : Likewise larger >= CST is equivalent to larger > CST-1. */
1887 : 211622 : if (TREE_CODE (smaller) == INTEGER_CST
1888 : 211622 : && INTEGRAL_TYPE_P (TREE_TYPE (smaller)))
1889 : : {
1890 : 178798 : wi::overflow_type overflow;
1891 : 178798 : if (cmp == GT_EXPR)
1892 : : {
1893 : 124875 : wide_int alt = wi::add (wi::to_wide (smaller), 1,
1894 : 41625 : TYPE_SIGN (TREE_TYPE (smaller)),
1895 : 41625 : &overflow);
1896 : 41625 : if (! overflow)
1897 : 41625 : alt_smaller = wide_int_to_tree (TREE_TYPE (smaller), alt);
1898 : 41625 : }
1899 : : else
1900 : : {
1901 : 411519 : wide_int alt = wi::sub (wi::to_wide (smaller), 1,
1902 : 137173 : TYPE_SIGN (TREE_TYPE (smaller)),
1903 : 137173 : &overflow);
1904 : 137173 : if (! overflow)
1905 : 137173 : alt_smaller = wide_int_to_tree (TREE_TYPE (smaller), alt);
1906 : 137173 : }
1907 : : }
1908 : : }
1909 : : else
1910 : : return false;
1911 : :
1912 : : /* Handle the special case of (signed_type)x < 0 being equivalent
1913 : : to x > MAX_VAL(signed_type) and (signed_type)x >= 0 equivalent
1914 : : to x <= MAX_VAL(signed_type). */
1915 : 347430 : if ((cmp == GE_EXPR || cmp == LT_EXPR)
1916 : 232174 : && INTEGRAL_TYPE_P (type)
1917 : 179907 : && TYPE_UNSIGNED (type)
1918 : 429438 : && integer_zerop (rhs))
1919 : : {
1920 : 10217 : tree op = gimple_cond_lhs (cond);
1921 : 10217 : if (TREE_CODE (op) == SSA_NAME
1922 : 10217 : && INTEGRAL_TYPE_P (TREE_TYPE (op))
1923 : 20434 : && !TYPE_UNSIGNED (TREE_TYPE (op)))
1924 : : {
1925 : 10217 : gimple *def_stmt = SSA_NAME_DEF_STMT (op);
1926 : 10217 : if (gimple_assign_cast_p (def_stmt))
1927 : : {
1928 : 4181 : tree op1 = gimple_assign_rhs1 (def_stmt);
1929 : 8362 : if (INTEGRAL_TYPE_P (TREE_TYPE (op1))
1930 : 4169 : && TYPE_UNSIGNED (TREE_TYPE (op1))
1931 : 4103 : && (TYPE_PRECISION (TREE_TYPE (op))
1932 : 4103 : == TYPE_PRECISION (TREE_TYPE (op1)))
1933 : 8269 : && useless_type_conversion_p (type, TREE_TYPE (op1)))
1934 : : {
1935 : 2422 : wide_int w1 = wi::max_value (TREE_TYPE (op));
1936 : 2422 : wide_int w2 = wi::add (w1, 1);
1937 : 2422 : if (cmp == LT_EXPR)
1938 : : {
1939 : 1709 : larger = op1;
1940 : 1709 : smaller = wide_int_to_tree (TREE_TYPE (op1), w1);
1941 : 1709 : alt_smaller = wide_int_to_tree (TREE_TYPE (op1), w2);
1942 : 1709 : alt_larger = NULL_TREE;
1943 : : }
1944 : : else
1945 : : {
1946 : 713 : smaller = op1;
1947 : 713 : larger = wide_int_to_tree (TREE_TYPE (op1), w1);
1948 : 713 : alt_larger = wide_int_to_tree (TREE_TYPE (op1), w2);
1949 : 713 : alt_smaller = NULL_TREE;
1950 : : }
1951 : 2422 : }
1952 : : }
1953 : : }
1954 : : }
1955 : :
1956 : : /* We need to know which is the true edge and which is the false
1957 : : edge so that we know if have abs or negative abs. */
1958 : 347430 : extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
1959 : :
1960 : : /* Forward the edges over the middle basic block. */
1961 : 347430 : if (true_edge->dest == middle_bb)
1962 : 234037 : true_edge = EDGE_SUCC (true_edge->dest, 0);
1963 : 347430 : if (false_edge->dest == middle_bb)
1964 : 113393 : false_edge = EDGE_SUCC (false_edge->dest, 0);
1965 : :
1966 : : /* When THREEWAY_P then e1 will point to the edge of the final transition
1967 : : from middle-bb to end. */
1968 : 347430 : if (true_edge == e0)
1969 : : {
1970 : 234037 : if (!threeway_p)
1971 : 191275 : gcc_assert (false_edge == e1);
1972 : : arg_true = arg0;
1973 : : arg_false = arg1;
1974 : : }
1975 : : else
1976 : : {
1977 : 113393 : gcc_assert (false_edge == e0);
1978 : 113393 : if (!threeway_p)
1979 : 112550 : gcc_assert (true_edge == e1);
1980 : : arg_true = arg1;
1981 : : arg_false = arg0;
1982 : : }
1983 : :
1984 : 347430 : if (empty_block_p (middle_bb)
1985 : 347430 : && (!threeway_p
1986 : 11095 : || empty_block_p (alt_middle_bb)))
1987 : : {
1988 : 149214 : if ((operand_equal_for_phi_arg_p (arg_true, smaller)
1989 : 130530 : || (alt_smaller
1990 : 72729 : && operand_equal_for_phi_arg_p (arg_true, alt_smaller)))
1991 : 153137 : && (operand_equal_for_phi_arg_p (arg_false, larger)
1992 : 21023 : || (alt_larger
1993 : 2359 : && operand_equal_for_phi_arg_p (arg_true, alt_larger))))
1994 : : {
1995 : : /* Case
1996 : :
1997 : : if (smaller < larger)
1998 : : rslt = smaller;
1999 : : else
2000 : : rslt = larger; */
2001 : : minmax = MIN_EXPR;
2002 : : }
2003 : 147630 : else if ((operand_equal_for_phi_arg_p (arg_false, smaller)
2004 : 130009 : || (alt_smaller
2005 : 75435 : && operand_equal_for_phi_arg_p (arg_false, alt_smaller)))
2006 : 159729 : && (operand_equal_for_phi_arg_p (arg_true, larger)
2007 : 27467 : || (alt_larger
2008 : 4813 : && operand_equal_for_phi_arg_p (arg_true, alt_larger))))
2009 : : minmax = MAX_EXPR;
2010 : : else
2011 : 145356 : return false;
2012 : : }
2013 : 198216 : else if (HONOR_NANS (type) || HONOR_SIGNED_ZEROS (type))
2014 : : /* The optimization may be unsafe due to NaNs. */
2015 : 9728 : return false;
2016 : 188488 : else if (middle_bb != alt_middle_bb && threeway_p)
2017 : : {
2018 : : /* Recognize the following case:
2019 : :
2020 : : if (smaller < larger)
2021 : : a = MIN (smaller, c);
2022 : : else
2023 : : b = MIN (larger, c);
2024 : : x = PHI <a, b>
2025 : :
2026 : : This is equivalent to
2027 : :
2028 : : a = MIN (smaller, c);
2029 : : x = MIN (larger, a); */
2030 : :
2031 : 35517 : gimple *assign = last_and_only_stmt (middle_bb);
2032 : 35517 : tree lhs, op0, op1, bound;
2033 : 35517 : tree alt_lhs, alt_op0, alt_op1;
2034 : 35517 : bool invert = false;
2035 : :
2036 : : /* When THREEWAY_P then e1 will point to the edge of the final transition
2037 : : from middle-bb to end. */
2038 : 35517 : if (true_edge == e0)
2039 : 34890 : gcc_assert (false_edge == EDGE_PRED (e1->src, 0));
2040 : : else
2041 : 627 : gcc_assert (true_edge == EDGE_PRED (e1->src, 0));
2042 : :
2043 : 35517 : bool valid_minmax_p = false;
2044 : 35517 : gimple_stmt_iterator it1
2045 : 35517 : = gsi_start_nondebug_after_labels_bb (middle_bb);
2046 : 35517 : gimple_stmt_iterator it2
2047 : 35517 : = gsi_start_nondebug_after_labels_bb (alt_middle_bb);
2048 : 35517 : if (gsi_one_nondebug_before_end_p (it1)
2049 : 35517 : && gsi_one_nondebug_before_end_p (it2))
2050 : : {
2051 : 9774 : gimple *stmt1 = gsi_stmt (it1);
2052 : 9774 : gimple *stmt2 = gsi_stmt (it2);
2053 : 9774 : if (is_gimple_assign (stmt1) && is_gimple_assign (stmt2))
2054 : : {
2055 : 7327 : enum tree_code code1 = gimple_assign_rhs_code (stmt1);
2056 : 7327 : enum tree_code code2 = gimple_assign_rhs_code (stmt2);
2057 : 7327 : valid_minmax_p = (code1 == MIN_EXPR || code1 == MAX_EXPR)
2058 : 7327 : && (code2 == MIN_EXPR || code2 == MAX_EXPR);
2059 : : }
2060 : : }
2061 : :
2062 : 40 : if (!valid_minmax_p)
2063 : : return false;
2064 : :
2065 : 40 : if (!assign
2066 : 40 : || gimple_code (assign) != GIMPLE_ASSIGN)
2067 : : return false;
2068 : :
2069 : : /* There cannot be any phi nodes in the middle bb. */
2070 : 40 : if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
2071 : : return false;
2072 : :
2073 : 40 : lhs = gimple_assign_lhs (assign);
2074 : 40 : ass_code = gimple_assign_rhs_code (assign);
2075 : 40 : if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
2076 : : return false;
2077 : :
2078 : 40 : op0 = gimple_assign_rhs1 (assign);
2079 : 40 : op1 = gimple_assign_rhs2 (assign);
2080 : :
2081 : 40 : assign = last_and_only_stmt (alt_middle_bb);
2082 : 40 : if (!assign
2083 : 40 : || gimple_code (assign) != GIMPLE_ASSIGN)
2084 : : return false;
2085 : :
2086 : : /* There cannot be any phi nodes in the alt middle bb. */
2087 : 40 : if (!gimple_seq_empty_p (phi_nodes (alt_middle_bb)))
2088 : : return false;
2089 : :
2090 : 40 : alt_lhs = gimple_assign_lhs (assign);
2091 : 40 : if (ass_code != gimple_assign_rhs_code (assign))
2092 : : return false;
2093 : :
2094 : 0 : if (!operand_equal_for_phi_arg_p (lhs, arg_true)
2095 : 0 : || !operand_equal_for_phi_arg_p (alt_lhs, arg_false))
2096 : 0 : return false;
2097 : :
2098 : 0 : alt_op0 = gimple_assign_rhs1 (assign);
2099 : 0 : alt_op1 = gimple_assign_rhs2 (assign);
2100 : :
2101 : 0 : if ((operand_equal_for_phi_arg_p (op0, smaller)
2102 : 0 : || (alt_smaller
2103 : 0 : && operand_equal_for_phi_arg_p (op0, alt_smaller)))
2104 : 0 : && (operand_equal_for_phi_arg_p (alt_op0, larger)
2105 : 0 : || (alt_larger
2106 : 0 : && operand_equal_for_phi_arg_p (alt_op0, alt_larger))))
2107 : : {
2108 : : /* We got here if the condition is true, i.e., SMALLER < LARGER. */
2109 : 0 : if (!operand_equal_for_phi_arg_p (op1, alt_op1))
2110 : : return false;
2111 : :
2112 : 0 : if ((arg0 = strip_bit_not (op0)) != NULL
2113 : 0 : && (arg1 = strip_bit_not (alt_op0)) != NULL
2114 : 0 : && (bound = strip_bit_not (op1)) != NULL)
2115 : : {
2116 : 0 : minmax = MAX_EXPR;
2117 : 0 : ass_code = invert_minmax_code (ass_code);
2118 : 0 : invert = true;
2119 : : }
2120 : : else
2121 : : {
2122 : : bound = op1;
2123 : : minmax = MIN_EXPR;
2124 : : arg0 = op0;
2125 : : arg1 = alt_op0;
2126 : : }
2127 : : }
2128 : 0 : else if ((operand_equal_for_phi_arg_p (op0, larger)
2129 : 0 : || (alt_larger
2130 : 0 : && operand_equal_for_phi_arg_p (op0, alt_larger)))
2131 : 0 : && (operand_equal_for_phi_arg_p (alt_op0, smaller)
2132 : 0 : || (alt_smaller
2133 : 0 : && operand_equal_for_phi_arg_p (alt_op0, alt_smaller))))
2134 : : {
2135 : : /* We got here if the condition is true, i.e., SMALLER > LARGER. */
2136 : 0 : if (!operand_equal_for_phi_arg_p (op1, alt_op1))
2137 : : return false;
2138 : :
2139 : 0 : if ((arg0 = strip_bit_not (op0)) != NULL
2140 : 0 : && (arg1 = strip_bit_not (alt_op0)) != NULL
2141 : 0 : && (bound = strip_bit_not (op1)) != NULL)
2142 : : {
2143 : 0 : minmax = MIN_EXPR;
2144 : 0 : ass_code = invert_minmax_code (ass_code);
2145 : 0 : invert = true;
2146 : : }
2147 : : else
2148 : : {
2149 : : bound = op1;
2150 : : minmax = MAX_EXPR;
2151 : : arg0 = op0;
2152 : : arg1 = alt_op0;
2153 : : }
2154 : : }
2155 : : else
2156 : 0 : return false;
2157 : :
2158 : : /* Emit the statement to compute min/max. */
2159 : 0 : location_t locus = gimple_location (last_nondebug_stmt (cond_bb));
2160 : 0 : gimple_seq stmts = NULL;
2161 : 0 : tree phi_result = gimple_phi_result (phi);
2162 : 0 : result = gimple_build (&stmts, locus, minmax, TREE_TYPE (phi_result),
2163 : : arg0, arg1);
2164 : 0 : result = gimple_build (&stmts, locus, ass_code, TREE_TYPE (phi_result),
2165 : : result, bound);
2166 : 0 : if (invert)
2167 : 0 : result = gimple_build (&stmts, locus, BIT_NOT_EXPR, TREE_TYPE (phi_result),
2168 : : result);
2169 : :
2170 : 0 : gsi = gsi_last_bb (cond_bb);
2171 : 0 : gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
2172 : :
2173 : 0 : replace_phi_edge_with_variable (cond_bb, e1, phi, result);
2174 : :
2175 : 0 : return true;
2176 : : }
2177 : 152971 : else if (!threeway_p
2178 : 152971 : || empty_block_p (alt_middle_bb))
2179 : : {
2180 : : /* Recognize the following case, assuming d <= u:
2181 : :
2182 : : if (a <= u)
2183 : : b = MAX (a, d);
2184 : : x = PHI <b, u>
2185 : :
2186 : : This is equivalent to
2187 : :
2188 : : b = MAX (a, d);
2189 : : x = MIN (b, u); */
2190 : :
2191 : 152971 : gimple *assign = last_and_only_stmt (middle_bb);
2192 : 152971 : tree lhs, op0, op1, bound;
2193 : :
2194 : 862318 : if (!single_pred_p (middle_bb))
2195 : : return false;
2196 : :
2197 : 147943 : if (!assign
2198 : 147943 : || gimple_code (assign) != GIMPLE_ASSIGN)
2199 : : return false;
2200 : :
2201 : : /* There cannot be any phi nodes in the middle bb. */
2202 : 79750 : if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
2203 : : return false;
2204 : :
2205 : 79730 : lhs = gimple_assign_lhs (assign);
2206 : 79730 : ass_code = gimple_assign_rhs_code (assign);
2207 : 79730 : if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
2208 : : return false;
2209 : 2905 : op0 = gimple_assign_rhs1 (assign);
2210 : 2905 : op1 = gimple_assign_rhs2 (assign);
2211 : :
2212 : 2905 : if (true_edge->src == middle_bb)
2213 : : {
2214 : : /* We got here if the condition is true, i.e., SMALLER < LARGER. */
2215 : 2398 : if (!operand_equal_for_phi_arg_p (lhs, arg_true))
2216 : : return false;
2217 : :
2218 : 2398 : if (operand_equal_for_phi_arg_p (arg_false, larger)
2219 : 2398 : || (alt_larger
2220 : 32 : && operand_equal_for_phi_arg_p (arg_false, alt_larger)))
2221 : : {
2222 : : /* Case
2223 : :
2224 : : if (smaller < larger)
2225 : : {
2226 : : r' = MAX_EXPR (smaller, bound)
2227 : : }
2228 : : r = PHI <r', larger> --> to be turned to MIN_EXPR. */
2229 : 1849 : if (ass_code != MAX_EXPR)
2230 : : return false;
2231 : :
2232 : 1832 : minmax = MIN_EXPR;
2233 : 1832 : if (operand_equal_for_phi_arg_p (op0, smaller)
2234 : 1832 : || (alt_smaller
2235 : 0 : && operand_equal_for_phi_arg_p (op0, alt_smaller)))
2236 : : bound = op1;
2237 : 916 : else if (operand_equal_for_phi_arg_p (op1, smaller)
2238 : 916 : || (alt_smaller
2239 : 0 : && operand_equal_for_phi_arg_p (op1, alt_smaller)))
2240 : : bound = op0;
2241 : : else
2242 : 0 : return false;
2243 : :
2244 : : /* We need BOUND <= LARGER. */
2245 : 1832 : if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
2246 : : bound, arg_false)))
2247 : : return false;
2248 : : }
2249 : 549 : else if (operand_equal_for_phi_arg_p (arg_false, smaller)
2250 : 549 : || (alt_smaller
2251 : 178 : && operand_equal_for_phi_arg_p (arg_false, alt_smaller)))
2252 : : {
2253 : : /* Case
2254 : :
2255 : : if (smaller < larger)
2256 : : {
2257 : : r' = MIN_EXPR (larger, bound)
2258 : : }
2259 : : r = PHI <r', smaller> --> to be turned to MAX_EXPR. */
2260 : 188 : if (ass_code != MIN_EXPR)
2261 : : return false;
2262 : :
2263 : 170 : minmax = MAX_EXPR;
2264 : 170 : if (operand_equal_for_phi_arg_p (op0, larger)
2265 : 170 : || (alt_larger
2266 : 10 : && operand_equal_for_phi_arg_p (op0, alt_larger)))
2267 : : bound = op1;
2268 : 11 : else if (operand_equal_for_phi_arg_p (op1, larger)
2269 : 11 : || (alt_larger
2270 : 10 : && operand_equal_for_phi_arg_p (op1, alt_larger)))
2271 : : bound = op0;
2272 : : else
2273 : 0 : return false;
2274 : :
2275 : : /* We need BOUND >= SMALLER. */
2276 : 170 : if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
2277 : : bound, arg_false)))
2278 : : return false;
2279 : : }
2280 : : else
2281 : 361 : return false;
2282 : : }
2283 : : else
2284 : : {
2285 : : /* We got here if the condition is false, i.e., SMALLER > LARGER. */
2286 : 507 : if (!operand_equal_for_phi_arg_p (lhs, arg_false))
2287 : : return false;
2288 : :
2289 : 507 : if (operand_equal_for_phi_arg_p (arg_true, larger)
2290 : 507 : || (alt_larger
2291 : 59 : && operand_equal_for_phi_arg_p (arg_true, alt_larger)))
2292 : : {
2293 : : /* Case
2294 : :
2295 : : if (smaller > larger)
2296 : : {
2297 : : r' = MIN_EXPR (smaller, bound)
2298 : : }
2299 : : r = PHI <r', larger> --> to be turned to MAX_EXPR. */
2300 : 21 : if (ass_code != MIN_EXPR)
2301 : : return false;
2302 : :
2303 : 21 : minmax = MAX_EXPR;
2304 : 21 : if (operand_equal_for_phi_arg_p (op0, smaller)
2305 : 21 : || (alt_smaller
2306 : 0 : && operand_equal_for_phi_arg_p (op0, alt_smaller)))
2307 : : bound = op1;
2308 : 0 : else if (operand_equal_for_phi_arg_p (op1, smaller)
2309 : 0 : || (alt_smaller
2310 : 0 : && operand_equal_for_phi_arg_p (op1, alt_smaller)))
2311 : : bound = op0;
2312 : : else
2313 : 0 : return false;
2314 : :
2315 : : /* We need BOUND >= LARGER. */
2316 : 21 : if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
2317 : : bound, arg_true)))
2318 : : return false;
2319 : : }
2320 : 486 : else if (operand_equal_for_phi_arg_p (arg_true, smaller)
2321 : 486 : || (alt_smaller
2322 : 60 : && operand_equal_for_phi_arg_p (arg_true, alt_smaller)))
2323 : : {
2324 : : /* Case
2325 : :
2326 : : if (smaller > larger)
2327 : : {
2328 : : r' = MAX_EXPR (larger, bound)
2329 : : }
2330 : : r = PHI <r', smaller> --> to be turned to MIN_EXPR. */
2331 : 7 : if (ass_code != MAX_EXPR)
2332 : : return false;
2333 : :
2334 : 7 : minmax = MIN_EXPR;
2335 : 7 : if (operand_equal_for_phi_arg_p (op0, larger))
2336 : : bound = op1;
2337 : 4 : else if (operand_equal_for_phi_arg_p (op1, larger))
2338 : : bound = op0;
2339 : : else
2340 : : return false;
2341 : :
2342 : : /* We need BOUND <= SMALLER. */
2343 : 3 : if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
2344 : : bound, arg_true)))
2345 : : return false;
2346 : : }
2347 : : else
2348 : 479 : return false;
2349 : : }
2350 : :
2351 : : /* Move the statement from the middle block. */
2352 : 18 : gsi = gsi_last_bb (cond_bb);
2353 : 18 : gsi_from = gsi_last_nondebug_bb (middle_bb);
2354 : 18 : reset_flow_sensitive_info (SINGLE_SSA_TREE_OPERAND (gsi_stmt (gsi_from),
2355 : : SSA_OP_DEF));
2356 : 18 : gsi_move_before (&gsi_from, &gsi);
2357 : : }
2358 : : else
2359 : : return false;
2360 : :
2361 : : /* Emit the statement to compute min/max. */
2362 : 3876 : gimple_seq stmts = NULL;
2363 : 3876 : tree phi_result = gimple_phi_result (phi);
2364 : :
2365 : : /* When we can't use a MIN/MAX_EXPR still make sure the expression
2366 : : stays in a form to be recognized by ISA that map to IEEE x > y ? x : y
2367 : : semantics (that's not IEEE max semantics). */
2368 : 3876 : if (HONOR_NANS (type) || HONOR_SIGNED_ZEROS (type))
2369 : : {
2370 : 3670 : result = gimple_build (&stmts, cmp, boolean_type_node,
2371 : : gimple_cond_lhs (cond), rhs);
2372 : 3670 : result = gimple_build (&stmts, COND_EXPR, TREE_TYPE (phi_result),
2373 : : result, arg_true, arg_false);
2374 : : }
2375 : : else
2376 : 206 : result = gimple_build (&stmts, minmax, TREE_TYPE (phi_result), arg0, arg1);
2377 : :
2378 : 3876 : gsi = gsi_last_bb (cond_bb);
2379 : 3876 : gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
2380 : :
2381 : 3876 : replace_phi_edge_with_variable (cond_bb, e1, phi, result);
2382 : :
2383 : 3876 : return true;
2384 : : }
2385 : :
2386 : : /* Attempt to optimize (x <=> y) cmp 0 and similar comparisons.
2387 : : For strong ordering <=> try to match something like:
2388 : : <bb 2> : // cond3_bb (== cond2_bb)
2389 : : if (x_4(D) != y_5(D))
2390 : : goto <bb 3>; [INV]
2391 : : else
2392 : : goto <bb 6>; [INV]
2393 : :
2394 : : <bb 3> : // cond_bb
2395 : : if (x_4(D) < y_5(D))
2396 : : goto <bb 6>; [INV]
2397 : : else
2398 : : goto <bb 4>; [INV]
2399 : :
2400 : : <bb 4> : // middle_bb
2401 : :
2402 : : <bb 6> : // phi_bb
2403 : : # iftmp.0_2 = PHI <1(4), 0(2), -1(3)>
2404 : : _1 = iftmp.0_2 == 0;
2405 : :
2406 : : and for partial ordering <=> something like:
2407 : :
2408 : : <bb 2> : // cond3_bb
2409 : : if (a_3(D) == b_5(D))
2410 : : goto <bb 6>; [50.00%]
2411 : : else
2412 : : goto <bb 3>; [50.00%]
2413 : :
2414 : : <bb 3> [local count: 536870913]: // cond2_bb
2415 : : if (a_3(D) < b_5(D))
2416 : : goto <bb 6>; [50.00%]
2417 : : else
2418 : : goto <bb 4>; [50.00%]
2419 : :
2420 : : <bb 4> [local count: 268435456]: // cond_bb
2421 : : if (a_3(D) > b_5(D))
2422 : : goto <bb 6>; [50.00%]
2423 : : else
2424 : : goto <bb 5>; [50.00%]
2425 : :
2426 : : <bb 5> [local count: 134217728]: // middle_bb
2427 : :
2428 : : <bb 6> [local count: 1073741824]: // phi_bb
2429 : : # SR.27_4 = PHI <0(2), -1(3), 1(4), -128(5)>
2430 : : _2 = SR.27_4 > 0; */
2431 : :
2432 : : static bool
2433 : 644246 : spaceship_replacement (basic_block cond_bb, basic_block middle_bb,
2434 : : edge e0, edge e1, gphi *phi,
2435 : : tree arg0, tree arg1)
2436 : : {
2437 : 644246 : tree phires = gimple_phi_result (phi);
2438 : 1280228 : if (!INTEGRAL_TYPE_P (TREE_TYPE (phires))
2439 : 503900 : || TYPE_UNSIGNED (TREE_TYPE (phires))
2440 : 259762 : || !tree_fits_shwi_p (arg0)
2441 : 72708 : || !tree_fits_shwi_p (arg1)
2442 : 41623 : || (!IN_RANGE (tree_to_shwi (arg0), -1, 1)
2443 : 22965 : && tree_to_shwi (arg0) != -128)
2444 : 663337 : || (!IN_RANGE (tree_to_shwi (arg1), -1, 1)
2445 : 2518 : && tree_to_shwi (arg1) != -128))
2446 : : return false;
2447 : :
2448 : 16621 : basic_block phi_bb = gimple_bb (phi);
2449 : 16621 : gcc_assert (phi_bb == e0->dest && phi_bb == e1->dest);
2450 : 16621 : if (!IN_RANGE (EDGE_COUNT (phi_bb->preds), 3, 4))
2451 : : return false;
2452 : :
2453 : 6804 : use_operand_p use_p;
2454 : 6804 : gimple *use_stmt;
2455 : 6804 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (phires))
2456 : : return false;
2457 : 6804 : if (!single_imm_use (phires, &use_p, &use_stmt))
2458 : : return false;
2459 : 6081 : enum tree_code cmp;
2460 : 6081 : tree lhs, rhs;
2461 : 6081 : gimple *orig_use_stmt = use_stmt;
2462 : 6081 : tree orig_use_lhs = NULL_TREE;
2463 : 6081 : tree temps[2] = { NULL_TREE, NULL_TREE };
2464 : :
2465 : : /* Handle std::partial_ordering::_M_reverse(), i.e.
2466 : : _1 = (unsigned char) phires;
2467 : : _2 = -_1;
2468 : : _3 = (signed char) _2;
2469 : : and uses of _3 in comparison instead of phires. */
2470 : 6081 : if (gimple_assign_cast_p (use_stmt))
2471 : : {
2472 : 253 : orig_use_lhs = gimple_assign_lhs (use_stmt);
2473 : 253 : temps[0] = orig_use_lhs;
2474 : 253 : tree ty1 = TREE_TYPE (gimple_assign_rhs1 (use_stmt));
2475 : 253 : tree ty2 = TREE_TYPE (orig_use_lhs);
2476 : :
2477 : 253 : if (!TYPE_UNSIGNED (ty2) || !INTEGRAL_TYPE_P (ty2))
2478 : : return false;
2479 : 213 : if (TYPE_PRECISION (ty2) != 8 || TYPE_PRECISION (ty1) < 8)
2480 : : return false;
2481 : 94 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig_use_lhs))
2482 : : return false;
2483 : 94 : if (!single_imm_use (orig_use_lhs, &use_p, &use_stmt))
2484 : : return false;
2485 : :
2486 : 93 : if (!is_gimple_assign (use_stmt)
2487 : 93 : || gimple_assign_rhs_code (use_stmt) != NEGATE_EXPR)
2488 : : return false;
2489 : :
2490 : 90 : orig_use_lhs = gimple_assign_lhs (use_stmt);
2491 : 90 : temps[1] = orig_use_lhs;
2492 : 90 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig_use_lhs))
2493 : : return false;
2494 : 90 : if (!single_imm_use (orig_use_lhs, &use_p, &use_stmt))
2495 : : return false;
2496 : :
2497 : 90 : if (!gimple_assign_cast_p (use_stmt))
2498 : : return false;
2499 : :
2500 : 90 : orig_use_lhs = gimple_assign_lhs (use_stmt);
2501 : 90 : tree ty3 = TREE_TYPE (orig_use_lhs);
2502 : :
2503 : 90 : if (!useless_type_conversion_p (ty3, ty1))
2504 : : return false;
2505 : 90 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig_use_lhs))
2506 : : return false;
2507 : 90 : if (!single_imm_use (orig_use_lhs, &use_p, &use_stmt))
2508 : : return false;
2509 : : }
2510 : 5918 : if (gimple_code (use_stmt) == GIMPLE_COND)
2511 : : {
2512 : 2092 : cmp = gimple_cond_code (use_stmt);
2513 : 2092 : lhs = gimple_cond_lhs (use_stmt);
2514 : 2092 : rhs = gimple_cond_rhs (use_stmt);
2515 : : }
2516 : 3826 : else if (is_gimple_assign (use_stmt))
2517 : : {
2518 : 2071 : if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
2519 : : {
2520 : 1265 : cmp = gimple_assign_rhs_code (use_stmt);
2521 : 1265 : lhs = gimple_assign_rhs1 (use_stmt);
2522 : 1265 : rhs = gimple_assign_rhs2 (use_stmt);
2523 : : }
2524 : 806 : else if (gimple_assign_rhs_code (use_stmt) == COND_EXPR)
2525 : : {
2526 : 0 : tree cond = gimple_assign_rhs1 (use_stmt);
2527 : 0 : if (!COMPARISON_CLASS_P (cond))
2528 : : return false;
2529 : 0 : cmp = TREE_CODE (cond);
2530 : 0 : lhs = TREE_OPERAND (cond, 0);
2531 : 0 : rhs = TREE_OPERAND (cond, 1);
2532 : : }
2533 : : else
2534 : : return false;
2535 : : }
2536 : : else
2537 : : return false;
2538 : 3357 : switch (cmp)
2539 : : {
2540 : 2894 : case EQ_EXPR:
2541 : 2894 : case NE_EXPR:
2542 : 2894 : case LT_EXPR:
2543 : 2894 : case GT_EXPR:
2544 : 2894 : case LE_EXPR:
2545 : 2894 : case GE_EXPR:
2546 : 2894 : break;
2547 : : default:
2548 : : return false;
2549 : : }
2550 : 5698 : if (lhs != (orig_use_lhs ? orig_use_lhs : phires)
2551 : 2637 : || !tree_fits_shwi_p (rhs)
2552 : 2376 : || !IN_RANGE (tree_to_shwi (rhs), -1, 1))
2553 : : return false;
2554 : :
2555 : 2350 : if (!empty_block_p (middle_bb))
2556 : : return false;
2557 : :
2558 : 4700 : gcond *cond1 = as_a <gcond *> (*gsi_last_bb (cond_bb));
2559 : 2350 : enum tree_code cmp1 = gimple_cond_code (cond1);
2560 : 2350 : switch (cmp1)
2561 : : {
2562 : 2183 : case LT_EXPR:
2563 : 2183 : case LE_EXPR:
2564 : 2183 : case GT_EXPR:
2565 : 2183 : case GE_EXPR:
2566 : 2183 : break;
2567 : : default:
2568 : : return false;
2569 : : }
2570 : 2183 : tree lhs1 = gimple_cond_lhs (cond1);
2571 : 2183 : tree rhs1 = gimple_cond_rhs (cond1);
2572 : 2183 : if (TREE_CODE (lhs1) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs1))
2573 : : return false;
2574 : 2183 : if (TREE_CODE (rhs1) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))
2575 : : return false;
2576 : :
2577 : 2183 : if (!single_pred_p (cond_bb) || !cond_only_block_p (cond_bb))
2578 : 14 : return false;
2579 : :
2580 : 2169 : basic_block cond2_bb = single_pred (cond_bb);
2581 : 2169 : if (EDGE_COUNT (cond2_bb->succs) != 2)
2582 : : return false;
2583 : 2169 : edge cond2_phi_edge;
2584 : 2169 : if (EDGE_SUCC (cond2_bb, 0)->dest == cond_bb)
2585 : : {
2586 : 1769 : if (EDGE_SUCC (cond2_bb, 1)->dest != phi_bb)
2587 : : return false;
2588 : : cond2_phi_edge = EDGE_SUCC (cond2_bb, 1);
2589 : : }
2590 : 400 : else if (EDGE_SUCC (cond2_bb, 0)->dest != phi_bb)
2591 : : return false;
2592 : : else
2593 : : cond2_phi_edge = EDGE_SUCC (cond2_bb, 0);
2594 : 2169 : tree arg2 = gimple_phi_arg_def (phi, cond2_phi_edge->dest_idx);
2595 : 2169 : if (!tree_fits_shwi_p (arg2))
2596 : : return false;
2597 : 4338 : gcond *cond2 = safe_dyn_cast <gcond *> (*gsi_last_bb (cond2_bb));
2598 : 2169 : if (!cond2)
2599 : : return false;
2600 : 2169 : enum tree_code cmp2 = gimple_cond_code (cond2);
2601 : 2169 : tree lhs2 = gimple_cond_lhs (cond2);
2602 : 2169 : tree rhs2 = gimple_cond_rhs (cond2);
2603 : 2169 : if (lhs2 == lhs1)
2604 : : {
2605 : 2169 : if (!operand_equal_p (rhs2, rhs1, 0))
2606 : : {
2607 : 201 : if ((cmp2 == EQ_EXPR || cmp2 == NE_EXPR)
2608 : 201 : && TREE_CODE (rhs1) == INTEGER_CST
2609 : 201 : && TREE_CODE (rhs2) == INTEGER_CST)
2610 : : {
2611 : : /* For integers, we can have cond2 x == 5
2612 : : and cond1 x < 5, x <= 4, x <= 5, x < 6,
2613 : : x > 5, x >= 6, x >= 5 or x > 4. */
2614 : 201 : if (tree_int_cst_lt (rhs1, rhs2))
2615 : : {
2616 : 201 : if (wi::ne_p (wi::to_wide (rhs1) + 1, wi::to_wide (rhs2)))
2617 : : return false;
2618 : 201 : if (cmp1 == LE_EXPR)
2619 : : cmp1 = LT_EXPR;
2620 : 0 : else if (cmp1 == GT_EXPR)
2621 : : cmp1 = GE_EXPR;
2622 : : else
2623 : : return false;
2624 : : }
2625 : : else
2626 : : {
2627 : 0 : gcc_checking_assert (tree_int_cst_lt (rhs2, rhs1));
2628 : 0 : if (wi::ne_p (wi::to_wide (rhs2) + 1, wi::to_wide (rhs1)))
2629 : : return false;
2630 : 0 : if (cmp1 == LT_EXPR)
2631 : : cmp1 = LE_EXPR;
2632 : 0 : else if (cmp1 == GE_EXPR)
2633 : : cmp1 = GT_EXPR;
2634 : : else
2635 : : return false;
2636 : : }
2637 : : rhs1 = rhs2;
2638 : : }
2639 : : else
2640 : : return false;
2641 : : }
2642 : : }
2643 : 0 : else if (lhs2 == rhs1)
2644 : : {
2645 : 0 : if (rhs2 != lhs1)
2646 : : return false;
2647 : : }
2648 : : else
2649 : : return false;
2650 : :
2651 : 2169 : tree arg3 = arg2;
2652 : 2169 : basic_block cond3_bb = cond2_bb;
2653 : 2169 : edge cond3_phi_edge = cond2_phi_edge;
2654 : 2169 : gcond *cond3 = cond2;
2655 : 2169 : enum tree_code cmp3 = cmp2;
2656 : 2169 : tree lhs3 = lhs2;
2657 : 2169 : tree rhs3 = rhs2;
2658 : 2169 : if (EDGE_COUNT (phi_bb->preds) == 4)
2659 : : {
2660 : 396 : if (absu_hwi (tree_to_shwi (arg2)) != 1)
2661 : : return false;
2662 : 396 : if ((cond2_phi_edge->flags & EDGE_FALSE_VALUE)
2663 : 396 : && HONOR_NANS (TREE_TYPE (lhs1)))
2664 : : return false;
2665 : 396 : if (e1->flags & EDGE_TRUE_VALUE)
2666 : : {
2667 : 396 : if (tree_to_shwi (arg0) != -128
2668 : 396 : || absu_hwi (tree_to_shwi (arg1)) != 1
2669 : 792 : || wi::to_widest (arg1) == wi::to_widest (arg2))
2670 : 0 : return false;
2671 : : }
2672 : 0 : else if (tree_to_shwi (arg1) != -128
2673 : 0 : || absu_hwi (tree_to_shwi (arg0)) != 1
2674 : 0 : || wi::to_widest (arg0) == wi::to_widest (arg2))
2675 : 0 : return false;
2676 : 396 : switch (cmp2)
2677 : : {
2678 : 396 : case LT_EXPR:
2679 : 396 : case LE_EXPR:
2680 : 396 : case GT_EXPR:
2681 : 396 : case GE_EXPR:
2682 : 396 : break;
2683 : : default:
2684 : : return false;
2685 : : }
2686 : : /* if (x < y) goto phi_bb; else fallthru;
2687 : : if (x > y) goto phi_bb; else fallthru;
2688 : : bbx:;
2689 : : phi_bb:;
2690 : : is ok, but if x and y are swapped in one of the comparisons,
2691 : : or the comparisons are the same and operands not swapped,
2692 : : or the true and false edges are swapped, it is not.
2693 : : For HONOR_NANS, the edge flags are irrelevant and the comparisons
2694 : : must be different for non-swapped operands and same for swapped
2695 : : operands. */
2696 : 396 : if ((lhs2 == lhs1)
2697 : 396 : ^ (HONOR_NANS (TREE_TYPE (lhs1))
2698 : 396 : ? ((cmp2 == LT_EXPR || cmp2 == LE_EXPR)
2699 : 268 : != (cmp1 == LT_EXPR || cmp1 == LE_EXPR))
2700 : 128 : : (((cond2_phi_edge->flags
2701 : 128 : & ((cmp2 == LT_EXPR || cmp2 == LE_EXPR)
2702 : 128 : ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE)) != 0)
2703 : 256 : != ((e1->flags
2704 : 128 : & ((cmp1 == LT_EXPR || cmp1 == LE_EXPR)
2705 : 128 : ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE)) != 0))))
2706 : : return false;
2707 : 396 : if (!single_pred_p (cond2_bb) || !cond_only_block_p (cond2_bb))
2708 : 0 : return false;
2709 : 396 : cond3_bb = single_pred (cond2_bb);
2710 : 396 : if (EDGE_COUNT (cond2_bb->succs) != 2)
2711 : : return false;
2712 : 396 : if (EDGE_SUCC (cond3_bb, 0)->dest == cond2_bb)
2713 : : {
2714 : 220 : if (EDGE_SUCC (cond3_bb, 1)->dest != phi_bb)
2715 : : return false;
2716 : : cond3_phi_edge = EDGE_SUCC (cond3_bb, 1);
2717 : : }
2718 : 176 : else if (EDGE_SUCC (cond3_bb, 0)->dest != phi_bb)
2719 : : return false;
2720 : : else
2721 : : cond3_phi_edge = EDGE_SUCC (cond3_bb, 0);
2722 : 396 : arg3 = gimple_phi_arg_def (phi, cond3_phi_edge->dest_idx);
2723 : 642961 : cond3 = safe_dyn_cast <gcond *> (*gsi_last_bb (cond3_bb));
2724 : 396 : if (!cond3)
2725 : : return false;
2726 : 396 : cmp3 = gimple_cond_code (cond3);
2727 : 396 : lhs3 = gimple_cond_lhs (cond3);
2728 : 396 : rhs3 = gimple_cond_rhs (cond3);
2729 : 396 : if (lhs3 == lhs1)
2730 : : {
2731 : 396 : if (!operand_equal_p (rhs3, rhs1, 0))
2732 : : return false;
2733 : : }
2734 : 0 : else if (lhs3 == rhs1)
2735 : : {
2736 : 0 : if (rhs3 != lhs1)
2737 : : return false;
2738 : : }
2739 : : else
2740 : : return false;
2741 : : }
2742 : 1773 : else if (absu_hwi (tree_to_shwi (arg0)) != 1
2743 : 1753 : || absu_hwi (tree_to_shwi (arg1)) != 1
2744 : 3506 : || wi::to_widest (arg0) == wi::to_widest (arg1)
2745 : 3526 : || HONOR_NANS (TREE_TYPE (lhs1)))
2746 : 20 : return false;
2747 : :
2748 : 2149 : if (!integer_zerop (arg3) || (cmp3 != EQ_EXPR && cmp3 != NE_EXPR))
2749 : : return false;
2750 : 2149 : if ((cond3_phi_edge->flags & (cmp3 == EQ_EXPR
2751 : 2149 : ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE)) == 0)
2752 : : return false;
2753 : :
2754 : : /* lhs1 one_cmp rhs1 results in phires of 1. */
2755 : 2149 : enum tree_code one_cmp;
2756 : 4298 : if ((cmp1 == LT_EXPR || cmp1 == LE_EXPR)
2757 : 2221 : ^ (!integer_onep ((e1->flags & EDGE_TRUE_VALUE) ? arg1 : arg0)))
2758 : : one_cmp = LT_EXPR;
2759 : : else
2760 : 1782 : one_cmp = GT_EXPR;
2761 : :
2762 : 2149 : enum tree_code res_cmp;
2763 : 2149 : bool negate_p = false;
2764 : 2149 : switch (cmp)
2765 : : {
2766 : 1270 : case EQ_EXPR:
2767 : 1270 : if (integer_zerop (rhs) && !HONOR_NANS (TREE_TYPE (lhs1)))
2768 : : res_cmp = EQ_EXPR;
2769 : 1186 : else if (integer_minus_onep (rhs))
2770 : 606 : res_cmp = one_cmp == LT_EXPR ? GT_EXPR : LT_EXPR;
2771 : 580 : else if (integer_onep (rhs))
2772 : : res_cmp = one_cmp;
2773 : : else
2774 : : return false;
2775 : : break;
2776 : 787 : case NE_EXPR:
2777 : 787 : if (integer_zerop (rhs) && !HONOR_NANS (TREE_TYPE (lhs1)))
2778 : : res_cmp = NE_EXPR;
2779 : 747 : else if (integer_minus_onep (rhs))
2780 : 529 : res_cmp = one_cmp == LT_EXPR ? LE_EXPR : GE_EXPR;
2781 : 312 : else if (integer_onep (rhs))
2782 : 402 : res_cmp = one_cmp == LT_EXPR ? GE_EXPR : LE_EXPR;
2783 : : else
2784 : : return false;
2785 : 757 : if (HONOR_NANS (TREE_TYPE (lhs1)))
2786 : : negate_p = true;
2787 : : break;
2788 : 25 : case LT_EXPR:
2789 : 25 : if (integer_onep (rhs))
2790 : 0 : res_cmp = one_cmp == LT_EXPR ? GE_EXPR : LE_EXPR;
2791 : 25 : else if (integer_zerop (rhs))
2792 : 25 : res_cmp = one_cmp == LT_EXPR ? GT_EXPR : LT_EXPR;
2793 : : else
2794 : : return false;
2795 : 25 : if (HONOR_NANS (TREE_TYPE (lhs1)))
2796 : : negate_p = true;
2797 : : break;
2798 : 2 : case LE_EXPR:
2799 : 2 : if (integer_zerop (rhs))
2800 : 2 : res_cmp = one_cmp == LT_EXPR ? GE_EXPR : LE_EXPR;
2801 : 0 : else if (integer_minus_onep (rhs))
2802 : 0 : res_cmp = one_cmp == LT_EXPR ? GT_EXPR : LT_EXPR;
2803 : : else
2804 : : return false;
2805 : 2 : if (HONOR_NANS (TREE_TYPE (lhs1)))
2806 : : negate_p = true;
2807 : : break;
2808 : 0 : case GT_EXPR:
2809 : 0 : if (integer_minus_onep (rhs))
2810 : 0 : res_cmp = one_cmp == LT_EXPR ? LE_EXPR : GE_EXPR;
2811 : 0 : else if (integer_zerop (rhs))
2812 : : res_cmp = one_cmp;
2813 : : else
2814 : : return false;
2815 : : break;
2816 : 65 : case GE_EXPR:
2817 : 65 : if (integer_zerop (rhs))
2818 : 65 : res_cmp = one_cmp == LT_EXPR ? LE_EXPR : GE_EXPR;
2819 : 0 : else if (integer_onep (rhs))
2820 : : res_cmp = one_cmp;
2821 : : else
2822 : : return false;
2823 : : break;
2824 : 0 : default:
2825 : 0 : gcc_unreachable ();
2826 : : }
2827 : 2077 : if (orig_use_lhs)
2828 : 88 : res_cmp = swap_tree_comparison (res_cmp);
2829 : :
2830 : 2077 : tree clhs1 = lhs1, crhs1 = rhs1;
2831 : 2077 : if (negate_p)
2832 : : {
2833 : 48 : if (cfun->can_throw_non_call_exceptions)
2834 : 0 : return false;
2835 : 48 : res_cmp = invert_tree_comparison (res_cmp, false);
2836 : 48 : clhs1 = make_ssa_name (boolean_type_node);
2837 : 48 : gimple *g = gimple_build_assign (clhs1, res_cmp, lhs1, rhs1);
2838 : 48 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2839 : 48 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2840 : 48 : crhs1 = boolean_false_node;
2841 : 48 : res_cmp = EQ_EXPR;
2842 : : }
2843 : :
2844 : 2077 : if (gimple_code (use_stmt) == GIMPLE_COND)
2845 : : {
2846 : 1400 : gcond *use_cond = as_a <gcond *> (use_stmt);
2847 : 1400 : gimple_cond_set_code (use_cond, res_cmp);
2848 : 1400 : gimple_cond_set_lhs (use_cond, clhs1);
2849 : 1400 : gimple_cond_set_rhs (use_cond, crhs1);
2850 : : }
2851 : 677 : else if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
2852 : : {
2853 : 677 : gimple_assign_set_rhs_code (use_stmt, res_cmp);
2854 : 677 : gimple_assign_set_rhs1 (use_stmt, clhs1);
2855 : 677 : gimple_assign_set_rhs2 (use_stmt, crhs1);
2856 : : }
2857 : : else
2858 : : {
2859 : 0 : tree cond = build2 (res_cmp, TREE_TYPE (gimple_assign_rhs1 (use_stmt)),
2860 : : clhs1, crhs1);
2861 : 0 : gimple_assign_set_rhs1 (use_stmt, cond);
2862 : : }
2863 : 2077 : update_stmt (use_stmt);
2864 : :
2865 : 2077 : if (MAY_HAVE_DEBUG_BIND_STMTS)
2866 : : {
2867 : 1747 : use_operand_p use_p;
2868 : 1747 : imm_use_iterator iter;
2869 : 1747 : bool has_debug_uses = false;
2870 : 1747 : bool has_cast1_debug_uses = false;
2871 : 1747 : bool has_neg_debug_uses = false;
2872 : 1747 : bool has_cast2_debug_uses = false;
2873 : 1791 : FOR_EACH_IMM_USE_FAST (use_p, iter, phires)
2874 : : {
2875 : 1791 : gimple *use_stmt = USE_STMT (use_p);
2876 : 1791 : if (is_gimple_debug (use_stmt))
2877 : : {
2878 : : has_debug_uses = true;
2879 : : break;
2880 : : }
2881 : : }
2882 : 1747 : if (orig_use_lhs)
2883 : : {
2884 : 88 : FOR_EACH_IMM_USE_FAST (use_p, iter, temps[0])
2885 : : {
2886 : 76 : gimple *use_stmt = USE_STMT (use_p);
2887 : 76 : if (is_gimple_debug (use_stmt))
2888 : : {
2889 : : has_debug_uses = true;
2890 : : has_cast1_debug_uses = true;
2891 : : break;
2892 : : }
2893 : : }
2894 : 88 : FOR_EACH_IMM_USE_FAST (use_p, iter, temps[1])
2895 : : {
2896 : 76 : gimple *use_stmt = USE_STMT (use_p);
2897 : 76 : if (is_gimple_debug (use_stmt))
2898 : : {
2899 : : has_debug_uses = true;
2900 : : has_cast1_debug_uses = true;
2901 : : has_neg_debug_uses = true;
2902 : : break;
2903 : : }
2904 : : }
2905 : 44 : FOR_EACH_IMM_USE_FAST (use_p, iter, orig_use_lhs)
2906 : : {
2907 : 32 : gimple *use_stmt = USE_STMT (use_p);
2908 : 32 : if (is_gimple_debug (use_stmt))
2909 : : {
2910 : : has_debug_uses = true;
2911 : : has_cast1_debug_uses = true;
2912 : : has_neg_debug_uses = true;
2913 : : has_cast2_debug_uses = true;
2914 : : break;
2915 : : }
2916 : : }
2917 : 44 : if (has_debug_uses)
2918 : : {
2919 : 44 : gimple_stmt_iterator gsi = gsi_for_stmt (orig_use_stmt);
2920 : 44 : tree zero = build_zero_cst (TREE_TYPE (temps[0]));
2921 : 44 : gimple_assign_set_rhs_with_ops (&gsi, INTEGER_CST, zero);
2922 : 44 : update_stmt (orig_use_stmt);
2923 : 44 : gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (temps[1]));
2924 : 44 : zero = build_zero_cst (TREE_TYPE (temps[1]));
2925 : 44 : gimple_assign_set_rhs_with_ops (&gsi, INTEGER_CST, zero);
2926 : 44 : update_stmt (SSA_NAME_DEF_STMT (temps[1]));
2927 : 44 : gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (orig_use_lhs));
2928 : 44 : zero = build_zero_cst (TREE_TYPE (orig_use_lhs));
2929 : 44 : gimple_assign_set_rhs_with_ops (&gsi, INTEGER_CST, zero);
2930 : 44 : update_stmt (SSA_NAME_DEF_STMT (orig_use_lhs));
2931 : : }
2932 : : }
2933 : :
2934 : 1747 : if (has_debug_uses)
2935 : : {
2936 : : /* If there are debug uses, emit something like:
2937 : : # DEBUG D#1 => i_2(D) > j_3(D) ? 1 : -1
2938 : : # DEBUG D#2 => i_2(D) == j_3(D) ? 0 : D#1
2939 : : where > stands for the comparison that yielded 1
2940 : : and replace debug uses of phi result with that D#2.
2941 : : Ignore the value of -128 if !HONOR_NANS, because if NaNs
2942 : : aren't expected, all floating point numbers should be
2943 : : comparable. If HONOR_NANS, emit something like:
2944 : : # DEBUG D#1 => i_2(D) < j_3(D) ? -1 : -128
2945 : : # DEBUG D#2 => i_2(D) > j_3(D) ? 1 : D#1
2946 : : # DEBUG D#3 => i_2(D) == j_3(D) ? 0 : D#2
2947 : : instead. */
2948 : 1747 : gimple_stmt_iterator gsi = gsi_after_labels (gimple_bb (phi));
2949 : 1747 : tree type = TREE_TYPE (phires);
2950 : 1747 : tree minus_one = build_int_cst (type, -1);
2951 : 1747 : if (HONOR_NANS (TREE_TYPE (lhs1)))
2952 : : {
2953 : 100 : tree temp3 = build_debug_expr_decl (type);
2954 : 200 : tree t = build2 (one_cmp == LT_EXPR ? GT_EXPR : LT_EXPR,
2955 : : boolean_type_node, lhs1, rhs2);
2956 : 100 : t = build3 (COND_EXPR, type, t, minus_one,
2957 : : build_int_cst (type, -128));
2958 : 100 : gimple *g = gimple_build_debug_bind (temp3, t, phi);
2959 : 100 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2960 : 100 : minus_one = temp3;
2961 : : }
2962 : 1747 : tree temp1 = build_debug_expr_decl (type);
2963 : 1747 : tree t = build2 (one_cmp, boolean_type_node, lhs1, rhs2);
2964 : 1747 : t = build3 (COND_EXPR, type, t, build_one_cst (type),
2965 : : minus_one);
2966 : 1747 : gimple *g = gimple_build_debug_bind (temp1, t, phi);
2967 : 1747 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2968 : 1747 : tree temp2 = build_debug_expr_decl (type);
2969 : 1747 : t = build2 (EQ_EXPR, boolean_type_node, lhs1, rhs2);
2970 : 1747 : t = build3 (COND_EXPR, type, t, build_zero_cst (type), temp1);
2971 : 1747 : g = gimple_build_debug_bind (temp2, t, phi);
2972 : 1747 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2973 : 1747 : replace_uses_by (phires, temp2);
2974 : 1747 : if (has_cast1_debug_uses)
2975 : : {
2976 : 32 : tree temp3 = build_debug_expr_decl (TREE_TYPE (temps[0]));
2977 : 32 : t = fold_convert (TREE_TYPE (temps[0]), temp2);
2978 : 32 : g = gimple_build_debug_bind (temp3, t, phi);
2979 : 32 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2980 : 32 : replace_uses_by (temps[0], temp3);
2981 : 32 : temp2 = temp3;
2982 : : }
2983 : 1747 : if (has_neg_debug_uses)
2984 : : {
2985 : 32 : tree temp3 = build_debug_expr_decl (TREE_TYPE (temps[1]));
2986 : 32 : t = fold_build1 (NEGATE_EXPR, TREE_TYPE (temps[1]), temp2);
2987 : 32 : g = gimple_build_debug_bind (temp3, t, phi);
2988 : 32 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2989 : 32 : replace_uses_by (temps[1], temp3);
2990 : 32 : temp2 = temp3;
2991 : : }
2992 : 1747 : if (has_cast2_debug_uses)
2993 : : {
2994 : 32 : tree temp3 = build_debug_expr_decl (TREE_TYPE (orig_use_lhs));
2995 : 32 : t = fold_convert (TREE_TYPE (orig_use_lhs), temp2);
2996 : 32 : g = gimple_build_debug_bind (temp3, t, phi);
2997 : 32 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2998 : 32 : replace_uses_by (orig_use_lhs, temp3);
2999 : : }
3000 : : }
3001 : : }
3002 : :
3003 : 2077 : if (orig_use_lhs)
3004 : : {
3005 : 88 : gimple_stmt_iterator gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (orig_use_lhs));
3006 : 88 : gsi_remove (&gsi, true);
3007 : 88 : gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (temps[1]));
3008 : 88 : gsi_remove (&gsi, true);
3009 : 88 : gsi = gsi_for_stmt (orig_use_stmt);
3010 : 88 : gsi_remove (&gsi, true);
3011 : 88 : release_ssa_name (orig_use_lhs);
3012 : 88 : release_ssa_name (temps[1]);
3013 : 88 : release_ssa_name (temps[0]);
3014 : : }
3015 : :
3016 : 2077 : gimple_stmt_iterator psi = gsi_for_stmt (phi);
3017 : 2077 : remove_phi_node (&psi, true);
3018 : 2077 : statistics_counter_event (cfun, "spaceship replacement", 1);
3019 : :
3020 : 2077 : return true;
3021 : : }
3022 : :
3023 : : /* Optimize x ? __builtin_fun (x) : C, where C is __builtin_fun (0).
3024 : : Convert
3025 : :
3026 : : <bb 2>
3027 : : if (b_4(D) != 0)
3028 : : goto <bb 3>
3029 : : else
3030 : : goto <bb 4>
3031 : :
3032 : : <bb 3>
3033 : : _2 = (unsigned long) b_4(D);
3034 : : _9 = __builtin_popcountl (_2);
3035 : : OR
3036 : : _9 = __builtin_popcountl (b_4(D));
3037 : :
3038 : : <bb 4>
3039 : : c_12 = PHI <0(2), _9(3)>
3040 : :
3041 : : Into
3042 : : <bb 2>
3043 : : _2 = (unsigned long) b_4(D);
3044 : : _9 = __builtin_popcountl (_2);
3045 : : OR
3046 : : _9 = __builtin_popcountl (b_4(D));
3047 : :
3048 : : <bb 4>
3049 : : c_12 = PHI <_9(2)>
3050 : :
3051 : : Similarly for __builtin_clz or __builtin_ctz if
3052 : : C?Z_DEFINED_VALUE_AT_ZERO is 2, optab is present and
3053 : : instead of 0 above it uses the value from that macro. */
3054 : :
3055 : : static bool
3056 : 449640 : cond_removal_in_builtin_zero_pattern (basic_block cond_bb,
3057 : : basic_block middle_bb,
3058 : : edge e1, edge e2, gphi *phi,
3059 : : tree arg0, tree arg1)
3060 : : {
3061 : 449640 : gimple_stmt_iterator gsi, gsi_from;
3062 : 449640 : gimple *call;
3063 : 449640 : gimple *cast = NULL;
3064 : 449640 : tree lhs, arg;
3065 : :
3066 : : /* Check that
3067 : : _2 = (unsigned long) b_4(D);
3068 : : _9 = __builtin_popcountl (_2);
3069 : : OR
3070 : : _9 = __builtin_popcountl (b_4(D));
3071 : : are the only stmts in the middle_bb. */
3072 : :
3073 : 449640 : gsi = gsi_start_nondebug_after_labels_bb (middle_bb);
3074 : 449640 : if (gsi_end_p (gsi))
3075 : : return false;
3076 : 272772 : cast = gsi_stmt (gsi);
3077 : 272772 : gsi_next_nondebug (&gsi);
3078 : 272772 : if (!gsi_end_p (gsi))
3079 : : {
3080 : 139367 : call = gsi_stmt (gsi);
3081 : 139367 : gsi_next_nondebug (&gsi);
3082 : 139367 : if (!gsi_end_p (gsi))
3083 : : return false;
3084 : : }
3085 : : else
3086 : : {
3087 : : call = cast;
3088 : : cast = NULL;
3089 : : }
3090 : :
3091 : : /* Check that we have a popcount/clz/ctz builtin. */
3092 : 183215 : if (!is_gimple_call (call))
3093 : : return false;
3094 : :
3095 : 6021 : lhs = gimple_get_lhs (call);
3096 : :
3097 : 6021 : if (lhs == NULL_TREE)
3098 : : return false;
3099 : :
3100 : 5995 : combined_fn cfn = gimple_call_combined_fn (call);
3101 : 5995 : if (gimple_call_num_args (call) != 1
3102 : 5995 : && (gimple_call_num_args (call) != 2
3103 : : || cfn == CFN_CLZ
3104 : 578 : || cfn == CFN_CTZ))
3105 : : return false;
3106 : :
3107 : 4443 : arg = gimple_call_arg (call, 0);
3108 : :
3109 : 4443 : internal_fn ifn = IFN_LAST;
3110 : 4443 : int val = 0;
3111 : 4443 : bool any_val = false;
3112 : 4443 : switch (cfn)
3113 : : {
3114 : : case CFN_BUILT_IN_BSWAP16:
3115 : : case CFN_BUILT_IN_BSWAP32:
3116 : : case CFN_BUILT_IN_BSWAP64:
3117 : : case CFN_BUILT_IN_BSWAP128:
3118 : : CASE_CFN_FFS:
3119 : : CASE_CFN_PARITY:
3120 : : CASE_CFN_POPCOUNT:
3121 : : break;
3122 : 864 : CASE_CFN_CLZ:
3123 : 864 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg)))
3124 : : {
3125 : 864 : tree type = TREE_TYPE (arg);
3126 : 864 : if (TREE_CODE (type) == BITINT_TYPE)
3127 : : {
3128 : 4 : if (gimple_call_num_args (call) == 1)
3129 : : {
3130 : : any_val = true;
3131 : : ifn = IFN_CLZ;
3132 : : break;
3133 : : }
3134 : 0 : if (!tree_fits_shwi_p (gimple_call_arg (call, 1)))
3135 : : return false;
3136 : 0 : HOST_WIDE_INT at_zero = tree_to_shwi (gimple_call_arg (call, 1));
3137 : 0 : if ((int) at_zero != at_zero)
3138 : : return false;
3139 : 0 : ifn = IFN_CLZ;
3140 : 0 : val = at_zero;
3141 : 0 : break;
3142 : : }
3143 : 860 : if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
3144 : 1695 : && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
3145 : : val) == 2)
3146 : : {
3147 : : ifn = IFN_CLZ;
3148 : : break;
3149 : : }
3150 : : }
3151 : : return false;
3152 : 245 : CASE_CFN_CTZ:
3153 : 245 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg)))
3154 : : {
3155 : 245 : tree type = TREE_TYPE (arg);
3156 : 245 : if (TREE_CODE (type) == BITINT_TYPE)
3157 : : {
3158 : 4 : if (gimple_call_num_args (call) == 1)
3159 : : {
3160 : : any_val = true;
3161 : : ifn = IFN_CTZ;
3162 : : break;
3163 : : }
3164 : 0 : if (!tree_fits_shwi_p (gimple_call_arg (call, 1)))
3165 : : return false;
3166 : 0 : HOST_WIDE_INT at_zero = tree_to_shwi (gimple_call_arg (call, 1));
3167 : 0 : if ((int) at_zero != at_zero)
3168 : : return false;
3169 : 0 : ifn = IFN_CTZ;
3170 : 0 : val = at_zero;
3171 : 0 : break;
3172 : : }
3173 : 241 : if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
3174 : 461 : && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
3175 : : val) == 2)
3176 : : {
3177 : : ifn = IFN_CTZ;
3178 : : break;
3179 : : }
3180 : : }
3181 : : return false;
3182 : 3 : case CFN_BUILT_IN_CLRSB:
3183 : 3 : val = TYPE_PRECISION (integer_type_node) - 1;
3184 : 3 : break;
3185 : 3 : case CFN_BUILT_IN_CLRSBL:
3186 : 3 : val = TYPE_PRECISION (long_integer_type_node) - 1;
3187 : 3 : break;
3188 : 3 : case CFN_BUILT_IN_CLRSBLL:
3189 : 3 : val = TYPE_PRECISION (long_long_integer_type_node) - 1;
3190 : 3 : break;
3191 : : default:
3192 : : return false;
3193 : : }
3194 : :
3195 : 134 : if (cast)
3196 : : {
3197 : : /* We have a cast stmt feeding popcount/clz/ctz builtin. */
3198 : : /* Check that we have a cast prior to that. */
3199 : 54 : if (gimple_code (cast) != GIMPLE_ASSIGN
3200 : 54 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (cast)))
3201 : : return false;
3202 : : /* Result of the cast stmt is the argument to the builtin. */
3203 : 24 : if (arg != gimple_assign_lhs (cast))
3204 : : return false;
3205 : 24 : arg = gimple_assign_rhs1 (cast);
3206 : : }
3207 : :
3208 : 208 : gcond *cond = dyn_cast <gcond *> (*gsi_last_bb (cond_bb));
3209 : :
3210 : : /* Cond_bb has a check for b_4 [!=|==] 0 before calling the popcount/clz/ctz
3211 : : builtin. */
3212 : 104 : if (!cond
3213 : 104 : || (gimple_cond_code (cond) != NE_EXPR
3214 : 12 : && gimple_cond_code (cond) != EQ_EXPR)
3215 : 104 : || !integer_zerop (gimple_cond_rhs (cond))
3216 : 104 : || arg != gimple_cond_lhs (cond))
3217 : 72 : return false;
3218 : :
3219 : : /* Canonicalize. */
3220 : 32 : if ((e2->flags & EDGE_TRUE_VALUE
3221 : 0 : && gimple_cond_code (cond) == NE_EXPR)
3222 : 32 : || (e1->flags & EDGE_TRUE_VALUE
3223 : 0 : && gimple_cond_code (cond) == EQ_EXPR))
3224 : : {
3225 : : std::swap (arg0, arg1);
3226 : : std::swap (e1, e2);
3227 : : }
3228 : :
3229 : : /* Check PHI arguments. */
3230 : 32 : if (lhs != arg0
3231 : 32 : || TREE_CODE (arg1) != INTEGER_CST)
3232 : : return false;
3233 : 24 : if (any_val)
3234 : : {
3235 : 0 : if (!tree_fits_shwi_p (arg1))
3236 : : return false;
3237 : 0 : HOST_WIDE_INT at_zero = tree_to_shwi (arg1);
3238 : 0 : if ((int) at_zero != at_zero)
3239 : : return false;
3240 : 0 : val = at_zero;
3241 : : }
3242 : 24 : else if (wi::to_wide (arg1) != val)
3243 : : return false;
3244 : :
3245 : : /* And insert the popcount/clz/ctz builtin and cast stmt before the
3246 : : cond_bb. */
3247 : 13 : gsi = gsi_last_bb (cond_bb);
3248 : 13 : if (cast)
3249 : : {
3250 : 13 : gsi_from = gsi_for_stmt (cast);
3251 : 13 : gsi_move_before (&gsi_from, &gsi);
3252 : 13 : reset_flow_sensitive_info (gimple_get_lhs (cast));
3253 : : }
3254 : 13 : gsi_from = gsi_for_stmt (call);
3255 : 13 : if (ifn == IFN_LAST
3256 : 13 : || (gimple_call_internal_p (call) && gimple_call_num_args (call) == 2))
3257 : 8 : gsi_move_before (&gsi_from, &gsi);
3258 : : else
3259 : : {
3260 : : /* For __builtin_c[lt]z* force .C[LT]Z ifn, because only
3261 : : the latter is well defined at zero. */
3262 : 5 : call = gimple_build_call_internal (ifn, 2, gimple_call_arg (call, 0),
3263 : 5 : build_int_cst (integer_type_node, val));
3264 : 5 : gimple_call_set_lhs (call, lhs);
3265 : 5 : gsi_insert_before (&gsi, call, GSI_SAME_STMT);
3266 : 5 : gsi_remove (&gsi_from, true);
3267 : : }
3268 : 13 : reset_flow_sensitive_info (lhs);
3269 : :
3270 : : /* Now update the PHI and remove unneeded bbs. */
3271 : 13 : replace_phi_edge_with_variable (cond_bb, e2, phi, lhs);
3272 : 13 : return true;
3273 : : }
3274 : :
3275 : : /* Auxiliary functions to determine the set of memory accesses which
3276 : : can't trap because they are preceded by accesses to the same memory
3277 : : portion. We do that for MEM_REFs, so we only need to track
3278 : : the SSA_NAME of the pointer indirectly referenced. The algorithm
3279 : : simply is a walk over all instructions in dominator order. When
3280 : : we see an MEM_REF we determine if we've already seen a same
3281 : : ref anywhere up to the root of the dominator tree. If we do the
3282 : : current access can't trap. If we don't see any dominating access
3283 : : the current access might trap, but might also make later accesses
3284 : : non-trapping, so we remember it. We need to be careful with loads
3285 : : or stores, for instance a load might not trap, while a store would,
3286 : : so if we see a dominating read access this doesn't mean that a later
3287 : : write access would not trap. Hence we also need to differentiate the
3288 : : type of access(es) seen.
3289 : :
3290 : : ??? We currently are very conservative and assume that a load might
3291 : : trap even if a store doesn't (write-only memory). This probably is
3292 : : overly conservative.
3293 : :
3294 : : We currently support a special case that for !TREE_ADDRESSABLE automatic
3295 : : variables, it could ignore whether something is a load or store because the
3296 : : local stack should be always writable. */
3297 : :
3298 : : /* A hash-table of references (MEM_REF/ARRAY_REF/COMPONENT_REF), and in which
3299 : : basic block an *_REF through it was seen, which would constitute a
3300 : : no-trap region for same accesses.
3301 : :
3302 : : Size is needed to support 2 MEM_REFs of different types, like
3303 : : MEM<double>(s_1) and MEM<long>(s_1), which would compare equal with
3304 : : OEP_ADDRESS_OF. */
3305 : : struct ref_to_bb
3306 : : {
3307 : : tree exp;
3308 : : HOST_WIDE_INT size;
3309 : : unsigned int phase;
3310 : : basic_block bb;
3311 : : };
3312 : :
3313 : : /* Hashtable helpers. */
3314 : :
3315 : : struct refs_hasher : free_ptr_hash<ref_to_bb>
3316 : : {
3317 : : static inline hashval_t hash (const ref_to_bb *);
3318 : : static inline bool equal (const ref_to_bb *, const ref_to_bb *);
3319 : : };
3320 : :
3321 : : /* Used for quick clearing of the hash-table when we see calls.
3322 : : Hash entries with phase < nt_call_phase are invalid. */
3323 : : static unsigned int nt_call_phase;
3324 : :
3325 : : /* The hash function. */
3326 : :
3327 : : inline hashval_t
3328 : 19872381 : refs_hasher::hash (const ref_to_bb *n)
3329 : : {
3330 : 19872381 : inchash::hash hstate;
3331 : 19872381 : inchash::add_expr (n->exp, hstate, OEP_ADDRESS_OF);
3332 : 19872381 : hstate.add_hwi (n->size);
3333 : 19872381 : return hstate.end ();
3334 : : }
3335 : :
3336 : : /* The equality function of *P1 and *P2. */
3337 : :
3338 : : inline bool
3339 : 14460513 : refs_hasher::equal (const ref_to_bb *n1, const ref_to_bb *n2)
3340 : : {
3341 : 14460513 : return operand_equal_p (n1->exp, n2->exp, OEP_ADDRESS_OF)
3342 : 14460513 : && n1->size == n2->size;
3343 : : }
3344 : :
3345 : : class nontrapping_dom_walker : public dom_walker
3346 : : {
3347 : : public:
3348 : 1042696 : nontrapping_dom_walker (cdi_direction direction, hash_set<tree> *ps)
3349 : 1042696 : : dom_walker (direction), m_nontrapping (ps), m_seen_refs (128)
3350 : 1042696 : {}
3351 : :
3352 : : edge before_dom_children (basic_block) final override;
3353 : : void after_dom_children (basic_block) final override;
3354 : :
3355 : : private:
3356 : :
3357 : : /* We see the expression EXP in basic block BB. If it's an interesting
3358 : : expression (an MEM_REF through an SSA_NAME) possibly insert the
3359 : : expression into the set NONTRAP or the hash table of seen expressions.
3360 : : STORE is true if this expression is on the LHS, otherwise it's on
3361 : : the RHS. */
3362 : : void add_or_mark_expr (basic_block, tree, bool);
3363 : :
3364 : : hash_set<tree> *m_nontrapping;
3365 : :
3366 : : /* The hash table for remembering what we've seen. */
3367 : : hash_table<refs_hasher> m_seen_refs;
3368 : : };
3369 : :
3370 : : /* Called by walk_dominator_tree, when entering the block BB. */
3371 : : edge
3372 : 11863249 : nontrapping_dom_walker::before_dom_children (basic_block bb)
3373 : : {
3374 : 11863249 : edge e;
3375 : 11863249 : edge_iterator ei;
3376 : 11863249 : gimple_stmt_iterator gsi;
3377 : :
3378 : : /* If we haven't seen all our predecessors, clear the hash-table. */
3379 : 26140365 : FOR_EACH_EDGE (e, ei, bb->preds)
3380 : 14966603 : if ((((size_t)e->src->aux) & 2) == 0)
3381 : : {
3382 : 689487 : nt_call_phase++;
3383 : 689487 : break;
3384 : : }
3385 : :
3386 : : /* Mark this BB as being on the path to dominator root and as visited. */
3387 : 11863249 : bb->aux = (void*)(1 | 2);
3388 : :
3389 : : /* And walk the statements in order. */
3390 : 104726072 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3391 : : {
3392 : 80999574 : gimple *stmt = gsi_stmt (gsi);
3393 : :
3394 : 80999574 : if ((gimple_code (stmt) == GIMPLE_ASM && gimple_vdef (stmt))
3395 : 81041605 : || (is_gimple_call (stmt)
3396 : 5600591 : && (!nonfreeing_call_p (stmt) || !nonbarrier_call_p (stmt))))
3397 : 5005966 : nt_call_phase++;
3398 : 91610384 : else if (gimple_assign_single_p (stmt) && !gimple_has_volatile_ops (stmt))
3399 : : {
3400 : 13429400 : add_or_mark_expr (bb, gimple_assign_lhs (stmt), true);
3401 : 13429400 : add_or_mark_expr (bb, gimple_assign_rhs1 (stmt), false);
3402 : : }
3403 : : }
3404 : 11863249 : return NULL;
3405 : : }
3406 : :
3407 : : /* Called by walk_dominator_tree, when basic block BB is exited. */
3408 : : void
3409 : 11863249 : nontrapping_dom_walker::after_dom_children (basic_block bb)
3410 : : {
3411 : : /* This BB isn't on the path to dominator root anymore. */
3412 : 11863249 : bb->aux = (void*)2;
3413 : 11863249 : }
3414 : :
3415 : : /* We see the expression EXP in basic block BB. If it's an interesting
3416 : : expression of:
3417 : : 1) MEM_REF
3418 : : 2) ARRAY_REF
3419 : : 3) COMPONENT_REF
3420 : : possibly insert the expression into the set NONTRAP or the hash table
3421 : : of seen expressions. STORE is true if this expression is on the LHS,
3422 : : otherwise it's on the RHS. */
3423 : : void
3424 : 26858800 : nontrapping_dom_walker::add_or_mark_expr (basic_block bb, tree exp, bool store)
3425 : : {
3426 : 26858800 : HOST_WIDE_INT size;
3427 : :
3428 : 26858800 : if ((TREE_CODE (exp) == MEM_REF || TREE_CODE (exp) == ARRAY_REF
3429 : 22523693 : || TREE_CODE (exp) == COMPONENT_REF)
3430 : 34151872 : && (size = int_size_in_bytes (TREE_TYPE (exp))) > 0)
3431 : : {
3432 : 11628174 : struct ref_to_bb map;
3433 : 11628174 : ref_to_bb **slot;
3434 : 11628174 : struct ref_to_bb *r2bb;
3435 : 11628174 : basic_block found_bb = 0;
3436 : :
3437 : 11628174 : if (!store)
3438 : : {
3439 : 5393548 : tree base = get_base_address (exp);
3440 : : /* Only record a LOAD of a local variable without address-taken, as
3441 : : the local stack is always writable. This allows cselim on a STORE
3442 : : with a dominating LOAD. */
3443 : 5393548 : if (!auto_var_p (base) || TREE_ADDRESSABLE (base))
3444 : 4400748 : return;
3445 : : }
3446 : :
3447 : : /* Try to find the last seen *_REF, which can trap. */
3448 : 7227426 : map.exp = exp;
3449 : 7227426 : map.size = size;
3450 : 7227426 : slot = m_seen_refs.find_slot (&map, INSERT);
3451 : 7227426 : r2bb = *slot;
3452 : 7227426 : if (r2bb && r2bb->phase >= nt_call_phase)
3453 : 316125 : found_bb = r2bb->bb;
3454 : :
3455 : : /* If we've found a trapping *_REF, _and_ it dominates EXP
3456 : : (it's in a basic block on the path from us to the dominator root)
3457 : : then we can't trap. */
3458 : 316125 : if (found_bb && (((size_t)found_bb->aux) & 1) == 1)
3459 : : {
3460 : 83060 : m_nontrapping->add (exp);
3461 : : }
3462 : : else
3463 : : {
3464 : : /* EXP might trap, so insert it into the hash table. */
3465 : 7144366 : if (r2bb)
3466 : : {
3467 : 1001862 : r2bb->phase = nt_call_phase;
3468 : 1001862 : r2bb->bb = bb;
3469 : : }
3470 : : else
3471 : : {
3472 : 6142504 : r2bb = XNEW (struct ref_to_bb);
3473 : 6142504 : r2bb->phase = nt_call_phase;
3474 : 6142504 : r2bb->bb = bb;
3475 : 6142504 : r2bb->exp = exp;
3476 : 6142504 : r2bb->size = size;
3477 : 6142504 : *slot = r2bb;
3478 : : }
3479 : : }
3480 : : }
3481 : : }
3482 : :
3483 : : /* This is the entry point of gathering non trapping memory accesses.
3484 : : It will do a dominator walk over the whole function, and it will
3485 : : make use of the bb->aux pointers. It returns a set of trees
3486 : : (the MEM_REFs itself) which can't trap. */
3487 : : static hash_set<tree> *
3488 : 1042696 : get_non_trapping (void)
3489 : : {
3490 : 1042696 : nt_call_phase = 0;
3491 : 1042696 : hash_set<tree> *nontrap = new hash_set<tree>;
3492 : :
3493 : 2085392 : nontrapping_dom_walker (CDI_DOMINATORS, nontrap)
3494 : 1042696 : .walk (cfun->cfg->x_entry_block_ptr);
3495 : :
3496 : 1042696 : clear_aux_for_blocks ();
3497 : 1042696 : return nontrap;
3498 : : }
3499 : :
3500 : : /* Do the main work of conditional store replacement. We already know
3501 : : that the recognized pattern looks like so:
3502 : :
3503 : : split:
3504 : : if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
3505 : : MIDDLE_BB:
3506 : : something
3507 : : fallthrough (edge E0)
3508 : : JOIN_BB:
3509 : : some more
3510 : :
3511 : : We check that MIDDLE_BB contains only one store, that that store
3512 : : doesn't trap (not via NOTRAP, but via checking if an access to the same
3513 : : memory location dominates us, or the store is to a local addressable
3514 : : object) and that the store has a "simple" RHS. */
3515 : :
3516 : : static bool
3517 : 424887 : cond_store_replacement (basic_block middle_bb, basic_block join_bb,
3518 : : edge e0, edge e1, hash_set<tree> *nontrap)
3519 : : {
3520 : 424887 : gimple *assign = last_and_only_stmt (middle_bb);
3521 : 424887 : tree lhs, rhs, name, name2;
3522 : 424887 : gphi *newphi;
3523 : 424887 : gassign *new_stmt;
3524 : 424887 : gimple_stmt_iterator gsi;
3525 : 424887 : location_t locus;
3526 : :
3527 : : /* Check if middle_bb contains of only one store. */
3528 : 424887 : if (!assign
3529 : 172139 : || !gimple_assign_single_p (assign)
3530 : 454919 : || gimple_has_volatile_ops (assign))
3531 : : return false;
3532 : :
3533 : : /* And no PHI nodes so all uses in the single stmt are also
3534 : : available where we insert to. */
3535 : 29781 : if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
3536 : : return false;
3537 : :
3538 : 29586 : locus = gimple_location (assign);
3539 : 29586 : lhs = gimple_assign_lhs (assign);
3540 : 29586 : rhs = gimple_assign_rhs1 (assign);
3541 : 29586 : if ((!REFERENCE_CLASS_P (lhs)
3542 : 29586 : && !DECL_P (lhs))
3543 : 29586 : || !is_gimple_reg_type (TREE_TYPE (lhs)))
3544 : : return false;
3545 : :
3546 : : /* Prove that we can move the store down. We could also check
3547 : : TREE_THIS_NOTRAP here, but in that case we also could move stores,
3548 : : whose value is not available readily, which we want to avoid. */
3549 : 16947 : if (!nontrap->contains (lhs))
3550 : : {
3551 : : /* If LHS is an access to a local variable without address-taken
3552 : : (or when we allow data races) and known not to trap, we could
3553 : : always safely move down the store. */
3554 : 11232 : tree base;
3555 : 11232 : if (ref_can_have_store_data_races (lhs)
3556 : 334 : || tree_could_trap_p (lhs)
3557 : : /* tree_could_trap_p is a predicate for rvalues, so check
3558 : : for readonly memory explicitly. */
3559 : 11459 : || ((base = get_base_address (lhs))
3560 : 227 : && ((DECL_P (base)
3561 : 220 : && TREE_READONLY (base))
3562 : 222 : || TREE_CODE (base) == STRING_CST)))
3563 : 11016 : return false;
3564 : : }
3565 : :
3566 : : /* Now we've checked the constraints, so do the transformation:
3567 : : 1) Remove the single store. */
3568 : 5931 : gsi = gsi_for_stmt (assign);
3569 : 5931 : unlink_stmt_vdef (assign);
3570 : 5931 : gsi_remove (&gsi, true);
3571 : 5931 : release_defs (assign);
3572 : :
3573 : : /* Make both store and load use alias-set zero as we have to
3574 : : deal with the case of the store being a conditional change
3575 : : of the dynamic type. */
3576 : 5931 : lhs = unshare_expr (lhs);
3577 : 5931 : tree *basep = &lhs;
3578 : 11907 : while (handled_component_p (*basep))
3579 : 5976 : basep = &TREE_OPERAND (*basep, 0);
3580 : 5931 : if (TREE_CODE (*basep) == MEM_REF
3581 : 5931 : || TREE_CODE (*basep) == TARGET_MEM_REF)
3582 : 651 : TREE_OPERAND (*basep, 1)
3583 : 1302 : = fold_convert (ptr_type_node, TREE_OPERAND (*basep, 1));
3584 : : else
3585 : 5280 : *basep = build2 (MEM_REF, TREE_TYPE (*basep),
3586 : : build_fold_addr_expr (*basep),
3587 : : build_zero_cst (ptr_type_node));
3588 : :
3589 : : /* 2) Insert a load from the memory of the store to the temporary
3590 : : on the edge which did not contain the store. */
3591 : 5931 : name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
3592 : 5931 : new_stmt = gimple_build_assign (name, lhs);
3593 : 5931 : gimple_set_location (new_stmt, locus);
3594 : 5931 : lhs = unshare_expr (lhs);
3595 : 5931 : {
3596 : : /* Set the no-warning bit on the rhs of the load to avoid uninit
3597 : : warnings. */
3598 : 5931 : tree rhs1 = gimple_assign_rhs1 (new_stmt);
3599 : 5931 : suppress_warning (rhs1, OPT_Wuninitialized);
3600 : : }
3601 : 5931 : gsi_insert_on_edge (e1, new_stmt);
3602 : :
3603 : : /* 3) Create a PHI node at the join block, with one argument
3604 : : holding the old RHS, and the other holding the temporary
3605 : : where we stored the old memory contents. */
3606 : 5931 : name2 = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
3607 : 5931 : newphi = create_phi_node (name2, join_bb);
3608 : 5931 : add_phi_arg (newphi, rhs, e0, locus);
3609 : 5931 : add_phi_arg (newphi, name, e1, locus);
3610 : :
3611 : 5931 : new_stmt = gimple_build_assign (lhs, gimple_phi_result (newphi));
3612 : :
3613 : : /* 4) Insert that PHI node. */
3614 : 5931 : gsi = gsi_after_labels (join_bb);
3615 : 5931 : if (gsi_end_p (gsi))
3616 : : {
3617 : 2020 : gsi = gsi_last_bb (join_bb);
3618 : 2020 : gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
3619 : : }
3620 : : else
3621 : 3911 : gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
3622 : :
3623 : 5931 : if (dump_file && (dump_flags & TDF_DETAILS))
3624 : : {
3625 : 7 : fprintf (dump_file, "\nConditional store replacement happened!");
3626 : 7 : fprintf (dump_file, "\nReplaced the store with a load.");
3627 : 7 : fprintf (dump_file, "\nInserted a new PHI statement in joint block:\n");
3628 : 7 : print_gimple_stmt (dump_file, new_stmt, 0, TDF_VOPS|TDF_MEMSYMS);
3629 : : }
3630 : 5931 : statistics_counter_event (cfun, "conditional store replacement", 1);
3631 : :
3632 : 5931 : return true;
3633 : : }
3634 : :
3635 : : /* Do the main work of conditional store replacement. */
3636 : :
3637 : : static bool
3638 : 667667 : cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,
3639 : : basic_block join_bb, gimple *then_assign,
3640 : : gimple *else_assign,
3641 : : gphi *vphi)
3642 : : {
3643 : 667667 : tree lhs_base, lhs, then_rhs, else_rhs, name;
3644 : 667667 : location_t then_locus, else_locus;
3645 : 667667 : gimple_stmt_iterator gsi;
3646 : 667667 : gphi *newphi;
3647 : 667667 : gassign *new_stmt;
3648 : :
3649 : 667667 : if (then_assign == NULL
3650 : 667667 : || !gimple_assign_single_p (then_assign)
3651 : 628102 : || gimple_clobber_p (then_assign)
3652 : 626139 : || gimple_has_volatile_ops (then_assign)
3653 : 626062 : || else_assign == NULL
3654 : 626062 : || !gimple_assign_single_p (else_assign)
3655 : 39507 : || gimple_clobber_p (else_assign)
3656 : 39496 : || gimple_has_volatile_ops (else_assign)
3657 : 39496 : || stmt_references_abnormal_ssa_name (then_assign)
3658 : 707151 : || stmt_references_abnormal_ssa_name (else_assign))
3659 : 628183 : return false;
3660 : :
3661 : 39484 : lhs = gimple_assign_lhs (then_assign);
3662 : 39484 : if (!is_gimple_reg_type (TREE_TYPE (lhs))
3663 : 39484 : || !operand_equal_p (lhs, gimple_assign_lhs (else_assign), 0))
3664 : 24002 : return false;
3665 : :
3666 : 15482 : lhs_base = get_base_address (lhs);
3667 : 15482 : if (lhs_base == NULL_TREE
3668 : 15482 : || (!DECL_P (lhs_base) && TREE_CODE (lhs_base) != MEM_REF))
3669 : : return false;
3670 : :
3671 : 15482 : then_rhs = gimple_assign_rhs1 (then_assign);
3672 : 15482 : else_rhs = gimple_assign_rhs1 (else_assign);
3673 : 15482 : then_locus = gimple_location (then_assign);
3674 : 15482 : else_locus = gimple_location (else_assign);
3675 : :
3676 : 15482 : if (dump_file && (dump_flags & TDF_DETAILS))
3677 : : {
3678 : 0 : fprintf(dump_file, "factoring out stores:\n\tthen:\n");
3679 : 0 : print_gimple_stmt (dump_file, then_assign, 0,
3680 : : TDF_VOPS|TDF_MEMSYMS);
3681 : 0 : fprintf(dump_file, "\telse:\n");
3682 : 0 : print_gimple_stmt (dump_file, else_assign, 0,
3683 : : TDF_VOPS|TDF_MEMSYMS);
3684 : 0 : fprintf (dump_file, "\n");
3685 : : }
3686 : :
3687 : : /* Now we've checked the constraints, so do the transformation:
3688 : : 1) Remove the stores. */
3689 : 15482 : gsi = gsi_for_stmt (then_assign);
3690 : 15482 : unlink_stmt_vdef (then_assign);
3691 : 15482 : gsi_remove (&gsi, true);
3692 : 15482 : release_defs (then_assign);
3693 : :
3694 : 15482 : gsi = gsi_for_stmt (else_assign);
3695 : 15482 : unlink_stmt_vdef (else_assign);
3696 : 15482 : gsi_remove (&gsi, true);
3697 : 15482 : release_defs (else_assign);
3698 : :
3699 : : /* 2) Create a PHI node at the join block, with one argument
3700 : : holding the old RHS, and the other holding the temporary
3701 : : where we stored the old memory contents. */
3702 : 15482 : name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
3703 : 15482 : newphi = create_phi_node (name, join_bb);
3704 : 15482 : add_phi_arg (newphi, then_rhs, EDGE_SUCC (then_bb, 0), then_locus);
3705 : 15482 : add_phi_arg (newphi, else_rhs, EDGE_SUCC (else_bb, 0), else_locus);
3706 : :
3707 : 15482 : new_stmt = gimple_build_assign (lhs, gimple_phi_result (newphi));
3708 : : /* Update the vdef for the new store statement. */
3709 : 15482 : tree newvphilhs = make_ssa_name (gimple_vop (cfun));
3710 : 15482 : tree vdef = gimple_phi_result (vphi);
3711 : 15482 : gimple_set_vuse (new_stmt, newvphilhs);
3712 : 15482 : gimple_set_vdef (new_stmt, vdef);
3713 : 15482 : gimple_phi_set_result (vphi, newvphilhs);
3714 : 15482 : SSA_NAME_DEF_STMT (vdef) = new_stmt;
3715 : 15482 : update_stmt (vphi);
3716 : 15482 : if (dump_file && (dump_flags & TDF_DETAILS))
3717 : : {
3718 : 0 : fprintf(dump_file, "to use phi:\n");
3719 : 0 : print_gimple_stmt (dump_file, newphi, 0,
3720 : : TDF_VOPS|TDF_MEMSYMS);
3721 : 0 : fprintf(dump_file, "\n");
3722 : 0 : print_gimple_stmt (dump_file, new_stmt, 0,
3723 : : TDF_VOPS|TDF_MEMSYMS);
3724 : 0 : fprintf(dump_file, "\n\n");
3725 : : }
3726 : :
3727 : : /* 3) Insert that PHI node. */
3728 : 15482 : gsi = gsi_after_labels (join_bb);
3729 : 15482 : if (gsi_end_p (gsi))
3730 : : {
3731 : 89 : gsi = gsi_last_bb (join_bb);
3732 : 89 : gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
3733 : : }
3734 : : else
3735 : 15393 : gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
3736 : :
3737 : 15482 : statistics_counter_event (cfun, "if-then-else store replacement", 1);
3738 : :
3739 : 15482 : return true;
3740 : : }
3741 : :
3742 : : /* Return the single store in BB with VDEF or NULL if there are
3743 : : other stores in the BB or loads following the store. VPHI is
3744 : : where the only use of the vdef should be. */
3745 : :
3746 : : static gimple *
3747 : 1562297 : single_trailing_store_in_bb (basic_block bb, tree vdef, gphi *vphi)
3748 : : {
3749 : 1562297 : if (SSA_NAME_IS_DEFAULT_DEF (vdef))
3750 : : return NULL;
3751 : 1542639 : gimple *store = SSA_NAME_DEF_STMT (vdef);
3752 : 1542639 : if (gimple_bb (store) != bb
3753 : 1542639 : || gimple_code (store) == GIMPLE_PHI)
3754 : : return NULL;
3755 : :
3756 : : /* Verify there is no other store in this BB. */
3757 : 3031258 : if (!SSA_NAME_IS_DEFAULT_DEF (gimple_vuse (store))
3758 : 1458765 : && gimple_bb (SSA_NAME_DEF_STMT (gimple_vuse (store))) == bb
3759 : 1670958 : && gimple_code (SSA_NAME_DEF_STMT (gimple_vuse (store))) != GIMPLE_PHI)
3760 : : return NULL;
3761 : :
3762 : : /* Verify there is no load or store after the store, the vdef of the store
3763 : : should only be used by the vphi joining the 2 bbs. */
3764 : 1361546 : use_operand_p use_p;
3765 : 1361546 : gimple *use_stmt;
3766 : 2723092 : if (!single_imm_use (gimple_vdef (store), &use_p, &use_stmt))
3767 : : return NULL;
3768 : 1350192 : if (use_stmt != vphi)
3769 : : return NULL;
3770 : :
3771 : : return store;
3772 : : }
3773 : :
3774 : : /* Limited Conditional store replacement. We already know
3775 : : that the recognized pattern looks like so:
3776 : :
3777 : : split:
3778 : : if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
3779 : : THEN_BB:
3780 : : ...
3781 : : ONLY_STORE = Y;
3782 : : ...
3783 : : goto JOIN_BB;
3784 : : ELSE_BB:
3785 : : ...
3786 : : ONLY_STORE = Z;
3787 : : ...
3788 : : fallthrough (edge E0)
3789 : : JOIN_BB:
3790 : : some more
3791 : :
3792 : : Handles only the case with single store in THEN_BB and ELSE_BB. That is
3793 : : cheap enough due to in phiopt and not worry about heurstics. Moving the store
3794 : : out might provide an opportunity for a phiopt to happen. */
3795 : :
3796 : : static bool
3797 : 734191 : cond_if_else_store_replacement_limited (basic_block then_bb, basic_block else_bb,
3798 : : basic_block join_bb)
3799 : : {
3800 : 734191 : gphi *vphi = get_virtual_phi (join_bb);
3801 : 734191 : if (!vphi)
3802 : : return false;
3803 : :
3804 : 665430 : tree then_vdef = PHI_ARG_DEF_FROM_EDGE (vphi, single_succ_edge (then_bb));
3805 : 665430 : gimple *then_assign = single_trailing_store_in_bb (then_bb, then_vdef, vphi);
3806 : 665430 : if (!then_assign)
3807 : : return false;
3808 : :
3809 : 525720 : tree else_vdef = PHI_ARG_DEF_FROM_EDGE (vphi, single_succ_edge (else_bb));
3810 : 525720 : gimple *else_assign = single_trailing_store_in_bb (else_bb, else_vdef, vphi);
3811 : 525720 : if (!else_assign)
3812 : : return false;
3813 : :
3814 : 500869 : return cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
3815 : 500869 : then_assign, else_assign, vphi);
3816 : : }
3817 : :
3818 : : /* Conditional store replacement. We already know
3819 : : that the recognized pattern looks like so:
3820 : :
3821 : : split:
3822 : : if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
3823 : : THEN_BB:
3824 : : ...
3825 : : X = Y;
3826 : : ...
3827 : : goto JOIN_BB;
3828 : : ELSE_BB:
3829 : : ...
3830 : : X = Z;
3831 : : ...
3832 : : fallthrough (edge E0)
3833 : : JOIN_BB:
3834 : : some more
3835 : :
3836 : : We check that it is safe to sink the store to JOIN_BB by verifying that
3837 : : there are no read-after-write or write-after-write dependencies in
3838 : : THEN_BB and ELSE_BB. */
3839 : :
3840 : : static bool
3841 : 223659 : cond_if_else_store_replacement (basic_block then_bb, basic_block else_bb,
3842 : : basic_block join_bb)
3843 : : {
3844 : 223659 : vec<data_reference_p> then_datarefs, else_datarefs;
3845 : 223659 : vec<ddr_p> then_ddrs, else_ddrs;
3846 : 223659 : gimple *then_store, *else_store;
3847 : 223659 : bool found, ok = false, res;
3848 : 223659 : tree then_lhs, else_lhs;
3849 : 223659 : basic_block blocks[3];
3850 : :
3851 : : /* Handle the case with single store in THEN_BB and ELSE_BB. That is
3852 : : cheap enough to always handle as it allows us to elide dependence
3853 : : checking. */
3854 : 223659 : gphi *vphi = get_virtual_phi (join_bb);
3855 : 223659 : if (!vphi)
3856 : : return false;
3857 : 206771 : tree then_vdef = PHI_ARG_DEF_FROM_EDGE (vphi, single_succ_edge (then_bb));
3858 : 206771 : gimple *then_assign = single_trailing_store_in_bb (then_bb, then_vdef, vphi);
3859 : 206771 : if (then_assign)
3860 : : {
3861 : 164376 : tree else_vdef = PHI_ARG_DEF_FROM_EDGE (vphi, single_succ_edge (else_bb));
3862 : 164376 : gimple *else_assign = single_trailing_store_in_bb (else_bb, else_vdef,
3863 : : vphi);
3864 : 164376 : if (else_assign)
3865 : 159227 : return cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
3866 : : then_assign, else_assign,
3867 : 159227 : vphi);
3868 : : }
3869 : :
3870 : : /* If either vectorization or if-conversion is disabled then do
3871 : : not sink any stores. */
3872 : 47544 : if (param_max_stores_to_sink == 0
3873 : 47543 : || (!flag_tree_loop_vectorize && !flag_tree_slp_vectorize)
3874 : 45749 : || !flag_tree_loop_if_convert)
3875 : : return false;
3876 : :
3877 : : /* Find data references. */
3878 : 45746 : then_datarefs.create (1);
3879 : 45746 : else_datarefs.create (1);
3880 : 45746 : if ((find_data_references_in_bb (NULL, then_bb, &then_datarefs)
3881 : 45746 : == chrec_dont_know)
3882 : 31851 : || !then_datarefs.length ()
3883 : 29369 : || (find_data_references_in_bb (NULL, else_bb, &else_datarefs)
3884 : 29369 : == chrec_dont_know)
3885 : 57152 : || !else_datarefs.length ())
3886 : : {
3887 : 34728 : free_data_refs (then_datarefs);
3888 : 34728 : free_data_refs (else_datarefs);
3889 : 34728 : return false;
3890 : : }
3891 : :
3892 : : /* Find pairs of stores with equal LHS. */
3893 : 11018 : auto_vec<std::pair<gimple *, gimple *>, 1> stores_pairs;
3894 : 69891 : for (auto then_dr : then_datarefs)
3895 : : {
3896 : 36837 : if (DR_IS_READ (then_dr))
3897 : 10228 : continue;
3898 : :
3899 : 26609 : then_store = DR_STMT (then_dr);
3900 : 26609 : then_lhs = gimple_get_lhs (then_store);
3901 : 26609 : if (then_lhs == NULL_TREE)
3902 : 0 : continue;
3903 : 26609 : found = false;
3904 : :
3905 : 143295 : for (auto else_dr : else_datarefs)
3906 : : {
3907 : 77435 : if (DR_IS_READ (else_dr))
3908 : 20062 : continue;
3909 : :
3910 : 57373 : else_store = DR_STMT (else_dr);
3911 : 57373 : else_lhs = gimple_get_lhs (else_store);
3912 : 57373 : if (else_lhs == NULL_TREE)
3913 : 0 : continue;
3914 : :
3915 : 57373 : if (operand_equal_p (then_lhs, else_lhs, 0))
3916 : : {
3917 : : found = true;
3918 : : break;
3919 : : }
3920 : : }
3921 : :
3922 : 26609 : if (!found)
3923 : 12642 : continue;
3924 : :
3925 : 13967 : stores_pairs.safe_push (std::make_pair (then_store, else_store));
3926 : : }
3927 : :
3928 : : /* No pairs of stores found. */
3929 : 11018 : if (!stores_pairs.length ()
3930 : 11018 : || stores_pairs.length () > (unsigned) param_max_stores_to_sink)
3931 : : {
3932 : 3622 : free_data_refs (then_datarefs);
3933 : 3622 : free_data_refs (else_datarefs);
3934 : 3622 : return false;
3935 : : }
3936 : :
3937 : : /* Compute and check data dependencies in both basic blocks. */
3938 : 7396 : then_ddrs.create (1);
3939 : 7396 : else_ddrs.create (1);
3940 : 7396 : if (!compute_all_dependences (then_datarefs, &then_ddrs,
3941 : 7396 : vNULL, false)
3942 : 14792 : || !compute_all_dependences (else_datarefs, &else_ddrs,
3943 : 7396 : vNULL, false))
3944 : : {
3945 : 0 : free_dependence_relations (then_ddrs);
3946 : 0 : free_dependence_relations (else_ddrs);
3947 : 0 : free_data_refs (then_datarefs);
3948 : 0 : free_data_refs (else_datarefs);
3949 : 0 : return false;
3950 : : }
3951 : 7396 : blocks[0] = then_bb;
3952 : 7396 : blocks[1] = else_bb;
3953 : 7396 : blocks[2] = join_bb;
3954 : 7396 : renumber_gimple_stmt_uids_in_blocks (blocks, 3);
3955 : :
3956 : : /* Check that there are no read-after-write or write-after-write dependencies
3957 : : in THEN_BB. */
3958 : 45839 : for (auto ddr : then_ddrs)
3959 : : {
3960 : 24429 : struct data_reference *dra = DDR_A (ddr);
3961 : 24429 : struct data_reference *drb = DDR_B (ddr);
3962 : :
3963 : 24429 : if (DDR_ARE_DEPENDENT (ddr) != chrec_known
3964 : 24429 : && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
3965 : 1079 : && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
3966 : 1857 : || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
3967 : 485 : && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
3968 : 1372 : || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
3969 : : {
3970 : 778 : free_dependence_relations (then_ddrs);
3971 : 778 : free_dependence_relations (else_ddrs);
3972 : 778 : free_data_refs (then_datarefs);
3973 : 778 : free_data_refs (else_datarefs);
3974 : 778 : return false;
3975 : : }
3976 : : }
3977 : :
3978 : : /* Check that there are no read-after-write or write-after-write dependencies
3979 : : in ELSE_BB. */
3980 : 29115 : for (auto ddr : else_ddrs)
3981 : : {
3982 : 9777 : struct data_reference *dra = DDR_A (ddr);
3983 : 9777 : struct data_reference *drb = DDR_B (ddr);
3984 : :
3985 : 9777 : if (DDR_ARE_DEPENDENT (ddr) != chrec_known
3986 : 9777 : && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
3987 : 511 : && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
3988 : 1027 : || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
3989 : 486 : && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
3990 : 541 : || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
3991 : : {
3992 : 516 : free_dependence_relations (then_ddrs);
3993 : 516 : free_dependence_relations (else_ddrs);
3994 : 516 : free_data_refs (then_datarefs);
3995 : 516 : free_data_refs (else_datarefs);
3996 : 516 : return false;
3997 : : }
3998 : : }
3999 : :
4000 : : /* Sink stores with same LHS. */
4001 : 25877 : for (auto &store_pair : stores_pairs)
4002 : : {
4003 : 7571 : then_store = store_pair.first;
4004 : 7571 : else_store = store_pair.second;
4005 : 7571 : res = cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
4006 : : then_store, else_store, vphi);
4007 : 7571 : ok = ok || res;
4008 : : }
4009 : :
4010 : 6102 : free_dependence_relations (then_ddrs);
4011 : 6102 : free_dependence_relations (else_ddrs);
4012 : 6102 : free_data_refs (then_datarefs);
4013 : 6102 : free_data_refs (else_datarefs);
4014 : :
4015 : 6102 : return ok;
4016 : 11018 : }
4017 : :
4018 : : /* Return TRUE if STMT has a VUSE whose corresponding VDEF is in BB. */
4019 : :
4020 : : static bool
4021 : 12398 : local_mem_dependence (gimple *stmt, basic_block bb)
4022 : : {
4023 : 24796 : tree vuse = gimple_vuse (stmt);
4024 : 12398 : gimple *def;
4025 : :
4026 : 12398 : if (!vuse)
4027 : : return false;
4028 : :
4029 : 12398 : def = SSA_NAME_DEF_STMT (vuse);
4030 : 12398 : return (def && gimple_bb (def) == bb);
4031 : : }
4032 : :
4033 : : /* Given a "diamond" control-flow pattern where BB0 tests a condition,
4034 : : BB1 and BB2 are "then" and "else" blocks dependent on this test,
4035 : : and BB3 rejoins control flow following BB1 and BB2, look for
4036 : : opportunities to hoist loads as follows. If BB3 contains a PHI of
4037 : : two loads, one each occurring in BB1 and BB2, and the loads are
4038 : : provably of adjacent fields in the same structure, then move both
4039 : : loads into BB0. Of course this can only be done if there are no
4040 : : dependencies preventing such motion.
4041 : :
4042 : : One of the hoisted loads will always be speculative, so the
4043 : : transformation is currently conservative:
4044 : :
4045 : : - The fields must be strictly adjacent.
4046 : : - The two fields must occupy a single memory block that is
4047 : : guaranteed to not cross a page boundary.
4048 : :
4049 : : The last is difficult to prove, as such memory blocks should be
4050 : : aligned on the minimum of the stack alignment boundary and the
4051 : : alignment guaranteed by heap allocation interfaces. Thus we rely
4052 : : on a parameter for the alignment value.
4053 : :
4054 : : Provided a good value is used for the last case, the first
4055 : : restriction could possibly be relaxed. */
4056 : :
4057 : : static void
4058 : 573611 : hoist_adjacent_loads (basic_block bb0, basic_block bb1,
4059 : : basic_block bb2, basic_block bb3)
4060 : : {
4061 : 573611 : unsigned HOST_WIDE_INT param_align = param_l1_cache_line_size;
4062 : 573611 : unsigned HOST_WIDE_INT param_align_bits = param_align * BITS_PER_UNIT;
4063 : 573611 : gphi_iterator gsi;
4064 : :
4065 : : /* Walk the phis in bb3 looking for an opportunity. We are looking
4066 : : for phis of two SSA names, one each of which is defined in bb1 and
4067 : : bb2. */
4068 : 1280609 : for (gsi = gsi_start_phis (bb3); !gsi_end_p (gsi); gsi_next (&gsi))
4069 : : {
4070 : 706998 : gphi *phi_stmt = gsi.phi ();
4071 : 706998 : gimple *def1, *def2;
4072 : 706998 : tree arg1, arg2, ref1, ref2, field1, field2;
4073 : 706998 : tree tree_offset1, tree_offset2, tree_size2, next;
4074 : 706998 : unsigned HOST_WIDE_INT offset1, offset2, size2, align1;
4075 : 706998 : gimple_stmt_iterator gsi2;
4076 : 706998 : basic_block bb_for_def1, bb_for_def2;
4077 : :
4078 : 706998 : if (gimple_phi_num_args (phi_stmt) != 2
4079 : 1413996 : || virtual_operand_p (gimple_phi_result (phi_stmt)))
4080 : 700799 : continue;
4081 : :
4082 : 174047 : arg1 = gimple_phi_arg_def (phi_stmt, 0);
4083 : 174047 : arg2 = gimple_phi_arg_def (phi_stmt, 1);
4084 : :
4085 : 203351 : if (TREE_CODE (arg1) != SSA_NAME
4086 : 157490 : || TREE_CODE (arg2) != SSA_NAME
4087 : 145210 : || SSA_NAME_IS_DEFAULT_DEF (arg1)
4088 : 318933 : || SSA_NAME_IS_DEFAULT_DEF (arg2))
4089 : 29304 : continue;
4090 : :
4091 : 144743 : def1 = SSA_NAME_DEF_STMT (arg1);
4092 : 144743 : def2 = SSA_NAME_DEF_STMT (arg2);
4093 : :
4094 : 144743 : if ((gimple_bb (def1) != bb1 || gimple_bb (def2) != bb2)
4095 : 155206 : && (gimple_bb (def2) != bb1 || gimple_bb (def1) != bb2))
4096 : 33432 : continue;
4097 : :
4098 : : /* Check the mode of the arguments to be sure a conditional move
4099 : : can be generated for it. */
4100 : 222622 : if (optab_handler (movcc_optab, TYPE_MODE (TREE_TYPE (arg1)))
4101 : : == CODE_FOR_nothing)
4102 : 5304 : continue;
4103 : :
4104 : : /* Both statements must be assignments whose RHS is a COMPONENT_REF. */
4105 : 106007 : if (!gimple_assign_single_p (def1)
4106 : 50569 : || !gimple_assign_single_p (def2)
4107 : 58944 : || gimple_has_volatile_ops (def1)
4108 : 164945 : || gimple_has_volatile_ops (def2))
4109 : 76538 : continue;
4110 : :
4111 : 29469 : ref1 = gimple_assign_rhs1 (def1);
4112 : 29469 : ref2 = gimple_assign_rhs1 (def2);
4113 : :
4114 : 29469 : if (TREE_CODE (ref1) != COMPONENT_REF
4115 : 17908 : || TREE_CODE (ref2) != COMPONENT_REF)
4116 : 11686 : continue;
4117 : :
4118 : : /* The zeroth operand of the two component references must be
4119 : : identical. It is not sufficient to compare get_base_address of
4120 : : the two references, because this could allow for different
4121 : : elements of the same array in the two trees. It is not safe to
4122 : : assume that the existence of one array element implies the
4123 : : existence of a different one. */
4124 : 17783 : if (!operand_equal_p (TREE_OPERAND (ref1, 0), TREE_OPERAND (ref2, 0), 0))
4125 : 6384 : continue;
4126 : :
4127 : 11399 : field1 = TREE_OPERAND (ref1, 1);
4128 : 11399 : field2 = TREE_OPERAND (ref2, 1);
4129 : :
4130 : : /* Check for field adjacency, and ensure field1 comes first. */
4131 : 11399 : for (next = DECL_CHAIN (field1);
4132 : 35092 : next && TREE_CODE (next) != FIELD_DECL;
4133 : 23693 : next = DECL_CHAIN (next))
4134 : : ;
4135 : :
4136 : 11399 : if (next != field2)
4137 : : {
4138 : 9651 : for (next = DECL_CHAIN (field2);
4139 : 11199 : next && TREE_CODE (next) != FIELD_DECL;
4140 : 1548 : next = DECL_CHAIN (next))
4141 : : ;
4142 : :
4143 : 9651 : if (next != field1)
4144 : 5200 : continue;
4145 : :
4146 : : std::swap (field1, field2);
4147 : : std::swap (def1, def2);
4148 : : }
4149 : :
4150 : 6199 : bb_for_def1 = gimple_bb (def1);
4151 : 6199 : bb_for_def2 = gimple_bb (def2);
4152 : :
4153 : : /* Check for proper alignment of the first field. */
4154 : 6199 : tree_offset1 = bit_position (field1);
4155 : 6199 : tree_offset2 = bit_position (field2);
4156 : 6199 : tree_size2 = DECL_SIZE (field2);
4157 : :
4158 : 6199 : if (!tree_fits_uhwi_p (tree_offset1)
4159 : 6199 : || !tree_fits_uhwi_p (tree_offset2)
4160 : 6199 : || !tree_fits_uhwi_p (tree_size2))
4161 : 0 : continue;
4162 : :
4163 : 6199 : offset1 = tree_to_uhwi (tree_offset1);
4164 : 6199 : offset2 = tree_to_uhwi (tree_offset2);
4165 : 6199 : size2 = tree_to_uhwi (tree_size2);
4166 : 6199 : align1 = DECL_ALIGN (field1) % param_align_bits;
4167 : :
4168 : 6199 : if (offset1 % BITS_PER_UNIT != 0)
4169 : 0 : continue;
4170 : :
4171 : : /* For profitability, the two field references should fit within
4172 : : a single cache line. */
4173 : 6199 : if (align1 + offset2 - offset1 + size2 > param_align_bits)
4174 : 0 : continue;
4175 : :
4176 : : /* The two expressions cannot be dependent upon vdefs defined
4177 : : in bb1/bb2. */
4178 : 6199 : if (local_mem_dependence (def1, bb_for_def1)
4179 : 6199 : || local_mem_dependence (def2, bb_for_def2))
4180 : 0 : continue;
4181 : :
4182 : : /* The conditions are satisfied; hoist the loads from bb1 and bb2 into
4183 : : bb0. We hoist the first one first so that a cache miss is handled
4184 : : efficiently regardless of hardware cache-fill policy. */
4185 : 6199 : gsi2 = gsi_for_stmt (def1);
4186 : 6199 : gsi_move_to_bb_end (&gsi2, bb0);
4187 : 6199 : gsi2 = gsi_for_stmt (def2);
4188 : 6199 : gsi_move_to_bb_end (&gsi2, bb0);
4189 : 6199 : statistics_counter_event (cfun, "hoisted loads", 1);
4190 : :
4191 : 6199 : if (dump_file && (dump_flags & TDF_DETAILS))
4192 : : {
4193 : 0 : fprintf (dump_file,
4194 : : "\nHoisting adjacent loads from %d and %d into %d: \n",
4195 : : bb_for_def1->index, bb_for_def2->index, bb0->index);
4196 : 0 : print_gimple_stmt (dump_file, def1, 0, TDF_VOPS|TDF_MEMSYMS);
4197 : 0 : print_gimple_stmt (dump_file, def2, 0, TDF_VOPS|TDF_MEMSYMS);
4198 : : }
4199 : : }
4200 : 573611 : }
4201 : :
4202 : : /* Determine whether we should attempt to hoist adjacent loads out of
4203 : : diamond patterns in pass_phiopt. Always hoist loads if
4204 : : -fhoist-adjacent-loads is specified and the target machine has
4205 : : both a conditional move instruction and a defined cache line size. */
4206 : :
4207 : : static bool
4208 : 3127989 : gate_hoist_loads (void)
4209 : : {
4210 : 3127989 : return (flag_hoist_adjacent_loads == 1
4211 : 3127989 : && param_l1_cache_line_size
4212 : 0 : && HAVE_conditional_move);
4213 : : }
4214 : :
4215 : : template <class func_type>
4216 : : static void
4217 : 6679394 : execute_over_cond_phis (func_type func)
4218 : : {
4219 : : unsigned n, i;
4220 : : basic_block *bb_order;
4221 : : basic_block bb;
4222 : : /* Search every basic block for COND_EXPR we may be able to optimize.
4223 : :
4224 : : We walk the blocks in order that guarantees that a block with
4225 : : a single predecessor is processed before the predecessor.
4226 : : This ensures that we collapse inner ifs before visiting the
4227 : : outer ones, and also that we do not try to visit a removed
4228 : : block. */
4229 : 6679394 : bb_order = single_pred_before_succ_order ();
4230 : 6679394 : n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
4231 : :
4232 : 60690137 : for (i = 0; i < n; i++)
4233 : : {
4234 : : basic_block bb1, bb2;
4235 : : edge e1, e2;
4236 : 54010743 : bool diamond_p = false;
4237 : :
4238 : 54010743 : bb = bb_order[i];
4239 : :
4240 : : /* Check to see if the last statement is a GIMPLE_COND. */
4241 : 54010743 : gcond *cond_stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
4242 : 32598794 : if (!cond_stmt)
4243 : 54010743 : continue;
4244 : :
4245 : 21411949 : e1 = EDGE_SUCC (bb, 0);
4246 : 21411949 : bb1 = e1->dest;
4247 : 21411949 : e2 = EDGE_SUCC (bb, 1);
4248 : 21411949 : bb2 = e2->dest;
4249 : :
4250 : : /* We cannot do the optimization on abnormal edges. */
4251 : 21411949 : if ((e1->flags & EDGE_ABNORMAL) != 0
4252 : 21411949 : || (e2->flags & EDGE_ABNORMAL) != 0)
4253 : 0 : continue;
4254 : :
4255 : : /* If either bb1's succ or bb2 or bb2's succ is non NULL. */
4256 : 21411949 : if (EDGE_COUNT (bb1->succs) == 0
4257 : 19930479 : || EDGE_COUNT (bb2->succs) == 0)
4258 : 4847357 : continue;
4259 : :
4260 : : /* Find the bb which is the fall through to the other. */
4261 : 16564592 : if (EDGE_SUCC (bb1, 0)->dest == bb2)
4262 : : ;
4263 : 14087971 : else if (EDGE_SUCC (bb2, 0)->dest == bb1)
4264 : : {
4265 : : std::swap (bb1, bb2);
4266 : : std::swap (e1, e2);
4267 : : }
4268 : 10772898 : else if (EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest
4269 : 12261241 : && single_succ_p (bb2))
4270 : : {
4271 : 1488343 : diamond_p = true;
4272 : 1488343 : e2 = EDGE_SUCC (bb2, 0);
4273 : : /* Make sure bb2 is just a fall through. */
4274 : 1488343 : if ((e2->flags & EDGE_FALLTHRU) == 0)
4275 : 57292 : continue;
4276 : : }
4277 : : else
4278 : 10772898 : continue;
4279 : :
4280 : 5734402 : e1 = EDGE_SUCC (bb1, 0);
4281 : :
4282 : : /* Make sure that bb1 is just a fall through. */
4283 : 5734402 : if (!single_succ_p (bb1)
4284 : 5734402 : || (e1->flags & EDGE_FALLTHRU) == 0)
4285 : 1419427 : continue;
4286 : :
4287 : 4314975 : func (bb, bb1, bb2, e1, e2, diamond_p, cond_stmt);
4288 : : }
4289 : 6679394 : free (bb_order);
4290 : 6679394 : }
4291 : :
4292 : : /* This pass tries to replaces an if-then-else block with an
4293 : : assignment. We have different kinds of transformations.
4294 : : Some of these transformations are also performed by the ifcvt
4295 : : RTL optimizer.
4296 : :
4297 : : PHI-OPT using Match-and-simplify infrastructure
4298 : : -----------------------
4299 : :
4300 : : The PHI-OPT pass will try to use match-and-simplify infrastructure
4301 : : (gimple_simplify) to do transformations. This is implemented in
4302 : : match_simplify_replacement.
4303 : :
4304 : : The way it works is it replaces:
4305 : : bb0:
4306 : : if (cond) goto bb2; else goto bb1;
4307 : : bb1:
4308 : : bb2:
4309 : : x = PHI <a (bb1), b (bb0), ...>;
4310 : :
4311 : : with a statement if it gets simplified from `cond ? b : a`.
4312 : :
4313 : : bb0:
4314 : : x1 = cond ? b : a;
4315 : : bb2:
4316 : : x = PHI <a (bb1), x1 (bb0), ...>;
4317 : : Bb1 might be removed as it becomes unreachable when doing the replacement.
4318 : : Though bb1 does not have to be considered a forwarding basic block from bb0.
4319 : :
4320 : : Will try to see if `(!cond) ? a : b` gets simplified (iff !cond simplifies);
4321 : : this is done not to have an explosion of patterns in match.pd.
4322 : : Note bb1 does not need to be completely empty, it can contain
4323 : : one statement which is known not to trap.
4324 : :
4325 : : It also can handle the case where we have two forwarding bbs (diamond):
4326 : : bb0:
4327 : : if (cond) goto bb2; else goto bb1;
4328 : : bb1: goto bb3;
4329 : : bb2: goto bb3;
4330 : : bb3:
4331 : : x = PHI <a (bb1), b (bb2), ...>;
4332 : : And that is replaced with a statement if it is simplified
4333 : : from `cond ? b : a`.
4334 : : Again bb1 and bb2 does not have to be completely empty but
4335 : : each can contain one statement which is known not to trap.
4336 : : But in this case bb1/bb2 can only be forwarding basic blocks.
4337 : :
4338 : : This fully replaces the old "Conditional Replacement",
4339 : : "ABS Replacement" transformations as they are now
4340 : : implmeneted in match.pd.
4341 : : Some parts of the "MIN/MAX Replacement" are re-implemented in match.pd.
4342 : :
4343 : : Value Replacement
4344 : : -----------------
4345 : :
4346 : : This transformation, implemented in value_replacement, replaces
4347 : :
4348 : : bb0:
4349 : : if (a != b) goto bb2; else goto bb1;
4350 : : bb1:
4351 : : bb2:
4352 : : x = PHI <a (bb1), b (bb0), ...>;
4353 : :
4354 : : with
4355 : :
4356 : : bb0:
4357 : : bb2:
4358 : : x = PHI <b (bb0), ...>;
4359 : :
4360 : : This opportunity can sometimes occur as a result of other
4361 : : optimizations.
4362 : :
4363 : :
4364 : : Another case caught by value replacement looks like this:
4365 : :
4366 : : bb0:
4367 : : t1 = a == CONST;
4368 : : t2 = b > c;
4369 : : t3 = t1 & t2;
4370 : : if (t3 != 0) goto bb1; else goto bb2;
4371 : : bb1:
4372 : : bb2:
4373 : : x = PHI (CONST, a)
4374 : :
4375 : : Gets replaced with:
4376 : : bb0:
4377 : : bb2:
4378 : : t1 = a == CONST;
4379 : : t2 = b > c;
4380 : : t3 = t1 & t2;
4381 : : x = a;
4382 : :
4383 : : MIN/MAX Replacement
4384 : : -------------------
4385 : :
4386 : : This transformation, minmax_replacement replaces
4387 : :
4388 : : bb0:
4389 : : if (a <= b) goto bb2; else goto bb1;
4390 : : bb1:
4391 : : bb2:
4392 : : x = PHI <b (bb1), a (bb0), ...>;
4393 : :
4394 : : with
4395 : :
4396 : : bb0:
4397 : : x' = MIN_EXPR (a, b)
4398 : : bb2:
4399 : : x = PHI <x' (bb0), ...>;
4400 : :
4401 : : A similar transformation is done for MAX_EXPR.
4402 : :
4403 : :
4404 : : This pass also performs a fifth transformation of a slightly different
4405 : : flavor.
4406 : :
4407 : : Factor operations in COND_EXPR
4408 : : ------------------------------
4409 : :
4410 : : This transformation factors the unary operations out of COND_EXPR with
4411 : : factor_out_conditional_operation.
4412 : :
4413 : : For example:
4414 : : if (a <= CST) goto <bb 3>; else goto <bb 4>;
4415 : : <bb 3>:
4416 : : tmp = (int) a;
4417 : : <bb 4>:
4418 : : tmp = PHI <tmp, CST>
4419 : :
4420 : : Into:
4421 : : if (a <= CST) goto <bb 3>; else goto <bb 4>;
4422 : : <bb 3>:
4423 : : <bb 4>:
4424 : : a = PHI <a, CST>
4425 : : tmp = (int) a;
4426 : :
4427 : : Adjacent Load Hoisting
4428 : : ----------------------
4429 : :
4430 : : This transformation replaces
4431 : :
4432 : : bb0:
4433 : : if (...) goto bb2; else goto bb1;
4434 : : bb1:
4435 : : x1 = (<expr>).field1;
4436 : : goto bb3;
4437 : : bb2:
4438 : : x2 = (<expr>).field2;
4439 : : bb3:
4440 : : # x = PHI <x1, x2>;
4441 : :
4442 : : with
4443 : :
4444 : : bb0:
4445 : : x1 = (<expr>).field1;
4446 : : x2 = (<expr>).field2;
4447 : : if (...) goto bb2; else goto bb1;
4448 : : bb1:
4449 : : goto bb3;
4450 : : bb2:
4451 : : bb3:
4452 : : # x = PHI <x1, x2>;
4453 : :
4454 : : The purpose of this transformation is to enable generation of conditional
4455 : : move instructions such as Intel CMOVE or PowerPC ISEL. Because one of
4456 : : the loads is speculative, the transformation is restricted to very
4457 : : specific cases to avoid introducing a page fault. We are looking for
4458 : : the common idiom:
4459 : :
4460 : : if (...)
4461 : : x = y->left;
4462 : : else
4463 : : x = y->right;
4464 : :
4465 : : where left and right are typically adjacent pointers in a tree structure. */
4466 : :
4467 : : namespace {
4468 : :
4469 : : const pass_data pass_data_phiopt =
4470 : : {
4471 : : GIMPLE_PASS, /* type */
4472 : : "phiopt", /* name */
4473 : : OPTGROUP_NONE, /* optinfo_flags */
4474 : : TV_TREE_PHIOPT, /* tv_id */
4475 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
4476 : : 0, /* properties_provided */
4477 : : 0, /* properties_destroyed */
4478 : : 0, /* todo_flags_start */
4479 : : 0, /* todo_flags_finish */
4480 : : };
4481 : :
4482 : : class pass_phiopt : public gimple_opt_pass
4483 : : {
4484 : : public:
4485 : 1142756 : pass_phiopt (gcc::context *ctxt)
4486 : 2285512 : : gimple_opt_pass (pass_data_phiopt, ctxt), early_p (false)
4487 : : {}
4488 : :
4489 : : /* opt_pass methods: */
4490 : 857067 : opt_pass * clone () final override { return new pass_phiopt (m_ctxt); }
4491 : 1142756 : void set_pass_param (unsigned n, bool param) final override
4492 : : {
4493 : 1142756 : gcc_assert (n == 0);
4494 : 1142756 : early_p = param;
4495 : 1142756 : }
4496 : 5640109 : bool gate (function *) final override { return flag_ssa_phiopt; }
4497 : : unsigned int execute (function *) final override;
4498 : :
4499 : : private:
4500 : : bool early_p;
4501 : : }; // class pass_phiopt
4502 : :
4503 : : } // anon namespace
4504 : :
4505 : : gimple_opt_pass *
4506 : 285689 : make_pass_phiopt (gcc::context *ctxt)
4507 : : {
4508 : 285689 : return new pass_phiopt (ctxt);
4509 : : }
4510 : :
4511 : : unsigned int
4512 : 5636698 : pass_phiopt::execute (function *)
4513 : : {
4514 : 5636698 : bool do_hoist_loads = !early_p ? gate_hoist_loads () : false;
4515 : 5636698 : bool cfgchanged = false;
4516 : :
4517 : 5636698 : calculate_dominance_info (CDI_DOMINATORS);
4518 : 5636698 : mark_ssa_maybe_undefs ();
4519 : :
4520 : 9049862 : auto phiopt_exec = [&] (basic_block bb, basic_block bb1,
4521 : : basic_block bb2, edge e1, edge e2,
4522 : : bool diamond_p, gcond *cond_stmt)
4523 : : {
4524 : 3413164 : if (diamond_p)
4525 : : {
4526 : 1064812 : basic_block bb3 = e1->dest;
4527 : :
4528 : 1064812 : if (!single_pred_p (bb1)
4529 : 2064230 : || !single_pred_p (bb2))
4530 : 2575142 : return;
4531 : :
4532 : 930218 : if (do_hoist_loads
4533 : 734228 : && !FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (cond_stmt)))
4534 : 727224 : && EDGE_COUNT (bb->succs) == 2
4535 : 727224 : && EDGE_COUNT (bb3->preds) == 2
4536 : : /* If one edge or the other is dominant, a conditional move
4537 : : is likely to perform worse than the well-predicted branch. */
4538 : 577836 : && !predictable_edge_p (EDGE_SUCC (bb, 0))
4539 : 1503829 : && !predictable_edge_p (EDGE_SUCC (bb, 1)))
4540 : 573611 : hoist_adjacent_loads (bb, bb1, bb2, bb3);
4541 : :
4542 : : /* Try to see if there are only one store in each side of the if
4543 : : and try to remove that. */
4544 : 930218 : if (EDGE_COUNT (bb3->preds) == 2)
4545 : 734191 : cond_if_else_store_replacement_limited (bb1, bb2, bb3);
4546 : : }
4547 : :
4548 : 930218 : gimple_stmt_iterator gsi;
4549 : :
4550 : : /* Check that we're looking for nested phis. */
4551 : 930218 : basic_block merge = diamond_p ? EDGE_SUCC (bb2, 0)->dest : bb2;
4552 : 3278570 : gimple_seq phis = phi_nodes (merge);
4553 : :
4554 : 3278570 : if (gimple_seq_empty_p (phis))
4555 : : return;
4556 : :
4557 : : /* Factor out operations from the phi if possible. */
4558 : 3264494 : if (single_pred_p (bb1)
4559 : 5682524 : && EDGE_COUNT (merge->preds) == 2)
4560 : : {
4561 : 5398250 : for (gsi = gsi_start (phis); !gsi_end_p (gsi); )
4562 : : {
4563 : 2980220 : gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
4564 : :
4565 : 2980220 : if (factor_out_conditional_operation (e1, e2, merge, phi,
4566 : : cond_stmt))
4567 : : {
4568 : : /* Start over if there was an operation that was factored out because the new phi might have another opportunity. */
4569 : 26189 : phis = phi_nodes (merge);
4570 : 5424439 : gsi = gsi_start (phis);
4571 : : }
4572 : : else
4573 : 2954031 : gsi_next (&gsi);
4574 : : }
4575 : : }
4576 : :
4577 : : /* Value replacement can work with more than one PHI
4578 : : so try that first. */
4579 : 3264494 : if (!early_p && !diamond_p)
4580 : 4178116 : for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
4581 : : {
4582 : 2444288 : gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
4583 : 2444288 : tree arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
4584 : 2444288 : tree arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
4585 : 2444288 : if (value_replacement (bb, bb1, e1, e2, phi, arg0, arg1) == 2)
4586 : : {
4587 : 1442 : cfgchanged = true;
4588 : 1442 : return;
4589 : : }
4590 : : }
4591 : :
4592 : 3263052 : gphi *phi = single_non_singleton_phi_for_edges (phis, e1, e2);
4593 : 3263052 : if (!phi)
4594 : : return;
4595 : :
4596 : 838022 : tree arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
4597 : 838022 : tree arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
4598 : :
4599 : : /* Something is wrong if we cannot find the arguments in the PHI
4600 : : node. */
4601 : 838022 : gcc_assert (arg0 != NULL_TREE && arg1 != NULL_TREE);
4602 : :
4603 : :
4604 : : /* Do the replacement of conditional if it can be done. */
4605 : 838022 : if (match_simplify_replacement (bb, bb1, bb2, e1, e2, phi,
4606 : 838022 : arg0, arg1, early_p, diamond_p))
4607 : 84241 : cfgchanged = true;
4608 : 753781 : else if (!early_p
4609 : 499295 : && !diamond_p
4610 : 465818 : && single_pred_p (bb1)
4611 : 1203421 : && cond_removal_in_builtin_zero_pattern (bb, bb1, e1, e2,
4612 : : phi, arg0, arg1))
4613 : 13 : cfgchanged = true;
4614 : 753768 : else if (minmax_replacement (bb, bb1, bb2, e1, e2, phi, arg0, arg1,
4615 : : diamond_p))
4616 : 3876 : cfgchanged = true;
4617 : 1587914 : else if (single_pred_p (bb1)
4618 : 704641 : && !diamond_p
4619 : 1394138 : && spaceship_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
4620 : 2077 : cfgchanged = true;
4621 : 5636698 : };
4622 : :
4623 : 5636698 : execute_over_cond_phis (phiopt_exec);
4624 : :
4625 : 5636698 : if (cfgchanged)
4626 : 63360 : return TODO_cleanup_cfg;
4627 : : return 0;
4628 : : }
4629 : :
4630 : : /* This pass tries to transform conditional stores into unconditional
4631 : : ones, enabling further simplifications with the simpler then and else
4632 : : blocks. In particular it replaces this:
4633 : :
4634 : : bb0:
4635 : : if (cond) goto bb2; else goto bb1;
4636 : : bb1:
4637 : : *p = RHS;
4638 : : bb2:
4639 : :
4640 : : with
4641 : :
4642 : : bb0:
4643 : : if (cond) goto bb1; else goto bb2;
4644 : : bb1:
4645 : : condtmp' = *p;
4646 : : bb2:
4647 : : condtmp = PHI <RHS, condtmp'>
4648 : : *p = condtmp;
4649 : :
4650 : : This transformation can only be done under several constraints,
4651 : : documented below. It also replaces:
4652 : :
4653 : : bb0:
4654 : : if (cond) goto bb2; else goto bb1;
4655 : : bb1:
4656 : : *p = RHS1;
4657 : : goto bb3;
4658 : : bb2:
4659 : : *p = RHS2;
4660 : : bb3:
4661 : :
4662 : : with
4663 : :
4664 : : bb0:
4665 : : if (cond) goto bb3; else goto bb1;
4666 : : bb1:
4667 : : bb3:
4668 : : condtmp = PHI <RHS1, RHS2>
4669 : : *p = condtmp; */
4670 : :
4671 : : namespace {
4672 : :
4673 : : const pass_data pass_data_cselim =
4674 : : {
4675 : : GIMPLE_PASS, /* type */
4676 : : "cselim", /* name */
4677 : : OPTGROUP_NONE, /* optinfo_flags */
4678 : : TV_TREE_PHIOPT, /* tv_id */
4679 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
4680 : : 0, /* properties_provided */
4681 : : 0, /* properties_destroyed */
4682 : : 0, /* todo_flags_start */
4683 : : 0, /* todo_flags_finish */
4684 : : };
4685 : :
4686 : : class pass_cselim : public gimple_opt_pass
4687 : : {
4688 : : public:
4689 : 285689 : pass_cselim (gcc::context *ctxt)
4690 : 571378 : : gimple_opt_pass (pass_data_cselim, ctxt)
4691 : : {}
4692 : :
4693 : : /* opt_pass methods: */
4694 : 1042778 : bool gate (function *) final override { return flag_tree_cselim; }
4695 : : unsigned int execute (function *) final override;
4696 : :
4697 : : }; // class pass_cselim
4698 : :
4699 : : } // anon namespace
4700 : :
4701 : : gimple_opt_pass *
4702 : 285689 : make_pass_cselim (gcc::context *ctxt)
4703 : : {
4704 : 285689 : return new pass_cselim (ctxt);
4705 : : }
4706 : :
4707 : : unsigned int
4708 : 1042696 : pass_cselim::execute (function *)
4709 : : {
4710 : 1042696 : bool cfgchanged = false;
4711 : 1042696 : hash_set<tree> *nontrap = 0;
4712 : 1042696 : unsigned todo = 0;
4713 : :
4714 : : /* ??? We are not interested in loop related info, but the following
4715 : : will create it, ICEing as we didn't init loops with pre-headers.
4716 : : An interfacing issue of find_data_references_in_bb. */
4717 : 1042696 : loop_optimizer_init (LOOPS_NORMAL);
4718 : 1042696 : scev_initialize ();
4719 : :
4720 : 1042696 : calculate_dominance_info (CDI_DOMINATORS);
4721 : :
4722 : : /* Calculate the set of non-trapping memory accesses. */
4723 : 1042696 : nontrap = get_non_trapping ();
4724 : :
4725 : 1944507 : auto cselim_exec = [&] (basic_block bb, basic_block bb1,
4726 : : basic_block bb2, edge e1, edge e2,
4727 : : bool diamond_p, gcond *)
4728 : : {
4729 : 901811 : if (diamond_p)
4730 : : {
4731 : 296257 : basic_block bb3 = e1->dest;
4732 : :
4733 : : /* Only handle sinking of store from 2 bbs only,
4734 : : The middle bbs don't need to come from the
4735 : : if always since we are sinking rather than
4736 : : hoisting. */
4737 : 296257 : if (EDGE_COUNT (bb3->preds) != 2)
4738 : : return;
4739 : 223659 : if (cond_if_else_store_replacement (bb1, bb2, bb3))
4740 : 8312 : cfgchanged = true;
4741 : 223659 : return;
4742 : : }
4743 : :
4744 : : /* Also make sure that bb1 only have one predecessor and that it
4745 : : is bb. */
4746 : 605554 : if (!single_pred_p (bb1)
4747 : 1167095 : || single_pred (bb1) != bb)
4748 : : return;
4749 : :
4750 : : /* bb1 is the middle block, bb2 the join block, bb the split block,
4751 : : e1 the fallthrough edge from bb1 to bb2. We can't do the
4752 : : optimization if the join block has more than two predecessors. */
4753 : 561541 : if (EDGE_COUNT (bb2->preds) > 2)
4754 : : return;
4755 : 424887 : if (cond_store_replacement (bb1, bb2, e1, e2, nontrap))
4756 : 5931 : cfgchanged = true;
4757 : 1042696 : };
4758 : :
4759 : 1042696 : execute_over_cond_phis (cselim_exec);
4760 : :
4761 : 2085392 : delete nontrap;
4762 : : /* If the CFG has changed, we should cleanup the CFG. */
4763 : 1042696 : if (cfgchanged)
4764 : : {
4765 : : /* In cond-store replacement we have added some loads on edges
4766 : : and new VOPS (as we moved the store, and created a load). */
4767 : 8722 : gsi_commit_edge_inserts ();
4768 : 8722 : todo = TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
4769 : : }
4770 : 1042696 : scev_finalize ();
4771 : 1042696 : loop_optimizer_finalize ();
4772 : 1042696 : return todo;
4773 : : }
|