Automatic Loop Tuning and Memory Management for Stencil Computations