Bug 66355 - defining a constructor inhibits optimization
Summary: defining a constructor inhibits optimization
Status: RESOLVED DUPLICATE of bug 4131
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.8.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-05-31 22:01 UTC by Kenneth Almquist
Modified: 2015-06-10 01:04 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Archive contains wrap-int-[012].cxx and makefile (469 bytes, application/gzip)
2015-05-31 22:01 UTC, Kenneth Almquist
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kenneth Almquist 2015-05-31 22:01:09 UTC
Created attachment 35659 [details]
Archive contains wrap-int-[012].cxx and makefile

Ideally, replacing the type "int" with a wrapper class that contains a single "int" value as a member should not impose any performance penalty.  Unfortunately, that is not the case if the wrapper class has a non-default constructor, which it almost certainly will.

The attached archive contains three programs, which can be compiled to assembly language using the included makefile.

wrap-int-0.cxx contains a very simple integer computation.

wrap-int-1.cxx does the same thing, but the "int" type has been replaced by a wrapper class.  The generated assembly instructions are same, so there is no performance penalty for using the wrapper class.

wrap-int-2.cxx is the same as wrap-int-1.cxx except that a constructor has been added to the wrapper class.  One would expect a good optimizer to generate the same code for this program as for the other two programs, but in fact the generated code is a good deal larger.

I'm guessing that what is happening is that if a type has a non-default constructor, the C++ front end treats constants of that type as variables rather than as contstants, so that even simple optimizations like constant folding are no longer performed by that back end.
Comment 1 Jonathan Wakely 2015-06-01 08:43:48 UTC
If you make the constructors and operator+ constexpr and compile with -std=gnu++14 then you remove the overhead.
Comment 2 Marc Glisse 2015-06-06 08:19:24 UTC
PR 65197 has links to a number of related PRs. The middle-end has no code to handle the case where we detect "late" (i.e. not in the front-end) that the initialization is constant.
Comment 3 Kenneth Almquist 2015-06-10 01:04:21 UTC
Thanks to the pointer provided by Marc Glisse, I found that this issue was reported back in 2001, in PR 4131.

*** This bug has been marked as a duplicate of bug 4131 ***