Through introducing a generalized optimal speed function to consider spatial position, slope grade and variable safe headway, the effect of slope in a single-lane highway on the traffic flow is investigated with the extended optimal speed model. The theoretical analysis and simulation results show that the flux of the whole road with the upgrade (or downgrade) increases linearly with density, saturates at a critical density, then maintains this saturated value in a certain density range and finally decreases with density. The value of saturated flux is equal to the maximum flux of the upgrade (or downgrade) without considering the slight influence of the driver's sensitivity. And the fundamental diagrams also depend on sensitivity, slope grade and slope length. The spatiotemporal pattern gives the segregation of different traffic phases caused by the rarefaction wave and the shock wave under a certain initial vehicle number. A comparison between the upgrade and the downgrade indicates that the value of saturated flux of the downgrade is larger than that of the upgrade under the same condition. This result is in accordance with the real traffic.