From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20678 invoked by alias); 3 Jan 2008 21:26:12 -0000 Received: (qmail 20658 invoked by uid 22791); 3 Jan 2008 21:26:10 -0000 X-Spam-Check-By: sourceware.org Received: from sbcs.cs.sunysb.edu (HELO sbcs.cs.sunysb.edu) (130.245.1.15) by sourceware.org (qpsmtpd/0.31) with ESMTP; Thu, 03 Jan 2008 21:21:32 +0000 Received: from [130.245.14.30] (sbrtjohnson [130.245.14.30]) by sbcs.cs.sunysb.edu (8.13.6/8.12.11) with ESMTP id m03LLK9o008454 for ; Thu, 3 Jan 2008 16:21:21 -0500 (EST) Message-ID: <477D5191.4070407@cs.sunysb.edu> Date: Thu, 03 Jan 2008 21:26:00 -0000 From: Rob Johnson User-Agent: Mozilla-Thunderbird 2.0.0.6 (X11/20071009) MIME-Version: 1.0 To: gcc@gcc.gnu.org Subject: Re: plugin help: Inserting a function call in gimple code? References: <477C1AA1.8000609@cs.sunysb.edu> <20080103102554.GA5061@dhcp64.suse.cz> In-Reply-To: <20080103102554.GA5061@dhcp64.suse.cz> Content-Type: text/plain; charset=ISO-8859-2; format=flowed Content-Transfer-Encoding: 7bit Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2008-01/txt/msg00028.txt.bz2 Martin Jambor wrote: > Hi, > > On Wed, Jan 02, 2008 at 06:13:37PM -0500, Rob Johnson wrote: >> I'm experimenting with the gimple plugin infrastructure and I'm having >> trouble instrumenting code in a way that is compatible with the optimizer. >> Here's a simple example that is intended to insert the function call >> "__memcheck_register_argv(argc, argv)" at the beginning of main. The code >> runs during pass_plugin_gimple (which comes right after pass_apply_inline >> in passes.c) and works great with -O0, but causes the compiler to crash >> with -O1 or higher. >> >> ------------------------------------- >> tree argv_registrar_type; >> tree argv_registrar; >> tree argv_registrar_call; >> >> argv_registrar_type = build_function_type_list (void_type_node, >> integer_type_node, >> build_pointer_type >> (build_pointer_type >> (char_type_node)), >> NULL_TREE); >> argv_registrar = build_fn_decl ("__memcheck_register_argv", >> argv_registrar_type); >> argv_registrar_call = build_call_expr (argv_registrar, 2, >> DECL_ARGUMENTS (cfun->decl), >> TREE_CHAIN (DECL_ARGUMENTS >> (cfun->decl))); > > DECL_ARGUMENTS is a tree chain of PARM_DECLs and in SSA GIMPLE, scalar > operands (integers and pointer are both scalar, is_gimple_reg() > predicate is there to identify variables that need to be converted to > SSA) need to be SSA_NAMEs of declarations (PARM_DECLs and VAR_DECLs in > particular). > > Therefore I suspect you need to create a different chain of respective > SSA_NAMES and pass that to build_call_expr(). You can get the default > SSA_NAME by calling gimple_default_def(). Yes! It seems it's slightly more complicated given that, with some optimization levels the code is sometimes turned into SSA and sometimes not, and that, depending on how the original program uses argv and argc, they may or may not already have default definitions. If the original program doesn't touch argv/argc, then we also have to update the set of referenced vars. Here's the code I wrote to construct the argc argument to the above call expression: tree argc; /* If we're doing SSA... */ if (cfun->gimple_df && cfun->gimple_df->default_defs) { argc = gimple_default_def (cfun, DECL_ARGUMENTS (cfun->decl)); if (!argc) { argc = DECL_ARGUMENTS (cfun->decl); mark_sym_for_renaming (argc); } add_referenced_var (DECL_ARGUMENTS (cfun->decl)); } else argc = DECL_ARGUMENTS (cfun->decl); The code for argv is similar. Does that look right? Once I get this plugin working, I will try to extract some useful building blocks for other plugin writers. Thanks for your help! Best, Rob