blob: 2e50c80c4e731bb1ea3ff9ca3ab0da7cf3bdaea5 [file] [log] [blame]
Dan Gohmand926b982009-08-26 14:53:06 +00001//===- 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//
Dan Gohman904d34c2010-03-01 17:56:04 +000013// This differs from traditional loop dependence analysis in that it tests
14// for dependencies within a single iteration of a loop, rather than
Dan Gohman8ba26b42010-06-18 00:53:08 +000015// dependencies between different iterations.
Dan Gohman904d34c2010-03-01 17:56:04 +000016//
Dan Gohmand926b982009-08-26 14:53:06 +000017// ScalarEvolution has a more complete understanding of pointer arithmetic
18// than BasicAliasAnalysis' collection of ad-hoc analyses.
19//
20//===----------------------------------------------------------------------===//
21
Chandler Carruthed235282015-08-14 03:11:16 +000022#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000023#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohmand926b982009-08-26 14:53:06 +000024using namespace llvm;
25
Chandler Carruth7b560d42015-09-09 17:55:00 +000026AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
27 const MemoryLocation &LocB) {
Dan Gohman08659662010-06-30 06:12:16 +000028 // If either of the memory references is empty, it doesn't matter what the
29 // pointer values are. This allows the code below to ignore this special
30 // case.
Dan Gohman41f14cf2010-09-14 21:25:10 +000031 if (LocA.Size == 0 || LocB.Size == 0)
Dan Gohman08659662010-06-30 06:12:16 +000032 return NoAlias;
33
Chandler Carruth7b560d42015-09-09 17:55:00 +000034 // This is SCEVAAResult. Get the SCEVs!
35 const SCEV *AS = SE.getSCEV(const_cast<Value *>(LocA.Ptr));
36 const SCEV *BS = SE.getSCEV(const_cast<Value *>(LocB.Ptr));
Dan Gohmand926b982009-08-26 14:53:06 +000037
38 // If they evaluate to the same expression, it's a MustAlias.
Chandler Carruth79687fa2015-08-14 03:12:16 +000039 if (AS == BS)
40 return MustAlias;
Dan Gohmand926b982009-08-26 14:53:06 +000041
42 // If something is known about the difference between the two addresses,
43 // see if it's enough to prove a NoAlias.
Chandler Carruth7b560d42015-09-09 17:55:00 +000044 if (SE.getEffectiveSCEVType(AS->getType()) ==
45 SE.getEffectiveSCEVType(BS->getType())) {
46 unsigned BitWidth = SE.getTypeSizeInBits(AS->getType());
Dan Gohman41f14cf2010-09-14 21:25:10 +000047 APInt ASizeInt(BitWidth, LocA.Size);
48 APInt BSizeInt(BitWidth, LocB.Size);
Dan Gohman08659662010-06-30 06:12:16 +000049
50 // Compute the difference between the two pointers.
Chandler Carruth7b560d42015-09-09 17:55:00 +000051 const SCEV *BA = SE.getMinusSCEV(BS, AS);
Dan Gohman08659662010-06-30 06:12:16 +000052
53 // Test whether the difference is known to be great enough that memory of
54 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
55 // are non-zero, which is special-cased above.
Chandler Carruth7b560d42015-09-09 17:55:00 +000056 if (ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) &&
57 (-BSizeInt).uge(SE.getUnsignedRange(BA).getUnsignedMax()))
Dan Gohman08659662010-06-30 06:12:16 +000058 return NoAlias;
59
60 // Folding the subtraction while preserving range information can be tricky
61 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
62 // and try again to see if things fold better that way.
63
64 // Compute the difference between the two pointers.
Chandler Carruth7b560d42015-09-09 17:55:00 +000065 const SCEV *AB = SE.getMinusSCEV(AS, BS);
Dan Gohman08659662010-06-30 06:12:16 +000066
67 // Test whether the difference is known to be great enough that memory of
68 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
69 // are non-zero, which is special-cased above.
Chandler Carruth7b560d42015-09-09 17:55:00 +000070 if (BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) &&
71 (-ASizeInt).uge(SE.getUnsignedRange(AB).getUnsignedMax()))
Dan Gohman08659662010-06-30 06:12:16 +000072 return NoAlias;
Dan Gohmand926b982009-08-26 14:53:06 +000073 }
74
75 // If ScalarEvolution can find an underlying object, form a new query.
76 // The correctness of this depends on ScalarEvolution not recognizing
77 // inttoptr and ptrtoint operators.
Dan Gohmanac45c912009-10-31 14:32:25 +000078 Value *AO = GetBaseValue(AS);
79 Value *BO = GetBaseValue(BS);
Dan Gohman41f14cf2010-09-14 21:25:10 +000080 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
Chandler Carruthecbd1682015-06-17 07:21:38 +000081 if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
82 AO ? +MemoryLocation::UnknownSize : LocA.Size,
Chandler Carruthac80dc72015-06-17 07:18:54 +000083 AO ? AAMDNodes() : LocA.AATags),
Chandler Carruthecbd1682015-06-17 07:21:38 +000084 MemoryLocation(BO ? BO : LocB.Ptr,
85 BO ? +MemoryLocation::UnknownSize : LocB.Size,
Chandler Carruthac80dc72015-06-17 07:18:54 +000086 BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
Dan Gohmand926b982009-08-26 14:53:06 +000087 return NoAlias;
88
89 // Forward the query to the next analysis.
Chandler Carruth7b560d42015-09-09 17:55:00 +000090 return AAResultBase::alias(LocA, LocB);
91}
92
93/// Given an expression, try to find a base value.
94///
95/// Returns null if none was found.
96Value *SCEVAAResult::GetBaseValue(const SCEV *S) {
97 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
98 // In an addrec, assume that the base will be in the start, rather
99 // than the step.
100 return GetBaseValue(AR->getStart());
101 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
102 // If there's a pointer operand, it'll be sorted at the end of the list.
103 const SCEV *Last = A->getOperand(A->getNumOperands() - 1);
104 if (Last->getType()->isPointerTy())
105 return GetBaseValue(Last);
106 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
107 // This is a leaf node.
108 return U->getValue();
109 }
110 // No Identified object found.
111 return nullptr;
112}
113
114SCEVAAResult SCEVAA::run(Function &F, AnalysisManager<Function> *AM) {
115 return SCEVAAResult(AM->getResult<TargetLibraryAnalysis>(F),
116 AM->getResult<ScalarEvolutionAnalysis>(F));
117}
118
119char SCEVAA::PassID;
120
121char SCEVAAWrapperPass::ID = 0;
122INITIALIZE_PASS_BEGIN(SCEVAAWrapperPass, "scev-aa",
123 "ScalarEvolution-based Alias Analysis", false, true)
124INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
125INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
126INITIALIZE_PASS_END(SCEVAAWrapperPass, "scev-aa",
127 "ScalarEvolution-based Alias Analysis", false, true)
128
129FunctionPass *llvm::createSCEVAAWrapperPass() {
130 return new SCEVAAWrapperPass();
131}
132
133SCEVAAWrapperPass::SCEVAAWrapperPass() : FunctionPass(ID) {
134 initializeSCEVAAWrapperPassPass(*PassRegistry::getPassRegistry());
135}
136
137bool SCEVAAWrapperPass::runOnFunction(Function &F) {
138 Result.reset(
139 new SCEVAAResult(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
140 getAnalysis<ScalarEvolutionWrapperPass>().getSE()));
141 return false;
142}
143
144void SCEVAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
145 AU.setPreservesAll();
146 AU.addRequired<ScalarEvolutionWrapperPass>();
147 AU.addRequired<TargetLibraryInfoWrapperPass>();
Dan Gohmand926b982009-08-26 14:53:06 +0000148}