ScalarEvolutionAliasAnalysis.cpp 6.9 KB

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