[Bug c++/82476] New: C++: Inlining fails for a simple function
arun11299 at gmail dot com
gcc-bugzilla@gcc.gnu.org
Sun Oct 8 17:48:00 GMT 2017
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82476
Bug ID: 82476
Summary: C++: Inlining fails for a simple function
Product: gcc
Version: 7.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: arun11299 at gmail dot com
Target Milestone: ---
Hello Folks,
I was just trying to figure out if a particular code of mine would provide me
with optimized code or not at O2 optimization level.
The code is:
{code}
#include <cstdio>
#include <utility>
class Object
{
public:
Object() { puts("Cons\n"); }
Object(const Object&) { puts("Copy Cons\n"); }
Object& operator=(Object&) { puts("Assign Cons\n"); return *this; }
Object(Object&&) { puts("Move Cons\n"); }
Object& operator=(Object&&) { puts("Move Assign\n"); return *this; }
~Object() { puts("Destructor\n"); }
};
struct result_type
{
Object obj;
unsigned val;
};
Object create_object()
{
Object obj;
return obj;
}
result_type get_result()
{
auto obj = create_object();
return {std::move(obj), 42};
//return std::pair<Object, unsigned>{obj, 42};
}
int main() {
auto res = get_result();
return 0;
}
{code}
Nothing fancy in here. Just a function returning an object by value.
The assembly generated (seen via https://godbolt.org/g/nvg1o4)
{code}
.LC0:
.string "Cons\n"
create_object():
push rbx
mov rbx, rdi
mov edi, OFFSET FLAT:.LC0
call puts
mov rax, rbx
pop rbx
ret
.LC1:
.string "Move Cons\n"
.LC2:
.string "Destructor\n"
get_result():
push rbx
mov rbx, rdi
mov edi, OFFSET FLAT:.LC0
call puts
mov edi, OFFSET FLAT:.LC1
call puts
mov DWORD PTR [rbx+4], 42
mov edi, OFFSET FLAT:.LC2
call puts
mov rax, rbx
pop rbx
ret
mov rbx, rax
mov edi, OFFSET FLAT:.LC2
call puts
mov rdi, rbx
call _Unwind_Resume
main:
sub rsp, 24
lea rdi, [rsp+8]
call get_result()
mov edi, OFFSET FLAT:.LC2
call puts
xor eax, eax
add rsp, 24
ret
{code}
As can be seen in the 'main' subroutine, "get_result" function call is not
getting inlined. Though it does get inlined if I add the "inline" keyword to
the function, but I dont think it should be required. Clang 5.0 is able to
inline the function call.
Compilation arguments: -O2 -std=c++14 -finline-functions
G++ version : 7.2
Ran on: Compiler explorer https://godbolt.org/g/nvg1o4
Thanks guys.
More information about the Gcc-bugs
mailing list