[PATCH] D29015: [LoopUnswitch] Fix introduction of UB when hoisted condition may be undef or poison

Juneyoung Lee via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 23 03:58:23 PST 2017


aqjune created this revision.
Herald added a subscriber: mzolotukhin.

Loop unswitch hoists branches on loop-invariant conditions. However, if this
condition is poison/undef and the branch wasn't originally reachable, loop
unswitch introduces UB (since the optimized code will branch on poison/undef and
the original one didn't)).
We fix this problem by freezing the condition to ensure we don't introduce UB.

We will now transform the following:
while (...) {

  if (C) { A }
  else   { B }

}

Into:
C' = freeze(C)
if (C') {

  while (...) { A }

} else {

  while (...) { B }

}

This patch fixes at least the following bug reports:

- https://llvm.org/bugs/show_bug.cgi?id=27506
- https://llvm.org/bugs/show_bug.cgi?id=31652


https://reviews.llvm.org/D29015

Files:
  lib/Transforms/Scalar/LoopUnswitch.cpp
  test/Transforms/LoopUnswitch/2011-11-18-SimpleSwitch.ll
  test/Transforms/LoopUnswitch/2011-11-18-TwoSwitches-Threshold.ll
  test/Transforms/LoopUnswitch/2011-11-18-TwoSwitches.ll
  test/Transforms/LoopUnswitch/2015-06-17-Metadata.ll
  test/Transforms/LoopUnswitch/copy-metadata.ll
  test/Transforms/LoopUnswitch/freeze.ll
  test/Transforms/LoopUnswitch/infinite-loop.ll
  test/Transforms/LoopUnswitch/msan.ll
  test/Transforms/LoopUnswitch/trivial-unswitch.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29015.85347.patch
Type: text/x-patch
Size: 28312 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170123/da7f9c1e/attachment.bin>


More information about the llvm-commits mailing list