Rafael Nadal produced a stirring fightback to beat Denis Shapovalov and reach the Italian Open quarter-finals.