blob: f15b1787f003e78bfcb74d2219f1e6dc03a4c447 [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"
Dan Gohmand926b982009-08-26 14:53:06 +000023using namespace llvm;
24
Chandler Carruth7b560d42015-09-09 17:55:00 +000025AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
26 const MemoryLocation &LocB) {
Dan Gohman08659662010-06-30 06:12:16 +000027 // If either of the memory references is empty, it doesn't matter what the
28 // pointer values are. This allows the code below to ignore this special
29 // case.
Dan Gohman41f14cf2010-09-14 21:25:10 +000030 if (LocA.Size == 0 || LocB.Size == 0)
Dan Gohman08659662010-06-30 06:12:16 +000031 return NoAlias;
32
Chandler Carruth7b560d42015-09-09 17:55:00 +000033 // This is SCEVAAResult. Get the SCEVs!
34 const SCEV *AS = SE.getSCEV(const_cast<Value *>(LocA.Ptr));
35 const SCEV *BS = SE.getSCEV(const_cast<Value *>(LocB.Ptr));
Dan Gohmand926b982009-08-26 14:53:06 +000036
37 // If they evaluate to the same expression, it's a MustAlias.
Chandler Carruth79687fa2015-08-14 03:12:16 +000038 if (AS == BS)
39 return MustAlias;
Dan Gohmand926b982009-08-26 14:53:06 +000040
41 // If something is known about the difference between the two addresses,
42 // see if it's enough to prove a NoAlias.
Chandler Carruth7b560d42015-09-09 17:55:00 +000043 if (SE.getEffectiveSCEVType(AS->getType()) ==
44 SE.getEffectiveSCEVType(BS->getType())) {
45 unsigned BitWidth = SE.getTypeSizeInBits(AS->getType());
Dan Gohman41f14cf2010-09-14 21:25:10 +000046 APInt ASizeInt(BitWidth, LocA.Size);
47 APInt BSizeInt(BitWidth, LocB.Size);
Dan Gohman08659662010-06-30 06:12:16 +000048
49 // Compute the difference between the two pointers.
Chandler Carruth7b560d42015-09-09 17:55:00 +000050 const SCEV *BA = SE.getMinusSCEV(BS, AS);
Dan Gohman08659662010-06-30 06:12:16 +000051
52 // Test whether the difference is known to be great enough that memory of
53 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
54 // are non-zero, which is special-cased above.
Chandler Carruth7b560d42015-09-09 17:55:00 +000055 if (ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) &&
56 (-BSizeInt).uge(SE.getUnsignedRange(BA).getUnsignedMax()))
Dan Gohman08659662010-06-30 06:12:16 +000057 return NoAlias;
58
59 // Folding the subtraction while preserving range information can be tricky
60 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
61 // and try again to see if things fold better that way.
62
63 // Compute the difference between the two pointers.
Chandler Carruth7b560d42015-09-09 17:55:00 +000064 const SCEV *AB = SE.getMinusSCEV(AS, BS);
Dan Gohman08659662010-06-30 06:12:16 +000065
66 // Test whether the difference is known to be great enough that memory of
67 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
68 // are non-zero, which is special-cased above.
Chandler Carruth7b560d42015-09-09 17:55:00 +000069 if (BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) &&
70 (-ASizeInt).uge(SE.getUnsignedRange(AB).getUnsignedMax()))
Dan Gohman08659662010-06-30 06:12:16 +000071 return NoAlias;
Dan Gohmand926b982009-08-26 14:53:06 +000072 }
73
74 // If ScalarEvolution can find an underlying object, form a new query.
75 // The correctness of this depends on ScalarEvolution not recognizing
76 // inttoptr and ptrtoint operators.
Dan Gohmanac45c912009-10-31 14:32:25 +000077 Value *AO = GetBaseValue(AS);
78 Value *BO = GetBaseValue(BS);
Dan Gohman41f14cf2010-09-14 21:25:10 +000079 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
Chandler Carruthecbd1682015-06-17 07:21:38 +000080 if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
81 AO ? +MemoryLocation::UnknownSize : LocA.Size,
Chandler Carruthac80dc72015-06-17 07:18:54 +000082 AO ? AAMDNodes() : LocA.AATags),
Chandler Carruthecbd1682015-06-17 07:21:38 +000083 MemoryLocation(BO ? BO : LocB.Ptr,
84 BO ? +MemoryLocation::UnknownSize : LocB.Size,
Chandler Carruthac80dc72015-06-17 07:18:54 +000085 BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
Dan Gohmand926b982009-08-26 14:53:06 +000086 return NoAlias;
87
88 // Forward the query to the next analysis.
Chandler Carruth7b560d42015-09-09 17:55:00 +000089 return AAResultBase::alias(LocA, LocB);
90}
91
92/// Given an expression, try to find a base value.
93///
94/// Returns null if none was found.
95Value *SCEVAAResult::GetBaseValue(const SCEV *S) {
96 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
97 // In an addrec, assume that the base will be in the start, rather
98 // than the step.
99 return GetBaseValue(AR->getStart());
100 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
101 // If there's a pointer operand, it'll be sorted at the end of the list.
102 const SCEV *Last = A->getOperand(A->getNumOperands() - 1);
103 if (Last->getType()->isPointerTy())
104 return GetBaseValue(Last);
105 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
106 // This is a leaf node.
107 return U->getValue();
108 }
109 // No Identified object found.
110 return nullptr;
111}
112
Chandler Carruthb4faf132016-03-11 10:22:49 +0000113char SCEVAA::PassID;
114
Sean Silva36e0d012016-08-09 00:28:15 +0000115SCEVAAResult SCEVAA::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +0000116 return SCEVAAResult(AM.getResult<ScalarEvolutionAnalysis>(F));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000117}
118
Chandler Carruth7b560d42015-09-09 17:55:00 +0000119char SCEVAAWrapperPass::ID = 0;
120INITIALIZE_PASS_BEGIN(SCEVAAWrapperPass, "scev-aa",
121 "ScalarEvolution-based Alias Analysis", false, true)
122INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000123INITIALIZE_PASS_END(SCEVAAWrapperPass, "scev-aa",
124 "ScalarEvolution-based Alias Analysis", false, true)
125
126FunctionPass *llvm::createSCEVAAWrapperPass() {
127 return new SCEVAAWrapperPass();
128}
129
130SCEVAAWrapperPass::SCEVAAWrapperPass() : FunctionPass(ID) {
131 initializeSCEVAAWrapperPassPass(*PassRegistry::getPassRegistry());
132}
133
134bool SCEVAAWrapperPass::runOnFunction(Function &F) {
135 Result.reset(
Chandler Carruth12884f72016-03-02 15:56:53 +0000136 new SCEVAAResult(getAnalysis<ScalarEvolutionWrapperPass>().getSE()));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000137 return false;
138}
139
140void SCEVAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
141 AU.setPreservesAll();
142 AU.addRequired<ScalarEvolutionWrapperPass>();
Dan Gohmand926b982009-08-26 14:53:06 +0000143}