[llvm-commits] [llvm] r135996 - /llvm/trunk/lib/Target/X86/README.txt

Benjamin Kramer benny.kra at googlemail.com
Mon Jul 25 15:30:00 PDT 2011


Author: d0k
Date: Mon Jul 25 17:30:00 2011
New Revision: 135996

URL: http://llvm.org/viewvc/llvm-project?rev=135996&view=rev
Log:
Add a note about efficient codegen for binary log.

Modified:
    llvm/trunk/lib/Target/X86/README.txt

Modified: llvm/trunk/lib/Target/X86/README.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/README.txt?rev=135996&r1=135995&r2=135996&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/README.txt (original)
+++ llvm/trunk/lib/Target/X86/README.txt Mon Jul 25 17:30:00 2011
@@ -2066,3 +2066,51 @@
 
 //===---------------------------------------------------------------------===//
 
+unsigned log2(unsigned x) {
+  return x > 1 ? 32-__builtin_clz(x-1) : 0;
+}
+
+generates (x86_64):
+	xorl	%eax, %eax
+	cmpl	$2, %edi
+	jb	LBB0_2
+## BB#1:
+	decl	%edi
+	movl	$63, %eax
+	bsrl	%edi, %ecx
+	cmovel	%eax, %ecx
+	xorl	$31, %ecx
+	movl	$32, %eax
+	subl	%ecx, %eax
+LBB0_2:
+	ret
+
+The cmov and the early test are redundant:
+	xorl	%eax, %eax
+	cmpl	$2, %edi
+	jb	LBB0_2
+## BB#1:
+	decl	%edi
+	bsrl	%edi, %ecx
+	xorl	$31, %ecx
+	movl	$32, %eax
+	subl	%ecx, %eax
+LBB0_2:
+	ret
+
+If we want to get really fancy we could use some two's complement magic:
+	xorl	%eax, %eax
+	cmpl	$2, %edi
+	jb	LBB0_2
+## BB#1:
+	decl	%edi
+	bsrl	%edi, %ecx
+	xorl	$-32, %ecx
+	leal    33(%ecx), %eax
+LBB0_2:
+	ret
+
+This is only useful on targets that can't encode the first operand of a sub
+directly.  The rule is C1 - (X^C2) -> (C1+1) + (X^~C2).
+
+//===---------------------------------------------------------------------===//





More information about the llvm-commits mailing list