[LIR] Re-instate r244880, reverted in r244884, factoring the handling of
[oota-llvm.git] / lib / Transforms / Scalar / LoopIdiomRecognize.cpp
index 0a4a7bb51e0ff94866758ba5c01c1ee26355de6e..ee5203105201f791815302dafe72402785f30867 100644 (file)
@@ -832,32 +832,29 @@ bool LoopIdiomRecognize::recognizePopcount() {
   // non-compact loop. Therefore, recognizing popcount idiom only makes sense
   // in a compact loop.
 
-  assert(CurLoop->isLoopSimplifyForm() &&
-         "Loop passes require simplified form!");
-
-  // Give up if the loop has multiple blocks.
-  if (CurLoop->getNumBlocks() != 1)
+  // Give up if the loop has multiple blocks or multiple backedges.
+  if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
     return false;
 
-  // If the loop is too big, bail out.
-  BasicBlock &LoopBB = *CurLoop->getHeader();
-  if (LoopBB.size() >= 20)
+  BasicBlock *LoopBody = *(CurLoop->block_begin());
+  if (LoopBody->size() >= 20) {
+    // The loop is too big, bail out.
     return false;
+  }
 
   // It should have a preheader containing nothing but an unconditional branch.
-  BasicBlock &PH = *CurLoop->getLoopPreheader();
-  if (&PH.front() != PH.getTerminator())
+  BasicBlock *PH = CurLoop->getLoopPreheader();
+  if (!PH)
+    return false;
+  if (&PH->front() != PH->getTerminator())
     return false;
-  // FIXME: Technically, it shouldn't matter what instruction we use as
-  // a terminator, the only property needed is the definition of a preheader:
-  // a single loop predecessor whose only successor is the loop header.
-  auto *EntryBI = dyn_cast<BranchInst>(PH.getTerminator());
+  auto *EntryBI = dyn_cast<BranchInst>(PH->getTerminator());
   if (!EntryBI || EntryBI->isConditional())
     return false;
 
   // It should have a precondition block where the generated popcount instrinsic
   // function can be inserted.
-  auto *PreCondBB = PH.getSinglePredecessor();
+  auto *PreCondBB = PH->getSinglePredecessor();
   if (!PreCondBB)
     return false;
   auto *PreCondBI = dyn_cast<BranchInst>(PreCondBB->getTerminator());