On Mar 20, 2012, at 1:21 PM, Richard Guenther wrote: > On Tue, 20 Mar 2012, Tristan Gingold wrote: > >> >> On Mar 15, 2012, at 10:37 AM, Richard Guenther wrote: >> >>> On Wed, 14 Mar 2012, Tristan Gingold wrote: >> […] >> >>> >>> Well. To make this work in LTO the "main" function (thus, the program >>> entry point) should be marked at cgraph level and all users of >>> MAIN_NAME_P should instead check a flag on the cgraph node. >>> >>>> Will write a predicate in tree.[ch]. >>> >>> Please instead transition "main-ness" to the graph. >> >> Hi, >> >> here is the patch I wrote. Does it match what you had in mind ? > > Basically yes. Comments below. > >> main_identifier_node is now set in tree.c > > Looks good, hopefully my review-grep was as good as yours ;)
[…] >> diff --git a/gcc/cfgexpand.c b/gcc/cfgexpand.c >> index bd21169..7a7a774 100644 >> --- a/gcc/cfgexpand.c >> +++ b/gcc/cfgexpand.c >> @@ -4513,9 +4513,8 @@ gimple_expand_cfg (void) >> >> /* If this function is `main', emit a call to `__main' >> to run global initializers, etc. */ >> - if (DECL_NAME (current_function_decl) >> - && MAIN_NAME_P (DECL_NAME (current_function_decl)) >> - && DECL_FILE_SCOPE_P (current_function_decl)) >> + if (DECL_FILE_SCOPE_P (current_function_decl) >> + && cgraph_main_function_p (cgraph_get_node (current_function_decl))) >> expand_main_function (); > > The DECL_FILE_SCOPE_P check is redundant, please remove them everywhere > you call cgraph_main_function_p. I suppose returning false if the > cgraph node is NULL in cgraph_main_function_p would be good. Ok. (I added the DECL_FILE_SCOPE_P check to avoid the cgraph lookup for speed reason) […] >> diff --git a/gcc/cgraphunit.c b/gcc/cgraphunit.c >> index 516f187..4a59f63 100644 >> --- a/gcc/cgraphunit.c >> +++ b/gcc/cgraphunit.c >> @@ -346,6 +346,10 @@ cgraph_finalize_function (tree decl, bool nested) >> notice_global_symbol (decl); >> node->local.finalized = true; >> node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL; >> + node->local.main_function = >> + DECL_FILE_SCOPE_P (decl) >> + && ((!DECL_ASSEMBLER_NAME_SET_P (decl) && MAIN_NAME_P (DECL_NAME >> (decl))) >> + || decl_assembler_name_equal (decl, main_identifier_node)); > > If we finalize a function we should always create an assembler name, > thus I'd change the above to > > node->local.main_function = decl_assembler_name_equal (decl, > main_identifier_node); Indeed. At worst, the assembler name is created during the call to notice_global_symbol. > btw, decl_assembler_name_equal doesn't seem to remove target-specific > mangling - do some OSes "mangle" main differently (I'm thinking of > leading underscores or complete renames)? Thus, I guess the > targets might want to be able to provide the main_identifier_assember_name > you use here. I think this is currently OK because decl_assembler_name_equal deals with leading underscore correctly. I have checked that on Darwin, which has a leading underscore. The only target that mangle names is i386 cygwin/mingw, which 'annotates' stdcall and fastcall function, but main() is regular. But I agree this mechanism is fragile. In order to make this mechanism stronger, we could add main_function_node, which designates the FUNCTION_DECL that is the main function (if not NULL_TREE), with a fallback on main_identifier_node for regular languages such as C or C++. Tristan.