c++ - Dead virtual function elimination -


question

(can clang or perhaps other optimizing tool shipped llvm identify unused virtual functions in c++ program, mark them dead code elimination? guess not.)

if there no such functionality shipped llvm, how 1 go implementing thing this? what's appropriate layer achieve this, , can find examples on build this?

thoughts

my first thought optimizer working on llvm bitcode or ir. after all, lot of optimizers written representation. simple dead code elimination easy enough: function neither called nor has address taken , stored somewhere dead code , can omitted final binary. virtual function has address taken , stored in virtual function table of corresponding class. in order identify whether function has chance of getting called, optimizer not have identify virtual function calls, identify type hierarchy map these virtual function calls possible implementations.

this makes things quite hard tackle @ bitcode level. might better handle somewhere closer front end, @ stage more type information available, , calls virtual function might more readily associated implementations of these functions. perhaps virtualcallchecker serve starting point.

one problem fact while it's possible combine bitcode of several objects single unit link time optimization, 1 hardly ever compiles source code of moderately sized project single translation unit. association between virtual function calls , implementations might have somehow maintained till stage. don't know if kind of custom annotation possible llvm; have seen no indication of in language specification.

but i'm having bit of trouble language specification in case. reference virtual in there virtuality , virtualindex properties of mdsubprogram, far have found no information @ semantics. no documentation, nor useful places inside llvm source code. might looking @ wrong documentation use case.

cross references

eliminate unused virtual functions asked pretty same thing in context of gcc, i'm looking llvm solution here. there used -fvtable-gc switch gcc, apparently buggy , got punted, , clang doesn't support either.

example:

struct foo {   virtual ~foo() { }   virtual int a() { return 12345001; }   virtual int b() { return 12345002; } };  struct bar : public foo {   virtual ~bar() { }   virtual int a() { return 12345003; }   virtual int b() { return 12345004; } };  int main(int argc, char** argv) {   foo* p = (argc & 1 ? new foo() : new bar());   int res = p->a();   delete p;   return res; }; 

how can write tool automatically rid of foo::b() , bar::b() in generated code? clang++ -fuse-ld=gold -o3 -flto clang 3.5.1 wasn't enough, objdump -d -c of resulting executable showed.

question focus changed

originally had been asking not how use clang or llvm effect, possibly third party tools achieve same if clang , llvm not task. questions asking tools frowned upon here, though, focus has shifted finding tool writing one. guess chances finding 1 slim in case, since web search revealed no hints in direction.


Popular posts from this blog