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.

MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
  • 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. – Oliver Charlesworth Jul 26 '11 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. – MetallicPriest Jul 26 '11 at 16:33
  • 2
    What about whole program optimization? Isn't it possible in gcc? – MetallicPriest Jul 26 '11 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. – Bo Persson Jul 26 '11 at 16:44
  • How is it that you don't have the code? If you don't have the code, how do you know what/why to inline? – Karl Knechtel Jul 26 '11 at 17:09
  • 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. – MetallicPriest Jul 26 '11 at 17:30

2 Answers2

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.

Community
  • 1
  • 1
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 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? – Vinicius Kamakura Jul 26 '11 at 17:56
  • 1
    @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*. – Michael Burr Jul 26 '11 at 18:13
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.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • I've heard such thing as whole program optimization. Isn't it possible in gcc? – MetallicPriest Jul 26 '11 at 16:35
  • 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. – Jens Gustedt Jul 26 '11 at 16:56