-
- // Our PHI nodes have stale predecessors after we have cloned our new blocks
- // into the CFG. Fix this by removing the stale predecessors.
- for (BasicBlock *BB : BlocksInFunclet) {
- for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
- Instruction *I = BI++;
- auto *PN = dyn_cast<PHINode>(I);
- // All the PHI nodes are bunched together at the start of the BB.
- // Stop once we've hit a non-PHI.
- if (!PN)
- break;
-
- // Create a list of all the incoming PHI values we'd like to remove.
- // This is done in two steps to avoid iterator invalidation issues.
- std::vector<unsigned> IndicesToRemove;
- for (unsigned Idx = 0, E = PN->getNumIncomingValues(); Idx != E;
- ++Idx) {
- BasicBlock *PredBB = PN->getIncomingBlock(Idx);
- if (BlockColors[PredBB].count(FuncletPadBB) == 0)
- IndicesToRemove.push_back(Idx);
- }
- // Remove incoming values in the reverse order to prevent invalidating
- // *successive* index.
- for (auto I = IndicesToRemove.rbegin(), E = IndicesToRemove.rend();
- I != E; ++I)
- PN->removeIncomingValue(*I);
- }
- }