This is an archive of the discontinued LLVM Phabricator instance.

[BasicAA] Compare GEP indices based on value
ClosedPublic

Authored by vsk on May 4 2016, 1:20 PM.

Details

Summary

Equivalent GEP indices with different types are treated as different
indices altogether, leading to a loss in AA precision. Fix the issue
(PR27418) by comparing indices based on their values.

Diff Detail

Repository
rL LLVM

Event Timeline

vsk updated this revision to Diff 56196.May 4 2016, 1:20 PM
vsk retitled this revision from to [BasicAA] Compare GEP indices based on value.
vsk updated this object.
vsk added a reviewer: ab.
vsk added subscribers: llvm-commits, spatel, hfinkel.
ab edited edge metadata.May 5 2016, 12:49 PM

Good catch; scary bug! The test is somewhat brittle though. What do you think of boiling it down to its essence; something like:

; PR27418 - Treat GEP indices with the same value but different types the same
; CHECK-LABEL: test_different_index_types
; CHECK: MustAlias: i16* %tmp1, i16* %tmp2
define void @test_different_index_types([2 x i16]* %arr) {
  %tmp1 = getelementptr [2 x i16], [2 x i16]* %arr, i16 0, i32 1
  %tmp2 = getelementptr [2 x i16], [2 x i16]* %arr, i16 0, i16 1
  ret void
}

in struct-geps.ll ? Looks like that actually used to return NoAlias (because of the assumption that C1 && C2 implies C1 != C2 later on).

vsk updated this revision to Diff 56454.May 6 2016, 1:34 PM
vsk edited edge metadata.
  • Updated with Ahmed's suggested test case.

Great! We've done a fix like this locally to get around the problem and it's worked so far.

However, I'm a little bit worried that there could be other places in AA where GEPs are considered to be noaliasing only based on checking Value pointers, e.g in

bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,

                                                const Value *V2) {
if (V != V2)
  return false;

but I don't know the code well enough to know if the code above can cause any similar problems or not.

ab accepted this revision.May 11 2016, 2:52 AM
ab edited edge metadata.

However, I'm a little bit worried that there could be other places in AA where GEPs are considered to be noaliasing only based on checking Value pointers, e.g in

bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,

                                                const Value *V2) {
if (V != V2)
  return false;

but I don't know the code well enough to know if the code above can cause any similar problems or not.

You're absolutely right that this problem could lurk elsewhere; isValueEqualInPotentialCycles was actually added to avoid that problem in r198290, for PHI cycles though. I think the same problem could occur for no-op instructions ("alias((add x, 0), x)"), but it looks like all the current users are safe. I'm not sure I have an answer to the general problem though :/

Re: this particular issue though: LGTM, thanks Vedant!

This revision is now accepted and ready to land.May 11 2016, 2:52 AM
This revision was automatically updated to reflect the committed changes.