Skip to content

Commit

Permalink
[BasicAA] Handle known non-zero variable index
Browse files Browse the repository at this point in the history
BasicAA currently handles cases like Scale*V0 + (-Scale)*V1 where
V0 != V1, but does not handle the simpler case of Scale*V with
V != 0. Add it based on an isKnownNonZero() call.

I'm not passing a context instruction for now, because the existing
approach of always using GEP1 for context could result in symmetry
issues.

Differential Revision: https://reviews.llvm.org/D93162
  • Loading branch information
nikic committed Dec 13, 2020
1 parent 05d1729 commit bb939eb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
8 changes: 6 additions & 2 deletions llvm/lib/Analysis/BasicAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,8 +1288,12 @@ AliasResult BasicAAResult::aliasGEP(
if (V1Size.hasValue() && V2Size.hasValue()) {
// Try to determine whether abs(VarIndex) > 0.
Optional<APInt> MinAbsVarIndex;
// TODO: Could handle single non-zero index as well.
if (DecompGEP1.VarIndices.size() == 2) {
if (DecompGEP1.VarIndices.size() == 1) {
// VarIndex = Scale*V. If V != 0 then abs(VarIndex) >= abs(Scale).
const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
if (isKnownNonZero(Var.V, DL))
MinAbsVarIndex = Var.Scale.abs();
} else if (DecompGEP1.VarIndices.size() == 2) {
// VarIndex = Scale*V0 + (-Scale)*V1.
// If V0 != V1 then abs(VarIndex) >= abs(Scale).
// Check that VisitedPhiBBs is empty, to avoid reasoning about
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Analysis/BasicAA/bug.23626.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ target triple = "x86_64-apple-darwin13.4.0"

; CHECK-LABEL: compute1
; CHECK: MayAlias: i32* %arrayidx8, i32* %out
; CHECK: MayAlias: i32* %arrayidx11, i32* %out
; CHECK: NoAlias: i32* %arrayidx11, i32* %out
; CHECK: MayAlias: i32* %arrayidx11, i32* %arrayidx8
; CHECK: MayAlias: i32* %arrayidx14, i32* %out
; CHECK: NoAlias: i32* %arrayidx14, i32* %out
; CHECK: MayAlias: i32* %arrayidx14, i32* %arrayidx8
; CHECK: MayAlias: i32* %arrayidx11, i32* %arrayidx14
define void @compute1(i32 %num.0.lcssa, i32* %out) {
Expand Down
8 changes: 3 additions & 5 deletions llvm/test/Analysis/BasicAA/sequential-gep.ll
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@ define void @add_non_zero_with_offset(i32* %p, i32 %addend, i32* %q) {
}

; CHECK-LABEL: non_zero_index_simple
; CHECK: MayAlias: i32* %gep, i32* %p
; CHECK: MayAlias: i16* %gep.16, i32* %p
; CHECK: NoAlias: i32* %gep, i32* %p
; CHECK: NoAlias: i16* %gep.16, i32* %p
; CHECK: MayAlias: i32* %p, i64* %gep.64
; TODO: First two could be NoAlias.
define void @non_zero_index_simple(i32* %p, i32* %q) {
%knownnonzero = load i32, i32* %q, !range !0
%gep = getelementptr i32, i32* %p, i32 %knownnonzero
Expand All @@ -125,8 +124,7 @@ define void @non_zero_index_simple(i32* %p, i32* %q) {

; CHECK-LABEL: non_zero_index_with_offset
; CHECK: MayAlias: i32* %gep, i32* %p
; CHECK: MayAlias: i16* %gep.16, i32* %p
; TODO: Last could be NoAlias.
; CHECK: NoAlias: i16* %gep.16, i32* %p
define void @non_zero_index_with_offset(i32* %p, i32* %q) {
%knownnonzero = load i32, i32* %q, !range !0
%p.8 = bitcast i32* %p to i8*
Expand Down

0 comments on commit bb939eb

Please sign in to comment.