@@ -1370,19 +1370,26 @@ bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
1370
1370
static bool hadUndefContentsBefore (MemorySSA *MSSA, BatchAAResults &BAA,
1371
1371
Value *V, MemoryAccess *Clobber,
1372
1372
MemoryLocation Loc, Value *Size) {
1373
+ Value *VBase = getUnderlyingObject (V);
1373
1374
while (1 ) {
1374
1375
Clobber = MSSA->getWalker ()->getClobberingMemoryAccess (Clobber, Loc, BAA);
1375
1376
MemoryDef *Def = dyn_cast<MemoryDef>(Clobber);
1376
1377
if (!Def)
1377
1378
return false ;
1378
1379
1379
1380
if (MSSA->isLiveOnEntryDef (Def))
1380
- return isa<AllocaInst>(getUnderlyingObject (V) );
1381
+ return isa<AllocaInst>(VBase );
1381
1382
1382
1383
if (auto *II = dyn_cast_or_null<IntrinsicInst>(Def->getMemoryInst ())) {
1383
1384
if (II->getIntrinsicID () == Intrinsic::lifetime_start) {
1384
1385
auto *LTSize = cast<ConstantInt>(II->getArgOperand (0 ));
1385
1386
1387
+ // Check if the SSA Walk ended early due to heuristics or actually
1388
+ // reached a lifetime instruction for this pointer.
1389
+ Value *IIBase = getUnderlyingObject (II->getArgOperand (1 ));
1390
+ if (VBase != IIBase)
1391
+ return false ;
1392
+
1386
1393
if (Size)
1387
1394
if (auto CSize = dyn_cast<ConstantInt>(Size))
1388
1395
if (BAA.isMustAlias (V, II->getArgOperand (1 )) &&
@@ -1393,8 +1400,8 @@ static bool hadUndefContentsBefore(MemorySSA *MSSA, BatchAAResults &BAA,
1393
1400
// does) and we're querying a pointer based on that alloca, then we know
1394
1401
// the memory is definitely undef, regardless of how exactly we alias.
1395
1402
// The size also doesn't matter, as an out-of-bounds access would be UB.
1396
- if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject (V) )) {
1397
- if (getUnderlyingObject (II-> getArgOperand ( 1 )) == Alloca) {
1403
+ if (auto *Alloca = dyn_cast<AllocaInst>(VBase )) {
1404
+ if (IIBase == Alloca) {
1398
1405
const DataLayout &DL = Alloca->getDataLayout ();
1399
1406
if (std::optional<TypeSize> AllocaSize =
1400
1407
Alloca->getAllocationSize (DL))
@@ -1405,6 +1412,11 @@ static bool hadUndefContentsBefore(MemorySSA *MSSA, BatchAAResults &BAA,
1405
1412
Clobber = Def->getDefiningAccess ();
1406
1413
continue ;
1407
1414
} else if (II->getIntrinsicID () == Intrinsic::lifetime_end) {
1415
+ // Check if the SSA Walk ended early due to heuristics or actually
1416
+ // reached a lifetime instruction for this pointer.
1417
+ Value *IIBase = getUnderlyingObject (II->getArgOperand (1 ));
1418
+ if (VBase != IIBase)
1419
+ return false ;
1408
1420
Clobber = Def->getDefiningAccess ();
1409
1421
continue ;
1410
1422
}
0 commit comments