Novak Djokovic rallies to reach Shanghai semifinals