[llvm-dev] BUGS n code generated for target i386 compiling	__bswapdi3, and for target x86-64 compiling __bswapsi2()
    Stefan Kanthak via llvm-dev 
    llvm-dev at lists.llvm.org
       
    Mon Nov 26 04:11:51 PST 2018
    
    
  
"Craig Topper" <craig.topper at gmail.com> wrote:
>I just compiled the two attached files in 32-bit mode and ran it.
> 
> It printed efcdab8967452301.
> 
> I verified via objdump that the my_bswap function contains the follow
> assembly which I believe matches the assembly you linked to on godbolt.
> 
> _my_bswap:
>    1f70: 55 pushl %ebp
>    1f71: 89 e5 movl %esp, %ebp
>    1f73: 8b 55 08 movl 8(%ebp), %edx
>    1f76: 8b 45 0c movl 12(%ebp), %eax
>    1f79: 0f c8 bswapl %eax
>    1f7b: 0f ca bswapl %edx
>    1f7d: 5d popl %ebp
>    1f7e: c3 retl
Contrary to my initial WRONG claim this code is CORRECT: two 32-bit BSWAP
instructions on the swapped halves of a 64-bit register are equivalent to
a 64-bit BSWAP instruction.
Sorry for the confusion, I should have slept another night before responding.
The other part of my post but holds: the following function, compiled for
x86-64, is NOT properly optimized (see <https://godbolt.org/z/uM9nvN>):
unsigned long __bswapsi2 (unsigned long ul)
{
    return (ul >> 3 * 8) & 0xff000000ul
         | (ul >>     8) & 0x00ff0000ul
         | (ul <<     8) & 0x0000ff00ul
         | (ul << 3 * 8) & 0x000000fful;
}
The expected optimized code is
__bswapsi2: # @__bswapsi2
    bswap edi
    mov   eax, edi
    ret
regards
Stefan Kanthak
[ fullquote removed ]
    
    
More information about the llvm-dev
mailing list