]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gimple-range-gori.cc
Use precision and sign to compare types for ranges
[thirdparty/gcc.git] / gcc / gimple-range-gori.cc
1 /* Gimple range GORI functions.
2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
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
33 /* RANGE_DEF_CHAIN is used to determine what SSA names in a block can
34 have range information calculated for them, and what the
35 dependencies on each other are.
36
37 Information for a basic block is calculated once and stored. It is
38 only calculated the first time a query is made, so if no queries
39 are made, there is little overhead.
40
41 The def_chain bitmap is indexed by SSA_NAME_VERSION. Bits are set
42 within this bitmap to indicate SSA names that are defined in the
43 SAME block and used to calculate this SSA name.
44
45
46 <bb 2> :
47 _1 = x_4(D) + -2;
48 _2 = _1 * 4;
49 j_7 = foo ();
50 q_5 = _2 + 3;
51 if (q_5 <= 13)
52
53 _1 : x_4(D)
54 _2 : 1 x_4(D)
55 q_5 : _1 _2 x_4(D)
56
57 This dump indicates the bits set in the def_chain vector.
58 as well as demonstrates the def_chain bits for the related ssa_names.
59
60 Checking the chain for _2 indicates that _1 and x_4 are used in
61 its evaluation.
62
63 Def chains also only include statements which are valid gimple
64 so a def chain will only span statements for which the range
65 engine implements operations for. */
66
67
68 class range_def_chain
69 {
70 public:
71 range_def_chain ();
72 ~range_def_chain ();
73 bool has_def_chain (tree name);
74 bitmap get_def_chain (tree name);
75 bool in_chain_p (tree name, tree def);
76 private:
77 vec<bitmap> m_def_chain; // SSA_NAME : def chain components.
78 void build_def_chain (tree name, bitmap result, basic_block bb);
79 };
80
81
82 // Construct a range_def_chain.
83
84 range_def_chain::range_def_chain ()
85 {
86 m_def_chain.create (0);
87 m_def_chain.safe_grow_cleared (num_ssa_names);
88 }
89
90 // Destruct a range_def_chain.
91
92 range_def_chain::~range_def_chain ()
93 {
94 unsigned x;
95 for (x = 0; x < m_def_chain.length (); ++x)
96 if (m_def_chain[x])
97 BITMAP_FREE (m_def_chain[x]);
98 m_def_chain.release ();
99 }
100
101 // Return true if NAME is in the def chain of DEF. If BB is provided,
102 // only return true if the defining statement of DEF is in BB.
103
104 bool
105 range_def_chain::in_chain_p (tree name, tree def)
106 {
107 gcc_checking_assert (gimple_range_ssa_p (def));
108 gcc_checking_assert (gimple_range_ssa_p (name));
109
110 // Get the defintion chain for DEF.
111 bitmap chain = get_def_chain (def);
112
113 if (chain == NULL)
114 return false;
115 return bitmap_bit_p (chain, SSA_NAME_VERSION (name));
116 }
117
118 // Build def_chains for NAME if it is in BB. Copy the def chain into RESULT.
119
120 void
121 range_def_chain::build_def_chain (tree name, bitmap result, basic_block bb)
122 {
123 bitmap b;
124 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
125 // Add this operand into the result.
126 bitmap_set_bit (result, SSA_NAME_VERSION (name));
127
128 if (gimple_bb (def_stmt) == bb && !is_a<gphi *>(def_stmt))
129 {
130 // Get the def chain for the operand.
131 b = get_def_chain (name);
132 // If there was one, copy it into result.
133 if (b)
134 bitmap_ior_into (result, b);
135 }
136 }
137
138 // Return TRUE if NAME has been processed for a def_chain.
139
140 inline bool
141 range_def_chain::has_def_chain (tree name)
142 {
143 // Ensure there is an entry in the internal vector.
144 unsigned v = SSA_NAME_VERSION (name);
145 if (v >= m_def_chain.length ())
146 m_def_chain.safe_grow_cleared (num_ssa_names + 1);
147 return (m_def_chain[v] != NULL);
148 }
149
150 // Calculate the def chain for NAME and all of its dependent
151 // operands. Only using names in the same BB. Return the bitmap of
152 // all names in the m_def_chain. This only works for supported range
153 // statements.
154
155 bitmap
156 range_def_chain::get_def_chain (tree name)
157 {
158 tree ssa1, ssa2, ssa3;
159 unsigned v = SSA_NAME_VERSION (name);
160
161 // If it has already been processed, just return the cached value.
162 if (has_def_chain (name))
163 return m_def_chain[v];
164
165 // No definition chain for default defs.
166 if (SSA_NAME_IS_DEFAULT_DEF (name))
167 return NULL;
168
169 gimple *stmt = SSA_NAME_DEF_STMT (name);
170 if (gimple_range_handler (stmt))
171 {
172 ssa1 = gimple_range_ssa_p (gimple_range_operand1 (stmt));
173 ssa2 = gimple_range_ssa_p (gimple_range_operand2 (stmt));
174 ssa3 = NULL_TREE;
175 }
176 else if (is_a<gassign *> (stmt)
177 && gimple_assign_rhs_code (stmt) == COND_EXPR)
178 {
179 gassign *st = as_a<gassign *> (stmt);
180 ssa1 = gimple_range_ssa_p (gimple_assign_rhs1 (st));
181 ssa2 = gimple_range_ssa_p (gimple_assign_rhs2 (st));
182 ssa3 = gimple_range_ssa_p (gimple_assign_rhs3 (st));
183 }
184 else
185 return NULL;
186
187 basic_block bb = gimple_bb (stmt);
188
189 m_def_chain[v] = BITMAP_ALLOC (NULL);
190
191 if (ssa1)
192 build_def_chain (ssa1, m_def_chain[v], bb);
193 if (ssa2)
194 build_def_chain (ssa2, m_def_chain[v], bb);
195 if (ssa3)
196 build_def_chain (ssa3, m_def_chain[v], bb);
197
198 // If we run into pathological cases where the defintion chains are
199 // huge (ie huge basic block fully unrolled) we might be able to limit
200 // this by deciding here that if some criteria is satisfied, we change the
201 // def_chain back to be just the ssa-names. That will help prevent chains
202 // of a_2 = b_6 + a_8 from creating a pathological case.
203 return m_def_chain[v];
204 }
205
206 // -------------------------------------------------------------------
207
208 /* GORI_MAP is used to accumulate what SSA names in a block can
209 generate range information, and provides tools for the block ranger
210 to enable it to efficiently calculate these ranges.
211
212 GORI stands for "Generates Outgoing Range Information."
213
214 It utilizes the range_def_chain class to contruct def_chains.
215 Information for a basic block is calculated once and stored. It is
216 only calculated the first time a query is made. If no queries are
217 made, there is little overhead.
218
219 one bitmap is maintained for each basic block:
220 m_outgoing : a set bit indicates a range can be generated for a name.
221
222 Generally speaking, the m_outgoing vector is the union of the
223 entire def_chain of all SSA names used in the last statement of the
224 block which generate ranges. */
225
226 class gori_map : public range_def_chain
227 {
228 public:
229 gori_map ();
230 ~gori_map ();
231
232 bool is_export_p (tree name, basic_block bb);
233 bool def_chain_in_export_p (tree name, basic_block bb);
234
235 void dump (FILE *f);
236 void dump (FILE *f, basic_block bb);
237 private:
238 bitmap_obstack m_bitmaps;
239 vec<bitmap> m_outgoing; // BB: Outgoing ranges calculatable on edges
240 void maybe_add_gori (tree name, basic_block bb);
241 void calculate_gori (basic_block bb);
242 bitmap exports (basic_block bb);
243 };
244
245
246 // Initialize a gori-map structure.
247
248 gori_map::gori_map ()
249 {
250 m_outgoing.create (0);
251 m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
252 bitmap_obstack_initialize (&m_bitmaps);
253 }
254
255 // Free any memory the GORI map allocated.
256
257 gori_map::~gori_map ()
258 {
259 bitmap_obstack_release (&m_bitmaps);
260 m_outgoing.release ();
261 }
262
263 // Return the bitmap vector of all export from BB. Calculate if necessary.
264
265 bitmap
266 gori_map::exports (basic_block bb)
267 {
268 if (!m_outgoing[bb->index])
269 calculate_gori (bb);
270 return m_outgoing[bb->index];
271 }
272
273 // Return true if NAME is can have ranges generated for it from basic
274 // block BB.
275
276 bool
277 gori_map::is_export_p (tree name, basic_block bb)
278 {
279 return bitmap_bit_p (exports (bb), SSA_NAME_VERSION (name));
280 }
281
282 // Return true if any element in the def chain of NAME is in the
283 // export list for BB.
284
285 bool
286 gori_map::def_chain_in_export_p (tree name, basic_block bb)
287 {
288 bitmap a = exports (bb);
289 bitmap b = get_def_chain (name);
290 if (a && b)
291 return bitmap_intersect_p (a, b);
292 return false;
293 }
294
295 // If NAME is non-NULL and defined in block BB, calculate the def
296 // chain and add it to m_outgoing.
297
298 void
299 gori_map::maybe_add_gori (tree name, basic_block bb)
300 {
301 if (name)
302 {
303 gimple *s = SSA_NAME_DEF_STMT (name);
304 bitmap r = get_def_chain (name);
305 // Check if there is a def chain, and it is in this block.
306 if (r && gimple_bb (s) == bb)
307 bitmap_copy (m_outgoing[bb->index], r);
308 // Def chain doesn't include itself, and even if there isn't a
309 // def chain, this name should be added to exports.
310 bitmap_set_bit (m_outgoing[bb->index], SSA_NAME_VERSION (name));
311 }
312 }
313
314 // Calculate all the required information for BB.
315
316 void
317 gori_map::calculate_gori (basic_block bb)
318 {
319 tree name;
320 if (bb->index >= (signed int)m_outgoing.length ())
321 m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
322 gcc_checking_assert (m_outgoing[bb->index] == NULL);
323 m_outgoing[bb->index] = BITMAP_ALLOC (&m_bitmaps);
324
325 // If this block's last statement may generate range informaiton, go
326 // calculate it.
327 gimple *stmt = gimple_outgoing_range_stmt_p (bb);
328 if (!stmt)
329 return;
330 if (is_a<gcond *> (stmt))
331 {
332 gcond *gc = as_a<gcond *>(stmt);
333 name = gimple_range_ssa_p (gimple_cond_lhs (gc));
334 maybe_add_gori (name, gimple_bb (stmt));
335
336 name = gimple_range_ssa_p (gimple_cond_rhs (gc));
337 maybe_add_gori (name, gimple_bb (stmt));
338 }
339 else
340 {
341 gswitch *gs = as_a<gswitch *>(stmt);
342 name = gimple_range_ssa_p (gimple_switch_index (gs));
343 maybe_add_gori (name, gimple_bb (stmt));
344 }
345 }
346
347 // Dump the table information for BB to file F.
348
349 void
350 gori_map::dump (FILE *f, basic_block bb)
351 {
352 bool header = false;
353 const char *header_string = "bb%-4d ";
354 const char *header2 = " ";
355 bool printed_something = false;;
356 unsigned x, y;
357 bitmap_iterator bi;
358
359 // BB was not processed.
360 if (!m_outgoing[bb->index])
361 return;
362
363 // Dump the def chain for each SSA_NAME defined in BB.
364 for (x = 1; x < num_ssa_names; x++)
365 {
366 tree name = ssa_name (x);
367 if (!name)
368 continue;
369 gimple *stmt = SSA_NAME_DEF_STMT (name);
370 bitmap chain = (has_def_chain (name) ? get_def_chain (name) : NULL);
371 if (stmt && gimple_bb (stmt) == bb && chain && !bitmap_empty_p (chain))
372 {
373 fprintf (f, header_string, bb->index);
374 header_string = header2;
375 header = true;
376 print_generic_expr (f, name, TDF_SLIM);
377 fprintf (f, " : ");
378 EXECUTE_IF_SET_IN_BITMAP (chain, 0, y, bi)
379 {
380 print_generic_expr (f, ssa_name (y), TDF_SLIM);
381 fprintf (f, " ");
382 }
383 fprintf (f, "\n");
384 }
385 }
386
387 printed_something |= header;
388
389 // Now dump the export vector.
390 header = false;
391 EXECUTE_IF_SET_IN_BITMAP (m_outgoing[bb->index], 0, y, bi)
392 {
393 if (!header)
394 {
395 fprintf (f, header_string, bb->index);
396 fprintf (f, "exports: ");
397 header_string = header2;
398 header = true;
399 }
400 print_generic_expr (f, ssa_name (y), TDF_SLIM);
401 fprintf (f, " ");
402 }
403 if (header)
404 fputc ('\n', f);
405
406 printed_something |= header;
407 if (printed_something)
408 fprintf (f, "\n");
409 }
410
411 // Dump the entire GORI map structure to file F.
412
413 void
414 gori_map::dump (FILE *f)
415 {
416 basic_block bb;
417 FOR_EACH_BB_FN (bb, cfun)
418 {
419 dump (f, bb);
420 if (m_outgoing[bb->index])
421 fprintf (f, "\n");
422 }
423 }
424
425 DEBUG_FUNCTION void
426 debug (gori_map &g)
427 {
428 g.dump (stderr);
429 }
430
431 // -------------------------------------------------------------------
432
433 // Construct a gori_compute object.
434
435 gori_compute::gori_compute ()
436 {
437 // Create a boolean_type true and false range.
438 m_bool_zero = int_range<2> (boolean_false_node, boolean_false_node);
439 m_bool_one = int_range<2> (boolean_true_node, boolean_true_node);
440 m_gori_map = new gori_map;
441 }
442
443 // Destruct a gori_compute_object.
444
445 gori_compute::~gori_compute ()
446 {
447 delete m_gori_map;
448 }
449
450 // Provide a default of VARYING for all incoming SSA names.
451
452 void
453 gori_compute::ssa_range_in_bb (irange &r, tree name, basic_block)
454 {
455 r.set_varying (TREE_TYPE (name));
456 }
457
458 void
459 gori_compute::expr_range_in_bb (irange &r, tree expr, basic_block bb)
460 {
461 if (gimple_range_ssa_p (expr))
462 ssa_range_in_bb (r, expr, bb);
463 else
464 get_tree_range (r, expr);
465 }
466
467 // Calculate the range for NAME if the lhs of statement S has the
468 // range LHS. Return the result in R. Return false if no range can be
469 // calculated.
470
471 bool
472 gori_compute::compute_name_range_op (irange &r, gimple *stmt,
473 const irange &lhs, tree name)
474 {
475 int_range_max op1_range, op2_range;
476
477 tree op1 = gimple_range_operand1 (stmt);
478 tree op2 = gimple_range_operand2 (stmt);
479
480 // Operand 1 is the name being looked for, evaluate it.
481 if (op1 == name)
482 {
483 expr_range_in_bb (op1_range, op1, gimple_bb (stmt));
484 if (!op2)
485 {
486 // The second parameter to a unary operation is the range
487 // for the type of operand1, but if it can be reduced
488 // further, the results will be better. Start with what we
489 // know of the range of OP1 instead of the full type.
490 return gimple_range_calc_op1 (r, stmt, lhs, op1_range);
491 }
492 // If we need the second operand, get a value and evaluate.
493 expr_range_in_bb (op2_range, op2, gimple_bb (stmt));
494 if (gimple_range_calc_op1 (r, stmt, lhs, op2_range))
495 r.intersect (op1_range);
496 else
497 r = op1_range;
498 return true;
499 }
500
501 if (op2 == name)
502 {
503 expr_range_in_bb (op1_range, op1, gimple_bb (stmt));
504 expr_range_in_bb (r, op2, gimple_bb (stmt));
505 if (gimple_range_calc_op2 (op2_range, stmt, lhs, op1_range))
506 r.intersect (op2_range);
507 return true;
508 }
509 return false;
510 }
511
512 // Given the switch S, return an evaluation in R for NAME when the lhs
513 // evaluates to LHS. Returning false means the name being looked for
514 // was not resolvable.
515
516 bool
517 gori_compute::compute_operand_range_switch (irange &r, gswitch *s,
518 const irange &lhs,
519 tree name)
520 {
521 tree op1 = gimple_switch_index (s);
522
523 // If name matches, the range is simply the range from the edge.
524 // Empty ranges are viral as they are on a path which isn't
525 // executable.
526 if (op1 == name || lhs.undefined_p ())
527 {
528 r = lhs;
529 return true;
530 }
531
532 // If op1 is in the defintion chain, pass lhs back.
533 if (gimple_range_ssa_p (op1) && m_gori_map->in_chain_p (name, op1))
534 return compute_operand_range (r, SSA_NAME_DEF_STMT (op1), lhs, name);
535
536 return false;
537 }
538
539 // Return TRUE if GS is a logical && or || expression.
540
541 static inline bool
542 is_gimple_logical_p (const gimple *gs)
543 {
544 // Look for boolean and/or condition.
545 if (gimple_code (gs) == GIMPLE_ASSIGN)
546 switch (gimple_expr_code (gs))
547 {
548 case TRUTH_AND_EXPR:
549 case TRUTH_OR_EXPR:
550 return true;
551
552 case BIT_AND_EXPR:
553 case BIT_IOR_EXPR:
554 // Bitwise operations on single bits are logical too.
555 if (range_compatible_p (TREE_TYPE (gimple_assign_rhs1 (gs)),
556 boolean_type_node))
557 return true;
558 break;
559
560 default:
561 break;
562 }
563 return false;
564 }
565
566 // Return an evaluation for NAME as it would appear in STMT when the
567 // statement's lhs evaluates to LHS. If successful, return TRUE and
568 // store the evaluation in R, otherwise return FALSE.
569
570 bool
571 gori_compute::compute_operand_range (irange &r, gimple *stmt,
572 const irange &lhs, tree name)
573 {
574 // Empty ranges are viral as they are on an unexecutable path.
575 if (lhs.undefined_p ())
576 {
577 r.set_undefined ();
578 return true;
579 }
580 if (is_a<gswitch *> (stmt))
581 return compute_operand_range_switch (r, as_a<gswitch *> (stmt), lhs, name);
582 if (!gimple_range_handler (stmt))
583 return false;
584
585 tree op1 = gimple_range_ssa_p (gimple_range_operand1 (stmt));
586 tree op2 = gimple_range_ssa_p (gimple_range_operand2 (stmt));
587
588 // The base ranger handles NAME on this statement.
589 if (op1 == name || op2 == name)
590 return compute_name_range_op (r, stmt, lhs, name);
591
592 if (is_gimple_logical_p (stmt))
593 return compute_logical_operands (r, stmt, lhs, name);
594
595 // NAME is not in this stmt, but one of the names in it ought to be
596 // derived from it.
597 bool op1_in_chain = op1 && m_gori_map->in_chain_p (name, op1);
598 bool op2_in_chain = op2 && m_gori_map->in_chain_p (name, op2);
599 if (op1_in_chain && op2_in_chain)
600 return compute_operand1_and_operand2_range (r, stmt, lhs, name);
601 if (op1_in_chain)
602 return compute_operand1_range (r, stmt, lhs, name);
603 if (op2_in_chain)
604 return compute_operand2_range (r, stmt, lhs, name);
605
606 // If neither operand is derived, this statement tells us nothing.
607 return false;
608 }
609
610 // Return TRUE if range R is either a true or false compatible range.
611
612 static bool
613 range_is_either_true_or_false (const irange &r)
614 {
615 if (r.undefined_p ())
616 return false;
617
618 // This is complicated by the fact that Ada has multi-bit booleans,
619 // so true can be ~[0, 0] (i.e. [1,MAX]).
620 tree type = r.type ();
621 gcc_checking_assert (range_compatible_p (type, boolean_type_node));
622 return (r.singleton_p () || !r.contains_p (build_zero_cst (type)));
623 }
624
625 // A pair of ranges for true/false paths.
626
627 struct tf_range
628 {
629 tf_range () { }
630 tf_range (const irange &t_range, const irange &f_range)
631 {
632 true_range = t_range;
633 false_range = f_range;
634 }
635 int_range_max true_range, false_range;
636 };
637
638 // Evaluate a binary logical expression by combining the true and
639 // false ranges for each of the operands based on the result value in
640 // the LHS.
641
642 bool
643 gori_compute::logical_combine (irange &r, enum tree_code code,
644 const irange &lhs,
645 const tf_range &op1, const tf_range &op2)
646 {
647 if (op1.true_range.varying_p ()
648 && op1.false_range.varying_p ()
649 && op2.true_range.varying_p ()
650 && op2.false_range.varying_p ())
651 return false;
652
653 // This is not a simple fold of a logical expression, rather it
654 // determines ranges which flow through the logical expression.
655 //
656 // Assuming x_8 is an unsigned char, and relational statements:
657 // b_1 = x_8 < 20
658 // b_2 = x_8 > 5
659 // consider the logical expression and branch:
660 // c_2 = b_1 && b_2
661 // if (c_2)
662 //
663 // To determine the range of x_8 on either edge of the branch, one
664 // must first determine what the range of x_8 is when the boolean
665 // values of b_1 and b_2 are both true and false.
666 // b_1 TRUE x_8 = [0, 19]
667 // b_1 FALSE x_8 = [20, 255]
668 // b_2 TRUE x_8 = [6, 255]
669 // b_2 FALSE x_8 = [0,5].
670 //
671 // These ranges are then combined based on the expected outcome of
672 // the branch. The range on the TRUE side of the branch must satisfy
673 // b_1 == true && b_2 == true
674 //
675 // In terms of x_8, that means both x_8 == [0, 19] and x_8 = [6, 255]
676 // must be true. The range of x_8 on the true side must be the
677 // intersection of both ranges since both must be true. Thus the
678 // range of x_8 on the true side is [6, 19].
679 //
680 // To determine the ranges on the FALSE side, all 3 combinations of
681 // failing ranges must be considered, and combined as any of them
682 // can cause the false result.
683 //
684 // If the LHS can be TRUE or FALSE, then evaluate both a TRUE and
685 // FALSE results and combine them. If we fell back to VARYING any
686 // range restrictions that have been discovered up to this point
687 // would be lost.
688 if (!range_is_either_true_or_false (lhs))
689 {
690 int_range_max r1;
691 if (logical_combine (r1, code, m_bool_zero, op1, op2)
692 && logical_combine (r, code, m_bool_one, op1, op2))
693 {
694 r.union_ (r1);
695 return true;
696 }
697 return false;
698 }
699
700 switch (code)
701 {
702 // A logical AND combines ranges from 2 boolean conditions.
703 // c_2 = b_1 && b_2
704 case TRUTH_AND_EXPR:
705 case BIT_AND_EXPR:
706 if (!lhs.zero_p ())
707 {
708 // The TRUE side is the intersection of the the 2 true ranges.
709 r = op1.true_range;
710 r.intersect (op2.true_range);
711 }
712 else
713 {
714 // The FALSE side is the union of the other 3 cases.
715 int_range_max ff (op1.false_range);
716 ff.intersect (op2.false_range);
717 int_range_max tf (op1.true_range);
718 tf.intersect (op2.false_range);
719 int_range_max ft (op1.false_range);
720 ft.intersect (op2.true_range);
721 r = ff;
722 r.union_ (tf);
723 r.union_ (ft);
724 }
725 break;
726 // A logical OR combines ranges from 2 boolean conditons.
727 // c_2 = b_1 || b_2
728 case TRUTH_OR_EXPR:
729 case BIT_IOR_EXPR:
730 if (lhs.zero_p ())
731 {
732 // An OR operation will only take the FALSE path if both
733 // operands are false, so [20, 255] intersect [0, 5] is the
734 // union: [0,5][20,255].
735 r = op1.false_range;
736 r.intersect (op2.false_range);
737 }
738 else
739 {
740 // The TRUE side of an OR operation will be the union of
741 // the other three combinations.
742 int_range_max tt (op1.true_range);
743 tt.intersect (op2.true_range);
744 int_range_max tf (op1.true_range);
745 tf.intersect (op2.false_range);
746 int_range_max ft (op1.false_range);
747 ft.intersect (op2.true_range);
748 r = tt;
749 r.union_ (tf);
750 r.union_ (ft);
751 }
752 break;
753 default:
754 gcc_unreachable ();
755 }
756
757 return true;
758 }
759
760 // Helper function for compute_logical_operands_in_chain that computes
761 // the range of logical statements that can be computed without
762 // chasing down operands. These are things like [0 = x | y] where we
763 // know neither operand can be non-zero, or [1 = x & y] where we know
764 // neither operand can be zero.
765
766 bool
767 gori_compute::optimize_logical_operands (tf_range &range,
768 gimple *stmt,
769 const irange &lhs,
770 tree name,
771 tree op)
772 {
773 enum tree_code code = gimple_expr_code (stmt);
774
775 // Optimize [0 = x | y], since neither operand can ever be non-zero.
776 if ((code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) && lhs.zero_p ())
777 {
778 if (!compute_operand_range (range.false_range, SSA_NAME_DEF_STMT (op),
779 m_bool_zero, name))
780 expr_range_in_bb (range.false_range, name, gimple_bb (stmt));
781 range.true_range = range.false_range;
782 return true;
783 }
784 // Optimize [1 = x & y], since neither operand can ever be zero.
785 if ((code == BIT_AND_EXPR || code == TRUTH_AND_EXPR) && lhs == m_bool_one)
786 {
787 if (!compute_operand_range (range.true_range, SSA_NAME_DEF_STMT (op),
788 m_bool_one, name))
789 expr_range_in_bb (range.true_range, name, gimple_bb (stmt));
790 range.false_range = range.true_range;
791 return true;
792 }
793 return false;
794 }
795
796 // Given a logical STMT, calculate true and false ranges for each
797 // potential path of NAME, assuming NAME came through the OP chain if
798 // OP_IN_CHAIN is true.
799
800 void
801 gori_compute::compute_logical_operands_in_chain (tf_range &range,
802 gimple *stmt,
803 const irange &lhs,
804 tree name,
805 tree op, bool op_in_chain)
806 {
807 if (!op_in_chain)
808 {
809 // If op is not in chain, use its known value.
810 expr_range_in_bb (range.true_range, name, gimple_bb (stmt));
811 range.false_range = range.true_range;
812 return;
813 }
814 if (optimize_logical_operands (range, stmt, lhs, name, op))
815 return;
816
817 // Calulate ranges for true and false on both sides, since the false
818 // path is not always a simple inversion of the true side.
819 if (!compute_operand_range (range.true_range, SSA_NAME_DEF_STMT (op),
820 m_bool_one, name))
821 expr_range_in_bb (range.true_range, name, gimple_bb (stmt));
822 if (!compute_operand_range (range.false_range, SSA_NAME_DEF_STMT (op),
823 m_bool_zero, name))
824 expr_range_in_bb (range.false_range, name, gimple_bb (stmt));
825 }
826
827 // Given a logical STMT, calculate true and false for each potential
828 // path using NAME, and resolve the outcome based on the logical
829 // operator.
830
831 bool
832 gori_compute::compute_logical_operands (irange &r, gimple *stmt,
833 const irange &lhs,
834 tree name)
835 {
836 // Reaching this point means NAME is not in this stmt, but one of
837 // the names in it ought to be derived from it.
838 tree op1 = gimple_range_operand1 (stmt);
839 tree op2 = gimple_range_operand2 (stmt);
840 gcc_checking_assert (op1 != name && op2 != name);
841
842 bool op1_in_chain = (gimple_range_ssa_p (op1)
843 && m_gori_map->in_chain_p (name, op1));
844 bool op2_in_chain = (gimple_range_ssa_p (op2)
845 && m_gori_map->in_chain_p (name, op2));
846
847 // If neither operand is derived, then this stmt tells us nothing.
848 if (!op1_in_chain && !op2_in_chain)
849 return false;
850
851 tf_range op1_range, op2_range;
852 compute_logical_operands_in_chain (op1_range, stmt, lhs,
853 name, op1, op1_in_chain);
854 compute_logical_operands_in_chain (op2_range, stmt, lhs,
855 name, op2, op2_in_chain);
856 return logical_combine (r, gimple_expr_code (stmt), lhs,
857 op1_range, op2_range);
858 }
859
860 // Calculate a range for NAME from the operand 1 position of STMT
861 // assuming the result of the statement is LHS. Return the range in
862 // R, or false if no range could be calculated.
863
864 bool
865 gori_compute::compute_operand1_range (irange &r, gimple *stmt,
866 const irange &lhs, tree name)
867 {
868 int_range_max op1_range, op2_range;
869 tree op1 = gimple_range_operand1 (stmt);
870 tree op2 = gimple_range_operand2 (stmt);
871
872 expr_range_in_bb (op1_range, op1, gimple_bb (stmt));
873
874 // Now calcuated the operand and put that result in r.
875 if (op2)
876 {
877 expr_range_in_bb (op2_range, op2, gimple_bb (stmt));
878 if (!gimple_range_calc_op1 (r, stmt, lhs, op2_range))
879 return false;
880 }
881 else
882 {
883 // We pass op1_range to the unary operation. Nomally it's a
884 // hidden range_for_type parameter, but sometimes having the
885 // actual range can result in better information.
886 if (!gimple_range_calc_op1 (r, stmt, lhs, op1_range))
887 return false;
888 }
889
890 // Intersect the calculated result with the known result.
891 op1_range.intersect (r);
892
893 gimple *src_stmt = SSA_NAME_DEF_STMT (op1);
894 // If def stmt is outside of this BB, then name must be an import.
895 if (!src_stmt || (gimple_bb (src_stmt) != gimple_bb (stmt)))
896 {
897 // If this isn't the right import statement, then abort calculation.
898 if (!src_stmt || gimple_get_lhs (src_stmt) != name)
899 return false;
900 return compute_name_range_op (r, src_stmt, op1_range, name);
901 }
902 // Then feed this range back as the LHS of the defining statement.
903 return compute_operand_range (r, src_stmt, op1_range, name);
904 }
905
906
907 // Calculate a range for NAME from the operand 2 position of S
908 // assuming the result of the statement is LHS. Return the range in
909 // R, or false if no range could be calculated.
910
911 bool
912 gori_compute::compute_operand2_range (irange &r, gimple *stmt,
913 const irange &lhs, tree name)
914 {
915 int_range_max op1_range, op2_range;
916 tree op1 = gimple_range_operand1 (stmt);
917 tree op2 = gimple_range_operand2 (stmt);
918
919 expr_range_in_bb (op1_range, op1, gimple_bb (stmt));
920 expr_range_in_bb (op2_range, op2, gimple_bb (stmt));
921
922 // Intersect with range for op2 based on lhs and op1.
923 if (!gimple_range_calc_op2 (r, stmt, lhs, op1_range))
924 return false;
925 op2_range.intersect (r);
926
927 gimple *src_stmt = SSA_NAME_DEF_STMT (op2);
928 // If def stmt is outside of this BB, then name must be an import.
929 if (!src_stmt || (gimple_bb (src_stmt) != gimple_bb (stmt)))
930 {
931 // If this isn't the right src statement, then abort calculation.
932 if (!src_stmt || gimple_get_lhs (src_stmt) != name)
933 return false;
934 return compute_name_range_op (r, src_stmt, op2_range, name);
935 }
936 // Then feed this range back as the LHS of the defining statement.
937 return compute_operand_range (r, src_stmt, op2_range, name);
938 }
939
940 // Calculate a range for NAME from both operand positions of S
941 // assuming the result of the statement is LHS. Return the range in
942 // R, or false if no range could be calculated.
943
944 bool
945 gori_compute::compute_operand1_and_operand2_range
946 (irange &r,
947 gimple *stmt,
948 const irange &lhs,
949 tree name)
950 {
951 int_range_max op_range;
952
953 // Calculate a good a range for op2. Since op1 == op2, this will
954 // have already included whatever the actual range of name is.
955 if (!compute_operand2_range (op_range, stmt, lhs, name))
956 return false;
957
958 // Now get the range thru op1.
959 if (!compute_operand1_range (r, stmt, lhs, name))
960 return false;
961
962 // Whichever range is the most permissive is the one we need to
963 // use. (?) OR is that true? Maybe this should be intersection?
964 r.union_ (op_range);
965 return true;
966 }
967
968 // Return TRUE if a range can be calcalated for NAME on edge E.
969
970 bool
971 gori_compute::has_edge_range_p (edge e, tree name)
972 {
973 return (m_gori_map->is_export_p (name, e->src)
974 || m_gori_map->def_chain_in_export_p (name, e->src));
975 }
976
977 // Dump what is known to GORI computes to listing file F.
978
979 void
980 gori_compute::dump (FILE *f)
981 {
982 m_gori_map->dump (f);
983 }
984
985 // Calculate a range on edge E and return it in R. Try to evaluate a
986 // range for NAME on this edge. Return FALSE if this is either not a
987 // control edge or NAME is not defined by this edge.
988
989 bool
990 gori_compute::outgoing_edge_range_p (irange &r, edge e, tree name)
991 {
992 int_range_max lhs;
993
994 gcc_checking_assert (gimple_range_ssa_p (name));
995 // Determine if there is an outgoing edge.
996 gimple *stmt = outgoing.edge_range_p (lhs, e);
997 if (!stmt)
998 return false;
999
1000 // If NAME can be calculated on the edge, use that.
1001 if (m_gori_map->is_export_p (name, e->src))
1002 {
1003 if (compute_operand_range (r, stmt, lhs, name))
1004 {
1005 // Sometimes compatible types get interchanged. See PR97360.
1006 // Make sure we are returning the type of the thing we asked for.
1007 if (!r.undefined_p () && r.type () != TREE_TYPE (name))
1008 {
1009 gcc_checking_assert (range_compatible_p (r.type (),
1010 TREE_TYPE (name)));
1011 range_cast (r, TREE_TYPE (name));
1012 }
1013 return true;
1014 }
1015 }
1016 return false;
1017 }
1018
1019 // --------------------------------------------------------------------------
1020
1021 // Cache for SSAs that appear on the RHS of a boolean assignment.
1022 //
1023 // Boolean assignments of logical expressions (i.e. LHS = j_5 > 999)
1024 // have SSA operands whose range depend on the LHS of the assigment.
1025 // That is, the range of j_5 when LHS is true is different than when
1026 // LHS is false.
1027 //
1028 // This class caches the TRUE/FALSE ranges of such SSAs to avoid
1029 // recomputing.
1030
1031 class logical_stmt_cache
1032 {
1033 public:
1034 logical_stmt_cache ();
1035 ~logical_stmt_cache ();
1036 void set_range (tree lhs, tree name, const tf_range &);
1037 bool get_range (tf_range &r, tree lhs, tree name) const;
1038 bool cacheable_p (gimple *, const irange *lhs_range = NULL) const;
1039 void dump (FILE *, gimple *stmt) const;
1040 tree same_cached_name (tree lhs1, tree lh2) const;
1041 private:
1042 tree cached_name (tree lhs) const;
1043 void slot_diagnostics (tree lhs, const tf_range &range) const;
1044 struct cache_entry
1045 {
1046 cache_entry (tree name, const irange &t_range, const irange &f_range);
1047 void dump (FILE *out) const;
1048 tree name;
1049 tf_range range;
1050 };
1051 vec<cache_entry *> m_ssa_cache;
1052 };
1053
1054 logical_stmt_cache::cache_entry::cache_entry (tree name,
1055 const irange &t_range,
1056 const irange &f_range)
1057 : name (name), range (t_range, f_range)
1058 {
1059 }
1060
1061 logical_stmt_cache::logical_stmt_cache ()
1062 {
1063 m_ssa_cache.create (num_ssa_names + num_ssa_names / 10);
1064 m_ssa_cache.safe_grow_cleared (num_ssa_names);
1065 }
1066
1067 logical_stmt_cache::~logical_stmt_cache ()
1068 {
1069 for (unsigned i = 0; i < m_ssa_cache.length (); ++i)
1070 if (m_ssa_cache[i])
1071 delete m_ssa_cache[i];
1072 m_ssa_cache.release ();
1073 }
1074
1075 // Dump cache_entry to OUT.
1076
1077 void
1078 logical_stmt_cache::cache_entry::dump (FILE *out) const
1079 {
1080 fprintf (out, "name=");
1081 print_generic_expr (out, name, TDF_SLIM);
1082 fprintf (out, " ");
1083 range.true_range.dump (out);
1084 fprintf (out, ", ");
1085 range.false_range.dump (out);
1086 fprintf (out, "\n");
1087 }
1088
1089 // Update range for cache entry of NAME as it appears in the defining
1090 // statement of LHS.
1091
1092 void
1093 logical_stmt_cache::set_range (tree lhs, tree name, const tf_range &range)
1094 {
1095 unsigned version = SSA_NAME_VERSION (lhs);
1096 if (version >= m_ssa_cache.length ())
1097 m_ssa_cache.safe_grow_cleared (num_ssa_names + num_ssa_names / 10);
1098
1099 cache_entry *slot = m_ssa_cache[version];
1100 slot_diagnostics (lhs, range);
1101 if (slot)
1102 {
1103 // The IL must have changed. Update the carried SSA name for
1104 // consistency. Testcase is libgomp.fortran/doacross1.f90.
1105 if (slot->name != name)
1106 slot->name = name;
1107 return;
1108 }
1109 m_ssa_cache[version]
1110 = new cache_entry (name, range.true_range, range.false_range);
1111 }
1112
1113 // If there is a cached entry of NAME, set it in R and return TRUE,
1114 // otherwise return FALSE. LHS is the defining statement where NAME
1115 // appeared.
1116
1117 bool
1118 logical_stmt_cache::get_range (tf_range &r, tree lhs, tree name) const
1119 {
1120 gcc_checking_assert (cacheable_p (SSA_NAME_DEF_STMT (lhs)));
1121 if (cached_name (lhs) == name)
1122 {
1123 unsigned version = SSA_NAME_VERSION (lhs);
1124 if (m_ssa_cache[version])
1125 {
1126 r = m_ssa_cache[version]->range;
1127 return true;
1128 }
1129 }
1130 return false;
1131 }
1132
1133 // If the defining statement of LHS is in the cache, return the SSA
1134 // operand being cached. That is, return SSA for LHS = SSA .RELOP. OP2.
1135
1136 tree
1137 logical_stmt_cache::cached_name (tree lhs) const
1138 {
1139 unsigned version = SSA_NAME_VERSION (lhs);
1140
1141 if (version >= m_ssa_cache.length ())
1142 return NULL;
1143
1144 if (m_ssa_cache[version])
1145 return m_ssa_cache[version]->name;
1146 return NULL;
1147 }
1148
1149 // Return TRUE if the cached name for LHS1 is the same as the
1150 // cached name for LHS2.
1151
1152 tree
1153 logical_stmt_cache::same_cached_name (tree lhs1, tree lhs2) const
1154 {
1155 tree name = cached_name (lhs1);
1156 if (name && name == cached_name (lhs2))
1157 return name;
1158 return NULL;
1159 }
1160
1161 // Return TRUE if STMT is a statement we are interested in caching.
1162 // LHS_RANGE is any known range for the LHS of STMT.
1163
1164 bool
1165 logical_stmt_cache::cacheable_p (gimple *stmt, const irange *lhs_range) const
1166 {
1167 if (gimple_code (stmt) == GIMPLE_ASSIGN
1168 && range_compatible_p (TREE_TYPE (gimple_assign_lhs (stmt)),
1169 boolean_type_node)
1170 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
1171 {
1172 switch (gimple_expr_code (stmt))
1173 {
1174 case TRUTH_AND_EXPR:
1175 case BIT_AND_EXPR:
1176 case TRUTH_OR_EXPR:
1177 case BIT_IOR_EXPR:
1178 return !lhs_range || range_is_either_true_or_false (*lhs_range);
1179 default:
1180 return false;
1181 }
1182 }
1183 return false;
1184 }
1185
1186 // Output debugging diagnostics for the cache entry for LHS. RANGE is
1187 // the new range that is being cached.
1188
1189 void
1190 logical_stmt_cache::slot_diagnostics (tree lhs, const tf_range &range) const
1191 {
1192 gimple *stmt = SSA_NAME_DEF_STMT (lhs);
1193 unsigned version = SSA_NAME_VERSION (lhs);
1194 cache_entry *slot = m_ssa_cache[version];
1195
1196 if (!slot)
1197 {
1198 if (DEBUG_RANGE_CACHE)
1199 {
1200 fprintf (dump_file ? dump_file : stderr, "registering range for: ");
1201 dump (dump_file ? dump_file : stderr, stmt);
1202 }
1203 return;
1204 }
1205 if (DEBUG_RANGE_CACHE)
1206 fprintf (dump_file ? dump_file : stderr,
1207 "reusing range for SSA #%d\n", version);
1208 if (CHECKING_P && (slot->range.true_range != range.true_range
1209 || slot->range.false_range != range.false_range))
1210 {
1211 fprintf (stderr, "FATAL: range altered for cached: ");
1212 dump (stderr, stmt);
1213 fprintf (stderr, "Attempt to change to:\n");
1214 fprintf (stderr, "TRUE=");
1215 range.true_range.dump (stderr);
1216 fprintf (stderr, ", FALSE=");
1217 range.false_range.dump (stderr);
1218 fprintf (stderr, "\n");
1219 gcc_unreachable ();
1220 }
1221 }
1222
1223 // Dump the cache information for STMT.
1224
1225 void
1226 logical_stmt_cache::dump (FILE *out, gimple *stmt) const
1227 {
1228 tree lhs = gimple_assign_lhs (stmt);
1229 cache_entry *entry = m_ssa_cache[SSA_NAME_VERSION (lhs)];
1230
1231 print_gimple_stmt (out, stmt, 0, TDF_SLIM);
1232 if (entry)
1233 {
1234 fprintf (out, "\tname = ");
1235 print_generic_expr (out, entry->name);
1236 fprintf (out, " lhs(%d)= ", SSA_NAME_VERSION (lhs));
1237 print_generic_expr (out, lhs);
1238 fprintf (out, "\n\tTRUE=");
1239 entry->range.true_range.dump (out);
1240 fprintf (out, ", FALSE=");
1241 entry->range.false_range.dump (out);
1242 fprintf (out, "\n");
1243 }
1244 else
1245 fprintf (out, "[EMPTY]\n");
1246 }
1247
1248 gori_compute_cache::gori_compute_cache ()
1249 {
1250 m_cache = new logical_stmt_cache;
1251 }
1252
1253 gori_compute_cache::~gori_compute_cache ()
1254 {
1255 delete m_cache;
1256 }
1257
1258 // Caching version of compute_operand_range. If NAME, as it appears
1259 // in STMT, has already been cached return it from the cache,
1260 // otherwise compute the operand range as normal and cache it.
1261
1262 bool
1263 gori_compute_cache::compute_operand_range (irange &r, gimple *stmt,
1264 const irange &lhs_range, tree name)
1265 {
1266 bool cacheable = m_cache->cacheable_p (stmt, &lhs_range);
1267 if (cacheable)
1268 {
1269 tree lhs = gimple_assign_lhs (stmt);
1270 tf_range range;
1271 if (m_cache->get_range (range, lhs, name))
1272 {
1273 if (lhs_range.zero_p ())
1274 r = range.false_range;
1275 else
1276 r = range.true_range;
1277 return true;
1278 }
1279 }
1280 if (super::compute_operand_range (r, stmt, lhs_range, name))
1281 {
1282 if (cacheable)
1283 cache_stmt (stmt);
1284 return true;
1285 }
1286 return false;
1287 }
1288
1289 // Cache STMT if possible.
1290
1291 void
1292 gori_compute_cache::cache_stmt (gimple *stmt)
1293 {
1294 gcc_checking_assert (m_cache->cacheable_p (stmt));
1295 enum tree_code code = gimple_expr_code (stmt);
1296 tree lhs = gimple_assign_lhs (stmt);
1297 tree op1 = gimple_range_operand1 (stmt);
1298 tree op2 = gimple_range_operand2 (stmt);
1299 int_range_max r_true_side, r_false_side;
1300
1301 // LHS = s_5 && 999.
1302 if (TREE_CODE (op2) == INTEGER_CST)
1303 {
1304 range_operator *handler = range_op_handler (code, TREE_TYPE (lhs));
1305 int_range_max op2_range;
1306 expr_range_in_bb (op2_range, op2, gimple_bb (stmt));
1307 tree type = TREE_TYPE (op1);
1308 handler->op1_range (r_true_side, type, m_bool_one, op2_range);
1309 handler->op1_range (r_false_side, type, m_bool_zero, op2_range);
1310 m_cache->set_range (lhs, op1, tf_range (r_true_side, r_false_side));
1311 }
1312 // LHS = s_5 && b_8.
1313 else if (tree cached_name = m_cache->same_cached_name (op1, op2))
1314 {
1315 tf_range op1_range, op2_range;
1316 gcc_assert (m_cache->get_range (op1_range, op1, cached_name));
1317 gcc_assert (m_cache->get_range (op2_range, op2, cached_name));
1318 gcc_assert (logical_combine (r_true_side, code, m_bool_one,
1319 op1_range, op2_range));
1320 gcc_assert (logical_combine (r_false_side, code, m_bool_zero,
1321 op1_range, op2_range));
1322 m_cache->set_range (lhs, cached_name,
1323 tf_range (r_true_side, r_false_side));
1324 }
1325 }