LLVM 18.0.0git
CoroSplit.cpp
Go to the documentation of this file.
1//===- CoroSplit.cpp - Converts a coroutine into a state machine ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8// This pass builds the coroutine frame and outlines resume and destroy parts
9// of the coroutine into separate functions.
10//
11// We present a coroutine to an LLVM as an ordinary function with suspension
12// points marked up with intrinsics. We let the optimizer party on the coroutine
13// as a single function for as long as possible. Shortly before the coroutine is
14// eligible to be inlined into its callers, we split up the coroutine into parts
15// corresponding to an initial, resume and destroy invocations of the coroutine,
16// add them to the current SCC and restart the IPO pipeline to optimize the
17// coroutine subfunctions we extracted before proceeding to the caller of the
18// coroutine.
19//===----------------------------------------------------------------------===//
20
22#include "CoroInstr.h"
23#include "CoroInternal.h"
24#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/ADT/Twine.h"
30#include "llvm/Analysis/CFG.h"
37#include "llvm/IR/Argument.h"
38#include "llvm/IR/Attributes.h"
39#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/CFG.h"
41#include "llvm/IR/CallingConv.h"
42#include "llvm/IR/Constants.h"
43#include "llvm/IR/DataLayout.h"
45#include "llvm/IR/Dominators.h"
46#include "llvm/IR/Function.h"
47#include "llvm/IR/GlobalValue.h"
49#include "llvm/IR/IRBuilder.h"
51#include "llvm/IR/InstrTypes.h"
52#include "llvm/IR/Instruction.h"
55#include "llvm/IR/LLVMContext.h"
56#include "llvm/IR/Module.h"
57#include "llvm/IR/Type.h"
58#include "llvm/IR/Value.h"
59#include "llvm/IR/Verifier.h"
61#include "llvm/Support/Debug.h"
70#include <cassert>
71#include <cstddef>
72#include <cstdint>
73#include <initializer_list>
74#include <iterator>
75
76using namespace llvm;
77
78#define DEBUG_TYPE "coro-split"
79
80namespace {
81
82/// A little helper class for building
83class CoroCloner {
84public:
85 enum class Kind {
86 /// The shared resume function for a switch lowering.
87 SwitchResume,
88
89 /// The shared unwind function for a switch lowering.
90 SwitchUnwind,
91
92 /// The shared cleanup function for a switch lowering.
93 SwitchCleanup,
94
95 /// An individual continuation function.
96 Continuation,
97
98 /// An async resume function.
99 Async,
100 };
101
102private:
103 Function &OrigF;
104 Function *NewF;
105 const Twine &Suffix;
106 coro::Shape &Shape;
107 Kind FKind;
110 Value *NewFramePtr = nullptr;
111
112 /// The active suspend instruction; meaningful only for continuation and async
113 /// ABIs.
114 AnyCoroSuspendInst *ActiveSuspend = nullptr;
115
116public:
117 /// Create a cloner for a switch lowering.
118 CoroCloner(Function &OrigF, const Twine &Suffix, coro::Shape &Shape,
119 Kind FKind)
120 : OrigF(OrigF), NewF(nullptr), Suffix(Suffix), Shape(Shape),
121 FKind(FKind), Builder(OrigF.getContext()) {
122 assert(Shape.ABI == coro::ABI::Switch);
123 }
124
125 /// Create a cloner for a continuation lowering.
126 CoroCloner(Function &OrigF, const Twine &Suffix, coro::Shape &Shape,
127 Function *NewF, AnyCoroSuspendInst *ActiveSuspend)
128 : OrigF(OrigF), NewF(NewF), Suffix(Suffix), Shape(Shape),
129 FKind(Shape.ABI == coro::ABI::Async ? Kind::Async : Kind::Continuation),
130 Builder(OrigF.getContext()), ActiveSuspend(ActiveSuspend) {
131 assert(Shape.ABI == coro::ABI::Retcon ||
132 Shape.ABI == coro::ABI::RetconOnce || Shape.ABI == coro::ABI::Async);
133 assert(NewF && "need existing function for continuation");
134 assert(ActiveSuspend && "need active suspend point for continuation");
135 }
136
137 Function *getFunction() const {
138 assert(NewF != nullptr && "declaration not yet set");
139 return NewF;
140 }
141
142 void create();
143
144private:
145 bool isSwitchDestroyFunction() {
146 switch (FKind) {
147 case Kind::Async:
148 case Kind::Continuation:
149 case Kind::SwitchResume:
150 return false;
151 case Kind::SwitchUnwind:
152 case Kind::SwitchCleanup:
153 return true;
154 }
155 llvm_unreachable("Unknown CoroCloner::Kind enum");
156 }
157
158 void replaceEntryBlock();
159 Value *deriveNewFramePointer();
160 void replaceRetconOrAsyncSuspendUses();
161 void replaceCoroSuspends();
162 void replaceCoroEnds();
164 void salvageDebugInfo();
165 void handleFinalSuspend();
166};
167
168} // end anonymous namespace
169
171 const coro::Shape &Shape, Value *FramePtr,
172 CallGraph *CG) {
173 assert(Shape.ABI == coro::ABI::Retcon ||
174 Shape.ABI == coro::ABI::RetconOnce);
176 return;
177
178 Shape.emitDealloc(Builder, FramePtr, CG);
179}
180
181/// Replace an llvm.coro.end.async.
182/// Will inline the must tail call function call if there is one.
183/// \returns true if cleanup of the coro.end block is needed, false otherwise.
186
187 auto *EndAsync = dyn_cast<CoroAsyncEndInst>(End);
188 if (!EndAsync) {
189 Builder.CreateRetVoid();
190 return true /*needs cleanup of coro.end block*/;
191 }
192
193 auto *MustTailCallFunc = EndAsync->getMustTailCallFunction();
194 if (!MustTailCallFunc) {
195 Builder.CreateRetVoid();
196 return true /*needs cleanup of coro.end block*/;
197 }
198
199 // Move the must tail call from the predecessor block into the end block.
200 auto *CoroEndBlock = End->getParent();
201 auto *MustTailCallFuncBlock = CoroEndBlock->getSinglePredecessor();
202 assert(MustTailCallFuncBlock && "Must have a single predecessor block");
203 auto It = MustTailCallFuncBlock->getTerminator()->getIterator();
204 auto *MustTailCall = cast<CallInst>(&*std::prev(It));
205 CoroEndBlock->splice(End->getIterator(), MustTailCallFuncBlock,
206 MustTailCall->getIterator());
207
208 // Insert the return instruction.
209 Builder.SetInsertPoint(End);
210 Builder.CreateRetVoid();
211 InlineFunctionInfo FnInfo;
212
213 // Remove the rest of the block, by splitting it into an unreachable block.
214 auto *BB = End->getParent();
215 BB->splitBasicBlock(End);
216 BB->getTerminator()->eraseFromParent();
217
218 auto InlineRes = InlineFunction(*MustTailCall, FnInfo);
219 assert(InlineRes.isSuccess() && "Expected inlining to succeed");
220 (void)InlineRes;
221
222 // We have cleaned up the coro.end block above.
223 return false;
224}
225
226/// Replace a non-unwind call to llvm.coro.end.
228 const coro::Shape &Shape, Value *FramePtr,
229 bool InResume, CallGraph *CG) {
230 // Start inserting right before the coro.end.
232
233 // Create the return instruction.
234 switch (Shape.ABI) {
235 // The cloned functions in switch-lowering always return void.
236 case coro::ABI::Switch:
237 assert(!cast<CoroEndInst>(End)->hasResults() &&
238 "switch coroutine should not return any values");
239 // coro.end doesn't immediately end the coroutine in the main function
240 // in this lowering, because we need to deallocate the coroutine.
241 if (!InResume)
242 return;
243 Builder.CreateRetVoid();
244 break;
245
246 // In async lowering this returns.
247 case coro::ABI::Async: {
248 bool CoroEndBlockNeedsCleanup = replaceCoroEndAsync(End);
249 if (!CoroEndBlockNeedsCleanup)
250 return;
251 break;
252 }
253
254 // In unique continuation lowering, the continuations always return void.
255 // But we may have implicitly allocated storage.
256 case coro::ABI::RetconOnce: {
258 auto *CoroEnd = cast<CoroEndInst>(End);
259 auto *RetTy = Shape.getResumeFunctionType()->getReturnType();
260
261 if (!CoroEnd->hasResults()) {
262 assert(RetTy->isVoidTy());
263 Builder.CreateRetVoid();
264 break;
265 }
266
267 auto *CoroResults = CoroEnd->getResults();
268 unsigned NumReturns = CoroResults->numReturns();
269
270 if (auto *RetStructTy = dyn_cast<StructType>(RetTy)) {
271 assert(RetStructTy->getNumElements() == NumReturns &&
272 "numbers of returns should match resume function singature");
273 Value *ReturnValue = UndefValue::get(RetStructTy);
274 unsigned Idx = 0;
275 for (Value *RetValEl : CoroResults->return_values())
276 ReturnValue = Builder.CreateInsertValue(ReturnValue, RetValEl, Idx++);
277 Builder.CreateRet(ReturnValue);
278 } else if (NumReturns == 0) {
279 assert(RetTy->isVoidTy());
280 Builder.CreateRetVoid();
281 } else {
282 assert(NumReturns == 1);
283 Builder.CreateRet(*CoroResults->retval_begin());
284 }
285 CoroResults->replaceAllUsesWith(ConstantTokenNone::get(CoroResults->getContext()));
286 CoroResults->eraseFromParent();
287 break;
288 }
289
290 // In non-unique continuation lowering, we signal completion by returning
291 // a null continuation.
292 case coro::ABI::Retcon: {
293 assert(!cast<CoroEndInst>(End)->hasResults() &&
294 "retcon coroutine should not return any values");
296 auto RetTy = Shape.getResumeFunctionType()->getReturnType();
297 auto RetStructTy = dyn_cast<StructType>(RetTy);
298 PointerType *ContinuationTy =
299 cast<PointerType>(RetStructTy ? RetStructTy->getElementType(0) : RetTy);
300
301 Value *ReturnValue = ConstantPointerNull::get(ContinuationTy);
302 if (RetStructTy) {
303 ReturnValue = Builder.CreateInsertValue(UndefValue::get(RetStructTy),
304 ReturnValue, 0);
305 }
306 Builder.CreateRet(ReturnValue);
307 break;
308 }
309 }
310
311 // Remove the rest of the block, by splitting it into an unreachable block.
312 auto *BB = End->getParent();
313 BB->splitBasicBlock(End);
314 BB->getTerminator()->eraseFromParent();
315}
316
317// Mark a coroutine as done, which implies that the coroutine is finished and
318// never get resumed.
319//
320// In resume-switched ABI, the done state is represented by storing zero in
321// ResumeFnAddr.
322//
323// NOTE: We couldn't omit the argument `FramePtr`. It is necessary because the
324// pointer to the frame in splitted function is not stored in `Shape`.
325static void markCoroutineAsDone(IRBuilder<> &Builder, const coro::Shape &Shape,
326 Value *FramePtr) {
327 assert(
328 Shape.ABI == coro::ABI::Switch &&
329 "markCoroutineAsDone is only supported for Switch-Resumed ABI for now.");
330 auto *GepIndex = Builder.CreateStructGEP(
332 "ResumeFn.addr");
333 auto *NullPtr = ConstantPointerNull::get(cast<PointerType>(
335 Builder.CreateStore(NullPtr, GepIndex);
336
337 // If the coroutine don't have unwind coro end, we could omit the store to
338 // the final suspend point since we could infer the coroutine is suspended
339 // at the final suspend point by the nullness of ResumeFnAddr.
340 // However, we can't skip it if the coroutine have unwind coro end. Since
341 // the coroutine reaches unwind coro end is considered suspended at the
342 // final suspend point (the ResumeFnAddr is null) but in fact the coroutine
343 // didn't complete yet. We need the IndexVal for the final suspend point
344 // to make the states clear.
347 assert(cast<CoroSuspendInst>(Shape.CoroSuspends.back())->isFinal() &&
348 "The final suspend should only live in the last position of "
349 "CoroSuspends.");
350 ConstantInt *IndexVal = Shape.getIndex(Shape.CoroSuspends.size() - 1);
351 auto *FinalIndex = Builder.CreateStructGEP(
352 Shape.FrameTy, FramePtr, Shape.getSwitchIndexField(), "index.addr");
353
354 Builder.CreateStore(IndexVal, FinalIndex);
355 }
356}
357
358/// Replace an unwind call to llvm.coro.end.
360 Value *FramePtr, bool InResume,
361 CallGraph *CG) {
363
364 switch (Shape.ABI) {
365 // In switch-lowering, this does nothing in the main function.
366 case coro::ABI::Switch: {
367 // In C++'s specification, the coroutine should be marked as done
368 // if promise.unhandled_exception() throws. The frontend will
369 // call coro.end(true) along this path.
370 //
371 // FIXME: We should refactor this once there is other language
372 // which uses Switch-Resumed style other than C++.
374 if (!InResume)
375 return;
376 break;
377 }
378 // In async lowering this does nothing.
379 case coro::ABI::Async:
380 break;
381 // In continuation-lowering, this frees the continuation storage.
382 case coro::ABI::Retcon:
383 case coro::ABI::RetconOnce:
385 break;
386 }
387
388 // If coro.end has an associated bundle, add cleanupret instruction.
389 if (auto Bundle = End->getOperandBundle(LLVMContext::OB_funclet)) {
390 auto *FromPad = cast<CleanupPadInst>(Bundle->Inputs[0]);
391 auto *CleanupRet = Builder.CreateCleanupRet(FromPad, nullptr);
392 End->getParent()->splitBasicBlock(End);
393 CleanupRet->getParent()->getTerminator()->eraseFromParent();
394 }
395}
396
397static void replaceCoroEnd(AnyCoroEndInst *End, const coro::Shape &Shape,
398 Value *FramePtr, bool InResume, CallGraph *CG) {
399 if (End->isUnwind())
400 replaceUnwindCoroEnd(End, Shape, FramePtr, InResume, CG);
401 else
402 replaceFallthroughCoroEnd(End, Shape, FramePtr, InResume, CG);
403
404 auto &Context = End->getContext();
405 End->replaceAllUsesWith(InResume ? ConstantInt::getTrue(Context)
407 End->eraseFromParent();
408}
409
410// Create an entry block for a resume function with a switch that will jump to
411// suspend points.
413 assert(Shape.ABI == coro::ABI::Switch);
414 LLVMContext &C = F.getContext();
415
416 // resume.entry:
417 // %index.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i32 0,
418 // i32 2
419 // % index = load i32, i32* %index.addr
420 // switch i32 %index, label %unreachable [
421 // i32 0, label %resume.0
422 // i32 1, label %resume.1
423 // ...
424 // ]
425
426 auto *NewEntry = BasicBlock::Create(C, "resume.entry", &F);
427 auto *UnreachBB = BasicBlock::Create(C, "unreachable", &F);
428
429 IRBuilder<> Builder(NewEntry);
430 auto *FramePtr = Shape.FramePtr;
431 auto *FrameTy = Shape.FrameTy;
432 auto *GepIndex = Builder.CreateStructGEP(
433 FrameTy, FramePtr, Shape.getSwitchIndexField(), "index.addr");
434 auto *Index = Builder.CreateLoad(Shape.getIndexType(), GepIndex, "index");
435 auto *Switch =
436 Builder.CreateSwitch(Index, UnreachBB, Shape.CoroSuspends.size());
437 Shape.SwitchLowering.ResumeSwitch = Switch;
438
439 size_t SuspendIndex = 0;
440 for (auto *AnyS : Shape.CoroSuspends) {
441 auto *S = cast<CoroSuspendInst>(AnyS);
442 ConstantInt *IndexVal = Shape.getIndex(SuspendIndex);
443
444 // Replace CoroSave with a store to Index:
445 // %index.addr = getelementptr %f.frame... (index field number)
446 // store i32 %IndexVal, i32* %index.addr1
447 auto *Save = S->getCoroSave();
448 Builder.SetInsertPoint(Save);
449 if (S->isFinal()) {
450 // The coroutine should be marked done if it reaches the final suspend
451 // point.
453 } else {
454 auto *GepIndex = Builder.CreateStructGEP(
455 FrameTy, FramePtr, Shape.getSwitchIndexField(), "index.addr");
456 Builder.CreateStore(IndexVal, GepIndex);
457 }
458
459 Save->replaceAllUsesWith(ConstantTokenNone::get(C));
460 Save->eraseFromParent();
461
462 // Split block before and after coro.suspend and add a jump from an entry
463 // switch:
464 //
465 // whateverBB:
466 // whatever
467 // %0 = call i8 @llvm.coro.suspend(token none, i1 false)
468 // switch i8 %0, label %suspend[i8 0, label %resume
469 // i8 1, label %cleanup]
470 // becomes:
471 //
472 // whateverBB:
473 // whatever
474 // br label %resume.0.landing
475 //
476 // resume.0: ; <--- jump from the switch in the resume.entry
477 // %0 = tail call i8 @llvm.coro.suspend(token none, i1 false)
478 // br label %resume.0.landing
479 //
480 // resume.0.landing:
481 // %1 = phi i8[-1, %whateverBB], [%0, %resume.0]
482 // switch i8 % 1, label %suspend [i8 0, label %resume
483 // i8 1, label %cleanup]
484
485 auto *SuspendBB = S->getParent();
486 auto *ResumeBB =
487 SuspendBB->splitBasicBlock(S, "resume." + Twine(SuspendIndex));
488 auto *LandingBB = ResumeBB->splitBasicBlock(
489 S->getNextNode(), ResumeBB->getName() + Twine(".landing"));
490 Switch->addCase(IndexVal, ResumeBB);
491
492 cast<BranchInst>(SuspendBB->getTerminator())->setSuccessor(0, LandingBB);
493 auto *PN = PHINode::Create(Builder.getInt8Ty(), 2, "");
494 PN->insertBefore(LandingBB->begin());
495 S->replaceAllUsesWith(PN);
496 PN->addIncoming(Builder.getInt8(-1), SuspendBB);
497 PN->addIncoming(S, ResumeBB);
498
499 ++SuspendIndex;
500 }
501
502 Builder.SetInsertPoint(UnreachBB);
503 Builder.CreateUnreachable();
504
505 Shape.SwitchLowering.ResumeEntryBlock = NewEntry;
506}
507
508// In the resume function, we remove the last case (when coro::Shape is built,
509// the final suspend point (if present) is always the last element of
510// CoroSuspends array) since it is an undefined behavior to resume a coroutine
511// suspended at the final suspend point.
512// In the destroy function, if it isn't possible that the ResumeFnAddr is NULL
513// and the coroutine doesn't suspend at the final suspend point actually (this
514// is possible since the coroutine is considered suspended at the final suspend
515// point if promise.unhandled_exception() exits via an exception), we can
516// remove the last case.
517void CoroCloner::handleFinalSuspend() {
518 assert(Shape.ABI == coro::ABI::Switch &&
519 Shape.SwitchLowering.HasFinalSuspend);
520
521 if (isSwitchDestroyFunction() && Shape.SwitchLowering.HasUnwindCoroEnd)
522 return;
523
524 auto *Switch = cast<SwitchInst>(VMap[Shape.SwitchLowering.ResumeSwitch]);
525 auto FinalCaseIt = std::prev(Switch->case_end());
526 BasicBlock *ResumeBB = FinalCaseIt->getCaseSuccessor();
527 Switch->removeCase(FinalCaseIt);
528 if (isSwitchDestroyFunction()) {
529 BasicBlock *OldSwitchBB = Switch->getParent();
530 auto *NewSwitchBB = OldSwitchBB->splitBasicBlock(Switch, "Switch");
531 Builder.SetInsertPoint(OldSwitchBB->getTerminator());
532 auto *GepIndex = Builder.CreateStructGEP(Shape.FrameTy, NewFramePtr,
534 "ResumeFn.addr");
535 auto *Load = Builder.CreateLoad(Shape.getSwitchResumePointerType(),
536 GepIndex);
537 auto *Cond = Builder.CreateIsNull(Load);
538 Builder.CreateCondBr(Cond, ResumeBB, NewSwitchBB);
539 OldSwitchBB->getTerminator()->eraseFromParent();
540 }
541}
542
543static FunctionType *
545 auto *AsyncSuspend = cast<CoroSuspendAsyncInst>(Suspend);
546 auto *StructTy = cast<StructType>(AsyncSuspend->getType());
547 auto &Context = Suspend->getParent()->getParent()->getContext();
548 auto *VoidTy = Type::getVoidTy(Context);
549 return FunctionType::get(VoidTy, StructTy->elements(), false);
550}
551
553 const Twine &Suffix,
554 Module::iterator InsertBefore,
555 AnyCoroSuspendInst *ActiveSuspend) {
556 Module *M = OrigF.getParent();
557 auto *FnTy = (Shape.ABI != coro::ABI::Async)
558 ? Shape.getResumeFunctionType()
559 : getFunctionTypeFromAsyncSuspend(ActiveSuspend);
560
561 Function *NewF =
562 Function::Create(FnTy, GlobalValue::LinkageTypes::InternalLinkage,
563 OrigF.getName() + Suffix);
564
565 M->getFunctionList().insert(InsertBefore, NewF);
566
567 return NewF;
568}
569
570/// Replace uses of the active llvm.coro.suspend.retcon/async call with the
571/// arguments to the continuation function.
572///
573/// This assumes that the builder has a meaningful insertion point.
574void CoroCloner::replaceRetconOrAsyncSuspendUses() {
575 assert(Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce ||
576 Shape.ABI == coro::ABI::Async);
577
578 auto NewS = VMap[ActiveSuspend];
579 if (NewS->use_empty()) return;
580
581 // Copy out all the continuation arguments after the buffer pointer into
582 // an easily-indexed data structure for convenience.
584 // The async ABI includes all arguments -- including the first argument.
585 bool IsAsyncABI = Shape.ABI == coro::ABI::Async;
586 for (auto I = IsAsyncABI ? NewF->arg_begin() : std::next(NewF->arg_begin()),
587 E = NewF->arg_end();
588 I != E; ++I)
589 Args.push_back(&*I);
590
591 // If the suspend returns a single scalar value, we can just do a simple
592 // replacement.
593 if (!isa<StructType>(NewS->getType())) {
594 assert(Args.size() == 1);
595 NewS->replaceAllUsesWith(Args.front());
596 return;
597 }
598
599 // Try to peephole extracts of an aggregate return.
600 for (Use &U : llvm::make_early_inc_range(NewS->uses())) {
601 auto *EVI = dyn_cast<ExtractValueInst>(U.getUser());
602 if (!EVI || EVI->getNumIndices() != 1)
603 continue;
604
605 EVI->replaceAllUsesWith(Args[EVI->getIndices().front()]);
606 EVI->eraseFromParent();
607 }
608
609 // If we have no remaining uses, we're done.
610 if (NewS->use_empty()) return;
611
612 // Otherwise, we need to create an aggregate.
613 Value *Agg = PoisonValue::get(NewS->getType());
614 for (size_t I = 0, E = Args.size(); I != E; ++I)
615 Agg = Builder.CreateInsertValue(Agg, Args[I], I);
616
617 NewS->replaceAllUsesWith(Agg);
618}
619
620void CoroCloner::replaceCoroSuspends() {
621 Value *SuspendResult;
622
623 switch (Shape.ABI) {
624 // In switch lowering, replace coro.suspend with the appropriate value
625 // for the type of function we're extracting.
626 // Replacing coro.suspend with (0) will result in control flow proceeding to
627 // a resume label associated with a suspend point, replacing it with (1) will
628 // result in control flow proceeding to a cleanup label associated with this
629 // suspend point.
630 case coro::ABI::Switch:
631 SuspendResult = Builder.getInt8(isSwitchDestroyFunction() ? 1 : 0);
632 break;
633
634 // In async lowering there are no uses of the result.
635 case coro::ABI::Async:
636 return;
637
638 // In returned-continuation lowering, the arguments from earlier
639 // continuations are theoretically arbitrary, and they should have been
640 // spilled.
641 case coro::ABI::RetconOnce:
642 case coro::ABI::Retcon:
643 return;
644 }
645
646 for (AnyCoroSuspendInst *CS : Shape.CoroSuspends) {
647 // The active suspend was handled earlier.
648 if (CS == ActiveSuspend) continue;
649
650 auto *MappedCS = cast<AnyCoroSuspendInst>(VMap[CS]);
651 MappedCS->replaceAllUsesWith(SuspendResult);
652 MappedCS->eraseFromParent();
653 }
654}
655
656void CoroCloner::replaceCoroEnds() {
657 for (AnyCoroEndInst *CE : Shape.CoroEnds) {
658 // We use a null call graph because there's no call graph node for
659 // the cloned function yet. We'll just be rebuilding that later.
660 auto *NewCE = cast<AnyCoroEndInst>(VMap[CE]);
661 replaceCoroEnd(NewCE, Shape, NewFramePtr, /*in resume*/ true, nullptr);
662 }
663}
664
666 ValueToValueMapTy *VMap) {
667 if (Shape.ABI == coro::ABI::Async && Shape.CoroSuspends.empty())
668 return;
669 Value *CachedSlot = nullptr;
670 auto getSwiftErrorSlot = [&](Type *ValueTy) -> Value * {
671 if (CachedSlot)
672 return CachedSlot;
673
674 // Check if the function has a swifterror argument.
675 for (auto &Arg : F.args()) {
676 if (Arg.isSwiftError()) {
677 CachedSlot = &Arg;
678 return &Arg;
679 }
680 }
681
682 // Create a swifterror alloca.
683 IRBuilder<> Builder(F.getEntryBlock().getFirstNonPHIOrDbg());
684 auto Alloca = Builder.CreateAlloca(ValueTy);
685 Alloca->setSwiftError(true);
686
687 CachedSlot = Alloca;
688 return Alloca;
689 };
690
691 for (CallInst *Op : Shape.SwiftErrorOps) {
692 auto MappedOp = VMap ? cast<CallInst>((*VMap)[Op]) : Op;
693 IRBuilder<> Builder(MappedOp);
694
695 // If there are no arguments, this is a 'get' operation.
696 Value *MappedResult;
697 if (Op->arg_empty()) {
698 auto ValueTy = Op->getType();
699 auto Slot = getSwiftErrorSlot(ValueTy);
700 MappedResult = Builder.CreateLoad(ValueTy, Slot);
701 } else {
702 assert(Op->arg_size() == 1);
703 auto Value = MappedOp->getArgOperand(0);
704 auto ValueTy = Value->getType();
705 auto Slot = getSwiftErrorSlot(ValueTy);
706 Builder.CreateStore(Value, Slot);
707 MappedResult = Slot;
708 }
709
710 MappedOp->replaceAllUsesWith(MappedResult);
711 MappedOp->eraseFromParent();
712 }
713
714 // If we're updating the original function, we've invalidated SwiftErrorOps.
715 if (VMap == nullptr) {
716 Shape.SwiftErrorOps.clear();
717 }
718}
719
720/// Returns all DbgVariableIntrinsic in F.
724 for (auto &I : instructions(F))
725 if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I))
726 Intrinsics.push_back(DVI);
727 return Intrinsics;
728}
729
730void CoroCloner::replaceSwiftErrorOps() {
731 ::replaceSwiftErrorOps(*NewF, Shape, &VMap);
732}
733
734void CoroCloner::salvageDebugInfo() {
738
739 // Only 64-bit ABIs have a register we can refer to with the entry value.
740 bool UseEntryValue =
741 llvm::Triple(OrigF.getParent()->getTargetTriple()).isArch64Bit();
742 for (DbgVariableIntrinsic *DVI : Worklist)
743 coro::salvageDebugInfo(ArgToAllocaMap, DVI, Shape.OptimizeFrame,
744 UseEntryValue);
745
746 // Remove all salvaged dbg.declare intrinsics that became
747 // either unreachable or stale due to the CoroSplit transformation.
748 DominatorTree DomTree(*NewF);
749 auto IsUnreachableBlock = [&](BasicBlock *BB) {
750 return !isPotentiallyReachable(&NewF->getEntryBlock(), BB, nullptr,
751 &DomTree);
752 };
753 for (DbgVariableIntrinsic *DVI : Worklist) {
754 if (IsUnreachableBlock(DVI->getParent()))
755 DVI->eraseFromParent();
756 else if (isa_and_nonnull<AllocaInst>(DVI->getVariableLocationOp(0))) {
757 // Count all non-debuginfo uses in reachable blocks.
758 unsigned Uses = 0;
759 for (auto *User : DVI->getVariableLocationOp(0)->users())
760 if (auto *I = dyn_cast<Instruction>(User))
761 if (!isa<AllocaInst>(I) && !IsUnreachableBlock(I->getParent()))
762 ++Uses;
763 if (!Uses)
764 DVI->eraseFromParent();
765 }
766 }
767}
768
769void CoroCloner::replaceEntryBlock() {
770 // In the original function, the AllocaSpillBlock is a block immediately
771 // following the allocation of the frame object which defines GEPs for
772 // all the allocas that have been moved into the frame, and it ends by
773 // branching to the original beginning of the coroutine. Make this
774 // the entry block of the cloned function.
775 auto *Entry = cast<BasicBlock>(VMap[Shape.AllocaSpillBlock]);
776 auto *OldEntry = &NewF->getEntryBlock();
777 Entry->setName("entry" + Suffix);
778 Entry->moveBefore(OldEntry);
779 Entry->getTerminator()->eraseFromParent();
780
781 // Clear all predecessors of the new entry block. There should be
782 // exactly one predecessor, which we created when splitting out
783 // AllocaSpillBlock to begin with.
784 assert(Entry->hasOneUse());
785 auto BranchToEntry = cast<BranchInst>(Entry->user_back());
786 assert(BranchToEntry->isUnconditional());
787 Builder.SetInsertPoint(BranchToEntry);
788 Builder.CreateUnreachable();
789 BranchToEntry->eraseFromParent();
790
791 // Branch from the entry to the appropriate place.
792 Builder.SetInsertPoint(Entry);
793 switch (Shape.ABI) {
794 case coro::ABI::Switch: {
795 // In switch-lowering, we built a resume-entry block in the original
796 // function. Make the entry block branch to this.
797 auto *SwitchBB =
798 cast<BasicBlock>(VMap[Shape.SwitchLowering.ResumeEntryBlock]);
799 Builder.CreateBr(SwitchBB);
800 break;
801 }
802 case coro::ABI::Async:
803 case coro::ABI::Retcon:
804 case coro::ABI::RetconOnce: {
805 // In continuation ABIs, we want to branch to immediately after the
806 // active suspend point. Earlier phases will have put the suspend in its
807 // own basic block, so just thread our jump directly to its successor.
808 assert((Shape.ABI == coro::ABI::Async &&
809 isa<CoroSuspendAsyncInst>(ActiveSuspend)) ||
810 ((Shape.ABI == coro::ABI::Retcon ||
811 Shape.ABI == coro::ABI::RetconOnce) &&
812 isa<CoroSuspendRetconInst>(ActiveSuspend)));
813 auto *MappedCS = cast<AnyCoroSuspendInst>(VMap[ActiveSuspend]);
814 auto Branch = cast<BranchInst>(MappedCS->getNextNode());
815 assert(Branch->isUnconditional());
816 Builder.CreateBr(Branch->getSuccessor(0));
817 break;
818 }
819 }
820
821 // Any static alloca that's still being used but not reachable from the new
822 // entry needs to be moved to the new entry.
823 Function *F = OldEntry->getParent();
824 DominatorTree DT{*F};
826 auto *Alloca = dyn_cast<AllocaInst>(&I);
827 if (!Alloca || I.use_empty())
828 continue;
829 if (DT.isReachableFromEntry(I.getParent()) ||
830 !isa<ConstantInt>(Alloca->getArraySize()))
831 continue;
832 I.moveBefore(*Entry, Entry->getFirstInsertionPt());
833 }
834}
835
836/// Derive the value of the new frame pointer.
837Value *CoroCloner::deriveNewFramePointer() {
838 // Builder should be inserting to the front of the new entry block.
839
840 switch (Shape.ABI) {
841 // In switch-lowering, the argument is the frame pointer.
842 case coro::ABI::Switch:
843 return &*NewF->arg_begin();
844 // In async-lowering, one of the arguments is an async context as determined
845 // by the `llvm.coro.id.async` intrinsic. We can retrieve the async context of
846 // the resume function from the async context projection function associated
847 // with the active suspend. The frame is located as a tail to the async
848 // context header.
849 case coro::ABI::Async: {
850 auto *ActiveAsyncSuspend = cast<CoroSuspendAsyncInst>(ActiveSuspend);
851 auto ContextIdx = ActiveAsyncSuspend->getStorageArgumentIndex() & 0xff;
852 auto *CalleeContext = NewF->getArg(ContextIdx);
853 auto *ProjectionFunc =
854 ActiveAsyncSuspend->getAsyncContextProjectionFunction();
855 auto DbgLoc =
856 cast<CoroSuspendAsyncInst>(VMap[ActiveSuspend])->getDebugLoc();
857 // Calling i8* (i8*)
858 auto *CallerContext = Builder.CreateCall(ProjectionFunc->getFunctionType(),
859 ProjectionFunc, CalleeContext);
860 CallerContext->setCallingConv(ProjectionFunc->getCallingConv());
861 CallerContext->setDebugLoc(DbgLoc);
862 // The frame is located after the async_context header.
863 auto &Context = Builder.getContext();
864 auto *FramePtrAddr = Builder.CreateConstInBoundsGEP1_32(
865 Type::getInt8Ty(Context), CallerContext,
866 Shape.AsyncLowering.FrameOffset, "async.ctx.frameptr");
867 // Inline the projection function.
869 auto InlineRes = InlineFunction(*CallerContext, InlineInfo);
870 assert(InlineRes.isSuccess());
871 (void)InlineRes;
872 return FramePtrAddr;
873 }
874 // In continuation-lowering, the argument is the opaque storage.
875 case coro::ABI::Retcon:
876 case coro::ABI::RetconOnce: {
877 Argument *NewStorage = &*NewF->arg_begin();
878 auto FramePtrTy = PointerType::getUnqual(Shape.FrameTy->getContext());
879
880 // If the storage is inline, just bitcast to the storage to the frame type.
881 if (Shape.RetconLowering.IsFrameInlineInStorage)
882 return NewStorage;
883
884 // Otherwise, load the real frame from the opaque storage.
885 return Builder.CreateLoad(FramePtrTy, NewStorage);
886 }
887 }
888 llvm_unreachable("bad ABI");
889}
890
891static void addFramePointerAttrs(AttributeList &Attrs, LLVMContext &Context,
892 unsigned ParamIndex, uint64_t Size,
893 Align Alignment, bool NoAlias) {
894 AttrBuilder ParamAttrs(Context);
895 ParamAttrs.addAttribute(Attribute::NonNull);
896 ParamAttrs.addAttribute(Attribute::NoUndef);
897
898 if (NoAlias)
899 ParamAttrs.addAttribute(Attribute::NoAlias);
900
901 ParamAttrs.addAlignmentAttr(Alignment);
902 ParamAttrs.addDereferenceableAttr(Size);
903 Attrs = Attrs.addParamAttributes(Context, ParamIndex, ParamAttrs);
904}
905
906static void addAsyncContextAttrs(AttributeList &Attrs, LLVMContext &Context,
907 unsigned ParamIndex) {
908 AttrBuilder ParamAttrs(Context);
909 ParamAttrs.addAttribute(Attribute::SwiftAsync);
910 Attrs = Attrs.addParamAttributes(Context, ParamIndex, ParamAttrs);
911}
912
913static void addSwiftSelfAttrs(AttributeList &Attrs, LLVMContext &Context,
914 unsigned ParamIndex) {
915 AttrBuilder ParamAttrs(Context);
916 ParamAttrs.addAttribute(Attribute::SwiftSelf);
917 Attrs = Attrs.addParamAttributes(Context, ParamIndex, ParamAttrs);
918}
919
920/// Clone the body of the original function into a resume function of
921/// some sort.
922void CoroCloner::create() {
923 // Create the new function if we don't already have one.
924 if (!NewF) {
925 NewF = createCloneDeclaration(OrigF, Shape, Suffix,
926 OrigF.getParent()->end(), ActiveSuspend);
927 }
928
929 // Replace all args with dummy instructions. If an argument is the old frame
930 // pointer, the dummy will be replaced by the new frame pointer once it is
931 // computed below. Uses of all other arguments should have already been
932 // rewritten by buildCoroutineFrame() to use loads/stores on the coroutine
933 // frame.
935 for (Argument &A : OrigF.args()) {
936 DummyArgs.push_back(new FreezeInst(PoisonValue::get(A.getType())));
937 VMap[&A] = DummyArgs.back();
938 }
939
941
942 // Ignore attempts to change certain attributes of the function.
943 // TODO: maybe there should be a way to suppress this during cloning?
944 auto savedVisibility = NewF->getVisibility();
945 auto savedUnnamedAddr = NewF->getUnnamedAddr();
946 auto savedDLLStorageClass = NewF->getDLLStorageClass();
947
948 // NewF's linkage (which CloneFunctionInto does *not* change) might not
949 // be compatible with the visibility of OrigF (which it *does* change),
950 // so protect against that.
951 auto savedLinkage = NewF->getLinkage();
952 NewF->setLinkage(llvm::GlobalValue::ExternalLinkage);
953
954 CloneFunctionInto(NewF, &OrigF, VMap,
955 CloneFunctionChangeType::LocalChangesOnly, Returns);
956
957 auto &Context = NewF->getContext();
958
959 // For async functions / continuations, adjust the scope line of the
960 // clone to the line number of the suspend point. However, only
961 // adjust the scope line when the files are the same. This ensures
962 // line number and file name belong together. The scope line is
963 // associated with all pre-prologue instructions. This avoids a jump
964 // in the linetable from the function declaration to the suspend point.
965 if (DISubprogram *SP = NewF->getSubprogram()) {
966 assert(SP != OrigF.getSubprogram() && SP->isDistinct());
967 if (ActiveSuspend)
968 if (auto DL = ActiveSuspend->getDebugLoc())
969 if (SP->getFile() == DL->getFile())
970 SP->setScopeLine(DL->getLine());
971 // Update the linkage name to reflect the modified symbol name. It
972 // is necessary to update the linkage name in Swift, since the
973 // mangling changes for resume functions. It might also be the
974 // right thing to do in C++, but due to a limitation in LLVM's
975 // AsmPrinter we can only do this if the function doesn't have an
976 // abstract specification, since the DWARF backend expects the
977 // abstract specification to contain the linkage name and asserts
978 // that they are identical.
979 if (SP->getUnit() &&
980 SP->getUnit()->getSourceLanguage() == dwarf::DW_LANG_Swift) {
981 SP->replaceLinkageName(MDString::get(Context, NewF->getName()));
982 if (auto *Decl = SP->getDeclaration()) {
983 auto *NewDecl = DISubprogram::get(
984 Decl->getContext(), Decl->getScope(), Decl->getName(),
985 NewF->getName(), Decl->getFile(), Decl->getLine(), Decl->getType(),
986 Decl->getScopeLine(), Decl->getContainingType(),
987 Decl->getVirtualIndex(), Decl->getThisAdjustment(),
988 Decl->getFlags(), Decl->getSPFlags(), Decl->getUnit(),
989 Decl->getTemplateParams(), nullptr, Decl->getRetainedNodes(),
990 Decl->getThrownTypes(), Decl->getAnnotations(),
991 Decl->getTargetFuncName());
992 SP->replaceDeclaration(NewDecl);
993 }
994 }
995 }
996
997 NewF->setLinkage(savedLinkage);
998 NewF->setVisibility(savedVisibility);
999 NewF->setUnnamedAddr(savedUnnamedAddr);
1000 NewF->setDLLStorageClass(savedDLLStorageClass);
1001 // The function sanitizer metadata needs to match the signature of the
1002 // function it is being attached to. However this does not hold for split
1003 // functions here. Thus remove the metadata for split functions.
1004 if (Shape.ABI == coro::ABI::Switch &&
1005 NewF->hasMetadata(LLVMContext::MD_func_sanitize))
1006 NewF->eraseMetadata(LLVMContext::MD_func_sanitize);
1007
1008 // Replace the attributes of the new function:
1009 auto OrigAttrs = NewF->getAttributes();
1010 auto NewAttrs = AttributeList();
1011
1012 switch (Shape.ABI) {
1013 case coro::ABI::Switch:
1014 // Bootstrap attributes by copying function attributes from the
1015 // original function. This should include optimization settings and so on.
1016 NewAttrs = NewAttrs.addFnAttributes(
1017 Context, AttrBuilder(Context, OrigAttrs.getFnAttrs()));
1018
1019 addFramePointerAttrs(NewAttrs, Context, 0, Shape.FrameSize,
1020 Shape.FrameAlign, /*NoAlias=*/false);
1021 break;
1022 case coro::ABI::Async: {
1023 auto *ActiveAsyncSuspend = cast<CoroSuspendAsyncInst>(ActiveSuspend);
1024 if (OrigF.hasParamAttribute(Shape.AsyncLowering.ContextArgNo,
1025 Attribute::SwiftAsync)) {
1026 uint32_t ArgAttributeIndices =
1027 ActiveAsyncSuspend->getStorageArgumentIndex();
1028 auto ContextArgIndex = ArgAttributeIndices & 0xff;
1029 addAsyncContextAttrs(NewAttrs, Context, ContextArgIndex);
1030
1031 // `swiftasync` must preceed `swiftself` so 0 is not a valid index for
1032 // `swiftself`.
1033 auto SwiftSelfIndex = ArgAttributeIndices >> 8;
1034 if (SwiftSelfIndex)
1035 addSwiftSelfAttrs(NewAttrs, Context, SwiftSelfIndex);
1036 }
1037
1038 // Transfer the original function's attributes.
1039 auto FnAttrs = OrigF.getAttributes().getFnAttrs();
1040 NewAttrs = NewAttrs.addFnAttributes(Context, AttrBuilder(Context, FnAttrs));
1041 break;
1042 }
1043 case coro::ABI::Retcon:
1044 case coro::ABI::RetconOnce:
1045 // If we have a continuation prototype, just use its attributes,
1046 // full-stop.
1047 NewAttrs = Shape.RetconLowering.ResumePrototype->getAttributes();
1048
1049 /// FIXME: Is it really good to add the NoAlias attribute?
1050 addFramePointerAttrs(NewAttrs, Context, 0,
1051 Shape.getRetconCoroId()->getStorageSize(),
1052 Shape.getRetconCoroId()->getStorageAlignment(),
1053 /*NoAlias=*/true);
1054
1055 break;
1056 }
1057
1058 switch (Shape.ABI) {
1059 // In these ABIs, the cloned functions always return 'void', and the
1060 // existing return sites are meaningless. Note that for unique
1061 // continuations, this includes the returns associated with suspends;
1062 // this is fine because we can't suspend twice.
1063 case coro::ABI::Switch:
1064 case coro::ABI::RetconOnce:
1065 // Remove old returns.
1066 for (ReturnInst *Return : Returns)
1067 changeToUnreachable(Return);
1068 break;
1069
1070 // With multi-suspend continuations, we'll already have eliminated the
1071 // original returns and inserted returns before all the suspend points,
1072 // so we want to leave any returns in place.
1073 case coro::ABI::Retcon:
1074 break;
1075 // Async lowering will insert musttail call functions at all suspend points
1076 // followed by a return.
1077 // Don't change returns to unreachable because that will trip up the verifier.
1078 // These returns should be unreachable from the clone.
1079 case coro::ABI::Async:
1080 break;
1081 }
1082
1083 NewF->setAttributes(NewAttrs);
1084 NewF->setCallingConv(Shape.getResumeFunctionCC());
1085
1086 // Set up the new entry block.
1087 replaceEntryBlock();
1088
1089 Builder.SetInsertPoint(&NewF->getEntryBlock().front());
1090 NewFramePtr = deriveNewFramePointer();
1091
1092 // Remap frame pointer.
1093 Value *OldFramePtr = VMap[Shape.FramePtr];
1094 NewFramePtr->takeName(OldFramePtr);
1095 OldFramePtr->replaceAllUsesWith(NewFramePtr);
1096
1097 // Remap vFrame pointer.
1098 auto *NewVFrame = Builder.CreateBitCast(
1099 NewFramePtr, Type::getInt8PtrTy(Builder.getContext()), "vFrame");
1100 Value *OldVFrame = cast<Value>(VMap[Shape.CoroBegin]);
1101 if (OldVFrame != NewVFrame)
1102 OldVFrame->replaceAllUsesWith(NewVFrame);
1103
1104 // All uses of the arguments should have been resolved by this point,
1105 // so we can safely remove the dummy values.
1106 for (Instruction *DummyArg : DummyArgs) {
1107 DummyArg->replaceAllUsesWith(PoisonValue::get(DummyArg->getType()));
1108 DummyArg->deleteValue();
1109 }
1110
1111 switch (Shape.ABI) {
1112 case coro::ABI::Switch:
1113 // Rewrite final suspend handling as it is not done via switch (allows to
1114 // remove final case from the switch, since it is undefined behavior to
1115 // resume the coroutine suspended at the final suspend point.
1116 if (Shape.SwitchLowering.HasFinalSuspend)
1117 handleFinalSuspend();
1118 break;
1119 case coro::ABI::Async:
1120 case coro::ABI::Retcon:
1121 case coro::ABI::RetconOnce:
1122 // Replace uses of the active suspend with the corresponding
1123 // continuation-function arguments.
1124 assert(ActiveSuspend != nullptr &&
1125 "no active suspend when lowering a continuation-style coroutine");
1126 replaceRetconOrAsyncSuspendUses();
1127 break;
1128 }
1129
1130 // Handle suspends.
1131 replaceCoroSuspends();
1132
1133 // Handle swifterror.
1135
1136 // Remove coro.end intrinsics.
1137 replaceCoroEnds();
1138
1139 // Salvage debug info that points into the coroutine frame.
1141
1142 // Eliminate coro.free from the clones, replacing it with 'null' in cleanup,
1143 // to suppress deallocation code.
1144 if (Shape.ABI == coro::ABI::Switch)
1145 coro::replaceCoroFree(cast<CoroIdInst>(VMap[Shape.CoroBegin->getId()]),
1146 /*Elide=*/ FKind == CoroCloner::Kind::SwitchCleanup);
1147}
1148
1149// Create a resume clone by cloning the body of the original function, setting
1150// new entry block and replacing coro.suspend an appropriate value to force
1151// resume or cleanup pass for every suspend point.
1152static Function *createClone(Function &F, const Twine &Suffix,
1153 coro::Shape &Shape, CoroCloner::Kind FKind) {
1154 CoroCloner Cloner(F, Suffix, Shape, FKind);
1155 Cloner.create();
1156 return Cloner.getFunction();
1157}
1158
1160 assert(Shape.ABI == coro::ABI::Async);
1161
1162 auto *FuncPtrStruct = cast<ConstantStruct>(
1164 auto *OrigRelativeFunOffset = FuncPtrStruct->getOperand(0);
1165 auto *OrigContextSize = FuncPtrStruct->getOperand(1);
1166 auto *NewContextSize = ConstantInt::get(OrigContextSize->getType(),
1168 auto *NewFuncPtrStruct = ConstantStruct::get(
1169 FuncPtrStruct->getType(), OrigRelativeFunOffset, NewContextSize);
1170
1171 Shape.AsyncLowering.AsyncFuncPointer->setInitializer(NewFuncPtrStruct);
1172}
1173
1175 if (Shape.ABI == coro::ABI::Async)
1177
1178 for (CoroAlignInst *CA : Shape.CoroAligns) {
1180 ConstantInt::get(CA->getType(), Shape.FrameAlign.value()));
1181 CA->eraseFromParent();
1182 }
1183
1184 if (Shape.CoroSizes.empty())
1185 return;
1186
1187 // In the same function all coro.sizes should have the same result type.
1188 auto *SizeIntrin = Shape.CoroSizes.back();
1189 Module *M = SizeIntrin->getModule();
1190 const DataLayout &DL = M->getDataLayout();
1191 auto Size = DL.getTypeAllocSize(Shape.FrameTy);
1192 auto *SizeConstant = ConstantInt::get(SizeIntrin->getType(), Size);
1193
1194 for (CoroSizeInst *CS : Shape.CoroSizes) {
1195 CS->replaceAllUsesWith(SizeConstant);
1196 CS->eraseFromParent();
1197 }
1198}
1199
1200// Create a global constant array containing pointers to functions provided and
1201// set Info parameter of CoroBegin to point at this constant. Example:
1202//
1203// @f.resumers = internal constant [2 x void(%f.frame*)*]
1204// [void(%f.frame*)* @f.resume, void(%f.frame*)* @f.destroy]
1205// define void @f() {
1206// ...
1207// call i8* @llvm.coro.begin(i8* null, i32 0, i8* null,
1208// i8* bitcast([2 x void(%f.frame*)*] * @f.resumers to i8*))
1209//
1210// Assumes that all the functions have the same signature.
1211static void setCoroInfo(Function &F, coro::Shape &Shape,
1213 // This only works under the switch-lowering ABI because coro elision
1214 // only works on the switch-lowering ABI.
1215 assert(Shape.ABI == coro::ABI::Switch);
1216
1217 SmallVector<Constant *, 4> Args(Fns.begin(), Fns.end());
1218 assert(!Args.empty());
1219 Function *Part = *Fns.begin();
1220 Module *M = Part->getParent();
1221 auto *ArrTy = ArrayType::get(Part->getType(), Args.size());
1222
1223 auto *ConstVal = ConstantArray::get(ArrTy, Args);
1224 auto *GV = new GlobalVariable(*M, ConstVal->getType(), /*isConstant=*/true,
1225 GlobalVariable::PrivateLinkage, ConstVal,
1226 F.getName() + Twine(".resumers"));
1227
1228 // Update coro.begin instruction to refer to this constant.
1229 LLVMContext &C = F.getContext();
1231 Shape.getSwitchCoroId()->setInfo(BC);
1232}
1233
1234// Store addresses of Resume/Destroy/Cleanup functions in the coroutine frame.
1235static void updateCoroFrame(coro::Shape &Shape, Function *ResumeFn,
1236 Function *DestroyFn, Function *CleanupFn) {
1237 assert(Shape.ABI == coro::ABI::Switch);
1238
1240
1241 auto *ResumeAddr = Builder.CreateStructGEP(
1243 "resume.addr");
1244 Builder.CreateStore(ResumeFn, ResumeAddr);
1245
1246 Value *DestroyOrCleanupFn = DestroyFn;
1247
1248 CoroIdInst *CoroId = Shape.getSwitchCoroId();
1249 if (CoroAllocInst *CA = CoroId->getCoroAlloc()) {
1250 // If there is a CoroAlloc and it returns false (meaning we elide the
1251 // allocation, use CleanupFn instead of DestroyFn).
1252 DestroyOrCleanupFn = Builder.CreateSelect(CA, DestroyFn, CleanupFn);
1253 }
1254
1255 auto *DestroyAddr = Builder.CreateStructGEP(
1257 "destroy.addr");
1258 Builder.CreateStore(DestroyOrCleanupFn, DestroyAddr);
1259}
1260
1263
1264#ifndef NDEBUG
1265 // For now, we do a mandatory verification step because we don't
1266 // entirely trust this pass. Note that we don't want to add a verifier
1267 // pass to FPM below because it will also verify all the global data.
1268 if (verifyFunction(F, &errs()))
1269 report_fatal_error("Broken function");
1270#endif
1271}
1272
1273// Assuming we arrived at the block NewBlock from Prev instruction, store
1274// PHI's incoming values in the ResolvedValues map.
1275static void
1277 DenseMap<Value *, Value *> &ResolvedValues) {
1278 auto *PrevBB = Prev->getParent();
1279 for (PHINode &PN : NewBlock->phis()) {
1280 auto V = PN.getIncomingValueForBlock(PrevBB);
1281 // See if we already resolved it.
1282 auto VI = ResolvedValues.find(V);
1283 if (VI != ResolvedValues.end())
1284 V = VI->second;
1285 // Remember the value.
1286 ResolvedValues[&PN] = V;
1287 }
1288}
1289
1290// Replace a sequence of branches leading to a ret, with a clone of a ret
1291// instruction. Suspend instruction represented by a switch, track the PHI
1292// values and select the correct case successor when possible.
1294 // There is nothing to simplify.
1295 if (isa<ReturnInst>(InitialInst))
1296 return false;
1297
1298 DenseMap<Value *, Value *> ResolvedValues;
1299 assert(InitialInst->getModule());
1300 const DataLayout &DL = InitialInst->getModule()->getDataLayout();
1301
1302 auto GetFirstValidInstruction = [](Instruction *I) {
1303 while (I) {
1304 // BitCastInst wouldn't generate actual code so that we could skip it.
1305 if (isa<BitCastInst>(I) || I->isDebugOrPseudoInst() ||
1306 I->isLifetimeStartOrEnd())
1307 I = I->getNextNode();
1308 else if (isInstructionTriviallyDead(I))
1309 // Duing we are in the middle of the transformation, we need to erase
1310 // the dead instruction manually.
1311 I = &*I->eraseFromParent();
1312 else
1313 break;
1314 }
1315 return I;
1316 };
1317
1318 auto TryResolveConstant = [&ResolvedValues](Value *V) {
1319 auto It = ResolvedValues.find(V);
1320 if (It != ResolvedValues.end())
1321 V = It->second;
1322 return dyn_cast<ConstantInt>(V);
1323 };
1324
1325 Instruction *I = InitialInst;
1326 while (I->isTerminator() || isa<CmpInst>(I)) {
1327 if (isa<ReturnInst>(I)) {
1328 ReplaceInstWithInst(InitialInst, I->clone());
1329 return true;
1330 }
1331
1332 if (auto *BR = dyn_cast<BranchInst>(I)) {
1333 unsigned SuccIndex = 0;
1334 if (BR->isConditional()) {
1335 // Handle the case the condition of the conditional branch is constant.
1336 // e.g.,
1337 //
1338 // br i1 false, label %cleanup, label %CoroEnd
1339 //
1340 // It is possible during the transformation. We could continue the
1341 // simplifying in this case.
1342 ConstantInt *Cond = TryResolveConstant(BR->getCondition());
1343 if (!Cond)
1344 return false;
1345
1346 SuccIndex = Cond->isOne() ? 0 : 1;
1347 }
1348
1349 BasicBlock *Succ = BR->getSuccessor(SuccIndex);
1350 scanPHIsAndUpdateValueMap(I, Succ, ResolvedValues);
1351 I = GetFirstValidInstruction(Succ->getFirstNonPHIOrDbgOrLifetime());
1352
1353 continue;
1354 }
1355
1356 if (auto *CondCmp = dyn_cast<CmpInst>(I)) {
1357 // If the case number of suspended switch instruction is reduced to
1358 // 1, then it is simplified to CmpInst in llvm::ConstantFoldTerminator.
1359 auto *BR = dyn_cast<BranchInst>(
1360 GetFirstValidInstruction(CondCmp->getNextNode()));
1361 if (!BR || !BR->isConditional() || CondCmp != BR->getCondition())
1362 return false;
1363
1364 // And the comparsion looks like : %cond = icmp eq i8 %V, constant.
1365 // So we try to resolve constant for the first operand only since the
1366 // second operand should be literal constant by design.
1367 ConstantInt *Cond0 = TryResolveConstant(CondCmp->getOperand(0));
1368 auto *Cond1 = dyn_cast<ConstantInt>(CondCmp->getOperand(1));
1369 if (!Cond0 || !Cond1)
1370 return false;
1371
1372 // Both operands of the CmpInst are Constant. So that we could evaluate
1373 // it immediately to get the destination.
1374 auto *ConstResult =
1375 dyn_cast_or_null<ConstantInt>(ConstantFoldCompareInstOperands(
1376 CondCmp->getPredicate(), Cond0, Cond1, DL));
1377 if (!ConstResult)
1378 return false;
1379
1380 ResolvedValues[BR->getCondition()] = ConstResult;
1381
1382 // Handle this branch in next iteration.
1383 I = BR;
1384 continue;
1385 }
1386
1387 if (auto *SI = dyn_cast<SwitchInst>(I)) {
1388 ConstantInt *Cond = TryResolveConstant(SI->getCondition());
1389 if (!Cond)
1390 return false;
1391
1392 BasicBlock *BB = SI->findCaseValue(Cond)->getCaseSuccessor();
1393 scanPHIsAndUpdateValueMap(I, BB, ResolvedValues);
1394 I = GetFirstValidInstruction(BB->getFirstNonPHIOrDbgOrLifetime());
1395 continue;
1396 }
1397
1398 return false;
1399 }
1400
1401 return false;
1402}
1403
1404// Check whether CI obeys the rules of musttail attribute.
1405static bool shouldBeMustTail(const CallInst &CI, const Function &F) {
1406 if (CI.isInlineAsm())
1407 return false;
1408
1409 // Match prototypes and calling conventions of resume function.
1410 FunctionType *CalleeTy = CI.getFunctionType();
1411 if (!CalleeTy->getReturnType()->isVoidTy() || (CalleeTy->getNumParams() != 1))
1412 return false;
1413
1414 Type *CalleeParmTy = CalleeTy->getParamType(0);
1415 if (!CalleeParmTy->isPointerTy() ||
1416 (CalleeParmTy->getPointerAddressSpace() != 0))
1417 return false;
1418
1419 if (CI.getCallingConv() != F.getCallingConv())
1420 return false;
1421
1422 // CI should not has any ABI-impacting function attributes.
1423 static const Attribute::AttrKind ABIAttrs[] = {
1424 Attribute::StructRet, Attribute::ByVal, Attribute::InAlloca,
1425 Attribute::Preallocated, Attribute::InReg, Attribute::Returned,
1426 Attribute::SwiftSelf, Attribute::SwiftError};
1427 AttributeList Attrs = CI.getAttributes();
1428 for (auto AK : ABIAttrs)
1429 if (Attrs.hasParamAttr(0, AK))
1430 return false;
1431
1432 return true;
1433}
1434
1435// Add musttail to any resume instructions that is immediately followed by a
1436// suspend (i.e. ret). We do this even in -O0 to support guaranteed tail call
1437// for symmetrical coroutine control transfer (C++ Coroutines TS extension).
1438// This transformation is done only in the resume part of the coroutine that has
1439// identical signature and calling convention as the coro.resume call.
1441 bool changed = false;
1442
1443 // Collect potential resume instructions.
1445 for (auto &I : instructions(F))
1446 if (auto *Call = dyn_cast<CallInst>(&I))
1447 if (shouldBeMustTail(*Call, F))
1448 Resumes.push_back(Call);
1449
1450 // Set musttail on those that are followed by a ret instruction.
1451 for (CallInst *Call : Resumes)
1452 // Skip targets which don't support tail call on the specific case.
1453 if (TTI.supportsTailCallFor(Call) &&
1454 simplifyTerminatorLeadingToRet(Call->getNextNode())) {
1455 Call->setTailCallKind(CallInst::TCK_MustTail);
1456 changed = true;
1457 }
1458
1459 if (changed)
1461}
1462
1463// Coroutine has no suspend points. Remove heap allocation for the coroutine
1464// frame if possible.
1466 auto *CoroBegin = Shape.CoroBegin;
1467 auto *CoroId = CoroBegin->getId();
1468 auto *AllocInst = CoroId->getCoroAlloc();
1469 switch (Shape.ABI) {
1470 case coro::ABI::Switch: {
1471 auto SwitchId = cast<CoroIdInst>(CoroId);
1472 coro::replaceCoroFree(SwitchId, /*Elide=*/AllocInst != nullptr);
1473 if (AllocInst) {
1474 IRBuilder<> Builder(AllocInst);
1475 auto *Frame = Builder.CreateAlloca(Shape.FrameTy);
1476 Frame->setAlignment(Shape.FrameAlign);
1477 auto *VFrame = Builder.CreateBitCast(Frame, Builder.getInt8PtrTy());
1478 AllocInst->replaceAllUsesWith(Builder.getFalse());
1479 AllocInst->eraseFromParent();
1480 CoroBegin->replaceAllUsesWith(VFrame);
1481 } else {
1482 CoroBegin->replaceAllUsesWith(CoroBegin->getMem());
1483 }
1484
1485 break;
1486 }
1487 case coro::ABI::Async:
1488 case coro::ABI::Retcon:
1489 case coro::ABI::RetconOnce:
1490 CoroBegin->replaceAllUsesWith(UndefValue::get(CoroBegin->getType()));
1491 break;
1492 }
1493
1494 CoroBegin->eraseFromParent();
1495}
1496
1497// SimplifySuspendPoint needs to check that there is no calls between
1498// coro_save and coro_suspend, since any of the calls may potentially resume
1499// the coroutine and if that is the case we cannot eliminate the suspend point.
1501 for (Instruction *I = From; I != To; I = I->getNextNode()) {
1502 // Assume that no intrinsic can resume the coroutine.
1503 if (isa<IntrinsicInst>(I))
1504 continue;
1505
1506 if (isa<CallBase>(I))
1507 return true;
1508 }
1509 return false;
1510}
1511
1512static bool hasCallsInBlocksBetween(BasicBlock *SaveBB, BasicBlock *ResDesBB) {
1515
1516 Set.insert(SaveBB);
1517 Worklist.push_back(ResDesBB);
1518
1519 // Accumulate all blocks between SaveBB and ResDesBB. Because CoroSaveIntr
1520 // returns a token consumed by suspend instruction, all blocks in between
1521 // will have to eventually hit SaveBB when going backwards from ResDesBB.
1522 while (!Worklist.empty()) {
1523 auto *BB = Worklist.pop_back_val();
1524 Set.insert(BB);
1525 for (auto *Pred : predecessors(BB))
1526 if (!Set.contains(Pred))
1527 Worklist.push_back(Pred);
1528 }
1529
1530 // SaveBB and ResDesBB are checked separately in hasCallsBetween.
1531 Set.erase(SaveBB);
1532 Set.erase(ResDesBB);
1533
1534 for (auto *BB : Set)
1535 if (hasCallsInBlockBetween(BB->getFirstNonPHI(), nullptr))
1536 return true;
1537
1538 return false;
1539}
1540
1541static bool hasCallsBetween(Instruction *Save, Instruction *ResumeOrDestroy) {
1542 auto *SaveBB = Save->getParent();
1543 auto *ResumeOrDestroyBB = ResumeOrDestroy->getParent();
1544
1545 if (SaveBB == ResumeOrDestroyBB)
1546 return hasCallsInBlockBetween(Save->getNextNode(), ResumeOrDestroy);
1547
1548 // Any calls from Save to the end of the block?
1549 if (hasCallsInBlockBetween(Save->getNextNode(), nullptr))
1550 return true;
1551
1552 // Any calls from begging of the block up to ResumeOrDestroy?
1553 if (hasCallsInBlockBetween(ResumeOrDestroyBB->getFirstNonPHI(),
1554 ResumeOrDestroy))
1555 return true;
1556
1557 // Any calls in all of the blocks between SaveBB and ResumeOrDestroyBB?
1558 if (hasCallsInBlocksBetween(SaveBB, ResumeOrDestroyBB))
1559 return true;
1560
1561 return false;
1562}
1563
1564// If a SuspendIntrin is preceded by Resume or Destroy, we can eliminate the
1565// suspend point and replace it with nornal control flow.
1567 CoroBeginInst *CoroBegin) {
1568 Instruction *Prev = Suspend->getPrevNode();
1569 if (!Prev) {
1570 auto *Pred = Suspend->getParent()->getSinglePredecessor();
1571 if (!Pred)
1572 return false;
1573 Prev = Pred->getTerminator();
1574 }
1575
1576 CallBase *CB = dyn_cast<CallBase>(Prev);
1577 if (!CB)
1578 return false;
1579
1580 auto *Callee = CB->getCalledOperand()->stripPointerCasts();
1581
1582 // See if the callsite is for resumption or destruction of the coroutine.
1583 auto *SubFn = dyn_cast<CoroSubFnInst>(Callee);
1584 if (!SubFn)
1585 return false;
1586
1587 // Does not refer to the current coroutine, we cannot do anything with it.
1588 if (SubFn->getFrame() != CoroBegin)
1589 return false;
1590
1591 // See if the transformation is safe. Specifically, see if there are any
1592 // calls in between Save and CallInstr. They can potenitally resume the
1593 // coroutine rendering this optimization unsafe.
1594 auto *Save = Suspend->getCoroSave();
1595 if (hasCallsBetween(Save, CB))
1596 return false;
1597
1598 // Replace llvm.coro.suspend with the value that results in resumption over
1599 // the resume or cleanup path.
1600 Suspend->replaceAllUsesWith(SubFn->getRawIndex());
1601 Suspend->eraseFromParent();
1602 Save->eraseFromParent();
1603
1604 // No longer need a call to coro.resume or coro.destroy.
1605 if (auto *Invoke = dyn_cast<InvokeInst>(CB)) {
1606 BranchInst::Create(Invoke->getNormalDest(), Invoke);
1607 }
1608
1609 // Grab the CalledValue from CB before erasing the CallInstr.
1610 auto *CalledValue = CB->getCalledOperand();
1611 CB->eraseFromParent();
1612
1613 // If no more users remove it. Usually it is a bitcast of SubFn.
1614 if (CalledValue != SubFn && CalledValue->user_empty())
1615 if (auto *I = dyn_cast<Instruction>(CalledValue))
1616 I->eraseFromParent();
1617
1618 // Now we are good to remove SubFn.
1619 if (SubFn->user_empty())
1620 SubFn->eraseFromParent();
1621
1622 return true;
1623}
1624
1625// Remove suspend points that are simplified.
1627 // Currently, the only simplification we do is switch-lowering-specific.
1628 if (Shape.ABI != coro::ABI::Switch)
1629 return;
1630
1631 auto &S = Shape.CoroSuspends;
1632 size_t I = 0, N = S.size();
1633 if (N == 0)
1634 return;
1635
1636 size_t ChangedFinalIndex = std::numeric_limits<size_t>::max();
1637 while (true) {
1638 auto SI = cast<CoroSuspendInst>(S[I]);
1639 // Leave final.suspend to handleFinalSuspend since it is undefined behavior
1640 // to resume a coroutine suspended at the final suspend point.
1641 if (!SI->isFinal() && simplifySuspendPoint(SI, Shape.CoroBegin)) {
1642 if (--N == I)
1643 break;
1644
1645 std::swap(S[I], S[N]);
1646
1647 if (cast<CoroSuspendInst>(S[I])->isFinal()) {
1649 ChangedFinalIndex = I;
1650 }
1651
1652 continue;
1653 }
1654 if (++I == N)
1655 break;
1656 }
1657 S.resize(N);
1658
1659 // Maintain final.suspend in case final suspend was swapped.
1660 // Due to we requrie the final suspend to be the last element of CoroSuspends.
1661 if (ChangedFinalIndex < N) {
1662 assert(cast<CoroSuspendInst>(S[ChangedFinalIndex])->isFinal());
1663 std::swap(S[ChangedFinalIndex], S.back());
1664 }
1665}
1666
1670 assert(Shape.ABI == coro::ABI::Switch);
1671
1672 createResumeEntryBlock(F, Shape);
1673 auto ResumeClone = createClone(F, ".resume", Shape,
1674 CoroCloner::Kind::SwitchResume);
1675 auto DestroyClone = createClone(F, ".destroy", Shape,
1676 CoroCloner::Kind::SwitchUnwind);
1677 auto CleanupClone = createClone(F, ".cleanup", Shape,
1678 CoroCloner::Kind::SwitchCleanup);
1679
1680 postSplitCleanup(*ResumeClone);
1681 postSplitCleanup(*DestroyClone);
1682 postSplitCleanup(*CleanupClone);
1683
1684 // Adding musttail call to support symmetric transfer.
1685 // Skip targets which don't support tail call.
1686 //
1687 // FIXME: Could we support symmetric transfer effectively without musttail
1688 // call?
1689 if (TTI.supportsTailCalls())
1690 addMustTailToCoroResumes(*ResumeClone, TTI);
1691
1692 // Store addresses resume/destroy/cleanup functions in the coroutine frame.
1693 updateCoroFrame(Shape, ResumeClone, DestroyClone, CleanupClone);
1694
1695 assert(Clones.empty());
1696 Clones.push_back(ResumeClone);
1697 Clones.push_back(DestroyClone);
1698 Clones.push_back(CleanupClone);
1699
1700 // Create a constant array referring to resume/destroy/clone functions pointed
1701 // by the last argument of @llvm.coro.info, so that CoroElide pass can
1702 // determined correct function to call.
1703 setCoroInfo(F, Shape, Clones);
1704}
1705
1707 Value *Continuation) {
1708 auto *ResumeIntrinsic = Suspend->getResumeFunction();
1709 auto &Context = Suspend->getParent()->getParent()->getContext();
1710 auto *Int8PtrTy = Type::getInt8PtrTy(Context);
1711
1712 IRBuilder<> Builder(ResumeIntrinsic);
1713 auto *Val = Builder.CreateBitOrPointerCast(Continuation, Int8PtrTy);
1714 ResumeIntrinsic->replaceAllUsesWith(Val);
1715 ResumeIntrinsic->eraseFromParent();
1717 UndefValue::get(Int8PtrTy));
1718}
1719
1720/// Coerce the arguments in \p FnArgs according to \p FnTy in \p CallArgs.
1721static void coerceArguments(IRBuilder<> &Builder, FunctionType *FnTy,
1722 ArrayRef<Value *> FnArgs,
1723 SmallVectorImpl<Value *> &CallArgs) {
1724 size_t ArgIdx = 0;
1725 for (auto *paramTy : FnTy->params()) {
1726 assert(ArgIdx < FnArgs.size());
1727 if (paramTy != FnArgs[ArgIdx]->getType())
1728 CallArgs.push_back(
1729 Builder.CreateBitOrPointerCast(FnArgs[ArgIdx], paramTy));
1730 else
1731 CallArgs.push_back(FnArgs[ArgIdx]);
1732 ++ArgIdx;
1733 }
1734}
1735
1738 IRBuilder<> &Builder) {
1739 auto *FnTy = MustTailCallFn->getFunctionType();
1740 // Coerce the arguments, llvm optimizations seem to ignore the types in
1741 // vaarg functions and throws away casts in optimized mode.
1742 SmallVector<Value *, 8> CallArgs;
1743 coerceArguments(Builder, FnTy, Arguments, CallArgs);
1744
1745 auto *TailCall = Builder.CreateCall(FnTy, MustTailCallFn, CallArgs);
1746 TailCall->setTailCallKind(CallInst::TCK_MustTail);
1747 TailCall->setDebugLoc(Loc);
1748 TailCall->setCallingConv(MustTailCallFn->getCallingConv());
1749 return TailCall;
1750}
1751
1754 assert(Shape.ABI == coro::ABI::Async);
1755 assert(Clones.empty());
1756 // Reset various things that the optimizer might have decided it
1757 // "knows" about the coroutine function due to not seeing a return.
1758 F.removeFnAttr(Attribute::NoReturn);
1759 F.removeRetAttr(Attribute::NoAlias);
1760 F.removeRetAttr(Attribute::NonNull);
1761
1762 auto &Context = F.getContext();
1763 auto *Int8PtrTy = Type::getInt8PtrTy(Context);
1764
1765 auto *Id = cast<CoroIdAsyncInst>(Shape.CoroBegin->getId());
1766 IRBuilder<> Builder(Id);
1767
1768 auto *FramePtr = Id->getStorage();
1769 FramePtr = Builder.CreateBitOrPointerCast(FramePtr, Int8PtrTy);
1770 FramePtr = Builder.CreateConstInBoundsGEP1_32(
1772 "async.ctx.frameptr");
1773
1774 // Map all uses of llvm.coro.begin to the allocated frame pointer.
1775 {
1776 // Make sure we don't invalidate Shape.FramePtr.
1777 TrackingVH<Value> Handle(Shape.FramePtr);
1779 Shape.FramePtr = Handle.getValPtr();
1780 }
1781
1782 // Create all the functions in order after the main function.
1783 auto NextF = std::next(F.getIterator());
1784
1785 // Create a continuation function for each of the suspend points.
1786 Clones.reserve(Shape.CoroSuspends.size());
1787 for (size_t Idx = 0, End = Shape.CoroSuspends.size(); Idx != End; ++Idx) {
1788 auto *Suspend = cast<CoroSuspendAsyncInst>(Shape.CoroSuspends[Idx]);
1789
1790 // Create the clone declaration.
1791 auto ResumeNameSuffix = ".resume.";
1792 auto ProjectionFunctionName =
1793 Suspend->getAsyncContextProjectionFunction()->getName();
1794 bool UseSwiftMangling = false;
1795 if (ProjectionFunctionName.equals("__swift_async_resume_project_context")) {
1796 ResumeNameSuffix = "TQ";
1797 UseSwiftMangling = true;
1798 } else if (ProjectionFunctionName.equals(
1799 "__swift_async_resume_get_context")) {
1800 ResumeNameSuffix = "TY";
1801 UseSwiftMangling = true;
1802 }
1803 auto *Continuation = createCloneDeclaration(
1804 F, Shape,
1805 UseSwiftMangling ? ResumeNameSuffix + Twine(Idx) + "_"
1806 : ResumeNameSuffix + Twine(Idx),
1807 NextF, Suspend);
1808 Clones.push_back(Continuation);
1809
1810 // Insert a branch to a new return block immediately before the suspend
1811 // point.
1812 auto *SuspendBB = Suspend->getParent();
1813 auto *NewSuspendBB = SuspendBB->splitBasicBlock(Suspend);
1814 auto *Branch = cast<BranchInst>(SuspendBB->getTerminator());
1815
1816 // Place it before the first suspend.
1817 auto *ReturnBB =
1818 BasicBlock::Create(F.getContext(), "coro.return", &F, NewSuspendBB);
1819 Branch->setSuccessor(0, ReturnBB);
1820
1821 IRBuilder<> Builder(ReturnBB);
1822
1823 // Insert the call to the tail call function and inline it.
1824 auto *Fn = Suspend->getMustTailCallFunction();
1825 SmallVector<Value *, 8> Args(Suspend->args());
1826 auto FnArgs = ArrayRef<Value *>(Args).drop_front(
1828 auto *TailCall =
1829 coro::createMustTailCall(Suspend->getDebugLoc(), Fn, FnArgs, Builder);
1830 Builder.CreateRetVoid();
1831 InlineFunctionInfo FnInfo;
1832 auto InlineRes = InlineFunction(*TailCall, FnInfo);
1833 assert(InlineRes.isSuccess() && "Expected inlining to succeed");
1834 (void)InlineRes;
1835
1836 // Replace the lvm.coro.async.resume intrisic call.
1837 replaceAsyncResumeFunction(Suspend, Continuation);
1838 }
1839
1840 assert(Clones.size() == Shape.CoroSuspends.size());
1841 for (size_t Idx = 0, End = Shape.CoroSuspends.size(); Idx != End; ++Idx) {
1842 auto *Suspend = Shape.CoroSuspends[Idx];
1843 auto *Clone = Clones[Idx];
1844
1845 CoroCloner(F, "resume." + Twine(Idx), Shape, Clone, Suspend).create();
1846 }
1847}
1848
1851 assert(Shape.ABI == coro::ABI::Retcon ||
1852 Shape.ABI == coro::ABI::RetconOnce);
1853 assert(Clones.empty());
1854
1855 // Reset various things that the optimizer might have decided it
1856 // "knows" about the coroutine function due to not seeing a return.
1857 F.removeFnAttr(Attribute::NoReturn);
1858 F.removeRetAttr(Attribute::NoAlias);
1859 F.removeRetAttr(Attribute::NonNull);
1860
1861 // Allocate the frame.
1862 auto *Id = cast<AnyCoroIdRetconInst>(Shape.CoroBegin->getId());
1863 Value *RawFramePtr;
1865 RawFramePtr = Id->getStorage();
1866 } else {
1867 IRBuilder<> Builder(Id);
1868
1869 // Determine the size of the frame.
1870 const DataLayout &DL = F.getParent()->getDataLayout();
1871 auto Size = DL.getTypeAllocSize(Shape.FrameTy);
1872
1873 // Allocate. We don't need to update the call graph node because we're
1874 // going to recompute it from scratch after splitting.
1875 // FIXME: pass the required alignment
1876 RawFramePtr = Shape.emitAlloc(Builder, Builder.getInt64(Size), nullptr);
1877 RawFramePtr =
1878 Builder.CreateBitCast(RawFramePtr, Shape.CoroBegin->getType());
1879
1880 // Stash the allocated frame pointer in the continuation storage.
1881 Builder.CreateStore(RawFramePtr, Id->getStorage());
1882 }
1883
1884 // Map all uses of llvm.coro.begin to the allocated frame pointer.
1885 {
1886 // Make sure we don't invalidate Shape.FramePtr.
1887 TrackingVH<Value> Handle(Shape.FramePtr);
1888 Shape.CoroBegin->replaceAllUsesWith(RawFramePtr);
1889 Shape.FramePtr = Handle.getValPtr();
1890 }
1891
1892 // Create a unique return block.
1893 BasicBlock *ReturnBB = nullptr;
1894 SmallVector<PHINode *, 4> ReturnPHIs;
1895
1896 // Create all the functions in order after the main function.
1897 auto NextF = std::next(F.getIterator());
1898
1899 // Create a continuation function for each of the suspend points.
1900 Clones.reserve(Shape.CoroSuspends.size());
1901 for (size_t i = 0, e = Shape.CoroSuspends.size(); i != e; ++i) {
1902 auto Suspend = cast<CoroSuspendRetconInst>(Shape.CoroSuspends[i]);
1903
1904 // Create the clone declaration.
1905 auto Continuation =
1906 createCloneDeclaration(F, Shape, ".resume." + Twine(i), NextF, nullptr);
1907 Clones.push_back(Continuation);
1908
1909 // Insert a branch to the unified return block immediately before
1910 // the suspend point.
1911 auto SuspendBB = Suspend->getParent();
1912 auto NewSuspendBB = SuspendBB->splitBasicBlock(Suspend);
1913 auto Branch = cast<BranchInst>(SuspendBB->getTerminator());
1914
1915 // Create the unified return block.
1916 if (!ReturnBB) {
1917 // Place it before the first suspend.
1918 ReturnBB = BasicBlock::Create(F.getContext(), "coro.return", &F,
1919 NewSuspendBB);
1920 Shape.RetconLowering.ReturnBlock = ReturnBB;
1921
1922 IRBuilder<> Builder(ReturnBB);
1923
1924 // Create PHIs for all the return values.
1925 assert(ReturnPHIs.empty());
1926
1927 // First, the continuation.
1928 ReturnPHIs.push_back(Builder.CreatePHI(Continuation->getType(),
1929 Shape.CoroSuspends.size()));
1930
1931 // Next, all the directly-yielded values.
1932 for (auto *ResultTy : Shape.getRetconResultTypes())
1933 ReturnPHIs.push_back(Builder.CreatePHI(ResultTy,
1934 Shape.CoroSuspends.size()));
1935
1936 // Build the return value.
1937 auto RetTy = F.getReturnType();
1938
1939 // Cast the continuation value if necessary.
1940 // We can't rely on the types matching up because that type would
1941 // have to be infinite.
1942 auto CastedContinuationTy =
1943 (ReturnPHIs.size() == 1 ? RetTy : RetTy->getStructElementType(0));
1944 auto *CastedContinuation =
1945 Builder.CreateBitCast(ReturnPHIs[0], CastedContinuationTy);
1946
1947 Value *RetV;
1948 if (ReturnPHIs.size() == 1) {
1949 RetV = CastedContinuation;
1950 } else {
1951 RetV = PoisonValue::get(RetTy);
1952 RetV = Builder.CreateInsertValue(RetV, CastedContinuation, 0);
1953 for (size_t I = 1, E = ReturnPHIs.size(); I != E; ++I)
1954 RetV = Builder.CreateInsertValue(RetV, ReturnPHIs[I], I);
1955 }
1956
1957 Builder.CreateRet(RetV);
1958 }
1959
1960 // Branch to the return block.
1961 Branch->setSuccessor(0, ReturnBB);
1962 ReturnPHIs[0]->addIncoming(Continuation, SuspendBB);
1963 size_t NextPHIIndex = 1;
1964 for (auto &VUse : Suspend->value_operands())
1965 ReturnPHIs[NextPHIIndex++]->addIncoming(&*VUse, SuspendBB);
1966 assert(NextPHIIndex == ReturnPHIs.size());
1967 }
1968
1969 assert(Clones.size() == Shape.CoroSuspends.size());
1970 for (size_t i = 0, e = Shape.CoroSuspends.size(); i != e; ++i) {
1971 auto Suspend = Shape.CoroSuspends[i];
1972 auto Clone = Clones[i];
1973
1974 CoroCloner(F, "resume." + Twine(i), Shape, Clone, Suspend).create();
1975 }
1976}
1977
1978namespace {
1979 class PrettyStackTraceFunction : public PrettyStackTraceEntry {
1980 Function &F;
1981 public:
1982 PrettyStackTraceFunction(Function &F) : F(F) {}
1983 void print(raw_ostream &OS) const override {
1984 OS << "While splitting coroutine ";
1985 F.printAsOperand(OS, /*print type*/ false, F.getParent());
1986 OS << "\n";
1987 }
1988 };
1989}
1990
1991static coro::Shape
1993 TargetTransformInfo &TTI, bool OptimizeFrame,
1994 std::function<bool(Instruction &)> MaterializableCallback) {
1995 PrettyStackTraceFunction prettyStackTrace(F);
1996
1997 // The suspend-crossing algorithm in buildCoroutineFrame get tripped
1998 // up by uses in unreachable blocks, so remove them as a first pass.
2000
2001 coro::Shape Shape(F, OptimizeFrame);
2002 if (!Shape.CoroBegin)
2003 return Shape;
2004
2005 simplifySuspendPoints(Shape);
2006 buildCoroutineFrame(F, Shape, MaterializableCallback);
2008
2009 // If there are no suspend points, no split required, just remove
2010 // the allocation and deallocation blocks, they are not needed.
2011 if (Shape.CoroSuspends.empty()) {
2013 } else {
2014 switch (Shape.ABI) {
2015 case coro::ABI::Switch:
2016 splitSwitchCoroutine(F, Shape, Clones, TTI);
2017 break;
2018 case coro::ABI::Async:
2019 splitAsyncCoroutine(F, Shape, Clones);
2020 break;
2021 case coro::ABI::Retcon:
2022 case coro::ABI::RetconOnce:
2023 splitRetconCoroutine(F, Shape, Clones);
2024 break;
2025 }
2026 }
2027
2028 // Replace all the swifterror operations in the original function.
2029 // This invalidates SwiftErrorOps in the Shape.
2030 replaceSwiftErrorOps(F, Shape, nullptr);
2031
2032 // Salvage debug intrinsics that point into the coroutine frame in the
2033 // original function. The Cloner has already salvaged debug info in the new
2034 // coroutine funclets.
2036 for (auto *DDI : collectDbgVariableIntrinsics(F))
2037 coro::salvageDebugInfo(ArgToAllocaMap, DDI, Shape.OptimizeFrame,
2038 false /*UseEntryValue*/);
2039
2040 return Shape;
2041}
2042
2043/// Remove calls to llvm.coro.end in the original function.
2044static void removeCoroEnds(const coro::Shape &Shape) {
2045 for (auto *End : Shape.CoroEnds) {
2046 replaceCoroEnd(End, Shape, Shape.FramePtr, /*in resume*/ false, nullptr);
2047 }
2048}
2049
2051 LazyCallGraph::Node &N, const coro::Shape &Shape,
2055 if (!Shape.CoroBegin)
2056 return;
2057
2058 if (Shape.ABI != coro::ABI::Switch)
2059 removeCoroEnds(Shape);
2060 else {
2061 for (llvm::AnyCoroEndInst *End : Shape.CoroEnds) {
2062 auto &Context = End->getContext();
2063 End->replaceAllUsesWith(ConstantInt::getFalse(Context));
2064 End->eraseFromParent();
2065 }
2066 }
2067
2068 if (!Clones.empty()) {
2069 switch (Shape.ABI) {
2070 case coro::ABI::Switch:
2071 // Each clone in the Switch lowering is independent of the other clones.
2072 // Let the LazyCallGraph know about each one separately.
2073 for (Function *Clone : Clones)
2074 CG.addSplitFunction(N.getFunction(), *Clone);
2075 break;
2076 case coro::ABI::Async:
2077 case coro::ABI::Retcon:
2078 case coro::ABI::RetconOnce:
2079 // Each clone in the Async/Retcon lowering references of the other clones.
2080 // Let the LazyCallGraph know about all of them at once.
2081 if (!Clones.empty())
2082 CG.addSplitRefRecursiveFunctions(N.getFunction(), Clones);
2083 break;
2084 }
2085
2086 // Let the CGSCC infra handle the changes to the original function.
2088 }
2089
2090 // Do some cleanup and let the CGSCC infra see if we've cleaned up any edges
2091 // to the split functions.
2092 postSplitCleanup(N.getFunction());
2094}
2095
2096/// Replace a call to llvm.coro.prepare.retcon.
2097static void replacePrepare(CallInst *Prepare, LazyCallGraph &CG,
2099 auto CastFn = Prepare->getArgOperand(0); // as an i8*
2100 auto Fn = CastFn->stripPointerCasts(); // as its original type
2101
2102 // Attempt to peephole this pattern:
2103 // %0 = bitcast [[TYPE]] @some_function to i8*
2104 // %1 = call @llvm.coro.prepare.retcon(i8* %0)
2105 // %2 = bitcast %1 to [[TYPE]]
2106 // ==>
2107 // %2 = @some_function
2108 for (Use &U : llvm::make_early_inc_range(Prepare->uses())) {
2109 // Look for bitcasts back to the original function type.
2110 auto *Cast = dyn_cast<BitCastInst>(U.getUser());
2111 if (!Cast || Cast->getType() != Fn->getType())
2112 continue;
2113
2114 // Replace and remove the cast.
2115 Cast->replaceAllUsesWith(Fn);
2116 Cast->eraseFromParent();
2117 }
2118
2119 // Replace any remaining uses with the function as an i8*.
2120 // This can never directly be a callee, so we don't need to update CG.
2121 Prepare->replaceAllUsesWith(CastFn);
2122 Prepare->eraseFromParent();
2123
2124 // Kill dead bitcasts.
2125 while (auto *Cast = dyn_cast<BitCastInst>(CastFn)) {
2126 if (!Cast->use_empty())
2127 break;
2128 CastFn = Cast->getOperand(0);
2129 Cast->eraseFromParent();
2130 }
2131}
2132
2133static bool replaceAllPrepares(Function *PrepareFn, LazyCallGraph &CG,
2135 bool Changed = false;
2136 for (Use &P : llvm::make_early_inc_range(PrepareFn->uses())) {
2137 // Intrinsics can only be used in calls.
2138 auto *Prepare = cast<CallInst>(P.getUser());
2139 replacePrepare(Prepare, CG, C);
2140 Changed = true;
2141 }
2142
2143 return Changed;
2144}
2145
2146static void addPrepareFunction(const Module &M,
2148 StringRef Name) {
2149 auto *PrepareFn = M.getFunction(Name);
2150 if (PrepareFn && !PrepareFn->use_empty())
2151 Fns.push_back(PrepareFn);
2152}
2153
2155 : MaterializableCallback(coro::defaultMaterializable),
2156 OptimizeFrame(OptimizeFrame) {}
2157
2161 // NB: One invariant of a valid LazyCallGraph::SCC is that it must contain a
2162 // non-zero number of nodes, so we assume that here and grab the first
2163 // node's function's module.
2164 Module &M = *C.begin()->getFunction().getParent();
2165 auto &FAM =
2166 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
2167
2168 // Check for uses of llvm.coro.prepare.retcon/async.
2169 SmallVector<Function *, 2> PrepareFns;
2170 addPrepareFunction(M, PrepareFns, "llvm.coro.prepare.retcon");
2171 addPrepareFunction(M, PrepareFns, "llvm.coro.prepare.async");
2172
2173 // Find coroutines for processing.
2175 for (LazyCallGraph::Node &N : C)
2176 if (N.getFunction().isPresplitCoroutine())
2177 Coroutines.push_back(&N);
2178
2179 if (Coroutines.empty() && PrepareFns.empty())
2180 return PreservedAnalyses::all();
2181
2182 if (Coroutines.empty()) {
2183 for (auto *PrepareFn : PrepareFns) {
2184 replaceAllPrepares(PrepareFn, CG, C);
2185 }
2186 }
2187
2188 // Split all the coroutines.
2189 for (LazyCallGraph::Node *N : Coroutines) {
2190 Function &F = N->getFunction();
2191 LLVM_DEBUG(dbgs() << "CoroSplit: Processing coroutine '" << F.getName()
2192 << "\n");
2193 F.setSplittedCoroutine();
2194
2197 const coro::Shape Shape =
2200 updateCallGraphAfterCoroutineSplit(*N, Shape, Clones, C, CG, AM, UR, FAM);
2201
2202 ORE.emit([&]() {
2203 return OptimizationRemark(DEBUG_TYPE, "CoroSplit", &F)
2204 << "Split '" << ore::NV("function", F.getName())
2205 << "' (frame_size=" << ore::NV("frame_size", Shape.FrameSize)
2206 << ", align=" << ore::NV("align", Shape.FrameAlign.value()) << ")";
2207 });
2208
2209 if (!Shape.CoroSuspends.empty()) {
2210 // Run the CGSCC pipeline on the original and newly split functions.
2211 UR.CWorklist.insert(&C);
2212 for (Function *Clone : Clones)
2213 UR.CWorklist.insert(CG.lookupSCC(CG.get(*Clone)));
2214 }
2215 }
2216
2217 if (!PrepareFns.empty()) {
2218 for (auto *PrepareFn : PrepareFns) {
2219 replaceAllPrepares(PrepareFn, CG, C);
2220 }
2221 }
2222
2223 return PreservedAnalyses::none();
2224}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
AMDGPU Lower Kernel Arguments
assume Assume Builder
This file contains the simple types necessary to represent the attributes associated with functions a...
BlockVerifier::State From
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file provides interfaces used to manipulate a call graph, regardless if it is a "old style" Call...
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Remove calls to llvm coro end in the original static function void removeCoroEnds(const coro::Shape &Shape)
Definition: CoroSplit.cpp:2044
static void addSwiftSelfAttrs(AttributeList &Attrs, LLVMContext &Context, unsigned ParamIndex)
Definition: CoroSplit.cpp:913
static void splitSwitchCoroutine(Function &F, coro::Shape &Shape, SmallVectorImpl< Function * > &Clones, TargetTransformInfo &TTI)
Definition: CoroSplit.cpp:1667
static bool hasCallsBetween(Instruction *Save, Instruction *ResumeOrDestroy)
Definition: CoroSplit.cpp:1541
static void replaceSwiftErrorOps(Function &F, coro::Shape &Shape, ValueToValueMapTy *VMap)
Definition: CoroSplit.cpp:665
static void addAsyncContextAttrs(AttributeList &Attrs, LLVMContext &Context, unsigned ParamIndex)
Definition: CoroSplit.cpp:906
static void addMustTailToCoroResumes(Function &F, TargetTransformInfo &TTI)
Definition: CoroSplit.cpp:1440
static void maybeFreeRetconStorage(IRBuilder<> &Builder, const coro::Shape &Shape, Value *FramePtr, CallGraph *CG)
Definition: CoroSplit.cpp:170
static bool hasCallsInBlocksBetween(BasicBlock *SaveBB, BasicBlock *ResDesBB)
Definition: CoroSplit.cpp:1512
static Function * createCloneDeclaration(Function &OrigF, coro::Shape &Shape, const Twine &Suffix, Module::iterator InsertBefore, AnyCoroSuspendInst *ActiveSuspend)
Definition: CoroSplit.cpp:552
static FunctionType * getFunctionTypeFromAsyncSuspend(AnyCoroSuspendInst *Suspend)
Definition: CoroSplit.cpp:544
static void addPrepareFunction(const Module &M, SmallVectorImpl< Function * > &Fns, StringRef Name)
Definition: CoroSplit.cpp:2146
static void updateCallGraphAfterCoroutineSplit(LazyCallGraph::Node &N, const coro::Shape &Shape, const SmallVectorImpl< Function * > &Clones, LazyCallGraph::SCC &C, LazyCallGraph &CG, CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, FunctionAnalysisManager &FAM)
Definition: CoroSplit.cpp:2050
static void simplifySuspendPoints(coro::Shape &Shape)
Definition: CoroSplit.cpp:1626
static void addFramePointerAttrs(AttributeList &Attrs, LLVMContext &Context, unsigned ParamIndex, uint64_t Size, Align Alignment, bool NoAlias)
Definition: CoroSplit.cpp:891
static bool replaceAllPrepares(Function *PrepareFn, LazyCallGraph &CG, LazyCallGraph::SCC &C)
Definition: CoroSplit.cpp:2133
static void replaceFallthroughCoroEnd(AnyCoroEndInst *End, const coro::Shape &Shape, Value *FramePtr, bool InResume, CallGraph *CG)
Replace a non-unwind call to llvm.coro.end.
Definition: CoroSplit.cpp:227
static void replaceFrameSizeAndAlignment(coro::Shape &Shape)
Definition: CoroSplit.cpp:1174
static SmallVector< DbgVariableIntrinsic *, 8 > collectDbgVariableIntrinsics(Function &F)
Returns all DbgVariableIntrinsic in F.
Definition: CoroSplit.cpp:722
static bool replaceCoroEndAsync(AnyCoroEndInst *End)
Replace an llvm.coro.end.async.
Definition: CoroSplit.cpp:184
Replace a call to llvm coro prepare static retcon void replacePrepare(CallInst *Prepare, LazyCallGraph &CG, LazyCallGraph::SCC &C)
Definition: CoroSplit.cpp:2097
static void replaceUnwindCoroEnd(AnyCoroEndInst *End, const coro::Shape &Shape, Value *FramePtr, bool InResume, CallGraph *CG)
Replace an unwind call to llvm.coro.end.
Definition: CoroSplit.cpp:359
static bool simplifySuspendPoint(CoroSuspendInst *Suspend, CoroBeginInst *CoroBegin)
Definition: CoroSplit.cpp:1566
static bool hasCallsInBlockBetween(Instruction *From, Instruction *To)
Definition: CoroSplit.cpp:1500
static void markCoroutineAsDone(IRBuilder<> &Builder, const coro::Shape &Shape, Value *FramePtr)
Definition: CoroSplit.cpp:325
static void splitAsyncCoroutine(Function &F, coro::Shape &Shape, SmallVectorImpl< Function * > &Clones)
Definition: CoroSplit.cpp:1752
static void updateAsyncFuncPointerContextSize(coro::Shape &Shape)
Definition: CoroSplit.cpp:1159
static void replaceCoroEnd(AnyCoroEndInst *End, const coro::Shape &Shape, Value *FramePtr, bool InResume, CallGraph *CG)
Definition: CoroSplit.cpp:397
static void setCoroInfo(Function &F, coro::Shape &Shape, ArrayRef< Function * > Fns)
Definition: CoroSplit.cpp:1211
static void handleNoSuspendCoroutine(coro::Shape &Shape)
Definition: CoroSplit.cpp:1465
static void updateCoroFrame(coro::Shape &Shape, Function *ResumeFn, Function *DestroyFn, Function *CleanupFn)
Definition: CoroSplit.cpp:1235
static void createResumeEntryBlock(Function &F, coro::Shape &Shape)
Definition: CoroSplit.cpp:412
static coro::Shape splitCoroutine(Function &F, SmallVectorImpl< Function * > &Clones, TargetTransformInfo &TTI, bool OptimizeFrame, std::function< bool(Instruction &)> MaterializableCallback)
Definition: CoroSplit.cpp:1992
static void postSplitCleanup(Function &F)
Definition: CoroSplit.cpp:1261
static bool simplifyTerminatorLeadingToRet(Instruction *InitialInst)
Definition: CoroSplit.cpp:1293
static void splitRetconCoroutine(Function &F, coro::Shape &Shape, SmallVectorImpl< Function * > &Clones)
Definition: CoroSplit.cpp:1849
static void scanPHIsAndUpdateValueMap(Instruction *Prev, BasicBlock *NewBlock, DenseMap< Value *, Value * > &ResolvedValues)
Definition: CoroSplit.cpp:1276
Coerce the arguments in p FnArgs according to p FnTy in p static CallArgs void coerceArguments(IRBuilder<> &Builder, FunctionType *FnTy, ArrayRef< Value * > FnArgs, SmallVectorImpl< Value * > &CallArgs)
Definition: CoroSplit.cpp:1721
static void replaceAsyncResumeFunction(CoroSuspendAsyncInst *Suspend, Value *Continuation)
Definition: CoroSplit.cpp:1706
static bool shouldBeMustTail(const CallInst &CI, const Function &F)
Definition: CoroSplit.cpp:1405
static Function * createClone(Function &F, const Twine &Suffix, coro::Shape &Shape, CoroCloner::Kind FKind)
Definition: CoroSplit.cpp:1152
return RetTy
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseMap class.
This file contains constants used for implementing Dwarf debug support.
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:469
static Function * getFunction(Constant *C)
Definition: Evaluator.cpp:236
@ InlineInfo
Rewrite Partial Register Uses
#define DEBUG_TYPE
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Select target instructions out of generic instructions
Implements a lazy call graph analysis and related passes for the new pass manager.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Module.h This file contains the declarations for the Module class.
LLVMContext & Context
#define P(N)
FunctionAnalysisManager FAM
This file provides a priority worklist.
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:40
This pass exposes codegen information to IR-level passes.
static const unsigned FramePtr
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:774
CoroAllocInst * getCoroAlloc()
Definition: CoroInstr.h:84
This class represents an incoming formal argument to a Function.
Definition: Argument.h:28
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:204
iterator end() const
Definition: ArrayRef.h:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
AttrBuilder & addAlignmentAttr(MaybeAlign Align)
This turns an alignment into the form used internally in Attribute.
AttrBuilder & addAttribute(Attribute::AttrKind Val)
Add an attribute to the builder.
AttrBuilder & addDereferenceableAttr(uint64_t Bytes)
This turns the number of dereferenceable bytes into the form used internally in Attribute.
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:84
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition: BasicBlock.h:393
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:105
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="", bool Before=false)
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:414
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:296
const Instruction * getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode, a debug intrinsic,...
Definition: BasicBlock.cpp:241
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:112
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:127
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1190
bool isInlineAsm() const
Check if this call is an inline asm statement.
Definition: InstrTypes.h:1479
CallingConv::ID getCallingConv() const
Definition: InstrTypes.h:1470
Value * getCalledOperand() const
Definition: InstrTypes.h:1405
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1357
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1270
AttributeList getAttributes() const
Return the parameter attributes for this call.
Definition: InstrTypes.h:1489
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:72
This class represents a function call, abstracting a target machine's calling convention.
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1235
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:2025
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:833
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:888
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:840
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1691
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1300
static ConstantTokenNone * get(LLVMContext &Context)
Return the ConstantTokenNone.
Definition: Constants.cpp:1415
This represents the llvm.coro.align instruction.
Definition: CoroInstr.h:603
This represents the llvm.coro.alloc instruction.
Definition: CoroInstr.h:70
This class represents the llvm.coro.begin instruction.
Definition: CoroInstr.h:420
AnyCoroIdInst * getId() const
Definition: CoroInstr.h:424
This represents the llvm.coro.id instruction.
Definition: CoroInstr.h:113
void setInfo(Constant *C)
Definition: CoroInstr.h:180
This represents the llvm.coro.size instruction.
Definition: CoroInstr.h:591
This represents the llvm.coro.suspend.async instruction.
Definition: CoroInstr.h:525
CoroAsyncResumeInst * getResumeFunction() const
Definition: CoroInstr.h:546
This represents the llvm.coro.suspend instruction.
Definition: CoroInstr.h:493
CoroSaveInst * getCoroSave() const
Definition: CoroInstr.h:497
DISubprogram * getSubprogram() const
Get the subprogram for this scope.
Subprogram description.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
This is the common base class for debug info intrinsics for variables.
A debug info location.
Definition: DebugLoc.h:33
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
iterator end()
Definition: DenseMap.h:84
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
This class represents a freeze function that returns random concrete value if an operand is either a ...
A proxy from a FunctionAnalysisManager to an SCC.
Type * getReturnType() const
Definition: DerivedTypes.h:124
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:138
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:176
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:239
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:320
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:652
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:290
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:458
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2625
This class captures the data input to the InlineFunction call, and records the auxiliary results prod...
Definition: Cloning.h:202
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:71
const BasicBlock * getParent() const
Definition: Instruction.h:90
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:83
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
A node in the call graph.
An SCC of the call graph.
A lazily constructed view of the call graph of a module.
void addSplitFunction(Function &OriginalFunction, Function &NewFunction)
Add a new function split/outlined from an existing function.
void addSplitRefRecursiveFunctions(Function &OriginalFunction, ArrayRef< Function * > NewFunctions)
Add new ref-recursive functions split/outlined from an existing function.
Node & get(Function &F)
Get a graph node for a given function, scanning it to populate the graph data as necessary.
SCC * lookupSCC(Node &N) const
Lookup a function's SCC in the graph.
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:499
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
FunctionListType::iterator iterator
The Function iterators.
Definition: Module.h:90
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.h:254
Diagnostic information for applied optimization remarks.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1743
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
PrettyStackTraceEntry - This class is used to represent a frame of the "pretty" stack trace that is d...
virtual void print(raw_ostream &OS) const =0
print - Emit information about this stack frame to OS.
Return a value (possibly void), from a function.
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false.
Definition: SmallPtrSet.h:380
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:366
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:390
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:451
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
void reserve(size_type N)
Definition: SmallVector.h:667
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Type * getTypeAtIndex(const Value *V) const
Given an index value into the type, return the type of the element.
Definition: Type.cpp:613
Analysis pass providing the TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
bool supportsTailCallFor(const CallBase *CB) const
If target supports tail call on CB.
bool supportsTailCalls() const
If the target supports tail calls.
Value handle that tracks a Value across RAUW.
Definition: ValueHandle.h:331
ValueTy * getValPtr() const
Definition: ValueHandle.h:335
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isArch64Bit() const
Test whether the architecture is 64-bit.
Definition: Triple.cpp:1465
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:255
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1724
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:535
iterator_range< user_iterator > users()
Definition: Value.h:421
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:688
iterator_range< use_iterator > uses()
Definition: Value.h:376
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:384
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:289
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ Switch
The "resume-switch" lowering, where there are separate resume and destroy functions that are shared b...
void salvageDebugInfo(SmallDenseMap< Argument *, AllocaInst *, 4 > &ArgToAllocaMap, DbgVariableIntrinsic *DVI, bool OptimizeFrame, bool IsEntryPoint)
Attempts to rewrite the location operand of debug intrinsics in terms of the coroutine frame pointer,...
Definition: CoroFrame.cpp:2804
void replaceCoroFree(CoroIdInst *CoroId, bool Elide)
Definition: Coroutines.cpp:130
CallInst * createMustTailCall(DebugLoc Loc, Function *MustTailCallFn, ArrayRef< Value * > Arguments, IRBuilder<> &)
Definition: CoroSplit.cpp:1736
DiagnosticInfoOptimizationBase::Argument NV
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI, Instruction *I)
Replace the instruction specified by BI with the instruction specified by I.
bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
Definition: Verifier.cpp:6589
void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
Definition: Utils.cpp:1394
LazyCallGraph::SCC & updateCGAndAnalysisManagerForFunctionPass(LazyCallGraph &G, LazyCallGraph::SCC &C, LazyCallGraph::Node &N, CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, FunctionAnalysisManager &FAM)
Helper to update the call graph after running a function pass.
LazyCallGraph::SCC & updateCGAndAnalysisManagerForCGSCCPass(LazyCallGraph &G, LazyCallGraph::SCC &C, LazyCallGraph::Node &N, CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, FunctionAnalysisManager &FAM)
Helper to update the call graph after running a CGSCC pass.
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:666
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition: Local.cpp:398
@ Async
"Asynchronous" unwind tables (instr precise)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
unsigned changeToUnreachable(Instruction *I, bool PreserveLCSSA=false, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Insert an unreachable instruction before the specified instruction, making it and the rest of the cod...
Definition: Local.cpp:2306
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DWARFExpression::Operation Op
InlineResult InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, bool MergeAttributes=false, AAResults *CalleeAAR=nullptr, bool InsertLifetime=true, Function *ForwardVarArgsTo=nullptr)
This function inlines the called function into the basic block of the caller.
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, CloneFunctionChangeType Changes, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=nullptr, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Clone OldFunc into NewFunc, transforming the old arguments into references to VMap values.
auto predecessors(const MachineBasicBlock *BB)
bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Remove all blocks that can not be reached from the function's entry.
Definition: Local.cpp:2670
bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...
Definition: CFG.cpp:231
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
Support structure for SCC passes to communicate updates the call graph back to the CGSCC pass manager...
SmallPriorityWorklist< LazyCallGraph::SCC *, 1 > & CWorklist
Worklist of the SCCs queued for processing.
const std::function< bool(Instruction &)> MaterializableCallback
Definition: CoroSplit.h:25
PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, LazyCallGraph &CG, CGSCCUpdateResult &UR)
Definition: CoroSplit.cpp:2158
CoroSplitPass(bool OptimizeFrame=false)
Definition: CoroSplit.cpp:2154
AsyncLoweringStorage AsyncLowering
Definition: CoroInternal.h:145
FunctionType * getResumeFunctionType() const
Definition: CoroInternal.h:184
IntegerType * getIndexType() const
Definition: CoroInternal.h:169
StructType * FrameTy
Definition: CoroInternal.h:101
CoroIdInst * getSwitchCoroId() const
Definition: CoroInternal.h:148
Instruction * getInsertPtAfterFramePtr() const
Definition: CoroInternal.h:242
SmallVector< CoroSizeInst *, 2 > CoroSizes
Definition: CoroInternal.h:79
SmallVector< AnyCoroSuspendInst *, 4 > CoroSuspends
Definition: CoroInternal.h:81
Value * emitAlloc(IRBuilder<> &Builder, Value *Size, CallGraph *CG) const
Allocate memory according to the rules of the active lowering.
Definition: Coroutines.cpp:454
SmallVector< CallInst *, 2 > SwiftErrorOps
Definition: CoroInternal.h:82
ConstantInt * getIndex(uint64_t Value) const
Definition: CoroInternal.h:174
bool OptimizeFrame
This would only be true if optimization are enabled.
Definition: CoroInternal.h:108
SwitchLoweringStorage SwitchLowering
Definition: CoroInternal.h:143
CoroBeginInst * CoroBegin
Definition: CoroInternal.h:77
ArrayRef< Type * > getRetconResultTypes() const
Definition: CoroInternal.h:200
void emitDealloc(IRBuilder<> &Builder, Value *Ptr, CallGraph *CG) const
Deallocate memory according to the rules of the active lowering.
Definition: Coroutines.cpp:477
RetconLoweringStorage RetconLowering
Definition: CoroInternal.h:144
SmallVector< CoroAlignInst *, 2 > CoroAligns
Definition: CoroInternal.h:80
SmallVector< AnyCoroEndInst *, 4 > CoroEnds
Definition: CoroInternal.h:78
unsigned getSwitchIndexField() const
Definition: CoroInternal.h:164