![]() |
| | #16 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| gcc 4.0 has "whole program optimization", which, like MS Visual Studio I believe in the most recent version(s) and I know Intel's compilers, allow the ENTIRE program to be considered for inlining, common subexpression, constant folding, and other such tricks that can not be solved with a traditional compiler that produces machine code immediately from each compilation unit (compilation unit == .cpp file in general, although sometimes that's not entirely true). Expect more of this from other compiler vendors, as machines get faster, memories bigger, etc. It used to be that a compiler needed to live within a few megabytes (or in the case of Turbo C, be able to generate code in a machine with 2MB of memory, and 640KB limit in some cases). Nowadays, machines have hundreds of megabytes or even gigabytes of RAM, and processors run 10-20x the speed they did 15 years ago. So we can allow the compiler to do more complicated "thinkiing", and still be able to compile more complicated code better than before. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #17 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| Eh? I thought LTO was still an experimental branch of the current GCC development.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #18 | |
| Registered Abuser Join Date: Jun 2006 Location: Toronto
Posts: 584
| Isn't function inlining only enabled during optimizations (i.e. by default optimization is off). In either case you may also be able to control the behaviour given some compile-time options: Quote:
__________________ MSDN knows exactly what you're looking for... | |
| @nthony is offline | |
| | #19 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| Of course it is enabled with optimizations only. If you do not enable optimizations, the compiler will leave the code alone - not make any changes. It does make sense to have it on when optimizations are not enabled.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is online now | |
| | #20 | |
| Kung Fu Kitty Join Date: Oct 2008 Location: Montreal, Canada
Posts: 107
| Quote:
Besides, I once tested how GNU dealt with uncalled leaf functions. I defined a bogus function in a program that never gets called, and it was still included in the source. I would think that removing an uncalled leaf would be the most basic of LTOs. | |
| Angus is offline | |
| | #21 | |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| Quote:
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law | |
| CornedBee is offline | |
| | #22 |
| Kung Fu Kitty Join Date: Oct 2008 Location: Montreal, Canada
Posts: 107
| I don't remember. I don't think I used any special keywords. It wasn't in a shared library, though, it was part of an executable. In what context do you mean "visibility"? |
| Angus is offline | |
| | #23 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| ELF visibility is whether other modules of the program can import a function. It applies equally to shared libraries (.so) as to core executables - ELF doesn't see much of a difference between them. If the visibility is the default public, then the linker cannot remove the function, because even shared objects loaded dynamically with dlopen() could reference it.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #24 | |
| Kung Fu Kitty Join Date: Oct 2008 Location: Montreal, Canada
Posts: 107
| Quote:
| |
| Angus is offline | |
| | #25 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| static, in the case of functions, will mean that the compiler KNOWS that no other module uses this function (unless it's used as a function pointer). -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #26 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| That's compiler-specific. Most use GCC's __attribute__ syntax. E.g. Code: __attribute__((visibility("hidden"))) void fn(); // Function is not visible outside the module.
__attribute__((visibility("default"))) void some_function(); // Function is visible.
http://gcc.gnu.org/wiki/Visibility Edit: And yeah, static functions are marked hidden automatically. I think those in the unnamed namespace might be as well.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #27 | |
| Kung Fu Kitty Join Date: Oct 2008 Location: Montreal, Canada
Posts: 107
| Quote:
| |
| Angus is offline | |
| | #28 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
For example, I could write some code in a shared library that does this: Code: extern void ErrorHandler(int errorcode);
void Error(int errorcode)
{
CleanUp();
ErrorHandler(errorcode);
}
I admit this is not a COMMON scenario, but it's certainly possible - and the compiler can't assume (at least not without special options supplied) that this MUSTN'T happen. In fact I have seen code similar to that in RTOS implementations, where the user-supplied side is SUPPOSED to supply certain functions with certain names and arguments, or the system will not work. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #29 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| Moreover, the executable can contain code like this: Code: for every file in directory
if file is a shared library
load shared library
if shared library contains plugin init function
register and initialize plugin
else
unload shared library
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #30 | |
| Kung Fu Kitty Join Date: Oct 2008 Location: Montreal, Canada
Posts: 107
| Quote:
Still, I don't believe GNU does LTO, and if you can't rely on LTO the onus for optimization wrt inlining is on the programmer. Last edited by Angus; 10-28-2008 at 09:55 AM. | |
| Angus is offline | |
![]() |
| Tags |
| constructors, destructors, inline |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Code review | Elysia | C++ Programming | 71 | 05-13-2008 09:42 PM |
| Inline functions and inheritance | hpy_gilmore8 | C++ Programming | 3 | 01-14-2004 06:46 PM |
| Certain functions | Lurker | C++ Programming | 3 | 12-26-2003 01:26 AM |
| bit shifting | Nor | C++ Programming | 9 | 08-08-2003 11:55 AM |
| When does the compiler listen to the inline specifier | Dang | C++ Programming | 5 | 09-17-2001 06:18 AM |