ScalarEvolutionAliasAnalysis.cpp 6.8 KB

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