adopt getAdjustedAnalysisPointer in a few more passes.
[oota-llvm.git] / lib / Analysis / ScalarEvolutionAliasAnalysis.cpp
1 //===- 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"
22 using namespace llvm;
23
24 namespace {
25   /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
26   /// implementation that uses ScalarEvolution to answer queries.
27   class ScalarEvolutionAliasAnalysis : public FunctionPass,
28                                        public AliasAnalysis {
29     ScalarEvolution *SE;
30
31   public:
32     static char ID; // Class identification, replacement for typeinfo
33     ScalarEvolutionAliasAnalysis() : FunctionPass(&ID), SE(0) {}
34
35     /// 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                                          
45   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
51     Value *GetBaseValue(const SCEV *S);
52   };
53 }  // End of anonymous namespace
54
55 // Register this pass...
56 char ScalarEvolutionAliasAnalysis::ID = 0;
57 static RegisterPass<ScalarEvolutionAliasAnalysis>
58 X("scev-aa", "ScalarEvolution-based Alias Analysis", false, true);
59
60 // Declare that we implement the AliasAnalysis interface
61 static RegisterAnalysisGroup<AliasAnalysis> Y(X);
62
63 FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
64   return new ScalarEvolutionAliasAnalysis();
65 }
66
67 void
68 ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
69   AU.addRequiredTransitive<ScalarEvolution>();
70   AU.setPreservesAll();
71   AliasAnalysis::getAnalysisUsage(AU);
72 }
73
74 bool
75 ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
76   InitializeAliasAnalysis(this);
77   SE = &getAnalysis<ScalarEvolution>();
78   return false;
79 }
80
81 /// GetBaseValue - Given an expression, try to find a
82 /// base value. Return null is none was found.
83 Value *
84 ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
85   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
86     // In an addrec, assume that the base will be in the start, rather
87     // than the step.
88     return GetBaseValue(AR->getStart());
89   } 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);
92     if (isa<PointerType>(Last->getType()))
93       return GetBaseValue(Last);
94   } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
95     // This is a leaf node.
96     return U->getValue();
97   }
98   // No Identified object found.
99   return 0;
100 }
101
102 AliasAnalysis::AliasResult
103 ScalarEvolutionAliasAnalysis::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.
130   Value *AO = GetBaseValue(AS);
131   Value *BO = GetBaseValue(BS);
132   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 }