Fundamental Limits of Two-layer Autoencoders, and Achieving Them with Gradient Methods