Refactor rewriting for PHI nodes into a separate function.