Debian Codegraph @

gcc/gimple-range-gori.cc

raw
1 /* Gimple range GORI functions.
2 Copyright (C) 2017-2025 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <[email protected]>
4 and Aldy Hernandez <[email protected]>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "gimple-pretty-print.h"
30 #include "gimple-range.h"
31
32 // Return TRUE if GS is a logical && or || expression.
33
34 static inline bool
35 is_gimple_logical_p (const gimple *gs)
36 {
37 // Look for boolean and/or condition.
38 if (is_gimple_assign (gs))
39 switch (gimple_expr_code (gs))
40 {
41 case TRUTH_AND_EXPR:
42 case TRUTH_OR_EXPR:
43 return true;
44
45 case BIT_AND_EXPR:
46 case BIT_IOR_EXPR:
47 // Bitwise operations on single bits are logical too.
48 if (types_compatible_p (TREE_TYPE (gimple_assign_rhs1 (gs)),
49 boolean_type_node))
50 return true;
51 break;
52
53 default:
54 break;
55 }
56 return false;
57 }
58
59 /* RANGE_DEF_CHAIN is used to determine which SSA names in a block can
60 have range information calculated for them, and what the
61 dependencies on each other are.
62
63 Information for a basic block is calculated once and stored. It is
64 only calculated the first time a query is made, so if no queries
65 are made, there is little overhead.
66
67 The def_chain bitmap is indexed by SSA_NAME_VERSION. Bits are set
68 within this bitmap to indicate SSA names that are defined in the
69 SAME block and used to calculate this SSA name.
70
71
72 <bb 2> :
73 _1 = x_4(D) + -2;
74 _2 = _1 * 4;
75 j_7 = foo ();
76 q_5 = _2 + 3;
77 if (q_5 <= 13)
78
79 _1 : x_4(D)
80 _2 : 1 x_4(D)
81 q_5 : _1 _2 x_4(D)
82
83 This dump indicates the bits set in the def_chain vector.
84 as well as demonstrates the def_chain bits for the related ssa_names.
85
86 Checking the chain for _2 indicates that _1 and x_4 are used in
87 its evaluation.
88
89 Def chains also only include statements which are valid gimple
90 so a def chain will only span statements for which the range
91 engine implements operations for. */
92
93
94 // Construct a range_def_chain.
95
96 range_def_chain::range_def_chain ()
97 {
98 bitmap_obstack_initialize (&m_bitmaps);
99 m_def_chain.create (0);
100 m_def_chain.safe_grow_cleared (num_ssa_names);
101 m_logical_depth = 0;
102 }
103
104 // Destruct a range_def_chain.
105
106 range_def_chain::~range_def_chain ()
107 {
108 m_def_chain.release ();
109 bitmap_obstack_release (&m_bitmaps);
110 }
111
112 // Return true if NAME is in the def chain of DEF. If BB is provided,
113 // only return true if the defining statement of DEF is in BB.
114
115 bool
116 range_def_chain::in_chain_p (tree name, tree def)
117 {
118 gcc_checking_assert (gimple_range_ssa_p (def));
119 gcc_checking_assert (gimple_range_ssa_p (name));
120
121 // Get the definition chain for DEF.
122 bitmap chain = get_def_chain (def);
123
124 if (chain == NULL)
125 return false;
126 return bitmap_bit_p (chain, SSA_NAME_VERSION (name));
127 }
128
129 // Add either IMP or the import list B to the import set of DATA.
130
131 void
132 range_def_chain::set_import (struct rdc &data, tree imp, bitmap b)
133 {
134 // If there are no imports, just return
135 if (imp == NULL_TREE && !b)
136 return;
137 if (!data.m_import)
138 data.m_import = BITMAP_ALLOC (&m_bitmaps);
139 if (imp != NULL_TREE)
140 bitmap_set_bit (data.m_import, SSA_NAME_VERSION (imp));
141 else
142 bitmap_ior_into (data.m_import, b);
143 }
144
145 // Return the import list for NAME.
146
147 bitmap
148 range_def_chain::get_imports (tree name)
149 {
150 if (!has_def_chain (name))
151 get_def_chain (name);
152 bitmap i = m_def_chain[SSA_NAME_VERSION (name)].m_import;
153 return i;
154 }
155
156 // Return true if IMPORT is an import to NAMEs def chain.
157
158 bool
159 range_def_chain::chain_import_p (tree name, tree import)
160 {
161 bitmap b = get_imports (name);
162 if (b)
163 return bitmap_bit_p (b, SSA_NAME_VERSION (import));
164 return false;
165 }
166
167 // Build def_chains for NAME if it is in BB. Copy the def chain into RESULT.
168
169 void
170 range_def_chain::register_dependency (tree name, tree dep, basic_block bb)
171 {
172 if (!gimple_range_ssa_p (dep))
173 return;
174
175 unsigned v = SSA_NAME_VERSION (name);
176 if (v >= m_def_chain.length ())
177 m_def_chain.safe_grow_cleared (num_ssa_names + 1);
178 struct rdc &src = m_def_chain[v];
179 gimple *def_stmt = SSA_NAME_DEF_STMT (dep);
180 unsigned dep_v = SSA_NAME_VERSION (dep);
181 bitmap b;
182
183 // Set the direct dependency cache entries.
184 if (!src.ssa1)
185 src.ssa1 = SSA_NAME_VERSION (dep);
186 else if (!src.ssa2 && src.ssa1 != SSA_NAME_VERSION (dep))
187 src.ssa2 = SSA_NAME_VERSION (dep);
188
189 // Don't calculate imports or export/dep chains if BB is not provided.
190 // This is usually the case for when the temporal cache wants the direct
191 // dependencies of a stmt.
192 if (!bb)
193 return;
194
195 if (!src.bm)
196 src.bm = BITMAP_ALLOC (&m_bitmaps);
197
198 // Add this operand into the result.
199 bitmap_set_bit (src.bm, dep_v);
200
201 if (gimple_bb (def_stmt) == bb && !is_a<gphi *>(def_stmt))
202 {
203 // Get the def chain for the operand.
204 b = get_def_chain (dep);
205 // If there was one, copy it into result. Access def_chain directly
206 // as the get_def_chain request above could reallocate the vector.
207 if (b)
208 bitmap_ior_into (m_def_chain[v].bm, b);
209 // And copy the import list.
210 set_import (m_def_chain[v], NULL_TREE, get_imports (dep));
211 }
212 else
213 // Originated outside the block, so it is an import.
214 set_import (src, dep, NULL);
215 }
216
217 bool
218 range_def_chain::def_chain_in_bitmap_p (tree name, bitmap b)
219 {
220 bitmap a = get_def_chain (name);
221 if (a && b)
222 return bitmap_intersect_p (a, b);
223 return false;
224 }
225
226 void
227 range_def_chain::add_def_chain_to_bitmap (bitmap b, tree name)
228 {
229 bitmap r = get_def_chain (name);
230 if (r)
231 bitmap_ior_into (b, r);
232 }
233
234
235 // Return TRUE if NAME has been processed for a def_chain.
236
237 inline bool
238 range_def_chain::has_def_chain (tree name)
239 {
240 // Ensure there is an entry in the internal vector.
241 unsigned v = SSA_NAME_VERSION (name);
242 if (v >= m_def_chain.length ())
243 m_def_chain.safe_grow_cleared (num_ssa_names + 1);
244 return (m_def_chain[v].ssa1 != 0);
245 }
246
247
248
249 // Calculate the def chain for NAME and all of its dependent
250 // operands. Only using names in the same BB. Return the bitmap of
251 // all names in the m_def_chain. This only works for supported range
252 // statements.
253
254 bitmap
255 range_def_chain::get_def_chain (tree name)
256 {
257 tree ssa[3];
258 unsigned v = SSA_NAME_VERSION (name);
259
260 // If it has already been processed, just return the cached value.
261 if (has_def_chain (name) && m_def_chain[v].bm)
262 return m_def_chain[v].bm;
263
264 // No definition chain for default defs.
265 if (SSA_NAME_IS_DEFAULT_DEF (name))
266 {
267 // A Default def is always an import.
268 set_import (m_def_chain[v], name, NULL);
269 return NULL;
270 }
271
272 gimple *stmt = SSA_NAME_DEF_STMT (name);
273 unsigned count = gimple_range_ssa_names (ssa, 3, stmt);
274 if (count == 0)
275 {
276 // Stmts not understood or with no operands are always imports.
277 set_import (m_def_chain[v], name, NULL);
278 return NULL;
279 }
280
281 // Terminate the def chains if we see too many cascading stmts.
282 if (m_logical_depth == param_ranger_logical_depth)
283 return NULL;
284
285 // Increase the depth if we have a pair of ssa-names.
286 if (count > 1)
287 m_logical_depth++;
288
289 for (unsigned x = 0; x < count; x++)
290 register_dependency (name, ssa[x], gimple_bb (stmt));
291
292 if (count > 1)
293 m_logical_depth--;
294
295 return m_def_chain[v].bm;
296 }
297
298 // Dump what we know for basic block BB to file F.
299
300 void
301 range_def_chain::dump (FILE *f, basic_block bb, const char *prefix)
302 {
303 unsigned x, y;
304 bitmap_iterator bi;
305
306 // Dump the def chain for each SSA_NAME defined in BB.
307 for (x = 1; x < num_ssa_names; x++)
308 {
309 tree name = ssa_name (x);
310 if (!name)
311 continue;
312 gimple *stmt = SSA_NAME_DEF_STMT (name);
313 if (!stmt || (bb && gimple_bb (stmt) != bb))
314 continue;
315 bitmap chain = (has_def_chain (name) ? get_def_chain (name) : NULL);
316 if (chain && !bitmap_empty_p (chain))
317 {
318 fprintf (f, prefix);
319 print_generic_expr (f, name, TDF_SLIM);
320 fprintf (f, " : ");
321
322 bitmap imports = get_imports (name);
323 EXECUTE_IF_SET_IN_BITMAP (chain, 0, y, bi)
324 {
325 print_generic_expr (f, ssa_name (y), TDF_SLIM);
326 if (imports && bitmap_bit_p (imports, y))
327 fprintf (f, "(I)");
328 fprintf (f, " ");
329 }
330 fprintf (f, "\n");
331 }
332 }
333 }
334
335
336 // -------------------------------------------------------------------
337
338 /* GORI_MAP is used to accumulate what SSA names in a block can
339 generate range information, and provides tools for the block ranger
340 to enable it to efficiently calculate these ranges.
341
342 GORI stands for "Generates Outgoing Range Information."
343
344 It utilizes the range_def_chain class to construct def_chains.
345 Information for a basic block is calculated once and stored. It is
346 only calculated the first time a query is made. If no queries are
347 made, there is little overhead.
348
349 one bitmap is maintained for each basic block:
350 m_outgoing : a set bit indicates a range can be generated for a name.
351
352 Generally speaking, the m_outgoing vector is the union of the
353 entire def_chain of all SSA names used in the last statement of the
354 block which generate ranges. */
355
356
357 // Initialize a gori-map structure.
358
359 gori_map::gori_map ()
360 {
361 m_outgoing.create (0);
362 m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
363 m_incoming.create (0);
364 m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
365 m_maybe_variant = BITMAP_ALLOC (&m_bitmaps);
366 }
367
368 // Free any memory the GORI map allocated.
369
370 gori_map::~gori_map ()
371 {
372 m_incoming.release ();
373 m_outgoing.release ();
374 }
375
376 // Return the bitmap vector of all export from BB. Calculate if necessary.
377
378 bitmap
379 gori_map::exports (basic_block bb)
380 {
381 if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
382 calculate_gori (bb);
383 return m_outgoing[bb->index];
384 }
385
386 // Return the bitmap vector of all exports AND their dependencies from BB
387 // in TMPBIT. Calculate if necessary. Return TMPBIT.
388
389 bitmap
390 gori_map::exports_and_deps (basic_block bb, bitmap tmpbit)
391 {
392 if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
393 calculate_gori (bb);
394 bitmap_copy (tmpbit, m_outgoing[bb->index]);
395 if (!bitmap_empty_p (tmpbit))
396 {
397 tree name;
398 FOR_EACH_GORI_EXPORT_NAME (this, bb, name)
399 {
400 bitmap dep = get_def_chain (name);
401 if (dep)
402 bitmap_ior_into (tmpbit, dep);
403 }
404 }
405 return tmpbit;
406 }
407
408 // Return the bitmap vector of all imports to BB. Calculate if necessary.
409
410 bitmap
411 gori_map::imports (basic_block bb)
412 {
413 if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
414 calculate_gori (bb);
415 return m_incoming[bb->index];
416 }
417
418 // Return true if NAME is can have ranges generated for it from basic
419 // block BB.
420
421 bool
422 gori_map::is_export_p (tree name, basic_block bb)
423 {
424 // If no BB is specified, test if it is exported anywhere in the IL.
425 if (!bb)
426 return bitmap_bit_p (m_maybe_variant, SSA_NAME_VERSION (name));
427 return bitmap_bit_p (exports (bb), SSA_NAME_VERSION (name));
428 }
429
430 // Set or clear the m_maybe_variant bit to determine if ranges will be tracked
431 // for NAME. A clear bit means they will NOT be tracked.
432
433 void
434 gori_map::set_range_invariant (tree name, bool invariant)
435 {
436 if (invariant)
437 bitmap_clear_bit (m_maybe_variant, SSA_NAME_VERSION (name));
438 else
439 bitmap_set_bit (m_maybe_variant, SSA_NAME_VERSION (name));
440 }
441
442 // Return true if NAME is an import to block BB.
443
444 bool
445 gori_map::is_import_p (tree name, basic_block bb)
446 {
447 // If no BB is specified, test if it is exported anywhere in the IL.
448 return bitmap_bit_p (imports (bb), SSA_NAME_VERSION (name));
449 }
450
451 // If NAME is non-NULL and defined in block BB, calculate the def
452 // chain and add it to m_outgoing.
453
454 void
455 gori_map::maybe_add_gori (tree name, basic_block bb)
456 {
457 if (name)
458 {
459 // Check if there is a def chain, regardless of the block.
460 add_def_chain_to_bitmap (m_outgoing[bb->index], name);
461 // Check for any imports.
462 bitmap imp = get_imports (name);
463 // If there were imports, add them so we can recompute
464 if (imp)
465 bitmap_ior_into (m_incoming[bb->index], imp);
466 // This name is always an import.
467 if (gimple_bb (SSA_NAME_DEF_STMT (name)) != bb)
468 bitmap_set_bit (m_incoming[bb->index], SSA_NAME_VERSION (name));
469
470 // Def chain doesn't include itself, and even if there isn't a
471 // def chain, this name should be added to exports.
472 bitmap_set_bit (m_outgoing[bb->index], SSA_NAME_VERSION (name));
473 }
474 }
475
476 // Calculate all the required information for BB.
477
478 void
479 gori_map::calculate_gori (basic_block bb)
480 {
481 tree name;
482 if (bb->index >= (signed int)m_outgoing.length ())
483 {
484 m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
485 m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
486 }
487 gcc_checking_assert (m_outgoing[bb->index] == NULL);
488 m_outgoing[bb->index] = BITMAP_ALLOC (&m_bitmaps);
489 m_incoming[bb->index] = BITMAP_ALLOC (&m_bitmaps);
490
491 if (single_succ_p (bb))
492 return;
493
494 // If this block's last statement may generate range information, go
495 // calculate it.
496 gimple *stmt = gimple_outgoing_range_stmt_p (bb);
497 if (!stmt)
498 return;
499 if (is_a<gcond *> (stmt))
500 {
501 gcond *gc = as_a<gcond *>(stmt);
502 name = gimple_range_ssa_p (gimple_cond_lhs (gc));
503 maybe_add_gori (name, gimple_bb (stmt));
504
505 name = gimple_range_ssa_p (gimple_cond_rhs (gc));
506 maybe_add_gori (name, gimple_bb (stmt));
507 }
508 else
509 {
510 // Do not process switches if they are too large.
511 if (EDGE_COUNT (bb->succs) > (unsigned)param_vrp_switch_limit)
512 return;
513 gswitch *gs = as_a<gswitch *>(stmt);
514 name = gimple_range_ssa_p (gimple_switch_index (gs));
515 maybe_add_gori (name, gimple_bb (stmt));
516 }
517 // Add this bitmap to the aggregate list of all outgoing names.
518 bitmap_ior_into (m_maybe_variant, m_outgoing[bb->index]);
519 }
520
521 // Dump the table information for BB to file F.
522
523 void
524 gori_map::dump (FILE *f, basic_block bb, bool verbose)
525 {
526 // BB was not processed.
527 if (!m_outgoing[bb->index] || bitmap_empty_p (m_outgoing[bb->index]))
528 return;
529
530 tree name;
531
532 bitmap imp = imports (bb);
533 if (!bitmap_empty_p (imp))
534 {
535 if (verbose)
536 fprintf (f, "bb<%u> Imports: ",bb->index);
537 else
538 fprintf (f, "Imports: ");
539 FOR_EACH_GORI_IMPORT_NAME (this, bb, name)
540 {
541 print_generic_expr (f, name, TDF_SLIM);
542 fprintf (f, " ");
543 }
544 fputc ('\n', f);
545 }
546
547 if (verbose)
548 fprintf (f, "bb<%u> Exports: ",bb->index);
549 else
550 fprintf (f, "Exports: ");
551 // Dump the export vector.
552 FOR_EACH_GORI_EXPORT_NAME (this, bb, name)
553 {
554 print_generic_expr (f, name, TDF_SLIM);
555 fprintf (f, " ");
556 }
557 fputc ('\n', f);
558
559 range_def_chain::dump (f, bb, " ");
560 }
561
562 // Dump the entire GORI map structure to file F.
563
564 void
565 gori_map::dump (FILE *f)
566 {
567 basic_block bb;
568 FOR_EACH_BB_FN (bb, cfun)
569 dump (f, bb);
570 }
571
572 DEBUG_FUNCTION void
573 debug (gori_map &g)
574 {
575 g.dump (stderr);
576 }
577
578 // -------------------------------------------------------------------
579
580 // Construct a gori_compute object.
581
582 gori_compute::gori_compute (gori_map &map, int not_executable_flag,
583 int sw_max_edges)
584 : gimple_outgoing_range (sw_max_edges), m_map (map), tracer ("GORI ")
585 {
586 m_not_executable_flag = not_executable_flag;
587 // Create a boolean_type true and false range.
588 m_bool_zero = range_false ();
589 m_bool_one = range_true ();
590 if (dump_file && (param_ranger_debug & RANGER_DEBUG_GORI))
591 tracer.enable_trace ();
592
593 // Reduce maximum recompute depth based on the size of the CFG to avoid
594 // excessive compuations in large CFGs.
595 m_recompute_depth = (int) param_ranger_recompute_depth
596 - (int) last_basic_block_for_fn (cfun) / 4096;
597 if (m_recompute_depth < 1)
598 m_recompute_depth = 1;
599 }
600
601 gori_compute::~gori_compute ()
602 {
603 }
604
605 // Given the switch S, return an evaluation in R for NAME when the lhs
606 // evaluates to LHS. Returning false means the name being looked for
607 // was not resolvable.
608
609 bool
610 gori_compute::compute_operand_range_switch (vrange &r, gswitch *s,
611 const vrange &lhs,
612 tree name, fur_source &src)
613 {
614 tree op1 = gimple_switch_index (s);
615
616 // If name matches, the range is simply the range from the edge.
617 // Empty ranges are viral as they are on a path which isn't
618 // executable.
619 if (op1 == name || lhs.undefined_p ())
620 {
621 r = lhs;
622 return true;
623 }
624
625 // If op1 is in the definition chain, pass lhs back.
626 if (gimple_range_ssa_p (op1) && m_map.in_chain_p (name, op1))
627 return compute_operand_range (r, SSA_NAME_DEF_STMT (op1), lhs, name, src);
628
629 return false;
630 }
631
632
633 // Return an evaluation for NAME as it would appear in STMT when the
634 // statement's lhs evaluates to LHS. If successful, return TRUE and
635 // store the evaluation in R, otherwise return FALSE.
636
637 bool
638 gori_compute::compute_operand_range (vrange &r, gimple *stmt,
639 const vrange &lhs, tree name,
640 fur_source &src, value_relation *rel)
641 {
642 value_relation vrel;
643 value_relation *vrel_ptr = rel;
644 // Empty ranges are viral as they are on an unexecutable path.
645 if (lhs.undefined_p ())
646 {
647 r.set_undefined ();
648 return true;
649 }
650 if (is_a<gswitch *> (stmt))
651 return compute_operand_range_switch (r, as_a<gswitch *> (stmt), lhs, name,
652 src);
653 gimple_range_op_handler handler (stmt);
654 if (!handler)
655 return false;
656
657 tree op1 = gimple_range_ssa_p (handler.operand1 ());
658 tree op2 = gimple_range_ssa_p (handler.operand2 ());
659
660 // If there is a relation betwen op1 and op2, use it instead as it is
661 // likely to be more applicable.
662 if (op1 && op2)
663 {
664 value_range r1, r2;
665 r1.set_varying (TREE_TYPE (op1));
666 r2.set_varying (TREE_TYPE (op2));
667 relation_kind k = handler.op1_op2_relation (lhs, r1, r2);
668 if (k != VREL_VARYING)
669 {
670 vrel.set_relation (k, op1, op2);
671 vrel_ptr = &vrel;
672 }
673 }
674
675 // Handle end of lookup first.
676 if (op1 == name)
677 return compute_operand1_range (r, handler, lhs, src, vrel_ptr);
678 if (op2 == name)
679 return compute_operand2_range (r, handler, lhs, src, vrel_ptr);
680
681 // NAME is not in this stmt, but one of the names in it ought to be
682 // derived from it.
683 bool op1_in_chain = op1 && m_map.in_chain_p (name, op1);
684 bool op2_in_chain = op2 && m_map.in_chain_p (name, op2);
685
686 // If neither operand is derived, then this stmt tells us nothing.
687 if (!op1_in_chain && !op2_in_chain)
688 return false;
689
690 // If either operand is in the def chain of the other (or they are equal), it
691 // will be evaluated twice and can result in an exponential time calculation.
692 // Instead just evaluate the one operand.
693 if (op1_in_chain && op2_in_chain)
694 {
695 if (m_map.in_chain_p (op1, op2) || op1 == op2)
696 op1_in_chain = false;
697 else if (m_map.in_chain_p (op2, op1))
698 op2_in_chain = false;
699 }
700
701 bool res = false;
702 // If the lhs doesn't tell us anything only a relation can possibly enhance
703 // the result.
704 if (lhs.varying_p ())
705 {
706 if (!vrel_ptr)
707 return false;
708 // If there is a relation (ie: x != y) , it can only be relevant if
709 // a) both elements are in the defchain
710 // c = x > y // (x and y are in c's defchain)
711 if (op1_in_chain)
712 res = m_map.in_chain_p (vrel_ptr->op1 (), op1)
713 && m_map.in_chain_p (vrel_ptr->op2 (), op1);
714 if (!res && op2_in_chain)
715 res = m_map.in_chain_p (vrel_ptr->op1 (), op2)
716 || m_map.in_chain_p (vrel_ptr->op2 (), op2);
717 if (!res)
718 {
719 // or b) one relation element is in the defchain of the other and the
720 // other is the LHS of this stmt.
721 // x = y + 2
722 if (vrel_ptr->op1 () == handler.lhs ()
723 && (vrel_ptr->op2 () == op1 || vrel_ptr->op2 () == op2))
724 res = true;
725 else if (vrel_ptr->op2 () == handler.lhs ()
726 && (vrel_ptr->op1 () == op1 || vrel_ptr->op1 () == op2))
727 res = true;
728 }
729 if (!res)
730 return false;
731 }
732
733 // Process logicals as they have special handling.
734 if (is_gimple_logical_p (stmt))
735 {
736 // If the lhs doesn't tell us anything, neither will combining operands.
737 if (lhs.varying_p ())
738 return false;
739
740 unsigned idx;
741 if ((idx = tracer.header ("compute_operand ")))
742 {
743 print_generic_expr (dump_file, name, TDF_SLIM);
744 fprintf (dump_file, " with LHS = ");
745 lhs.dump (dump_file);
746 fprintf (dump_file, " at stmt ");
747 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
748 }
749
750 tree type = TREE_TYPE (name);
751 value_range op1_trange (type), op1_frange (type);
752 value_range op2_trange (type), op2_frange (type);
753 compute_logical_operands (op1_trange, op1_frange, handler,
754 as_a <irange> (lhs),
755 name, src, op1, op1_in_chain);
756 compute_logical_operands (op2_trange, op2_frange, handler,
757 as_a <irange> (lhs),
758 name, src, op2, op2_in_chain);
759 res = logical_combine (r,
760 gimple_expr_code (stmt),
761 as_a <irange> (lhs),
762 op1_trange, op1_frange, op2_trange, op2_frange);
763 if (idx)
764 tracer.trailer (idx, "compute_operand", res, name, r);
765 return res;
766 }
767 // Follow the appropriate operands now.
768 if (op1_in_chain && op2_in_chain)
769 return compute_operand1_and_operand2_range (r, handler, lhs, name, src,
770 vrel_ptr);
771 value_range vr;
772 gimple *src_stmt;
773 if (op1_in_chain)
774 {
775 vr.set_type (TREE_TYPE (op1));
776 if (!compute_operand1_range (vr, handler, lhs, src, vrel_ptr))
777 return false;
778 src_stmt = SSA_NAME_DEF_STMT (op1);
779 }
780 else
781 {
782 gcc_checking_assert (op2_in_chain);
783 vr.set_type (TREE_TYPE (op2));
784 if (!compute_operand2_range (vr, handler, lhs, src, vrel_ptr))
785 return false;
786 src_stmt = SSA_NAME_DEF_STMT (op2);
787 }
788
789 gcc_checking_assert (src_stmt);
790 // Then feed this range back as the LHS of the defining statement.
791 return compute_operand_range (r, src_stmt, vr, name, src, vrel_ptr);
792 // If neither operand is derived, this statement tells us nothing.
793 }
794
795
796 // Return TRUE if range R is either a true or false compatible range.
797
798 static bool
799 range_is_either_true_or_false (const irange &r)
800 {
801 if (r.undefined_p ())
802 return false;
803
804 // This is complicated by the fact that Ada has multi-bit booleans,
805 // so true can be ~[0, 0] (i.e. [1,MAX]).
806 tree type = r.type ();
807 gcc_checking_assert (range_compatible_p (type, boolean_type_node));
808 return (r.singleton_p ()
809 || !r.contains_p (wi::zero (TYPE_PRECISION (type))));
810 }
811
812 // Evaluate a binary logical expression by combining the true and
813 // false ranges for each of the operands based on the result value in
814 // the LHS.
815
816 bool
817 gori_compute::logical_combine (vrange &r, enum tree_code code,
818 const irange &lhs,
819 const vrange &op1_true, const vrange &op1_false,
820 const vrange &op2_true, const vrange &op2_false)
821 {
822 if (op1_true.varying_p () && op1_false.varying_p ()
823 && op2_true.varying_p () && op2_false.varying_p ())
824 return false;
825
826 unsigned idx;
827 if ((idx = tracer.header ("logical_combine")))
828 {
829 switch (code)
830 {
831 case TRUTH_OR_EXPR:
832 case BIT_IOR_EXPR:
833 fprintf (dump_file, " || ");
834 break;
835 case TRUTH_AND_EXPR:
836 case BIT_AND_EXPR:
837 fprintf (dump_file, " && ");
838 break;
839 default:
840 break;
841 }
842 fprintf (dump_file, " with LHS = ");
843 lhs.dump (dump_file);
844 fputc ('\n', dump_file);
845
846 tracer.print (idx, "op1_true = ");
847 op1_true.dump (dump_file);
848 fprintf (dump_file, " op1_false = ");
849 op1_false.dump (dump_file);
850 fputc ('\n', dump_file);
851 tracer.print (idx, "op2_true = ");
852 op2_true.dump (dump_file);
853 fprintf (dump_file, " op2_false = ");
854 op2_false.dump (dump_file);
855 fputc ('\n', dump_file);
856 }
857
858 // This is not a simple fold of a logical expression, rather it
859 // determines ranges which flow through the logical expression.
860 //
861 // Assuming x_8 is an unsigned char, and relational statements:
862 // b_1 = x_8 < 20
863 // b_2 = x_8 > 5
864 // consider the logical expression and branch:
865 // c_2 = b_1 && b_2
866 // if (c_2)
867 //
868 // To determine the range of x_8 on either edge of the branch, one
869 // must first determine what the range of x_8 is when the boolean
870 // values of b_1 and b_2 are both true and false.
871 // b_1 TRUE x_8 = [0, 19]
872 // b_1 FALSE x_8 = [20, 255]
873 // b_2 TRUE x_8 = [6, 255]
874 // b_2 FALSE x_8 = [0,5].
875 //
876 // These ranges are then combined based on the expected outcome of
877 // the branch. The range on the TRUE side of the branch must satisfy
878 // b_1 == true && b_2 == true
879 //
880 // In terms of x_8, that means both x_8 == [0, 19] and x_8 = [6, 255]
881 // must be true. The range of x_8 on the true side must be the
882 // intersection of both ranges since both must be true. Thus the
883 // range of x_8 on the true side is [6, 19].
884 //
885 // To determine the ranges on the FALSE side, all 3 combinations of
886 // failing ranges must be considered, and combined as any of them
887 // can cause the false result.
888 //
889 // If the LHS can be TRUE or FALSE, then evaluate both a TRUE and
890 // FALSE results and combine them. If we fell back to VARYING any
891 // range restrictions that have been discovered up to this point
892 // would be lost.
893 if (!range_is_either_true_or_false (lhs))
894 {
895 bool res;
896 value_range r1 (r);
897 if (logical_combine (r1, code, m_bool_zero, op1_true, op1_false,
898 op2_true, op2_false)
899 && logical_combine (r, code, m_bool_one, op1_true, op1_false,
900 op2_true, op2_false))
901 {
902 r.union_ (r1);
903 res = true;
904 }
905 else
906 res = false;
907 if (idx && res)
908 {
909 tracer.print (idx, "logical_combine produced ");
910 r.dump (dump_file);
911 fputc ('\n', dump_file);
912 }
913 return res;
914 }
915
916 switch (code)
917 {
918 // A logical AND combines ranges from 2 boolean conditions.
919 // c_2 = b_1 && b_2
920 case TRUTH_AND_EXPR:
921 case BIT_AND_EXPR:
922 if (!lhs.zero_p ())
923 {
924 // The TRUE side is the intersection of the 2 true ranges.
925 r = op1_true;
926 r.intersect (op2_true);
927 }
928 else
929 {
930 // The FALSE side is the union of the other 3 cases.
931 value_range ff (op1_false);
932 ff.intersect (op2_false);
933 value_range tf (op1_true);
934 tf.intersect (op2_false);
935 value_range ft (op1_false);
936 ft.intersect (op2_true);
937 r = ff;
938 r.union_ (tf);
939 r.union_ (ft);
940 }
941 break;
942 // A logical OR combines ranges from 2 boolean conditions.
943 // c_2 = b_1 || b_2
944 case TRUTH_OR_EXPR:
945 case BIT_IOR_EXPR:
946 if (lhs.zero_p ())
947 {
948 // An OR operation will only take the FALSE path if both
949 // operands are false simultaneously, which means they should
950 // be intersected. !(x || y) == !x && !y
951 r = op1_false;
952 r.intersect (op2_false);
953 }
954 else
955 {
956 // The TRUE side of an OR operation will be the union of
957 // the other three combinations.
958 value_range tt (op1_true);
959 tt.intersect (op2_true);
960 value_range tf (op1_true);
961 tf.intersect (op2_false);
962 value_range ft (op1_false);
963 ft.intersect (op2_true);
964 r = tt;
965 r.union_ (tf);
966 r.union_ (ft);
967 }
968 break;
969 default:
970 gcc_unreachable ();
971 }
972
973 if (idx)
974 tracer.trailer (idx, "logical_combine", true, NULL_TREE, r);
975 return true;
976 }
977
978
979 // Given a logical STMT, calculate true and false ranges for each
980 // potential path of NAME, assuming NAME came through the OP chain if
981 // OP_IN_CHAIN is true.
982
983 void
984 gori_compute::compute_logical_operands (vrange &true_range, vrange &false_range,
985 gimple_range_op_handler &handler,
986 const irange &lhs,
987 tree name, fur_source &src,
988 tree op, bool op_in_chain)
989 {
990 gimple *stmt = handler.stmt ();
991 gimple *src_stmt = gimple_range_ssa_p (op) ? SSA_NAME_DEF_STMT (op) : NULL;
992 if (!op_in_chain || !src_stmt || m_map.chain_import_p (handler.lhs (), op))
993 {
994 // If op is not in the def chain, or defined in this block,
995 // use its known value on entry to the block.
996 src.get_operand (true_range, name);
997 false_range = true_range;
998 unsigned idx;
999 if ((idx = tracer.header ("logical_operand")))
1000 {
1001 print_generic_expr (dump_file, op, TDF_SLIM);
1002 fprintf (dump_file, " not in computation chain. Queried.\n");
1003 tracer.trailer (idx, "logical_operand", true, NULL_TREE, true_range);
1004 }
1005 return;
1006 }
1007
1008 enum tree_code code = gimple_expr_code (stmt);
1009 // Optimize [0 = x | y], since neither operand can ever be non-zero.
1010 if ((code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) && lhs.zero_p ())
1011 {
1012 if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name,
1013 src))
1014 src.get_operand (false_range, name);
1015 true_range = false_range;
1016 return;
1017 }
1018
1019 // Optimize [1 = x & y], since neither operand can ever be zero.
1020 if ((code == BIT_AND_EXPR || code == TRUTH_AND_EXPR) && lhs == m_bool_one)
1021 {
1022 if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
1023 src.get_operand (true_range, name);
1024 false_range = true_range;
1025 return;
1026 }
1027
1028 // Calculate ranges for true and false on both sides, since the false
1029 // path is not always a simple inversion of the true side.
1030 if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
1031 src.get_operand (true_range, name);
1032 if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name, src))
1033 src.get_operand (false_range, name);
1034 }
1035
1036
1037 // This routine will try to refine the ranges of OP1 and OP2 given a relation
1038 // K between them. In order to perform this refinement, one of the operands
1039 // must be in the definition chain of the other. The use is refined using
1040 // op1/op2_range on the statement, and the definition is then recalculated
1041 // using the relation.
1042
1043 bool
1044 gori_compute::refine_using_relation (tree op1, vrange &op1_range,
1045 tree op2, vrange &op2_range,
1046 fur_source &src, relation_kind k)
1047 {
1048 gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
1049 gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
1050
1051 if (k == VREL_VARYING || k == VREL_EQ || k == VREL_UNDEFINED)
1052 return false;
1053
1054 bool change = false;
1055 bool op1_def_p = m_map.in_chain_p (op2, op1);
1056 if (!op1_def_p)
1057 if (!m_map.in_chain_p (op1, op2))
1058 return false;
1059
1060 tree def_op = op1_def_p ? op1 : op2;
1061 tree use_op = op1_def_p ? op2 : op1;
1062
1063 if (!op1_def_p)
1064 k = relation_swap (k);
1065
1066 // op1_def is true if we want to look up op1, otherwise we want op2.
1067 // if neither is the case, we returned in the above check.
1068
1069 gimple *def_stmt = SSA_NAME_DEF_STMT (def_op);
1070 gimple_range_op_handler op_handler (def_stmt);
1071 if (!op_handler)
1072 return false;
1073 tree def_op1 = op_handler.operand1 ();
1074 tree def_op2 = op_handler.operand2 ();
1075 // if the def isn't binary, the relation will not be useful.
1076 if (!def_op2)
1077 return false;
1078
1079 // Determine if op2 is directly referenced as an operand.
1080 if (def_op1 == use_op)
1081 {
1082 // def_stmt has op1 in the 1st operand position.
1083 value_range other_op (TREE_TYPE (def_op2));
1084 src.get_operand (other_op, def_op2);
1085
1086 // Using op1_range as the LHS, and relation REL, evaluate op2.
1087 tree type = TREE_TYPE (def_op1);
1088 value_range new_result (type);
1089 if (!op_handler.op1_range (new_result, type,
1090 op1_def_p ? op1_range : op2_range,
1091 other_op, relation_trio::lhs_op1 (k)))
1092 return false;
1093 if (op1_def_p)
1094 {
1095 change |= op2_range.intersect (new_result);
1096 // Recalculate op2.
1097 if (op_handler.fold_range (new_result, type, op2_range, other_op))
1098 {
1099 change |= op1_range.intersect (new_result);
1100 }
1101 }
1102 else
1103 {
1104 change |= op1_range.intersect (new_result);
1105 // Recalculate op1.
1106 if (op_handler.fold_range (new_result, type, op1_range, other_op))
1107 {
1108 change |= op2_range.intersect (new_result);
1109 }
1110 }
1111 }
1112 else if (def_op2 == use_op)
1113 {
1114 // def_stmt has op1 in the 1st operand position.
1115 value_range other_op (TREE_TYPE (def_op1));
1116 src.get_operand (other_op, def_op1);
1117
1118 // Using op1_range as the LHS, and relation REL, evaluate op2.
1119 tree type = TREE_TYPE (def_op2);
1120 value_range new_result (type);
1121 if (!op_handler.op2_range (new_result, type,
1122 op1_def_p ? op1_range : op2_range,
1123 other_op, relation_trio::lhs_op2 (k)))
1124 return false;
1125 if (op1_def_p)
1126 {
1127 change |= op2_range.intersect (new_result);
1128 // Recalculate op1.
1129 if (op_handler.fold_range (new_result, type, other_op, op2_range))
1130 {
1131 change |= op1_range.intersect (new_result);
1132 }
1133 }
1134 else
1135 {
1136 change |= op1_range.intersect (new_result);
1137 // Recalculate op2.
1138 if (op_handler.fold_range (new_result, type, other_op, op1_range))
1139 {
1140 change |= op2_range.intersect (new_result);
1141 }
1142 }
1143 }
1144 return change;
1145 }
1146
1147 // Calculate a range for NAME from the operand 1 position of STMT
1148 // assuming the result of the statement is LHS. Return the range in
1149 // R, or false if no range could be calculated.
1150
1151 bool
1152 gori_compute::compute_operand1_range (vrange &r,
1153 gimple_range_op_handler &handler,
1154 const vrange &lhs,
1155 fur_source &src, value_relation *rel)
1156 {
1157 gimple *stmt = handler.stmt ();
1158 tree op1 = handler.operand1 ();
1159 tree op2 = handler.operand2 ();
1160 tree lhs_name = gimple_get_lhs (stmt);
1161
1162 relation_trio trio;
1163 if (rel)
1164 trio = rel->create_trio (lhs_name, op1, op2);
1165
1166 value_range op1_range (TREE_TYPE (op1));
1167 value_range op2_range (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
1168
1169 // Fetch the known range for op1 in this block.
1170 src.get_operand (op1_range, op1);
1171
1172 // Now range-op calculate and put that result in r.
1173 if (op2)
1174 {
1175 src.get_operand (op2_range, op2);
1176
1177 relation_kind op_op = trio.op1_op2 ();
1178 if (op_op != VREL_VARYING)
1179 refine_using_relation (op1, op1_range, op2, op2_range, src, op_op);
1180
1181 // If op1 == op2, create a new trio for just this call.
1182 if (op1 == op2 && gimple_range_ssa_p (op1))
1183 trio = relation_trio (trio.lhs_op1 (), trio.lhs_op2 (), VREL_EQ);
1184 if (!handler.calc_op1 (r, lhs, op2_range, trio))
1185 return false;
1186 }
1187 else
1188 {
1189 // We pass op1_range to the unary operation. Normally it's a
1190 // hidden range_for_type parameter, but sometimes having the
1191 // actual range can result in better information.
1192 if (!handler.calc_op1 (r, lhs, op1_range, trio))
1193 return false;
1194 }
1195
1196 unsigned idx;
1197 if ((idx = tracer.header ("compute op 1 (")))
1198 {
1199 print_generic_expr (dump_file, op1, TDF_SLIM);
1200 fprintf (dump_file, ") at ");
1201 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1202 tracer.print (idx, "LHS =");
1203 lhs.dump (dump_file);
1204 if (op2 && TREE_CODE (op2) == SSA_NAME)
1205 {
1206 fprintf (dump_file, ", ");
1207 print_generic_expr (dump_file, op2, TDF_SLIM);
1208 fprintf (dump_file, " = ");
1209 op2_range.dump (dump_file);
1210 }
1211 fprintf (dump_file, "\n");
1212 tracer.print (idx, "Computes ");
1213 print_generic_expr (dump_file, op1, TDF_SLIM);
1214 fprintf (dump_file, " = ");
1215 r.dump (dump_file);
1216 fprintf (dump_file, " intersect Known range : ");
1217 op1_range.dump (dump_file);
1218 fputc ('\n', dump_file);
1219 }
1220
1221 r.intersect (op1_range);
1222 if (idx)
1223 tracer.trailer (idx, "produces ", true, op1, r);
1224 return true;
1225 }
1226
1227
1228 // Calculate a range for NAME from the operand 2 position of S
1229 // assuming the result of the statement is LHS. Return the range in
1230 // R, or false if no range could be calculated.
1231
1232 bool
1233 gori_compute::compute_operand2_range (vrange &r,
1234 gimple_range_op_handler &handler,
1235 const vrange &lhs,
1236 fur_source &src, value_relation *rel)
1237 {
1238 gimple *stmt = handler.stmt ();
1239 tree op1 = handler.operand1 ();
1240 tree op2 = handler.operand2 ();
1241 tree lhs_name = gimple_get_lhs (stmt);
1242
1243 value_range op1_range (TREE_TYPE (op1));
1244 value_range op2_range (TREE_TYPE (op2));
1245
1246 src.get_operand (op1_range, op1);
1247 src.get_operand (op2_range, op2);
1248
1249 relation_trio trio;
1250 if (rel)
1251 trio = rel->create_trio (lhs_name, op1, op2);
1252 relation_kind op_op = trio.op1_op2 ();
1253
1254 if (op_op != VREL_VARYING)
1255 refine_using_relation (op1, op1_range, op2, op2_range, src, op_op);
1256
1257 // If op1 == op2, create a new trio for this stmt.
1258 if (op1 == op2 && gimple_range_ssa_p (op1))
1259 trio = relation_trio (trio.lhs_op1 (), trio.lhs_op2 (), VREL_EQ);
1260 // Intersect with range for op2 based on lhs and op1.
1261 if (!handler.calc_op2 (r, lhs, op1_range, trio))
1262 return false;
1263
1264 unsigned idx;
1265 if ((idx = tracer.header ("compute op 2 (")))
1266 {
1267 print_generic_expr (dump_file, op2, TDF_SLIM);
1268 fprintf (dump_file, ") at ");
1269 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1270 tracer.print (idx, "LHS = ");
1271 lhs.dump (dump_file);
1272 if (TREE_CODE (op1) == SSA_NAME)
1273 {
1274 fprintf (dump_file, ", ");
1275 print_generic_expr (dump_file, op1, TDF_SLIM);
1276 fprintf (dump_file, " = ");
1277 op1_range.dump (dump_file);
1278 }
1279 fprintf (dump_file, "\n");
1280 tracer.print (idx, "Computes ");
1281 print_generic_expr (dump_file, op2, TDF_SLIM);
1282 fprintf (dump_file, " = ");
1283 r.dump (dump_file);
1284 fprintf (dump_file, " intersect Known range : ");
1285 op2_range.dump (dump_file);
1286 fputc ('\n', dump_file);
1287 }
1288 // Intersect the calculated result with the known result and return if done.
1289 r.intersect (op2_range);
1290 if (idx)
1291 tracer.trailer (idx, " produces ", true, op2, r);
1292 return true;
1293 }
1294
1295 // Calculate a range for NAME from both operand positions of S
1296 // assuming the result of the statement is LHS. Return the range in
1297 // R, or false if no range could be calculated.
1298
1299 bool
1300 gori_compute::compute_operand1_and_operand2_range (vrange &r,
1301 gimple_range_op_handler
1302 &handler,
1303 const vrange &lhs,
1304 tree name,
1305 fur_source &src,
1306 value_relation *rel)
1307 {
1308 value_range op_range (TREE_TYPE (name));
1309
1310 value_range vr (TREE_TYPE (handler.operand2 ()));
1311 // Calculate a good a range through op2.
1312 if (!compute_operand2_range (vr, handler, lhs, src, rel))
1313 return false;
1314 gimple *src_stmt = SSA_NAME_DEF_STMT (handler.operand2 ());
1315 gcc_checking_assert (src_stmt);
1316 // Then feed this range back as the LHS of the defining statement.
1317 if (!compute_operand_range (r, src_stmt, vr, name, src, rel))
1318 return false;
1319
1320 // Now get the range thru op1.
1321 vr.set_type (TREE_TYPE (handler.operand1 ()));
1322 if (!compute_operand1_range (vr, handler, lhs, src, rel))
1323 return false;
1324 src_stmt = SSA_NAME_DEF_STMT (handler.operand1 ());
1325 gcc_checking_assert (src_stmt);
1326 // Then feed this range back as the LHS of the defining statement.
1327 if (!compute_operand_range (op_range, src_stmt, vr, name, src, rel))
1328 return false;
1329
1330 // Both operands have to be simultaneously true, so perform an intersection.
1331 r.intersect (op_range);
1332 return true;
1333 }
1334
1335 // Return TRUE if NAME can be recomputed on any edge exiting BB. If any
1336 // direct dependent is exported, it may also change the computed value of NAME.
1337
1338 bool
1339 gori_compute::may_recompute_p (tree name, basic_block bb, int depth)
1340 {
1341 tree dep1 = m_map.depend1 (name);
1342 tree dep2 = m_map.depend2 (name);
1343
1344 // If the first dependency is not set, there is no recomputation.
1345 // Dependencies reflect original IL, not current state. Check if the
1346 // SSA_NAME is still valid as well.
1347 if (!dep1)
1348 return false;
1349
1350 // Don't recalculate PHIs or statements with side_effects.
1351 gimple *s = SSA_NAME_DEF_STMT (name);
1352 if (is_a<gphi *> (s) || gimple_has_side_effects (s))
1353 return false;
1354
1355 if (!dep2)
1356 {
1357 // -1 indicates a default param, convert it to the real default.
1358 if (depth == -1)
1359 depth = m_recompute_depth;
1360
1361 bool res = m_map.is_export_p (dep1, bb);
1362 if (res || depth <= 1)
1363 return res;
1364 // Check another level of recomputation.
1365 return may_recompute_p (dep1, bb, --depth);
1366 }
1367 // Two dependencies terminate the depth of the search.
1368 return m_map.is_export_p (dep1, bb) || m_map.is_export_p (dep2, bb);
1369 }
1370
1371 // Return TRUE if NAME can be recomputed on edge E. If any direct dependent
1372 // is exported on edge E, it may change the computed value of NAME.
1373
1374 bool
1375 gori_compute::may_recompute_p (tree name, edge e, int depth)
1376 {
1377 gcc_checking_assert (e);
1378 return may_recompute_p (name, e->src, depth);
1379 }
1380
1381
1382 // Return TRUE if a range can be calculated or recomputed for NAME on any
1383 // edge exiting BB.
1384
1385 bool
1386 gori_compute::has_edge_range_p (tree name, basic_block bb)
1387 {
1388 // Check if NAME is an export or can be recomputed.
1389 if (bb)
1390 return m_map.is_export_p (name, bb) || may_recompute_p (name, bb);
1391
1392 // If no block is specified, check for anywhere in the IL.
1393 return m_map.is_export_p (name) || may_recompute_p (name);
1394 }
1395
1396 // Return TRUE if a range can be calculated or recomputed for NAME on edge E.
1397
1398 bool
1399 gori_compute::has_edge_range_p (tree name, edge e)
1400 {
1401 gcc_checking_assert (e);
1402 return has_edge_range_p (name, e->src);
1403 }
1404
1405 // Calculate a range on edge E and return it in R. Try to evaluate a
1406 // range for NAME on this edge. Return FALSE if this is either not a
1407 // control edge or NAME is not defined by this edge.
1408
1409 bool
1410 gori_compute::edge_range_p (vrange &r, edge e, tree name, range_query &q)
1411 {
1412 unsigned idx;
1413
1414 if ((e->flags & m_not_executable_flag))
1415 {
1416 r.set_undefined ();
1417 if (dump_file && (dump_flags & TDF_DETAILS))
1418 fprintf (dump_file, "Outgoing edge %d->%d unexecutable.\n",
1419 e->src->index, e->dest->index);
1420 return true;
1421 }
1422
1423 gcc_checking_assert (gimple_range_ssa_p (name));
1424 int_range_max lhs;
1425 // Determine if there is an outgoing edge.
1426 gimple *stmt = gimple_outgoing_range::edge_range_p (lhs, e);
1427 if (!stmt)
1428 return false;
1429
1430 fur_stmt src (stmt, &q);
1431 // If NAME can be calculated on the edge, use that.
1432 if (m_map.is_export_p (name, e->src))
1433 {
1434 bool res;
1435 if ((idx = tracer.header ("outgoing_edge")))
1436 {
1437 fprintf (dump_file, " for ");
1438 print_generic_expr (dump_file, name, TDF_SLIM);
1439 fprintf (dump_file, " on edge %d->%d\n",
1440 e->src->index, e->dest->index);
1441 }
1442 if ((res = compute_operand_range (r, stmt, lhs, name, src)))
1443 {
1444 // Sometimes compatible types get interchanged. See PR97360.
1445 // Make sure we are returning the type of the thing we asked for.
1446 if (!r.undefined_p () && r.type () != TREE_TYPE (name))
1447 {
1448 gcc_checking_assert (range_compatible_p (r.type (),
1449 TREE_TYPE (name)));
1450 range_cast (r, TREE_TYPE (name));
1451 }
1452 }
1453 if (idx)
1454 tracer.trailer (idx, "outgoing_edge", res, name, r);
1455 return res;
1456 }
1457 // If NAME isn't exported, check if it can be recomputed.
1458 else if (may_recompute_p (name, e))
1459 {
1460 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1461
1462 if ((idx = tracer.header ("recomputation")))
1463 {
1464 fprintf (dump_file, " attempt on edge %d->%d for ",
1465 e->src->index, e->dest->index);
1466 print_gimple_stmt (dump_file, def_stmt, 0, TDF_SLIM);
1467 }
1468 // Simply calculate DEF_STMT on edge E using the range query Q.
1469 fold_range (r, def_stmt, e, &q);
1470 if (idx)
1471 tracer.trailer (idx, "recomputation", true, name, r);
1472 return true;
1473 }
1474 return false;
1475 }
1476
1477 // Dump what is known to GORI computes to listing file F.
1478
1479 void
1480 gori_compute::dump (FILE *f)
1481 {
1482 m_map.gori_map::dump (f);
1483 }
1484
1485 // ------------------------------------------------------------------------
1486 // GORI iterator. Although we have bitmap iterators, don't expose that it
1487 // is currently a bitmap. Use an export iterator to hide future changes.
1488
1489 // Construct a basic iterator over an export bitmap.
1490
1491 gori_export_iterator::gori_export_iterator (bitmap b)
1492 {
1493 bm = b;
1494 if (b)
1495 bmp_iter_set_init (&bi, b, 1, &y);
1496 }
1497
1498
1499 // Move to the next export bitmap spot.
1500
1501 void
1502 gori_export_iterator::next ()
1503 {
1504 bmp_iter_next (&bi, &y);
1505 }
1506
1507
1508 // Fetch the name of the next export in the export list. Return NULL if
1509 // iteration is done.
1510
1511 tree
1512 gori_export_iterator::get_name ()
1513 {
1514 if (!bm)
1515 return NULL_TREE;
1516
1517 while (bmp_iter_set (&bi, &y))
1518 {
1519 tree t = ssa_name (y);
1520 if (t)
1521 return t;
1522 next ();
1523 }
1524 return NULL_TREE;
1525 }
1526
1527 // This is a helper class to set up STMT with a known LHS for further GORI
1528 // processing.
1529
1530 class gori_stmt_info : public gimple_range_op_handler
1531 {
1532 public:
1533 gori_stmt_info (vrange &lhs, gimple *stmt, range_query *q);
1534 value_range op1_range;
1535 value_range op2_range;
1536 tree ssa1;
1537 tree ssa2;
1538 };
1539
1540
1541 // Uses query Q to get the known ranges on STMT with a LHS range
1542 // for op1_range and op2_range and set ssa1 and ssa2 if either or both of
1543 // those operands are SSA_NAMES.
1544
1545 gori_stmt_info::gori_stmt_info (vrange &lhs, gimple *stmt, range_query *q)
1546 : gimple_range_op_handler (stmt)
1547 {
1548 ssa1 = NULL;
1549 ssa2 = NULL;
1550 // Don't handle switches as yet for vector processing.
1551 if (is_a<gswitch *> (stmt))
1552 return;
1553
1554 // No frther processing for VARYING or undefined.
1555 if (lhs.undefined_p () || lhs.varying_p ())
1556 return;
1557
1558 // If there is no range-op handler, we are also done.
1559 if (!*this)
1560 return;
1561
1562 // Only evaluate logical cases if both operands must be the same as the LHS.
1563 // Otherwise its becomes exponential in time, as well as more complicated.
1564 if (is_gimple_logical_p (stmt))
1565 {
1566 gcc_checking_assert (range_compatible_p (lhs.type (), boolean_type_node));
1567 enum tree_code code = gimple_expr_code (stmt);
1568 if (code == TRUTH_OR_EXPR || code == BIT_IOR_EXPR)
1569 {
1570 // [0, 0] = x || y means both x and y must be zero.
1571 if (!lhs.singleton_p () || !lhs.zero_p ())
1572 return;
1573 }
1574 else if (code == TRUTH_AND_EXPR || code == BIT_AND_EXPR)
1575 {
1576 // [1, 1] = x && y means both x and y must be one.
1577 if (!lhs.singleton_p () || lhs.zero_p ())
1578 return;
1579 }
1580 }
1581
1582 tree op1 = operand1 ();
1583 tree op2 = operand2 ();
1584 ssa1 = gimple_range_ssa_p (op1);
1585 ssa2 = gimple_range_ssa_p (op2);
1586 // If both operands are the same, only process one of them.
1587 if (ssa1 && ssa1 == ssa2)
1588 ssa2 = NULL_TREE;
1589
1590 // Extract current ranges for the operands.
1591 fur_stmt src (stmt, q);
1592 if (op1)
1593 {
1594 op1_range.set_type (TREE_TYPE (op1));
1595 src.get_operand (op1_range, op1);
1596 }
1597
1598 // And satisfy the second operand for single op satements.
1599 if (op2)
1600 {
1601 op2_range.set_type (TREE_TYPE (op2));
1602 src.get_operand (op2_range, op2);
1603 }
1604 else if (op1)
1605 op2_range = op1_range;
1606 return;
1607 }
1608
1609
1610 // Process STMT using LHS as the range of the LHS. Invoke GORI processing
1611 // to resolve ranges for all SSA_NAMES feeding STMT which may be altered
1612 // based on LHS. Fill R with the results, and resolve all incoming
1613 // ranges using range-query Q.
1614
1615 static void
1616 gori_calc_operands (vrange &lhs, gimple *stmt, ssa_cache &r, range_query *q)
1617 {
1618 struct gori_stmt_info si(lhs, stmt, q);
1619 if (!si)
1620 return;
1621
1622 value_range tmp;
1623 // Now evaluate operand ranges, and set them in the edge cache.
1624 // If there was already a range, leave it and do no further evaluation.
1625 if (si.ssa1 && !r.has_range (si.ssa1))
1626 {
1627 tmp.set_type (TREE_TYPE (si.ssa1));
1628 if (si.calc_op1 (tmp, lhs, si.op2_range))
1629 si.op1_range.intersect (tmp);
1630 if (!si.op1_range.varying_p ())
1631 {
1632 r.set_range (si.ssa1, si.op1_range);
1633 gimple *src = SSA_NAME_DEF_STMT (si.ssa1);
1634 // If defintion is in the same basic lock, evaluate it.
1635 if (src && gimple_bb (src) == gimple_bb (stmt))
1636 gori_calc_operands (si.op1_range, src, r, q);
1637 }
1638 }
1639
1640 if (si.ssa2 && !r.has_range (si.ssa2))
1641 {
1642 tmp.set_type (TREE_TYPE (si.ssa2));
1643 if (si.calc_op2 (tmp, lhs, si.op1_range))
1644 si.op2_range.intersect (tmp);
1645 if (!si.op2_range.varying_p ())
1646 {
1647 r.set_range (si.ssa2, si.op2_range);
1648 gimple *src = SSA_NAME_DEF_STMT (si.ssa2);
1649 if (src && gimple_bb (src) == gimple_bb (stmt))
1650 gori_calc_operands (si.op2_range, src, r, q);
1651 }
1652 }
1653 }
1654
1655 // Use ssa_cache R as a repository for all outgoing ranges on edge E that
1656 // can be calculated. Use Q to establish starting edge ranges anbd to resolve
1657 // operand values. If Q is NULL use the current range
1658 // query available to the system.
1659
1660 bool
1661 gori_on_edge (ssa_cache &r, edge e, range_query *q)
1662 {
1663 if (!q)
1664 q = get_range_query (cfun);
1665 // Start with an empty vector
1666 r.clear ();
1667 int_range_max lhs;
1668 // Determine if there is an outgoing edge.
1669 gimple *stmt = q->gori ().edge_range_p (lhs, e);
1670 if (!stmt)
1671 return false;
1672 gori_calc_operands (lhs, stmt, r, q);
1673 return true;
1674 }
1675
1676 // Helper for GORI_NAME_ON_EDGE which uses query Q to determine if STMT
1677 // provides a range for NAME, and returns it in R if so. If it does not,
1678 // continue processing feeding statments until we run out of statements
1679 // or fine a range for NAME.
1680
1681 bool
1682 gori_name_helper (vrange &r, tree name, vrange &lhs, gimple *stmt,
1683 range_query *q)
1684 {
1685 struct gori_stmt_info si(lhs, stmt, q);
1686 if (!si)
1687 return false;
1688
1689 if (si.ssa1 == name)
1690 return si.calc_op1 (r, lhs, si.op2_range);
1691 if (si.ssa2 == name)
1692 return si.calc_op2 (r, lhs, si.op1_range);
1693
1694 value_range tmp;
1695 // Now evaluate operand ranges, and set them in the edge cache.
1696 // If there was already a range, leave it and do no further evaluation.
1697 if (si.ssa1)
1698 {
1699 tmp.set_type (TREE_TYPE (si.ssa1));
1700 if (si.calc_op1 (tmp, lhs, si.op2_range))
1701 si.op1_range.intersect (tmp);
1702 gimple *src = SSA_NAME_DEF_STMT (si.ssa1);
1703 // If defintion is in the same basic lock, evaluate it.
1704 if (src && gimple_bb (src) == gimple_bb (stmt))
1705 if (gori_name_helper (r, name, si.op1_range, src, q))
1706 return true;
1707 }
1708
1709 if (si.ssa2)
1710 {
1711 tmp.set_type (TREE_TYPE (si.ssa2));
1712 if (si.calc_op2 (tmp, lhs, si.op1_range))
1713 si.op2_range.intersect (tmp);
1714 gimple *src = SSA_NAME_DEF_STMT (si.ssa2);
1715 if (src && gimple_bb (src) == gimple_bb (stmt))
1716 if (gori_name_helper (r, name, si.op2_range, src, q))
1717 return true;
1718 }
1719 return false;
1720 }
1721
1722 // Check if NAME has an outgoing range on edge E. Use query Q to evaluate
1723 // the operands. Return TRUE and the range in R if there is an outgoing range.
1724 // This is like gori_on_edge except it only looks for the single name and
1725 // does not require an ssa_cache.
1726
1727 bool
1728 gori_name_on_edge (vrange &r, tree name, edge e, range_query *q)
1729 {
1730 int_range_max lhs;
1731 gimple *stmt = gimple_outgoing_range_stmt_p (e->src);
1732 if (!stmt || !is_a<gcond *> (stmt))
1733 return false;
1734 gcond_edge_range (lhs, e);
1735 return gori_name_helper (r, name, lhs, stmt, q);
1736 }