From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22440 invoked by alias); 1 Feb 2011 19:52:17 -0000 Received: (qmail 22423 invoked by uid 22791); 1 Feb 2011 19:52:16 -0000 X-SWARE-Spam-Status: No, hits=-2.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,TW_OV X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 01 Feb 2011 19:52:12 +0000 From: "ian at airs dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/47579] New: STL size() == 0 does unnecessary shift X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: ian at airs dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Date: Tue, 01 Feb 2011 19:52:00 -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 X-SW-Source: 2011-02/txt/msg00166.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47579 Summary: STL size() == 0 does unnecessary shift Product: gcc Version: 4.6.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization AssignedTo: unassigned@gcc.gnu.org ReportedBy: ian@airs.com Consider this C++ code: #include extern void b1(), b2(); void foo(const std::vector& v) { if (v.size() == 0) b1(); else b2(); } When I compile it with current mainline with -O2 on x86_64, I get this: movq 8(%rdi), %rax subq (%rdi), %rax sarq $2, %rax testq %rax, %rax ... That sarq instruction is useless. We know that the two values being subtracted are both aligned pointers, so we should know that the two least significant bits of the result are zero. And that should be enough to let us know that we don't need to shift before comparing for equality with zero.