LLVM  8.0.1
CoroCleanup.cpp
Go to the documentation of this file.
1 //===- CoroCleanup.cpp - Coroutine Cleanup Pass ---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // This pass lowers all remaining coroutine intrinsics.
10 //===----------------------------------------------------------------------===//
11 
12 #include "CoroInternal.h"
13 #include "llvm/IR/IRBuilder.h"
14 #include "llvm/IR/InstIterator.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Transforms/Scalar.h"
18 
19 using namespace llvm;
20 
21 #define DEBUG_TYPE "coro-cleanup"
22 
23 namespace {
24 // Created on demand if CoroCleanup pass has work to do.
25 struct Lowerer : coro::LowererBase {
26  IRBuilder<> Builder;
27  Lowerer(Module &M) : LowererBase(M), Builder(Context) {}
28  bool lowerRemainingCoroIntrinsics(Function &F);
29 };
30 }
31 
32 static void simplifyCFG(Function &F) {
35 
36  FPM.doInitialization();
37  FPM.run(F);
38  FPM.doFinalization();
39 }
40 
41 static void lowerSubFn(IRBuilder<> &Builder, CoroSubFnInst *SubFn) {
42  Builder.SetInsertPoint(SubFn);
43  Value *FrameRaw = SubFn->getFrame();
44  int Index = SubFn->getIndex();
45 
46  auto *FrameTy = StructType::get(
47  SubFn->getContext(), {Builder.getInt8PtrTy(), Builder.getInt8PtrTy()});
48  PointerType *FramePtrTy = FrameTy->getPointerTo();
49 
50  Builder.SetInsertPoint(SubFn);
51  auto *FramePtr = Builder.CreateBitCast(FrameRaw, FramePtrTy);
52  auto *Gep = Builder.CreateConstInBoundsGEP2_32(FrameTy, FramePtr, 0, Index);
53  auto *Load = Builder.CreateLoad(Gep);
54 
55  SubFn->replaceAllUsesWith(Load);
56 }
57 
58 bool Lowerer::lowerRemainingCoroIntrinsics(Function &F) {
59  bool Changed = false;
60 
61  for (auto IB = inst_begin(F), E = inst_end(F); IB != E;) {
62  Instruction &I = *IB++;
63  if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
64  switch (II->getIntrinsicID()) {
65  default:
66  continue;
68  II->replaceAllUsesWith(II->getArgOperand(1));
69  break;
71  II->replaceAllUsesWith(II->getArgOperand(1));
72  break;
74  II->replaceAllUsesWith(ConstantInt::getTrue(Context));
75  break;
76  case Intrinsic::coro_id:
77  II->replaceAllUsesWith(ConstantTokenNone::get(Context));
78  break;
80  lowerSubFn(Builder, cast<CoroSubFnInst>(II));
81  break;
82  }
83  II->eraseFromParent();
84  Changed = true;
85  }
86  }
87 
88  if (Changed) {
89  // After replacement were made we can cleanup the function body a little.
90  simplifyCFG(F);
91  }
92  return Changed;
93 }
94 
95 //===----------------------------------------------------------------------===//
96 // Top Level Driver
97 //===----------------------------------------------------------------------===//
98 
99 namespace {
100 
101 struct CoroCleanup : FunctionPass {
102  static char ID; // Pass identification, replacement for typeid
103 
104  CoroCleanup() : FunctionPass(ID) {
106  }
107 
108  std::unique_ptr<Lowerer> L;
109 
110  // This pass has work to do only if we find intrinsics we are going to lower
111  // in the module.
112  bool doInitialization(Module &M) override {
113  if (coro::declaresIntrinsics(M, {"llvm.coro.alloc", "llvm.coro.begin",
114  "llvm.coro.subfn.addr", "llvm.coro.free",
115  "llvm.coro.id"}))
116  L = llvm::make_unique<Lowerer>(M);
117  return false;
118  }
119 
120  bool runOnFunction(Function &F) override {
121  if (L)
122  return L->lowerRemainingCoroIntrinsics(F);
123  return false;
124  }
125  void getAnalysisUsage(AnalysisUsage &AU) const override {
126  if (!L)
127  AU.setPreservesAll();
128  }
129  StringRef getPassName() const override { return "Coroutine Cleanup"; }
130 };
131 }
132 
133 char CoroCleanup::ID = 0;
134 INITIALIZE_PASS(CoroCleanup, "coro-cleanup",
135  "Lower all coroutine related intrinsics", false, false)
136 
137 Pass *llvm::createCoroCleanupPass() { return new CoroCleanup(); }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:24
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve &#39;CreateLoad(Ty, Ptr, "...")&#39; correctly, instead of converting the string to &#39;bool...
Definition: IRBuilder.h:1357
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:705
INITIALIZE_PASS(CoroCleanup, "coro-cleanup", "Lower all coroutine related intrinsics", false, false) Pass *llvm
F(f)
FunctionPass * createCFGSimplificationPass(unsigned Threshold=1, bool ForwardSwitchCond=false, bool ConvertSwitch=false, bool KeepLoops=true, bool SinkCommon=false, std::function< bool(const Function &)> Ftor=nullptr)
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:132
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
Definition: Type.cpp:652
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:743
static StructType * get(LLVMContext &Context, ArrayRef< Type *> Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:342
This class represents the llvm.coro.subfn.addr instruction.
Definition: CoroInstr.h:35
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1732
void add(Pass *P) override
Add a pass to the queue of passes to run.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:127
Class to represent pointers.
Definition: DerivedTypes.h:467
static bool runOnFunction(Function &F, bool PostInlining)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Pass * createCoroCleanupPass()
Lower all remaining coroutine intrinsics.
ResumeKind getIndex() const
Definition: CoroInstr.h:49
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
FunctionPassManager manages FunctionPasses and BasicBlockPassManagers.
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:385
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:578
void setPreservesAll()
Set by analyses that do not transform their input at all.
void initializeCoroCleanupPass(PassRegistry &)
#define I(x, y, z)
Definition: MD5.cpp:58
Value * CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1, const Twine &Name="")
Definition: IRBuilder.h:1553
static ConstantTokenNone * get(LLVMContext &Context)
Return the ConstantTokenNone.
Definition: Constants.cpp:1130
Value * getFrame() const
Definition: CoroInstr.h:48
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:566
LLVM Value Representation.
Definition: Value.h:73
static void lowerSubFn(IRBuilder<> &Builder, CoroSubFnInst *SubFn)
Definition: CoroCleanup.cpp:41
bool declaresIntrinsics(Module &M, std::initializer_list< StringRef >)
Definition: Coroutines.cpp:140
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:133
bool simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI, const SimplifyCFGOptions &Options={}, SmallPtrSetImpl< BasicBlock *> *LoopHeaders=nullptr)
This function is used to do simplification of a CFG.