From 48283ba3a18235aad90877ef3bfe616bd902eb1b Mon Sep 17 00:00:00 2001 From: Davide Italiano Date: Tue, 8 May 2018 23:28:15 +0000 Subject: [SimplifyCFG] Fix a crash when folding PHIs. We enter MergeBlockIntoPredecessor with a block looking like this: for.inc.us-lcssa: ; preds = %cond.end %k.1.lcssa.ph = phi i32 [ %conv15, %cond.end ] %t.3.lcssa.ph = phi i32 [ %k.1.lcssa.ph, %cond.end ] br label %for.inc, !dbg !66 [note the first arg of the PHI being a PHI]. FoldSingleEntryPHINodes gets rid of both PHIs (calling, eraseFromParent). But right before we call the function, we push into IncomingValues the only argument of the PHIs, and shortly after we try to iterate over something which has been invalidated before :( The fix its not trying to remove PHIs which have an incoming value coming from the same BB we're looking at. Fixes PR37300 and rdar://problem/39910460 Differential Revision: https://reviews.llvm.org/D46568 llvm-svn: 331824 --- llvm/lib/Transforms/Utils/BasicBlockUtils.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'llvm/lib/Transforms/Utils/BasicBlockUtils.cpp') diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp index bf5b4ef48fa..9b6c3c447eb 100644 --- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -149,10 +149,11 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT, return false; // Begin by getting rid of unneeded PHIs. - SmallVector IncomingValues; + SmallVector, 4> IncomingValues; if (isa(BB->front())) { for (PHINode &PN : BB->phis()) - if (PN.getIncomingValue(0) != &PN) + if (!isa(PN.getIncomingValue(0)) || + cast(PN.getIncomingValue(0))->getParent() != BB) IncomingValues.push_back(PN.getIncomingValue(0)); FoldSingleEntryPHINodes(BB, MemDep); } @@ -168,8 +169,8 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT, PredBB->getInstList().splice(PredBB->end(), BB->getInstList()); // Eliminate duplicate dbg.values describing the entry PHI node post-splice. - for (auto *Incoming : IncomingValues) { - if (isa(Incoming)) { + for (auto Incoming : IncomingValues) { + if (isa(*Incoming)) { SmallVector DbgValues; SmallDenseSet, 2> DbgValueSet; -- cgit v1.2.3