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: unassig...@gcc.gnu.org ReportedBy: i...@airs.com Consider this C++ code: #include <vector> extern void b1(), b2(); void foo(const std::vector<int>& 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.