OSDN Git Service

* tree-mudflap.c (ml_decl_cache_locals, md_build_check_statement_for):
[pf3gnuchains/gcc-fork.git] / gcc / tree-mudflap.c
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>
5
6 This file is part of GCC.
7
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.
12
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.
17
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, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.  */
22
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "hard-reg-set.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "tm_p.h"
32 #include "basic-block.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "tree-inline.h"
36 #include "tree-gimple.h"
37 #include "tree-flow.h"
38 #include "tree-mudflap.h"
39 #include "tree-dump.h"
40 #include "tree-pass.h"
41 #include "hashtab.h"
42 #include "diagnostic.h"
43 #include <demangle.h>
44 #include "langhooks.h"
45 #include "ggc.h"
46 #include "cgraph.h"
47 #include "toplev.h"
48
49 /* Internal function decls */
50
51
52 /* Options.  */
53 #define flag_mudflap_threads (flag_mudflap == 2)
54
55 /* Helpers.  */
56 static tree mf_build_string (const char *string);
57 static tree mf_varname_tree (tree);
58 static tree mf_file_function_line_tree (location_t);
59
60 /* Indirection-related instrumentation.  */
61 static void mf_decl_cache_locals (void);
62 static void mf_decl_clear_locals (void);
63 static void mf_xform_derefs (void);
64 static unsigned int execute_mudflap_function_ops (void);
65
66 /* Addressable variables instrumentation.  */
67 static void mf_xform_decls (tree, tree);
68 static tree mx_xfn_xform_decls (tree *, int *, void *);
69 static void mx_register_decls (tree, tree *);
70 static unsigned int execute_mudflap_function_decls (void);
71
72
73 /* ------------------------------------------------------------------------ */
74 /* Some generally helpful functions for mudflap instrumentation.  */
75
76 /* Build a reference to a literal string.  */
77 static tree
78 mf_build_string (const char *string)
79 {
80   size_t len = strlen (string);
81   tree result = mf_mark (build_string (len + 1, string));
82
83   TREE_TYPE (result) = build_array_type
84     (char_type_node, build_index_type (build_int_cst (NULL_TREE, len)));
85   TREE_CONSTANT (result) = 1;
86   TREE_INVARIANT (result) = 1;
87   TREE_READONLY (result) = 1;
88   TREE_STATIC (result) = 1;
89
90   result = build1 (ADDR_EXPR, build_pointer_type (char_type_node), result);
91
92   return mf_mark (result);
93 }
94
95 /* Create a properly typed STRING_CST node that describes the given
96    declaration.  It will be used as an argument for __mf_register().
97    Try to construct a helpful string, including file/function/variable
98    name.  */
99
100 static tree
101 mf_varname_tree (tree decl)
102 {
103   static pretty_printer buf_rec;
104   static int initialized = 0;
105   pretty_printer *buf = & buf_rec;
106   const char *buf_contents;
107   tree result;
108
109   gcc_assert (decl);
110
111   if (!initialized)
112     {
113       pp_construct (buf, /* prefix */ NULL, /* line-width */ 0);
114       initialized = 1;
115     }
116   pp_clear_output_area (buf);
117
118   /* Add FILENAME[:LINENUMBER[:COLUMNNUMBER]].  */
119   {
120     expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (decl));
121     const char *sourcefile;
122     unsigned sourceline = xloc.line;
123     unsigned sourcecolumn = 0;
124 #ifdef USE_MAPPED_LOCATION
125     sourcecolumn = xloc.column;
126 #endif
127     sourcefile = xloc.file;
128     if (sourcefile == NULL && current_function_decl != NULL_TREE)
129       sourcefile = DECL_SOURCE_FILE (current_function_decl);
130     if (sourcefile == NULL)
131       sourcefile = "<unknown file>";
132
133     pp_string (buf, sourcefile);
134
135     if (sourceline != 0)
136       {
137         pp_string (buf, ":");
138         pp_decimal_int (buf, sourceline);
139
140         if (sourcecolumn != 0)
141           {
142             pp_string (buf, ":");
143             pp_decimal_int (buf, sourcecolumn);
144           }
145       }
146   }
147
148   if (current_function_decl != NULL_TREE)
149     {
150       /* Add (FUNCTION) */
151       pp_string (buf, " (");
152       {
153         const char *funcname = NULL;
154         if (DECL_NAME (current_function_decl))
155           funcname = lang_hooks.decl_printable_name (current_function_decl, 1);
156         if (funcname == NULL)
157           funcname = "anonymous fn";
158
159         pp_string (buf, funcname);
160       }
161       pp_string (buf, ") ");
162     }
163   else
164     pp_string (buf, " ");
165
166   /* Add <variable-declaration>, possibly demangled.  */
167   {
168     const char *declname = NULL;
169
170     if (DECL_NAME (decl) != NULL)
171       {
172         if (strcmp ("GNU C++", lang_hooks.name) == 0)
173           {
174             /* The gcc/cp decl_printable_name hook doesn't do as good a job as
175                the libiberty demangler.  */
176             declname = cplus_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)),
177                                        DMGL_AUTO | DMGL_VERBOSE);
178           }
179         if (declname == NULL)
180           declname = lang_hooks.decl_printable_name (decl, 3);
181       }
182     if (declname == NULL)
183       declname = "<unnamed variable>";
184
185     pp_string (buf, declname);
186   }
187
188   /* Return the lot as a new STRING_CST.  */
189   buf_contents = pp_base_formatted_text (buf);
190   result = mf_build_string (buf_contents);
191   pp_clear_output_area (buf);
192
193   return result;
194 }
195
196
197 /* And another friend, for producing a simpler message.  */
198
199 static tree
200 mf_file_function_line_tree (location_t location)
201 {
202   expanded_location xloc = expand_location (location);
203   const char *file = NULL, *colon, *line, *op, *name, *cp;
204   char linecolbuf[30]; /* Enough for two decimal numbers plus a colon.  */
205   char *string;
206   tree result;
207
208   /* Add FILENAME[:LINENUMBER[:COLUMNNUMBER]].  */
209   file = xloc.file;
210   if (file == NULL && current_function_decl != NULL_TREE)
211     file = DECL_SOURCE_FILE (current_function_decl);
212   if (file == NULL)
213     file = "<unknown file>";
214
215   if (xloc.line > 0)
216     {
217 #ifdef USE_MAPPED_LOCATION
218       if (xloc.column > 0)
219         sprintf (linecolbuf, "%d:%d", xloc.line, xloc.column);
220       else
221 #endif
222         sprintf (linecolbuf, "%d", xloc.line);
223       colon = ":";
224       line = linecolbuf;
225     }
226   else
227     colon = line = "";
228
229   /* Add (FUNCTION).  */
230   name = lang_hooks.decl_printable_name (current_function_decl, 1);
231   if (name)
232     {
233       op = " (";
234       cp = ")";
235     }
236   else
237     op = name = cp = "";
238
239   string = concat (file, colon, line, op, name, cp, NULL);
240   result = mf_build_string (string);
241   free (string);
242
243   return result;
244 }
245
246
247 /* global tree nodes */
248
249 /* Global tree objects for global variables and functions exported by
250    mudflap runtime library.  mf_init_extern_trees must be called
251    before using these.  */
252
253 /* uintptr_t (usually "unsigned long") */
254 static GTY (()) tree mf_uintptr_type;
255
256 /* struct __mf_cache { uintptr_t low; uintptr_t high; }; */
257 static GTY (()) tree mf_cache_struct_type;
258
259 /* struct __mf_cache * const */
260 static GTY (()) tree mf_cache_structptr_type;
261
262 /* extern struct __mf_cache __mf_lookup_cache []; */
263 static GTY (()) tree mf_cache_array_decl;
264
265 /* extern unsigned char __mf_lc_shift; */
266 static GTY (()) tree mf_cache_shift_decl;
267
268 /* extern uintptr_t __mf_lc_mask; */
269 static GTY (()) tree mf_cache_mask_decl;
270
271 /* Their function-scope local shadows, used in single-threaded mode only.  */
272
273 /* auto const unsigned char __mf_lc_shift_l; */
274 static GTY (()) tree mf_cache_shift_decl_l;
275
276 /* auto const uintptr_t __mf_lc_mask_l; */
277 static GTY (()) tree mf_cache_mask_decl_l;
278
279 /* extern void __mf_check (void *ptr, size_t sz, int type, const char *); */
280 static GTY (()) tree mf_check_fndecl;
281
282 /* extern void __mf_register (void *ptr, size_t sz, int type, const char *); */
283 static GTY (()) tree mf_register_fndecl;
284
285 /* extern void __mf_unregister (void *ptr, size_t sz, int type); */
286 static GTY (()) tree mf_unregister_fndecl;
287
288 /* extern void __mf_init (); */
289 static GTY (()) tree mf_init_fndecl;
290
291 /* extern int __mf_set_options (const char*); */
292 static GTY (()) tree mf_set_options_fndecl;
293
294
295 /* Helper for mudflap_init: construct a decl with the given category,
296    name, and type, mark it an external reference, and pushdecl it.  */
297 static inline tree
298 mf_make_builtin (enum tree_code category, const char *name, tree type)
299 {
300   tree decl = mf_mark (build_decl (category, get_identifier (name), type));
301   TREE_PUBLIC (decl) = 1;
302   DECL_EXTERNAL (decl) = 1;
303   lang_hooks.decls.pushdecl (decl);
304   return decl;
305 }
306
307 /* Helper for mudflap_init: construct a tree corresponding to the type
308      struct __mf_cache { uintptr_t low; uintptr_t high; };
309      where uintptr_t is the FIELD_TYPE argument.  */
310 static inline tree
311 mf_make_mf_cache_struct_type (tree field_type)
312 {
313   /* There is, abominably, no language-independent way to construct a
314      RECORD_TYPE.  So we have to call the basic type construction
315      primitives by hand.  */
316   tree fieldlo = build_decl (FIELD_DECL, get_identifier ("low"), field_type);
317   tree fieldhi = build_decl (FIELD_DECL, get_identifier ("high"), field_type);
318
319   tree struct_type = make_node (RECORD_TYPE);
320   DECL_CONTEXT (fieldlo) = struct_type;
321   DECL_CONTEXT (fieldhi) = struct_type;
322   TREE_CHAIN (fieldlo) = fieldhi;
323   TYPE_FIELDS (struct_type) = fieldlo;
324   TYPE_NAME (struct_type) = get_identifier ("__mf_cache");
325   layout_type (struct_type);
326
327   return struct_type;
328 }
329
330 #define build_function_type_0(rtype)            \
331   build_function_type (rtype, void_list_node)
332 #define build_function_type_1(rtype, arg1)                 \
333   build_function_type (rtype, tree_cons (0, arg1, void_list_node))
334 #define build_function_type_3(rtype, arg1, arg2, arg3)                  \
335   build_function_type (rtype, tree_cons (0, arg1, tree_cons (0, arg2,   \
336                                                              tree_cons (0, arg3, void_list_node))))
337 #define build_function_type_4(rtype, arg1, arg2, arg3, arg4)            \
338   build_function_type (rtype, tree_cons (0, arg1, tree_cons (0, arg2,   \
339                                                              tree_cons (0, arg3, tree_cons (0, arg4, \
340                                                                                             void_list_node)))))
341
342 /* Initialize the global tree nodes that correspond to mf-runtime.h
343    declarations.  */
344 void
345 mudflap_init (void)
346 {
347   static bool done = false;
348   tree mf_const_string_type;
349   tree mf_cache_array_type;
350   tree mf_check_register_fntype;
351   tree mf_unregister_fntype;
352   tree mf_init_fntype;
353   tree mf_set_options_fntype;
354
355   if (done)
356     return;
357   done = true;
358
359   mf_uintptr_type = lang_hooks.types.type_for_mode (ptr_mode,
360                                                     /*unsignedp=*/true);
361   mf_const_string_type
362     = build_pointer_type (build_qualified_type
363                           (char_type_node, TYPE_QUAL_CONST));
364
365   mf_cache_struct_type = mf_make_mf_cache_struct_type (mf_uintptr_type);
366   mf_cache_structptr_type = build_pointer_type (mf_cache_struct_type);
367   mf_cache_array_type = build_array_type (mf_cache_struct_type, 0);
368   mf_check_register_fntype =
369     build_function_type_4 (void_type_node, ptr_type_node, size_type_node,
370                            integer_type_node, mf_const_string_type);
371   mf_unregister_fntype =
372     build_function_type_3 (void_type_node, ptr_type_node, size_type_node,
373                            integer_type_node);
374   mf_init_fntype =
375     build_function_type_0 (void_type_node);
376   mf_set_options_fntype =
377     build_function_type_1 (integer_type_node, mf_const_string_type);
378
379   mf_cache_array_decl = mf_make_builtin (VAR_DECL, "__mf_lookup_cache",
380                                          mf_cache_array_type);
381   mf_cache_shift_decl = mf_make_builtin (VAR_DECL, "__mf_lc_shift",
382                                          unsigned_char_type_node);
383   mf_cache_mask_decl = mf_make_builtin (VAR_DECL, "__mf_lc_mask",
384                                         mf_uintptr_type);
385   /* Don't process these in mudflap_enqueue_decl, should they come by
386      there for some reason.  */
387   mf_mark (mf_cache_array_decl);
388   mf_mark (mf_cache_shift_decl);
389   mf_mark (mf_cache_mask_decl);
390   mf_check_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_check",
391                                      mf_check_register_fntype);
392   mf_register_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_register",
393                                         mf_check_register_fntype);
394   mf_unregister_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_unregister",
395                                           mf_unregister_fntype);
396   mf_init_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_init",
397                                     mf_init_fntype);
398   mf_set_options_fndecl = mf_make_builtin (FUNCTION_DECL, "__mf_set_options",
399                                            mf_set_options_fntype);
400 }
401 #undef build_function_type_4
402 #undef build_function_type_3
403 #undef build_function_type_1
404 #undef build_function_type_0
405
406
407 /* ------------------------------------------------------------------------ */
408 /* Memory reference transforms. Perform the mudflap indirection-related
409    tree transforms on the current function.
410
411    This is the second part of the mudflap instrumentation.  It works on
412    low-level GIMPLE using the CFG, because we want to run this pass after
413    tree optimizations have been performed, but we have to preserve the CFG
414    for expansion from trees to RTL.  */
415
416 static unsigned int
417 execute_mudflap_function_ops (void)
418 {
419   /* Don't instrument functions such as the synthetic constructor
420      built during mudflap_finish_file.  */
421   if (mf_marked_p (current_function_decl) ||
422       DECL_ARTIFICIAL (current_function_decl))
423     return 0;
424
425   push_gimplify_context ();
426
427   /* In multithreaded mode, don't cache the lookup cache parameters.  */
428   if (! flag_mudflap_threads)
429     mf_decl_cache_locals ();
430
431   mf_xform_derefs ();
432
433   if (! flag_mudflap_threads)
434     mf_decl_clear_locals ();
435
436   pop_gimplify_context (NULL);
437   return 0;
438 }
439
440 /* Create and initialize local shadow variables for the lookup cache
441    globals.  Put their decls in the *_l globals for use by
442    mf_build_check_statement_for.  */
443
444 static void
445 mf_decl_cache_locals (void)
446 {
447   tree t, shift_init_stmts, mask_init_stmts;
448   tree_stmt_iterator tsi;
449
450   /* Build the cache vars.  */
451   mf_cache_shift_decl_l
452     = mf_mark (create_tmp_var (TREE_TYPE (mf_cache_shift_decl),
453                                "__mf_lookup_shift_l"));
454
455   mf_cache_mask_decl_l
456     = mf_mark (create_tmp_var (TREE_TYPE (mf_cache_mask_decl),
457                                "__mf_lookup_mask_l"));
458
459   /* Build initialization nodes for the cache vars.  We just load the
460      globals into the cache variables.  */
461   t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (mf_cache_shift_decl_l),
462               mf_cache_shift_decl_l, mf_cache_shift_decl);
463   add_referenced_var (mf_cache_shift_decl);
464   SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (current_function_decl));
465   gimplify_to_stmt_list (&t);
466   shift_init_stmts = t;
467
468   t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (mf_cache_mask_decl_l),
469               mf_cache_mask_decl_l, mf_cache_mask_decl);
470   add_referenced_var (mf_cache_mask_decl);
471   SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (current_function_decl));
472   gimplify_to_stmt_list (&t);
473   mask_init_stmts = t;
474
475   /* Anticipating multiple entry points, we insert the cache vars
476      initializers in each successor of the ENTRY_BLOCK_PTR.  */
477   for (tsi = tsi_start (shift_init_stmts);
478        ! tsi_end_p (tsi);
479        tsi_next (&tsi))
480     insert_edge_copies (tsi_stmt (tsi), ENTRY_BLOCK_PTR);
481
482   for (tsi = tsi_start (mask_init_stmts);
483        ! tsi_end_p (tsi);
484        tsi_next (&tsi))
485     insert_edge_copies (tsi_stmt (tsi), ENTRY_BLOCK_PTR);
486   bsi_commit_edge_inserts ();
487 }
488
489
490 static void
491 mf_decl_clear_locals (void)
492 {
493   /* Unset local shadows.  */
494   mf_cache_shift_decl_l = NULL_TREE;
495   mf_cache_mask_decl_l = NULL_TREE;
496 }
497
498 static void
499 mf_build_check_statement_for (tree base, tree limit,
500                               block_stmt_iterator *instr_bsi,
501                               location_t *locus, tree dirflag)
502 {
503   tree_stmt_iterator head, tsi;
504   block_stmt_iterator bsi;
505   basic_block cond_bb, then_bb, join_bb;
506   edge e;
507   tree cond, t, u, v;
508   tree mf_base;
509   tree mf_elem;
510   tree mf_limit;
511
512   /* We first need to split the current basic block, and start altering
513      the CFG.  This allows us to insert the statements we're about to
514      construct into the right basic blocks.  */
515
516   cond_bb = bb_for_stmt (bsi_stmt (*instr_bsi));
517   bsi = *instr_bsi;
518   bsi_prev (&bsi);
519   if (! bsi_end_p (bsi))
520     e = split_block (cond_bb, bsi_stmt (bsi));
521   else
522     e = split_block_after_labels (cond_bb);
523   cond_bb = e->src;
524   join_bb = e->dest;
525
526   /* A recap at this point: join_bb is the basic block at whose head
527      is the gimple statement for which this check expression is being
528      built.  cond_bb is the (possibly new, synthetic) basic block the
529      end of which will contain the cache-lookup code, and a
530      conditional that jumps to the cache-miss code or, much more
531      likely, over to join_bb.  */
532
533   /* Create the bb that contains the cache-miss fallback block (mf_check).  */
534   then_bb = create_empty_bb (cond_bb);
535   make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
536   make_single_succ_edge (then_bb, join_bb, EDGE_FALLTHRU);
537
538   /* Mark the pseudo-fallthrough edge from cond_bb to join_bb.  */
539   e = find_edge (cond_bb, join_bb);
540   e->flags = EDGE_FALSE_VALUE;
541   e->count = cond_bb->count;
542   e->probability = REG_BR_PROB_BASE;
543
544   /* Update dominance info.  Note that bb_join's data was
545      updated by split_block.  */
546   if (dom_info_available_p (CDI_DOMINATORS))
547     {
548       set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
549       set_immediate_dominator (CDI_DOMINATORS, join_bb, cond_bb);
550     }
551
552   /* Build our local variables.  */
553   mf_elem = create_tmp_var (mf_cache_structptr_type, "__mf_elem");
554   mf_base = create_tmp_var (mf_uintptr_type, "__mf_base");
555   mf_limit = create_tmp_var (mf_uintptr_type, "__mf_limit");
556
557   /* Build: __mf_base = (uintptr_t) <base address expression>.  */
558   t = build2 (GIMPLE_MODIFY_STMT, void_type_node, mf_base,
559               convert (mf_uintptr_type, unshare_expr (base)));
560   SET_EXPR_LOCUS (t, locus);
561   gimplify_to_stmt_list (&t);
562   head = tsi_start (t);
563   tsi = tsi_last (t);
564
565   /* Build: __mf_limit = (uintptr_t) <limit address expression>.  */
566   t = build2 (GIMPLE_MODIFY_STMT, void_type_node, mf_limit,
567               convert (mf_uintptr_type, unshare_expr (limit)));
568   SET_EXPR_LOCUS (t, locus);
569   gimplify_to_stmt_list (&t);
570   tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
571
572   /* Build: __mf_elem = &__mf_lookup_cache [(__mf_base >> __mf_shift)
573                                             & __mf_mask].  */
574   t = build2 (RSHIFT_EXPR, mf_uintptr_type, mf_base,
575               (flag_mudflap_threads ? mf_cache_shift_decl : mf_cache_shift_decl_l));
576   add_referenced_var (mf_cache_shift_decl);
577   t = build2 (BIT_AND_EXPR, mf_uintptr_type, t,
578               (flag_mudflap_threads ? mf_cache_mask_decl : mf_cache_mask_decl_l));
579   add_referenced_var (mf_cache_mask_decl);
580   t = build4 (ARRAY_REF,
581               TREE_TYPE (TREE_TYPE (mf_cache_array_decl)),
582               mf_cache_array_decl, t, NULL_TREE, NULL_TREE);
583   add_referenced_var (mf_cache_array_decl);
584   t = build1 (ADDR_EXPR, mf_cache_structptr_type, t);
585   t = build2 (GIMPLE_MODIFY_STMT, void_type_node, mf_elem, t);
586   add_referenced_var (mf_elem);
587   SET_EXPR_LOCUS (t, locus);
588   gimplify_to_stmt_list (&t);
589   tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
590
591   /* Quick validity check.
592
593      if (__mf_elem->low > __mf_base
594          || (__mf_elem_high < __mf_limit))
595         {
596           __mf_check ();
597           ... and only if single-threaded:
598           __mf_lookup_shift_1 = f...;
599           __mf_lookup_mask_l = ...;
600         }
601
602      It is expected that this body of code is rarely executed so we mark
603      the edge to the THEN clause of the conditional jump as unlikely.  */
604
605   /* Construct t <-- '__mf_elem->low  > __mf_base'.  */
606   t = build3 (COMPONENT_REF, mf_uintptr_type,
607               build1 (INDIRECT_REF, mf_cache_struct_type, mf_elem),
608               TYPE_FIELDS (mf_cache_struct_type), NULL_TREE);
609   t = build2 (GT_EXPR, boolean_type_node, t, mf_base);
610   add_referenced_var (mf_base);
611
612   /* Construct '__mf_elem->high < __mf_limit'.
613
614      First build:
615         1) u <--  '__mf_elem->high'
616         2) v <--  '__mf_limit'.
617
618      Then build 'u <-- (u < v).  */
619
620   u = build3 (COMPONENT_REF, mf_uintptr_type,
621               build1 (INDIRECT_REF, mf_cache_struct_type, mf_elem),
622               TREE_CHAIN (TYPE_FIELDS (mf_cache_struct_type)), NULL_TREE);
623
624   v = mf_limit;
625
626   u = build2 (LT_EXPR, boolean_type_node, u, v);
627
628   /* Build the composed conditional: t <-- 't || u'.  Then store the
629      result of the evaluation of 't' in a temporary variable which we
630      can use as the condition for the conditional jump.  */
631   t = build2 (TRUTH_OR_EXPR, boolean_type_node, t, u);
632   cond = create_tmp_var (boolean_type_node, "__mf_unlikely_cond");
633   t = build2 (GIMPLE_MODIFY_STMT, boolean_type_node, cond, t);
634   gimplify_to_stmt_list (&t);
635   tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
636
637   /* Build the conditional jump.  'cond' is just a temporary so we can
638      simply build a void COND_EXPR.  We do need labels in both arms though.  */
639   t = build3 (COND_EXPR, void_type_node, cond,
640               build1 (GOTO_EXPR, void_type_node, tree_block_label (then_bb)),
641               build1 (GOTO_EXPR, void_type_node, tree_block_label (join_bb)));
642   SET_EXPR_LOCUS (t, locus);
643   tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
644
645   /* At this point, after so much hard work, we have only constructed
646      the conditional jump,
647
648      if (__mf_elem->low > __mf_base
649          || (__mf_elem_high < __mf_limit))
650
651      The lowered GIMPLE tree representing this code is in the statement
652      list starting at 'head'.
653
654      We can insert this now in the current basic block, i.e. the one that
655      the statement we're instrumenting was originally in.  */
656   bsi = bsi_last (cond_bb);
657   for (tsi = head; ! tsi_end_p (tsi); tsi_next (&tsi))
658     bsi_insert_after (&bsi, tsi_stmt (tsi), BSI_CONTINUE_LINKING);
659
660   /*  Now build up the body of the cache-miss handling:
661
662      __mf_check();
663      refresh *_l vars.
664
665      This is the body of the conditional.  */
666   
667   u = tree_cons (NULL_TREE,
668                  mf_file_function_line_tree (locus == NULL ? UNKNOWN_LOCATION
669                                              : *locus),
670                  NULL_TREE);
671   u = tree_cons (NULL_TREE, dirflag, u);
672   /* NB: we pass the overall [base..limit] range to mf_check.  */
673   u = tree_cons (NULL_TREE, 
674                  fold_build2 (PLUS_EXPR, integer_type_node,
675                               fold_build2 (MINUS_EXPR, mf_uintptr_type, mf_limit, mf_base),
676                               integer_one_node),
677                  u);
678   u = tree_cons (NULL_TREE, mf_base, u);
679   t = build_function_call_expr (mf_check_fndecl, u);
680   gimplify_to_stmt_list (&t);
681   head = tsi_start (t);
682   tsi = tsi_last (t);
683
684   if (! flag_mudflap_threads)
685     {
686       t = build2 (GIMPLE_MODIFY_STMT, void_type_node,
687                   mf_cache_shift_decl_l, mf_cache_shift_decl);
688       tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
689
690       t = build2 (GIMPLE_MODIFY_STMT, void_type_node,
691                   mf_cache_mask_decl_l, mf_cache_mask_decl);
692       tsi_link_after (&tsi, t, TSI_CONTINUE_LINKING);
693     }
694
695   /* Insert the check code in the THEN block.  */
696   bsi = bsi_start (then_bb);
697   for (tsi = head; ! tsi_end_p (tsi); tsi_next (&tsi))
698     bsi_insert_after (&bsi, tsi_stmt (tsi), BSI_CONTINUE_LINKING);
699
700   *instr_bsi = bsi_start (join_bb);
701   bsi_next (instr_bsi);
702 }
703
704
705 /* Check whether the given decl, generally a VAR_DECL or PARM_DECL, is
706    eligible for instrumentation.  For the mudflap1 pass, this implies
707    that it should be registered with the libmudflap runtime.  For the
708    mudflap2 pass this means instrumenting an indirection operation with
709    respect to the object.
710 */
711 static int
712 mf_decl_eligible_p (tree decl)
713 {
714   return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
715           /* The decl must have its address taken.  In the case of
716              arrays, this flag is also set if the indexes are not
717              compile-time known valid constants.  */
718           && TREE_ADDRESSABLE (decl)    /* XXX: not sufficient: return-by-value structs! */
719           /* The type of the variable must be complete.  */
720           && COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (decl))
721           /* The decl hasn't been decomposed somehow.  */
722           && !DECL_HAS_VALUE_EXPR_P (decl));
723 }
724
725
726 static void
727 mf_xform_derefs_1 (block_stmt_iterator *iter, tree *tp,
728                    location_t *locus, tree dirflag)
729 {
730   tree type, base, limit, addr, size, t;
731
732   /* Don't instrument read operations.  */
733   if (dirflag == integer_zero_node && flag_mudflap_ignore_reads)
734     return;
735
736   /* Don't instrument marked nodes.  */
737   if (mf_marked_p (*tp))
738     return;
739
740   t = *tp;
741   type = TREE_TYPE (t);
742
743   if (type == error_mark_node)
744     return;
745
746   size = TYPE_SIZE_UNIT (type);
747
748   switch (TREE_CODE (t))
749     {
750     case ARRAY_REF:
751     case COMPONENT_REF:
752       {
753         /* This is trickier than it may first appear.  The reason is
754            that we are looking at expressions from the "inside out" at
755            this point.  We may have a complex nested aggregate/array
756            expression (e.g. "a.b[i].c"), maybe with an indirection as
757            the leftmost operator ("p->a.b.d"), where instrumentation
758            is necessary.  Or we may have an innocent "a.b.c"
759            expression that must not be instrumented.  We need to
760            recurse all the way down the nesting structure to figure it
761            out: looking just at the outer node is not enough.  */          
762         tree var;
763         int component_ref_only = (TREE_CODE (t) == COMPONENT_REF);
764         /* If we have a bitfield component reference, we must note the
765            innermost addressable object in ELT, from which we will
766            construct the byte-addressable bounds of the bitfield.  */
767         tree elt = NULL_TREE;
768         int bitfield_ref_p = (TREE_CODE (t) == COMPONENT_REF
769                               && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)));
770
771         /* Iterate to the top of the ARRAY_REF/COMPONENT_REF
772            containment hierarchy to find the outermost VAR_DECL.  */
773         var = TREE_OPERAND (t, 0);
774         while (1)
775           {
776             if (bitfield_ref_p && elt == NULL_TREE
777                 && (TREE_CODE (var) == ARRAY_REF || TREE_CODE (var) == COMPONENT_REF))
778               elt = var;
779         
780             if (TREE_CODE (var) == ARRAY_REF)
781               {
782                 component_ref_only = 0;
783                 var = TREE_OPERAND (var, 0);
784               }
785             else if (TREE_CODE (var) == COMPONENT_REF)
786               var = TREE_OPERAND (var, 0);
787             else if (INDIRECT_REF_P (var))
788               {
789                 base = TREE_OPERAND (var, 0);
790                 break;
791               }
792             else 
793               {
794                 gcc_assert (TREE_CODE (var) == VAR_DECL 
795                             || TREE_CODE (var) == PARM_DECL
796                             || TREE_CODE (var) == RESULT_DECL
797                             || TREE_CODE (var) == STRING_CST);
798                 /* Don't instrument this access if the underlying
799                    variable is not "eligible".  This test matches
800                    those arrays that have only known-valid indexes,
801                    and thus are not labeled TREE_ADDRESSABLE.  */
802                 if (! mf_decl_eligible_p (var) || component_ref_only)
803                   return;
804                 else
805                   {
806                     base = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (var)), var);
807                     break;
808                   }
809               }
810           }
811
812         /* Handle the case of ordinary non-indirection structure
813            accesses.  These have only nested COMPONENT_REF nodes (no
814            INDIRECT_REF), but pass through the above filter loop.
815            Note that it's possible for such a struct variable to match
816            the eligible_p test because someone else might take its
817            address sometime.  */
818
819         /* We need special processing for bitfield components, because
820            their addresses cannot be taken.  */
821         if (bitfield_ref_p)
822           {
823             tree field = TREE_OPERAND (t, 1);
824
825             if (TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST)
826               size = DECL_SIZE_UNIT (field);
827             
828             if (elt)
829               elt = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (elt)), elt);
830             addr = fold_convert (ptr_type_node, elt ? elt : base);
831             addr = fold_build2 (PLUS_EXPR, ptr_type_node,
832                                 addr, fold_convert (ptr_type_node,
833                                                     byte_position (field)));
834           }
835         else
836           addr = build1 (ADDR_EXPR, build_pointer_type (type), t);
837
838         limit = fold_build2 (MINUS_EXPR, mf_uintptr_type,
839                              fold_build2 (PLUS_EXPR, mf_uintptr_type,
840                                           convert (mf_uintptr_type, addr),
841                                           size),
842                              integer_one_node);
843       }
844       break;
845
846     case INDIRECT_REF:
847       addr = TREE_OPERAND (t, 0);
848       base = addr;
849       limit = fold_build2 (MINUS_EXPR, ptr_type_node,
850                            fold_build2 (PLUS_EXPR, ptr_type_node, base, size),
851                            integer_one_node);
852       break;
853
854     case TARGET_MEM_REF:
855       addr = tree_mem_ref_addr (ptr_type_node, t);
856       base = addr;
857       limit = fold_build2 (MINUS_EXPR, ptr_type_node,
858                            fold_build2 (PLUS_EXPR, ptr_type_node, base, size),
859                            build_int_cst (ptr_type_node, 1));
860       break;
861
862     case ARRAY_RANGE_REF:
863       warning (0, "mudflap checking not yet implemented for ARRAY_RANGE_REF");
864       return;
865
866     case BIT_FIELD_REF:
867       /* ??? merge with COMPONENT_REF code above? */
868       {
869         tree ofs, rem, bpu;
870
871         /* If we're not dereferencing something, then the access
872            must be ok.  */
873         if (TREE_CODE (TREE_OPERAND (t, 0)) != INDIRECT_REF)
874           return;
875
876         bpu = bitsize_int (BITS_PER_UNIT);
877         ofs = convert (bitsizetype, TREE_OPERAND (t, 2));
878         rem = size_binop (TRUNC_MOD_EXPR, ofs, bpu);
879         ofs = size_binop (TRUNC_DIV_EXPR, ofs, bpu);
880
881         size = convert (bitsizetype, TREE_OPERAND (t, 1));
882         size = size_binop (PLUS_EXPR, size, rem);
883         size = size_binop (CEIL_DIV_EXPR, size, bpu);
884         size = convert (sizetype, size);
885
886         addr = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
887         addr = convert (ptr_type_node, addr);
888         addr = fold_build2 (PLUS_EXPR, ptr_type_node, addr, ofs);
889
890         base = addr;
891         limit = fold_build2 (MINUS_EXPR, ptr_type_node,
892                              fold_build2 (PLUS_EXPR, ptr_type_node, base, size),
893                              integer_one_node);
894       }
895       break;
896
897     default:
898       return;
899     }
900
901   mf_build_check_statement_for (base, limit, iter, locus, dirflag);
902 }
903
904 static void
905 mf_xform_derefs (void)
906 {
907   basic_block bb, next;
908   block_stmt_iterator i;
909   int saved_last_basic_block = last_basic_block;
910
911   bb = ENTRY_BLOCK_PTR ->next_bb;
912   do
913     {
914       next = bb->next_bb;
915       for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
916         {
917           tree s = bsi_stmt (i);
918
919           /* Only a few GIMPLE statements can reference memory.  */
920           switch (TREE_CODE (s))
921             {
922             case GIMPLE_MODIFY_STMT:
923               mf_xform_derefs_1 (&i, &GIMPLE_STMT_OPERAND (s, 0),
924                                  EXPR_LOCUS (s), integer_one_node);
925               mf_xform_derefs_1 (&i, &GIMPLE_STMT_OPERAND (s, 1),
926                                  EXPR_LOCUS (s), integer_zero_node);
927               break;
928
929             case RETURN_EXPR:
930               if (TREE_OPERAND (s, 0) != NULL_TREE)
931                 {
932                   if (TREE_CODE (TREE_OPERAND (s, 0)) == GIMPLE_MODIFY_STMT)
933                     mf_xform_derefs_1 (&i, &GIMPLE_STMT_OPERAND
934                                              (TREE_OPERAND (s, 0), 1),
935                                        EXPR_LOCUS (s), integer_zero_node);
936                   else
937                     mf_xform_derefs_1 (&i, &TREE_OPERAND (s, 0), EXPR_LOCUS (s),
938                                        integer_zero_node);
939                 }
940               break;
941
942             default:
943               ;
944             }
945         }
946       bb = next;
947     }
948   while (bb && bb->index <= saved_last_basic_block);
949 }
950
951 /* ------------------------------------------------------------------------ */
952 /* ADDR_EXPR transforms.  Perform the declaration-related mudflap tree
953    transforms on the current function.
954
955    This is the first part of the mudflap instrumentation.  It works on
956    high-level GIMPLE because after lowering, all variables are moved out
957    of their BIND_EXPR binding context, and we lose liveness information
958    for the declarations we wish to instrument.  */
959
960 static unsigned int
961 execute_mudflap_function_decls (void)
962 {
963   /* Don't instrument functions such as the synthetic constructor
964      built during mudflap_finish_file.  */
965   if (mf_marked_p (current_function_decl) ||
966       DECL_ARTIFICIAL (current_function_decl))
967     return 0;
968
969   push_gimplify_context ();
970
971   mf_xform_decls (DECL_SAVED_TREE (current_function_decl),
972                   DECL_ARGUMENTS (current_function_decl));
973
974   pop_gimplify_context (NULL);
975   return 0;
976 }
977
978 /* This struct is passed between mf_xform_decls to store state needed
979    during the traversal searching for objects that have their
980    addresses taken.  */
981 struct mf_xform_decls_data
982 {
983   tree param_decls;
984 };
985
986
987 /* Synthesize a CALL_EXPR and a TRY_FINALLY_EXPR, for this chain of
988    _DECLs if appropriate.  Arrange to call the __mf_register function
989    now, and the __mf_unregister function later for each.  */
990 static void
991 mx_register_decls (tree decl, tree *stmt_list)
992 {
993   tree finally_stmts = NULL_TREE;
994   tree_stmt_iterator initially_stmts = tsi_start (*stmt_list);
995
996   while (decl != NULL_TREE)
997     {
998       if (mf_decl_eligible_p (decl) 
999           /* Not already processed.  */
1000           && ! mf_marked_p (decl)
1001           /* Automatic variable.  */
1002           && ! DECL_EXTERNAL (decl)
1003           && ! TREE_STATIC (decl))
1004         {
1005           tree size = NULL_TREE, variable_name;
1006           tree unregister_fncall, unregister_fncall_params;
1007           tree register_fncall, register_fncall_params;
1008
1009           size = convert (size_type_node, TYPE_SIZE_UNIT (TREE_TYPE (decl)));
1010
1011           /* (& VARIABLE, sizeof (VARIABLE), __MF_TYPE_STACK) */
1012           unregister_fncall_params =
1013             tree_cons (NULL_TREE,
1014                        convert (ptr_type_node,
1015                                 mf_mark (build1 (ADDR_EXPR,
1016                                                  build_pointer_type (TREE_TYPE (decl)),
1017                                                  decl))),
1018                        tree_cons (NULL_TREE, 
1019                                   size,
1020                                   tree_cons (NULL_TREE,
1021                                              /* __MF_TYPE_STACK */
1022                                              build_int_cst (NULL_TREE, 3),
1023                                              NULL_TREE)));
1024           /* __mf_unregister (...) */
1025           unregister_fncall = build_function_call_expr (mf_unregister_fndecl,
1026                                                         unregister_fncall_params);
1027
1028           /* (& VARIABLE, sizeof (VARIABLE), __MF_TYPE_STACK, "name") */
1029           variable_name = mf_varname_tree (decl);
1030           register_fncall_params =
1031             tree_cons (NULL_TREE,
1032                    convert (ptr_type_node,
1033                             mf_mark (build1 (ADDR_EXPR,
1034                                              build_pointer_type (TREE_TYPE (decl)),
1035                                              decl))),
1036                        tree_cons (NULL_TREE,
1037                                   size,
1038                                   tree_cons (NULL_TREE,
1039                                              /* __MF_TYPE_STACK */
1040                                              build_int_cst (NULL_TREE, 3),
1041                                              tree_cons (NULL_TREE,
1042                                                         variable_name,
1043                                                         NULL_TREE))));
1044
1045           /* __mf_register (...) */
1046           register_fncall = build_function_call_expr (mf_register_fndecl,
1047                                                       register_fncall_params);
1048
1049           /* Accumulate the two calls.  */
1050           /* ??? Set EXPR_LOCATION.  */
1051           gimplify_stmt (&register_fncall);
1052           gimplify_stmt (&unregister_fncall);
1053
1054           /* Add the __mf_register call at the current appending point.  */
1055           if (tsi_end_p (initially_stmts))
1056             warning (0, "mudflap cannot track %qs in stub function",
1057                      IDENTIFIER_POINTER (DECL_NAME (decl)));
1058           else
1059             {
1060               tsi_link_before (&initially_stmts, register_fncall, TSI_SAME_STMT);
1061
1062               /* Accumulate the FINALLY piece.  */
1063               append_to_statement_list (unregister_fncall, &finally_stmts);
1064             }
1065           mf_mark (decl);
1066         }
1067
1068       decl = TREE_CHAIN (decl);
1069     }
1070
1071   /* Actually, (initially_stmts!=NULL) <=> (finally_stmts!=NULL) */
1072   if (finally_stmts != NULL_TREE)
1073     {
1074       tree t = build2 (TRY_FINALLY_EXPR, void_type_node,
1075                        *stmt_list, finally_stmts);
1076       *stmt_list = NULL;
1077       append_to_statement_list (t, stmt_list);
1078     }
1079 }
1080
1081
1082 /* Process every variable mentioned in BIND_EXPRs.  */
1083 static tree
1084 mx_xfn_xform_decls (tree *t, int *continue_p, void *data)
1085 {
1086   struct mf_xform_decls_data* d = (struct mf_xform_decls_data*) data;
1087
1088   if (*t == NULL_TREE || *t == error_mark_node)
1089     {
1090       *continue_p = 0;
1091       return NULL_TREE;
1092     }
1093
1094   *continue_p = 1;
1095
1096   switch (TREE_CODE (*t))
1097     {
1098     case BIND_EXPR:
1099       {
1100         /* Process function parameters now (but only once).  */
1101         mx_register_decls (d->param_decls, &BIND_EXPR_BODY (*t));
1102         d->param_decls = NULL_TREE;
1103
1104         mx_register_decls (BIND_EXPR_VARS (*t), &BIND_EXPR_BODY (*t));
1105       }
1106       break;
1107
1108     default:
1109       break;
1110     }
1111
1112   return NULL_TREE;
1113 }
1114
1115 /* Perform the object lifetime tracking mudflap transform on the given function
1116    tree.  The tree is mutated in place, with possibly copied subtree nodes.
1117
1118    For every auto variable declared, if its address is ever taken
1119    within the function, then supply its lifetime to the mudflap
1120    runtime with the __mf_register and __mf_unregister calls.
1121 */
1122
1123 static void
1124 mf_xform_decls (tree fnbody, tree fnparams)
1125 {
1126   struct mf_xform_decls_data d;
1127   d.param_decls = fnparams;
1128   walk_tree_without_duplicates (&fnbody, mx_xfn_xform_decls, &d);
1129 }
1130
1131
1132 /* ------------------------------------------------------------------------ */
1133 /* Externally visible mudflap functions.  */
1134
1135
1136 /* Mark and return the given tree node to prevent further mudflap
1137    transforms.  */
1138 static GTY ((param_is (union tree_node))) htab_t marked_trees = NULL;
1139
1140 tree
1141 mf_mark (tree t)
1142 {
1143   void **slot;
1144
1145   if (marked_trees == NULL)
1146     marked_trees = htab_create_ggc (31, htab_hash_pointer, htab_eq_pointer, NULL);
1147
1148   slot = htab_find_slot (marked_trees, t, INSERT);
1149   *slot = t;
1150   return t;
1151 }
1152
1153 int
1154 mf_marked_p (tree t)
1155 {
1156   void *entry;
1157
1158   if (marked_trees == NULL)
1159     return 0;
1160
1161   entry = htab_find (marked_trees, t);
1162   return (entry != NULL);
1163 }
1164
1165 /* Remember given node as a static of some kind: global data,
1166    function-scope static, or an anonymous constant.  Its assembler
1167    label is given.  */
1168
1169 /* A list of globals whose incomplete declarations we encountered.
1170    Instead of emitting the __mf_register call for them here, it's
1171    delayed until program finish time.  If they're still incomplete by
1172    then, warnings are emitted.  */
1173
1174 static GTY (()) VEC(tree,gc) *deferred_static_decls;
1175
1176 /* A list of statements for calling __mf_register() at startup time.  */
1177 static GTY (()) tree enqueued_call_stmt_chain;
1178
1179 static void
1180 mudflap_register_call (tree obj, tree object_size, tree varname)
1181 {
1182   tree arg, args, call_stmt;
1183
1184   args = tree_cons (NULL_TREE, varname, NULL_TREE);
1185
1186   arg = build_int_cst (NULL_TREE, 4); /* __MF_TYPE_STATIC */
1187   args = tree_cons (NULL_TREE, arg, args);
1188
1189   arg = convert (size_type_node, object_size);
1190   args = tree_cons (NULL_TREE, arg, args);
1191
1192   arg = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (obj)), obj);
1193   arg = convert (ptr_type_node, arg);
1194   args = tree_cons (NULL_TREE, arg, args);
1195
1196   call_stmt = build_function_call_expr (mf_register_fndecl, args);
1197
1198   append_to_statement_list (call_stmt, &enqueued_call_stmt_chain);
1199 }
1200
1201 void
1202 mudflap_enqueue_decl (tree obj)
1203 {
1204   if (mf_marked_p (obj))
1205     return;
1206
1207   /* We don't need to process variable decls that are internally
1208      generated extern.  If we did, we'd end up with warnings for them
1209      during mudflap_finish_file ().  That would confuse the user,
1210      since the text would refer to variables that don't show up in the
1211      user's source code.  */
1212   if (DECL_P (obj) && DECL_EXTERNAL (obj) && DECL_ARTIFICIAL (obj))
1213     return;
1214
1215   VEC_safe_push (tree, gc, deferred_static_decls, obj);
1216 }
1217
1218
1219 void
1220 mudflap_enqueue_constant (tree obj)
1221 {
1222   tree object_size, varname;
1223
1224   if (mf_marked_p (obj))
1225     return;
1226
1227   if (TREE_CODE (obj) == STRING_CST)
1228     object_size = build_int_cst (NULL_TREE, TREE_STRING_LENGTH (obj));
1229   else
1230     object_size = size_in_bytes (TREE_TYPE (obj));
1231
1232   if (TREE_CODE (obj) == STRING_CST)
1233     varname = mf_build_string ("string literal");
1234   else
1235     varname = mf_build_string ("constant");
1236
1237   mudflap_register_call (obj, object_size, varname);
1238 }
1239
1240
1241 /* Emit any file-wide instrumentation.  */
1242 void
1243 mudflap_finish_file (void)
1244 {
1245   tree ctor_statements = NULL_TREE;
1246
1247   /* No need to continue when there were errors.  */
1248   if (errorcount != 0 || sorrycount != 0)
1249     return;
1250
1251   /* Insert a call to __mf_init.  */
1252   {
1253     tree call2_stmt = build_function_call_expr (mf_init_fndecl, NULL_TREE);
1254     append_to_statement_list (call2_stmt, &ctor_statements);
1255   }
1256   
1257   /* If appropriate, call __mf_set_options to pass along read-ignore mode.  */
1258   if (flag_mudflap_ignore_reads)
1259     {
1260       tree arg = tree_cons (NULL_TREE, 
1261                             mf_build_string ("-ignore-reads"), NULL_TREE);
1262       tree call_stmt = build_function_call_expr (mf_set_options_fndecl, arg);
1263       append_to_statement_list (call_stmt, &ctor_statements);
1264     }
1265
1266   /* Process all enqueued object decls.  */
1267   if (deferred_static_decls)
1268     {
1269       size_t i;
1270       tree obj;
1271       for (i = 0; VEC_iterate (tree, deferred_static_decls, i, obj); i++)
1272         {
1273           gcc_assert (DECL_P (obj));
1274
1275           if (mf_marked_p (obj))
1276             continue;
1277
1278           /* Omit registration for static unaddressed objects.  NB:
1279              Perform registration for non-static objects regardless of
1280              TREE_USED or TREE_ADDRESSABLE, because they may be used
1281              from other compilation units.  */
1282           if (! TREE_PUBLIC (obj) && ! TREE_ADDRESSABLE (obj))
1283             continue;
1284
1285           if (! COMPLETE_TYPE_P (TREE_TYPE (obj)))
1286             {
1287               warning (0, "mudflap cannot track unknown size extern %qs",
1288                        IDENTIFIER_POINTER (DECL_NAME (obj)));
1289               continue;
1290             }
1291           
1292           mudflap_register_call (obj, 
1293                                  size_in_bytes (TREE_TYPE (obj)),
1294                                  mf_varname_tree (obj));
1295         }
1296
1297       VEC_truncate (tree, deferred_static_decls, 0);
1298     }
1299
1300   /* Append all the enqueued registration calls.  */
1301   if (enqueued_call_stmt_chain)
1302     {
1303       append_to_statement_list (enqueued_call_stmt_chain, &ctor_statements);
1304       enqueued_call_stmt_chain = NULL_TREE;
1305     }
1306
1307   cgraph_build_static_cdtor ('I', ctor_statements, 
1308                              MAX_RESERVED_INIT_PRIORITY-1);
1309 }
1310
1311
1312 static bool
1313 gate_mudflap (void)
1314 {
1315   return flag_mudflap != 0;
1316 }
1317
1318 struct tree_opt_pass pass_mudflap_1 = 
1319 {
1320   "mudflap1",                           /* name */
1321   gate_mudflap,                         /* gate */
1322   execute_mudflap_function_decls,       /* execute */
1323   NULL,                                 /* sub */
1324   NULL,                                 /* next */
1325   0,                                    /* static_pass_number */
1326   0,                                    /* tv_id */
1327   PROP_gimple_any,                      /* properties_required */
1328   0,                                    /* properties_provided */
1329   0,                                    /* properties_destroyed */
1330   0,                                    /* todo_flags_start */
1331   TODO_dump_func,                       /* todo_flags_finish */
1332   0                                     /* letter */
1333 };
1334
1335 struct tree_opt_pass pass_mudflap_2 = 
1336 {
1337   "mudflap2",                           /* name */
1338   gate_mudflap,                         /* gate */
1339   execute_mudflap_function_ops,         /* execute */
1340   NULL,                                 /* sub */
1341   NULL,                                 /* next */
1342   0,                                    /* static_pass_number */
1343   0,                                    /* tv_id */
1344   PROP_gimple_leh,                      /* properties_required */
1345   0,                                    /* properties_provided */
1346   0,                                    /* properties_destroyed */
1347   0,                                    /* todo_flags_start */
1348   TODO_verify_flow | TODO_verify_stmts
1349   | TODO_dump_func,                     /* todo_flags_finish */
1350   0                                     /* letter */
1351 };
1352
1353 #include "gt-tree-mudflap.h"