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"
22 #include "llvm/Support/Compiler.h"
26 /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
27 /// implementation that uses ScalarEvolution to answer queries.
28 class VISIBILITY_HIDDEN ScalarEvolutionAliasAnalysis : public FunctionPass,
29 public AliasAnalysis {
33 static char ID; // Class identification, replacement for typeinfo
34 ScalarEvolutionAliasAnalysis() : FunctionPass(&ID), SE(0) {}
37 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
38 virtual bool runOnFunction(Function &F);
39 virtual AliasResult alias(const Value *V1, unsigned V1Size,
40 const Value *V2, unsigned V2Size);
42 Value *GetUnderlyingIdentifiedObject(const SCEV *S);
44 } // End of anonymous namespace
46 // Register this pass...
47 char ScalarEvolutionAliasAnalysis::ID = 0;
48 static RegisterPass<ScalarEvolutionAliasAnalysis>
49 X("scev-aa", "ScalarEvolution-based Alias Analysis", false, true);
51 // Declare that we implement the AliasAnalysis interface
52 static RegisterAnalysisGroup<AliasAnalysis> Y(X);
54 FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
55 return new ScalarEvolutionAliasAnalysis();
59 ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.addRequiredTransitive<ScalarEvolution>();
62 AliasAnalysis::getAnalysisUsage(AU);
66 ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
67 InitializeAliasAnalysis(this);
68 SE = &getAnalysis<ScalarEvolution>();
73 ScalarEvolutionAliasAnalysis::GetUnderlyingIdentifiedObject(const SCEV *S) {
74 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
75 return GetUnderlyingIdentifiedObject(AR->getStart());
76 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
77 // If there's a pointer operand, it'll be sorted at the end of the list.
78 const SCEV *Last = A->getOperand(A->getNumOperands()-1);
79 if (isa<PointerType>(Last->getType()))
80 return GetUnderlyingIdentifiedObject(Last);
81 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
82 // Determine if we've found an Identified object.
83 Value *V = U->getValue();
84 if (isIdentifiedObject(V))
87 // No Identified object found.
91 AliasAnalysis::AliasResult
92 ScalarEvolutionAliasAnalysis::alias(const Value *A, unsigned ASize,
93 const Value *B, unsigned BSize) {
94 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
95 const SCEV *AS = SE->getSCEV(const_cast<Value *>(A));
96 const SCEV *BS = SE->getSCEV(const_cast<Value *>(B));
98 // If they evaluate to the same expression, it's a MustAlias.
99 if (AS == BS) return MustAlias;
101 // If something is known about the difference between the two addresses,
102 // see if it's enough to prove a NoAlias.
103 if (SE->getEffectiveSCEVType(AS->getType()) ==
104 SE->getEffectiveSCEVType(BS->getType())) {
105 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
106 APInt AI(BitWidth, ASize);
107 const SCEV *BA = SE->getMinusSCEV(BS, AS);
108 if (AI.ule(SE->getUnsignedRange(BA).getUnsignedMin())) {
109 APInt BI(BitWidth, BSize);
110 const SCEV *AB = SE->getMinusSCEV(AS, BS);
111 if (BI.ule(SE->getUnsignedRange(AB).getUnsignedMin()))
116 // If ScalarEvolution can find an underlying object, form a new query.
117 // The correctness of this depends on ScalarEvolution not recognizing
118 // inttoptr and ptrtoint operators.
119 Value *AO = GetUnderlyingIdentifiedObject(AS);
120 Value *BO = GetUnderlyingIdentifiedObject(BS);
121 if ((AO && AO != A) || (BO && BO != B))
122 if (alias(AO ? AO : A, AO ? ~0u : ASize,
123 BO ? BO : B, BO ? ~0u : BSize) == NoAlias)
126 // Forward the query to the next analysis.
127 return AliasAnalysis::alias(A, ASize, B, BSize);