1 //===- TypeBasedAliasAnalysis.cpp - Type-Based Alias Analysis -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the TypeBasedAliasAnalysis pass, which implements
11 // metadata-based TBAA.
13 // In LLVM IR, memory does not have types, so LLVM's own type system is not
14 // suitable for doing TBAA. Instead, metadata is added to the IR to describe
15 // a type system of a higher level language.
17 // This pass is language-independent. The type system is encoded in
18 // metadata. This allows this pass to support typical C and C++ TBAA, but
19 // it can also support custom aliasing behavior for other languages.
21 // This is a work-in-progress. It doesn't work yet, and the metadata
22 // format isn't stable.
24 // The current metadata format is very simple. MDNodes have up to three
26 // !0 = metadata !{ !"name", !1, 0 }
27 // The first field is an identity field. It can be any MDString which
28 // uniquely identifies the type. The second field identifies the type's
29 // parent node in the tree, or is null or omitted for a root node.
30 // If the third field is present, it's an integer which if equal to 1
31 // indicates that the type is "constant" (meaning
32 // pointsToConstantMemory should return true; see
33 // http://llvm.org/docs/AliasAnalysis.html#OtherItfs).
35 // TODO: The current metadata encoding scheme doesn't support struct
36 // fields. For example:
41 // void foo(struct X *x, struct X *y, double *p) {
45 // Struct X has a double member, so the store to *x can alias the store to *p.
46 // Currently it's not possible to precisely describe all the things struct X
47 // aliases, so struct assignments must use conservative TBAA nodes. There's
48 // no scheme for attaching metadata to @llvm.memcpy yet either.
50 //===----------------------------------------------------------------------===//
52 #include "llvm/Analysis/AliasAnalysis.h"
53 #include "llvm/Analysis/Passes.h"
54 #include "llvm/Module.h"
55 #include "llvm/Metadata.h"
56 #include "llvm/Pass.h"
57 #include "llvm/Support/CommandLine.h"
60 // For testing purposes, enable TBAA only via a special option.
61 static cl::opt<bool> EnableTBAA("enable-tbaa");
64 /// TBAANode - This is a simple wrapper around an MDNode which provides a
65 /// higher-level interface by hiding the details of how alias analysis
66 /// information is encoded in its operands.
71 TBAANode() : Node(0) {}
72 explicit TBAANode(const MDNode *N) : Node(N) {}
74 /// getNode - Get the MDNode for this TBAANode.
75 const MDNode *getNode() const { return Node; }
77 /// getParent - Get this TBAANode's Alias tree parent.
78 TBAANode getParent() const {
79 if (Node->getNumOperands() < 2)
81 MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
84 // Ok, this node has a valid parent. Return it.
88 /// TypeIsImmutable - Test if this TBAANode represents a type for objects
89 /// which are not modified (by any means) in the context where this
90 /// AliasAnalysis is relevant.
91 bool TypeIsImmutable() const {
92 if (Node->getNumOperands() < 3)
94 ConstantInt *CI = dyn_cast<ConstantInt>(Node->getOperand(2));
97 // TODO: Think about the encoding.
104 /// TypeBasedAliasAnalysis - This is a simple alias analysis
105 /// implementation that uses TypeBased to answer queries.
106 class TypeBasedAliasAnalysis : public ImmutablePass,
107 public AliasAnalysis {
109 static char ID; // Class identification, replacement for typeinfo
110 TypeBasedAliasAnalysis() : ImmutablePass(ID) {
111 initializeTypeBasedAliasAnalysisPass(*PassRegistry::getPassRegistry());
114 virtual void initializePass() {
115 InitializeAliasAnalysis(this);
118 /// getAdjustedAnalysisPointer - This method is used when a pass implements
119 /// an analysis interface through multiple inheritance. If needed, it
120 /// should override this to adjust the this pointer as needed for the
121 /// specified pass info.
122 virtual void *getAdjustedAnalysisPointer(const void *PI) {
123 if (PI == &AliasAnalysis::ID)
124 return (AliasAnalysis*)this;
128 bool Aliases(const MDNode *A, const MDNode *B) const;
131 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
132 virtual AliasResult alias(const Location &LocA, const Location &LocB);
133 virtual bool pointsToConstantMemory(const Location &Loc);
135 } // End of anonymous namespace
137 // Register this pass...
138 char TypeBasedAliasAnalysis::ID = 0;
139 INITIALIZE_AG_PASS(TypeBasedAliasAnalysis, AliasAnalysis, "tbaa",
140 "Type-Based Alias Analysis", false, true, false)
142 ImmutablePass *llvm::createTypeBasedAliasAnalysisPass() {
143 return new TypeBasedAliasAnalysis();
147 TypeBasedAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
148 AU.setPreservesAll();
149 AliasAnalysis::getAnalysisUsage(AU);
152 /// Aliases - Test whether the type represented by A may alias the
153 /// type represented by B.
155 TypeBasedAliasAnalysis::Aliases(const MDNode *A,
156 const MDNode *B) const {
157 // Keep track of the root node for A and B.
158 TBAANode RootA, RootB;
160 // Climb the tree from A to see if we reach B.
161 for (TBAANode T(A); ; ) {
162 if (T.getNode() == B)
163 // B is an ancestor of A.
172 // Climb the tree from B to see if we reach A.
173 for (TBAANode T(B); ; ) {
174 if (T.getNode() == A)
175 // A is an ancestor of B.
184 // Neither node is an ancestor of the other.
186 // If they have different roots, they're part of different potentially
187 // unrelated type systems, so we must be conservative.
188 if (RootA.getNode() != RootB.getNode())
191 // If they have the same root, then we've proved there's no alias.
195 AliasAnalysis::AliasResult
196 TypeBasedAliasAnalysis::alias(const Location &LocA,
197 const Location &LocB) {
199 return AliasAnalysis::alias(LocA, LocB);
201 // Get the attached MDNodes. If either value lacks a tbaa MDNode, we must
203 const MDNode *AM = LocA.TBAATag;
204 if (!AM) return AliasAnalysis::alias(LocA, LocB);
205 const MDNode *BM = LocB.TBAATag;
206 if (!BM) return AliasAnalysis::alias(LocA, LocB);
208 // If they may alias, chain to the next AliasAnalysis.
210 return AliasAnalysis::alias(LocA, LocB);
212 // Otherwise return a definitive result.
216 bool TypeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc) {
218 return AliasAnalysis::pointsToConstantMemory(Loc);
220 const MDNode *M = Loc.TBAATag;
221 if (!M) return false;
223 // If this is an "immutable" type, we can assume the pointer is pointing
224 // to constant memory.
225 if (TBAANode(M).TypeIsImmutable())
228 return AliasAnalysis::pointsToConstantMemory(Loc);