This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] Fold ne/eq comparison of a pointer produced by a noalias function
AbandonedPublic

Authored by xbolva00 on Sep 17 2018, 1:26 AM.

Details

Reviewers
spatel
hfinkel
Summary

Example:

declare noalias i8* @malloc(i64)

define i1 @compare_noalias(i64 %x, i8* %y) {

%m = call i8* @malloc(i64 %x)
%r = icmp eq i8* %m, %y
ret i1 %r

}

In this case %r must be false because @malloc has the 'noalias' attribute.

Fixes PR38956

Diff Detail

Repository
rL LLVM

Event Timeline

xbolva00 created this revision.Sep 17 2018, 1:26 AM

Are there tests with comparisons with nullptr? (Also mind the nullptr is valid thingy)

test/Transforms/InstSimplify/icmp-noalias-pointers.ll
22–23

Just return i1? I don't think we care about anything else other than the i1.

Yes, I will add more tests. Suggestions welcome.

test/Transforms/InstSimplify/icmp-noalias-pointers.ll
22–23

Ok, I will change it.

xbolva00 updated this revision to Diff 165725.Sep 17 2018, 2:00 AM
hfinkel requested changes to this revision.Sep 17 2018, 10:52 AM
hfinkel added a subscriber: hfinkel.

Unfortunately, I don't think that we can do this. noalias semantics only provide information about the overlap of memory accesses using the resulting pointers, in the context of other necessary constraints, and does not strongly constrain the pointer values. For example, it might be that:

int *x = malloc(n);
free(x);
int *y = malloc(n);
if (y == x) // should we always fold this to false?
  printf("malloc reused memory");

Also, what happens if the result is null, and you're comparing to a value which also happens to be null?

This revision now requires changes to proceed.Sep 17 2018, 10:52 AM
xbolva00 abandoned this revision.Sep 17 2018, 11:06 AM
xbolva00 reclaimed this revision.Sep 17 2018, 11:11 AM

Ok.. so maybe

  • disable this fold when comparing noalias ptr with null
  • disable also when cmp' rhs and lhs are both noalias ptrs

?

Any further caveats?

This revision now requires changes to proceed.Sep 17 2018, 11:11 AM

int f(int n) {

int *x = (int *)malloc(n);
free(x);
int *y =  (int *)malloc(n);
return (y == x) ;

}

This has been already folded to true even before this patch.

disable this fold when comparing noalias ptr with null

But it's not just comparing to a nullptr constant, it's also comparing to another SSA value that, dynamically, is null.

int f(int n) {

int *x = (int *)malloc(n);
free(x);
int *y =  (int *)malloc(n);
return (y == x) ;

}

This has been already folded to true even before this patch.

You need to look at how this folding happens? Does it still happen if you actually use the allocation for something? We fold unused allocations away, as I recall.

Yes, we only fold if it is not used. :)

disable this fold when comparing noalias ptr with null

But it's not just comparing to a nullptr constant, it's also comparing to another SSA value that, dynamically, is null.

Yeah.

But like the original idea was, we could handle comparison of noalias ptr to ptr pointing to global vars/functions, no?

xbolva00 abandoned this revision.Sep 24 2018, 8:25 PM