24 #define DEBUG_TYPE "coro-elide" 36 Lowerer(
Module &M) : LowererBase(M) {}
56 if (ValueTy != IntrTy) {
82 if (
auto *Call = dyn_cast<CallInst>(&
I))
86 if (Call->isMustTailCall())
88 "marked as musttail");
89 Call->setTailCall(
false);
96 return cast<PointerType>(ArgType)->getElementType();
102 if (!isa<AllocaInst>(&
I))
121 for (
auto *CA : CoroAllocs) {
122 CA->replaceAllUsesWith(False);
123 CA->eraseFromParent();
135 for (
auto *CB : CoroBegins) {
136 CB->replaceAllUsesWith(FrameVoidPtr);
137 CB->eraseFromParent();
148 if (CoroAllocs.empty())
159 auto *TI =
B.getTerminator();
160 if (TI->getNumSuccessors() == 0 && !TI->isExceptionalTerminator() &&
161 !isa<UnreachableInst>(TI))
179 if (
auto *CB = dyn_cast<CoroBeginInst>(DA->getFrame()))
180 ReferencedCoroBegins.
insert(CB);
188 return ReferencedCoroBegins.
size() == CoroBegins.size();
201 if (
auto *CB = dyn_cast<CoroBeginInst>(U))
202 CoroBegins.push_back(CB);
203 else if (
auto *CA = dyn_cast<CoroAllocInst>(U))
204 CoroAllocs.push_back(CA);
205 else if (
auto *CF = dyn_cast<CoroFreeInst>(U))
206 CoroFrees.push_back(CF);
215 if (
auto *II = dyn_cast<CoroSubFnInst>(U))
216 switch (II->getIndex()) {
218 ResumeAddr.push_back(II);
221 DestroyAddr.push_back(II);
231 assert(Resumers &&
"PostSplit coro.id Info argument must refer to an array" 232 "of coroutine subfunctions");
233 auto *ResumeAddrConstant =
238 bool ShouldElide = shouldElide(CoroId->
getFunction(), DT);
247 auto *FrameTy =
getFrameType(cast<Function>(ResumeAddrConstant));
248 elideHeapAllocations(CoroId->
getFunction(), FrameTy, AA);
260 if (
auto *SubFn = dyn_cast<CoroSubFnInst>(&
I))
264 if (DevirtAddr.
empty())
269 assert(DevirtFn &&
"coro.devirt.fn not found");
286 std::unique_ptr<Lowerer> L;
288 bool doInitialization(
Module &M)
override {
290 L = llvm::make_unique<Lowerer>(M);
298 bool Changed =
false;
307 if (
auto *CII = dyn_cast<CoroIdInst>(&
I))
308 if (CII->getInfo().isPostSplit())
310 if (CII->getCoroutine() != CII->getFunction())
311 L->CoroIds.push_back(CII);
314 if (L->CoroIds.empty())
317 AAResults &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
318 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
320 for (
auto *CII : L->CoroIds)
321 Changed |= L->processCoroId(CII, AA, DT);
329 StringRef getPassName()
const override {
return "Coroutine Elision"; }
335 CoroElide,
"coro-elide",
336 "Coroutine frame allocation elision and indirect calls replacement",
false,
340 CoroElide,
"coro-elide",
341 "Coroutine frame allocation elision and indirect calls replacement",
false,
Pass interface - Implemented by all 'passes'.
A parsed version of the target data layout string in and methods for querying it. ...
static ConstantInt * getFalse(LLVMContext &Context)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This represents the llvm.coro.alloc instruction.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
void initializeCoroElidePass(PassRegistry &)
void push_back(const T &Elt)
This class represents a function call, abstracting a target machine's calling convention.
The two locations do not alias at all.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
iv Induction Variable Users
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
unsigned getAllocaAddrSpace() const
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
static bool replaceDevirtTrigger(Function &F)
This class represents the llvm.coro.subfn.addr instruction.
Type * getType() const
All values are typed, get the type of this value.
This class represents a no-op cast from one type to another.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
const BasicBlock & getEntryBlock() const
static bool runOnFunction(Function &F, bool PostInlining)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
#define CORO_PRESPLIT_ATTR
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
This is an important base class in LLVM.
bool isPointerTy() const
True if this is an instance of PointerType.
Pass * createCoroElidePass()
Analyze coroutines use sites, devirtualize resume/destroy calls and elide heap allocation for corouti...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
static void replaceWithConstant(Constant *Value, SmallVectorImpl< CoroSubFnInst *> &Users)
FunctionPass class - This class is used to implement most global optimizations.
static void removeTailCallAttribute(AllocaInst *Frame, AAResults &AA)
const Function * getFunction() const
Return the function this instruction belongs to.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static Type * getFrameType(Function *Resume)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
This class represents the llvm.coro.begin instruction.
ConstantArray - Constant Array Declarations.
iterator_range< user_iterator > users()
static Instruction * getFirstNonAllocaInTheEntryBlock(Function *F)
static bool operandReferences(CallInst *CI, AllocaInst *Frame, AAResults &AA)
LLVM_NODISCARD bool empty() const
iterator_range< value_op_iterator > operand_values()
#define CORO_DEVIRT_TRIGGER_FN
void replaceCoroFree(CoroIdInst *CoroId, bool Elide)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
bool declaresIntrinsics(Module &M, std::initializer_list< StringRef >)
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
StringRef - Represent a constant reference to a string, i.e.
inst_range instructions(Function *F)
Legacy analysis pass which computes a DominatorTree.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
INITIALIZE_PASS_BEGIN(CoroElide, "coro-elide", "Coroutine frame allocation elision and indirect calls replacement", false, false) INITIALIZE_PASS_END(CoroElide
an instruction to allocate memory on the stack