[llvm-commits] [PATCH] Add a instcombine optimization.

Benjamin Kramer benny.kra at googlemail.com
Wed Jul 7 17:18:35 PDT 2010


The attached patch adds a new transformation to instcombine:
(X >s -1) ? C1 : C2 --> ((X >>s 31) & (C2 - C1)) + C1
and (X <s 0) ? C2 : C1 --> ((X >>s 31) & (C2 - C1)) + C1

this way the conditional can be avoided.

for this function (on x86)
int pn(int n) {
  return n >= 0 ? 1 : -1;
}

we used to generate:
	testl	%edi, %edi
	movl	$1, %ecx
	movl	$-1, %eax
	cmovnsl	%ecx, %eax

now:
	sarl	$31, %edi
	movl	%edi, %eax
	orl	$1, %eax


and a more general example
int sel(int n) {
  return n >= 0 ? 60 : 100;
}

we used to generate:
	testl	%edi, %edi
	movl	$60, %ecx
	movl	$100, %eax
	cmovnsl	%ecx, %eax

now:
	sarl	$31, %edi
	andl	$40, %edi
	leal	60(%rdi), %eax

-------------- next part --------------
A non-text attachment was scrubbed...
Name: branchless.patch
Type: application/octet-stream
Size: 3456 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20100708/d16fea21/attachment.obj>


More information about the llvm-commits mailing list