-// The SetVector == operator uses the std::vector == operator, so it doesn't
-// actually tell us whether or not the two sets contain the same colors. This
-// function does that.
-// FIXME: Would it be better to add a isSetEquivalent() method to SetVector?
-static bool isBlockColorSetEquivalent(SetVector<BasicBlock *> &SetA,
- SetVector<BasicBlock *> &SetB) {
- if (SetA.size() != SetB.size())
- return false;
- for (auto *Color : SetA)
- if (!SetB.count(Color))
- return false;
- return true;
-}
-
-// TODO: Share loads for same-funclet uses (requires dominators if funclets
-// aren't properly nested).
-void WinEHPrepare::demoteNonlocalUses(Value *V,
- SetVector<BasicBlock *> &ColorsForBB,
- Function &F) {
- // Tokens can only be used non-locally due to control flow involving
- // unreachable edges. Don't try to demote the token usage, we'll simply
- // delete the cloned user later.
- if (isa<CatchPadInst>(V) || isa<CleanupPadInst>(V))
- return;
-
- DenseMap<BasicBlock *, Value *> Loads;
- AllocaInst *SpillSlot = nullptr;
- for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;) {
- Use &U = *UI++;
- auto *UsingInst = cast<Instruction>(U.getUser());
- BasicBlock *UsingBB = UsingInst->getParent();
-
- // Is the Use inside a block which is colored the same as the Def?
- // If so, we don't need to escape the Def because we will clone
- // ourselves our own private copy.
- SetVector<BasicBlock *> &ColorsForUsingBB = BlockColors[UsingBB];
- if (isBlockColorSetEquivalent(ColorsForUsingBB, ColorsForBB))
- continue;
-
- replaceUseWithLoad(V, U, SpillSlot, Loads, F);
- }
- if (SpillSlot) {
- // Insert stores of the computed value into the stack slot.
- // We have to be careful if I is an invoke instruction,
- // because we can't insert the store AFTER the terminator instruction.
- BasicBlock::iterator InsertPt;
- if (isa<Argument>(V)) {
- InsertPt = F.getEntryBlock().getTerminator()->getIterator();
- } else if (isa<TerminatorInst>(V)) {
- auto *II = cast<InvokeInst>(V);
- // We cannot demote invoke instructions to the stack if their normal
- // edge is critical. Therefore, split the critical edge and create a
- // basic block into which the store can be inserted.
- if (!II->getNormalDest()->getSinglePredecessor()) {
- unsigned SuccNum =
- GetSuccessorNumber(II->getParent(), II->getNormalDest());
- assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
- BasicBlock *NewBlock = SplitCriticalEdge(II, SuccNum);
- assert(NewBlock && "Unable to split critical edge.");
- // Update the color mapping for the newly split edge.
- SetVector<BasicBlock *> &ColorsForUsingBB = BlockColors[II->getParent()];
- BlockColors[NewBlock] = ColorsForUsingBB;
- for (BasicBlock *FuncletPad : ColorsForUsingBB)
- FuncletBlocks[FuncletPad].insert(NewBlock);
- }
- InsertPt = II->getNormalDest()->getFirstInsertionPt();
- } else {
- InsertPt = cast<Instruction>(V)->getIterator();
- ++InsertPt;
- // Don't insert before PHI nodes or EH pad instrs.
- for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
- ;
- }
- new StoreInst(V, SpillSlot, &*InsertPt);
- }
-}
-