This is an archive of the discontinued LLVM Phabricator instance.

[BasicAA] Make better use of zext and sign information.
ClosedPublic

Authored by njw45 on Oct 20 2014, 5:12 AM.

Details

Reviewers
hfinkel
Summary
  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.

The original patch forgot to check if the Scale in VariableGEPIndex flipped the sign of the variable. The BasicAA pass iterates over the instructions in the order they appear in the function, and so BasicAliasAnalysis::aliasGEP is called with the variable it first comes across as parameter GEP1. Adding a %reorder label puts the definition of %a after %b so aliasGEP is called with %b as the first parameter and %a as the second. aliasGEP later calculates that %a == %b + 1 - %idxprom where %idxprom >= 0 (if %a was passed as the first parameter it would calculate %b == %a - 1 + %idxprom where %idxprom >= 0) - ignoring that %idxprom is scaled by -1 here lead the patch to incorrectly conclude that %a > %b.

This patch now ensures that isValueEqualInPotentialCycles is called for the existing modulo-distance check as it too is only valid if the variable doesn't change across cycles. The fix described in (1) makes this codepath execute more frequently, as variables that were within a small negative distance of each other were being interpreted as being a very large positive distance away from each other. The code now correctly identifies more variables as being outside a scale-modulo-distance of each other, and so declares more variables as NoAlias.

Thanks to Lang to isolating the first bug, Rich Barton for isolating the second bug and Hal for improving the patch.

Diff Detail

Event Timeline

njw45 updated this revision to Diff 15137.Oct 20 2014, 5:12 AM
njw45 retitled this revision from to [BasicAA] Revert "Revert r218714 - Make better use of zext and sign information.".
njw45 updated this object.
njw45 edited the test plan for this revision. (Show Details)
njw45 retitled this revision from [BasicAA] Revert "Revert r218714 - Make better use of zext and sign information." to [BasicAA] Make better use of zext and sign information..Oct 20 2014, 5:25 AM
njw45 updated this object.
njw45 edited the test plan for this revision. (Show Details)
njw45 added a reviewer: hfinkel.
njw45 updated this object.
njw45 updated this object.
njw45 updated this object.
njw45 added a subscriber: Unknown Object (MLST).
hfinkel added inline comments.Oct 22 2014, 11:34 PM
lib/Analysis/BasicAliasAnalysis.cpp
1066

If I change this PartialAlias return to MayAlias, all of the regression tests still pass. Is there a test covering this?

njw45 added a comment.Oct 23 2014, 2:41 PM

You're right - that call doesn't actually affect anything so I've removed it. It seems the actual cause of Rich's bug was using AllPositive to break out of the loop; if the loop break condition i != e is changed to i != e && AllPositive then the test_modulo_analysis_with_global test I've added will fail as the Modulo will be calculated incorrectly (as the last loop iteration is skipped, so Modulo isn't updated with its Scale). I've also added a few more tests and comments for the modulo functionality too, as I spent a bit of time exploring how it works. Thanks -

hfinkel edited edge metadata.Oct 23 2014, 3:42 PM

You're right - that call doesn't actually affect anything so I've removed it. It seems the actual cause of Rich's bug was using AllPositive to break out of the loop; if the loop break condition i != e is changed to i != e && AllPositive then the test_modulo_analysis_with_global test I've added will fail as the Modulo will be calculated incorrectly (as the last loop iteration is skipped, so Modulo isn't updated with its Scale). I've also added a few more tests and comments for the modulo functionality too, as I spent a bit of time exploring how it works. Thanks -

Okay, thanks! I'll retest with this version.

Hi @hfinkel - is this change OK? ComputeSignBit is safe to use in loops as it takes into account phi nodes, and the == EK_ZeroEx check is safe in loops as, no matter how the variable changes between iterations, zero-extensions will always guarantee a zero sign bit. The isValueEqualInPotentialCycles check is therefore definitely not needed as all the variable analysis holds no matter how the variables change between loop iterations. Thanks -

Nick

I still need to run tests with the patch. I hope to get to this later this week.

-Hal

  • Original Message -----

From: "Nick White" <n.j.white@gmail.com>
To: "n j white" <n.j.white@gmail.com>, hfinkel@anl.gov
Cc: llvm-commits@cs.uiuc.edu
Sent: Wednesday, October 29, 2014 12:42:08 PM
Subject: Re: [PATCH] [BasicAA] Make better use of zext and sign information.

Hi @hfinkel - is this change OK? ComputeSignBit is safe to use in
loops as it takes into account phi nodes, and the == EK_ZeroEx
check is safe in loops as, no matter how the variable changes
between iterations, zero-extensions will always guarantee a zero
sign bit. The isValueEqualInPotentialCycles check is therefore
definitely not needed as all the variable analysis holds no matter
how the variables change between loop iterations. Thanks -

Nick

http://reviews.llvm.org/D5866

njw45 updated this revision to Diff 15988.Nov 10 2014, 8:52 AM
njw45 edited edge metadata.

zext

njw45 added a comment.Nov 10 2014, 9:02 AM

Hi @hfinkel - have you had a chance to run your tests on this patch yet? I've added another enhancement to GetLinearExpression - basically to convert ConstantInts to Offsets (see test_const_eval and test_const_eval_scaled for the situations this improves). I've you have I'll submit this change as a separate review, otherwise could you run your tests on this latest version of the patch? Thanks -

Nick

  • Original Message -----

From: "Nick White" <n.j.white@gmail.com>
To: "n j white" <n.j.white@gmail.com>, hfinkel@anl.gov
Cc: llvm-commits@cs.uiuc.edu
Sent: Monday, November 10, 2014 11:02:49 AM
Subject: Re: [PATCH] [BasicAA] Make better use of zext and sign information.

Hi @hfinkel - have you had a chance to run your tests on this patch
yet? I've added another enhancement to GetLinearExpression -
basically to convert ConstantInts to Offsets (see
test_const_eval and test_const_eval_scaled for the
situations this improves). I've you have I'll submit this change as
a separate review, otherwise could you run your tests on this latest
version of the patch? Thanks -

I'll do that.

-Hal

Nick

http://reviews.llvm.org/D5866

hfinkel accepted this revision.Nov 13 2014, 1:18 AM
hfinkel edited edge metadata.
This revision is now accepted and ready to land.Nov 13 2014, 1:18 AM
hfinkel closed this revision.Nov 13 2014, 1:18 AM

r221876.