When I compiled a cpp program using g++ -Winline xx.cpp
, I got no warning about the incorrect inline declaration. My code is
#include <iostream>
inline int test(int n)
{
int res=0;
for(int i=0;i<n;++i)
res+=i;
return res;
}
int main()
{
int n=100;
int mul=test(n);
std::cout<<mul<<std::endl;
return 0;
}
The assembly code of main is:
(gdb) disassemble main
Dump of assembler code for function main():
0x080485f4 <+0>: push %ebp
0x080485f5 <+1>: mov %esp,%ebp
0x080485f7 <+3>: and $0xfffffff0,%esp
0x080485fa <+6>: sub $0x20,%esp
0x080485fd <+9>: movl $0x64,0x18(%esp)
0x08048605 <+17>: mov 0x18(%esp),%eax
0x08048609 <+21>: mov %eax,(%esp)
0x0804860c <+24>: call 0x804869c <test(int)>
0x08048611 <+29>: mov %eax,0x1c(%esp)
0x08048615 <+33>: mov 0x1c(%esp),%eax
0x08048619 <+37>: mov %eax,0x4(%esp)
0x0804861d <+41>: movl $0x804a040,(%esp)
0x08048624 <+48>: call 0x80484c0 <_ZNSolsEi@plt>
0x08048629 <+53>: movl $0x8048530,0x4(%esp)
0x08048631 <+61>: mov %eax,(%esp)
0x08048634 <+64>: call 0x8048520 <_ZNSolsEPFRSoS_E@plt>
0x08048639 <+69>: mov $0x0,%eax
0x0804863e <+74>: leave
0x0804863f <+75>: ret
End of assembler dump.
It is clear that test is not inlined, but I wonder why there's no warning about it.
As also stated in the comments (by abyss.7), you won't get inlining unless you enable optimizations (even -O1 would be enough). However your current code would get further optimized then (gcc can statically compute the result), so you won't see that.
Instead, consider a slightly different code:
#include <iostream>
int delta = 1;
inline int test(int n)
{
int res = 0;
for(int i=0;i<n;++i)
res *= delta;
return res;
}
int main()
{
int n=100;
int mul=test(n);
std::cout<<mul<<std::endl;
return 0;
}
The fact that delta is declared globally prevents the compiler from statically optimizing it at this level (since it might be changed by another function). Changing the +=
to *=
prevents it from being multiplied by n and added once, so you would still get the loop.
Compiling with gcc 4.2.2 (oldest one i could find) with no optimizations, you get -
0000000000400a64 <main>:
400a64: 55 push %rbp
400a65: 48 89 e5 mov %rsp,%rbp
400a68: 48 83 ec 10 sub $0x10,%rsp
400a6c: c7 45 f8 64 00 00 00 movl $0x64,0xfffffffffffffff8(%rbp)
400a73: 8b 7d f8 mov 0xfffffffffffffff8(%rbp),%edi
400a76: e8 25 00 00 00 callq 400aa0 <_Z4testi>
400a7b: 89 45 fc mov %eax,0xfffffffffffffffc(%rbp)
400a7e: 8b 75 fc mov 0xfffffffffffffffc(%rbp),%esi
400a81: bf b0 12 50 00 mov $0x5012b0,%edi
400a86: e8 1d fd ff ff callq 4007a8 <_ZNSolsEi@plt>
400a8b: 48 89 c7 mov %rax,%rdi
400a8e: be d8 07 40 00 mov $0x4007d8,%esi
400a93: e8 20 fd ff ff callq 4007b8 <_ZNSolsEPFRSoS_E@plt>
400a98: b8 00 00 00 00 mov $0x0,%eax
400a9d: c9 leaveq
400a9e: c3 retq
Compiling with -O1, it turns into
000000000040083c <main>:
40083c: 48 83 ec 08 sub $0x8,%rsp
400840: 8b 15 12 05 10 00 mov 1049874(%rip),%edx # 500d58 <delta>
400846: be 00 00 00 00 mov $0x0,%esi
40084b: b8 00 00 00 00 mov $0x0,%eax
400850: 0f af f2 imul %edx,%esi
400853: 83 c0 01 add $0x1,%eax
400856: 83 f8 64 cmp $0x64,%eax
400859: 75 f5 jne 400850 <main+0x14>
40085b: bf 60 0d 50 00 mov $0x500d60,%edi
400860: e8 73 fe ff ff callq 4006d8 <_ZNSolsEi@plt>
400865: 48 89 c7 mov %rax,%rdi
400868: e8 7b fe ff ff callq 4006e8 <_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@plt>
40086d: b8 00 00 00 00 mov $0x0,%eax
400872: 48 83 c4 08 add $0x8,%rsp
400876: c3 retq
So the loop got obviously inlined
EDIT
The reason why -Winline doesn't report a warning is that without optimizations gcc never even tries to perform the inline (and therefor doesn't fail).
Adding a variable length array in my above function :
char buf[n];
and compiling with -O1, produces the following warning:
test2.cpp:5: warning: function 'int test(int)' can never be inlined because it uses alloca (override using the always_inline attribute
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.