<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Jan 15, 2011, at 5:02 PM, Chris Lattner wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: Optima; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="font-family: monospace; ">One major advantage of recognizing popcount though is that the optimizer has a chance of hacking on it, and there are a lot of instcombine xforms that we could do.  Here are some that I note looking at the bc file for crafy (when hacked to use builtin_popcount).  I attached the bc file below if you're interested.<br></span></span></blockquote></div><br><div>I noticed that we have intrinsics and ISD nodes for ctlz, cttz, and ctpop, but none for parity. Is that on purpose?</div><div><br></div><div>Currently, clang compiles __builtin_parity to:</div><div><br></div><div><div>define i32 @f(i32 %x) nounwind readnone ssp {</div><div>entry:</div><div>  %tmp1 = tail call i32 @llvm.ctpop.i32(i32 %x)</div><div>  %tmp2 = and i32 %tmp1, 1</div><div>  ret i32 %tmp2</div><div>}</div></div><div><br></div><div>With corresponding horrible codegen on x86. The IR would be fine for a target with a POPCNT instruction and no parity, but it is much cheaper to compute just the parity when you don't have a POPCNT.</div><div><br></div><div>You want something like:</div><div><br></div><div>  x ^= x >> 16;</div><div>  x ^= x >> 8;</div><div>  return parity_bit(eflags);</div><div><br></div><div>Or portably in compiler-rt:</div><div><br></div><div><div>si_int</div><div>__paritysi2(si_int a)</div><div>{</div><div>    su_int x = (su_int)a;</div><div>    x ^= x >> 16;</div><div>    x ^= x >> 8;</div><div>    x ^= x >> 4;</div><div>    return (0x6996 >> (x & 0xF)) & 1;</div><div>}</div></div><div><br></div><div>/jakob</div><div><br></div></body></html>