LLVM API Documentation

ScalarEvolutionAliasAnalysis.cpp
Go to the documentation of this file.
00001 //===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the ScalarEvolutionAliasAnalysis pass, which implements a
00011 // simple alias analysis implemented in terms of ScalarEvolution queries.
00012 //
00013 // This differs from traditional loop dependence analysis in that it tests
00014 // for dependencies within a single iteration of a loop, rather than
00015 // dependencies between different iterations.
00016 //
00017 // ScalarEvolution has a more complete understanding of pointer arithmetic
00018 // than BasicAliasAnalysis' collection of ad-hoc analyses.
00019 //
00020 //===----------------------------------------------------------------------===//
00021 
00022 #include "llvm/Analysis/Passes.h"
00023 #include "llvm/Analysis/AliasAnalysis.h"
00024 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
00025 #include "llvm/Pass.h"
00026 using namespace llvm;
00027 
00028 namespace {
00029   /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
00030   /// implementation that uses ScalarEvolution to answer queries.
00031   class ScalarEvolutionAliasAnalysis : public FunctionPass,
00032                                        public AliasAnalysis {
00033     ScalarEvolution *SE;
00034 
00035   public:
00036     static char ID; // Class identification, replacement for typeinfo
00037     ScalarEvolutionAliasAnalysis() : FunctionPass(ID), SE(nullptr) {
00038       initializeScalarEvolutionAliasAnalysisPass(
00039         *PassRegistry::getPassRegistry());
00040     }
00041 
00042     /// getAdjustedAnalysisPointer - This method is used when a pass implements
00043     /// an analysis interface through multiple inheritance.  If needed, it
00044     /// should override this to adjust the this pointer as needed for the
00045     /// specified pass info.
00046     void *getAdjustedAnalysisPointer(AnalysisID PI) override {
00047       if (PI == &AliasAnalysis::ID)
00048         return (AliasAnalysis*)this;
00049       return this;
00050     }
00051 
00052   private:
00053     void getAnalysisUsage(AnalysisUsage &AU) const override;
00054     bool runOnFunction(Function &F) override;
00055     AliasResult alias(const Location &LocA, const Location &LocB) override;
00056 
00057     Value *GetBaseValue(const SCEV *S);
00058   };
00059 }  // End of anonymous namespace
00060 
00061 // Register this pass...
00062 char ScalarEvolutionAliasAnalysis::ID = 0;
00063 INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
00064                    "ScalarEvolution-based Alias Analysis", false, true, false)
00065 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
00066 INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
00067                     "ScalarEvolution-based Alias Analysis", false, true, false)
00068 
00069 FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
00070   return new ScalarEvolutionAliasAnalysis();
00071 }
00072 
00073 void
00074 ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
00075   AU.addRequiredTransitive<ScalarEvolution>();
00076   AU.setPreservesAll();
00077   AliasAnalysis::getAnalysisUsage(AU);
00078 }
00079 
00080 bool
00081 ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
00082   InitializeAliasAnalysis(this);
00083   SE = &getAnalysis<ScalarEvolution>();
00084   return false;
00085 }
00086 
00087 /// GetBaseValue - Given an expression, try to find a
00088 /// base value. Return null is none was found.
00089 Value *
00090 ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
00091   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
00092     // In an addrec, assume that the base will be in the start, rather
00093     // than the step.
00094     return GetBaseValue(AR->getStart());
00095   } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
00096     // If there's a pointer operand, it'll be sorted at the end of the list.
00097     const SCEV *Last = A->getOperand(A->getNumOperands()-1);
00098     if (Last->getType()->isPointerTy())
00099       return GetBaseValue(Last);
00100   } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
00101     // This is a leaf node.
00102     return U->getValue();
00103   }
00104   // No Identified object found.
00105   return nullptr;
00106 }
00107 
00108 AliasAnalysis::AliasResult
00109 ScalarEvolutionAliasAnalysis::alias(const Location &LocA,
00110                                     const Location &LocB) {
00111   // If either of the memory references is empty, it doesn't matter what the
00112   // pointer values are. This allows the code below to ignore this special
00113   // case.
00114   if (LocA.Size == 0 || LocB.Size == 0)
00115     return NoAlias;
00116 
00117   // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
00118   const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
00119   const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
00120 
00121   // If they evaluate to the same expression, it's a MustAlias.
00122   if (AS == BS) return MustAlias;
00123 
00124   // If something is known about the difference between the two addresses,
00125   // see if it's enough to prove a NoAlias.
00126   if (SE->getEffectiveSCEVType(AS->getType()) ==
00127       SE->getEffectiveSCEVType(BS->getType())) {
00128     unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
00129     APInt ASizeInt(BitWidth, LocA.Size);
00130     APInt BSizeInt(BitWidth, LocB.Size);
00131 
00132     // Compute the difference between the two pointers.
00133     const SCEV *BA = SE->getMinusSCEV(BS, AS);
00134 
00135     // Test whether the difference is known to be great enough that memory of
00136     // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
00137     // are non-zero, which is special-cased above.
00138     if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
00139         (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
00140       return NoAlias;
00141 
00142     // Folding the subtraction while preserving range information can be tricky
00143     // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
00144     // and try again to see if things fold better that way.
00145 
00146     // Compute the difference between the two pointers.
00147     const SCEV *AB = SE->getMinusSCEV(AS, BS);
00148 
00149     // Test whether the difference is known to be great enough that memory of
00150     // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
00151     // are non-zero, which is special-cased above.
00152     if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
00153         (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
00154       return NoAlias;
00155   }
00156 
00157   // If ScalarEvolution can find an underlying object, form a new query.
00158   // The correctness of this depends on ScalarEvolution not recognizing
00159   // inttoptr and ptrtoint operators.
00160   Value *AO = GetBaseValue(AS);
00161   Value *BO = GetBaseValue(BS);
00162   if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
00163     if (alias(Location(AO ? AO : LocA.Ptr,
00164                        AO ? +UnknownSize : LocA.Size,
00165                        AO ? AAMDNodes() : LocA.AATags),
00166               Location(BO ? BO : LocB.Ptr,
00167                        BO ? +UnknownSize : LocB.Size,
00168                        BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
00169       return NoAlias;
00170 
00171   // Forward the query to the next analysis.
00172   return AliasAnalysis::alias(LocA, LocB);
00173 }