Skip to content

Commit feaa5fa

Browse files
committed
stop walk
1 parent b1ec0fc commit feaa5fa

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,19 +1370,26 @@ bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
13701370
static bool hadUndefContentsBefore(MemorySSA *MSSA, BatchAAResults &BAA,
13711371
Value *V, MemoryAccess *Clobber,
13721372
MemoryLocation Loc, Value *Size) {
1373+
Value *VBase = getUnderlyingObject(V);
13731374
while (1) {
13741375
Clobber = MSSA->getWalker()->getClobberingMemoryAccess(Clobber, Loc, BAA);
13751376
MemoryDef *Def = dyn_cast<MemoryDef>(Clobber);
13761377
if (!Def)
13771378
return false;
13781379

13791380
if (MSSA->isLiveOnEntryDef(Def))
1380-
return isa<AllocaInst>(getUnderlyingObject(V));
1381+
return isa<AllocaInst>(VBase);
13811382

13821383
if (auto *II = dyn_cast_or_null<IntrinsicInst>(Def->getMemoryInst())) {
13831384
if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
13841385
auto *LTSize = cast<ConstantInt>(II->getArgOperand(0));
13851386

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+
13861393
if (Size)
13871394
if (auto CSize = dyn_cast<ConstantInt>(Size))
13881395
if (BAA.isMustAlias(V, II->getArgOperand(1)) &&
@@ -1393,8 +1400,8 @@ static bool hadUndefContentsBefore(MemorySSA *MSSA, BatchAAResults &BAA,
13931400
// does) and we're querying a pointer based on that alloca, then we know
13941401
// the memory is definitely undef, regardless of how exactly we alias.
13951402
// 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) {
13981405
const DataLayout &DL = Alloca->getDataLayout();
13991406
if (std::optional<TypeSize> AllocaSize =
14001407
Alloca->getAllocationSize(DL))
@@ -1405,6 +1412,11 @@ static bool hadUndefContentsBefore(MemorySSA *MSSA, BatchAAResults &BAA,
14051412
Clobber = Def->getDefiningAccess();
14061413
continue;
14071414
} 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;
14081420
Clobber = Def->getDefiningAccess();
14091421
continue;
14101422
}

0 commit comments

Comments
 (0)