1*09467b48Spatrick //===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===// 2*09467b48Spatrick // 3*09467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*09467b48Spatrick // See https://llvm.org/LICENSE.txt for license information. 5*09467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*09467b48Spatrick // 7*09467b48Spatrick //===----------------------------------------------------------------------===// 8*09467b48Spatrick // 9*09467b48Spatrick // This file defines the ScalarEvolutionAliasAnalysis pass, which implements a 10*09467b48Spatrick // simple alias analysis implemented in terms of ScalarEvolution queries. 11*09467b48Spatrick // 12*09467b48Spatrick // This differs from traditional loop dependence analysis in that it tests 13*09467b48Spatrick // for dependencies within a single iteration of a loop, rather than 14*09467b48Spatrick // dependencies between different iterations. 15*09467b48Spatrick // 16*09467b48Spatrick // ScalarEvolution has a more complete understanding of pointer arithmetic 17*09467b48Spatrick // than BasicAliasAnalysis' collection of ad-hoc analyses. 18*09467b48Spatrick // 19*09467b48Spatrick //===----------------------------------------------------------------------===// 20*09467b48Spatrick 21*09467b48Spatrick #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 22*09467b48Spatrick #include "llvm/InitializePasses.h" 23*09467b48Spatrick using namespace llvm; 24*09467b48Spatrick 25*09467b48Spatrick AliasResult SCEVAAResult::alias(const MemoryLocation &LocA, 26*09467b48Spatrick const MemoryLocation &LocB, AAQueryInfo &AAQI) { 27*09467b48Spatrick // If either of the memory references is empty, it doesn't matter what the 28*09467b48Spatrick // pointer values are. This allows the code below to ignore this special 29*09467b48Spatrick // case. 30*09467b48Spatrick if (LocA.Size.isZero() || LocB.Size.isZero()) 31*09467b48Spatrick return NoAlias; 32*09467b48Spatrick 33*09467b48Spatrick // This is SCEVAAResult. Get the SCEVs! 34*09467b48Spatrick const SCEV *AS = SE.getSCEV(const_cast<Value *>(LocA.Ptr)); 35*09467b48Spatrick const SCEV *BS = SE.getSCEV(const_cast<Value *>(LocB.Ptr)); 36*09467b48Spatrick 37*09467b48Spatrick // If they evaluate to the same expression, it's a MustAlias. 38*09467b48Spatrick if (AS == BS) 39*09467b48Spatrick return MustAlias; 40*09467b48Spatrick 41*09467b48Spatrick // If something is known about the difference between the two addresses, 42*09467b48Spatrick // see if it's enough to prove a NoAlias. 43*09467b48Spatrick if (SE.getEffectiveSCEVType(AS->getType()) == 44*09467b48Spatrick SE.getEffectiveSCEVType(BS->getType())) { 45*09467b48Spatrick unsigned BitWidth = SE.getTypeSizeInBits(AS->getType()); 46*09467b48Spatrick APInt ASizeInt(BitWidth, LocA.Size.hasValue() 47*09467b48Spatrick ? LocA.Size.getValue() 48*09467b48Spatrick : MemoryLocation::UnknownSize); 49*09467b48Spatrick APInt BSizeInt(BitWidth, LocB.Size.hasValue() 50*09467b48Spatrick ? LocB.Size.getValue() 51*09467b48Spatrick : MemoryLocation::UnknownSize); 52*09467b48Spatrick 53*09467b48Spatrick // Compute the difference between the two pointers. 54*09467b48Spatrick const SCEV *BA = SE.getMinusSCEV(BS, AS); 55*09467b48Spatrick 56*09467b48Spatrick // Test whether the difference is known to be great enough that memory of 57*09467b48Spatrick // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt 58*09467b48Spatrick // are non-zero, which is special-cased above. 59*09467b48Spatrick if (ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) && 60*09467b48Spatrick (-BSizeInt).uge(SE.getUnsignedRange(BA).getUnsignedMax())) 61*09467b48Spatrick return NoAlias; 62*09467b48Spatrick 63*09467b48Spatrick // Folding the subtraction while preserving range information can be tricky 64*09467b48Spatrick // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS 65*09467b48Spatrick // and try again to see if things fold better that way. 66*09467b48Spatrick 67*09467b48Spatrick // Compute the difference between the two pointers. 68*09467b48Spatrick const SCEV *AB = SE.getMinusSCEV(AS, BS); 69*09467b48Spatrick 70*09467b48Spatrick // Test whether the difference is known to be great enough that memory of 71*09467b48Spatrick // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt 72*09467b48Spatrick // are non-zero, which is special-cased above. 73*09467b48Spatrick if (BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) && 74*09467b48Spatrick (-ASizeInt).uge(SE.getUnsignedRange(AB).getUnsignedMax())) 75*09467b48Spatrick return NoAlias; 76*09467b48Spatrick } 77*09467b48Spatrick 78*09467b48Spatrick // If ScalarEvolution can find an underlying object, form a new query. 79*09467b48Spatrick // The correctness of this depends on ScalarEvolution not recognizing 80*09467b48Spatrick // inttoptr and ptrtoint operators. 81*09467b48Spatrick Value *AO = GetBaseValue(AS); 82*09467b48Spatrick Value *BO = GetBaseValue(BS); 83*09467b48Spatrick if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr)) 84*09467b48Spatrick if (alias(MemoryLocation(AO ? AO : LocA.Ptr, 85*09467b48Spatrick AO ? LocationSize::unknown() : LocA.Size, 86*09467b48Spatrick AO ? AAMDNodes() : LocA.AATags), 87*09467b48Spatrick MemoryLocation(BO ? BO : LocB.Ptr, 88*09467b48Spatrick BO ? LocationSize::unknown() : LocB.Size, 89*09467b48Spatrick BO ? AAMDNodes() : LocB.AATags), 90*09467b48Spatrick AAQI) == NoAlias) 91*09467b48Spatrick return NoAlias; 92*09467b48Spatrick 93*09467b48Spatrick // Forward the query to the next analysis. 94*09467b48Spatrick return AAResultBase::alias(LocA, LocB, AAQI); 95*09467b48Spatrick } 96*09467b48Spatrick 97*09467b48Spatrick /// Given an expression, try to find a base value. 98*09467b48Spatrick /// 99*09467b48Spatrick /// Returns null if none was found. 100*09467b48Spatrick Value *SCEVAAResult::GetBaseValue(const SCEV *S) { 101*09467b48Spatrick if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { 102*09467b48Spatrick // In an addrec, assume that the base will be in the start, rather 103*09467b48Spatrick // than the step. 104*09467b48Spatrick return GetBaseValue(AR->getStart()); 105*09467b48Spatrick } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { 106*09467b48Spatrick // If there's a pointer operand, it'll be sorted at the end of the list. 107*09467b48Spatrick const SCEV *Last = A->getOperand(A->getNumOperands() - 1); 108*09467b48Spatrick if (Last->getType()->isPointerTy()) 109*09467b48Spatrick return GetBaseValue(Last); 110*09467b48Spatrick } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 111*09467b48Spatrick // This is a leaf node. 112*09467b48Spatrick return U->getValue(); 113*09467b48Spatrick } 114*09467b48Spatrick // No Identified object found. 115*09467b48Spatrick return nullptr; 116*09467b48Spatrick } 117*09467b48Spatrick 118*09467b48Spatrick AnalysisKey SCEVAA::Key; 119*09467b48Spatrick 120*09467b48Spatrick SCEVAAResult SCEVAA::run(Function &F, FunctionAnalysisManager &AM) { 121*09467b48Spatrick return SCEVAAResult(AM.getResult<ScalarEvolutionAnalysis>(F)); 122*09467b48Spatrick } 123*09467b48Spatrick 124*09467b48Spatrick char SCEVAAWrapperPass::ID = 0; 125*09467b48Spatrick INITIALIZE_PASS_BEGIN(SCEVAAWrapperPass, "scev-aa", 126*09467b48Spatrick "ScalarEvolution-based Alias Analysis", false, true) 127*09467b48Spatrick INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) 128*09467b48Spatrick INITIALIZE_PASS_END(SCEVAAWrapperPass, "scev-aa", 129*09467b48Spatrick "ScalarEvolution-based Alias Analysis", false, true) 130*09467b48Spatrick 131*09467b48Spatrick FunctionPass *llvm::createSCEVAAWrapperPass() { 132*09467b48Spatrick return new SCEVAAWrapperPass(); 133*09467b48Spatrick } 134*09467b48Spatrick 135*09467b48Spatrick SCEVAAWrapperPass::SCEVAAWrapperPass() : FunctionPass(ID) { 136*09467b48Spatrick initializeSCEVAAWrapperPassPass(*PassRegistry::getPassRegistry()); 137*09467b48Spatrick } 138*09467b48Spatrick 139*09467b48Spatrick bool SCEVAAWrapperPass::runOnFunction(Function &F) { 140*09467b48Spatrick Result.reset( 141*09467b48Spatrick new SCEVAAResult(getAnalysis<ScalarEvolutionWrapperPass>().getSE())); 142*09467b48Spatrick return false; 143*09467b48Spatrick } 144*09467b48Spatrick 145*09467b48Spatrick void SCEVAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 146*09467b48Spatrick AU.setPreservesAll(); 147*09467b48Spatrick AU.addRequired<ScalarEvolutionWrapperPass>(); 148*09467b48Spatrick } 149