1 /* Gimple range edge functionaluity.
2 Copyright (C) 2020-2022 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
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)
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.
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/>. */
25 #include "coretypes.h"
30 #include "gimple-pretty-print.h"
31 #include "gimple-iterator.h"
33 #include "gimple-range.h"
35 // If there is a range control statment at the end of block BB, return it.
36 // Otherwise return NULL.
39 gimple_outgoing_range_stmt_p (basic_block bb
)
41 gimple_stmt_iterator gsi
= gsi_last_nondebug_bb (bb
);
44 gimple
*s
= gsi_stmt (gsi
);
45 if (is_a
<gcond
*> (s
) && range_op_handler (s
))
46 return gsi_stmt (gsi
);
47 if (is_a
<gswitch
*> (s
))
48 return gsi_stmt (gsi
);
54 // Return a TRUE or FALSE range representing the edge value of a GCOND.
57 gcond_edge_range (irange
&r
, edge e
)
59 gcc_checking_assert (e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
));
60 if (e
->flags
& EDGE_TRUE_VALUE
)
61 r
= int_range
<2> (boolean_true_node
, boolean_true_node
);
63 r
= int_range
<2> (boolean_false_node
, boolean_false_node
);
67 gimple_outgoing_range::gimple_outgoing_range (int max_sw_edges
)
70 m_max_edges
= max_sw_edges
;
74 gimple_outgoing_range::~gimple_outgoing_range ()
81 // Get a range for a switch edge E from statement S and return it in R.
82 // Use a cached value if it exists, or calculate it if not.
85 gimple_outgoing_range::switch_edge_range (irange
&r
, gswitch
*sw
, edge e
)
87 // ADA currently has cases where the index is 64 bits and the case
88 // arguments are 32 bit, causing a trap when we create a case_range.
89 // Until this is resolved (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87798)
90 // punt on switches where the labels dont match the argument.
91 if (gimple_switch_num_labels (sw
) > 1 &&
92 TYPE_PRECISION (TREE_TYPE (CASE_LOW (gimple_switch_label (sw
, 1)))) !=
93 TYPE_PRECISION (TREE_TYPE (gimple_switch_index (sw
))))
97 m_edge_table
= new hash_map
<edge
, irange
*> (n_edges_for_fn (cfun
));
99 irange
**val
= m_edge_table
->get (e
);
102 calc_switch_ranges (sw
);
103 val
= m_edge_table
->get (e
);
104 gcc_checking_assert (val
);
111 // Calculate all switch edges from SW and cache them in the hash table.
114 gimple_outgoing_range::calc_switch_ranges (gswitch
*sw
)
118 lim
= gimple_switch_num_labels (sw
);
119 tree type
= TREE_TYPE (gimple_switch_index (sw
));
120 edge default_edge
= gimple_switch_default_edge (cfun
, sw
);
122 // This should be the first call into this switch.
124 // Allocate an int_range_max for the default range case, start with
125 // varying and intersect each other case from it.
126 int_range_max
default_range (type
);
128 for (x
= 1; x
< lim
; x
++)
130 edge e
= gimple_switch_edge (cfun
, sw
, x
);
132 // If this edge is the same as the default edge, do nothing else.
133 if (e
== default_edge
)
136 tree low
= CASE_LOW (gimple_switch_label (sw
, x
));
137 tree high
= CASE_HIGH (gimple_switch_label (sw
, x
));
141 // Remove the case range from the default case.
142 int_range_max
def_range (low
, high
);
143 range_cast (def_range
, type
);
145 default_range
.intersect (def_range
);
147 // Create/union this case with anything on else on the edge.
148 int_range_max
case_range (low
, high
);
149 range_cast (case_range
, type
);
150 irange
*&slot
= m_edge_table
->get_or_insert (e
, &existed
);
153 // If this doesn't change the value, move on.
154 if (!case_range
.union_ (*slot
))
156 if (slot
->fits_p (case_range
))
162 // If there was an existing range and it doesn't fit, we lose the memory.
163 // It'll get reclaimed when the obstack is freed. This seems less
164 // intrusive than allocating max ranges for each case.
165 slot
= m_range_allocator
.clone
<irange
> (case_range
);
168 irange
*&slot
= m_edge_table
->get_or_insert (default_edge
, &existed
);
169 // This should be the first call into this switch.
170 gcc_checking_assert (!existed
);
171 irange
*dr
= m_range_allocator
.clone
<irange
> (default_range
);
176 // Calculate the range forced on on edge E by control flow, return it
177 // in R. Return the statment which defines the range, otherwise
181 gimple_outgoing_range::edge_range_p (irange
&r
, edge e
)
183 if (single_succ_p (e
->src
))
186 // Determine if there is an outgoing edge.
187 gimple
*s
= gimple_outgoing_range_stmt_p (e
->src
);
191 if (is_a
<gcond
*> (s
))
193 gcond_edge_range (r
, e
);
197 // Only process switches if it within the size limit.
198 if (EDGE_COUNT (e
->src
->succs
) > (unsigned)m_max_edges
)
201 gcc_checking_assert (is_a
<gswitch
*> (s
));
202 gswitch
*sw
= as_a
<gswitch
*> (s
);
204 // Switches can only be integers.
205 if (switch_edge_range (as_a
<irange
> (r
), sw
, e
))