1 //===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ScalarEvolutionAliasAnalysis pass, which implements a
11 // simple alias analysis implemented in terms of ScalarEvolution queries.
13 // ScalarEvolution has a more complete understanding of pointer arithmetic
14 // than BasicAliasAnalysis' collection of ad-hoc analyses.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
20 #include "llvm/Analysis/Passes.h"
21 #include "llvm/Pass.h"
25 /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
26 /// implementation that uses ScalarEvolution to answer queries.
27 class ScalarEvolutionAliasAnalysis : public FunctionPass,
28 public AliasAnalysis {
32 static char ID; // Class identification, replacement for typeinfo
33 ScalarEvolutionAliasAnalysis() : FunctionPass(&ID), SE(0) {}
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;
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);
51 Value *GetBaseValue(const SCEV *S);
53 } // End of anonymous namespace
55 // Register this pass...
56 char ScalarEvolutionAliasAnalysis::ID = 0;
57 static RegisterPass<ScalarEvolutionAliasAnalysis>
58 X("scev-aa", "ScalarEvolution-based Alias Analysis", false, true);
60 // Declare that we implement the AliasAnalysis interface
61 static RegisterAnalysisGroup<AliasAnalysis> Y(X);
63 FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
64 return new ScalarEvolutionAliasAnalysis();
68 ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
69 AU.addRequiredTransitive<ScalarEvolution>();
71 AliasAnalysis::getAnalysisUsage(AU);
75 ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
76 InitializeAliasAnalysis(this);
77 SE = &getAnalysis<ScalarEvolution>();
81 /// GetBaseValue - Given an expression, try to find a
82 /// base value. Return null is none was found.
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
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.
98 // No Identified object found.
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));
109 // If they evaluate to the same expression, it's a MustAlias.
110 if (AS == BS) return MustAlias;
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()))
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)
137 // Forward the query to the next analysis.
138 return AliasAnalysis::alias(A, ASize, B, BSize);