@@ -329,56 +329,49 @@ class GVNHoist {
329
329
return I1DFS < I2DFS;
330
330
}
331
331
332
- // Return true when there are memory uses of Def in BB.
333
- bool hasMemoryUseOnPath (const Instruction *NewPt, MemoryDef *Def, const BasicBlock *BB) {
334
- const Instruction *OldPt = Def->getMemoryInst ();
332
+ // Return true when there are users of Def in BB.
333
+ bool hasMemoryUseOnPath (MemoryAccess *Def, const BasicBlock *BB,
334
+ const Instruction *OldPt) {
335
+ const BasicBlock *DefBB = Def->getBlock ();
335
336
const BasicBlock *OldBB = OldPt->getParent ();
336
- const BasicBlock *NewBB = NewPt->getParent ();
337
337
338
- bool ReachedNewPt = false ;
339
- MemoryLocation DefLoc = MemoryLocation::get (OldPt);
340
- const MemorySSA::AccessList *Acc = MSSA->getBlockAccesses (BB);
341
- if (!Acc)
342
- return false ;
338
+ for (User *U : Def->users ())
339
+ if (auto *MU = dyn_cast<MemoryUse>(U)) {
340
+ // FIXME: MU->getBlock() does not get updated when we move the instruction.
341
+ BasicBlock *UBB = MU->getMemoryInst ()->getParent ();
342
+ // Only analyze uses in BB.
343
+ if (BB != UBB)
344
+ continue ;
343
345
344
- for (const MemoryAccess &MA : *Acc) {
345
- auto *MU = dyn_cast<MemoryUse>(&MA);
346
- if (!MU)
347
- continue ;
346
+ // A use in the same block as the Def is on the path.
347
+ if (UBB == DefBB) {
348
+ assert (MSSA->locallyDominates (Def, MU) && " def not dominating use" );
349
+ return true ;
350
+ }
348
351
349
- // Do not check whether MU aliases Def when MU occurs after OldPt.
350
- if (BB == OldBB && firstInBB (OldPt, MU->getMemoryInst ()))
351
- break ;
352
+ if (UBB != OldBB)
353
+ return true ;
352
354
353
- // Do not check whether MU aliases Def when MU occurs before NewPt.
354
- if (BB == NewBB) {
355
- if (!ReachedNewPt) {
356
- if (firstInBB (MU->getMemoryInst (), NewPt))
357
- continue ;
358
- ReachedNewPt = true ;
359
- }
355
+ // It is only harmful to hoist when the use is before OldPt.
356
+ if (firstInBB (MU->getMemoryInst (), OldPt))
357
+ return true ;
360
358
}
361
359
362
- if (!AA->isNoAlias (DefLoc, MemoryLocation::get (MU->getMemoryInst ())))
363
- return true ;
364
- }
365
-
366
360
return false ;
367
361
}
368
362
369
363
// Return true when there are exception handling or loads of memory Def
370
- // between Def and NewPt. This function is only called for stores: Def is
371
- // the MemoryDef of the store to be hoisted.
364
+ // between OldPt and NewPt.
372
365
373
366
// Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
374
367
// return true when the counter NBBsOnAllPaths reaces 0, except when it is
375
368
// initialized to -1 which is unlimited.
376
- bool hasEHOrLoadsOnPath (const Instruction *NewPt, MemoryDef *Def ,
377
- int &NBBsOnAllPaths) {
369
+ bool hasEHOrLoadsOnPath (const Instruction *NewPt, const Instruction *OldPt ,
370
+ MemoryAccess *Def, int &NBBsOnAllPaths) {
378
371
const BasicBlock *NewBB = NewPt->getParent ();
379
- const BasicBlock *OldBB = Def-> getBlock ();
372
+ const BasicBlock *OldBB = OldPt-> getParent ();
380
373
assert (DT->dominates (NewBB, OldBB) && " invalid path" );
381
- assert (DT->dominates (Def->getDefiningAccess ()-> getBlock (), NewBB) &&
374
+ assert (DT->dominates (Def->getBlock (), NewBB) &&
382
375
" def does not dominate new hoisting point" );
383
376
384
377
// Walk all basic blocks reachable in depth-first iteration on the inverse
@@ -397,7 +390,7 @@ class GVNHoist {
397
390
return true ;
398
391
399
392
// Check that we do not move a store past loads.
400
- if (hasMemoryUseOnPath (NewPt, Def, *I))
393
+ if (hasMemoryUseOnPath (Def, *I, OldPt ))
401
394
return true ;
402
395
403
396
// Stop walk once the limit is reached.
@@ -480,7 +473,7 @@ class GVNHoist {
480
473
481
474
// Check for unsafe hoistings due to side effects.
482
475
if (K == InsKind::Store) {
483
- if (hasEHOrLoadsOnPath (NewPt, dyn_cast<MemoryDef>(U) , NBBsOnAllPaths))
476
+ if (hasEHOrLoadsOnPath (NewPt, OldPt, D , NBBsOnAllPaths))
484
477
return false ;
485
478
} else if (hasEHOnPath (NewBB, OldBB, NBBsOnAllPaths))
486
479
return false ;
0 commit comments