Skip to content

Commit

Permalink
[BasicAA] Account for wrapping when using abs(VarIndex) >= abs(Scale).
Browse files Browse the repository at this point in the history
The patch adds an extra check to only set MinAbsVarIndex if
abs(V * Scale) won't wrap. In the absence of IsNSW, try to use the
bitwidths of the original V and Scale to rule out wrapping.

Attempt to model https://alive2.llvm.org/ce/z/HE8ZKj

The code in the else if below probably needs the same treatment, but I
need to come up with a test first.

Reviewed By: asbirlea

Differential Revision: https://reviews.llvm.org/D121695
  • Loading branch information
fhahn committed Mar 18, 2022
1 parent a36c2dd commit 1b7ef6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
27 changes: 25 additions & 2 deletions llvm/lib/Analysis/BasicAliasAnalysis.cpp
Expand Up @@ -1297,8 +1297,31 @@ AliasResult BasicAAResult::aliasGEP(
const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
if (Var.Val.TruncBits == 0 &&
isKnownNonZero(Var.Val.V, DL, 0, &AC, Var.CxtI, DT)) {
// If V != 0 then abs(VarIndex) >= abs(Scale).
MinAbsVarIndex = Var.Scale.abs();
// If V != 0, then abs(VarIndex) > 0.
MinAbsVarIndex = APInt(Var.Scale.getBitWidth(), 1);

// 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)) {
// If V != 0 then abs(VarIndex) >= abs(Scale).
MinAbsVarIndex = Var.Scale.abs();
}
}
} else if (DecompGEP1.VarIndices.size() == 2) {
// VarIndex = Scale*V0 + (-Scale)*V1.
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Analysis/BasicAA/gep-modulo.ll
Expand Up @@ -323,9 +323,9 @@ define void @may_overflow_mul_scale_neg([200 x [ 6 x i8]]* %ptr, i64 %idx.1,i64
; If %v == 10581764700698480926, %idx == 917, so %gep.917 and %gep.idx may alias.
define i8 @mul_may_overflow_var_nonzero_minabsvarindex_one_index([2000 x i8]* %arr, i8 %x, i64 %v) {
; CHECK-LABEL: Function: mul_may_overflow_var_nonzero_minabsvarindex_one_index: 4 pointers, 0 call sites
; CHECK-NEXT: NoAlias: [2000 x i8]* %arr, i8* %gep.idx
; CHECK-NEXT: MayAlias: [2000 x i8]* %arr, i8* %gep.idx
; CHECK-NEXT: PartialAlias (off 917): [2000 x i8]* %arr, i8* %gep.917
; CHECK-NEXT: NoAlias: i8* %gep.917, i8* %gep.idx
; CHECK-NEXT: MayAlias: i8* %gep.917, i8* %gep.idx
; CHECK-NEXT: MustAlias: [2000 x i8]* %arr, i8* %gep.0
; CHECK-NEXT: NoAlias: i8* %gep.0, i8* %gep.idx
; CHECK-NEXT: NoAlias: i8* %gep.0, i8* %gep.917
Expand Down

0 comments on commit 1b7ef6a

Please sign in to comment.