ScalarEvolutionAliasAnalysis.cpp 6.9 KB

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