xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/ScalarEvolutionAliasAnalysis.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines the ScalarEvolutionAliasAnalysis pass, which implements a
100b57cec5SDimitry Andric // simple alias analysis implemented in terms of ScalarEvolution queries.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric // This differs from traditional loop dependence analysis in that it tests
130b57cec5SDimitry Andric // for dependencies within a single iteration of a loop, rather than
140b57cec5SDimitry Andric // dependencies between different iterations.
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric // ScalarEvolution has a more complete understanding of pointer arithmetic
170b57cec5SDimitry Andric // than BasicAliasAnalysis' collection of ad-hoc analyses.
180b57cec5SDimitry Andric //
190b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
22fe6060f1SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h"
2381ad6265SDimitry Andric #include "llvm/Analysis/ScalarEvolutionExpressions.h"
24480093f4SDimitry Andric #include "llvm/InitializePasses.h"
250b57cec5SDimitry Andric using namespace llvm;
260b57cec5SDimitry Andric 
canComputePointerDiff(ScalarEvolution & SE,const SCEV * A,const SCEV * B)27349cc55cSDimitry Andric static bool canComputePointerDiff(ScalarEvolution &SE,
28349cc55cSDimitry Andric                                   const SCEV *A, const SCEV *B) {
29349cc55cSDimitry Andric   if (SE.getEffectiveSCEVType(A->getType()) !=
30349cc55cSDimitry Andric       SE.getEffectiveSCEVType(B->getType()))
31349cc55cSDimitry Andric     return false;
32349cc55cSDimitry Andric 
335f757f3fSDimitry Andric   return SE.instructionCouldExistWithOperands(A, B);
34349cc55cSDimitry Andric }
35349cc55cSDimitry Andric 
alias(const MemoryLocation & LocA,const MemoryLocation & LocB,AAQueryInfo & AAQI,const Instruction *)360b57cec5SDimitry Andric AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
37bdd1243dSDimitry Andric                                 const MemoryLocation &LocB, AAQueryInfo &AAQI,
38bdd1243dSDimitry Andric                                 const Instruction *) {
390b57cec5SDimitry Andric   // If either of the memory references is empty, it doesn't matter what the
400b57cec5SDimitry Andric   // pointer values are. This allows the code below to ignore this special
410b57cec5SDimitry Andric   // case.
420b57cec5SDimitry Andric   if (LocA.Size.isZero() || LocB.Size.isZero())
43fe6060f1SDimitry Andric     return AliasResult::NoAlias;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   // This is SCEVAAResult. Get the SCEVs!
460b57cec5SDimitry Andric   const SCEV *AS = SE.getSCEV(const_cast<Value *>(LocA.Ptr));
470b57cec5SDimitry Andric   const SCEV *BS = SE.getSCEV(const_cast<Value *>(LocB.Ptr));
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   // If they evaluate to the same expression, it's a MustAlias.
500b57cec5SDimitry Andric   if (AS == BS)
51fe6060f1SDimitry Andric     return AliasResult::MustAlias;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   // If something is known about the difference between the two addresses,
540b57cec5SDimitry Andric   // see if it's enough to prove a NoAlias.
55349cc55cSDimitry Andric   if (canComputePointerDiff(SE, AS, BS)) {
560b57cec5SDimitry Andric     unsigned BitWidth = SE.getTypeSizeInBits(AS->getType());
570b57cec5SDimitry Andric     APInt ASizeInt(BitWidth, LocA.Size.hasValue()
585f757f3fSDimitry Andric                                  ? static_cast<uint64_t>(LocA.Size.getValue())
590b57cec5SDimitry Andric                                  : MemoryLocation::UnknownSize);
600b57cec5SDimitry Andric     APInt BSizeInt(BitWidth, LocB.Size.hasValue()
615f757f3fSDimitry Andric                                  ? static_cast<uint64_t>(LocB.Size.getValue())
620b57cec5SDimitry Andric                                  : MemoryLocation::UnknownSize);
630b57cec5SDimitry Andric 
64*0fca6ea1SDimitry Andric     // Firstly, try to convert the two pointers into ptrtoint expressions to
65*0fca6ea1SDimitry Andric     // handle two pointers with different pointer bases.
66*0fca6ea1SDimitry Andric     // Either both pointers are used with ptrtoint or neither, so we can't end
67*0fca6ea1SDimitry Andric     // up with a ptr + int mix.
68*0fca6ea1SDimitry Andric     const SCEV *AInt =
69*0fca6ea1SDimitry Andric         SE.getPtrToIntExpr(AS, SE.getEffectiveSCEVType(AS->getType()));
70*0fca6ea1SDimitry Andric     const SCEV *BInt =
71*0fca6ea1SDimitry Andric         SE.getPtrToIntExpr(BS, SE.getEffectiveSCEVType(BS->getType()));
72*0fca6ea1SDimitry Andric     if (!isa<SCEVCouldNotCompute>(AInt) && !isa<SCEVCouldNotCompute>(BInt)) {
73*0fca6ea1SDimitry Andric       AS = AInt;
74*0fca6ea1SDimitry Andric       BS = BInt;
75*0fca6ea1SDimitry Andric     }
76*0fca6ea1SDimitry Andric 
770b57cec5SDimitry Andric     // Compute the difference between the two pointers.
780b57cec5SDimitry Andric     const SCEV *BA = SE.getMinusSCEV(BS, AS);
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric     // Test whether the difference is known to be great enough that memory of
810b57cec5SDimitry Andric     // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
820b57cec5SDimitry Andric     // are non-zero, which is special-cased above.
83fe6060f1SDimitry Andric     if (!isa<SCEVCouldNotCompute>(BA) &&
84fe6060f1SDimitry Andric         ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) &&
850b57cec5SDimitry Andric         (-BSizeInt).uge(SE.getUnsignedRange(BA).getUnsignedMax()))
86fe6060f1SDimitry Andric       return AliasResult::NoAlias;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric     // Folding the subtraction while preserving range information can be tricky
890b57cec5SDimitry Andric     // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
900b57cec5SDimitry Andric     // and try again to see if things fold better that way.
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric     // Compute the difference between the two pointers.
930b57cec5SDimitry Andric     const SCEV *AB = SE.getMinusSCEV(AS, BS);
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric     // Test whether the difference is known to be great enough that memory of
960b57cec5SDimitry Andric     // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
970b57cec5SDimitry Andric     // are non-zero, which is special-cased above.
98fe6060f1SDimitry Andric     if (!isa<SCEVCouldNotCompute>(AB) &&
99fe6060f1SDimitry Andric         BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) &&
1000b57cec5SDimitry Andric         (-ASizeInt).uge(SE.getUnsignedRange(AB).getUnsignedMax()))
101fe6060f1SDimitry Andric       return AliasResult::NoAlias;
1020b57cec5SDimitry Andric   }
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   // If ScalarEvolution can find an underlying object, form a new query.
1050b57cec5SDimitry Andric   // The correctness of this depends on ScalarEvolution not recognizing
1060b57cec5SDimitry Andric   // inttoptr and ptrtoint operators.
1070b57cec5SDimitry Andric   Value *AO = GetBaseValue(AS);
1080b57cec5SDimitry Andric   Value *BO = GetBaseValue(BS);
1090b57cec5SDimitry Andric   if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
1100b57cec5SDimitry Andric     if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
111e8d8bef9SDimitry Andric                              AO ? LocationSize::beforeOrAfterPointer()
112e8d8bef9SDimitry Andric                                 : LocA.Size,
1130b57cec5SDimitry Andric                              AO ? AAMDNodes() : LocA.AATags),
1140b57cec5SDimitry Andric               MemoryLocation(BO ? BO : LocB.Ptr,
115e8d8bef9SDimitry Andric                              BO ? LocationSize::beforeOrAfterPointer()
116e8d8bef9SDimitry Andric                                 : LocB.Size,
1170b57cec5SDimitry Andric                              BO ? AAMDNodes() : LocB.AATags),
118bdd1243dSDimitry Andric               AAQI, nullptr) == AliasResult::NoAlias)
119fe6060f1SDimitry Andric       return AliasResult::NoAlias;
1200b57cec5SDimitry Andric 
1215f757f3fSDimitry Andric   return AliasResult::MayAlias;
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric /// Given an expression, try to find a base value.
1250b57cec5SDimitry Andric ///
1260b57cec5SDimitry Andric /// Returns null if none was found.
GetBaseValue(const SCEV * S)1270b57cec5SDimitry Andric Value *SCEVAAResult::GetBaseValue(const SCEV *S) {
1280b57cec5SDimitry Andric   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
1290b57cec5SDimitry Andric     // In an addrec, assume that the base will be in the start, rather
1300b57cec5SDimitry Andric     // than the step.
1310b57cec5SDimitry Andric     return GetBaseValue(AR->getStart());
1320b57cec5SDimitry Andric   } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
1330b57cec5SDimitry Andric     // If there's a pointer operand, it'll be sorted at the end of the list.
1340b57cec5SDimitry Andric     const SCEV *Last = A->getOperand(A->getNumOperands() - 1);
1350b57cec5SDimitry Andric     if (Last->getType()->isPointerTy())
1360b57cec5SDimitry Andric       return GetBaseValue(Last);
1370b57cec5SDimitry Andric   } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
1380b57cec5SDimitry Andric     // This is a leaf node.
1390b57cec5SDimitry Andric     return U->getValue();
1400b57cec5SDimitry Andric   }
1410b57cec5SDimitry Andric   // No Identified object found.
1420b57cec5SDimitry Andric   return nullptr;
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
invalidate(Function & Fn,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator & Inv)145fe6060f1SDimitry Andric bool SCEVAAResult::invalidate(Function &Fn, const PreservedAnalyses &PA,
146fe6060f1SDimitry Andric                               FunctionAnalysisManager::Invalidator &Inv) {
147fe6060f1SDimitry Andric   // We don't care if this analysis itself is preserved, it has no state. But
148fe6060f1SDimitry Andric   // we need to check that the analyses it depends on have been.
149fe6060f1SDimitry Andric   return Inv.invalidate<ScalarEvolutionAnalysis>(Fn, PA);
150fe6060f1SDimitry Andric }
151fe6060f1SDimitry Andric 
1520b57cec5SDimitry Andric AnalysisKey SCEVAA::Key;
1530b57cec5SDimitry Andric 
run(Function & F,FunctionAnalysisManager & AM)1540b57cec5SDimitry Andric SCEVAAResult SCEVAA::run(Function &F, FunctionAnalysisManager &AM) {
1550b57cec5SDimitry Andric   return SCEVAAResult(AM.getResult<ScalarEvolutionAnalysis>(F));
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric char SCEVAAWrapperPass::ID = 0;
1590b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(SCEVAAWrapperPass, "scev-aa",
1600b57cec5SDimitry Andric                       "ScalarEvolution-based Alias Analysis", false, true)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)1610b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
1620b57cec5SDimitry Andric INITIALIZE_PASS_END(SCEVAAWrapperPass, "scev-aa",
1630b57cec5SDimitry Andric                     "ScalarEvolution-based Alias Analysis", false, true)
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric FunctionPass *llvm::createSCEVAAWrapperPass() {
1660b57cec5SDimitry Andric   return new SCEVAAWrapperPass();
1670b57cec5SDimitry Andric }
1680b57cec5SDimitry Andric 
SCEVAAWrapperPass()1690b57cec5SDimitry Andric SCEVAAWrapperPass::SCEVAAWrapperPass() : FunctionPass(ID) {
1700b57cec5SDimitry Andric   initializeSCEVAAWrapperPassPass(*PassRegistry::getPassRegistry());
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric 
runOnFunction(Function & F)1730b57cec5SDimitry Andric bool SCEVAAWrapperPass::runOnFunction(Function &F) {
1740b57cec5SDimitry Andric   Result.reset(
1750b57cec5SDimitry Andric       new SCEVAAResult(getAnalysis<ScalarEvolutionWrapperPass>().getSE()));
1760b57cec5SDimitry Andric   return false;
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const1790b57cec5SDimitry Andric void SCEVAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1800b57cec5SDimitry Andric   AU.setPreservesAll();
1810b57cec5SDimitry Andric   AU.addRequired<ScalarEvolutionWrapperPass>();
1820b57cec5SDimitry Andric }
183