LLVM  3.7.0
ScalarEvolutionAliasAnalysis.cpp
Go to the documentation of this file.
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 
22 #include "llvm/Analysis/Passes.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Pass.h"
27 using namespace llvm;
28 
29 namespace {
30  /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
31  /// implementation that uses ScalarEvolution to answer queries.
32  class ScalarEvolutionAliasAnalysis : public FunctionPass,
33  public AliasAnalysis {
34  ScalarEvolution *SE;
35 
36  public:
37  static char ID; // Class identification, replacement for typeinfo
38  ScalarEvolutionAliasAnalysis() : FunctionPass(ID), SE(nullptr) {
41  }
42 
43  /// getAdjustedAnalysisPointer - This method is used when a pass implements
44  /// an analysis interface through multiple inheritance. If needed, it
45  /// should override this to adjust the this pointer as needed for the
46  /// specified pass info.
47  void *getAdjustedAnalysisPointer(AnalysisID PI) override {
48  if (PI == &AliasAnalysis::ID)
49  return (AliasAnalysis*)this;
50  return this;
51  }
52 
53  private:
54  void getAnalysisUsage(AnalysisUsage &AU) const override;
55  bool runOnFunction(Function &F) override;
56  AliasResult alias(const MemoryLocation &LocA,
57  const MemoryLocation &LocB) override;
58 
59  Value *GetBaseValue(const SCEV *S);
60  };
61 } // End of anonymous namespace
62 
63 // Register this pass...
65 INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
66  "ScalarEvolution-based Alias Analysis", false, true, false)
68 INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
69  "ScalarEvolution-based Alias Analysis", false, true, false)
70 
72  return new ScalarEvolutionAliasAnalysis();
73 }
74 
75 void
76 ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
78  AU.setPreservesAll();
80 }
81 
82 bool
83 ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
84  InitializeAliasAnalysis(this, &F.getParent()->getDataLayout());
85  SE = &getAnalysis<ScalarEvolution>();
86  return false;
87 }
88 
89 /// GetBaseValue - Given an expression, try to find a
90 /// base value. Return null is none was found.
91 Value *
92 ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
93  if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
94  // In an addrec, assume that the base will be in the start, rather
95  // than the step.
96  return GetBaseValue(AR->getStart());
97  } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
98  // If there's a pointer operand, it'll be sorted at the end of the list.
99  const SCEV *Last = A->getOperand(A->getNumOperands()-1);
100  if (Last->getType()->isPointerTy())
101  return GetBaseValue(Last);
102  } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
103  // This is a leaf node.
104  return U->getValue();
105  }
106  // No Identified object found.
107  return nullptr;
108 }
109 
110 AliasResult ScalarEvolutionAliasAnalysis::alias(const MemoryLocation &LocA,
111  const MemoryLocation &LocB) {
112  // If either of the memory references is empty, it doesn't matter what the
113  // pointer values are. This allows the code below to ignore this special
114  // case.
115  if (LocA.Size == 0 || LocB.Size == 0)
116  return NoAlias;
117 
118  // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
119  const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
120  const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
121 
122  // If they evaluate to the same expression, it's a MustAlias.
123  if (AS == BS) return MustAlias;
124 
125  // If something is known about the difference between the two addresses,
126  // see if it's enough to prove a NoAlias.
127  if (SE->getEffectiveSCEVType(AS->getType()) ==
128  SE->getEffectiveSCEVType(BS->getType())) {
129  unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
130  APInt ASizeInt(BitWidth, LocA.Size);
131  APInt BSizeInt(BitWidth, LocB.Size);
132 
133  // Compute the difference between the two pointers.
134  const SCEV *BA = SE->getMinusSCEV(BS, AS);
135 
136  // Test whether the difference is known to be great enough that memory of
137  // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
138  // are non-zero, which is special-cased above.
139  if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
140  (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
141  return NoAlias;
142 
143  // Folding the subtraction while preserving range information can be tricky
144  // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
145  // and try again to see if things fold better that way.
146 
147  // Compute the difference between the two pointers.
148  const SCEV *AB = SE->getMinusSCEV(AS, BS);
149 
150  // Test whether the difference is known to be great enough that memory of
151  // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
152  // are non-zero, which is special-cased above.
153  if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
154  (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
155  return NoAlias;
156  }
157 
158  // If ScalarEvolution can find an underlying object, form a new query.
159  // The correctness of this depends on ScalarEvolution not recognizing
160  // inttoptr and ptrtoint operators.
161  Value *AO = GetBaseValue(AS);
162  Value *BO = GetBaseValue(BS);
163  if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
164  if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
165  AO ? +MemoryLocation::UnknownSize : LocA.Size,
166  AO ? AAMDNodes() : LocA.AATags),
167  MemoryLocation(BO ? BO : LocB.Ptr,
168  BO ? +MemoryLocation::UnknownSize : LocB.Size,
169  BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
170  return NoAlias;
171 
172  // Forward the query to the next analysis.
173  return AliasAnalysis::alias(LocA, LocB);
174 }
The two locations precisely alias each other.
Definition: AliasAnalysis.h:84
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
FunctionPass * createScalarEvolutionAliasAnalysisPass()
scev ScalarEvolution based Alias Analysis
ScalarEvolution - This class is the main scalar evolution driver.
void initializeScalarEvolutionAliasAnalysisPass(PassRegistry &)
The two locations do not alias at all.
Definition: AliasAnalysis.h:78
F(f)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
SCEVAddRecExpr - This node represents a polynomial recurrence on the trip count of the specified loop...
SCEVUnknown - This means that we are dealing with an entirely unknown SCEV value, and only represent ...
Type * getType() const
getType - Return the LLVM type of this SCEV expression.
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:72
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
Definition: Type.h:217
scev ScalarEvolution based Alias false
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
virtual AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
Alias Queries...
Module.h This file contains the declarations for the Module class.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:548
INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis,"scev-aa","ScalarEvolution-based Alias Analysis", false, true, false) INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis
Class for arbitrary precision integers.
Definition: APInt.h:73
SCEVAddExpr - This node represents an addition of some number of SCEVs.
void setPreservesAll()
Set by analyses that do not transform their input at all.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
SCEV - This class represents an analyzed expression in the program.
AnalysisUsage & addRequiredTransitive()
const void * AnalysisID
Definition: Pass.h:47
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
LLVM Value Representation.
Definition: Value.h:69
scev ScalarEvolution based Alias true
virtual void getAnalysisUsage(AnalysisUsage &AU) const
getAnalysisUsage - All alias analysis implementations should invoke this directly (using AliasAnalysi...
#define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def)
Definition: PassSupport.h:205
uint64_t Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known...