How do I tell that the GCC (and specifically, G ++) optimize the tail recursion in a specific function Is doing ? (Because it has come a number of times: I do not have to test that the GCC can adapt the tail recursion in general. I want to know if the my tail optimizes the regenerative function. )
If your answer is "look at the person who generates" then I want to know what I want, and whether I can write a simple program that examines angler that is optimizable or not Only
I know that this question appears in the form of part of the question 5 months ago. However, I do not think the answer to that question of this part was done satisfactorily. (The answer was "The easiest way to check if compiler optimization (which I know) makes a call that would otherwise see the stack overflow - or assembly output.")
< Div class = "post-text" itemprop = "text">
Let's use. Compile it, but do not assemble the GCC:
gcc -std = c99 -s-o2 test.c
Now let's see that the _atoi
function As a result, the test.s file (GCC 4.0.1 on Mac OS 10.5):
.text. Align 4,0x90 _atoi: pushl% ebp testl% ex,% Ex movl% esp,% ebp movl% ex,% ecx je L3 Align 4,0x90 L5: movzbl (% ecx),% ex testb% al,% al je L3 (% edx,% edx, 4),% edx movsbl% al,% ex incl% ecx leal -48 (% Eax,% edx, 2),% edx jne l5 Align 4xx90 L3: Leave movl% edx,% eax ret
The compiler has optimized tail call on this function. We can tell because there is no call
instruction in that code, whereas the original C code was clearly a function call. In addition, we can see the jne l5
instruction, which jumps backward in the function, which indicates the loop when there was no loop in the C code. If you recompile with optimization, you will see a row called call _atoi
, and you will not see any backlogs.
You can automate it or not another substance. Specification of the assembler code depends on the code you are compiling.
You can search for the program, I think. Print the current value of the stack pointer to function (register ESP on x86). If the function prints the same value as the first call, such as for recursive calls, the compiler has adapted the tail-call. It is necessary to modify the required work for this idea, though, and it may be affected by how the compiler fits the function. If the test is successful (printing the same ESP value both times), then I think it is appropriate to understand that adaptation will be done without your instrumentation, but if the test fails, we do not know it Except for failure instrumentation code.
Comments
Post a Comment