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