Evan Jones
2005-Apr-22 01:34 UTC
[LLVMdev] isa and friends as an alternative to dynamic cast?
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) and there are some other similar casting tools defined in Casting.h. Why should I use these instead of C++'s built-in features? Evan Jones
Misha Brukman
2005-Apr-22 01:46 UTC
[LLVMdev] isa and friends as an alternative to dynamic cast?
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
Tanya Lattner
2005-Apr-22 02:20 UTC
[LLVMdev] isa and friends as an alternative to dynamic cast?
> 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)Misha seems to have answered your question, but documentation is found here: http://llvm.cs.uiuc.edu/docs/ProgrammersManual.html#isa -Tanya
Evan Jones
2005-Apr-22 02:43 UTC
[LLVMdev] isa and friends as an alternative to dynamic cast?
On Thu, 2005-21-04 at 21:20 -0500, Tanya Lattner wrote:> Misha seems to have answered your question, but documentation is found > here: > http://llvm.cs.uiuc.edu/docs/ProgrammersManual.html#isaAh, perfect! Thank you both. Evan Jones
Reid Spencer
2005-Apr-22 02:43 UTC
[LLVMdev] isa and friends as an alternative to dynamic cast?
Evan, In case it wasn't obvious from Misha's answer, the main reason for doing this is speed. RTTI is not very quick. Since LLVM is well contained, there are no IR classes that LLVM doesn't know about it. Using dynamic_cast is really only warranted when blending libraries together that have inheritance relationships between them that aren't known in one or more of the libraries. Since we don't have that situation in LLVM, using the various Casting.h facilities allows all that "dynamic" casting to actually be done statically, hence no runtime performance penalty. If you wonder why LLVM is fast .. this is just one of the many reasons. Reid. On Thu, 2005-04-21 at 21:34 -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) > > and there are some other similar casting tools defined in Casting.h. Why > should I use these instead of C++'s built-in features? > > Evan Jones > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://mail.cs.uiuc.edu/mailman/listinfo/llvmdev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20050421/303632f8/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20050421/303632f8/attachment.sig>
Evan Jones
2005-Apr-22 02:47 UTC
[LLVMdev] isa and friends as an alternative to dynamic cast?
On Thu, 2005-21-04 at 19:43 -0700, Reid Spencer wrote:> In case it wasn't obvious from Misha's answer, the main reason for > doing this is speed. RTTI is not very quick.Right. This is why I was somewhat suprized to see the "isa" facilities included in LLVM without also disabling rtti. It will reduce the memory footprint a fair bit if you do disable it, at least based on my experience with other C++ projects. Evan Jones
Chris Lattner
2005-Apr-22 05:07 UTC
[LLVMdev] isa and friends as an alternative to dynamic cast?
On Thu, 21 Apr 2005, Reid Spencer wrote:> one or more of the libraries. Since we don't have that situation in > LLVM, using the various Casting.h facilities allows all that "dynamic" > casting to actually be done statically, hence no runtime performance > penalty.Reid is right. isa on an instruction literally loads a field out of the instruction and compares it against a constant. As another example, a good compiler (ahem, like LLVM :) ), turns this: if (... = dyn_cast<LoadInst>(Y)) { ... } else if (... = dyn_cast<StoreInst>(Y)) { ... } else if (... = dyn_cast<AllocaInst>(Y)) { ... Into: switch (Y->getOpcode()) { case Instruction::Load: ... case Instruction::Store: ... case Instruction::AllocaInst: ... Which is something that is just not possible with RTTI. -Chris -- http://nondot.org/sabre/ http://llvm.cs.uiuc.edu/
Apparently Analagous Threads
- [LLVMdev] isa and friends as an alternative to dynamic cast?
- [LLVMdev] isa and friends as an alternative to dynamic cast?
- [LLVMdev] TableGen: Requesting feedback for "TGContext"
- [LLVMdev] isa and friends as an alternative to dynamic cast?
- [LLVMdev] TableGen: Requesting feedback for "TGContext"