Another tail call optimization question

Hello,

Could anyone kindly explain to me why the ‘g()’ in the following function cannot have tail call optimization?

void f(int* x);
void g();
void h(int v) {
f(&v);
g();
}

A while ago I was taught that tail call optimization cannot apply if local variables needs to be kept alive, but ‘g()’ doesn’t seem to require anything to be alive on the stack.
I tried to manually add ‘tail’ to the emitted LLVM IR and it appears to work.

Any idea how I could fix this and let clang automatically generate tail call?

Thanks!

Best,
Haoran

Hi Haoran,

Hi Alexey,

Thanks for the explanation! That’s really a tricky case, I understand it now.

Best,
Haoran

Alexey <avl.lapshin@gmail.com> 于2020年10月3日周六 上午3:35写道: