ObjCARC: do not increment past the end of the BB

The `BasicBlock::getFirstInsertionPt` call may return `std::end` for the
BB.  Dereferencing the end iterator results in an assertion failure
"(!NodePtr->isKnownSentinel()), function operator*".  Ensure that the
returned iterator is valid before dereferencing it.  If the end is
returned, move one position backward to get a valid insertion point.

llvm-svn: 316401
diff --git a/llvm/lib/Transforms/ObjCARC/PtrState.cpp b/llvm/lib/Transforms/ObjCARC/PtrState.cpp
index d13e941..c512ff5 100644
--- a/llvm/lib/Transforms/ObjCARC/PtrState.cpp
+++ b/llvm/lib/Transforms/ObjCARC/PtrState.cpp
@@ -250,10 +250,14 @@
     // If this is an invoke instruction, we're scanning it as part of
     // one of its successor blocks, since we can't insert code after it
     // in its own block, and we don't want to split critical edges.
-    if (isa<InvokeInst>(Inst))
-      InsertReverseInsertPt(&*BB->getFirstInsertionPt());
-    else
-      InsertReverseInsertPt(&*++Inst->getIterator());
+    BasicBlock::iterator InsertAfter;
+    if (isa<InvokeInst>(Inst)) {
+      const auto IP = BB->getFirstInsertionPt();
+      InsertAfter = IP == BB->end() ? std::prev(BB->end()) : IP;
+    } else {
+      InsertAfter = std::next(Inst->getIterator());
+    }
+    InsertReverseInsertPt(&*InsertAfter);
   };
 
   // Check for possible direct uses.