diff options
| author | Benjamin Kramer <benny.kra@googlemail.com> | 2015-10-21 16:33:15 +0000 |
|---|---|---|
| committer | Benjamin Kramer <benny.kra@googlemail.com> | 2015-10-21 16:33:15 +0000 |
| commit | 998039e2f606cb90841acee47f828b7163309ece (patch) | |
| tree | f9929ae24c495fc19e7eb19d7cf0a97a542c097e | |
| parent | f88d22970d5f7d859b86a17058675564b2b442af (diff) | |
| download | bcm5719-llvm-998039e2f606cb90841acee47f828b7163309ece.tar.gz bcm5719-llvm-998039e2f606cb90841acee47f828b7163309ece.zip | |
Shrink DynTypedNode by one pointer from 40 to 32 bytes (on x86_64).
The MemoizationData cache was introduced to avoid a series of enum
compares at the cost of making DynTypedNode bigger. This change reverts
to using an enum compare but instead of building a chain of comparison
the enum values are reordered so the check can be performed with a
simple greater than. The alternative would be to steal a bit from the
enum but I think that's a more complex solution and not really needed
here.
I tried this on several large .cpp files with clang-tidy and didn't
notice any performance difference. The test change is due to matchers
being sorted by their node kind.
Differential Revision: http://reviews.llvm.org/D13946
llvm-svn: 250905
| -rw-r--r-- | clang/include/clang/AST/ASTTypeTraits.h | 44 | ||||
| -rw-r--r-- | clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp | 4 |
2 files changed, 27 insertions, 21 deletions
diff --git a/clang/include/clang/AST/ASTTypeTraits.h b/clang/include/clang/AST/ASTTypeTraits.h index dc3c34f28d9..420c125d289 100644 --- a/clang/include/clang/AST/ASTTypeTraits.h +++ b/clang/include/clang/AST/ASTTypeTraits.h @@ -106,18 +106,25 @@ public: } }; + /// Check if the given ASTNodeKind identifies a type that offers pointer + /// identity. This is useful for the fast path in DynTypedNode. + bool hasPointerIdentity() const { + return KindId > NKI_LastKindWithoutPointerIdentity; + } + private: /// \brief Kind ids. /// /// Includes all possible base and derived kinds. enum NodeKindId { NKI_None, - NKI_CXXCtorInitializer, NKI_TemplateArgument, - NKI_NestedNameSpecifier, NKI_NestedNameSpecifierLoc, NKI_QualType, NKI_TypeLoc, + NKI_LastKindWithoutPointerIdentity = NKI_TypeLoc, + NKI_CXXCtorInitializer, + NKI_NestedNameSpecifier, NKI_Decl, #define DECL(DERIVED, BASE) NKI_##DERIVED##Decl, #include "clang/AST/DeclNodes.inc" @@ -238,7 +245,11 @@ public: /// Note that this is not supported by all AST nodes. For AST nodes /// that don't have a pointer-defined identity inside the AST, this /// method returns NULL. - const void *getMemoizationData() const { return MemoizationData; } + const void *getMemoizationData() const { + return NodeKind.hasPointerIdentity() + ? *reinterpret_cast<void *const *>(Storage.buffer) + : nullptr; + } /// \brief Prints the node to the given output stream. void print(llvm::raw_ostream &OS, const PrintingPolicy &PP) const; @@ -286,18 +297,18 @@ private: template <typename T, typename BaseT> struct DynCastPtrConverter { static const T *get(ASTNodeKind NodeKind, const char Storage[]) { if (ASTNodeKind::getFromNodeKind<T>().isBaseOf(NodeKind)) - return cast<T>(*reinterpret_cast<BaseT *const *>(Storage)); + return &getUnchecked(NodeKind, Storage); return nullptr; } static const T &getUnchecked(ASTNodeKind NodeKind, const char Storage[]) { assert(ASTNodeKind::getFromNodeKind<T>().isBaseOf(NodeKind)); - return *cast<T>(*reinterpret_cast<BaseT *const *>(Storage)); + return *cast<T>(static_cast<const BaseT *>( + *reinterpret_cast<const void *const *>(Storage))); } static DynTypedNode create(const BaseT &Node) { DynTypedNode Result; Result.NodeKind = ASTNodeKind::getFromNode(Node); - Result.MemoizationData = &Node; - new (Result.Storage.buffer) const BaseT * (&Node); + new (Result.Storage.buffer) const void *(&Node); return Result; } }; @@ -306,18 +317,18 @@ private: template <typename T> struct PtrConverter { static const T *get(ASTNodeKind NodeKind, const char Storage[]) { if (ASTNodeKind::getFromNodeKind<T>().isSame(NodeKind)) - return *reinterpret_cast<T *const *>(Storage); + return &getUnchecked(NodeKind, Storage); return nullptr; } static const T &getUnchecked(ASTNodeKind NodeKind, const char Storage[]) { assert(ASTNodeKind::getFromNodeKind<T>().isSame(NodeKind)); - return **reinterpret_cast<T *const *>(Storage); + return *static_cast<const T *>( + *reinterpret_cast<const void *const *>(Storage)); } static DynTypedNode create(const T &Node) { DynTypedNode Result; Result.NodeKind = ASTNodeKind::getFromNodeKind<T>(); - Result.MemoizationData = &Node; - new (Result.Storage.buffer) const T * (&Node); + new (Result.Storage.buffer) const void *(&Node); return Result; } }; @@ -336,14 +347,12 @@ private: static DynTypedNode create(const T &Node) { DynTypedNode Result; Result.NodeKind = ASTNodeKind::getFromNodeKind<T>(); - Result.MemoizationData = nullptr; new (Result.Storage.buffer) T(Node); return Result; } }; ASTNodeKind NodeKind; - const void *MemoizationData; /// \brief Stores the data of the node. /// @@ -353,12 +362,9 @@ private: /// \c QualTypes, \c NestedNameSpecifierLocs, \c TypeLocs and /// \c TemplateArguments on the other hand do not have storage or unique /// pointers and thus need to be stored by value. - typedef llvm::AlignedCharArrayUnion< - Decl *, Stmt *, Type *, NestedNameSpecifier *, CXXCtorInitializer *> - KindsByPointer; - llvm::AlignedCharArrayUnion<KindsByPointer, TemplateArgument, - NestedNameSpecifierLoc, QualType, TypeLoc> - Storage; + llvm::AlignedCharArrayUnion<const void *, TemplateArgument, + NestedNameSpecifierLoc, QualType, + TypeLoc> Storage; }; template <typename T> diff --git a/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp b/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp index ca1dfefd538..66960a0bb2f 100644 --- a/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp +++ b/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp @@ -455,8 +455,8 @@ TEST_F(RegistryTest, Completion) { // Polymorphic. EXPECT_TRUE(hasCompletion( Comps, "hasDescendant(", - "Matcher<NestedNameSpecifier|NestedNameSpecifierLoc|QualType|...> " - "hasDescendant(Matcher<CXXCtorInitializer|NestedNameSpecifier|" + "Matcher<TemplateArgument|NestedNameSpecifier|NestedNameSpecifierLoc|...>" + " hasDescendant(Matcher<TemplateArgument|NestedNameSpecifier|" "NestedNameSpecifierLoc|...>)")); CompVector WhileComps = getCompletions("whileStmt", 0); |

