Line data Source code
1 : /* Classes for purging state at function_points.
2 : Copyright (C) 2019-2026 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 : #define INCLUDE_SET
22 : #include "analyzer/common.h"
23 :
24 : #include "timevar.h"
25 : #include "gimple-pretty-print.h"
26 : #include "tree-vrp.h"
27 : #include "gimple-ssa.h"
28 : #include "stringpool.h"
29 : #include "tree-ssanames.h"
30 : #include "tree-phinodes.h"
31 : #include "options.h"
32 : #include "ssa-iterators.h"
33 : #include "gimple-iterator.h"
34 : #include "gimple-walk.h"
35 : #include "cgraph.h"
36 :
37 : #include "analyzer/call-string.h"
38 : #include "analyzer/supergraph.h"
39 : #include "analyzer/program-point.h"
40 : #include "analyzer/analyzer-logging.h"
41 : #include "analyzer/state-purge.h"
42 : #include "analyzer/store.h"
43 : #include "analyzer/region-model.h"
44 :
45 : #if ENABLE_ANALYZER
46 :
47 : /* Given NODE at an access, determine if this access is within
48 : a decl that could be consider for purging, and if so, return the decl. */
49 :
50 : static tree
51 43618 : get_candidate_for_purging (tree node)
52 : {
53 43618 : tree iter = node;
54 61410 : while (1)
55 52514 : switch (TREE_CODE (iter))
56 : {
57 : default:
58 : return NULL_TREE;
59 :
60 8896 : case ADDR_EXPR:
61 8896 : case MEM_REF:
62 8896 : case COMPONENT_REF:
63 8896 : iter = TREE_OPERAND (iter, 0);
64 8896 : continue;
65 :
66 28760 : case VAR_DECL:
67 28760 : if (is_global_var (iter))
68 : return NULL_TREE;
69 : else
70 : return iter;
71 :
72 : case PARM_DECL:
73 : case RESULT_DECL:
74 : return iter;
75 : }
76 : }
77 :
78 : /* Class-based handler for walk_stmt_load_store_addr_ops at a particular
79 : ana::operation, for populating the worklists within a state_purge_map. */
80 :
81 298864 : class gimple_op_visitor : public log_user
82 : {
83 : public:
84 149432 : gimple_op_visitor (state_purge_map *map,
85 : const superedge &sedge)
86 : : log_user (map->get_logger ()),
87 149432 : m_map (map),
88 149432 : m_sedge (sedge),
89 149432 : m_fun (sedge.m_src->get_function ())
90 : {
91 149432 : gcc_assert (m_fun);
92 149432 : }
93 :
94 14634 : bool on_load (gimple *stmt, tree base, tree op)
95 : {
96 14634 : LOG_FUNC (get_logger ());
97 14634 : if (get_logger ())
98 : {
99 9 : pretty_printer pp;
100 9 : pp_gimple_stmt_1 (&pp, stmt, 0, (dump_flags_t)0);
101 9 : log ("on_load: %s; base: %qE, op: %qE",
102 : pp_formatted_text (&pp), base, op);
103 9 : }
104 14634 : if (tree node = get_candidate_for_purging (base))
105 4386 : add_needed (node);
106 29268 : return true;
107 14634 : }
108 :
109 12660 : bool on_store (gimple *stmt, tree base, tree op)
110 : {
111 12660 : LOG_FUNC (get_logger ());
112 12660 : if (get_logger ())
113 : {
114 7 : pretty_printer pp;
115 7 : pp_gimple_stmt_1 (&pp, stmt, 0, (dump_flags_t)0);
116 7 : log ("on_store: %s; base: %qE, op: %qE",
117 : pp_formatted_text (&pp), base, op);
118 7 : }
119 25320 : return true;
120 12660 : }
121 :
122 16071 : bool on_addr (gimple *stmt, tree base, tree op)
123 : {
124 16071 : LOG_FUNC (get_logger ());
125 16071 : if (get_logger ())
126 : {
127 3 : pretty_printer pp;
128 3 : pp_gimple_stmt_1 (&pp, stmt, 0, (dump_flags_t)0);
129 3 : log ("on_addr: %s; base: %qE, op: %qE",
130 : pp_formatted_text (&pp), base, op);
131 3 : }
132 16071 : if (TREE_CODE (op) != ADDR_EXPR)
133 : return true;
134 16008 : if (tree node = get_candidate_for_purging (base))
135 : {
136 4295 : add_needed (node);
137 4295 : add_pointed_to (node);
138 : }
139 : return true;
140 16071 : }
141 :
142 : private:
143 8681 : void add_needed (tree decl)
144 : {
145 8681 : gcc_assert (get_candidate_for_purging (decl) == decl);
146 8681 : state_purge_per_decl &data
147 8681 : = get_or_create_data_for_decl (decl);
148 8681 : data.add_needed_at (*m_sedge.m_src);
149 8681 : }
150 :
151 4295 : void add_pointed_to (tree decl)
152 : {
153 4295 : gcc_assert (get_candidate_for_purging (decl) == decl);
154 4295 : get_or_create_data_for_decl (decl).add_pointed_to_at (*m_sedge.m_src);
155 4295 : }
156 :
157 : state_purge_per_decl &
158 12976 : get_or_create_data_for_decl (tree decl)
159 : {
160 12976 : return m_map->get_or_create_data_for_decl (*m_fun, decl);
161 : }
162 :
163 : state_purge_map *m_map;
164 : const superedge &m_sedge;
165 : const function *m_fun;
166 : };
167 :
168 : static bool
169 14634 : my_load_cb (gimple *stmt, tree base, tree op, void *user_data)
170 : {
171 14634 : gimple_op_visitor *x = (gimple_op_visitor *)user_data;
172 14634 : return x->on_load (stmt, base, op);
173 : }
174 :
175 : static bool
176 12660 : my_store_cb (gimple *stmt, tree base, tree op, void *user_data)
177 : {
178 12660 : gimple_op_visitor *x = (gimple_op_visitor *)user_data;
179 12660 : return x->on_store (stmt, base, op);
180 : }
181 :
182 : static bool
183 16071 : my_addr_cb (gimple *stmt, tree base, tree op, void *user_data)
184 : {
185 16071 : gimple_op_visitor *x = (gimple_op_visitor *)user_data;
186 16071 : return x->on_addr (stmt, base, op);
187 : }
188 :
189 : /* state_purge_map's ctor. Walk all SSA names in all functions, building
190 : a state_purge_per_ssa_name instance for each.
191 : Also, walk all loads and address-taken ops of local variables, building
192 : a state_purge_per_decl as appropriate. */
193 :
194 3562 : state_purge_map::state_purge_map (const supergraph &sg,
195 : region_model_manager *mgr,
196 : logger *logger)
197 3562 : : log_user (logger), m_sg (sg)
198 : {
199 3562 : LOG_FUNC (logger);
200 :
201 3562 : auto_timevar tv (TV_ANALYZER_STATE_PURGE);
202 :
203 3562 : cgraph_node *node;
204 15206 : FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
205 : {
206 11644 : function *fun = node->get_fun ();
207 11644 : gcc_assert (fun);
208 11644 : if (logger)
209 6 : log ("function: %s", function_name (fun));
210 11644 : tree name;
211 11644 : unsigned int i;;
212 161557 : FOR_EACH_SSA_NAME (i, name, fun)
213 : {
214 : /* For now, don't bother tracking the .MEM SSA names. */
215 149877 : if (tree var = SSA_NAME_VAR (name))
216 100946 : if (TREE_CODE (var) == VAR_DECL)
217 90278 : if (VAR_DECL_IS_VIRTUAL_OPERAND (var))
218 70571 : continue;
219 79306 : m_ssa_map.put (name, new state_purge_per_ssa_name (*this, name, *fun));
220 : }
221 : }
222 :
223 : /* Find all uses of local vars.
224 : We iterate through all operations, finding loads, stores, and
225 : address-taken operations on locals, building a pair of worklists. */
226 216565 : for (auto sedge : sg.m_edges)
227 : {
228 205887 : if (logger)
229 81 : log ("edge: SN %i -> SN %i",
230 81 : sedge->m_src->m_id,
231 81 : sedge->m_dest->m_id);
232 205887 : if (auto op = sedge->get_op ())
233 : {
234 149432 : gimple_op_visitor v (this, *sedge);
235 149432 : op->walk_load_store_addr_ops (&v,
236 : my_load_cb, my_store_cb, my_addr_cb);
237 149432 : }
238 : }
239 :
240 : /* Now iterate through the m_decl_map, processing each pair of worklists. */
241 3562 : for (state_purge_map::decl_iterator iter = begin_decls ();
242 11939 : iter != end_decls ();
243 3586 : ++iter)
244 : {
245 3586 : state_purge_per_decl *per_decl_data = (*iter).second;
246 3586 : per_decl_data->process_worklists (*this, mgr);
247 : }
248 3562 : }
249 :
250 : /* state_purge_map's dtor. */
251 :
252 3562 : state_purge_map::~state_purge_map ()
253 : {
254 86162 : for (auto iter : m_ssa_map)
255 158612 : delete iter.second;
256 8353 : for (auto iter : m_decl_map)
257 7172 : delete iter.second;
258 3562 : }
259 :
260 : /* Get the state_purge_per_decl for local DECL within FUN, creating it
261 : if necessary. */
262 :
263 : state_purge_per_decl &
264 12976 : state_purge_map::get_or_create_data_for_decl (const function &fun, tree decl)
265 : {
266 25952 : if (state_purge_per_decl **slot
267 12976 : = const_cast <decl_map_t&> (m_decl_map).get (decl))
268 9390 : return **slot;
269 3586 : state_purge_per_decl *result = new state_purge_per_decl (*this, decl, fun);
270 3586 : m_decl_map.put (decl, result);
271 3586 : return *result;
272 : }
273 :
274 : void
275 0 : state_purge_map::on_duplicated_node (const supernode &old_snode,
276 : const supernode &new_snode)
277 : {
278 0 : for (auto iter : m_ssa_map)
279 0 : iter.second->on_duplicated_node (old_snode, new_snode);
280 0 : for (auto iter : m_decl_map)
281 0 : iter.second->on_duplicated_node (old_snode, new_snode);
282 0 : }
283 :
284 : /* class state_purge_per_ssa_name : public state_purge_per_tree. */
285 :
286 : /* state_purge_per_ssa_name's ctor.
287 :
288 : Locate all uses of VAR within FUN.
289 : Walk backwards from each use, marking program points, until
290 : we reach the def stmt, populating m_points_needing_var.
291 :
292 : We have to track program points rather than
293 : just stmts since there could be empty basic blocks on the way. */
294 :
295 79306 : state_purge_per_ssa_name::state_purge_per_ssa_name (const state_purge_map &map,
296 : tree name,
297 : const function &fun)
298 79306 : : state_purge_per_tree (fun), m_snodes_needing_name (), m_name (name)
299 : {
300 79306 : LOG_FUNC (map.get_logger ());
301 :
302 79306 : if (map.get_logger ())
303 : {
304 23 : map.log ("SSA name: %qE within %qD", name, fun.decl);
305 :
306 : /* Show def stmt. */
307 23 : const gimple *def_stmt = SSA_NAME_DEF_STMT (name);
308 23 : pretty_printer pp;
309 23 : pp_gimple_stmt_1 (&pp, def_stmt, 0, (dump_flags_t)0);
310 23 : map.log ("def stmt: %s", pp_formatted_text (&pp));
311 23 : }
312 :
313 79306 : auto_vec<const supernode *> worklist;
314 :
315 : /* Add all immediate uses of name to the worklist.
316 : Compare with debug_immediate_uses. */
317 79306 : imm_use_iterator iter;
318 79306 : use_operand_p use_p;
319 181341 : FOR_EACH_IMM_USE_FAST (use_p, iter, name)
320 : {
321 102035 : if (USE_STMT (use_p))
322 : {
323 102035 : const gimple *use_stmt = USE_STMT (use_p);
324 102035 : if (map.get_logger ())
325 : {
326 29 : pretty_printer pp;
327 29 : pp_gimple_stmt_1 (&pp, use_stmt, 0, (dump_flags_t)0);
328 29 : map.log ("used by stmt: %s", pp_formatted_text (&pp));
329 29 : }
330 :
331 102035 : if (is_gimple_debug (use_stmt))
332 : {
333 : /* We skipped debug stmts when building the supergraph,
334 : so ignore them now. */
335 447 : if (map.get_logger ())
336 0 : map.log ("skipping debug stmt");
337 447 : continue;
338 : }
339 :
340 : /* If it's a use within a phi node, then we care about
341 : which in-edge we came from. */
342 101588 : if (use_stmt->code == GIMPLE_PHI)
343 : {
344 9316 : const gphi *phi = as_a <const gphi *> (use_stmt);
345 : /* Find arguments (and thus CFG in-edges) which use NAME. */
346 35957 : for (unsigned arg_idx = 0;
347 35957 : arg_idx < gimple_phi_num_args (phi);
348 : ++arg_idx)
349 : {
350 26641 : if (name == gimple_phi_arg (phi, arg_idx)->def)
351 : {
352 10940 : edge in_edge = gimple_phi_arg_edge (phi, arg_idx);
353 10940 : const superedge *in_sedge
354 10940 : = map.get_sg ().get_superedge_for_phis (in_edge);
355 10940 : if (in_sedge)
356 : {
357 10874 : add_to_worklist (*in_sedge->m_src,
358 : &worklist,
359 : map.get_logger ());
360 10874 : m_snodes_needing_name.add (in_sedge->m_src);
361 : }
362 : else
363 : {
364 : /* Should only happen for abnormal edges, which
365 : get skipped in supergraph construction. */
366 66 : gcc_assert (in_edge->flags & EDGE_ABNORMAL);
367 : }
368 : }
369 : }
370 : }
371 : else
372 : {
373 92272 : const supernode *snode
374 92272 : = map.get_sg ().get_supernode_for_stmt (use_stmt);
375 92272 : add_to_worklist (*snode, &worklist, map.get_logger ());
376 92272 : m_snodes_needing_name.add (snode);
377 : }
378 : }
379 79306 : }
380 :
381 : /* Process worklist by walking backwards until we reach the def stmt. */
382 79306 : {
383 79306 : log_scope s (map.get_logger (), "processing worklist");
384 451087 : while (worklist.length () > 0)
385 : {
386 292475 : const supernode *snode = worklist.pop ();
387 292475 : gcc_assert (snode);
388 292475 : process_supernode (*snode, &worklist, map);
389 : }
390 79306 : }
391 :
392 79306 : if (map.get_logger ())
393 : {
394 23 : map.log ("%qE in %qD is needed to process:", name, fun.decl);
395 : /* Log m_snodes_needing_name, sorting it to avoid churn when comparing
396 : dumps. */
397 23 : std::set<int> indices;
398 23 : auto_vec<const supernode *> snodes;
399 97 : for (auto iter : m_snodes_needing_name)
400 74 : indices.insert (iter->m_id);
401 97 : for (auto iter : indices)
402 74 : map.get_logger ()->log (" SN %i", iter);
403 23 : }
404 79306 : }
405 :
406 : /* Return true if the SSA name is needed at POINT. */
407 :
408 : bool
409 788209 : state_purge_per_ssa_name::needed_at_supernode_p (const supernode *snode) const
410 : {
411 788209 : return const_cast <point_set_t &> (m_snodes_needing_name).contains (snode);
412 : }
413 :
414 : /* Add SNODE to *WORKLIST if the supernode has not already been seen.
415 : Subroutine of ctor. */
416 :
417 : void
418 328556 : state_purge_per_ssa_name::add_to_worklist (const supernode &snode,
419 : auto_vec<const supernode *> *worklist,
420 : logger *logger)
421 : {
422 328556 : LOG_FUNC (logger);
423 :
424 328556 : gcc_assert (snode.get_function () == &get_function ());
425 :
426 328556 : if (m_snodes_needing_name.contains (&snode))
427 : {
428 36081 : if (logger)
429 8 : logger->log ("SN %i already seen for %qE", snode.m_id, m_name);
430 : }
431 : else
432 : {
433 292475 : if (logger)
434 74 : logger->log ("not seen; adding SN %i to worklist for %qE",
435 74 : snode.m_id, m_name);
436 292475 : m_snodes_needing_name.add (&snode);
437 292475 : worklist->safe_push (&snode);
438 : }
439 328556 : }
440 :
441 : /* Process SNODE, popped from WORKLIST.
442 : Iterate over predecessors of SNODE, adding to WORKLIST. */
443 :
444 : void
445 292475 : state_purge_per_ssa_name::process_supernode (const supernode &snode,
446 : auto_vec<const supernode *> *worklist,
447 : const state_purge_map &map)
448 : {
449 292475 : logger *logger = map.get_logger ();
450 292475 : LOG_FUNC (logger);
451 292475 : if (logger)
452 74 : logger->log ("considering SN %i for %qE", snode.m_id, m_name);
453 :
454 1155061 : for (auto in_edge : snode.m_preds)
455 : {
456 298066 : if (logger)
457 78 : logger->log ("considering edge from SN %i", in_edge->m_src->m_id);
458 298066 : bool defines_ssa_name = false;
459 298066 : if (auto op = in_edge->get_op ())
460 246617 : if (op->defines_ssa_name_p (m_name))
461 72656 : defines_ssa_name = true;
462 72656 : if (defines_ssa_name)
463 : {
464 72656 : if (logger)
465 25 : logger->log ("op defines %qE", m_name);
466 : }
467 : else
468 225410 : add_to_worklist (*in_edge->m_src, worklist, logger);
469 : }
470 292475 : }
471 :
472 : void
473 0 : state_purge_per_ssa_name::on_duplicated_node (const supernode &old_snode,
474 : const supernode &new_snode)
475 : {
476 0 : if (m_snodes_needing_name.contains (&old_snode))
477 0 : m_snodes_needing_name.add (&new_snode);
478 0 : }
479 :
480 : /* class state_purge_per_decl : public state_purge_per_tree. */
481 :
482 : /* state_purge_per_decl's ctor. */
483 :
484 3586 : state_purge_per_decl::state_purge_per_decl (const state_purge_map &map,
485 : tree decl,
486 : const function &fun)
487 : : state_purge_per_tree (fun),
488 3586 : m_decl (decl)
489 : {
490 : /* The RESULT_DECL is always needed at the end of its function. */
491 3586 : if (TREE_CODE (decl) == RESULT_DECL)
492 : {
493 34 : supernode *exit_snode = map.get_sg ().get_node_for_function_exit (fun);
494 34 : add_needed_at (*exit_snode);
495 : }
496 3586 : }
497 :
498 : /* Mark the value of the decl (or a subvalue within it) as being needed
499 : at SNODE. */
500 :
501 : void
502 8715 : state_purge_per_decl::add_needed_at (const supernode &snode)
503 : {
504 8715 : m_snodes_needing_decl.add (&snode);
505 8715 : }
506 :
507 : /* Mark that a pointer to the decl (or a region within it) is taken
508 : at SNODE. */
509 :
510 : void
511 4295 : state_purge_per_decl::add_pointed_to_at (const supernode &snode)
512 : {
513 4295 : m_snodes_taking_address.add (&snode);
514 4295 : }
515 :
516 : /* Process the worklists for this decl:
517 : (a) walk backwards from snodes where we know the value of the decl
518 : is needed, marking snodes until we get to a stmt that fully overwrites
519 : the decl.
520 : (b) walk forwards from snodes where the address of the decl is taken,
521 : marking snodes as potentially needing the value of the decl. */
522 :
523 : void
524 3586 : state_purge_per_decl::process_worklists (const state_purge_map &map,
525 : region_model_manager *mgr)
526 : {
527 3586 : logger *logger = map.get_logger ();
528 3586 : LOG_SCOPE (logger);
529 3586 : if (logger)
530 2 : logger->log ("decl: %qE within %qD", m_decl, get_fndecl ());
531 :
532 : /* Worklist for walking backwards from uses. */
533 3586 : {
534 3586 : auto_vec<const supernode *> worklist;
535 3586 : point_set_t seen;
536 :
537 : /* Add all uses of the decl to the worklist. */
538 12253 : for (auto iter : m_snodes_needing_decl)
539 8667 : worklist.safe_push (iter);
540 :
541 3586 : region_model model (mgr->get_node_manager ());
542 3586 : model.push_frame (get_function (), nullptr, nullptr, nullptr);
543 :
544 : /* Process worklist by walking backwards until we reach a stmt
545 : that fully overwrites the decl. */
546 3586 : {
547 3586 : log_scope s (logger, "processing worklist");
548 71186 : while (worklist.length () > 0)
549 : {
550 64014 : const supernode *snode = worklist.pop ();
551 64014 : process_supernode_backwards (*snode, &worklist, &seen, map, model);
552 : }
553 3586 : }
554 3586 : }
555 :
556 : /* Worklist for walking forwards from address-taken snodes. */
557 3586 : {
558 3586 : auto_vec<const supernode *> worklist;
559 3586 : point_set_t seen;
560 :
561 : /* Add all uses of the decl to the worklist. */
562 7847 : for (auto iter : m_snodes_taking_address)
563 : {
564 4261 : worklist.safe_push (iter);
565 :
566 : /* Add to m_snodes_needing_decl (now that we traversed
567 : it above for the backward worklist). */
568 4261 : m_snodes_needing_decl.add (iter);
569 : }
570 :
571 : /* Process worklist by walking forwards. */
572 3586 : {
573 3586 : log_scope s (logger, "processing worklist");
574 65624 : while (worklist.length () > 0)
575 : {
576 58452 : const supernode *snode = worklist.pop ();
577 58452 : process_supernode_forwards (*snode, &worklist, &seen, map);
578 : }
579 3586 : }
580 3586 : }
581 3586 : }
582 :
583 : /* Add SNODE to *WORKLIST if the point is not already in *seen.
584 : Add newly seen supernodes to *SEEN and to m_snodes_needing_name. */
585 :
586 : void
587 121714 : state_purge_per_decl::add_to_worklist (const supernode &snode,
588 : auto_vec<const supernode *> *worklist,
589 : point_set_t *seen,
590 : logger *logger)
591 : {
592 121714 : LOG_FUNC (logger);
593 121714 : if (logger)
594 16 : logger->log ("SN %i for worklist for %qE", snode.m_id, m_decl);
595 :
596 121714 : gcc_assert (snode.get_function () == &get_function ());
597 :
598 121714 : if (seen->contains (&snode))
599 : {
600 12176 : if (logger)
601 0 : logger->log ("already seen for %qE", m_decl);
602 : }
603 : else
604 : {
605 109538 : if (logger)
606 16 : logger->log ("not seen; adding to worklist for %qE", m_decl);
607 109538 : m_snodes_needing_decl.add (&snode);
608 109538 : seen->add (&snode);
609 109538 : worklist->safe_push (&snode);
610 : }
611 121714 : }
612 :
613 : /* Determine if REG_A and REG_B express writing to exactly the same
614 : set of bits.
615 : For example, when "s.field" is the only field of "s", and there's no
616 : padding, this should return true. */
617 :
618 : static bool
619 33746 : same_binding_p (const region *reg_a, const region *reg_b,
620 : region_model_manager &mgr)
621 : {
622 33746 : if (reg_a->get_base_region () != reg_b->get_base_region ())
623 : return false;
624 3871 : if (reg_a->empty_p (mgr))
625 : return false;
626 3871 : if (reg_b->empty_p (mgr))
627 : return false;
628 :
629 3871 : auto mem_range_a = reg_a->get_memory_range (mgr);
630 3871 : auto mem_range_b = reg_b->get_memory_range (mgr);
631 :
632 3871 : return mem_range_a == mem_range_b;
633 : }
634 :
635 : /* Return true if STMT fully overwrites DECL. */
636 :
637 : static bool
638 47579 : fully_overwrites_p (const gimple *stmt, tree decl,
639 : const region_model &model)
640 : {
641 47579 : if (tree lhs = gimple_get_lhs (stmt))
642 : {
643 : /* Determine if LHS fully overwrites DECL.
644 : We can't just check for equality; consider the case of
645 : "s.field = EXPR;" where the stmt writes to the only field
646 : of "s", and there's no padding. */
647 33746 : const region *lhs_reg = model.get_lvalue (lhs, nullptr);
648 33746 : const region *decl_reg = model.get_lvalue (decl, nullptr);
649 33746 : if (same_binding_p (lhs_reg, decl_reg, *model.get_manager ()))
650 : return true;
651 : }
652 : return false;
653 : }
654 :
655 : /* Process SNODE, popped from *WORKLIST.
656 : Iterate over predecessors of SNODE, adding to *WORKLIST and *SEEN,
657 : until we get to a stmt that fully overwrites the decl. */
658 :
659 : void
660 64014 : state_purge_per_decl::
661 : process_supernode_backwards (const supernode &snode,
662 : auto_vec<const supernode *> *worklist,
663 : point_set_t *seen,
664 : const state_purge_map &map,
665 : const region_model &model)
666 : {
667 64014 : logger *logger = map.get_logger ();
668 64014 : LOG_FUNC (logger);
669 64014 : if (logger)
670 10 : logger->log ("considering SN %i for %qE", snode.m_id, m_decl);
671 :
672 247895 : for (auto in_edge : snode.m_preds)
673 : {
674 63025 : if (logger)
675 8 : logger->log ("considering edge from SN %i", in_edge->m_src->m_id);
676 :
677 63025 : bool fully_overwrites_decl = false;
678 63025 : if (auto op = in_edge->get_op ())
679 : {
680 : /* This is somewhat equivalent to how the SSA case handles
681 : def-stmts. */
682 49809 : if (auto stmt = op->maybe_get_stmt ())
683 47579 : if (fully_overwrites_p (stmt, m_decl, model)
684 : /* ...but we mustn't be at a point that also consumes the
685 : current value of the decl when it's generating the new
686 : value, for cases such as
687 : struct st s;
688 : s = foo ();
689 : s = bar (s);
690 : where we want to make sure that we don't stop at the:
691 : s = bar (s);
692 : since otherwise we would erroneously purge the state of "s"
693 : after:
694 : s = foo ();
695 : */
696 47579 : && !m_snodes_needing_decl.contains (&snode))
697 0 : fully_overwrites_decl = true;
698 : }
699 47579 : if (fully_overwrites_decl)
700 : {
701 0 : if (logger)
702 0 : logger->log ("op fully overwrites %qE; terminating", m_decl);
703 : }
704 : else
705 63025 : add_to_worklist (*in_edge->m_src, worklist, seen, logger);
706 : }
707 64014 : }
708 :
709 : /* Process SNODE, popped from *WORKLIST.
710 : Iterate over successors of SNODE, adding to *WORKLIST and *SEEN. */
711 :
712 : void
713 58452 : state_purge_per_decl::
714 : process_supernode_forwards (const supernode &snode,
715 : auto_vec<const supernode *> *worklist,
716 : point_set_t *seen,
717 : const state_purge_map &map)
718 : {
719 58452 : logger *logger = map.get_logger ();
720 58452 : LOG_FUNC (logger);
721 58452 : if (logger)
722 10 : logger->log ("considering SN %i for %qE", snode.m_id, m_decl);
723 :
724 227805 : for (auto out_edge : snode.m_succs)
725 58689 : add_to_worklist (*out_edge->m_dest, worklist, seen, logger);
726 58452 : }
727 :
728 : /* Return true if the decl is needed at SNODE. */
729 :
730 : bool
731 102050 : state_purge_per_decl::needed_at_supernode_p (const supernode *snode) const
732 : {
733 102050 : return const_cast <point_set_t &> (m_snodes_needing_decl).contains (snode);
734 : }
735 :
736 : void
737 0 : state_purge_per_decl::on_duplicated_node (const supernode &old_snode,
738 : const supernode &new_snode)
739 : {
740 0 : if (m_snodes_needing_decl.contains (&old_snode))
741 0 : m_snodes_needing_decl.add (&new_snode);
742 0 : if (m_snodes_taking_address.contains (&old_snode))
743 0 : m_snodes_taking_address.add (&new_snode);
744 0 : }
745 : /* class state_purge_annotator : public dot_annotator. */
746 :
747 : /* Implementation of dot_annotator::add_node_annotations vfunc for
748 : state_purge_annotator.
749 :
750 : Add an additional record showing which names are purged on entry
751 : to the supernode N. */
752 :
753 : /* Print V to GV as a comma-separated list in braces within a <TR>,
754 : titling it with TITLE.
755 :
756 : Subroutine of state_purge_annotator::print_needed. */
757 :
758 : static void
759 382 : print_vec_of_names (graphviz_out *gv, const char *title,
760 : const auto_vec<tree> &v)
761 : {
762 382 : pretty_printer *pp = gv->get_pp ();
763 382 : tree name;
764 382 : unsigned i;
765 382 : gv->begin_trtd ();
766 382 : pp_printf (pp, "%s: {", title);
767 3134 : FOR_EACH_VEC_ELT (v, i, name)
768 : {
769 2370 : if (i > 0)
770 2030 : pp_string (pp, ", ");
771 2370 : pp_printf (pp, "%qE", name);
772 : }
773 382 : pp_printf (pp, "}");
774 382 : pp_write_text_as_html_like_dot_to_stream (pp);
775 382 : gv->end_tdtr ();
776 382 : pp_newline (pp);
777 382 : }
778 :
779 : void
780 191 : state_purge_annotator::add_node_annotations (graphviz_out *gv,
781 : const supernode &snode) const
782 : {
783 191 : if (m_map == nullptr)
784 0 : return;
785 :
786 191 : auto_vec<tree> needed;
787 191 : auto_vec<tree> not_needed;
788 191 : for (state_purge_map::ssa_iterator iter = m_map->begin_ssas ();
789 9764 : iter != m_map->end_ssas ();
790 4691 : ++iter)
791 : {
792 4691 : tree name = (*iter).first;
793 4691 : state_purge_per_ssa_name *per_name_data = (*iter).second;
794 4691 : if (&per_name_data->get_function () == snode.get_function ())
795 : {
796 2370 : if (per_name_data->needed_at_supernode_p (&snode))
797 509 : needed.safe_push (name);
798 : else
799 1861 : not_needed.safe_push (name);
800 : }
801 : }
802 191 : for (state_purge_map::decl_iterator iter = m_map->begin_decls ();
803 191 : iter != m_map->end_decls ();
804 0 : ++iter)
805 : {
806 0 : tree decl = (*iter).first;
807 0 : state_purge_per_decl *per_decl_data = (*iter).second;
808 0 : if (&per_decl_data->get_function () == snode.get_function ())
809 : {
810 0 : if (per_decl_data->needed_at_supernode_p (&snode))
811 0 : needed.safe_push (decl);
812 : else
813 0 : not_needed.safe_push (decl);
814 : }
815 : }
816 :
817 191 : print_vec_of_names (gv, "needed here", needed);
818 191 : print_vec_of_names (gv, "not needed here", not_needed);
819 191 : }
820 :
821 : #endif /* #if ENABLE_ANALYZER */
|