blob: 289d4f8ae49ae21d00aa2e0b07b8985245ca0cb5 [file] [log] [blame]
Dan Gohman2385e0e2009-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 Gohman7cbf5a82010-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 Gohman4f46e142010-06-18 00:53:08 +000015// dependencies between different iterations.
Dan Gohman7cbf5a82010-03-01 17:56:04 +000016//
Dan Gohman2385e0e2009-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 Carruth217849d2015-08-14 03:11:16 +000022#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Dan Gohman2385e0e2009-08-26 14:53:06 +000023using namespace llvm;
24
Chandler Carruth91468332015-09-09 17:55:00 +000025AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
26 const MemoryLocation &LocB) {
Dan Gohman49bda912010-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.
George Burgess IVf50772a2018-12-22 18:23:21 +000030 if (LocA.Size.isZero() || LocB.Size.isZero())
Dan Gohman49bda912010-06-30 06:12:16 +000031 return NoAlias;
32
Chandler Carruth91468332015-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 Gohman2385e0e2009-08-26 14:53:06 +000036
37 // If they evaluate to the same expression, it's a MustAlias.
Chandler Carruth72644df2015-08-14 03:12:16 +000038 if (AS == BS)
39 return MustAlias;
Dan Gohman2385e0e2009-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 Carruth91468332015-09-09 17:55:00 +000043 if (SE.getEffectiveSCEVType(AS->getType()) ==
44 SE.getEffectiveSCEVType(BS->getType())) {
45 unsigned BitWidth = SE.getTypeSizeInBits(AS->getType());
George Burgess IVdef2c062018-10-09 03:18:56 +000046 APInt ASizeInt(BitWidth, LocA.Size.hasValue()
47 ? LocA.Size.getValue()
48 : MemoryLocation::UnknownSize);
49 APInt BSizeInt(BitWidth, LocB.Size.hasValue()
50 ? LocB.Size.getValue()
51 : MemoryLocation::UnknownSize);
Dan Gohman49bda912010-06-30 06:12:16 +000052
53 // Compute the difference between the two pointers.
Chandler Carruth91468332015-09-09 17:55:00 +000054 const SCEV *BA = SE.getMinusSCEV(BS, AS);
Dan Gohman49bda912010-06-30 06:12:16 +000055
56 // Test whether the difference is known to be great enough that memory of
57 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
58 // are non-zero, which is special-cased above.
Chandler Carruth91468332015-09-09 17:55:00 +000059 if (ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) &&
60 (-BSizeInt).uge(SE.getUnsignedRange(BA).getUnsignedMax()))
Dan Gohman49bda912010-06-30 06:12:16 +000061 return NoAlias;
62
63 // Folding the subtraction while preserving range information can be tricky
64 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
65 // and try again to see if things fold better that way.
66
67 // Compute the difference between the two pointers.
Chandler Carruth91468332015-09-09 17:55:00 +000068 const SCEV *AB = SE.getMinusSCEV(AS, BS);
Dan Gohman49bda912010-06-30 06:12:16 +000069
70 // Test whether the difference is known to be great enough that memory of
71 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
72 // are non-zero, which is special-cased above.
Chandler Carruth91468332015-09-09 17:55:00 +000073 if (BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) &&
74 (-ASizeInt).uge(SE.getUnsignedRange(AB).getUnsignedMax()))
Dan Gohman49bda912010-06-30 06:12:16 +000075 return NoAlias;
Dan Gohman2385e0e2009-08-26 14:53:06 +000076 }
77
78 // If ScalarEvolution can find an underlying object, form a new query.
79 // The correctness of this depends on ScalarEvolution not recognizing
80 // inttoptr and ptrtoint operators.
Dan Gohman576fd762009-10-31 14:32:25 +000081 Value *AO = GetBaseValue(AS);
82 Value *BO = GetBaseValue(BS);
Dan Gohmanb2143b62010-09-14 21:25:10 +000083 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
Chandler Carruth2cdca0c42015-06-17 07:21:38 +000084 if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
George Burgess IVf50772a2018-12-22 18:23:21 +000085 AO ? LocationSize::unknown() : LocA.Size,
Chandler Carruth4d7ed392015-06-17 07:18:54 +000086 AO ? AAMDNodes() : LocA.AATags),
Chandler Carruth2cdca0c42015-06-17 07:21:38 +000087 MemoryLocation(BO ? BO : LocB.Ptr,
George Burgess IVf50772a2018-12-22 18:23:21 +000088 BO ? LocationSize::unknown() : LocB.Size,
Chandler Carruth4d7ed392015-06-17 07:18:54 +000089 BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
Dan Gohman2385e0e2009-08-26 14:53:06 +000090 return NoAlias;
91
92 // Forward the query to the next analysis.
Chandler Carruth91468332015-09-09 17:55:00 +000093 return AAResultBase::alias(LocA, LocB);
94}
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 Carruth33d56812016-11-23 17:53:26 +0000117AnalysisKey SCEVAA::Key;
Chandler Carruthe95015f2016-03-11 10:22:49 +0000118
Sean Silva20b343c2016-08-09 00:28:15 +0000119SCEVAAResult SCEVAA::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruth8e27cb22016-03-11 11:05:24 +0000120 return SCEVAAResult(AM.getResult<ScalarEvolutionAnalysis>(F));
Chandler Carruth91468332015-09-09 17:55:00 +0000121}
122
Chandler Carruth91468332015-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 Carruth91468332015-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 Carruthcf88e922016-03-02 15:56:53 +0000140 new SCEVAAResult(getAnalysis<ScalarEvolutionWrapperPass>().getSE()));
Chandler Carruth91468332015-09-09 17:55:00 +0000141 return false;
142}
143
144void SCEVAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
145 AU.setPreservesAll();
146 AU.addRequired<ScalarEvolutionWrapperPass>();
Dan Gohman2385e0e2009-08-26 14:53:06 +0000147}