[LoopUnrolling] Fix loop size check for peeling

Summary:
We should check if loop size allows us to peel at least one iteration
before we do so.

Patch by Max Kazantsev!

Reviewers: sanjoy, mkuper, efriedma

Reviewed By: mkuper

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D30632

llvm-svn: 297122
diff --git a/llvm/lib/Transforms/Utils/LoopUnrollPeel.cpp b/llvm/lib/Transforms/Utils/LoopUnrollPeel.cpp
index 55bfc25..b8147fc 100644
--- a/llvm/lib/Transforms/Utils/LoopUnrollPeel.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUnrollPeel.cpp
@@ -75,7 +75,9 @@
   // its only back edge. If there is such Phi, peeling 1 iteration from the
   // loop is profitable, because starting from 2nd iteration we will have an
   // invariant instead of this Phi.
-  if (auto *BackEdge = L->getLoopLatch()) {
+  if (LoopSize <= UP.Threshold) {
+    BasicBlock *BackEdge = L->getLoopLatch();
+    assert(BackEdge && "Loop is not in simplified form?");
     BasicBlock *Header = L->getHeader();
     // Iterate over Phis to find one with invariant input on back edge.
     bool FoundCandidate = false;