[cfe-dev] Could key functions be used to reason about availability of inline method definitions?

Hans Wennborg via cfe-dev cfe-dev at lists.llvm.org
Mon May 28 07:46:26 PDT 2018


I was poking around some object files and noticed function definitions
that seemed unnecessary. They were for inline functions not actually
used in the file except by reference from a vtable.

For example:

  struct Rectangle {
    virtual int colour() { return 1; }
    virtual void draw();
  };

  struct Square : public Rectangle {
    virtual void draw() override;
  };

  void Square::draw() {}

Both Clang and GCC will emit a definition of Rectangle::colour() here
because it's referenced by Square's vtable which we must define, and
it's an inline function so we don't know if it's defined anywhere
else.

But, don't we actually know it's defined elsewhere? Since Rectangle
has a key function, its vtable will be defined elsewhere. That vtable
will reference Rectangle::colour(), so that too must have a definition
elsewhere. Could we use this reasoning to omit the definition of
colour() in the current TU, or at least mark it available_externally?

I'm thinking exploiting this could allow the compiler to emit a bit
less code. Or is there something I'm missing?

Cheers,
Hans



More information about the cfe-dev mailing list