From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id D57183839C58; Wed, 16 Jun 2021 11:10:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D57183839C58 From: "rguenther at suse dot de" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/101061] tree-vrp misoptimization on skylake+ using union-based aliasing Date: Wed, 16 Jun 2021 11:10:12 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 8.5.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: rguenther at suse dot de X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Jun 2021 11:10:13 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101061 --- Comment #14 from rguenther at suse dot de --- On Wed, 16 Jun 2021, alexander.grund@tu-dresden.de wrote: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101061 >=20 > --- Comment #13 from Alexander Grund --- > > But what you can see is that the resulting pointer is used for the init= ialization and not the placement address as literally written in source. >=20 > So I assume it was supposed to be "Y::Y (D_6557, 1);" ? >=20 > > I'm not sure how one can solve this issue with using placement new > > but are unions not sufficiently restricted so that copy assignment > > should work (and activate the appropriate union member)? Thus > >=20 > > slot->mutable_value =3D pair(k, v); >=20 > The problem is not the copy, the problem is that the value may contain an= y kind > of data, think e.g. a pair of strings. And at the initial point (i.e. fir= st > emplace) the slot is a casted pointer into uninitialized data. I.e. the a= bove > would be an assignment into an object which does not exist. And (especial= ly) > for such non-trivial types this would break. >=20 > I think it will work for trivial types though, although it is UB due to > lifetime rules: You can't use an object (here: assign to) which has not s= tarted > its lifetime yet. I see. I would need to read up what kind of restrictions recent C++ standards place on union members, but in C a store to a non-active union member makes that active and IIRC for tradidional POD data types the same should hold true for C++, even w/o requiring an explicit placement new. > However e.g. pair has custom copy and regular constructors so I think it = will > run into the issue you mentioned: The ctor will access the object via the > this-pointer and not via the full union-thing and hence might misoptimise= later >=20 > This would mean that in conclusion the use case of putting std::pairs in = an > union and accessing them via aliasing is unsupported by (at least) GCC. I= s that > correct? Without restricting the set of C++ features used, yes. Even accessing the data via union.memb.getX (); would involve a 'this' pointer and thus break things. std::pair is probably a special-case that might work since you use pair.first rather than a method though ;) You can see this type-punning via unions exception was invented for C ;)=