[LLVMdev] rotate

Michael Gottesman mgottesman at apple.com
Sat Jul 28 21:11:13 PDT 2012


*NOTE* IIRC compiling this with -O0 on x86-64 can yield the wrong result since clang will emit shifts and on intel shifts are mod the register size:

=====
	.section	__TEXT,__text,regular,pure_instructions
	.globl	_ror
	.align	4, 0x90
_ror:                                   ## @ror
	.cfi_startproc
## BB#0:
	pushq	%rbp
Ltmp2:
	.cfi_def_cfa_offset 16
Ltmp3:
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
Ltmp4:
	.cfi_def_cfa_register %rbp
	movl	%edi, -4(%rbp)
	movq	%rsi, -16(%rbp)
	movl	-4(%rbp), %edi
	movq	-16(%rbp), %rsi
	movl	%esi, %eax
	movl	%eax, %ecx
                                        ## kill: CL<def> ECX<kill>
	shrl	%cl, %edi
	movl	-4(%rbp), %eax
	movabsq	$32, %rsi
	subq	-16(%rbp), %rsi
	movl	%esi, %edx
	movl	%edx, %ecx
                                        ## kill: CL<def> ECX<kill>
	shll	%cl, %eax
	orl	%eax, %edi
	movl	%edi, %eax
	popq	%rbp
	ret
	.cfi_endproc


.subsections_via_symbols
=====

Michael

On Jul 28, 2012, at 9:04 PM, reed kotler <rkotler at mips.com> wrote:

> Nice!
> 
> Clever compiler..
> 
> 
> On 07/28/2012 08:55 PM, Michael Gottesman wrote:
>> I can get clang/llvm to emit a rotate instruction on x86-64 when compiling C by just using -Os and the rotate from Hacker's Delight i.e.,
>> 
>> ======
>> #include<stdlib.h>
>> #include<stdint.h>
>> 
>> uint32_t ror(uint32_t input, size_t rot_bits)
>> {
>>   return (input>>  rot_bits) | (input<<  ((sizeof(input)<<  3) - rot_bits));
>> }
>> ======
>> 
>> Then compile with (assuming you are on OS X):
>> ======
>> ISYSROOT=$(xcodebuild -sdk macosx -version PlatformPath)/Developer/SDKs/MacOSX10.8.sdk
>> $(xcrun -find clang) -isysroot $ISYSROOT ror.c -c -S -Os -o -
>> ======
>> 
>> yielding an assembly output of:
>> ======
>> 	.section	__TEXT,__text,regular,pure_instructions
>> 	.globl	_rotr
>> _rotr:                                  ## @rotr
>> 	.cfi_startproc
>> ## BB#0:
>> 	pushq	%rbp
>> Ltmp2:
>> 	.cfi_def_cfa_offset 16
>> Ltmp3:
>> 	.cfi_offset %rbp, -16
>> 	movq	%rsp, %rbp
>> Ltmp4:
>> 	.cfi_def_cfa_register %rbp
>> 	movb	%sil, %cl
>> 	rorl	%cl, %edi<==== Rotate instruction
>> 	movl	%edi, %eax
>> 	popq	%rbp
>> 	ret
>> 	.cfi_endproc
>> .subsections_via_symbols
>> ======
>> 
>> I hope this helps.
>> 
>> Michael
>> 
>> On Jul 28, 2012, at 8:29 PM, reed kotler<rkotler at mips.com>  wrote:
>> 
>>> in C or C++, how can I get clang/llvm to try and do a "rotate".
>>> 
>>> (want to test this code in the mips16 port)
>>> 
>>> i.e. emit rotr node.
>>> 
>>> tia.
>>> 
>>> reed
>>> 
>>> _______________________________________________
>>> LLVM Developers mailing list
>>> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
> 




More information about the llvm-dev mailing list