10b57cec5SDimitry Andric //===----- CGCoroutine.cpp - Emit LLVM Code for C++ coroutines ------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This contains code dealing with C++ code generation of coroutines.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "CGCleanup.h"
140b57cec5SDimitry Andric #include "CodeGenFunction.h"
150b57cec5SDimitry Andric #include "llvm/ADT/ScopeExit.h"
160b57cec5SDimitry Andric #include "clang/AST/StmtCXX.h"
170b57cec5SDimitry Andric #include "clang/AST/StmtVisitor.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric using namespace clang;
200b57cec5SDimitry Andric using namespace CodeGen;
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using llvm::Value;
230b57cec5SDimitry Andric using llvm::BasicBlock;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace {
260b57cec5SDimitry Andric enum class AwaitKind { Init, Normal, Yield, Final };
270b57cec5SDimitry Andric static constexpr llvm::StringLiteral AwaitKindStr[] = {"init", "await", "yield",
280b57cec5SDimitry Andric                                                        "final"};
290b57cec5SDimitry Andric }
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric struct clang::CodeGen::CGCoroData {
320b57cec5SDimitry Andric   // What is the current await expression kind and how many
330b57cec5SDimitry Andric   // await/yield expressions were encountered so far.
340b57cec5SDimitry Andric   // These are used to generate pretty labels for await expressions in LLVM IR.
350b57cec5SDimitry Andric   AwaitKind CurrentAwaitKind = AwaitKind::Init;
360b57cec5SDimitry Andric   unsigned AwaitNum = 0;
370b57cec5SDimitry Andric   unsigned YieldNum = 0;
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   // How many co_return statements are in the coroutine. Used to decide whether
400b57cec5SDimitry Andric   // we need to add co_return; equivalent at the end of the user authored body.
410b57cec5SDimitry Andric   unsigned CoreturnCount = 0;
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   // A branch to this block is emitted when coroutine needs to suspend.
440b57cec5SDimitry Andric   llvm::BasicBlock *SuspendBB = nullptr;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   // The promise type's 'unhandled_exception' handler, if it defines one.
470b57cec5SDimitry Andric   Stmt *ExceptionHandler = nullptr;
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   // A temporary i1 alloca that stores whether 'await_resume' threw an
500b57cec5SDimitry Andric   // exception. If it did, 'true' is stored in this variable, and the coroutine
510b57cec5SDimitry Andric   // body must be skipped. If the promise type does not define an exception
520b57cec5SDimitry Andric   // handler, this is null.
530b57cec5SDimitry Andric   llvm::Value *ResumeEHVar = nullptr;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   // Stores the jump destination just before the coroutine memory is freed.
560b57cec5SDimitry Andric   // This is the destination that every suspend point jumps to for the cleanup
570b57cec5SDimitry Andric   // branch.
580b57cec5SDimitry Andric   CodeGenFunction::JumpDest CleanupJD;
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   // Stores the jump destination just before the final suspend. The co_return
610b57cec5SDimitry Andric   // statements jumps to this point after calling return_xxx promise member.
620b57cec5SDimitry Andric   CodeGenFunction::JumpDest FinalJD;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   // Stores the llvm.coro.id emitted in the function so that we can supply it
650b57cec5SDimitry Andric   // as the first argument to coro.begin, coro.alloc and coro.free intrinsics.
660b57cec5SDimitry Andric   // Note: llvm.coro.id returns a token that cannot be directly expressed in a
670b57cec5SDimitry Andric   // builtin.
680b57cec5SDimitry Andric   llvm::CallInst *CoroId = nullptr;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   // Stores the llvm.coro.begin emitted in the function so that we can replace
710b57cec5SDimitry Andric   // all coro.frame intrinsics with direct SSA value of coro.begin that returns
720b57cec5SDimitry Andric   // the address of the coroutine frame of the current coroutine.
730b57cec5SDimitry Andric   llvm::CallInst *CoroBegin = nullptr;
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   // Stores the last emitted coro.free for the deallocate expressions, we use it
760b57cec5SDimitry Andric   // to wrap dealloc code with if(auto mem = coro.free) dealloc(mem).
770b57cec5SDimitry Andric   llvm::CallInst *LastCoroFree = nullptr;
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   // If coro.id came from the builtin, remember the expression to give better
800b57cec5SDimitry Andric   // diagnostic. If CoroIdExpr is nullptr, the coro.id was created by
810b57cec5SDimitry Andric   // EmitCoroutineBody.
820b57cec5SDimitry Andric   CallExpr const *CoroIdExpr = nullptr;
830b57cec5SDimitry Andric };
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric // Defining these here allows to keep CGCoroData private to this file.
CGCoroInfo()860b57cec5SDimitry Andric clang::CodeGen::CodeGenFunction::CGCoroInfo::CGCoroInfo() {}
~CGCoroInfo()870b57cec5SDimitry Andric CodeGenFunction::CGCoroInfo::~CGCoroInfo() {}
880b57cec5SDimitry Andric 
createCoroData(CodeGenFunction & CGF,CodeGenFunction::CGCoroInfo & CurCoro,llvm::CallInst * CoroId,CallExpr const * CoroIdExpr=nullptr)890b57cec5SDimitry Andric static void createCoroData(CodeGenFunction &CGF,
900b57cec5SDimitry Andric                            CodeGenFunction::CGCoroInfo &CurCoro,
910b57cec5SDimitry Andric                            llvm::CallInst *CoroId,
920b57cec5SDimitry Andric                            CallExpr const *CoroIdExpr = nullptr) {
930b57cec5SDimitry Andric   if (CurCoro.Data) {
940b57cec5SDimitry Andric     if (CurCoro.Data->CoroIdExpr)
950b57cec5SDimitry Andric       CGF.CGM.Error(CoroIdExpr->getBeginLoc(),
960b57cec5SDimitry Andric                     "only one __builtin_coro_id can be used in a function");
970b57cec5SDimitry Andric     else if (CoroIdExpr)
980b57cec5SDimitry Andric       CGF.CGM.Error(CoroIdExpr->getBeginLoc(),
990b57cec5SDimitry Andric                     "__builtin_coro_id shall not be used in a C++ coroutine");
1000b57cec5SDimitry Andric     else
1010b57cec5SDimitry Andric       llvm_unreachable("EmitCoroutineBodyStatement called twice?");
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric     return;
1040b57cec5SDimitry Andric   }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   CurCoro.Data = std::unique_ptr<CGCoroData>(new CGCoroData);
1070b57cec5SDimitry Andric   CurCoro.Data->CoroId = CoroId;
1080b57cec5SDimitry Andric   CurCoro.Data->CoroIdExpr = CoroIdExpr;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric // Synthesize a pretty name for a suspend point.
buildSuspendPrefixStr(CGCoroData & Coro,AwaitKind Kind)1120b57cec5SDimitry Andric static SmallString<32> buildSuspendPrefixStr(CGCoroData &Coro, AwaitKind Kind) {
1130b57cec5SDimitry Andric   unsigned No = 0;
1140b57cec5SDimitry Andric   switch (Kind) {
1150b57cec5SDimitry Andric   case AwaitKind::Init:
1160b57cec5SDimitry Andric   case AwaitKind::Final:
1170b57cec5SDimitry Andric     break;
1180b57cec5SDimitry Andric   case AwaitKind::Normal:
1190b57cec5SDimitry Andric     No = ++Coro.AwaitNum;
1200b57cec5SDimitry Andric     break;
1210b57cec5SDimitry Andric   case AwaitKind::Yield:
1220b57cec5SDimitry Andric     No = ++Coro.YieldNum;
1230b57cec5SDimitry Andric     break;
1240b57cec5SDimitry Andric   }
1250b57cec5SDimitry Andric   SmallString<32> Prefix(AwaitKindStr[static_cast<unsigned>(Kind)]);
1260b57cec5SDimitry Andric   if (No > 1) {
1270b57cec5SDimitry Andric     Twine(No).toVector(Prefix);
1280b57cec5SDimitry Andric   }
1290b57cec5SDimitry Andric   return Prefix;
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric 
1325f757f3fSDimitry Andric // Check if function can throw based on prototype noexcept, also works for
1335f757f3fSDimitry Andric // destructors which are implicitly noexcept but can be marked noexcept(false).
FunctionCanThrow(const FunctionDecl * D)1345f757f3fSDimitry Andric static bool FunctionCanThrow(const FunctionDecl *D) {
1355f757f3fSDimitry Andric   const auto *Proto = D->getType()->getAs<FunctionProtoType>();
1365f757f3fSDimitry Andric   if (!Proto) {
1375f757f3fSDimitry Andric     // Function proto is not found, we conservatively assume throwing.
1380b57cec5SDimitry Andric     return true;
1390b57cec5SDimitry Andric   }
1405f757f3fSDimitry Andric   return !isNoexceptExceptionSpec(Proto->getExceptionSpecType()) ||
1415f757f3fSDimitry Andric          Proto->canThrow() != CT_Cannot;
1425f757f3fSDimitry Andric }
1435f757f3fSDimitry Andric 
ResumeStmtCanThrow(const Stmt * S)1445f757f3fSDimitry Andric static bool ResumeStmtCanThrow(const Stmt *S) {
1455f757f3fSDimitry Andric   if (const auto *CE = dyn_cast<CallExpr>(S)) {
1465f757f3fSDimitry Andric     const auto *Callee = CE->getDirectCallee();
1475f757f3fSDimitry Andric     if (!Callee)
1485f757f3fSDimitry Andric       // We don't have direct callee. Conservatively assume throwing.
1495f757f3fSDimitry Andric       return true;
1505f757f3fSDimitry Andric 
1515f757f3fSDimitry Andric     if (FunctionCanThrow(Callee))
1525f757f3fSDimitry Andric       return true;
1535f757f3fSDimitry Andric 
1545f757f3fSDimitry Andric     // Fall through to visit the children.
1555f757f3fSDimitry Andric   }
1565f757f3fSDimitry Andric 
1575f757f3fSDimitry Andric   if (const auto *TE = dyn_cast<CXXBindTemporaryExpr>(S)) {
1585f757f3fSDimitry Andric     // Special handling of CXXBindTemporaryExpr here as calling of Dtor of the
1595f757f3fSDimitry Andric     // temporary is not part of `children()` as covered in the fall through.
1605f757f3fSDimitry Andric     // We need to mark entire statement as throwing if the destructor of the
1615f757f3fSDimitry Andric     // temporary throws.
1625f757f3fSDimitry Andric     const auto *Dtor = TE->getTemporary()->getDestructor();
1635f757f3fSDimitry Andric     if (FunctionCanThrow(Dtor))
1645f757f3fSDimitry Andric       return true;
1655f757f3fSDimitry Andric 
1665f757f3fSDimitry Andric     // Fall through to visit the children.
1675f757f3fSDimitry Andric   }
1685f757f3fSDimitry Andric 
1695f757f3fSDimitry Andric   for (const auto *child : S->children())
1705f757f3fSDimitry Andric     if (ResumeStmtCanThrow(child))
1715f757f3fSDimitry Andric       return true;
1725f757f3fSDimitry Andric 
1735f757f3fSDimitry Andric   return false;
1745f757f3fSDimitry Andric }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric // Emit suspend expression which roughly looks like:
1770b57cec5SDimitry Andric //
1780b57cec5SDimitry Andric //   auto && x = CommonExpr();
1790b57cec5SDimitry Andric //   if (!x.await_ready()) {
1800b57cec5SDimitry Andric //      llvm_coro_save();
1810b57cec5SDimitry Andric //      x.await_suspend(...);     (*)
1820b57cec5SDimitry Andric //      llvm_coro_suspend(); (**)
1830b57cec5SDimitry Andric //   }
1840b57cec5SDimitry Andric //   x.await_resume();
1850b57cec5SDimitry Andric //
1860b57cec5SDimitry Andric // where the result of the entire expression is the result of x.await_resume()
1870b57cec5SDimitry Andric //
1880b57cec5SDimitry Andric //   (*) If x.await_suspend return type is bool, it allows to veto a suspend:
1890b57cec5SDimitry Andric //      if (x.await_suspend(...))
1900b57cec5SDimitry Andric //        llvm_coro_suspend();
1910b57cec5SDimitry Andric //
1920b57cec5SDimitry Andric //  (**) llvm_coro_suspend() encodes three possible continuations as
1930b57cec5SDimitry Andric //       a switch instruction:
1940b57cec5SDimitry Andric //
1950b57cec5SDimitry Andric //  %where-to = call i8 @llvm.coro.suspend(...)
1960b57cec5SDimitry Andric //  switch i8 %where-to, label %coro.ret [ ; jump to epilogue to suspend
1970b57cec5SDimitry Andric //    i8 0, label %yield.ready   ; go here when resumed
1980b57cec5SDimitry Andric //    i8 1, label %yield.cleanup ; go here when destroyed
1990b57cec5SDimitry Andric //  ]
2000b57cec5SDimitry Andric //
2010b57cec5SDimitry Andric //  See llvm's docs/Coroutines.rst for more details.
2020b57cec5SDimitry Andric //
2030b57cec5SDimitry Andric namespace {
2040b57cec5SDimitry Andric   struct LValueOrRValue {
2050b57cec5SDimitry Andric     LValue LV;
2060b57cec5SDimitry Andric     RValue RV;
2070b57cec5SDimitry Andric   };
2080b57cec5SDimitry Andric }
emitSuspendExpression(CodeGenFunction & CGF,CGCoroData & Coro,CoroutineSuspendExpr const & S,AwaitKind Kind,AggValueSlot aggSlot,bool ignoreResult,bool forLValue)2090b57cec5SDimitry Andric static LValueOrRValue emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Coro,
2100b57cec5SDimitry Andric                                     CoroutineSuspendExpr const &S,
2110b57cec5SDimitry Andric                                     AwaitKind Kind, AggValueSlot aggSlot,
2120b57cec5SDimitry Andric                                     bool ignoreResult, bool forLValue) {
2130b57cec5SDimitry Andric   auto *E = S.getCommonExpr();
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   auto Binder =
2160b57cec5SDimitry Andric       CodeGenFunction::OpaqueValueMappingData::bind(CGF, S.getOpaqueValue(), E);
2170b57cec5SDimitry Andric   auto UnbindOnExit = llvm::make_scope_exit([&] { Binder.unbind(CGF); });
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric   auto Prefix = buildSuspendPrefixStr(Coro, Kind);
2200b57cec5SDimitry Andric   BasicBlock *ReadyBlock = CGF.createBasicBlock(Prefix + Twine(".ready"));
2210b57cec5SDimitry Andric   BasicBlock *SuspendBlock = CGF.createBasicBlock(Prefix + Twine(".suspend"));
2220b57cec5SDimitry Andric   BasicBlock *CleanupBlock = CGF.createBasicBlock(Prefix + Twine(".cleanup"));
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric   // If expression is ready, no need to suspend.
2250b57cec5SDimitry Andric   CGF.EmitBranchOnBoolExpr(S.getReadyExpr(), ReadyBlock, SuspendBlock, 0);
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric   // Otherwise, emit suspend logic.
2280b57cec5SDimitry Andric   CGF.EmitBlock(SuspendBlock);
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric   auto &Builder = CGF.Builder;
2310b57cec5SDimitry Andric   llvm::Function *CoroSave = CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_save);
2320b57cec5SDimitry Andric   auto *NullPtr = llvm::ConstantPointerNull::get(CGF.CGM.Int8PtrTy);
2330b57cec5SDimitry Andric   auto *SaveCall = Builder.CreateCall(CoroSave, {NullPtr});
2340b57cec5SDimitry Andric 
23506c3fb27SDimitry Andric   CGF.CurCoro.InSuspendBlock = true;
2360b57cec5SDimitry Andric   auto *SuspendRet = CGF.EmitScalarExpr(S.getSuspendExpr());
23706c3fb27SDimitry Andric   CGF.CurCoro.InSuspendBlock = false;
2385f757f3fSDimitry Andric 
2390b57cec5SDimitry Andric   if (SuspendRet != nullptr && SuspendRet->getType()->isIntegerTy(1)) {
2400b57cec5SDimitry Andric     // Veto suspension if requested by bool returning await_suspend.
2410b57cec5SDimitry Andric     BasicBlock *RealSuspendBlock =
2420b57cec5SDimitry Andric         CGF.createBasicBlock(Prefix + Twine(".suspend.bool"));
2430b57cec5SDimitry Andric     CGF.Builder.CreateCondBr(SuspendRet, RealSuspendBlock, ReadyBlock);
2440b57cec5SDimitry Andric     CGF.EmitBlock(RealSuspendBlock);
2450b57cec5SDimitry Andric   }
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   // Emit the suspend point.
2480b57cec5SDimitry Andric   const bool IsFinalSuspend = (Kind == AwaitKind::Final);
2490b57cec5SDimitry Andric   llvm::Function *CoroSuspend =
2500b57cec5SDimitry Andric       CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_suspend);
2510b57cec5SDimitry Andric   auto *SuspendResult = Builder.CreateCall(
2520b57cec5SDimitry Andric       CoroSuspend, {SaveCall, Builder.getInt1(IsFinalSuspend)});
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric   // Create a switch capturing three possible continuations.
2550b57cec5SDimitry Andric   auto *Switch = Builder.CreateSwitch(SuspendResult, Coro.SuspendBB, 2);
2560b57cec5SDimitry Andric   Switch->addCase(Builder.getInt8(0), ReadyBlock);
2570b57cec5SDimitry Andric   Switch->addCase(Builder.getInt8(1), CleanupBlock);
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric   // Emit cleanup for this suspend point.
2600b57cec5SDimitry Andric   CGF.EmitBlock(CleanupBlock);
2610b57cec5SDimitry Andric   CGF.EmitBranchThroughCleanup(Coro.CleanupJD);
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   // Emit await_resume expression.
2640b57cec5SDimitry Andric   CGF.EmitBlock(ReadyBlock);
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric   // Exception handling requires additional IR. If the 'await_resume' function
2670b57cec5SDimitry Andric   // is marked as 'noexcept', we avoid generating this additional IR.
2680b57cec5SDimitry Andric   CXXTryStmt *TryStmt = nullptr;
2690b57cec5SDimitry Andric   if (Coro.ExceptionHandler && Kind == AwaitKind::Init &&
2705f757f3fSDimitry Andric       ResumeStmtCanThrow(S.getResumeExpr())) {
2710b57cec5SDimitry Andric     Coro.ResumeEHVar =
2720b57cec5SDimitry Andric         CGF.CreateTempAlloca(Builder.getInt1Ty(), Prefix + Twine("resume.eh"));
2730b57cec5SDimitry Andric     Builder.CreateFlagStore(true, Coro.ResumeEHVar);
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric     auto Loc = S.getResumeExpr()->getExprLoc();
2760b57cec5SDimitry Andric     auto *Catch = new (CGF.getContext())
2770b57cec5SDimitry Andric         CXXCatchStmt(Loc, /*exDecl=*/nullptr, Coro.ExceptionHandler);
27881ad6265SDimitry Andric     auto *TryBody = CompoundStmt::Create(CGF.getContext(), S.getResumeExpr(),
27981ad6265SDimitry Andric                                          FPOptionsOverride(), Loc, Loc);
2800b57cec5SDimitry Andric     TryStmt = CXXTryStmt::Create(CGF.getContext(), Loc, TryBody, Catch);
2810b57cec5SDimitry Andric     CGF.EnterCXXTryStmt(*TryStmt);
2825f757f3fSDimitry Andric     CGF.EmitStmt(TryBody);
2835f757f3fSDimitry Andric     // We don't use EmitCXXTryStmt here. We need to store to ResumeEHVar that
2845f757f3fSDimitry Andric     // doesn't exist in the body.
2855f757f3fSDimitry Andric     Builder.CreateFlagStore(false, Coro.ResumeEHVar);
2865f757f3fSDimitry Andric     CGF.ExitCXXTryStmt(*TryStmt);
2875f757f3fSDimitry Andric     LValueOrRValue Res;
2885f757f3fSDimitry Andric     // We are not supposed to obtain the value from init suspend await_resume().
2895f757f3fSDimitry Andric     Res.RV = RValue::getIgnored();
2905f757f3fSDimitry Andric     return Res;
2910b57cec5SDimitry Andric   }
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric   LValueOrRValue Res;
2940b57cec5SDimitry Andric   if (forLValue)
2950b57cec5SDimitry Andric     Res.LV = CGF.EmitLValue(S.getResumeExpr());
2960b57cec5SDimitry Andric   else
2970b57cec5SDimitry Andric     Res.RV = CGF.EmitAnyExpr(S.getResumeExpr(), aggSlot, ignoreResult);
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   return Res;
3000b57cec5SDimitry Andric }
3010b57cec5SDimitry Andric 
EmitCoawaitExpr(const CoawaitExpr & E,AggValueSlot aggSlot,bool ignoreResult)3020b57cec5SDimitry Andric RValue CodeGenFunction::EmitCoawaitExpr(const CoawaitExpr &E,
3030b57cec5SDimitry Andric                                         AggValueSlot aggSlot,
3040b57cec5SDimitry Andric                                         bool ignoreResult) {
3050b57cec5SDimitry Andric   return emitSuspendExpression(*this, *CurCoro.Data, E,
3060b57cec5SDimitry Andric                                CurCoro.Data->CurrentAwaitKind, aggSlot,
3070b57cec5SDimitry Andric                                ignoreResult, /*forLValue*/false).RV;
3080b57cec5SDimitry Andric }
EmitCoyieldExpr(const CoyieldExpr & E,AggValueSlot aggSlot,bool ignoreResult)3090b57cec5SDimitry Andric RValue CodeGenFunction::EmitCoyieldExpr(const CoyieldExpr &E,
3100b57cec5SDimitry Andric                                         AggValueSlot aggSlot,
3110b57cec5SDimitry Andric                                         bool ignoreResult) {
3120b57cec5SDimitry Andric   return emitSuspendExpression(*this, *CurCoro.Data, E, AwaitKind::Yield,
3130b57cec5SDimitry Andric                                aggSlot, ignoreResult, /*forLValue*/false).RV;
3140b57cec5SDimitry Andric }
3150b57cec5SDimitry Andric 
EmitCoreturnStmt(CoreturnStmt const & S)3160b57cec5SDimitry Andric void CodeGenFunction::EmitCoreturnStmt(CoreturnStmt const &S) {
3170b57cec5SDimitry Andric   ++CurCoro.Data->CoreturnCount;
3180b57cec5SDimitry Andric   const Expr *RV = S.getOperand();
3195ffd83dbSDimitry Andric   if (RV && RV->getType()->isVoidType() && !isa<InitListExpr>(RV)) {
3205ffd83dbSDimitry Andric     // Make sure to evaluate the non initlist expression of a co_return
3215ffd83dbSDimitry Andric     // with a void expression for side effects.
3220b57cec5SDimitry Andric     RunCleanupsScope cleanupScope(*this);
3230b57cec5SDimitry Andric     EmitIgnoredExpr(RV);
3240b57cec5SDimitry Andric   }
3250b57cec5SDimitry Andric   EmitStmt(S.getPromiseCall());
3260b57cec5SDimitry Andric   EmitBranchThroughCleanup(CurCoro.Data->FinalJD);
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric #ifndef NDEBUG
getCoroutineSuspendExprReturnType(const ASTContext & Ctx,const CoroutineSuspendExpr * E)3310b57cec5SDimitry Andric static QualType getCoroutineSuspendExprReturnType(const ASTContext &Ctx,
3320b57cec5SDimitry Andric   const CoroutineSuspendExpr *E) {
3330b57cec5SDimitry Andric   const auto *RE = E->getResumeExpr();
3340b57cec5SDimitry Andric   // Is it possible for RE to be a CXXBindTemporaryExpr wrapping
3350b57cec5SDimitry Andric   // a MemberCallExpr?
3360b57cec5SDimitry Andric   assert(isa<CallExpr>(RE) && "unexpected suspend expression type");
3370b57cec5SDimitry Andric   return cast<CallExpr>(RE)->getCallReturnType(Ctx);
3380b57cec5SDimitry Andric }
3390b57cec5SDimitry Andric #endif
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric LValue
EmitCoawaitLValue(const CoawaitExpr * E)3420b57cec5SDimitry Andric CodeGenFunction::EmitCoawaitLValue(const CoawaitExpr *E) {
3430b57cec5SDimitry Andric   assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() &&
3440b57cec5SDimitry Andric          "Can't have a scalar return unless the return type is a "
3450b57cec5SDimitry Andric          "reference type!");
3460b57cec5SDimitry Andric   return emitSuspendExpression(*this, *CurCoro.Data, *E,
3470b57cec5SDimitry Andric                                CurCoro.Data->CurrentAwaitKind, AggValueSlot::ignored(),
3480b57cec5SDimitry Andric                                /*ignoreResult*/false, /*forLValue*/true).LV;
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric LValue
EmitCoyieldLValue(const CoyieldExpr * E)3520b57cec5SDimitry Andric CodeGenFunction::EmitCoyieldLValue(const CoyieldExpr *E) {
3530b57cec5SDimitry Andric   assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() &&
3540b57cec5SDimitry Andric          "Can't have a scalar return unless the return type is a "
3550b57cec5SDimitry Andric          "reference type!");
3560b57cec5SDimitry Andric   return emitSuspendExpression(*this, *CurCoro.Data, *E,
3570b57cec5SDimitry Andric                                AwaitKind::Yield, AggValueSlot::ignored(),
3580b57cec5SDimitry Andric                                /*ignoreResult*/false, /*forLValue*/true).LV;
3590b57cec5SDimitry Andric }
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric // Hunts for the parameter reference in the parameter copy/move declaration.
3620b57cec5SDimitry Andric namespace {
3630b57cec5SDimitry Andric struct GetParamRef : public StmtVisitor<GetParamRef> {
3640b57cec5SDimitry Andric public:
3650b57cec5SDimitry Andric   DeclRefExpr *Expr = nullptr;
GetParamRef__anon6d0e35e80411::GetParamRef3660b57cec5SDimitry Andric   GetParamRef() {}
VisitDeclRefExpr__anon6d0e35e80411::GetParamRef3670b57cec5SDimitry Andric   void VisitDeclRefExpr(DeclRefExpr *E) {
3680b57cec5SDimitry Andric     assert(Expr == nullptr && "multilple declref in param move");
3690b57cec5SDimitry Andric     Expr = E;
3700b57cec5SDimitry Andric   }
VisitStmt__anon6d0e35e80411::GetParamRef3710b57cec5SDimitry Andric   void VisitStmt(Stmt *S) {
3720b57cec5SDimitry Andric     for (auto *C : S->children()) {
3730b57cec5SDimitry Andric       if (C)
3740b57cec5SDimitry Andric         Visit(C);
3750b57cec5SDimitry Andric     }
3760b57cec5SDimitry Andric   }
3770b57cec5SDimitry Andric };
3780b57cec5SDimitry Andric }
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric // This class replaces references to parameters to their copies by changing
3810b57cec5SDimitry Andric // the addresses in CGF.LocalDeclMap and restoring back the original values in
3820b57cec5SDimitry Andric // its destructor.
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric namespace {
3850b57cec5SDimitry Andric   struct ParamReferenceReplacerRAII {
3860b57cec5SDimitry Andric     CodeGenFunction::DeclMapTy SavedLocals;
3870b57cec5SDimitry Andric     CodeGenFunction::DeclMapTy& LocalDeclMap;
3880b57cec5SDimitry Andric 
ParamReferenceReplacerRAII__anon6d0e35e80511::ParamReferenceReplacerRAII3890b57cec5SDimitry Andric     ParamReferenceReplacerRAII(CodeGenFunction::DeclMapTy &LocalDeclMap)
3900b57cec5SDimitry Andric         : LocalDeclMap(LocalDeclMap) {}
3910b57cec5SDimitry Andric 
addCopy__anon6d0e35e80511::ParamReferenceReplacerRAII3920b57cec5SDimitry Andric     void addCopy(DeclStmt const *PM) {
3930b57cec5SDimitry Andric       // Figure out what param it refers to.
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric       assert(PM->isSingleDecl());
3960b57cec5SDimitry Andric       VarDecl const*VD = static_cast<VarDecl const*>(PM->getSingleDecl());
3970b57cec5SDimitry Andric       Expr const *InitExpr = VD->getInit();
3980b57cec5SDimitry Andric       GetParamRef Visitor;
3990b57cec5SDimitry Andric       Visitor.Visit(const_cast<Expr*>(InitExpr));
4000b57cec5SDimitry Andric       assert(Visitor.Expr);
4010b57cec5SDimitry Andric       DeclRefExpr *DREOrig = Visitor.Expr;
4020b57cec5SDimitry Andric       auto *PD = DREOrig->getDecl();
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric       auto it = LocalDeclMap.find(PD);
4050b57cec5SDimitry Andric       assert(it != LocalDeclMap.end() && "parameter is not found");
4060b57cec5SDimitry Andric       SavedLocals.insert({ PD, it->second });
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric       auto copyIt = LocalDeclMap.find(VD);
4090b57cec5SDimitry Andric       assert(copyIt != LocalDeclMap.end() && "parameter copy is not found");
4100b57cec5SDimitry Andric       it->second = copyIt->getSecond();
4110b57cec5SDimitry Andric     }
4120b57cec5SDimitry Andric 
~ParamReferenceReplacerRAII__anon6d0e35e80511::ParamReferenceReplacerRAII4130b57cec5SDimitry Andric     ~ParamReferenceReplacerRAII() {
4140b57cec5SDimitry Andric       for (auto&& SavedLocal : SavedLocals) {
4150b57cec5SDimitry Andric         LocalDeclMap.insert({SavedLocal.first, SavedLocal.second});
4160b57cec5SDimitry Andric       }
4170b57cec5SDimitry Andric     }
4180b57cec5SDimitry Andric   };
4190b57cec5SDimitry Andric }
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric // For WinEH exception representation backend needs to know what funclet coro.end
4220b57cec5SDimitry Andric // belongs to. That information is passed in a funclet bundle.
4230b57cec5SDimitry Andric static SmallVector<llvm::OperandBundleDef, 1>
getBundlesForCoroEnd(CodeGenFunction & CGF)4240b57cec5SDimitry Andric getBundlesForCoroEnd(CodeGenFunction &CGF) {
4250b57cec5SDimitry Andric   SmallVector<llvm::OperandBundleDef, 1> BundleList;
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric   if (llvm::Instruction *EHPad = CGF.CurrentFuncletPad)
4280b57cec5SDimitry Andric     BundleList.emplace_back("funclet", EHPad);
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric   return BundleList;
4310b57cec5SDimitry Andric }
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric namespace {
4340b57cec5SDimitry Andric // We will insert coro.end to cut any of the destructors for objects that
4350b57cec5SDimitry Andric // do not need to be destroyed once the coroutine is resumed.
4360b57cec5SDimitry Andric // See llvm/docs/Coroutines.rst for more details about coro.end.
4370b57cec5SDimitry Andric struct CallCoroEnd final : public EHScopeStack::Cleanup {
Emit__anon6d0e35e80611::CallCoroEnd4380b57cec5SDimitry Andric   void Emit(CodeGenFunction &CGF, Flags flags) override {
4390b57cec5SDimitry Andric     auto &CGM = CGF.CGM;
4400b57cec5SDimitry Andric     auto *NullPtr = llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
4410b57cec5SDimitry Andric     llvm::Function *CoroEndFn = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
4420b57cec5SDimitry Andric     // See if we have a funclet bundle to associate coro.end with. (WinEH)
4430b57cec5SDimitry Andric     auto Bundles = getBundlesForCoroEnd(CGF);
4445f757f3fSDimitry Andric     auto *CoroEnd =
4455f757f3fSDimitry Andric       CGF.Builder.CreateCall(CoroEndFn,
4465f757f3fSDimitry Andric                              {NullPtr, CGF.Builder.getTrue(),
4475f757f3fSDimitry Andric                               llvm::ConstantTokenNone::get(CoroEndFn->getContext())},
4485f757f3fSDimitry Andric                              Bundles);
4490b57cec5SDimitry Andric     if (Bundles.empty()) {
4500b57cec5SDimitry Andric       // Otherwise, (landingpad model), create a conditional branch that leads
4510b57cec5SDimitry Andric       // either to a cleanup block or a block with EH resume instruction.
4520b57cec5SDimitry Andric       auto *ResumeBB = CGF.getEHResumeBlock(/*isCleanup=*/true);
4530b57cec5SDimitry Andric       auto *CleanupContBB = CGF.createBasicBlock("cleanup.cont");
4540b57cec5SDimitry Andric       CGF.Builder.CreateCondBr(CoroEnd, ResumeBB, CleanupContBB);
4550b57cec5SDimitry Andric       CGF.EmitBlock(CleanupContBB);
4560b57cec5SDimitry Andric     }
4570b57cec5SDimitry Andric   }
4580b57cec5SDimitry Andric };
4590b57cec5SDimitry Andric }
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric namespace {
4620b57cec5SDimitry Andric // Make sure to call coro.delete on scope exit.
4630b57cec5SDimitry Andric struct CallCoroDelete final : public EHScopeStack::Cleanup {
4640b57cec5SDimitry Andric   Stmt *Deallocate;
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric   // Emit "if (coro.free(CoroId, CoroBegin)) Deallocate;"
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric   // Note: That deallocation will be emitted twice: once for a normal exit and
4690b57cec5SDimitry Andric   // once for exceptional exit. This usage is safe because Deallocate does not
4700b57cec5SDimitry Andric   // contain any declarations. The SubStmtBuilder::makeNewAndDeleteExpr()
4710b57cec5SDimitry Andric   // builds a single call to a deallocation function which is safe to emit
4720b57cec5SDimitry Andric   // multiple times.
Emit__anon6d0e35e80711::CallCoroDelete4730b57cec5SDimitry Andric   void Emit(CodeGenFunction &CGF, Flags) override {
4740b57cec5SDimitry Andric     // Remember the current point, as we are going to emit deallocation code
4750b57cec5SDimitry Andric     // first to get to coro.free instruction that is an argument to a delete
4760b57cec5SDimitry Andric     // call.
4770b57cec5SDimitry Andric     BasicBlock *SaveInsertBlock = CGF.Builder.GetInsertBlock();
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric     auto *FreeBB = CGF.createBasicBlock("coro.free");
4800b57cec5SDimitry Andric     CGF.EmitBlock(FreeBB);
4810b57cec5SDimitry Andric     CGF.EmitStmt(Deallocate);
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric     auto *AfterFreeBB = CGF.createBasicBlock("after.coro.free");
4840b57cec5SDimitry Andric     CGF.EmitBlock(AfterFreeBB);
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric     // We should have captured coro.free from the emission of deallocate.
4870b57cec5SDimitry Andric     auto *CoroFree = CGF.CurCoro.Data->LastCoroFree;
4880b57cec5SDimitry Andric     if (!CoroFree) {
4890b57cec5SDimitry Andric       CGF.CGM.Error(Deallocate->getBeginLoc(),
4900b57cec5SDimitry Andric                     "Deallocation expressoin does not refer to coro.free");
4910b57cec5SDimitry Andric       return;
4920b57cec5SDimitry Andric     }
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric     // Get back to the block we were originally and move coro.free there.
4950b57cec5SDimitry Andric     auto *InsertPt = SaveInsertBlock->getTerminator();
4960b57cec5SDimitry Andric     CoroFree->moveBefore(InsertPt);
4970b57cec5SDimitry Andric     CGF.Builder.SetInsertPoint(InsertPt);
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric     // Add if (auto *mem = coro.free) Deallocate;
5000b57cec5SDimitry Andric     auto *NullPtr = llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
5010b57cec5SDimitry Andric     auto *Cond = CGF.Builder.CreateICmpNE(CoroFree, NullPtr);
5020b57cec5SDimitry Andric     CGF.Builder.CreateCondBr(Cond, FreeBB, AfterFreeBB);
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric     // No longer need old terminator.
5050b57cec5SDimitry Andric     InsertPt->eraseFromParent();
5060b57cec5SDimitry Andric     CGF.Builder.SetInsertPoint(AfterFreeBB);
5070b57cec5SDimitry Andric   }
CallCoroDelete__anon6d0e35e80711::CallCoroDelete5080b57cec5SDimitry Andric   explicit CallCoroDelete(Stmt *DeallocStmt) : Deallocate(DeallocStmt) {}
5090b57cec5SDimitry Andric };
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric 
51206c3fb27SDimitry Andric namespace {
51306c3fb27SDimitry Andric struct GetReturnObjectManager {
51406c3fb27SDimitry Andric   CodeGenFunction &CGF;
51506c3fb27SDimitry Andric   CGBuilderTy &Builder;
51606c3fb27SDimitry Andric   const CoroutineBodyStmt &S;
51706c3fb27SDimitry Andric   // When true, performs RVO for the return object.
51806c3fb27SDimitry Andric   bool DirectEmit = false;
51906c3fb27SDimitry Andric 
52006c3fb27SDimitry Andric   Address GroActiveFlag;
52106c3fb27SDimitry Andric   CodeGenFunction::AutoVarEmission GroEmission;
52206c3fb27SDimitry Andric 
GetReturnObjectManager__anon6d0e35e80811::GetReturnObjectManager52306c3fb27SDimitry Andric   GetReturnObjectManager(CodeGenFunction &CGF, const CoroutineBodyStmt &S)
52406c3fb27SDimitry Andric       : CGF(CGF), Builder(CGF.Builder), S(S), GroActiveFlag(Address::invalid()),
52506c3fb27SDimitry Andric         GroEmission(CodeGenFunction::AutoVarEmission::invalid()) {
52606c3fb27SDimitry Andric     // The call to get_­return_­object is sequenced before the call to
52706c3fb27SDimitry Andric     // initial_­suspend and is invoked at most once, but there are caveats
52806c3fb27SDimitry Andric     // regarding on whether the prvalue result object may be initialized
52906c3fb27SDimitry Andric     // directly/eager or delayed, depending on the types involved.
53006c3fb27SDimitry Andric     //
53106c3fb27SDimitry Andric     // More info at https://github.com/cplusplus/papers/issues/1414
53206c3fb27SDimitry Andric     //
53306c3fb27SDimitry Andric     // The general cases:
53406c3fb27SDimitry Andric     // 1. Same type of get_return_object and coroutine return type (direct
53506c3fb27SDimitry Andric     // emission):
53606c3fb27SDimitry Andric     //  - Constructed in the return slot.
53706c3fb27SDimitry Andric     // 2. Different types (delayed emission):
53806c3fb27SDimitry Andric     //  - Constructed temporary object prior to initial suspend initialized with
53906c3fb27SDimitry Andric     //  a call to get_return_object()
54006c3fb27SDimitry Andric     //  - When coroutine needs to to return to the caller and needs to construct
54106c3fb27SDimitry Andric     //  return value for the coroutine it is initialized with expiring value of
54206c3fb27SDimitry Andric     //  the temporary obtained above.
54306c3fb27SDimitry Andric     //
54406c3fb27SDimitry Andric     // Direct emission for void returning coroutines or GROs.
54506c3fb27SDimitry Andric     DirectEmit = [&]() {
54606c3fb27SDimitry Andric       auto *RVI = S.getReturnValueInit();
54706c3fb27SDimitry Andric       assert(RVI && "expected RVI");
54806c3fb27SDimitry Andric       auto GroType = RVI->getType();
54906c3fb27SDimitry Andric       return CGF.getContext().hasSameType(GroType, CGF.FnRetTy);
55006c3fb27SDimitry Andric     }();
55106c3fb27SDimitry Andric   }
55206c3fb27SDimitry Andric 
55306c3fb27SDimitry Andric   // The gro variable has to outlive coroutine frame and coroutine promise, but,
55406c3fb27SDimitry Andric   // it can only be initialized after coroutine promise was created, thus, we
55506c3fb27SDimitry Andric   // split its emission in two parts. EmitGroAlloca emits an alloca and sets up
55606c3fb27SDimitry Andric   // cleanups. Later when coroutine promise is available we initialize the gro
55706c3fb27SDimitry Andric   // and sets the flag that the cleanup is now active.
EmitGroAlloca__anon6d0e35e80811::GetReturnObjectManager55806c3fb27SDimitry Andric   void EmitGroAlloca() {
55906c3fb27SDimitry Andric     if (DirectEmit)
56006c3fb27SDimitry Andric       return;
56106c3fb27SDimitry Andric 
56206c3fb27SDimitry Andric     auto *GroDeclStmt = dyn_cast_or_null<DeclStmt>(S.getResultDecl());
56306c3fb27SDimitry Andric     if (!GroDeclStmt) {
56406c3fb27SDimitry Andric       // If get_return_object returns void, no need to do an alloca.
56506c3fb27SDimitry Andric       return;
56606c3fb27SDimitry Andric     }
56706c3fb27SDimitry Andric 
56806c3fb27SDimitry Andric     auto *GroVarDecl = cast<VarDecl>(GroDeclStmt->getSingleDecl());
56906c3fb27SDimitry Andric 
57006c3fb27SDimitry Andric     // Set GRO flag that it is not initialized yet
57106c3fb27SDimitry Andric     GroActiveFlag = CGF.CreateTempAlloca(Builder.getInt1Ty(), CharUnits::One(),
57206c3fb27SDimitry Andric                                          "gro.active");
57306c3fb27SDimitry Andric     Builder.CreateStore(Builder.getFalse(), GroActiveFlag);
57406c3fb27SDimitry Andric 
57506c3fb27SDimitry Andric     GroEmission = CGF.EmitAutoVarAlloca(*GroVarDecl);
5765f757f3fSDimitry Andric     auto *GroAlloca = dyn_cast_or_null<llvm::AllocaInst>(
5775f757f3fSDimitry Andric         GroEmission.getOriginalAllocatedAddress().getPointer());
5785f757f3fSDimitry Andric     assert(GroAlloca && "expected alloca to be emitted");
5795f757f3fSDimitry Andric     GroAlloca->setMetadata(llvm::LLVMContext::MD_coro_outside_frame,
5805f757f3fSDimitry Andric                            llvm::MDNode::get(CGF.CGM.getLLVMContext(), {}));
58106c3fb27SDimitry Andric 
58206c3fb27SDimitry Andric     // Remember the top of EHStack before emitting the cleanup.
58306c3fb27SDimitry Andric     auto old_top = CGF.EHStack.stable_begin();
58406c3fb27SDimitry Andric     CGF.EmitAutoVarCleanups(GroEmission);
58506c3fb27SDimitry Andric     auto top = CGF.EHStack.stable_begin();
58606c3fb27SDimitry Andric 
58706c3fb27SDimitry Andric     // Make the cleanup conditional on gro.active
58806c3fb27SDimitry Andric     for (auto b = CGF.EHStack.find(top), e = CGF.EHStack.find(old_top); b != e;
58906c3fb27SDimitry Andric          b++) {
59006c3fb27SDimitry Andric       if (auto *Cleanup = dyn_cast<EHCleanupScope>(&*b)) {
59106c3fb27SDimitry Andric         assert(!Cleanup->hasActiveFlag() && "cleanup already has active flag?");
59206c3fb27SDimitry Andric         Cleanup->setActiveFlag(GroActiveFlag);
59306c3fb27SDimitry Andric         Cleanup->setTestFlagInEHCleanup();
59406c3fb27SDimitry Andric         Cleanup->setTestFlagInNormalCleanup();
59506c3fb27SDimitry Andric       }
59606c3fb27SDimitry Andric     }
59706c3fb27SDimitry Andric   }
59806c3fb27SDimitry Andric 
EmitGroInit__anon6d0e35e80811::GetReturnObjectManager59906c3fb27SDimitry Andric   void EmitGroInit() {
60006c3fb27SDimitry Andric     if (DirectEmit) {
60106c3fb27SDimitry Andric       // ReturnValue should be valid as long as the coroutine's return type
60206c3fb27SDimitry Andric       // is not void. The assertion could help us to reduce the check later.
60306c3fb27SDimitry Andric       assert(CGF.ReturnValue.isValid() == (bool)S.getReturnStmt());
60406c3fb27SDimitry Andric       // Now we have the promise, initialize the GRO.
60506c3fb27SDimitry Andric       // We need to emit `get_return_object` first. According to:
60606c3fb27SDimitry Andric       // [dcl.fct.def.coroutine]p7
60706c3fb27SDimitry Andric       // The call to get_return_­object is sequenced before the call to
60806c3fb27SDimitry Andric       // initial_suspend and is invoked at most once.
60906c3fb27SDimitry Andric       //
61006c3fb27SDimitry Andric       // So we couldn't emit return value when we emit return statment,
61106c3fb27SDimitry Andric       // otherwise the call to get_return_object wouldn't be in front
61206c3fb27SDimitry Andric       // of initial_suspend.
61306c3fb27SDimitry Andric       if (CGF.ReturnValue.isValid()) {
61406c3fb27SDimitry Andric         CGF.EmitAnyExprToMem(S.getReturnValue(), CGF.ReturnValue,
61506c3fb27SDimitry Andric                              S.getReturnValue()->getType().getQualifiers(),
61606c3fb27SDimitry Andric                              /*IsInit*/ true);
61706c3fb27SDimitry Andric       }
61806c3fb27SDimitry Andric       return;
61906c3fb27SDimitry Andric     }
62006c3fb27SDimitry Andric 
62106c3fb27SDimitry Andric     if (!GroActiveFlag.isValid()) {
62206c3fb27SDimitry Andric       // No Gro variable was allocated. Simply emit the call to
62306c3fb27SDimitry Andric       // get_return_object.
62406c3fb27SDimitry Andric       CGF.EmitStmt(S.getResultDecl());
62506c3fb27SDimitry Andric       return;
62606c3fb27SDimitry Andric     }
62706c3fb27SDimitry Andric 
62806c3fb27SDimitry Andric     CGF.EmitAutoVarInit(GroEmission);
62906c3fb27SDimitry Andric     Builder.CreateStore(Builder.getTrue(), GroActiveFlag);
63006c3fb27SDimitry Andric   }
63106c3fb27SDimitry Andric };
63206c3fb27SDimitry Andric } // namespace
63306c3fb27SDimitry Andric 
emitBodyAndFallthrough(CodeGenFunction & CGF,const CoroutineBodyStmt & S,Stmt * Body)6340b57cec5SDimitry Andric static void emitBodyAndFallthrough(CodeGenFunction &CGF,
6350b57cec5SDimitry Andric                                    const CoroutineBodyStmt &S, Stmt *Body) {
6360b57cec5SDimitry Andric   CGF.EmitStmt(Body);
6370b57cec5SDimitry Andric   const bool CanFallthrough = CGF.Builder.GetInsertBlock();
6380b57cec5SDimitry Andric   if (CanFallthrough)
6390b57cec5SDimitry Andric     if (Stmt *OnFallthrough = S.getFallthroughHandler())
6400b57cec5SDimitry Andric       CGF.EmitStmt(OnFallthrough);
6410b57cec5SDimitry Andric }
6420b57cec5SDimitry Andric 
EmitCoroutineBody(const CoroutineBodyStmt & S)6430b57cec5SDimitry Andric void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) {
6445f757f3fSDimitry Andric   auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getPtrTy());
6450b57cec5SDimitry Andric   auto &TI = CGM.getContext().getTargetInfo();
6460b57cec5SDimitry Andric   unsigned NewAlign = TI.getNewAlign() / TI.getCharWidth();
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric   auto *EntryBB = Builder.GetInsertBlock();
6490b57cec5SDimitry Andric   auto *AllocBB = createBasicBlock("coro.alloc");
6500b57cec5SDimitry Andric   auto *InitBB = createBasicBlock("coro.init");
6510b57cec5SDimitry Andric   auto *FinalBB = createBasicBlock("coro.final");
6520b57cec5SDimitry Andric   auto *RetBB = createBasicBlock("coro.ret");
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric   auto *CoroId = Builder.CreateCall(
6550b57cec5SDimitry Andric       CGM.getIntrinsic(llvm::Intrinsic::coro_id),
6560b57cec5SDimitry Andric       {Builder.getInt32(NewAlign), NullPtr, NullPtr, NullPtr});
6570b57cec5SDimitry Andric   createCoroData(*this, CurCoro, CoroId);
6580b57cec5SDimitry Andric   CurCoro.Data->SuspendBB = RetBB;
659fe6060f1SDimitry Andric   assert(ShouldEmitLifetimeMarkers &&
660fe6060f1SDimitry Andric          "Must emit lifetime intrinsics for coroutines");
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric   // Backend is allowed to elide memory allocations, to help it, emit
6630b57cec5SDimitry Andric   // auto mem = coro.alloc() ? 0 : ... allocation code ...;
6640b57cec5SDimitry Andric   auto *CoroAlloc = Builder.CreateCall(
6650b57cec5SDimitry Andric       CGM.getIntrinsic(llvm::Intrinsic::coro_alloc), {CoroId});
6660b57cec5SDimitry Andric 
6670b57cec5SDimitry Andric   Builder.CreateCondBr(CoroAlloc, AllocBB, InitBB);
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric   EmitBlock(AllocBB);
6700b57cec5SDimitry Andric   auto *AllocateCall = EmitScalarExpr(S.getAllocate());
6710b57cec5SDimitry Andric   auto *AllocOrInvokeContBB = Builder.GetInsertBlock();
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric   // Handle allocation failure if 'ReturnStmtOnAllocFailure' was provided.
6740b57cec5SDimitry Andric   if (auto *RetOnAllocFailure = S.getReturnStmtOnAllocFailure()) {
6750b57cec5SDimitry Andric     auto *RetOnFailureBB = createBasicBlock("coro.ret.on.failure");
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric     // See if allocation was successful.
6780b57cec5SDimitry Andric     auto *NullPtr = llvm::ConstantPointerNull::get(Int8PtrTy);
6790b57cec5SDimitry Andric     auto *Cond = Builder.CreateICmpNE(AllocateCall, NullPtr);
68006c3fb27SDimitry Andric     // Expect the allocation to be successful.
68106c3fb27SDimitry Andric     emitCondLikelihoodViaExpectIntrinsic(Cond, Stmt::LH_Likely);
6820b57cec5SDimitry Andric     Builder.CreateCondBr(Cond, InitBB, RetOnFailureBB);
6830b57cec5SDimitry Andric 
6840b57cec5SDimitry Andric     // If not, return OnAllocFailure object.
6850b57cec5SDimitry Andric     EmitBlock(RetOnFailureBB);
6860b57cec5SDimitry Andric     EmitStmt(RetOnAllocFailure);
6870b57cec5SDimitry Andric   }
6880b57cec5SDimitry Andric   else {
6890b57cec5SDimitry Andric     Builder.CreateBr(InitBB);
6900b57cec5SDimitry Andric   }
6910b57cec5SDimitry Andric 
6920b57cec5SDimitry Andric   EmitBlock(InitBB);
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric   // Pass the result of the allocation to coro.begin.
6950b57cec5SDimitry Andric   auto *Phi = Builder.CreatePHI(VoidPtrTy, 2);
6960b57cec5SDimitry Andric   Phi->addIncoming(NullPtr, EntryBB);
6970b57cec5SDimitry Andric   Phi->addIncoming(AllocateCall, AllocOrInvokeContBB);
6980b57cec5SDimitry Andric   auto *CoroBegin = Builder.CreateCall(
6990b57cec5SDimitry Andric       CGM.getIntrinsic(llvm::Intrinsic::coro_begin), {CoroId, Phi});
7000b57cec5SDimitry Andric   CurCoro.Data->CoroBegin = CoroBegin;
7010b57cec5SDimitry Andric 
70206c3fb27SDimitry Andric   GetReturnObjectManager GroManager(*this, S);
70306c3fb27SDimitry Andric   GroManager.EmitGroAlloca();
70406c3fb27SDimitry Andric 
7050b57cec5SDimitry Andric   CurCoro.Data->CleanupJD = getJumpDestInCurrentScope(RetBB);
7060b57cec5SDimitry Andric   {
707fe6060f1SDimitry Andric     CGDebugInfo *DI = getDebugInfo();
7080b57cec5SDimitry Andric     ParamReferenceReplacerRAII ParamReplacer(LocalDeclMap);
7090b57cec5SDimitry Andric     CodeGenFunction::RunCleanupsScope ResumeScope(*this);
7100b57cec5SDimitry Andric     EHStack.pushCleanup<CallCoroDelete>(NormalAndEHCleanup, S.getDeallocate());
7110b57cec5SDimitry Andric 
712fe6060f1SDimitry Andric     // Create mapping between parameters and copy-params for coroutine function.
713bdd1243dSDimitry Andric     llvm::ArrayRef<const Stmt *> ParamMoves = S.getParamMoves();
714fe6060f1SDimitry Andric     assert(
715fe6060f1SDimitry Andric         (ParamMoves.size() == 0 || (ParamMoves.size() == FnArgs.size())) &&
716fe6060f1SDimitry Andric         "ParamMoves and FnArgs should be the same size for coroutine function");
717fe6060f1SDimitry Andric     if (ParamMoves.size() == FnArgs.size() && DI)
718fe6060f1SDimitry Andric       for (const auto Pair : llvm::zip(FnArgs, ParamMoves))
719fe6060f1SDimitry Andric         DI->getCoroutineParameterMappings().insert(
720fe6060f1SDimitry Andric             {std::get<0>(Pair), std::get<1>(Pair)});
721fe6060f1SDimitry Andric 
7220b57cec5SDimitry Andric     // Create parameter copies. We do it before creating a promise, since an
7230b57cec5SDimitry Andric     // evolution of coroutine TS may allow promise constructor to observe
7240b57cec5SDimitry Andric     // parameter copies.
7250b57cec5SDimitry Andric     for (auto *PM : S.getParamMoves()) {
7260b57cec5SDimitry Andric       EmitStmt(PM);
7270b57cec5SDimitry Andric       ParamReplacer.addCopy(cast<DeclStmt>(PM));
7280b57cec5SDimitry Andric       // TODO: if(CoroParam(...)) need to surround ctor and dtor
7290b57cec5SDimitry Andric       // for the copy, so that llvm can elide it if the copy is
7300b57cec5SDimitry Andric       // not needed.
7310b57cec5SDimitry Andric     }
7320b57cec5SDimitry Andric 
7330b57cec5SDimitry Andric     EmitStmt(S.getPromiseDeclStmt());
7340b57cec5SDimitry Andric 
7350b57cec5SDimitry Andric     Address PromiseAddr = GetAddrOfLocalVar(S.getPromiseDecl());
7360b57cec5SDimitry Andric     auto *PromiseAddrVoidPtr =
7370b57cec5SDimitry Andric         new llvm::BitCastInst(PromiseAddr.getPointer(), VoidPtrTy, "", CoroId);
7380b57cec5SDimitry Andric     // Update CoroId to refer to the promise. We could not do it earlier because
7390b57cec5SDimitry Andric     // promise local variable was not emitted yet.
7400b57cec5SDimitry Andric     CoroId->setArgOperand(1, PromiseAddrVoidPtr);
7410b57cec5SDimitry Andric 
74206c3fb27SDimitry Andric     // Now we have the promise, initialize the GRO
74306c3fb27SDimitry Andric     GroManager.EmitGroInit();
7440b57cec5SDimitry Andric 
7450b57cec5SDimitry Andric     EHStack.pushCleanup<CallCoroEnd>(EHCleanup);
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric     CurCoro.Data->CurrentAwaitKind = AwaitKind::Init;
7480b57cec5SDimitry Andric     CurCoro.Data->ExceptionHandler = S.getExceptionHandler();
7490b57cec5SDimitry Andric     EmitStmt(S.getInitSuspendStmt());
7500b57cec5SDimitry Andric     CurCoro.Data->FinalJD = getJumpDestInCurrentScope(FinalBB);
7510b57cec5SDimitry Andric 
7520b57cec5SDimitry Andric     CurCoro.Data->CurrentAwaitKind = AwaitKind::Normal;
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric     if (CurCoro.Data->ExceptionHandler) {
7550b57cec5SDimitry Andric       // If we generated IR to record whether an exception was thrown from
7560b57cec5SDimitry Andric       // 'await_resume', then use that IR to determine whether the coroutine
7570b57cec5SDimitry Andric       // body should be skipped.
7580b57cec5SDimitry Andric       // If we didn't generate the IR (perhaps because 'await_resume' was marked
7590b57cec5SDimitry Andric       // as 'noexcept'), then we skip this check.
7600b57cec5SDimitry Andric       BasicBlock *ContBB = nullptr;
7610b57cec5SDimitry Andric       if (CurCoro.Data->ResumeEHVar) {
7620b57cec5SDimitry Andric         BasicBlock *BodyBB = createBasicBlock("coro.resumed.body");
7630b57cec5SDimitry Andric         ContBB = createBasicBlock("coro.resumed.cont");
7640b57cec5SDimitry Andric         Value *SkipBody = Builder.CreateFlagLoad(CurCoro.Data->ResumeEHVar,
7650b57cec5SDimitry Andric                                                  "coro.resumed.eh");
7660b57cec5SDimitry Andric         Builder.CreateCondBr(SkipBody, ContBB, BodyBB);
7670b57cec5SDimitry Andric         EmitBlock(BodyBB);
7680b57cec5SDimitry Andric       }
7690b57cec5SDimitry Andric 
7700b57cec5SDimitry Andric       auto Loc = S.getBeginLoc();
7710b57cec5SDimitry Andric       CXXCatchStmt Catch(Loc, /*exDecl=*/nullptr,
7720b57cec5SDimitry Andric                          CurCoro.Data->ExceptionHandler);
7730b57cec5SDimitry Andric       auto *TryStmt =
7740b57cec5SDimitry Andric           CXXTryStmt::Create(getContext(), Loc, S.getBody(), &Catch);
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric       EnterCXXTryStmt(*TryStmt);
7770b57cec5SDimitry Andric       emitBodyAndFallthrough(*this, S, TryStmt->getTryBlock());
7780b57cec5SDimitry Andric       ExitCXXTryStmt(*TryStmt);
7790b57cec5SDimitry Andric 
7800b57cec5SDimitry Andric       if (ContBB)
7810b57cec5SDimitry Andric         EmitBlock(ContBB);
7820b57cec5SDimitry Andric     }
7830b57cec5SDimitry Andric     else {
7840b57cec5SDimitry Andric       emitBodyAndFallthrough(*this, S, S.getBody());
7850b57cec5SDimitry Andric     }
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric     // See if we need to generate final suspend.
7880b57cec5SDimitry Andric     const bool CanFallthrough = Builder.GetInsertBlock();
7890b57cec5SDimitry Andric     const bool HasCoreturns = CurCoro.Data->CoreturnCount > 0;
7900b57cec5SDimitry Andric     if (CanFallthrough || HasCoreturns) {
7910b57cec5SDimitry Andric       EmitBlock(FinalBB);
7920b57cec5SDimitry Andric       CurCoro.Data->CurrentAwaitKind = AwaitKind::Final;
7930b57cec5SDimitry Andric       EmitStmt(S.getFinalSuspendStmt());
7940b57cec5SDimitry Andric     } else {
7950b57cec5SDimitry Andric       // We don't need FinalBB. Emit it to make sure the block is deleted.
7960b57cec5SDimitry Andric       EmitBlock(FinalBB, /*IsFinished=*/true);
7970b57cec5SDimitry Andric     }
7980b57cec5SDimitry Andric   }
7990b57cec5SDimitry Andric 
8000b57cec5SDimitry Andric   EmitBlock(RetBB);
8010b57cec5SDimitry Andric   // Emit coro.end before getReturnStmt (and parameter destructors), since
8020b57cec5SDimitry Andric   // resume and destroy parts of the coroutine should not include them.
8030b57cec5SDimitry Andric   llvm::Function *CoroEnd = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
8045f757f3fSDimitry Andric   Builder.CreateCall(CoroEnd,
8055f757f3fSDimitry Andric                      {NullPtr, Builder.getFalse(),
8065f757f3fSDimitry Andric                       llvm::ConstantTokenNone::get(CoroEnd->getContext())});
8070b57cec5SDimitry Andric 
80881ad6265SDimitry Andric   if (Stmt *Ret = S.getReturnStmt()) {
80981ad6265SDimitry Andric     // Since we already emitted the return value above, so we shouldn't
81081ad6265SDimitry Andric     // emit it again here.
81106c3fb27SDimitry Andric     if (GroManager.DirectEmit)
81281ad6265SDimitry Andric       cast<ReturnStmt>(Ret)->setRetValue(nullptr);
8130b57cec5SDimitry Andric     EmitStmt(Ret);
81481ad6265SDimitry Andric   }
81504eeddc0SDimitry Andric 
81681ad6265SDimitry Andric   // LLVM require the frontend to mark the coroutine.
81781ad6265SDimitry Andric   CurFn->setPresplitCoroutine();
8185f757f3fSDimitry Andric 
8195f757f3fSDimitry Andric   if (CXXRecordDecl *RD = FnRetTy->getAsCXXRecordDecl();
8205f757f3fSDimitry Andric       RD && RD->hasAttr<CoroOnlyDestroyWhenCompleteAttr>())
8215f757f3fSDimitry Andric     CurFn->setCoroDestroyOnlyWhenComplete();
8220b57cec5SDimitry Andric }
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric // Emit coroutine intrinsic and patch up arguments of the token type.
EmitCoroutineIntrinsic(const CallExpr * E,unsigned int IID)8250b57cec5SDimitry Andric RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E,
8260b57cec5SDimitry Andric                                                unsigned int IID) {
8270b57cec5SDimitry Andric   SmallVector<llvm::Value *, 8> Args;
8280b57cec5SDimitry Andric   switch (IID) {
8290b57cec5SDimitry Andric   default:
8300b57cec5SDimitry Andric     break;
8310b57cec5SDimitry Andric   // The coro.frame builtin is replaced with an SSA value of the coro.begin
8320b57cec5SDimitry Andric   // intrinsic.
8330b57cec5SDimitry Andric   case llvm::Intrinsic::coro_frame: {
8340b57cec5SDimitry Andric     if (CurCoro.Data && CurCoro.Data->CoroBegin) {
8350b57cec5SDimitry Andric       return RValue::get(CurCoro.Data->CoroBegin);
8360b57cec5SDimitry Andric     }
8370b57cec5SDimitry Andric     CGM.Error(E->getBeginLoc(), "this builtin expect that __builtin_coro_begin "
8380b57cec5SDimitry Andric                                 "has been used earlier in this function");
8395f757f3fSDimitry Andric     auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getPtrTy());
8400b57cec5SDimitry Andric     return RValue::get(NullPtr);
8410b57cec5SDimitry Andric   }
842bdd1243dSDimitry Andric   case llvm::Intrinsic::coro_size: {
843bdd1243dSDimitry Andric     auto &Context = getContext();
844bdd1243dSDimitry Andric     CanQualType SizeTy = Context.getSizeType();
845bdd1243dSDimitry Andric     llvm::IntegerType *T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
846bdd1243dSDimitry Andric     llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::coro_size, T);
847bdd1243dSDimitry Andric     return RValue::get(Builder.CreateCall(F));
848bdd1243dSDimitry Andric   }
849bdd1243dSDimitry Andric   case llvm::Intrinsic::coro_align: {
850bdd1243dSDimitry Andric     auto &Context = getContext();
851bdd1243dSDimitry Andric     CanQualType SizeTy = Context.getSizeType();
852bdd1243dSDimitry Andric     llvm::IntegerType *T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
853bdd1243dSDimitry Andric     llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::coro_align, T);
854bdd1243dSDimitry Andric     return RValue::get(Builder.CreateCall(F));
855bdd1243dSDimitry Andric   }
8560b57cec5SDimitry Andric   // The following three intrinsics take a token parameter referring to a token
8570b57cec5SDimitry Andric   // returned by earlier call to @llvm.coro.id. Since we cannot represent it in
8580b57cec5SDimitry Andric   // builtins, we patch it up here.
8590b57cec5SDimitry Andric   case llvm::Intrinsic::coro_alloc:
8600b57cec5SDimitry Andric   case llvm::Intrinsic::coro_begin:
8610b57cec5SDimitry Andric   case llvm::Intrinsic::coro_free: {
8620b57cec5SDimitry Andric     if (CurCoro.Data && CurCoro.Data->CoroId) {
8630b57cec5SDimitry Andric       Args.push_back(CurCoro.Data->CoroId);
8640b57cec5SDimitry Andric       break;
8650b57cec5SDimitry Andric     }
8660b57cec5SDimitry Andric     CGM.Error(E->getBeginLoc(), "this builtin expect that __builtin_coro_id has"
8670b57cec5SDimitry Andric                                 " been used earlier in this function");
8680b57cec5SDimitry Andric     // Fallthrough to the next case to add TokenNone as the first argument.
869bdd1243dSDimitry Andric     [[fallthrough]];
8700b57cec5SDimitry Andric   }
8710b57cec5SDimitry Andric   // @llvm.coro.suspend takes a token parameter. Add token 'none' as the first
8720b57cec5SDimitry Andric   // argument.
8730b57cec5SDimitry Andric   case llvm::Intrinsic::coro_suspend:
8740b57cec5SDimitry Andric     Args.push_back(llvm::ConstantTokenNone::get(getLLVMContext()));
8750b57cec5SDimitry Andric     break;
8760b57cec5SDimitry Andric   }
8770b57cec5SDimitry Andric   for (const Expr *Arg : E->arguments())
8780b57cec5SDimitry Andric     Args.push_back(EmitScalarExpr(Arg));
8795f757f3fSDimitry Andric   // @llvm.coro.end takes a token parameter. Add token 'none' as the last
8805f757f3fSDimitry Andric   // argument.
8815f757f3fSDimitry Andric   if (IID == llvm::Intrinsic::coro_end)
8825f757f3fSDimitry Andric     Args.push_back(llvm::ConstantTokenNone::get(getLLVMContext()));
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric   llvm::Function *F = CGM.getIntrinsic(IID);
8850b57cec5SDimitry Andric   llvm::CallInst *Call = Builder.CreateCall(F, Args);
8860b57cec5SDimitry Andric 
8870b57cec5SDimitry Andric   // Note: The following code is to enable to emit coro.id and coro.begin by
8880b57cec5SDimitry Andric   // hand to experiment with coroutines in C.
8890b57cec5SDimitry Andric   // If we see @llvm.coro.id remember it in the CoroData. We will update
8900b57cec5SDimitry Andric   // coro.alloc, coro.begin and coro.free intrinsics to refer to it.
8910b57cec5SDimitry Andric   if (IID == llvm::Intrinsic::coro_id) {
8920b57cec5SDimitry Andric     createCoroData(*this, CurCoro, Call, E);
8930b57cec5SDimitry Andric   }
8940b57cec5SDimitry Andric   else if (IID == llvm::Intrinsic::coro_begin) {
8950b57cec5SDimitry Andric     if (CurCoro.Data)
8960b57cec5SDimitry Andric       CurCoro.Data->CoroBegin = Call;
8970b57cec5SDimitry Andric   }
8980b57cec5SDimitry Andric   else if (IID == llvm::Intrinsic::coro_free) {
8990b57cec5SDimitry Andric     // Remember the last coro_free as we need it to build the conditional
9000b57cec5SDimitry Andric     // deletion of the coroutine frame.
9010b57cec5SDimitry Andric     if (CurCoro.Data)
9020b57cec5SDimitry Andric       CurCoro.Data->LastCoroFree = Call;
9030b57cec5SDimitry Andric   }
9040b57cec5SDimitry Andric   return RValue::get(Call);
9050b57cec5SDimitry Andric }
906