https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95859
Bug ID: 95859 Summary: Statically true asserts not recognized as such with -O2, but with -O1, -Og, -O3 Product: gcc Version: 10.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: tobi at gcc dot gnu.org Target Milestone: --- With -O2 compiling for x86-64, the compiler doesn't recognize that a number of assertions in the code are true and they end up as function calls in the assembly output. They are removed with -O1, -Og, -O3 but not with -O2 or -O2 + "all the additional compiler flags for -O3 given in the documentation" (that's a documentation bug on top of this misoptimization). The asserts look something like this in the assembled form: template <N> assertion(int n) { assert(n == N); } and are only called with compile-time constant arguments, i.e. assertion<3>(3) and the like. The final output from the tree passes contains the assertion calls at all optimization levels, so the rtl optimizers appear to perform worst in -O2 in this particular case. I don't see why the calls couldn't be removed before lowering to RTL. I'm not sure how to extract preprocessed output form the compiler explorer, but will provide it once I can put my hands on a recent gcc. Source code reproduced below, the compiler explorer link is here: https://godbolt.org/z/REJDhy #include <Eigen/Geometry> struct m34 { float m[3][4]; }; typedef Eigen::Transform<double, 3, Eigen::AffineCompact, Eigen::DontAlign> unalignedTrafo3d; using Trafo3d = unalignedTrafo3d; Trafo3d func34(m34 mat) { using mapType = Eigen::Map<Eigen::Matrix<float, 3, 4, Eigen::RowMajor>>; // should compile to a conversion of 12 floats to double. auto mR = Trafo3d{ mapType{ (float*)mat.m }.cast<double>() }; return mR; }