We study the antiferrodistortive instability and its interaction with ferroelectricity in cubic perovskite compounds. Our first-principles calculations show that coexistence of both instabilities is very common. We develop a first-principles scheme to study the thermodynamics of these compounds when both instabilities are present, and apply it to SrTiO$_3$. We find that increased pressure enhances the antiferrodistortive instability while suppressing the ferroelectric one. Moreover, the presence of one instability tends to suppress the other. A very rich $P$--$T$ phase diagram results.