blob: 96da0a24cddd4d1e1d79315099510c03501391a3 [file] [log] [blame]
Dan Gohmand926b982009-08-26 14:53:06 +00001//===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohmand926b982009-08-26 14:53:06 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ScalarEvolutionAliasAnalysis pass, which implements a
10// simple alias analysis implemented in terms of ScalarEvolution queries.
11//
Dan Gohman904d34c2010-03-01 17:56:04 +000012// This differs from traditional loop dependence analysis in that it tests
13// for dependencies within a single iteration of a loop, rather than
Dan Gohman8ba26b42010-06-18 00:53:08 +000014// dependencies between different iterations.
Dan Gohman904d34c2010-03-01 17:56:04 +000015//
Dan Gohmand926b982009-08-26 14:53:06 +000016// ScalarEvolution has a more complete understanding of pointer arithmetic
17// than BasicAliasAnalysis' collection of ad-hoc analyses.
18//
19//===----------------------------------------------------------------------===//
20
Chandler Carruthed235282015-08-14 03:11:16 +000021#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Dan Gohmand926b982009-08-26 14:53:06 +000022using namespace llvm;
23
Chandler Carruth7b560d42015-09-09 17:55:00 +000024AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
Alina Sbirleabfc779e2019-03-22 17:22:19 +000025 const MemoryLocation &LocB, AAQueryInfo &AAQI) {
Dan Gohman08659662010-06-30 06:12:16 +000026 // If either of the memory references is empty, it doesn't matter what the
27 // pointer values are. This allows the code below to ignore this special
28 // case.
George Burgess IV640be692018-12-22 18:23:21 +000029 if (LocA.Size.isZero() || LocB.Size.isZero())
Dan Gohman08659662010-06-30 06:12:16 +000030 return NoAlias;
31
Chandler Carruth7b560d42015-09-09 17:55:00 +000032 // This is SCEVAAResult. Get the SCEVs!
33 const SCEV *AS = SE.getSCEV(const_cast<Value *>(LocA.Ptr));
34 const SCEV *BS = SE.getSCEV(const_cast<Value *>(LocB.Ptr));
Dan Gohmand926b982009-08-26 14:53:06 +000035
36 // If they evaluate to the same expression, it's a MustAlias.
Chandler Carruth79687fa2015-08-14 03:12:16 +000037 if (AS == BS)
38 return MustAlias;
Dan Gohmand926b982009-08-26 14:53:06 +000039
40 // If something is known about the difference between the two addresses,
41 // see if it's enough to prove a NoAlias.
Chandler Carruth7b560d42015-09-09 17:55:00 +000042 if (SE.getEffectiveSCEVType(AS->getType()) ==
43 SE.getEffectiveSCEVType(BS->getType())) {
44 unsigned BitWidth = SE.getTypeSizeInBits(AS->getType());
George Burgess IVf96e6182018-10-09 03:18:56 +000045 APInt ASizeInt(BitWidth, LocA.Size.hasValue()
46 ? LocA.Size.getValue()
47 : MemoryLocation::UnknownSize);
48 APInt BSizeInt(BitWidth, LocB.Size.hasValue()
49 ? LocB.Size.getValue()
50 : MemoryLocation::UnknownSize);
Dan Gohman08659662010-06-30 06:12:16 +000051
52 // Compute the difference between the two pointers.
Chandler Carruth7b560d42015-09-09 17:55:00 +000053 const SCEV *BA = SE.getMinusSCEV(BS, AS);
Dan Gohman08659662010-06-30 06:12:16 +000054
55 // Test whether the difference is known to be great enough that memory of
56 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
57 // are non-zero, which is special-cased above.
Chandler Carruth7b560d42015-09-09 17:55:00 +000058 if (ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) &&
59 (-BSizeInt).uge(SE.getUnsignedRange(BA).getUnsignedMax()))
Dan Gohman08659662010-06-30 06:12:16 +000060 return NoAlias;
61
62 // Folding the subtraction while preserving range information can be tricky
63 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
64 // and try again to see if things fold better that way.
65
66 // Compute the difference between the two pointers.
Chandler Carruth7b560d42015-09-09 17:55:00 +000067 const SCEV *AB = SE.getMinusSCEV(AS, BS);
Dan Gohman08659662010-06-30 06:12:16 +000068
69 // Test whether the difference is known to be great enough that memory of
70 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
71 // are non-zero, which is special-cased above.
Chandler Carruth7b560d42015-09-09 17:55:00 +000072 if (BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) &&
73 (-ASizeInt).uge(SE.getUnsignedRange(AB).getUnsignedMax()))
Dan Gohman08659662010-06-30 06:12:16 +000074 return NoAlias;
Dan Gohmand926b982009-08-26 14:53:06 +000075 }
76
77 // If ScalarEvolution can find an underlying object, form a new query.
78 // The correctness of this depends on ScalarEvolution not recognizing
79 // inttoptr and ptrtoint operators.
Dan Gohmanac45c912009-10-31 14:32:25 +000080 Value *AO = GetBaseValue(AS);
81 Value *BO = GetBaseValue(BS);
Dan Gohman41f14cf2010-09-14 21:25:10 +000082 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
Chandler Carruthecbd1682015-06-17 07:21:38 +000083 if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
George Burgess IV640be692018-12-22 18:23:21 +000084 AO ? LocationSize::unknown() : LocA.Size,
Chandler Carruthac80dc72015-06-17 07:18:54 +000085 AO ? AAMDNodes() : LocA.AATags),
Chandler Carruthecbd1682015-06-17 07:21:38 +000086 MemoryLocation(BO ? BO : LocB.Ptr,
George Burgess IV640be692018-12-22 18:23:21 +000087 BO ? LocationSize::unknown() : LocB.Size,
Alina Sbirleabfc779e2019-03-22 17:22:19 +000088 BO ? AAMDNodes() : LocB.AATags),
89 AAQI) == NoAlias)
Dan Gohmand926b982009-08-26 14:53:06 +000090 return NoAlias;
91
92 // Forward the query to the next analysis.
Alina Sbirleabfc779e2019-03-22 17:22:19 +000093 return AAResultBase::alias(LocA, LocB, AAQI);
Chandler Carruth7b560d42015-09-09 17:55:00 +000094}
95
96/// Given an expression, try to find a base value.
97///
98/// Returns null if none was found.
99Value *SCEVAAResult::GetBaseValue(const SCEV *S) {
100 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
101 // In an addrec, assume that the base will be in the start, rather
102 // than the step.
103 return GetBaseValue(AR->getStart());
104 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
105 // If there's a pointer operand, it'll be sorted at the end of the list.
106 const SCEV *Last = A->getOperand(A->getNumOperands() - 1);
107 if (Last->getType()->isPointerTy())
108 return GetBaseValue(Last);
109 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
110 // This is a leaf node.
111 return U->getValue();
112 }
113 // No Identified object found.
114 return nullptr;
115}
116
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000117AnalysisKey SCEVAA::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000118
Sean Silva36e0d012016-08-09 00:28:15 +0000119SCEVAAResult SCEVAA::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +0000120 return SCEVAAResult(AM.getResult<ScalarEvolutionAnalysis>(F));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000121}
122
Chandler Carruth7b560d42015-09-09 17:55:00 +0000123char SCEVAAWrapperPass::ID = 0;
124INITIALIZE_PASS_BEGIN(SCEVAAWrapperPass, "scev-aa",
125 "ScalarEvolution-based Alias Analysis", false, true)
126INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000127INITIALIZE_PASS_END(SCEVAAWrapperPass, "scev-aa",
128 "ScalarEvolution-based Alias Analysis", false, true)
129
130FunctionPass *llvm::createSCEVAAWrapperPass() {
131 return new SCEVAAWrapperPass();
132}
133
134SCEVAAWrapperPass::SCEVAAWrapperPass() : FunctionPass(ID) {
135 initializeSCEVAAWrapperPassPass(*PassRegistry::getPassRegistry());
136}
137
138bool SCEVAAWrapperPass::runOnFunction(Function &F) {
139 Result.reset(
Chandler Carruth12884f72016-03-02 15:56:53 +0000140 new SCEVAAResult(getAnalysis<ScalarEvolutionWrapperPass>().getSE()));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000141 return false;
142}
143
144void SCEVAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
145 AU.setPreservesAll();
146 AU.addRequired<ScalarEvolutionWrapperPass>();
Dan Gohmand926b982009-08-26 14:53:06 +0000147}