f009328854b17a0d59e1bfb969c835ec84c5ee1d
[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 // This differs from traditional loop dependence analysis in that it tests
14 // for dependencies within a single iteration of a loop, rather than
15 // dependencies between different iterations.
16 //
17 // ScalarEvolution has a more complete understanding of pointer arithmetic
18 // than BasicAliasAnalysis' collection of ad-hoc analyses.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
24 #include "llvm/Analysis/Passes.h"
25 #include "llvm/Pass.h"
26 using namespace llvm;
27
28 namespace {
29   /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
30   /// implementation that uses ScalarEvolution to answer queries.
31   class ScalarEvolutionAliasAnalysis : public FunctionPass,
32                                        public AliasAnalysis {
33     ScalarEvolution *SE;
34
35   public:
36     static char ID; // Class identification, replacement for typeinfo
37     ScalarEvolutionAliasAnalysis() : FunctionPass(ID), SE(0) {}
38
39     /// getAdjustedAnalysisPointer - This method is used when a pass implements
40     /// an analysis interface through multiple inheritance.  If needed, it
41     /// should override this to adjust the this pointer as needed for the
42     /// specified pass info.
43     virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
44       if (PI == &AliasAnalysis::ID)
45         return (AliasAnalysis*)this;
46       return this;
47     }
48
49   private:
50     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
51     virtual bool runOnFunction(Function &F);
52     virtual AliasResult alias(const Location &LocA, const Location &LocB);
53
54     Value *GetBaseValue(const SCEV *S);
55   };
56 }  // End of anonymous namespace
57
58 // Register this pass...
59 char ScalarEvolutionAliasAnalysis::ID = 0;
60 INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
61                    "ScalarEvolution-based Alias Analysis", false, true, false)
62 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
63 INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
64                     "ScalarEvolution-based Alias Analysis", false, true, false)
65
66 FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
67   return new ScalarEvolutionAliasAnalysis();
68 }
69
70 void
71 ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
72   AU.addRequiredTransitive<ScalarEvolution>();
73   AU.setPreservesAll();
74   AliasAnalysis::getAnalysisUsage(AU);
75 }
76
77 bool
78 ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
79   InitializeAliasAnalysis(this);
80   SE = &getAnalysis<ScalarEvolution>();
81   return false;
82 }
83
84 /// GetBaseValue - Given an expression, try to find a
85 /// base value. Return null is none was found.
86 Value *
87 ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
88   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
89     // In an addrec, assume that the base will be in the start, rather
90     // than the step.
91     return GetBaseValue(AR->getStart());
92   } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
93     // If there's a pointer operand, it'll be sorted at the end of the list.
94     const SCEV *Last = A->getOperand(A->getNumOperands()-1);
95     if (Last->getType()->isPointerTy())
96       return GetBaseValue(Last);
97   } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
98     // This is a leaf node.
99     return U->getValue();
100   }
101   // No Identified object found.
102   return 0;
103 }
104
105 AliasAnalysis::AliasResult
106 ScalarEvolutionAliasAnalysis::alias(const Location &LocA,
107                                     const Location &LocB) {
108   // If either of the memory references is empty, it doesn't matter what the
109   // pointer values are. This allows the code below to ignore this special
110   // case.
111   if (LocA.Size == 0 || LocB.Size == 0)
112     return NoAlias;
113
114   // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
115   const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
116   const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
117
118   // If they evaluate to the same expression, it's a MustAlias.
119   if (AS == BS) return MustAlias;
120
121   // If something is known about the difference between the two addresses,
122   // see if it's enough to prove a NoAlias.
123   if (SE->getEffectiveSCEVType(AS->getType()) ==
124       SE->getEffectiveSCEVType(BS->getType())) {
125     unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
126     APInt ASizeInt(BitWidth, LocA.Size);
127     APInt BSizeInt(BitWidth, LocB.Size);
128
129     // Compute the difference between the two pointers.
130     const SCEV *BA = SE->getMinusSCEV(BS, AS);
131
132     // Test whether the difference is known to be great enough that memory of
133     // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
134     // are non-zero, which is special-cased above.
135     if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
136         (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
137       return NoAlias;
138
139     // Folding the subtraction while preserving range information can be tricky
140     // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
141     // and try again to see if things fold better that way.
142
143     // Compute the difference between the two pointers.
144     const SCEV *AB = SE->getMinusSCEV(AS, BS);
145
146     // Test whether the difference is known to be great enough that memory of
147     // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
148     // are non-zero, which is special-cased above.
149     if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
150         (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
151       return NoAlias;
152   }
153
154   // If ScalarEvolution can find an underlying object, form a new query.
155   // The correctness of this depends on ScalarEvolution not recognizing
156   // inttoptr and ptrtoint operators.
157   Value *AO = GetBaseValue(AS);
158   Value *BO = GetBaseValue(BS);
159   if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
160     if (alias(Location(AO ? AO : LocA.Ptr,
161                        AO ? +UnknownSize : LocA.Size,
162                        AO ? 0 : LocA.TBAATag),
163               Location(BO ? BO : LocB.Ptr,
164                        BO ? +UnknownSize : LocB.Size,
165                        BO ? 0 : LocB.TBAATag)) == NoAlias)
166       return NoAlias;
167
168   // Forward the query to the next analysis.
169   return AliasAnalysis::alias(LocA, LocB);
170 }