On Wed, May 4, 2011 at 6:16 AM, Matt Davis <mattdav...@gmail.com> wrote:
> I am writing a gcc plugin and am trying to detect if a value assigned by a
> function call, is a global variable or not.  Unfortunately, all calls to
> 'is_global_var' with a DECL type are returning false.
>
> My pass executes after alias analysis, and ipa analysis.  The
> cfun->gimple_df->ipa_pta is set to true, so I know the pta analysis should 
> have
> resolved global information.

is_global_var is all you need, no need for PTA analysis (which doesn't
change this but simply uses is_global_var as well).

> Plugin code:
>    if (is_gimple_call(stmt))
>    {
>        gimple_debug_bb(stmt);
>        tree lhs = gimple_call_lhs(stmt);
>        if (lhs && is_global_var(SSA_NAME_VAR(lhs)))
>          printf("Global detected\n");

That will only reliably work if the global is not of is_gimple_reg_type (),
otherwise the call will store to an automatic temporary and the store
to the global will happen in a separate statement.

>    }
>
>
> Source code (in Go):
>    package main
>
>    type T struct {id int}
>    var myglobal *T;
>
>    func fn() *T {
>        myglobal = new(T); // Should be detected as global
>        return myglobal;
>    }
>
>    func main() {
>        t := fn();
>    }
>
>
> Basic Block dump as my plugin code executes for function 'fn':
>    <bb 2>:
>    # .MEM_4 = VDEF <.MEM_3(D)>
>    main.myglobal.13_1 = __go_new_nopointers (4);

assigns to a temporary

>    # .MEM_5 = VDEF <.MEM_4>
>    main.myglobal = main.myglobal.13_1;

and here is the store

You can try looking up the store if the LHS of the call is an SSA name
by looking at its immediate uses, but of course for

int glob;

foo()
{
  int i = call(); // not global
  glob = i;
}

this would also find the store to glob.

So I'm not sure you can recover all information up to source level
precision.

Richard.

>    # VUSE <.MEM_5>
>    D.186_2 = main.myglobal;
>    return D.186_2;
>
>
> Any insight would be helpful.
> Thanks!
>
> -Matt
>

Reply via email to