diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp @@ -1505,8 +1505,8 @@ // the blocks in the same order. This will help identical PHIs be eliminated // by other passes. Other passes shouldn't depend on this for correctness // however. - PHINode *FirstPN = cast(PN.getParent()->begin()); - if (&PN != FirstPN) + PHINode *FirstPN = dyn_cast(PN.getParent()->begin()); + if (FirstPN && &PN != FirstPN) for (unsigned I = 0, E = FirstPN->getNumIncomingValues(); I != E; ++I) { BasicBlock *BBA = PN.getIncomingBlock(I); BasicBlock *BBB = FirstPN->getIncomingBlock(I);