Theory of phonon dynamics near a charge-density-wave instability