blob: 58d3322683a94bee809456363a657d35fbb17b53 [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
Dan Gohmand926b982009-08-26 14:53:06 +000025// Register this pass...
26char ScalarEvolutionAliasAnalysis::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000027INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
Chandler Carruth79687fa2015-08-14 03:12:16 +000028 "ScalarEvolution-based Alias Analysis", false, true,
29 false)
Owen Anderson8ac477f2010-10-12 19:48:12 +000030INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
31INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
Chandler Carruth79687fa2015-08-14 03:12:16 +000032 "ScalarEvolution-based Alias Analysis", false, true,
33 false)
Dan Gohmand926b982009-08-26 14:53:06 +000034
35FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
36 return new ScalarEvolutionAliasAnalysis();
37}
38
Chandler Carruth79687fa2015-08-14 03:12:16 +000039void ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmand926b982009-08-26 14:53:06 +000040 AU.addRequiredTransitive<ScalarEvolution>();
41 AU.setPreservesAll();
42 AliasAnalysis::getAnalysisUsage(AU);
43}
44
Chandler Carruth79687fa2015-08-14 03:12:16 +000045bool ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
Mehdi Amini46a43552015-03-04 18:43:29 +000046 InitializeAliasAnalysis(this, &F.getParent()->getDataLayout());
Dan Gohmand926b982009-08-26 14:53:06 +000047 SE = &getAnalysis<ScalarEvolution>();
48 return false;
49}
50
Chandler Carruth55eec8b2015-08-14 03:14:50 +000051/// Given an expression, try to find a base value.
52///
53/// Returns null if none was found.
Chandler Carruth79687fa2015-08-14 03:12:16 +000054Value *ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
Dan Gohmand926b982009-08-26 14:53:06 +000055 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
Dan Gohman311d0692009-08-29 23:36:57 +000056 // In an addrec, assume that the base will be in the start, rather
57 // than the step.
Dan Gohmanac45c912009-10-31 14:32:25 +000058 return GetBaseValue(AR->getStart());
Dan Gohmand926b982009-08-26 14:53:06 +000059 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
60 // If there's a pointer operand, it'll be sorted at the end of the list.
Chandler Carruth79687fa2015-08-14 03:12:16 +000061 const SCEV *Last = A->getOperand(A->getNumOperands() - 1);
Duncan Sands19d0b472010-02-16 11:11:14 +000062 if (Last->getType()->isPointerTy())
Dan Gohmanac45c912009-10-31 14:32:25 +000063 return GetBaseValue(Last);
Dan Gohmand926b982009-08-26 14:53:06 +000064 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
Dan Gohmanac45c912009-10-31 14:32:25 +000065 // This is a leaf node.
66 return U->getValue();
Dan Gohmand926b982009-08-26 14:53:06 +000067 }
68 // No Identified object found.
Craig Topper9f008862014-04-15 04:59:12 +000069 return nullptr;
Dan Gohmand926b982009-08-26 14:53:06 +000070}
71
Chandler Carruthc3f49eb2015-06-22 02:16:51 +000072AliasResult ScalarEvolutionAliasAnalysis::alias(const MemoryLocation &LocA,
73 const MemoryLocation &LocB) {
Dan Gohman08659662010-06-30 06:12:16 +000074 // If either of the memory references is empty, it doesn't matter what the
75 // pointer values are. This allows the code below to ignore this special
76 // case.
Dan Gohman41f14cf2010-09-14 21:25:10 +000077 if (LocA.Size == 0 || LocB.Size == 0)
Dan Gohman08659662010-06-30 06:12:16 +000078 return NoAlias;
79
Dan Gohmand926b982009-08-26 14:53:06 +000080 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
Dan Gohman41f14cf2010-09-14 21:25:10 +000081 const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
82 const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
Dan Gohmand926b982009-08-26 14:53:06 +000083
84 // If they evaluate to the same expression, it's a MustAlias.
Chandler Carruth79687fa2015-08-14 03:12:16 +000085 if (AS == BS)
86 return MustAlias;
Dan Gohmand926b982009-08-26 14:53:06 +000087
88 // If something is known about the difference between the two addresses,
89 // see if it's enough to prove a NoAlias.
90 if (SE->getEffectiveSCEVType(AS->getType()) ==
91 SE->getEffectiveSCEVType(BS->getType())) {
92 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
Dan Gohman41f14cf2010-09-14 21:25:10 +000093 APInt ASizeInt(BitWidth, LocA.Size);
94 APInt BSizeInt(BitWidth, LocB.Size);
Dan Gohman08659662010-06-30 06:12:16 +000095
96 // Compute the difference between the two pointers.
Dan Gohmand926b982009-08-26 14:53:06 +000097 const SCEV *BA = SE->getMinusSCEV(BS, AS);
Dan Gohman08659662010-06-30 06:12:16 +000098
99 // Test whether the difference is known to be great enough that memory of
100 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
101 // are non-zero, which is special-cased above.
102 if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
103 (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
104 return NoAlias;
105
106 // Folding the subtraction while preserving range information can be tricky
107 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
108 // and try again to see if things fold better that way.
109
110 // Compute the difference between the two pointers.
111 const SCEV *AB = SE->getMinusSCEV(AS, BS);
112
113 // Test whether the difference is known to be great enough that memory of
114 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
115 // are non-zero, which is special-cased above.
116 if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
117 (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
118 return NoAlias;
Dan Gohmand926b982009-08-26 14:53:06 +0000119 }
120
121 // If ScalarEvolution can find an underlying object, form a new query.
122 // The correctness of this depends on ScalarEvolution not recognizing
123 // inttoptr and ptrtoint operators.
Dan Gohmanac45c912009-10-31 14:32:25 +0000124 Value *AO = GetBaseValue(AS);
125 Value *BO = GetBaseValue(BS);
Dan Gohman41f14cf2010-09-14 21:25:10 +0000126 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
Chandler Carruthecbd1682015-06-17 07:21:38 +0000127 if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
128 AO ? +MemoryLocation::UnknownSize : LocA.Size,
Chandler Carruthac80dc72015-06-17 07:18:54 +0000129 AO ? AAMDNodes() : LocA.AATags),
Chandler Carruthecbd1682015-06-17 07:21:38 +0000130 MemoryLocation(BO ? BO : LocB.Ptr,
131 BO ? +MemoryLocation::UnknownSize : LocB.Size,
Chandler Carruthac80dc72015-06-17 07:18:54 +0000132 BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
Dan Gohmand926b982009-08-26 14:53:06 +0000133 return NoAlias;
134
135 // Forward the query to the next analysis.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000136 return AliasAnalysis::alias(LocA, LocB);
Dan Gohmand926b982009-08-26 14:53:06 +0000137}