https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86062
Bug ID: 86062 Summary: Missed redundancy elimination with struct and array Product: gcc Version: 9.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: glisse at gcc dot gnu.org Target Milestone: --- #include <array> struct I { double i,s; I(double d):i(d),s(d){} }; typedef std::array<double,3> P; typedef std::array<I,3> AP; static AP c(P const&p){ return {p[0],p[1],p[2]}; } template<class T> auto const& ac(T const&p, int n){return p[n];} static double g(P const&p, int n) { I res = ac(c(p),n); return res.s-res.i; } __attribute__((flatten)) double fff(P p){ return g(p,1); } compiles at -O2 to the following double res$8; struct AP D.30336; struct I res; double _2; double _3; <bb 2> [local count: 1073741825]: _2 = MEM[(const value_type &)&p + 8]; MEM[(const struct I &)&D.30336 + 16] = _2; MEM[(const struct I &)&D.30336 + 24] = _2; res = MEM[(const struct I &)&D.30336][1]; res$8_13 = MEM[(struct I *)&res + 8B]; D.30336 ={v} {CLOBBER}; _3 = res$8_13 - _2; return _3; Or with -O3 double res$8; struct AP D.30336; struct I res; double _2; double _3; vector(2) double _9; <bb 2> [local count: 1073741825]: _2 = MEM[(const value_type &)&p + 8]; _9 = {_2, _2}; MEM[(const struct I &)&D.30336 + 16] = _9; res = MEM[(const struct I &)&D.30336][1]; res$8_13 = MEM[(struct I *)&res + 8B]; D.30336 ={v} {CLOBBER}; _3 = res$8_13 - _2; return _3; While we should be able to notice that res$8_13 and 2 are the same (and thus simplify to 0 with -ffast-math).