[llvm-commits] [PATCH] Bug 14664: InstCombine missing canonicalization of sub-and into a select

Ahmad, Muhammad T muhammad.t.ahmad at intel.com
Tue Jan 22 12:58:32 PST 2013


For the transform: (and (sext bool to A) B) --> (select boo, B, 0) that we discussed, it breaks the following test:

in file: test/Transforms/InstCombine/logical-select.ll:
define i32 @foo(i32 %a, i32 %b, i32 %c, i32 %d) nounwind {
  %e = icmp slt i32 %a, %b
  %f = sext i1 %e to i32
  %g = and i32 %c, %f
  %h = xor i32 %f, -1
  %i = and i32 %d, %h
  %j = or i32 %g, %i
  ret i32 %j
; CHECK: %e = icmp slt i32 %a, %b
; CHECK: %j = select i1 %e, i32 %c, i32 %d
; CHECK: ret i32 %j
}

because the sext-and (%f-%g) gets converted to select too early and the pattern match which converts it all to select fails.

I see 2 solutions to this problem:
1. Either change this test temporarily, then submit another transform for sext-xor-and --> select, and then change the test again.
2. Submit the transform sext-and --> select and sext-xor-and --> select in one patch.


The proposed transform sequence:

define i32 @foo(i32 %a, i32 %b, i32 %c, i32 %d) nounwind {
  %e = icmp slt i32 %a, %b
  %f = sext i1 %e to i32
  %g = and i32 %c, %f
  %h = xor i32 %f, -1
  %i = and i32 %d, %h
  %j = or i32 %g, %i
  ret i32 %j
}

  ||
  ||
  V

define i32 @foo(i32 %a, i32 %b, i32 %c, i32 %d) nounwind {
  %e = icmp slt i32 %a, %b
  %f = sext i1 %e to i32
  %g = select i1 %e, i32 %c, i32 0
  %h = xor i32 %f, -1
  %i = and i32 %d, %h
  %j = or i32 %g, %i
  ret i32 %j
}

  ||
  ||
  V

define i32 @foo(i32 %a, i32 %b, i32 %c, i32 %d) nounwind {
  %e = icmp slt i32 %a, %b
  %g = select i1 %e, i32 %c, i32 0
  %i = select i1 %e, i32 0, i32 %d
  %j = or i32 %g, %i
  ret i32 %j
}

  ||
  ||
  V

define i32 @foo(i32 %a, i32 %b, i32 %c, i32 %d) nounwind {
  %e = icmp slt i32 %a, %b
  %j = select i1 %e, i32 %c, i32 %d
  ret i32 %j
}


Please advise.

Thanks.
- Muhammad Tauqir



From: Nadav Rotem [nrotem at apple.com]

Sent: Friday, January 18, 2013 4:21 PM

To: Duncan Sands

Cc: llvm-commits at cs.uiuc.edu Commits; Ahmad, Muhammad T

Subject: Re: [llvm-commits] [PATCH] Bug 14664: InstCombine missing canonicalization of sub-and into a select








On Jan 18, 2013, at 12:28 AM, Duncan Sands <baldrick at free.fr> wrote:

how
 about converting "sub 0, zext A to B" into "sext A to B" instead?  There are
a
 bunch of existing optimizations which deal with expressions like
"and
 (sext A to B), C",and they would then kick in.



I think that it is a good idea. 


 I'm not sure that they
would
 produce a select, in fact it's not clear to me that a select is the best
choice
 here.  



I think that Select is a better canonicalization.



Anyway,
 if you do the sub -> sext optimization then the select
optimization
 becomes independent and can be discussed separately.





I agree. Muhammad, can you implement this as two optimizations ?



Also, it's worth looking at the "match" API that instcombine uses. It usually makes the code cleaner. 



Thanks,
Nadav











More information about the llvm-commits mailing list