From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 1E2663858D34; Mon, 27 May 2024 09:29:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1E2663858D34 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1716802170; bh=picxtPLTqOzqiGE6hSKLrVAG2Tj6iqMC9sSa27c+bE8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=m3L84kejEAxTeZQlbZgAZoPsykYy9T5IfLeJsLh1uP0YU6Bx3oEXYwu24AzYuwZcf 5S6RF2D9GWoperJ5DKNRR6ZFvMMT3IYE/HqZpY+pJtrgzakm92Xs8zdL+U8kA+n/qq p+ziDAxeXT8NgLq42qbRifiZ6Njy9mqiJgjyo9MQ= From: "user202729 at protonmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug ipa/110057] Missed devirtualization opportunities Date: Mon, 27 May 2024 09:29:27 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: ipa X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: user202729 at protonmail dot com 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: cc 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D110057 user202729 changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |user202729 at protonmail d= ot com --- Comment #11 from user202729 --- One way this could be implemented is the following. * implement built-in `__builtin_vptr(object)` and `__builtin_vptr(Type)` th= at obtains the pointer to the vtable (or NULL if the object type has no virtual method) * in the implementation `vector.pop_back()` etc., add the the following: if (__builtin_vptr(this->_M_impl._M_finish) !=3D __builtin_vptr(_Tp)) __builtin_unreachable(); * the optimizer should be able to figure out the rest. What do you think about this approach? -------- Alternatively, we can also use the existing `typeid()` --- although the optimizer is not smart enough to optimize the following code yet. ``` #include #include struct Base{ Base() {} virtual void f(){ std::printf("Base\n"); } }; struct Derived final: Base{ Derived() {} virtual void f(){ std::printf("Derived\n"); } }; void f(Base* b){ if(!b or typeid(*b)!=3Dtypeid(Base)) __builtin_unreachable(); b->f(); } ```=