instruction combiner patch

I once wrote:

I've patched the instruction combiner to return UndefValue when the
index is constant and known to be out of range for either an ArrayType
or PackedType.

That patch had to be removed as it broke SPEC. I've since updated the
patch, and am ready to break spec again. :wink: This patch adds constant
folding of multidimensional arrays even when the index to those arrays
is a single value, ala:

  int x[2][2] = { {40, 41}, {42, 43} };
  int *y = x;
  return y[2];

becomes:

  return 42;

Similarly, it also generates undef on out of range arrays, but only if
it's out of the range of the whole multi-dimensional array this time.

Patch attached.

Nick Lewycky

multidimensionalindex.patch (7.89 KB)

What would it do for:

struct foo {
int x;
int y[0];
};
...
struct foo* f = ...;
f->y[10];

This type of things is quite common and making it undef will break
things. I don't think the undef part can work safely.

Andrew

Andrew Lenharth wrote:

What would it do for:

struct foo {
int x;
int y[0];
};
...
struct foo* f = ...;
f->y[10];

This type of things is quite common and making it undef will break
things. I don't think the undef part can work safely.

It works fine. Constant folding of loads only applies to cases where the
array is a known constant at compile time, so no transformation is done
here.

Nick