Best practice for very large if-else-statement using LLVMs RTTI system

Viewed 63

I am currently writing a piece of software that relies on another library, which makes heavy use of LLVMs RTTI system. I cannot change the API of said library and it forces me to implement very large if-else-statements over several types and their sub-types. Usually I would have used at a switch-statement instead, but that is obviously not possible using LLVM's dyn_cast<>().

Below is an example of what I am currently doing, however, this turned out to be a real bottleneck. Are there any better ways of achieving the same, but with less overhead?

Thanks a ton!

 if (const SomeClass *casted = dyn_cast<SomeClass>(something))
 {
     ...
 }

 else if (const anotherClass *casted = dyn_cast<AnotherClass>(something))
 {
     ...
 }

 ...

 else
 {
     ...
 }
2 Answers

Reffereing LLVM Programmers Manual. InstVisitor is recommended in such scenarios.

Note that the dyn_cast<> operator, like C++’s dynamic_cast<> or Java’s instanceof operator, can be abused. In particular, you should not use big chained if/then/else blocks to check for lots of different variants of classes. If you find yourself wanting to do this, it is much cleaner and more efficient to use the InstVisitor class to dispatch over the instruction type directly.

Related