MemoryDependenceAnalysis.cpp 62 KB

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