blob: 373524b6c151d07ae9c2ab66920a44ab935e0221 [file] [log] [blame]
Chris Lattner53ad0ed2002-08-22 18:25:32 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner53ad0ed2002-08-22 18:25:32 +00009//
10// This file implements the generic AliasAnalysis interface which is used as the
11// common interface used by all clients and implementations of alias analysis.
12//
13// This file also implements the default version of the AliasAnalysis interface
14// that is to be used when no other implementation is specified. This does some
15// simple tests that detect obvious cases: two different global pointers cannot
16// alias, a global cannot alias a malloc, two different mallocs cannot alias,
17// etc.
18//
19// This alias analysis implementation really isn't very good for anything, but
20// it is very fast, and makes a nice clean default implementation. Because it
21// handles lots of little corner cases, other, more complex, alias analysis
22// implementations may choose to rely on this pass to resolve these simple and
23// easy cases.
24//
25//===----------------------------------------------------------------------===//
26
Chris Lattnerd501c132003-02-26 19:41:54 +000027#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000028#include "llvm/BasicBlock.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000029#include "llvm/iMemory.h"
Chris Lattner14ac8772003-02-26 19:26:51 +000030#include "llvm/Target/TargetData.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000031
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
33
Chris Lattner53ad0ed2002-08-22 18:25:32 +000034// Register the AliasAnalysis interface, providing a nice name to refer to.
Chris Lattnerdc1ad192003-02-03 21:16:17 +000035namespace {
36 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
Chris Lattnerdc1ad192003-02-03 21:16:17 +000037}
Chris Lattner53ad0ed2002-08-22 18:25:32 +000038
Chris Lattner14ac8772003-02-26 19:26:51 +000039AliasAnalysis::ModRefResult
40AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
41 return alias(L->getOperand(0), TD->getTypeSize(L->getType()),
42 P, Size) ? Ref : NoModRef;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000043}
44
Chris Lattner14ac8772003-02-26 19:26:51 +000045AliasAnalysis::ModRefResult
46AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
Chris Lattnerf4d904d2004-01-30 22:16:42 +000047 // If the stored address cannot alias the pointer in question, then the
48 // pointer cannot be modified by the store.
49 if (!alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()),
50 P, Size))
51 return NoModRef;
52
53 // If the pointer is a pointer to constant memory, then it could not have been
54 // modified by this store.
55 return pointsToConstantMemory(P) ? NoModRef : Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +000056}
57
58
Chris Lattner53ad0ed2002-08-22 18:25:32 +000059// AliasAnalysis destructor: DO NOT move this to the header file for
60// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
61// the AliasAnalysis.o file in the current .a file, causing alias analysis
62// support to not be included in the tool correctly!
63//
64AliasAnalysis::~AliasAnalysis() {}
65
Chris Lattner14ac8772003-02-26 19:26:51 +000066/// setTargetData - Subclasses must call this method to initialize the
67/// AliasAnalysis interface before any other methods are called.
68///
69void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
70 TD = &P->getAnalysis<TargetData>();
71}
72
73// getAnalysisUsage - All alias analysis implementations should invoke this
74// directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
75// TargetData is required by the pass.
76void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
77 AU.addRequired<TargetData>(); // All AA's need TargetData.
78}
79
Chris Lattnerf9355f62002-08-22 22:46:39 +000080/// canBasicBlockModify - Return true if it is possible for execution of the
81/// specified basic block to modify the value pointed to by Ptr.
82///
Chris Lattner14ac8772003-02-26 19:26:51 +000083bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
84 const Value *Ptr, unsigned Size) {
85 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
Chris Lattner53ad0ed2002-08-22 18:25:32 +000086}
87
Chris Lattnerf9355f62002-08-22 22:46:39 +000088/// canInstructionRangeModify - Return true if it is possible for the execution
89/// of the specified instructions to modify the value pointed to by Ptr. The
90/// instructions to consider are all of the instructions in the range of [I1,I2]
91/// INCLUSIVE. I1 and I2 must be in the same basic block.
92///
Chris Lattner53ad0ed2002-08-22 18:25:32 +000093bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
94 const Instruction &I2,
Chris Lattner14ac8772003-02-26 19:26:51 +000095 const Value *Ptr, unsigned Size) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +000096 assert(I1.getParent() == I2.getParent() &&
97 "Instructions not in same basic block!");
Chris Lattner53ad0ed2002-08-22 18:25:32 +000098 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
99 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
100 ++E; // Convert from inclusive to exclusive range.
101
Chris Lattner14ac8772003-02-26 19:26:51 +0000102 for (; I != E; ++I) // Check every instruction in range
103 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000104 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000105 return false;
106}
107
Chris Lattnerd501c132003-02-26 19:41:54 +0000108// Because of the way .a files work, we must force the BasicAA implementation to
109// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
110// the risk of AliasAnalysis being used, but the default implementation not
111// being linked into the tool that uses it.
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000112//
Chris Lattnerd501c132003-02-26 19:41:54 +0000113extern void BasicAAStub();
114static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub);
Chris Lattner14ac8772003-02-26 19:26:51 +0000115
Chris Lattner8dcd17c2003-02-26 19:57:10 +0000116
117namespace {
118 struct NoAA : public ImmutablePass, public AliasAnalysis {
119 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
120 AliasAnalysis::getAnalysisUsage(AU);
121 }
122
123 virtual void initializePass() {
124 InitializeAliasAnalysis(this);
125 }
126 };
127
128 // Register this pass...
129 RegisterOpt<NoAA>
130 X("no-aa", "No Alias Analysis (always returns 'may' alias)");
131
132 // Declare that we implement the AliasAnalysis interface
133 RegisterAnalysisGroup<AliasAnalysis, NoAA> Y;
134} // End of anonymous namespace
Brian Gaeked0fde302003-11-11 22:41:34 +0000135
136} // End llvm namespace