[LoopIdiomRecognize] Add a test case to show incorrect transformation of an infinite...
authorCraig Topper <craig.topper@intel.com>
Thu, 3 May 2018 23:50:29 +0000 (23:50 +0000)
committerCraig Topper <craig.topper@intel.com>
Thu, 3 May 2018 23:50:29 +0000 (23:50 +0000)
commita3f39ee33d79799ed09d33d241cf02adfeea4ad6
tree9d6ead397d6b00b037805c0156bf1a355bd6e306
parentb51804e0d8c0ecbc5befda89b376e189c652f6b3
[LoopIdiomRecognize] Add a test case to show incorrect transformation of an infinite loop with side effets into a countable loop using ctlz.

We currently recognize this idiom where x is signed and thus the shift in an ashr.

int cnt = 0;
while (x) {
  x >>= 1; // arithmetic shift right
  ++cnt;
}

and turn it into (bitwidth - ctlz(x)). And if there is anything else in the loop we will create a new loop that runs that many times.

If x is initially negative, the shift result will never be 0 and thus the loop is infinite. If you put something with side effects in the loop, that side effect will now only happen bitwidth times instead of an infinite number of times.

So this transform is only safe for logical shift right (which we don't currently recognize) or if we can prove that x cannot be negative before the loop.

llvm-svn: 331493
llvm/test/Transforms/LoopIdiom/X86/ctlz.ll