Line data Source code
1 : /* Pretty print support for value ranges.
2 : Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 : Contributed by Aldy Hernandez <aldyh@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "ssa.h"
28 : #include "tree-pretty-print.h"
29 : #include "fold-const.h"
30 : #include "gimple-range.h"
31 : #include "value-range-pretty-print.h"
32 :
33 : static void
34 90930 : print_int_bound (pretty_printer *pp, const wide_int &bound, tree type)
35 : {
36 90930 : wide_int type_min = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
37 90930 : wide_int type_max = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
38 :
39 90930 : if (INTEGRAL_TYPE_P (type)
40 87198 : && !TYPE_UNSIGNED (type)
41 23792 : && bound == type_min
42 91448 : && TYPE_PRECISION (type) != 1)
43 518 : pp_string (pp, "-INF");
44 90412 : else if (bound == type_max && TYPE_PRECISION (type) != 1)
45 14015 : pp_string (pp, "+INF");
46 : else
47 76397 : pp_wide_int (pp, bound, TYPE_SIGN (type));
48 90930 : }
49 :
50 : static void
51 41508 : print_irange_bitmasks (pretty_printer *pp, const irange_bitmask &bm)
52 : {
53 41508 : if (bm.unknown_p ())
54 32996 : return;
55 :
56 8512 : pp_string (pp, " MASK ");
57 8512 : char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p;
58 8512 : unsigned len_mask, len_val;
59 17024 : if (print_hex_buf_size (bm.mask (), &len_mask)
60 8512 : | print_hex_buf_size (bm.value (), &len_val))
61 0 : p = XALLOCAVEC (char, MAX (len_mask, len_val));
62 : else
63 : p = buf;
64 8512 : print_hex (bm.mask (), p);
65 8512 : pp_string (pp, p);
66 8512 : pp_string (pp, " VALUE ");
67 8512 : print_hex (bm.value (), p);
68 8512 : pp_string (pp, p);
69 : }
70 :
71 : void
72 603 : vrange_printer::visit (const unsupported_range &r) const
73 : {
74 603 : pp_string (pp, "[unsupported_range] ");
75 603 : if (r.undefined_p ())
76 : {
77 0 : pp_string (pp, "UNDEFINED");
78 0 : return;
79 : }
80 603 : if (r.varying_p ())
81 : {
82 603 : pp_string (pp, "VARYING");
83 603 : return;
84 : }
85 0 : gcc_unreachable ();
86 : }
87 :
88 : void
89 42024 : vrange_printer::visit (const irange &r) const
90 : {
91 42024 : pp_string (pp, "[irange] ");
92 42024 : if (r.undefined_p ())
93 : {
94 16 : pp_string (pp, "UNDEFINED");
95 16 : return;
96 : }
97 42008 : dump_generic_node (pp, r.type (), 0, TDF_NONE | TDF_NOUID, false);
98 42008 : pp_character (pp, ' ');
99 42008 : if (r.varying_p ())
100 : {
101 2366 : pp_string (pp, "VARYING");
102 2366 : return;
103 : }
104 83241 : for (unsigned i = 0; i < r.num_pairs (); ++i)
105 : {
106 43599 : pp_character (pp, '[');
107 43599 : print_int_bound (pp, r.lower_bound (i), r.type ());
108 43599 : pp_string (pp, ", ");
109 43599 : print_int_bound (pp, r.upper_bound (i), r.type ());
110 43599 : pp_character (pp, ']');
111 : }
112 39642 : print_irange_bitmasks (pp, r.m_bitmask);
113 : }
114 :
115 : void
116 1866 : vrange_printer::print_addr (const prange &r) const
117 : {
118 1866 : bool known_p = false;
119 1866 : tree addr = r.addr_known ();
120 : if (addr)
121 : known_p = true;
122 : else
123 1583 : addr = r.addr_excluded ();
124 :
125 : if (addr)
126 : {
127 285 : if (known_p)
128 283 : pp_string (pp, " -> ");
129 : else
130 2 : pp_string (pp, " ! -> ");
131 285 : dump_generic_node (pp, addr, 0, TDF_NONE | TDF_NOUID, false);
132 : }
133 1866 : }
134 :
135 : void
136 1975 : vrange_printer::visit (const prange &r) const
137 : {
138 1975 : pp_string (pp, "[prange] ");
139 1975 : if (r.undefined_p ())
140 : {
141 6 : pp_string (pp, "UNDEFINED");
142 6 : return;
143 : }
144 1969 : dump_generic_node (pp, r.type (), 0, TDF_NONE | TDF_NOUID, false);
145 1969 : pp_character (pp, ' ');
146 1969 : if (r.varying_p ())
147 : {
148 103 : pp_string (pp, "VARYING");
149 103 : return;
150 : }
151 :
152 1866 : pp_character (pp, '[');
153 1866 : print_int_bound (pp, r.lower_bound (), r.type ());
154 1866 : pp_string (pp, ", ");
155 1866 : print_int_bound (pp, r.upper_bound (), r.type ());
156 1866 : pp_character (pp, ']');
157 1866 : print_irange_bitmasks (pp, r.m_bitmask);
158 : // Dump the address field if there is one.
159 1866 : print_addr (r);
160 : }
161 :
162 : void
163 390 : vrange_printer::print_real_value (tree type, const REAL_VALUE_TYPE &r) const
164 : {
165 390 : char s[100];
166 390 : real_to_decimal_for_mode (s, &r, sizeof (s), 0, 1, TYPE_MODE (type));
167 390 : pp_string (pp, s);
168 390 : if (!DECIMAL_FLOAT_TYPE_P (type)
169 : // real_to_hexadecimal prints infinities and NAN as text. No
170 : // need to print them twice.
171 390 : && !real_isinf (&r)
172 691 : && !real_isnan (&r))
173 : {
174 301 : real_to_hexadecimal (s, &r, sizeof (s), 0, 1);
175 301 : pp_printf (pp, " (%s)", s);
176 : }
177 390 : }
178 :
179 : // Print an frange.
180 :
181 : void
182 259 : vrange_printer::visit (const frange &r) const
183 : {
184 259 : pp_string (pp, "[frange] ");
185 259 : if (r.undefined_p ())
186 : {
187 5 : pp_string (pp, "UNDEFINED");
188 5 : return;
189 : }
190 254 : tree type = r.type ();
191 254 : dump_generic_node (pp, type, 0, TDF_NONE, false);
192 254 : pp_string (pp, " ");
193 254 : if (r.varying_p ())
194 : {
195 69 : pp_string (pp, "VARYING");
196 69 : print_frange_nan (r);
197 69 : return;
198 : }
199 185 : if (r.known_isnan ())
200 3 : pp_string (pp, "[]");
201 : else
202 377 : for (unsigned i = 0; i < r.num_pairs (); ++i)
203 : {
204 195 : pp_character (pp, '[');
205 195 : print_real_value (type, r.lower_bound (i));
206 195 : pp_string (pp, ", ");
207 195 : print_real_value (type, r.upper_bound (i));
208 195 : pp_character (pp, ']');
209 : }
210 185 : print_frange_nan (r);
211 : }
212 :
213 : // Print the NAN info for an frange.
214 :
215 : void
216 254 : vrange_printer::print_frange_nan (const frange &r) const
217 : {
218 425 : if (r.maybe_isnan ())
219 : {
220 83 : if (r.m_pos_nan && r.m_neg_nan)
221 : {
222 70 : pp_string (pp, " +-NAN");
223 70 : return;
224 : }
225 13 : bool nan_sign = r.m_neg_nan;
226 13 : if (nan_sign)
227 0 : pp_string (pp, " -NAN");
228 : else
229 13 : pp_string (pp, " +NAN");
230 : }
231 : }
|