Optimizing Memory Cost With Loop Transformations