]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r303197, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Transforms / Coroutines / CoroFrame.cpp
1 //===- CoroFrame.cpp - Builds and manipulates coroutine frame -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // This file contains classes used to discover if for a particular value
10 // there from sue to definition that crosses a suspend block.
11 //
12 // Using the information discovered we form a Coroutine Frame structure to
13 // contain those values. All uses of those values are replaced with appropriate
14 // GEP + load from the coroutine frame. At the point of the definition we spill
15 // the value into the coroutine frame.
16 //
17 // TODO: pack values tightly using liveness info.
18 //===----------------------------------------------------------------------===//
19
20 #include "CoroInternal.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/IR/CFG.h"
23 #include "llvm/IR/Dominators.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/InstIterator.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/circular_raw_ostream.h"
29 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
30 #include "llvm/Transforms/Utils/Local.h"
31
32 using namespace llvm;
33
34 // The "coro-suspend-crossing" flag is very noisy. There is another debug type,
35 // "coro-frame", which results in leaner debug spew.
36 #define DEBUG_TYPE "coro-suspend-crossing"
37
38 enum { SmallVectorThreshold = 32 };
39
40 // Provides two way mapping between the blocks and numbers.
41 namespace {
42 class BlockToIndexMapping {
43   SmallVector<BasicBlock *, SmallVectorThreshold> V;
44
45 public:
46   size_t size() const { return V.size(); }
47
48   BlockToIndexMapping(Function &F) {
49     for (BasicBlock &BB : F)
50       V.push_back(&BB);
51     std::sort(V.begin(), V.end());
52   }
53
54   size_t blockToIndex(BasicBlock *BB) const {
55     auto *I = std::lower_bound(V.begin(), V.end(), BB);
56     assert(I != V.end() && *I == BB && "BasicBlockNumberng: Unknown block");
57     return I - V.begin();
58   }
59
60   BasicBlock *indexToBlock(unsigned Index) const { return V[Index]; }
61 };
62 } // end anonymous namespace
63
64 // The SuspendCrossingInfo maintains data that allows to answer a question
65 // whether given two BasicBlocks A and B there is a path from A to B that
66 // passes through a suspend point.
67 //
68 // For every basic block 'i' it maintains a BlockData that consists of:
69 //   Consumes:  a bit vector which contains a set of indices of blocks that can
70 //              reach block 'i'
71 //   Kills: a bit vector which contains a set of indices of blocks that can
72 //          reach block 'i', but one of the path will cross a suspend point
73 //   Suspend: a boolean indicating whether block 'i' contains a suspend point.
74 //   End: a boolean indicating whether block 'i' contains a coro.end intrinsic.
75 //
76 namespace {
77 struct SuspendCrossingInfo {
78   BlockToIndexMapping Mapping;
79
80   struct BlockData {
81     BitVector Consumes;
82     BitVector Kills;
83     bool Suspend = false;
84     bool End = false;
85   };
86   SmallVector<BlockData, SmallVectorThreshold> Block;
87
88   iterator_range<succ_iterator> successors(BlockData const &BD) const {
89     BasicBlock *BB = Mapping.indexToBlock(&BD - &Block[0]);
90     return llvm::successors(BB);
91   }
92
93   BlockData &getBlockData(BasicBlock *BB) {
94     return Block[Mapping.blockToIndex(BB)];
95   }
96
97   void dump() const;
98   void dump(StringRef Label, BitVector const &BV) const;
99
100   SuspendCrossingInfo(Function &F, coro::Shape &Shape);
101
102   bool hasPathCrossingSuspendPoint(BasicBlock *DefBB, BasicBlock *UseBB) const {
103     size_t const DefIndex = Mapping.blockToIndex(DefBB);
104     size_t const UseIndex = Mapping.blockToIndex(UseBB);
105
106     assert(Block[UseIndex].Consumes[DefIndex] && "use must consume def");
107     bool const Result = Block[UseIndex].Kills[DefIndex];
108     DEBUG(dbgs() << UseBB->getName() << " => " << DefBB->getName()
109                  << " answer is " << Result << "\n");
110     return Result;
111   }
112
113   bool isDefinitionAcrossSuspend(BasicBlock *DefBB, User *U) const {
114     auto *I = cast<Instruction>(U);
115
116     // We rewrote PHINodes, so that only the ones with exactly one incoming
117     // value need to be analyzed.
118     if (auto *PN = dyn_cast<PHINode>(I))
119       if (PN->getNumIncomingValues() > 1)
120         return false;
121
122     BasicBlock *UseBB = I->getParent();
123     return hasPathCrossingSuspendPoint(DefBB, UseBB);
124   }
125
126   bool isDefinitionAcrossSuspend(Argument &A, User *U) const {
127     return isDefinitionAcrossSuspend(&A.getParent()->getEntryBlock(), U);
128   }
129
130   bool isDefinitionAcrossSuspend(Instruction &I, User *U) const {
131     return isDefinitionAcrossSuspend(I.getParent(), U);
132   }
133 };
134 } // end anonymous namespace
135
136 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
137 LLVM_DUMP_METHOD void SuspendCrossingInfo::dump(StringRef Label,
138                                                 BitVector const &BV) const {
139   dbgs() << Label << ":";
140   for (size_t I = 0, N = BV.size(); I < N; ++I)
141     if (BV[I])
142       dbgs() << " " << Mapping.indexToBlock(I)->getName();
143   dbgs() << "\n";
144 }
145
146 LLVM_DUMP_METHOD void SuspendCrossingInfo::dump() const {
147   for (size_t I = 0, N = Block.size(); I < N; ++I) {
148     BasicBlock *const B = Mapping.indexToBlock(I);
149     dbgs() << B->getName() << ":\n";
150     dump("   Consumes", Block[I].Consumes);
151     dump("      Kills", Block[I].Kills);
152   }
153   dbgs() << "\n";
154 }
155 #endif
156
157 SuspendCrossingInfo::SuspendCrossingInfo(Function &F, coro::Shape &Shape)
158     : Mapping(F) {
159   const size_t N = Mapping.size();
160   Block.resize(N);
161
162   // Initialize every block so that it consumes itself
163   for (size_t I = 0; I < N; ++I) {
164     auto &B = Block[I];
165     B.Consumes.resize(N);
166     B.Kills.resize(N);
167     B.Consumes.set(I);
168   }
169
170   // Mark all CoroEnd Blocks. We do not propagate Kills beyond coro.ends as
171   // the code beyond coro.end is reachable during initial invocation of the
172   // coroutine.
173   for (auto *CE : Shape.CoroEnds)
174     getBlockData(CE->getParent()).End = true;
175
176   // Mark all suspend blocks and indicate that they kill everything they
177   // consume. Note, that crossing coro.save also requires a spill, as any code
178   // between coro.save and coro.suspend may resume the coroutine and all of the
179   // state needs to be saved by that time.
180   auto markSuspendBlock = [&](IntrinsicInst *BarrierInst) {
181     BasicBlock *SuspendBlock = BarrierInst->getParent();
182     auto &B = getBlockData(SuspendBlock);
183     B.Suspend = true;
184     B.Kills |= B.Consumes;
185   };
186   for (CoroSuspendInst *CSI : Shape.CoroSuspends) {
187     markSuspendBlock(CSI);
188     markSuspendBlock(CSI->getCoroSave());
189   }
190
191   // Iterate propagating consumes and kills until they stop changing.
192   int Iteration = 0;
193   (void)Iteration;
194
195   bool Changed;
196   do {
197     DEBUG(dbgs() << "iteration " << ++Iteration);
198     DEBUG(dbgs() << "==============\n");
199
200     Changed = false;
201     for (size_t I = 0; I < N; ++I) {
202       auto &B = Block[I];
203       for (BasicBlock *SI : successors(B)) {
204
205         auto SuccNo = Mapping.blockToIndex(SI);
206
207         // Saved Consumes and Kills bitsets so that it is easy to see
208         // if anything changed after propagation.
209         auto &S = Block[SuccNo];
210         auto SavedConsumes = S.Consumes;
211         auto SavedKills = S.Kills;
212
213         // Propagate Kills and Consumes from block B into its successor S.
214         S.Consumes |= B.Consumes;
215         S.Kills |= B.Kills;
216
217         // If block B is a suspend block, it should propagate kills into the
218         // its successor for every block B consumes.
219         if (B.Suspend) {
220           S.Kills |= B.Consumes;
221         }
222         if (S.Suspend) {
223           // If block S is a suspend block, it should kill all of the blocks it
224           // consumes.
225           S.Kills |= S.Consumes;
226         } else if (S.End) {
227           // If block S is an end block, it should not propagate kills as the
228           // blocks following coro.end() are reached during initial invocation
229           // of the coroutine while all the data are still available on the
230           // stack or in the registers.
231           S.Kills.reset();
232         } else {
233           // This is reached when S block it not Suspend nor coro.end and it
234           // need to make sure that it is not in the kill set.
235           S.Kills.reset(SuccNo);
236         }
237
238         // See if anything changed.
239         Changed |= (S.Kills != SavedKills) || (S.Consumes != SavedConsumes);
240
241         if (S.Kills != SavedKills) {
242           DEBUG(dbgs() << "\nblock " << I << " follower " << SI->getName()
243                        << "\n");
244           DEBUG(dump("S.Kills", S.Kills));
245           DEBUG(dump("SavedKills", SavedKills));
246         }
247         if (S.Consumes != SavedConsumes) {
248           DEBUG(dbgs() << "\nblock " << I << " follower " << SI << "\n");
249           DEBUG(dump("S.Consume", S.Consumes));
250           DEBUG(dump("SavedCons", SavedConsumes));
251         }
252       }
253     }
254   } while (Changed);
255   DEBUG(dump());
256 }
257
258 #undef DEBUG_TYPE // "coro-suspend-crossing"
259 #define DEBUG_TYPE "coro-frame"
260
261 // We build up the list of spills for every case where a use is separated
262 // from the definition by a suspend point.
263
264 struct Spill : std::pair<Value *, Instruction *> {
265   using base = std::pair<Value *, Instruction *>;
266
267   Spill(Value *Def, User *U) : base(Def, cast<Instruction>(U)) {}
268
269   Value *def() const { return first; }
270   Instruction *user() const { return second; }
271   BasicBlock *userBlock() const { return second->getParent(); }
272
273   std::pair<Value *, BasicBlock *> getKey() const {
274     return {def(), userBlock()};
275   }
276
277   bool operator<(Spill const &rhs) const { return getKey() < rhs.getKey(); }
278 };
279
280 // Note that there may be more than one record with the same value of Def in
281 // the SpillInfo vector.
282 using SpillInfo = SmallVector<Spill, 8>;
283
284 #ifndef NDEBUG
285 static void dump(StringRef Title, SpillInfo const &Spills) {
286   dbgs() << "------------- " << Title << "--------------\n";
287   Value *CurrentValue = nullptr;
288   for (auto const &E : Spills) {
289     if (CurrentValue != E.def()) {
290       CurrentValue = E.def();
291       CurrentValue->dump();
292     }
293     dbgs() << "   user: ";
294     E.user()->dump();
295   }
296 }
297 #endif
298
299 // Build a struct that will keep state for an active coroutine.
300 //   struct f.frame {
301 //     ResumeFnTy ResumeFnAddr;
302 //     ResumeFnTy DestroyFnAddr;
303 //     int ResumeIndex;
304 //     ... promise (if present) ...
305 //     ... spills ...
306 //   };
307 static StructType *buildFrameType(Function &F, coro::Shape &Shape,
308                                   SpillInfo &Spills) {
309   LLVMContext &C = F.getContext();
310   SmallString<32> Name(F.getName());
311   Name.append(".Frame");
312   StructType *FrameTy = StructType::create(C, Name);
313   auto *FramePtrTy = FrameTy->getPointerTo();
314   auto *FnTy = FunctionType::get(Type::getVoidTy(C), FramePtrTy,
315                                  /*IsVarArgs=*/false);
316   auto *FnPtrTy = FnTy->getPointerTo();
317
318   // Figure out how wide should be an integer type storing the suspend index.
319   unsigned IndexBits = std::max(1U, Log2_64_Ceil(Shape.CoroSuspends.size()));
320   Type *PromiseType = Shape.PromiseAlloca
321                           ? Shape.PromiseAlloca->getType()->getElementType()
322                           : Type::getInt1Ty(C);
323   SmallVector<Type *, 8> Types{FnPtrTy, FnPtrTy, PromiseType,
324                                Type::getIntNTy(C, IndexBits)};
325   Value *CurrentDef = nullptr;
326
327   // Create an entry for every spilled value.
328   for (auto const &S : Spills) {
329     if (CurrentDef == S.def())
330       continue;
331
332     CurrentDef = S.def();
333     // PromiseAlloca was already added to Types array earlier.
334     if (CurrentDef == Shape.PromiseAlloca)
335       continue;
336
337     Type *Ty = nullptr;
338     if (auto *AI = dyn_cast<AllocaInst>(CurrentDef))
339       Ty = AI->getAllocatedType();
340     else
341       Ty = CurrentDef->getType();
342
343     Types.push_back(Ty);
344   }
345   FrameTy->setBody(Types);
346
347   return FrameTy;
348 }
349
350 // Replace all alloca and SSA values that are accessed across suspend points
351 // with GetElementPointer from coroutine frame + loads and stores. Create an
352 // AllocaSpillBB that will become the new entry block for the resume parts of
353 // the coroutine:
354 //
355 //    %hdl = coro.begin(...)
356 //    whatever
357 //
358 // becomes:
359 //
360 //    %hdl = coro.begin(...)
361 //    %FramePtr = bitcast i8* hdl to %f.frame*
362 //    br label %AllocaSpillBB
363 //
364 //  AllocaSpillBB:
365 //    ; geps corresponding to allocas that were moved to coroutine frame
366 //    br label PostSpill
367 //
368 //  PostSpill:
369 //    whatever
370 //
371 //
372 static Instruction *insertSpills(SpillInfo &Spills, coro::Shape &Shape) {
373   auto *CB = Shape.CoroBegin;
374   IRBuilder<> Builder(CB->getNextNode());
375   PointerType *FramePtrTy = Shape.FrameTy->getPointerTo();
376   auto *FramePtr =
377       cast<Instruction>(Builder.CreateBitCast(CB, FramePtrTy, "FramePtr"));
378   Type *FrameTy = FramePtrTy->getElementType();
379
380   Value *CurrentValue = nullptr;
381   BasicBlock *CurrentBlock = nullptr;
382   Value *CurrentReload = nullptr;
383   unsigned Index = coro::Shape::LastKnownField;
384
385   // We need to keep track of any allocas that need "spilling"
386   // since they will live in the coroutine frame now, all access to them
387   // need to be changed, not just the access across suspend points
388   // we remember allocas and their indices to be handled once we processed
389   // all the spills.
390   SmallVector<std::pair<AllocaInst *, unsigned>, 4> Allocas;
391   // Promise alloca (if present) has a fixed field number (Shape::PromiseField)
392   if (Shape.PromiseAlloca)
393     Allocas.emplace_back(Shape.PromiseAlloca, coro::Shape::PromiseField);
394
395   // Create a load instruction to reload the spilled value from the coroutine
396   // frame.
397   auto CreateReload = [&](Instruction *InsertBefore) {
398     Builder.SetInsertPoint(InsertBefore);
399     auto *G = Builder.CreateConstInBoundsGEP2_32(FrameTy, FramePtr, 0, Index,
400                                                  CurrentValue->getName() +
401                                                      Twine(".reload.addr"));
402     return isa<AllocaInst>(CurrentValue)
403                ? G
404                : Builder.CreateLoad(G,
405                                     CurrentValue->getName() + Twine(".reload"));
406   };
407
408   for (auto const &E : Spills) {
409     // If we have not seen the value, generate a spill.
410     if (CurrentValue != E.def()) {
411       CurrentValue = E.def();
412       CurrentBlock = nullptr;
413       CurrentReload = nullptr;
414
415       ++Index;
416
417       if (auto *AI = dyn_cast<AllocaInst>(CurrentValue)) {
418         // Spilled AllocaInst will be replaced with GEP from the coroutine frame
419         // there is no spill required.
420         Allocas.emplace_back(AI, Index);
421         if (!AI->isStaticAlloca())
422           report_fatal_error("Coroutines cannot handle non static allocas yet");
423       } else {
424         // Otherwise, create a store instruction storing the value into the
425         // coroutine frame.
426
427         Instruction *InsertPt = nullptr;
428         if (isa<Argument>(CurrentValue)) {
429           // For arguments, we will place the store instruction right after
430           // the coroutine frame pointer instruction, i.e. bitcast of
431           // coro.begin from i8* to %f.frame*.
432           InsertPt = FramePtr->getNextNode();
433         } else if (auto *II = dyn_cast<InvokeInst>(CurrentValue)) {
434           // If we are spilling the result of the invoke instruction, split the
435           // normal edge and insert the spill in the new block.
436           auto NewBB = SplitEdge(II->getParent(), II->getNormalDest());
437           InsertPt = NewBB->getTerminator();
438         } else if (dyn_cast<PHINode>(CurrentValue)) {
439           // Skip the PHINodes and EH pads instructions.
440           InsertPt =
441               &*cast<Instruction>(E.def())->getParent()->getFirstInsertionPt();
442         } else {
443           // For all other values, the spill is placed immediately after
444           // the definition.
445           assert(!isa<TerminatorInst>(E.def()) && "unexpected terminator");
446           InsertPt = cast<Instruction>(E.def())->getNextNode();
447         }
448
449         Builder.SetInsertPoint(InsertPt);
450         auto *G = Builder.CreateConstInBoundsGEP2_32(
451             FrameTy, FramePtr, 0, Index,
452             CurrentValue->getName() + Twine(".spill.addr"));
453         Builder.CreateStore(CurrentValue, G);
454       }
455     }
456
457     // If we have not seen the use block, generate a reload in it.
458     if (CurrentBlock != E.userBlock()) {
459       CurrentBlock = E.userBlock();
460       CurrentReload = CreateReload(&*CurrentBlock->getFirstInsertionPt());
461     }
462
463     // If we have a single edge PHINode, remove it and replace it with a reload
464     // from the coroutine frame. (We already took care of multi edge PHINodes
465     // by rewriting them in the rewritePHIs function).
466     if (auto *PN = dyn_cast<PHINode>(E.user())) {
467       assert(PN->getNumIncomingValues() == 1 && "unexpected number of incoming "
468                                                 "values in the PHINode");
469       PN->replaceAllUsesWith(CurrentReload);
470       PN->eraseFromParent();
471       continue;
472     }
473
474     // Replace all uses of CurrentValue in the current instruction with reload.
475     E.user()->replaceUsesOfWith(CurrentValue, CurrentReload);
476   }
477
478   BasicBlock *FramePtrBB = FramePtr->getParent();
479   Shape.AllocaSpillBlock =
480       FramePtrBB->splitBasicBlock(FramePtr->getNextNode(), "AllocaSpillBB");
481   Shape.AllocaSpillBlock->splitBasicBlock(&Shape.AllocaSpillBlock->front(),
482                                           "PostSpill");
483
484   Builder.SetInsertPoint(&Shape.AllocaSpillBlock->front());
485   // If we found any allocas, replace all of their remaining uses with Geps.
486   for (auto &P : Allocas) {
487     auto *G =
488         Builder.CreateConstInBoundsGEP2_32(FrameTy, FramePtr, 0, P.second);
489     // We are not using ReplaceInstWithInst(P.first, cast<Instruction>(G)) here,
490     // as we are changing location of the instruction.
491     G->takeName(P.first);
492     P.first->replaceAllUsesWith(G);
493     P.first->eraseFromParent();
494   }
495   return FramePtr;
496 }
497
498 // Sets the unwind edge of an instruction to a particular successor.
499 static void setUnwindEdgeTo(TerminatorInst *TI, BasicBlock *Succ) {
500   if (auto *II = dyn_cast<InvokeInst>(TI))
501     II->setUnwindDest(Succ);
502   else if (auto *CS = dyn_cast<CatchSwitchInst>(TI))
503     CS->setUnwindDest(Succ);
504   else if (auto *CR = dyn_cast<CleanupReturnInst>(TI))
505     CR->setUnwindDest(Succ);
506   else
507     llvm_unreachable("unexpected terminator instruction");
508 }
509
510 // Replaces all uses of OldPred with the NewPred block in all PHINodes in a
511 // block.
512 static void updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred,
513                            BasicBlock *NewPred,
514                            PHINode *LandingPadReplacement) {
515   unsigned BBIdx = 0;
516   for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
517     PHINode *PN = cast<PHINode>(I);
518
519     // We manually update the LandingPadReplacement PHINode and it is the last
520     // PHI Node. So, if we find it, we are done.
521     if (LandingPadReplacement == PN)
522       break;
523
524     // Reuse the previous value of BBIdx if it lines up.  In cases where we
525     // have multiple phi nodes with *lots* of predecessors, this is a speed
526     // win because we don't have to scan the PHI looking for TIBB.  This
527     // happens because the BB list of PHI nodes are usually in the same
528     // order.
529     if (PN->getIncomingBlock(BBIdx) != OldPred)
530       BBIdx = PN->getBasicBlockIndex(OldPred);
531
532     assert(BBIdx != (unsigned)-1 && "Invalid PHI Index!");
533     PN->setIncomingBlock(BBIdx, NewPred);
534   }
535 }
536
537 // Uses SplitEdge unless the successor block is an EHPad, in which case do EH
538 // specific handling.
539 static BasicBlock *ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ,
540                                     LandingPadInst *OriginalPad,
541                                     PHINode *LandingPadReplacement) {
542   auto *PadInst = Succ->getFirstNonPHI();
543   if (!LandingPadReplacement && !PadInst->isEHPad())
544     return SplitEdge(BB, Succ);
545
546   auto *NewBB = BasicBlock::Create(BB->getContext(), "", BB->getParent(), Succ);
547   setUnwindEdgeTo(BB->getTerminator(), NewBB);
548   updatePhiNodes(Succ, BB, NewBB, LandingPadReplacement);
549
550   if (LandingPadReplacement) {
551     auto *NewLP = OriginalPad->clone();
552     auto *Terminator = BranchInst::Create(Succ, NewBB);
553     NewLP->insertBefore(Terminator);
554     LandingPadReplacement->addIncoming(NewLP, NewBB);
555     return NewBB;
556   }
557   Value *ParentPad = nullptr;
558   if (auto *FuncletPad = dyn_cast<FuncletPadInst>(PadInst))
559     ParentPad = FuncletPad->getParentPad();
560   else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(PadInst))
561     ParentPad = CatchSwitch->getParentPad();
562   else
563     llvm_unreachable("handling for other EHPads not implemented yet");
564
565   auto *NewCleanupPad = CleanupPadInst::Create(ParentPad, {}, "", NewBB);
566   CleanupReturnInst::Create(NewCleanupPad, Succ, NewBB);
567   return NewBB;
568 }
569
570 static void rewritePHIs(BasicBlock &BB) {
571   // For every incoming edge we will create a block holding all
572   // incoming values in a single PHI nodes.
573   //
574   // loop:
575   //    %n.val = phi i32[%n, %entry], [%inc, %loop]
576   //
577   // It will create:
578   //
579   // loop.from.entry:
580   //    %n.loop.pre = phi i32 [%n, %entry]
581   //    br %label loop
582   // loop.from.loop:
583   //    %inc.loop.pre = phi i32 [%inc, %loop]
584   //    br %label loop
585   //
586   // After this rewrite, further analysis will ignore any phi nodes with more
587   // than one incoming edge.
588
589   // TODO: Simplify PHINodes in the basic block to remove duplicate
590   // predecessors.
591
592   LandingPadInst *LandingPad = nullptr;
593   PHINode *ReplPHI = nullptr;
594   if ((LandingPad = dyn_cast_or_null<LandingPadInst>(BB.getFirstNonPHI()))) {
595     // ehAwareSplitEdge will clone the LandingPad in all the edge blocks.
596     // We replace the original landing pad with a PHINode that will collect the
597     // results from all of them.
598     ReplPHI = PHINode::Create(LandingPad->getType(), 1, "", LandingPad);
599     ReplPHI->takeName(LandingPad);
600     LandingPad->replaceAllUsesWith(ReplPHI);
601     // We will erase the original landing pad at the end of this function after
602     // ehAwareSplitEdge cloned it in the transition blocks.
603   }
604
605   SmallVector<BasicBlock *, 8> Preds(pred_begin(&BB), pred_end(&BB));
606   for (BasicBlock *Pred : Preds) {
607     auto *IncomingBB = ehAwareSplitEdge(Pred, &BB, LandingPad, ReplPHI);
608     IncomingBB->setName(BB.getName() + Twine(".from.") + Pred->getName());
609     auto *PN = cast<PHINode>(&BB.front());
610     do {
611       int Index = PN->getBasicBlockIndex(IncomingBB);
612       Value *V = PN->getIncomingValue(Index);
613       PHINode *InputV = PHINode::Create(
614           V->getType(), 1, V->getName() + Twine(".") + BB.getName(),
615           &IncomingBB->front());
616       InputV->addIncoming(V, Pred);
617       PN->setIncomingValue(Index, InputV);
618       PN = dyn_cast<PHINode>(PN->getNextNode());
619     } while (PN != ReplPHI); // ReplPHI is either null or the PHI that replaced
620                              // the landing pad.
621   }
622
623   if (LandingPad) {
624     // Calls to ehAwareSplitEdge function cloned the original lading pad.
625     // No longer need it.
626     LandingPad->eraseFromParent();
627   }
628 }
629
630 static void rewritePHIs(Function &F) {
631   SmallVector<BasicBlock *, 8> WorkList;
632
633   for (BasicBlock &BB : F)
634     if (auto *PN = dyn_cast<PHINode>(&BB.front()))
635       if (PN->getNumIncomingValues() > 1)
636         WorkList.push_back(&BB);
637
638   for (BasicBlock *BB : WorkList)
639     rewritePHIs(*BB);
640 }
641
642 // Check for instructions that we can recreate on resume as opposed to spill
643 // the result into a coroutine frame.
644 static bool materializable(Instruction &V) {
645   return isa<CastInst>(&V) || isa<GetElementPtrInst>(&V) ||
646          isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<SelectInst>(&V);
647 }
648
649 // Check for structural coroutine intrinsics that should not be spilled into
650 // the coroutine frame.
651 static bool isCoroutineStructureIntrinsic(Instruction &I) {
652   return isa<CoroIdInst>(&I) || isa<CoroBeginInst>(&I) ||
653          isa<CoroSaveInst>(&I) || isa<CoroSuspendInst>(&I);
654 }
655
656 // For every use of the value that is across suspend point, recreate that value
657 // after a suspend point.
658 static void rewriteMaterializableInstructions(IRBuilder<> &IRB,
659                                               SpillInfo const &Spills) {
660   BasicBlock *CurrentBlock = nullptr;
661   Instruction *CurrentMaterialization = nullptr;
662   Instruction *CurrentDef = nullptr;
663
664   for (auto const &E : Spills) {
665     // If it is a new definition, update CurrentXXX variables.
666     if (CurrentDef != E.def()) {
667       CurrentDef = cast<Instruction>(E.def());
668       CurrentBlock = nullptr;
669       CurrentMaterialization = nullptr;
670     }
671
672     // If we have not seen this block, materialize the value.
673     if (CurrentBlock != E.userBlock()) {
674       CurrentBlock = E.userBlock();
675       CurrentMaterialization = cast<Instruction>(CurrentDef)->clone();
676       CurrentMaterialization->setName(CurrentDef->getName());
677       CurrentMaterialization->insertBefore(
678           &*CurrentBlock->getFirstInsertionPt());
679     }
680
681     if (auto *PN = dyn_cast<PHINode>(E.user())) {
682       assert(PN->getNumIncomingValues() == 1 && "unexpected number of incoming "
683                                                 "values in the PHINode");
684       PN->replaceAllUsesWith(CurrentMaterialization);
685       PN->eraseFromParent();
686       continue;
687     }
688
689     // Replace all uses of CurrentDef in the current instruction with the
690     // CurrentMaterialization for the block.
691     E.user()->replaceUsesOfWith(CurrentDef, CurrentMaterialization);
692   }
693 }
694
695 // Move early uses of spilled variable after CoroBegin.
696 // For example, if a parameter had address taken, we may end up with the code
697 // like:
698 //        define @f(i32 %n) {
699 //          %n.addr = alloca i32
700 //          store %n, %n.addr
701 //          ...
702 //          call @coro.begin
703 //    we need to move the store after coro.begin
704 static void moveSpillUsesAfterCoroBegin(Function &F, SpillInfo const &Spills,
705                                         CoroBeginInst *CoroBegin) {
706   DominatorTree DT(F);
707   SmallVector<Instruction *, 8> NeedsMoving;
708
709   Value *CurrentValue = nullptr;
710
711   for (auto const &E : Spills) {
712     if (CurrentValue == E.def())
713       continue;
714
715     CurrentValue = E.def();
716
717     for (User *U : CurrentValue->users()) {
718       Instruction *I = cast<Instruction>(U);
719       if (!DT.dominates(CoroBegin, I)) {
720         // TODO: Make this more robust. Currently if we run into a situation
721         // where simple instruction move won't work we panic and
722         // report_fatal_error.
723         for (User *UI : I->users()) {
724           if (!DT.dominates(CoroBegin, cast<Instruction>(UI)))
725             report_fatal_error("cannot move instruction since its users are not"
726                                " dominated by CoroBegin");
727         }
728
729         DEBUG(dbgs() << "will move: " << *I << "\n");
730         NeedsMoving.push_back(I);
731       }
732     }
733   }
734
735   Instruction *InsertPt = CoroBegin->getNextNode();
736   for (Instruction *I : NeedsMoving)
737     I->moveBefore(InsertPt);
738 }
739
740 // Splits the block at a particular instruction unless it is the first
741 // instruction in the block with a single predecessor.
742 static BasicBlock *splitBlockIfNotFirst(Instruction *I, const Twine &Name) {
743   auto *BB = I->getParent();
744   if (&BB->front() == I) {
745     if (BB->getSinglePredecessor()) {
746       BB->setName(Name);
747       return BB;
748     }
749   }
750   return BB->splitBasicBlock(I, Name);
751 }
752
753 // Split above and below a particular instruction so that it
754 // will be all alone by itself in a block.
755 static void splitAround(Instruction *I, const Twine &Name) {
756   splitBlockIfNotFirst(I, Name);
757   splitBlockIfNotFirst(I->getNextNode(), "After" + Name);
758 }
759
760 void coro::buildCoroutineFrame(Function &F, Shape &Shape) {
761   // Lower coro.dbg.declare to coro.dbg.value, since we are going to rewrite
762   // access to local variables.
763   LowerDbgDeclare(F);
764
765   Shape.PromiseAlloca = Shape.CoroBegin->getId()->getPromise();
766   if (Shape.PromiseAlloca) {
767     Shape.CoroBegin->getId()->clearPromise();
768   }
769
770   // Make sure that all coro.save, coro.suspend and the fallthrough coro.end
771   // intrinsics are in their own blocks to simplify the logic of building up
772   // SuspendCrossing data.
773   for (CoroSuspendInst *CSI : Shape.CoroSuspends) {
774     splitAround(CSI->getCoroSave(), "CoroSave");
775     splitAround(CSI, "CoroSuspend");
776   }
777
778   // Put fallthrough CoroEnd into its own block. Note: Shape::buildFrom places
779   // the fallthrough coro.end as the first element of CoroEnds array.
780   splitAround(Shape.CoroEnds.front(), "CoroEnd");
781
782   // Transforms multi-edge PHI Nodes, so that any value feeding into a PHI will
783   // never has its definition separated from the PHI by the suspend point.
784   rewritePHIs(F);
785
786   // Build suspend crossing info.
787   SuspendCrossingInfo Checker(F, Shape);
788
789   IRBuilder<> Builder(F.getContext());
790   SpillInfo Spills;
791
792   // See if there are materializable instructions across suspend points.
793   for (Instruction &I : instructions(F))
794     if (materializable(I))
795       for (User *U : I.users())
796         if (Checker.isDefinitionAcrossSuspend(I, U))
797           Spills.emplace_back(&I, U);
798
799   // Rewrite materializable instructions to be materialized at the use point.
800   DEBUG(dump("Materializations", Spills));
801   rewriteMaterializableInstructions(Builder, Spills);
802
803   // Collect the spills for arguments and other not-materializable values.
804   Spills.clear();
805   for (Argument &A : F.args())
806     for (User *U : A.users())
807       if (Checker.isDefinitionAcrossSuspend(A, U))
808         Spills.emplace_back(&A, U);
809
810   for (Instruction &I : instructions(F)) {
811     // Values returned from coroutine structure intrinsics should not be part
812     // of the Coroutine Frame.
813     if (isCoroutineStructureIntrinsic(I))
814       continue;
815     // The Coroutine Promise always included into coroutine frame, no need to
816     // check for suspend crossing.
817     if (Shape.PromiseAlloca == &I)
818       continue;
819
820     for (User *U : I.users())
821       if (Checker.isDefinitionAcrossSuspend(I, U)) {
822         // We cannot spill a token.
823         if (I.getType()->isTokenTy())
824           report_fatal_error(
825               "token definition is separated from the use by a suspend point");
826         assert(!materializable(I) &&
827                "rewriteMaterializable did not do its job");
828         Spills.emplace_back(&I, U);
829       }
830   }
831   DEBUG(dump("Spills", Spills));
832   moveSpillUsesAfterCoroBegin(F, Spills, Shape.CoroBegin);
833   Shape.FrameTy = buildFrameType(F, Shape, Spills);
834   Shape.FramePtr = insertSpills(Spills, Shape);
835 }