Debian Codegraph @

gcc/analyzer/region-model-manager.cc

raw
1 /* Consolidation of svalues and regions.
2 Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <[email protected]>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 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, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 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 #define INCLUDE_VECTOR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "diagnostic-core.h"
27 #include "gimple-pretty-print.h"
28 #include "function.h"
29 #include "basic-block.h"
30 #include "gimple.h"
31 #include "gimple-iterator.h"
32 #include "diagnostic-core.h"
33 #include "graphviz.h"
34 #include "options.h"
35 #include "cgraph.h"
36 #include "tree-dfa.h"
37 #include "stringpool.h"
38 #include "convert.h"
39 #include "target.h"
40 #include "fold-const.h"
41 #include "tree-pretty-print.h"
42 #include "bitmap.h"
43 #include "analyzer/analyzer.h"
44 #include "analyzer/analyzer-logging.h"
45 #include "ordered-hash-map.h"
46 #include "options.h"
47 #include "analyzer/supergraph.h"
48 #include "sbitmap.h"
49 #include "analyzer/call-string.h"
50 #include "analyzer/program-point.h"
51 #include "analyzer/store.h"
52 #include "analyzer/region-model.h"
53 #include "analyzer/constraint-manager.h"
54
55 #if ENABLE_ANALYZER
56
57 namespace ana {
58
59 /* class region_model_manager. */
60
61 /* region_model_manager's ctor. */
62
63 region_model_manager::region_model_manager (logger *logger)
64 : m_logger (logger),
65 m_next_symbol_id (0),
66 m_empty_call_string (),
67 m_root_region (alloc_symbol_id ()),
68 m_stack_region (alloc_symbol_id (), &m_root_region),
69 m_heap_region (alloc_symbol_id (), &m_root_region),
70 m_unknown_NULL (NULL),
71 m_checking_feasibility (false),
72 m_max_complexity (0, 0),
73 m_code_region (alloc_symbol_id (), &m_root_region),
74 m_fndecls_map (), m_labels_map (),
75 m_globals_region (alloc_symbol_id (), &m_root_region),
76 m_globals_map (),
77 m_thread_local_region (alloc_symbol_id (), &m_root_region),
78 m_errno_region (alloc_symbol_id (), &m_thread_local_region),
79 m_store_mgr (this),
80 m_range_mgr (new bounded_ranges_manager ()),
81 m_known_fn_mgr (logger)
82 {
83 }
84
85 /* region_model_manager's dtor. Delete all of the managed svalues
86 and regions. */
87
88 region_model_manager::~region_model_manager ()
89 {
90 /* Delete consolidated svalues. */
91 for (constants_map_t::iterator iter = m_constants_map.begin ();
92 iter != m_constants_map.end (); ++iter)
93 delete (*iter).second;
94 for (unknowns_map_t::iterator iter = m_unknowns_map.begin ();
95 iter != m_unknowns_map.end (); ++iter)
96 delete (*iter).second;
97 delete m_unknown_NULL;
98 for (poisoned_values_map_t::iterator iter = m_poisoned_values_map.begin ();
99 iter != m_poisoned_values_map.end (); ++iter)
100 delete (*iter).second;
101 for (setjmp_values_map_t::iterator iter = m_setjmp_values_map.begin ();
102 iter != m_setjmp_values_map.end (); ++iter)
103 delete (*iter).second;
104 for (initial_values_map_t::iterator iter = m_initial_values_map.begin ();
105 iter != m_initial_values_map.end (); ++iter)
106 delete (*iter).second;
107 for (pointer_values_map_t::iterator iter = m_pointer_values_map.begin ();
108 iter != m_pointer_values_map.end (); ++iter)
109 delete (*iter).second;
110 for (unaryop_values_map_t::iterator iter = m_unaryop_values_map.begin ();
111 iter != m_unaryop_values_map.end (); ++iter)
112 delete (*iter).second;
113 for (binop_values_map_t::iterator iter = m_binop_values_map.begin ();
114 iter != m_binop_values_map.end (); ++iter)
115 delete (*iter).second;
116 for (sub_values_map_t::iterator iter = m_sub_values_map.begin ();
117 iter != m_sub_values_map.end (); ++iter)
118 delete (*iter).second;
119 for (auto iter : m_repeated_values_map)
120 delete iter.second;
121 for (auto iter : m_bits_within_values_map)
122 delete iter.second;
123 for (unmergeable_values_map_t::iterator iter
124 = m_unmergeable_values_map.begin ();
125 iter != m_unmergeable_values_map.end (); ++iter)
126 delete (*iter).second;
127 for (widening_values_map_t::iterator iter = m_widening_values_map.begin ();
128 iter != m_widening_values_map.end (); ++iter)
129 delete (*iter).second;
130 for (compound_values_map_t::iterator iter = m_compound_values_map.begin ();
131 iter != m_compound_values_map.end (); ++iter)
132 delete (*iter).second;
133 for (conjured_values_map_t::iterator iter = m_conjured_values_map.begin ();
134 iter != m_conjured_values_map.end (); ++iter)
135 delete (*iter).second;
136 for (auto iter : m_asm_output_values_map)
137 delete iter.second;
138 for (auto iter : m_const_fn_result_values_map)
139 delete iter.second;
140
141 /* Delete consolidated regions. */
142 for (fndecls_map_t::iterator iter = m_fndecls_map.begin ();
143 iter != m_fndecls_map.end (); ++iter)
144 delete (*iter).second;
145 for (labels_map_t::iterator iter = m_labels_map.begin ();
146 iter != m_labels_map.end (); ++iter)
147 delete (*iter).second;
148 for (globals_map_t::iterator iter = m_globals_map.begin ();
149 iter != m_globals_map.end (); ++iter)
150 delete (*iter).second;
151 for (string_map_t::iterator iter = m_string_map.begin ();
152 iter != m_string_map.end (); ++iter)
153 delete (*iter).second;
154
155 delete m_range_mgr;
156 }
157
158 /* Return true if C exceeds the complexity limit for svalues. */
159
160 bool
161 region_model_manager::too_complex_p (const complexity &c) const
162 {
163 if (c.m_max_depth > (unsigned)param_analyzer_max_svalue_depth)
164 return true;
165 return false;
166 }
167
168 /* If SVAL exceeds the complexity limit for svalues, delete it
169 and return true.
170 Otherwise update m_max_complexity and return false. */
171
172 bool
173 region_model_manager::reject_if_too_complex (svalue *sval)
174 {
175 if (m_checking_feasibility)
176 return false;
177
178 const complexity &c = sval->get_complexity ();
179 if (!too_complex_p (c))
180 {
181 if (m_max_complexity.m_num_nodes < c.m_num_nodes)
182 m_max_complexity.m_num_nodes = c.m_num_nodes;
183 if (m_max_complexity.m_max_depth < c.m_max_depth)
184 m_max_complexity.m_max_depth = c.m_max_depth;
185 return false;
186 }
187
188 pretty_printer pp;
189 pp_format_decoder (&pp) = default_tree_printer;
190 sval->dump_to_pp (&pp, true);
191 if (warning_at (input_location, OPT_Wanalyzer_symbol_too_complex,
192 "symbol too complicated: %qs",
193 pp_formatted_text (&pp)))
194 inform (input_location,
195 "max_depth %i exceeds --param=analyzer-max-svalue-depth=%i",
196 c.m_max_depth, param_analyzer_max_svalue_depth);
197
198 delete sval;
199 return true;
200 }
201
202 /* Macro for imposing a complexity limit on svalues, for use within
203 region_model_manager member functions.
204
205 If SVAL exceeds the complexity limit, delete it and return an UNKNOWN
206 value of the same type.
207 Otherwise update m_max_complexity and carry on. */
208
209 #define RETURN_UNKNOWN_IF_TOO_COMPLEX(SVAL) \
210 do { \
211 svalue *sval_ = (SVAL); \
212 tree type_ = sval_->get_type (); \
213 if (reject_if_too_complex (sval_)) \
214 return get_or_create_unknown_svalue (type_); \
215 } while (0)
216
217 /* svalue consolidation. */
218
219 /* Return the svalue * for a constant_svalue for CST_EXPR,
220 creating it if necessary.
221 The constant_svalue instances are reused, based on pointer equality
222 of trees */
223
224 const svalue *
225 region_model_manager::get_or_create_constant_svalue (tree type, tree cst_expr)
226 {
227 gcc_assert (cst_expr);
228 gcc_assert (CONSTANT_CLASS_P (cst_expr));
229 gcc_assert (type == TREE_TYPE (cst_expr) || type == NULL_TREE);
230
231 constant_svalue::key_t key (type, cst_expr);
232 constant_svalue **slot = m_constants_map.get (key);
233 if (slot)
234 return *slot;
235 constant_svalue *cst_sval
236 = new constant_svalue (alloc_symbol_id (), type, cst_expr);
237 RETURN_UNKNOWN_IF_TOO_COMPLEX (cst_sval);
238 m_constants_map.put (key, cst_sval);
239 return cst_sval;
240 }
241
242 const svalue *
243 region_model_manager::get_or_create_constant_svalue (tree cst_expr)
244 {
245 tree type = TREE_TYPE (cst_expr);
246 if (TREE_CODE (cst_expr) == RAW_DATA_CST)
247 /* The type of a RAW_DATA_CST is the type of each element, rather than
248 that of the constant as a whole, so use NULL_TREE for simplicity. */
249 type = NULL_TREE;
250 return get_or_create_constant_svalue (type, cst_expr);
251 }
252
253 /* Return the svalue * for a constant_svalue for the INTEGER_CST
254 for VAL of type TYPE, creating it if necessary. */
255
256 const svalue *
257 region_model_manager::get_or_create_int_cst (tree type,
258 const poly_wide_int_ref &cst)
259 {
260 tree effective_type = type;
261 if (!type)
262 effective_type = ptrdiff_type_node;
263 gcc_assert (INTEGRAL_TYPE_P (effective_type)
264 || POINTER_TYPE_P (effective_type));
265 tree tree_cst = wide_int_to_tree (effective_type, cst);
266 return get_or_create_constant_svalue (type, tree_cst);
267 }
268
269 /* Return the svalue * for the constant_svalue for the NULL pointer
270 of POINTER_TYPE, creating it if necessary. */
271
272 const svalue *
273 region_model_manager::get_or_create_null_ptr (tree pointer_type)
274 {
275 gcc_assert (pointer_type);
276 gcc_assert (POINTER_TYPE_P (pointer_type));
277 return get_or_create_int_cst (pointer_type, 0);
278 }
279
280 /* Return the svalue * for a unknown_svalue for TYPE (which can be NULL),
281 creating it if necessary.
282 The unknown_svalue instances are reused, based on pointer equality
283 of the types */
284
285 const svalue *
286 region_model_manager::get_or_create_unknown_svalue (tree type)
287 {
288 /* Don't create unknown values when doing feasibility testing;
289 instead, create a unique svalue. */
290 if (m_checking_feasibility)
291 return create_unique_svalue (type);
292
293 /* Special-case NULL, so that the hash_map can use NULL as the
294 "empty" value. */
295 if (type == NULL_TREE)
296 {
297 if (!m_unknown_NULL)
298 m_unknown_NULL = new unknown_svalue (alloc_symbol_id (), type);
299 return m_unknown_NULL;
300 }
301
302 unknown_svalue **slot = m_unknowns_map.get (type);
303 if (slot)
304 return *slot;
305 unknown_svalue *sval = new unknown_svalue (alloc_symbol_id (), type);
306 m_unknowns_map.put (type, sval);
307 return sval;
308 }
309
310 /* Return a freshly-allocated svalue of TYPE, owned by this manager. */
311
312 const svalue *
313 region_model_manager::create_unique_svalue (tree type)
314 {
315 svalue *sval = new placeholder_svalue (alloc_symbol_id (), type, "unique");
316 m_managed_dynamic_svalues.safe_push (sval);
317 return sval;
318 }
319
320 /* Return the svalue * for the initial value of REG, creating it if
321 necessary. */
322
323 const svalue *
324 region_model_manager::get_or_create_initial_value (const region *reg,
325 bool check_poisoned)
326 {
327 if (!reg->can_have_initial_svalue_p () && check_poisoned)
328 return get_or_create_poisoned_svalue (POISON_KIND_UNINIT,
329 reg->get_type ());
330
331 /* The initial value of a cast is a cast of the initial value. */
332 if (const cast_region *cast_reg = reg->dyn_cast_cast_region ())
333 {
334 const region *original_reg = cast_reg->get_parent_region ();
335 return get_or_create_cast (cast_reg->get_type (),
336 get_or_create_initial_value (original_reg));
337 }
338
339 /* Simplify:
340 INIT_VAL(ELEMENT_REG(STRING_REG), CONSTANT_SVAL)
341 to:
342 CONSTANT_SVAL(STRING[N]). */
343 if (const element_region *element_reg = reg->dyn_cast_element_region ())
344 if (tree cst_idx = element_reg->get_index ()->maybe_get_constant ())
345 if (const string_region *string_reg
346 = element_reg->get_parent_region ()->dyn_cast_string_region ())
347 if (tree_fits_shwi_p (cst_idx))
348 {
349 HOST_WIDE_INT idx = tree_to_shwi (cst_idx);
350 tree string_cst = string_reg->get_string_cst ();
351 if (idx >= 0 && idx <= TREE_STRING_LENGTH (string_cst))
352 {
353 int ch = TREE_STRING_POINTER (string_cst)[idx];
354 return get_or_create_int_cst (reg->get_type (), ch);
355 }
356 }
357
358 /* INIT_VAL (*UNKNOWN_PTR) -> UNKNOWN_VAL. */
359 if (reg->symbolic_for_unknown_ptr_p ())
360 return get_or_create_unknown_svalue (reg->get_type ());
361
362 if (initial_svalue **slot = m_initial_values_map.get (reg))
363 return *slot;
364 initial_svalue *initial_sval
365 = new initial_svalue (alloc_symbol_id (), reg->get_type (), reg);
366 RETURN_UNKNOWN_IF_TOO_COMPLEX (initial_sval);
367 m_initial_values_map.put (reg, initial_sval);
368 return initial_sval;
369 }
370
371 /* Return the svalue * for R using type TYPE, creating it if
372 necessary. */
373
374 const svalue *
375 region_model_manager::get_or_create_setjmp_svalue (const setjmp_record &r,
376 tree type)
377 {
378 setjmp_svalue::key_t key (r, type);
379 if (setjmp_svalue **slot = m_setjmp_values_map.get (key))
380 return *slot;
381 setjmp_svalue *setjmp_sval = new setjmp_svalue (r, alloc_symbol_id (), type);
382 RETURN_UNKNOWN_IF_TOO_COMPLEX (setjmp_sval);
383 m_setjmp_values_map.put (key, setjmp_sval);
384 return setjmp_sval;
385 }
386
387 /* Return the svalue * for a poisoned value of KIND and TYPE, creating it if
388 necessary. */
389
390 const svalue *
391 region_model_manager::get_or_create_poisoned_svalue (enum poison_kind kind,
392 tree type)
393 {
394 poisoned_svalue::key_t key (kind, type);
395 if (poisoned_svalue **slot = m_poisoned_values_map.get (key))
396 return *slot;
397 poisoned_svalue *poisoned_sval
398 = new poisoned_svalue (kind, alloc_symbol_id (), type);
399 RETURN_UNKNOWN_IF_TOO_COMPLEX (poisoned_sval);
400 m_poisoned_values_map.put (key, poisoned_sval);
401 return poisoned_sval;
402 }
403
404 /* Return the svalue * for a pointer to POINTEE of type PTR_TYPE,
405 creating it if necessary. */
406
407 const svalue *
408 region_model_manager::get_ptr_svalue (tree ptr_type, const region *pointee)
409 {
410 /* If this is a symbolic region from dereferencing a pointer, and the types
411 match, then return the original pointer. */
412 if (const symbolic_region *sym_reg = pointee->dyn_cast_symbolic_region ())
413 if (ptr_type == sym_reg->get_pointer ()->get_type ())
414 return sym_reg->get_pointer ();
415
416 region_svalue::key_t key (ptr_type, pointee);
417 if (region_svalue **slot = m_pointer_values_map.get (key))
418 return *slot;
419 region_svalue *sval
420 = new region_svalue (alloc_symbol_id (), ptr_type, pointee);
421 RETURN_UNKNOWN_IF_TOO_COMPLEX (sval);
422 m_pointer_values_map.put (key, sval);
423 return sval;
424 }
425
426 /* Subroutine of region_model_manager::get_or_create_unaryop.
427 Attempt to fold the inputs and return a simpler svalue *.
428 Otherwise, return NULL. */
429
430 const svalue *
431 region_model_manager::maybe_fold_unaryop (tree type, enum tree_code op,
432 const svalue *arg)
433 {
434 /* Ops on "unknown" are also unknown. */
435 if (arg->get_kind () == SK_UNKNOWN)
436 return get_or_create_unknown_svalue (type);
437 /* Likewise for "poisoned". */
438 else if (const poisoned_svalue *poisoned_sval
439 = arg->dyn_cast_poisoned_svalue ())
440 return get_or_create_poisoned_svalue (poisoned_sval->get_poison_kind (),
441 type);
442
443 gcc_assert (arg->can_have_associated_state_p ());
444
445 switch (op)
446 {
447 default: break;
448 case VIEW_CONVERT_EXPR:
449 case NOP_EXPR:
450 {
451 if (!type)
452 return nullptr;
453
454 /* Handle redundant casts. */
455 if (arg->get_type ()
456 && useless_type_conversion_p (arg->get_type (), type))
457 return arg;
458
459 /* Fold "cast<TYPE> (cast <INNER_TYPE> (innermost_arg))
460 => "cast<TYPE> (innermost_arg)",
461 unless INNER_TYPE is narrower than TYPE. */
462 if (const svalue *innermost_arg = arg->maybe_undo_cast ())
463 {
464 if (tree inner_type = arg->get_type ())
465 if (TYPE_SIZE (type)
466 && TYPE_SIZE (inner_type)
467 && (fold_binary (LE_EXPR, boolean_type_node,
468 TYPE_SIZE (type), TYPE_SIZE (inner_type))
469 == boolean_true_node))
470 return maybe_fold_unaryop (type, op, innermost_arg);
471 }
472 /* Avoid creating symbolic regions for pointer casts by
473 simplifying (T*)(&REGION) to ((T*)&REGION). */
474 if (const region_svalue *region_sval = arg->dyn_cast_region_svalue ())
475 if (POINTER_TYPE_P (type)
476 && region_sval->get_type ()
477 && POINTER_TYPE_P (region_sval->get_type ()))
478 return get_ptr_svalue (type, region_sval->get_pointee ());
479
480 /* Casting all zeroes should give all zeroes. */
481 if (type
482 && arg->all_zeroes_p ()
483 && (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)))
484 return get_or_create_int_cst (type, 0);
485 }
486 break;
487 case TRUTH_NOT_EXPR:
488 {
489 /* Invert comparisons e.g. "!(x == y)" => "x != y". */
490 if (const binop_svalue *binop = arg->dyn_cast_binop_svalue ())
491 if (TREE_CODE_CLASS (binop->get_op ()) == tcc_comparison)
492 {
493 enum tree_code inv_op
494 = invert_tree_comparison (binop->get_op (),
495 HONOR_NANS (binop->get_type ()));
496 if (inv_op != ERROR_MARK)
497 return get_or_create_binop (binop->get_type (), inv_op,
498 binop->get_arg0 (),
499 binop->get_arg1 ());
500 }
501 }
502 break;
503 case NEGATE_EXPR:
504 {
505 /* -(-(VAL)) is VAL, for integer types. */
506 if (const unaryop_svalue *unaryop = arg->dyn_cast_unaryop_svalue ())
507 if (unaryop->get_op () == NEGATE_EXPR
508 && type == unaryop->get_type ()
509 && type
510 && INTEGRAL_TYPE_P (type))
511 return unaryop->get_arg ();
512 }
513 break;
514 }
515
516 /* Constants. */
517 if (tree cst = arg->maybe_get_constant ())
518 if (tree result = fold_unary (op, type, cst))
519 {
520 if (CONSTANT_CLASS_P (result))
521 return get_or_create_constant_svalue (result);
522
523 /* fold_unary can return casts of constants; try to handle them. */
524 if (op != NOP_EXPR
525 && type
526 && TREE_CODE (result) == NOP_EXPR
527 && CONSTANT_CLASS_P (TREE_OPERAND (result, 0)))
528 {
529 const svalue *inner_cst
530 = get_or_create_constant_svalue (TREE_OPERAND (result, 0));
531 return get_or_create_cast (type,
532 get_or_create_cast (TREE_TYPE (result),
533 inner_cst));
534 }
535 }
536
537 return NULL;
538 }
539
540 /* Return the svalue * for an unary operation OP on ARG with a result of
541 type TYPE, creating it if necessary. */
542
543 const svalue *
544 region_model_manager::get_or_create_unaryop (tree type, enum tree_code op,
545 const svalue *arg)
546 {
547 if (const svalue *folded = maybe_fold_unaryop (type, op, arg))
548 return folded;
549 unaryop_svalue::key_t key (type, op, arg);
550 if (unaryop_svalue **slot = m_unaryop_values_map.get (key))
551 return *slot;
552 unaryop_svalue *unaryop_sval
553 = new unaryop_svalue (alloc_symbol_id (), type, op, arg);
554 RETURN_UNKNOWN_IF_TOO_COMPLEX (unaryop_sval);
555 m_unaryop_values_map.put (key, unaryop_sval);
556 return unaryop_sval;
557 }
558
559 /* Get a tree code for a cast to DST_TYPE from SRC_TYPE.
560 Use NOP_EXPR if possible (e.g. to help fold_unary convert casts
561 of 0 to (T*) to simple pointer constants), but use FIX_TRUNC_EXPR
562 and VIEW_CONVERT_EXPR for cases that fold_unary would otherwise crash
563 on. */
564
565 static enum tree_code
566 get_code_for_cast (tree dst_type, tree src_type)
567 {
568 if (!dst_type)
569 return NOP_EXPR;
570 if (!src_type)
571 return NOP_EXPR;
572
573 if (SCALAR_FLOAT_TYPE_P (src_type))
574 {
575 if (TREE_CODE (dst_type) == INTEGER_TYPE)
576 return FIX_TRUNC_EXPR;
577 else
578 return VIEW_CONVERT_EXPR;
579 }
580
581 return NOP_EXPR;
582 }
583
584 /* Return the svalue * for a cast of ARG to type TYPE, creating it
585 if necessary. */
586
587 const svalue *
588 region_model_manager::get_or_create_cast (tree type, const svalue *arg)
589 {
590 /* No-op if the types are the same. */
591 if (type == arg->get_type ())
592 return arg;
593
594 /* Don't attempt to handle casts involving vector types for now. */
595 if (type)
596 if (VECTOR_TYPE_P (type)
597 || (arg->get_type ()
598 && VECTOR_TYPE_P (arg->get_type ())))
599 return get_or_create_unknown_svalue (type);
600
601 enum tree_code op = get_code_for_cast (type, arg->get_type ());
602 return get_or_create_unaryop (type, op, arg);
603 }
604
605 /* Subroutine of region_model_manager::maybe_fold_binop for handling
606 (TYPE)(COMPOUND_SVAL BIT_AND_EXPR CST) that may have been generated by
607 optimize_bit_field_compare, where CST is from ARG1.
608
609 Support masking out bits from a compound_svalue for comparing a bitfield
610 against a value, as generated by optimize_bit_field_compare for
611 BITFIELD == VALUE.
612
613 If COMPOUND_SVAL has a value for the appropriate bits, return it,
614 shifted accordingly.
615 Otherwise return NULL. */
616
617 const svalue *
618 region_model_manager::
619 maybe_undo_optimize_bit_field_compare (tree type,
620 const compound_svalue *compound_sval,
621 tree cst,
622 const svalue *arg1)
623 {
624 if (!type)
625 return nullptr;
626 if (!INTEGRAL_TYPE_P (type))
627 return NULL;
628
629 const binding_map &map = compound_sval->get_map ();
630 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (cst);
631 /* If "mask" is a contiguous range of set bits, see if the
632 compound_sval has a value for those bits. */
633 bit_range bits (0, 0);
634 if (!bit_range::from_mask (mask, &bits))
635 return NULL;
636
637 bit_range bound_bits (bits);
638 if (BYTES_BIG_ENDIAN)
639 bound_bits = bit_range (BITS_PER_UNIT - bits.get_next_bit_offset (),
640 bits.m_size_in_bits);
641 const concrete_binding *conc
642 = get_store_manager ()->get_concrete_binding (bound_bits);
643 const svalue *sval = map.get (conc);
644 if (!sval)
645 return NULL;
646
647 /* We have a value;
648 shift it by the correct number of bits. */
649 const svalue *lhs = get_or_create_cast (type, sval);
650 HOST_WIDE_INT bit_offset = bits.get_start_bit_offset ().to_shwi ();
651 const svalue *shift_sval = get_or_create_int_cst (type, bit_offset);
652 const svalue *shifted_sval = get_or_create_binop (type, LSHIFT_EXPR,
653 lhs, shift_sval);
654 /* Reapply the mask (needed for negative
655 signed bitfields). */
656 return get_or_create_binop (type, BIT_AND_EXPR,
657 shifted_sval, arg1);
658 }
659
660 /* Subroutine of region_model_manager::get_or_create_binop.
661 Attempt to fold the inputs and return a simpler svalue *.
662 Otherwise, return NULL. */
663
664 const svalue *
665 region_model_manager::maybe_fold_binop (tree type, enum tree_code op,
666 const svalue *arg0,
667 const svalue *arg1)
668 {
669 tree cst0 = arg0->maybe_get_constant ();
670 tree cst1 = arg1->maybe_get_constant ();
671 /* (CST OP CST). */
672 if (cst0 && cst1)
673 {
674 if (type)
675 {
676 if (tree result = fold_binary (op, type, cst0, cst1))
677 if (CONSTANT_CLASS_P (result))
678 return get_or_create_constant_svalue (result);
679 }
680 else
681 {
682 if (tree result = int_const_binop (op, cst0, cst1, -1))
683 return get_or_create_constant_svalue (NULL_TREE, result);
684 }
685 }
686
687 if ((type && FLOAT_TYPE_P (type))
688 || (arg0->get_type () && FLOAT_TYPE_P (arg0->get_type ()))
689 || (arg1->get_type () && FLOAT_TYPE_P (arg1->get_type ())))
690 return NULL;
691
692 switch (op)
693 {
694 default:
695 break;
696 case POINTER_PLUS_EXPR:
697 case PLUS_EXPR:
698 /* (VAL + 0) -> VAL. */
699 if (cst1 && zerop (cst1))
700 return get_or_create_cast (type, arg0);
701 /* X + (-X) -> 0. */
702 if (const unaryop_svalue *unary_op = arg1->dyn_cast_unaryop_svalue ())
703 if (unary_op->get_op () == NEGATE_EXPR
704 && unary_op->get_arg () == arg0)
705 return get_or_create_int_cst (type, 0);
706 break;
707 case MINUS_EXPR:
708 /* (VAL - 0) -> VAL. */
709 if (cst1 && zerop (cst1))
710 return get_or_create_cast (type, arg0);
711 /* (0 - VAL) -> -VAL. */
712 if (cst0 && zerop (cst0))
713 return get_or_create_unaryop (type, NEGATE_EXPR, arg1);
714 /* (X + Y) - X -> Y. */
715 if (const binop_svalue *binop = arg0->dyn_cast_binop_svalue ())
716 if (binop->get_op () == PLUS_EXPR)
717 if (binop->get_arg0 () == arg1)
718 return get_or_create_cast (type, binop->get_arg1 ());
719 break;
720 case MULT_EXPR:
721 /* (VAL * 0). */
722 if (cst1
723 && zerop (cst1)
724 && (type == NULL_TREE || INTEGRAL_TYPE_P (type)))
725 return get_or_create_int_cst (type, 0);
726 /* (VAL * 1) -> VAL. */
727 if (cst1 && integer_onep (cst1))
728 return get_or_create_cast (type, arg0);
729 break;
730 case BIT_AND_EXPR:
731 if (cst1)
732 {
733 if (zerop (cst1)
734 && (type == NULL_TREE || INTEGRAL_TYPE_P (type)))
735 /* "(ARG0 & 0)" -> "0". */
736 return get_or_create_int_cst (type, 0);
737
738 if (const compound_svalue *compound_sval
739 = arg0->dyn_cast_compound_svalue ())
740 if (const svalue *sval
741 = maybe_undo_optimize_bit_field_compare (type,
742 compound_sval,
743 cst1, arg1))
744 return sval;
745 }
746 if (arg0->get_type () == boolean_type_node
747 && arg1->get_type () == boolean_type_node)
748 {
749 /* If the LHS are both _Bool, then... */
750 /* ..."(1 & x) -> x". */
751 if (cst0 && !zerop (cst0))
752 return get_or_create_cast (type, arg1);
753 /* ..."(x & 1) -> x". */
754 if (cst1 && !zerop (cst1))
755 return get_or_create_cast (type, arg0);
756 /* ..."(0 & x) -> 0". */
757 if (cst0 && zerop (cst0))
758 return get_or_create_int_cst (type, 0);
759 /* ..."(x & 0) -> 0". */
760 if (cst1 && zerop (cst1))
761 return get_or_create_int_cst (type, 0);
762 }
763 break;
764 case BIT_IOR_EXPR:
765 if (arg0->get_type () == boolean_type_node
766 && arg1->get_type () == boolean_type_node)
767 {
768 /* If the LHS are both _Bool, then... */
769 /* ..."(1 | x) -> 1". */
770 if (cst0 && !zerop (cst0))
771 return get_or_create_int_cst (type, 1);
772 /* ..."(x | 1) -> 1". */
773 if (cst1 && !zerop (cst1))
774 return get_or_create_int_cst (type, 1);
775 /* ..."(0 | x) -> x". */
776 if (cst0 && zerop (cst0))
777 return get_or_create_cast (type, arg1);
778 /* ..."(x | 0) -> x". */
779 if (cst1 && zerop (cst1))
780 return get_or_create_cast (type, arg0);
781 }
782 break;
783 case TRUTH_ANDIF_EXPR:
784 case TRUTH_AND_EXPR:
785 if (cst1)
786 {
787 if (zerop (cst1) && INTEGRAL_TYPE_P (type))
788 /* "(ARG0 && 0)" -> "0". */
789 return get_or_create_constant_svalue (build_int_cst (type, 0));
790 else
791 /* "(ARG0 && nonzero-cst)" -> "ARG0". */
792 return get_or_create_cast (type, arg0);
793 }
794 break;
795 case TRUTH_ORIF_EXPR:
796 case TRUTH_OR_EXPR:
797 if (cst1)
798 {
799 if (zerop (cst1))
800 /* "(ARG0 || 0)" -> "ARG0". */
801 return get_or_create_cast (type, arg0);
802 else
803 /* "(ARG0 && nonzero-cst)" -> "nonzero-cst". */
804 return get_or_create_cast (type, arg1);
805 }
806 break;
807 }
808
809 /* For associative ops, fold "(X op CST_A) op CST_B)" to
810 "X op (CST_A op CST_B)". */
811 if (cst1 && associative_tree_code (op))
812 if (const binop_svalue *binop = arg0->dyn_cast_binop_svalue ())
813 if (binop->get_op () == op
814 && binop->get_arg1 ()->maybe_get_constant ())
815 return get_or_create_binop
816 (type, op, binop->get_arg0 (),
817 get_or_create_binop (type, op,
818 binop->get_arg1 (), arg1));
819
820 /* associative_tree_code is false for POINTER_PLUS_EXPR, but we
821 can fold:
822 "(PTR ptr+ CST_A) ptr+ CST_B)" to "PTR ptr+ (CST_A ptr+ CST_B)"
823 e.g. in data-model-1.c: test_4c. */
824 if (cst1 && op == POINTER_PLUS_EXPR)
825 if (const binop_svalue *binop = arg0->dyn_cast_binop_svalue ())
826 if (binop->get_op () == POINTER_PLUS_EXPR)
827 if (binop->get_arg1 ()->maybe_get_constant ())
828 return get_or_create_binop
829 (type, op, binop->get_arg0 (),
830 get_or_create_binop (size_type_node, op,
831 binop->get_arg1 (), arg1));
832
833 /* Distribute multiplication by a constant through addition/subtraction:
834 (X + Y) * CST => (X * CST) + (Y * CST). */
835 if (cst1 && op == MULT_EXPR)
836 if (const binop_svalue *binop = arg0->dyn_cast_binop_svalue ())
837 if (binop->get_op () == PLUS_EXPR
838 || binop->get_op () == MINUS_EXPR)
839 {
840 return get_or_create_binop
841 (type, binop->get_op (),
842 get_or_create_binop (type, op,
843 binop->get_arg0 (), arg1),
844 get_or_create_binop (type, op,
845 binop->get_arg1 (), arg1));
846 }
847
848
849 /* Typeless operations, assumed to be effectively arbitrary sized
850 integers following normal arithmetic rules. */
851 if (!type)
852 switch (op)
853 {
854 default:
855 break;
856 case MINUS_EXPR:
857 {
858 /* (X - X) -> 0. */
859 if (arg0 == arg1)
860 return get_or_create_int_cst (type, 0);
861
862 /* (X + A) - (A + B) -> (A - B). */
863 if (const binop_svalue *binop0 = arg0->dyn_cast_binop_svalue ())
864 if (const binop_svalue *binop1 = arg1->dyn_cast_binop_svalue ())
865 if (binop0->get_op () == PLUS_EXPR
866 && binop1->get_op () == PLUS_EXPR
867 && binop0->get_arg0 () == binop1->get_arg0 ())
868 return get_or_create_binop (NULL_TREE, op,
869 binop0->get_arg1 (),
870 binop1->get_arg1 ());
871 }
872 break;
873
874 case EXACT_DIV_EXPR:
875 {
876 if (const unaryop_svalue *unaryop0 = arg0->dyn_cast_unaryop_svalue ())
877 {
878 if (unaryop0->get_op () == NOP_EXPR)
879 if (const svalue *sval = maybe_fold_binop (NULL_TREE, op,
880 unaryop0->get_arg (),
881 arg1))
882 return sval;
883 }
884 if (const binop_svalue *binop0 = arg0->dyn_cast_binop_svalue ())
885 {
886 switch (binop0->get_op ())
887 {
888 default:
889 break;
890
891 case PLUS_EXPR:
892 case MINUS_EXPR:
893 /* (A op B) / C -> (A / C) op (B / C). */
894 {
895 if (const svalue *op_on_a
896 = maybe_fold_binop (NULL_TREE, op,
897 binop0->get_arg0 (), arg1))
898 if (const svalue *op_on_b
899 = maybe_fold_binop (NULL_TREE, op,
900 binop0->get_arg1 (), arg1))
901 return get_or_create_binop (NULL_TREE,
902 binop0->get_op (),
903 op_on_a, op_on_b);
904 }
905 break;
906
907 case MULT_EXPR:
908 /* (A * B) / C -> A * (B / C) if C is a divisor of B.
909 In particular, this should also handle the case
910 (A * B) / B -> A. */
911 if (const svalue *b_div_c
912 = maybe_fold_binop (NULL_TREE, op,
913 binop0->get_arg1 (), arg1))
914 return get_or_create_binop (NULL_TREE, binop0->get_op (),
915 binop0->get_arg0 (), b_div_c);
916 }
917 }
918 }
919 break;
920 }
921
922 /* etc. */
923
924 return NULL;
925 }
926
927 /* Return the svalue * for an binary operation OP on ARG0 and ARG1
928 with a result of type TYPE, creating it if necessary. */
929
930 const svalue *
931 region_model_manager::get_or_create_binop (tree type, enum tree_code op,
932 const svalue *arg0,
933 const svalue *arg1)
934 {
935 /* For commutative ops, put any constant on the RHS. */
936 if (arg0->maybe_get_constant () && commutative_tree_code (op))
937 std::swap (arg0, arg1);
938
939 if (const svalue *folded = maybe_fold_binop (type, op, arg0, arg1))
940 return folded;
941
942 /* Ops on "unknown"/"poisoned" are unknown (unless we were able to fold
943 it via an identity in maybe_fold_binop). */
944 if (!arg0->can_have_associated_state_p ()
945 || !arg1->can_have_associated_state_p ())
946 return get_or_create_unknown_svalue (type);
947
948 binop_svalue::key_t key (type, op, arg0, arg1);
949 if (binop_svalue **slot = m_binop_values_map.get (key))
950 return *slot;
951 binop_svalue *binop_sval
952 = new binop_svalue (alloc_symbol_id (), type, op, arg0, arg1);
953 RETURN_UNKNOWN_IF_TOO_COMPLEX (binop_sval);
954 m_binop_values_map.put (key, binop_sval);
955 return binop_sval;
956 }
957
958 /* Subroutine of region_model_manager::get_or_create_sub_svalue.
959 Return a folded svalue, or NULL. */
960
961 const svalue *
962 region_model_manager::maybe_fold_sub_svalue (tree type,
963 const svalue *parent_svalue,
964 const region *subregion)
965 {
966 /* Subvalues of "unknown"/"poisoned" are unknown. */
967 if (!parent_svalue->can_have_associated_state_p ())
968 return get_or_create_unknown_svalue (type);
969
970 /* If we have a subregion of a zero-fill, it's zero. */
971 if (const unaryop_svalue *unary
972 = parent_svalue->dyn_cast_unaryop_svalue ())
973 {
974 if (unary->get_op () == NOP_EXPR
975 || unary->get_op () == VIEW_CONVERT_EXPR)
976 if (tree cst = unary->get_arg ()->maybe_get_constant ())
977 if (zerop (cst) && type)
978 {
979 const svalue *cst_sval
980 = get_or_create_constant_svalue (cst);
981 return get_or_create_cast (type, cst_sval);
982 }
983 }
984
985 /* Handle getting individual chars from a STRING_CST or RAW_DATA_CST. */
986 if (tree cst = parent_svalue->maybe_get_constant ())
987 if (TREE_CODE (cst) == STRING_CST
988 || TREE_CODE (cst) == RAW_DATA_CST)
989 {
990 /* If we have a concrete 1-byte access within the parent region... */
991 byte_range subregion_bytes (0, 0);
992 if (subregion->get_relative_concrete_byte_range (&subregion_bytes)
993 && subregion_bytes.m_size_in_bytes == 1
994 && type)
995 {
996 /* ...then attempt to get that char from the constant. */
997 HOST_WIDE_INT hwi_start_byte
998 = subregion_bytes.m_start_byte_offset.to_shwi ();
999 tree cst_idx
1000 = build_int_cst_type (size_type_node, hwi_start_byte);
1001 if (const svalue *char_sval
1002 = maybe_get_char_from_cst (cst, cst_idx))
1003 return get_or_create_cast (type, char_sval);
1004 }
1005 }
1006
1007 if (const initial_svalue *init_sval
1008 = parent_svalue->dyn_cast_initial_svalue ())
1009 {
1010 /* SUB(INIT(r)).FIELD -> INIT(r.FIELD)
1011 i.e.
1012 Subvalue(InitialValue(R1), FieldRegion(R2, F))
1013 -> InitialValue(FieldRegion(R1, F)). */
1014 if (const field_region *field_reg = subregion->dyn_cast_field_region ())
1015 {
1016 const region *field_reg_new
1017 = get_field_region (init_sval->get_region (),
1018 field_reg->get_field ());
1019 return get_or_create_initial_value (field_reg_new);
1020 }
1021 /* SUB(INIT(r)[ELEMENT] -> INIT(e[ELEMENT])
1022 i.e.
1023 Subvalue(InitialValue(R1), ElementRegion(R2, IDX))
1024 -> InitialValue(ElementRegion(R1, IDX)). */
1025 if (const element_region *element_reg = subregion->dyn_cast_element_region ())
1026 {
1027 const region *element_reg_new
1028 = get_element_region (init_sval->get_region (),
1029 element_reg->get_type (),
1030 element_reg->get_index ());
1031 return get_or_create_initial_value (element_reg_new);
1032 }
1033 }
1034
1035 if (const repeated_svalue *repeated_sval
1036 = parent_svalue->dyn_cast_repeated_svalue ())
1037 if (type)
1038 return get_or_create_cast (type, repeated_sval->get_inner_svalue ());
1039
1040 return NULL;
1041 }
1042
1043 /* Return the svalue * for extracting a subvalue of type TYPE from
1044 PARENT_SVALUE based on SUBREGION, creating it if necessary. */
1045
1046 const svalue *
1047 region_model_manager::get_or_create_sub_svalue (tree type,
1048 const svalue *parent_svalue,
1049 const region *subregion)
1050 {
1051 if (const svalue *folded
1052 = maybe_fold_sub_svalue (type, parent_svalue, subregion))
1053 return folded;
1054
1055 sub_svalue::key_t key (type, parent_svalue, subregion);
1056 if (sub_svalue **slot = m_sub_values_map.get (key))
1057 return *slot;
1058 sub_svalue *sub_sval
1059 = new sub_svalue (alloc_symbol_id (), type, parent_svalue, subregion);
1060 RETURN_UNKNOWN_IF_TOO_COMPLEX (sub_sval);
1061 m_sub_values_map.put (key, sub_sval);
1062 return sub_sval;
1063 }
1064
1065 /* Subroutine of region_model_manager::get_or_create_repeated_svalue.
1066 Return a folded svalue, or NULL. */
1067
1068 const svalue *
1069 region_model_manager::maybe_fold_repeated_svalue (tree type,
1070 const svalue *outer_size,
1071 const svalue *inner_svalue)
1072 {
1073 /* Repeated "unknown"/"poisoned" is unknown. */
1074 if (!outer_size->can_have_associated_state_p ()
1075 || !inner_svalue->can_have_associated_state_p ())
1076 return get_or_create_unknown_svalue (type);
1077
1078 /* If INNER_SVALUE is the same size as OUTER_SIZE,
1079 turn into simply a cast. */
1080 if (tree cst_outer_num_bytes = outer_size->maybe_get_constant ())
1081 {
1082 HOST_WIDE_INT num_bytes_inner_svalue
1083 = int_size_in_bytes (inner_svalue->get_type ());
1084 if (num_bytes_inner_svalue != -1)
1085 if (num_bytes_inner_svalue
1086 == (HOST_WIDE_INT)tree_to_uhwi (cst_outer_num_bytes))
1087 {
1088 if (type)
1089 return get_or_create_cast (type, inner_svalue);
1090 else
1091 return inner_svalue;
1092 }
1093 }
1094
1095 /* Handle zero-fill of a specific type. */
1096 if (tree cst = inner_svalue->maybe_get_constant ())
1097 if (zerop (cst) && type)
1098 return get_or_create_cast (type, inner_svalue);
1099
1100 return NULL;
1101 }
1102
1103 /* Return the svalue * of type TYPE in which INNER_SVALUE is repeated
1104 enough times to be of size OUTER_SIZE, creating it if necessary.
1105 e.g. for filling buffers with a constant value. */
1106
1107 const svalue *
1108 region_model_manager::get_or_create_repeated_svalue (tree type,
1109 const svalue *outer_size,
1110 const svalue *inner_svalue)
1111 {
1112 if (const svalue *folded
1113 = maybe_fold_repeated_svalue (type, outer_size, inner_svalue))
1114 return folded;
1115
1116 repeated_svalue::key_t key (type, outer_size, inner_svalue);
1117 if (repeated_svalue **slot = m_repeated_values_map.get (key))
1118 return *slot;
1119 repeated_svalue *repeated_sval
1120 = new repeated_svalue (alloc_symbol_id (), type, outer_size, inner_svalue);
1121 RETURN_UNKNOWN_IF_TOO_COMPLEX (repeated_sval);
1122 m_repeated_values_map.put (key, repeated_sval);
1123 return repeated_sval;
1124 }
1125
1126 /* Attempt to get the bit_range for FIELD within a RECORD_TYPE.
1127 Return true and write the result to OUT if successful.
1128 Return false otherwise. */
1129
1130 static bool
1131 get_bit_range_for_field (tree field, bit_range *out)
1132 {
1133 bit_size_t bit_size;
1134 if (!int_size_in_bits (TREE_TYPE (field), &bit_size))
1135 return false;
1136 int field_bit_offset = int_bit_position (field);
1137 *out = bit_range (field_bit_offset, bit_size);
1138 return true;
1139 }
1140
1141 /* Attempt to get the byte_range for FIELD within a RECORD_TYPE.
1142 Return true and write the result to OUT if successful.
1143 Return false otherwise. */
1144
1145 static bool
1146 get_byte_range_for_field (tree field, byte_range *out)
1147 {
1148 bit_range field_bits (0, 0);
1149 if (!get_bit_range_for_field (field, &field_bits))
1150 return false;
1151 return field_bits.as_byte_range (out);
1152 }
1153
1154 /* Attempt to determine if there is a specific field within RECORD_TYPE
1155 at BYTES. If so, return it, and write the location of BYTES relative
1156 to the field to *OUT_RANGE_WITHIN_FIELD.
1157 Otherwise, return NULL_TREE.
1158 For example, given:
1159 struct foo { uint32 a; uint32; b};
1160 and
1161 bytes = {bytes 6-7} (of foo)
1162 we have bytes 3-4 of field b. */
1163
1164 static tree
1165 get_field_at_byte_range (tree record_type, const byte_range &bytes,
1166 byte_range *out_range_within_field)
1167 {
1168 bit_offset_t bit_offset = bytes.m_start_byte_offset * BITS_PER_UNIT;
1169
1170 tree field = get_field_at_bit_offset (record_type, bit_offset);
1171 if (!field)
1172 return NULL_TREE;
1173
1174 byte_range field_bytes (0,0);
1175 if (!get_byte_range_for_field (field, &field_bytes))
1176 return NULL_TREE;
1177
1178 /* Is BYTES fully within field_bytes? */
1179 byte_range bytes_within_field (0,0);
1180 if (!field_bytes.contains_p (bytes, &bytes_within_field))
1181 return NULL_TREE;
1182
1183 *out_range_within_field = bytes_within_field;
1184 return field;
1185 }
1186
1187 /* Subroutine of region_model_manager::get_or_create_bits_within.
1188 Return a folded svalue, or NULL. */
1189
1190 const svalue *
1191 region_model_manager::maybe_fold_bits_within_svalue (tree type,
1192 const bit_range &bits,
1193 const svalue *inner_svalue)
1194 {
1195 tree inner_type = inner_svalue->get_type ();
1196 /* Fold:
1197 BITS_WITHIN ((0, sizeof (VAL), VAL))
1198 to:
1199 CAST(TYPE, VAL). */
1200 if (bits.m_start_bit_offset == 0 && inner_type)
1201 {
1202 bit_size_t inner_type_size;
1203 if (int_size_in_bits (inner_type, &inner_type_size))
1204 if (inner_type_size == bits.m_size_in_bits)
1205 {
1206 if (type)
1207 return get_or_create_cast (type, inner_svalue);
1208 else
1209 return inner_svalue;
1210 }
1211 }
1212
1213 /* Kind-specific folding. */
1214 if (const svalue *sval
1215 = inner_svalue->maybe_fold_bits_within (type, bits, this))
1216 return sval;
1217
1218 byte_range bytes (0,0);
1219 if (bits.as_byte_range (&bytes) && inner_type)
1220 switch (TREE_CODE (inner_type))
1221 {
1222 default:
1223 break;
1224 case ARRAY_TYPE:
1225 {
1226 /* Fold:
1227 BITS_WITHIN (range, KIND(REG))
1228 to:
1229 BITS_WITHIN (range - offsetof(ELEMENT), KIND(REG.ELEMENT))
1230 if range1 is a byte-range fully within one ELEMENT. */
1231 tree element_type = TREE_TYPE (inner_type);
1232 HOST_WIDE_INT element_byte_size
1233 = int_size_in_bytes (element_type);
1234 if (element_byte_size > 0)
1235 {
1236 HOST_WIDE_INT start_idx
1237 = (bytes.get_start_byte_offset ().to_shwi ()
1238 / element_byte_size);
1239 HOST_WIDE_INT last_idx
1240 = (bytes.get_last_byte_offset ().to_shwi ()
1241 / element_byte_size);
1242 if (start_idx == last_idx)
1243 {
1244 if (const initial_svalue *initial_sval
1245 = inner_svalue->dyn_cast_initial_svalue ())
1246 {
1247 bit_offset_t start_of_element
1248 = start_idx * element_byte_size * BITS_PER_UNIT;
1249 bit_range bits_within_element
1250 (bits.m_start_bit_offset - start_of_element,
1251 bits.m_size_in_bits);
1252 const svalue *idx_sval
1253 = get_or_create_int_cst (integer_type_node, start_idx);
1254 const region *element_reg =
1255 get_element_region (initial_sval->get_region (),
1256 element_type, idx_sval);
1257 const svalue *element_reg_sval
1258 = get_or_create_initial_value (element_reg);
1259 return get_or_create_bits_within (type,
1260 bits_within_element,
1261 element_reg_sval);
1262 }
1263 }
1264 }
1265 }
1266 break;
1267 case RECORD_TYPE:
1268 {
1269 /* Fold:
1270 BYTES_WITHIN (range, KIND(REG))
1271 to:
1272 BYTES_WITHIN (range - offsetof(FIELD), KIND(REG.FIELD))
1273 if range1 is fully within FIELD. */
1274 byte_range bytes_within_field (0, 0);
1275 if (tree field = get_field_at_byte_range (inner_type, bytes,
1276 &bytes_within_field))
1277 {
1278 if (const initial_svalue *initial_sval
1279 = inner_svalue->dyn_cast_initial_svalue ())
1280 {
1281 const region *field_reg =
1282 get_field_region (initial_sval->get_region (), field);
1283 const svalue *initial_reg_sval
1284 = get_or_create_initial_value (field_reg);
1285 return get_or_create_bits_within
1286 (type,
1287 bytes_within_field.as_bit_range (),
1288 initial_reg_sval);
1289 }
1290 }
1291 }
1292 break;
1293 }
1294 return NULL;
1295 }
1296
1297 /* Return the svalue * of type TYPE for extracting BITS from INNER_SVALUE,
1298 creating it if necessary. */
1299
1300 const svalue *
1301 region_model_manager::get_or_create_bits_within (tree type,
1302 const bit_range &bits,
1303 const svalue *inner_svalue)
1304 {
1305 if (const svalue *folded
1306 = maybe_fold_bits_within_svalue (type, bits, inner_svalue))
1307 return folded;
1308
1309 bits_within_svalue::key_t key (type, bits, inner_svalue);
1310 if (bits_within_svalue **slot = m_bits_within_values_map.get (key))
1311 return *slot;
1312 bits_within_svalue *bits_within_sval
1313 = new bits_within_svalue (alloc_symbol_id (), type, bits, inner_svalue);
1314 RETURN_UNKNOWN_IF_TOO_COMPLEX (bits_within_sval);
1315 m_bits_within_values_map.put (key, bits_within_sval);
1316 return bits_within_sval;
1317 }
1318
1319 /* Return the svalue * that decorates ARG as being unmergeable,
1320 creating it if necessary. */
1321
1322 const svalue *
1323 region_model_manager::get_or_create_unmergeable (const svalue *arg)
1324 {
1325 if (arg->get_kind () == SK_UNMERGEABLE)
1326 return arg;
1327
1328 if (unmergeable_svalue **slot = m_unmergeable_values_map.get (arg))
1329 return *slot;
1330 unmergeable_svalue *unmergeable_sval
1331 = new unmergeable_svalue (alloc_symbol_id (), arg);
1332 RETURN_UNKNOWN_IF_TOO_COMPLEX (unmergeable_sval);
1333 m_unmergeable_values_map.put (arg, unmergeable_sval);
1334 return unmergeable_sval;
1335 }
1336
1337 /* Return the svalue * of type TYPE for the merger of value BASE_SVAL
1338 and ITER_SVAL at POINT, creating it if necessary. */
1339
1340 const svalue *
1341 region_model_manager::
1342 get_or_create_widening_svalue (tree type,
1343 const function_point &point,
1344 const svalue *base_sval,
1345 const svalue *iter_sval)
1346 {
1347 gcc_assert (base_sval->get_kind () != SK_WIDENING);
1348 gcc_assert (iter_sval->get_kind () != SK_WIDENING);
1349 widening_svalue::key_t key (type, point, base_sval, iter_sval);
1350 if (widening_svalue **slot = m_widening_values_map.get (key))
1351 return *slot;
1352 widening_svalue *widening_sval
1353 = new widening_svalue (alloc_symbol_id (), type, point, base_sval,
1354 iter_sval);
1355 RETURN_UNKNOWN_IF_TOO_COMPLEX (widening_sval);
1356 m_widening_values_map.put (key, widening_sval);
1357 return widening_sval;
1358 }
1359
1360 /* Return the svalue * of type TYPE for the compound values in MAP,
1361 creating it if necessary. */
1362
1363 const svalue *
1364 region_model_manager::get_or_create_compound_svalue (tree type,
1365 const binding_map &map)
1366 {
1367 compound_svalue::key_t tmp_key (type, &map);
1368 if (compound_svalue **slot = m_compound_values_map.get (tmp_key))
1369 return *slot;
1370 compound_svalue *compound_sval
1371 = new compound_svalue (alloc_symbol_id (), type, map);
1372 RETURN_UNKNOWN_IF_TOO_COMPLEX (compound_sval);
1373 /* Use make_key rather than reusing the key, so that we use a
1374 ptr to compound_sval's binding_map, rather than the MAP param. */
1375 m_compound_values_map.put (compound_sval->make_key (), compound_sval);
1376 return compound_sval;
1377 }
1378
1379 /* class conjured_purge. */
1380
1381 /* Purge state relating to SVAL. */
1382
1383 void
1384 conjured_purge::purge (const conjured_svalue *sval) const
1385 {
1386 m_model->purge_state_involving (sval, m_ctxt);
1387 }
1388
1389 /* Return the svalue * of type TYPE for the value conjured for ID_REG
1390 at STMT (using IDX for any further disambiguation),
1391 creating it if necessary.
1392 Use P to purge existing state from the svalue, for the case where a
1393 conjured_svalue would be reused along an execution path. */
1394
1395 const svalue *
1396 region_model_manager::get_or_create_conjured_svalue (tree type,
1397 const gimple *stmt,
1398 const region *id_reg,
1399 const conjured_purge &p,
1400 unsigned idx)
1401 {
1402 conjured_svalue::key_t key (type, stmt, id_reg, idx);
1403 if (conjured_svalue **slot = m_conjured_values_map.get (key))
1404 {
1405 const conjured_svalue *sval = *slot;
1406 /* We're reusing an existing conjured_svalue, perhaps from a different
1407 state within this analysis, or perhaps from an earlier state on this
1408 execution path. For the latter, purge any state involving the "new"
1409 svalue from the current program_state. */
1410 p.purge (sval);
1411 return sval;
1412 }
1413 conjured_svalue *conjured_sval
1414 = new conjured_svalue (alloc_symbol_id (), type, stmt, id_reg, idx);
1415 RETURN_UNKNOWN_IF_TOO_COMPLEX (conjured_sval);
1416 m_conjured_values_map.put (key, conjured_sval);
1417 return conjured_sval;
1418 }
1419
1420 /* Subroutine of region_model_manager::get_or_create_asm_output_svalue.
1421 Return a folded svalue, or NULL. */
1422
1423 const svalue *
1424 region_model_manager::
1425 maybe_fold_asm_output_svalue (tree type,
1426 const vec<const svalue *> &inputs)
1427 {
1428 /* Unknown inputs should lead to unknown results. */
1429 for (const auto &iter : inputs)
1430 if (iter->get_kind () == SK_UNKNOWN)
1431 return get_or_create_unknown_svalue (type);
1432
1433 return NULL;
1434 }
1435
1436 /* Return the svalue * of type TYPE for OUTPUT_IDX of the deterministic
1437 asm stmt ASM_STMT, given INPUTS as inputs. */
1438
1439 const svalue *
1440 region_model_manager::
1441 get_or_create_asm_output_svalue (tree type,
1442 const gasm *asm_stmt,
1443 unsigned output_idx,
1444 const vec<const svalue *> &inputs)
1445 {
1446 gcc_assert (inputs.length () <= asm_output_svalue::MAX_INPUTS);
1447
1448 if (const svalue *folded
1449 = maybe_fold_asm_output_svalue (type, inputs))
1450 return folded;
1451
1452 const char *asm_string = gimple_asm_string (asm_stmt);
1453 const unsigned noutputs = gimple_asm_noutputs (asm_stmt);
1454
1455 asm_output_svalue::key_t key (type, asm_string, output_idx, inputs);
1456 if (asm_output_svalue **slot = m_asm_output_values_map.get (key))
1457 return *slot;
1458 asm_output_svalue *asm_output_sval
1459 = new asm_output_svalue (alloc_symbol_id (), type, asm_string, output_idx,
1460 noutputs, inputs);
1461 RETURN_UNKNOWN_IF_TOO_COMPLEX (asm_output_sval);
1462 m_asm_output_values_map.put (key, asm_output_sval);
1463 return asm_output_sval;
1464 }
1465
1466 /* Return the svalue * of type TYPE for OUTPUT_IDX of a deterministic
1467 asm stmt with string ASM_STRING with NUM_OUTPUTS outputs, given
1468 INPUTS as inputs. */
1469
1470 const svalue *
1471 region_model_manager::
1472 get_or_create_asm_output_svalue (tree type,
1473 const char *asm_string,
1474 unsigned output_idx,
1475 unsigned num_outputs,
1476 const vec<const svalue *> &inputs)
1477 {
1478 gcc_assert (inputs.length () <= asm_output_svalue::MAX_INPUTS);
1479
1480 if (const svalue *folded
1481 = maybe_fold_asm_output_svalue (type, inputs))
1482 return folded;
1483
1484 asm_output_svalue::key_t key (type, asm_string, output_idx, inputs);
1485 if (asm_output_svalue **slot = m_asm_output_values_map.get (key))
1486 return *slot;
1487 asm_output_svalue *asm_output_sval
1488 = new asm_output_svalue (alloc_symbol_id (), type, asm_string, output_idx,
1489 num_outputs, inputs);
1490 RETURN_UNKNOWN_IF_TOO_COMPLEX (asm_output_sval);
1491 m_asm_output_values_map.put (key, asm_output_sval);
1492 return asm_output_sval;
1493 }
1494
1495 /* Return the svalue * of type TYPE for the result of a call to FNDECL
1496 with __attribute__((const)), given INPUTS as inputs. */
1497
1498 const svalue *
1499 region_model_manager::
1500 get_or_create_const_fn_result_svalue (tree type,
1501 tree fndecl,
1502 const vec<const svalue *> &inputs)
1503 {
1504 gcc_assert (fndecl);
1505 gcc_assert (DECL_P (fndecl));
1506 gcc_assert (TREE_READONLY (fndecl));
1507 gcc_assert (inputs.length () <= const_fn_result_svalue::MAX_INPUTS);
1508
1509 const_fn_result_svalue::key_t key (type, fndecl, inputs);
1510 if (const_fn_result_svalue **slot = m_const_fn_result_values_map.get (key))
1511 return *slot;
1512 const_fn_result_svalue *const_fn_result_sval
1513 = new const_fn_result_svalue (alloc_symbol_id (), type, fndecl, inputs);
1514 RETURN_UNKNOWN_IF_TOO_COMPLEX (const_fn_result_sval);
1515 m_const_fn_result_values_map.put (key, const_fn_result_sval);
1516 return const_fn_result_sval;
1517 }
1518
1519 /* Given DATA_CST (a STRING_CST or RAW_DATA_CST) and BYTE_OFFSET_CST a constant,
1520 attempt to get the character at that offset, returning either
1521 the svalue for the character constant, or NULL if unsuccessful. */
1522
1523 const svalue *
1524 region_model_manager::maybe_get_char_from_cst (tree data_cst,
1525 tree byte_offset_cst)
1526 {
1527 switch (TREE_CODE (data_cst))
1528 {
1529 default: gcc_unreachable ();
1530 case STRING_CST:
1531 return maybe_get_char_from_string_cst (data_cst, byte_offset_cst);
1532 case RAW_DATA_CST:
1533 return maybe_get_char_from_raw_data_cst (data_cst, byte_offset_cst);
1534 }
1535 }
1536
1537 /* Get a tree for the size of STRING_CST, or NULL_TREE.
1538 Note that this may be larger than TREE_STRING_LENGTH (implying
1539 a run of trailing zero bytes from TREE_STRING_LENGTH up to this
1540 higher limit). */
1541
1542 tree
1543 get_string_cst_size (const_tree string_cst)
1544 {
1545 gcc_assert (TREE_CODE (string_cst) == STRING_CST);
1546 gcc_assert (TREE_CODE (TREE_TYPE (string_cst)) == ARRAY_TYPE);
1547
1548 return TYPE_SIZE_UNIT (TREE_TYPE (string_cst));
1549 }
1550
1551 /* Given STRING_CST, a STRING_CST and BYTE_OFFSET_CST a constant,
1552 attempt to get the character at that offset, returning either
1553 the svalue for the character constant, or NULL if unsuccessful. */
1554
1555 const svalue *
1556 region_model_manager::maybe_get_char_from_string_cst (tree string_cst,
1557 tree byte_offset_cst)
1558 {
1559 gcc_assert (TREE_CODE (string_cst) == STRING_CST);
1560
1561 /* Adapted from fold_read_from_constant_string. */
1562 scalar_int_mode char_mode;
1563 if (TREE_CODE (byte_offset_cst) == INTEGER_CST
1564 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string_cst))),
1565 &char_mode)
1566 && GET_MODE_SIZE (char_mode) == 1)
1567 {
1568 /* If we're beyond the string_cst, the read is unsuccessful. */
1569 if (compare_constants (byte_offset_cst,
1570 GE_EXPR,
1571 get_string_cst_size (string_cst)).is_true ())
1572 return NULL;
1573
1574 int char_val;
1575 if (compare_tree_int (byte_offset_cst,
1576 TREE_STRING_LENGTH (string_cst)) < 0)
1577 /* We're within the area defined by TREE_STRING_POINTER. */
1578 char_val = (TREE_STRING_POINTER (string_cst)
1579 [TREE_INT_CST_LOW (byte_offset_cst)]);
1580 else
1581 /* We're in the padding area of trailing zeroes. */
1582 char_val = 0;
1583 tree char_cst
1584 = build_int_cst_type (TREE_TYPE (TREE_TYPE (string_cst)), char_val);
1585 return get_or_create_constant_svalue (char_cst);
1586 }
1587 return NULL;
1588 }
1589
1590 /* Given RAW_DATA_CST, a RAW_DATA_CST and BYTE_OFFSET_CST a constant,
1591 attempt to get the character at that offset, returning either
1592 the svalue for the character constant, or NULL if unsuccessful. */
1593
1594 const svalue *
1595 region_model_manager::maybe_get_char_from_raw_data_cst (tree raw_data_cst,
1596 tree byte_offset_cst)
1597 {
1598 gcc_assert (TREE_CODE (raw_data_cst) == RAW_DATA_CST);
1599 gcc_assert (TREE_CODE (byte_offset_cst) == INTEGER_CST);
1600
1601 offset_int o = (wi::to_offset (byte_offset_cst));
1602 if (o >= 0 && o < RAW_DATA_LENGTH (raw_data_cst))
1603 return get_or_create_int_cst
1604 (TREE_TYPE (raw_data_cst),
1605 RAW_DATA_UCHAR_ELT (raw_data_cst, o.to_uhwi ()));
1606 return nullptr;
1607 }
1608
1609 /* region consolidation. */
1610
1611 /* Return the region for FNDECL, creating it if necessary. */
1612
1613 const function_region *
1614 region_model_manager::get_region_for_fndecl (tree fndecl)
1615 {
1616 gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
1617
1618 function_region **slot = m_fndecls_map.get (fndecl);
1619 if (slot)
1620 return *slot;
1621 function_region *reg
1622 = new function_region (alloc_symbol_id (), &m_code_region, fndecl);
1623 m_fndecls_map.put (fndecl, reg);
1624 return reg;
1625 }
1626
1627 /* Return the region for LABEL, creating it if necessary. */
1628
1629 const label_region *
1630 region_model_manager::get_region_for_label (tree label)
1631 {
1632 gcc_assert (TREE_CODE (label) == LABEL_DECL);
1633
1634 label_region **slot = m_labels_map.get (label);
1635 if (slot)
1636 return *slot;
1637
1638 tree fndecl = DECL_CONTEXT (label);
1639 gcc_assert (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL);
1640
1641 const function_region *func_reg = get_region_for_fndecl (fndecl);
1642 label_region *reg
1643 = new label_region (alloc_symbol_id (), func_reg, label);
1644 m_labels_map.put (label, reg);
1645 return reg;
1646 }
1647
1648 /* Return the region for EXPR, creating it if necessary. */
1649
1650 const decl_region *
1651 region_model_manager::get_region_for_global (tree expr)
1652 {
1653 gcc_assert (VAR_P (expr));
1654
1655 decl_region **slot = m_globals_map.get (expr);
1656 if (slot)
1657 return *slot;
1658 decl_region *reg
1659 = new decl_region (alloc_symbol_id (), &m_globals_region, expr);
1660 m_globals_map.put (expr, reg);
1661 return reg;
1662 }
1663
1664 /* Return the region for an unknown access of type REGION_TYPE,
1665 creating it if necessary.
1666 This is a symbolic_region, where the pointer is an unknown_svalue
1667 of type &REGION_TYPE. */
1668
1669 const region *
1670 region_model_manager::get_unknown_symbolic_region (tree region_type)
1671 {
1672 tree ptr_type = region_type ? build_pointer_type (region_type) : NULL_TREE;
1673 const svalue *unknown_ptr = get_or_create_unknown_svalue (ptr_type);
1674 return get_symbolic_region (unknown_ptr);
1675 }
1676
1677 /* Return the region that describes accessing field FIELD of PARENT,
1678 creating it if necessary. */
1679
1680 const region *
1681 region_model_manager::get_field_region (const region *parent, tree field)
1682 {
1683 gcc_assert (TREE_CODE (field) == FIELD_DECL);
1684
1685 /* (*UNKNOWN_PTR).field is (*UNKNOWN_PTR_OF_&FIELD_TYPE). */
1686 if (parent->symbolic_for_unknown_ptr_p ())
1687 return get_unknown_symbolic_region (TREE_TYPE (field));
1688
1689 field_region::key_t key (parent, field);
1690 if (field_region *reg = m_field_regions.get (key))
1691 return reg;
1692
1693 field_region *field_reg
1694 = new field_region (alloc_symbol_id (), parent, field);
1695 m_field_regions.put (key, field_reg);
1696 return field_reg;
1697 }
1698
1699 /* Return the region that describes accessing the element of type
1700 ELEMENT_TYPE at index INDEX of PARENT, creating it if necessary. */
1701
1702 const region *
1703 region_model_manager::get_element_region (const region *parent,
1704 tree element_type,
1705 const svalue *index)
1706 {
1707 /* (UNKNOWN_PTR[IDX]) is (UNKNOWN_PTR). */
1708 if (parent->symbolic_for_unknown_ptr_p ())
1709 return get_unknown_symbolic_region (element_type);
1710
1711 element_region::key_t key (parent, element_type, index);
1712 if (element_region *reg = m_element_regions.get (key))
1713 return reg;
1714
1715 element_region *element_reg
1716 = new element_region (alloc_symbol_id (), parent, element_type, index);
1717 m_element_regions.put (key, element_reg);
1718 return element_reg;
1719 }
1720
1721 /* Return the region that describes accessing the subregion of type
1722 ELEMENT_TYPE at offset BYTE_OFFSET within PARENT, creating it if
1723 necessary. */
1724
1725 const region *
1726 region_model_manager::get_offset_region (const region *parent,
1727 tree type,
1728 const svalue *byte_offset)
1729 {
1730 /* (UNKNOWN_PTR + OFFSET) is (UNKNOWN_PTR). */
1731 if (parent->symbolic_for_unknown_ptr_p ())
1732 return get_unknown_symbolic_region (type);
1733
1734 /* If BYTE_OFFSET is zero, return PARENT. */
1735 if (tree cst_offset = byte_offset->maybe_get_constant ())
1736 if (zerop (cst_offset))
1737 return get_cast_region (parent, type);
1738
1739 /* Fold OFFSET_REGION(OFFSET_REGION(REG, X), Y)
1740 to OFFSET_REGION(REG, (X + Y)). */
1741 if (const offset_region *parent_offset_reg
1742 = parent->dyn_cast_offset_region ())
1743 {
1744 const svalue *sval_x = parent_offset_reg->get_byte_offset ();
1745 const svalue *sval_sum
1746 = get_or_create_binop (byte_offset->get_type (),
1747 PLUS_EXPR, sval_x, byte_offset);
1748 return get_offset_region (parent->get_parent_region (), type, sval_sum);
1749 }
1750
1751 offset_region::key_t key (parent, type, byte_offset);
1752 if (offset_region *reg = m_offset_regions.get (key))
1753 return reg;
1754
1755 offset_region *offset_reg
1756 = new offset_region (alloc_symbol_id (), parent, type, byte_offset);
1757 m_offset_regions.put (key, offset_reg);
1758 return offset_reg;
1759 }
1760
1761 /* Return the region that describes accessing the subregion of type
1762 TYPE of size BYTE_SIZE_SVAL within PARENT, creating it if necessary. */
1763
1764 const region *
1765 region_model_manager::get_sized_region (const region *parent,
1766 tree type,
1767 const svalue *byte_size_sval)
1768 {
1769 if (parent->symbolic_for_unknown_ptr_p ())
1770 return get_unknown_symbolic_region (type);
1771
1772 if (byte_size_sval->get_type () != size_type_node)
1773 byte_size_sval = get_or_create_cast (size_type_node, byte_size_sval);
1774
1775 /* If PARENT is already that size, return it. */
1776 const svalue *parent_byte_size_sval = parent->get_byte_size_sval (this);
1777 if (tree parent_size_cst = parent_byte_size_sval->maybe_get_constant ())
1778 if (tree size_cst = byte_size_sval->maybe_get_constant ())
1779 {
1780 tree comparison
1781 = fold_binary (EQ_EXPR, boolean_type_node, parent_size_cst, size_cst);
1782 if (comparison == boolean_true_node)
1783 return parent;
1784 }
1785
1786 sized_region::key_t key (parent, type, byte_size_sval);
1787 if (sized_region *reg = m_sized_regions.get (key))
1788 return reg;
1789
1790 sized_region *sized_reg
1791 = new sized_region (alloc_symbol_id (), parent, type, byte_size_sval);
1792 m_sized_regions.put (key, sized_reg);
1793 return sized_reg;
1794 }
1795
1796 /* Return the region that describes accessing PARENT_REGION as if
1797 it were of type TYPE, creating it if necessary. */
1798
1799 const region *
1800 region_model_manager::get_cast_region (const region *original_region,
1801 tree type)
1802 {
1803 /* If types match, return ORIGINAL_REGION. */
1804 if (type == original_region->get_type ())
1805 return original_region;
1806
1807 if (original_region->symbolic_for_unknown_ptr_p ())
1808 return get_unknown_symbolic_region (type);
1809
1810 cast_region::key_t key (original_region, type);
1811 if (cast_region *reg = m_cast_regions.get (key))
1812 return reg;
1813
1814 cast_region *cast_reg
1815 = new cast_region (alloc_symbol_id (), original_region, type);
1816 m_cast_regions.put (key, cast_reg);
1817 return cast_reg;
1818 }
1819
1820 /* Return the frame_region for call to FUN from CALLING_FRAME, creating it
1821 if necessary. CALLING_FRAME may be NULL. */
1822
1823 const frame_region *
1824 region_model_manager::get_frame_region (const frame_region *calling_frame,
1825 const function &fun)
1826 {
1827 int index = calling_frame ? calling_frame->get_index () + 1 : 0;
1828
1829 frame_region::key_t key (calling_frame, fun);
1830 if (frame_region *reg = m_frame_regions.get (key))
1831 return reg;
1832
1833 frame_region *frame_reg
1834 = new frame_region (alloc_symbol_id (), &m_stack_region, calling_frame,
1835 fun, index);
1836 m_frame_regions.put (key, frame_reg);
1837 return frame_reg;
1838 }
1839
1840 /* Return the region that describes dereferencing SVAL, creating it
1841 if necessary. */
1842
1843 const region *
1844 region_model_manager::get_symbolic_region (const svalue *sval)
1845 {
1846 symbolic_region::key_t key (&m_root_region, sval);
1847 if (symbolic_region *reg = m_symbolic_regions.get (key))
1848 return reg;
1849
1850 symbolic_region *symbolic_reg
1851 = new symbolic_region (alloc_symbol_id (), &m_root_region, sval);
1852 m_symbolic_regions.put (key, symbolic_reg);
1853 return symbolic_reg;
1854 }
1855
1856 /* Return the region that describes accessing STRING_CST, creating it
1857 if necessary. */
1858
1859 const string_region *
1860 region_model_manager::get_region_for_string (tree string_cst)
1861 {
1862 gcc_assert (TREE_CODE (string_cst) == STRING_CST);
1863
1864 string_region **slot = m_string_map.get (string_cst);
1865 if (slot)
1866 return *slot;
1867 string_region *reg
1868 = new string_region (alloc_symbol_id (), &m_root_region, string_cst);
1869 m_string_map.put (string_cst, reg);
1870 return reg;
1871 }
1872
1873 /* Return the region that describes accessing BITS within PARENT as TYPE,
1874 creating it if necessary. */
1875
1876 const region *
1877 region_model_manager::get_bit_range (const region *parent, tree type,
1878 const bit_range &bits)
1879 {
1880 gcc_assert (parent);
1881
1882 if (parent->symbolic_for_unknown_ptr_p ())
1883 return get_unknown_symbolic_region (type);
1884
1885 bit_range_region::key_t key (parent, type, bits);
1886 if (bit_range_region *reg = m_bit_range_regions.get (key))
1887 return reg;
1888
1889 bit_range_region *bit_range_reg
1890 = new bit_range_region (alloc_symbol_id (), parent, type, bits);
1891 m_bit_range_regions.put (key, bit_range_reg);
1892 return bit_range_reg;
1893 }
1894
1895 /* Return the region that describes accessing the IDX-th variadic argument
1896 within PARENT_FRAME, creating it if necessary. */
1897
1898 const var_arg_region *
1899 region_model_manager::get_var_arg_region (const frame_region *parent_frame,
1900 unsigned idx)
1901 {
1902 gcc_assert (parent_frame);
1903
1904 var_arg_region::key_t key (parent_frame, idx);
1905 if (var_arg_region *reg = m_var_arg_regions.get (key))
1906 return reg;
1907
1908 var_arg_region *var_arg_reg
1909 = new var_arg_region (alloc_symbol_id (), parent_frame, idx);
1910 m_var_arg_regions.put (key, var_arg_reg);
1911 return var_arg_reg;
1912 }
1913
1914 /* If we see a tree code we don't know how to handle, rather than
1915 ICE or generate bogus results, create a dummy region, and notify
1916 CTXT so that it can mark the new state as being not properly
1917 modelled. The exploded graph can then stop exploring that path,
1918 since any diagnostics we might issue will have questionable
1919 validity. */
1920
1921 const region *
1922 region_model_manager::
1923 get_region_for_unexpected_tree_code (region_model_context *ctxt,
1924 tree t,
1925 const dump_location_t &loc)
1926 {
1927 tree type = TYPE_P (t) ? t : TREE_TYPE (t);
1928 region *new_reg
1929 = new unknown_region (alloc_symbol_id (), &m_root_region, type);
1930 if (ctxt)
1931 ctxt->on_unexpected_tree_code (t, loc);
1932 return new_reg;
1933 }
1934
1935 /* Return a region describing a heap-allocated block of memory.
1936 Reuse an existing heap_allocated_region is its id is not within
1937 BASE_REGS_IN_USE. */
1938
1939 const region *
1940 region_model_manager::
1941 get_or_create_region_for_heap_alloc (const bitmap &base_regs_in_use)
1942 {
1943 /* Try to reuse an existing region, if it's unreferenced in the
1944 client state. */
1945 for (auto existing_reg : m_managed_dynamic_regions)
1946 if (!bitmap_bit_p (base_regs_in_use, existing_reg->get_id ()))
1947 if (existing_reg->get_kind () == RK_HEAP_ALLOCATED)
1948 return existing_reg;
1949
1950 /* All existing ones (if any) are in use; create a new one. */
1951 region *reg
1952 = new heap_allocated_region (alloc_symbol_id (), &m_heap_region);
1953 m_managed_dynamic_regions.safe_push (reg);
1954 return reg;
1955 }
1956
1957 /* Return a new region describing a block of memory allocated within FRAME. */
1958
1959 const region *
1960 region_model_manager::create_region_for_alloca (const frame_region *frame)
1961 {
1962 gcc_assert (frame);
1963 region *reg = new alloca_region (alloc_symbol_id (), frame);
1964 m_managed_dynamic_regions.safe_push (reg);
1965 return reg;
1966 }
1967
1968 /* Log OBJ to LOGGER. */
1969
1970 template <typename T>
1971 static void
1972 log_managed_object (logger *logger, const T *obj)
1973 {
1974 logger->start_log_line ();
1975 pretty_printer *pp = logger->get_printer ();
1976 pp_string (pp, " ");
1977 obj->dump_to_pp (pp, true);
1978 logger->end_log_line ();
1979 }
1980
1981 /* Specialization for frame_region, which also logs the count of locals
1982 managed by the frame_region. */
1983
1984 template <>
1985 void
1986 log_managed_object (logger *logger, const frame_region *obj)
1987 {
1988 logger->start_log_line ();
1989 pretty_printer *pp = logger->get_printer ();
1990 pp_string (pp, " ");
1991 obj->dump_to_pp (pp, true);
1992 pp_printf (pp, " [with %i region(s) for locals]", obj->get_num_locals ());
1993 logger->end_log_line ();
1994 }
1995
1996 /* Dump the number of objects that were managed by UNIQ_MAP to LOGGER.
1997 If SHOW_OBJS is true, also dump the objects themselves. */
1998
1999 template <typename K, typename T>
2000 static void
2001 log_uniq_map (logger *logger, bool show_objs, const char *title,
2002 const hash_map<K, T*> &uniq_map)
2003 {
2004 logger->log (" # %s: %li", title, (long)uniq_map.elements ());
2005 if (!show_objs)
2006 return;
2007 auto_vec<const T *> vec_objs (uniq_map.elements ());
2008 for (typename hash_map<K, T*>::iterator iter = uniq_map.begin ();
2009 iter != uniq_map.end (); ++iter)
2010 vec_objs.quick_push ((*iter).second);
2011
2012 vec_objs.qsort (T::cmp_ptr_ptr);
2013
2014 unsigned i;
2015 const T *obj;
2016 FOR_EACH_VEC_ELT (vec_objs, i, obj)
2017 log_managed_object<T> (logger, obj);
2018 }
2019
2020 /* Dump the number of objects that were managed by MAP to LOGGER.
2021 If SHOW_OBJS is true, also dump the objects themselves. */
2022
2023 template <typename T>
2024 static void
2025 log_uniq_map (logger *logger, bool show_objs, const char *title,
2026 const consolidation_map<T> &map)
2027 {
2028 logger->log (" # %s: %li", title, (long)map.elements ());
2029 if (!show_objs)
2030 return;
2031
2032 auto_vec<const T *> vec_objs (map.elements ());
2033 for (typename consolidation_map<T>::iterator iter = map.begin ();
2034 iter != map.end (); ++iter)
2035 vec_objs.quick_push ((*iter).second);
2036
2037 vec_objs.qsort (T::cmp_ptr_ptr);
2038
2039 unsigned i;
2040 const T *obj;
2041 FOR_EACH_VEC_ELT (vec_objs, i, obj)
2042 log_managed_object<T> (logger, obj);
2043 }
2044
2045 /* Dump the number of objects of each class that were managed by this
2046 manager to LOGGER.
2047 If SHOW_OBJS is true, also dump the objects themselves. */
2048
2049 void
2050 region_model_manager::log_stats (logger *logger, bool show_objs) const
2051 {
2052 LOG_SCOPE (logger);
2053 logger->log ("call string consolidation");
2054 m_empty_call_string.recursive_log (logger);
2055 logger->log ("next symbol id: %i", m_next_symbol_id);
2056 logger->log ("svalue consolidation");
2057 log_uniq_map (logger, show_objs, "constant_svalue", m_constants_map);
2058 log_uniq_map (logger, show_objs, "unknown_svalue", m_unknowns_map);
2059 if (m_unknown_NULL)
2060 log_managed_object (logger, m_unknown_NULL);
2061 log_uniq_map (logger, show_objs, "poisoned_svalue", m_poisoned_values_map);
2062 log_uniq_map (logger, show_objs, "setjmp_svalue", m_setjmp_values_map);
2063 log_uniq_map (logger, show_objs, "initial_svalue", m_initial_values_map);
2064 log_uniq_map (logger, show_objs, "region_svalue", m_pointer_values_map);
2065 log_uniq_map (logger, show_objs, "unaryop_svalue", m_unaryop_values_map);
2066 log_uniq_map (logger, show_objs, "binop_svalue", m_binop_values_map);
2067 log_uniq_map (logger, show_objs, "sub_svalue", m_sub_values_map);
2068 log_uniq_map (logger, show_objs, "repeated_svalue", m_repeated_values_map);
2069 log_uniq_map (logger, show_objs, "bits_within_svalue",
2070 m_bits_within_values_map);
2071 log_uniq_map (logger, show_objs, "unmergeable_svalue",
2072 m_unmergeable_values_map);
2073 log_uniq_map (logger, show_objs, "widening_svalue", m_widening_values_map);
2074 log_uniq_map (logger, show_objs, "compound_svalue", m_compound_values_map);
2075 log_uniq_map (logger, show_objs, "conjured_svalue", m_conjured_values_map);
2076 log_uniq_map (logger, show_objs, "asm_output_svalue",
2077 m_asm_output_values_map);
2078 log_uniq_map (logger, show_objs, "const_fn_result_svalue",
2079 m_const_fn_result_values_map);
2080
2081 logger->log ("max accepted svalue num_nodes: %i",
2082 m_max_complexity.m_num_nodes);
2083 logger->log ("max accepted svalue max_depth: %i",
2084 m_max_complexity.m_max_depth);
2085
2086 logger->log ("region consolidation");
2087 log_uniq_map (logger, show_objs, "function_region", m_fndecls_map);
2088 log_uniq_map (logger, show_objs, "label_region", m_labels_map);
2089 log_uniq_map (logger, show_objs, "decl_region for globals", m_globals_map);
2090 log_uniq_map (logger, show_objs, "field_region", m_field_regions);
2091 log_uniq_map (logger, show_objs, "element_region", m_element_regions);
2092 log_uniq_map (logger, show_objs, "offset_region", m_offset_regions);
2093 log_uniq_map (logger, show_objs, "sized_region", m_sized_regions);
2094 log_uniq_map (logger, show_objs, "cast_region", m_cast_regions);
2095 log_uniq_map (logger, show_objs, "frame_region", m_frame_regions);
2096 log_uniq_map (logger, show_objs, "symbolic_region", m_symbolic_regions);
2097 log_uniq_map (logger, show_objs, "string_region", m_string_map);
2098 log_uniq_map (logger, show_objs, "bit_range_region", m_bit_range_regions);
2099 log_uniq_map (logger, show_objs, "var_arg_region", m_var_arg_regions);
2100 logger->log (" # managed dynamic regions: %i",
2101 m_managed_dynamic_regions.length ());
2102 m_store_mgr.log_stats (logger, show_objs);
2103 m_range_mgr->log_stats (logger, show_objs);
2104 }
2105
2106 /* Dump the number of objects of each class that were managed by this
2107 manager to LOGGER.
2108 If SHOW_OBJS is true, also dump the objects themselves.
2109 This is here so it can use log_uniq_map. */
2110
2111 void
2112 store_manager::log_stats (logger *logger, bool show_objs) const
2113 {
2114 LOG_SCOPE (logger);
2115 log_uniq_map (logger, show_objs, "concrete_binding",
2116 m_concrete_binding_key_mgr);
2117 log_uniq_map (logger, show_objs, "symbolic_binding",
2118 m_symbolic_binding_key_mgr);
2119 }
2120
2121 /* Emit a warning showing DECL_REG->tracked_p () for use in DejaGnu tests
2122 (using -fdump-analyzer-untracked). */
2123
2124 static void
2125 dump_untracked_region (const decl_region *decl_reg)
2126 {
2127 tree decl = decl_reg->get_decl ();
2128 if (TREE_CODE (decl) != VAR_DECL)
2129 return;
2130 /* For now, don't emit the status of decls in the constant pool, to avoid
2131 differences in DejaGnu test results between targets that use these vs
2132 those that don't.
2133 (Eventually these decls should probably be untracked and we should test
2134 for that, but that's not stage 4 material). */
2135 if (DECL_IN_CONSTANT_POOL (decl))
2136 return;
2137 warning_at (DECL_SOURCE_LOCATION (decl), 0,
2138 "track %qD: %s",
2139 decl, (decl_reg->tracked_p () ? "yes" : "no"));
2140 }
2141
2142 /* Implementation of -fdump-analyzer-untracked. */
2143
2144 void
2145 region_model_manager::dump_untracked_regions () const
2146 {
2147 for (auto iter : m_globals_map)
2148 {
2149 const decl_region *decl_reg = iter.second;
2150 dump_untracked_region (decl_reg);
2151 }
2152 for (auto frame_iter : m_frame_regions)
2153 {
2154 const frame_region *frame_reg = frame_iter.second;
2155 frame_reg->dump_untracked_regions ();
2156 }
2157 }
2158
2159 void
2160 frame_region::dump_untracked_regions () const
2161 {
2162 for (auto iter : m_locals)
2163 {
2164 const decl_region *decl_reg = iter.second;
2165 dump_untracked_region (decl_reg);
2166 }
2167 }
2168
2169 } // namespace ana
2170
2171 #endif /* #if ENABLE_ANALYZER */