6

I want to inline some functions of which I don't have the code. They are present in an object file. Is there a way to do it with gcc?

In other words, I want to perform inlining of those functions while linking my own code files with the object file that contain those functions.

6
  • 1
    I'm going to guess (not certain, so not an answer): no. An object file is basically already machine code; the compiler cannot optimize across this. At best, inlining could eliminate the function-call overhead, but you're still going to need to to push the stack, etc. Commented Jul 26, 2011 at 16:24
  • 1
    Yes, that was my question, Can the linker perform inlining, that is take the function and embed it as inlined code when generating the full code. Commented Jul 26, 2011 at 16:33
  • 2
    What about whole program optimization? Isn't it possible in gcc? Commented Jul 26, 2011 at 16:36
  • @Metallic - Whole program optimization is possible, but requires a new compiler option. Then you have to recompile all files with this option. Commented Jul 26, 2011 at 16:44
  • 1
    Well the people who have written the code have provided me it in an object form. I am trying to use their library and gain speedup. I wanted to experiment with inlining their functions to see if it improves speed. I know for sure, that those functions don't do compute intensive tasks. Commented Jul 26, 2011 at 17:30

2 Answers 2

7

Starting with version 4.5, GCC supports the -flto switch which enables Link Time Optimization (LTO). LTO can inline functions from separate object files.

There's a catch though. Because of the way that -flto works, it'll only be of use for object files that were compiled using that switch. As I understand it, GCC implements LTO by placing a intermediate form of the source code into the object file - if that intermediate code isn't in the object file, the code in that object file won't be 'inlined'.

See Can the linker inline functions? for some additional details.

Sign up to request clarification or add additional context in comments.

2 Comments

That is interesting. But wouldn't just knowing the start and end address of a function enough to "copy it" and inline it? Or am I talking crazy here?
@hexa: I don't pretend to know the details. I imagine that there are a lot of situations where it's more complex than that. Also, you'd want LTO to be able to handle further optimizations like removing needless copying of parameters and return values. Working from intermediate code makes those optimizations easier/possible. Essentially what GCC's LTO does is allow you to compile all modules as if they were one large C module (while keeping module local names from conflicting with each other), so the compiler can see everything.
2

What you want to do is just the contrary of inlining. Inlining means that you have the source and you want the compiler to generate code as if that source was defined in place of the caller.

What with some effort perhaps could be done would be to extract the object code and place it inside the newly generated object code for your functions. But this makes not much sense: the only advantage of inlining is that the optimizer can work across the boundaries of functions. E.g to avoid spill of registers, do constant propagation or eliminate dead code. All this would be nearly impossible when you only have the object.

2 Comments

I've heard such thing as whole program optimization. Isn't it possible in gcc?
Whole program optimization is usually understood as the following: you have access to all source, concat it all in one moster compilation unit and let the optimizer take its pleasure.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.