Additive manufacturing (AM) or three-dimensional (3D) printing is a promising technology that enables the direct fabrication of products with complex shapes without extra tooling and fixturing. However, control of 3D shape deformation in AM built products has been a challenging issue due to geometric complexity, product varieties, material phase changing and shrinkage, and interlayer bonding. One viable approach for accuracy control is through compensation of the product design to offset the geometric shape deformation. This work provides an analytical foundation to achieve optimal compensation for high-precision AM. We first present the optimal compensation policy or the optimal amount of compensation for two-dimensional (2D) shape deformation. By analyzing its optimality property, we propose the minimum area deviation (MAD) criterion to offset 2D shape deformation. This result is then generalized by establishing the minimum volume deviation (MVD) criterion and by deriving the optimal amount of compensation for 3D shape deformation. Furthermore, MAD and MVD criteria provide convenient quality measure or quality index for AM built products that facilitate online monitoring and feedback control of shape geometric accuracy.