The gamma-ray luminosity function of millisecond pulsars and implications for the GeV excess