From a0715579f0023db159d56a4b1dd8c7549c713498 Mon Sep 17 00:00:00 2001 From: Hal Finkel Date: Tue, 30 Sep 2014 22:43:40 +0000 Subject: [PATCH] [BasicAA] Make better use of zext and sign information Two related things: 1. Fixes a bug when calculating the offset in GetLinearExpression. The code previously used zext to extend the offset, so negative offsets were converted to large positive ones. 2. Enhance aliasGEP to deduce that, if the difference between two GEP allocations is positive and all the variables that govern the offset are also positive (i.e. the offset is strictly after the higher base pointer), then locations that fit in the gap between the two base pointers are NoAlias. Patch by Nick White! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218714 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/BasicAliasAnalysis.cpp | 31 +++++++++++++- test/Analysis/BasicAA/phi-aa.ll | 1 + test/Analysis/BasicAA/zext.ll | 65 +++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 test/Analysis/BasicAA/zext.ll diff --git a/lib/Analysis/BasicAliasAnalysis.cpp b/lib/Analysis/BasicAliasAnalysis.cpp index 9cfd02c0218..f4052b9dbab 100644 --- a/lib/Analysis/BasicAliasAnalysis.cpp +++ b/lib/Analysis/BasicAliasAnalysis.cpp @@ -254,7 +254,10 @@ static Value *GetLinearExpression(Value *V, APInt &Scale, APInt &Offset, Value *Result = GetLinearExpression(CastOp, Scale, Offset, Extension, DL, Depth+1, AT, DT); Scale = Scale.zext(OldWidth); - Offset = Offset.zext(OldWidth); + + // We have to sign-extend even if Extension == EK_ZeroExt as we can't + // decompose a sign extension (i.e. zext(x - 1) != zext(x) - zext(-1)). + Offset = Offset.sext(OldWidth); return Result; } @@ -1055,8 +1058,26 @@ BasicAliasAnalysis::aliasGEP(const GEPOperator *GEP1, uint64_t V1Size, // Grab the least significant bit set in any of the scales. if (!GEP1VariableIndices.empty()) { uint64_t Modulo = 0; - for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i) + bool AllPositive = true; + for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i) { + const Value *V = GEP1VariableIndices[i].V; Modulo |= (uint64_t)GEP1VariableIndices[i].Scale; + + // If the variable's been zero-extended or begins with a zero then + // we know it's positive. regardless of whether the value is signed + // or unsigned. + bool SignKnownZero, SignKnownOne; + ComputeSignBit( + const_cast(V), + SignKnownZero, SignKnownOne, + DL, 0, AT, nullptr, DT); + bool IsZExt = GEP1VariableIndices[i].Extension == EK_ZeroExt; + AllPositive &= IsZExt || SignKnownZero; + + // If the Value is currently positive but could change in a cycle, + // then we can't guarantee it'll always br positive. + AllPositive &= isValueEqualInPotentialCycles(V, V); + } Modulo = Modulo ^ (Modulo & (Modulo - 1)); // We can compute the difference between the two addresses @@ -1066,6 +1087,12 @@ BasicAliasAnalysis::aliasGEP(const GEPOperator *GEP1, uint64_t V1Size, if (V1Size != UnknownSize && V2Size != UnknownSize && ModOffset >= V2Size && V1Size <= Modulo - ModOffset) return NoAlias; + + // If we know all the variables are positive, then GEP1 >= GEP1BasePtr. + // If GEP1BasePtr > V2 (GEP1BaseOffset > 0) then we know the pointers + // don't alias if V2Size can fit in the gap between V2 and GEP1BasePtr. + if (AllPositive && GEP1BaseOffset > 0 && V2Size <= (uint64_t) GEP1BaseOffset) + return NoAlias; } // Statically, we can see that the base objects are the same, but the diff --git a/test/Analysis/BasicAA/phi-aa.ll b/test/Analysis/BasicAA/phi-aa.ll index 74279e1c4c9..c1100f1d36f 100644 --- a/test/Analysis/BasicAA/phi-aa.ll +++ b/test/Analysis/BasicAA/phi-aa.ll @@ -39,6 +39,7 @@ return: ; CHECK-LABEL: pr18068 ; CHECK: MayAlias: i32* %0, i32* %arrayidx5 +; CHECK: NoAlias: i32* %arrayidx13, i32* %arrayidx5 define i32 @pr18068(i32* %jj7, i32* %j) { entry: diff --git a/test/Analysis/BasicAA/zext.ll b/test/Analysis/BasicAA/zext.ll new file mode 100644 index 00000000000..07912d5a4ea --- /dev/null +++ b/test/Analysis/BasicAA/zext.ll @@ -0,0 +1,65 @@ +; RUN: opt < %s -basicaa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; CHECK-LABEL: test_with_zext +; CHECK: NoAlias: i8* %a, i8* %b + +define void @test_with_zext() { + %1 = tail call i8* @malloc(i64 120) + %a = getelementptr inbounds i8* %1, i64 8 + %2 = getelementptr inbounds i8* %1, i64 16 + %3 = zext i32 3 to i64 + %b = getelementptr inbounds i8* %2, i64 %3 + ret void +} + +; CHECK-LABEL: test_with_lshr +; CHECK: NoAlias: i8* %a, i8* %b + +define void @test_with_lshr(i64 %i) { + %1 = tail call i8* @malloc(i64 120) + %a = getelementptr inbounds i8* %1, i64 8 + %2 = getelementptr inbounds i8* %1, i64 16 + %3 = lshr i64 %i, 2 + %b = getelementptr inbounds i8* %2, i64 %3 + ret void +} + +; CHECK-LABEL: test_with_a_loop +; CHECK: NoAlias: i8* %a, i8* %b + +define void @test_with_a_loop() { + %1 = tail call i8* @malloc(i64 120) + %a = getelementptr inbounds i8* %1, i64 8 + %2 = getelementptr inbounds i8* %1, i64 16 + br label %for.loop + +for.loop: + %i = phi i32 [ 0, %0 ], [ %i.next, %for.loop ] + %3 = zext i32 %i to i64 + %b = getelementptr inbounds i8* %2, i64 %3 + %i.next = add nuw nsw i32 %i, 1 + %4 = icmp eq i32 %i.next, 10 + br i1 %4, label %for.loop.exit, label %for.loop + +for.loop.exit: + ret void +} + +; CHECK-LABEL: test_sign_extension +; CHECK: PartialAlias: i64* %b.i64, i8* %a + +define void @test_sign_extension(i32 %p) { + %1 = tail call i8* @malloc(i64 120) + %p.64 = zext i32 %p to i64 + %a = getelementptr inbounds i8* %1, i64 %p.64 + %p.minus1 = add i32 %p, -1 + %p.minus1.64 = zext i32 %p.minus1 to i64 + %b.i8 = getelementptr inbounds i8* %1, i64 %p.minus1.64 + %b.i64 = bitcast i8* %b.i8 to i64* + ret void +} + +; Function Attrs: nounwind +declare noalias i8* @malloc(i64) -- 2.34.1