#ifndef LLVM_LIB_TRANSFORMS_COROUTINES_COROINSTR_H
#define LLVM_LIB_TRANSFORMS_COROUTINES_COROINSTR_H
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/Support/raw_ostream.h"
namespace llvm {
class LLVM_LIBRARY_VISIBILITY CoroSubFnInst : public IntrinsicInst {
enum { FrameArg, IndexArg };
public:
enum ResumeKind {
RestartTrigger = -1,
ResumeIndex,
DestroyIndex,
CleanupIndex,
IndexLast,
IndexFirst = RestartTrigger
};
Value *getFrame() const { return getArgOperand(FrameArg); }
ResumeKind getIndex() const {
int64_t Index = getRawIndex()->getValue().getSExtValue();
assert(Index >= IndexFirst && Index < IndexLast &&
"unexpected CoroSubFnInst index argument");
return static_cast<ResumeKind>(Index);
}
ConstantInt *getRawIndex() const {
return cast<ConstantInt>(getArgOperand(IndexArg));
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_subfn_addr;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroAllocInst : public IntrinsicInst {
public:
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_alloc;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY AnyCoroIdInst : public IntrinsicInst {
public:
CoroAllocInst *getCoroAlloc() {
for (User *U : users())
if (auto *CA = dyn_cast<CoroAllocInst>(U))
return CA;
return nullptr;
}
IntrinsicInst *getCoroBegin() {
for (User *U : users())
if (auto *II = dyn_cast<IntrinsicInst>(U))
if (II->getIntrinsicID() == Intrinsic::coro_begin)
return II;
llvm_unreachable("no coro.begin associated with coro.id");
}
static bool classof(const IntrinsicInst *I) {
auto ID = I->getIntrinsicID();
return ID == Intrinsic::coro_id || ID == Intrinsic::coro_id_retcon ||
ID == Intrinsic::coro_id_retcon_once ||
ID == Intrinsic::coro_id_async;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroIdInst : public AnyCoroIdInst {
enum { AlignArg, PromiseArg, CoroutineArg, InfoArg };
public:
AllocaInst *getPromise() const {
Value *Arg = getArgOperand(PromiseArg);
return isa<ConstantPointerNull>(Arg)
? nullptr
: cast<AllocaInst>(Arg->stripPointerCasts());
}
void clearPromise() {
Value *Arg = getArgOperand(PromiseArg);
setArgOperand(PromiseArg,
ConstantPointerNull::get(Type::getInt8PtrTy(getContext())));
if (isa<AllocaInst>(Arg))
return;
assert((isa<BitCastInst>(Arg) || isa<GetElementPtrInst>(Arg)) &&
"unexpected instruction designating the promise");
auto *Inst = cast<Instruction>(Arg);
if (Inst->use_empty()) {
Inst->eraseFromParent();
return;
}
Inst->moveBefore(getCoroBegin()->getNextNode());
}
struct Info {
ConstantStruct *OutlinedParts = nullptr;
ConstantArray *Resumers = nullptr;
bool hasOutlinedParts() const { return OutlinedParts != nullptr; }
bool isPostSplit() const { return Resumers != nullptr; }
bool isPreSplit() const { return !isPostSplit(); }
};
Info getInfo() const {
Info Result;
auto *GV = dyn_cast<GlobalVariable>(getRawInfo());
if (!GV)
return Result;
assert(GV->isConstant() && GV->hasDefinitiveInitializer());
Constant *Initializer = GV->getInitializer();
if ((Result.OutlinedParts = dyn_cast<ConstantStruct>(Initializer)))
return Result;
Result.Resumers = cast<ConstantArray>(Initializer);
return Result;
}
Constant *getRawInfo() const {
return cast<Constant>(getArgOperand(InfoArg)->stripPointerCasts());
}
void setInfo(Constant *C) { setArgOperand(InfoArg, C); }
Function *getCoroutine() const {
return cast<Function>(getArgOperand(CoroutineArg)->stripPointerCasts());
}
void setCoroutineSelf() {
assert(isa<ConstantPointerNull>(getArgOperand(CoroutineArg)) &&
"Coroutine argument is already assigned");
auto *const Int8PtrTy = Type::getInt8PtrTy(getContext());
setArgOperand(CoroutineArg,
ConstantExpr::getBitCast(getFunction(), Int8PtrTy));
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_id;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY AnyCoroIdRetconInst : public AnyCoroIdInst {
enum { SizeArg, AlignArg, StorageArg, PrototypeArg, AllocArg, DeallocArg };
public:
void checkWellFormed() const;
uint64_t getStorageSize() const {
return cast<ConstantInt>(getArgOperand(SizeArg))->getZExtValue();
}
Align getStorageAlignment() const {
return cast<ConstantInt>(getArgOperand(AlignArg))->getAlignValue();
}
Value *getStorage() const {
return getArgOperand(StorageArg);
}
Function *getPrototype() const {
return cast<Function>(getArgOperand(PrototypeArg)->stripPointerCasts());
}
Function *getAllocFunction() const {
return cast<Function>(getArgOperand(AllocArg)->stripPointerCasts());
}
Function *getDeallocFunction() const {
return cast<Function>(getArgOperand(DeallocArg)->stripPointerCasts());
}
static bool classof(const IntrinsicInst *I) {
auto ID = I->getIntrinsicID();
return ID == Intrinsic::coro_id_retcon
|| ID == Intrinsic::coro_id_retcon_once;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroIdRetconInst
: public AnyCoroIdRetconInst {
public:
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_id_retcon;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroIdRetconOnceInst
: public AnyCoroIdRetconInst {
public:
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_id_retcon_once;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroIdAsyncInst : public AnyCoroIdInst {
enum { SizeArg, AlignArg, StorageArg, AsyncFuncPtrArg };
public:
void checkWellFormed() const;
uint64_t getStorageSize() const {
return cast<ConstantInt>(getArgOperand(SizeArg))->getZExtValue();
}
Align getStorageAlignment() const {
return cast<ConstantInt>(getArgOperand(AlignArg))->getAlignValue();
}
Value *getStorage() const {
return getParent()->getParent()->getArg(getStorageArgumentIndex());
}
unsigned getStorageArgumentIndex() const {
auto *Arg = cast<ConstantInt>(getArgOperand(StorageArg));
return Arg->getZExtValue();
}
GlobalVariable *getAsyncFunctionPointer() const {
return cast<GlobalVariable>(
getArgOperand(AsyncFuncPtrArg)->stripPointerCasts());
}
static bool classof(const IntrinsicInst *I) {
auto ID = I->getIntrinsicID();
return ID == Intrinsic::coro_id_async;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroAsyncContextAllocInst : public IntrinsicInst {
enum { AsyncFuncPtrArg };
public:
GlobalVariable *getAsyncFunctionPointer() const {
return cast<GlobalVariable>(
getArgOperand(AsyncFuncPtrArg)->stripPointerCasts());
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_async_context_alloc;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroAsyncContextDeallocInst
: public IntrinsicInst {
enum { AsyncContextArg };
public:
Value *getAsyncContext() const {
return getArgOperand(AsyncContextArg)->stripPointerCasts();
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_async_context_dealloc;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroAsyncResumeInst : public IntrinsicInst {
public:
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_async_resume;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroAsyncSizeReplace : public IntrinsicInst {
public:
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_async_size_replace;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroFrameInst : public IntrinsicInst {
public:
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_frame;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroFreeInst : public IntrinsicInst {
enum { IdArg, FrameArg };
public:
Value *getFrame() const { return getArgOperand(FrameArg); }
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_free;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroBeginInst : public IntrinsicInst {
enum { IdArg, MemArg };
public:
AnyCoroIdInst *getId() const {
return cast<AnyCoroIdInst>(getArgOperand(IdArg));
}
Value *getMem() const { return getArgOperand(MemArg); }
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_begin;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroSaveInst : public IntrinsicInst {
public:
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_save;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroPromiseInst : public IntrinsicInst {
enum { FrameArg, AlignArg, FromArg };
public:
bool isFromPromise() const {
return cast<Constant>(getArgOperand(FromArg))->isOneValue();
}
Align getAlignment() const {
return cast<ConstantInt>(getArgOperand(AlignArg))->getAlignValue();
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_promise;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY AnyCoroSuspendInst : public IntrinsicInst {
public:
CoroSaveInst *getCoroSave() const;
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_suspend ||
I->getIntrinsicID() == Intrinsic::coro_suspend_async ||
I->getIntrinsicID() == Intrinsic::coro_suspend_retcon;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroSuspendInst : public AnyCoroSuspendInst {
enum { SaveArg, FinalArg };
public:
CoroSaveInst *getCoroSave() const {
Value *Arg = getArgOperand(SaveArg);
if (auto *SI = dyn_cast<CoroSaveInst>(Arg))
return SI;
assert(isa<ConstantTokenNone>(Arg));
return nullptr;
}
bool isFinal() const {
return cast<Constant>(getArgOperand(FinalArg))->isOneValue();
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_suspend;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
inline CoroSaveInst *AnyCoroSuspendInst::getCoroSave() const {
if (auto Suspend = dyn_cast<CoroSuspendInst>(this))
return Suspend->getCoroSave();
return nullptr;
}
class LLVM_LIBRARY_VISIBILITY CoroSuspendAsyncInst : public AnyCoroSuspendInst {
public:
enum {
StorageArgNoArg,
ResumeFunctionArg,
AsyncContextProjectionArg,
MustTailCallFuncArg
};
void checkWellFormed() const;
unsigned getStorageArgumentIndex() const {
auto *Arg = cast<ConstantInt>(getArgOperand(StorageArgNoArg));
return Arg->getZExtValue();
}
Function *getAsyncContextProjectionFunction() const {
return cast<Function>(
getArgOperand(AsyncContextProjectionArg)->stripPointerCasts());
}
CoroAsyncResumeInst *getResumeFunction() const {
return cast<CoroAsyncResumeInst>(
getArgOperand(ResumeFunctionArg)->stripPointerCasts());
}
Function *getMustTailCallFunction() const {
return cast<Function>(
getArgOperand(MustTailCallFuncArg)->stripPointerCasts());
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_suspend_async;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroSuspendRetconInst : public AnyCoroSuspendInst {
public:
op_iterator value_begin() { return arg_begin(); }
const_op_iterator value_begin() const { return arg_begin(); }
op_iterator value_end() { return arg_end(); }
const_op_iterator value_end() const { return arg_end(); }
iterator_range<op_iterator> value_operands() {
return make_range(value_begin(), value_end());
}
iterator_range<const_op_iterator> value_operands() const {
return make_range(value_begin(), value_end());
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_suspend_retcon;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroSizeInst : public IntrinsicInst {
public:
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_size;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroAlignInst : public IntrinsicInst {
public:
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_align;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY AnyCoroEndInst : public IntrinsicInst {
enum { FrameArg, UnwindArg };
public:
bool isFallthrough() const { return !isUnwind(); }
bool isUnwind() const {
return cast<Constant>(getArgOperand(UnwindArg))->isOneValue();
}
static bool classof(const IntrinsicInst *I) {
auto ID = I->getIntrinsicID();
return ID == Intrinsic::coro_end || ID == Intrinsic::coro_end_async;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroEndInst : public AnyCoroEndInst {
public:
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_end;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroAsyncEndInst : public AnyCoroEndInst {
enum { FrameArg, UnwindArg, MustTailCallFuncArg };
public:
void checkWellFormed() const;
Function *getMustTailCallFunction() const {
if (arg_size() < 3)
return nullptr;
return cast<Function>(
getArgOperand(MustTailCallFuncArg)->stripPointerCasts());
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_end_async;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroAllocaAllocInst : public IntrinsicInst {
enum { SizeArg, AlignArg };
public:
Value *getSize() const {
return getArgOperand(SizeArg);
}
Align getAlignment() const {
return cast<ConstantInt>(getArgOperand(AlignArg))->getAlignValue();
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_alloca_alloc;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroAllocaGetInst : public IntrinsicInst {
enum { AllocArg };
public:
CoroAllocaAllocInst *getAlloc() const {
return cast<CoroAllocaAllocInst>(getArgOperand(AllocArg));
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_alloca_get;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
class LLVM_LIBRARY_VISIBILITY CoroAllocaFreeInst : public IntrinsicInst {
enum { AllocArg };
public:
CoroAllocaAllocInst *getAlloc() const {
return cast<CoroAllocaAllocInst>(getArgOperand(AllocArg));
}
static bool classof(const IntrinsicInst *I) {
return I->getIntrinsicID() == Intrinsic::coro_alloca_free;
}
static bool classof(const Value *V) {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
};
}
#endif