3d8f76ee24c393c0adfeaa26a4a16a1df3461a11
[platform/upstream/gcc.git] / gcc / analyzer / region-model-manager.h
1 /* Consolidation of svalues and regions.
2    Copyright (C) 2020-2022 Free Software Foundation, Inc.
3    Contributed by David Malcolm <dmalcolm@redhat.com>.
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 #ifndef GCC_ANALYZER_REGION_MODEL_MANAGER_H
22 #define GCC_ANALYZER_REGION_MODEL_MANAGER_H
23
24 namespace ana {
25
26 /* A class responsible for owning and consolidating region and svalue
27    instances.
28    region and svalue instances are immutable as far as clients are
29    concerned, so they are provided as "const" ptrs.  */
30
31 class region_model_manager
32 {
33 public:
34   region_model_manager (logger *logger = NULL);
35   ~region_model_manager ();
36
37   /* call_string consolidation.  */
38   const call_string &get_empty_call_string () const
39   {
40     return m_empty_call_string;
41   }
42
43   /* svalue consolidation.  */
44   const svalue *get_or_create_constant_svalue (tree cst_expr);
45   const svalue *get_or_create_int_cst (tree type, poly_int64);
46   const svalue *get_or_create_unknown_svalue (tree type);
47   const svalue *get_or_create_setjmp_svalue (const setjmp_record &r,
48                                              tree type);
49   const svalue *get_or_create_poisoned_svalue (enum poison_kind kind,
50                                                tree type);
51   const svalue *get_or_create_initial_value (const region *reg);
52   const svalue *get_ptr_svalue (tree ptr_type, const region *pointee);
53   const svalue *get_or_create_unaryop (tree type, enum tree_code op,
54                                        const svalue *arg);
55   const svalue *get_or_create_cast (tree type, const svalue *arg);
56   const svalue *get_or_create_binop (tree type,
57                                      enum tree_code op,
58                                      const svalue *arg0, const svalue *arg1);
59   const svalue *get_or_create_sub_svalue (tree type,
60                                           const svalue *parent_svalue,
61                                           const region *subregion);
62   const svalue *get_or_create_repeated_svalue (tree type,
63                                                const svalue *outer_size,
64                                                const svalue *inner_svalue);
65   const svalue *get_or_create_bits_within (tree type,
66                                            const bit_range &bits,
67                                            const svalue *inner_svalue);
68   const svalue *get_or_create_unmergeable (const svalue *arg);
69   const svalue *get_or_create_widening_svalue (tree type,
70                                                const function_point &point,
71                                                const svalue *base_svalue,
72                                                const svalue *iter_svalue);
73   const svalue *get_or_create_compound_svalue (tree type,
74                                                const binding_map &map);
75   const svalue *get_or_create_conjured_svalue (tree type, const gimple *stmt,
76                                                const region *id_reg,
77                                                const conjured_purge &p);
78   const svalue *
79   get_or_create_asm_output_svalue (tree type,
80                                    const gasm *asm_stmt,
81                                    unsigned output_idx,
82                                    const vec<const svalue *> &inputs);
83   const svalue *
84   get_or_create_asm_output_svalue (tree type,
85                                    const char *asm_string,
86                                    unsigned output_idx,
87                                    unsigned num_outputs,
88                                    const vec<const svalue *> &inputs);
89   const svalue *
90   get_or_create_const_fn_result_svalue (tree type,
91                                         tree fndecl,
92                                         const vec<const svalue *> &inputs);
93
94   const svalue *maybe_get_char_from_string_cst (tree string_cst,
95                                                 tree byte_offset_cst);
96
97   /* Dynamically-allocated svalue instances.
98      The number of these within the analysis can grow arbitrarily.
99      They are still owned by the manager.  */
100   const svalue *create_unique_svalue (tree type);
101
102   /* region consolidation.  */
103   const stack_region * get_stack_region () const { return &m_stack_region; }
104   const heap_region *get_heap_region () const { return &m_heap_region; }
105   const code_region *get_code_region () const { return &m_code_region; }
106   const globals_region *get_globals_region () const
107   {
108     return &m_globals_region;
109   }
110   const function_region *get_region_for_fndecl (tree fndecl);
111   const label_region *get_region_for_label (tree label);
112   const decl_region *get_region_for_global (tree expr);
113   const region *get_field_region (const region *parent, tree field);
114   const region *get_element_region (const region *parent,
115                                     tree element_type,
116                                     const svalue *index);
117   const region *get_offset_region (const region *parent,
118                                    tree type,
119                                    const svalue *byte_offset);
120   const region *get_sized_region (const region *parent,
121                                   tree type,
122                                   const svalue *byte_size_sval);
123   const region *get_cast_region (const region *original_region,
124                                  tree type);
125   const frame_region *get_frame_region (const frame_region *calling_frame,
126                                         function *fun);
127   const region *get_symbolic_region (const svalue *sval);
128   const string_region *get_region_for_string (tree string_cst);
129   const region *get_bit_range (const region *parent, tree type,
130                                const bit_range &bits);
131   const var_arg_region *get_var_arg_region (const frame_region *parent,
132                                             unsigned idx);
133
134   const region *get_unknown_symbolic_region (tree region_type);
135
136   const region *
137   get_region_for_unexpected_tree_code (region_model_context *ctxt,
138                                        tree t,
139                                        const dump_location_t &loc);
140
141   unsigned alloc_region_id () { return m_next_region_id++; }
142
143   store_manager *get_store_manager () { return &m_store_mgr; }
144   bounded_ranges_manager *get_range_manager () const { return m_range_mgr; }
145
146   known_function_manager *get_known_function_manager ()
147   {
148     return &m_known_fn_mgr;
149   }
150
151   /* Dynamically-allocated region instances.
152      The number of these within the analysis can grow arbitrarily.
153      They are still owned by the manager.  */
154   const region *create_region_for_heap_alloc ();
155   const region *create_region_for_alloca (const frame_region *frame);
156
157   void log_stats (logger *logger, bool show_objs) const;
158
159   void begin_checking_feasibility (void) { m_checking_feasibility = true; }
160   void end_checking_feasibility (void) { m_checking_feasibility = false; }
161
162   logger *get_logger () const { return m_logger; }
163
164   void dump_untracked_regions () const;
165
166 private:
167   bool too_complex_p (const complexity &c) const;
168   bool reject_if_too_complex (svalue *sval);
169
170   const svalue *maybe_fold_unaryop (tree type, enum tree_code op,
171                                     const svalue *arg);
172   const svalue *maybe_fold_binop (tree type, enum tree_code op,
173                                   const svalue *arg0, const svalue *arg1);
174   const svalue *maybe_fold_sub_svalue (tree type,
175                                        const svalue *parent_svalue,
176                                        const region *subregion);
177   const svalue *maybe_fold_repeated_svalue (tree type,
178                                             const svalue *outer_size,
179                                             const svalue *inner_svalue);
180   const svalue *maybe_fold_bits_within_svalue (tree type,
181                                                const bit_range &bits,
182                                                const svalue *inner_svalue);
183   const svalue *maybe_undo_optimize_bit_field_compare (tree type,
184                                                        const compound_svalue *compound_sval,
185                                                        tree cst, const svalue *arg1);
186   const svalue *maybe_fold_asm_output_svalue (tree type,
187                                               const vec<const svalue *> &inputs);
188
189   logger *m_logger;
190
191   const call_string m_empty_call_string;
192
193   unsigned m_next_region_id;
194   root_region m_root_region;
195   stack_region m_stack_region;
196   heap_region m_heap_region;
197
198   /* svalue consolidation.  */
199   typedef hash_map<tree, constant_svalue *> constants_map_t;
200   constants_map_t m_constants_map;
201
202   typedef hash_map<tree, unknown_svalue *> unknowns_map_t;
203   unknowns_map_t m_unknowns_map;
204   const unknown_svalue *m_unknown_NULL;
205
206   typedef hash_map<poisoned_svalue::key_t,
207                    poisoned_svalue *> poisoned_values_map_t;
208   poisoned_values_map_t m_poisoned_values_map;
209
210   typedef hash_map<setjmp_svalue::key_t,
211                    setjmp_svalue *> setjmp_values_map_t;
212   setjmp_values_map_t m_setjmp_values_map;
213
214   typedef hash_map<const region *, initial_svalue *> initial_values_map_t;
215   initial_values_map_t m_initial_values_map;
216
217   typedef hash_map<region_svalue::key_t, region_svalue *> pointer_values_map_t;
218   pointer_values_map_t m_pointer_values_map;
219
220   typedef hash_map<unaryop_svalue::key_t,
221                    unaryop_svalue *> unaryop_values_map_t;
222   unaryop_values_map_t m_unaryop_values_map;
223
224   typedef hash_map<binop_svalue::key_t, binop_svalue *> binop_values_map_t;
225   binop_values_map_t m_binop_values_map;
226
227   typedef hash_map<sub_svalue::key_t, sub_svalue *> sub_values_map_t;
228   sub_values_map_t m_sub_values_map;
229
230   typedef hash_map<repeated_svalue::key_t,
231                    repeated_svalue *> repeated_values_map_t;
232   repeated_values_map_t m_repeated_values_map;
233
234   typedef hash_map<bits_within_svalue::key_t,
235                    bits_within_svalue *> bits_within_values_map_t;
236   bits_within_values_map_t m_bits_within_values_map;
237
238   typedef hash_map<const svalue *,
239                    unmergeable_svalue *> unmergeable_values_map_t;
240   unmergeable_values_map_t m_unmergeable_values_map;
241
242   typedef hash_map<widening_svalue::key_t,
243                    widening_svalue */*,
244                    widening_svalue::key_t::hash_map_traits*/>
245     widening_values_map_t;
246   widening_values_map_t m_widening_values_map;
247
248   typedef hash_map<compound_svalue::key_t,
249                    compound_svalue *> compound_values_map_t;
250   compound_values_map_t m_compound_values_map;
251
252   typedef hash_map<conjured_svalue::key_t,
253                    conjured_svalue *> conjured_values_map_t;
254   conjured_values_map_t m_conjured_values_map;
255
256   typedef hash_map<asm_output_svalue::key_t,
257                    asm_output_svalue *> asm_output_values_map_t;
258   asm_output_values_map_t m_asm_output_values_map;
259
260   typedef hash_map<const_fn_result_svalue::key_t,
261                    const_fn_result_svalue *> const_fn_result_values_map_t;
262   const_fn_result_values_map_t m_const_fn_result_values_map;
263
264   bool m_checking_feasibility;
265
266   /* "Dynamically-allocated" svalue instances.
267      The number of these within the analysis can grow arbitrarily.
268      They are still owned by the manager.  */
269   auto_delete_vec<svalue> m_managed_dynamic_svalues;
270
271   /* Maximum complexity of svalues that weren't rejected.  */
272   complexity m_max_complexity;
273
274   /* region consolidation.  */
275
276   code_region m_code_region;
277   typedef hash_map<tree, function_region *> fndecls_map_t;
278   typedef fndecls_map_t::iterator fndecls_iterator_t;
279   fndecls_map_t m_fndecls_map;
280
281   typedef hash_map<tree, label_region *> labels_map_t;
282   typedef labels_map_t::iterator labels_iterator_t;
283   labels_map_t m_labels_map;
284
285   globals_region m_globals_region;
286   typedef hash_map<tree, decl_region *> globals_map_t;
287   typedef globals_map_t::iterator globals_iterator_t;
288   globals_map_t m_globals_map;
289
290   consolidation_map<field_region> m_field_regions;
291   consolidation_map<element_region> m_element_regions;
292   consolidation_map<offset_region> m_offset_regions;
293   consolidation_map<sized_region> m_sized_regions;
294   consolidation_map<cast_region> m_cast_regions;
295   consolidation_map<frame_region> m_frame_regions;
296   consolidation_map<symbolic_region> m_symbolic_regions;
297
298   typedef hash_map<tree, string_region *> string_map_t;
299   string_map_t m_string_map;
300
301   consolidation_map<bit_range_region> m_bit_range_regions;
302   consolidation_map<var_arg_region> m_var_arg_regions;
303
304   store_manager m_store_mgr;
305
306   bounded_ranges_manager *m_range_mgr;
307
308   known_function_manager m_known_fn_mgr;
309
310   /* "Dynamically-allocated" region instances.
311      The number of these within the analysis can grow arbitrarily.
312      They are still owned by the manager.  */
313   auto_delete_vec<region> m_managed_dynamic_regions;
314 };
315
316 } // namespace ana
317
318 #endif /* GCC_ANALYZER_REGION_MODEL_MANAGER_H */