We systemically investigated the effects of Sn doping on the geometrical and electronic properties of TiO2 by means of first-principles electronic structure calculations. Our results indicate a band gap reduction of 0.12 eV when Sn is substituted for Ti in rutile TiO2, leading to a. red-shift of the optical adsorption edge. However, the band gap increases with increasing the Sn doping level. In contrast, when O is replaced by Sn, though this substitutional doping is not energetically preferred, the excitation energy increases slightly compared with the undoped case due to the well-known "band-filling mechanism", leading to a blue-shift of the optical absorption edge. For doped anatase TiO2, the substitution of Sn for Ti may also increase the band gap and hence the optical gap. The results provide explanations not only for the red-shift and blue-shift of the optical absorption edge,in different experiments, but also for the different electronic properties between Sn-doped anatase and rutile TiO2, with a certain Sn content resulting in the transformation of the two phases.