3 ms·
LoopIdiomRecognize::recognizePopcount? Then I think it's more about "the surprising ability of modern compilers to recognize popcount" than general "cleverness.
by _yosefk 10y ago
LoopIdiomRecognize::recognizePopcount? Then I think it's more about "the surprising ability of modern compilers to recognize popcount" than general "cleverness."
(I suspected it was this kind of thing, but I thought maybe it was something more generic.)
- acqq 10y agoYes most other loops that produce the same result wouldn't be recognized, just this very specific, note the steps: // step 1: Check if the loop-back branch is in desirable form. // step 2: detect instructions corresponding to "x2 = x1 & (x1 - 1)" // step 3: Check the recurrence of variable X // step 4: Find the instruction which count the population: cnt2 = cnt1 + 1 It's very, very specific. But they allow that the loop has something more, see the resulting transformation at the end: // After this step, this loop (conceptually) would look like following: // newcnt = __builtin_ctpop(x); // t = newcnt; // if (x) // do { cnt++; x &= x-1; t--) } while (t > 0); This can be later potentially optimized more, in the given simplest case the rest is removed by the later optimization steps.