blob: 4816a67d102f8d6a487300b01651848f44fd06d5 [file] [log] [blame]
Chris Lattner53ad0ed2002-08-22 18:25:32 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
2//
3// This file implements the generic AliasAnalysis interface which is used as the
4// common interface used by all clients and implementations of alias analysis.
5//
6// This file also implements the default version of the AliasAnalysis interface
7// that is to be used when no other implementation is specified. This does some
8// simple tests that detect obvious cases: two different global pointers cannot
9// alias, a global cannot alias a malloc, two different mallocs cannot alias,
10// etc.
11//
12// This alias analysis implementation really isn't very good for anything, but
13// it is very fast, and makes a nice clean default implementation. Because it
14// handles lots of little corner cases, other, more complex, alias analysis
15// implementations may choose to rely on this pass to resolve these simple and
16// easy cases.
17//
18//===----------------------------------------------------------------------===//
19
Chris Lattnerd501c132003-02-26 19:41:54 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000021#include "llvm/BasicBlock.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000022#include "llvm/iMemory.h"
Chris Lattner14ac8772003-02-26 19:26:51 +000023#include "llvm/Target/TargetData.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000024
25// Register the AliasAnalysis interface, providing a nice name to refer to.
Chris Lattnerdc1ad192003-02-03 21:16:17 +000026namespace {
27 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
Chris Lattnerdc1ad192003-02-03 21:16:17 +000028}
Chris Lattner53ad0ed2002-08-22 18:25:32 +000029
Chris Lattner14ac8772003-02-26 19:26:51 +000030AliasAnalysis::ModRefResult
31AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
32 return alias(L->getOperand(0), TD->getTypeSize(L->getType()),
33 P, Size) ? Ref : NoModRef;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000034}
35
Chris Lattner14ac8772003-02-26 19:26:51 +000036AliasAnalysis::ModRefResult
37AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
38 return alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()),
39 P, Size) ? Mod : NoModRef;
40}
41
42
Chris Lattner53ad0ed2002-08-22 18:25:32 +000043// AliasAnalysis destructor: DO NOT move this to the header file for
44// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
45// the AliasAnalysis.o file in the current .a file, causing alias analysis
46// support to not be included in the tool correctly!
47//
48AliasAnalysis::~AliasAnalysis() {}
49
Chris Lattner14ac8772003-02-26 19:26:51 +000050/// setTargetData - Subclasses must call this method to initialize the
51/// AliasAnalysis interface before any other methods are called.
52///
53void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
54 TD = &P->getAnalysis<TargetData>();
55}
56
57// getAnalysisUsage - All alias analysis implementations should invoke this
58// directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
59// TargetData is required by the pass.
60void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.addRequired<TargetData>(); // All AA's need TargetData.
62}
63
Chris Lattnerf9355f62002-08-22 22:46:39 +000064/// canBasicBlockModify - Return true if it is possible for execution of the
65/// specified basic block to modify the value pointed to by Ptr.
66///
Chris Lattner14ac8772003-02-26 19:26:51 +000067bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
68 const Value *Ptr, unsigned Size) {
69 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
Chris Lattner53ad0ed2002-08-22 18:25:32 +000070}
71
Chris Lattnerf9355f62002-08-22 22:46:39 +000072/// canInstructionRangeModify - Return true if it is possible for the execution
73/// of the specified instructions to modify the value pointed to by Ptr. The
74/// instructions to consider are all of the instructions in the range of [I1,I2]
75/// INCLUSIVE. I1 and I2 must be in the same basic block.
76///
Chris Lattner53ad0ed2002-08-22 18:25:32 +000077bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
78 const Instruction &I2,
Chris Lattner14ac8772003-02-26 19:26:51 +000079 const Value *Ptr, unsigned Size) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +000080 assert(I1.getParent() == I2.getParent() &&
81 "Instructions not in same basic block!");
Chris Lattner53ad0ed2002-08-22 18:25:32 +000082 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
83 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
84 ++E; // Convert from inclusive to exclusive range.
85
Chris Lattner14ac8772003-02-26 19:26:51 +000086 for (; I != E; ++I) // Check every instruction in range
87 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +000088 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000089 return false;
90}
91
Chris Lattnerd501c132003-02-26 19:41:54 +000092// Because of the way .a files work, we must force the BasicAA implementation to
93// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
94// the risk of AliasAnalysis being used, but the default implementation not
95// being linked into the tool that uses it.
Chris Lattner53ad0ed2002-08-22 18:25:32 +000096//
Chris Lattnerd501c132003-02-26 19:41:54 +000097extern void BasicAAStub();
98static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub);
Chris Lattner14ac8772003-02-26 19:26:51 +000099