https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77295
Richard Biener <rguenth at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |NEW Last reconfirmed| |2016-08-19 Ever confirmed|0 |1 --- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> --- Why does the implementation not simply copy the whole union with an assignment? It's easier for GCC to optimize that. Because with the current one you'll have <bb 2>: MEM[(struct &)this_7(D)] ={v} {CLOBBER}; _1 = x_9(D)->which; this_7(D)->which = _1; _2 = x_9(D)->which; switch (_2) <default: <L2>, case 1: <L0>, case 2: <L1>> <L0>: _3 = x_9(D)->D.2254.v1; this_7(D)->D.2254.v1 = _3; goto <bb 5> (<L2>); <L1>: _4 = x_9(D)->D.2254.v2; this_7(D)->D.2254.v2 = _4; <L2>: return; where it's hard to argue the desired transform is profitable in general (x->which might get known via inlining and if you'd optimize the above earlier then you'd at least lose TBAA info on the stores / loads). Also note sth else above - we read from x->which twice (this may point to x). There's a related PR somewhere about this missed optimization, with a patch (that has cost even if that special situation doesn't apply, thus I didn't pursue it).