Nick Lewycky | 7ed1dbf | 2013-06-10 23:10:59 +0000 | [diff] [blame] | 1 | //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation -------------===// |
Owen Anderson | c0daf5f | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 |
Owen Anderson | c0daf5f | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements an analysis that determines, for a given memory |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 10 | // operation, what preceding memory operations it depends on. It builds on |
Owen Anderson | fa78835 | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 11 | // alias analysis information, and tries to provide a lazy, caching interface to |
Owen Anderson | c0daf5f | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 12 | // a common kind of alias information query. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Owen Anderson | c0daf5f | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/AliasAnalysis.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AssumptionCache.h" |
Victor Hernandez | f390e04 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/MemoryBuiltins.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/MemoryLocation.h" |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/OrderedBasicBlock.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/PHITransAddr.h" |
John Brawn | cd5f37f | 2018-07-31 14:19:29 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/PhiValues.h" |
Chandler Carruth | d06034d | 2015-08-12 17:47:44 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Attributes.h" |
| 32 | #include "llvm/IR/BasicBlock.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 35 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Function.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 38 | #include "llvm/IR/InstrTypes.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 39 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 40 | #include "llvm/IR/Instructions.h" |
| 41 | #include "llvm/IR/IntrinsicInst.h" |
| 42 | #include "llvm/IR/LLVMContext.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Metadata.h" |
| 44 | #include "llvm/IR/Module.h" |
Chandler Carruth | aa0ab63 | 2014-03-04 12:09:19 +0000 | [diff] [blame] | 45 | #include "llvm/IR/PredIteratorCache.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Type.h" |
| 47 | #include "llvm/IR/Use.h" |
| 48 | #include "llvm/IR/User.h" |
| 49 | #include "llvm/IR/Value.h" |
| 50 | #include "llvm/Pass.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 51 | #include "llvm/Support/AtomicOrdering.h" |
| 52 | #include "llvm/Support/Casting.h" |
| 53 | #include "llvm/Support/CommandLine.h" |
| 54 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 56 | #include "llvm/Support/MathExtras.h" |
| 57 | #include <algorithm> |
| 58 | #include <cassert> |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 59 | #include <cstdint> |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 60 | #include <iterator> |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 61 | #include <utility> |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 62 | |
Owen Anderson | c0daf5f | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 63 | using namespace llvm; |
| 64 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 65 | #define DEBUG_TYPE "memdep" |
| 66 | |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 67 | STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses"); |
| 68 | STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses"); |
Chris Lattner | e7d7e13 | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 69 | STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses"); |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 70 | |
| 71 | STATISTIC(NumCacheNonLocalPtr, |
| 72 | "Number of fully cached non-local ptr responses"); |
| 73 | STATISTIC(NumCacheDirtyNonLocalPtr, |
| 74 | "Number of cached, but dirty, non-local ptr responses"); |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 75 | STATISTIC(NumUncacheNonLocalPtr, "Number of uncached non-local ptr responses"); |
Chris Lattner | 5ed409e | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 76 | STATISTIC(NumCacheCompleteNonLocalPtr, |
| 77 | "Number of block queries that were completely cached"); |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 78 | |
Eli Friedman | 8b098b0 | 2011-06-15 23:59:25 +0000 | [diff] [blame] | 79 | // Limit for the number of instructions to scan in a block. |
Jingyue Wu | d058ea9 | 2015-07-21 21:50:39 +0000 | [diff] [blame] | 80 | |
| 81 | static cl::opt<unsigned> BlockScanLimit( |
| 82 | "memdep-block-scan-limit", cl::Hidden, cl::init(100), |
| 83 | cl::desc("The number of instructions to scan in a block in memory " |
| 84 | "dependency analysis (default = 100)")); |
Eli Friedman | 8b098b0 | 2011-06-15 23:59:25 +0000 | [diff] [blame] | 85 | |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 86 | static cl::opt<unsigned> |
| 87 | BlockNumberLimit("memdep-block-number-limit", cl::Hidden, cl::init(1000), |
| 88 | cl::desc("The number of blocks to scan during memory " |
| 89 | "dependency analysis (default = 1000)")); |
Joerg Sonnenberger | 36894dc | 2016-02-20 11:24:44 +0000 | [diff] [blame] | 90 | |
Bruno Cardoso Lopes | e3c513a | 2014-10-01 20:07:13 +0000 | [diff] [blame] | 91 | // Limit on the number of memdep results to process. |
Aaron Ballman | 254dd7e | 2014-10-02 13:17:11 +0000 | [diff] [blame] | 92 | static const unsigned int NumResultsLimit = 100; |
Bruno Cardoso Lopes | e3c513a | 2014-10-01 20:07:13 +0000 | [diff] [blame] | 93 | |
Chandler Carruth | 40e21f2 | 2016-03-07 12:30:06 +0000 | [diff] [blame] | 94 | /// This is a helper function that removes Val from 'Inst's set in ReverseMap. |
| 95 | /// |
| 96 | /// If the set becomes empty, remove Inst's entry. |
Chris Lattner | de4440c | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 97 | template <typename KeyTy> |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 98 | static void |
| 99 | RemoveFromReverseMap(DenseMap<Instruction *, SmallPtrSet<KeyTy, 4>> &ReverseMap, |
| 100 | Instruction *Inst, KeyTy Val) { |
| 101 | typename DenseMap<Instruction *, SmallPtrSet<KeyTy, 4>>::iterator InstIt = |
| 102 | ReverseMap.find(Inst); |
Chris Lattner | de4440c | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 103 | assert(InstIt != ReverseMap.end() && "Reverse map out of sync?"); |
| 104 | bool Found = InstIt->second.erase(Val); |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 105 | assert(Found && "Invalid reverse map!"); |
| 106 | (void)Found; |
Chris Lattner | de4440c | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 107 | if (InstIt->second.empty()) |
| 108 | ReverseMap.erase(InstIt); |
| 109 | } |
| 110 | |
Chandler Carruth | 40e21f2 | 2016-03-07 12:30:06 +0000 | [diff] [blame] | 111 | /// If the given instruction references a specific memory location, fill in Loc |
| 112 | /// with the details, otherwise set Loc.Ptr to null. |
| 113 | /// |
| 114 | /// Returns a ModRefInfo value describing the general behavior of the |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 115 | /// instruction. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 116 | static ModRefInfo GetLocation(const Instruction *Inst, MemoryLocation &Loc, |
Chandler Carruth | d06034d | 2015-08-12 17:47:44 +0000 | [diff] [blame] | 117 | const TargetLibraryInfo &TLI) { |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 118 | if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 119 | if (LI->isUnordered()) { |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 120 | Loc = MemoryLocation::get(LI); |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 121 | return ModRefInfo::Ref; |
Jakub Staszak | fa41def | 2013-03-20 23:53:45 +0000 | [diff] [blame] | 122 | } |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 123 | if (LI->getOrdering() == AtomicOrdering::Monotonic) { |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 124 | Loc = MemoryLocation::get(LI); |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 125 | return ModRefInfo::ModRef; |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 126 | } |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 127 | Loc = MemoryLocation(); |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 128 | return ModRefInfo::ModRef; |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 132 | if (SI->isUnordered()) { |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 133 | Loc = MemoryLocation::get(SI); |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 134 | return ModRefInfo::Mod; |
Jakub Staszak | fa41def | 2013-03-20 23:53:45 +0000 | [diff] [blame] | 135 | } |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 136 | if (SI->getOrdering() == AtomicOrdering::Monotonic) { |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 137 | Loc = MemoryLocation::get(SI); |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 138 | return ModRefInfo::ModRef; |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 139 | } |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 140 | Loc = MemoryLocation(); |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 141 | return ModRefInfo::ModRef; |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (const VAArgInst *V = dyn_cast<VAArgInst>(Inst)) { |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 145 | Loc = MemoryLocation::get(V); |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 146 | return ModRefInfo::ModRef; |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chandler Carruth | d06034d | 2015-08-12 17:47:44 +0000 | [diff] [blame] | 149 | if (const CallInst *CI = isFreeCall(Inst, &TLI)) { |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 150 | // calls to free() deallocate the entire structure |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 151 | Loc = MemoryLocation(CI->getArgOperand(0)); |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 152 | return ModRefInfo::Mod; |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 155 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 156 | switch (II->getIntrinsicID()) { |
| 157 | case Intrinsic::lifetime_start: |
| 158 | case Intrinsic::lifetime_end: |
| 159 | case Intrinsic::invariant_start: |
Philip Reames | f5ff5d5 | 2018-01-17 19:57:19 +0000 | [diff] [blame] | 160 | Loc = MemoryLocation::getForArgument(II, 1, TLI); |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 161 | // These intrinsics don't really modify the memory, but returning Mod |
| 162 | // will allow them to be handled conservatively. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 163 | return ModRefInfo::Mod; |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 164 | case Intrinsic::invariant_end: |
Philip Reames | f5ff5d5 | 2018-01-17 19:57:19 +0000 | [diff] [blame] | 165 | Loc = MemoryLocation::getForArgument(II, 2, TLI); |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 166 | // These intrinsics don't really modify the memory, but returning Mod |
| 167 | // will allow them to be handled conservatively. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 168 | return ModRefInfo::Mod; |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 169 | default: |
| 170 | break; |
| 171 | } |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 172 | } |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 173 | |
| 174 | // Otherwise, just do the coarse-grained thing that always works. |
| 175 | if (Inst->mayWriteToMemory()) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 176 | return ModRefInfo::ModRef; |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 177 | if (Inst->mayReadFromMemory()) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 178 | return ModRefInfo::Ref; |
| 179 | return ModRefInfo::NoModRef; |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 180 | } |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 181 | |
Chandler Carruth | 40e21f2 | 2016-03-07 12:30:06 +0000 | [diff] [blame] | 182 | /// Private helper for finding the local dependencies of a call site. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 183 | MemDepResult MemoryDependenceResults::getCallDependencyFrom( |
| 184 | CallBase *Call, bool isReadOnlyCall, BasicBlock::iterator ScanIt, |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 185 | BasicBlock *BB) { |
Fedor Sergeev | 92e160a | 2019-08-10 01:23:38 +0000 | [diff] [blame] | 186 | unsigned Limit = getDefaultBlockScanLimit(); |
Eli Friedman | 8b098b0 | 2011-06-15 23:59:25 +0000 | [diff] [blame] | 187 | |
Henric Karlsson | 54a53bd | 2016-10-06 10:58:41 +0000 | [diff] [blame] | 188 | // Walk backwards through the block, looking for dependencies. |
Chris Lattner | 51ba8d0 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 189 | while (ScanIt != BB->begin()) { |
Mikael Holmen | 279790b | 2017-10-25 06:15:32 +0000 | [diff] [blame] | 190 | Instruction *Inst = &*--ScanIt; |
| 191 | // Debug intrinsics don't cause dependences and should not affect Limit |
| 192 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 193 | continue; |
| 194 | |
Eli Friedman | 8b098b0 | 2011-06-15 23:59:25 +0000 | [diff] [blame] | 195 | // Limit the amount of scanning we do so we don't end up with quadratic |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 196 | // running time on extreme testcases. |
Eli Friedman | 8b098b0 | 2011-06-15 23:59:25 +0000 | [diff] [blame] | 197 | --Limit; |
| 198 | if (!Limit) |
| 199 | return MemDepResult::getUnknown(); |
| 200 | |
Owen Anderson | 9c88457 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 201 | // If this inst is a memory op, get the pointer it accessed |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 202 | MemoryLocation Loc; |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 203 | ModRefInfo MR = GetLocation(Inst, Loc, TLI); |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 204 | if (Loc.Ptr) { |
| 205 | // A simple instruction. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 206 | if (isModOrRefSet(AA.getModRefInfo(Call, Loc))) |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 207 | return MemDepResult::getClobber(Inst); |
| 208 | continue; |
| 209 | } |
| 210 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 211 | if (auto *CallB = dyn_cast<CallBase>(Inst)) { |
Chris Lattner | 0e3d633 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 212 | // If these two calls do not interfere, look past it. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 213 | if (isNoModRef(AA.getModRefInfo(Call, CallB))) { |
| 214 | // If the two calls are the same, return Inst as a Def, so that |
| 215 | // Call can be found redundant and eliminated. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 216 | if (isReadOnlyCall && !isModSet(MR) && |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 217 | Call->isIdenticalToWhenDefined(CallB)) |
Dan Gohman | 26ef7c7 | 2010-08-05 22:09:15 +0000 | [diff] [blame] | 218 | return MemDepResult::getDef(Inst); |
| 219 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 220 | // Otherwise if the two calls don't interact (e.g. CallB is readnone) |
Dan Gohman | 26ef7c7 | 2010-08-05 22:09:15 +0000 | [diff] [blame] | 221 | // keep scanning. |
Nadav Rotem | 5d4e205 | 2012-08-13 23:03:43 +0000 | [diff] [blame] | 222 | continue; |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 223 | } else |
Chris Lattner | 0e3d633 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 224 | return MemDepResult::getClobber(Inst); |
Chris Lattner | ff862c4 | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 225 | } |
Nadav Rotem | 5d4e205 | 2012-08-13 23:03:43 +0000 | [diff] [blame] | 226 | |
| 227 | // If we could not obtain a pointer for the instruction and the instruction |
| 228 | // touches memory then assume that this is a dependency. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 229 | if (isModOrRefSet(MR)) |
Nadav Rotem | 5d4e205 | 2012-08-13 23:03:43 +0000 | [diff] [blame] | 230 | return MemDepResult::getClobber(Inst); |
Owen Anderson | 9c88457 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 231 | } |
Nadav Rotem | 5d4e205 | 2012-08-13 23:03:43 +0000 | [diff] [blame] | 232 | |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 233 | // No dependence found. If this is the entry block of the function, it is |
| 234 | // unknown, otherwise it is non-local. |
Chris Lattner | 2faa2c7 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 235 | if (BB != &BB->getParent()->getEntryBlock()) |
| 236 | return MemDepResult::getNonLocal(); |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 237 | return MemDepResult::getNonFuncLocal(); |
Owen Anderson | 9c88457 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 240 | unsigned MemoryDependenceResults::getLoadLoadClobberFullWidthSize( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 241 | const Value *MemLocBase, int64_t MemLocOffs, unsigned MemLocSize, |
| 242 | const LoadInst *LI) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 243 | // We can only extend simple integer loads. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 244 | if (!isa<IntegerType>(LI->getType()) || !LI->isSimple()) |
| 245 | return 0; |
Kostya Serebryany | 3838f27 | 2013-02-13 05:59:45 +0000 | [diff] [blame] | 246 | |
| 247 | // Load widening is hostile to ThreadSanitizer: it may cause false positives |
| 248 | // or make the reports more cryptic (access sizes are wrong). |
Duncan P. N. Exon Smith | b3fc83c | 2015-02-14 00:12:15 +0000 | [diff] [blame] | 249 | if (LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread)) |
Kostya Serebryany | 3838f27 | 2013-02-13 05:59:45 +0000 | [diff] [blame] | 250 | return 0; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 251 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 252 | const DataLayout &DL = LI->getModule()->getDataLayout(); |
| 253 | |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 254 | // Get the base of this load. |
| 255 | int64_t LIOffs = 0; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 256 | const Value *LIBase = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 257 | GetPointerBaseWithConstantOffset(LI->getPointerOperand(), LIOffs, DL); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 259 | // If the two pointers are not based on the same pointer, we can't tell that |
| 260 | // they are related. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 261 | if (LIBase != MemLocBase) |
| 262 | return 0; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 264 | // Okay, the two values are based on the same pointer, but returned as |
| 265 | // no-alias. This happens when we have things like two byte loads at "P+1" |
| 266 | // and "P+3". Check to see if increasing the size of the "LI" load up to its |
| 267 | // alignment (or the largest native integer type) will allow us to load all |
| 268 | // the bits required by MemLoc. |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 270 | // If MemLoc is before LI, then no widening of LI will help us out. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 271 | if (MemLocOffs < LIOffs) |
| 272 | return 0; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 274 | // Get the alignment of the load in bytes. We assume that it is safe to load |
| 275 | // any legal integer up to this size without a problem. For example, if we're |
| 276 | // looking at an i8 load on x86-32 that is known 1024 byte aligned, we can |
| 277 | // widen it up to an i32 load. If it is known 2-byte aligned, we can widen it |
| 278 | // to i16. |
| 279 | unsigned LoadAlign = LI->getAlignment(); |
| 280 | |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 281 | int64_t MemLocEnd = MemLocOffs + MemLocSize; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 283 | // If no amount of rounding up will let MemLoc fit into LI, then bail out. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 284 | if (LIOffs + LoadAlign < MemLocEnd) |
| 285 | return 0; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 287 | // This is the size of the load to try. Start with the next larger power of |
| 288 | // two. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 289 | unsigned NewLoadByteSize = LI->getType()->getPrimitiveSizeInBits() / 8U; |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 290 | NewLoadByteSize = NextPowerOf2(NewLoadByteSize); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 291 | |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 292 | while (true) { |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 293 | // If this load size is bigger than our known alignment or would not fit |
| 294 | // into a native integer register, then we fail. |
| 295 | if (NewLoadByteSize > LoadAlign || |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 296 | !DL.fitsInLegalInteger(NewLoadByteSize * 8)) |
Chris Lattner | 827a270 | 2011-04-28 07:29:08 +0000 | [diff] [blame] | 297 | return 0; |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 298 | |
Duncan P. N. Exon Smith | b3fc83c | 2015-02-14 00:12:15 +0000 | [diff] [blame] | 299 | if (LIOffs + NewLoadByteSize > MemLocEnd && |
Evgeniy Stepanov | c667c1f | 2017-12-09 00:21:41 +0000 | [diff] [blame] | 300 | (LI->getParent()->getParent()->hasFnAttribute( |
| 301 | Attribute::SanitizeAddress) || |
| 302 | LI->getParent()->getParent()->hasFnAttribute( |
| 303 | Attribute::SanitizeHWAddress))) |
Kostya Serebryany | 9e0d377 | 2012-02-06 22:48:56 +0000 | [diff] [blame] | 304 | // We will be reading past the location accessed by the original program. |
| 305 | // While this is safe in a regular build, Address Safety analysis tools |
| 306 | // may start reporting false warnings. So, don't do widening. |
| 307 | return 0; |
Kostya Serebryany | 9e0d377 | 2012-02-06 22:48:56 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 309 | // If a load of this width would include all of MemLoc, then we succeed. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 310 | if (LIOffs + NewLoadByteSize >= MemLocEnd) |
Chris Lattner | 827a270 | 2011-04-28 07:29:08 +0000 | [diff] [blame] | 311 | return NewLoadByteSize; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 313 | NewLoadByteSize <<= 1; |
| 314 | } |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Philip Reames | a7ad6a5 | 2015-01-26 18:54:27 +0000 | [diff] [blame] | 317 | static bool isVolatile(Instruction *Inst) { |
Davide Italiano | f15fb36 | 2017-06-25 22:12:59 +0000 | [diff] [blame] | 318 | if (auto *LI = dyn_cast<LoadInst>(Inst)) |
Philip Reames | a7ad6a5 | 2015-01-26 18:54:27 +0000 | [diff] [blame] | 319 | return LI->isVolatile(); |
Davide Italiano | f15fb36 | 2017-06-25 22:12:59 +0000 | [diff] [blame] | 320 | if (auto *SI = dyn_cast<StoreInst>(Inst)) |
Philip Reames | a7ad6a5 | 2015-01-26 18:54:27 +0000 | [diff] [blame] | 321 | return SI->isVolatile(); |
Davide Italiano | f15fb36 | 2017-06-25 22:12:59 +0000 | [diff] [blame] | 322 | if (auto *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) |
Philip Reames | a7ad6a5 | 2015-01-26 18:54:27 +0000 | [diff] [blame] | 323 | return AI->isVolatile(); |
| 324 | return false; |
| 325 | } |
| 326 | |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 327 | MemDepResult MemoryDependenceResults::getPointerDependencyFrom( |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 328 | const MemoryLocation &MemLoc, bool isLoad, BasicBlock::iterator ScanIt, |
Florian Hahn | 6c30243 | 2019-03-28 19:17:31 +0000 | [diff] [blame] | 329 | BasicBlock *BB, Instruction *QueryInst, unsigned *Limit, |
| 330 | OrderedBasicBlock *OBB) { |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 331 | MemDepResult InvariantGroupDependency = MemDepResult::getUnknown(); |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 332 | if (QueryInst != nullptr) { |
| 333 | if (auto *LI = dyn_cast<LoadInst>(QueryInst)) { |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 334 | InvariantGroupDependency = getInvariantGroupPointerDependency(LI, BB); |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 335 | |
Piotr Padlewski | e41beed | 2017-01-11 16:23:54 +0000 | [diff] [blame] | 336 | if (InvariantGroupDependency.isDef()) |
| 337 | return InvariantGroupDependency; |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 340 | MemDepResult SimpleDep = getSimplePointerDependencyFrom( |
Florian Hahn | 6c30243 | 2019-03-28 19:17:31 +0000 | [diff] [blame] | 341 | MemLoc, isLoad, ScanIt, BB, QueryInst, Limit, OBB); |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 342 | if (SimpleDep.isDef()) |
| 343 | return SimpleDep; |
| 344 | // Non-local invariant group dependency indicates there is non local Def |
| 345 | // (it only returns nonLocal if it finds nonLocal def), which is better than |
| 346 | // local clobber and everything else. |
| 347 | if (InvariantGroupDependency.isNonLocal()) |
| 348 | return InvariantGroupDependency; |
| 349 | |
| 350 | assert(InvariantGroupDependency.isUnknown() && |
| 351 | "InvariantGroupDependency should be only unknown at this point"); |
| 352 | return SimpleDep; |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | MemDepResult |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 356 | MemoryDependenceResults::getInvariantGroupPointerDependency(LoadInst *LI, |
Piotr Padlewski | 01659cb | 2016-11-08 18:20:51 +0000 | [diff] [blame] | 357 | BasicBlock *BB) { |
Piotr Padlewski | ce35826 | 2018-05-18 23:53:46 +0000 | [diff] [blame] | 358 | |
Philip Reames | 27820f9 | 2019-09-04 17:28:48 +0000 | [diff] [blame] | 359 | if (!LI->hasMetadata(LLVMContext::MD_invariant_group)) |
Piotr Padlewski | da36215 | 2016-12-30 18:45:07 +0000 | [diff] [blame] | 360 | return MemDepResult::getUnknown(); |
| 361 | |
Piotr Padlewski | 09ad678 | 2017-01-08 22:26:06 +0000 | [diff] [blame] | 362 | // Take the ptr operand after all casts and geps 0. This way we can search |
| 363 | // cast graph down only. |
| 364 | Value *LoadOperand = LI->getPointerOperand()->stripPointerCasts(); |
| 365 | |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 366 | // It's is not safe to walk the use list of global value, because function |
| 367 | // passes aren't allowed to look outside their functions. |
Piotr Padlewski | 09ad678 | 2017-01-08 22:26:06 +0000 | [diff] [blame] | 368 | // FIXME: this could be fixed by filtering instructions from outside |
| 369 | // of current function. |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 370 | if (isa<GlobalValue>(LoadOperand)) |
| 371 | return MemDepResult::getUnknown(); |
| 372 | |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 373 | // Queue to process all pointers that are equivalent to load operand. |
Piotr Padlewski | da36215 | 2016-12-30 18:45:07 +0000 | [diff] [blame] | 374 | SmallVector<const Value *, 8> LoadOperandsQueue; |
Piotr Padlewski | 09ad678 | 2017-01-08 22:26:06 +0000 | [diff] [blame] | 375 | LoadOperandsQueue.push_back(LoadOperand); |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 376 | |
| 377 | Instruction *ClosestDependency = nullptr; |
| 378 | // Order of instructions in uses list is unpredictible. In order to always |
| 379 | // get the same result, we will look for the closest dominance. |
| 380 | auto GetClosestDependency = [this](Instruction *Best, Instruction *Other) { |
| 381 | assert(Other && "Must call it with not null instruction"); |
| 382 | if (Best == nullptr || DT.dominates(Best, Other)) |
| 383 | return Other; |
| 384 | return Best; |
| 385 | }; |
| 386 | |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 387 | // FIXME: This loop is O(N^2) because dominates can be O(n) and in worst case |
| 388 | // we will see all the instructions. This should be fixed in MSSA. |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 389 | while (!LoadOperandsQueue.empty()) { |
Piotr Padlewski | da36215 | 2016-12-30 18:45:07 +0000 | [diff] [blame] | 390 | const Value *Ptr = LoadOperandsQueue.pop_back_val(); |
Piotr Padlewski | 09ad678 | 2017-01-08 22:26:06 +0000 | [diff] [blame] | 391 | assert(Ptr && !isa<GlobalValue>(Ptr) && |
| 392 | "Null or GlobalValue should not be inserted"); |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 393 | |
Piotr Padlewski | da36215 | 2016-12-30 18:45:07 +0000 | [diff] [blame] | 394 | for (const Use &Us : Ptr->uses()) { |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 395 | auto *U = dyn_cast<Instruction>(Us.getUser()); |
Chandler Carruth | aef32bd | 2016-03-11 13:46:00 +0000 | [diff] [blame] | 396 | if (!U || U == LI || !DT.dominates(U, LI)) |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 397 | continue; |
| 398 | |
Piotr Padlewski | da36215 | 2016-12-30 18:45:07 +0000 | [diff] [blame] | 399 | // Bitcast or gep with zeros are using Ptr. Add to queue to check it's |
| 400 | // users. U = bitcast Ptr |
| 401 | if (isa<BitCastInst>(U)) { |
Piotr Padlewski | 09ad678 | 2017-01-08 22:26:06 +0000 | [diff] [blame] | 402 | LoadOperandsQueue.push_back(U); |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 403 | continue; |
| 404 | } |
Piotr Padlewski | 09ad678 | 2017-01-08 22:26:06 +0000 | [diff] [blame] | 405 | // Gep with zeros is equivalent to bitcast. |
| 406 | // FIXME: we are not sure if some bitcast should be canonicalized to gep 0 |
| 407 | // or gep 0 to bitcast because of SROA, so there are 2 forms. When |
| 408 | // typeless pointers will be ready then both cases will be gone |
| 409 | // (and this BFS also won't be needed). |
Piotr Padlewski | da36215 | 2016-12-30 18:45:07 +0000 | [diff] [blame] | 410 | if (auto *GEP = dyn_cast<GetElementPtrInst>(U)) |
| 411 | if (GEP->hasAllZeroIndices()) { |
Piotr Padlewski | 09ad678 | 2017-01-08 22:26:06 +0000 | [diff] [blame] | 412 | LoadOperandsQueue.push_back(U); |
Piotr Padlewski | da36215 | 2016-12-30 18:45:07 +0000 | [diff] [blame] | 413 | continue; |
| 414 | } |
| 415 | |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 416 | // If we hit load/store with the same invariant.group metadata (and the |
| 417 | // same pointer operand) we can assume that value pointed by pointer |
| 418 | // operand didn't change. |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 419 | if ((isa<LoadInst>(U) || isa<StoreInst>(U)) && |
Philip Reames | 27820f9 | 2019-09-04 17:28:48 +0000 | [diff] [blame] | 420 | U->hasMetadata(LLVMContext::MD_invariant_group)) |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 421 | ClosestDependency = GetClosestDependency(ClosestDependency, U); |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 424 | |
| 425 | if (!ClosestDependency) |
| 426 | return MemDepResult::getUnknown(); |
| 427 | if (ClosestDependency->getParent() == BB) |
| 428 | return MemDepResult::getDef(ClosestDependency); |
| 429 | // Def(U) can't be returned here because it is non-local. If local |
| 430 | // dependency won't be found then return nonLocal counting that the |
| 431 | // user will call getNonLocalPointerDependency, which will return cached |
| 432 | // result. |
| 433 | NonLocalDefsCache.try_emplace( |
| 434 | LI, NonLocalDepResult(ClosestDependency->getParent(), |
| 435 | MemDepResult::getDef(ClosestDependency), nullptr)); |
Piotr Padlewski | 153fe60 | 2018-05-18 22:40:34 +0000 | [diff] [blame] | 436 | ReverseNonLocalDefsCache[ClosestDependency].insert(LI); |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 437 | return MemDepResult::getNonLocal(); |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 440 | MemDepResult MemoryDependenceResults::getSimplePointerDependencyFrom( |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 441 | const MemoryLocation &MemLoc, bool isLoad, BasicBlock::iterator ScanIt, |
Florian Hahn | 6c30243 | 2019-03-28 19:17:31 +0000 | [diff] [blame] | 442 | BasicBlock *BB, Instruction *QueryInst, unsigned *Limit, |
| 443 | OrderedBasicBlock *OBB) { |
Shuxin Yang | 408bdad | 2013-03-06 17:48:48 +0000 | [diff] [blame] | 444 | bool isInvariantLoad = false; |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 445 | |
Fedor Sergeev | 92e160a | 2019-08-10 01:23:38 +0000 | [diff] [blame] | 446 | unsigned DefaultLimit = getDefaultBlockScanLimit(); |
Florian Hahn | 6c30243 | 2019-03-28 19:17:31 +0000 | [diff] [blame] | 447 | if (!Limit) |
| 448 | Limit = &DefaultLimit; |
Bob Haarman | 3db1764 | 2016-08-26 16:34:27 +0000 | [diff] [blame] | 449 | |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 450 | // We must be careful with atomic accesses, as they may allow another thread |
Chad Rosier | 02e831c | 2016-06-28 17:19:10 +0000 | [diff] [blame] | 451 | // to touch this location, clobbering it. We are conservative: if the |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 452 | // QueryInst is not a simple (non-atomic) memory access, we automatically |
| 453 | // return getClobber. |
| 454 | // If it is simple, we know based on the results of |
| 455 | // "Compiler testing via a theory of sound optimisations in the C11/C++11 |
| 456 | // memory model" in PLDI 2013, that a non-atomic location can only be |
| 457 | // clobbered between a pair of a release and an acquire action, with no |
| 458 | // access to the location in between. |
| 459 | // Here is an example for giving the general intuition behind this rule. |
| 460 | // In the following code: |
| 461 | // store x 0; |
| 462 | // release action; [1] |
| 463 | // acquire action; [4] |
| 464 | // %val = load x; |
| 465 | // It is unsafe to replace %val by 0 because another thread may be running: |
| 466 | // acquire action; [2] |
| 467 | // store x 42; |
| 468 | // release action; [3] |
| 469 | // with synchronization from 1 to 2 and from 3 to 4, resulting in %val |
| 470 | // being 42. A key property of this program however is that if either |
| 471 | // 1 or 4 were missing, there would be a race between the store of 42 |
Chad Rosier | 02e831c | 2016-06-28 17:19:10 +0000 | [diff] [blame] | 472 | // either the store of 0 or the load (making the whole program racy). |
Nick Lewycky | 947ca8a | 2016-01-04 16:44:44 +0000 | [diff] [blame] | 473 | // The paper mentioned above shows that the same property is respected |
Chad Rosier | 02e831c | 2016-06-28 17:19:10 +0000 | [diff] [blame] | 474 | // by every program that can detect any optimization of that kind: either |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 475 | // it is racy (undefined) or there is a release followed by an acquire |
| 476 | // between the pair of accesses under consideration. |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 477 | |
Philip Reames | 4dbd88f | 2015-03-24 23:54:54 +0000 | [diff] [blame] | 478 | // If the load is invariant, we "know" that it doesn't alias *any* write. We |
| 479 | // do want to respect mustalias results since defs are useful for value |
| 480 | // forwarding, but any mayalias write can be assumed to be noalias. |
| 481 | // Arguably, this logic should be pushed inside AliasAnalysis itself. |
Shuxin Yang | 408bdad | 2013-03-06 17:48:48 +0000 | [diff] [blame] | 482 | if (isLoad && QueryInst) { |
| 483 | LoadInst *LI = dyn_cast<LoadInst>(QueryInst); |
Philip Reames | 27820f9 | 2019-09-04 17:28:48 +0000 | [diff] [blame] | 484 | if (LI && LI->hasMetadata(LLVMContext::MD_invariant_load)) |
Shuxin Yang | 408bdad | 2013-03-06 17:48:48 +0000 | [diff] [blame] | 485 | isInvariantLoad = true; |
| 486 | } |
Eli Friedman | 8b098b0 | 2011-06-15 23:59:25 +0000 | [diff] [blame] | 487 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 488 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
| 489 | |
Florian Hahn | 6c30243 | 2019-03-28 19:17:31 +0000 | [diff] [blame] | 490 | // If the caller did not provide an ordered basic block, |
| 491 | // create one to lazily compute and cache instruction |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 492 | // positions inside a BB. This is used to provide fast queries for relative |
| 493 | // position between two instructions in a BB and can be used by |
| 494 | // AliasAnalysis::callCapturesBefore. |
Florian Hahn | 6c30243 | 2019-03-28 19:17:31 +0000 | [diff] [blame] | 495 | OrderedBasicBlock OBBTmp(BB); |
| 496 | if (!OBB) |
| 497 | OBB = &OBBTmp; |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 498 | |
Krzysztof Parzyszek | e261e5a | 2016-02-22 23:07:43 +0000 | [diff] [blame] | 499 | // Return "true" if and only if the instruction I is either a non-simple |
| 500 | // load or a non-simple store. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 501 | auto isNonSimpleLoadOrStore = [](Instruction *I) -> bool { |
Krzysztof Parzyszek | e261e5a | 2016-02-22 23:07:43 +0000 | [diff] [blame] | 502 | if (auto *LI = dyn_cast<LoadInst>(I)) |
| 503 | return !LI->isSimple(); |
| 504 | if (auto *SI = dyn_cast<StoreInst>(I)) |
| 505 | return !SI->isSimple(); |
| 506 | return false; |
| 507 | }; |
| 508 | |
| 509 | // Return "true" if I is not a load and not a store, but it does access |
| 510 | // memory. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 511 | auto isOtherMemAccess = [](Instruction *I) -> bool { |
Krzysztof Parzyszek | e261e5a | 2016-02-22 23:07:43 +0000 | [diff] [blame] | 512 | return !isa<LoadInst>(I) && !isa<StoreInst>(I) && I->mayReadOrWriteMemory(); |
| 513 | }; |
| 514 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 515 | // Walk backwards through the basic block, looking for dependencies. |
Philip Reames | 090a824 | 2015-02-15 19:07:31 +0000 | [diff] [blame] | 516 | while (ScanIt != BB->begin()) { |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 517 | Instruction *Inst = &*--ScanIt; |
Yunzhong Gao | 5cbcf56 | 2013-11-14 01:10:52 +0000 | [diff] [blame] | 518 | |
| 519 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) |
| 520 | // Debug intrinsics don't (and can't) cause dependencies. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 521 | if (isa<DbgInfoIntrinsic>(II)) |
| 522 | continue; |
Yunzhong Gao | 5cbcf56 | 2013-11-14 01:10:52 +0000 | [diff] [blame] | 523 | |
Eli Friedman | 8b098b0 | 2011-06-15 23:59:25 +0000 | [diff] [blame] | 524 | // Limit the amount of scanning we do so we don't end up with quadratic |
| 525 | // running time on extreme testcases. |
Bob Haarman | 3db1764 | 2016-08-26 16:34:27 +0000 | [diff] [blame] | 526 | --*Limit; |
| 527 | if (!*Limit) |
Eli Friedman | 8b098b0 | 2011-06-15 23:59:25 +0000 | [diff] [blame] | 528 | return MemDepResult::getUnknown(); |
| 529 | |
Chris Lattner | 506b858 | 2009-12-01 21:15:15 +0000 | [diff] [blame] | 530 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
Owen Anderson | 2b2bd28 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 531 | // If we reach a lifetime begin or end marker, then the query ends here |
| 532 | // because the value is undefined. |
Chris Lattner | a58edd1 | 2010-09-06 03:58:04 +0000 | [diff] [blame] | 533 | if (II->getIntrinsicID() == Intrinsic::lifetime_start) { |
Owen Anderson | b9878ee | 2009-12-02 07:35:19 +0000 | [diff] [blame] | 534 | // FIXME: This only considers queries directly on the invariant-tagged |
| 535 | // pointer, not on query pointers that are indexed off of them. It'd |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 536 | // be nice to handle that at some point (the right approach is to use |
| 537 | // GetPointerBaseWithConstantOffset). |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 538 | if (AA.isMustAlias(MemoryLocation(II->getArgOperand(1)), MemLoc)) |
Owen Anderson | 2b2bd28 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 539 | return MemDepResult::getDef(II); |
Chris Lattner | a58edd1 | 2010-09-06 03:58:04 +0000 | [diff] [blame] | 540 | continue; |
Owen Anderson | d0e86d5 | 2009-10-28 06:18:42 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 544 | // Values depend on loads if the pointers are must aliased. This means |
| 545 | // that a load depends on another must aliased load from the same value. |
| 546 | // One exception is atomic loads: a value can depend on an atomic load that |
| 547 | // it does not alias with when this atomic load indicates that another |
| 548 | // thread may be accessing the location. |
Chris Lattner | 0e3d633 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 549 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Philip Reames | a7ad6a5 | 2015-01-26 18:54:27 +0000 | [diff] [blame] | 550 | // While volatile access cannot be eliminated, they do not have to clobber |
| 551 | // non-aliasing locations, as normal accesses, for example, can be safely |
| 552 | // reordered with volatile accesses. |
| 553 | if (LI->isVolatile()) { |
| 554 | if (!QueryInst) |
| 555 | // Original QueryInst *may* be volatile |
| 556 | return MemDepResult::getClobber(LI); |
| 557 | if (isVolatile(QueryInst)) |
| 558 | // Ordering required if QueryInst is itself volatile |
| 559 | return MemDepResult::getClobber(LI); |
| 560 | // Otherwise, volatile doesn't imply any special ordering |
| 561 | } |
Krzysztof Parzyszek | e261e5a | 2016-02-22 23:07:43 +0000 | [diff] [blame] | 562 | |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 563 | // Atomic loads have complications involved. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 564 | // A Monotonic (or higher) load is OK if the query inst is itself not |
| 565 | // atomic. |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 566 | // FIXME: This is overly conservative. |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 567 | if (LI->isAtomic() && isStrongerThanUnordered(LI->getOrdering())) { |
Krzysztof Parzyszek | e261e5a | 2016-02-22 23:07:43 +0000 | [diff] [blame] | 568 | if (!QueryInst || isNonSimpleLoadOrStore(QueryInst) || |
| 569 | isOtherMemAccess(QueryInst)) |
Robin Morisset | 9e98e7f | 2014-08-18 22:18:14 +0000 | [diff] [blame] | 570 | return MemDepResult::getClobber(LI); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 571 | if (LI->getOrdering() != AtomicOrdering::Monotonic) |
David Majnemer | e165502 | 2015-03-21 06:19:17 +0000 | [diff] [blame] | 572 | return MemDepResult::getClobber(LI); |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 573 | } |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 574 | |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 575 | MemoryLocation LoadLoc = MemoryLocation::get(LI); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 0e3d633 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 577 | // If we found a pointer, check if it could be the same as our pointer. |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 578 | AliasResult R = AA.alias(LoadLoc, MemLoc); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 579 | |
Chris Lattner | 6f83d06 | 2011-04-26 01:21:15 +0000 | [diff] [blame] | 580 | if (isLoad) { |
Dehao Chen | 22ce5eb | 2016-09-09 18:42:35 +0000 | [diff] [blame] | 581 | if (R == NoAlias) |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 582 | continue; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 6f83d06 | 2011-04-26 01:21:15 +0000 | [diff] [blame] | 584 | // Must aliased loads are defs of each other. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 585 | if (R == MustAlias) |
Chris Lattner | 6f83d06 | 2011-04-26 01:21:15 +0000 | [diff] [blame] | 586 | return MemDepResult::getDef(Inst); |
| 587 | |
Dan Gohman | a471751 | 2011-06-04 06:48:50 +0000 | [diff] [blame] | 588 | #if 0 // FIXME: Temporarily disabled. GVN is cleverly rewriting loads |
| 589 | // in terms of clobbering loads, but since it does this by looking |
| 590 | // at the clobbering load directly, it doesn't know about any |
| 591 | // phi translation that may have happened along the way. |
| 592 | |
Chris Lattner | 6f83d06 | 2011-04-26 01:21:15 +0000 | [diff] [blame] | 593 | // If we have a partial alias, then return this as a clobber for the |
| 594 | // client to handle. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 595 | if (R == PartialAlias) |
Chris Lattner | 6f83d06 | 2011-04-26 01:21:15 +0000 | [diff] [blame] | 596 | return MemDepResult::getClobber(Inst); |
Dan Gohman | a471751 | 2011-06-04 06:48:50 +0000 | [diff] [blame] | 597 | #endif |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 598 | |
Chris Lattner | 6f83d06 | 2011-04-26 01:21:15 +0000 | [diff] [blame] | 599 | // Random may-alias loads don't depend on each other without a |
| 600 | // dependence. |
Chris Lattner | 80c0818 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 601 | continue; |
Chris Lattner | 6f83d06 | 2011-04-26 01:21:15 +0000 | [diff] [blame] | 602 | } |
Dan Gohman | 15a4396 | 2010-10-29 01:14:04 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 604 | // Stores don't depend on other no-aliased accesses. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 605 | if (R == NoAlias) |
Chris Lattner | 7aab279 | 2011-04-26 22:42:01 +0000 | [diff] [blame] | 606 | continue; |
| 607 | |
Dan Gohman | 15a4396 | 2010-10-29 01:14:04 +0000 | [diff] [blame] | 608 | // Stores don't alias loads from read-only memory. |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 609 | if (AA.pointsToConstantMemory(LoadLoc)) |
Dan Gohman | 15a4396 | 2010-10-29 01:14:04 +0000 | [diff] [blame] | 610 | continue; |
| 611 | |
Chris Lattner | 6f83d06 | 2011-04-26 01:21:15 +0000 | [diff] [blame] | 612 | // Stores depend on may/must aliased loads. |
Chris Lattner | 0e3d633 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 613 | return MemDepResult::getDef(Inst); |
| 614 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 615 | |
Chris Lattner | 0e3d633 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 616 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 617 | // Atomic stores have complications involved. |
Robin Morisset | 163ef04 | 2014-08-29 20:32:58 +0000 | [diff] [blame] | 618 | // A Monotonic store is OK if the query inst is itself not atomic. |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 619 | // FIXME: This is overly conservative. |
Krzysztof Parzyszek | e261e5a | 2016-02-22 23:07:43 +0000 | [diff] [blame] | 620 | if (!SI->isUnordered() && SI->isAtomic()) { |
| 621 | if (!QueryInst || isNonSimpleLoadOrStore(QueryInst) || |
| 622 | isOtherMemAccess(QueryInst)) |
Robin Morisset | 9e98e7f | 2014-08-18 22:18:14 +0000 | [diff] [blame] | 623 | return MemDepResult::getClobber(SI); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 624 | if (SI->getOrdering() != AtomicOrdering::Monotonic) |
David Majnemer | e165502 | 2015-03-21 06:19:17 +0000 | [diff] [blame] | 625 | return MemDepResult::getClobber(SI); |
Robin Morisset | 4ffe8aa | 2014-08-18 22:18:11 +0000 | [diff] [blame] | 626 | } |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 627 | |
Robin Morisset | 9e98e7f | 2014-08-18 22:18:14 +0000 | [diff] [blame] | 628 | // FIXME: this is overly conservative. |
| 629 | // While volatile access cannot be eliminated, they do not have to clobber |
| 630 | // non-aliasing locations, as normal accesses can for example be reordered |
| 631 | // with volatile accesses. |
| 632 | if (SI->isVolatile()) |
Krzysztof Parzyszek | e261e5a | 2016-02-22 23:07:43 +0000 | [diff] [blame] | 633 | if (!QueryInst || isNonSimpleLoadOrStore(QueryInst) || |
| 634 | isOtherMemAccess(QueryInst)) |
| 635 | return MemDepResult::getClobber(SI); |
Robin Morisset | 9e98e7f | 2014-08-18 22:18:14 +0000 | [diff] [blame] | 636 | |
Chris Lattner | 02274a7 | 2009-05-25 21:28:56 +0000 | [diff] [blame] | 637 | // If alias analysis can tell that this store is guaranteed to not modify |
| 638 | // the query pointer, ignore it. Use getModRefInfo to handle cases where |
| 639 | // the query pointer points to constant memory etc. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 640 | if (!isModOrRefSet(AA.getModRefInfo(SI, MemLoc))) |
Chris Lattner | 02274a7 | 2009-05-25 21:28:56 +0000 | [diff] [blame] | 641 | continue; |
| 642 | |
| 643 | // Ok, this store might clobber the query pointer. Check to see if it is |
| 644 | // a must alias: in this case, we want to return this as a def. |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 645 | // FIXME: Use ModRefInfo::Must bit from getModRefInfo call above. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 646 | MemoryLocation StoreLoc = MemoryLocation::get(SI); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 0e3d633 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 648 | // If we found a pointer, check if it could be the same as our pointer. |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 649 | AliasResult R = AA.alias(StoreLoc, MemLoc); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 650 | |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 651 | if (R == NoAlias) |
Chris Lattner | 0e3d633 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 652 | continue; |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 653 | if (R == MustAlias) |
Dan Gohman | ba5d0ab | 2010-12-13 22:47:57 +0000 | [diff] [blame] | 654 | return MemDepResult::getDef(Inst); |
Shuxin Yang | 408bdad | 2013-03-06 17:48:48 +0000 | [diff] [blame] | 655 | if (isInvariantLoad) |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 656 | continue; |
Dan Gohman | ba5d0ab | 2010-12-13 22:47:57 +0000 | [diff] [blame] | 657 | return MemDepResult::getClobber(Inst); |
Owen Anderson | c0daf5f | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 658 | } |
Chris Lattner | 3ff6d01 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 659 | |
| 660 | // If this is an allocation, and if we know that the accessed pointer is to |
Chris Lattner | 0e3d633 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 661 | // the allocation, return Def. This means that there is no dependence and |
Chris Lattner | 3ff6d01 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 662 | // the access can be optimized based on that. For example, a load could |
Philip Reames | d9f4a3d | 2016-03-09 23:19:56 +0000 | [diff] [blame] | 663 | // turn into undef. Note that we can bypass the allocation itself when |
| 664 | // looking for a clobber in many cases; that's an alias property and is |
| 665 | // handled by BasicAA. |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 666 | if (isa<AllocaInst>(Inst) || isNoAliasFn(Inst, &TLI)) { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 667 | const Value *AccessPtr = GetUnderlyingObject(MemLoc.Ptr, DL); |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 668 | if (AccessPtr == Inst || AA.isMustAlias(Inst, AccessPtr)) |
Victor Hernandez | 537d8d9 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 669 | return MemDepResult::getDef(Inst); |
Victor Hernandez | 537d8d9 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Philip Reames | 4dbd88f | 2015-03-24 23:54:54 +0000 | [diff] [blame] | 672 | if (isInvariantLoad) |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 673 | continue; |
Philip Reames | 4dbd88f | 2015-03-24 23:54:54 +0000 | [diff] [blame] | 674 | |
Philip Reames | b568113 | 2016-03-25 22:40:35 +0000 | [diff] [blame] | 675 | // A release fence requires that all stores complete before it, but does |
| 676 | // not prevent the reordering of following loads or stores 'before' the |
| 677 | // fence. As a result, we look past it when finding a dependency for |
Jordan Rupprecht | 6387fa2 | 2019-02-23 01:28:32 +0000 | [diff] [blame] | 678 | // loads. DSE uses this to find preceding stores to delete and thus we |
Philip Reames | b568113 | 2016-03-25 22:40:35 +0000 | [diff] [blame] | 679 | // can't bypass the fence if the query instruction is a store. |
| 680 | if (FenceInst *FI = dyn_cast<FenceInst>(Inst)) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 681 | if (isLoad && FI->getOrdering() == AtomicOrdering::Release) |
Philip Reames | b568113 | 2016-03-25 22:40:35 +0000 | [diff] [blame] | 682 | continue; |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 683 | |
Chris Lattner | 0e3d633 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 684 | // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer. |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 685 | ModRefInfo MR = AA.getModRefInfo(Inst, MemLoc); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 686 | // If necessary, perform additional analysis. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 687 | if (isModAndRefSet(MR)) |
Florian Hahn | 6c30243 | 2019-03-28 19:17:31 +0000 | [diff] [blame] | 688 | MR = AA.callCapturesBefore(Inst, MemLoc, &DT, OBB); |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 689 | switch (clearMust(MR)) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 690 | case ModRefInfo::NoModRef: |
Chris Lattner | 41efb68 | 2008-12-09 19:47:40 +0000 | [diff] [blame] | 691 | // If the call has no effect on the queried pointer, just ignore it. |
Chris Lattner | 81f19e9 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 692 | continue; |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 693 | case ModRefInfo::Mod: |
Owen Anderson | fc16e5a | 2009-10-28 06:30:52 +0000 | [diff] [blame] | 694 | return MemDepResult::getClobber(Inst); |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 695 | case ModRefInfo::Ref: |
Chris Lattner | 41efb68 | 2008-12-09 19:47:40 +0000 | [diff] [blame] | 696 | // If the call is known to never store to the pointer, and if this is a |
| 697 | // load query, we can safely ignore it (scan past it). |
| 698 | if (isLoad) |
| 699 | continue; |
Galina Kistanova | 0b69e36 | 2017-05-31 22:02:05 +0000 | [diff] [blame] | 700 | LLVM_FALLTHROUGH; |
Chris Lattner | 41efb68 | 2008-12-09 19:47:40 +0000 | [diff] [blame] | 701 | default: |
| 702 | // Otherwise, there is a potential dependence. Return a clobber. |
| 703 | return MemDepResult::getClobber(Inst); |
| 704 | } |
Owen Anderson | c0daf5f | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 705 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 706 | |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 707 | // No dependence found. If this is the entry block of the function, it is |
| 708 | // unknown, otherwise it is non-local. |
Chris Lattner | 2faa2c7 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 709 | if (BB != &BB->getParent()->getEntryBlock()) |
| 710 | return MemDepResult::getNonLocal(); |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 711 | return MemDepResult::getNonFuncLocal(); |
Owen Anderson | c0daf5f | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Florian Hahn | 6c30243 | 2019-03-28 19:17:31 +0000 | [diff] [blame] | 714 | MemDepResult MemoryDependenceResults::getDependency(Instruction *QueryInst, |
| 715 | OrderedBasicBlock *OBB) { |
Chris Lattner | 51ba8d0 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 716 | Instruction *ScanPos = QueryInst; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 51ba8d0 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 718 | // Check for a cached result |
Chris Lattner | 47e81d0 | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 719 | MemDepResult &LocalCache = LocalDeps[QueryInst]; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 720 | |
Chris Lattner | e7d7e13 | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 721 | // If the cached entry is non-dirty, just return it. Note that this depends |
Chris Lattner | 47e81d0 | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 722 | // on MemDepResult's default constructing to 'dirty'. |
| 723 | if (!LocalCache.isDirty()) |
| 724 | return LocalCache; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 51ba8d0 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 726 | // Otherwise, if we have a dirty entry, we know we can start the scan at that |
| 727 | // instruction, which may save us some work. |
Chris Lattner | 47e81d0 | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 728 | if (Instruction *Inst = LocalCache.getInst()) { |
Chris Lattner | 51ba8d0 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 729 | ScanPos = Inst; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 730 | |
Chris Lattner | de4440c | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 731 | RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst); |
Chris Lattner | 4410427 | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 732 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 733 | |
Chris Lattner | 5a78604 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 734 | BasicBlock *QueryParent = QueryInst->getParent(); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 51ba8d0 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 736 | // Do the scan. |
Chris Lattner | 5a78604 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 737 | if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) { |
Piotr Padlewski | 01659cb | 2016-11-08 18:20:51 +0000 | [diff] [blame] | 738 | // No dependence found. If this is the entry block of the function, it is |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 739 | // unknown, otherwise it is non-local. |
Chris Lattner | 2faa2c7 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 740 | if (QueryParent != &QueryParent->getParent()->getEntryBlock()) |
| 741 | LocalCache = MemDepResult::getNonLocal(); |
| 742 | else |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 743 | LocalCache = MemDepResult::getNonFuncLocal(); |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 744 | } else { |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 745 | MemoryLocation MemLoc; |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 746 | ModRefInfo MR = GetLocation(QueryInst, MemLoc, TLI); |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 747 | if (MemLoc.Ptr) { |
| 748 | // If we can do a pointer scan, make it happen. |
Alina Sbirlea | d6037eb | 2017-12-07 00:43:19 +0000 | [diff] [blame] | 749 | bool isLoad = !isModSet(MR); |
Piotr Padlewski | 01659cb | 2016-11-08 18:20:51 +0000 | [diff] [blame] | 750 | if (auto *II = dyn_cast<IntrinsicInst>(QueryInst)) |
Owen Anderson | 97f0cf3 | 2011-05-17 00:05:49 +0000 | [diff] [blame] | 751 | isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_start; |
Chris Lattner | e48c31c | 2010-11-21 07:34:32 +0000 | [diff] [blame] | 752 | |
Florian Hahn | 6c30243 | 2019-03-28 19:17:31 +0000 | [diff] [blame] | 753 | LocalCache = |
| 754 | getPointerDependencyFrom(MemLoc, isLoad, ScanPos->getIterator(), |
| 755 | QueryParent, QueryInst, nullptr, OBB); |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 756 | } else if (auto *QueryCall = dyn_cast<CallBase>(QueryInst)) { |
| 757 | bool isReadOnly = AA.onlyReadsMemory(QueryCall); |
| 758 | LocalCache = getCallDependencyFrom(QueryCall, isReadOnly, |
| 759 | ScanPos->getIterator(), QueryParent); |
Dan Gohman | 1d760ce | 2010-11-10 21:51:35 +0000 | [diff] [blame] | 760 | } else |
| 761 | // Non-memory instruction. |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 762 | LocalCache = MemDepResult::getUnknown(); |
Nick Lewycky | 218a339 | 2009-11-28 21:27:49 +0000 | [diff] [blame] | 763 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 764 | |
Chris Lattner | 51ba8d0 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 765 | // Remember the result! |
Chris Lattner | 47e81d0 | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 766 | if (Instruction *I = LocalCache.getInst()) |
Chris Lattner | 9f1988ab | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 767 | ReverseLocalDeps[I].insert(QueryInst); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 768 | |
Chris Lattner | 47e81d0 | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 769 | return LocalCache; |
Chris Lattner | 51ba8d0 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Chris Lattner | f09619d | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 772 | #ifndef NDEBUG |
Chandler Carruth | 40e21f2 | 2016-03-07 12:30:06 +0000 | [diff] [blame] | 773 | /// This method is used when -debug is specified to verify that cache arrays |
| 774 | /// are properly kept sorted. |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 775 | static void AssertSorted(MemoryDependenceResults::NonLocalDepInfo &Cache, |
Chris Lattner | f09619d | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 776 | int Count = -1) { |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 777 | if (Count == -1) |
| 778 | Count = Cache.size(); |
Craig Topper | e30b8ca | 2016-01-03 19:43:40 +0000 | [diff] [blame] | 779 | assert(std::is_sorted(Cache.begin(), Cache.begin() + Count) && |
| 780 | "Cache isn't sorted!"); |
Chris Lattner | f09619d | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 781 | } |
| 782 | #endif |
| 783 | |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 784 | const MemoryDependenceResults::NonLocalDepInfo & |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 785 | MemoryDependenceResults::getNonLocalCallDependency(CallBase *QueryCall) { |
| 786 | assert(getDependency(QueryCall).isNonLocal() && |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 787 | "getNonLocalCallDependency should only be used on calls with " |
| 788 | "non-local deps!"); |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 789 | PerInstNLInfo &CacheP = NonLocalDeps[QueryCall]; |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 790 | NonLocalDepInfo &Cache = CacheP.first; |
Chris Lattner | 2059753 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 791 | |
Chandler Carruth | 40e21f2 | 2016-03-07 12:30:06 +0000 | [diff] [blame] | 792 | // This is the set of blocks that need to be recomputed. In the cached case, |
| 793 | // this can happen due to instructions being deleted etc. In the uncached |
| 794 | // case, this starts out as the set of predecessors we care about. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 795 | SmallVector<BasicBlock *, 32> DirtyBlocks; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 796 | |
Chris Lattner | 2059753 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 797 | if (!Cache.empty()) { |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 798 | // Okay, we have a cache entry. If we know it is not dirty, just return it |
| 799 | // with no computation. |
| 800 | if (!CacheP.second) { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 801 | ++NumCacheNonLocal; |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 802 | return Cache; |
| 803 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 804 | |
Chris Lattner | 2059753 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 805 | // If we already have a partially computed set of results, scan them to |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 806 | // determine what is dirty, seeding our initial DirtyBlocks worklist. |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 807 | for (auto &Entry : Cache) |
| 808 | if (Entry.getResult().isDirty()) |
| 809 | DirtyBlocks.push_back(Entry.getBB()); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 810 | |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 811 | // Sort the cache so that we can do fast binary search lookups below. |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 812 | llvm::sort(Cache); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 813 | |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 814 | ++NumCacheDirtyNonLocal; |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 815 | // cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: " |
Chris Lattner | 2059753 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 816 | // << Cache.size() << " cached: " << *QueryInst; |
| 817 | } else { |
| 818 | // Seed DirtyBlocks with each of the preds of QueryInst's block. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 819 | BasicBlock *QueryBB = QueryCall->getParent(); |
Daniel Berlin | b4e7a4a | 2015-04-21 21:11:50 +0000 | [diff] [blame] | 820 | for (BasicBlock *Pred : PredCache.get(QueryBB)) |
| 821 | DirtyBlocks.push_back(Pred); |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 822 | ++NumUncacheNonLocal; |
Chris Lattner | 2059753 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 823 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 824 | |
Chris Lattner | 702e46e | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 825 | // isReadonlyCall - If this is a read-only call, we can be more aggressive. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 826 | bool isReadonlyCall = AA.onlyReadsMemory(QueryCall); |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 827 | |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 828 | SmallPtrSet<BasicBlock *, 32> Visited; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 829 | |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 830 | unsigned NumSortedEntries = Cache.size(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 831 | LLVM_DEBUG(AssertSorted(Cache)); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 832 | |
Chris Lattner | 2059753 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 833 | // Iterate while we still have blocks to update. |
| 834 | while (!DirtyBlocks.empty()) { |
| 835 | BasicBlock *DirtyBB = DirtyBlocks.back(); |
| 836 | DirtyBlocks.pop_back(); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 837 | |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 838 | // Already processed this block? |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 839 | if (!Visited.insert(DirtyBB).second) |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 840 | continue; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 841 | |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 842 | // Do a binary search to see if we already have an entry for this block in |
| 843 | // the cache set. If so, find it. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 844 | LLVM_DEBUG(AssertSorted(Cache, NumSortedEntries)); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 845 | NonLocalDepInfo::iterator Entry = |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 846 | std::upper_bound(Cache.begin(), Cache.begin() + NumSortedEntries, |
| 847 | NonLocalDepEntry(DirtyBB)); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 848 | if (Entry != Cache.begin() && std::prev(Entry)->getBB() == DirtyBB) |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 849 | --Entry; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 850 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 851 | NonLocalDepEntry *ExistingResult = nullptr; |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 852 | if (Entry != Cache.begin() + NumSortedEntries && |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 853 | Entry->getBB() == DirtyBB) { |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 854 | // If we already have an entry, and if it isn't already dirty, the block |
| 855 | // is done. |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 856 | if (!Entry->getResult().isDirty()) |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 857 | continue; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 858 | |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 859 | // Otherwise, remember this slot so we can update the value. |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 860 | ExistingResult = &*Entry; |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 861 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 862 | |
Chris Lattner | 2059753 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 863 | // If the dirty entry has a pointer, start scanning from it so we don't have |
| 864 | // to rescan the entire block. |
| 865 | BasicBlock::iterator ScanPos = DirtyBB->end(); |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 866 | if (ExistingResult) { |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 867 | if (Instruction *Inst = ExistingResult->getResult().getInst()) { |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 868 | ScanPos = Inst->getIterator(); |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 869 | // We're removing QueryInst's use of Inst. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 870 | RemoveFromReverseMap<Instruction *>(ReverseNonLocalDeps, Inst, |
| 871 | QueryCall); |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 872 | } |
Chris Lattner | 1b810bd | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 873 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 874 | |
Chris Lattner | 60444f8 | 2008-11-30 01:26:32 +0000 | [diff] [blame] | 875 | // Find out if this block has a local dependency for QueryInst. |
Chris Lattner | ed494f7 | 2008-12-07 01:21:14 +0000 | [diff] [blame] | 876 | MemDepResult Dep; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 877 | |
Chris Lattner | 254314e | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 878 | if (ScanPos != DirtyBB->begin()) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 879 | Dep = getCallDependencyFrom(QueryCall, isReadonlyCall, ScanPos, DirtyBB); |
Chris Lattner | 254314e | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 880 | } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) { |
| 881 | // No dependence found. If this is the entry block of the function, it is |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 882 | // a clobber, otherwise it is unknown. |
Chris Lattner | 254314e | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 883 | Dep = MemDepResult::getNonLocal(); |
Chris Lattner | 5a78604 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 884 | } else { |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 885 | Dep = MemDepResult::getNonFuncLocal(); |
Chris Lattner | 5a78604 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 886 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 887 | |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 888 | // If we had a dirty entry for the block, update it. Otherwise, just add |
| 889 | // a new entry. |
| 890 | if (ExistingResult) |
Chris Lattner | 9b7d99e | 2009-12-22 04:25:02 +0000 | [diff] [blame] | 891 | ExistingResult->setResult(Dep); |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 892 | else |
Chris Lattner | 9b7d99e | 2009-12-22 04:25:02 +0000 | [diff] [blame] | 893 | Cache.push_back(NonLocalDepEntry(DirtyBB, Dep)); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 894 | |
Chris Lattner | 2059753 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 895 | // If the block has a dependency (i.e. it isn't completely transparent to |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 896 | // the value), remember the association! |
| 897 | if (!Dep.isNonLocal()) { |
Chris Lattner | 2059753 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 898 | // Keep the ReverseNonLocalDeps map up to date so we can efficiently |
| 899 | // update this when we remove instructions. |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 900 | if (Instruction *Inst = Dep.getInst()) |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 901 | ReverseNonLocalDeps[Inst].insert(QueryCall); |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 902 | } else { |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 903 | |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 904 | // If the block *is* completely transparent to the load, we need to check |
| 905 | // the predecessors of this block. Add them to our worklist. |
Daniel Berlin | b4e7a4a | 2015-04-21 21:11:50 +0000 | [diff] [blame] | 906 | for (BasicBlock *Pred : PredCache.get(DirtyBB)) |
| 907 | DirtyBlocks.push_back(Pred); |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 908 | } |
Chris Lattner | 2059753 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 909 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 910 | |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 911 | return Cache; |
Chris Lattner | 2059753 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 914 | void MemoryDependenceResults::getNonLocalPointerDependency( |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 915 | Instruction *QueryInst, SmallVectorImpl<NonLocalDepResult> &Result) { |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 916 | const MemoryLocation Loc = MemoryLocation::get(QueryInst); |
Philip Reames | 567feb9 | 2015-01-09 00:04:22 +0000 | [diff] [blame] | 917 | bool isLoad = isa<LoadInst>(QueryInst); |
| 918 | BasicBlock *FromBB = QueryInst->getParent(); |
| 919 | assert(FromBB); |
Philip Reames | 33d7f9d | 2015-01-09 00:26:45 +0000 | [diff] [blame] | 920 | |
| 921 | assert(Loc.Ptr->getType()->isPointerTy() && |
| 922 | "Can't get pointer deps of a non-pointer!"); |
| 923 | Result.clear(); |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 924 | { |
Piotr Padlewski | 153fe60 | 2018-05-18 22:40:34 +0000 | [diff] [blame] | 925 | // Check if there is cached Def with invariant.group. |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 926 | auto NonLocalDefIt = NonLocalDefsCache.find(QueryInst); |
| 927 | if (NonLocalDefIt != NonLocalDefsCache.end()) { |
Piotr Padlewski | 153fe60 | 2018-05-18 22:40:34 +0000 | [diff] [blame] | 928 | Result.push_back(NonLocalDefIt->second); |
| 929 | ReverseNonLocalDefsCache[NonLocalDefIt->second.getResult().getInst()] |
| 930 | .erase(QueryInst); |
Piotr Padlewski | 9530883 | 2017-01-12 11:33:58 +0000 | [diff] [blame] | 931 | NonLocalDefsCache.erase(NonLocalDefIt); |
| 932 | return; |
| 933 | } |
| 934 | } |
Philip Reames | 33d7f9d | 2015-01-09 00:26:45 +0000 | [diff] [blame] | 935 | // This routine does not expect to deal with volatile instructions. |
| 936 | // Doing so would require piping through the QueryInst all the way through. |
Philip Reames | 567feb9 | 2015-01-09 00:04:22 +0000 | [diff] [blame] | 937 | // TODO: volatiles can't be elided, but they can be reordered with other |
Philip Reames | 33d7f9d | 2015-01-09 00:26:45 +0000 | [diff] [blame] | 938 | // non-volatile accesses. |
Philip Reames | a7ad6a5 | 2015-01-26 18:54:27 +0000 | [diff] [blame] | 939 | |
Philip Reames | 567feb9 | 2015-01-09 00:04:22 +0000 | [diff] [blame] | 940 | // We currently give up on any instruction which is ordered, but we do handle |
| 941 | // atomic instructions which are unordered. |
| 942 | // TODO: Handle ordered instructions |
| 943 | auto isOrdered = [](Instruction *Inst) { |
| 944 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
| 945 | return !LI->isUnordered(); |
| 946 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 947 | return !SI->isUnordered(); |
| 948 | } |
| 949 | return false; |
| 950 | }; |
Philip Reames | 33d7f9d | 2015-01-09 00:26:45 +0000 | [diff] [blame] | 951 | if (isVolatile(QueryInst) || isOrdered(QueryInst)) { |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 952 | Result.push_back(NonLocalDepResult(FromBB, MemDepResult::getUnknown(), |
Philip Reames | 33d7f9d | 2015-01-09 00:26:45 +0000 | [diff] [blame] | 953 | const_cast<Value *>(Loc.Ptr))); |
| 954 | return; |
| 955 | } |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 956 | const DataLayout &DL = FromBB->getModule()->getDataLayout(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 957 | PHITransAddr Address(const_cast<Value *>(Loc.Ptr), DL, &AC); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 958 | |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 959 | // This is the set of blocks we've inspected, and the pointer we consider in |
| 960 | // each block. Because of critical edges, we currently bail out if querying |
| 961 | // a block with multiple different pointers. This can happen during PHI |
| 962 | // translation. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 963 | DenseMap<BasicBlock *, Value *> Visited; |
Chandler Carruth | b32febe | 2016-03-07 12:45:07 +0000 | [diff] [blame] | 964 | if (getNonLocalPointerDepFromBB(QueryInst, Address, Loc, isLoad, FromBB, |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 965 | Result, Visited, true)) |
| 966 | return; |
Chris Lattner | 7ed5ccc | 2008-12-15 04:58:29 +0000 | [diff] [blame] | 967 | Result.clear(); |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 968 | Result.push_back(NonLocalDepResult(FromBB, MemDepResult::getUnknown(), |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 969 | const_cast<Value *>(Loc.Ptr))); |
Chris Lattner | 7564a3b | 2008-12-07 02:56:57 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Chandler Carruth | 40e21f2 | 2016-03-07 12:30:06 +0000 | [diff] [blame] | 972 | /// Compute the memdep value for BB with Pointer/PointeeSize using either |
| 973 | /// cached information in Cache or by doing a lookup (which may use dirty cache |
| 974 | /// info if available). |
| 975 | /// |
| 976 | /// If we do a lookup, add the result to the cache. |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 977 | MemDepResult MemoryDependenceResults::GetNonLocalInfoForBlock( |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 978 | Instruction *QueryInst, const MemoryLocation &Loc, bool isLoad, |
| 979 | BasicBlock *BB, NonLocalDepInfo *Cache, unsigned NumSortedEntries) { |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 980 | |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 981 | // Do a binary search to see if we already have an entry for this block in |
| 982 | // the cache set. If so, find it. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 983 | NonLocalDepInfo::iterator Entry = std::upper_bound( |
| 984 | Cache->begin(), Cache->begin() + NumSortedEntries, NonLocalDepEntry(BB)); |
| 985 | if (Entry != Cache->begin() && (Entry - 1)->getBB() == BB) |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 986 | --Entry; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 987 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 988 | NonLocalDepEntry *ExistingResult = nullptr; |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 989 | if (Entry != Cache->begin() + NumSortedEntries && Entry->getBB() == BB) |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 990 | ExistingResult = &*Entry; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 991 | |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 992 | // If we have a cached entry, and it is non-dirty, use it as the value for |
| 993 | // this dependency. |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 994 | if (ExistingResult && !ExistingResult->getResult().isDirty()) { |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 995 | ++NumCacheNonLocalPtr; |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 996 | return ExistingResult->getResult(); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 999 | // Otherwise, we have to scan for the value. If we have a dirty cache |
| 1000 | // entry, start scanning from its position, otherwise we scan from the end |
| 1001 | // of the block. |
| 1002 | BasicBlock::iterator ScanPos = BB->end(); |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 1003 | if (ExistingResult && ExistingResult->getResult().getInst()) { |
| 1004 | assert(ExistingResult->getResult().getInst()->getParent() == BB && |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1005 | "Instruction invalidated?"); |
| 1006 | ++NumCacheDirtyNonLocalPtr; |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1007 | ScanPos = ExistingResult->getResult().getInst()->getIterator(); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1008 | |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1009 | // Eliminating the dirty entry from 'Cache', so update the reverse info. |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1010 | ValueIsLoadPair CacheKey(Loc.Ptr, isLoad); |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1011 | RemoveFromReverseMap(ReverseNonLocalPtrDeps, &*ScanPos, CacheKey); |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1012 | } else { |
| 1013 | ++NumUncacheNonLocalPtr; |
| 1014 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1015 | |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1016 | // Scan the block for the dependency. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1017 | MemDepResult Dep = |
| 1018 | getPointerDependencyFrom(Loc, isLoad, ScanPos, BB, QueryInst); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1019 | |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1020 | // If we had a dirty entry for the block, update it. Otherwise, just add |
| 1021 | // a new entry. |
| 1022 | if (ExistingResult) |
Chris Lattner | 9b7d99e | 2009-12-22 04:25:02 +0000 | [diff] [blame] | 1023 | ExistingResult->setResult(Dep); |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1024 | else |
Chris Lattner | 9b7d99e | 2009-12-22 04:25:02 +0000 | [diff] [blame] | 1025 | Cache->push_back(NonLocalDepEntry(BB, Dep)); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1027 | // If the block has a dependency (i.e. it isn't completely transparent to |
| 1028 | // the value), remember the reverse association because we just added it |
| 1029 | // to Cache! |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 1030 | if (!Dep.isDef() && !Dep.isClobber()) |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1031 | return Dep; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1032 | |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1033 | // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently |
| 1034 | // update MemDep when we remove instructions. |
| 1035 | Instruction *Inst = Dep.getInst(); |
| 1036 | assert(Inst && "Didn't depend on anything?"); |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1037 | ValueIsLoadPair CacheKey(Loc.Ptr, isLoad); |
Chris Lattner | 8eda11b | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1038 | ReverseNonLocalPtrDeps[Inst].insert(CacheKey); |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1039 | return Dep; |
| 1040 | } |
| 1041 | |
Chandler Carruth | 40e21f2 | 2016-03-07 12:30:06 +0000 | [diff] [blame] | 1042 | /// Sort the NonLocalDepInfo cache, given a certain number of elements in the |
| 1043 | /// array that are already properly ordered. |
| 1044 | /// |
| 1045 | /// This is optimized for the case when only a few entries are added. |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1046 | static void |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1047 | SortNonLocalDepInfoCache(MemoryDependenceResults::NonLocalDepInfo &Cache, |
Chris Lattner | 370aada | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 1048 | unsigned NumSortedEntries) { |
| 1049 | switch (Cache.size() - NumSortedEntries) { |
| 1050 | case 0: |
| 1051 | // done, no new entries. |
| 1052 | break; |
| 1053 | case 2: { |
| 1054 | // Two new entries, insert the last one into place. |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 1055 | NonLocalDepEntry Val = Cache.back(); |
Chris Lattner | 370aada | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 1056 | Cache.pop_back(); |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1057 | MemoryDependenceResults::NonLocalDepInfo::iterator Entry = |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1058 | std::upper_bound(Cache.begin(), Cache.end() - 1, Val); |
Chris Lattner | 370aada | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 1059 | Cache.insert(Entry, Val); |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 1060 | LLVM_FALLTHROUGH; |
Chris Lattner | 370aada | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 1061 | } |
| 1062 | case 1: |
| 1063 | // One new entry, Just insert the new value at the appropriate position. |
| 1064 | if (Cache.size() != 1) { |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 1065 | NonLocalDepEntry Val = Cache.back(); |
Chris Lattner | 370aada | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 1066 | Cache.pop_back(); |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1067 | MemoryDependenceResults::NonLocalDepInfo::iterator Entry = |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1068 | std::upper_bound(Cache.begin(), Cache.end(), Val); |
Chris Lattner | 370aada | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 1069 | Cache.insert(Entry, Val); |
| 1070 | } |
| 1071 | break; |
| 1072 | default: |
| 1073 | // Added many values, do a full scale sort. |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 1074 | llvm::sort(Cache); |
Chris Lattner | 370aada | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | |
Chandler Carruth | 40e21f2 | 2016-03-07 12:30:06 +0000 | [diff] [blame] | 1079 | /// Perform a dependency query based on pointer/pointeesize starting at the end |
| 1080 | /// of StartBB. |
| 1081 | /// |
| 1082 | /// Add any clobber/def results to the results vector and keep track of which |
| 1083 | /// blocks are visited in 'Visited'. |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1084 | /// |
| 1085 | /// This has special behavior for the first block queries (when SkipFirstBlock |
| 1086 | /// is true). In this special case, it ignores the contents of the specified |
| 1087 | /// block and starts returning dependence info for its predecessors. |
| 1088 | /// |
Chandler Carruth | b32febe | 2016-03-07 12:45:07 +0000 | [diff] [blame] | 1089 | /// This function returns true on success, or false to indicate that it could |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1090 | /// not compute dependence information for some reason. This should be treated |
| 1091 | /// as a clobber dependence on the first instruction in the predecessor block. |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1092 | bool MemoryDependenceResults::getNonLocalPointerDepFromBB( |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 1093 | Instruction *QueryInst, const PHITransAddr &Pointer, |
| 1094 | const MemoryLocation &Loc, bool isLoad, BasicBlock *StartBB, |
| 1095 | SmallVectorImpl<NonLocalDepResult> &Result, |
| 1096 | DenseMap<BasicBlock *, Value *> &Visited, bool SkipFirstBlock) { |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1097 | // Look up the cached info for Pointer. |
Chris Lattner | 972e6d8 | 2009-12-09 01:59:31 +0000 | [diff] [blame] | 1098 | ValueIsLoadPair CacheKey(Pointer.getAddr(), isLoad); |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1099 | |
Dan Gohman | 0a6021a | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 1100 | // Set up a temporary NLPI value. If the map doesn't yet have an entry for |
| 1101 | // CacheKey, this value will be inserted as the associated value. Otherwise, |
| 1102 | // it'll be ignored, and we'll have to check to see if the cached size and |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1103 | // aa tags are consistent with the current query. |
Dan Gohman | 0a6021a | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 1104 | NonLocalPointerInfo InitialNLPI; |
| 1105 | InitialNLPI.Size = Loc.Size; |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1106 | InitialNLPI.AATags = Loc.AATags; |
Dan Gohman | 0a6021a | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 1107 | |
| 1108 | // Get the NLPI for CacheKey, inserting one into the map if it doesn't |
| 1109 | // already have one. |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1110 | std::pair<CachedNonLocalPointerInfo::iterator, bool> Pair = |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1111 | NonLocalPointerDeps.insert(std::make_pair(CacheKey, InitialNLPI)); |
Dan Gohman | 0a6021a | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 1112 | NonLocalPointerInfo *CacheInfo = &Pair.first->second; |
| 1113 | |
Dan Gohman | 2e8ca44 | 2010-11-10 21:45:11 +0000 | [diff] [blame] | 1114 | // If we already have a cache entry for this CacheKey, we may need to do some |
| 1115 | // work to reconcile the cache entry and the current query. |
Dan Gohman | 0a6021a | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 1116 | if (!Pair.second) { |
George Burgess IV | 40dc63e | 2018-10-10 06:39:40 +0000 | [diff] [blame] | 1117 | if (CacheInfo->Size != Loc.Size) { |
| 1118 | bool ThrowOutEverything; |
| 1119 | if (CacheInfo->Size.hasValue() && Loc.Size.hasValue()) { |
| 1120 | // FIXME: We may be able to do better in the face of results with mixed |
| 1121 | // precision. We don't appear to get them in practice, though, so just |
| 1122 | // be conservative. |
| 1123 | ThrowOutEverything = |
| 1124 | CacheInfo->Size.isPrecise() != Loc.Size.isPrecise() || |
| 1125 | CacheInfo->Size.getValue() < Loc.Size.getValue(); |
| 1126 | } else { |
| 1127 | // For our purposes, unknown size > all others. |
| 1128 | ThrowOutEverything = !Loc.Size.hasValue(); |
| 1129 | } |
| 1130 | |
| 1131 | if (ThrowOutEverything) { |
| 1132 | // The query's Size is greater than the cached one. Throw out the |
| 1133 | // cached data and proceed with the query at the greater size. |
| 1134 | CacheInfo->Pair = BBSkipFirstBlockPair(); |
| 1135 | CacheInfo->Size = Loc.Size; |
| 1136 | for (auto &Entry : CacheInfo->NonLocalDeps) |
| 1137 | if (Instruction *Inst = Entry.getResult().getInst()) |
| 1138 | RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey); |
| 1139 | CacheInfo->NonLocalDeps.clear(); |
| 1140 | } else { |
| 1141 | // This query's Size is less than the cached one. Conservatively restart |
| 1142 | // the query using the greater size. |
| 1143 | return getNonLocalPointerDepFromBB( |
| 1144 | QueryInst, Pointer, Loc.getWithNewSize(CacheInfo->Size), isLoad, |
| 1145 | StartBB, Result, Visited, SkipFirstBlock); |
| 1146 | } |
Dan Gohman | 0a6021a | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1149 | // If the query's AATags are inconsistent with the cached one, |
Dan Gohman | 2e8ca44 | 2010-11-10 21:45:11 +0000 | [diff] [blame] | 1150 | // conservatively throw out the cached data and restart the query with |
| 1151 | // no tag if needed. |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1152 | if (CacheInfo->AATags != Loc.AATags) { |
| 1153 | if (CacheInfo->AATags) { |
Dan Gohman | 2e8ca44 | 2010-11-10 21:45:11 +0000 | [diff] [blame] | 1154 | CacheInfo->Pair = BBSkipFirstBlockPair(); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1155 | CacheInfo->AATags = AAMDNodes(); |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1156 | for (auto &Entry : CacheInfo->NonLocalDeps) |
| 1157 | if (Instruction *Inst = Entry.getResult().getInst()) |
Dan Gohman | 6791936 | 2010-11-10 22:35:02 +0000 | [diff] [blame] | 1158 | RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey); |
Dan Gohman | 2e8ca44 | 2010-11-10 21:45:11 +0000 | [diff] [blame] | 1159 | CacheInfo->NonLocalDeps.clear(); |
| 1160 | } |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1161 | if (Loc.AATags) |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1162 | return getNonLocalPointerDepFromBB( |
| 1163 | QueryInst, Pointer, Loc.getWithoutAATags(), isLoad, StartBB, Result, |
| 1164 | Visited, SkipFirstBlock); |
Dan Gohman | 0a6021a | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 1165 | } |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | NonLocalDepInfo *Cache = &CacheInfo->NonLocalDeps; |
Chris Lattner | 5ed409e | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1169 | |
| 1170 | // If we have valid cached information for exactly the block we are |
| 1171 | // investigating, just return it with no recomputation. |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1172 | if (CacheInfo->Pair == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) { |
Chris Lattner | 8b4be37 | 2008-12-16 07:10:09 +0000 | [diff] [blame] | 1173 | // We have a fully cached result for this query then we can just return the |
| 1174 | // cached results and populate the visited set. However, we have to verify |
| 1175 | // that we don't already have conflicting results for these blocks. Check |
| 1176 | // to ensure that if a block in the results set is in the visited set that |
| 1177 | // it was for the same pointer query. |
| 1178 | if (!Visited.empty()) { |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1179 | for (auto &Entry : *Cache) { |
| 1180 | DenseMap<BasicBlock *, Value *>::iterator VI = |
| 1181 | Visited.find(Entry.getBB()); |
Chris Lattner | 972e6d8 | 2009-12-09 01:59:31 +0000 | [diff] [blame] | 1182 | if (VI == Visited.end() || VI->second == Pointer.getAddr()) |
| 1183 | continue; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1184 | |
Chandler Carruth | b32febe | 2016-03-07 12:45:07 +0000 | [diff] [blame] | 1185 | // We have a pointer mismatch in a block. Just return false, saying |
Chris Lattner | 8b4be37 | 2008-12-16 07:10:09 +0000 | [diff] [blame] | 1186 | // that something was clobbered in this result. We could also do a |
| 1187 | // non-fully cached query, but there is little point in doing this. |
Chandler Carruth | b32febe | 2016-03-07 12:45:07 +0000 | [diff] [blame] | 1188 | return false; |
Chris Lattner | 8b4be37 | 2008-12-16 07:10:09 +0000 | [diff] [blame] | 1189 | } |
| 1190 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1191 | |
Chris Lattner | 9b7d99e | 2009-12-22 04:25:02 +0000 | [diff] [blame] | 1192 | Value *Addr = Pointer.getAddr(); |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1193 | for (auto &Entry : *Cache) { |
| 1194 | Visited.insert(std::make_pair(Entry.getBB(), Addr)); |
| 1195 | if (Entry.getResult().isNonLocal()) { |
Matt Arsenault | c23753a | 2013-05-06 02:07:24 +0000 | [diff] [blame] | 1196 | continue; |
| 1197 | } |
| 1198 | |
Chandler Carruth | aef32bd | 2016-03-11 13:46:00 +0000 | [diff] [blame] | 1199 | if (DT.isReachableFromEntry(Entry.getBB())) { |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1200 | Result.push_back( |
| 1201 | NonLocalDepResult(Entry.getBB(), Entry.getResult(), Addr)); |
Matt Arsenault | c23753a | 2013-05-06 02:07:24 +0000 | [diff] [blame] | 1202 | } |
Chris Lattner | 8b4be37 | 2008-12-16 07:10:09 +0000 | [diff] [blame] | 1203 | } |
Chris Lattner | 5ed409e | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1204 | ++NumCacheCompleteNonLocalPtr; |
Chandler Carruth | b32febe | 2016-03-07 12:45:07 +0000 | [diff] [blame] | 1205 | return true; |
Chris Lattner | 5ed409e | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1206 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1207 | |
Chris Lattner | 5ed409e | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1208 | // Otherwise, either this is a new block, a block with an invalid cache |
| 1209 | // pointer or one that we're about to invalidate by putting more info into it |
| 1210 | // than its valid cache info. If empty, the result will be valid cache info, |
| 1211 | // otherwise it isn't. |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1212 | if (Cache->empty()) |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1213 | CacheInfo->Pair = BBSkipFirstBlockPair(StartBB, SkipFirstBlock); |
Dan Gohman | c87c843 | 2010-11-11 00:42:22 +0000 | [diff] [blame] | 1214 | else |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1215 | CacheInfo->Pair = BBSkipFirstBlockPair(); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1216 | |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1217 | SmallVector<BasicBlock *, 32> Worklist; |
Chris Lattner | 5ed409e | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1218 | Worklist.push_back(StartBB); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1219 | |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1220 | // PredList used inside loop. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1221 | SmallVector<std::pair<BasicBlock *, PHITransAddr>, 16> PredList; |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1222 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1223 | // Keep track of the entries that we know are sorted. Previously cached |
| 1224 | // entries will all be sorted. The entries we add we only sort on demand (we |
| 1225 | // don't insert every element into its sorted position). We know that we |
| 1226 | // won't get any reuse from currently inserted values, because we don't |
| 1227 | // revisit blocks after we insert info for them. |
| 1228 | unsigned NumSortedEntries = Cache->size(); |
Joerg Sonnenberger | 36894dc | 2016-02-20 11:24:44 +0000 | [diff] [blame] | 1229 | unsigned WorklistEntries = BlockNumberLimit; |
| 1230 | bool GotWorklistLimit = false; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1231 | LLVM_DEBUG(AssertSorted(*Cache)); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1232 | |
Chris Lattner | 2faa2c7 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 1233 | while (!Worklist.empty()) { |
Chris Lattner | 7564a3b | 2008-12-07 02:56:57 +0000 | [diff] [blame] | 1234 | BasicBlock *BB = Worklist.pop_back_val(); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1235 | |
Bruno Cardoso Lopes | e3c513a | 2014-10-01 20:07:13 +0000 | [diff] [blame] | 1236 | // If we do process a large number of blocks it becomes very expensive and |
| 1237 | // likely it isn't worth worrying about |
| 1238 | if (Result.size() > NumResultsLimit) { |
| 1239 | Worklist.clear(); |
| 1240 | // Sort it now (if needed) so that recursive invocations of |
| 1241 | // getNonLocalPointerDepFromBB and other routines that could reuse the |
| 1242 | // cache value will only see properly sorted cache arrays. |
| 1243 | if (Cache && NumSortedEntries != Cache->size()) { |
| 1244 | SortNonLocalDepInfoCache(*Cache, NumSortedEntries); |
Bruno Cardoso Lopes | e3c513a | 2014-10-01 20:07:13 +0000 | [diff] [blame] | 1245 | } |
| 1246 | // Since we bail out, the "Cache" set won't contain all of the |
| 1247 | // results for the query. This is ok (we can still use it to accelerate |
| 1248 | // specific block queries) but we can't do the fastpath "return all |
| 1249 | // results from the set". Clear out the indicator for this. |
| 1250 | CacheInfo->Pair = BBSkipFirstBlockPair(); |
Chandler Carruth | b32febe | 2016-03-07 12:45:07 +0000 | [diff] [blame] | 1251 | return false; |
Bruno Cardoso Lopes | e3c513a | 2014-10-01 20:07:13 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Chris Lattner | 75510d8 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 1254 | // Skip the first block if we have it. |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1255 | if (!SkipFirstBlock) { |
Chris Lattner | 75510d8 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 1256 | // Analyze the dependency of *Pointer in FromBB. See if we already have |
| 1257 | // been here. |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1258 | assert(Visited.count(BB) && "Should check 'visited' before adding to WL"); |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | 75510d8 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 1260 | // Get the dependency info for Pointer in BB. If we have cached |
| 1261 | // information, we will use it, otherwise we compute it. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1262 | LLVM_DEBUG(AssertSorted(*Cache, NumSortedEntries)); |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1263 | MemDepResult Dep = GetNonLocalInfoForBlock(QueryInst, Loc, isLoad, BB, |
| 1264 | Cache, NumSortedEntries); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1265 | |
Chris Lattner | 75510d8 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 1266 | // If we got a Def or Clobber, add this to the list of results. |
Matt Arsenault | c23753a | 2013-05-06 02:07:24 +0000 | [diff] [blame] | 1267 | if (!Dep.isNonLocal()) { |
Chandler Carruth | aef32bd | 2016-03-11 13:46:00 +0000 | [diff] [blame] | 1268 | if (DT.isReachableFromEntry(BB)) { |
Matt Arsenault | c23753a | 2013-05-06 02:07:24 +0000 | [diff] [blame] | 1269 | Result.push_back(NonLocalDepResult(BB, Dep, Pointer.getAddr())); |
| 1270 | continue; |
| 1271 | } |
Chris Lattner | 75510d8 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 1272 | } |
Chris Lattner | 2faa2c7 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 1273 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1274 | |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1275 | // If 'Pointer' is an instruction defined in this block, then we need to do |
| 1276 | // phi translation to change it into a value live in the predecessor block. |
Chris Lattner | 972e6d8 | 2009-12-09 01:59:31 +0000 | [diff] [blame] | 1277 | // If not, we just add the predecessors to the worklist and scan them with |
| 1278 | // the same Pointer. |
| 1279 | if (!Pointer.NeedsPHITranslationFromBlock(BB)) { |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1280 | SkipFirstBlock = false; |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1281 | SmallVector<BasicBlock *, 16> NewBlocks; |
Daniel Berlin | b4e7a4a | 2015-04-21 21:11:50 +0000 | [diff] [blame] | 1282 | for (BasicBlock *Pred : PredCache.get(BB)) { |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1283 | // Verify that we haven't looked at this block yet. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1284 | std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> InsertRes = |
| 1285 | Visited.insert(std::make_pair(Pred, Pointer.getAddr())); |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1286 | if (InsertRes.second) { |
| 1287 | // First time we've looked at *PI. |
Daniel Berlin | b4e7a4a | 2015-04-21 21:11:50 +0000 | [diff] [blame] | 1288 | NewBlocks.push_back(Pred); |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1289 | continue; |
| 1290 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1292 | // If we have seen this block before, but it was with a different |
| 1293 | // pointer then we have a phi translation failure and we have to treat |
| 1294 | // this as a clobber. |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1295 | if (InsertRes.first->second != Pointer.getAddr()) { |
| 1296 | // Make sure to clean up the Visited map before continuing on to |
| 1297 | // PredTranslationFailure. |
| 1298 | for (unsigned i = 0; i < NewBlocks.size(); i++) |
| 1299 | Visited.erase(NewBlocks[i]); |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1300 | goto PredTranslationFailure; |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1301 | } |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1302 | } |
Joerg Sonnenberger | 36894dc | 2016-02-20 11:24:44 +0000 | [diff] [blame] | 1303 | if (NewBlocks.size() > WorklistEntries) { |
| 1304 | // Make sure to clean up the Visited map before continuing on to |
| 1305 | // PredTranslationFailure. |
| 1306 | for (unsigned i = 0; i < NewBlocks.size(); i++) |
| 1307 | Visited.erase(NewBlocks[i]); |
| 1308 | GotWorklistLimit = true; |
| 1309 | goto PredTranslationFailure; |
| 1310 | } |
| 1311 | WorklistEntries -= NewBlocks.size(); |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1312 | Worklist.append(NewBlocks.begin(), NewBlocks.end()); |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1313 | continue; |
| 1314 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1315 | |
Chris Lattner | 972e6d8 | 2009-12-09 01:59:31 +0000 | [diff] [blame] | 1316 | // We do need to do phi translation, if we know ahead of time we can't phi |
| 1317 | // translate this value, don't even try. |
| 1318 | if (!Pointer.IsPotentiallyPHITranslatable()) |
| 1319 | goto PredTranslationFailure; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1320 | |
Chris Lattner | 2f0c1c4 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 1321 | // We may have added values to the cache list before this PHI translation. |
| 1322 | // If so, we haven't done anything to ensure that the cache remains sorted. |
| 1323 | // Sort it now (if needed) so that recursive invocations of |
| 1324 | // getNonLocalPointerDepFromBB and other routines that could reuse the cache |
| 1325 | // value will only see properly sorted cache arrays. |
| 1326 | if (Cache && NumSortedEntries != Cache->size()) { |
Chris Lattner | 370aada | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 1327 | SortNonLocalDepInfoCache(*Cache, NumSortedEntries); |
Chris Lattner | 2f0c1c4 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 1328 | NumSortedEntries = Cache->size(); |
| 1329 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1330 | Cache = nullptr; |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1331 | |
| 1332 | PredList.clear(); |
Daniel Berlin | b4e7a4a | 2015-04-21 21:11:50 +0000 | [diff] [blame] | 1333 | for (BasicBlock *Pred : PredCache.get(BB)) { |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1334 | PredList.push_back(std::make_pair(Pred, Pointer)); |
| 1335 | |
Chris Lattner | 972e6d8 | 2009-12-09 01:59:31 +0000 | [diff] [blame] | 1336 | // Get the PHI translated pointer in this predecessor. This can fail if |
| 1337 | // not translatable, in which case the getAddr() returns null. |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1338 | PHITransAddr &PredPointer = PredList.back().second; |
Chandler Carruth | aef32bd | 2016-03-11 13:46:00 +0000 | [diff] [blame] | 1339 | PredPointer.PHITranslateValue(BB, Pred, &DT, /*MustDominate=*/false); |
Chris Lattner | 972e6d8 | 2009-12-09 01:59:31 +0000 | [diff] [blame] | 1340 | Value *PredPtrVal = PredPointer.getAddr(); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1341 | |
Chris Lattner | ac32329 | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1342 | // Check to see if we have already visited this pred block with another |
| 1343 | // pointer. If so, we can't do this lookup. This failure can occur |
| 1344 | // with PHI translation when a critical edge exists and the PHI node in |
| 1345 | // the successor translates to a pointer value different than the |
| 1346 | // pointer the block was first analyzed with. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1347 | std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> InsertRes = |
| 1348 | Visited.insert(std::make_pair(Pred, PredPtrVal)); |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1349 | |
Chris Lattner | ac32329 | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1350 | if (!InsertRes.second) { |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1351 | // We found the pred; take it off the list of preds to visit. |
| 1352 | PredList.pop_back(); |
| 1353 | |
Chris Lattner | ac32329 | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1354 | // If the predecessor was visited with PredPtr, then we already did |
| 1355 | // the analysis and can ignore it. |
Chris Lattner | 972e6d8 | 2009-12-09 01:59:31 +0000 | [diff] [blame] | 1356 | if (InsertRes.first->second == PredPtrVal) |
Chris Lattner | ac32329 | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1357 | continue; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | ac32329 | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1359 | // Otherwise, the block was previously analyzed with a different |
| 1360 | // pointer. We can't represent the result of this case, so we just |
| 1361 | // treat this as a phi translation failure. |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1362 | |
| 1363 | // Make sure to clean up the Visited map before continuing on to |
| 1364 | // PredTranslationFailure. |
Matt Arsenault | 2080ecd | 2013-03-29 18:48:42 +0000 | [diff] [blame] | 1365 | for (unsigned i = 0, n = PredList.size(); i < n; ++i) |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1366 | Visited.erase(PredList[i].first); |
| 1367 | |
Chris Lattner | ac32329 | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1368 | goto PredTranslationFailure; |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1369 | } |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | // Actually process results here; this need to be a separate loop to avoid |
| 1373 | // calling getNonLocalPointerDepFromBB for blocks we don't want to return |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1374 | // any results for. (getNonLocalPointerDepFromBB will modify our |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1375 | // datastructures in ways the code after the PredTranslationFailure label |
| 1376 | // doesn't expect.) |
Matt Arsenault | 2080ecd | 2013-03-29 18:48:42 +0000 | [diff] [blame] | 1377 | for (unsigned i = 0, n = PredList.size(); i < n; ++i) { |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1378 | BasicBlock *Pred = PredList[i].first; |
| 1379 | PHITransAddr &PredPointer = PredList[i].second; |
| 1380 | Value *PredPtrVal = PredPointer.getAddr(); |
| 1381 | |
| 1382 | bool CanTranslate = true; |
Chris Lattner | 2be52e7 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 1383 | // If PHI translation was unable to find an available pointer in this |
| 1384 | // predecessor, then we have to assume that the pointer is clobbered in |
| 1385 | // that predecessor. We can still do PRE of the load, which would insert |
| 1386 | // a computation of the pointer in this predecessor. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1387 | if (!PredPtrVal) |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1388 | CanTranslate = false; |
| 1389 | |
| 1390 | // FIXME: it is entirely possible that PHI translating will end up with |
| 1391 | // the same value. Consider PHI translating something like: |
| 1392 | // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need* |
| 1393 | // to recurse here, pedantically speaking. |
| 1394 | |
| 1395 | // If getNonLocalPointerDepFromBB fails here, that means the cached |
| 1396 | // result conflicted with the Visited list; we have to conservatively |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 1397 | // assume it is unknown, but this also does not block PRE of the load. |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1398 | if (!CanTranslate || |
Chandler Carruth | b32febe | 2016-03-07 12:45:07 +0000 | [diff] [blame] | 1399 | !getNonLocalPointerDepFromBB(QueryInst, PredPointer, |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1400 | Loc.getWithNewPtr(PredPtrVal), isLoad, |
| 1401 | Pred, Result, Visited)) { |
Chris Lattner | 9c2053b | 2009-12-01 07:33:32 +0000 | [diff] [blame] | 1402 | // Add the entry to the Result list. |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 1403 | NonLocalDepResult Entry(Pred, MemDepResult::getUnknown(), PredPtrVal); |
Chris Lattner | 9c2053b | 2009-12-01 07:33:32 +0000 | [diff] [blame] | 1404 | Result.push_back(Entry); |
| 1405 | |
Chris Lattner | 25bf6f8 | 2009-12-19 21:29:22 +0000 | [diff] [blame] | 1406 | // Since we had a phi translation failure, the cache for CacheKey won't |
| 1407 | // include all of the entries that we need to immediately satisfy future |
| 1408 | // queries. Mark this in NonLocalPointerDeps by setting the |
| 1409 | // BBSkipFirstBlockPair pointer to null. This requires reuse of the |
| 1410 | // cached value to do more work but not miss the phi trans failure. |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1411 | NonLocalPointerInfo &NLPI = NonLocalPointerDeps[CacheKey]; |
| 1412 | NLPI.Pair = BBSkipFirstBlockPair(); |
Chris Lattner | 2be52e7 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 1413 | continue; |
Chris Lattner | 2be52e7 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 1414 | } |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1415 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1416 | |
Chris Lattner | ac32329 | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1417 | // Refresh the CacheInfo/Cache pointer so that it isn't invalidated. |
| 1418 | CacheInfo = &NonLocalPointerDeps[CacheKey]; |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1419 | Cache = &CacheInfo->NonLocalDeps; |
Chris Lattner | ac32329 | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1420 | NumSortedEntries = Cache->size(); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1421 | |
Chris Lattner | ac32329 | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1422 | // Since we did phi translation, the "Cache" set won't contain all of the |
| 1423 | // results for the query. This is ok (we can still use it to accelerate |
| 1424 | // specific block queries) but we can't do the fastpath "return all |
| 1425 | // results from the set" Clear out the indicator for this. |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1426 | CacheInfo->Pair = BBSkipFirstBlockPair(); |
Chris Lattner | ac32329 | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1427 | SkipFirstBlock = false; |
| 1428 | continue; |
Chris Lattner | c49f5ac | 2009-11-26 23:18:49 +0000 | [diff] [blame] | 1429 | |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1430 | PredTranslationFailure: |
Eli Friedman | 4b6eeb9 | 2011-06-01 23:16:53 +0000 | [diff] [blame] | 1431 | // The following code is "failure"; we can't produce a sane translation |
| 1432 | // for the given block. It assumes that we haven't modified any of |
| 1433 | // our datastructures while processing the current block. |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1434 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1435 | if (!Cache) { |
Chris Lattner | 3f4591c | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 1436 | // Refresh the CacheInfo/Cache pointer if it got invalidated. |
| 1437 | CacheInfo = &NonLocalPointerDeps[CacheKey]; |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1438 | Cache = &CacheInfo->NonLocalDeps; |
Chris Lattner | 3f4591c | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 1439 | NumSortedEntries = Cache->size(); |
Chris Lattner | 3f4591c | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 1440 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1441 | |
Chris Lattner | 25bf6f8 | 2009-12-19 21:29:22 +0000 | [diff] [blame] | 1442 | // Since we failed phi translation, the "Cache" set won't contain all of the |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1443 | // results for the query. This is ok (we can still use it to accelerate |
| 1444 | // specific block queries) but we can't do the fastpath "return all |
Chris Lattner | 25bf6f8 | 2009-12-19 21:29:22 +0000 | [diff] [blame] | 1445 | // results from the set". Clear out the indicator for this. |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1446 | CacheInfo->Pair = BBSkipFirstBlockPair(); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1447 | |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 1448 | // If *nothing* works, mark the pointer as unknown. |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1449 | // |
| 1450 | // If this is the magic first block, return this as a clobber of the whole |
| 1451 | // incoming value. Since we can't phi translate to one of the predecessors, |
| 1452 | // we have to bail out. |
| 1453 | if (SkipFirstBlock) |
Chandler Carruth | b32febe | 2016-03-07 12:45:07 +0000 | [diff] [blame] | 1454 | return false; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1455 | |
Joerg Sonnenberger | 36894dc | 2016-02-20 11:24:44 +0000 | [diff] [blame] | 1456 | bool foundBlock = false; |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1457 | for (NonLocalDepEntry &I : llvm::reverse(*Cache)) { |
Joerg Sonnenberger | 36894dc | 2016-02-20 11:24:44 +0000 | [diff] [blame] | 1458 | if (I.getBB() != BB) |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1459 | continue; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1460 | |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1461 | assert((GotWorklistLimit || I.getResult().isNonLocal() || |
Chandler Carruth | aef32bd | 2016-03-11 13:46:00 +0000 | [diff] [blame] | 1462 | !DT.isReachableFromEntry(BB)) && |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1463 | "Should only be here with transparent block"); |
Joerg Sonnenberger | 36894dc | 2016-02-20 11:24:44 +0000 | [diff] [blame] | 1464 | foundBlock = true; |
| 1465 | I.setResult(MemDepResult::getUnknown()); |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1466 | Result.push_back( |
| 1467 | NonLocalDepResult(I.getBB(), I.getResult(), Pointer.getAddr())); |
Chris Lattner | ff9f3db | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1468 | break; |
Chris Lattner | 7564a3b | 2008-12-07 02:56:57 +0000 | [diff] [blame] | 1469 | } |
Mehdi Amini | 89038a1 | 2016-04-02 05:34:19 +0000 | [diff] [blame] | 1470 | (void)foundBlock; (void)GotWorklistLimit; |
Joerg Sonnenberger | 36894dc | 2016-02-20 11:24:44 +0000 | [diff] [blame] | 1471 | assert((foundBlock || GotWorklistLimit) && "Current block not in cache?"); |
Chris Lattner | 2faa2c7 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 1472 | } |
Chris Lattner | 3f4591c | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 1473 | |
Chris Lattner | f903fe1 | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1474 | // Okay, we're done now. If we added new values to the cache, re-sort it. |
Chris Lattner | 370aada | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 1475 | SortNonLocalDepInfoCache(*Cache, NumSortedEntries); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1476 | LLVM_DEBUG(AssertSorted(*Cache)); |
Chandler Carruth | b32febe | 2016-03-07 12:45:07 +0000 | [diff] [blame] | 1477 | return true; |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Piotr Padlewski | 153fe60 | 2018-05-18 22:40:34 +0000 | [diff] [blame] | 1480 | /// If P exists in CachedNonLocalPointerInfo or NonLocalDefsCache, remove it. |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1481 | void MemoryDependenceResults::RemoveCachedNonLocalPointerDependencies( |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1482 | ValueIsLoadPair P) { |
Piotr Padlewski | 153fe60 | 2018-05-18 22:40:34 +0000 | [diff] [blame] | 1483 | |
| 1484 | // Most of the time this cache is empty. |
| 1485 | if (!NonLocalDefsCache.empty()) { |
| 1486 | auto it = NonLocalDefsCache.find(P.getPointer()); |
| 1487 | if (it != NonLocalDefsCache.end()) { |
| 1488 | RemoveFromReverseMap(ReverseNonLocalDefsCache, |
| 1489 | it->second.getResult().getInst(), P.getPointer()); |
| 1490 | NonLocalDefsCache.erase(it); |
| 1491 | } |
| 1492 | |
| 1493 | if (auto *I = dyn_cast<Instruction>(P.getPointer())) { |
| 1494 | auto toRemoveIt = ReverseNonLocalDefsCache.find(I); |
| 1495 | if (toRemoveIt != ReverseNonLocalDefsCache.end()) { |
| 1496 | for (const auto &entry : toRemoveIt->second) |
| 1497 | NonLocalDefsCache.erase(entry); |
| 1498 | ReverseNonLocalDefsCache.erase(toRemoveIt); |
| 1499 | } |
| 1500 | } |
| 1501 | } |
| 1502 | |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1503 | CachedNonLocalPointerInfo::iterator It = NonLocalPointerDeps.find(P); |
| 1504 | if (It == NonLocalPointerDeps.end()) |
| 1505 | return; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1506 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1507 | // Remove all of the entries in the BB->val map. This involves removing |
| 1508 | // instructions from the reverse map. |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1509 | NonLocalDepInfo &PInfo = It->second.NonLocalDeps; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1510 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1511 | for (unsigned i = 0, e = PInfo.size(); i != e; ++i) { |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 1512 | Instruction *Target = PInfo[i].getResult().getInst(); |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1513 | if (!Target) |
| 1514 | continue; // Ignore non-local dep results. |
Chris Lattner | 0c31547 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 1515 | assert(Target->getParent() == PInfo[i].getBB()); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1516 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1517 | // Eliminating the dirty entry from 'Cache', so update the reverse info. |
Chris Lattner | 8eda11b | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1518 | RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P); |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1519 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1520 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1521 | // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo). |
| 1522 | NonLocalPointerDeps.erase(It); |
Chris Lattner | 2faa2c7 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1525 | void MemoryDependenceResults::invalidateCachedPointerInfo(Value *Ptr) { |
Chris Lattner | fa9f99a | 2008-12-09 22:06:23 +0000 | [diff] [blame] | 1526 | // If Ptr isn't really a pointer, just ignore it. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1527 | if (!Ptr->getType()->isPointerTy()) |
| 1528 | return; |
Chris Lattner | fa9f99a | 2008-12-09 22:06:23 +0000 | [diff] [blame] | 1529 | // Flush store info for the pointer. |
| 1530 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false)); |
| 1531 | // Flush load info for the pointer. |
| 1532 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true)); |
John Brawn | cd5f37f | 2018-07-31 14:19:29 +0000 | [diff] [blame] | 1533 | // Invalidate phis that use the pointer. |
| 1534 | PV.invalidateValue(Ptr); |
Chris Lattner | fa9f99a | 2008-12-09 22:06:23 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1537 | void MemoryDependenceResults::invalidateCachedPredecessors() { |
Daniel Berlin | b4e7a4a | 2015-04-21 21:11:50 +0000 | [diff] [blame] | 1538 | PredCache.clear(); |
Bob Wilson | 92cdb6e | 2010-02-16 19:51:59 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1541 | void MemoryDependenceResults::removeInstruction(Instruction *RemInst) { |
Chris Lattner | a25d3952 | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 1542 | // Walk through the Non-local dependencies, removing this one as the value |
| 1543 | // for any cached queries. |
Chris Lattner | 1b810bd | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1544 | NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst); |
| 1545 | if (NLDI != NonLocalDeps.end()) { |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 1546 | NonLocalDepInfo &BlockMap = NLDI->second.first; |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1547 | for (auto &Entry : BlockMap) |
| 1548 | if (Instruction *Inst = Entry.getResult().getInst()) |
Chris Lattner | de4440c | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 1549 | RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst); |
Chris Lattner | 1b810bd | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1550 | NonLocalDeps.erase(NLDI); |
| 1551 | } |
Owen Anderson | 086b2c4 | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | a25d3952 | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 1553 | // If we have a cached local dependence query for this instruction, remove it. |
Chris Lattner | de04e11 | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 1554 | LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst); |
| 1555 | if (LocalDepEntry != LocalDeps.end()) { |
Chris Lattner | ada1f87 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 1556 | // Remove us from DepInst's reverse set now that the local dep info is gone. |
Chris Lattner | de4440c | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 1557 | if (Instruction *Inst = LocalDepEntry->second.getInst()) |
| 1558 | RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst); |
Chris Lattner | ada1f87 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 1559 | |
Chris Lattner | 73c2545 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 1560 | // Remove this local dependency info. |
Chris Lattner | de04e11 | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 1561 | LocalDeps.erase(LocalDepEntry); |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1562 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1564 | // If we have any cached pointer dependencies on this instruction, remove |
| 1565 | // them. If the instruction has non-pointer type, then it can't be a pointer |
| 1566 | // base. |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1567 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1568 | // Remove it from both the load info and the store info. The instruction |
| 1569 | // can't be in either of these maps if it is non-pointer. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1570 | if (RemInst->getType()->isPointerTy()) { |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1571 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false)); |
| 1572 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true)); |
| 1573 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1574 | |
Chris Lattner | d3d9111 | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 1575 | // Loop over all of the things that depend on the instruction we're removing. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1576 | SmallVector<std::pair<Instruction *, Instruction *>, 8> ReverseDepsToAdd; |
Chris Lattner | 82b7034 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 1577 | |
| 1578 | // If we find RemInst as a clobber or Def in any of the maps for other values, |
| 1579 | // we need to replace its entry with a dirty version of the instruction after |
| 1580 | // it. If RemInst is a terminator, we use a null dirty value. |
| 1581 | // |
| 1582 | // Using a dirty version of the instruction after RemInst saves having to scan |
| 1583 | // the entire block to get to this point. |
| 1584 | MemDepResult NewDirtyVal; |
| 1585 | if (!RemInst->isTerminator()) |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1586 | NewDirtyVal = MemDepResult::getDirty(&*++RemInst->getIterator()); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1587 | |
Chris Lattner | 9f1988ab | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 1588 | ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst); |
| 1589 | if (ReverseDepIt != ReverseLocalDeps.end()) { |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1590 | // RemInst can't be the terminator if it has local stuff depending on it. |
Chandler Carruth | 9ae926b | 2018-08-26 09:51:22 +0000 | [diff] [blame] | 1591 | assert(!ReverseDepIt->second.empty() && !RemInst->isTerminator() && |
Chris Lattner | ada1f87 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 1592 | "Nothing can locally depend on a terminator"); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1593 | |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1594 | for (Instruction *InstDependingOnRemInst : ReverseDepIt->second) { |
Chris Lattner | 1b810bd | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1595 | assert(InstDependingOnRemInst != RemInst && |
| 1596 | "Already removed our local dep info"); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1597 | |
Chris Lattner | 82b7034 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 1598 | LocalDeps[InstDependingOnRemInst] = NewDirtyVal; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | ada1f87 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 1600 | // Make sure to remember that new things depend on NewDepInst. |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1601 | assert(NewDirtyVal.getInst() && |
| 1602 | "There is no way something else can have " |
Chris Lattner | 82b7034 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 1603 | "a local dep on this if it is a terminator!"); |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1604 | ReverseDepsToAdd.push_back( |
| 1605 | std::make_pair(NewDirtyVal.getInst(), InstDependingOnRemInst)); |
Chris Lattner | d3d9111 | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 1606 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1607 | |
Chris Lattner | 63bd586 | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 1608 | ReverseLocalDeps.erase(ReverseDepIt); |
| 1609 | |
| 1610 | // Add new reverse deps after scanning the set, to avoid invalidating the |
| 1611 | // 'ReverseDeps' reference. |
| 1612 | while (!ReverseDepsToAdd.empty()) { |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1613 | ReverseLocalDeps[ReverseDepsToAdd.back().first].insert( |
| 1614 | ReverseDepsToAdd.back().second); |
Chris Lattner | 63bd586 | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 1615 | ReverseDepsToAdd.pop_back(); |
| 1616 | } |
Owen Anderson | c0daf5f | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 1617 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1618 | |
Chris Lattner | 9f1988ab | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 1619 | ReverseDepIt = ReverseNonLocalDeps.find(RemInst); |
| 1620 | if (ReverseDepIt != ReverseNonLocalDeps.end()) { |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1621 | for (Instruction *I : ReverseDepIt->second) { |
| 1622 | assert(I != RemInst && "Already removed NonLocalDep info for RemInst"); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1623 | |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1624 | PerInstNLInfo &INLD = NonLocalDeps[I]; |
Chris Lattner | 4410427 | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 1625 | // The information is now dirty! |
Chris Lattner | 7e61daf | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 1626 | INLD.second = true; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1627 | |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1628 | for (auto &Entry : INLD.first) { |
| 1629 | if (Entry.getResult().getInst() != RemInst) |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1630 | continue; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1631 | |
Chris Lattner | 1b810bd | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1632 | // Convert to a dirty entry for the subsequent instruction. |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1633 | Entry.setResult(NewDirtyVal); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | 82b7034 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 1635 | if (Instruction *NextI = NewDirtyVal.getInst()) |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1636 | ReverseDepsToAdd.push_back(std::make_pair(NextI, I)); |
Chris Lattner | 1b810bd | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1637 | } |
| 1638 | } |
Chris Lattner | 63bd586 | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 1639 | |
| 1640 | ReverseNonLocalDeps.erase(ReverseDepIt); |
| 1641 | |
Chris Lattner | e7d7e13 | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 1642 | // Add new reverse deps after scanning the set, to avoid invalidating 'Set' |
| 1643 | while (!ReverseDepsToAdd.empty()) { |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1644 | ReverseNonLocalDeps[ReverseDepsToAdd.back().first].insert( |
| 1645 | ReverseDepsToAdd.back().second); |
Chris Lattner | e7d7e13 | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 1646 | ReverseDepsToAdd.pop_back(); |
| 1647 | } |
Owen Anderson | 5f208be | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 1648 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1649 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1650 | // If the instruction is in ReverseNonLocalPtrDeps then it appears as a |
| 1651 | // value in the NonLocalPointerDeps info. |
| 1652 | ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt = |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1653 | ReverseNonLocalPtrDeps.find(RemInst); |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1654 | if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) { |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1655 | SmallVector<std::pair<Instruction *, ValueIsLoadPair>, 8> |
| 1656 | ReversePtrDepsToAdd; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1657 | |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1658 | for (ValueIsLoadPair P : ReversePtrDepIt->second) { |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1659 | assert(P.getPointer() != RemInst && |
| 1660 | "Already removed NonLocalPointerDeps info for RemInst"); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1661 | |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1662 | NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].NonLocalDeps; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | 5ed409e | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1664 | // The cache is not valid for any specific block anymore. |
Dan Gohman | 2348393 | 2010-09-22 21:41:02 +0000 | [diff] [blame] | 1665 | NonLocalPointerDeps[P].Pair = BBSkipFirstBlockPair(); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1666 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1667 | // Update any entries for RemInst to use the instruction after it. |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1668 | for (auto &Entry : NLPDI) { |
| 1669 | if (Entry.getResult().getInst() != RemInst) |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1670 | continue; |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1671 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1672 | // Convert to a dirty entry for the subsequent instruction. |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1673 | Entry.setResult(NewDirtyVal); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1674 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1675 | if (Instruction *NewDirtyInst = NewDirtyVal.getInst()) |
| 1676 | ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P)); |
| 1677 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1678 | |
Chris Lattner | 3f4591c | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 1679 | // Re-sort the NonLocalDepInfo. Changing the dirty entry to its |
| 1680 | // subsequent value may invalidate the sortedness. |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 1681 | llvm::sort(NLPDI); |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1682 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1683 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1684 | ReverseNonLocalPtrDeps.erase(ReversePtrDepIt); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1685 | |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1686 | while (!ReversePtrDepsToAdd.empty()) { |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1687 | ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first].insert( |
| 1688 | ReversePtrDepsToAdd.back().second); |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1689 | ReversePtrDepsToAdd.pop_back(); |
| 1690 | } |
| 1691 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1692 | |
John Brawn | cd5f37f | 2018-07-31 14:19:29 +0000 | [diff] [blame] | 1693 | // Invalidate phis that use the removed instruction. |
| 1694 | PV.invalidateValue(RemInst); |
| 1695 | |
Chris Lattner | 1b810bd | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1696 | assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1697 | LLVM_DEBUG(verifyRemoved(RemInst)); |
Owen Anderson | c0daf5f | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 1698 | } |
Chandler Carruth | 40e21f2 | 2016-03-07 12:30:06 +0000 | [diff] [blame] | 1699 | |
| 1700 | /// Verify that the specified instruction does not occur in our internal data |
| 1701 | /// structures. |
| 1702 | /// |
| 1703 | /// This function verifies by asserting in debug builds. |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1704 | void MemoryDependenceResults::verifyRemoved(Instruction *D) const { |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1705 | #ifndef NDEBUG |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1706 | for (const auto &DepKV : LocalDeps) { |
| 1707 | assert(DepKV.first != D && "Inst occurs in data structures"); |
| 1708 | assert(DepKV.second.getInst() != D && "Inst occurs in data structures"); |
Chris Lattner | b8ec75b | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1709 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1710 | |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1711 | for (const auto &DepKV : NonLocalPointerDeps) { |
| 1712 | assert(DepKV.first.getPointer() != D && "Inst occurs in NLPD map key"); |
| 1713 | for (const auto &Entry : DepKV.second.NonLocalDeps) |
| 1714 | assert(Entry.getResult().getInst() != D && "Inst occurs as NLPD value"); |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1715 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1716 | |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1717 | for (const auto &DepKV : NonLocalDeps) { |
| 1718 | assert(DepKV.first != D && "Inst occurs in data structures"); |
| 1719 | const PerInstNLInfo &INLD = DepKV.second; |
| 1720 | for (const auto &Entry : INLD.first) |
| 1721 | assert(Entry.getResult().getInst() != D && |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1722 | "Inst occurs in data structures"); |
Chris Lattner | b8ec75b | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1723 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1724 | |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1725 | for (const auto &DepKV : ReverseLocalDeps) { |
| 1726 | assert(DepKV.first != D && "Inst occurs in data structures"); |
| 1727 | for (Instruction *Inst : DepKV.second) |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1728 | assert(Inst != D && "Inst occurs in data structures"); |
Chris Lattner | 1b810bd | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1729 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1730 | |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1731 | for (const auto &DepKV : ReverseNonLocalDeps) { |
| 1732 | assert(DepKV.first != D && "Inst occurs in data structures"); |
| 1733 | for (Instruction *Inst : DepKV.second) |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1734 | assert(Inst != D && "Inst occurs in data structures"); |
Chris Lattner | 1b810bd | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1735 | } |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1736 | |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1737 | for (const auto &DepKV : ReverseNonLocalPtrDeps) { |
| 1738 | assert(DepKV.first != D && "Inst occurs in rev NLPD map"); |
Jakub Staszak | b0a7eed | 2013-03-20 21:47:51 +0000 | [diff] [blame] | 1739 | |
Chandler Carruth | af8321e | 2016-03-07 15:12:57 +0000 | [diff] [blame] | 1740 | for (ValueIsLoadPair P : DepKV.second) |
Chandler Carruth | 60fb1b4 | 2016-03-07 10:19:30 +0000 | [diff] [blame] | 1741 | assert(P != ValueIsLoadPair(D, false) && P != ValueIsLoadPair(D, true) && |
Chris Lattner | a28355d | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1742 | "Inst occurs in ReverseNonLocalPtrDeps map"); |
| 1743 | } |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1744 | #endif |
Chris Lattner | b8ec75b | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1745 | } |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1746 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 1747 | AnalysisKey MemoryDependenceAnalysis::Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 1748 | |
Fedor Sergeev | 92e160a | 2019-08-10 01:23:38 +0000 | [diff] [blame] | 1749 | MemoryDependenceAnalysis::MemoryDependenceAnalysis() |
| 1750 | : DefaultBlockScanLimit(BlockScanLimit) {} |
| 1751 | |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1752 | MemoryDependenceResults |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 1753 | MemoryDependenceAnalysis::run(Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 1754 | auto &AA = AM.getResult<AAManager>(F); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1755 | auto &AC = AM.getResult<AssumptionAnalysis>(F); |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 1756 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
Chandler Carruth | aef32bd | 2016-03-11 13:46:00 +0000 | [diff] [blame] | 1757 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
John Brawn | cd5f37f | 2018-07-31 14:19:29 +0000 | [diff] [blame] | 1758 | auto &PV = AM.getResult<PhiValuesAnalysis>(F); |
Fedor Sergeev | 92e160a | 2019-08-10 01:23:38 +0000 | [diff] [blame] | 1759 | return MemoryDependenceResults(AA, AC, TLI, DT, PV, DefaultBlockScanLimit); |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | char MemoryDependenceWrapperPass::ID = 0; |
| 1763 | |
| 1764 | INITIALIZE_PASS_BEGIN(MemoryDependenceWrapperPass, "memdep", |
| 1765 | "Memory Dependence Analysis", false, true) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1766 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1767 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Chandler Carruth | aef32bd | 2016-03-11 13:46:00 +0000 | [diff] [blame] | 1768 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1769 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
John Brawn | cd5f37f | 2018-07-31 14:19:29 +0000 | [diff] [blame] | 1770 | INITIALIZE_PASS_DEPENDENCY(PhiValuesWrapperPass) |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1771 | INITIALIZE_PASS_END(MemoryDependenceWrapperPass, "memdep", |
| 1772 | "Memory Dependence Analysis", false, true) |
| 1773 | |
| 1774 | MemoryDependenceWrapperPass::MemoryDependenceWrapperPass() : FunctionPass(ID) { |
| 1775 | initializeMemoryDependenceWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 1776 | } |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 1777 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1778 | MemoryDependenceWrapperPass::~MemoryDependenceWrapperPass() = default; |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1779 | |
| 1780 | void MemoryDependenceWrapperPass::releaseMemory() { |
| 1781 | MemDep.reset(); |
| 1782 | } |
| 1783 | |
| 1784 | void MemoryDependenceWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1785 | AU.setPreservesAll(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1786 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | aef32bd | 2016-03-11 13:46:00 +0000 | [diff] [blame] | 1787 | AU.addRequired<DominatorTreeWrapperPass>(); |
John Brawn | cd5f37f | 2018-07-31 14:19:29 +0000 | [diff] [blame] | 1788 | AU.addRequired<PhiValuesWrapperPass>(); |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1789 | AU.addRequiredTransitive<AAResultsWrapperPass>(); |
| 1790 | AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>(); |
| 1791 | } |
| 1792 | |
Chandler Carruth | e14524c | 2016-12-27 19:33:04 +0000 | [diff] [blame] | 1793 | bool MemoryDependenceResults::invalidate(Function &F, const PreservedAnalyses &PA, |
| 1794 | FunctionAnalysisManager::Invalidator &Inv) { |
| 1795 | // Check whether our analysis is preserved. |
| 1796 | auto PAC = PA.getChecker<MemoryDependenceAnalysis>(); |
| 1797 | if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>()) |
| 1798 | // If not, give up now. |
| 1799 | return true; |
| 1800 | |
| 1801 | // Check whether the analyses we depend on became invalid for any reason. |
| 1802 | if (Inv.invalidate<AAManager>(F, PA) || |
| 1803 | Inv.invalidate<AssumptionAnalysis>(F, PA) || |
John Brawn | cd5f37f | 2018-07-31 14:19:29 +0000 | [diff] [blame] | 1804 | Inv.invalidate<DominatorTreeAnalysis>(F, PA) || |
| 1805 | Inv.invalidate<PhiValuesAnalysis>(F, PA)) |
Chandler Carruth | e14524c | 2016-12-27 19:33:04 +0000 | [diff] [blame] | 1806 | return true; |
| 1807 | |
| 1808 | // Otherwise this analysis result remains valid. |
| 1809 | return false; |
| 1810 | } |
| 1811 | |
Bob Haarman | 3db1764 | 2016-08-26 16:34:27 +0000 | [diff] [blame] | 1812 | unsigned MemoryDependenceResults::getDefaultBlockScanLimit() const { |
Fedor Sergeev | 92e160a | 2019-08-10 01:23:38 +0000 | [diff] [blame] | 1813 | return DefaultBlockScanLimit; |
Bob Haarman | 3db1764 | 2016-08-26 16:34:27 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1816 | bool MemoryDependenceWrapperPass::runOnFunction(Function &F) { |
| 1817 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1818 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 1819 | auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); |
Chandler Carruth | aef32bd | 2016-03-11 13:46:00 +0000 | [diff] [blame] | 1820 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
John Brawn | cd5f37f | 2018-07-31 14:19:29 +0000 | [diff] [blame] | 1821 | auto &PV = getAnalysis<PhiValuesWrapperPass>().getResult(); |
Fedor Sergeev | 92e160a | 2019-08-10 01:23:38 +0000 | [diff] [blame] | 1822 | MemDep.emplace(AA, AC, TLI, DT, PV, BlockScanLimit); |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 1823 | return false; |
| 1824 | } |