Dan Gohman | d926b98 | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 1 | //===- 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 Gohman | 904d34c | 2010-03-01 17:56:04 +0000 | [diff] [blame] | 13 | // This differs from traditional loop dependence analysis in that it tests |
| 14 | // for dependencies within a single iteration of a loop, rather than |
Dan Gohman | 8ba26b4 | 2010-06-18 00:53:08 +0000 | [diff] [blame] | 15 | // dependencies between different iterations. |
Dan Gohman | 904d34c | 2010-03-01 17:56:04 +0000 | [diff] [blame] | 16 | // |
Dan Gohman | d926b98 | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 17 | // ScalarEvolution has a more complete understanding of pointer arithmetic |
| 18 | // than BasicAliasAnalysis' collection of ad-hoc analyses. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Chandler Carruth | ed23528 | 2015-08-14 03:11:16 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Dan Gohman | d926b98 | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 26 | AliasResult SCEVAAResult::alias(const MemoryLocation &LocA, |
| 27 | const MemoryLocation &LocB) { |
Dan Gohman | 0865966 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 28 | // 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 Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 31 | if (LocA.Size == 0 || LocB.Size == 0) |
Dan Gohman | 0865966 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 32 | return NoAlias; |
| 33 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 34 | // 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 Gohman | d926b98 | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 37 | |
| 38 | // If they evaluate to the same expression, it's a MustAlias. |
Chandler Carruth | 79687fa | 2015-08-14 03:12:16 +0000 | [diff] [blame] | 39 | if (AS == BS) |
| 40 | return MustAlias; |
Dan Gohman | d926b98 | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 41 | |
| 42 | // If something is known about the difference between the two addresses, |
| 43 | // see if it's enough to prove a NoAlias. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 44 | if (SE.getEffectiveSCEVType(AS->getType()) == |
| 45 | SE.getEffectiveSCEVType(BS->getType())) { |
| 46 | unsigned BitWidth = SE.getTypeSizeInBits(AS->getType()); |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 47 | APInt ASizeInt(BitWidth, LocA.Size); |
| 48 | APInt BSizeInt(BitWidth, LocB.Size); |
Dan Gohman | 0865966 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 49 | |
| 50 | // Compute the difference between the two pointers. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 51 | const SCEV *BA = SE.getMinusSCEV(BS, AS); |
Dan Gohman | 0865966 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 52 | |
| 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 Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 56 | if (ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) && |
| 57 | (-BSizeInt).uge(SE.getUnsignedRange(BA).getUnsignedMax())) |
Dan Gohman | 0865966 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 58 | 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 Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 65 | const SCEV *AB = SE.getMinusSCEV(AS, BS); |
Dan Gohman | 0865966 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 66 | |
| 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 Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 70 | if (BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) && |
| 71 | (-ASizeInt).uge(SE.getUnsignedRange(AB).getUnsignedMax())) |
Dan Gohman | 0865966 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 72 | return NoAlias; |
Dan Gohman | d926b98 | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 73 | } |
| 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 Gohman | ac45c91 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 78 | Value *AO = GetBaseValue(AS); |
| 79 | Value *BO = GetBaseValue(BS); |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 80 | if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr)) |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 81 | if (alias(MemoryLocation(AO ? AO : LocA.Ptr, |
| 82 | AO ? +MemoryLocation::UnknownSize : LocA.Size, |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 83 | AO ? AAMDNodes() : LocA.AATags), |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 84 | MemoryLocation(BO ? BO : LocB.Ptr, |
| 85 | BO ? +MemoryLocation::UnknownSize : LocB.Size, |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 86 | BO ? AAMDNodes() : LocB.AATags)) == NoAlias) |
Dan Gohman | d926b98 | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 87 | return NoAlias; |
| 88 | |
| 89 | // Forward the query to the next analysis. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 90 | 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. |
| 96 | Value *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 | |
| 114 | SCEVAAResult SCEVAA::run(Function &F, AnalysisManager<Function> *AM) { |
| 115 | return SCEVAAResult(AM->getResult<TargetLibraryAnalysis>(F), |
| 116 | AM->getResult<ScalarEvolutionAnalysis>(F)); |
| 117 | } |
| 118 | |
| 119 | char SCEVAA::PassID; |
| 120 | |
| 121 | char SCEVAAWrapperPass::ID = 0; |
| 122 | INITIALIZE_PASS_BEGIN(SCEVAAWrapperPass, "scev-aa", |
| 123 | "ScalarEvolution-based Alias Analysis", false, true) |
| 124 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
| 125 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 126 | INITIALIZE_PASS_END(SCEVAAWrapperPass, "scev-aa", |
| 127 | "ScalarEvolution-based Alias Analysis", false, true) |
| 128 | |
| 129 | FunctionPass *llvm::createSCEVAAWrapperPass() { |
| 130 | return new SCEVAAWrapperPass(); |
| 131 | } |
| 132 | |
| 133 | SCEVAAWrapperPass::SCEVAAWrapperPass() : FunctionPass(ID) { |
| 134 | initializeSCEVAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 135 | } |
| 136 | |
| 137 | bool SCEVAAWrapperPass::runOnFunction(Function &F) { |
| 138 | Result.reset( |
| 139 | new SCEVAAResult(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(), |
| 140 | getAnalysis<ScalarEvolutionWrapperPass>().getSE())); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | void SCEVAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 145 | AU.setPreservesAll(); |
| 146 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
| 147 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Dan Gohman | d926b98 | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 148 | } |