Line data Source code
1 : /* SSA Dominator optimizations for trees
2 : Copyright (C) 2001-2026 Free Software Foundation, Inc.
3 : Contributed by Diego Novillo <dnovillo@redhat.com>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "tree-pass.h"
28 : #include "ssa.h"
29 : #include "gimple-pretty-print.h"
30 : #include "fold-const.h"
31 : #include "cfganal.h"
32 : #include "cfgloop.h"
33 : #include "gimple-iterator.h"
34 : #include "gimple-fold.h"
35 : #include "tree-eh.h"
36 : #include "tree-inline.h"
37 : #include "tree-cfg.h"
38 : #include "tree-into-ssa.h"
39 : #include "domwalk.h"
40 : #include "tree-ssa-propagate.h"
41 : #include "tree-ssa-threadupdate.h"
42 : #include "tree-ssa-scopedtables.h"
43 : #include "tree-ssa-threadedge.h"
44 : #include "tree-ssa-dom.h"
45 : #include "tree-cfgcleanup.h"
46 : #include "dbgcnt.h"
47 : #include "alloc-pool.h"
48 : #include "tree-vrp.h"
49 : #include "vr-values.h"
50 : #include "gimple-range.h"
51 : #include "gimple-range-path.h"
52 : #include "alias.h"
53 :
54 : /* This file implements optimizations on the dominator tree. */
55 :
56 : /* Structure for recording edge equivalences.
57 :
58 : Computing and storing the edge equivalences instead of creating
59 : them on-demand can save significant amounts of time, particularly
60 : for pathological cases involving switch statements.
61 :
62 : These structures live for a single iteration of the dominator
63 : optimizer in the edge's AUX field. At the end of an iteration we
64 : free each of these structures. */
65 : class edge_info
66 : {
67 : public:
68 : typedef std::pair <tree, tree> equiv_pair;
69 : edge_info (edge);
70 : ~edge_info ();
71 :
72 : /* Record a simple LHS = RHS equivalence. This may trigger
73 : calls to derive_equivalences. */
74 : void record_simple_equiv (tree, tree);
75 :
76 : /* If traversing this edge creates simple equivalences, we store
77 : them as LHS/RHS pairs within this vector. */
78 : vec<equiv_pair> simple_equivalences;
79 :
80 : /* Traversing an edge may also indicate one or more particular conditions
81 : are true or false. */
82 : vec<cond_equivalence> cond_equivalences;
83 :
84 : private:
85 : /* Derive equivalences by walking the use-def chains. */
86 : void derive_equivalences (tree, tree, int);
87 : };
88 :
89 : /* Track whether or not we have changed the control flow graph. */
90 : static bool cfg_altered;
91 :
92 : /* Bitmap of blocks that have had EH statements cleaned. We should
93 : remove their dead edges eventually. */
94 : static bitmap need_eh_cleanup;
95 : static vec<gimple *> need_noreturn_fixup;
96 :
97 : /* Bitmap of SSA names that have no use in an assignment or call that
98 : defines an SSA name, so back_propagate_equivalences has nothing to do
99 : for them. A bit is cleared when DOM adds such a use. */
100 : static bitmap names_without_foldable_uses;
101 :
102 : /* Statistics for dominator optimizations. */
103 : struct opt_stats_d
104 : {
105 : long num_stmts;
106 : long num_exprs_considered;
107 : long num_re;
108 : long num_const_prop;
109 : long num_copy_prop;
110 : };
111 :
112 : static struct opt_stats_d opt_stats;
113 :
114 : /* Local functions. */
115 : static void record_equality (tree, tree, class const_and_copies *);
116 : static void record_equivalences_from_phis (basic_block);
117 : static void dump_dominator_optimization_stats (FILE *file,
118 : hash_table<expr_elt_hasher> *);
119 : static void record_temporary_equivalences (edge, class const_and_copies *,
120 : class avail_exprs_stack *, bitmap);
121 :
122 : /* Constructor for EDGE_INFO. An EDGE_INFO instance is always
123 : associated with an edge E. */
124 :
125 36916107 : edge_info::edge_info (edge e)
126 : {
127 : /* Free the old one associated with E, if it exists and
128 : associate our new object with E. */
129 36916107 : free_dom_edge_info (e);
130 36916107 : e->aux = this;
131 :
132 : /* And initialize the embedded vectors. */
133 36916107 : simple_equivalences = vNULL;
134 36916107 : cond_equivalences = vNULL;
135 36916107 : }
136 :
137 : /* Destructor just needs to release the vectors. */
138 :
139 36916107 : edge_info::~edge_info (void)
140 : {
141 36916107 : this->cond_equivalences.release ();
142 36916107 : this->simple_equivalences.release ();
143 36916107 : }
144 :
145 : /* NAME is known to have the value VALUE, which must be a constant.
146 :
147 : Walk through its use-def chain to see if there are other equivalences
148 : we might be able to derive.
149 :
150 : RECURSION_LIMIT controls how far back we recurse through the use-def
151 : chains. */
152 :
153 : void
154 13516029 : edge_info::derive_equivalences (tree name, tree value, int recursion_limit)
155 : {
156 15685673 : if (TREE_CODE (name) != SSA_NAME || TREE_CODE (value) != INTEGER_CST)
157 : return;
158 :
159 : /* This records the equivalence for the toplevel object. Do
160 : this before checking the recursion limit. */
161 15685361 : simple_equivalences.safe_push (equiv_pair (name, value));
162 :
163 : /* Limit how far up the use-def chains we are willing to walk. */
164 15685361 : if (recursion_limit == 0)
165 : return;
166 :
167 : /* We can walk up the use-def chains to potentially find more
168 : equivalences. */
169 15671917 : auto *def_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (name));
170 10246850 : if (def_stmt)
171 : {
172 10246850 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
173 10246850 : switch (code)
174 : {
175 : /* If the result of an OR is zero, then its operands are, too. */
176 590924 : case BIT_IOR_EXPR:
177 590924 : if (integer_zerop (value))
178 : {
179 324994 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
180 324994 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
181 :
182 324994 : value = build_zero_cst (TREE_TYPE (rhs1));
183 324994 : derive_equivalences (rhs1, value, recursion_limit - 1);
184 324994 : value = build_zero_cst (TREE_TYPE (rhs2));
185 324994 : derive_equivalences (rhs2, value, recursion_limit - 1);
186 : }
187 : break;
188 :
189 : /* If the result of an AND is nonzero, then its operands are, too. */
190 1105801 : case BIT_AND_EXPR:
191 1105801 : if (!integer_zerop (value))
192 : {
193 470944 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
194 470944 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
195 :
196 : /* If either operand has a boolean range, then we
197 : know its value must be one, otherwise we just know it
198 : is nonzero. The former is clearly useful, I haven't
199 : seen cases where the latter is helpful yet. */
200 470944 : if (TREE_CODE (rhs1) == SSA_NAME)
201 : {
202 470944 : if (ssa_name_has_boolean_range (rhs1))
203 : {
204 296768 : value = build_one_cst (TREE_TYPE (rhs1));
205 296768 : derive_equivalences (rhs1, value, recursion_limit - 1);
206 : }
207 : }
208 470944 : if (TREE_CODE (rhs2) == SSA_NAME)
209 : {
210 299427 : if (ssa_name_has_boolean_range (rhs2))
211 : {
212 296485 : value = build_one_cst (TREE_TYPE (rhs2));
213 296485 : derive_equivalences (rhs2, value, recursion_limit - 1);
214 : }
215 : }
216 : }
217 : break;
218 :
219 : /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
220 : set via a widening type conversion, then we may be able to record
221 : additional equivalences. */
222 449964 : CASE_CONVERT:
223 449964 : {
224 449964 : tree rhs = gimple_assign_rhs1 (def_stmt);
225 449964 : tree rhs_type = TREE_TYPE (rhs);
226 449964 : if (INTEGRAL_TYPE_P (rhs_type)
227 447005 : && (TYPE_PRECISION (TREE_TYPE (name))
228 447005 : >= TYPE_PRECISION (rhs_type))
229 696769 : && int_fits_type_p (value, rhs_type))
230 236288 : derive_equivalences (rhs,
231 : fold_convert (rhs_type, value),
232 : recursion_limit - 1);
233 : break;
234 : }
235 :
236 : /* We can invert the operation of these codes trivially if
237 : one of the RHS operands is a constant to produce a known
238 : value for the other RHS operand. */
239 1080178 : case POINTER_PLUS_EXPR:
240 1080178 : case PLUS_EXPR:
241 1080178 : {
242 1080178 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
243 1080178 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
244 :
245 : /* If the second argument is a constant, then we can compute
246 : a constant value for the first one. */
247 1080178 : if (TREE_CODE (rhs2) == INTEGER_CST
248 1023867 : && TREE_CODE (rhs1) == SSA_NAME)
249 1023865 : derive_equivalences (rhs1,
250 1023865 : fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
251 : value, rhs2),
252 : recursion_limit - 1);
253 : break;
254 : }
255 :
256 : /* If one of the operands is a constant, then we can compute
257 : the value of the other operand. If both operands are
258 : SSA_NAMEs, then they must be equal if the result is zero. */
259 76987 : case MINUS_EXPR:
260 76987 : {
261 76987 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
262 76987 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
263 :
264 : /* If either argument is a constant, then we can compute
265 : a constant value for the nonconstant argument. */
266 76987 : if (TREE_CODE (rhs1) == INTEGER_CST
267 642 : && TREE_CODE (rhs2) == SSA_NAME)
268 642 : derive_equivalences (rhs2,
269 642 : fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
270 : rhs1, value),
271 : recursion_limit - 1);
272 76345 : else if (TREE_CODE (rhs2) == INTEGER_CST
273 31264 : && TREE_CODE (rhs1) == SSA_NAME)
274 31264 : derive_equivalences (rhs1,
275 31264 : fold_binary (PLUS_EXPR, TREE_TYPE (rhs1),
276 : value, rhs2),
277 : recursion_limit - 1);
278 45081 : else if (integer_zerop (value))
279 : {
280 32896 : tree cond = build2 (EQ_EXPR, boolean_type_node,
281 : gimple_assign_rhs1 (def_stmt),
282 : gimple_assign_rhs2 (def_stmt));
283 16448 : tree inverted = invert_truthvalue (cond);
284 16448 : record_conditions (&this->cond_equivalences, cond, inverted);
285 : }
286 : break;
287 : }
288 :
289 750495 : case EQ_EXPR:
290 750495 : case NE_EXPR:
291 750495 : {
292 352963 : if ((code == EQ_EXPR && integer_onep (value))
293 892553 : || (code == NE_EXPR && integer_zerop (value)))
294 : {
295 466468 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
296 466468 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
297 :
298 : /* If the second argument is a constant, then record the
299 : first argument as being the same as that constant.
300 :
301 : If neither operand is a constant, then we have a
302 : conditional name == name equivalence. */
303 466468 : if (TREE_CODE (rhs2) == INTEGER_CST)
304 241707 : derive_equivalences (rhs1, rhs2, recursion_limit - 1);
305 : }
306 : else
307 : {
308 568054 : tree cond = build2 (code, boolean_type_node,
309 : gimple_assign_rhs1 (def_stmt),
310 284027 : gimple_assign_rhs2 (def_stmt));
311 284027 : tree inverted = invert_truthvalue (cond);
312 284027 : if (integer_zerop (value))
313 142058 : std::swap (cond, inverted);
314 284027 : record_conditions (&this->cond_equivalences, cond, inverted);
315 : }
316 : break;
317 : }
318 :
319 : /* For BIT_NOT and NEGATE, we can just apply the operation to the
320 : VALUE to get the new equivalence. It will always be a constant
321 : so we can recurse. */
322 14399 : case BIT_NOT_EXPR:
323 14399 : case NEGATE_EXPR:
324 14399 : {
325 14399 : tree rhs = gimple_assign_rhs1 (def_stmt);
326 14399 : tree res;
327 : /* If this is a NOT and the operand has a boolean range, then we
328 : know its value must be zero or one. We are not supposed to
329 : have a BIT_NOT_EXPR for boolean types with precision > 1 in
330 : the general case, see e.g. the handling of TRUTH_NOT_EXPR in
331 : the gimplifier, but it can be generated by match.pd out of
332 : a BIT_XOR_EXPR wrapped in a BIT_AND_EXPR. Now the handling
333 : of BIT_AND_EXPR above already forces a specific semantics for
334 : boolean types with precision > 1 so we must do the same here,
335 : otherwise we could change the semantics of TRUTH_NOT_EXPR for
336 : boolean types with precision > 1. */
337 14399 : if (code == BIT_NOT_EXPR
338 14158 : && TREE_CODE (rhs) == SSA_NAME
339 28557 : && ssa_name_has_boolean_range (rhs))
340 : {
341 13704 : if ((TREE_INT_CST_LOW (value) & 1) == 0)
342 6271 : res = build_one_cst (TREE_TYPE (rhs));
343 : else
344 7433 : res = build_zero_cst (TREE_TYPE (rhs));
345 : }
346 : else
347 695 : res = fold_build1 (code, TREE_TYPE (rhs), value);
348 14399 : derive_equivalences (rhs, res, recursion_limit - 1);
349 14399 : break;
350 : }
351 :
352 6178102 : default:
353 6178102 : {
354 6178102 : if (TREE_CODE_CLASS (code) == tcc_comparison)
355 : {
356 1039524 : tree cond = build2 (code, boolean_type_node,
357 : gimple_assign_rhs1 (def_stmt),
358 519762 : gimple_assign_rhs2 (def_stmt));
359 519762 : tree inverted = invert_truthvalue (cond);
360 519762 : if (integer_zerop (value))
361 219100 : std::swap (cond, inverted);
362 519762 : record_conditions (&this->cond_equivalences, cond, inverted);
363 519762 : break;
364 : }
365 : break;
366 : }
367 : }
368 : }
369 : }
370 :
371 : void
372 16192204 : edge_info::record_simple_equiv (tree lhs, tree rhs)
373 : {
374 : /* If the RHS is a constant, then we may be able to derive
375 : further equivalences. Else just record the name = name
376 : equivalence. */
377 16192204 : if (TREE_CODE (rhs) == INTEGER_CST)
378 12894267 : derive_equivalences (lhs, rhs, 4);
379 : else
380 3297937 : simple_equivalences.safe_push (equiv_pair (lhs, rhs));
381 16192204 : }
382 :
383 : /* Free the edge_info data attached to E, if it exists and
384 : clear e->aux. */
385 :
386 : void
387 133909142 : free_dom_edge_info (edge e)
388 : {
389 133909142 : class edge_info *edge_info = (class edge_info *)e->aux;
390 :
391 133909142 : if (edge_info)
392 36916107 : delete edge_info;
393 133909142 : e->aux = NULL;
394 133909142 : }
395 :
396 : /* Free all EDGE_INFO structures associated with edges in the CFG.
397 : If a particular edge can be threaded, copy the redirection
398 : target from the EDGE_INFO structure into the edge's AUX field
399 : as required by code to update the CFG and SSA graph for
400 : jump threading. */
401 :
402 : static void
403 2136819 : free_all_edge_infos (void)
404 : {
405 2136819 : basic_block bb;
406 2136819 : edge_iterator ei;
407 2136819 : edge e;
408 :
409 24142418 : FOR_EACH_BB_FN (bb, cfun)
410 : {
411 53098322 : FOR_EACH_EDGE (e, ei, bb->preds)
412 31092723 : free_dom_edge_info (e);
413 : }
414 2136819 : }
415 :
416 : /* We have finished optimizing BB, record any information implied by
417 : taking a specific outgoing edge from BB. */
418 :
419 : static void
420 46067881 : record_edge_info (basic_block bb)
421 : {
422 46067881 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
423 46067881 : class edge_info *edge_info;
424 :
425 : /* Free all the outgoing edge info data associated with
426 : BB's outgoing edges. */
427 46067881 : edge e;
428 46067881 : edge_iterator ei;
429 110200081 : FOR_EACH_EDGE (e, ei, bb->succs)
430 64132200 : free_dom_edge_info (e);
431 :
432 46067881 : if (! gsi_end_p (gsi))
433 : {
434 39702163 : gimple *stmt = gsi_stmt (gsi);
435 39702163 : location_t loc = gimple_location (stmt);
436 :
437 39702163 : if (gimple_code (stmt) == GIMPLE_SWITCH)
438 : {
439 118649 : gswitch *switch_stmt = as_a <gswitch *> (stmt);
440 118649 : tree index = gimple_switch_index (switch_stmt);
441 :
442 118649 : if (TREE_CODE (index) == SSA_NAME)
443 : {
444 118615 : int i;
445 118615 : int n_labels = gimple_switch_num_labels (switch_stmt);
446 : /* info contains NULL, error_mark_node or a value.
447 : error_mark signifies there are multiple values already.
448 : A NULL signifies there it is uninitialized.
449 : A value signifies that is the only value on that edge
450 : to that bb. */
451 118615 : tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
452 :
453 983021 : for (i = 0; i < n_labels; i++)
454 : {
455 745791 : tree label = gimple_switch_label (switch_stmt, i);
456 745791 : basic_block target_bb
457 745791 : = label_to_block (cfun, CASE_LABEL (label));
458 : /* The default case is a case with multiple values.
459 : If the value is already set then it has multiple values. */
460 745791 : if (CASE_HIGH (label)
461 707547 : || !CASE_LOW (label)
462 1334723 : || info[target_bb->index])
463 242624 : info[target_bb->index] = error_mark_node;
464 : else
465 : /* Record the one value that can be on that edge to the
466 : target_bb. */
467 503167 : info[target_bb->index] = CASE_LOW (label);
468 : }
469 :
470 767651 : FOR_EACH_EDGE (e, ei, bb->succs)
471 : {
472 649036 : basic_block target_bb = e->dest;
473 649036 : tree value = info[target_bb->index];
474 :
475 649036 : if (value != NULL && value != error_mark_node)
476 : {
477 468141 : tree x = fold_convert_loc (loc, TREE_TYPE (index),
478 : value);
479 468141 : edge_info = new class edge_info (e);
480 468141 : edge_info->record_simple_equiv (index, x);
481 : }
482 : }
483 118615 : free (info);
484 : }
485 : }
486 :
487 : /* A COND_EXPR may create equivalences too. */
488 39702163 : if (gimple_code (stmt) == GIMPLE_COND)
489 : {
490 18405853 : edge true_edge;
491 18405853 : edge false_edge;
492 :
493 18405853 : tree op0 = gimple_cond_lhs (stmt);
494 18405853 : tree op1 = gimple_cond_rhs (stmt);
495 18405853 : enum tree_code code = gimple_cond_code (stmt);
496 :
497 18405853 : extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
498 :
499 : /* Special case comparing booleans against a constant as we
500 : know the value of OP0 on both arms of the branch. i.e., we
501 : can record an equivalence for OP0 rather than COND.
502 :
503 : However, don't do this if the constant isn't zero or one.
504 : Such conditionals will get optimized more thoroughly during
505 : the domwalk. */
506 18405853 : if ((code == EQ_EXPR || code == NE_EXPR)
507 14116735 : && TREE_CODE (op0) == SSA_NAME
508 13935395 : && ssa_name_has_boolean_range (op0)
509 2065056 : && is_gimple_min_invariant (op1)
510 20405265 : && (integer_zerop (op1) || integer_onep (op1)))
511 : {
512 1999412 : tree true_val = constant_boolean_node (true, TREE_TYPE (op0));
513 1999412 : tree false_val = constant_boolean_node (false, TREE_TYPE (op0));
514 :
515 1999412 : if (code == EQ_EXPR)
516 : {
517 78464 : edge_info = new class edge_info (true_edge);
518 117245 : edge_info->record_simple_equiv (op0,
519 78464 : (integer_zerop (op1)
520 : ? false_val : true_val));
521 78464 : edge_info = new class edge_info (false_edge);
522 117245 : edge_info->record_simple_equiv (op0,
523 78464 : (integer_zerop (op1)
524 : ? true_val : false_val));
525 : }
526 : else
527 : {
528 1920948 : edge_info = new class edge_info (true_edge);
529 1924381 : edge_info->record_simple_equiv (op0,
530 1920948 : (integer_zerop (op1)
531 : ? true_val : false_val));
532 1920948 : edge_info = new class edge_info (false_edge);
533 1924381 : edge_info->record_simple_equiv (op0,
534 1920948 : (integer_zerop (op1)
535 : ? false_val : true_val));
536 : }
537 : }
538 16406441 : else if (TREE_CODE (op0) == SSA_NAME
539 16406441 : && (TREE_CODE (op1) == SSA_NAME
540 12022123 : || is_gimple_min_invariant (op1)))
541 : {
542 16224571 : tree cond = build2 (code, boolean_type_node, op0, op1);
543 16224571 : tree inverted = invert_truthvalue_loc (loc, cond);
544 16224571 : bool can_infer_simple_equiv
545 16939038 : = !(HONOR_SIGNED_ZEROS (op1) && real_maybe_zerop (op1))
546 16632349 : && !DECIMAL_FLOAT_MODE_P (element_mode (TREE_TYPE (op1)));
547 16224571 : class edge_info *edge_info;
548 :
549 16224571 : edge_info = new class edge_info (true_edge);
550 16224571 : record_conditions (&edge_info->cond_equivalences, cond, inverted);
551 :
552 16224571 : if (can_infer_simple_equiv && code == EQ_EXPR)
553 5375796 : edge_info->record_simple_equiv (op0, op1);
554 :
555 16224571 : edge_info = new class edge_info (false_edge);
556 16224571 : record_conditions (&edge_info->cond_equivalences, inverted, cond);
557 :
558 16224571 : if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
559 6349443 : edge_info->record_simple_equiv (op0, op1);
560 : }
561 : }
562 : }
563 46067881 : }
564 :
565 : class dom_jt_state : public jt_state
566 : {
567 : public:
568 2136819 : dom_jt_state (const_and_copies *copies, avail_exprs_stack *avails)
569 2136819 : : m_copies (copies), m_avails (avails)
570 : {
571 2136819 : bitmap_tree_view (m_blocks_on_stack);
572 2136819 : }
573 17209679 : void push (edge e) override
574 : {
575 17209679 : m_copies->push_marker ();
576 17209679 : m_avails->push_marker ();
577 17209679 : jt_state::push (e);
578 17209679 : }
579 17209679 : void pop () override
580 : {
581 17209679 : m_copies->pop_to_marker ();
582 17209679 : m_avails->pop_to_marker ();
583 17209679 : jt_state::pop ();
584 17209679 : }
585 15923823 : void register_equivs_edge (edge e) override
586 : {
587 15923823 : record_temporary_equivalences (e, m_copies, m_avails, m_blocks_on_stack);
588 15923823 : }
589 : void register_equiv (tree dest, tree src, bool update) override;
590 66039280 : bitmap get_blocks_on_stack () { return m_blocks_on_stack; }
591 : private:
592 : const_and_copies *m_copies;
593 : avail_exprs_stack *m_avails;
594 : /* Set of blocks on the stack, to be used for medium-fast
595 : dominance queries in back_propagate_equivalences. */
596 : auto_bitmap m_blocks_on_stack;
597 : };
598 :
599 : void
600 21736610 : dom_jt_state::register_equiv (tree dest, tree src, bool)
601 : {
602 21736610 : m_copies->record_const_or_copy (dest, src);
603 21736610 : }
604 :
605 2136819 : class dom_jt_simplifier : public hybrid_jt_simplifier
606 : {
607 : public:
608 2136819 : dom_jt_simplifier (avail_exprs_stack *avails, gimple_ranger *ranger,
609 : path_range_query *query)
610 4273638 : : hybrid_jt_simplifier (ranger, query), m_avails (avails) { }
611 :
612 : private:
613 : tree simplify (gimple *, gimple *, basic_block, jt_state *) override;
614 : avail_exprs_stack *m_avails;
615 : };
616 :
617 : tree
618 32141221 : dom_jt_simplifier::simplify (gimple *stmt, gimple *within_stmt,
619 : basic_block bb, jt_state *state)
620 : {
621 : /* First see if the conditional is in the hash table. */
622 32141221 : tree cached_lhs = m_avails->lookup_avail_expr (stmt, false, true);
623 32141221 : if (cached_lhs)
624 : return cached_lhs;
625 :
626 : /* Otherwise call the ranger if possible. */
627 31301573 : if (state)
628 8795768 : return hybrid_jt_simplifier::simplify (stmt, within_stmt, bb, state);
629 :
630 : return NULL;
631 : }
632 :
633 4273638 : class dom_opt_dom_walker : public dom_walker
634 : {
635 : public:
636 2136819 : dom_opt_dom_walker (cdi_direction direction,
637 : jump_threader *threader,
638 : dom_jt_state *state,
639 : gimple_ranger *ranger,
640 : const_and_copies *const_and_copies,
641 : avail_exprs_stack *avail_exprs_stack)
642 4273638 : : dom_walker (direction, REACHABLE_BLOCKS)
643 : {
644 2136819 : m_ranger = ranger;
645 2136819 : m_state = state;
646 2136819 : m_const_and_copies = const_and_copies;
647 2136819 : m_avail_exprs_stack = avail_exprs_stack;
648 2136819 : m_threader = threader;
649 : }
650 :
651 : edge before_dom_children (basic_block) final override;
652 : void after_dom_children (basic_block) final override;
653 :
654 : private:
655 :
656 : /* Unwindable equivalences, both const/copy and expression varieties. */
657 : class const_and_copies *m_const_and_copies;
658 : class avail_exprs_stack *m_avail_exprs_stack;
659 :
660 : /* Optimize a single statement within a basic block using the
661 : various tables maintained by DOM. Returns the taken edge if
662 : the statement is a conditional with a statically determined
663 : value. */
664 : edge optimize_stmt (basic_block, gimple_stmt_iterator *, bool *);
665 :
666 : void set_global_ranges_from_unreachable_edges (basic_block);
667 :
668 : void fold_cond (gimple *);
669 : void record_equivalences_from_incoming_edge (basic_block);
670 : void eliminate_redundant_computations (gimple_stmt_iterator *);
671 : void record_equivalences_from_stmt (gassign *, int);
672 : tree const_or_copy_of_stmt (gimple *);
673 :
674 : jump_threader *m_threader;
675 : gimple_ranger *m_ranger;
676 : dom_jt_state *m_state;
677 : };
678 :
679 : /* Jump threading, redundancy elimination and const/copy propagation.
680 :
681 : This pass may expose new symbols that need to be renamed into SSA. For
682 : every new symbol exposed, its corresponding bit will be set in
683 : VARS_TO_RENAME. */
684 :
685 : namespace {
686 :
687 : const pass_data pass_data_dominator =
688 : {
689 : GIMPLE_PASS, /* type */
690 : "dom", /* name */
691 : OPTGROUP_NONE, /* optinfo_flags */
692 : TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
693 : ( PROP_cfg | PROP_ssa ), /* properties_required */
694 : 0, /* properties_provided */
695 : 0, /* properties_destroyed */
696 : 0, /* todo_flags_start */
697 : ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
698 : };
699 :
700 : class pass_dominator : public gimple_opt_pass
701 : {
702 : public:
703 908955 : pass_dominator (gcc::context *ctxt)
704 : : gimple_opt_pass (pass_data_dominator, ctxt),
705 1817910 : may_peel_loop_headers_p (false)
706 : {}
707 :
708 : /* opt_pass methods: */
709 605970 : opt_pass * clone () final override { return new pass_dominator (m_ctxt); }
710 908955 : void set_pass_param (unsigned int n, bool param) final override
711 : {
712 908955 : gcc_assert (n == 0);
713 908955 : may_peel_loop_headers_p = param;
714 908955 : }
715 2137643 : bool gate (function *) final override { return flag_tree_dom != 0; }
716 : unsigned int execute (function *) final override;
717 :
718 : private:
719 : /* This flag is used to prevent loops from being peeled repeatedly in jump
720 : threading; it will be removed once we preserve loop structures throughout
721 : the compilation -- we will be able to mark the affected loops directly in
722 : jump threading, and avoid peeling them next time. */
723 : bool may_peel_loop_headers_p;
724 : }; // class pass_dominator
725 :
726 : unsigned int
727 2136819 : pass_dominator::execute (function *fun)
728 : {
729 2136819 : memset (&opt_stats, 0, sizeof (opt_stats));
730 :
731 : /* Create our hash tables. */
732 2136819 : hash_table<expr_elt_hasher> *avail_exprs
733 2136819 : = new hash_table<expr_elt_hasher> (1024);
734 2136819 : class avail_exprs_stack *avail_exprs_stack
735 2136819 : = new class avail_exprs_stack (avail_exprs);
736 2136819 : class const_and_copies *const_and_copies = new class const_and_copies ();
737 2136819 : need_eh_cleanup = BITMAP_ALLOC (NULL);
738 2136819 : need_noreturn_fixup.create (0);
739 2136819 : names_without_foldable_uses = BITMAP_ALLOC (NULL);
740 2136819 : bitmap_tree_view (names_without_foldable_uses);
741 :
742 2136819 : calculate_dominance_info (CDI_DOMINATORS);
743 2136819 : cfg_altered = false;
744 :
745 : /* We need to know loop structures in order to avoid destroying them
746 : in jump threading. Note that we still can e.g. thread through loop
747 : headers to an exit edge, or through loop header to the loop body, assuming
748 : that we update the loop info.
749 :
750 : TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
751 : to several overly conservative bail-outs in jump threading, case
752 : gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
753 : missing. We should improve jump threading in future then
754 : LOOPS_HAVE_PREHEADERS won't be needed here. */
755 2136819 : loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES
756 : | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS);
757 :
758 : /* We need accurate information regarding back edges in the CFG
759 : for jump threading; this may include back edges that are not part of
760 : a single loop. */
761 2136819 : mark_dfs_back_edges ();
762 :
763 : /* We want to create the edge info structures before the dominator walk
764 : so that they'll be in place for the jump threader, particularly when
765 : threading through a join block.
766 :
767 : The conditions will be lazily updated with global equivalences as
768 : we reach them during the dominator walk. */
769 2136819 : basic_block bb;
770 24142418 : FOR_EACH_BB_FN (bb, fun)
771 22005599 : record_edge_info (bb);
772 :
773 : /* Recursively walk the dominator tree optimizing statements. */
774 2136819 : gimple_ranger *ranger = enable_ranger (fun);
775 2136819 : path_range_query path_query (*ranger);
776 2136819 : dom_jt_simplifier simplifier (avail_exprs_stack, ranger, &path_query);
777 2136819 : dom_jt_state state (const_and_copies, avail_exprs_stack);
778 2136819 : jump_threader threader (&simplifier, &state);
779 2136819 : dom_opt_dom_walker walker (CDI_DOMINATORS,
780 : &threader,
781 : &state,
782 : ranger,
783 : const_and_copies,
784 2136819 : avail_exprs_stack);
785 2136819 : walker.walk (fun->cfg->x_entry_block_ptr);
786 :
787 2136819 : ranger->export_global_ranges ();
788 2136819 : disable_ranger (fun);
789 :
790 : /* Look for blocks where we cleared EDGE_EXECUTABLE on an outgoing
791 : edge. When found, remove jump threads which contain any outgoing
792 : edge from the affected block. */
793 2136819 : if (cfg_altered)
794 : {
795 4802686 : FOR_EACH_BB_FN (bb, fun)
796 : {
797 4739271 : edge_iterator ei;
798 4739271 : edge e;
799 :
800 : /* First see if there are any edges without EDGE_EXECUTABLE
801 : set. */
802 4739271 : bool found = false;
803 11370120 : FOR_EACH_EDGE (e, ei, bb->succs)
804 : {
805 6861550 : if ((e->flags & EDGE_EXECUTABLE) == 0)
806 : {
807 : found = true;
808 : break;
809 : }
810 : }
811 :
812 : /* If there were any such edges found, then remove jump threads
813 : containing any edge leaving BB. */
814 4739271 : if (found)
815 673428 : FOR_EACH_EDGE (e, ei, bb->succs)
816 442727 : threader.remove_jump_threads_including (e);
817 : }
818 : }
819 :
820 2136819 : {
821 2136819 : gimple_stmt_iterator gsi;
822 2136819 : basic_block bb;
823 24142418 : FOR_EACH_BB_FN (bb, fun)
824 : {
825 226276357 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
826 182265159 : update_stmt_if_modified (gsi_stmt (gsi));
827 : }
828 : }
829 :
830 : /* If we exposed any new variables, go ahead and put them into
831 : SSA form now, before we handle jump threading. This simplifies
832 : interactions between rewriting of _DECL nodes into SSA form
833 : and rewriting SSA_NAME nodes into SSA form after block
834 : duplication and CFG manipulation. */
835 2136819 : update_ssa (TODO_update_ssa);
836 :
837 2136819 : free_all_edge_infos ();
838 :
839 : /* Thread jumps, creating duplicate blocks as needed. */
840 2136819 : cfg_altered |= threader.thread_through_all_blocks (may_peel_loop_headers_p);
841 :
842 2136819 : if (cfg_altered)
843 123389 : free_dominance_info (CDI_DOMINATORS);
844 :
845 : /* Removal of statements may make some EH edges dead. Purge
846 : such edges from the CFG as needed. */
847 2136819 : if (!bitmap_empty_p (need_eh_cleanup))
848 : {
849 559 : unsigned i;
850 559 : bitmap_iterator bi;
851 :
852 : /* Jump threading may have created forwarder blocks from blocks
853 : needing EH cleanup; the new successor of these blocks, which
854 : has inherited from the original block, needs the cleanup.
855 : Don't clear bits in the bitmap, as that can break the bitmap
856 : iterator. */
857 1803 : EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
858 : {
859 1244 : basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
860 1244 : if (bb == NULL)
861 0 : continue;
862 2567 : while (single_succ_p (bb)
863 1378 : && (single_succ_edge (bb)->flags
864 151 : & (EDGE_EH|EDGE_DFS_BACK)) == 0)
865 134 : bb = single_succ (bb);
866 1244 : if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
867 55 : continue;
868 1189 : if ((unsigned) bb->index != i)
869 59 : bitmap_set_bit (need_eh_cleanup, bb->index);
870 : }
871 :
872 559 : gimple_purge_all_dead_eh_edges (need_eh_cleanup);
873 559 : bitmap_clear (need_eh_cleanup);
874 : }
875 :
876 : /* Fixup stmts that became noreturn calls. This may require splitting
877 : blocks and thus isn't possible during the dominator walk or before
878 : jump threading finished. Do this in reverse order so we don't
879 : inadvertently remove a stmt we want to fixup by visiting a dominating
880 : now noreturn call first. */
881 2136825 : while (!need_noreturn_fixup.is_empty ())
882 : {
883 6 : gimple *stmt = need_noreturn_fixup.pop ();
884 : /* We might have elided the stmt and its BB above. */
885 6 : if (!gimple_bb (stmt))
886 1 : continue;
887 5 : if (dump_file && dump_flags & TDF_DETAILS)
888 : {
889 0 : fprintf (dump_file, "Fixing up noreturn call ");
890 0 : print_gimple_stmt (dump_file, stmt, 0);
891 0 : fprintf (dump_file, "\n");
892 : }
893 5 : fixup_noreturn_call (stmt);
894 : }
895 :
896 2136819 : statistics_counter_event (fun, "Redundant expressions eliminated",
897 2136819 : opt_stats.num_re);
898 2136819 : statistics_counter_event (fun, "Constants propagated",
899 2136819 : opt_stats.num_const_prop);
900 2136819 : statistics_counter_event (fun, "Copies propagated",
901 2136819 : opt_stats.num_copy_prop);
902 :
903 : /* Debugging dumps. */
904 2136819 : if (dump_file && (dump_flags & TDF_STATS))
905 36 : dump_dominator_optimization_stats (dump_file, avail_exprs);
906 :
907 2136819 : loop_optimizer_finalize ();
908 :
909 : /* Delete our main hashtable. */
910 2136819 : delete avail_exprs;
911 2136819 : avail_exprs = NULL;
912 :
913 : /* Free asserted bitmaps and stacks. */
914 2136819 : BITMAP_FREE (need_eh_cleanup);
915 2136819 : need_noreturn_fixup.release ();
916 2136819 : BITMAP_FREE (names_without_foldable_uses);
917 2136819 : delete avail_exprs_stack;
918 2136819 : delete const_and_copies;
919 :
920 2136819 : return 0;
921 2136819 : }
922 :
923 : } // anon namespace
924 :
925 : gimple_opt_pass *
926 302985 : make_pass_dominator (gcc::context *ctxt)
927 : {
928 302985 : return new pass_dominator (ctxt);
929 : }
930 :
931 : /* Valueize hook for gimple_fold_stmt_to_constant_1. */
932 :
933 : static tree
934 35321839 : dom_valueize (tree t)
935 : {
936 35321839 : if (TREE_CODE (t) == SSA_NAME)
937 : {
938 25337931 : tree tem = SSA_NAME_VALUE (t);
939 20087652 : if (tem)
940 3220679 : return tem;
941 : }
942 : return t;
943 : }
944 :
945 : /* We have just found an equivalence for LHS on an edge E.
946 : Look backwards to other uses of LHS and see if we can derive
947 : additional equivalences that are valid on edge E. */
948 : static void
949 12343953 : back_propagate_equivalences (tree lhs, edge e,
950 : class const_and_copies *const_and_copies,
951 : bitmap domby)
952 : {
953 12343953 : use_operand_p use_p;
954 12343953 : imm_use_iterator iter;
955 12343953 : basic_block dest = e->dest;
956 12343953 : bool domok = (dom_info_state (CDI_DOMINATORS) == DOM_OK);
957 12343953 : unsigned version = SSA_NAME_VERSION (lhs);
958 :
959 : /* If an earlier walk found no use of LHS that can produce an
960 : equivalence, there is nothing to do. With checking enabled, walk
961 : the uses anyway to verify that no such use has been added. */
962 12343953 : bool cached_p = bitmap_bit_p (names_without_foldable_uses, version);
963 12343953 : if (cached_p && !flag_checking)
964 49 : return;
965 :
966 : /* Iterate over the uses of LHS to see if any dominate E->dest.
967 : If so, they may create useful equivalences too.
968 :
969 : ??? If the code gets re-organized to a worklist to catch more
970 : indirect opportunities and it is made to handle PHIs then this
971 : should only consider use_stmts in basic-blocks we have already visited. */
972 12343904 : bool foldable_use_p = false;
973 51778675 : FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
974 : {
975 39434771 : gimple *use_stmt = USE_STMT (use_p);
976 :
977 : /* Filter out statements that can never produce a useful
978 : equivalence. gimple_fold_stmt_to_constant_1 does not fold
979 : PHI nodes. */
980 39434771 : if (gimple_code (use_stmt) == GIMPLE_PHI)
981 3154353 : continue;
982 36280418 : tree lhs2 = gimple_get_lhs (use_stmt);
983 36280418 : if (!lhs2 || TREE_CODE (lhs2) != SSA_NAME)
984 28764253 : continue;
985 :
986 7516165 : gcc_checking_assert (!cached_p);
987 7516165 : foldable_use_p = true;
988 :
989 : /* Often the use is in DEST, which we trivially know we can't use.
990 : This is cheaper than the dominator set tests below. */
991 7516165 : if (dest == gimple_bb (use_stmt))
992 110409 : continue;
993 :
994 7405756 : if (domok)
995 : {
996 4337002 : if (!dominated_by_p (CDI_DOMINATORS, dest, gimple_bb (use_stmt)))
997 2149423 : continue;
998 : }
999 : else
1000 : {
1001 : /* We can use the set of BBs on the stack from a domwalk
1002 : for a medium fast way to query dominance. Profiling
1003 : has shown non-fast query dominance tests here can be fairly
1004 : expensive. */
1005 : /* This tests if USE_STMT does not dominate DEST. */
1006 3068754 : if (!bitmap_bit_p (domby, gimple_bb (use_stmt)->index))
1007 2083168 : continue;
1008 : }
1009 :
1010 : /* At this point USE_STMT dominates DEST and may result in a
1011 : useful equivalence. Try to simplify its RHS to a constant
1012 : or SSA_NAME. */
1013 3173165 : tree res = gimple_fold_stmt_to_constant_1 (use_stmt, dom_valueize,
1014 : no_follow_ssa_edges);
1015 3173165 : if (res && (TREE_CODE (res) == SSA_NAME || is_gimple_min_invariant (res)))
1016 2498940 : record_equality (lhs2, res, const_and_copies);
1017 12343904 : }
1018 :
1019 12343904 : if (!foldable_use_p)
1020 8793316 : bitmap_set_bit (names_without_foldable_uses, version);
1021 : }
1022 :
1023 : /* Record into CONST_AND_COPIES and AVAIL_EXPRS_STACK any equivalences implied
1024 : by traversing edge E (which are cached in E->aux).
1025 :
1026 : Callers are responsible for managing the unwinding markers. */
1027 : static void
1028 33838539 : record_temporary_equivalences (edge e,
1029 : class const_and_copies *const_and_copies,
1030 : class avail_exprs_stack *avail_exprs_stack,
1031 : bitmap blocks_on_stack)
1032 : {
1033 33838539 : int i;
1034 33838539 : class edge_info *edge_info = (class edge_info *) e->aux;
1035 :
1036 : /* If we have info associated with this edge, record it into
1037 : our equivalence tables. */
1038 33838539 : if (edge_info)
1039 : {
1040 : cond_equivalence *eq;
1041 : /* If we have 0 = COND or 1 = COND equivalences, record them
1042 : into our expression hash tables. */
1043 67962609 : for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1044 43669130 : avail_exprs_stack->record_cond (eq);
1045 :
1046 : edge_info::equiv_pair *seq;
1047 36637432 : for (i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
1048 : {
1049 12343953 : tree lhs = seq->first;
1050 12343953 : if (!lhs || TREE_CODE (lhs) != SSA_NAME)
1051 0 : continue;
1052 :
1053 : /* Record the simple NAME = VALUE equivalence. */
1054 12343953 : tree rhs = seq->second;
1055 :
1056 : /* If this is a SSA_NAME = SSA_NAME equivalence and one operand is
1057 : cheaper to compute than the other, then set up the equivalence
1058 : such that we replace the expensive one with the cheap one.
1059 :
1060 : If they are the same cost to compute, then do not record
1061 : anything. */
1062 12343953 : if (TREE_CODE (lhs) == SSA_NAME && TREE_CODE (rhs) == SSA_NAME)
1063 : {
1064 1759414 : gimple *rhs_def = SSA_NAME_DEF_STMT (rhs);
1065 1759414 : int rhs_cost = estimate_num_insns (rhs_def, &eni_size_weights);
1066 :
1067 1759414 : gimple *lhs_def = SSA_NAME_DEF_STMT (lhs);
1068 1759414 : int lhs_cost = estimate_num_insns (lhs_def, &eni_size_weights);
1069 :
1070 1759414 : if (rhs_cost > lhs_cost)
1071 199060 : record_equality (rhs, lhs, const_and_copies);
1072 1560354 : else if (rhs_cost < lhs_cost)
1073 455406 : record_equality (lhs, rhs, const_and_copies);
1074 : }
1075 : else
1076 10584539 : record_equality (lhs, rhs, const_and_copies);
1077 :
1078 :
1079 : /* Any equivalence found for LHS may result in additional
1080 : equivalences for other uses of LHS that we have already
1081 : processed. */
1082 12343953 : back_propagate_equivalences (lhs, e, const_and_copies,
1083 : blocks_on_stack);
1084 : }
1085 : }
1086 33838539 : }
1087 :
1088 : /* PHI nodes can create equivalences too.
1089 :
1090 : Ignoring any alternatives which are the same as the result, if
1091 : all the alternatives are equal, then the PHI node creates an
1092 : equivalence. */
1093 :
1094 : static void
1095 24062282 : record_equivalences_from_phis (basic_block bb)
1096 : {
1097 24062282 : gphi_iterator gsi;
1098 :
1099 33610566 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
1100 : {
1101 9548284 : gphi *phi = gsi.phi ();
1102 :
1103 : /* We might eliminate the PHI, so advance GSI now. */
1104 9548284 : gsi_next (&gsi);
1105 :
1106 9548284 : tree lhs = gimple_phi_result (phi);
1107 9548284 : tree rhs = NULL;
1108 9548284 : size_t i;
1109 :
1110 17184031 : for (i = 0; i < gimple_phi_num_args (phi); i++)
1111 : {
1112 16087205 : tree t = gimple_phi_arg_def (phi, i);
1113 :
1114 : /* Ignore alternatives which are the same as our LHS. Since
1115 : LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1116 : can simply compare pointers. */
1117 16087205 : if (lhs == t)
1118 32135 : continue;
1119 :
1120 : /* If the associated edge is not marked as executable, then it
1121 : can be ignored. */
1122 16055070 : if ((gimple_phi_arg_edge (phi, i)->flags & EDGE_EXECUTABLE) == 0)
1123 125167 : continue;
1124 :
1125 15929903 : t = dom_valueize (t);
1126 :
1127 : /* If T is an SSA_NAME and its associated edge is a backedge,
1128 : then quit as we cannot utilize this equivalence. */
1129 15929903 : if (TREE_CODE (t) == SSA_NAME
1130 15929903 : && (gimple_phi_arg_edge (phi, i)->flags & EDGE_DFS_BACK))
1131 : break;
1132 :
1133 : /* If we have not processed an alternative yet, then set
1134 : RHS to this alternative. */
1135 12772500 : if (rhs == NULL)
1136 : rhs = t;
1137 : /* If we have processed an alternative (stored in RHS), then
1138 : see if it is equal to this one. If it isn't, then stop
1139 : the search. */
1140 5858073 : else if (! operand_equal_for_phi_arg_p (rhs, t))
1141 : break;
1142 : }
1143 :
1144 : /* If we had no interesting alternatives, then all the RHS alternatives
1145 : must have been the same as LHS. */
1146 9548284 : if (!rhs)
1147 2633857 : rhs = lhs;
1148 :
1149 : /* If we managed to iterate through each PHI alternative without
1150 : breaking out of the loop, then we have a PHI which may create
1151 : a useful equivalence. We do not need to record unwind data for
1152 : this, since this is a true assignment and not an equivalence
1153 : inferred from a comparison. All uses of this ssa name are dominated
1154 : by this assignment, so unwinding just costs time and space. */
1155 9548284 : if (i == gimple_phi_num_args (phi))
1156 : {
1157 1096826 : if (may_propagate_copy (lhs, rhs))
1158 599872 : set_ssa_name_value (lhs, rhs);
1159 993908 : else if (virtual_operand_p (lhs))
1160 : {
1161 496484 : gimple *use_stmt;
1162 496484 : imm_use_iterator iter;
1163 496484 : use_operand_p use_p;
1164 : /* For virtual operands we have to propagate into all uses as
1165 : otherwise we will create overlapping life-ranges. */
1166 1496893 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
1167 2067778 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1168 1033889 : SET_USE (use_p, rhs);
1169 496484 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1170 72 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
1171 496484 : gimple_stmt_iterator tmp_gsi = gsi_for_stmt (phi);
1172 496484 : remove_phi_node (&tmp_gsi, true);
1173 : }
1174 : }
1175 : }
1176 24062282 : }
1177 :
1178 : /* Return true if all uses of NAME are dominated by STMT or feed STMT
1179 : via a chain of single immediate uses. */
1180 :
1181 : static bool
1182 409375 : all_uses_feed_or_dominated_by_stmt (tree name, gimple *stmt)
1183 : {
1184 409375 : use_operand_p use_p, use2_p;
1185 409375 : imm_use_iterator iter;
1186 409375 : basic_block stmt_bb = gimple_bb (stmt);
1187 :
1188 1655900 : FOR_EACH_IMM_USE_FAST (use_p, iter, name)
1189 : {
1190 1466241 : gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
1191 2704152 : if (use_stmt == stmt
1192 1330712 : || is_gimple_debug (use_stmt)
1193 2191247 : || (gimple_bb (use_stmt) != stmt_bb
1194 562654 : && dominated_by_p (CDI_DOMINATORS,
1195 562654 : gimple_bb (use_stmt), stmt_bb)))
1196 1237911 : continue;
1197 : while (use_stmt != stmt
1198 432552 : && is_gimple_assign (use_stmt)
1199 380051 : && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
1200 807441 : && single_imm_use (gimple_assign_lhs (use_stmt),
1201 : &use2_p, &use_stmt2))
1202 212836 : use_stmt = use_stmt2;
1203 228330 : if (use_stmt != stmt)
1204 219716 : return false;
1205 219716 : }
1206 189659 : return true;
1207 : }
1208 :
1209 : /* Handle
1210 : _4 = x_3 & 31;
1211 : if (_4 != 0)
1212 : goto <bb 6>;
1213 : else
1214 : goto <bb 7>;
1215 : <bb 6>:
1216 : __builtin_unreachable ();
1217 : <bb 7>:
1218 :
1219 : If x_3 has no other immediate uses (checked by caller), var is the
1220 : x_3 var, we can clear low 5 bits from the non-zero bitmask. */
1221 :
1222 : static void
1223 169970 : maybe_set_nonzero_bits (edge e, tree var)
1224 : {
1225 169970 : basic_block cond_bb = e->src;
1226 339940 : gcond *cond = safe_dyn_cast <gcond *> (*gsi_last_bb (cond_bb));
1227 169970 : tree cst;
1228 :
1229 169970 : if (cond == NULL
1230 169970 : || gimple_cond_code (cond) != ((e->flags & EDGE_TRUE_VALUE)
1231 169970 : ? EQ_EXPR : NE_EXPR)
1232 52401 : || TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
1233 2039 : || !integer_zerop (gimple_cond_rhs (cond)))
1234 : return;
1235 :
1236 1970 : auto stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (gimple_cond_lhs (cond)));
1237 1953 : if (!stmt
1238 1953 : || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
1239 25 : || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
1240 : return;
1241 3 : if (gimple_assign_rhs1 (stmt) != var)
1242 : {
1243 2 : gimple *stmt2;
1244 :
1245 2 : if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
1246 : return;
1247 2 : stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
1248 2 : if (!gimple_assign_cast_p (stmt2)
1249 0 : || gimple_assign_rhs1 (stmt2) != var
1250 0 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
1251 2 : || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
1252 0 : != TYPE_PRECISION (TREE_TYPE (var))))
1253 : return;
1254 : }
1255 1 : cst = gimple_assign_rhs2 (stmt);
1256 1 : set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var),
1257 2 : wi::to_wide (cst)));
1258 : }
1259 :
1260 : /* Set global ranges that can be determined from the C->M edge:
1261 :
1262 : <bb C>:
1263 : ...
1264 : if (something)
1265 : goto <bb N>;
1266 : else
1267 : goto <bb M>;
1268 : <bb N>:
1269 : __builtin_unreachable ();
1270 : <bb M>:
1271 : */
1272 :
1273 : void
1274 24062282 : dom_opt_dom_walker::set_global_ranges_from_unreachable_edges (basic_block bb)
1275 : {
1276 24062282 : edge pred_e = single_pred_edge_ignoring_loop_edges (bb, false);
1277 24062282 : if (!pred_e)
1278 23835206 : return;
1279 :
1280 17909198 : gimple *stmt = *gsi_last_bb (pred_e->src);
1281 17909198 : if (!stmt
1282 15053327 : || gimple_code (stmt) != GIMPLE_COND
1283 30219981 : || !assert_unreachable_fallthru_edge_p (pred_e))
1284 : return;
1285 :
1286 : // Bail if the condition leading to the unreachable edge has 2 ssa-names.
1287 : // See PR 125501.
1288 229654 : if ((TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
1289 229654 : && TREE_CODE (gimple_cond_rhs (stmt)) == SSA_NAME))
1290 : return;
1291 :
1292 : // Exporting a global range due to a __builtin_unreachable may cause the
1293 : // next range query in the export list to terminate in GORI earlier than
1294 : // desired. The entire point is to bypass the need for the unreachable by
1295 : // setting globals, but we need to make sure all the ranges are correct first.
1296 : // Save the ranges as they are caluculated and export them at once.
1297 :
1298 227076 : struct range_pair
1299 : {
1300 : tree name;
1301 : vrange_storage *range;
1302 : };
1303 227076 : vrange_allocator allocator;
1304 227076 : auto_vec<range_pair, 8> ranges;
1305 :
1306 227076 : tree name;
1307 227076 : value_range r;
1308 636451 : FOR_EACH_GORI_EXPORT_NAME (m_ranger->gori_ssa (), pred_e->src, name)
1309 409375 : if (all_uses_feed_or_dominated_by_stmt (name, stmt)
1310 : // The condition must post-dominate the definition point.
1311 599034 : && (SSA_NAME_IS_DEFAULT_DEF (name)
1312 189524 : || (gimple_bb (SSA_NAME_DEF_STMT (name))
1313 189524 : == pred_e->src)))
1314 : {
1315 183074 : r.set_range_class (TREE_TYPE (name));
1316 183074 : if (m_ranger->range_on_edge (r, pred_e, name)
1317 183074 : && !r.varying_p ()
1318 353111 : && !r.undefined_p ())
1319 : {
1320 169970 : vrange_storage *p = allocator.clone (r);
1321 169970 : ranges.safe_push ( {name, p} );
1322 : }
1323 : }
1324 :
1325 397046 : for (unsigned i = 0; i < ranges.length (); ++i)
1326 : {
1327 169970 : tree name = ranges[i].name;
1328 169970 : tree type = TREE_TYPE (name);
1329 169970 : r.set_range_class (type);
1330 169970 : ranges[i].range->get_vrange (r, type);
1331 : // Nothing at this late stage we can do if the write fails.
1332 169970 : set_range_info (name, r);
1333 169970 : maybe_set_nonzero_bits (pred_e, name);
1334 : }
1335 227076 : }
1336 :
1337 : /* Record any equivalences created by the incoming edge to BB. If BB
1338 : has more than one incoming edge, then no equivalence is created. */
1339 :
1340 : void
1341 24062282 : dom_opt_dom_walker::record_equivalences_from_incoming_edge (basic_block bb)
1342 : {
1343 24062282 : edge e;
1344 24062282 : basic_block parent;
1345 :
1346 : /* If our parent block ended with a control statement, then we may be
1347 : able to record some equivalences based on which outgoing edge from
1348 : the parent was followed. */
1349 24062282 : parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1350 :
1351 24062282 : e = single_pred_edge_ignoring_loop_edges (bb, true);
1352 :
1353 : /* If we had a single incoming edge from our parent block, then enter
1354 : any data associated with the edge into our tables. */
1355 24062282 : if (e && e->src == parent)
1356 17914716 : record_temporary_equivalences (e, m_const_and_copies, m_avail_exprs_stack,
1357 17914716 : m_state->get_blocks_on_stack ());
1358 24062282 : }
1359 :
1360 : /* Dump statistics for the hash table HTAB. */
1361 :
1362 : static void
1363 36 : htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
1364 : {
1365 68 : fprintf (file, "size " HOST_SIZE_T_PRINT_DEC ", " HOST_SIZE_T_PRINT_DEC
1366 : " elements, %f collision/search ratio\n",
1367 36 : (fmt_size_t) htab.size (),
1368 36 : (fmt_size_t) htab.elements (),
1369 : htab.collisions ());
1370 36 : }
1371 :
1372 : /* Dump SSA statistics on FILE. */
1373 :
1374 : static void
1375 36 : dump_dominator_optimization_stats (FILE *file,
1376 : hash_table<expr_elt_hasher> *avail_exprs)
1377 : {
1378 36 : fprintf (file, "Total number of statements: %6ld\n\n",
1379 : opt_stats.num_stmts);
1380 36 : fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1381 : opt_stats.num_exprs_considered);
1382 :
1383 36 : fprintf (file, "\nHash table statistics:\n");
1384 :
1385 36 : fprintf (file, " avail_exprs: ");
1386 36 : htab_statistics (file, *avail_exprs);
1387 36 : }
1388 :
1389 :
1390 : /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1391 : This constrains the cases in which we may treat this as assignment. */
1392 :
1393 : static void
1394 13737945 : record_equality (tree x, tree y, class const_and_copies *const_and_copies)
1395 : {
1396 13737945 : tree prev_x = NULL, prev_y = NULL;
1397 :
1398 13737945 : if (tree_swap_operands_p (x, y))
1399 616618 : std::swap (x, y);
1400 :
1401 : /* Most of the time tree_swap_operands_p does what we want. But there
1402 : are cases where we know one operand is better for copy propagation than
1403 : the other. Given no other code cares about ordering of equality
1404 : comparison operators for that purpose, we just handle the special cases
1405 : here. */
1406 13737945 : if (TREE_CODE (x) == SSA_NAME && TREE_CODE (y) == SSA_NAME)
1407 : {
1408 : /* If one operand is a single use operand, then make it
1409 : X. This will preserve its single use properly and if this
1410 : conditional is eliminated, the computation of X can be
1411 : eliminated as well. */
1412 1126696 : if (has_single_use (y) && ! has_single_use (x))
1413 : std::swap (x, y);
1414 : }
1415 13737945 : if (TREE_CODE (x) == SSA_NAME)
1416 13737945 : prev_x = SSA_NAME_VALUE (x);
1417 13737945 : if (TREE_CODE (y) == SSA_NAME)
1418 1126696 : prev_y = SSA_NAME_VALUE (y);
1419 :
1420 : /* If one of the previous values is invariant, or invariant in more loops
1421 : (by depth), then use that.
1422 : Otherwise it doesn't matter which value we choose, just so
1423 : long as we canonicalize on one value. */
1424 13737945 : if (is_gimple_min_invariant (y))
1425 : ;
1426 1126696 : else if (is_gimple_min_invariant (x))
1427 : prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1428 1126696 : else if (prev_x && is_gimple_min_invariant (prev_x))
1429 : x = y, y = prev_x, prev_x = prev_y;
1430 1033968 : else if (prev_y)
1431 13737945 : y = prev_y;
1432 :
1433 : /* After the swapping, we must have one SSA_NAME. */
1434 13737945 : if (TREE_CODE (x) != SSA_NAME)
1435 : return;
1436 :
1437 : /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1438 : variable compared against zero. If we're honoring signed zeros,
1439 : then we cannot record this value unless we know that the value is
1440 : nonzero. */
1441 13737945 : if (HONOR_SIGNED_ZEROS (x)
1442 13737945 : && (TREE_CODE (y) != REAL_CST
1443 172970 : || real_equal (&dconst0, &TREE_REAL_CST (y))))
1444 : return;
1445 :
1446 13737444 : const_and_copies->record_const_or_copy (x, y, prev_x);
1447 : }
1448 :
1449 : /* Returns true when STMT is a simple iv increment. It detects the
1450 : following situation:
1451 :
1452 : i_1 = phi (..., i_k)
1453 : [...]
1454 : i_j = i_{j-1} for each j : 2 <= j <= k-1
1455 : [...]
1456 : i_k = i_{k-1} +/- ... */
1457 :
1458 : bool
1459 44272099 : simple_iv_increment_p (gimple *stmt)
1460 : {
1461 44272099 : enum tree_code code;
1462 44272099 : tree lhs, preinc;
1463 44272099 : gimple *phi;
1464 44272099 : size_t i;
1465 :
1466 44272099 : gassign *assign = dyn_cast<gassign *> (stmt);
1467 34015882 : if (!assign)
1468 : return false;
1469 :
1470 34015882 : lhs = gimple_assign_lhs (assign);
1471 34015882 : if (TREE_CODE (lhs) != SSA_NAME)
1472 : return false;
1473 :
1474 34015882 : code = gimple_assign_rhs_code (assign);
1475 34015882 : if (code != PLUS_EXPR
1476 34015882 : && code != MINUS_EXPR
1477 34015882 : && code != POINTER_PLUS_EXPR)
1478 : return false;
1479 :
1480 8174667 : preinc = gimple_assign_rhs1 (assign);
1481 8174667 : if (TREE_CODE (preinc) != SSA_NAME)
1482 : return false;
1483 :
1484 7894967 : phi = SSA_NAME_DEF_STMT (preinc);
1485 7920981 : while (gimple_code (phi) != GIMPLE_PHI)
1486 : {
1487 : /* Follow trivial copies, but not the DEF used in a back edge,
1488 : so that we don't prevent coalescing. */
1489 5341830 : if (!gimple_assign_ssa_name_copy_p (phi))
1490 : return false;
1491 26014 : preinc = gimple_assign_rhs1 (phi);
1492 26014 : phi = SSA_NAME_DEF_STMT (preinc);
1493 : }
1494 :
1495 5060000 : for (i = 0; i < gimple_phi_num_args (phi); i++)
1496 4097889 : if (gimple_phi_arg_def (phi, i) == lhs)
1497 : return true;
1498 :
1499 : return false;
1500 : }
1501 :
1502 : /* Propagate know values from SSA_NAME_VALUE into the PHI nodes of the
1503 : successors of BB. */
1504 :
1505 : static void
1506 24062282 : cprop_into_successor_phis (basic_block bb,
1507 : class const_and_copies *const_and_copies)
1508 : {
1509 24062282 : edge e;
1510 24062282 : edge_iterator ei;
1511 :
1512 57154921 : FOR_EACH_EDGE (e, ei, bb->succs)
1513 : {
1514 33092639 : int indx;
1515 33092639 : gphi_iterator gsi;
1516 :
1517 : /* If this is an abnormal edge, then we do not want to copy propagate
1518 : into the PHI alternative associated with this edge. */
1519 33092639 : if (e->flags & EDGE_ABNORMAL)
1520 19428199 : continue;
1521 :
1522 33077968 : gsi = gsi_start_phis (e->dest);
1523 33077968 : if (gsi_end_p (gsi))
1524 19413528 : continue;
1525 :
1526 : /* We may have an equivalence associated with this edge. While
1527 : we cannot propagate it into non-dominated blocks, we can
1528 : propagate them into PHIs in non-dominated blocks. */
1529 :
1530 : /* Push the unwind marker so we can reset the const and copies
1531 : table back to its original state after processing this edge. */
1532 13664440 : const_and_copies->push_marker ();
1533 :
1534 : /* Extract and record any simple NAME = VALUE equivalences.
1535 :
1536 : Don't bother with [01] = COND equivalences, they're not useful
1537 : here. */
1538 13664440 : class edge_info *edge_info = (class edge_info *) e->aux;
1539 :
1540 13664440 : if (edge_info)
1541 : {
1542 : edge_info::equiv_pair *seq;
1543 8584824 : for (int i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
1544 : {
1545 3305717 : tree lhs = seq->first;
1546 3305717 : tree rhs = seq->second;
1547 :
1548 3305717 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
1549 3305717 : const_and_copies->record_const_or_copy (lhs, rhs);
1550 : }
1551 :
1552 : }
1553 :
1554 13664440 : indx = e->dest_idx;
1555 37411558 : for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1556 : {
1557 23747118 : tree new_val;
1558 23747118 : use_operand_p orig_p;
1559 23747118 : tree orig_val;
1560 23747118 : gphi *phi = gsi.phi ();
1561 :
1562 : /* The alternative may be associated with a constant, so verify
1563 : it is an SSA_NAME before doing anything with it. */
1564 23747118 : orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1565 23747118 : orig_val = get_use_from_ptr (orig_p);
1566 23747118 : if (TREE_CODE (orig_val) != SSA_NAME)
1567 3278658 : continue;
1568 :
1569 : /* If we have *ORIG_P in our constant/copy table, then replace
1570 : ORIG_P with its value in our constant/copy table. */
1571 20468460 : new_val = SSA_NAME_VALUE (orig_val);
1572 20468460 : if (new_val
1573 20468460 : && new_val != orig_val
1574 20468460 : && may_propagate_copy (orig_val, new_val))
1575 1162124 : propagate_value (orig_p, new_val);
1576 : }
1577 :
1578 13664440 : const_and_copies->pop_to_marker ();
1579 : }
1580 24062282 : }
1581 :
1582 : edge
1583 24062282 : dom_opt_dom_walker::before_dom_children (basic_block bb)
1584 : {
1585 24062282 : gimple_stmt_iterator gsi;
1586 :
1587 24062282 : if (dump_file && (dump_flags & TDF_DETAILS))
1588 764 : fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1589 :
1590 : /* Push a marker on the stacks of local information so that we know how
1591 : far to unwind when we finalize this block. */
1592 24062282 : m_avail_exprs_stack->push_marker ();
1593 24062282 : m_const_and_copies->push_marker ();
1594 24062282 : bitmap_set_bit (m_state->get_blocks_on_stack (), bb->index);
1595 :
1596 24062282 : record_equivalences_from_incoming_edge (bb);
1597 24062282 : set_global_ranges_from_unreachable_edges (bb);
1598 :
1599 : /* PHI nodes can create equivalences too. */
1600 24062282 : record_equivalences_from_phis (bb);
1601 :
1602 : /* Create equivalences from redundant PHIs. PHIs are only truly
1603 : redundant when they exist in the same block, so push another
1604 : marker and unwind right afterwards. */
1605 24062282 : m_avail_exprs_stack->push_marker ();
1606 33114082 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1607 9051800 : eliminate_redundant_computations (&gsi);
1608 24062282 : m_avail_exprs_stack->pop_to_marker ();
1609 :
1610 24062282 : edge taken_edge = NULL;
1611 : /* Initialize visited flag ahead of us, it has undefined state on
1612 : pass entry. */
1613 229367866 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1614 181243302 : gimple_set_visited (gsi_stmt (gsi), false);
1615 386425023 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
1616 : {
1617 : /* Do not optimize a stmt twice, substitution might end up with
1618 : _3 = _3 which is not valid. */
1619 362362741 : if (gimple_visited_p (gsi_stmt (gsi)))
1620 : {
1621 181119291 : gsi_next (&gsi);
1622 181119291 : continue;
1623 : }
1624 :
1625 181243450 : bool removed_p = false;
1626 181243450 : taken_edge = this->optimize_stmt (bb, &gsi, &removed_p);
1627 181243450 : if (!removed_p)
1628 : {
1629 181119291 : gimple *stmt = gsi_stmt (gsi);
1630 181119291 : gimple_set_visited (stmt, true);
1631 :
1632 : /* Propagation and folding may have added uses to STMT, which
1633 : back_propagate_equivalences has to consider. */
1634 181119291 : tree lhs = gimple_get_lhs (stmt);
1635 181119291 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
1636 : {
1637 36968090 : ssa_op_iter iter;
1638 36968090 : tree use;
1639 73019009 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1640 36050919 : bitmap_clear_bit (names_without_foldable_uses,
1641 36050919 : SSA_NAME_VERSION (use));
1642 : }
1643 : }
1644 :
1645 : /* Go back and visit stmts inserted by folding after substituting
1646 : into the stmt at gsi. */
1647 181243450 : if (gsi_end_p (gsi))
1648 : {
1649 2807 : gcc_checking_assert (removed_p);
1650 2807 : gsi = gsi_last_bb (bb);
1651 2807 : while (!gsi_end_p (gsi) && !gimple_visited_p (gsi_stmt (gsi)))
1652 0 : gsi_prev (&gsi);
1653 : }
1654 : else
1655 : {
1656 181240791 : do
1657 : {
1658 181240791 : gsi_prev (&gsi);
1659 : }
1660 362481434 : while (!gsi_end_p (gsi) && !gimple_visited_p (gsi_stmt (gsi)));
1661 : }
1662 181243450 : if (gsi_end_p (gsi))
1663 39657026 : gsi = gsi_start_bb (bb);
1664 : else
1665 161414937 : gsi_next (&gsi);
1666 : }
1667 :
1668 : /* Now prepare to process dominated blocks. */
1669 24062282 : record_edge_info (bb);
1670 24062282 : cprop_into_successor_phis (bb, m_const_and_copies);
1671 24062282 : if (taken_edge && !dbg_cnt (dom_unreachable_edges))
1672 0 : return NULL;
1673 :
1674 : return taken_edge;
1675 : }
1676 :
1677 : /* We have finished processing the dominator children of BB, perform
1678 : any finalization actions in preparation for leaving this node in
1679 : the dominator tree. */
1680 :
1681 : void
1682 24062282 : dom_opt_dom_walker::after_dom_children (basic_block bb)
1683 : {
1684 24062282 : if (param_dom_jump_threading)
1685 24061533 : m_threader->thread_outgoing_edges (bb);
1686 24062282 : bitmap_clear_bit (m_state->get_blocks_on_stack (), bb->index);
1687 24062282 : m_avail_exprs_stack->pop_to_marker ();
1688 24062282 : m_const_and_copies->pop_to_marker ();
1689 24062282 : }
1690 :
1691 : /* Return the constant STMT computes, and barring that, an operand its
1692 : result is equivalent to. NULL if neither. */
1693 :
1694 : tree
1695 65022958 : dom_opt_dom_walker::const_or_copy_of_stmt (gimple *stmt)
1696 : {
1697 65022958 : if (tree val = m_ranger->value_of_stmt (stmt))
1698 : return val;
1699 :
1700 : /* We could remove this gate, but we'd end up with tons of useless
1701 : replacements (casts, copies, etc). Best to keep it to binops, which is
1702 : what the original code did. */
1703 64305542 : auto assign = dyn_cast<gassign *> (stmt);
1704 45044822 : if (!assign || gimple_assign_rhs_class (assign) != GIMPLE_BINARY_RHS)
1705 : return NULL_TREE;
1706 :
1707 12047802 : tree lhs = gimple_assign_lhs (assign);
1708 12047802 : tree rhs1 = gimple_assign_rhs1 (assign);
1709 12047802 : tree rhs2 = gimple_assign_rhs2 (assign);
1710 12047802 : if (m_ranger->relation ().query (assign, lhs, rhs1) == VREL_EQ)
1711 : return rhs1;
1712 12044317 : if (m_ranger->relation ().query (assign, lhs, rhs2) == VREL_EQ)
1713 43 : return rhs2;
1714 :
1715 : return NULL_TREE;
1716 : }
1717 :
1718 : /* Search for redundant computations in STMT. If any are found, then
1719 : replace them with the variable holding the result of the computation.
1720 :
1721 : If safe, record this expression into the tables. */
1722 :
1723 : void
1724 65500530 : dom_opt_dom_walker::eliminate_redundant_computations (gimple_stmt_iterator *gsi)
1725 : {
1726 65500530 : tree expr_type;
1727 65500530 : tree cached_lhs;
1728 65500530 : tree def;
1729 65500530 : bool insert = true;
1730 65500530 : bool assigns_var_p = false;
1731 :
1732 65500530 : gimple *stmt = gsi_stmt (*gsi);
1733 :
1734 65500530 : if (gimple_code (stmt) == GIMPLE_PHI)
1735 9051800 : def = gimple_phi_result (stmt);
1736 : else
1737 56448730 : def = gimple_get_lhs (stmt);
1738 :
1739 : /* Certain expressions on the RHS can be optimized away, but cannot
1740 : themselves be entered into the hash tables. */
1741 65500530 : if (! def
1742 56254079 : || TREE_CODE (def) != SSA_NAME
1743 43143039 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1744 34089825 : || gimple_vdef (stmt)
1745 : /* Do not record equivalences for increments of ivs. This would create
1746 : overlapping live ranges for a very questionable gain. */
1747 108632371 : || simple_iv_increment_p (stmt))
1748 : insert = false;
1749 :
1750 : /* Check if the expression has been computed before. */
1751 65500530 : cached_lhs = m_avail_exprs_stack->lookup_avail_expr (stmt, insert, true);
1752 :
1753 : /* Otherwise, ask the ranger. */
1754 65500530 : if (!cached_lhs)
1755 65022958 : cached_lhs = const_or_copy_of_stmt (stmt);
1756 :
1757 65500530 : opt_stats.num_exprs_considered++;
1758 :
1759 : /* Get the type of the expression we are trying to optimize. */
1760 65500530 : if (auto assign = dyn_cast<gassign *> (stmt))
1761 : {
1762 45928465 : expr_type = TREE_TYPE (gimple_assign_lhs (assign));
1763 45928465 : assigns_var_p = true;
1764 : }
1765 19572065 : else if (gimple_code (stmt) == GIMPLE_COND)
1766 9187134 : expr_type = boolean_type_node;
1767 10384931 : else if (is_gimple_call (stmt))
1768 : {
1769 1273814 : gcc_assert (gimple_call_lhs (stmt));
1770 1273814 : expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1771 1273814 : assigns_var_p = true;
1772 : }
1773 9111117 : else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1774 59317 : expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
1775 9051800 : else if (gimple_code (stmt) == GIMPLE_PHI)
1776 : /* We can't propagate into a phi, so the logic below doesn't apply.
1777 : Instead record an equivalence between the cached LHS and the
1778 : PHI result of this statement, provided they are in the same block.
1779 : This should be sufficient to kill the redundant phi. */
1780 : {
1781 9051800 : if (def && cached_lhs)
1782 251146 : m_const_and_copies->record_const_or_copy (def, cached_lhs);
1783 : return;
1784 : }
1785 : else
1786 0 : gcc_unreachable ();
1787 :
1788 56448730 : if (!cached_lhs)
1789 : return;
1790 :
1791 : /* It is safe to ignore types here since we have already done
1792 : type checking in the hashing and equality routines. In fact
1793 : type checking here merely gets in the way of constant
1794 : propagation. Also, make sure that it is safe to propagate
1795 : CACHED_LHS into the expression in STMT. */
1796 947370 : if ((TREE_CODE (cached_lhs) != SSA_NAME
1797 552566 : && (assigns_var_p
1798 30351 : || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1799 947370 : || may_propagate_copy_into_stmt (stmt, cached_lhs))
1800 : {
1801 932582 : gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1802 : || is_gimple_min_invariant (cached_lhs));
1803 :
1804 932582 : if (dump_file && (dump_flags & TDF_DETAILS))
1805 : {
1806 20 : fprintf (dump_file, " Replaced redundant expr '");
1807 20 : print_gimple_expr (dump_file, stmt, 0, dump_flags);
1808 20 : fprintf (dump_file, "' with '");
1809 20 : print_generic_expr (dump_file, cached_lhs, dump_flags);
1810 20 : fprintf (dump_file, "'\n");
1811 : }
1812 :
1813 932582 : opt_stats.num_re++;
1814 :
1815 932582 : if (assigns_var_p
1816 932582 : && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
1817 0 : cached_lhs = fold_convert (expr_type, cached_lhs);
1818 :
1819 932582 : propagate_tree_value_into_stmt (gsi, cached_lhs);
1820 :
1821 : /* Since it is always necessary to mark the result as modified,
1822 : perhaps we should move this into propagate_tree_value_into_stmt
1823 : itself. */
1824 932582 : gimple_set_modified (gsi_stmt (*gsi), true);
1825 : }
1826 : }
1827 :
1828 : /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
1829 : the available expressions table or the const_and_copies table.
1830 : Detect and record those equivalences.
1831 :
1832 : We handle only very simple copy equivalences here. The heavy
1833 : lifing is done by eliminate_redundant_computations. */
1834 :
1835 : void
1836 49634172 : dom_opt_dom_walker::record_equivalences_from_stmt (gassign *stmt,
1837 : int may_optimize_p)
1838 : {
1839 49634172 : tree lhs;
1840 49634172 : enum tree_code lhs_code;
1841 :
1842 49634172 : gcc_assert (is_gimple_assign (stmt));
1843 :
1844 49634172 : lhs = gimple_assign_lhs (stmt);
1845 49634172 : lhs_code = TREE_CODE (lhs);
1846 :
1847 49634172 : if (lhs_code == SSA_NAME
1848 49634172 : && gimple_assign_single_p (stmt))
1849 : {
1850 16185099 : tree rhs = gimple_assign_rhs1 (stmt);
1851 :
1852 : /* If the RHS of the assignment is a constant or another variable that
1853 : may be propagated, register it in the CONST_AND_COPIES table. We
1854 : do not need to record unwind data for this, since this is a true
1855 : assignment and not an equivalence inferred from a comparison. All
1856 : uses of this ssa name are dominated by this assignment, so unwinding
1857 : just costs time and space. */
1858 16185099 : if (may_optimize_p
1859 16185099 : && (TREE_CODE (rhs) == SSA_NAME
1860 14094067 : || is_gimple_min_invariant (rhs)))
1861 : {
1862 2101695 : rhs = dom_valueize (rhs);
1863 :
1864 2101695 : if (dump_file && (dump_flags & TDF_DETAILS))
1865 : {
1866 73 : fprintf (dump_file, "==== ASGN ");
1867 73 : print_generic_expr (dump_file, lhs);
1868 73 : fprintf (dump_file, " = ");
1869 73 : print_generic_expr (dump_file, rhs);
1870 73 : fprintf (dump_file, "\n");
1871 : }
1872 :
1873 2101695 : set_ssa_name_value (lhs, rhs);
1874 : }
1875 : }
1876 :
1877 : /* Make sure we can propagate &x + CST. */
1878 49634172 : if (lhs_code == SSA_NAME
1879 33473768 : && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1880 1682273 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
1881 49832310 : && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
1882 : {
1883 6578 : tree op0 = gimple_assign_rhs1 (stmt);
1884 6578 : tree op1 = gimple_assign_rhs2 (stmt);
1885 6578 : tree new_rhs
1886 13156 : = build1 (ADDR_EXPR, TREE_TYPE (op0),
1887 6578 : fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)),
1888 : unshare_expr (op0), fold_convert (ptr_type_node,
1889 : op1)));
1890 6578 : if (dump_file && (dump_flags & TDF_DETAILS))
1891 : {
1892 0 : fprintf (dump_file, "==== ASGN ");
1893 0 : print_generic_expr (dump_file, lhs);
1894 0 : fprintf (dump_file, " = ");
1895 0 : print_generic_expr (dump_file, new_rhs);
1896 0 : fprintf (dump_file, "\n");
1897 : }
1898 :
1899 6578 : set_ssa_name_value (lhs, new_rhs);
1900 : }
1901 :
1902 : /* A memory store, even an aliased store, creates a useful
1903 : equivalence. By exchanging the LHS and RHS, creating suitable
1904 : vops and recording the result in the available expression table,
1905 : we may be able to expose more redundant loads. */
1906 95446745 : if (!gimple_has_volatile_ops (stmt)
1907 74426609 : && gimple_references_memory_p (stmt)
1908 24792437 : && gimple_assign_single_p (stmt)
1909 24792437 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1910 19377682 : || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1911 61562339 : && !is_gimple_reg (lhs))
1912 : {
1913 11762854 : tree rhs = gimple_assign_rhs1 (stmt);
1914 11762854 : gassign *new_stmt;
1915 :
1916 : /* Build a new statement with the RHS and LHS exchanged. */
1917 11762854 : if (TREE_CODE (rhs) == SSA_NAME)
1918 : {
1919 : /* NOTE tuples. The call to gimple_build_assign below replaced
1920 : a call to build_gimple_modify_stmt, which did not set the
1921 : SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
1922 : may cause an SSA validation failure, as the LHS may be a
1923 : default-initialized name and should have no definition. I'm
1924 : a bit dubious of this, as the artificial statement that we
1925 : generate here may in fact be ill-formed, but it is simply
1926 : used as an internal device in this pass, and never becomes
1927 : part of the CFG. */
1928 5279971 : gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1929 5279971 : new_stmt = gimple_build_assign (rhs, lhs);
1930 5279971 : SSA_NAME_DEF_STMT (rhs) = defstmt;
1931 : }
1932 : else
1933 6482883 : new_stmt = gimple_build_assign (rhs, lhs);
1934 :
1935 23525708 : gimple_set_vuse (new_stmt, gimple_vdef (stmt));
1936 :
1937 : /* Finally enter the statement into the available expression
1938 : table. */
1939 11762854 : m_avail_exprs_stack->lookup_avail_expr (new_stmt, true, true);
1940 : }
1941 49634172 : }
1942 :
1943 : /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1944 : CONST_AND_COPIES. */
1945 :
1946 : static void
1947 82505761 : cprop_operand (gimple *stmt, use_operand_p op_p, range_query *query)
1948 : {
1949 82505761 : tree val;
1950 82505761 : tree op = USE_FROM_PTR (op_p);
1951 :
1952 : /* If the operand has a known constant value or it is known to be a
1953 : copy of some other variable, use the value or copy stored in
1954 : CONST_AND_COPIES. */
1955 82505761 : val = SSA_NAME_VALUE (op);
1956 55710182 : if (!val)
1957 : {
1958 78448137 : value_range r (TREE_TYPE (op));
1959 78448137 : tree single;
1960 154409765 : if (query->range_of_expr (r, op, stmt) && r.singleton_p (&single))
1961 8587 : val = single;
1962 78448137 : }
1963 :
1964 82505761 : if (val && val != op)
1965 : {
1966 : /* Certain operands are not allowed to be copy propagated due
1967 : to their interaction with exception handling and some GCC
1968 : extensions. */
1969 4064776 : if (!may_propagate_copy (op, val))
1970 : return;
1971 :
1972 : /* Do not propagate copies into BIVs.
1973 : See PR23821 and PR62217 for how this can disturb IV and
1974 : number of iteration analysis. */
1975 4063139 : if (TREE_CODE (val) != INTEGER_CST)
1976 : {
1977 3277544 : gimple *def = SSA_NAME_DEF_STMT (op);
1978 3277544 : if (gimple_code (def) == GIMPLE_PHI
1979 3277544 : && gimple_bb (def)->loop_father->header == gimple_bb (def))
1980 : return;
1981 : }
1982 :
1983 : /* Dump details. */
1984 4051760 : if (dump_file && (dump_flags & TDF_DETAILS))
1985 : {
1986 76 : fprintf (dump_file, " Replaced '");
1987 76 : print_generic_expr (dump_file, op, dump_flags);
1988 76 : fprintf (dump_file, "' with %s '",
1989 76 : (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
1990 76 : print_generic_expr (dump_file, val, dump_flags);
1991 76 : fprintf (dump_file, "'\n");
1992 : }
1993 :
1994 4051760 : if (TREE_CODE (val) != SSA_NAME)
1995 953652 : opt_stats.num_const_prop++;
1996 : else
1997 3098108 : opt_stats.num_copy_prop++;
1998 :
1999 4051760 : propagate_value (op_p, val);
2000 :
2001 : /* And note that we modified this statement. This is now
2002 : safe, even if we changed virtual operands since we will
2003 : rescan the statement and rewrite its operands again. */
2004 4051760 : gimple_set_modified (stmt, true);
2005 : }
2006 : }
2007 :
2008 : /* Fold the conditional STMT with range info. */
2009 :
2010 : void
2011 55391989 : dom_opt_dom_walker::fold_cond (gimple *stmt)
2012 : {
2013 55391989 : gcond *cond = dyn_cast <gcond *> (stmt);
2014 55391989 : if (!cond)
2015 46256787 : return;
2016 :
2017 9135202 : simplify_using_ranges simplify (m_ranger);
2018 9135202 : if (simplify.fold_cond (cond))
2019 83273 : gimple_set_modified (cond, true);
2020 9135202 : }
2021 :
2022 : /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2023 : known value for that SSA_NAME (or NULL if no value is known).
2024 :
2025 : Propagate values from CONST_AND_COPIES into the uses, vuses and
2026 : vdef_ops of STMT. */
2027 :
2028 : static void
2029 181243450 : cprop_into_stmt (gimple *stmt, range_query *query)
2030 : {
2031 181243450 : use_operand_p op_p;
2032 181243450 : ssa_op_iter iter;
2033 181243450 : tree last_copy_propagated_op = NULL;
2034 :
2035 263749896 : FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
2036 : {
2037 82506446 : tree old_op = USE_FROM_PTR (op_p);
2038 :
2039 : /* If we have A = B and B = A in the copy propagation tables
2040 : (due to an equality comparison), avoid substituting B for A
2041 : then A for B in the trivially discovered cases. This allows
2042 : optimization of statements were A and B appear as input
2043 : operands. */
2044 82506446 : if (old_op != last_copy_propagated_op)
2045 : {
2046 82505761 : cprop_operand (stmt, op_p, query);
2047 :
2048 82505761 : tree new_op = USE_FROM_PTR (op_p);
2049 82505761 : if (new_op != old_op && TREE_CODE (new_op) == SSA_NAME)
2050 82506446 : last_copy_propagated_op = new_op;
2051 : }
2052 : }
2053 181243450 : }
2054 :
2055 : /* If STMT is a comparison of two uniform vectors reduce it to a comparison
2056 : of scalar objects, otherwise leave STMT unchanged. */
2057 :
2058 : static void
2059 181243450 : reduce_vector_comparison_to_scalar_comparison (gimple *stmt)
2060 : {
2061 181243450 : if (gimple_code (stmt) == GIMPLE_COND)
2062 : {
2063 9187134 : tree lhs = gimple_cond_lhs (stmt);
2064 9187134 : tree rhs = gimple_cond_rhs (stmt);
2065 :
2066 : /* We may have a vector comparison where both arms are uniform
2067 : vectors. If so, we can simplify the vector comparison down
2068 : to a scalar comparison. */
2069 9187134 : if (VECTOR_TYPE_P (TREE_TYPE (lhs))
2070 9187134 : && VECTOR_TYPE_P (TREE_TYPE (rhs)))
2071 : {
2072 : /* If either operand is an SSA_NAME, then look back to its
2073 : defining statement to try and get at a suitable source. */
2074 2643 : if (TREE_CODE (rhs) == SSA_NAME)
2075 : {
2076 0 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
2077 0 : if (gimple_assign_single_p (def_stmt))
2078 0 : rhs = gimple_assign_rhs1 (def_stmt);
2079 : }
2080 :
2081 2643 : if (TREE_CODE (lhs) == SSA_NAME)
2082 : {
2083 2643 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
2084 2643 : if (gimple_assign_single_p (def_stmt))
2085 4 : lhs = gimple_assign_rhs1 (def_stmt);
2086 : }
2087 :
2088 : /* Now see if they are both uniform vectors and if so replace
2089 : the vector comparison with a scalar comparison. */
2090 2643 : tree rhs_elem = rhs ? uniform_vector_p (rhs) : NULL_TREE;
2091 2643 : tree lhs_elem = lhs ? uniform_vector_p (lhs) : NULL_TREE;
2092 2643 : if (rhs_elem && lhs_elem)
2093 : {
2094 3 : if (dump_file && dump_flags & TDF_DETAILS)
2095 : {
2096 0 : fprintf (dump_file, "Reducing vector comparison: ");
2097 0 : print_gimple_stmt (dump_file, stmt, 0);
2098 : }
2099 :
2100 3 : gimple_cond_set_rhs (as_a <gcond *>(stmt), rhs_elem);
2101 3 : gimple_cond_set_lhs (as_a <gcond *>(stmt), lhs_elem);
2102 3 : gimple_set_modified (stmt, true);
2103 :
2104 3 : if (dump_file && dump_flags & TDF_DETAILS)
2105 : {
2106 0 : fprintf (dump_file, "To scalar equivalent: ");
2107 0 : print_gimple_stmt (dump_file, stmt, 0);
2108 0 : fprintf (dump_file, "\n");
2109 : }
2110 : }
2111 : }
2112 : }
2113 181243450 : }
2114 :
2115 : /* Optimize the statement in block BB pointed to by iterator SI.
2116 :
2117 : We try to perform some simplistic global redundancy elimination and
2118 : constant propagation:
2119 :
2120 : 1- To detect global redundancy, we keep track of expressions that have
2121 : been computed in this block and its dominators. If we find that the
2122 : same expression is computed more than once, we eliminate repeated
2123 : computations by using the target of the first one.
2124 :
2125 : 2- Constant values and copy assignments. This is used to do very
2126 : simplistic constant and copy propagation. When a constant or copy
2127 : assignment is found, we map the value on the RHS of the assignment to
2128 : the variable in the LHS in the CONST_AND_COPIES table.
2129 :
2130 : 3- Very simple redundant store elimination is performed.
2131 :
2132 : 4- We can simplify a condition to a constant or from a relational
2133 : condition to an equality condition. */
2134 :
2135 : edge
2136 181243450 : dom_opt_dom_walker::optimize_stmt (basic_block bb, gimple_stmt_iterator *si,
2137 : bool *removed_p)
2138 : {
2139 181243450 : gimple *stmt, *old_stmt;
2140 181243450 : bool may_optimize_p;
2141 181243450 : bool modified_p = false;
2142 181243450 : bool was_noreturn;
2143 181243450 : edge retval = NULL;
2144 :
2145 181243450 : old_stmt = stmt = gsi_stmt (*si);
2146 181243450 : was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
2147 :
2148 181243450 : if (dump_file && (dump_flags & TDF_DETAILS))
2149 : {
2150 1626 : fprintf (dump_file, "Optimizing statement ");
2151 1626 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2152 : }
2153 :
2154 : /* STMT may be a comparison of uniform vectors that we can simplify
2155 : down to a comparison of scalars. Do that transformation first
2156 : so that all the scalar optimizations from here onward apply. */
2157 181243450 : reduce_vector_comparison_to_scalar_comparison (stmt);
2158 :
2159 181243450 : update_stmt_if_modified (stmt);
2160 181243450 : opt_stats.num_stmts++;
2161 :
2162 : /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
2163 181243450 : cprop_into_stmt (stmt, m_ranger);
2164 :
2165 : /* If the statement has been modified with constant replacements,
2166 : fold its RHS before checking for redundant computations. */
2167 362161730 : if (gimple_modified_p (stmt))
2168 : {
2169 3885130 : tree rhs = NULL;
2170 :
2171 : /* Try to fold the statement making sure that STMT is kept
2172 : up to date. */
2173 3885130 : if (fold_stmt (si))
2174 : {
2175 400657 : stmt = gsi_stmt (*si);
2176 400657 : gimple_set_modified (stmt, true);
2177 :
2178 400657 : if (dump_file && (dump_flags & TDF_DETAILS))
2179 : {
2180 10 : fprintf (dump_file, " Folded to: ");
2181 10 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2182 : }
2183 : }
2184 :
2185 : /* We only need to consider cases that can yield a gimple operand. */
2186 3885130 : if (gimple_assign_single_p (stmt))
2187 1454415 : rhs = gimple_assign_rhs1 (stmt);
2188 2430715 : else if (gimple_code (stmt) == GIMPLE_GOTO)
2189 2 : rhs = gimple_goto_dest (stmt);
2190 181245533 : else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2191 : /* This should never be an ADDR_EXPR. */
2192 2083 : rhs = gimple_switch_index (swtch_stmt);
2193 :
2194 1456500 : if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2195 82413 : recompute_tree_invariant_for_addr_expr (rhs);
2196 :
2197 : /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2198 : even if fold_stmt updated the stmt already and thus cleared
2199 : gimple_modified_p flag on it. */
2200 : modified_p = true;
2201 : }
2202 :
2203 : /* Check for redundant computations. Do this optimization only
2204 : for assignments that have no volatile ops and conditionals. */
2205 181243450 : may_optimize_p = (!gimple_has_side_effects (stmt)
2206 181243450 : && (is_gimple_assign (stmt)
2207 121594713 : || (is_gimple_call (stmt)
2208 1278178 : && gimple_call_lhs (stmt) != NULL_TREE)
2209 120316899 : || gimple_code (stmt) == GIMPLE_COND
2210 111129765 : || gimple_code (stmt) == GIMPLE_SWITCH));
2211 :
2212 : if (may_optimize_p)
2213 : {
2214 56448730 : if (gimple_code (stmt) == GIMPLE_CALL)
2215 : {
2216 : /* Resolve __builtin_constant_p. If it hasn't been
2217 : folded to integer_one_node by now, it's fairly
2218 : certain that the value simply isn't constant. */
2219 1277814 : tree callee = gimple_call_fndecl (stmt);
2220 1277814 : if (callee
2221 1277814 : && fndecl_built_in_p (callee, BUILT_IN_CONSTANT_P))
2222 : {
2223 4000 : propagate_tree_value_into_stmt (si, integer_zero_node);
2224 4000 : stmt = gsi_stmt (*si);
2225 : }
2226 : }
2227 :
2228 56448730 : update_stmt_if_modified (stmt);
2229 56448730 : eliminate_redundant_computations (si);
2230 56448730 : stmt = gsi_stmt (*si);
2231 :
2232 : /* Perform simple redundant store elimination. */
2233 56448730 : auto assign = dyn_cast<gassign *> (stmt);
2234 56448730 : if (gimple_assign_single_p (stmt)
2235 28648063 : && TREE_CODE (gimple_assign_lhs (assign)) != SSA_NAME
2236 69500205 : && (TREE_CODE (gimple_assign_lhs (assign)) != VAR_DECL
2237 1065283 : || !DECL_HARD_REGISTER (gimple_assign_lhs (assign))))
2238 : {
2239 13049403 : tree lhs = gimple_assign_lhs (assign);
2240 13049403 : tree rhs = gimple_assign_rhs1 (assign);
2241 13049403 : tree cached_lhs;
2242 13049403 : gassign *new_stmt;
2243 13049403 : rhs = dom_valueize (rhs);
2244 : /* Build a new statement with the RHS and LHS exchanged. */
2245 13049403 : if (TREE_CODE (rhs) == SSA_NAME)
2246 : {
2247 5400120 : gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
2248 5400120 : new_stmt = gimple_build_assign (rhs, lhs);
2249 5400120 : SSA_NAME_DEF_STMT (rhs) = defstmt;
2250 : }
2251 : else
2252 7649283 : new_stmt = gimple_build_assign (rhs, lhs);
2253 26098806 : gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2254 13049403 : expr_hash_elt *elt = NULL;
2255 13049403 : cached_lhs = m_avail_exprs_stack->lookup_avail_expr (new_stmt, false,
2256 : false, &elt);
2257 13049403 : if (cached_lhs
2258 854369 : && operand_equal_p (rhs, cached_lhs, 0)
2259 13306829 : && refs_same_for_tbaa_p (elt->expr ()->kind == EXPR_SINGLE
2260 128713 : ? elt->expr ()->ops.single.rhs
2261 : : NULL_TREE, lhs))
2262 : {
2263 124159 : basic_block bb = gimple_bb (stmt);
2264 124159 : unlink_stmt_vdef (stmt);
2265 124159 : if (gsi_remove (si, true))
2266 : {
2267 0 : bitmap_set_bit (need_eh_cleanup, bb->index);
2268 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2269 0 : fprintf (dump_file, " Flagged to clear EH edges.\n");
2270 : }
2271 124159 : release_defs (stmt);
2272 124159 : *removed_p = true;
2273 124159 : return retval;
2274 : }
2275 : }
2276 :
2277 : /* If this statement was not redundant, we may still be able to simplify
2278 : it, which may in turn allow other part of DOM or other passes to do
2279 : a better job. */
2280 112649142 : if (!gimple_modified_p (stmt))
2281 55391989 : fold_cond (stmt);
2282 : }
2283 :
2284 : /* Record any additional equivalences created by this statement. */
2285 181119291 : if (auto assign = dyn_cast<gassign *> (stmt))
2286 49634172 : record_equivalences_from_stmt (assign, may_optimize_p);
2287 :
2288 : /* If STMT is a COND_EXPR or SWITCH_EXPR and it was modified, then we may
2289 : know where it goes. */
2290 362238582 : if (gimple_modified_p (stmt) || modified_p)
2291 : {
2292 4499428 : tree val = NULL;
2293 :
2294 4499428 : if (gimple_code (stmt) == GIMPLE_COND)
2295 568916 : val = fold_binary_loc (gimple_location (stmt),
2296 : gimple_cond_code (stmt), boolean_type_node,
2297 : gimple_cond_lhs (stmt),
2298 : gimple_cond_rhs (stmt));
2299 3930512 : else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2300 2083 : val = gimple_switch_index (swtch_stmt);
2301 :
2302 570999 : if (val && TREE_CODE (val) == INTEGER_CST)
2303 : {
2304 179450 : retval = find_taken_edge (bb, val);
2305 179450 : if (retval)
2306 : {
2307 : /* Fix the condition to be either true or false. */
2308 179450 : if (gimple_code (stmt) == GIMPLE_COND)
2309 : {
2310 179416 : if (integer_zerop (val))
2311 118673 : gimple_cond_make_false (as_a <gcond *> (stmt));
2312 60743 : else if (integer_onep (val))
2313 60743 : gimple_cond_make_true (as_a <gcond *> (stmt));
2314 : else
2315 0 : gcc_unreachable ();
2316 :
2317 179416 : gimple_set_modified (stmt, true);
2318 : }
2319 :
2320 : /* Further simplifications may be possible. */
2321 179450 : cfg_altered = true;
2322 : }
2323 : }
2324 :
2325 4499428 : update_stmt_if_modified (stmt);
2326 :
2327 : /* If we simplified a statement in such a way as to be shown that it
2328 : cannot trap, update the eh information and the cfg to match. */
2329 4499428 : if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2330 : {
2331 1411 : bitmap_set_bit (need_eh_cleanup, bb->index);
2332 1411 : if (dump_file && (dump_flags & TDF_DETAILS))
2333 0 : fprintf (dump_file, " Flagged to clear EH edges.\n");
2334 : }
2335 :
2336 4499428 : if (!was_noreturn
2337 4499428 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
2338 6 : need_noreturn_fixup.safe_push (stmt);
2339 : }
2340 : return retval;
2341 : }
|