MemoryDependenceAnalysis.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements an analysis that determines, for a given memory
  11. // operation, what preceding memory operations it depends on. It builds on
  12. // alias analysis information, and tries to provide a lazy, caching interface to
  13. // a common kind of alias information query.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Analysis/MemoryDependenceAnalysis.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/Statistic.h"
  19. #include "llvm/Analysis/AliasAnalysis.h"
  20. #include "llvm/Analysis/InstructionSimplify.h"
  21. #include "llvm/Analysis/MemoryBuiltins.h"
  22. #include "llvm/Analysis/PHITransAddr.h"
  23. #include "llvm/Analysis/ValueTracking.h"
  24. #include "llvm/IR/DataLayout.h"
  25. #include "llvm/IR/Dominators.h"
  26. #include "llvm/IR/Function.h"
  27. #include "llvm/IR/Instructions.h"
  28. #include "llvm/IR/IntrinsicInst.h"
  29. #include "llvm/IR/LLVMContext.h"
  30. #include "llvm/IR/PredIteratorCache.h"
  31. #include "llvm/Support/Debug.h"
  32. using namespace llvm;
  33. #define DEBUG_TYPE "memdep"
  34. STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
  35. STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
  36. STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
  37. STATISTIC(NumCacheNonLocalPtr,
  38. "Number of fully cached non-local ptr responses");
  39. STATISTIC(NumCacheDirtyNonLocalPtr,
  40. "Number of cached, but dirty, non-local ptr responses");
  41. STATISTIC(NumUncacheNonLocalPtr,
  42. "Number of uncached non-local ptr responses");
  43. STATISTIC(NumCacheCompleteNonLocalPtr,
  44. "Number of block queries that were completely cached");
  45. // Limit for the number of instructions to scan in a block.
  46. static const int BlockScanLimit = 100;
  47. char MemoryDependenceAnalysis::ID = 0;
  48. // Register this pass...
  49. INITIALIZE_PASS_BEGIN(MemoryDependenceAnalysis, "memdep",
  50. "Memory Dependence Analysis", false, true)
  51. INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
  52. INITIALIZE_PASS_END(MemoryDependenceAnalysis, "memdep",
  53. "Memory Dependence Analysis", false, true)
  54. MemoryDependenceAnalysis::MemoryDependenceAnalysis()
  55. : FunctionPass(ID), PredCache() {
  56. initializeMemoryDependenceAnalysisPass(*PassRegistry::getPassRegistry());
  57. }
  58. MemoryDependenceAnalysis::~MemoryDependenceAnalysis() {
  59. }
  60. /// Clean up memory in between runs
  61. void MemoryDependenceAnalysis::releaseMemory() {
  62. LocalDeps.clear();
  63. NonLocalDeps.clear();
  64. NonLocalPointerDeps.clear();
  65. ReverseLocalDeps.clear();
  66. ReverseNonLocalDeps.clear();
  67. ReverseNonLocalPtrDeps.clear();
  68. PredCache->clear();
  69. }
  70. /// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
  71. ///
  72. void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
  73. AU.setPreservesAll();
  74. AU.addRequiredTransitive<AliasAnalysis>();
  75. }
  76. bool MemoryDependenceAnalysis::runOnFunction(Function &) {
  77. AA = &getAnalysis<AliasAnalysis>();
  78. DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
  79. DL = DLP ? &DLP->getDataLayout() : nullptr;
  80. DominatorTreeWrapperPass *DTWP =
  81. getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  82. DT = DTWP ? &DTWP->getDomTree() : nullptr;
  83. if (!PredCache)
  84. PredCache.reset(new PredIteratorCache());
  85. return false;
  86. }
  87. /// RemoveFromReverseMap - This is a helper function that removes Val from
  88. /// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry.
  89. template <typename KeyTy>
  90. static void RemoveFromReverseMap(DenseMap<Instruction*,
  91. SmallPtrSet<KeyTy, 4> > &ReverseMap,
  92. Instruction *Inst, KeyTy Val) {
  93. typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator
  94. InstIt = ReverseMap.find(Inst);
  95. assert(InstIt != ReverseMap.end() && "Reverse map out of sync?");
  96. bool Found = InstIt->second.erase(Val);
  97. assert(Found && "Invalid reverse map!"); (void)Found;
  98. if (InstIt->second.empty())
  99. ReverseMap.erase(InstIt);
  100. }
  101. /// GetLocation - If the given instruction references a specific memory
  102. /// location, fill in Loc with the details, otherwise set Loc.Ptr to null.
  103. /// Return a ModRefInfo value describing the general behavior of the
  104. /// instruction.
  105. static
  106. AliasAnalysis::ModRefResult GetLocation(const Instruction *Inst,
  107. AliasAnalysis::Location &Loc,
  108. AliasAnalysis *AA) {
  109. if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
  110. if (LI->isUnordered()) {
  111. Loc = AA->getLocation(LI);
  112. return AliasAnalysis::Ref;
  113. }
  114. if (LI->getOrdering() == Monotonic) {
  115. Loc = AA->getLocation(LI);
  116. return AliasAnalysis::ModRef;
  117. }
  118. Loc = AliasAnalysis::Location();
  119. return AliasAnalysis::ModRef;
  120. }
  121. if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
  122. if (SI->isUnordered()) {
  123. Loc = AA->getLocation(SI);
  124. return AliasAnalysis::Mod;
  125. }
  126. if (SI->getOrdering() == Monotonic) {
  127. Loc = AA->getLocation(SI);
  128. return AliasAnalysis::ModRef;
  129. }
  130. Loc = AliasAnalysis::Location();
  131. return AliasAnalysis::ModRef;
  132. }
  133. if (const VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
  134. Loc = AA->getLocation(V);
  135. return AliasAnalysis::ModRef;
  136. }
  137. if (const CallInst *CI = isFreeCall(Inst, AA->getTargetLibraryInfo())) {
  138. // calls to free() deallocate the entire structure
  139. Loc = AliasAnalysis::Location(CI->getArgOperand(0));
  140. return AliasAnalysis::Mod;
  141. }
  142. if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
  143. AAMDNodes AAInfo;
  144. switch (II->getIntrinsicID()) {
  145. case Intrinsic::lifetime_start:
  146. case Intrinsic::lifetime_end:
  147. case Intrinsic::invariant_start:
  148. II->getAAMetadata(AAInfo);
  149. Loc = AliasAnalysis::Location(II->getArgOperand(1),
  150. cast<ConstantInt>(II->getArgOperand(0))
  151. ->getZExtValue(), AAInfo);
  152. // These intrinsics don't really modify the memory, but returning Mod
  153. // will allow them to be handled conservatively.
  154. return AliasAnalysis::Mod;
  155. case Intrinsic::invariant_end:
  156. II->getAAMetadata(AAInfo);
  157. Loc = AliasAnalysis::Location(II->getArgOperand(2),
  158. cast<ConstantInt>(II->getArgOperand(1))
  159. ->getZExtValue(), AAInfo);
  160. // These intrinsics don't really modify the memory, but returning Mod
  161. // will allow them to be handled conservatively.
  162. return AliasAnalysis::Mod;
  163. default:
  164. break;
  165. }
  166. }
  167. // Otherwise, just do the coarse-grained thing that always works.
  168. if (Inst->mayWriteToMemory())
  169. return AliasAnalysis::ModRef;
  170. if (Inst->mayReadFromMemory())
  171. return AliasAnalysis::Ref;
  172. return AliasAnalysis::NoModRef;
  173. }
  174. /// getCallSiteDependencyFrom - Private helper for finding the local
  175. /// dependencies of a call site.
  176. MemDepResult MemoryDependenceAnalysis::
  177. getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall,
  178. BasicBlock::iterator ScanIt, BasicBlock *BB) {
  179. unsigned Limit = BlockScanLimit;
  180. // Walk backwards through the block, looking for dependencies
  181. while (ScanIt != BB->begin()) {
  182. // Limit the amount of scanning we do so we don't end up with quadratic
  183. // running time on extreme testcases.
  184. --Limit;
  185. if (!Limit)
  186. return MemDepResult::getUnknown();
  187. Instruction *Inst = --ScanIt;
  188. // If this inst is a memory op, get the pointer it accessed
  189. AliasAnalysis::Location Loc;
  190. AliasAnalysis::ModRefResult MR = GetLocation(Inst, Loc, AA);
  191. if (Loc.Ptr) {
  192. // A simple instruction.
  193. if (AA->getModRefInfo(CS, Loc) != AliasAnalysis::NoModRef)
  194. return MemDepResult::getClobber(Inst);
  195. continue;
  196. }
  197. if (CallSite InstCS = cast<Value>(Inst)) {
  198. // Debug intrinsics don't cause dependences.
  199. if (isa<DbgInfoIntrinsic>(Inst)) continue;
  200. // If these two calls do not interfere, look past it.
  201. switch (AA->getModRefInfo(CS, InstCS)) {
  202. case AliasAnalysis::NoModRef:
  203. // If the two calls are the same, return InstCS as a Def, so that
  204. // CS can be found redundant and eliminated.
  205. if (isReadOnlyCall && !(MR & AliasAnalysis::Mod) &&
  206. CS.getInstruction()->isIdenticalToWhenDefined(Inst))
  207. return MemDepResult::getDef(Inst);
  208. // Otherwise if the two calls don't interact (e.g. InstCS is readnone)
  209. // keep scanning.
  210. continue;
  211. default:
  212. return MemDepResult::getClobber(Inst);
  213. }
  214. }
  215. // If we could not obtain a pointer for the instruction and the instruction
  216. // touches memory then assume that this is a dependency.
  217. if (MR != AliasAnalysis::NoModRef)
  218. return MemDepResult::getClobber(Inst);
  219. }
  220. // No dependence found. If this is the entry block of the function, it is
  221. // unknown, otherwise it is non-local.
  222. if (BB != &BB->getParent()->getEntryBlock())
  223. return MemDepResult::getNonLocal();
  224. return MemDepResult::getNonFuncLocal();
  225. }
  226. /// isLoadLoadClobberIfExtendedToFullWidth - Return true if LI is a load that
  227. /// would fully overlap MemLoc if done as a wider legal integer load.
  228. ///
  229. /// MemLocBase, MemLocOffset are lazily computed here the first time the
  230. /// base/offs of memloc is needed.
  231. static bool
  232. isLoadLoadClobberIfExtendedToFullWidth(const AliasAnalysis::Location &MemLoc,
  233. const Value *&MemLocBase,
  234. int64_t &MemLocOffs,
  235. const LoadInst *LI,
  236. const DataLayout *DL) {
  237. // If we have no target data, we can't do this.
  238. if (!DL) return false;
  239. // If we haven't already computed the base/offset of MemLoc, do so now.
  240. if (!MemLocBase)
  241. MemLocBase = GetPointerBaseWithConstantOffset(MemLoc.Ptr, MemLocOffs, DL);
  242. unsigned Size = MemoryDependenceAnalysis::
  243. getLoadLoadClobberFullWidthSize(MemLocBase, MemLocOffs, MemLoc.Size,
  244. LI, *DL);
  245. return Size != 0;
  246. }
  247. /// getLoadLoadClobberFullWidthSize - This is a little bit of analysis that
  248. /// looks at a memory location for a load (specified by MemLocBase, Offs,
  249. /// and Size) and compares it against a load. If the specified load could
  250. /// be safely widened to a larger integer load that is 1) still efficient,
  251. /// 2) safe for the target, and 3) would provide the specified memory
  252. /// location value, then this function returns the size in bytes of the
  253. /// load width to use. If not, this returns zero.
  254. unsigned MemoryDependenceAnalysis::
  255. getLoadLoadClobberFullWidthSize(const Value *MemLocBase, int64_t MemLocOffs,
  256. unsigned MemLocSize, const LoadInst *LI,
  257. const DataLayout &DL) {
  258. // We can only extend simple integer loads.
  259. if (!isa<IntegerType>(LI->getType()) || !LI->isSimple()) return 0;
  260. // Load widening is hostile to ThreadSanitizer: it may cause false positives
  261. // or make the reports more cryptic (access sizes are wrong).
  262. if (LI->getParent()->getParent()->getAttributes().
  263. hasAttribute(AttributeSet::FunctionIndex, Attribute::SanitizeThread))
  264. return 0;
  265. // Get the base of this load.
  266. int64_t LIOffs = 0;
  267. const Value *LIBase =
  268. GetPointerBaseWithConstantOffset(LI->getPointerOperand(), LIOffs, &DL);
  269. // If the two pointers are not based on the same pointer, we can't tell that
  270. // they are related.
  271. if (LIBase != MemLocBase) return 0;
  272. // Okay, the two values are based on the same pointer, but returned as
  273. // no-alias. This happens when we have things like two byte loads at "P+1"
  274. // and "P+3". Check to see if increasing the size of the "LI" load up to its
  275. // alignment (or the largest native integer type) will allow us to load all
  276. // the bits required by MemLoc.
  277. // If MemLoc is before LI, then no widening of LI will help us out.
  278. if (MemLocOffs < LIOffs) return 0;
  279. // Get the alignment of the load in bytes. We assume that it is safe to load
  280. // any legal integer up to this size without a problem. For example, if we're
  281. // looking at an i8 load on x86-32 that is known 1024 byte aligned, we can
  282. // widen it up to an i32 load. If it is known 2-byte aligned, we can widen it
  283. // to i16.
  284. unsigned LoadAlign = LI->getAlignment();
  285. int64_t MemLocEnd = MemLocOffs+MemLocSize;
  286. // If no amount of rounding up will let MemLoc fit into LI, then bail out.
  287. if (LIOffs+LoadAlign < MemLocEnd) return 0;
  288. // This is the size of the load to try. Start with the next larger power of
  289. // two.
  290. unsigned NewLoadByteSize = LI->getType()->getPrimitiveSizeInBits()/8U;
  291. NewLoadByteSize = NextPowerOf2(NewLoadByteSize);
  292. while (1) {
  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 ||
  296. !DL.fitsInLegalInteger(NewLoadByteSize*8))
  297. return 0;
  298. if (LIOffs+NewLoadByteSize > MemLocEnd &&
  299. LI->getParent()->getParent()->getAttributes().
  300. hasAttribute(AttributeSet::FunctionIndex, Attribute::SanitizeAddress))
  301. // We will be reading past the location accessed by the original program.
  302. // While this is safe in a regular build, Address Safety analysis tools
  303. // may start reporting false warnings. So, don't do widening.
  304. return 0;
  305. // If a load of this width would include all of MemLoc, then we succeed.
  306. if (LIOffs+NewLoadByteSize >= MemLocEnd)
  307. return NewLoadByteSize;
  308. NewLoadByteSize <<= 1;
  309. }
  310. }
  311. /// getPointerDependencyFrom - Return the instruction on which a memory
  312. /// location depends. If isLoad is true, this routine ignores may-aliases with
  313. /// read-only operations. If isLoad is false, this routine ignores may-aliases
  314. /// with reads from read-only locations. If possible, pass the query
  315. /// instruction as well; this function may take advantage of the metadata
  316. /// annotated to the query instruction to refine the result.
  317. MemDepResult MemoryDependenceAnalysis::
  318. getPointerDependencyFrom(const AliasAnalysis::Location &MemLoc, bool isLoad,
  319. BasicBlock::iterator ScanIt, BasicBlock *BB,
  320. Instruction *QueryInst) {
  321. const Value *MemLocBase = nullptr;
  322. int64_t MemLocOffset = 0;
  323. unsigned Limit = BlockScanLimit;
  324. bool isInvariantLoad = false;
  325. // We must be careful with atomic accesses, as they may allow another thread
  326. // to touch this location, cloberring it. We are conservative: if the
  327. // QueryInst is not a simple (non-atomic) memory access, we automatically
  328. // return getClobber.
  329. // If it is simple, we know based on the results of
  330. // "Compiler testing via a theory of sound optimisations in the C11/C++11
  331. // memory model" in PLDI 2013, that a non-atomic location can only be
  332. // clobbered between a pair of a release and an acquire action, with no
  333. // access to the location in between.
  334. // Here is an example for giving the general intuition behind this rule.
  335. // In the following code:
  336. // store x 0;
  337. // release action; [1]
  338. // acquire action; [4]
  339. // %val = load x;
  340. // It is unsafe to replace %val by 0 because another thread may be running:
  341. // acquire action; [2]
  342. // store x 42;
  343. // release action; [3]
  344. // with synchronization from 1 to 2 and from 3 to 4, resulting in %val
  345. // being 42. A key property of this program however is that if either
  346. // 1 or 4 were missing, there would be a race between the store of 42
  347. // either the store of 0 or the load (making the whole progam racy).
  348. // The paper mentionned above shows that the same property is respected
  349. // by every program that can detect any optimisation of that kind: either
  350. // it is racy (undefined) or there is a release followed by an acquire
  351. // between the pair of accesses under consideration.
  352. bool HasSeenAcquire = false;
  353. if (isLoad && QueryInst) {
  354. LoadInst *LI = dyn_cast<LoadInst>(QueryInst);
  355. if (LI && LI->getMetadata(LLVMContext::MD_invariant_load) != nullptr)
  356. isInvariantLoad = true;
  357. }
  358. // Walk backwards through the basic block, looking for dependencies.
  359. while (ScanIt != BB->begin()) {
  360. Instruction *Inst = --ScanIt;
  361. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))
  362. // Debug intrinsics don't (and can't) cause dependencies.
  363. if (isa<DbgInfoIntrinsic>(II)) continue;
  364. // Limit the amount of scanning we do so we don't end up with quadratic
  365. // running time on extreme testcases.
  366. --Limit;
  367. if (!Limit)
  368. return MemDepResult::getUnknown();
  369. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
  370. // If we reach a lifetime begin or end marker, then the query ends here
  371. // because the value is undefined.
  372. if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
  373. // FIXME: This only considers queries directly on the invariant-tagged
  374. // pointer, not on query pointers that are indexed off of them. It'd
  375. // be nice to handle that at some point (the right approach is to use
  376. // GetPointerBaseWithConstantOffset).
  377. if (AA->isMustAlias(AliasAnalysis::Location(II->getArgOperand(1)),
  378. MemLoc))
  379. return MemDepResult::getDef(II);
  380. continue;
  381. }
  382. }
  383. // Values depend on loads if the pointers are must aliased. This means that
  384. // a load depends on another must aliased load from the same value.
  385. // One exception is atomic loads: a value can depend on an atomic load that it
  386. // does not alias with when this atomic load indicates that another thread may
  387. // be accessing the location.
  388. if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
  389. // Atomic loads have complications involved.
  390. // A Monotonic (or higher) load is OK if the query inst is itself not atomic.
  391. // An Acquire (or higher) load sets the HasSeenAcquire flag, so that any
  392. // release store will know to return getClobber.
  393. // FIXME: This is overly conservative.
  394. if (!LI->isUnordered()) {
  395. if (!QueryInst)
  396. return MemDepResult::getClobber(LI);
  397. if (auto *QueryLI = dyn_cast<LoadInst>(QueryInst))
  398. if (!QueryLI->isSimple())
  399. return MemDepResult::getClobber(LI);
  400. if (auto *QuerySI = dyn_cast<StoreInst>(QueryInst))
  401. if (!QuerySI->isSimple())
  402. return MemDepResult::getClobber(LI);
  403. if (isAtLeastAcquire(LI->getOrdering()))
  404. HasSeenAcquire = true;
  405. }
  406. // FIXME: this is overly conservative.
  407. // While volatile access cannot be eliminated, they do not have to clobber
  408. // non-aliasing locations, as normal accesses can for example be reordered
  409. // with volatile accesses.
  410. if (LI->isVolatile())
  411. return MemDepResult::getClobber(LI);
  412. AliasAnalysis::Location LoadLoc = AA->getLocation(LI);
  413. // If we found a pointer, check if it could be the same as our pointer.
  414. AliasAnalysis::AliasResult R = AA->alias(LoadLoc, MemLoc);
  415. if (isLoad) {
  416. if (R == AliasAnalysis::NoAlias) {
  417. // If this is an over-aligned integer load (for example,
  418. // "load i8* %P, align 4") see if it would obviously overlap with the
  419. // queried location if widened to a larger load (e.g. if the queried
  420. // location is 1 byte at P+1). If so, return it as a load/load
  421. // clobber result, allowing the client to decide to widen the load if
  422. // it wants to.
  423. if (IntegerType *ITy = dyn_cast<IntegerType>(LI->getType()))
  424. if (LI->getAlignment()*8 > ITy->getPrimitiveSizeInBits() &&
  425. isLoadLoadClobberIfExtendedToFullWidth(MemLoc, MemLocBase,
  426. MemLocOffset, LI, DL))
  427. return MemDepResult::getClobber(Inst);
  428. continue;
  429. }
  430. // Must aliased loads are defs of each other.
  431. if (R == AliasAnalysis::MustAlias)
  432. return MemDepResult::getDef(Inst);
  433. #if 0 // FIXME: Temporarily disabled. GVN is cleverly rewriting loads
  434. // in terms of clobbering loads, but since it does this by looking
  435. // at the clobbering load directly, it doesn't know about any
  436. // phi translation that may have happened along the way.
  437. // If we have a partial alias, then return this as a clobber for the
  438. // client to handle.
  439. if (R == AliasAnalysis::PartialAlias)
  440. return MemDepResult::getClobber(Inst);
  441. #endif
  442. // Random may-alias loads don't depend on each other without a
  443. // dependence.
  444. continue;
  445. }
  446. // Stores don't depend on other no-aliased accesses.
  447. if (R == AliasAnalysis::NoAlias)
  448. continue;
  449. // Stores don't alias loads from read-only memory.
  450. if (AA->pointsToConstantMemory(LoadLoc))
  451. continue;
  452. // Stores depend on may/must aliased loads.
  453. return MemDepResult::getDef(Inst);
  454. }
  455. if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
  456. // Atomic stores have complications involved.
  457. // A Monotonic store is OK if the query inst is itself not atomic.
  458. // A Release (or higher) store further requires that no acquire load
  459. // has been seen.
  460. // FIXME: This is overly conservative.
  461. if (!SI->isUnordered()) {
  462. if (!QueryInst)
  463. return MemDepResult::getClobber(SI);
  464. if (auto *QueryLI = dyn_cast<LoadInst>(QueryInst))
  465. if (!QueryLI->isSimple())
  466. return MemDepResult::getClobber(SI);
  467. if (auto *QuerySI = dyn_cast<StoreInst>(QueryInst))
  468. if (!QuerySI->isSimple())
  469. return MemDepResult::getClobber(SI);
  470. if (HasSeenAcquire && isAtLeastRelease(SI->getOrdering()))
  471. return MemDepResult::getClobber(SI);
  472. }
  473. // FIXME: this is overly conservative.
  474. // While volatile access cannot be eliminated, they do not have to clobber
  475. // non-aliasing locations, as normal accesses can for example be reordered
  476. // with volatile accesses.
  477. if (SI->isVolatile())
  478. return MemDepResult::getClobber(SI);
  479. // If alias analysis can tell that this store is guaranteed to not modify
  480. // the query pointer, ignore it. Use getModRefInfo to handle cases where
  481. // the query pointer points to constant memory etc.
  482. if (AA->getModRefInfo(SI, MemLoc) == AliasAnalysis::NoModRef)
  483. continue;
  484. // Ok, this store might clobber the query pointer. Check to see if it is
  485. // a must alias: in this case, we want to return this as a def.
  486. AliasAnalysis::Location StoreLoc = AA->getLocation(SI);
  487. // If we found a pointer, check if it could be the same as our pointer.
  488. AliasAnalysis::AliasResult R = AA->alias(StoreLoc, MemLoc);
  489. if (R == AliasAnalysis::NoAlias)
  490. continue;
  491. if (R == AliasAnalysis::MustAlias)
  492. return MemDepResult::getDef(Inst);
  493. if (isInvariantLoad)
  494. continue;
  495. return MemDepResult::getClobber(Inst);
  496. }
  497. // If this is an allocation, and if we know that the accessed pointer is to
  498. // the allocation, return Def. This means that there is no dependence and
  499. // the access can be optimized based on that. For example, a load could
  500. // turn into undef.
  501. // Note: Only determine this to be a malloc if Inst is the malloc call, not
  502. // a subsequent bitcast of the malloc call result. There can be stores to
  503. // the malloced memory between the malloc call and its bitcast uses, and we
  504. // need to continue scanning until the malloc call.
  505. const TargetLibraryInfo *TLI = AA->getTargetLibraryInfo();
  506. if (isa<AllocaInst>(Inst) || isNoAliasFn(Inst, TLI)) {
  507. const Value *AccessPtr = GetUnderlyingObject(MemLoc.Ptr, DL);
  508. if (AccessPtr == Inst || AA->isMustAlias(Inst, AccessPtr))
  509. return MemDepResult::getDef(Inst);
  510. // Be conservative if the accessed pointer may alias the allocation.
  511. if (AA->alias(Inst, AccessPtr) != AliasAnalysis::NoAlias)
  512. return MemDepResult::getClobber(Inst);
  513. // If the allocation is not aliased and does not read memory (like
  514. // strdup), it is safe to ignore.
  515. if (isa<AllocaInst>(Inst) ||
  516. isMallocLikeFn(Inst, TLI) || isCallocLikeFn(Inst, TLI))
  517. continue;
  518. }
  519. // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
  520. AliasAnalysis::ModRefResult MR = AA->getModRefInfo(Inst, MemLoc);
  521. // If necessary, perform additional analysis.
  522. if (MR == AliasAnalysis::ModRef)
  523. MR = AA->callCapturesBefore(Inst, MemLoc, DT);
  524. switch (MR) {
  525. case AliasAnalysis::NoModRef:
  526. // If the call has no effect on the queried pointer, just ignore it.
  527. continue;
  528. case AliasAnalysis::Mod:
  529. return MemDepResult::getClobber(Inst);
  530. case AliasAnalysis::Ref:
  531. // If the call is known to never store to the pointer, and if this is a
  532. // load query, we can safely ignore it (scan past it).
  533. if (isLoad)
  534. continue;
  535. default:
  536. // Otherwise, there is a potential dependence. Return a clobber.
  537. return MemDepResult::getClobber(Inst);
  538. }
  539. }
  540. // No dependence found. If this is the entry block of the function, it is
  541. // unknown, otherwise it is non-local.
  542. if (BB != &BB->getParent()->getEntryBlock())
  543. return MemDepResult::getNonLocal();
  544. return MemDepResult::getNonFuncLocal();
  545. }
  546. /// getDependency - Return the instruction on which a memory operation
  547. /// depends.
  548. MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
  549. Instruction *ScanPos = QueryInst;
  550. // Check for a cached result
  551. MemDepResult &LocalCache = LocalDeps[QueryInst];
  552. // If the cached entry is non-dirty, just return it. Note that this depends
  553. // on MemDepResult's default constructing to 'dirty'.
  554. if (!LocalCache.isDirty())
  555. return LocalCache;
  556. // Otherwise, if we have a dirty entry, we know we can start the scan at that
  557. // instruction, which may save us some work.
  558. if (Instruction *Inst = LocalCache.getInst()) {
  559. ScanPos = Inst;
  560. RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst);
  561. }
  562. BasicBlock *QueryParent = QueryInst->getParent();
  563. // Do the scan.
  564. if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {
  565. // No dependence found. If this is the entry block of the function, it is
  566. // unknown, otherwise it is non-local.
  567. if (QueryParent != &QueryParent->getParent()->getEntryBlock())
  568. LocalCache = MemDepResult::getNonLocal();
  569. else
  570. LocalCache = MemDepResult::getNonFuncLocal();
  571. } else {
  572. AliasAnalysis::Location MemLoc;
  573. AliasAnalysis::ModRefResult MR = GetLocation(QueryInst, MemLoc, AA);
  574. if (MemLoc.Ptr) {
  575. // If we can do a pointer scan, make it happen.
  576. bool isLoad = !(MR & AliasAnalysis::Mod);
  577. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(QueryInst))
  578. isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_start;
  579. LocalCache = getPointerDependencyFrom(MemLoc, isLoad, ScanPos,
  580. QueryParent, QueryInst);
  581. } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
  582. CallSite QueryCS(QueryInst);
  583. bool isReadOnly = AA->onlyReadsMemory(QueryCS);
  584. LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos,
  585. QueryParent);
  586. } else
  587. // Non-memory instruction.
  588. LocalCache = MemDepResult::getUnknown();
  589. }
  590. // Remember the result!
  591. if (Instruction *I = LocalCache.getInst())
  592. ReverseLocalDeps[I].insert(QueryInst);
  593. return LocalCache;
  594. }
  595. #ifndef NDEBUG
  596. /// AssertSorted - This method is used when -debug is specified to verify that
  597. /// cache arrays are properly kept sorted.
  598. static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
  599. int Count = -1) {
  600. if (Count == -1) Count = Cache.size();
  601. if (Count == 0) return;
  602. for (unsigned i = 1; i != unsigned(Count); ++i)
  603. assert(!(Cache[i] < Cache[i-1]) && "Cache isn't sorted!");
  604. }
  605. #endif
  606. /// getNonLocalCallDependency - Perform a full dependency query for the
  607. /// specified call, returning the set of blocks that the value is
  608. /// potentially live across. The returned set of results will include a
  609. /// "NonLocal" result for all blocks where the value is live across.
  610. ///
  611. /// This method assumes the instruction returns a "NonLocal" dependency
  612. /// within its own block.
  613. ///
  614. /// This returns a reference to an internal data structure that may be
  615. /// invalidated on the next non-local query or when an instruction is
  616. /// removed. Clients must copy this data if they want it around longer than
  617. /// that.
  618. const MemoryDependenceAnalysis::NonLocalDepInfo &
  619. MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) {
  620. assert(getDependency(QueryCS.getInstruction()).isNonLocal() &&
  621. "getNonLocalCallDependency should only be used on calls with non-local deps!");
  622. PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()];
  623. NonLocalDepInfo &Cache = CacheP.first;
  624. /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
  625. /// the cached case, this can happen due to instructions being deleted etc. In
  626. /// the uncached case, this starts out as the set of predecessors we care
  627. /// about.
  628. SmallVector<BasicBlock*, 32> DirtyBlocks;
  629. if (!Cache.empty()) {
  630. // Okay, we have a cache entry. If we know it is not dirty, just return it
  631. // with no computation.
  632. if (!CacheP.second) {
  633. ++NumCacheNonLocal;
  634. return Cache;
  635. }
  636. // If we already have a partially computed set of results, scan them to
  637. // determine what is dirty, seeding our initial DirtyBlocks worklist.
  638. for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
  639. I != E; ++I)
  640. if (I->getResult().isDirty())
  641. DirtyBlocks.push_back(I->getBB());
  642. // Sort the cache so that we can do fast binary search lookups below.
  643. std::sort(Cache.begin(), Cache.end());
  644. ++NumCacheDirtyNonLocal;
  645. //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
  646. // << Cache.size() << " cached: " << *QueryInst;
  647. } else {
  648. // Seed DirtyBlocks with each of the preds of QueryInst's block.
  649. BasicBlock *QueryBB = QueryCS.getInstruction()->getParent();
  650. for (BasicBlock **PI = PredCache->GetPreds(QueryBB); *PI; ++PI)
  651. DirtyBlocks.push_back(*PI);
  652. ++NumUncacheNonLocal;
  653. }
  654. // isReadonlyCall - If this is a read-only call, we can be more aggressive.
  655. bool isReadonlyCall = AA->onlyReadsMemory(QueryCS);
  656. SmallPtrSet<BasicBlock*, 64> Visited;
  657. unsigned NumSortedEntries = Cache.size();
  658. DEBUG(AssertSorted(Cache));
  659. // Iterate while we still have blocks to update.
  660. while (!DirtyBlocks.empty()) {
  661. BasicBlock *DirtyBB = DirtyBlocks.back();
  662. DirtyBlocks.pop_back();
  663. // Already processed this block?
  664. if (!Visited.insert(DirtyBB))
  665. continue;
  666. // Do a binary search to see if we already have an entry for this block in
  667. // the cache set. If so, find it.
  668. DEBUG(AssertSorted(Cache, NumSortedEntries));
  669. NonLocalDepInfo::iterator Entry =
  670. std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
  671. NonLocalDepEntry(DirtyBB));
  672. if (Entry != Cache.begin() && std::prev(Entry)->getBB() == DirtyBB)
  673. --Entry;
  674. NonLocalDepEntry *ExistingResult = nullptr;
  675. if (Entry != Cache.begin()+NumSortedEntries &&
  676. Entry->getBB() == DirtyBB) {
  677. // If we already have an entry, and if it isn't already dirty, the block
  678. // is done.
  679. if (!Entry->getResult().isDirty())
  680. continue;
  681. // Otherwise, remember this slot so we can update the value.
  682. ExistingResult = &*Entry;
  683. }
  684. // If the dirty entry has a pointer, start scanning from it so we don't have
  685. // to rescan the entire block.
  686. BasicBlock::iterator ScanPos = DirtyBB->end();
  687. if (ExistingResult) {
  688. if (Instruction *Inst = ExistingResult->getResult().getInst()) {
  689. ScanPos = Inst;
  690. // We're removing QueryInst's use of Inst.
  691. RemoveFromReverseMap(ReverseNonLocalDeps, Inst,
  692. QueryCS.getInstruction());
  693. }
  694. }
  695. // Find out if this block has a local dependency for QueryInst.
  696. MemDepResult Dep;
  697. if (ScanPos != DirtyBB->begin()) {
  698. Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB);
  699. } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) {
  700. // No dependence found. If this is the entry block of the function, it is
  701. // a clobber, otherwise it is unknown.
  702. Dep = MemDepResult::getNonLocal();
  703. } else {
  704. Dep = MemDepResult::getNonFuncLocal();
  705. }
  706. // If we had a dirty entry for the block, update it. Otherwise, just add
  707. // a new entry.
  708. if (ExistingResult)
  709. ExistingResult->setResult(Dep);
  710. else
  711. Cache.push_back(NonLocalDepEntry(DirtyBB, Dep));
  712. // If the block has a dependency (i.e. it isn't completely transparent to
  713. // the value), remember the association!
  714. if (!Dep.isNonLocal()) {
  715. // Keep the ReverseNonLocalDeps map up to date so we can efficiently
  716. // update this when we remove instructions.
  717. if (Instruction *Inst = Dep.getInst())
  718. ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction());
  719. } else {
  720. // If the block *is* completely transparent to the load, we need to check
  721. // the predecessors of this block. Add them to our worklist.
  722. for (BasicBlock **PI = PredCache->GetPreds(DirtyBB); *PI; ++PI)
  723. DirtyBlocks.push_back(*PI);
  724. }
  725. }
  726. return Cache;
  727. }
  728. /// getNonLocalPointerDependency - Perform a full dependency query for an
  729. /// access to the specified (non-volatile) memory location, returning the
  730. /// set of instructions that either define or clobber the value.
  731. ///
  732. /// This method assumes the pointer has a "NonLocal" dependency within its
  733. /// own block.
  734. ///
  735. void MemoryDependenceAnalysis::
  736. getNonLocalPointerDependency(const AliasAnalysis::Location &Loc, bool isLoad,
  737. BasicBlock *FromBB,
  738. SmallVectorImpl<NonLocalDepResult> &Result) {
  739. assert(Loc.Ptr->getType()->isPointerTy() &&
  740. "Can't get pointer deps of a non-pointer!");
  741. Result.clear();
  742. PHITransAddr Address(const_cast<Value *>(Loc.Ptr), DL);
  743. // This is the set of blocks we've inspected, and the pointer we consider in
  744. // each block. Because of critical edges, we currently bail out if querying
  745. // a block with multiple different pointers. This can happen during PHI
  746. // translation.
  747. DenseMap<BasicBlock*, Value*> Visited;
  748. if (!getNonLocalPointerDepFromBB(Address, Loc, isLoad, FromBB,
  749. Result, Visited, true))
  750. return;
  751. Result.clear();
  752. Result.push_back(NonLocalDepResult(FromBB,
  753. MemDepResult::getUnknown(),
  754. const_cast<Value *>(Loc.Ptr)));
  755. }
  756. /// GetNonLocalInfoForBlock - Compute the memdep value for BB with
  757. /// Pointer/PointeeSize using either cached information in Cache or by doing a
  758. /// lookup (which may use dirty cache info if available). If we do a lookup,
  759. /// add the result to the cache.
  760. MemDepResult MemoryDependenceAnalysis::
  761. GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc,
  762. bool isLoad, BasicBlock *BB,
  763. NonLocalDepInfo *Cache, unsigned NumSortedEntries) {
  764. // Do a binary search to see if we already have an entry for this block in
  765. // the cache set. If so, find it.
  766. NonLocalDepInfo::iterator Entry =
  767. std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries,
  768. NonLocalDepEntry(BB));
  769. if (Entry != Cache->begin() && (Entry-1)->getBB() == BB)
  770. --Entry;
  771. NonLocalDepEntry *ExistingResult = nullptr;
  772. if (Entry != Cache->begin()+NumSortedEntries && Entry->getBB() == BB)
  773. ExistingResult = &*Entry;
  774. // If we have a cached entry, and it is non-dirty, use it as the value for
  775. // this dependency.
  776. if (ExistingResult && !ExistingResult->getResult().isDirty()) {
  777. ++NumCacheNonLocalPtr;
  778. return ExistingResult->getResult();
  779. }
  780. // Otherwise, we have to scan for the value. If we have a dirty cache
  781. // entry, start scanning from its position, otherwise we scan from the end
  782. // of the block.
  783. BasicBlock::iterator ScanPos = BB->end();
  784. if (ExistingResult && ExistingResult->getResult().getInst()) {
  785. assert(ExistingResult->getResult().getInst()->getParent() == BB &&
  786. "Instruction invalidated?");
  787. ++NumCacheDirtyNonLocalPtr;
  788. ScanPos = ExistingResult->getResult().getInst();
  789. // Eliminating the dirty entry from 'Cache', so update the reverse info.
  790. ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
  791. RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey);
  792. } else {
  793. ++NumUncacheNonLocalPtr;
  794. }
  795. // Scan the block for the dependency.
  796. MemDepResult Dep = getPointerDependencyFrom(Loc, isLoad, ScanPos, BB);
  797. // If we had a dirty entry for the block, update it. Otherwise, just add
  798. // a new entry.
  799. if (ExistingResult)
  800. ExistingResult->setResult(Dep);
  801. else
  802. Cache->push_back(NonLocalDepEntry(BB, Dep));
  803. // If the block has a dependency (i.e. it isn't completely transparent to
  804. // the value), remember the reverse association because we just added it
  805. // to Cache!
  806. if (!Dep.isDef() && !Dep.isClobber())
  807. return Dep;
  808. // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently
  809. // update MemDep when we remove instructions.
  810. Instruction *Inst = Dep.getInst();
  811. assert(Inst && "Didn't depend on anything?");
  812. ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
  813. ReverseNonLocalPtrDeps[Inst].insert(CacheKey);
  814. return Dep;
  815. }
  816. /// SortNonLocalDepInfoCache - Sort the NonLocalDepInfo cache, given a certain
  817. /// number of elements in the array that are already properly ordered. This is
  818. /// optimized for the case when only a few entries are added.
  819. static void
  820. SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
  821. unsigned NumSortedEntries) {
  822. switch (Cache.size() - NumSortedEntries) {
  823. case 0:
  824. // done, no new entries.
  825. break;
  826. case 2: {
  827. // Two new entries, insert the last one into place.
  828. NonLocalDepEntry Val = Cache.back();
  829. Cache.pop_back();
  830. MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
  831. std::upper_bound(Cache.begin(), Cache.end()-1, Val);
  832. Cache.insert(Entry, Val);
  833. // FALL THROUGH.
  834. }
  835. case 1:
  836. // One new entry, Just insert the new value at the appropriate position.
  837. if (Cache.size() != 1) {
  838. NonLocalDepEntry Val = Cache.back();
  839. Cache.pop_back();
  840. MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
  841. std::upper_bound(Cache.begin(), Cache.end(), Val);
  842. Cache.insert(Entry, Val);
  843. }
  844. break;
  845. default:
  846. // Added many values, do a full scale sort.
  847. std::sort(Cache.begin(), Cache.end());
  848. break;
  849. }
  850. }
  851. /// getNonLocalPointerDepFromBB - Perform a dependency query based on
  852. /// pointer/pointeesize starting at the end of StartBB. Add any clobber/def
  853. /// results to the results vector and keep track of which blocks are visited in
  854. /// 'Visited'.
  855. ///
  856. /// This has special behavior for the first block queries (when SkipFirstBlock
  857. /// is true). In this special case, it ignores the contents of the specified
  858. /// block and starts returning dependence info for its predecessors.
  859. ///
  860. /// This function returns false on success, or true to indicate that it could
  861. /// not compute dependence information for some reason. This should be treated
  862. /// as a clobber dependence on the first instruction in the predecessor block.
  863. bool MemoryDependenceAnalysis::
  864. getNonLocalPointerDepFromBB(const PHITransAddr &Pointer,
  865. const AliasAnalysis::Location &Loc,
  866. bool isLoad, BasicBlock *StartBB,
  867. SmallVectorImpl<NonLocalDepResult> &Result,
  868. DenseMap<BasicBlock*, Value*> &Visited,
  869. bool SkipFirstBlock) {
  870. // Look up the cached info for Pointer.
  871. ValueIsLoadPair CacheKey(Pointer.getAddr(), isLoad);
  872. // Set up a temporary NLPI value. If the map doesn't yet have an entry for
  873. // CacheKey, this value will be inserted as the associated value. Otherwise,
  874. // it'll be ignored, and we'll have to check to see if the cached size and
  875. // aa tags are consistent with the current query.
  876. NonLocalPointerInfo InitialNLPI;
  877. InitialNLPI.Size = Loc.Size;
  878. InitialNLPI.AATags = Loc.AATags;
  879. // Get the NLPI for CacheKey, inserting one into the map if it doesn't
  880. // already have one.
  881. std::pair<CachedNonLocalPointerInfo::iterator, bool> Pair =
  882. NonLocalPointerDeps.insert(std::make_pair(CacheKey, InitialNLPI));
  883. NonLocalPointerInfo *CacheInfo = &Pair.first->second;
  884. // If we already have a cache entry for this CacheKey, we may need to do some
  885. // work to reconcile the cache entry and the current query.
  886. if (!Pair.second) {
  887. if (CacheInfo->Size < Loc.Size) {
  888. // The query's Size is greater than the cached one. Throw out the
  889. // cached data and proceed with the query at the greater size.
  890. CacheInfo->Pair = BBSkipFirstBlockPair();
  891. CacheInfo->Size = Loc.Size;
  892. for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(),
  893. DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI)
  894. if (Instruction *Inst = DI->getResult().getInst())
  895. RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
  896. CacheInfo->NonLocalDeps.clear();
  897. } else if (CacheInfo->Size > Loc.Size) {
  898. // This query's Size is less than the cached one. Conservatively restart
  899. // the query using the greater size.
  900. return getNonLocalPointerDepFromBB(Pointer,
  901. Loc.getWithNewSize(CacheInfo->Size),
  902. isLoad, StartBB, Result, Visited,
  903. SkipFirstBlock);
  904. }
  905. // If the query's AATags are inconsistent with the cached one,
  906. // conservatively throw out the cached data and restart the query with
  907. // no tag if needed.
  908. if (CacheInfo->AATags != Loc.AATags) {
  909. if (CacheInfo->AATags) {
  910. CacheInfo->Pair = BBSkipFirstBlockPair();
  911. CacheInfo->AATags = AAMDNodes();
  912. for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(),
  913. DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI)
  914. if (Instruction *Inst = DI->getResult().getInst())
  915. RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
  916. CacheInfo->NonLocalDeps.clear();
  917. }
  918. if (Loc.AATags)
  919. return getNonLocalPointerDepFromBB(Pointer, Loc.getWithoutAATags(),
  920. isLoad, StartBB, Result, Visited,
  921. SkipFirstBlock);
  922. }
  923. }
  924. NonLocalDepInfo *Cache = &CacheInfo->NonLocalDeps;
  925. // If we have valid cached information for exactly the block we are
  926. // investigating, just return it with no recomputation.
  927. if (CacheInfo->Pair == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) {
  928. // We have a fully cached result for this query then we can just return the
  929. // cached results and populate the visited set. However, we have to verify
  930. // that we don't already have conflicting results for these blocks. Check
  931. // to ensure that if a block in the results set is in the visited set that
  932. // it was for the same pointer query.
  933. if (!Visited.empty()) {
  934. for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
  935. I != E; ++I) {
  936. DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->getBB());
  937. if (VI == Visited.end() || VI->second == Pointer.getAddr())
  938. continue;
  939. // We have a pointer mismatch in a block. Just return clobber, saying
  940. // that something was clobbered in this result. We could also do a
  941. // non-fully cached query, but there is little point in doing this.
  942. return true;
  943. }
  944. }
  945. Value *Addr = Pointer.getAddr();
  946. for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
  947. I != E; ++I) {
  948. Visited.insert(std::make_pair(I->getBB(), Addr));
  949. if (I->getResult().isNonLocal()) {
  950. continue;
  951. }
  952. if (!DT) {
  953. Result.push_back(NonLocalDepResult(I->getBB(),
  954. MemDepResult::getUnknown(),
  955. Addr));
  956. } else if (DT->isReachableFromEntry(I->getBB())) {
  957. Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(), Addr));
  958. }
  959. }
  960. ++NumCacheCompleteNonLocalPtr;
  961. return false;
  962. }
  963. // Otherwise, either this is a new block, a block with an invalid cache
  964. // pointer or one that we're about to invalidate by putting more info into it
  965. // than its valid cache info. If empty, the result will be valid cache info,
  966. // otherwise it isn't.
  967. if (Cache->empty())
  968. CacheInfo->Pair = BBSkipFirstBlockPair(StartBB, SkipFirstBlock);
  969. else
  970. CacheInfo->Pair = BBSkipFirstBlockPair();
  971. SmallVector<BasicBlock*, 32> Worklist;
  972. Worklist.push_back(StartBB);
  973. // PredList used inside loop.
  974. SmallVector<std::pair<BasicBlock*, PHITransAddr>, 16> PredList;
  975. // Keep track of the entries that we know are sorted. Previously cached
  976. // entries will all be sorted. The entries we add we only sort on demand (we
  977. // don't insert every element into its sorted position). We know that we
  978. // won't get any reuse from currently inserted values, because we don't
  979. // revisit blocks after we insert info for them.
  980. unsigned NumSortedEntries = Cache->size();
  981. DEBUG(AssertSorted(*Cache));
  982. while (!Worklist.empty()) {
  983. BasicBlock *BB = Worklist.pop_back_val();
  984. // Skip the first block if we have it.
  985. if (!SkipFirstBlock) {
  986. // Analyze the dependency of *Pointer in FromBB. See if we already have
  987. // been here.
  988. assert(Visited.count(BB) && "Should check 'visited' before adding to WL");
  989. // Get the dependency info for Pointer in BB. If we have cached
  990. // information, we will use it, otherwise we compute it.
  991. DEBUG(AssertSorted(*Cache, NumSortedEntries));
  992. MemDepResult Dep = GetNonLocalInfoForBlock(Loc, isLoad, BB, Cache,
  993. NumSortedEntries);
  994. // If we got a Def or Clobber, add this to the list of results.
  995. if (!Dep.isNonLocal()) {
  996. if (!DT) {
  997. Result.push_back(NonLocalDepResult(BB,
  998. MemDepResult::getUnknown(),
  999. Pointer.getAddr()));
  1000. continue;
  1001. } else if (DT->isReachableFromEntry(BB)) {
  1002. Result.push_back(NonLocalDepResult(BB, Dep, Pointer.getAddr()));
  1003. continue;
  1004. }
  1005. }
  1006. }
  1007. // If 'Pointer' is an instruction defined in this block, then we need to do
  1008. // phi translation to change it into a value live in the predecessor block.
  1009. // If not, we just add the predecessors to the worklist and scan them with
  1010. // the same Pointer.
  1011. if (!Pointer.NeedsPHITranslationFromBlock(BB)) {
  1012. SkipFirstBlock = false;
  1013. SmallVector<BasicBlock*, 16> NewBlocks;
  1014. for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
  1015. // Verify that we haven't looked at this block yet.
  1016. std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
  1017. InsertRes = Visited.insert(std::make_pair(*PI, Pointer.getAddr()));
  1018. if (InsertRes.second) {
  1019. // First time we've looked at *PI.
  1020. NewBlocks.push_back(*PI);
  1021. continue;
  1022. }
  1023. // If we have seen this block before, but it was with a different
  1024. // pointer then we have a phi translation failure and we have to treat
  1025. // this as a clobber.
  1026. if (InsertRes.first->second != Pointer.getAddr()) {
  1027. // Make sure to clean up the Visited map before continuing on to
  1028. // PredTranslationFailure.
  1029. for (unsigned i = 0; i < NewBlocks.size(); i++)
  1030. Visited.erase(NewBlocks[i]);
  1031. goto PredTranslationFailure;
  1032. }
  1033. }
  1034. Worklist.append(NewBlocks.begin(), NewBlocks.end());
  1035. continue;
  1036. }
  1037. // We do need to do phi translation, if we know ahead of time we can't phi
  1038. // translate this value, don't even try.
  1039. if (!Pointer.IsPotentiallyPHITranslatable())
  1040. goto PredTranslationFailure;
  1041. // We may have added values to the cache list before this PHI translation.
  1042. // If so, we haven't done anything to ensure that the cache remains sorted.
  1043. // Sort it now (if needed) so that recursive invocations of
  1044. // getNonLocalPointerDepFromBB and other routines that could reuse the cache
  1045. // value will only see properly sorted cache arrays.
  1046. if (Cache && NumSortedEntries != Cache->size()) {
  1047. SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
  1048. NumSortedEntries = Cache->size();
  1049. }
  1050. Cache = nullptr;
  1051. PredList.clear();
  1052. for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
  1053. BasicBlock *Pred = *PI;
  1054. PredList.push_back(std::make_pair(Pred, Pointer));
  1055. // Get the PHI translated pointer in this predecessor. This can fail if
  1056. // not translatable, in which case the getAddr() returns null.
  1057. PHITransAddr &PredPointer = PredList.back().second;
  1058. PredPointer.PHITranslateValue(BB, Pred, nullptr);
  1059. Value *PredPtrVal = PredPointer.getAddr();
  1060. // Check to see if we have already visited this pred block with another
  1061. // pointer. If so, we can't do this lookup. This failure can occur
  1062. // with PHI translation when a critical edge exists and the PHI node in
  1063. // the successor translates to a pointer value different than the
  1064. // pointer the block was first analyzed with.
  1065. std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
  1066. InsertRes = Visited.insert(std::make_pair(Pred, PredPtrVal));
  1067. if (!InsertRes.second) {
  1068. // We found the pred; take it off the list of preds to visit.
  1069. PredList.pop_back();
  1070. // If the predecessor was visited with PredPtr, then we already did
  1071. // the analysis and can ignore it.
  1072. if (InsertRes.first->second == PredPtrVal)
  1073. continue;
  1074. // Otherwise, the block was previously analyzed with a different
  1075. // pointer. We can't represent the result of this case, so we just
  1076. // treat this as a phi translation failure.
  1077. // Make sure to clean up the Visited map before continuing on to
  1078. // PredTranslationFailure.
  1079. for (unsigned i = 0, n = PredList.size(); i < n; ++i)
  1080. Visited.erase(PredList[i].first);
  1081. goto PredTranslationFailure;
  1082. }
  1083. }
  1084. // Actually process results here; this need to be a separate loop to avoid
  1085. // calling getNonLocalPointerDepFromBB for blocks we don't want to return
  1086. // any results for. (getNonLocalPointerDepFromBB will modify our
  1087. // datastructures in ways the code after the PredTranslationFailure label
  1088. // doesn't expect.)
  1089. for (unsigned i = 0, n = PredList.size(); i < n; ++i) {
  1090. BasicBlock *Pred = PredList[i].first;
  1091. PHITransAddr &PredPointer = PredList[i].second;
  1092. Value *PredPtrVal = PredPointer.getAddr();
  1093. bool CanTranslate = true;
  1094. // If PHI translation was unable to find an available pointer in this
  1095. // predecessor, then we have to assume that the pointer is clobbered in
  1096. // that predecessor. We can still do PRE of the load, which would insert
  1097. // a computation of the pointer in this predecessor.
  1098. if (!PredPtrVal)
  1099. CanTranslate = false;
  1100. // FIXME: it is entirely possible that PHI translating will end up with
  1101. // the same value. Consider PHI translating something like:
  1102. // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need*
  1103. // to recurse here, pedantically speaking.
  1104. // If getNonLocalPointerDepFromBB fails here, that means the cached
  1105. // result conflicted with the Visited list; we have to conservatively
  1106. // assume it is unknown, but this also does not block PRE of the load.
  1107. if (!CanTranslate ||
  1108. getNonLocalPointerDepFromBB(PredPointer,
  1109. Loc.getWithNewPtr(PredPtrVal),
  1110. isLoad, Pred,
  1111. Result, Visited)) {
  1112. // Add the entry to the Result list.
  1113. NonLocalDepResult Entry(Pred, MemDepResult::getUnknown(), PredPtrVal);
  1114. Result.push_back(Entry);
  1115. // Since we had a phi translation failure, the cache for CacheKey won't
  1116. // include all of the entries that we need to immediately satisfy future
  1117. // queries. Mark this in NonLocalPointerDeps by setting the
  1118. // BBSkipFirstBlockPair pointer to null. This requires reuse of the
  1119. // cached value to do more work but not miss the phi trans failure.
  1120. NonLocalPointerInfo &NLPI = NonLocalPointerDeps[CacheKey];
  1121. NLPI.Pair = BBSkipFirstBlockPair();
  1122. continue;
  1123. }
  1124. }
  1125. // Refresh the CacheInfo/Cache pointer so that it isn't invalidated.
  1126. CacheInfo = &NonLocalPointerDeps[CacheKey];
  1127. Cache = &CacheInfo->NonLocalDeps;
  1128. NumSortedEntries = Cache->size();
  1129. // Since we did phi translation, the "Cache" set won't contain all of the
  1130. // results for the query. This is ok (we can still use it to accelerate
  1131. // specific block queries) but we can't do the fastpath "return all
  1132. // results from the set" Clear out the indicator for this.
  1133. CacheInfo->Pair = BBSkipFirstBlockPair();
  1134. SkipFirstBlock = false;
  1135. continue;
  1136. PredTranslationFailure:
  1137. // The following code is "failure"; we can't produce a sane translation
  1138. // for the given block. It assumes that we haven't modified any of
  1139. // our datastructures while processing the current block.
  1140. if (!Cache) {
  1141. // Refresh the CacheInfo/Cache pointer if it got invalidated.
  1142. CacheInfo = &NonLocalPointerDeps[CacheKey];
  1143. Cache = &CacheInfo->NonLocalDeps;
  1144. NumSortedEntries = Cache->size();
  1145. }
  1146. // Since we failed phi translation, the "Cache" set won't contain all of the
  1147. // results for the query. This is ok (we can still use it to accelerate
  1148. // specific block queries) but we can't do the fastpath "return all
  1149. // results from the set". Clear out the indicator for this.
  1150. CacheInfo->Pair = BBSkipFirstBlockPair();
  1151. // If *nothing* works, mark the pointer as unknown.
  1152. //
  1153. // If this is the magic first block, return this as a clobber of the whole
  1154. // incoming value. Since we can't phi translate to one of the predecessors,
  1155. // we have to bail out.
  1156. if (SkipFirstBlock)
  1157. return true;
  1158. for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) {
  1159. assert(I != Cache->rend() && "Didn't find current block??");
  1160. if (I->getBB() != BB)
  1161. continue;
  1162. assert(I->getResult().isNonLocal() &&
  1163. "Should only be here with transparent block");
  1164. I->setResult(MemDepResult::getUnknown());
  1165. Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(),
  1166. Pointer.getAddr()));
  1167. break;
  1168. }
  1169. }
  1170. // Okay, we're done now. If we added new values to the cache, re-sort it.
  1171. SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
  1172. DEBUG(AssertSorted(*Cache));
  1173. return false;
  1174. }
  1175. /// RemoveCachedNonLocalPointerDependencies - If P exists in
  1176. /// CachedNonLocalPointerInfo, remove it.
  1177. void MemoryDependenceAnalysis::
  1178. RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) {
  1179. CachedNonLocalPointerInfo::iterator It =
  1180. NonLocalPointerDeps.find(P);
  1181. if (It == NonLocalPointerDeps.end()) return;
  1182. // Remove all of the entries in the BB->val map. This involves removing
  1183. // instructions from the reverse map.
  1184. NonLocalDepInfo &PInfo = It->second.NonLocalDeps;
  1185. for (unsigned i = 0, e = PInfo.size(); i != e; ++i) {
  1186. Instruction *Target = PInfo[i].getResult().getInst();
  1187. if (!Target) continue; // Ignore non-local dep results.
  1188. assert(Target->getParent() == PInfo[i].getBB());
  1189. // Eliminating the dirty entry from 'Cache', so update the reverse info.
  1190. RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P);
  1191. }
  1192. // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo).
  1193. NonLocalPointerDeps.erase(It);
  1194. }
  1195. /// invalidateCachedPointerInfo - This method is used to invalidate cached
  1196. /// information about the specified pointer, because it may be too
  1197. /// conservative in memdep. This is an optional call that can be used when
  1198. /// the client detects an equivalence between the pointer and some other
  1199. /// value and replaces the other value with ptr. This can make Ptr available
  1200. /// in more places that cached info does not necessarily keep.
  1201. void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) {
  1202. // If Ptr isn't really a pointer, just ignore it.
  1203. if (!Ptr->getType()->isPointerTy()) return;
  1204. // Flush store info for the pointer.
  1205. RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false));
  1206. // Flush load info for the pointer.
  1207. RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true));
  1208. }
  1209. /// invalidateCachedPredecessors - Clear the PredIteratorCache info.
  1210. /// This needs to be done when the CFG changes, e.g., due to splitting
  1211. /// critical edges.
  1212. void MemoryDependenceAnalysis::invalidateCachedPredecessors() {
  1213. PredCache->clear();
  1214. }
  1215. /// removeInstruction - Remove an instruction from the dependence analysis,
  1216. /// updating the dependence of instructions that previously depended on it.
  1217. /// This method attempts to keep the cache coherent using the reverse map.
  1218. void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
  1219. // Walk through the Non-local dependencies, removing this one as the value
  1220. // for any cached queries.
  1221. NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
  1222. if (NLDI != NonLocalDeps.end()) {
  1223. NonLocalDepInfo &BlockMap = NLDI->second.first;
  1224. for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
  1225. DI != DE; ++DI)
  1226. if (Instruction *Inst = DI->getResult().getInst())
  1227. RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst);
  1228. NonLocalDeps.erase(NLDI);
  1229. }
  1230. // If we have a cached local dependence query for this instruction, remove it.
  1231. //
  1232. LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
  1233. if (LocalDepEntry != LocalDeps.end()) {
  1234. // Remove us from DepInst's reverse set now that the local dep info is gone.
  1235. if (Instruction *Inst = LocalDepEntry->second.getInst())
  1236. RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst);
  1237. // Remove this local dependency info.
  1238. LocalDeps.erase(LocalDepEntry);
  1239. }
  1240. // If we have any cached pointer dependencies on this instruction, remove
  1241. // them. If the instruction has non-pointer type, then it can't be a pointer
  1242. // base.
  1243. // Remove it from both the load info and the store info. The instruction
  1244. // can't be in either of these maps if it is non-pointer.
  1245. if (RemInst->getType()->isPointerTy()) {
  1246. RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false));
  1247. RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true));
  1248. }
  1249. // Loop over all of the things that depend on the instruction we're removing.
  1250. //
  1251. SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
  1252. // If we find RemInst as a clobber or Def in any of the maps for other values,
  1253. // we need to replace its entry with a dirty version of the instruction after
  1254. // it. If RemInst is a terminator, we use a null dirty value.
  1255. //
  1256. // Using a dirty version of the instruction after RemInst saves having to scan
  1257. // the entire block to get to this point.
  1258. MemDepResult NewDirtyVal;
  1259. if (!RemInst->isTerminator())
  1260. NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst));
  1261. ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
  1262. if (ReverseDepIt != ReverseLocalDeps.end()) {
  1263. // RemInst can't be the terminator if it has local stuff depending on it.
  1264. assert(!ReverseDepIt->second.empty() && !isa<TerminatorInst>(RemInst) &&
  1265. "Nothing can locally depend on a terminator");
  1266. for (Instruction *InstDependingOnRemInst : ReverseDepIt->second) {
  1267. assert(InstDependingOnRemInst != RemInst &&
  1268. "Already removed our local dep info");
  1269. LocalDeps[InstDependingOnRemInst] = NewDirtyVal;
  1270. // Make sure to remember that new things depend on NewDepInst.
  1271. assert(NewDirtyVal.getInst() && "There is no way something else can have "
  1272. "a local dep on this if it is a terminator!");
  1273. ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(),
  1274. InstDependingOnRemInst));
  1275. }
  1276. ReverseLocalDeps.erase(ReverseDepIt);
  1277. // Add new reverse deps after scanning the set, to avoid invalidating the
  1278. // 'ReverseDeps' reference.
  1279. while (!ReverseDepsToAdd.empty()) {
  1280. ReverseLocalDeps[ReverseDepsToAdd.back().first]
  1281. .insert(ReverseDepsToAdd.back().second);
  1282. ReverseDepsToAdd.pop_back();
  1283. }
  1284. }
  1285. ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
  1286. if (ReverseDepIt != ReverseNonLocalDeps.end()) {
  1287. for (Instruction *I : ReverseDepIt->second) {
  1288. assert(I != RemInst && "Already removed NonLocalDep info for RemInst");
  1289. PerInstNLInfo &INLD = NonLocalDeps[I];
  1290. // The information is now dirty!
  1291. INLD.second = true;
  1292. for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
  1293. DE = INLD.first.end(); DI != DE; ++DI) {
  1294. if (DI->getResult().getInst() != RemInst) continue;
  1295. // Convert to a dirty entry for the subsequent instruction.
  1296. DI->setResult(NewDirtyVal);
  1297. if (Instruction *NextI = NewDirtyVal.getInst())
  1298. ReverseDepsToAdd.push_back(std::make_pair(NextI, I));
  1299. }
  1300. }
  1301. ReverseNonLocalDeps.erase(ReverseDepIt);
  1302. // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
  1303. while (!ReverseDepsToAdd.empty()) {
  1304. ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
  1305. .insert(ReverseDepsToAdd.back().second);
  1306. ReverseDepsToAdd.pop_back();
  1307. }
  1308. }
  1309. // If the instruction is in ReverseNonLocalPtrDeps then it appears as a
  1310. // value in the NonLocalPointerDeps info.
  1311. ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt =
  1312. ReverseNonLocalPtrDeps.find(RemInst);
  1313. if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) {
  1314. SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd;
  1315. for (ValueIsLoadPair P : ReversePtrDepIt->second) {
  1316. assert(P.getPointer() != RemInst &&
  1317. "Already removed NonLocalPointerDeps info for RemInst");
  1318. NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].NonLocalDeps;
  1319. // The cache is not valid for any specific block anymore.
  1320. NonLocalPointerDeps[P].Pair = BBSkipFirstBlockPair();
  1321. // Update any entries for RemInst to use the instruction after it.
  1322. for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end();
  1323. DI != DE; ++DI) {
  1324. if (DI->getResult().getInst() != RemInst) continue;
  1325. // Convert to a dirty entry for the subsequent instruction.
  1326. DI->setResult(NewDirtyVal);
  1327. if (Instruction *NewDirtyInst = NewDirtyVal.getInst())
  1328. ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P));
  1329. }
  1330. // Re-sort the NonLocalDepInfo. Changing the dirty entry to its
  1331. // subsequent value may invalidate the sortedness.
  1332. std::sort(NLPDI.begin(), NLPDI.end());
  1333. }
  1334. ReverseNonLocalPtrDeps.erase(ReversePtrDepIt);
  1335. while (!ReversePtrDepsToAdd.empty()) {
  1336. ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first]
  1337. .insert(ReversePtrDepsToAdd.back().second);
  1338. ReversePtrDepsToAdd.pop_back();
  1339. }
  1340. }
  1341. assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
  1342. AA->deleteValue(RemInst);
  1343. DEBUG(verifyRemoved(RemInst));
  1344. }
  1345. /// verifyRemoved - Verify that the specified instruction does not occur
  1346. /// in our internal data structures. This function verifies by asserting in
  1347. /// debug builds.
  1348. void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
  1349. #ifndef NDEBUG
  1350. for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
  1351. E = LocalDeps.end(); I != E; ++I) {
  1352. assert(I->first != D && "Inst occurs in data structures");
  1353. assert(I->second.getInst() != D &&
  1354. "Inst occurs in data structures");
  1355. }
  1356. for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(),
  1357. E = NonLocalPointerDeps.end(); I != E; ++I) {
  1358. assert(I->first.getPointer() != D && "Inst occurs in NLPD map key");
  1359. const NonLocalDepInfo &Val = I->second.NonLocalDeps;
  1360. for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end();
  1361. II != E; ++II)
  1362. assert(II->getResult().getInst() != D && "Inst occurs as NLPD value");
  1363. }
  1364. for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
  1365. E = NonLocalDeps.end(); I != E; ++I) {
  1366. assert(I->first != D && "Inst occurs in data structures");
  1367. const PerInstNLInfo &INLD = I->second;
  1368. for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
  1369. EE = INLD.first.end(); II != EE; ++II)
  1370. assert(II->getResult().getInst() != D && "Inst occurs in data structures");
  1371. }
  1372. for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
  1373. E = ReverseLocalDeps.end(); I != E; ++I) {
  1374. assert(I->first != D && "Inst occurs in data structures");
  1375. for (Instruction *Inst : I->second)
  1376. assert(Inst != D && "Inst occurs in data structures");
  1377. }
  1378. for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
  1379. E = ReverseNonLocalDeps.end();
  1380. I != E; ++I) {
  1381. assert(I->first != D && "Inst occurs in data structures");
  1382. for (Instruction *Inst : I->second)
  1383. assert(Inst != D && "Inst occurs in data structures");
  1384. }
  1385. for (ReverseNonLocalPtrDepTy::const_iterator
  1386. I = ReverseNonLocalPtrDeps.begin(),
  1387. E = ReverseNonLocalPtrDeps.end(); I != E; ++I) {
  1388. assert(I->first != D && "Inst occurs in rev NLPD map");
  1389. for (ValueIsLoadPair P : I->second)
  1390. assert(P != ValueIsLoadPair(D, false) &&
  1391. P != ValueIsLoadPair(D, true) &&
  1392. "Inst occurs in ReverseNonLocalPtrDeps map");
  1393. }
  1394. #endif
  1395. }