我知道noreturn应该用于那些预期不会将控制权返回给调用者的函数,但我找不到生成的汇编代码的差异。 有谁知道会生成不同代码的例子吗?
编辑:noreturn不会在它之后生成清理代码。
答案 0 :(得分:2)
returns_twice
禁用了一些gcc优化。
作为一个例子,在一个旧的gcc版本中我已经存在:尾调用优化,全局公共子表达式消除,跳转绕过。
returns_twice
使用calls_setjmp
机制(在源树周围):
if (flags & ECF_RETURNS_TWICE)
cfun->calls_setjmp = true;
tco(gcc/tree-tailcall.c
):
static bool
suitable_for_tail_call_opt_p (void)
{
[...]
/* Any function that calls setjmp might have longjmp called from
any called function. ??? We really should represent this
properly in the CFG so that this needn't be special cased. */
if (cfun->calls_setjmp)
return false;
gcse(gcc/gcse.c
):
static int
gcse_main (rtx f ATTRIBUTE_UNUSED)
{
[...]
/* We do not construct an accurate cfg in functions which call
setjmp, so just punt to be safe. */
if (cfun->calls_setjmp)
return 0;
跳跃绕过(gcc/gcse.c
):
static int
bypass_jumps (void)
{
[...]
/* We do not construct an accurate cfg in functions which call
setjmp, so just punt to be safe. */
if (cfun->calls_setjmp)
return 0;
该函数既不是纯函数也不是const(gcc/ipa-pure-const.c
):
/* Check the parameters of a function call to CALL_EXPR to see if
there are any references in the parameters that are not allowed for
pure or const functions. Also check to see if this is either an
indirect call, a call outside the compilation unit, or has special
attributes that may also effect the purity. The CALL_EXPR node for
the entire call expression. */
static void
check_call (funct_state local, gimple call)
{
[...]
/* When bad things happen to bad functions, they cannot be const
or pure. */
if (setjmp_call_p (callee_t))
{
local->pure_const_state = IPA_NEITHER;
local->looping = false;
}
无法内联函数(gcc/tree-inline.c
):
/* A callback for walk_gimple_seq to handle statements. Returns
non-NULL iff a function can not be inlined. Also sets the reason
why. */
static tree
inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
struct walk_stmt_info *wip)
{
[...]
/* We cannot inline functions that call setjmp. */
if (setjmp_call_p (t))
{
inline_forbidden_reason
= G_("function %q+F can never be inlined because it uses setjmp");
*handled_ops_p = true;
return t;
}
它还会影响函数堆栈帧中的寄存器保存区域。
实施例(TCO):
func.c:
int func(void)
{
return 0;
}
tco.c:
extern int func(void) /*__attribute__((returns_twice))*/;
int main()
{
return func();
}
不会返回两次:
00000000004003a0 <main>:
4003a0: e9 0b 01 00 00 jmpq 4004b0 <func>
4003a5: 90 nop
4003a6: 90 nop
4003a7: 90 nop
返回两次:
00000000004003a0 <main>:
4003a0: 48 83 ec 08 sub $0x8,%rsp
4003a4: e8 17 01 00 00 callq 4004c0 <func>
4003a9: 48 83 c4 08 add $0x8,%rsp
4003ad: c3 retq
4003ae: 90 nop
4003af: 90 nop