CTPOP and zeroes

Quick question. The 'ISD::CTPOP' node allows a target to lower the counting of ones in a word to a single instruction. Our target also has an instruction for counting the zeroes in a word. Does CTPOP support counting of zeroes as well as ones instead of doing either "CTPOP(INVERT(operand))" or "N-bits - CTPOP(operand)"?



There is no target-independent ISD opcode for the operation you're describing (as far as I know, no in-tree target has an instruction like that). I would suggest handling this using pattern-matching in your backend, e.g. "def : Pat<(i32 (ctpop (not i32:$a))), (INVCTPOP R:$a)>;".


1 Like

Thanks very much Eli, that is a good approach. I assumed that this is the canonicalized form, but if not, I am sure I can add a simple pass to canonicalize other probable patterns to ensure a match.

All the best,