Perhaps it is because you are using ECF_TM_PURE when defining the
built-in in cobol1.cc:

  #define ATTR_TMPURE_NORETURN_NOTHROW_LEAF_COLD_LIST 
(ECF_TM_PURE|ECF_NORETURN|ECF_NOTHROW|ECF_LEAF|ECF_COLD)

  [...]
  gfc_define_builtin ("__builtin_exit", ftype, BUILT_IN_EXIT,
          "exit", ATTR_TMPURE_NORETURN_NOTHROW_LEAF_COLD_LIST);

In gcc/builtins.def:

  DEF_LIB_BUILTIN        (BUILT_IN_EXIT, "exit", BT_FN_VOID_INT, 
ATTR_NORETURN_NOTHROW_LIST)

So you want ECF_NORETURN and ECF_NOTHROW.

> I stated that poorly.  After I generate the GENERIC, and I hand the tree
> over to the middle end, it is the call to BUILT_IN_EXIT that seems to be
> disappearing.
>
> Everything I describe here is occurring with a -O0 build of GCC and
> GCOBOL.
>
>> -----Original Message-----
>> From: Robert Dubner <rdub...@symas.com>
>> Sent: Thursday, April 3, 2025 18:16
>> To: 'GCC Mailing List' <gcc@gcc.gnu.org>
>> Cc: Robert Dubner <rdub...@symas.com>
>> Subject: COBOL: Call to builtin_decl_explicit (BUILT_IN_EXIT), is
>> optimized away.
>> 
>> The COBOL compiler has this routine:
>> 
>> void
>> gg_exit(tree exit_code)
>>   {
>>   tree the_call =
>>       build_call_expr_loc(location_from_lineno(),
>>                           builtin_decl_explicit (BUILT_IN_EXIT),
>>                           1,
>>                           exit_code);
>>   gg_append_statement(the_call);
>>   }
>> 
>> I have found that when GCOBOL is used with -O2, -O3, or -Os, the call to
>> gg_exit() is optimized away, and the intended exit value is lost, and I
>> end up with zero.
>> 
>> By changing the routine to
>> 
>> void
>> gg_exit(tree exit_code)
>>   {
>>   tree args[1] = {exit_code};
>>   tree function = gg_get_function_address(INT, "exit");
>>   tree the_call = build_call_array_loc (location_from_lineno(),
>>                                         VOID,
>>                                         function,
>>                                         1,
>>                                         args);
>>   gg_append_statement(the_call);
>>   }
>> 
>> the call is not optimized away, and the generated executable behaves as
>> expected.
>> 
>> How do I prevent the call to gg_exit() from being optimized away?
>> 
>> Thanks!

Reply via email to