Skip to content

[BasicAA] Account for wrapping when using abs(Scale*V0 + (-Scale)*V1) >= abs(Scale) #137755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions llvm/lib/Analysis/BasicAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,23 @@ AliasResult BasicAAResult::aliasGEP(
if (Range1.intersectWith(Range2).isEmptySet())
return AliasResult::NoAlias;

// Check if abs(V*Scale) >= abs(Scale) holds in the presence of
// potentially wrapping math.
auto MultiplyByScaleNoWrap = [](const VariableGEPIndex &Var) {
if (Var.IsNSW)
return true;

int ValOrigBW = Var.Val.V->getType()->getPrimitiveSizeInBits();
// If Scale is small enough so that abs(V*Scale) >= abs(Scale) holds.
// The max value of abs(V) is 2^ValOrigBW - 1. Multiplying with a
// constant smaller than 2^(bitwidth(Val) - ValOrigBW) won't wrap.
int MaxScaleValueBW = Var.Val.getBitWidth() - ValOrigBW;
if (MaxScaleValueBW <= 0)
return false;
return Var.Scale.ule(
APInt::getMaxValue(MaxScaleValueBW).zext(Var.Scale.getBitWidth()));
};

// Try to determine the range of values for VarIndex such that
// VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex.
std::optional<APInt> MinAbsVarIndex;
Expand All @@ -1309,22 +1326,6 @@ AliasResult BasicAAResult::aliasGEP(
const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
if (Var.Val.TruncBits == 0 &&
isKnownNonZero(Var.Val.V, SimplifyQuery(DL, DT, &AC, Var.CxtI))) {
// Check if abs(V*Scale) >= abs(Scale) holds in the presence of
// potentially wrapping math.
auto MultiplyByScaleNoWrap = [](const VariableGEPIndex &Var) {
if (Var.IsNSW)
return true;

int ValOrigBW = Var.Val.V->getType()->getPrimitiveSizeInBits();
// If Scale is small enough so that abs(V*Scale) >= abs(Scale) holds.
// The max value of abs(V) is 2^ValOrigBW - 1. Multiplying with a
// constant smaller than 2^(bitwidth(Val) - ValOrigBW) won't wrap.
int MaxScaleValueBW = Var.Val.getBitWidth() - ValOrigBW;
if (MaxScaleValueBW <= 0)
return false;
return Var.Scale.ule(
APInt::getMaxValue(MaxScaleValueBW).zext(Var.Scale.getBitWidth()));
};
// Refine MinAbsVarIndex, if abs(Scale*V) >= abs(Scale) holds in the
// presence of potentially wrapping math.
if (MultiplyByScaleNoWrap(Var)) {
Expand All @@ -1341,6 +1342,7 @@ AliasResult BasicAAResult::aliasGEP(
const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1];
if (Var0.hasNegatedScaleOf(Var1) && Var0.Val.TruncBits == 0 &&
Var0.Val.hasSameCastsAs(Var1.Val) && !AAQI.MayBeCrossIteration &&
MultiplyByScaleNoWrap(Var0) && MultiplyByScaleNoWrap(Var1) &&
isKnownNonEqual(Var0.Val.V, Var1.Val.V,
SimplifyQuery(DL, DT, &AC, /*CxtI=*/Var0.CxtI
? Var0.CxtI
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/Analysis/BasicAA/gep-modulo.ll
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,17 @@ entry:
}

declare void @llvm.assume(i1)

define i64 @mul_may_overflow_var_nonzero_minabsvarindex_two_index(i64 %arg, ptr %arg1) {
; CHECK-LABEL: Function: mul_may_overflow_var_nonzero_minabsvarindex_two_index
; CHECK-LABEL: MayAlias: i64* %getelementptr, i64* %getelementptr2
bb:
%xor = xor i64 %arg, -9223372036854775808
%getelementptr = getelementptr i64, ptr %arg1, i64 %xor
%load = load i64, ptr %getelementptr, align 8
%getelementptr2 = getelementptr i64, ptr %arg1, i64 %arg
store i64 1, ptr %getelementptr2, align 8
%load3 = load i64, ptr %getelementptr, align 8
%mul = mul i64 %load, %load3
ret i64 %mul
}