Skip to content

missed optimization: b &= ~a #2563

@edwintorok

Description

@edwintorok
Bugzilla Link 2191
Resolution FIXED
Resolved on Jan 21, 2009 00:23
Version unspecified
OS Linux
CC @asl,@isanbard,@sunfishcode,@nlewycky

Extended Description

Using llvm-gcc SVN r49213 on the simple testcase below generates code with extra mov instructions.

unsigned sum(unsigned a, unsigned b)
{
do {
a ^= b;
b &= ~a;
} while (b <<= 1);
return a;
}

This is the assembly generated, note the extra movl, and testl after the and:

sum:
.align 16
.LBB1_1: # bb
xorl %esi, %edi
movl %edi, %eax
xorl $2147483647, %eax
andl %esi, %eax
addl %eax, %eax
testl %eax, %eax
movl %eax, %esi
jne .LBB1_1 # bb
.LBB1_2: # bb12
movl %edi, %eax
ret
.size sum, .-sum

This should be a more optimized version (maybe xor could be changed to a not, not sure):
sum:
.align 16
.LBB1_1: # bb
xorl %esi, %edi
movl %edi, %eax
xorl $2147483647, %eax
andl %eax, %esi
addl %esi, %esi
jne .LBB1_1 # bb
.LBB1_2: # bb12
movl %edi, %eax
ret
.size sum, .-sum

FWIW, gcc doesn't generate the optimized version either, here is what I get with gcc-4.3 -O3:
sum:
.align 16
.LBB1_1: # bb
xorl %esi, %edi
movl %edi, %eax
xorl $2147483647, %eax
andl %esi, %eax
addl %eax, %eax
testl %eax, %eax
movl %eax, %esi
jne .LBB1_1 # bb
.LBB1_2: # bb12
movl %edi, %

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzilla

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions