2005-04-29 Jim Tison <jtison@us.ibm.com>
[official-gcc.git] / gcc / tree-mudflap.c
blobb28a427c82b389b588610826f27415ae70a27fe7
1 /* Mudflap: narrow-pointer bounds-checking by tree rewriting.
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Frank Ch. Eigler <fche@redhat.com>
4 and Graydon Hoare <graydon@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
24 #include "config.h"
25 #include "errors.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "hard-reg-set.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "tm_p.h"
33 #include "basic-block.h"
34 #include "flags.h"
35 #include "function.h"
36 #include "tree-inline.h"
37 #include "tree-gimple.h"
38 #include "tree-flow.h"
39 #include "tree-mudflap.h"
40 #include "tree-dump.h"
41 #include "tree-pass.h"
42 #include "hashtab.h"
43 #include "diagnostic.h"
44 #include <demangle.h>
45 #include "langhooks.h"
46 #include "ggc.h"
47 #include "cgraph.h"
49 /* Internal function decls */
51 /* Helpers. */
52 static tree mf_build_string (const char *string);
53 static tree mf_varname_tree (tree);
54 static tree mf_file_function_line_tree (location_t);
56 /* Indirection-related instrumentation. */
57 static void mf_decl_cache_locals (void);
58 static void mf_decl_clear_locals (void);
59 static void mf_xform_derefs (void);
60 static void execute_mudflap_function_ops (void);
62 /* Addressable variables instrumentation. */
63 static void mf_xform_decls (tree, tree);
64 static tree mx_xfn_xform_decls (tree *, int *, void *);
65 static void mx_register_decls (tree, tree *);
66 static void execute_mudflap_function_decls (void);
69 /* ------------------------------------------------------------------------ */
70 /* Some generally helpful functions for mudflap instrumentation. */
72 /* Build a reference to a literal string. */
73 static tree
74 mf_build_string (const char *string)
76 size_t len = strlen (string);
77 tree result = mf_mark (build_string (len + 1, string));
79 TREE_TYPE (result) = build_array_type
80 (char_type_node, build_index_type (build_int_cst (NULL_TREE, len)));
81 TREE_CONSTANT (result) = 1;
82 TREE_INVARIANT (result) = 1;
83 TREE_READONLY (result) = 1;
84 TREE_STATIC (result) = 1;
86 result = build1 (ADDR_EXPR, build_pointer_type (char_type_node), result);
88 return mf_mark (result);
91 /* Create a properly typed STRING_CST node that describes the given
92 declaration. It will be used as an argument for __mf_register().
93 Try to construct a helpful string, including file/function/variable
94 name. */
96 static tree
97 mf_varname_tree (tree decl)
99 static pretty_printer buf_rec;
100 static int initialized = 0;
101 pretty_printer *buf = & buf_rec;
102 const char *buf_contents;
103 tree result;
105 gcc_assert (decl);
107 if (!initialized)
109 pp_construct (buf, /* prefix */ NULL, /* line-width */ 0);
110 initialized = 1;
112 pp_clear_output_area (buf);
114 /* Add FILENAME[:LINENUMBER[:COLUMNNUMBER]]. */
116 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (decl));
117 const char *sourcefile;
118 unsigned sourceline = xloc.line;
119 unsigned sourcecolumn = 0;
120 #ifdef USE_MAPPED_LOCATION
121 sourcecolumn = xloc.column;
122 #endif
123 sourcefile = xloc.file;
124 if (sourcefile == NULL && current_function_decl != NULL_TREE)
125 sourcefile = DECL_SOURCE_FILE (current_function_decl);
126 if (sourcefile == NULL)
127 sourcefile = "<unknown file>";
129 pp_string (buf, sourcefile);
131 if (sourceline != 0)
133 pp_string (buf, ":");
134 pp_decimal_int (buf, sourceline);
136 if (sourcecolumn != 0)
138 pp_string (buf, ":");
139 pp_decimal_int (buf, sourcecolumn);
144 if (current_function_decl != NULL_TREE)
146 /* Add (FUNCTION) */
147 pp_string (buf, " (");
149 const char *funcname = NULL;
150 if (DECL_NAME (current_function_decl))
151 funcname = lang_hooks.decl_printable_name (current_function_decl, 1);
152 if (funcname == NULL)
153 funcname = "anonymous fn";
155 pp_string (buf, funcname);
157 pp_string (buf, ") ");
159 else
160 pp_string (buf, " ");
162 /* Add <variable-declaration>, possibly demangled. */
164 const char *declname = NULL;
166 if (DECL_NAME (decl) != NULL)
168 if (strcmp ("GNU C++", lang_hooks.name) == 0)
170 /* The gcc/cp decl_printable_name hook doesn't do as good a job as
171 the libiberty demangler. */
172 declname = cplus_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)),
173 DMGL_AUTO | DMGL_VERBOSE);
175 if (declname == NULL)
176 declname = lang_hooks.decl_printable_name (decl, 3);
178 if (declname == NULL)
179 declname = "<unnamed variable>";
181 pp_string (buf, declname);
184 /* Return the lot as a new STRING_CST. */
185 buf_contents = pp_base_formatted_text (buf);
186 result = mf_build_string (buf_contents);
187 pp_clear_output_area (buf);
189 return result;
193 /* And another friend, for producing a simpler message. */
195 static tree
196 mf_file_function_line_tree (location_t location)
198 expanded_location xloc = expand_location (location);
199 const char *file = NULL, *colon, *line, *op, *name, *cp;
200 char linecolbuf[30]; /* Enough for two decimal numbers plus a colon. */
201 char *string;
202 tree result;
204 /* Add FILENAME[:LINENUMBER[:COLUMNNUMBER]]. */
205 file = xloc.file;
206 if (file == NULL && current_function_decl != NULL_TREE)
207 file = DECL_SOURCE_FILE (current_function_decl);
208 if (file == NULL)
209 file = "<unknown file>";
211 if (xloc.line > 0)
213 #ifdef USE_MAPPED_LOCATION
214 if (xloc.column > 0)
215 sprintf (linecolbuf, "%d:%d", xloc.line, xloc.column);
216 else
217 #endif
218 sprintf (linecolbuf, "%d", xloc.line);
219 colon = ":";
220 line = linecolbuf;
222 else
223 colon = line = "";
225 /* Add (FUNCTION). */
226 name = lang_hooks.decl_printable_name (current_function_decl, 1);
227 if (name)
229 op = " (";
230 cp = ")";
232 else
233 op = name = cp = "";
235 string = concat (file, colon, line, op, name, cp, NULL);
236 result = mf_build_string (string);
237 free (string);
239 return result;
243 /* global tree nodes */
245 /* Global tree objects for global variables and functions exported by
246 mudflap runtime library. mf_init_extern_trees must be called
247 before using these. */
249 /* uintptr_t (usually "unsigned long") */
250 static GTY (()) tree mf_uintptr_type;
252 /* struct __mf_cache { uintptr_t low; uintptr_t high; }; */
253 static GTY (()) tree mf_cache_struct_type;
255 /* struct __mf_cache * const */
256 static GTY (()) tree mf_cache_structptr_type;
258 /* extern struct __mf_cache __mf_lookup_cache []; */
259 static GTY (()) tree mf_cache_array_decl;
261 /* extern unsigned char __mf_lc_shift; */
262 static GTY (()) tree mf_cache_shift_decl;
264 /* extern uintptr_t __mf_lc_mask; */
265 static GTY (()) tree mf_cache_mask_decl;
267 /* Their function-scope local shadows, used in single-threaded mode only. */
269 /* auto const unsigned char __mf_lc_shift_l; */
270 static GTY (()) tree mf_cache_shift_decl_l;
272 /* auto const uintptr_t __mf_lc_mask_l; */
273 static GTY (()) tree mf_cache_mask_decl_l;
275 /* extern void __mf_check (void *ptr, size_t sz, int type, const char *); */
276 static GTY (()) tree mf_check_fndecl;
278 /* extern void __mf_register (void *ptr, size_t sz, int type, const char *); */
279 static GTY (()) tree mf_register_fndecl;
281 /* extern void __mf_unregister (void *ptr, size_t sz, int type); */
282 static GTY (()) tree mf_unregister_fndecl;
284 /* extern void __mf_init (); */
285 static GTY (()) tree mf_init_fndecl;
287 /* extern int __mf_set_options (const char*); */
288 static GTY (()) tree mf_set_options_fndecl;
291 /* Helper for mudflap_init: construct a decl with the given category,
292 name, and type, mark it an external reference, and pushdecl it. */
293 static inline tree
294 mf_make_builtin (enum tree_code category, const char *name, tree type)
296 tree decl = mf_mark (build_decl (category, get_identifier (name), type));
297 TREE_PUBLIC (decl) = 1;
298 DECL_EXTERNAL (decl) = 1;
299 lang_hooks.decls.pushdecl (decl);
300 return decl;
303 /* Helper for mudflap_init: construct a tree corresponding to the type
304 struct __mf_cache { uintptr_t low; uintptr_t high; };
305 where uintptr_t is the FIELD_TYPE argument. */
306 static inline tree
307 mf_make_mf_cache_struct_type (tree field_type)
309 /* There is, abominably, no language-independent way to construct a
310 RECORD_TYPE. So we have to call the basic type construction
311 primitives by hand. */
312 tree fieldlo = build_decl (FIELD_DECL, get_identifier ("low"), field_type);
313 tree fieldhi = build_decl (FIELD_DECL, get_identifier ("high"), field_type);
315 tree struct_type = make_node (RECORD_TYPE);
316 DECL_CONTEXT (fieldlo) = struct_type;
317 DECL_CONTEXT (fieldhi) = struct_type;
318 TREE_CHAIN (fieldlo) = fieldhi;
319 TYPE_FIELDS (struct_type) = fieldlo;
320 TYPE_NAME (struct_type) = get_identifier ("__mf_cache");
321 layout_type (struct_type);
323 return struct_type;
326 #define build_function_type_0(rtype) \
327 build_function_type (rtype, void_list_node)
328 #define build_function_type_1(rtype, arg1) \
329 build_function_type (rtype, tree_cons (0, arg1, void_list_node))
330 #define build_function_type_3(rtype, arg1, arg2, arg3) \
331 build_function_type (rtype, tree_cons (0, arg1, tree_cons (0, arg2, \
332 tree_cons (0, arg3, void_list_node))))
333 #define build_function_type_4(rtype, arg1, arg2, arg3, arg4) \
334 build_function_type (rtype, tree_cons (0, arg1, tree_cons (0, arg2, \
335 tree_cons (0, arg3, tree_cons (0, arg4, \
336 void_list_node)))))
338 /* Initialize the global tree nodes that correspond to mf-runtime.h
339 declarations. */
340 void
341 mudflap_init (void)
343 static bool done = false;
344 tree mf_const_string_type;
345 tree mf_cache_array_type;
346 tree mf_check_register_fntype;
347 tree mf_unregister_fntype;
348 tree mf_init_fntype;
349 tree mf_set_options_fntype;
351 if (done)
352 return;
353 done = true;
355 mf_uintptr_type = lang_hooks.types.type_for_mode (ptr_mode,
356 /*unsignedp=*/true);
357 mf_const_string_type
358 = build_pointer_type (build_qualified_type
359 (char_type_node, TYPE_QUAL_CONST));
361 mf_cache_struct_type = mf_make_mf_cache_struct_type (mf_uintptr_type);
362 mf_cache_structptr_type = build_pointer_type (mf_cache_struct_type);
363 mf_cache_array_type = build_array_type (mf_cache_struct_type, 0);
364 mf_check_register_fntype =
365 build_function_type_4 (void_type_node, ptr_type_node, size_type_node,
366 integer_type_node, mf_const_string_type);
367 mf_unregister_fntype =
368 build_function_type_3 (void_type_node, ptr_type_node, size_type_node,
369 integer_type_node);
370 mf_init_fntype =
371 build_function_type_0 (void_type_node);
372 mf_set_options_fntype =
373 build_function_type_1 (integer_type_node, mf_const_string_type);
375 mf_cache_array_decl = mf_make_builtin (VAR_DECL, "__mf_lookup_cache",
376 mf_cache_array_type);
377 mf_cache_shift_decl = mf_make_builtin (VAR_DECL, "__mf_lc_shift",
378 unsigned_char_type_node);
379 mf_cache_mask_decl = mf_make_builtin (VAR_DECL, "__mf_lc_mask",
380 mf_uintptr_type);
381 /* Don't process these in mudflap_enqueue_decl, should they come by
382 there for some reason. */
383 mf_mark (mf_cache_array_decl);
384 mf_mark (mf_cache_shift_decl);
385 mf_mark (mf_cache_mask_decl);
386 mf_check_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_check",
387 mf_check_register_fntype);
388 mf_register_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_register",
389 mf_check_register_fntype);
390 mf_unregister_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_unregister",
391 mf_unregister_fntype);
392 mf_init_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_init",
393 mf_init_fntype);
394 mf_set_options_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_set_options",
395 mf_set_options_fntype);
397 #undef build_function_type_4
398 #undef build_function_type_3
399 #undef build_function_type_1
400 #undef build_function_type_0
403 /* ------------------------------------------------------------------------ */
404 /* Memory reference transforms. Perform the mudflap indirection-related
405 tree transforms on the current function.
407 This is the second part of the mudflap instrumentation. It works on
408 low-level GIMPLE using the CFG, because we want to run this pass after
409 tree optimizations have been performed, but we have to preserve the CFG
410 for expansion from trees to RTL. */
412 static void
413 execute_mudflap_function_ops (void)
415 /* Don't instrument functions such as the synthetic constructor
416 built during mudflap_finish_file. */
417 if (mf_marked_p (current_function_decl) ||
418 DECL_ARTIFICIAL (current_function_decl))
419 return;
421 push_gimplify_context ();
423 /* In multithreaded mode, don't cache the lookup cache parameters. */
424 if (! flag_mudflap_threads)
425 mf_decl_cache_locals ();
427 mf_xform_derefs ();
429 if (! flag_mudflap_threads)
430 mf_decl_clear_locals ();
432 pop_gimplify_context (NULL);
435 /* Create and initialize local shadow variables for the lookup cache
436 globals. Put their decls in the *_l globals for use by
437 mf_build_check_statement_for. */
439 static void
440 mf_decl_cache_locals (void)
442 tree t, shift_init_stmts, mask_init_stmts;
443 tree_stmt_iterator tsi;
445 /* Build the cache vars. */
446 mf_cache_shift_decl_l
447 = mf_mark (create_tmp_var (TREE_TYPE (mf_cache_shift_decl),
448 "__mf_lookup_shift_l"));
450 mf_cache_mask_decl_l
451 = mf_mark (create_tmp_var (TREE_TYPE (mf_cache_mask_decl),
452 "__mf_lookup_mask_l"));
454 /* Build initialization nodes for the cache vars. We just load the
455 globals into the cache variables. */
456 t = build (MODIFY_EXPR, TREE_TYPE (mf_cache_shift_decl_l),
457 mf_cache_shift_decl_l, mf_cache_shift_decl);
458 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (current_function_decl));
459 gimplify_to_stmt_list (&t);
460 shift_init_stmts = t;
462 t = build (MODIFY_EXPR, TREE_TYPE (mf_cache_mask_decl_l),
463 mf_cache_mask_decl_l, mf_cache_mask_decl);
464 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (current_function_decl));
465 gimplify_to_stmt_list (&t);
466 mask_init_stmts = t;
468 /* Anticipating multiple entry points, we insert the cache vars
469 initializers in each successor of the ENTRY_BLOCK_PTR. */
470 for (tsi = tsi_start (shift_init_stmts);
471 ! tsi_end_p (tsi);
472 tsi_next (&tsi))
473 insert_edge_copies (tsi_stmt (tsi), ENTRY_BLOCK_PTR);
475 for (tsi = tsi_start (mask_init_stmts);
476 ! tsi_end_p (tsi);
477 tsi_next (&tsi))
478 insert_edge_copies (tsi_stmt (tsi), ENTRY_BLOCK_PTR);
479 bsi_commit_edge_inserts ();
483 static void
484 mf_decl_clear_locals (void)
486 /* Unset local shadows. */
487 mf_cache_shift_decl_l = NULL_TREE;
488 mf_cache_mask_decl_l = NULL_TREE;
491 static void
492 mf_build_check_statement_for (tree base, tree limit,
493 block_stmt_iterator *instr_bsi,
494 location_t *locus, tree dirflag)
496 tree_stmt_iterator head, tsi;
497 block_stmt_iterator bsi;
498 basic_block cond_bb, then_bb, join_bb;
499 edge e;
500 tree cond, t, u, v;
501 tree mf_base;
502 tree mf_elem;
503 tree mf_limit;
505 /* We first need to split the current basic block, and start altering
506 the CFG. This allows us to insert the statements we're about to
507 construct into the right basic blocks. */
509 cond_bb = bb_for_stmt (bsi_stmt (*instr_bsi));
510 bsi = *instr_bsi;
511 bsi_prev (&bsi);
512 if (! bsi_end_p (bsi))
513 e = split_block (cond_bb, bsi_stmt (bsi));
514 else
515 e = split_block_after_labels (cond_bb);
516 cond_bb = e->src;
517 join_bb = e->dest;
519 /* A recap at this point: join_bb is the basic block at whose head
520 is the gimple statement for which this check expression is being
521 built. cond_bb is the (possibly new, synthetic) basic block the
522 end of which will contain the cache-lookup code, and a
523 conditional that jumps to the cache-miss code or, much more
524 likely, over to join_bb. */
526 /* Create the bb that contains the cache-miss fallback block (mf_check). */
527 then_bb = create_empty_bb (cond_bb);
528 make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
529 make_single_succ_edge (then_bb, join_bb, EDGE_FALLTHRU);
531 /* We expect that the conditional jump we will construct will not
532 be taken very often as it basically is an exception condition. */
533 predict_edge_def (single_pred_edge (then_bb), PRED_MUDFLAP, NOT_TAKEN);
535 /* Mark the pseudo-fallthrough edge from cond_bb to join_bb. */
536 e = find_edge (cond_bb, join_bb);
537 e->flags = EDGE_FALSE_VALUE;
538 predict_edge_def (e, PRED_MUDFLAP, TAKEN);
540 /* Update dominance info. Note that bb_join's data was
541 updated by split_block. */
542 if (dom_info_available_p (CDI_DOMINATORS))
544 set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
545 set_immediate_dominator (CDI_DOMINATORS, join_bb, cond_bb);
548 /* Build our local variables. */
549 mf_elem = create_tmp_var (mf_cache_structptr_type, "__mf_elem");
550 mf_base = create_tmp_var (mf_uintptr_type, "__mf_base");
551 mf_limit = create_tmp_var (mf_uintptr_type, "__mf_limit");
553 /* Build: __mf_base = (uintptr_t) <base address expression>. */
554 t = build (MODIFY_EXPR, void_type_node, mf_base,
555 convert (mf_uintptr_type, unshare_expr (base)));
556 SET_EXPR_LOCUS (t, locus);
557 gimplify_to_stmt_list (&t);
558 head = tsi_start (t);
559 tsi = tsi_last (t);
561 /* Build: __mf_limit = (uintptr_t) <limit address expression>. */
562 t = build (MODIFY_EXPR, void_type_node, mf_limit,
563 convert (mf_uintptr_type, unshare_expr (limit)));
564 SET_EXPR_LOCUS (t, locus);
565 gimplify_to_stmt_list (&t);
566 tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
568 /* Build: __mf_elem = &__mf_lookup_cache [(__mf_base >> __mf_shift)
569 & __mf_mask]. */
570 t = build (RSHIFT_EXPR, mf_uintptr_type, mf_base,
571 (flag_mudflap_threads ? mf_cache_shift_decl : mf_cache_shift_decl_l));
572 t = build (BIT_AND_EXPR, mf_uintptr_type, t,
573 (flag_mudflap_threads ? mf_cache_mask_decl : mf_cache_mask_decl_l));
574 t = build (ARRAY_REF,
575 TREE_TYPE (TREE_TYPE (mf_cache_array_decl)),
576 mf_cache_array_decl, t, NULL_TREE, NULL_TREE);
577 t = build1 (ADDR_EXPR, mf_cache_structptr_type, t);
578 t = build (MODIFY_EXPR, void_type_node, mf_elem, t);
579 SET_EXPR_LOCUS (t, locus);
580 gimplify_to_stmt_list (&t);
581 tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
583 /* Quick validity check.
585 if (__mf_elem->low > __mf_base
586 || (__mf_elem_high < __mf_limit))
588 __mf_check ();
589 ... and only if single-threaded:
590 __mf_lookup_shift_1 = f...;
591 __mf_lookup_mask_l = ...;
594 It is expected that this body of code is rarely executed so we mark
595 the edge to the THEN clause of the conditional jump as unlikely. */
597 /* Construct t <-- '__mf_elem->low > __mf_base'. */
598 t = build (COMPONENT_REF, mf_uintptr_type,
599 build1 (INDIRECT_REF, mf_cache_struct_type, mf_elem),
600 TYPE_FIELDS (mf_cache_struct_type), NULL_TREE);
601 t = build (GT_EXPR, boolean_type_node, t, mf_base);
603 /* Construct '__mf_elem->high < __mf_limit'.
605 First build:
606 1) u <-- '__mf_elem->high'
607 2) v <-- '__mf_limit'.
609 Then build 'u <-- (u < v). */
611 u = build (COMPONENT_REF, mf_uintptr_type,
612 build1 (INDIRECT_REF, mf_cache_struct_type, mf_elem),
613 TREE_CHAIN (TYPE_FIELDS (mf_cache_struct_type)), NULL_TREE);
615 v = mf_limit;
617 u = build (LT_EXPR, boolean_type_node, u, v);
619 /* Build the composed conditional: t <-- 't || u'. Then store the
620 result of the evaluation of 't' in a temporary variable which we
621 can use as the condition for the conditional jump. */
622 t = build (TRUTH_OR_EXPR, boolean_type_node, t, u);
623 cond = create_tmp_var (boolean_type_node, "__mf_unlikely_cond");
624 t = build (MODIFY_EXPR, boolean_type_node, cond, t);
625 gimplify_to_stmt_list (&t);
626 tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
628 /* Build the conditional jump. 'cond' is just a temporary so we can
629 simply build a void COND_EXPR. We do need labels in both arms though. */
630 t = build (COND_EXPR, void_type_node, cond,
631 build (GOTO_EXPR, void_type_node, tree_block_label (then_bb)),
632 build (GOTO_EXPR, void_type_node, tree_block_label (join_bb)));
633 SET_EXPR_LOCUS (t, locus);
634 tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
636 /* At this point, after so much hard work, we have only constructed
637 the conditional jump,
639 if (__mf_elem->low > __mf_base
640 || (__mf_elem_high < __mf_limit))
642 The lowered GIMPLE tree representing this code is in the statement
643 list starting at 'head'.
645 We can insert this now in the current basic block, i.e. the one that
646 the statement we're instrumenting was originally in. */
647 bsi = bsi_last (cond_bb);
648 for (tsi = head; ! tsi_end_p (tsi); tsi_next (&tsi))
649 bsi_insert_after (&bsi, tsi_stmt (tsi), BSI_CONTINUE_LINKING);
651 /* Now build up the body of the cache-miss handling:
653 __mf_check();
654 refresh *_l vars.
656 This is the body of the conditional. */
658 u = tree_cons (NULL_TREE,
659 mf_file_function_line_tree (locus == NULL ? UNKNOWN_LOCATION
660 : *locus),
661 NULL_TREE);
662 u = tree_cons (NULL_TREE, dirflag, u);
663 /* NB: we pass the overall [base..limit] range to mf_check. */
664 u = tree_cons (NULL_TREE,
665 fold (build (PLUS_EXPR, integer_type_node,
666 fold (build (MINUS_EXPR, mf_uintptr_type, mf_limit, mf_base)),
667 integer_one_node)),
669 u = tree_cons (NULL_TREE, mf_base, u);
670 t = build_function_call_expr (mf_check_fndecl, u);
671 gimplify_to_stmt_list (&t);
672 head = tsi_start (t);
673 tsi = tsi_last (t);
675 if (! flag_mudflap_threads)
677 t = build (MODIFY_EXPR, void_type_node,
678 mf_cache_shift_decl_l, mf_cache_shift_decl);
679 tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
681 t = build (MODIFY_EXPR, void_type_node,
682 mf_cache_mask_decl_l, mf_cache_mask_decl);
683 tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
686 /* Insert the check code in the THEN block. */
687 bsi = bsi_start (then_bb);
688 for (tsi = head; ! tsi_end_p (tsi); tsi_next (&tsi))
689 bsi_insert_after (&bsi, tsi_stmt (tsi), BSI_CONTINUE_LINKING);
691 *instr_bsi = bsi_start (join_bb);
692 bsi_next (instr_bsi);
696 /* Check whether the given decl, generally a VAR_DECL or PARM_DECL, is
697 eligible for instrumentation. For the mudflap1 pass, this implies
698 that it should be registered with the libmudflap runtime. For the
699 mudflap2 pass this means instrumenting an indirection operation with
700 respect to the object.
702 static int
703 mf_decl_eligible_p (tree decl)
705 return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
706 /* The decl must have its address taken. In the case of
707 arrays, this flag is also set if the indexes are not
708 compile-time known valid constants. */
709 && TREE_ADDRESSABLE (decl) /* XXX: not sufficient: return-by-value structs! */
710 /* The type of the variable must be complete. */
711 && COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (decl))
712 /* The decl hasn't been decomposed somehow. */
713 && DECL_VALUE_EXPR (decl) == NULL);
717 static void
718 mf_xform_derefs_1 (block_stmt_iterator *iter, tree *tp,
719 location_t *locus, tree dirflag)
721 tree type, base, limit, addr, size, t;
723 /* Don't instrument read operations. */
724 if (dirflag == integer_zero_node && flag_mudflap_ignore_reads)
725 return;
727 /* Don't instrument marked nodes. */
728 if (mf_marked_p (*tp))
729 return;
731 t = *tp;
732 type = TREE_TYPE (t);
733 size = TYPE_SIZE_UNIT (type);
735 switch (TREE_CODE (t))
737 case ARRAY_REF:
738 case COMPONENT_REF:
740 /* This is trickier than it may first appear. The reason is
741 that we are looking at expressions from the "inside out" at
742 this point. We may have a complex nested aggregate/array
743 expression (e.g. "a.b[i].c"), maybe with an indirection as
744 the leftmost operator ("p->a.b.d"), where instrumentation
745 is necessary. Or we may have an innocent "a.b.c"
746 expression that must not be instrumented. We need to
747 recurse all the way down the nesting structure to figure it
748 out: looking just at the outer node is not enough. */
749 tree var;
750 int component_ref_only = (TREE_CODE (t) == COMPONENT_REF);
751 /* If we have a bitfield component reference, we must note the
752 innermost addressable object in ELT, from which we will
753 construct the byte-addressable bounds of the bitfield. */
754 tree elt = NULL_TREE;
755 int bitfield_ref_p = (TREE_CODE (t) == COMPONENT_REF
756 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)));
758 /* Iterate to the top of the ARRAY_REF/COMPONENT_REF
759 containment hierarchy to find the outermost VAR_DECL. */
760 var = TREE_OPERAND (t, 0);
761 while (1)
763 if (bitfield_ref_p && elt == NULL_TREE
764 && (TREE_CODE (var) == ARRAY_REF || TREE_CODE (var) == COMPONENT_REF))
765 elt = var;
767 if (TREE_CODE (var) == ARRAY_REF)
769 component_ref_only = 0;
770 var = TREE_OPERAND (var, 0);
772 else if (TREE_CODE (var) == COMPONENT_REF)
773 var = TREE_OPERAND (var, 0);
774 else if (INDIRECT_REF_P (var))
776 base = TREE_OPERAND (var, 0);
777 break;
779 else
781 gcc_assert (TREE_CODE (var) == VAR_DECL
782 || TREE_CODE (var) == PARM_DECL
783 || TREE_CODE (var) == RESULT_DECL
784 || TREE_CODE (var) == STRING_CST);
785 /* Don't instrument this access if the underlying
786 variable is not "eligible". This test matches
787 those arrays that have only known-valid indexes,
788 and thus are not labeled TREE_ADDRESSABLE. */
789 if (! mf_decl_eligible_p (var) || component_ref_only)
790 return;
791 else
793 base = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (var)), var);
794 break;
799 /* Handle the case of ordinary non-indirection structure
800 accesses. These have only nested COMPONENT_REF nodes (no
801 INDIRECT_REF), but pass through the above filter loop.
802 Note that it's possible for such a struct variable to match
803 the eligible_p test because someone else might take its
804 address sometime. */
806 /* We need special processing for bitfield components, because
807 their addresses cannot be taken. */
808 if (bitfield_ref_p)
810 tree field = TREE_OPERAND (t, 1);
812 if (TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST)
813 size = DECL_SIZE_UNIT (field);
815 if (elt)
816 elt = build1 (ADDR_EXPR, build_pointer_type TREE_TYPE (elt), elt);
817 addr = fold_convert (ptr_type_node, elt ? elt : base);
818 addr = fold (build (PLUS_EXPR, ptr_type_node,
819 addr, fold_convert (ptr_type_node,
820 byte_position (field))));
822 else
823 addr = build1 (ADDR_EXPR, build_pointer_type (type), t);
825 limit = fold (build (MINUS_EXPR, mf_uintptr_type,
826 fold (build2 (PLUS_EXPR, mf_uintptr_type,
827 convert (mf_uintptr_type, addr),
828 size)),
829 integer_one_node));
831 break;
833 case INDIRECT_REF:
834 addr = TREE_OPERAND (t, 0);
835 base = addr;
836 limit = fold (build (MINUS_EXPR, ptr_type_node,
837 fold (build (PLUS_EXPR, ptr_type_node, base, size)),
838 integer_one_node));
839 break;
841 case ARRAY_RANGE_REF:
842 warning (0, "mudflap checking not yet implemented for ARRAY_RANGE_REF");
843 return;
845 case BIT_FIELD_REF:
846 /* ??? merge with COMPONENT_REF code above? */
848 tree ofs, rem, bpu;
850 /* If we're not dereferencing something, then the access
851 must be ok. */
852 if (TREE_CODE (TREE_OPERAND (t, 0)) != INDIRECT_REF)
853 return;
855 bpu = bitsize_int (BITS_PER_UNIT);
856 ofs = convert (bitsizetype, TREE_OPERAND (t, 2));
857 rem = size_binop (TRUNC_MOD_EXPR, ofs, bpu);
858 ofs = size_binop (TRUNC_DIV_EXPR, ofs, bpu);
860 size = convert (bitsizetype, TREE_OPERAND (t, 1));
861 size = size_binop (PLUS_EXPR, size, rem);
862 size = size_binop (CEIL_DIV_EXPR, size, bpu);
863 size = convert (sizetype, size);
865 addr = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
866 addr = convert (ptr_type_node, addr);
867 addr = fold (build (PLUS_EXPR, ptr_type_node, addr, ofs));
869 base = addr;
870 limit = fold (build (MINUS_EXPR, ptr_type_node,
871 fold (build (PLUS_EXPR, ptr_type_node, base, size)),
872 integer_one_node));
874 break;
876 default:
877 return;
880 mf_build_check_statement_for (base, limit, iter, locus, dirflag);
883 static void
884 mf_xform_derefs (void)
886 basic_block bb, next;
887 block_stmt_iterator i;
888 int saved_last_basic_block = last_basic_block;
890 bb = ENTRY_BLOCK_PTR ->next_bb;
893 next = bb->next_bb;
894 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
896 tree s = bsi_stmt (i);
898 /* Only a few GIMPLE statements can reference memory. */
899 switch (TREE_CODE (s))
901 case MODIFY_EXPR:
902 mf_xform_derefs_1 (&i, &TREE_OPERAND (s, 0), EXPR_LOCUS (s),
903 integer_one_node);
904 mf_xform_derefs_1 (&i, &TREE_OPERAND (s, 1), EXPR_LOCUS (s),
905 integer_zero_node);
906 break;
908 case RETURN_EXPR:
909 if (TREE_OPERAND (s, 0) != NULL_TREE)
911 if (TREE_CODE (TREE_OPERAND (s, 0)) == MODIFY_EXPR)
912 mf_xform_derefs_1 (&i, &TREE_OPERAND (TREE_OPERAND (s, 0), 1),
913 EXPR_LOCUS (s), integer_zero_node);
914 else
915 mf_xform_derefs_1 (&i, &TREE_OPERAND (s, 0), EXPR_LOCUS (s),
916 integer_zero_node);
918 break;
920 default:
924 bb = next;
926 while (bb && bb->index <= saved_last_basic_block);
929 /* ------------------------------------------------------------------------ */
930 /* ADDR_EXPR transforms. Perform the declaration-related mudflap tree
931 transforms on the current function.
933 This is the first part of the mudflap instrumentation. It works on
934 high-level GIMPLE because after lowering, all variables are moved out
935 of their BIND_EXPR binding context, and we lose liveness information
936 for the declarations we wish to instrument. */
938 static void
939 execute_mudflap_function_decls (void)
941 /* Don't instrument functions such as the synthetic constructor
942 built during mudflap_finish_file. */
943 if (mf_marked_p (current_function_decl) ||
944 DECL_ARTIFICIAL (current_function_decl))
945 return;
947 push_gimplify_context ();
949 mf_xform_decls (DECL_SAVED_TREE (current_function_decl),
950 DECL_ARGUMENTS (current_function_decl));
952 pop_gimplify_context (NULL);
955 /* This struct is passed between mf_xform_decls to store state needed
956 during the traversal searching for objects that have their
957 addresses taken. */
958 struct mf_xform_decls_data
960 tree param_decls;
964 /* Synthesize a CALL_EXPR and a TRY_FINALLY_EXPR, for this chain of
965 _DECLs if appropriate. Arrange to call the __mf_register function
966 now, and the __mf_unregister function later for each. */
967 static void
968 mx_register_decls (tree decl, tree *stmt_list)
970 tree finally_stmts = NULL_TREE;
971 tree_stmt_iterator initially_stmts = tsi_start (*stmt_list);
973 while (decl != NULL_TREE)
975 if (mf_decl_eligible_p (decl)
976 /* Not already processed. */
977 && ! mf_marked_p (decl)
978 /* Automatic variable. */
979 && ! DECL_EXTERNAL (decl)
980 && ! TREE_STATIC (decl))
982 tree size = NULL_TREE, variable_name;
983 tree unregister_fncall, unregister_fncall_params;
984 tree register_fncall, register_fncall_params;
986 size = convert (size_type_node, TYPE_SIZE_UNIT (TREE_TYPE (decl)));
988 /* (& VARIABLE, sizeof (VARIABLE), __MF_TYPE_STACK) */
989 unregister_fncall_params =
990 tree_cons (NULL_TREE,
991 convert (ptr_type_node,
992 mf_mark (build1 (ADDR_EXPR,
993 build_pointer_type (TREE_TYPE (decl)),
994 decl))),
995 tree_cons (NULL_TREE,
996 size,
997 tree_cons (NULL_TREE,
998 /* __MF_TYPE_STACK */
999 build_int_cst (NULL_TREE, 3),
1000 NULL_TREE)));
1001 /* __mf_unregister (...) */
1002 unregister_fncall = build_function_call_expr (mf_unregister_fndecl,
1003 unregister_fncall_params);
1005 /* (& VARIABLE, sizeof (VARIABLE), __MF_TYPE_STACK, "name") */
1006 variable_name = mf_varname_tree (decl);
1007 register_fncall_params =
1008 tree_cons (NULL_TREE,
1009 convert (ptr_type_node,
1010 mf_mark (build1 (ADDR_EXPR,
1011 build_pointer_type (TREE_TYPE (decl)),
1012 decl))),
1013 tree_cons (NULL_TREE,
1014 size,
1015 tree_cons (NULL_TREE,
1016 /* __MF_TYPE_STACK */
1017 build_int_cst (NULL_TREE, 3),
1018 tree_cons (NULL_TREE,
1019 variable_name,
1020 NULL_TREE))));
1022 /* __mf_register (...) */
1023 register_fncall = build_function_call_expr (mf_register_fndecl,
1024 register_fncall_params);
1026 /* Accumulate the two calls. */
1027 /* ??? Set EXPR_LOCATION. */
1028 gimplify_stmt (&register_fncall);
1029 gimplify_stmt (&unregister_fncall);
1031 /* Add the __mf_register call at the current appending point. */
1032 if (tsi_end_p (initially_stmts))
1033 warning (0, "mudflap cannot track %qs in stub function",
1034 IDENTIFIER_POINTER (DECL_NAME (decl)));
1035 else
1037 tsi_link_before (&initially_stmts, register_fncall, TSI_SAME_STMT);
1039 /* Accumulate the FINALLY piece. */
1040 append_to_statement_list (unregister_fncall, &finally_stmts);
1042 mf_mark (decl);
1045 decl = TREE_CHAIN (decl);
1048 /* Actually, (initially_stmts!=NULL) <=> (finally_stmts!=NULL) */
1049 if (finally_stmts != NULL_TREE)
1051 tree t = build (TRY_FINALLY_EXPR, void_type_node,
1052 *stmt_list, finally_stmts);
1053 *stmt_list = NULL;
1054 append_to_statement_list (t, stmt_list);
1059 /* Process every variable mentioned in BIND_EXPRs. */
1060 static tree
1061 mx_xfn_xform_decls (tree *t, int *continue_p, void *data)
1063 struct mf_xform_decls_data* d = (struct mf_xform_decls_data*) data;
1065 if (*t == NULL_TREE || *t == error_mark_node)
1067 *continue_p = 0;
1068 return NULL_TREE;
1071 *continue_p = 1;
1073 switch (TREE_CODE (*t))
1075 case BIND_EXPR:
1077 /* Process function parameters now (but only once). */
1078 mx_register_decls (d->param_decls, &BIND_EXPR_BODY (*t));
1079 d->param_decls = NULL_TREE;
1081 mx_register_decls (BIND_EXPR_VARS (*t), &BIND_EXPR_BODY (*t));
1083 break;
1085 default:
1086 break;
1089 return NULL_TREE;
1092 /* Perform the object lifetime tracking mudflap transform on the given function
1093 tree. The tree is mutated in place, with possibly copied subtree nodes.
1095 For every auto variable declared, if its address is ever taken
1096 within the function, then supply its lifetime to the mudflap
1097 runtime with the __mf_register and __mf_unregister calls.
1100 static void
1101 mf_xform_decls (tree fnbody, tree fnparams)
1103 struct mf_xform_decls_data d;
1104 d.param_decls = fnparams;
1105 walk_tree_without_duplicates (&fnbody, mx_xfn_xform_decls, &d);
1109 /* ------------------------------------------------------------------------ */
1110 /* Externally visible mudflap functions. */
1113 /* Mark and return the given tree node to prevent further mudflap
1114 transforms. */
1115 static GTY ((param_is (union tree_node))) htab_t marked_trees = NULL;
1117 tree
1118 mf_mark (tree t)
1120 void **slot;
1122 if (marked_trees == NULL)
1123 marked_trees = htab_create_ggc (31, htab_hash_pointer, htab_eq_pointer, NULL);
1125 slot = htab_find_slot (marked_trees, t, INSERT);
1126 *slot = t;
1127 return t;
1131 mf_marked_p (tree t)
1133 void *entry;
1135 if (marked_trees == NULL)
1136 return 0;
1138 entry = htab_find (marked_trees, t);
1139 return (entry != NULL);
1142 /* Remember given node as a static of some kind: global data,
1143 function-scope static, or an anonymous constant. Its assembler
1144 label is given. */
1146 /* A list of globals whose incomplete declarations we encountered.
1147 Instead of emitting the __mf_register call for them here, it's
1148 delayed until program finish time. If they're still incomplete by
1149 then, warnings are emitted. */
1151 static GTY (()) varray_type deferred_static_decls;
1153 /* A list of statements for calling __mf_register() at startup time. */
1154 static GTY (()) tree enqueued_call_stmt_chain;
1156 static void
1157 mudflap_register_call (tree obj, tree object_size, tree varname)
1159 tree arg, args, call_stmt;
1161 args = tree_cons (NULL_TREE, varname, NULL_TREE);
1163 arg = build_int_cst (NULL_TREE, 4); /* __MF_TYPE_STATIC */
1164 args = tree_cons (NULL_TREE, arg, args);
1166 arg = convert (size_type_node, object_size);
1167 args = tree_cons (NULL_TREE, arg, args);
1169 arg = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (obj)), obj);
1170 arg = convert (ptr_type_node, arg);
1171 args = tree_cons (NULL_TREE, arg, args);
1173 call_stmt = build_function_call_expr (mf_register_fndecl, args);
1175 append_to_statement_list (call_stmt, &enqueued_call_stmt_chain);
1178 void
1179 mudflap_enqueue_decl (tree obj)
1181 if (mf_marked_p (obj))
1182 return;
1184 /* We don't need to process variable decls that are internally
1185 generated extern. If we did, we'd end up with warnings for them
1186 during mudflap_finish_file (). That would confuse the user,
1187 since the text would refer to variables that don't show up in the
1188 user's source code. */
1189 if (DECL_P (obj) && DECL_EXTERNAL (obj) && DECL_ARTIFICIAL (obj))
1190 return;
1192 if (! deferred_static_decls)
1193 VARRAY_TREE_INIT (deferred_static_decls, 10, "deferred static list");
1195 VARRAY_PUSH_TREE (deferred_static_decls, obj);
1199 void
1200 mudflap_enqueue_constant (tree obj)
1202 tree object_size, varname;
1204 if (mf_marked_p (obj))
1205 return;
1207 if (TREE_CODE (obj) == STRING_CST)
1208 object_size = build_int_cst (NULL_TREE, TREE_STRING_LENGTH (obj));
1209 else
1210 object_size = size_in_bytes (TREE_TYPE (obj));
1212 if (TREE_CODE (obj) == STRING_CST)
1213 varname = mf_build_string ("string literal");
1214 else
1215 varname = mf_build_string ("constant");
1217 mudflap_register_call (obj, object_size, varname);
1221 /* Emit any file-wide instrumentation. */
1222 void
1223 mudflap_finish_file (void)
1225 tree ctor_statements = NULL_TREE;
1227 /* Insert a call to __mf_init. */
1229 tree call2_stmt = build_function_call_expr (mf_init_fndecl, NULL_TREE);
1230 append_to_statement_list (call2_stmt, &ctor_statements);
1233 /* If appropriate, call __mf_set_options to pass along read-ignore mode. */
1234 if (flag_mudflap_ignore_reads)
1236 tree arg = tree_cons (NULL_TREE,
1237 mf_build_string ("-ignore-reads"), NULL_TREE);
1238 tree call_stmt = build_function_call_expr (mf_set_options_fndecl, arg);
1239 append_to_statement_list (call_stmt, &ctor_statements);
1242 /* Process all enqueued object decls. */
1243 if (deferred_static_decls)
1245 size_t i;
1246 for (i = 0; i < VARRAY_ACTIVE_SIZE (deferred_static_decls); i++)
1248 tree obj = VARRAY_TREE (deferred_static_decls, i);
1250 gcc_assert (DECL_P (obj));
1252 if (mf_marked_p (obj))
1253 continue;
1255 /* Omit registration for static unaddressed objects. NB:
1256 Perform registration for non-static objects regardless of
1257 TREE_USED or TREE_ADDRESSABLE, because they may be used
1258 from other compilation units. */
1259 if (TREE_STATIC (obj) && ! TREE_ADDRESSABLE (obj))
1260 continue;
1262 if (! COMPLETE_TYPE_P (TREE_TYPE (obj)))
1264 warning (0, "mudflap cannot track unknown size extern %qs",
1265 IDENTIFIER_POINTER (DECL_NAME (obj)));
1266 continue;
1269 mudflap_register_call (obj,
1270 size_in_bytes (TREE_TYPE (obj)),
1271 mf_varname_tree (obj));
1274 VARRAY_CLEAR (deferred_static_decls);
1277 /* Append all the enqueued registration calls. */
1278 if (enqueued_call_stmt_chain)
1280 append_to_statement_list (enqueued_call_stmt_chain, &ctor_statements);
1281 enqueued_call_stmt_chain = NULL_TREE;
1284 cgraph_build_static_cdtor ('I', ctor_statements,
1285 MAX_RESERVED_INIT_PRIORITY-1);
1289 static bool
1290 gate_mudflap (void)
1292 return flag_mudflap != 0;
1295 struct tree_opt_pass pass_mudflap_1 =
1297 "mudflap1", /* name */
1298 gate_mudflap, /* gate */
1299 execute_mudflap_function_decls, /* execute */
1300 NULL, /* sub */
1301 NULL, /* next */
1302 0, /* static_pass_number */
1303 0, /* tv_id */
1304 PROP_gimple_any, /* properties_required */
1305 0, /* properties_provided */
1306 0, /* properties_destroyed */
1307 0, /* todo_flags_start */
1308 TODO_dump_func, /* todo_flags_finish */
1309 0 /* letter */
1312 struct tree_opt_pass pass_mudflap_2 =
1314 "mudflap2", /* name */
1315 gate_mudflap, /* gate */
1316 execute_mudflap_function_ops, /* execute */
1317 NULL, /* sub */
1318 NULL, /* next */
1319 0, /* static_pass_number */
1320 0, /* tv_id */
1321 PROP_gimple_leh, /* properties_required */
1322 0, /* properties_provided */
1323 0, /* properties_destroyed */
1324 0, /* todo_flags_start */
1325 TODO_verify_flow | TODO_verify_stmts
1326 | TODO_dump_func, /* todo_flags_finish */
1327 0 /* letter */
1330 #include "gt-tree-mudflap.h"