When looking at ipa's creation of calls to main
(https://gcc.gnu.org/ml/gcc-patches/2015-12/msg01812.html), I discovered this
piece of code in tree-inline.c.
Explicitly calling main is a strange thing to do from anywhere but crt0 (and
ill-formed c++). However, we should probably not consider main as an inlinable
function without some user annotation. I guess we might encounter something
like the testcase with whole-program optimization.
tested on x86_64-linux, ok?
nathan
2015-12-18 Nathan Sidwell <nat...@acm.org>
gcc/
* tree-inline.c (tree_inlinable_function_p): Don't consider main
inlinable, unless told so.
gcc/testsuite/
* gcc.dg/ipa/inline-9.c: New.
Index: tree-inline.c
===================================================================
--- tree-inline.c (revision 231815)
+++ tree-inline.c (working copy)
@@ -3793,6 +3793,16 @@ tree_inlinable_function_p (tree fn)
inlinable = false;
}
+ else if (!DECL_DECLARED_INLINE_P (fn)
+ && !always_inline
+ && TREE_PUBLIC (fn)
+ && DECL_FILE_SCOPE_P (fn)
+ && MAIN_NAME_P (DECL_NAME (fn)))
+ /* Inlining main is usually a bad idea, but not forbidden. So
+ only inibit inlining it, if the user's not explicitly asked for
+ it to be inlinable. */
+ inlinable = false;
+
/* Squirrel away the result so that we don't have to check again. */
DECL_UNINLINABLE (fn) = !inlinable;
Index: testsuite/gcc.dg/ipa/inline-9.c
===================================================================
--- testsuite/gcc.dg/ipa/inline-9.c (revision 0)
+++ testsuite/gcc.dg/ipa/inline-9.c (working copy)
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-O2 -fdump-tree-inline_param1 -fdump-tree-einline" } */
+
+/* Make sure we don't inline 'main'. */
+
+int main (int argc, char **argv)
+{
+ return argc;
+}
+
+int x ()
+{
+ return main (1, 0);
+}
+
+/* { dg-final { scan-tree-dump-not "for main/1 inlinable" "inline_param1" } } */
+/* { df-final { scan-tree-dump "main \\(1, 0B\\)" "einline" } } */