TypeBasedAliasAnalysis.cpp revision de38897cfc49f09ffc84fb023a76c076f4b2d402
1c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//===- TypeBasedAliasAnalysis.cpp - Type-Based Alias Analysis -------------===//
2c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//
3c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//                     The LLVM Compiler Infrastructure
4c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//
5c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// This file is distributed under the University of Illinois Open Source
6c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// License. See LICENSE.TXT for details.
7c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//
8c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//===----------------------------------------------------------------------===//
9c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//
10c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// This file defines the TypeBasedAliasAnalysis pass, which implements
11c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// metadata-based TBAA.
12c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//
13c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// In LLVM IR, memory does not have types, so LLVM's own type system is not
14c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// suitable for doing TBAA. Instead, metadata is added to the IR to describe
15c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// a type system of a higher level language.
16c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//
17c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// This pass is language-independent. The type system is encoded in
18c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// metadata. This allows this pass to support typical C and C++ TBAA, but
19c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// it can also support custom aliasing behavior for other languages.
20c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//
21c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// This is a work-in-progress. It doesn't work yet, and the metadata
22c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// format isn't stable.
23c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//
24de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// The current metadata format is very simple. MDNodes have up to three
25de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// fields, e.g.:
26de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman//   !0 = metadata !{ !"name", !1, 0 }
27de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// The first field is an identity field. It can be any MDString which
28de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// uniquely identifies the type. The second field identifies the type's
29de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// parent node in the tree, or is null or omitted for a root node.
30de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// If the third field is present, it's an integer which if equal to 1
31de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// indicates that the type is "constant".
32de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman//
33de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// TODO: The current metadata encoding scheme doesn't support struct
34de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// fields. For example:
35de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman//   struct X {
36de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman//     double d;
37de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman//     int i;
38de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman//   };
39de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman//   void foo(struct X *x, struct X *y, double *p) {
40de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman//     *x = *y;
41de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman//     *p = 0.0;
42de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman//   }
43de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// Struct X has a double member, so the store to *x can alias the store to *p.
44de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// Currently it's not possible to precisely describe all the things struct X
45de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// aliases, so struct assignments must use conservative TBAA nodes. There's
46de38897cfc49f09ffc84fb023a76c076f4b2d402Dan Gohman// no scheme for attaching metadata to @llvm.memcpy yet either.
47c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//
48c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman//===----------------------------------------------------------------------===//
49c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
50c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman#include "llvm/Analysis/AliasAnalysis.h"
51c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman#include "llvm/Analysis/Passes.h"
52c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman#include "llvm/Module.h"
53c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman#include "llvm/Metadata.h"
54c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman#include "llvm/Pass.h"
5501b58f637c23e203dd95a4709bdd40cdfc31ffa9Dan Gohman#include "llvm/Support/CommandLine.h"
56c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohmanusing namespace llvm;
57c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
5801b58f637c23e203dd95a4709bdd40cdfc31ffa9Dan Gohman// For testing purposes, enable TBAA only via a special option.
5901b58f637c23e203dd95a4709bdd40cdfc31ffa9Dan Gohmanstatic cl::opt<bool> EnableTBAA("enable-tbaa");
6001b58f637c23e203dd95a4709bdd40cdfc31ffa9Dan Gohman
61c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohmannamespace {
62c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  /// TBAANode - This is a simple wrapper around an MDNode which provides a
63c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  /// higher-level interface by hiding the details of how alias analysis
64c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  /// information is encoded in its operands.
65c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  class TBAANode {
66c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    const MDNode *Node;
67c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
68c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  public:
69c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    TBAANode() : Node(0) {}
70e6291ae96efa1e27ca6606ac59b17e35d45c057eDan Gohman    explicit TBAANode(const MDNode *N) : Node(N) {}
71c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
72c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    /// getNode - Get the MDNode for this TBAANode.
73c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    const MDNode *getNode() const { return Node; }
74c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
750b62f95ea81d8954c21a2ae54602be10e5e1bb7eDan Gohman    /// getParent - Get this TBAANode's Alias tree parent.
76c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    TBAANode getParent() const {
77c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      if (Node->getNumOperands() < 2)
78c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman        return TBAANode();
79c05d8aa6db088b0f2e2d569d59d87de11cec9c29Dan Gohman      MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
80c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      if (!P)
81c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman        return TBAANode();
82c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      // Ok, this node has a valid parent. Return it.
83c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      return TBAANode(P);
84c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    }
85c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
86c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    /// TypeIsImmutable - Test if this TBAANode represents a type for objects
87c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    /// which are not modified (by any means) in the context where this
88c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    /// AliasAnalysis is relevant.
89c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    bool TypeIsImmutable() const {
90c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      if (Node->getNumOperands() < 3)
91c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman        return false;
92c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      ConstantInt *CI = dyn_cast<ConstantInt>(Node->getOperand(2));
93c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      if (!CI)
94c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman        return false;
95c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      // TODO: Think about the encoding.
96c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      return CI->isOne();
97c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    }
98c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  };
99c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman}
100c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
101c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohmannamespace {
102c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  /// TypeBasedAliasAnalysis - This is a simple alias analysis
103c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  /// implementation that uses TypeBased to answer queries.
104c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  class TypeBasedAliasAnalysis : public ImmutablePass,
105c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman                                 public AliasAnalysis {
106c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  public:
107c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    static char ID; // Class identification, replacement for typeinfo
108081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    TypeBasedAliasAnalysis() : ImmutablePass(ID) {
109081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson      initializeTypeBasedAliasAnalysisPass(*PassRegistry::getPassRegistry());
110081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    }
111c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
112633e7023177837537adabf775395ebe7ab51b38fDan Gohman    virtual void initializePass() {
113633e7023177837537adabf775395ebe7ab51b38fDan Gohman      InitializeAliasAnalysis(this);
114633e7023177837537adabf775395ebe7ab51b38fDan Gohman    }
115633e7023177837537adabf775395ebe7ab51b38fDan Gohman
116c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    /// getAdjustedAnalysisPointer - This method is used when a pass implements
117c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    /// an analysis interface through multiple inheritance.  If needed, it
118c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    /// should override this to adjust the this pointer as needed for the
119c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    /// specified pass info.
12090c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson    virtual void *getAdjustedAnalysisPointer(const void *PI) {
12190c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson      if (PI == &AliasAnalysis::ID)
122c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman        return (AliasAnalysis*)this;
123c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      return this;
124c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    }
125c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
126ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman    bool Aliases(const MDNode *A, const MDNode *B) const;
127ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman
128c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  private:
129c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    virtual void getAnalysisUsage(AnalysisUsage &AU) const;
130b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    virtual AliasResult alias(const Location &LocA, const Location &LocB);
131b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    virtual bool pointsToConstantMemory(const Location &Loc);
132c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  };
133c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman}  // End of anonymous namespace
134c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
135c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman// Register this pass...
136c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohmanchar TypeBasedAliasAnalysis::ID = 0;
137c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan GohmanINITIALIZE_AG_PASS(TypeBasedAliasAnalysis, AliasAnalysis, "tbaa",
138ce665bd2e2b581ab0858d1afe359192bac96b868Owen Anderson                   "Type-Based Alias Analysis", false, true, false)
139c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
140c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan GohmanImmutablePass *llvm::createTypeBasedAliasAnalysisPass() {
141c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  return new TypeBasedAliasAnalysis();
142c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman}
143c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
144c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohmanvoid
145c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan GohmanTypeBasedAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
146c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  AU.setPreservesAll();
147c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  AliasAnalysis::getAnalysisUsage(AU);
148c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman}
149c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
150ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman/// Aliases - Test whether the type represented by A may alias the
151ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman/// type represented by B.
152ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohmanbool
153ba13864483b696e7f8e2058c3a50b5d901f2213bDan GohmanTypeBasedAliasAnalysis::Aliases(const MDNode *A,
154ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman                                const MDNode *B) const {
155c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  // Keep track of the root node for A and B.
156c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  TBAANode RootA, RootB;
157c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
1580b62f95ea81d8954c21a2ae54602be10e5e1bb7eDan Gohman  // Climb the tree from A to see if we reach B.
159ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  for (TBAANode T(A); ; ) {
160ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman    if (T.getNode() == B)
161c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      // B is an ancestor of A.
162ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman      return true;
163c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
164c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    RootA = T;
165c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    T = T.getParent();
166c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    if (!T.getNode())
167c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      break;
168c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  }
169c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
1700b62f95ea81d8954c21a2ae54602be10e5e1bb7eDan Gohman  // Climb the tree from B to see if we reach A.
171ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  for (TBAANode T(B); ; ) {
172ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman    if (T.getNode() == A)
173c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      // A is an ancestor of B.
174ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman      return true;
175c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
176c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    RootB = T;
177c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    T = T.getParent();
178c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman    if (!T.getNode())
179c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman      break;
180c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  }
181c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
182c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  // Neither node is an ancestor of the other.
183c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
184c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  // If they have different roots, they're part of different potentially
185c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  // unrelated type systems, so we must be conservative.
186ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  if (RootA.getNode() != RootB.getNode())
187ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman    return true;
188ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman
189ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  // If they have the same root, then we've proved there's no alias.
190ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  return false;
191ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman}
192ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman
193ba13864483b696e7f8e2058c3a50b5d901f2213bDan GohmanAliasAnalysis::AliasResult
194ba13864483b696e7f8e2058c3a50b5d901f2213bDan GohmanTypeBasedAliasAnalysis::alias(const Location &LocA,
195ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman                              const Location &LocB) {
196ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  if (!EnableTBAA)
197ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman    return AliasAnalysis::alias(LocA, LocB);
198ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman
199ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  // Get the attached MDNodes. If either value lacks a tbaa MDNode, we must
200ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  // be conservative.
201ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  const MDNode *AM = LocA.TBAATag;
202ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  if (!AM) return AliasAnalysis::alias(LocA, LocB);
203ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  const MDNode *BM = LocB.TBAATag;
204ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  if (!BM) return AliasAnalysis::alias(LocA, LocB);
205ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman
206ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  // If they may alias, chain to the next AliasAnalysis.
207ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  if (Aliases(AM, BM))
208ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman    return AliasAnalysis::alias(LocA, LocB);
209ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman
210ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  // Otherwise return a definitive result.
211ba13864483b696e7f8e2058c3a50b5d901f2213bDan Gohman  return NoAlias;
212c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman}
213c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
214b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohmanbool TypeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc) {
21501b58f637c23e203dd95a4709bdd40cdfc31ffa9Dan Gohman  if (!EnableTBAA)
21601b58f637c23e203dd95a4709bdd40cdfc31ffa9Dan Gohman    return AliasAnalysis::pointsToConstantMemory(Loc);
21701b58f637c23e203dd95a4709bdd40cdfc31ffa9Dan Gohman
218e6291ae96efa1e27ca6606ac59b17e35d45c057eDan Gohman  const MDNode *M = Loc.TBAATag;
219c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  if (!M) return false;
220c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman
221c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  // If this is an "immutable" type, we can assume the pointer is pointing
222c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman  // to constant memory.
223acf50f5136c6f1daca2e78db756514a88470516bDan Gohman  if (TBAANode(M).TypeIsImmutable())
224acf50f5136c6f1daca2e78db756514a88470516bDan Gohman    return true;
225acf50f5136c6f1daca2e78db756514a88470516bDan Gohman
226acf50f5136c6f1daca2e78db756514a88470516bDan Gohman  return AliasAnalysis::pointsToConstantMemory(Loc);
227c182cb5784cfe557f3c8fa6cfb208c02d2ed42d5Dan Gohman}
228