This fixes a crash found by @zhendongsu when running SimplifyCFG.
This is the gist:
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 way I propose(d) to fix this is that of not pushing into IncomingValues if PN.getIncomingValue(0) isa<PHINode>.
It seems enough to cover all the cases. Eli proposed to move the debug info handling in the function which takes care of folding the phi, although this is a little tricky because we remove the redundant dbg.values post slice (so it's left as a follow up).
An attempt to fix:
http://llvm.org/bugs/show_bug.cgi?id=37300 and rdar://problem/39910460
The thing that worries me the most is the testcase (as it's admittedly, insane). I spent some time trying to reduce it but I was never able to get to something manageable in size (or something which doesn't screw up debug info). bugpoint itself doesn't seem to help much here. Ideas on how to reduce further are appreciated :)