This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

GCC plugin: copying function's arguments


Hi,

I am working on a GCC plugin that instruments the applications being compiled. The applications are written in C and are built with GCC 4.7 (4.8 and 4.9 are also an option) on an x86 Linux system.

My plugin implements a compilation pass which is placed after "ssa" standard pass and operates on GIMPLE representation. Among other things, I need to implement the following but currently cannot figure out how to do it correctly.

When processing a C function, I need to insert the code at its beginning that copies its arguments to the local variables I create, for future processing.

My first naive implementation looked as follows:
------------------
tree p;
gimple_seq seq = NULL;
gimple_stmt_iterator gsi = gsi_start_bb(single_succ(ENTRY_BLOCK_PTR));

for (p = DECL_ARGUMENTS(current_function_decl); p; p = DECL_CHAIN(param)) {
    tree copy_par;
    copy_par = create_tmp_var(TREE_TYPE(p), NULL);
    add_referenced_var(copy_par);
    copy_par = make_ssa_name(copy_par, NULL);
    g = gimple_build_assign(copy_par, p);
    SSA_NAME_DEF_STMT(copy_par) = g;
    gimple_seq_add_stmt_without_update (&seq, g);
    ... // more processing here
}
...
gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
------------------

This way, however, an invalid assignment of the parameter declaration to a variable is created according to a dump:
gimple_assign <parm_decl, D.2206_11, par, NULL>

'D.2206_11' corresponds to the local variable I created, 'par' - the argument of the function I want to copy.

GCC crashes at some later pass as a result, probably trying to process this added statement. I suppose this is because 'p' is not the variable holding the value of the respective argument but a declaration of that variable. Is it this way? And how to get that variable?

I tried using gimple_build_assign_with_ops(NOP_EXPR, copy_par, p, NULL_TREE) instead of gimple_build_assign() but it did not do either. GCC still crashes at the same place. GDB log with the backtrace is below but I feel I am just missing something fundamental here.

------------------
Program received signal SIGSEGV, Segmentation fault.
[Switching to process 32166]
var_to_partition (map=<optimized out>, var=<optimized out>) at ../../gcc/tree-ssa-live.h:150
150	    part = map->partition_to_view[part];
(gdb) bt
#0 var_to_partition (map=<optimized out>, var=<optimized out>) at ../../gcc/tree-ssa-live.h:150 #1 live_track_process_use (use=<optimized out>, ptr=0x8ce9690) at ../../gcc/tree-ssa-coalesce.c:768 #2 build_ssa_conflict_graph (liveinfo=0x8ce6d10) at ../../gcc/tree-ssa-coalesce.c:887
#3  coalesce_ssa_name () at ../../gcc/tree-ssa-coalesce.c:1415
#4 0x084345ec in remove_ssa_form (sa=0x8b1208c, perform_ter=false) at ../../gcc/tree-outof-ssa.c:910
#5  rewrite_out_of_ssa (sa=0x8b1208c) at ../../gcc/tree-outof-ssa.c:1146
#6  0x081ab3b3 in gimple_expand_cfg () at ../../gcc/cfgexpand.c:4416
#7 0x08353f1d in execute_one_pass (pass=0x8b09ca0) at ../../gcc/passes.c:2084 #8 0x08354275 in execute_pass_list (pass=0x8b09ca0) at ../../gcc/passes.c:2139 #9 0x084329a4 in tree_rest_of_compilation (fndecl=0xb7022280) at ../../gcc/tree-optimize.c:422 #10 0x081c6674 in cgraph_expand_function (node=0xb6f5aa40) at ../../gcc/cgraphunit.c:1837
#11 0x081c80c3 in cgraph_output_in_order () at ../../gcc/cgraphunit.c:2002
#12 cgraph_optimize () at ../../gcc/cgraphunit.c:2213
#13 0x081c83ff in cgraph_finalize_compilation_unit () at ../../gcc/cgraphunit.c:1344
#14 0x081023dc in c_write_global_declarations () at ../../gcc/c-decl.c:10034
#15 0x083eba51 in compile_file () at ../../gcc/toplev.c:573
#16 do_compile () at ../../gcc/toplev.c:1929
#17 toplev_main (argc=18, argv=0xbfffedd4) at ../../gcc/toplev.c:2005
#18 0x080f104b in main (argc=18, argv=0xbfffedd4) at ../../gcc/main.c:36
(gdb) p part
$1 = 2053731167
(gdb) l
145	{
146	  int part;
147	
148	  part = partition_find (map->var_partition, SSA_NAME_VERSION (var));
149	  if (map->partition_to_view)
150	    part = map->partition_to_view[part];
151	  return part;
152	}
------------------

I also looked at traversal of trees starting from TYPE_ARG_TYPES (TREE_TYPE (current_function_decl)) and further via TREE_CHAIN(...) but that seems to give the types of the arguments rather than the respective variables.

So, the question is, how to add copying of the function's arguments properly?

Any help is appreciated.

Regards,
Eugene

--
Eugene Shatokhin, ROSA
www.rosalab.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]