InstCombine: Fold comparisons between unguessable allocas and other pointers
authorHans Wennborg <hans@hanshq.net>
Wed, 7 Oct 2015 00:20:07 +0000 (00:20 +0000)
committerHans Wennborg <hans@hanshq.net>
Wed, 7 Oct 2015 00:20:07 +0000 (00:20 +0000)
commit70c52f7d6cb88b275d7ef235745dc61d292e8abe
tree45a19262951f3b99ba00906815b2c3e1794b317a
parentd545f1a5b888425babb7f3f7d5c8540ea0e04391
InstCombine: Fold comparisons between unguessable allocas and other pointers

This will allow us to optimize code such as:

  int f(int *p) {
    int x;
    return p == &x;
  }

as well as:

  int *allocate(void);
  int f() {
    int x;
    int *p = allocate();
    return p == &x;
  }

The folding can only be done under certain circumstances. Even though p and &x
cannot alias, the comparison must still return true if the pointer
representations are equal. If a user successfully generates a p that's a
correct guess for &x, comparison should return true even though p is an invalid
pointer.

This patch argues that if the address of the alloca isn't observable outside the
function, the function can act as-if the address is impossible to guess from the
outside. The tricky part is keeping the act consistent: if we fold p == &x to
false in one place, we must make sure to fold any other comparisons based on
those pointers similarly. To ensure that, we only fold when &x is involved
exactly once in comparison instructions.

Differential Revision: http://reviews.llvm.org/D13358

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249490 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/InstCombine/InstCombineCompares.cpp
lib/Transforms/InstCombine/InstCombineInternal.h
test/Transforms/InstCombine/compare-alloca.ll [new file with mode: 0644]