[LLVMdev] isa and friends as an alternative to dynamic cast?
Misha Brukman
brukman at uiuc.edu
Thu Apr 21 18:46:51 PDT 2005
On Thu, Apr 21, 2005 at 09:34:14PM -0400, Evan Jones wrote:
> I see a bunch of definitions scattered throughout LLVM, and I could not
> find good documentation on them. I don't understand why they exist when
> LLVM is being compiled with RTTI enabled. It seems to me that:
>
> isa<T>(x) is a substitute for (dynamic_cast<T>(x) != NULL)
Not really. C++ dynamic_cast<T>(x), as I understand it, is a dynamic
type-checker that will walk the class hierarchy to determine if "x" is a
subclass of "T", and hence, "is it a thing of type T". This means a
bunch of loads to traverse the tree.
LLVM does not have a complex hierarchy of Instructions and Values, so
this is just extra overhead. If you look at
llvm/include/llvm/Instructions.h, for example, look at the classof()
implementations by each instruction. isa<> is defined in terms of these
classof() member functions. LLVM also provides a dyn_cast<T>(x) that is
"like" dynamic_cast<T>(x), but faster, because it benefits from LLVM's
class hierarchy.
In Casting.h, you'll find:
// isa<X> - Return true if the parameter to the template is an instance
// of the template type argument. Used like this:
//
// if (isa<Type*>(myVal)) { ... }
//
template <typename To, typename From>
inline bool isa_impl(const From &Val) {
return To::classof(&Val);
}
Also take a look at the definition of dyn_cast<X> in the same file.
--
Misha Brukman :: http://misha.brukman.net :: http://llvm.cs.uiuc.edu
More information about the llvm-dev
mailing list