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. ;-) 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 -------------- next part -------------- A non-text attachment was scrubbed... Name: multidimensionalindex.patch Type: text/x-patch Size: 8076 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20060607/823e45fe/attachment.bin>
On Wed, 2006-06-07 at 03:17 -0400, Nick Lewycky wrote:> 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. ;-) 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.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