12385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman//===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===// 22385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// 32385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// The LLVM Compiler Infrastructure 42385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// 52385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// This file is distributed under the University of Illinois Open Source 62385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// License. See LICENSE.TXT for details. 72385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// 82385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman//===----------------------------------------------------------------------===// 92385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// 102385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// This file defines the ScalarEvolutionAliasAnalysis pass, which implements a 112385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// simple alias analysis implemented in terms of ScalarEvolution queries. 122385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// 137cbf5a803e58b844a537cbf63eb0364d01a842a1Dan Gohman// This differs from traditional loop dependence analysis in that it tests 147cbf5a803e58b844a537cbf63eb0364d01a842a1Dan Gohman// for dependencies within a single iteration of a loop, rather than 154f46e14b8ab3861a8dbea14ed70cb9527ae24a06Dan Gohman// dependencies between different iterations. 167cbf5a803e58b844a537cbf63eb0364d01a842a1Dan Gohman// 172385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// ScalarEvolution has a more complete understanding of pointer arithmetic 182385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// than BasicAliasAnalysis' collection of ad-hoc analyses. 192385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// 202385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman//===----------------------------------------------------------------------===// 212385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 22d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Analysis/Passes.h" 232385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman#include "llvm/Analysis/AliasAnalysis.h" 242385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman#include "llvm/Analysis/ScalarEvolutionExpressions.h" 252385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman#include "llvm/Pass.h" 262385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohmanusing namespace llvm; 272385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 282385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohmannamespace { 292385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis 302385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman /// implementation that uses ScalarEvolution to answer queries. 316726b6d75a8b679068a58cb954ba97cf9d1690baNick Lewycky class ScalarEvolutionAliasAnalysis : public FunctionPass, 326726b6d75a8b679068a58cb954ba97cf9d1690baNick Lewycky public AliasAnalysis { 332385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman ScalarEvolution *SE; 342385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 352385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman public: 362385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman static char ID; // Class identification, replacement for typeinfo 37081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson ScalarEvolutionAliasAnalysis() : FunctionPass(ID), SE(0) { 38081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson initializeScalarEvolutionAliasAnalysisPass( 39081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson *PassRegistry::getPassRegistry()); 40081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson } 412385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 421bc76d48d476446f226f06f0aced7efb268f2536Chris Lattner /// getAdjustedAnalysisPointer - This method is used when a pass implements 431bc76d48d476446f226f06f0aced7efb268f2536Chris Lattner /// an analysis interface through multiple inheritance. If needed, it 441bc76d48d476446f226f06f0aced7efb268f2536Chris Lattner /// should override this to adjust the this pointer as needed for the 451bc76d48d476446f226f06f0aced7efb268f2536Chris Lattner /// specified pass info. 4690c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson virtual void *getAdjustedAnalysisPointer(AnalysisID PI) { 4790c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson if (PI == &AliasAnalysis::ID) 481bc76d48d476446f226f06f0aced7efb268f2536Chris Lattner return (AliasAnalysis*)this; 491bc76d48d476446f226f06f0aced7efb268f2536Chris Lattner return this; 501bc76d48d476446f226f06f0aced7efb268f2536Chris Lattner } 517cbf5a803e58b844a537cbf63eb0364d01a842a1Dan Gohman 522385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman private: 532385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman virtual void getAnalysisUsage(AnalysisUsage &AU) const; 542385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman virtual bool runOnFunction(Function &F); 55b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman virtual AliasResult alias(const Location &LocA, const Location &LocB); 562385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 57576fd76a682a1d6a6912d27ca432460dcdf0f738Dan Gohman Value *GetBaseValue(const SCEV *S); 582385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman }; 592385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman} // End of anonymous namespace 602385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 612385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman// Register this pass... 622385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohmanchar ScalarEvolutionAliasAnalysis::ID = 0; 632ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa", 64ce665bd2e2b581ab0858d1afe359192bac96b868Owen Anderson "ScalarEvolution-based Alias Analysis", false, true, false) 652ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_PASS_DEPENDENCY(ScalarEvolution) 662ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa", 672ab36d350293c77fc8941ce1023e4899df7e3a82Owen Anderson "ScalarEvolution-based Alias Analysis", false, true, false) 682385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 692385e0e22ca482f2896dfa975a08db2f54926c09Dan GohmanFunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() { 702385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman return new ScalarEvolutionAliasAnalysis(); 712385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman} 722385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 732385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohmanvoid 742385e0e22ca482f2896dfa975a08db2f54926c09Dan GohmanScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 752385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman AU.addRequiredTransitive<ScalarEvolution>(); 762385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman AU.setPreservesAll(); 772385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman AliasAnalysis::getAnalysisUsage(AU); 782385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman} 792385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 802385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohmanbool 812385e0e22ca482f2896dfa975a08db2f54926c09Dan GohmanScalarEvolutionAliasAnalysis::runOnFunction(Function &F) { 822385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman InitializeAliasAnalysis(this); 832385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman SE = &getAnalysis<ScalarEvolution>(); 842385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman return false; 852385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman} 862385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 87576fd76a682a1d6a6912d27ca432460dcdf0f738Dan Gohman/// GetBaseValue - Given an expression, try to find a 88576fd76a682a1d6a6912d27ca432460dcdf0f738Dan Gohman/// base value. Return null is none was found. 892385e0e22ca482f2896dfa975a08db2f54926c09Dan GohmanValue * 90576fd76a682a1d6a6912d27ca432460dcdf0f738Dan GohmanScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) { 912385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { 92ed77e52dd972ada58608f5540d3d0d434899114eDan Gohman // In an addrec, assume that the base will be in the start, rather 93ed77e52dd972ada58608f5540d3d0d434899114eDan Gohman // than the step. 94576fd76a682a1d6a6912d27ca432460dcdf0f738Dan Gohman return GetBaseValue(AR->getStart()); 952385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { 962385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman // If there's a pointer operand, it'll be sorted at the end of the list. 972385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman const SCEV *Last = A->getOperand(A->getNumOperands()-1); 981df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands if (Last->getType()->isPointerTy()) 99576fd76a682a1d6a6912d27ca432460dcdf0f738Dan Gohman return GetBaseValue(Last); 1002385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 101576fd76a682a1d6a6912d27ca432460dcdf0f738Dan Gohman // This is a leaf node. 102576fd76a682a1d6a6912d27ca432460dcdf0f738Dan Gohman return U->getValue(); 1032385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman } 1042385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman // No Identified object found. 1052385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman return 0; 1062385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman} 1072385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 1082385e0e22ca482f2896dfa975a08db2f54926c09Dan GohmanAliasAnalysis::AliasResult 109b2143b6247901ae4eca2192ee134564c4f5f7853Dan GohmanScalarEvolutionAliasAnalysis::alias(const Location &LocA, 110b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman const Location &LocB) { 11149bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // If either of the memory references is empty, it doesn't matter what the 11249bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // pointer values are. This allows the code below to ignore this special 11349bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // case. 114b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman if (LocA.Size == 0 || LocB.Size == 0) 11549bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman return NoAlias; 11649bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman 1172385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman // This is ScalarEvolutionAliasAnalysis. Get the SCEVs! 118b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr)); 119b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr)); 1202385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 1212385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman // If they evaluate to the same expression, it's a MustAlias. 1222385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman if (AS == BS) return MustAlias; 1232385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 1242385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman // If something is known about the difference between the two addresses, 1252385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman // see if it's enough to prove a NoAlias. 1262385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman if (SE->getEffectiveSCEVType(AS->getType()) == 1272385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman SE->getEffectiveSCEVType(BS->getType())) { 1282385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman unsigned BitWidth = SE->getTypeSizeInBits(AS->getType()); 129b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman APInt ASizeInt(BitWidth, LocA.Size); 130b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman APInt BSizeInt(BitWidth, LocB.Size); 13149bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman 13249bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // Compute the difference between the two pointers. 1332385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman const SCEV *BA = SE->getMinusSCEV(BS, AS); 13449bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman 13549bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // Test whether the difference is known to be great enough that memory of 13649bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt 13749bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // are non-zero, which is special-cased above. 13849bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) && 13949bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax())) 14049bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman return NoAlias; 14149bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman 14249bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // Folding the subtraction while preserving range information can be tricky 14349bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS 14449bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // and try again to see if things fold better that way. 14549bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman 14649bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // Compute the difference between the two pointers. 14749bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman const SCEV *AB = SE->getMinusSCEV(AS, BS); 14849bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman 14949bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // Test whether the difference is known to be great enough that memory of 15049bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt 15149bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman // are non-zero, which is special-cased above. 15249bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) && 15349bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax())) 15449bda917dbfbb09e2410272ebb1c6e4a2a3f45a2Dan Gohman return NoAlias; 1552385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman } 1562385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 1572385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman // If ScalarEvolution can find an underlying object, form a new query. 1582385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman // The correctness of this depends on ScalarEvolution not recognizing 1592385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman // inttoptr and ptrtoint operators. 160576fd76a682a1d6a6912d27ca432460dcdf0f738Dan Gohman Value *AO = GetBaseValue(AS); 161576fd76a682a1d6a6912d27ca432460dcdf0f738Dan Gohman Value *BO = GetBaseValue(BS); 162b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr)) 163b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman if (alias(Location(AO ? AO : LocA.Ptr, 164b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman AO ? +UnknownSize : LocA.Size, 165b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman AO ? 0 : LocA.TBAATag), 166b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman Location(BO ? BO : LocB.Ptr, 167b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman BO ? +UnknownSize : LocB.Size, 168b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman BO ? 0 : LocB.TBAATag)) == NoAlias) 1692385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman return NoAlias; 1702385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman 1712385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman // Forward the query to the next analysis. 172b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return AliasAnalysis::alias(LocA, LocB); 1732385e0e22ca482f2896dfa975a08db2f54926c09Dan Gohman} 174