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