[llvm-commits] [PATCH] optimization of -(x != 0) to use neg in X86

Nick Lewycky nicholas at mxc.ca
Fri Apr 27 22:25:16 PDT 2012


manman ren wrote:
>
> This patch will optimize -(x != 0) on X86
> FROM
> cmpl	$0x01,%edi
> sbbl	%eax,%eax
> notl	%eax
> TO
> negl %edi
> sbbl %eax %eax
>
> Please review&  provide feedback,

By the way, I'd like to teach the IR optimizers to turn this:

+define i32 @test15(i32 %x) nounwind {
+entry:
+  %cmp = icmp ne i32 %x, 0
+  %sub = sext i1 %cmp to i32
+  ret i32 %sub
+; CHECK: test15:
+; CHECK: negl
+; CHECK: sbbl
+}

into this:

+define i32 @test16(i32 %x) nounwind {
+entry:
+  %c = icmp eq i32 %x, 0
+  %d = select i1 %c, i32 0, i32 -1
+  ret i32 %d
+; CHECK: test16:
+; CHECK: negl
+; CHECK: sbbl
+}

on the theory that any series of operations we do that turns a bool into 
two values can always be written as a single select.

What we don't have is an audit of the backends to make sure that they 
lower selects efficiently (for example, avoiding cmov on x86). I'm not 
sure if that's something you can work on, but it's something to think about.

Nick



More information about the llvm-commits mailing list