From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 38453 invoked by alias); 14 Oct 2015 11:06:27 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 38073 invoked by uid 48); 14 Oct 2015 11:06:22 -0000 From: "morwenn29 at hotmail dot fr" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/67962] New: Optimization opportunity with conditional swap Date: Wed, 14 Oct 2015 11:06:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 5.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: minor X-Bugzilla-Who: morwenn29 at hotmail dot fr 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-10/txt/msg01080.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67962 Bug ID: 67962 Summary: Optimization opportunity with conditional swap Product: gcc Version: 5.2.0 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: morwenn29 at hotmail dot fr Target Milestone: --- If have some algorithms that use an extensive number of conditional swaps like this (a few hundreds I guess): if (y < x) { std::swap(x, y); } I thought that such a construct could be optimized by the compiler, but it appears that the following function is more performant with integers most of the time: void swap_if(int& x, int& y) { int dx = x; int dy = y; int tmp = x = std::min(dx, dy); y ^= dx ^ tmp; } Would it be possible for g++ to recognize this kind of construct and optimize it, at least for integer types? Reordering two values seems like something common enough so that optimizing it could also benefit existing code. As a side note, I hope that Bugzilla is he right place for this kind of request. Sorry if it isn't.