blob: 564ec4b11669e1db1a1bb74b715c01d9e4bf7583 [file] [log] [blame]
Dan Gohman2385e0e2009-08-26 14:53:06 +00001//===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ScalarEvolutionAliasAnalysis pass, which implements a
11// simple alias analysis implemented in terms of ScalarEvolution queries.
12//
13// ScalarEvolution has a more complete understanding of pointer arithmetic
14// than BasicAliasAnalysis' collection of ad-hoc analyses.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/Analysis/AliasAnalysis.h"
19#include "llvm/Analysis/ScalarEvolutionExpressions.h"
20#include "llvm/Analysis/Passes.h"
21#include "llvm/Pass.h"
Dan Gohman2385e0e2009-08-26 14:53:06 +000022using namespace llvm;
23
24namespace {
25 /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
26 /// implementation that uses ScalarEvolution to answer queries.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000027 class ScalarEvolutionAliasAnalysis : public FunctionPass,
28 public AliasAnalysis {
Dan Gohman2385e0e2009-08-26 14:53:06 +000029 ScalarEvolution *SE;
30
31 public:
32 static char ID; // Class identification, replacement for typeinfo
33 ScalarEvolutionAliasAnalysis() : FunctionPass(&ID), SE(0) {}
34
Chris Lattner1bc76d42010-01-20 20:09:02 +000035 /// getAdjustedAnalysisPointer - This method is used when a pass implements
36 /// an analysis interface through multiple inheritance. If needed, it
37 /// should override this to adjust the this pointer as needed for the
38 /// specified pass info.
39 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
40 if (PI->isPassID(&AliasAnalysis::ID))
41 return (AliasAnalysis*)this;
42 return this;
43 }
44
Dan Gohman2385e0e2009-08-26 14:53:06 +000045 private:
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
47 virtual bool runOnFunction(Function &F);
48 virtual AliasResult alias(const Value *V1, unsigned V1Size,
49 const Value *V2, unsigned V2Size);
50
Dan Gohman576fd762009-10-31 14:32:25 +000051 Value *GetBaseValue(const SCEV *S);
Dan Gohman2385e0e2009-08-26 14:53:06 +000052 };
53} // End of anonymous namespace
54
55// Register this pass...
56char ScalarEvolutionAliasAnalysis::ID = 0;
57static RegisterPass<ScalarEvolutionAliasAnalysis>
58X("scev-aa", "ScalarEvolution-based Alias Analysis", false, true);
59
60// Declare that we implement the AliasAnalysis interface
61static RegisterAnalysisGroup<AliasAnalysis> Y(X);
62
63FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
64 return new ScalarEvolutionAliasAnalysis();
65}
66
67void
68ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
69 AU.addRequiredTransitive<ScalarEvolution>();
70 AU.setPreservesAll();
71 AliasAnalysis::getAnalysisUsage(AU);
72}
73
74bool
75ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
76 InitializeAliasAnalysis(this);
77 SE = &getAnalysis<ScalarEvolution>();
78 return false;
79}
80
Dan Gohman576fd762009-10-31 14:32:25 +000081/// GetBaseValue - Given an expression, try to find a
82/// base value. Return null is none was found.
Dan Gohman2385e0e2009-08-26 14:53:06 +000083Value *
Dan Gohman576fd762009-10-31 14:32:25 +000084ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
Dan Gohman2385e0e2009-08-26 14:53:06 +000085 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
Dan Gohmaned77e522009-08-29 23:36:57 +000086 // In an addrec, assume that the base will be in the start, rather
87 // than the step.
Dan Gohman576fd762009-10-31 14:32:25 +000088 return GetBaseValue(AR->getStart());
Dan Gohman2385e0e2009-08-26 14:53:06 +000089 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
90 // If there's a pointer operand, it'll be sorted at the end of the list.
91 const SCEV *Last = A->getOperand(A->getNumOperands()-1);
Duncan Sands1df98592010-02-16 11:11:14 +000092 if (Last->getType()->isPointerTy())
Dan Gohman576fd762009-10-31 14:32:25 +000093 return GetBaseValue(Last);
Dan Gohman2385e0e2009-08-26 14:53:06 +000094 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
Dan Gohman576fd762009-10-31 14:32:25 +000095 // This is a leaf node.
96 return U->getValue();
Dan Gohman2385e0e2009-08-26 14:53:06 +000097 }
98 // No Identified object found.
99 return 0;
100}
101
102AliasAnalysis::AliasResult
103ScalarEvolutionAliasAnalysis::alias(const Value *A, unsigned ASize,
104 const Value *B, unsigned BSize) {
105 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
106 const SCEV *AS = SE->getSCEV(const_cast<Value *>(A));
107 const SCEV *BS = SE->getSCEV(const_cast<Value *>(B));
108
109 // If they evaluate to the same expression, it's a MustAlias.
110 if (AS == BS) return MustAlias;
111
112 // If something is known about the difference between the two addresses,
113 // see if it's enough to prove a NoAlias.
114 if (SE->getEffectiveSCEVType(AS->getType()) ==
115 SE->getEffectiveSCEVType(BS->getType())) {
116 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
117 APInt AI(BitWidth, ASize);
118 const SCEV *BA = SE->getMinusSCEV(BS, AS);
119 if (AI.ule(SE->getUnsignedRange(BA).getUnsignedMin())) {
120 APInt BI(BitWidth, BSize);
121 const SCEV *AB = SE->getMinusSCEV(AS, BS);
122 if (BI.ule(SE->getUnsignedRange(AB).getUnsignedMin()))
123 return NoAlias;
124 }
125 }
126
127 // If ScalarEvolution can find an underlying object, form a new query.
128 // The correctness of this depends on ScalarEvolution not recognizing
129 // inttoptr and ptrtoint operators.
Dan Gohman576fd762009-10-31 14:32:25 +0000130 Value *AO = GetBaseValue(AS);
131 Value *BO = GetBaseValue(BS);
Dan Gohman2385e0e2009-08-26 14:53:06 +0000132 if ((AO && AO != A) || (BO && BO != B))
133 if (alias(AO ? AO : A, AO ? ~0u : ASize,
134 BO ? BO : B, BO ? ~0u : BSize) == NoAlias)
135 return NoAlias;
136
137 // Forward the query to the next analysis.
138 return AliasAnalysis::alias(A, ASize, B, BSize);
139}