<div dir="ltr">Hi Hal, Nick,<div><br></div><div>It looks like this broke SPEC2k6/464.h264ref on x86-64 (and possibly ARM as well - I'm still investigating there). Do you guys have access to SPEC2k6? Have you seen any failures locally?</div><div><br></div><div>Cheers,</div><div>Lang.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 6, 2014 at 11:38 AM, Hal Finkel <span dir="ltr"><<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: hfinkel<br>
Date: Mon Oct  6 13:37:59 2014<br>
New Revision: 219135<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=219135&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=219135&view=rev</a><br>
Log:<br>
[BasicAA] Revert "Revert r218714 - Make better use of zext and sign information."<br>
<br>
This reverts r218944, which reverted r218714, plus a bug fix.<br>
<br>
Description of the bug in r218714 (by Nick)<br>
<br>
The original patch forgot to check if the Scale in VariableGEPIndex flipped the<br>
sign of the variable. The BasicAA pass iterates over the instructions in the<br>
order they appear in the function, and so BasicAliasAnalysis::aliasGEP is<br>
called with the variable it first comes across as parameter GEP1. Adding a<br>
%reorder label puts the definition of %a after %b so aliasGEP is called with %b<br>
as the first parameter and %a as the second. aliasGEP later calculates that %a<br>
== %b + 1 - %idxprom where %idxprom >= 0 (if %a was passed as the first<br>
parameter it would calculate %b == %a - 1 + %idxprom where %idxprom >= 0) -<br>
ignoring that %idxprom is scaled by -1 here lead the patch to incorrectly<br>
conclude that %a > %b.<br>
<br>
Revised patch by Nick White, thanks! Thanks to Lang to isolating the bug.<br>
Slightly modified by me to add an early exit from the loop and avoid<br>
unnecessary, but expensive, function calls.<br>
<br>
Original commit message:<br>
<br>
Two related things:<br>
<br>
 1. Fixes a bug when calculating the offset in GetLinearExpression. The code<br>
    previously used zext to extend the offset, so negative offsets were converted<br>
    to large positive ones.<br>
<br>
 2. Enhance aliasGEP to deduce that, if the difference between two GEP<br>
    allocations is positive and all the variables that govern the offset are also<br>
    positive (i.e. the offset is strictly after the higher base pointer), then<br>
    locations that fit in the gap between the two base pointers are NoAlias.<br>
<br>
Patch by Nick White!<br>
<br>
Added:<br>
    llvm/trunk/test/Analysis/BasicAA/zext.ll<br>
Modified:<br>
    llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp<br>
    llvm/trunk/test/Analysis/BasicAA/phi-aa.ll<br>
<br>
Modified: llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp?rev=219135&r1=219134&r2=219135&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp?rev=219135&r1=219134&r2=219135&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp (original)<br>
+++ llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp Mon Oct  6 13:37:59 2014<br>
@@ -254,7 +254,10 @@ static Value *GetLinearExpression(Value<br>
     Value *Result = GetLinearExpression(CastOp, Scale, Offset, Extension,<br>
                                         DL, Depth+1, AT, DT);<br>
     Scale = Scale.zext(OldWidth);<br>
-    Offset = Offset.zext(OldWidth);<br>
+<br>
+    // We have to sign-extend even if Extension == EK_ZeroExt as we can't<br>
+    // decompose a sign extension (i.e. zext(x - 1) != zext(x) - zext(-1)).<br>
+    Offset = Offset.sext(OldWidth);<br>
<br>
     return Result;<br>
   }<br>
@@ -1055,8 +1058,38 @@ BasicAliasAnalysis::aliasGEP(const GEPOp<br>
   // Grab the least significant bit set in any of the scales.<br>
   if (!GEP1VariableIndices.empty()) {<br>
     uint64_t Modulo = 0;<br>
-    for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i)<br>
+    bool AllPositive = true;<br>
+    for (unsigned i = 0, e = GEP1VariableIndices.size();<br>
+         i != e && AllPositive; ++i) {<br>
+      const Value *V = GEP1VariableIndices[i].V;<br>
       Modulo |= (uint64_t)GEP1VariableIndices[i].Scale;<br>
+<br>
+      bool SignKnownZero, SignKnownOne;<br>
+      ComputeSignBit(<br>
+        const_cast<Value *>(V),<br>
+        SignKnownZero, SignKnownOne,<br>
+        DL, 0, AT, nullptr, DT);<br>
+<br>
+      // Zero-extension widens the variable, and so forces the sign<br>
+      // bit to zero.<br>
+      bool IsZExt = GEP1VariableIndices[i].Extension == EK_ZeroExt;<br>
+      SignKnownZero |= IsZExt;<br>
+      SignKnownOne &= !IsZExt;<br>
+<br>
+      // If the variable begins with a zero then we know it's<br>
+      // positive, regardless of whether the value is signed or<br>
+      // unsigned.<br>
+      int64_t Scale = GEP1VariableIndices[i].Scale;<br>
+      AllPositive &=<br>
+        (SignKnownZero && Scale >= 0) ||<br>
+        (SignKnownOne && Scale < 0);<br>
+<br>
+      // If the Value is currently positive but could change in a cycle,<br>
+      // then we can't guarantee it'll always br positive.<br>
+      if (AllPositive && !isValueEqualInPotentialCycles(V, V))<br>
+        AllPositive = false;<br>
+    }<br>
+<br>
     Modulo = Modulo ^ (Modulo & (Modulo - 1));<br>
<br>
     // We can compute the difference between the two addresses<br>
@@ -1066,6 +1099,12 @@ BasicAliasAnalysis::aliasGEP(const GEPOp<br>
     if (V1Size != UnknownSize && V2Size != UnknownSize &&<br>
         ModOffset >= V2Size && V1Size <= Modulo - ModOffset)<br>
       return NoAlias;<br>
+<br>
+    // If we know all the variables are positive, then GEP1 >= GEP1BasePtr.<br>
+    // If GEP1BasePtr > V2 (GEP1BaseOffset > 0) then we know the pointers<br>
+    // don't alias if V2Size can fit in the gap between V2 and GEP1BasePtr.<br>
+    if (AllPositive && GEP1BaseOffset > 0 && V2Size <= (uint64_t) GEP1BaseOffset)<br>
+      return NoAlias;<br>
   }<br>
<br>
   // Statically, we can see that the base objects are the same, but the<br>
<br>
Modified: llvm/trunk/test/Analysis/BasicAA/phi-aa.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/BasicAA/phi-aa.ll?rev=219135&r1=219134&r2=219135&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/BasicAA/phi-aa.ll?rev=219135&r1=219134&r2=219135&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/Analysis/BasicAA/phi-aa.ll (original)<br>
+++ llvm/trunk/test/Analysis/BasicAA/phi-aa.ll Mon Oct  6 13:37:59 2014<br>
@@ -39,6 +39,7 @@ return:<br>
<br>
 ; CHECK-LABEL: pr18068<br>
 ; CHECK: MayAlias: i32* %0, i32* %arrayidx5<br>
+; CHECK: NoAlias: i32* %arrayidx13, i32* %arrayidx5<br>
<br>
 define i32 @pr18068(i32* %jj7, i32* %j) {<br>
 entry:<br>
<br>
Added: llvm/trunk/test/Analysis/BasicAA/zext.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/BasicAA/zext.ll?rev=219135&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/BasicAA/zext.ll?rev=219135&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Analysis/BasicAA/zext.ll (added)<br>
+++ llvm/trunk/test/Analysis/BasicAA/zext.ll Mon Oct  6 13:37:59 2014<br>
@@ -0,0 +1,87 @@<br>
+; RUN: opt < %s -basicaa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s<br>
+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"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK-LABEL: test_with_zext<br>
+; CHECK:  NoAlias: i8* %a, i8* %b<br>
+<br>
+define void @test_with_zext() {<br>
+  %1 = tail call i8* @malloc(i64 120)<br>
+  %a = getelementptr inbounds i8* %1, i64 8<br>
+  %2 = getelementptr inbounds i8* %1, i64 16<br>
+  %3 = zext i32 3 to i64<br>
+  %b = getelementptr inbounds i8* %2, i64 %3<br>
+  ret void<br>
+}<br>
+<br>
+; CHECK-LABEL: test_with_lshr<br>
+; CHECK:  NoAlias: i8* %a, i8* %b<br>
+<br>
+define void @test_with_lshr(i64 %i) {<br>
+  %1 = tail call i8* @malloc(i64 120)<br>
+  %a = getelementptr inbounds i8* %1, i64 8<br>
+  %2 = getelementptr inbounds i8* %1, i64 16<br>
+  %3 = lshr i64 %i, 2<br>
+  %b = getelementptr inbounds i8* %2, i64 %3<br>
+  ret void<br>
+}<br>
+<br>
+; CHECK-LABEL: test_with_a_loop<br>
+; CHECK:  NoAlias: i8* %a, i8* %b<br>
+<br>
+define void @test_with_a_loop() {<br>
+  %1 = tail call i8* @malloc(i64 120)<br>
+  %a = getelementptr inbounds i8* %1, i64 8<br>
+  %2 = getelementptr inbounds i8* %1, i64 16<br>
+  br label %for.loop<br>
+<br>
+for.loop:<br>
+  %i = phi i32 [ 0, %0 ], [ %i.next, %for.loop ]<br>
+  %3 = zext i32 %i to i64<br>
+  %b = getelementptr inbounds i8* %2, i64 %3<br>
+  %i.next = add nuw nsw i32 %i, 1<br>
+  %4 = icmp eq i32 %i.next, 10<br>
+  br i1 %4, label %for.loop.exit, label %for.loop<br>
+<br>
+for.loop.exit:<br>
+  ret void<br>
+}<br>
+<br>
+; CHECK-LABEL: test_sign_extension<br>
+; CHECK:  PartialAlias: i64* %b.i64, i8* %a<br>
+<br>
+define void @test_sign_extension(i32 %p) {<br>
+  %1 = tail call i8* @malloc(i64 120)<br>
+  %p.64 = zext i32 %p to i64<br>
+  %a = getelementptr inbounds i8* %1, i64 %p.64<br>
+  %p.minus1 = add i32 %p, -1<br>
+  %p.minus1.64 = zext i32 %p.minus1 to i64<br>
+  %b.i8 = getelementptr inbounds i8* %1, i64 %p.minus1.64<br>
+  %b.i64 = bitcast i8* %b.i8 to i64*<br>
+  ret void<br>
+}<br>
+<br>
+; CHECK-LABEL: test_fe_tools<br>
+; CHECK:  PartialAlias: i32* %a, i32* %b<br>
+<br>
+define void @test_fe_tools([8 x i32]* %values) {<br>
+  br label %reorder<br>
+<br>
+for.loop:<br>
+  %i = phi i32 [ 0, %reorder ], [ %i.next, %for.loop ]<br>
+  %idxprom = zext i32 %i to i64<br>
+  %b = getelementptr inbounds [8 x i32]* %values, i64 0, i64 %idxprom<br>
+  %i.next = add nuw nsw i32 %i, 1<br>
+  %1 = icmp eq i32 %i.next, 10<br>
+  br i1 %1, label %for.loop.exit, label %for.loop<br>
+<br>
+reorder:<br>
+  %a = getelementptr inbounds [8 x i32]* %values, i64 0, i64 1<br>
+  br label %for.loop<br>
+<br>
+for.loop.exit:<br>
+  ret void<br>
+}<br>
+<br>
+; Function Attrs: nounwind<br>
+declare noalias i8* @malloc(i64)<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>