1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file implements the generic AliasAnalysis interface which is used as the
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// common interface used by all clients and implementations of alias analysis.
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file also implements the default version of the AliasAnalysis interface
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// that is to be used when no other implementation is specified.  This does some
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// simple tests that detect obvious cases: two different global pointers cannot
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// alias, a global cannot alias a malloc, two different mallocs cannot alias,
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// etc.
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This alias analysis implementation really isn't very good for anything, but
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// it is very fast, and makes a nice clean default implementation.  Because it
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// handles lots of little corner cases, other, more complex, alias analysis
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// implementations may choose to rely on this pass to resolve these simple and
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// easy cases.
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Analysis/AliasAnalysis.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Pass.h"
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/BasicBlock.h"
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Function.h"
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/IntrinsicInst.h"
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instructions.h"
3319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/LLVMContext.h"
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Type.h"
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetData.h"
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Register the AliasAnalysis interface, providing a nice name to refer to.
3919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA)
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanchar AliasAnalysis::ID = 0;
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Default chaining methods
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::AliasResult
4719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::alias(const Location &LocA, const Location &LocB) {
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
4919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return AA->alias(LocA, LocB);
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool AliasAnalysis::pointsToConstantMemory(const Location &Loc,
5319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           bool OrLocal) {
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
5519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return AA->pointsToConstantMemory(Loc, OrLocal);
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasAnalysis::deleteValue(Value *V) {
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AA->deleteValue(V);
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasAnalysis::copyValue(Value *From, Value *To) {
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AA->copyValue(From, To);
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid AliasAnalysis::addEscapingUse(Use &U) {
6919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
7019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AA->addEscapingUse(U);
7119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
7219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::ModRefResult
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             const Location &Loc) {
7719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ModRefBehavior MRB = getModRefBehavior(CS);
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MRB == DoesNotAccessMemory)
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return NoModRef;
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ModRefResult Mask = ModRef;
8419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (onlyReadsMemory(MRB))
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask = Ref;
8619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (onlyAccessesArgPointees(MRB)) {
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool doesAlias = false;
8919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (doesAccessArgPointees(MRB)) {
9019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MDNode *CSTag = CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
9119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
9219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman           AI != AE; ++AI) {
9319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        const Value *Arg = *AI;
9419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!Arg->getType()->isPointerTy())
9519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          continue;
9619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Location CSLoc(Arg, UnknownSize, CSTag);
9719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!isNoAlias(CSLoc, Loc)) {
9819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          doesAlias = true;
9919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          break;
10019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
10219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!doesAlias)
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return NoModRef;
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If Loc is a constant memory location, the call definitely could not
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // modify the memory location.
10919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if ((Mask & Mod) && pointsToConstantMemory(Loc))
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask = ModRefResult(Mask & ~Mod);
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If this is the end of the chain, don't forward.
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!AA) return Mask;
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, fall back to the next AA in the chain. But we can merge
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // in any mask we've managed to compute.
11719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::ModRefResult
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
12219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If CS1 or CS2 are readnone, they don't interact.
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ModRefBehavior CS1B = getModRefBehavior(CS1);
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (CS1B == DoesNotAccessMemory) return NoModRef;
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ModRefBehavior CS2B = getModRefBehavior(CS2);
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (CS2B == DoesNotAccessMemory) return NoModRef;
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If they both only read from memory, there is no dependence.
13219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return NoModRef;
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasAnalysis::ModRefResult Mask = ModRef;
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If CS1 only reads memory, the only dependence on CS2 can be
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // from CS1 reading memory written by CS2.
13919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (onlyReadsMemory(CS1B))
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask = ModRefResult(Mask & Ref);
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If CS2 only access memory through arguments, accumulate the mod/ref
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // information from CS1's references to the memory referenced by
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // CS2's arguments.
14519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (onlyAccessesArgPointees(CS2B)) {
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AliasAnalysis::ModRefResult R = NoModRef;
14719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (doesAccessArgPointees(CS2B)) {
14819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MDNode *CS2Tag = CS2.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
14919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (ImmutableCallSite::arg_iterator
15019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman           I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
15119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        const Value *Arg = *I;
15219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!Arg->getType()->isPointerTy())
15319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          continue;
15419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Location CS2Loc(Arg, UnknownSize, CS2Tag);
15519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        R = ModRefResult((R | getModRefInfo(CS1, CS2Loc)) & Mask);
15619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (R == Mask)
15719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          break;
15819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return R;
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If CS1 only accesses memory through arguments, check if CS2 references
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // any of the memory referenced by CS1's arguments. If not, return NoModRef.
16519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (onlyAccessesArgPointees(CS1B)) {
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AliasAnalysis::ModRefResult R = NoModRef;
16719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (doesAccessArgPointees(CS1B)) {
16819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MDNode *CS1Tag = CS1.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
16919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (ImmutableCallSite::arg_iterator
17019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman           I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
17119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        const Value *Arg = *I;
17219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!Arg->getType()->isPointerTy())
17319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          continue;
17419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Location CS1Loc(Arg, UnknownSize, CS1Tag);
17519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (getModRefInfo(CS2, CS1Loc) != NoModRef) {
17619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          R = Mask;
17719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          break;
17819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
18019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (R == NoModRef)
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return R;
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
18519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If this is the end of the chain, don't forward.
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!AA) return Mask;
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, fall back to the next AA in the chain. But we can merge
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // in any mask we've managed to compute.
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::ModRefBehavior
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
19519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ModRefBehavior Min = UnknownModRefBehavior;
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Call back into the alias analysis with the other form of getModRefBehavior
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // to see if it can give a better response.
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const Function *F = CS.getCalledFunction())
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Min = getModRefBehavior(F);
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
20419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If this is the end of the chain, don't forward.
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!AA) return Min;
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, fall back to the next AA in the chain. But we can merge
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // in any result we've managed to compute.
20919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::ModRefBehavior
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::getModRefBehavior(const Function *F) {
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return AA->getModRefBehavior(F);
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// AliasAnalysis non-virtual helper method implementation
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
22219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) {
22319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Location(LI->getPointerOperand(),
22419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  getTypeStoreSize(LI->getType()),
22519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  LI->getMetadata(LLVMContext::MD_tbaa));
22619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
22719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
22819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) {
22919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Location(SI->getPointerOperand(),
23019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  getTypeStoreSize(SI->getValueOperand()->getType()),
23119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  SI->getMetadata(LLVMContext::MD_tbaa));
23219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
23319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
23419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) {
23519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Location(VI->getPointerOperand(),
23619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  UnknownSize,
23719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  VI->getMetadata(LLVMContext::MD_tbaa));
23819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
23919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
24019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::Location
24119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::getLocation(const AtomicCmpXchgInst *CXI) {
24219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Location(CXI->getPointerOperand(),
24319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  getTypeStoreSize(CXI->getCompareOperand()->getType()),
24419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  CXI->getMetadata(LLVMContext::MD_tbaa));
24519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
24619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
24719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::Location
24819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::getLocation(const AtomicRMWInst *RMWI) {
24919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Location(RMWI->getPointerOperand(),
25019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  getTypeStoreSize(RMWI->getValOperand()->getType()),
25119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  RMWI->getMetadata(LLVMContext::MD_tbaa));
25219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
25319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
25419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::Location
25519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::getLocationForSource(const MemTransferInst *MTI) {
25619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint64_t Size = UnknownSize;
25719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
25819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Size = C->getValue().getZExtValue();
25919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
26019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // memcpy/memmove can have TBAA tags. For memcpy, they apply
26119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // to both the source and the destination.
26219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
26319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
26419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Location(MTI->getRawSource(), Size, TBAATag);
26519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
26619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
26719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::Location
26819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) {
26919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint64_t Size = UnknownSize;
27019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
27119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Size = C->getValue().getZExtValue();
27219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
27319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // memcpy/memmove can have TBAA tags. For memcpy, they apply
27419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // to both the source and the destination.
27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
27619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
27719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Location(MTI->getRawDest(), Size, TBAATag);
27819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
27919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
28019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
28119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::ModRefResult
28319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
28419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Be conservative in the face of volatile/atomic.
28519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!L->isUnordered())
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ModRef;
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the load address doesn't alias the given address, it doesn't read
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // or write the specified memory.
29019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!alias(getLocation(L), Loc))
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return NoModRef;
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, a load just reads.
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Ref;
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::ModRefResult
29819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
29919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Be conservative in the face of volatile/atomic.
30019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!S->isUnordered())
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ModRef;
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the store address cannot alias the pointer in question, then the
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // specified memory cannot be modified by the store.
30519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!alias(getLocation(S), Loc))
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return NoModRef;
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the pointer is a pointer to constant memory, then it could not have been
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // modified by this store.
31019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (pointsToConstantMemory(Loc))
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return NoModRef;
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, a store just writes.
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Mod;
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
31719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::ModRefResult
31819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
31919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If the va_arg address cannot alias the pointer in question, then the
32019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // specified memory cannot be accessed by the va_arg.
32119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!alias(getLocation(V), Loc))
32219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return NoModRef;
32319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
32419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If the pointer is a pointer to constant memory, then it could not have been
32519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // modified by this va_arg.
32619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (pointsToConstantMemory(Loc))
32719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return NoModRef;
32819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
32919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Otherwise, a va_arg reads and writes.
33019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return ModRef;
331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
33319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::ModRefResult
33419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) {
33519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
33619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (CX->getOrdering() > Monotonic)
33719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return ModRef;
33819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
33919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If the cmpxchg address does not alias the location, it does not access it.
34019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!alias(getLocation(CX), Loc))
34119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return NoModRef;
34219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
34319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return ModRef;
34419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
34519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
34619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::ModRefResult
34719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) {
34819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
34919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (RMW->getOrdering() > Monotonic)
35019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return ModRef;
35119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
35219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If the atomicrmw address does not alias the location, it does not access it.
35319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!alias(getLocation(RMW), Loc))
35419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return NoModRef;
35519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
35619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return ModRef;
35719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
35819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
35919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// AliasAnalysis destructor: DO NOT move this to the header file for
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// the AliasAnalysis.o file in the current .a file, causing alias analysis
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// support to not be included in the tool correctly!
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasAnalysis::~AliasAnalysis() {}
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// AliasAnalysis interface before any other methods are called.
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TD = P->getAnalysisIfAvailable<TargetData>();
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AA = &P->getAnalysis<AliasAnalysis>();
373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// getAnalysisUsage - All alias analysis implementations should invoke this
376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// directly (using AliasAnalysis::getAnalysisUsage(AU)).
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AU.addRequired<AliasAnalysis>();         // All AA's chain
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getTypeStoreSize - Return the TargetData store size for the given type,
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// if known, or a conservative value otherwise.
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
38419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanuint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) {
38519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return TD ? TD->getTypeStoreSize(Ty) : UnknownSize;
386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// canBasicBlockModify - Return true if it is possible for execution of the
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specified basic block to modify the value pointed to by Ptr.
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
39219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        const Location &Loc) {
39319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return canInstructionRangeModify(BB.front(), BB.back(), Loc);
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// canInstructionRangeModify - Return true if it is possible for the execution
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// of the specified instructions to modify the value pointed to by Ptr.  The
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// instructions to consider are all of the instructions in the range of [I1,I2]
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// INCLUSIVE.  I1 and I2 must be in the same basic block.
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                              const Instruction &I2,
40319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                              const Location &Loc) {
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(I1.getParent() == I2.getParent() &&
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Instructions not in same basic block!");
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BasicBlock::const_iterator I = &I1;
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BasicBlock::const_iterator E = &I2;
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ++E;  // Convert from inclusive to exclusive range.
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (; I != E; ++I) // Check every instruction in range
41119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (getModRefInfo(I, Loc) & Mod)
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isNoAliasCall - Return true if this pointer is returned by a noalias
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// function.
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool llvm::isNoAliasCall(const Value *V) {
41919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isa<CallInst>(V) || isa<InvokeInst>(V))
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ImmutableCallSite(cast<Instruction>(V))
421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .paramHasAttr(0, Attribute::NoAlias);
422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isIdentifiedObject - Return true if this pointer refers to a distinct and
426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// identifiable object.  This returns true for:
427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///    Global Variables and Functions (but not Global Aliases)
428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///    Allocas and Mallocs
429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///    ByVal and NoAlias Arguments
430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///    NoAlias returns
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool llvm::isIdentifiedObject(const Value *V) {
433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<AllocaInst>(V))
434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isNoAliasCall(V))
438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const Argument *A = dyn_cast<Argument>(V))
440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return A->hasNoAliasAttr() || A->hasByValAttr();
441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
443