The origin of planets orbiting millisecond pulsars