From 9c729067e93739dcd6deb0f5333d2328425d603c Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Tue, 1 Nov 2016 02:58:30 +0000 Subject: [PATCH] [TBAA] Use wrapper objects instead of raw getOperand s; NFC This is intended to make the semantic intent clearer. The wrapper objects are now generic to avoid `const_cast` s. Since `const` ness is part of the API of `MDNode::getMostGenericTBAA` (and therefore I can't make things `const` all the way through without some code churn outside TypeBasedAliasAnalysis.cpp), this seemed like the cleanest solution. llvm-svn: 285665 --- llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp | 80 ++++++++++++++++------------ 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp index 26d7113..1baa095 100644 --- a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp +++ b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp @@ -135,28 +135,29 @@ using namespace llvm; static cl::opt EnableTBAA("enable-tbaa", cl::init(true)); namespace { -/// TBAANode - This is a simple wrapper around an MDNode which provides a -/// higher-level interface by hiding the details of how alias analysis -/// information is encoded in its operands. -class TBAANode { - const MDNode *Node; +/// This is a simple wrapper around an MDNode which provides a higher-level +/// interface by hiding the details of how alias analysis information is encoded +/// in its operands. +template +class TBAANodeImpl { + MDNodeTy *Node; public: - TBAANode() : Node(nullptr) {} - explicit TBAANode(const MDNode *N) : Node(N) {} + TBAANodeImpl() : Node(nullptr) {} + explicit TBAANodeImpl(MDNodeTy *N) : Node(N) {} /// getNode - Get the MDNode for this TBAANode. - const MDNode *getNode() const { return Node; } + MDNodeTy *getNode() const { return Node; } /// getParent - Get this TBAANode's Alias tree parent. - TBAANode getParent() const { + TBAANodeImpl getParent() const { if (Node->getNumOperands() < 2) - return TBAANode(); - MDNode *P = dyn_cast_or_null(Node->getOperand(1)); + return TBAANodeImpl(); + MDNodeTy *P = dyn_cast_or_null(Node->getOperand(1)); if (!P) - return TBAANode(); + return TBAANodeImpl(); // Ok, this node has a valid parent. Return it. - return TBAANode(P); + return TBAANodeImpl(P); } /// Test if this TBAANode represents a type for objects which are @@ -172,23 +173,31 @@ public: } }; +/// \name Specializations of \c TBAANodeImpl for const and non const qualified +/// \c MDNode. +/// @{ +typedef TBAANodeImpl TBAANode; +typedef TBAANodeImpl MutableTBAANode; +/// @} + /// This is a simple wrapper around an MDNode which provides a /// higher-level interface by hiding the details of how alias analysis /// information is encoded in its operands. -class TBAAStructTagNode { +template +class TBAAStructTagNodeImpl { /// This node should be created with createTBAAStructTagNode. - const MDNode *Node; + MDNodeTy *Node; public: - explicit TBAAStructTagNode(const MDNode *N) : Node(N) {} + explicit TBAAStructTagNodeImpl(MDNodeTy *N) : Node(N) {} /// Get the MDNode for this TBAAStructTagNode. - const MDNode *getNode() const { return Node; } + MDNodeTy *getNode() const { return Node; } - const MDNode *getBaseType() const { + MDNodeTy *getBaseType() const { return dyn_cast_or_null(Node->getOperand(0)); } - const MDNode *getAccessType() const { + MDNodeTy *getAccessType() const { return dyn_cast_or_null(Node->getOperand(1)); } uint64_t getOffset() const { @@ -207,6 +216,13 @@ public: } }; +/// \name Specializations of \c TBAAStructTagNodeImpl for const and non const +/// qualified \c MDNods. +/// @{ +typedef TBAAStructTagNodeImpl TBAAStructTagNode; +typedef TBAAStructTagNodeImpl MutableTBAAStructTagNode; +/// @} + /// This is a simple wrapper around an MDNode which provides a /// higher-level interface by hiding the details of how alias analysis /// information is encoded in its operands. @@ -403,32 +419,30 @@ MDNode *MDNode::getMostGenericTBAA(MDNode *A, MDNode *B) { // For struct-path aware TBAA, we use the access type of the tag. bool StructPath = isStructPathTBAA(A) && isStructPathTBAA(B); if (StructPath) { - A = cast_or_null(A->getOperand(1)); + A = cast_or_null(MutableTBAAStructTagNode(A).getAccessType()); if (!A) return nullptr; - B = cast_or_null(B->getOperand(1)); + B = cast_or_null(MutableTBAAStructTagNode(B).getAccessType()); if (!B) return nullptr; } SmallSetVector PathA; - MDNode *T = A; - while (T) { - if (PathA.count(T)) + MutableTBAANode TA(A); + while (TA.getNode()) { + if (PathA.count(TA.getNode())) report_fatal_error("Cycle found in TBAA metadata."); - PathA.insert(T); - T = T->getNumOperands() >= 2 ? cast_or_null(T->getOperand(1)) - : nullptr; + PathA.insert(TA.getNode()); + TA = TA.getParent(); } SmallSetVector PathB; - T = B; - while (T) { - if (PathB.count(T)) + MutableTBAANode TB(B); + while (TB.getNode()) { + if (PathB.count(TB.getNode())) report_fatal_error("Cycle found in TBAA metadata."); - PathB.insert(T); - T = T->getNumOperands() >= 2 ? cast_or_null(T->getOperand(1)) - : nullptr; + PathB.insert(TB.getNode()); + TB = TB.getParent(); } int IA = PathA.size() - 1; -- 2.7.4