Skip to content

Commit a4e9739

Browse files
committed
[Analysis] Bail out for negative offsets in isDereferenceableAndAlignedInLoop
This patch is effectively NFC because it doesn't actually affect any existing tests. It's not possible to even write a test for it, because by accident negative offsets are caught by a subsequent unsigned add overflow check. However, I think it's better to bail out explicitly for negative offsets so that it's more consistent with the unsigned remainder and add calculations.
1 parent 221d5c5 commit a4e9739

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

llvm/lib/Analysis/Loads.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ bool llvm::isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
313313
const auto *Offset = dyn_cast<SCEVConstant>(StartS->getOperand(0));
314314
const auto *NewBase = dyn_cast<SCEVUnknown>(StartS->getOperand(1));
315315
if (StartS->getNumOperands() == 2 && Offset && NewBase) {
316+
// The following code below assumes the offset is unsigned, but GEP
317+
// offsets are treated as signed so we can end up with a signed value
318+
// here too. For example, suppose the initial PHI value is (i8 255),
319+
// the offset will be treated as (i8 -1) and sign-extended to (i64 -1).
320+
if (Offset->getAPInt().isNegative())
321+
return false;
322+
316323
// For the moment, restrict ourselves to the case where the offset is a
317324
// multiple of the requested alignment and the base is aligned.
318325
// TODO: generalize if a case found which warrants

0 commit comments

Comments
 (0)