【发布时间】:2011-11-02 17:26:54
【问题描述】:
当我对我的程序进行反汇编时,我看到 gcc 在使用 -O3 编译时正在使用 jmp 进行第二个 pthread_wait_barrier 调用。为什么会这样?
使用 jmp 代替 call 有什么好处。编译器在这里玩什么花样?我猜它在这里执行尾调用优化。
顺便说一下,我在这里使用的是静态链接。
__attribute__ ((noinline)) void my_pthread_barrier_wait(
volatile int tid, pthread_barrier_t *pbar )
{
pthread_barrier_wait( pbar );
if ( tid == 0 )
{
if ( !rollbacked )
{
take_checkpoint_or_rollback( ++iter == 4 );
}
}
//getcontext( &context[tid] );
SETJMP( tid );
asm("addr2jmp:");
pthread_barrier_wait( pbar );
// My suspicion was right, gcc was performing tail call optimization,
// which was messing up with my SETJMP/LONGJMP implementation, so here I
// put a dummy function to avoid that.
dummy_var = dummy_func();
}
【问题讨论】:
-
向我们展示源代码。
-
jmp 只是将执行转移到一个新位置。 call 将东西压入堆栈,因此使用起来稍微贵一些。
-
看起来它正在执行尾调用优化。
-
可以添加汇编代码吗?