]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gimple-range-gori.cc
Fix profile update in tree_transform_and_unroll_loop
[thirdparty/gcc.git] / gcc / gimple-range-gori.cc
CommitLineData
90e88fd3 1/* Gimple range GORI functions.
aeee4812 2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
90e88fd3
AM
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along 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
329d2f0d
AM
32// Return TRUE if GS is a logical && or || expression.
33
34static inline bool
35is_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}
90e88fd3 58
c2164470 59/* RANGE_DEF_CHAIN is used to determine which SSA names in a block can
90e88fd3
AM
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
90e88fd3
AM
94// Construct a range_def_chain.
95
96range_def_chain::range_def_chain ()
97{
c2164470 98 bitmap_obstack_initialize (&m_bitmaps);
90e88fd3
AM
99 m_def_chain.create (0);
100 m_def_chain.safe_grow_cleared (num_ssa_names);
329d2f0d 101 m_logical_depth = 0;
90e88fd3
AM
102}
103
104// Destruct a range_def_chain.
105
106range_def_chain::~range_def_chain ()
107{
90e88fd3 108 m_def_chain.release ();
c2164470 109 bitmap_obstack_release (&m_bitmaps);
90e88fd3
AM
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
115bool
116range_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
c46b5b0a 121 // Get the definition chain for DEF.
90e88fd3
AM
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
c2164470
AM
129// Add either IMP or the import list B to the import set of DATA.
130
131void
132range_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
147bitmap
148range_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;
c2164470
AM
153 return i;
154}
155
156// Return true if IMPORT is an import to NAMEs def chain.
157
158bool
159range_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
90e88fd3
AM
167// Build def_chains for NAME if it is in BB. Copy the def chain into RESULT.
168
169void
c2164470 170range_def_chain::register_dependency (tree name, tree dep, basic_block bb)
90e88fd3 171{
c2164470
AM
172 if (!gimple_range_ssa_p (dep))
173 return;
174
175 unsigned v = SSA_NAME_VERSION (name);
7f0cfeb1
BE
176 if (v >= m_def_chain.length ())
177 m_def_chain.safe_grow_cleared (num_ssa_names + 1);
c2164470
AM
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);
90e88fd3 181 bitmap b;
c2164470
AM
182
183 // Set the direct dependency cache entries.
184 if (!src.ssa1)
40c7f943
AM
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);
c2164470
AM
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
90e88fd3 198 // Add this operand into the result.
c2164470 199 bitmap_set_bit (src.bm, dep_v);
90e88fd3
AM
200
201 if (gimple_bb (def_stmt) == bb && !is_a<gphi *>(def_stmt))
202 {
203 // Get the def chain for the operand.
c2164470 204 b = get_def_chain (dep);
ab202b65
AM
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.
90e88fd3 207 if (b)
ab202b65 208 bitmap_ior_into (m_def_chain[v].bm, b);
c2164470 209 // And copy the import list.
ab202b65 210 set_import (m_def_chain[v], NULL_TREE, get_imports (dep));
90e88fd3 211 }
c2164470
AM
212 else
213 // Originated outside the block, so it is an import.
214 set_import (src, dep, NULL);
215}
216
217bool
218range_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
226void
227range_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);
90e88fd3
AM
232}
233
c2164470 234
90e88fd3
AM
235// Return TRUE if NAME has been processed for a def_chain.
236
237inline bool
238range_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);
c2164470 244 return (m_def_chain[v].ssa1 != 0);
90e88fd3
AM
245}
246
c2164470
AM
247
248
90e88fd3
AM
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
254bitmap
255range_def_chain::get_def_chain (tree name)
256{
80f78716 257 tree ssa[3];
90e88fd3
AM
258 unsigned v = SSA_NAME_VERSION (name);
259
260 // If it has already been processed, just return the cached value.
e15425e8 261 if (has_def_chain (name) && m_def_chain[v].bm)
c2164470 262 return m_def_chain[v].bm;
90e88fd3
AM
263
264 // No definition chain for default defs.
265 if (SSA_NAME_IS_DEFAULT_DEF (name))
c2164470
AM
266 {
267 // A Default def is always an import.
268 set_import (m_def_chain[v], name, NULL);
269 return NULL;
270 }
90e88fd3
AM
271
272 gimple *stmt = SSA_NAME_DEF_STMT (name);
80f78716
AM
273 unsigned count = gimple_range_ssa_names (ssa, 3, stmt);
274 if (count == 0)
90e88fd3 275 {
80f78716 276 // Stmts not understood or with no operands are always imports.
c2164470
AM
277 set_import (m_def_chain[v], name, NULL);
278 return NULL;
279 }
90e88fd3 280
ee448a52
AM
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.
80f78716 286 if (count > 1)
ee448a52
AM
287 m_logical_depth++;
288
80f78716
AM
289 for (unsigned x = 0; x < count; x++)
290 register_dependency (name, ssa[x], gimple_bb (stmt));
90e88fd3 291
80f78716 292 if (count > 1)
329d2f0d
AM
293 m_logical_depth--;
294
c2164470 295 return m_def_chain[v].bm;
90e88fd3
AM
296}
297
c2164470
AM
298// Dump what we know for basic block BB to file F.
299
300void
301range_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
90e88fd3
AM
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
c46b5b0a 344 It utilizes the range_def_chain class to construct def_chains.
90e88fd3
AM
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
90e88fd3
AM
356
357// Initialize a gori-map structure.
358
359gori_map::gori_map ()
360{
361 m_outgoing.create (0);
362 m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
c2164470
AM
363 m_incoming.create (0);
364 m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
2dd1f944 365 m_maybe_variant = BITMAP_ALLOC (&m_bitmaps);
90e88fd3
AM
366}
367
368// Free any memory the GORI map allocated.
369
370gori_map::~gori_map ()
371{
c2164470 372 m_incoming.release ();
90e88fd3
AM
373 m_outgoing.release ();
374}
375
376// Return the bitmap vector of all export from BB. Calculate if necessary.
377
378bitmap
379gori_map::exports (basic_block bb)
380{
c2164470 381 if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
90e88fd3
AM
382 calculate_gori (bb);
383 return m_outgoing[bb->index];
384}
385
c2164470
AM
386// Return the bitmap vector of all imports to BB. Calculate if necessary.
387
388bitmap
389gori_map::imports (basic_block bb)
390{
391 if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
392 calculate_gori (bb);
393 return m_incoming[bb->index];
394}
395
90e88fd3
AM
396// Return true if NAME is can have ranges generated for it from basic
397// block BB.
398
399bool
400gori_map::is_export_p (tree name, basic_block bb)
401{
7f359556
AM
402 // If no BB is specified, test if it is exported anywhere in the IL.
403 if (!bb)
2dd1f944 404 return bitmap_bit_p (m_maybe_variant, SSA_NAME_VERSION (name));
90e88fd3
AM
405 return bitmap_bit_p (exports (bb), SSA_NAME_VERSION (name));
406}
407
6c849e2f
AM
408// Set or clear the m_maybe_variant bit to determine if ranges will be tracked
409// for NAME. A clear bit means they will NOT be tracked.
2dd1f944
AM
410
411void
6c849e2f 412gori_map::set_range_invariant (tree name, bool invariant)
2dd1f944 413{
6c849e2f
AM
414 if (invariant)
415 bitmap_clear_bit (m_maybe_variant, SSA_NAME_VERSION (name));
416 else
417 bitmap_set_bit (m_maybe_variant, SSA_NAME_VERSION (name));
2dd1f944
AM
418}
419
c2164470 420// Return true if NAME is an import to block BB.
90e88fd3
AM
421
422bool
c2164470 423gori_map::is_import_p (tree name, basic_block bb)
90e88fd3 424{
c2164470
AM
425 // If no BB is specified, test if it is exported anywhere in the IL.
426 return bitmap_bit_p (imports (bb), SSA_NAME_VERSION (name));
90e88fd3
AM
427}
428
429// If NAME is non-NULL and defined in block BB, calculate the def
430// chain and add it to m_outgoing.
431
432void
433gori_map::maybe_add_gori (tree name, basic_block bb)
434{
435 if (name)
436 {
c2164470
AM
437 // Check if there is a def chain, regardless of the block.
438 add_def_chain_to_bitmap (m_outgoing[bb->index], name);
439 // Check for any imports.
440 bitmap imp = get_imports (name);
441 // If there were imports, add them so we can recompute
442 if (imp)
443 bitmap_ior_into (m_incoming[bb->index], imp);
444 // This name is always an import.
445 if (gimple_bb (SSA_NAME_DEF_STMT (name)) != bb)
446 bitmap_set_bit (m_incoming[bb->index], SSA_NAME_VERSION (name));
447
90e88fd3
AM
448 // Def chain doesn't include itself, and even if there isn't a
449 // def chain, this name should be added to exports.
450 bitmap_set_bit (m_outgoing[bb->index], SSA_NAME_VERSION (name));
451 }
452}
453
454// Calculate all the required information for BB.
455
456void
457gori_map::calculate_gori (basic_block bb)
458{
459 tree name;
460 if (bb->index >= (signed int)m_outgoing.length ())
c2164470
AM
461 {
462 m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
463 m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
464 }
90e88fd3
AM
465 gcc_checking_assert (m_outgoing[bb->index] == NULL);
466 m_outgoing[bb->index] = BITMAP_ALLOC (&m_bitmaps);
c2164470 467 m_incoming[bb->index] = BITMAP_ALLOC (&m_bitmaps);
90e88fd3 468
63c59f05
RS
469 if (single_succ_p (bb))
470 return;
471
c46b5b0a 472 // If this block's last statement may generate range information, go
90e88fd3
AM
473 // calculate it.
474 gimple *stmt = gimple_outgoing_range_stmt_p (bb);
475 if (!stmt)
476 return;
477 if (is_a<gcond *> (stmt))
478 {
479 gcond *gc = as_a<gcond *>(stmt);
480 name = gimple_range_ssa_p (gimple_cond_lhs (gc));
481 maybe_add_gori (name, gimple_bb (stmt));
482
483 name = gimple_range_ssa_p (gimple_cond_rhs (gc));
484 maybe_add_gori (name, gimple_bb (stmt));
485 }
486 else
487 {
3ca950c3 488 // Do not process switches if they are too large.
b6dea04f 489 if (EDGE_COUNT (bb->succs) > (unsigned)param_vrp_switch_limit)
3ca950c3 490 return;
90e88fd3
AM
491 gswitch *gs = as_a<gswitch *>(stmt);
492 name = gimple_range_ssa_p (gimple_switch_index (gs));
493 maybe_add_gori (name, gimple_bb (stmt));
494 }
7f359556 495 // Add this bitmap to the aggregate list of all outgoing names.
2dd1f944 496 bitmap_ior_into (m_maybe_variant, m_outgoing[bb->index]);
90e88fd3
AM
497}
498
499// Dump the table information for BB to file F.
500
501void
c2164470 502gori_map::dump (FILE *f, basic_block bb, bool verbose)
90e88fd3 503{
90e88fd3 504 // BB was not processed.
c2164470 505 if (!m_outgoing[bb->index] || bitmap_empty_p (m_outgoing[bb->index]))
90e88fd3
AM
506 return;
507
c2164470
AM
508 tree name;
509
510 bitmap imp = imports (bb);
511 if (!bitmap_empty_p (imp))
90e88fd3 512 {
c2164470
AM
513 if (verbose)
514 fprintf (f, "bb<%u> Imports: ",bb->index);
515 else
516 fprintf (f, "Imports: ");
517 FOR_EACH_GORI_IMPORT_NAME (*this, bb, name)
518 {
90e88fd3 519 print_generic_expr (f, name, TDF_SLIM);
c2164470 520 fprintf (f, " ");
90e88fd3 521 }
c2164470 522 fputc ('\n', f);
90e88fd3
AM
523 }
524
c2164470
AM
525 if (verbose)
526 fprintf (f, "bb<%u> Exports: ",bb->index);
527 else
528 fprintf (f, "Exports: ");
529 // Dump the export vector.
530 FOR_EACH_GORI_EXPORT_NAME (*this, bb, name)
90e88fd3 531 {
c2164470 532 print_generic_expr (f, name, TDF_SLIM);
90e88fd3
AM
533 fprintf (f, " ");
534 }
c2164470 535 fputc ('\n', f);
90e88fd3 536
c2164470 537 range_def_chain::dump (f, bb, " ");
90e88fd3
AM
538}
539
540// Dump the entire GORI map structure to file F.
541
542void
543gori_map::dump (FILE *f)
544{
545 basic_block bb;
546 FOR_EACH_BB_FN (bb, cfun)
c2164470 547 dump (f, bb);
90e88fd3
AM
548}
549
550DEBUG_FUNCTION void
551debug (gori_map &g)
552{
553 g.dump (stderr);
554}
555
556// -------------------------------------------------------------------
557
558// Construct a gori_compute object.
559
3ca950c3 560gori_compute::gori_compute (int not_executable_flag)
b6dea04f 561 : outgoing (param_vrp_switch_limit), tracer ("GORI ")
90e88fd3 562{
053e1d64 563 m_not_executable_flag = not_executable_flag;
90e88fd3 564 // Create a boolean_type true and false range.
cb779afe
AH
565 m_bool_zero = range_false ();
566 m_bool_one = range_true ();
9cb114fd 567 if (dump_file && (param_ranger_debug & RANGER_DEBUG_GORI))
4759e1e0 568 tracer.enable_trace ();
90e88fd3
AM
569}
570
90e88fd3
AM
571// Given the switch S, return an evaluation in R for NAME when the lhs
572// evaluates to LHS. Returning false means the name being looked for
573// was not resolvable.
574
575bool
45c8523d
AH
576gori_compute::compute_operand_range_switch (vrange &r, gswitch *s,
577 const vrange &lhs,
47ea02bb 578 tree name, fur_source &src)
90e88fd3
AM
579{
580 tree op1 = gimple_switch_index (s);
581
582 // If name matches, the range is simply the range from the edge.
583 // Empty ranges are viral as they are on a path which isn't
584 // executable.
585 if (op1 == name || lhs.undefined_p ())
586 {
587 r = lhs;
588 return true;
589 }
590
c46b5b0a 591 // If op1 is in the definition chain, pass lhs back.
28ceee1b 592 if (gimple_range_ssa_p (op1) && in_chain_p (name, op1))
47ea02bb 593 return compute_operand_range (r, SSA_NAME_DEF_STMT (op1), lhs, name, src);
90e88fd3
AM
594
595 return false;
596}
597
90e88fd3
AM
598
599// Return an evaluation for NAME as it would appear in STMT when the
600// statement's lhs evaluates to LHS. If successful, return TRUE and
601// store the evaluation in R, otherwise return FALSE.
602
603bool
45c8523d
AH
604gori_compute::compute_operand_range (vrange &r, gimple *stmt,
605 const vrange &lhs, tree name,
431cdfbe 606 fur_source &src, value_relation *rel)
90e88fd3 607{
431cdfbe
AM
608 value_relation vrel;
609 value_relation *vrel_ptr = rel;
90e88fd3
AM
610 // Empty ranges are viral as they are on an unexecutable path.
611 if (lhs.undefined_p ())
612 {
613 r.set_undefined ();
614 return true;
615 }
616 if (is_a<gswitch *> (stmt))
47ea02bb
AM
617 return compute_operand_range_switch (r, as_a<gswitch *> (stmt), lhs, name,
618 src);
51ce0638
AM
619 gimple_range_op_handler handler (stmt);
620 if (!handler)
90e88fd3
AM
621 return false;
622
51ce0638
AM
623 tree op1 = gimple_range_ssa_p (handler.operand1 ());
624 tree op2 = gimple_range_ssa_p (handler.operand2 ());
90e88fd3 625
70d1e3f4
AM
626 // If there is a relation betwen op1 and op2, use it instead as it is
627 // likely to be more applicable.
628 if (op1 && op2)
629 {
630 relation_kind k = handler.op1_op2_relation (lhs);
631 if (k != VREL_VARYING)
632 {
633 vrel.set_relation (k, op1, op2);
634 vrel_ptr = &vrel;
635 }
636 }
637
47ea02bb
AM
638 // Handle end of lookup first.
639 if (op1 == name)
018e7f16 640 return compute_operand1_range (r, handler, lhs, src, vrel_ptr);
47ea02bb 641 if (op2 == name)
988b07a6 642 return compute_operand2_range (r, handler, lhs, src, vrel_ptr);
90e88fd3
AM
643
644 // NAME is not in this stmt, but one of the names in it ought to be
645 // derived from it.
28ceee1b
AM
646 bool op1_in_chain = op1 && in_chain_p (name, op1);
647 bool op2_in_chain = op2 && in_chain_p (name, op2);
47ea02bb
AM
648
649 // If neither operand is derived, then this stmt tells us nothing.
650 if (!op1_in_chain && !op2_in_chain)
651 return false;
652
f0375705
AM
653 // If either operand is in the def chain of the other (or they are equal), it
654 // will be evaluated twice and can result in an exponential time calculation.
655 // Instead just evaluate the one operand.
656 if (op1_in_chain && op2_in_chain)
657 {
658 if (in_chain_p (op1, op2) || op1 == op2)
659 op1_in_chain = false;
660 else if (in_chain_p (op2, op1))
661 op2_in_chain = false;
662 }
663
0963cb5f
AM
664 bool res = false;
665 // If the lhs doesn't tell us anything only a relation can possibly enhance
666 // the result.
667 if (lhs.varying_p ())
668 {
669 if (!vrel_ptr)
670 return false;
671 // If there is a relation (ie: x != y) , it can only be relevant if
672 // a) both elements are in the defchain
673 // c = x > y // (x and y are in c's defchain)
674 if (op1_in_chain)
675 res = in_chain_p (vrel_ptr->op1 (), op1)
676 && in_chain_p (vrel_ptr->op2 (), op1);
677 if (!res && op2_in_chain)
678 res = in_chain_p (vrel_ptr->op1 (), op2)
679 || in_chain_p (vrel_ptr->op2 (), op2);
680 if (!res)
681 {
682 // or b) one relation element is in the defchain of the other and the
683 // other is the LHS of this stmt.
684 // x = y + 2
685 if (vrel_ptr->op1 () == handler.lhs ()
686 && (vrel_ptr->op2 () == op1 || vrel_ptr->op2 () == op2))
687 res = true;
688 else if (vrel_ptr->op2 () == handler.lhs ()
689 && (vrel_ptr->op1 () == op1 || vrel_ptr->op1 () == op2))
690 res = true;
691 }
692 if (!res)
693 return false;
694 }
1626ec53 695
47ea02bb
AM
696 // Process logicals as they have special handling.
697 if (is_gimple_logical_p (stmt))
698 {
1626ec53
AM
699 // If the lhs doesn't tell us anything, neither will combining operands.
700 if (lhs.varying_p ())
701 return false;
702
4759e1e0
AM
703 unsigned idx;
704 if ((idx = tracer.header ("compute_operand ")))
705 {
706 print_generic_expr (dump_file, name, TDF_SLIM);
707 fprintf (dump_file, " with LHS = ");
708 lhs.dump (dump_file);
709 fprintf (dump_file, " at stmt ");
710 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
711 }
712
45c8523d
AH
713 tree type = TREE_TYPE (name);
714 Value_Range op1_trange (type), op1_frange (type);
715 Value_Range op2_trange (type), op2_frange (type);
51ce0638 716 compute_logical_operands (op1_trange, op1_frange, handler,
45c8523d 717 as_a <irange> (lhs),
47ea02bb 718 name, src, op1, op1_in_chain);
51ce0638 719 compute_logical_operands (op2_trange, op2_frange, handler,
45c8523d 720 as_a <irange> (lhs),
47ea02bb 721 name, src, op2, op2_in_chain);
45c8523d
AH
722 res = logical_combine (r,
723 gimple_expr_code (stmt),
724 as_a <irange> (lhs),
4759e1e0
AM
725 op1_trange, op1_frange, op2_trange, op2_frange);
726 if (idx)
727 tracer.trailer (idx, "compute_operand", res, name, r);
778099c4 728 return res;
47ea02bb 729 }
47ea02bb 730 // Follow the appropriate operands now.
778099c4
AM
731 if (op1_in_chain && op2_in_chain)
732 return compute_operand1_and_operand2_range (r, handler, lhs, name, src,
733 vrel_ptr);
734 Value_Range vr;
735 gimple *src_stmt;
736 if (op1_in_chain)
018e7f16 737 {
778099c4 738 vr.set_type (TREE_TYPE (op1));
018e7f16
AM
739 if (!compute_operand1_range (vr, handler, lhs, src, vrel_ptr))
740 return false;
778099c4 741 src_stmt = SSA_NAME_DEF_STMT (op1);
018e7f16 742 }
778099c4 743 else
988b07a6 744 {
778099c4
AM
745 gcc_checking_assert (op2_in_chain);
746 vr.set_type (TREE_TYPE (op2));
988b07a6
AM
747 if (!compute_operand2_range (vr, handler, lhs, src, vrel_ptr))
748 return false;
778099c4 749 src_stmt = SSA_NAME_DEF_STMT (op2);
988b07a6 750 }
90e88fd3 751
778099c4
AM
752 gcc_checking_assert (src_stmt);
753 // Then feed this range back as the LHS of the defining statement.
754 return compute_operand_range (r, src_stmt, vr, name, src, vrel_ptr);
90e88fd3 755 // If neither operand is derived, this statement tells us nothing.
90e88fd3
AM
756}
757
47ea02bb 758
90e88fd3
AM
759// Return TRUE if range R is either a true or false compatible range.
760
761static bool
762range_is_either_true_or_false (const irange &r)
763{
764 if (r.undefined_p ())
765 return false;
766
767 // This is complicated by the fact that Ada has multi-bit booleans,
768 // so true can be ~[0, 0] (i.e. [1,MAX]).
769 tree type = r.type ();
6e02de94 770 gcc_checking_assert (range_compatible_p (type, boolean_type_node));
cb779afe
AH
771 return (r.singleton_p ()
772 || !r.contains_p (wi::zero (TYPE_PRECISION (type))));
90e88fd3
AM
773}
774
90e88fd3
AM
775// Evaluate a binary logical expression by combining the true and
776// false ranges for each of the operands based on the result value in
777// the LHS.
778
779bool
45c8523d 780gori_compute::logical_combine (vrange &r, enum tree_code code,
90e88fd3 781 const irange &lhs,
45c8523d
AH
782 const vrange &op1_true, const vrange &op1_false,
783 const vrange &op2_true, const vrange &op2_false)
90e88fd3 784{
47ea02bb
AM
785 if (op1_true.varying_p () && op1_false.varying_p ()
786 && op2_true.varying_p () && op2_false.varying_p ())
90e88fd3
AM
787 return false;
788
4759e1e0
AM
789 unsigned idx;
790 if ((idx = tracer.header ("logical_combine")))
791 {
792 switch (code)
793 {
794 case TRUTH_OR_EXPR:
795 case BIT_IOR_EXPR:
796 fprintf (dump_file, " || ");
797 break;
798 case TRUTH_AND_EXPR:
799 case BIT_AND_EXPR:
800 fprintf (dump_file, " && ");
801 break;
802 default:
803 break;
804 }
805 fprintf (dump_file, " with LHS = ");
806 lhs.dump (dump_file);
807 fputc ('\n', dump_file);
808
809 tracer.print (idx, "op1_true = ");
810 op1_true.dump (dump_file);
811 fprintf (dump_file, " op1_false = ");
812 op1_false.dump (dump_file);
813 fputc ('\n', dump_file);
814 tracer.print (idx, "op2_true = ");
815 op2_true.dump (dump_file);
816 fprintf (dump_file, " op2_false = ");
817 op2_false.dump (dump_file);
818 fputc ('\n', dump_file);
819 }
820
90e88fd3
AM
821 // This is not a simple fold of a logical expression, rather it
822 // determines ranges which flow through the logical expression.
823 //
824 // Assuming x_8 is an unsigned char, and relational statements:
825 // b_1 = x_8 < 20
826 // b_2 = x_8 > 5
827 // consider the logical expression and branch:
828 // c_2 = b_1 && b_2
829 // if (c_2)
830 //
831 // To determine the range of x_8 on either edge of the branch, one
832 // must first determine what the range of x_8 is when the boolean
833 // values of b_1 and b_2 are both true and false.
834 // b_1 TRUE x_8 = [0, 19]
835 // b_1 FALSE x_8 = [20, 255]
836 // b_2 TRUE x_8 = [6, 255]
837 // b_2 FALSE x_8 = [0,5].
838 //
839 // These ranges are then combined based on the expected outcome of
840 // the branch. The range on the TRUE side of the branch must satisfy
841 // b_1 == true && b_2 == true
842 //
843 // In terms of x_8, that means both x_8 == [0, 19] and x_8 = [6, 255]
844 // must be true. The range of x_8 on the true side must be the
845 // intersection of both ranges since both must be true. Thus the
846 // range of x_8 on the true side is [6, 19].
847 //
848 // To determine the ranges on the FALSE side, all 3 combinations of
849 // failing ranges must be considered, and combined as any of them
850 // can cause the false result.
851 //
852 // If the LHS can be TRUE or FALSE, then evaluate both a TRUE and
853 // FALSE results and combine them. If we fell back to VARYING any
854 // range restrictions that have been discovered up to this point
855 // would be lost.
856 if (!range_is_either_true_or_false (lhs))
857 {
4759e1e0 858 bool res;
45c8523d 859 Value_Range r1 (r);
47ea02bb
AM
860 if (logical_combine (r1, code, m_bool_zero, op1_true, op1_false,
861 op2_true, op2_false)
862 && logical_combine (r, code, m_bool_one, op1_true, op1_false,
863 op2_true, op2_false))
90e88fd3
AM
864 {
865 r.union_ (r1);
4759e1e0 866 res = true;
90e88fd3 867 }
4759e1e0
AM
868 else
869 res = false;
576d5245
AH
870 if (idx && res)
871 {
872 tracer.print (idx, "logical_combine produced ");
873 r.dump (dump_file);
874 fputc ('\n', dump_file);
875 }
90e88fd3
AM
876 }
877
878 switch (code)
879 {
880 // A logical AND combines ranges from 2 boolean conditions.
881 // c_2 = b_1 && b_2
882 case TRUTH_AND_EXPR:
883 case BIT_AND_EXPR:
884 if (!lhs.zero_p ())
885 {
027e3041 886 // The TRUE side is the intersection of the 2 true ranges.
47ea02bb
AM
887 r = op1_true;
888 r.intersect (op2_true);
90e88fd3
AM
889 }
890 else
891 {
892 // The FALSE side is the union of the other 3 cases.
45c8523d 893 Value_Range ff (op1_false);
47ea02bb 894 ff.intersect (op2_false);
45c8523d 895 Value_Range tf (op1_true);
47ea02bb 896 tf.intersect (op2_false);
45c8523d 897 Value_Range ft (op1_false);
47ea02bb 898 ft.intersect (op2_true);
90e88fd3
AM
899 r = ff;
900 r.union_ (tf);
901 r.union_ (ft);
902 }
903 break;
c46b5b0a 904 // A logical OR combines ranges from 2 boolean conditions.
90e88fd3
AM
905 // c_2 = b_1 || b_2
906 case TRUTH_OR_EXPR:
907 case BIT_IOR_EXPR:
908 if (lhs.zero_p ())
909 {
910 // An OR operation will only take the FALSE path if both
c46b5b0a 911 // operands are false simultaneously, which means they should
7d26a337 912 // be intersected. !(x || y) == !x && !y
47ea02bb
AM
913 r = op1_false;
914 r.intersect (op2_false);
90e88fd3
AM
915 }
916 else
917 {
918 // The TRUE side of an OR operation will be the union of
919 // the other three combinations.
45c8523d 920 Value_Range tt (op1_true);
47ea02bb 921 tt.intersect (op2_true);
45c8523d 922 Value_Range tf (op1_true);
47ea02bb 923 tf.intersect (op2_false);
45c8523d 924 Value_Range ft (op1_false);
47ea02bb 925 ft.intersect (op2_true);
90e88fd3
AM
926 r = tt;
927 r.union_ (tf);
928 r.union_ (ft);
929 }
930 break;
931 default:
932 gcc_unreachable ();
933 }
934
4759e1e0
AM
935 if (idx)
936 tracer.trailer (idx, "logical_combine", true, NULL_TREE, r);
90e88fd3
AM
937 return true;
938}
939
90e88fd3
AM
940
941// Given a logical STMT, calculate true and false ranges for each
942// potential path of NAME, assuming NAME came through the OP chain if
943// OP_IN_CHAIN is true.
944
945void
45c8523d 946gori_compute::compute_logical_operands (vrange &true_range, vrange &false_range,
51ce0638 947 gimple_range_op_handler &handler,
47ea02bb
AM
948 const irange &lhs,
949 tree name, fur_source &src,
950 tree op, bool op_in_chain)
90e88fd3 951{
51ce0638 952 gimple *stmt = handler.stmt ();
7d26a337 953 gimple *src_stmt = gimple_range_ssa_p (op) ? SSA_NAME_DEF_STMT (op) : NULL;
51ce0638 954 if (!op_in_chain || !src_stmt || chain_import_p (handler.lhs (), op))
90e88fd3 955 {
7d26a337
AM
956 // If op is not in the def chain, or defined in this block,
957 // use its known value on entry to the block.
47ea02bb
AM
958 src.get_operand (true_range, name);
959 false_range = true_range;
4759e1e0
AM
960 unsigned idx;
961 if ((idx = tracer.header ("logical_operand")))
962 {
963 print_generic_expr (dump_file, op, TDF_SLIM);
964 fprintf (dump_file, " not in computation chain. Queried.\n");
965 tracer.trailer (idx, "logical_operand", true, NULL_TREE, true_range);
966 }
90e88fd3
AM
967 return;
968 }
90e88fd3 969
47ea02bb
AM
970 enum tree_code code = gimple_expr_code (stmt);
971 // Optimize [0 = x | y], since neither operand can ever be non-zero.
972 if ((code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) && lhs.zero_p ())
973 {
974 if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name,
975 src))
976 src.get_operand (false_range, name);
977 true_range = false_range;
978 return;
979 }
90e88fd3 980
47ea02bb
AM
981 // Optimize [1 = x & y], since neither operand can ever be zero.
982 if ((code == BIT_AND_EXPR || code == TRUTH_AND_EXPR) && lhs == m_bool_one)
983 {
984 if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
985 src.get_operand (true_range, name);
986 false_range = true_range;
987 return;
988 }
90e88fd3 989
47ea02bb
AM
990 // Calculate ranges for true and false on both sides, since the false
991 // path is not always a simple inversion of the true side.
992 if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
993 src.get_operand (true_range, name);
994 if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name, src))
995 src.get_operand (false_range, name);
90e88fd3
AM
996}
997
67166c9e
AM
998
999// This routine will try to refine the ranges of OP1 and OP2 given a relation
1000// K between them. In order to perform this refinement, one of the operands
1001// must be in the definition chain of the other. The use is refined using
c46b5b0a 1002// op1/op2_range on the statement, and the definition is then recalculated
67166c9e
AM
1003// using the relation.
1004
1005bool
1006gori_compute::refine_using_relation (tree op1, vrange &op1_range,
1007 tree op2, vrange &op2_range,
1008 fur_source &src, relation_kind k)
1009{
1010 gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
1011 gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
99fda5de
AM
1012
1013 if (k == VREL_VARYING || k == VREL_EQ || k == VREL_UNDEFINED)
1014 return false;
67166c9e
AM
1015
1016 bool change = false;
1017 bool op1_def_p = in_chain_p (op2, op1);
1018 if (!op1_def_p)
1019 if (!in_chain_p (op1, op2))
1020 return false;
1021
1022 tree def_op = op1_def_p ? op1 : op2;
1023 tree use_op = op1_def_p ? op2 : op1;
1024
1025 if (!op1_def_p)
1026 k = relation_swap (k);
1027
1028 // op1_def is true if we want to look up op1, otherwise we want op2.
1029 // if neither is the case, we returned in the above check.
1030
1031 gimple *def_stmt = SSA_NAME_DEF_STMT (def_op);
1032 gimple_range_op_handler op_handler (def_stmt);
1033 if (!op_handler)
1034 return false;
1035 tree def_op1 = op_handler.operand1 ();
1036 tree def_op2 = op_handler.operand2 ();
1037 // if the def isn't binary, the relation will not be useful.
1038 if (!def_op2)
1039 return false;
1040
1041 // Determine if op2 is directly referenced as an operand.
1042 if (def_op1 == use_op)
1043 {
1044 // def_stmt has op1 in the 1st operand position.
1045 Value_Range other_op (TREE_TYPE (def_op2));
1046 src.get_operand (other_op, def_op2);
1047
1048 // Using op1_range as the LHS, and relation REL, evaluate op2.
1049 tree type = TREE_TYPE (def_op1);
1050 Value_Range new_result (type);
1051 if (!op_handler.op1_range (new_result, type,
1052 op1_def_p ? op1_range : op2_range,
99fda5de 1053 other_op, relation_trio::lhs_op1 (k)))
67166c9e
AM
1054 return false;
1055 if (op1_def_p)
1056 {
1057 change |= op2_range.intersect (new_result);
1058 // Recalculate op2.
1059 if (op_handler.fold_range (new_result, type, op2_range, other_op))
1060 {
1061 change |= op1_range.intersect (new_result);
1062 }
1063 }
1064 else
1065 {
1066 change |= op1_range.intersect (new_result);
1067 // Recalculate op1.
1068 if (op_handler.fold_range (new_result, type, op1_range, other_op))
1069 {
1070 change |= op2_range.intersect (new_result);
1071 }
1072 }
1073 }
1074 else if (def_op2 == use_op)
1075 {
1076 // def_stmt has op1 in the 1st operand position.
1077 Value_Range other_op (TREE_TYPE (def_op1));
1078 src.get_operand (other_op, def_op1);
1079
1080 // Using op1_range as the LHS, and relation REL, evaluate op2.
1081 tree type = TREE_TYPE (def_op2);
1082 Value_Range new_result (type);
1083 if (!op_handler.op2_range (new_result, type,
1084 op1_def_p ? op1_range : op2_range,
99fda5de 1085 other_op, relation_trio::lhs_op2 (k)))
67166c9e
AM
1086 return false;
1087 if (op1_def_p)
1088 {
1089 change |= op2_range.intersect (new_result);
1090 // Recalculate op1.
1091 if (op_handler.fold_range (new_result, type, other_op, op2_range))
1092 {
1093 change |= op1_range.intersect (new_result);
1094 }
1095 }
1096 else
1097 {
1098 change |= op1_range.intersect (new_result);
1099 // Recalculate op2.
1100 if (op_handler.fold_range (new_result, type, other_op, op1_range))
1101 {
1102 change |= op2_range.intersect (new_result);
1103 }
1104 }
1105 }
1106 return change;
1107}
1108
90e88fd3
AM
1109// Calculate a range for NAME from the operand 1 position of STMT
1110// assuming the result of the statement is LHS. Return the range in
1111// R, or false if no range could be calculated.
1112
1113bool
51ce0638
AM
1114gori_compute::compute_operand1_range (vrange &r,
1115 gimple_range_op_handler &handler,
018e7f16 1116 const vrange &lhs,
431cdfbe 1117 fur_source &src, value_relation *rel)
90e88fd3 1118{
51ce0638
AM
1119 gimple *stmt = handler.stmt ();
1120 tree op1 = handler.operand1 ();
1121 tree op2 = handler.operand2 ();
67166c9e 1122 tree lhs_name = gimple_get_lhs (stmt);
51ce0638 1123
99fda5de
AM
1124 relation_trio trio;
1125 if (rel)
1126 trio = rel->create_trio (lhs_name, op1, op2);
1127
45c8523d 1128 Value_Range op1_range (TREE_TYPE (op1));
45c8523d 1129 Value_Range op2_range (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
90e88fd3 1130
47ea02bb
AM
1131 // Fetch the known range for op1 in this block.
1132 src.get_operand (op1_range, op1);
90e88fd3 1133
c46b5b0a 1134 // Now range-op calculate and put that result in r.
90e88fd3
AM
1135 if (op2)
1136 {
47ea02bb 1137 src.get_operand (op2_range, op2);
dd63bba0 1138
70d1e3f4 1139 relation_kind op_op = trio.op1_op2 ();
99fda5de
AM
1140 if (op_op != VREL_VARYING)
1141 refine_using_relation (op1, op1_range, op2, op2_range, src, op_op);
1142
dd63bba0
AM
1143 // If op1 == op2, create a new trio for just this call.
1144 if (op1 == op2 && gimple_range_ssa_p (op1))
1145 trio = relation_trio (trio.lhs_op1 (), trio.lhs_op2 (), VREL_EQ);
018e7f16 1146 if (!handler.calc_op1 (r, lhs, op2_range, trio))
90e88fd3
AM
1147 return false;
1148 }
1149 else
1150 {
c46b5b0a 1151 // We pass op1_range to the unary operation. Normally it's a
90e88fd3
AM
1152 // hidden range_for_type parameter, but sometimes having the
1153 // actual range can result in better information.
018e7f16 1154 if (!handler.calc_op1 (r, lhs, op1_range, trio))
90e88fd3
AM
1155 return false;
1156 }
1157
4759e1e0
AM
1158 unsigned idx;
1159 if ((idx = tracer.header ("compute op 1 (")))
1160 {
1161 print_generic_expr (dump_file, op1, TDF_SLIM);
1162 fprintf (dump_file, ") at ");
1163 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1164 tracer.print (idx, "LHS =");
1165 lhs.dump (dump_file);
1166 if (op2 && TREE_CODE (op2) == SSA_NAME)
1167 {
1168 fprintf (dump_file, ", ");
1169 print_generic_expr (dump_file, op2, TDF_SLIM);
1170 fprintf (dump_file, " = ");
1171 op2_range.dump (dump_file);
1172 }
1173 fprintf (dump_file, "\n");
1174 tracer.print (idx, "Computes ");
1175 print_generic_expr (dump_file, op1, TDF_SLIM);
1176 fprintf (dump_file, " = ");
018e7f16 1177 r.dump (dump_file);
4759e1e0
AM
1178 fprintf (dump_file, " intersect Known range : ");
1179 op1_range.dump (dump_file);
1180 fputc ('\n', dump_file);
1181 }
90e88fd3 1182
018e7f16 1183 r.intersect (op1_range);
4759e1e0 1184 if (idx)
018e7f16
AM
1185 tracer.trailer (idx, "produces ", true, op1, r);
1186 return true;
90e88fd3
AM
1187}
1188
1189
1190// Calculate a range for NAME from the operand 2 position of S
1191// assuming the result of the statement is LHS. Return the range in
1192// R, or false if no range could be calculated.
1193
1194bool
51ce0638
AM
1195gori_compute::compute_operand2_range (vrange &r,
1196 gimple_range_op_handler &handler,
988b07a6 1197 const vrange &lhs,
431cdfbe 1198 fur_source &src, value_relation *rel)
90e88fd3 1199{
51ce0638
AM
1200 gimple *stmt = handler.stmt ();
1201 tree op1 = handler.operand1 ();
1202 tree op2 = handler.operand2 ();
67166c9e 1203 tree lhs_name = gimple_get_lhs (stmt);
51ce0638 1204
45c8523d
AH
1205 Value_Range op1_range (TREE_TYPE (op1));
1206 Value_Range op2_range (TREE_TYPE (op2));
90e88fd3 1207
47ea02bb
AM
1208 src.get_operand (op1_range, op1);
1209 src.get_operand (op2_range, op2);
99fda5de
AM
1210
1211 relation_trio trio;
67166c9e 1212 if (rel)
99fda5de
AM
1213 trio = rel->create_trio (lhs_name, op1, op2);
1214 relation_kind op_op = trio.op1_op2 ();
dd63bba0 1215
99fda5de
AM
1216 if (op_op != VREL_VARYING)
1217 refine_using_relation (op1, op1_range, op2, op2_range, src, op_op);
67166c9e 1218
dd63bba0
AM
1219 // If op1 == op2, create a new trio for this stmt.
1220 if (op1 == op2 && gimple_range_ssa_p (op1))
1221 trio = relation_trio (trio.lhs_op1 (), trio.lhs_op2 (), VREL_EQ);
90e88fd3 1222 // Intersect with range for op2 based on lhs and op1.
988b07a6 1223 if (!handler.calc_op2 (r, lhs, op1_range, trio))
46f4a397 1224 return false;
90e88fd3 1225
4759e1e0
AM
1226 unsigned idx;
1227 if ((idx = tracer.header ("compute op 2 (")))
1228 {
1229 print_generic_expr (dump_file, op2, TDF_SLIM);
1230 fprintf (dump_file, ") at ");
1231 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1232 tracer.print (idx, "LHS = ");
1233 lhs.dump (dump_file);
1234 if (TREE_CODE (op1) == SSA_NAME)
1235 {
1236 fprintf (dump_file, ", ");
1237 print_generic_expr (dump_file, op1, TDF_SLIM);
1238 fprintf (dump_file, " = ");
1239 op1_range.dump (dump_file);
1240 }
1241 fprintf (dump_file, "\n");
1242 tracer.print (idx, "Computes ");
1243 print_generic_expr (dump_file, op2, TDF_SLIM);
1244 fprintf (dump_file, " = ");
988b07a6 1245 r.dump (dump_file);
4759e1e0
AM
1246 fprintf (dump_file, " intersect Known range : ");
1247 op2_range.dump (dump_file);
1248 fputc ('\n', dump_file);
1249 }
47ea02bb 1250 // Intersect the calculated result with the known result and return if done.
988b07a6 1251 r.intersect (op2_range);
4759e1e0 1252 if (idx)
988b07a6
AM
1253 tracer.trailer (idx, " produces ", true, op2, r);
1254 return true;
90e88fd3
AM
1255}
1256
1257// Calculate a range for NAME from both operand positions of S
1258// assuming the result of the statement is LHS. Return the range in
1259// R, or false if no range could be calculated.
1260
1261bool
45c8523d 1262gori_compute::compute_operand1_and_operand2_range (vrange &r,
51ce0638
AM
1263 gimple_range_op_handler
1264 &handler,
45c8523d 1265 const vrange &lhs,
47ea02bb 1266 tree name,
431cdfbe
AM
1267 fur_source &src,
1268 value_relation *rel)
90e88fd3 1269{
45c8523d 1270 Value_Range op_range (TREE_TYPE (name));
90e88fd3 1271
988b07a6 1272 Value_Range vr (TREE_TYPE (handler.operand2 ()));
4dfeb1cd 1273 // Calculate a good a range through op2.
988b07a6
AM
1274 if (!compute_operand2_range (vr, handler, lhs, src, rel))
1275 return false;
1276 gimple *src_stmt = SSA_NAME_DEF_STMT (handler.operand2 ());
1277 gcc_checking_assert (src_stmt);
1278 // Then feed this range back as the LHS of the defining statement.
1279 if (!compute_operand_range (r, src_stmt, vr, name, src, rel))
90e88fd3
AM
1280 return false;
1281
1282 // Now get the range thru op1.
988b07a6 1283 vr.set_type (TREE_TYPE (handler.operand1 ()));
018e7f16
AM
1284 if (!compute_operand1_range (vr, handler, lhs, src, rel))
1285 return false;
988b07a6 1286 src_stmt = SSA_NAME_DEF_STMT (handler.operand1 ());
018e7f16
AM
1287 gcc_checking_assert (src_stmt);
1288 // Then feed this range back as the LHS of the defining statement.
1289 if (!compute_operand_range (op_range, src_stmt, vr, name, src, rel))
90e88fd3
AM
1290 return false;
1291
47ea02bb
AM
1292 // Both operands have to be simultaneously true, so perform an intersection.
1293 r.intersect (op_range);
90e88fd3
AM
1294 return true;
1295}
90e88fd3 1296
ed4a5f57 1297// Return TRUE if NAME can be recomputed on any edge exiting BB. If any
c46b5b0a 1298// direct dependent is exported, it may also change the computed value of NAME.
786188e8
AM
1299
1300bool
429a7a88 1301gori_compute::may_recompute_p (tree name, basic_block bb, int depth)
786188e8
AM
1302{
1303 tree dep1 = depend1 (name);
1304 tree dep2 = depend2 (name);
1305
c46b5b0a 1306 // If the first dependency is not set, there is no recomputation.
7f056d5f
AM
1307 // Dependencies reflect original IL, not current state. Check if the
1308 // SSA_NAME is still valid as well.
40c7f943 1309 if (!dep1)
786188e8
AM
1310 return false;
1311
1312 // Don't recalculate PHIs or statements with side_effects.
1313 gimple *s = SSA_NAME_DEF_STMT (name);
1314 if (is_a<gphi *> (s) || gimple_has_side_effects (s))
1315 return false;
1316
429a7a88
AM
1317 if (!dep2)
1318 {
1319 // -1 indicates a default param, convert it to the real default.
1320 if (depth == -1)
1321 {
1322 depth = (int)param_ranger_recompute_depth;
1323 gcc_checking_assert (depth >= 1);
1324 }
786188e8 1325
429a7a88
AM
1326 bool res = (bb ? is_export_p (dep1, bb) : is_export_p (dep1));
1327 if (res || depth <= 1)
1328 return res;
1329 // Check another level of recomputation.
1330 return may_recompute_p (dep1, bb, --depth);
1331 }
1332 // Two dependencies terminate the depth of the search.
1333 if (bb)
1334 return is_export_p (dep1, bb) || is_export_p (dep2, bb);
1335 else
1336 return is_export_p (dep1) || is_export_p (dep2);
786188e8
AM
1337}
1338
c46b5b0a 1339// Return TRUE if NAME can be recomputed on edge E. If any direct dependent
ed4a5f57
AM
1340// is exported on edge E, it may change the computed value of NAME.
1341
1342bool
429a7a88 1343gori_compute::may_recompute_p (tree name, edge e, int depth)
ed4a5f57
AM
1344{
1345 gcc_checking_assert (e);
429a7a88 1346 return may_recompute_p (name, e->src, depth);
ed4a5f57
AM
1347}
1348
1349
1350// Return TRUE if a range can be calculated or recomputed for NAME on any
1351// edge exiting BB.
1352
1353bool
1354gori_compute::has_edge_range_p (tree name, basic_block bb)
1355{
1356 // Check if NAME is an export or can be recomputed.
1357 if (bb)
1358 return is_export_p (name, bb) || may_recompute_p (name, bb);
1359
1360 // If no block is specified, check for anywhere in the IL.
1361 return is_export_p (name) || may_recompute_p (name);
1362}
1363
1364// Return TRUE if a range can be calculated or recomputed for NAME on edge E.
1365
1366bool
1367gori_compute::has_edge_range_p (tree name, edge e)
1368{
1369 gcc_checking_assert (e);
1370 return has_edge_range_p (name, e->src);
1371}
1372
90e88fd3
AM
1373// Calculate a range on edge E and return it in R. Try to evaluate a
1374// range for NAME on this edge. Return FALSE if this is either not a
1375// control edge or NAME is not defined by this edge.
1376
1377bool
45c8523d 1378gori_compute::outgoing_edge_range_p (vrange &r, edge e, tree name,
47ea02bb 1379 range_query &q)
90e88fd3 1380{
4759e1e0 1381 unsigned idx;
90e88fd3 1382
053e1d64 1383 if ((e->flags & m_not_executable_flag))
73cf73af
AM
1384 {
1385 r.set_undefined ();
1386 if (dump_file && (dump_flags & TDF_DETAILS))
1387 fprintf (dump_file, "Outgoing edge %d->%d unexecutable.\n",
1388 e->src->index, e->dest->index);
1389 return true;
1390 }
1391
90e88fd3 1392 gcc_checking_assert (gimple_range_ssa_p (name));
45c8523d 1393 int_range_max lhs;
90e88fd3
AM
1394 // Determine if there is an outgoing edge.
1395 gimple *stmt = outgoing.edge_range_p (lhs, e);
1396 if (!stmt)
1397 return false;
1398
87f9ac93 1399 fur_stmt src (stmt, &q);
90e88fd3 1400 // If NAME can be calculated on the edge, use that.
28ceee1b 1401 if (is_export_p (name, e->src))
6e02de94 1402 {
4759e1e0
AM
1403 bool res;
1404 if ((idx = tracer.header ("outgoing_edge")))
1405 {
1406 fprintf (dump_file, " for ");
1407 print_generic_expr (dump_file, name, TDF_SLIM);
1408 fprintf (dump_file, " on edge %d->%d\n",
1409 e->src->index, e->dest->index);
1410 }
1411 if ((res = compute_operand_range (r, stmt, lhs, name, src)))
6e02de94
AM
1412 {
1413 // Sometimes compatible types get interchanged. See PR97360.
1414 // Make sure we are returning the type of the thing we asked for.
1415 if (!r.undefined_p () && r.type () != TREE_TYPE (name))
1416 {
1417 gcc_checking_assert (range_compatible_p (r.type (),
1418 TREE_TYPE (name)));
1419 range_cast (r, TREE_TYPE (name));
1420 }
6e02de94 1421 }
4759e1e0
AM
1422 if (idx)
1423 tracer.trailer (idx, "outgoing_edge", res, name, r);
1424 return res;
6e02de94 1425 }
786188e8
AM
1426 // If NAME isn't exported, check if it can be recomputed.
1427 else if (may_recompute_p (name, e))
1428 {
1429 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1430
4759e1e0 1431 if ((idx = tracer.header ("recomputation")))
786188e8 1432 {
4759e1e0 1433 fprintf (dump_file, " attempt on edge %d->%d for ",
786188e8 1434 e->src->index, e->dest->index);
4759e1e0 1435 print_gimple_stmt (dump_file, def_stmt, 0, TDF_SLIM);
786188e8 1436 }
8cbaa093 1437 // Simply calculate DEF_STMT on edge E using the range query Q.
786188e8 1438 fold_range (r, def_stmt, e, &q);
4759e1e0
AM
1439 if (idx)
1440 tracer.trailer (idx, "recomputation", true, name, r);
786188e8
AM
1441 return true;
1442 }
90e88fd3
AM
1443 return false;
1444}
1445
e15425e8
AM
1446// Given COND ? OP1 : OP2 with ranges R1 for OP1 and R2 for OP2, Use gori
1447// to further resolve R1 and R2 if there are any dependencies between
1448// OP1 and COND or OP2 and COND. All values can are to be calculated using SRC
1449// as the origination source location for operands..
1450// Effectively, use COND an the edge condition and solve for OP1 on the true
1451// edge and OP2 on the false edge.
1452
1453bool
45c8523d 1454gori_compute::condexpr_adjust (vrange &r1, vrange &r2, gimple *, tree cond,
e15425e8
AM
1455 tree op1, tree op2, fur_source &src)
1456{
e15425e8
AM
1457 tree ssa1 = gimple_range_ssa_p (op1);
1458 tree ssa2 = gimple_range_ssa_p (op2);
1459 if (!ssa1 && !ssa2)
1460 return false;
68e00633 1461 if (TREE_CODE (cond) != SSA_NAME)
e15425e8 1462 return false;
68e00633
RB
1463 gassign *cond_def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (cond));
1464 if (!cond_def
1465 || TREE_CODE_CLASS (gimple_assign_rhs_code (cond_def)) != tcc_comparison)
e15425e8 1466 return false;
68e00633
RB
1467 tree type = TREE_TYPE (gimple_assign_rhs1 (cond_def));
1468 if (!range_compatible_p (type, TREE_TYPE (gimple_assign_rhs2 (cond_def))))
1469 return false;
2eb50117 1470 range_op_handler hand (gimple_assign_rhs_code (cond_def));
e15425e8
AM
1471 if (!hand)
1472 return false;
1473
68e00633
RB
1474 tree c1 = gimple_range_ssa_p (gimple_assign_rhs1 (cond_def));
1475 tree c2 = gimple_range_ssa_p (gimple_assign_rhs2 (cond_def));
e15425e8
AM
1476
1477 // Only solve if there is one SSA name in the condition.
1478 if ((!c1 && !c2) || (c1 && c2))
1479 return false;
1480
1481 // Pick up the current values of each part of the condition.
45c8523d
AH
1482 tree rhs1 = gimple_assign_rhs1 (cond_def);
1483 tree rhs2 = gimple_assign_rhs2 (cond_def);
1484 Value_Range cl (TREE_TYPE (rhs1));
1485 Value_Range cr (TREE_TYPE (rhs2));
1486 src.get_operand (cl, rhs1);
1487 src.get_operand (cr, rhs2);
e15425e8
AM
1488
1489 tree cond_name = c1 ? c1 : c2;
1490 gimple *def_stmt = SSA_NAME_DEF_STMT (cond_name);
1491
1492 // Evaluate the value of COND_NAME on the true and false edges, using either
1493 // the op1 or op2 routines based on its location.
45c8523d 1494 Value_Range cond_true (type), cond_false (type);
e15425e8
AM
1495 if (c1)
1496 {
cf5bea76 1497 if (!hand.op1_range (cond_false, type, m_bool_zero, cr))
e15425e8 1498 return false;
cf5bea76 1499 if (!hand.op1_range (cond_true, type, m_bool_one, cr))
e15425e8
AM
1500 return false;
1501 cond_false.intersect (cl);
1502 cond_true.intersect (cl);
1503 }
1504 else
1505 {
cf5bea76 1506 if (!hand.op2_range (cond_false, type, m_bool_zero, cl))
e15425e8 1507 return false;
cf5bea76 1508 if (!hand.op2_range (cond_true, type, m_bool_one, cl))
e15425e8
AM
1509 return false;
1510 cond_false.intersect (cr);
1511 cond_true.intersect (cr);
1512 }
1513
1514 unsigned idx;
1515 if ((idx = tracer.header ("cond_expr evaluation : ")))
1516 {
1517 fprintf (dump_file, " range1 = ");
1518 r1.dump (dump_file);
1519 fprintf (dump_file, ", range2 = ");
1520 r1.dump (dump_file);
1521 fprintf (dump_file, "\n");
1522 }
1523
1524 // Now solve for SSA1 or SSA2 if they are in the dependency chain.
1525 if (ssa1 && in_chain_p (ssa1, cond_name))
1526 {
ef623bb5
AM
1527 Value_Range tmp1 (TREE_TYPE (ssa1));
1528 if (compute_operand_range (tmp1, def_stmt, cond_true, ssa1, src))
1529 r1.intersect (tmp1);
e15425e8
AM
1530 }
1531 if (ssa2 && in_chain_p (ssa2, cond_name))
1532 {
ef623bb5
AM
1533 Value_Range tmp2 (TREE_TYPE (ssa2));
1534 if (compute_operand_range (tmp2, def_stmt, cond_false, ssa2, src))
1535 r2.intersect (tmp2);
e15425e8
AM
1536 }
1537 if (idx)
1538 {
1539 tracer.print (idx, "outgoing: range1 = ");
1540 r1.dump (dump_file);
1541 fprintf (dump_file, ", range2 = ");
1542 r1.dump (dump_file);
1543 fprintf (dump_file, "\n");
1544 tracer.trailer (idx, "cond_expr", true, cond_name, cond_true);
1545 }
1546 return true;
1547}
1548
ed4a5f57
AM
1549// Dump what is known to GORI computes to listing file F.
1550
1551void
1552gori_compute::dump (FILE *f)
1553{
1554 gori_map::dump (f);
1555}
c2164470
AM
1556
1557// ------------------------------------------------------------------------
1558// GORI iterator. Although we have bitmap iterators, don't expose that it
1559// is currently a bitmap. Use an export iterator to hide future changes.
1560
1561// Construct a basic iterator over an export bitmap.
1562
1563gori_export_iterator::gori_export_iterator (bitmap b)
1564{
1565 bm = b;
1566 if (b)
1567 bmp_iter_set_init (&bi, b, 1, &y);
1568}
1569
1570
1571// Move to the next export bitmap spot.
1572
1573void
1574gori_export_iterator::next ()
1575{
1576 bmp_iter_next (&bi, &y);
1577}
1578
1579
1580// Fetch the name of the next export in the export list. Return NULL if
1581// iteration is done.
1582
1583tree
1584gori_export_iterator::get_name ()
1585{
1586 if (!bm)
1587 return NULL_TREE;
1588
1589 while (bmp_iter_set (&bi, &y))
1590 {
1591 tree t = ssa_name (y);
1592 if (t)
1593 return t;
1594 next ();
1595 }
1596 return NULL_TREE;
1597}