[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
This commit is contained in:
Sanjoy Das 2016-11-01 02:58:30 +00:00
parent 6e5610fa4d
commit 9c729067e9
1 changed files with 47 additions and 33 deletions

View File

@ -135,28 +135,29 @@ using namespace llvm;
static cl::opt<bool> 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<typename MDNodeTy>
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<MDNodeTy> getParent() const {
if (Node->getNumOperands() < 2)
return TBAANode();
MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
return TBAANodeImpl<MDNodeTy>();
MDNodeTy *P = dyn_cast_or_null<MDNodeTy>(Node->getOperand(1));
if (!P)
return TBAANode();
return TBAANodeImpl<MDNodeTy>();
// Ok, this node has a valid parent. Return it.
return TBAANode(P);
return TBAANodeImpl<MDNodeTy>(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<const MDNode> TBAANode;
typedef TBAANodeImpl<MDNode> 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<typename MDNodeTy>
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<MDNode>(Node->getOperand(0));
}
const MDNode *getAccessType() const {
MDNodeTy *getAccessType() const {
return dyn_cast_or_null<MDNode>(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<const MDNode> TBAAStructTagNode;
typedef TBAAStructTagNodeImpl<MDNode> 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<MDNode>(A->getOperand(1));
A = cast_or_null<MDNode>(MutableTBAAStructTagNode(A).getAccessType());
if (!A)
return nullptr;
B = cast_or_null<MDNode>(B->getOperand(1));
B = cast_or_null<MDNode>(MutableTBAAStructTagNode(B).getAccessType());
if (!B)
return nullptr;
}
SmallSetVector<MDNode *, 4> 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<MDNode>(T->getOperand(1))
: nullptr;
PathA.insert(TA.getNode());
TA = TA.getParent();
}
SmallSetVector<MDNode *, 4> 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<MDNode>(T->getOperand(1))
: nullptr;
PathB.insert(TB.getNode());
TB = TB.getParent();
}
int IA = PathA.size() - 1;