[LLVMdev] GEP instructions: is it possible to reverse-engineer array accesses?

Dan Gohman gohman at apple.com
Tue Oct 18 11:13:22 PDT 2011

On Oct 18, 2011, at 10:00 AM, Gabriel Rodríguez wrote:

> Dear All,
> As of late I am having a hard time getting my head around how array accesses
> are translated by Clang into LLVM IR: the often misunderstood GEP instruction.
> I am trying to reverse-engineer array accesses to discover the number of dimensions
> and actual indexes of the original, and I am beginning to wonder whether this is
> possible at all. To illustrate (some of) my troubles, consider the following code and
> the LLVM IR for both 32 and 64 bit memory addresses:
> --
> original C:
> #define N 1000
> int main(int argc, char **argv)
> {
>   int i, k;
>   float aux, A[N][N];

See "How does VLA addressing work with GEPs?" in the GEP FAQ.  In short, you have to
reverse-engineer, and the ScalarEvolution library can help you with that.

Also, one thing not mentioned in the FAQ is that if you want to assume that the dimensions
are independent (in other words, that the inner dimension is never over-indexed), you
have to prove it for yourself. Even though overindexing may be prohibited at the source level,
it's valid at the LLVM IR level, and some LLVM optimizations do use it.  ScalarEvolution
can help with this as well, though it doesn't do everything.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20111018/f82bf28e/attachment.html>

More information about the llvm-dev mailing list