clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CoroFrame.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | #include "CoroInternal.h" |
| 18 | #include "llvm/ADT/BitVector.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/Analysis/PtrUseVisitor.h" |
| 21 | #include "llvm/Analysis/StackLifetime.h" |
| 22 | #include "llvm/Config/llvm-config.h" |
| 23 | #include "llvm/IR/CFG.h" |
| 24 | #include "llvm/IR/DIBuilder.h" |
| 25 | #include "llvm/IR/Dominators.h" |
| 26 | #include "llvm/IR/IRBuilder.h" |
| 27 | #include "llvm/IR/InstIterator.h" |
| 28 | #include "llvm/Support/CommandLine.h" |
| 29 | #include "llvm/Support/Debug.h" |
| 30 | #include "llvm/Support/MathExtras.h" |
| 31 | #include "llvm/Support/OptimizedStructLayout.h" |
| 32 | #include "llvm/Support/circular_raw_ostream.h" |
| 33 | #include "llvm/Support/raw_ostream.h" |
| 34 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 35 | #include "llvm/Transforms/Utils/Local.h" |
| 36 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
| 37 | #include <algorithm> |
| 38 | |
| 39 | using namespace llvm; |
| 40 | |
| 41 | |
| 42 | |
| 43 | #define DEBUG_TYPE "coro-suspend-crossing" |
| 44 | |
| 45 | static cl::opt<bool> EnableReuseStorageInFrame( |
| 46 | "reuse-storage-in-coroutine-frame", cl::Hidden, |
| 47 | cl::desc( |
| 48 | "Enable the optimization which would reuse the storage in the coroutine \ |
| 49 | frame for allocas whose liferanges are not overlapped, for testing purposes"), |
| 50 | llvm::cl::init(false)); |
| 51 | |
| 52 | enum { SmallVectorThreshold = 32 }; |
| 53 | |
| 54 | |
| 55 | namespace { |
| 56 | class BlockToIndexMapping { |
| 57 | SmallVector<BasicBlock *, SmallVectorThreshold> V; |
| 58 | |
| 59 | public: |
| 60 | size_t size() const { return V.size(); } |
| 61 | |
| 62 | BlockToIndexMapping(Function &F) { |
| 63 | for (BasicBlock &BB : F) |
| 64 | V.push_back(&BB); |
| 65 | llvm::sort(V); |
| 66 | } |
| 67 | |
| 68 | size_t blockToIndex(BasicBlock *BB) const { |
| 69 | auto *I = llvm::lower_bound(V, BB); |
| 70 | assert(I != V.end() && *I == BB && "BasicBlockNumberng: Unknown block"); |
| 71 | return I - V.begin(); |
| 72 | } |
| 73 | |
| 74 | BasicBlock *indexToBlock(unsigned Index) const { return V[Index]; } |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | namespace { |
| 91 | struct SuspendCrossingInfo { |
| 92 | BlockToIndexMapping Mapping; |
| 93 | |
| 94 | struct BlockData { |
| 95 | BitVector Consumes; |
| 96 | BitVector Kills; |
| 97 | bool Suspend = false; |
| 98 | bool End = false; |
| 99 | }; |
| 100 | SmallVector<BlockData, SmallVectorThreshold> Block; |
| 101 | |
| 102 | iterator_range<succ_iterator> successors(BlockData const &BD) const { |
| 103 | BasicBlock *BB = Mapping.indexToBlock(&BD - &Block[0]); |
| 104 | return llvm::successors(BB); |
| 105 | } |
| 106 | |
| 107 | BlockData &getBlockData(BasicBlock *BB) { |
| 108 | return Block[Mapping.blockToIndex(BB)]; |
| 109 | } |
| 110 | |
| 111 | void dump() const; |
| 112 | void dump(StringRef Label, BitVector const &BV) const; |
| 113 | |
| 114 | SuspendCrossingInfo(Function &F, coro::Shape &Shape); |
| 115 | |
| 116 | bool hasPathCrossingSuspendPoint(BasicBlock *DefBB, BasicBlock *UseBB) const { |
| 117 | size_t const DefIndex = Mapping.blockToIndex(DefBB); |
| 118 | size_t const UseIndex = Mapping.blockToIndex(UseBB); |
| 119 | |
| 120 | bool const Result = Block[UseIndex].Kills[DefIndex]; |
| 121 | LLVM_DEBUG(dbgs() << UseBB->getName() << " => " << DefBB->getName() |
| 122 | << " answer is " << Result << "\n"); |
| 123 | return Result; |
| 124 | } |
| 125 | |
| 126 | bool isDefinitionAcrossSuspend(BasicBlock *DefBB, User *U) const { |
| 127 | auto *I = cast<Instruction>(U); |
| 128 | |
| 129 | |
| 130 | |
| 131 | if (auto *PN = dyn_cast<PHINode>(I)) |
| 132 | if (PN->getNumIncomingValues() > 1) |
| 133 | return false; |
| 134 | |
| 135 | BasicBlock *UseBB = I->getParent(); |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | if (isa<CoroSuspendRetconInst>(I) || isa<CoroSuspendAsyncInst>(I)) { |
| 141 | UseBB = UseBB->getSinglePredecessor(); |
| 142 | assert(UseBB && "should have split coro.suspend into its own block"); |
| 143 | } |
| 144 | |
| 145 | return hasPathCrossingSuspendPoint(DefBB, UseBB); |
| 146 | } |
| 147 | |
| 148 | bool isDefinitionAcrossSuspend(Argument &A, User *U) const { |
| 149 | return isDefinitionAcrossSuspend(&A.getParent()->getEntryBlock(), U); |
| 150 | } |
| 151 | |
| 152 | bool isDefinitionAcrossSuspend(Instruction &I, User *U) const { |
| 153 | auto *DefBB = I.getParent(); |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | if (isa<AnyCoroSuspendInst>(I)) { |
| 159 | DefBB = DefBB->getSingleSuccessor(); |
| 160 | assert(DefBB && "should have split coro.suspend into its own block"); |
| 161 | } |
| 162 | |
| 163 | return isDefinitionAcrossSuspend(DefBB, U); |
| 164 | } |
| 165 | |
| 166 | bool isDefinitionAcrossSuspend(Value &V, User *U) const { |
| 167 | if (auto *Arg = dyn_cast<Argument>(&V)) |
| 168 | return isDefinitionAcrossSuspend(*Arg, U); |
| 169 | if (auto *Inst = dyn_cast<Instruction>(&V)) |
| 170 | return isDefinitionAcrossSuspend(*Inst, U); |
| 171 | |
| 172 | llvm_unreachable( |
| 173 | "Coroutine could only collect Argument and Instruction now."); |
| 174 | } |
| 175 | }; |
| 176 | } |
| 177 | |
| 178 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 179 | LLVM_DUMP_METHOD void SuspendCrossingInfo::dump(StringRef Label, |
| 180 | BitVector const &BV) const { |
| 181 | dbgs() << Label << ":"; |
| 182 | for (size_t I = 0, N = BV.size(); I < N; ++I) |
| 183 | if (BV[I]) |
| 184 | dbgs() << " " << Mapping.indexToBlock(I)->getName(); |
| 185 | dbgs() << "\n"; |
| 186 | } |
| 187 | |
| 188 | LLVM_DUMP_METHOD void SuspendCrossingInfo::dump() const { |
| 189 | for (size_t I = 0, N = Block.size(); I < N; ++I) { |
| 190 | BasicBlock *const B = Mapping.indexToBlock(I); |
| 191 | dbgs() << B->getName() << ":\n"; |
| 192 | dump(" Consumes", Block[I].Consumes); |
| 193 | dump(" Kills", Block[I].Kills); |
| 194 | } |
| 195 | dbgs() << "\n"; |
| 196 | } |
| 197 | #endif |
| 198 | |
| 199 | SuspendCrossingInfo::SuspendCrossingInfo(Function &F, coro::Shape &Shape) |
| 200 | : Mapping(F) { |
| 201 | const size_t N = Mapping.size(); |
| 202 | Block.resize(N); |
| 203 | |
| 204 | |
| 205 | for (size_t I = 0; I < N; ++I) { |
| 206 | auto &B = Block[I]; |
| 207 | B.Consumes.resize(N); |
| 208 | B.Kills.resize(N); |
| 209 | B.Consumes.set(I); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | for (auto *CE : Shape.CoroEnds) |
| 216 | getBlockData(CE->getParent()).End = true; |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | auto markSuspendBlock = [&](IntrinsicInst *BarrierInst) { |
| 223 | BasicBlock *SuspendBlock = BarrierInst->getParent(); |
| 224 | auto &B = getBlockData(SuspendBlock); |
| 225 | B.Suspend = true; |
| 226 | B.Kills |= B.Consumes; |
| 227 | }; |
| 228 | for (auto *CSI : Shape.CoroSuspends) { |
| 229 | markSuspendBlock(CSI); |
| 230 | if (auto *Save = CSI->getCoroSave()) |
| 231 | markSuspendBlock(Save); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | int Iteration = 0; |
| 236 | (void)Iteration; |
| 237 | |
| 238 | bool Changed; |
| 239 | do { |
| 240 | LLVM_DEBUG(dbgs() << "iteration " << ++Iteration); |
| 241 | LLVM_DEBUG(dbgs() << "==============\n"); |
| 242 | |
| 243 | Changed = false; |
| 244 | for (size_t I = 0; I < N; ++I) { |
| 245 | auto &B = Block[I]; |
| 246 | for (BasicBlock *SI : successors(B)) { |
| 247 | |
| 248 | auto SuccNo = Mapping.blockToIndex(SI); |
| 249 | |
| 250 | |
| 251 | |
| 252 | auto &S = Block[SuccNo]; |
| 253 | auto SavedConsumes = S.Consumes; |
| 254 | auto SavedKills = S.Kills; |
| 255 | |
| 256 | |
| 257 | S.Consumes |= B.Consumes; |
| 258 | S.Kills |= B.Kills; |
| 259 | |
| 260 | |
| 261 | |
| 262 | if (B.Suspend) { |
| 263 | S.Kills |= B.Consumes; |
| 264 | } |
| 265 | if (S.Suspend) { |
| 266 | |
| 267 | |
| 268 | S.Kills |= S.Consumes; |
| 269 | } else if (S.End) { |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | S.Kills.reset(); |
| 275 | } else { |
| 276 | |
| 277 | |
| 278 | S.Kills.reset(SuccNo); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | Changed |= (S.Kills != SavedKills) || (S.Consumes != SavedConsumes); |
| 283 | |
| 284 | if (S.Kills != SavedKills) { |
| 285 | LLVM_DEBUG(dbgs() << "\nblock " << I << " follower " << SI->getName() |
| 286 | << "\n"); |
| 287 | LLVM_DEBUG(dump("S.Kills", S.Kills)); |
| 288 | LLVM_DEBUG(dump("SavedKills", SavedKills)); |
| 289 | } |
| 290 | if (S.Consumes != SavedConsumes) { |
| 291 | LLVM_DEBUG(dbgs() << "\nblock " << I << " follower " << SI << "\n"); |
| 292 | LLVM_DEBUG(dump("S.Consume", S.Consumes)); |
| 293 | LLVM_DEBUG(dump("SavedCons", SavedConsumes)); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } while (Changed); |
| 298 | LLVM_DEBUG(dump()); |
| 299 | } |
| 300 | |
| 301 | #undef DEBUG_TYPE // "coro-suspend-crossing" |
| 302 | #define DEBUG_TYPE "coro-frame" |
| 303 | |
| 304 | namespace { |
| 305 | class FrameTypeBuilder; |
| 306 | |
| 307 | using SpillInfo = SmallMapVector<Value *, SmallVector<Instruction *, 2>, 8>; |
| 308 | struct AllocaInfo { |
| 309 | AllocaInst *Alloca; |
| 310 | DenseMap<Instruction *, llvm::Optional<APInt>> Aliases; |
| 311 | bool MayWriteBeforeCoroBegin; |
| 312 | AllocaInfo(AllocaInst *Alloca, |
| 313 | DenseMap<Instruction *, llvm::Optional<APInt>> Aliases, |
| 314 | bool MayWriteBeforeCoroBegin) |
| 315 | : Alloca(Alloca), Aliases(std::move(Aliases)), |
| 316 | MayWriteBeforeCoroBegin(MayWriteBeforeCoroBegin) {} |
| 317 | }; |
| 318 | struct FrameDataInfo { |
| 319 | |
| 320 | |
| 321 | SpillInfo Spills; |
| 322 | |
| 323 | |
| 324 | SmallVector<AllocaInfo, 8> Allocas; |
| 325 | |
| 326 | SmallVector<Value *, 8> getAllDefs() const { |
| 327 | SmallVector<Value *, 8> Defs; |
| 328 | for (const auto &P : Spills) |
| 329 | Defs.push_back(P.first); |
| 330 | for (const auto &A : Allocas) |
| 331 | Defs.push_back(A.Alloca); |
| 332 | return Defs; |
| 333 | } |
| 334 | |
| 335 | uint32_t getFieldIndex(Value *V) const { |
| 336 | auto Itr = FieldIndexMap.find(V); |
| 337 | assert(Itr != FieldIndexMap.end() && |
| 338 | "Value does not have a frame field index"); |
| 339 | return Itr->second; |
| 340 | } |
| 341 | |
| 342 | void setFieldIndex(Value *V, uint32_t Index) { |
| 343 | assert((LayoutIndexUpdateStarted || FieldIndexMap.count(V) == 0) && |
| 344 | "Cannot set the index for the same field twice."); |
| 345 | FieldIndexMap[V] = Index; |
| 346 | } |
| 347 | |
| 348 | uint64_t getAlign(Value *V) const { |
| 349 | auto Iter = FieldAlignMap.find(V); |
| 350 | assert(Iter != FieldAlignMap.end()); |
| 351 | return Iter->second; |
| 352 | } |
| 353 | |
| 354 | void setAlign(Value *V, uint64_t Align) { |
| 355 | assert(FieldAlignMap.count(V) == 0); |
| 356 | FieldAlignMap.insert({V, Align}); |
| 357 | } |
| 358 | |
| 359 | uint64_t getOffset(Value *V) const { |
| 360 | auto Iter = FieldOffsetMap.find(V); |
| 361 | assert(Iter != FieldOffsetMap.end()); |
| 362 | return Iter->second; |
| 363 | } |
| 364 | |
| 365 | void setOffset(Value *V, uint64_t Offset) { |
| 366 | assert(FieldOffsetMap.count(V) == 0); |
| 367 | FieldOffsetMap.insert({V, Offset}); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | void updateLayoutIndex(FrameTypeBuilder &B); |
| 372 | |
| 373 | private: |
| 374 | |
| 375 | |
| 376 | bool LayoutIndexUpdateStarted = false; |
| 377 | |
| 378 | |
| 379 | |
| 380 | DenseMap<Value *, uint32_t> FieldIndexMap; |
| 381 | |
| 382 | |
| 383 | DenseMap<Value *, uint64_t> FieldAlignMap; |
| 384 | |
| 385 | |
| 386 | DenseMap<Value *, uint64_t> FieldOffsetMap; |
| 387 | }; |
| 388 | } |
| 389 | |
| 390 | #ifndef NDEBUG |
| 391 | static void dumpSpills(StringRef Title, const SpillInfo &Spills) { |
| 392 | dbgs() << "------------- " << Title << "--------------\n"; |
| 393 | for (const auto &E : Spills) { |
| 394 | E.first->dump(); |
| 395 | dbgs() << " user: "; |
| 396 | for (auto *I : E.second) |
| 397 | I->dump(); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static void dumpAllocas(const SmallVectorImpl<AllocaInfo> &Allocas) { |
| 402 | dbgs() << "------------- Allocas --------------\n"; |
| 403 | for (const auto &A : Allocas) { |
| 404 | A.Alloca->dump(); |
| 405 | } |
| 406 | } |
| 407 | #endif |
| 408 | |
| 409 | namespace { |
| 410 | using FieldIDType = size_t; |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | class FrameTypeBuilder { |
| 416 | private: |
| 417 | struct Field { |
| 418 | uint64_t Size; |
| 419 | uint64_t Offset; |
| 420 | Type *Ty; |
| 421 | FieldIDType LayoutFieldIndex; |
| 422 | Align Alignment; |
| 423 | Align TyAlignment; |
| 424 | }; |
| 425 | |
| 426 | const DataLayout &DL; |
| 427 | LLVMContext &Context; |
| 428 | uint64_t StructSize = 0; |
| 429 | Align StructAlign; |
| 430 | bool IsFinished = false; |
| 431 | |
| 432 | Optional<Align> MaxFrameAlignment; |
| 433 | |
| 434 | SmallVector<Field, 8> Fields; |
| 435 | DenseMap<Value*, unsigned> FieldIndexByKey; |
| 436 | |
| 437 | public: |
| 438 | FrameTypeBuilder(LLVMContext &Context, DataLayout const &DL, |
| 439 | Optional<Align> MaxFrameAlignment) |
| 440 | : DL(DL), Context(Context), MaxFrameAlignment(MaxFrameAlignment) {} |
| 441 | |
| 442 | |
| 443 | |
| 444 | LLVM_NODISCARD FieldIDType addFieldForAlloca(AllocaInst *AI, |
| 445 | bool IsHeader = false) { |
| 446 | Type *Ty = AI->getAllocatedType(); |
| 447 | |
| 448 | |
| 449 | if (AI->isArrayAllocation()) { |
| 1 | Assuming the condition is false | |
|
| |
| 450 | if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) |
| 451 | Ty = ArrayType::get(Ty, CI->getValue().getZExtValue()); |
| 452 | else |
| 453 | report_fatal_error("Coroutines cannot handle non static allocas yet"); |
| 454 | } |
| 455 | |
| 456 | return addField(Ty, AI->getAlign(), IsHeader); |
| 3 | | Calling 'FrameTypeBuilder::addField' | |
|
| 457 | } |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | void addFieldForAllocas(const Function &F, FrameDataInfo &FrameData, |
| 487 | coro::Shape &Shape); |
| 488 | |
| 489 | |
| 490 | LLVM_NODISCARD FieldIDType addField(Type *Ty, MaybeAlign FieldAlignment, |
| 491 | bool IsHeader = false, |
| 492 | bool IsSpillOfValue = false) { |
| 493 | assert(!IsFinished && "adding fields to a finished builder"); |
| 494 | assert(Ty && "must provide a type for a field"); |
| 495 | |
| 496 | |
| 497 | uint64_t FieldSize = DL.getTypeAllocSize(Ty); |
| 498 | |
| 499 | |
| 500 | |
| 501 | if (FieldSize == 0) { |
| 4 | | Assuming 'FieldSize' is not equal to 0 | |
|
| |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | auto ABIAlign = DL.getABITypeAlign(Ty); |
| 510 | Align TyAlignment = |
| 511 | (IsSpillOfValue && MaxFrameAlignment) |
| 512 | ? (*MaxFrameAlignment < ABIAlign ? *MaxFrameAlignment : ABIAlign) |
| 513 | : ABIAlign; |
| 514 | if (!FieldAlignment) { |
| |
| 515 | FieldAlignment = TyAlignment; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | uint64_t Offset; |
| 520 | if (IsHeader) { |
| 7 | | Assuming 'IsHeader' is true | |
|
| |
| 521 | Offset = alignTo(StructSize, FieldAlignment); |
| |
| 522 | StructSize = Offset + FieldSize; |
| 523 | |
| 524 | |
| 525 | } else { |
| 526 | Offset = OptimizedStructLayoutField::FlexibleOffset; |
| 527 | } |
| 528 | |
| 529 | Fields.push_back({FieldSize, Offset, Ty, 0, *FieldAlignment, TyAlignment}); |
| 530 | return Fields.size() - 1; |
| 531 | } |
| 532 | |
| 533 | |
| 534 | void finish(StructType *Ty); |
| 535 | |
| 536 | uint64_t getStructSize() const { |
| 537 | assert(IsFinished && "not yet finished!"); |
| 538 | return StructSize; |
| 539 | } |
| 540 | |
| 541 | Align getStructAlign() const { |
| 542 | assert(IsFinished && "not yet finished!"); |
| 543 | return StructAlign; |
| 544 | } |
| 545 | |
| 546 | FieldIDType getLayoutFieldIndex(FieldIDType Id) const { |
| 547 | assert(IsFinished && "not yet finished!"); |
| 548 | return Fields[Id].LayoutFieldIndex; |
| 549 | } |
| 550 | |
| 551 | Field getLayoutField(FieldIDType Id) const { |
| 552 | assert(IsFinished && "not yet finished!"); |
| 553 | return Fields[Id]; |
| 554 | } |
| 555 | }; |
| 556 | } |
| 557 | |
| 558 | void FrameDataInfo::updateLayoutIndex(FrameTypeBuilder &B) { |
| 559 | auto Updater = [&](Value *I) { |
| 560 | auto Field = B.getLayoutField(getFieldIndex(I)); |
| 561 | setFieldIndex(I, Field.LayoutFieldIndex); |
| 562 | setAlign(I, Field.Alignment.value()); |
| 563 | setOffset(I, Field.Offset); |
| 564 | }; |
| 565 | LayoutIndexUpdateStarted = true; |
| 566 | for (auto &S : Spills) |
| 567 | Updater(S.first); |
| 568 | for (const auto &A : Allocas) |
| 569 | Updater(A.Alloca); |
| 570 | LayoutIndexUpdateStarted = false; |
| 571 | } |
| 572 | |
| 573 | void FrameTypeBuilder::addFieldForAllocas(const Function &F, |
| 574 | FrameDataInfo &FrameData, |
| 575 | coro::Shape &Shape) { |
| 576 | using AllocaSetType = SmallVector<AllocaInst *, 4>; |
| 577 | SmallVector<AllocaSetType, 4> NonOverlapedAllocas; |
| 578 | |
| 579 | |
| 580 | |
| 581 | struct RTTIHelper { |
| 582 | std::function<void()> func; |
| 583 | RTTIHelper(std::function<void()> &&func) : func(func) {} |
| 584 | ~RTTIHelper() { func(); } |
| 585 | } Helper([&]() { |
| 586 | for (auto AllocaList : NonOverlapedAllocas) { |
| 587 | auto *LargestAI = *AllocaList.begin(); |
| 588 | FieldIDType Id = addFieldForAlloca(LargestAI); |
| 589 | for (auto *Alloca : AllocaList) |
| 590 | FrameData.setFieldIndex(Alloca, Id); |
| 591 | } |
| 592 | }); |
| 593 | |
| 594 | if (!Shape.ReuseFrameSlot && !EnableReuseStorageInFrame) { |
| 595 | for (const auto &A : FrameData.Allocas) { |
| 596 | AllocaInst *Alloca = A.Alloca; |
| 597 | NonOverlapedAllocas.emplace_back(AllocaSetType(1, Alloca)); |
| 598 | } |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | DenseMap<SwitchInst *, BasicBlock *> DefaultSuspendDest; |
| 615 | for (auto CoroSuspendInst : Shape.CoroSuspends) { |
| 616 | for (auto U : CoroSuspendInst->users()) { |
| 617 | if (auto *ConstSWI = dyn_cast<SwitchInst>(U)) { |
| 618 | auto *SWI = const_cast<SwitchInst *>(ConstSWI); |
| 619 | DefaultSuspendDest[SWI] = SWI->getDefaultDest(); |
| 620 | SWI->setDefaultDest(SWI->getSuccessor(1)); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | auto ExtractAllocas = [&]() { |
| 626 | AllocaSetType Allocas; |
| 627 | Allocas.reserve(FrameData.Allocas.size()); |
| 628 | for (const auto &A : FrameData.Allocas) |
| 629 | Allocas.push_back(A.Alloca); |
| 630 | return Allocas; |
| 631 | }; |
| 632 | StackLifetime StackLifetimeAnalyzer(F, ExtractAllocas(), |
| 633 | StackLifetime::LivenessType::May); |
| 634 | StackLifetimeAnalyzer.run(); |
| 635 | auto IsAllocaInferenre = [&](const AllocaInst *AI1, const AllocaInst *AI2) { |
| 636 | return StackLifetimeAnalyzer.getLiveRange(AI1).overlaps( |
| 637 | StackLifetimeAnalyzer.getLiveRange(AI2)); |
| 638 | }; |
| 639 | auto GetAllocaSize = [&](const AllocaInfo &A) { |
| 640 | Optional<TypeSize> RetSize = A.Alloca->getAllocationSizeInBits(DL); |
| 641 | assert(RetSize && "Variable Length Arrays (VLA) are not supported.\n"); |
| 642 | assert(!RetSize->isScalable() && "Scalable vectors are not yet supported"); |
| 643 | return RetSize->getFixedSize(); |
| 644 | }; |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | sort(FrameData.Allocas, [&](const auto &Iter1, const auto &Iter2) { |
| 650 | return GetAllocaSize(Iter1) > GetAllocaSize(Iter2); |
| 651 | }); |
| 652 | for (const auto &A : FrameData.Allocas) { |
| 653 | AllocaInst *Alloca = A.Alloca; |
| 654 | bool Merged = false; |
| 655 | |
| 656 | |
| 657 | |
| 658 | for (auto &AllocaSet : NonOverlapedAllocas) { |
| 659 | assert(!AllocaSet.empty() && "Processing Alloca Set is not empty.\n"); |
| 660 | bool NoInference = none_of(AllocaSet, [&](auto Iter) { |
| 661 | return IsAllocaInferenre(Alloca, Iter); |
| 662 | }); |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | bool Alignable = [&]() -> bool { |
| 670 | auto *LargestAlloca = *AllocaSet.begin(); |
| 671 | return LargestAlloca->getAlign().value() % Alloca->getAlign().value() == |
| 672 | 0; |
| 673 | }(); |
| 674 | bool CouldMerge = NoInference && Alignable; |
| 675 | if (!CouldMerge) |
| 676 | continue; |
| 677 | AllocaSet.push_back(Alloca); |
| 678 | Merged = true; |
| 679 | break; |
| 680 | } |
| 681 | if (!Merged) { |
| 682 | NonOverlapedAllocas.emplace_back(AllocaSetType(1, Alloca)); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | |
| 687 | for (auto SwitchAndDefaultDest : DefaultSuspendDest) { |
| 688 | SwitchInst *SWI = SwitchAndDefaultDest.first; |
| 689 | BasicBlock *DestBB = SwitchAndDefaultDest.second; |
| 690 | SWI->setDefaultDest(DestBB); |
| 691 | } |
| 692 | |
| 693 | LLVM_DEBUG(for (auto &AllocaSet |
| 694 | : NonOverlapedAllocas) { |
| 695 | if (AllocaSet.size() > 1) { |
| 696 | dbgs() << "In Function:" << F.getName() << "\n"; |
| 697 | dbgs() << "Find Union Set " |
| 698 | << "\n"; |
| 699 | dbgs() << "\tAllocas are \n"; |
| 700 | for (auto Alloca : AllocaSet) |
| 701 | dbgs() << "\t\t" << *Alloca << "\n"; |
| 702 | } |
| 703 | }); |
| 704 | } |
| 705 | |
| 706 | void FrameTypeBuilder::finish(StructType *Ty) { |
| 707 | assert(!IsFinished && "already finished!"); |
| 708 | |
| 709 | |
| 710 | |
| 711 | SmallVector<OptimizedStructLayoutField, 8> LayoutFields; |
| 712 | LayoutFields.reserve(Fields.size()); |
| 713 | for (auto &Field : Fields) { |
| 714 | LayoutFields.emplace_back(&Field, Field.Size, Field.Alignment, |
| 715 | Field.Offset); |
| 716 | } |
| 717 | |
| 718 | |
| 719 | auto SizeAndAlign = performOptimizedStructLayout(LayoutFields); |
| 720 | StructSize = SizeAndAlign.first; |
| 721 | StructAlign = SizeAndAlign.second; |
| 722 | |
| 723 | auto getField = [](const OptimizedStructLayoutField &LayoutField) -> Field & { |
| 724 | return *static_cast<Field *>(const_cast<void*>(LayoutField.Id)); |
| 725 | }; |
| 726 | |
| 727 | |
| 728 | |
| 729 | bool Packed = [&] { |
| 730 | for (auto &LayoutField : LayoutFields) { |
| 731 | auto &F = getField(LayoutField); |
| 732 | if (!isAligned(F.TyAlignment, LayoutField.Offset)) |
| 733 | return true; |
| 734 | } |
| 735 | return false; |
| 736 | }(); |
| 737 | |
| 738 | |
| 739 | SmallVector<Type*, 16> FieldTypes; |
| 740 | FieldTypes.reserve(LayoutFields.size() * 3 / 2); |
| 741 | uint64_t LastOffset = 0; |
| 742 | for (auto &LayoutField : LayoutFields) { |
| 743 | auto &F = getField(LayoutField); |
| 744 | |
| 745 | auto Offset = LayoutField.Offset; |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | assert(Offset >= LastOffset); |
| 751 | if (Offset != LastOffset) { |
| 752 | if (Packed || alignTo(LastOffset, F.TyAlignment) != Offset) |
| 753 | FieldTypes.push_back(ArrayType::get(Type::getInt8Ty(Context), |
| 754 | Offset - LastOffset)); |
| 755 | } |
| 756 | |
| 757 | F.Offset = Offset; |
| 758 | F.LayoutFieldIndex = FieldTypes.size(); |
| 759 | |
| 760 | FieldTypes.push_back(F.Ty); |
| 761 | LastOffset = Offset + F.Size; |
| 762 | } |
| 763 | |
| 764 | Ty->setBody(FieldTypes, Packed); |
| 765 | |
| 766 | #ifndef NDEBUG |
| 767 | |
| 768 | auto Layout = DL.getStructLayout(Ty); |
| 769 | for (auto &F : Fields) { |
| 770 | assert(Ty->getElementType(F.LayoutFieldIndex) == F.Ty); |
| 771 | assert(Layout->getElementOffset(F.LayoutFieldIndex) == F.Offset); |
| 772 | } |
| 773 | #endif |
| 774 | |
| 775 | IsFinished = true; |
| 776 | } |
| 777 | |
| 778 | static void cacheDIVar(FrameDataInfo &FrameData, |
| 779 | DenseMap<Value *, DILocalVariable *> &DIVarCache) { |
| 780 | for (auto *V : FrameData.getAllDefs()) { |
| 781 | if (DIVarCache.find(V) != DIVarCache.end()) |
| 782 | continue; |
| 783 | |
| 784 | auto DDIs = FindDbgDeclareUses(V); |
| 785 | auto *I = llvm::find_if(DDIs, [](DbgDeclareInst *DDI) { |
| 786 | return DDI->getExpression()->getNumElements() == 0; |
| 787 | }); |
| 788 | if (I != DDIs.end()) |
| 789 | DIVarCache.insert({V, (*I)->getVariable()}); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | |
| 794 | |
| 795 | static StringRef solveTypeName(Type *Ty) { |
| 796 | if (Ty->isIntegerTy()) { |
| 797 | |
| 798 | SmallString<16> Buffer; |
| 799 | raw_svector_ostream OS(Buffer); |
| 800 | OS << "__int_" << cast<IntegerType>(Ty)->getBitWidth(); |
| 801 | auto *MDName = MDString::get(Ty->getContext(), OS.str()); |
| 802 | return MDName->getString(); |
| 803 | } |
| 804 | |
| 805 | if (Ty->isFloatingPointTy()) { |
| 806 | if (Ty->isFloatTy()) |
| 807 | return "__float_"; |
| 808 | if (Ty->isDoubleTy()) |
| 809 | return "__double_"; |
| 810 | return "__floating_type_"; |
| 811 | } |
| 812 | |
| 813 | if (Ty->isPointerTy()) { |
| 814 | auto *PtrTy = cast<PointerType>(Ty); |
| 815 | Type *PointeeTy = PtrTy->getElementType(); |
| 816 | auto Name = solveTypeName(PointeeTy); |
| 817 | if (Name == "UnknownType") |
| 818 | return "PointerType"; |
| 819 | SmallString<16> Buffer; |
| 820 | Twine(Name + "_Ptr").toStringRef(Buffer); |
| 821 | auto *MDName = MDString::get(Ty->getContext(), Buffer.str()); |
| 822 | return MDName->getString(); |
| 823 | } |
| 824 | |
| 825 | if (Ty->isStructTy()) { |
| 826 | if (!cast<StructType>(Ty)->hasName()) |
| 827 | return "__LiteralStructType_"; |
| 828 | |
| 829 | auto Name = Ty->getStructName(); |
| 830 | |
| 831 | SmallString<16> Buffer(Name); |
| 832 | for_each(Buffer, [](auto &Iter) { |
| 833 | if (Iter == '.' || Iter == ':') |
| 834 | Iter = '_'; |
| 835 | }); |
| 836 | auto *MDName = MDString::get(Ty->getContext(), Buffer.str()); |
| 837 | return MDName->getString(); |
| 838 | } |
| 839 | |
| 840 | return "UnknownType"; |
| 841 | } |
| 842 | |
| 843 | static DIType *solveDIType(DIBuilder &Builder, Type *Ty, DataLayout &Layout, |
| 844 | DIScope *Scope, unsigned LineNum, |
| 845 | DenseMap<Type *, DIType *> &DITypeCache) { |
| 846 | if (DIType *DT = DITypeCache.lookup(Ty)) |
| 847 | return DT; |
| 848 | |
| 849 | StringRef Name = solveTypeName(Ty); |
| 850 | |
| 851 | DIType *RetType = nullptr; |
| 852 | |
| 853 | if (Ty->isIntegerTy()) { |
| 854 | auto BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 855 | RetType = Builder.createBasicType(Name, BitWidth, dwarf::DW_ATE_signed, |
| 856 | llvm::DINode::FlagArtificial); |
| 857 | } else if (Ty->isFloatingPointTy()) { |
| 858 | RetType = Builder.createBasicType(Name, Layout.getTypeSizeInBits(Ty), |
| 859 | dwarf::DW_ATE_float, |
| 860 | llvm::DINode::FlagArtificial); |
| 861 | } else if (Ty->isPointerTy()) { |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | RetType = Builder.createBasicType(Name, Layout.getTypeSizeInBits(Ty), |
| 870 | dwarf::DW_ATE_address, |
| 871 | llvm::DINode::FlagArtificial); |
| 872 | } else if (Ty->isStructTy()) { |
| 873 | auto *DIStruct = Builder.createStructType( |
| 874 | Scope, Name, Scope->getFile(), LineNum, Layout.getTypeSizeInBits(Ty), |
| 875 | Layout.getPrefTypeAlignment(Ty), llvm::DINode::FlagArtificial, nullptr, |
| 876 | llvm::DINodeArray()); |
| 877 | |
| 878 | auto *StructTy = cast<StructType>(Ty); |
| 879 | SmallVector<Metadata *, 16> Elements; |
| 880 | for (unsigned I = 0; I < StructTy->getNumElements(); I++) { |
| 881 | DIType *DITy = solveDIType(Builder, StructTy->getElementType(I), Layout, |
| 882 | Scope, LineNum, DITypeCache); |
| 883 | assert(DITy); |
| 884 | Elements.push_back(Builder.createMemberType( |
| 885 | Scope, DITy->getName(), Scope->getFile(), LineNum, |
| 886 | DITy->getSizeInBits(), DITy->getAlignInBits(), |
| 887 | Layout.getStructLayout(StructTy)->getElementOffsetInBits(I), |
| 888 | llvm::DINode::FlagArtificial, DITy)); |
| 889 | } |
| 890 | |
| 891 | Builder.replaceArrays(DIStruct, Builder.getOrCreateArray(Elements)); |
| 892 | |
| 893 | RetType = DIStruct; |
| 894 | } else { |
| 895 | LLVM_DEBUG(dbgs() << "Unresolved Type: " << *Ty << "\n";); |
| 896 | SmallString<32> Buffer; |
| 897 | raw_svector_ostream OS(Buffer); |
| 898 | OS << Name.str() << "_" << Layout.getTypeSizeInBits(Ty); |
| 899 | RetType = Builder.createBasicType(OS.str(), Layout.getTypeSizeInBits(Ty), |
| 900 | dwarf::DW_ATE_address, |
| 901 | llvm::DINode::FlagArtificial); |
| 902 | } |
| 903 | |
| 904 | DITypeCache.insert({Ty, RetType}); |
| 905 | return RetType; |
| 906 | } |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | static void buildFrameDebugInfo(Function &F, coro::Shape &Shape, |
| 921 | FrameDataInfo &FrameData) { |
| 922 | DISubprogram *DIS = F.getSubprogram(); |
| 923 | |
| 924 | |
| 925 | |
| 926 | if (!DIS || !DIS->getUnit() || |
| 927 | !dwarf::isCPlusPlus( |
| 928 | (dwarf::SourceLanguage)DIS->getUnit()->getSourceLanguage())) |
| 929 | return; |
| 930 | |
| 931 | assert(Shape.ABI == coro::ABI::Switch && |
| 932 | "We could only build debug infomation for C++ coroutine now.\n"); |
| 933 | |
| 934 | DIBuilder DBuilder(*F.getParent(), false); |
| 935 | |
| 936 | AllocaInst *PromiseAlloca = Shape.getPromiseAlloca(); |
| 937 | assert(PromiseAlloca && |
| 938 | "Coroutine with switch ABI should own Promise alloca"); |
| 939 | |
| 940 | TinyPtrVector<DbgDeclareInst *> DIs = FindDbgDeclareUses(PromiseAlloca); |
| 941 | if (DIs.empty()) |
| 942 | return; |
| 943 | |
| 944 | DbgDeclareInst *PromiseDDI = DIs.front(); |
| 945 | DILocalVariable *PromiseDIVariable = PromiseDDI->getVariable(); |
| 946 | DILocalScope *PromiseDIScope = PromiseDIVariable->getScope(); |
| 947 | DIFile *DFile = PromiseDIScope->getFile(); |
| 948 | DILocation *DILoc = PromiseDDI->getDebugLoc().get(); |
| 949 | unsigned LineNum = PromiseDIVariable->getLine(); |
| 950 | |
| 951 | DICompositeType *FrameDITy = DBuilder.createStructType( |
| 952 | DIS, "__coro_frame_ty", DFile, LineNum, Shape.FrameSize * 8, |
| 953 | Shape.FrameAlign.value() * 8, llvm::DINode::FlagArtificial, nullptr, |
| 954 | llvm::DINodeArray()); |
| 955 | StructType *FrameTy = Shape.FrameTy; |
| 956 | SmallVector<Metadata *, 16> Elements; |
| 957 | DataLayout Layout = F.getParent()->getDataLayout(); |
| 958 | |
| 959 | DenseMap<Value *, DILocalVariable *> DIVarCache; |
| 960 | cacheDIVar(FrameData, DIVarCache); |
| 961 | |
| 962 | unsigned ResumeIndex = coro::Shape::SwitchFieldIndex::Resume; |
| 963 | unsigned DestroyIndex = coro::Shape::SwitchFieldIndex::Destroy; |
| 964 | unsigned IndexIndex = Shape.SwitchLowering.IndexField; |
| 965 | |
| 966 | DenseMap<unsigned, StringRef> NameCache; |
| 967 | NameCache.insert({ResumeIndex, "__resume_fn"}); |
| 968 | NameCache.insert({DestroyIndex, "__destroy_fn"}); |
| 969 | NameCache.insert({IndexIndex, "__coro_index"}); |
| 970 | |
| 971 | Type *ResumeFnTy = FrameTy->getElementType(ResumeIndex), |
| 972 | *DestroyFnTy = FrameTy->getElementType(DestroyIndex), |
| 973 | *IndexTy = FrameTy->getElementType(IndexIndex); |
| 974 | |
| 975 | DenseMap<unsigned, DIType *> TyCache; |
| 976 | TyCache.insert({ResumeIndex, |
| 977 | DBuilder.createBasicType("__resume_fn", |
| 978 | Layout.getTypeSizeInBits(ResumeFnTy), |
| 979 | dwarf::DW_ATE_address)}); |
| 980 | TyCache.insert( |
| 981 | {DestroyIndex, DBuilder.createBasicType( |
| 982 | "__destroy_fn", Layout.getTypeSizeInBits(DestroyFnTy), |
| 983 | dwarf::DW_ATE_address)}); |
| 984 | |
| 985 | |
| 986 | |
| 987 | TyCache.insert({IndexIndex, DBuilder.createBasicType( |
| 988 | "__coro_index", |
| 989 | (Layout.getTypeSizeInBits(IndexTy) < 8) |
| 990 | ? 8 |
| 991 | : Layout.getTypeSizeInBits(IndexTy), |
| 992 | dwarf::DW_ATE_unsigned_char)}); |
| 993 | |
| 994 | for (auto *V : FrameData.getAllDefs()) { |
| 995 | if (DIVarCache.find(V) == DIVarCache.end()) |
| 996 | continue; |
| 997 | |
| 998 | auto Index = FrameData.getFieldIndex(V); |
| 999 | |
| 1000 | NameCache.insert({Index, DIVarCache[V]->getName()}); |
| 1001 | TyCache.insert({Index, DIVarCache[V]->getType()}); |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | DenseMap<unsigned, std::pair<unsigned, unsigned>> OffsetCache; |
| 1006 | |
| 1007 | OffsetCache.insert({ResumeIndex, {8, 0}}); |
| 1008 | OffsetCache.insert({DestroyIndex, {8, 8}}); |
| 1009 | OffsetCache.insert( |
| 1010 | {IndexIndex, |
| 1011 | {Shape.SwitchLowering.IndexAlign, Shape.SwitchLowering.IndexOffset}}); |
| 1012 | |
| 1013 | for (auto *V : FrameData.getAllDefs()) { |
| 1014 | auto Index = FrameData.getFieldIndex(V); |
| 1015 | |
| 1016 | OffsetCache.insert( |
| 1017 | {Index, {FrameData.getAlign(V), FrameData.getOffset(V)}}); |
| 1018 | } |
| 1019 | |
| 1020 | DenseMap<Type *, DIType *> DITypeCache; |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | unsigned UnknownTypeNum = 0; |
| 1026 | for (unsigned Index = 0; Index < FrameTy->getNumElements(); Index++) { |
| 1027 | if (OffsetCache.find(Index) == OffsetCache.end()) |
| 1028 | continue; |
| 1029 | |
| 1030 | std::string Name; |
| 1031 | uint64_t SizeInBits; |
| 1032 | uint32_t AlignInBits; |
| 1033 | uint64_t OffsetInBits; |
| 1034 | DIType *DITy = nullptr; |
| 1035 | |
| 1036 | Type *Ty = FrameTy->getElementType(Index); |
| 1037 | assert(Ty->isSized() && "We can't handle type which is not sized.\n"); |
| 1038 | SizeInBits = Layout.getTypeSizeInBits(Ty).getFixedSize(); |
| 1039 | AlignInBits = OffsetCache[Index].first * 8; |
| 1040 | OffsetInBits = OffsetCache[Index].second * 8; |
| 1041 | |
| 1042 | if (NameCache.find(Index) != NameCache.end()) { |
| 1043 | Name = NameCache[Index].str(); |
| 1044 | DITy = TyCache[Index]; |
| 1045 | } else { |
| 1046 | DITy = solveDIType(DBuilder, Ty, Layout, FrameDITy, LineNum, DITypeCache); |
| 1047 | assert(DITy && "SolveDIType shouldn't return nullptr.\n"); |
| 1048 | Name = DITy->getName().str(); |
| 1049 | Name += "_" + std::to_string(UnknownTypeNum); |
| 1050 | UnknownTypeNum++; |
| 1051 | } |
| 1052 | |
| 1053 | Elements.push_back(DBuilder.createMemberType( |
| 1054 | FrameDITy, Name, DFile, LineNum, SizeInBits, AlignInBits, OffsetInBits, |
| 1055 | llvm::DINode::FlagArtificial, DITy)); |
| 1056 | } |
| 1057 | |
| 1058 | DBuilder.replaceArrays(FrameDITy, DBuilder.getOrCreateArray(Elements)); |
| 1059 | |
| 1060 | auto *FrameDIVar = DBuilder.createAutoVariable(PromiseDIScope, "__coro_frame", |
| 1061 | DFile, LineNum, FrameDITy, |
| 1062 | true, DINode::FlagArtificial); |
| 1063 | assert(FrameDIVar->isValidLocationForIntrinsic(PromiseDDI->getDebugLoc())); |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | if (auto *SubProgram = dyn_cast<DISubprogram>(PromiseDIScope)) { |
| 1073 | auto RetainedNodes = SubProgram->getRetainedNodes(); |
| 1074 | SmallVector<Metadata *, 32> RetainedNodesVec(RetainedNodes.begin(), |
| 1075 | RetainedNodes.end()); |
| 1076 | RetainedNodesVec.push_back(FrameDIVar); |
| 1077 | SubProgram->replaceOperandWith( |
| 1078 | 7, (MDTuple::get(F.getContext(), RetainedNodesVec))); |
| 1079 | } |
| 1080 | |
| 1081 | DBuilder.insertDeclare(Shape.FramePtr, FrameDIVar, |
| 1082 | DBuilder.createExpression(), DILoc, |
| 1083 | Shape.FramePtr->getNextNode()); |
| 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | static StructType *buildFrameType(Function &F, coro::Shape &Shape, |
| 1095 | FrameDataInfo &FrameData) { |
| 1096 | LLVMContext &C = F.getContext(); |
| 1097 | const DataLayout &DL = F.getParent()->getDataLayout(); |
| 1098 | StructType *FrameTy = [&] { |
| 1099 | SmallString<32> Name(F.getName()); |
| 1100 | Name.append(".Frame"); |
| 1101 | return StructType::create(C, Name); |
| 1102 | }(); |
| 1103 | |
| 1104 | |
| 1105 | Optional<Align> MaxFrameAlignment; |
| 1106 | if (Shape.ABI == coro::ABI::Async) |
| 1107 | MaxFrameAlignment = Shape.AsyncLowering.getContextAlignment(); |
| 1108 | FrameTypeBuilder B(C, DL, MaxFrameAlignment); |
| 1109 | |
| 1110 | AllocaInst *PromiseAlloca = Shape.getPromiseAlloca(); |
| 1111 | Optional<FieldIDType> SwitchIndexFieldId; |
| 1112 | |
| 1113 | if (Shape.ABI == coro::ABI::Switch) { |
| 1114 | auto *FramePtrTy = FrameTy->getPointerTo(); |
| 1115 | auto *FnTy = FunctionType::get(Type::getVoidTy(C), FramePtrTy, |
| 1116 | false); |
| 1117 | auto *FnPtrTy = FnTy->getPointerTo(); |
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | (void)B.addField(FnPtrTy, None, true); |
| 1122 | (void)B.addField(FnPtrTy, None, true); |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | if (PromiseAlloca) |
| 1128 | FrameData.setFieldIndex( |
| 1129 | PromiseAlloca, B.addFieldForAlloca(PromiseAlloca, true)); |
| 1130 | |
| 1131 | |
| 1132 | |
| 1133 | unsigned IndexBits = std::max(1U, Log2_64_Ceil(Shape.CoroSuspends.size())); |
| 1134 | Type *IndexType = Type::getIntNTy(C, IndexBits); |
| 1135 | |
| 1136 | SwitchIndexFieldId = B.addField(IndexType, None); |
| 1137 | } else { |
| 1138 | assert(PromiseAlloca == nullptr && "lowering doesn't support promises"); |
| 1139 | } |
| 1140 | |
| 1141 | |
| 1142 | |
| 1143 | B.addFieldForAllocas(F, FrameData, Shape); |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | if (Shape.ABI == coro::ABI::Switch && PromiseAlloca) |
| 1149 | |
| 1150 | |
| 1151 | FrameData.Allocas.emplace_back( |
| 1152 | PromiseAlloca, DenseMap<Instruction *, llvm::Optional<APInt>>{}, false); |
| 1153 | |
| 1154 | for (auto &S : FrameData.Spills) { |
| 1155 | Type *FieldType = S.first->getType(); |
| 1156 | |
| 1157 | |
| 1158 | if (const Argument *A = dyn_cast<Argument>(S.first)) |
| 1159 | if (A->hasByValAttr()) |
| 1160 | FieldType = A->getParamByValType(); |
| 1161 | FieldIDType Id = |
| 1162 | B.addField(FieldType, None, false , true ); |
| 1163 | FrameData.setFieldIndex(S.first, Id); |
| 1164 | } |
| 1165 | |
| 1166 | B.finish(FrameTy); |
| 1167 | FrameData.updateLayoutIndex(B); |
| 1168 | Shape.FrameAlign = B.getStructAlign(); |
| 1169 | Shape.FrameSize = B.getStructSize(); |
| 1170 | |
| 1171 | switch (Shape.ABI) { |
| 1172 | case coro::ABI::Switch: { |
| 1173 | |
| 1174 | auto IndexField = B.getLayoutField(*SwitchIndexFieldId); |
| 1175 | Shape.SwitchLowering.IndexField = IndexField.LayoutFieldIndex; |
| 1176 | Shape.SwitchLowering.IndexAlign = IndexField.Alignment.value(); |
| 1177 | Shape.SwitchLowering.IndexOffset = IndexField.Offset; |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | Shape.FrameSize = alignTo(Shape.FrameSize, Shape.FrameAlign); |
| 1182 | break; |
| 1183 | } |
| 1184 | |
| 1185 | |
| 1186 | case coro::ABI::Retcon: |
| 1187 | case coro::ABI::RetconOnce: { |
| 1188 | auto Id = Shape.getRetconCoroId(); |
| 1189 | Shape.RetconLowering.IsFrameInlineInStorage |
| 1190 | = (B.getStructSize() <= Id->getStorageSize() && |
| 1191 | B.getStructAlign() <= Id->getStorageAlignment()); |
| 1192 | break; |
| 1193 | } |
| 1194 | case coro::ABI::Async: { |
| 1195 | Shape.AsyncLowering.FrameOffset = |
| 1196 | alignTo(Shape.AsyncLowering.ContextHeaderSize, Shape.FrameAlign); |
| 1197 | |
| 1198 | |
| 1199 | Shape.AsyncLowering.ContextSize = |
| 1200 | alignTo(Shape.AsyncLowering.FrameOffset + Shape.FrameSize, |
| 1201 | Shape.AsyncLowering.getContextAlignment()); |
| 1202 | if (Shape.AsyncLowering.getContextAlignment() < Shape.FrameAlign) { |
| 1203 | report_fatal_error( |
| 1204 | "The alignment requirment of frame variables cannot be higher than " |
| 1205 | "the alignment of the async function context"); |
| 1206 | } |
| 1207 | break; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | return FrameTy; |
| 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | namespace { |
| 1240 | struct AllocaUseVisitor : PtrUseVisitor<AllocaUseVisitor> { |
| 1241 | using Base = PtrUseVisitor<AllocaUseVisitor>; |
| 1242 | AllocaUseVisitor(const DataLayout &DL, const DominatorTree &DT, |
| 1243 | const CoroBeginInst &CB, const SuspendCrossingInfo &Checker) |
| 1244 | : PtrUseVisitor(DL), DT(DT), CoroBegin(CB), Checker(Checker) {} |
| 1245 | |
| 1246 | void visit(Instruction &I) { |
| 1247 | Users.insert(&I); |
| 1248 | Base::visit(I); |
| 1249 | |
| 1250 | |
| 1251 | if (PI.isEscaped() && !DT.dominates(&CoroBegin, PI.getEscapingInst())) { |
| 1252 | MayWriteBeforeCoroBegin = true; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | |
| 1257 | void visit(Instruction *I) { return visit(*I); } |
| 1258 | |
| 1259 | void visitPHINode(PHINode &I) { |
| 1260 | enqueueUsers(I); |
| 1261 | handleAlias(I); |
| 1262 | } |
| 1263 | |
| 1264 | void visitSelectInst(SelectInst &I) { |
| 1265 | enqueueUsers(I); |
| 1266 | handleAlias(I); |
| 1267 | } |
| 1268 | |
| 1269 | void visitStoreInst(StoreInst &SI) { |
| 1270 | |
| 1271 | |
| 1272 | handleMayWrite(SI); |
| 1273 | |
| 1274 | if (SI.getValueOperand() != U->get()) |
| 1275 | return; |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | auto IsSimpleStoreThenLoad = [&]() { |
| 1288 | auto *AI = dyn_cast<AllocaInst>(SI.getPointerOperand()); |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | if (!AI) |
| 1293 | return false; |
| 1294 | |
| 1295 | SmallVector<Instruction *, 4> StoreAliases = {AI}; |
| 1296 | while (!StoreAliases.empty()) { |
| 1297 | Instruction *I = StoreAliases.pop_back_val(); |
| 1298 | for (User *U : I->users()) { |
| 1299 | |
| 1300 | |
| 1301 | if (auto *LI = dyn_cast<LoadInst>(U)) { |
| 1302 | enqueueUsers(*LI); |
| 1303 | handleAlias(*LI); |
| 1304 | continue; |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | if (auto *S = dyn_cast<StoreInst>(U)) |
| 1309 | if (S->getPointerOperand() == I) |
| 1310 | continue; |
| 1311 | if (auto *II = dyn_cast<IntrinsicInst>(U)) |
| 1312 | if (II->isLifetimeStartOrEnd()) |
| 1313 | continue; |
| 1314 | |
| 1315 | |
| 1316 | if (auto *BI = dyn_cast<BitCastInst>(U)) { |
| 1317 | StoreAliases.push_back(BI); |
| 1318 | continue; |
| 1319 | } |
| 1320 | return false; |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | return true; |
| 1325 | }; |
| 1326 | |
| 1327 | if (!IsSimpleStoreThenLoad()) |
| 1328 | PI.setEscaped(&SI); |
| 1329 | } |
| 1330 | |
| 1331 | |
| 1332 | void visitMemIntrinsic(MemIntrinsic &MI) { handleMayWrite(MI); } |
| 1333 | |
| 1334 | void visitBitCastInst(BitCastInst &BC) { |
| 1335 | Base::visitBitCastInst(BC); |
| 1336 | handleAlias(BC); |
| 1337 | } |
| 1338 | |
| 1339 | void visitAddrSpaceCastInst(AddrSpaceCastInst &ASC) { |
| 1340 | Base::visitAddrSpaceCastInst(ASC); |
| 1341 | handleAlias(ASC); |
| 1342 | } |
| 1343 | |
| 1344 | void visitGetElementPtrInst(GetElementPtrInst &GEPI) { |
| 1345 | |
| 1346 | Base::visitGetElementPtrInst(GEPI); |
| 1347 | handleAlias(GEPI); |
| 1348 | } |
| 1349 | |
| 1350 | void visitIntrinsicInst(IntrinsicInst &II) { |
| 1351 | if (II.getIntrinsicID() != Intrinsic::lifetime_start) |
| 1352 | return Base::visitIntrinsicInst(II); |
| 1353 | LifetimeStarts.insert(&II); |
| 1354 | } |
| 1355 | |
| 1356 | void visitCallBase(CallBase &CB) { |
| 1357 | for (unsigned Op = 0, OpCount = CB.getNumArgOperands(); Op < OpCount; ++Op) |
| 1358 | if (U->get() == CB.getArgOperand(Op) && !CB.doesNotCapture(Op)) |
| 1359 | PI.setEscaped(&CB); |
| 1360 | handleMayWrite(CB); |
| 1361 | } |
| 1362 | |
| 1363 | bool getShouldLiveOnFrame() const { |
| 1364 | if (!ShouldLiveOnFrame) |
| 1365 | ShouldLiveOnFrame = computeShouldLiveOnFrame(); |
| 1366 | return ShouldLiveOnFrame.getValue(); |
| 1367 | } |
| 1368 | |
| 1369 | bool getMayWriteBeforeCoroBegin() const { return MayWriteBeforeCoroBegin; } |
| 1370 | |
| 1371 | DenseMap<Instruction *, llvm::Optional<APInt>> getAliasesCopy() const { |
| 1372 | assert(getShouldLiveOnFrame() && "This method should only be called if the " |
| 1373 | "alloca needs to live on the frame."); |
| 1374 | for (const auto &P : AliasOffetMap) |
| 1375 | if (!P.second) |
| 1376 | report_fatal_error("Unable to handle an alias with unknown offset " |
| 1377 | "created before CoroBegin."); |
| 1378 | return AliasOffetMap; |
| 1379 | } |
| 1380 | |
| 1381 | private: |
| 1382 | const DominatorTree &DT; |
| 1383 | const CoroBeginInst &CoroBegin; |
| 1384 | const SuspendCrossingInfo &Checker; |
| 1385 | |
| 1386 | |
| 1387 | |
| 1388 | DenseMap<Instruction *, llvm::Optional<APInt>> AliasOffetMap{}; |
| 1389 | SmallPtrSet<Instruction *, 4> Users{}; |
| 1390 | SmallPtrSet<IntrinsicInst *, 2> LifetimeStarts{}; |
| 1391 | bool MayWriteBeforeCoroBegin{false}; |
| 1392 | |
| 1393 | mutable llvm::Optional<bool> ShouldLiveOnFrame{}; |
| 1394 | |
| 1395 | bool computeShouldLiveOnFrame() const { |
| 1396 | |
| 1397 | |
| 1398 | |
| 1399 | |
| 1400 | if (!LifetimeStarts.empty()) { |
| 1401 | for (auto *I : Users) |
| 1402 | for (auto *S : LifetimeStarts) |
| 1403 | if (Checker.isDefinitionAcrossSuspend(*S, I)) |
| 1404 | return true; |
| 1405 | return false; |
| 1406 | } |
| 1407 | |
| 1408 | |
| 1409 | |
| 1410 | |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | |
| 1416 | |
| 1417 | if (PI.isEscaped()) |
| 1418 | return true; |
| 1419 | |
| 1420 | for (auto *U1 : Users) |
| 1421 | for (auto *U2 : Users) |
| 1422 | if (Checker.isDefinitionAcrossSuspend(*U1, U2)) |
| 1423 | return true; |
| 1424 | |
| 1425 | return false; |
| 1426 | } |
| 1427 | |
| 1428 | void handleMayWrite(const Instruction &I) { |
| 1429 | if (!DT.dominates(&CoroBegin, &I)) |
| 1430 | MayWriteBeforeCoroBegin = true; |
| 1431 | } |
| 1432 | |
| 1433 | bool usedAfterCoroBegin(Instruction &I) { |
| 1434 | for (auto &U : I.uses()) |
| 1435 | if (DT.dominates(&CoroBegin, U)) |
| 1436 | return true; |
| 1437 | return false; |
| 1438 | } |
| 1439 | |
| 1440 | void handleAlias(Instruction &I) { |
| 1441 | |
| 1442 | |
| 1443 | |
| 1444 | if (DT.dominates(&CoroBegin, &I) || !usedAfterCoroBegin(I)) |
| 1445 | return; |
| 1446 | |
| 1447 | if (!IsOffsetKnown) { |
| 1448 | AliasOffetMap[&I].reset(); |
| 1449 | } else { |
| 1450 | auto Itr = AliasOffetMap.find(&I); |
| 1451 | if (Itr == AliasOffetMap.end()) { |
| 1452 | AliasOffetMap[&I] = Offset; |
| 1453 | } else if (Itr->second.hasValue() && Itr->second.getValue() != Offset) { |
| 1454 | |
| 1455 | |
| 1456 | AliasOffetMap[&I].reset(); |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | }; |
| 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | static Instruction *splitBeforeCatchSwitch(CatchSwitchInst *CatchSwitch) { |
| 1473 | BasicBlock *CurrentBlock = CatchSwitch->getParent(); |
| 1474 | BasicBlock *NewBlock = CurrentBlock->splitBasicBlock(CatchSwitch); |
| 1475 | CurrentBlock->getTerminator()->eraseFromParent(); |
| 1476 | |
| 1477 | auto *CleanupPad = |
| 1478 | CleanupPadInst::Create(CatchSwitch->getParentPad(), {}, "", CurrentBlock); |
| 1479 | auto *CleanupRet = |
| 1480 | CleanupReturnInst::Create(CleanupPad, NewBlock, CurrentBlock); |
| 1481 | return CleanupRet; |
| 1482 | } |
| 1483 | |
| 1484 | static void createFramePtr(coro::Shape &Shape) { |
| 1485 | auto *CB = Shape.CoroBegin; |
| 1486 | IRBuilder<> Builder(CB->getNextNode()); |
| 1487 | StructType *FrameTy = Shape.FrameTy; |
| 1488 | PointerType *FramePtrTy = FrameTy->getPointerTo(); |
| 1489 | Shape.FramePtr = |
| 1490 | cast<Instruction>(Builder.CreateBitCast(CB, FramePtrTy, "FramePtr")); |
| 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | |
| 1495 | |
| 1496 | |
| 1497 | |
| 1498 | |
| 1499 | |
| 1500 | |
| 1501 | |
| 1502 | |
| 1503 | |
| 1504 | |
| 1505 | |
| 1506 | |
| 1507 | |
| 1508 | |
| 1509 | |
| 1510 | |
| 1511 | |
| 1512 | |
| 1513 | |
| 1514 | |
| 1515 | static Instruction *insertSpills(const FrameDataInfo &FrameData, |
| 1516 | coro::Shape &Shape) { |
| 1517 | auto *CB = Shape.CoroBegin; |
| 1518 | LLVMContext &C = CB->getContext(); |
| 1519 | IRBuilder<> Builder(C); |
| 1520 | StructType *FrameTy = Shape.FrameTy; |
| 1521 | Instruction *FramePtr = Shape.FramePtr; |
| 1522 | DominatorTree DT(*CB->getFunction()); |
| 1523 | SmallDenseMap<llvm::Value *, llvm::AllocaInst *, 4> DbgPtrAllocaCache; |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | auto GetFramePointer = [&](Value *Orig) -> Value * { |
| 1529 | FieldIDType Index = FrameData.getFieldIndex(Orig); |
| 1530 | SmallVector<Value *, 3> Indices = { |
| 1531 | ConstantInt::get(Type::getInt32Ty(C), 0), |
| 1532 | ConstantInt::get(Type::getInt32Ty(C), Index), |
| 1533 | }; |
| 1534 | |
| 1535 | if (auto *AI = dyn_cast<AllocaInst>(Orig)) { |
| 1536 | if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
| 1537 | auto Count = CI->getValue().getZExtValue(); |
| 1538 | if (Count > 1) { |
| 1539 | Indices.push_back(ConstantInt::get(Type::getInt32Ty(C), 0)); |
| 1540 | } |
| 1541 | } else { |
| 1542 | report_fatal_error("Coroutines cannot handle non static allocas yet"); |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | auto GEP = cast<GetElementPtrInst>( |
| 1547 | Builder.CreateInBoundsGEP(FrameTy, FramePtr, Indices)); |
| 1548 | if (isa<AllocaInst>(Orig)) { |
| 1549 | |
| 1550 | |
| 1551 | |
| 1552 | |
| 1553 | |
| 1554 | |
| 1555 | |
| 1556 | if (GEP->getResultElementType() != Orig->getType()) |
| 1557 | return Builder.CreateBitCast(GEP, Orig->getType(), |
| 1558 | Orig->getName() + Twine(".cast")); |
| 1559 | } |
| 1560 | return GEP; |
| 1561 | }; |
| 1562 | |
| 1563 | for (auto const &E : FrameData.Spills) { |
| 1564 | Value *Def = E.first; |
| 1565 | auto SpillAlignment = Align(FrameData.getAlign(Def)); |
| 1566 | |
| 1567 | |
| 1568 | Instruction *InsertPt = nullptr; |
| 1569 | bool NeedToCopyArgPtrValue = false; |
| 1570 | if (auto *Arg = dyn_cast<Argument>(Def)) { |
| 1571 | |
| 1572 | |
| 1573 | |
| 1574 | InsertPt = FramePtr->getNextNode(); |
| 1575 | |
| 1576 | |
| 1577 | |
| 1578 | Arg->getParent()->removeParamAttr(Arg->getArgNo(), Attribute::NoCapture); |
| 1579 | |
| 1580 | if (Arg->hasByValAttr()) |
| 1581 | NeedToCopyArgPtrValue = true; |
| 1582 | |
| 1583 | } else if (auto *CSI = dyn_cast<AnyCoroSuspendInst>(Def)) { |
| 1584 | |
| 1585 | |
| 1586 | InsertPt = CSI->getParent()->getSingleSuccessor()->getFirstNonPHI(); |
| 1587 | } else { |
| 1588 | auto *I = cast<Instruction>(Def); |
| 1589 | if (!DT.dominates(CB, I)) { |
| 1590 | |
| 1591 | |
| 1592 | InsertPt = FramePtr->getNextNode(); |
| 1593 | } else if (auto *II = dyn_cast<InvokeInst>(I)) { |
| 1594 | |
| 1595 | |
| 1596 | auto *NewBB = SplitEdge(II->getParent(), II->getNormalDest()); |
| 1597 | InsertPt = NewBB->getTerminator(); |
| 1598 | } else if (isa<PHINode>(I)) { |
| 1599 | |
| 1600 | BasicBlock *DefBlock = I->getParent(); |
| 1601 | if (auto *CSI = dyn_cast<CatchSwitchInst>(DefBlock->getTerminator())) |
| 1602 | InsertPt = splitBeforeCatchSwitch(CSI); |
| 1603 | else |
| 1604 | InsertPt = &*DefBlock->getFirstInsertionPt(); |
| 1605 | } else { |
| 1606 | assert(!I->isTerminator() && "unexpected terminator"); |
| 1607 | |
| 1608 | |
| 1609 | InsertPt = I->getNextNode(); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | auto Index = FrameData.getFieldIndex(Def); |
| 1614 | Builder.SetInsertPoint(InsertPt); |
| 1615 | auto *G = Builder.CreateConstInBoundsGEP2_32( |
| 1616 | FrameTy, FramePtr, 0, Index, Def->getName() + Twine(".spill.addr")); |
| 1617 | if (NeedToCopyArgPtrValue) { |
| 1618 | |
| 1619 | |
| 1620 | auto *Value = |
| 1621 | Builder.CreateLoad(Def->getType()->getPointerElementType(), Def); |
| 1622 | Builder.CreateAlignedStore(Value, G, SpillAlignment); |
| 1623 | } else { |
| 1624 | Builder.CreateAlignedStore(Def, G, SpillAlignment); |
| 1625 | } |
| 1626 | |
| 1627 | BasicBlock *CurrentBlock = nullptr; |
| 1628 | Value *CurrentReload = nullptr; |
| 1629 | for (auto *U : E.second) { |
| 1630 | |
| 1631 | |
| 1632 | |
| 1633 | if (CurrentBlock != U->getParent()) { |
| 1634 | CurrentBlock = U->getParent(); |
| 1635 | Builder.SetInsertPoint(&*CurrentBlock->getFirstInsertionPt()); |
| 1636 | |
| 1637 | auto *GEP = GetFramePointer(E.first); |
| 1638 | GEP->setName(E.first->getName() + Twine(".reload.addr")); |
| 1639 | if (NeedToCopyArgPtrValue) |
| 1640 | CurrentReload = GEP; |
| 1641 | else |
| 1642 | CurrentReload = Builder.CreateAlignedLoad( |
| 1643 | FrameTy->getElementType(FrameData.getFieldIndex(E.first)), GEP, |
| 1644 | SpillAlignment, E.first->getName() + Twine(".reload")); |
| 1645 | |
| 1646 | TinyPtrVector<DbgDeclareInst *> DIs = FindDbgDeclareUses(Def); |
| 1647 | for (DbgDeclareInst *DDI : DIs) { |
| 1648 | bool AllowUnresolved = false; |
| 1649 | |
| 1650 | |
| 1651 | |
| 1652 | DIBuilder(*CurrentBlock->getParent()->getParent(), AllowUnresolved) |
| 1653 | .insertDeclare(CurrentReload, DDI->getVariable(), |
| 1654 | DDI->getExpression(), DDI->getDebugLoc(), |
| 1655 | &*Builder.GetInsertPoint()); |
| 1656 | |
| 1657 | |
| 1658 | coro::salvageDebugInfo(DbgPtrAllocaCache, DDI, Shape.ReuseFrameSlot); |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | |
| 1663 | |
| 1664 | |
| 1665 | if (auto *PN = dyn_cast<PHINode>(U)) { |
| 1666 | assert(PN->getNumIncomingValues() == 1 && |
| 1667 | "unexpected number of incoming " |
| 1668 | "values in the PHINode"); |
| 1669 | PN->replaceAllUsesWith(CurrentReload); |
| 1670 | PN->eraseFromParent(); |
| 1671 | continue; |
| 1672 | } |
| 1673 | |
| 1674 | |
| 1675 | |
| 1676 | U->replaceUsesOfWith(Def, CurrentReload); |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | BasicBlock *FramePtrBB = FramePtr->getParent(); |
| 1681 | |
| 1682 | auto SpillBlock = |
| 1683 | FramePtrBB->splitBasicBlock(FramePtr->getNextNode(), "AllocaSpillBB"); |
| 1684 | SpillBlock->splitBasicBlock(&SpillBlock->front(), "PostSpill"); |
| 1685 | Shape.AllocaSpillBlock = SpillBlock; |
| 1686 | |
| 1687 | |
| 1688 | if (Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce || |
| 1689 | Shape.ABI == coro::ABI::Async) { |
| 1690 | |
| 1691 | Builder.SetInsertPoint(&SpillBlock->front()); |
| 1692 | for (const auto &P : FrameData.Allocas) { |
| 1693 | AllocaInst *Alloca = P.Alloca; |
| 1694 | auto *G = GetFramePointer(Alloca); |
| 1695 | |
| 1696 | |
| 1697 | |
| 1698 | G->takeName(Alloca); |
| 1699 | Alloca->replaceAllUsesWith(G); |
| 1700 | Alloca->eraseFromParent(); |
| 1701 | } |
| 1702 | return FramePtr; |
| 1703 | } |
| 1704 | |
| 1705 | |
| 1706 | |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | Builder.SetInsertPoint(&Shape.AllocaSpillBlock->front()); |
| 1711 | SmallVector<Instruction *, 4> UsersToUpdate; |
| 1712 | for (const auto &A : FrameData.Allocas) { |
| 1713 | AllocaInst *Alloca = A.Alloca; |
| 1714 | UsersToUpdate.clear(); |
| 1715 | for (User *U : Alloca->users()) { |
| 1716 | auto *I = cast<Instruction>(U); |
| 1717 | if (DT.dominates(CB, I)) |
| 1718 | UsersToUpdate.push_back(I); |
| 1719 | } |
| 1720 | if (UsersToUpdate.empty()) |
| 1721 | continue; |
| 1722 | auto *G = GetFramePointer(Alloca); |
| 1723 | G->setName(Alloca->getName() + Twine(".reload.addr")); |
| 1724 | |
| 1725 | SmallVector<DbgVariableIntrinsic *, 4> DIs; |
| 1726 | findDbgUsers(DIs, Alloca); |
| 1727 | for (auto *DVI : DIs) |
| 1728 | DVI->replaceUsesOfWith(Alloca, G); |
| 1729 | |
| 1730 | for (Instruction *I : UsersToUpdate) |
| 1731 | I->replaceUsesOfWith(Alloca, G); |
| 1732 | } |
| 1733 | Builder.SetInsertPoint(FramePtr->getNextNode()); |
| 1734 | for (const auto &A : FrameData.Allocas) { |
| 1735 | AllocaInst *Alloca = A.Alloca; |
| 1736 | if (A.MayWriteBeforeCoroBegin) { |
| 1737 | |
| 1738 | if (Alloca->isArrayAllocation()) |
| 1739 | report_fatal_error( |
| 1740 | "Coroutines cannot handle copying of array allocas yet"); |
| 1741 | |
| 1742 | auto *G = GetFramePointer(Alloca); |
| 1743 | auto *Value = Builder.CreateLoad(Alloca->getAllocatedType(), Alloca); |
| 1744 | Builder.CreateStore(Value, G); |
| 1745 | } |
| 1746 | |
| 1747 | |
| 1748 | |
| 1749 | for (const auto &Alias : A.Aliases) { |
| 1750 | auto *FramePtr = GetFramePointer(Alloca); |
| 1751 | auto *FramePtrRaw = |
| 1752 | Builder.CreateBitCast(FramePtr, Type::getInt8PtrTy(C)); |
| 1753 | auto *AliasPtr = Builder.CreateGEP( |
| 1754 | Type::getInt8Ty(C), FramePtrRaw, |
| 1755 | ConstantInt::get(Type::getInt64Ty(C), Alias.second.getValue())); |
| 1756 | auto *AliasPtrTyped = |
| 1757 | Builder.CreateBitCast(AliasPtr, Alias.first->getType()); |
| 1758 | Alias.first->replaceUsesWithIf( |
| 1759 | AliasPtrTyped, [&](Use &U) { return DT.dominates(CB, U); }); |
| 1760 | } |
| 1761 | } |
| 1762 | return FramePtr; |
| 1763 | } |
| 1764 | |
| 1765 | |
| 1766 | |
| 1767 | static void movePHIValuesToInsertedBlock(BasicBlock *SuccBB, |
| 1768 | BasicBlock *InsertedBB, |
| 1769 | BasicBlock *PredBB, |
| 1770 | PHINode *UntilPHI = nullptr) { |
| 1771 | auto *PN = cast<PHINode>(&SuccBB->front()); |
| 1772 | do { |
| 1773 | int Index = PN->getBasicBlockIndex(InsertedBB); |
| 1774 | Value *V = PN->getIncomingValue(Index); |
| 1775 | PHINode *InputV = PHINode::Create( |
| 1776 | V->getType(), 1, V->getName() + Twine(".") + SuccBB->getName(), |
| 1777 | &InsertedBB->front()); |
| 1778 | InputV->addIncoming(V, PredBB); |
| 1779 | PN->setIncomingValue(Index, InputV); |
| 1780 | PN = dyn_cast<PHINode>(PN->getNextNode()); |
| 1781 | } while (PN != UntilPHI); |
| 1782 | } |
| 1783 | |
| 1784 | |
| 1785 | static void rewritePHIsForCleanupPad(BasicBlock *CleanupPadBB, |
| 1786 | CleanupPadInst *CleanupPad) { |
| 1787 | |
| 1788 | |
| 1789 | |
| 1790 | |
| 1791 | |
| 1792 | |
| 1793 | |
| 1794 | |
| 1795 | |
| 1796 | |
| 1797 | |
| 1798 | |
| 1799 | |
| 1800 | |
| 1801 | |
| 1802 | |
| 1803 | |
| 1804 | |
| 1805 | |
| 1806 | |
| 1807 | |
| 1808 | |
| 1809 | |
| 1810 | |
| 1811 | |
| 1812 | |
| 1813 | |
| 1814 | |
| 1815 | auto *UnreachBB = BasicBlock::Create( |
| 1816 | CleanupPadBB->getContext(), "unreachable", CleanupPadBB->getParent()); |
| 1817 | IRBuilder<> Builder(UnreachBB); |
| 1818 | Builder.CreateUnreachable(); |
| 1819 | |
| 1820 | |
| 1821 | auto *NewCleanupPadBB = |
| 1822 | BasicBlock::Create(CleanupPadBB->getContext(), |
| 1823 | CleanupPadBB->getName() + Twine(".corodispatch"), |
| 1824 | CleanupPadBB->getParent(), CleanupPadBB); |
| 1825 | Builder.SetInsertPoint(NewCleanupPadBB); |
| 1826 | auto *SwitchType = Builder.getInt8Ty(); |
| 1827 | auto *SetDispatchValuePN = |
| 1828 | Builder.CreatePHI(SwitchType, pred_size(CleanupPadBB)); |
| 1829 | CleanupPad->removeFromParent(); |
| 1830 | CleanupPad->insertAfter(SetDispatchValuePN); |
| 1831 | auto *SwitchOnDispatch = Builder.CreateSwitch(SetDispatchValuePN, UnreachBB, |
| 1832 | pred_size(CleanupPadBB)); |
| 1833 | |
| 1834 | int SwitchIndex = 0; |
| 1835 | SmallVector<BasicBlock *, 8> Preds(predecessors(CleanupPadBB)); |
| 1836 | for (BasicBlock *Pred : Preds) { |
| 1837 | |
| 1838 | auto *CaseBB = BasicBlock::Create(CleanupPadBB->getContext(), |
| 1839 | CleanupPadBB->getName() + |
| 1840 | Twine(".from.") + Pred->getName(), |
| 1841 | CleanupPadBB->getParent(), CleanupPadBB); |
| 1842 | updatePhiNodes(CleanupPadBB, Pred, CaseBB); |
| 1843 | CaseBB->setName(CleanupPadBB->getName() + Twine(".from.") + |
| 1844 | Pred->getName()); |
| 1845 | Builder.SetInsertPoint(CaseBB); |
| 1846 | Builder.CreateBr(CleanupPadBB); |
| 1847 | movePHIValuesToInsertedBlock(CleanupPadBB, CaseBB, NewCleanupPadBB); |
| 1848 | |
| 1849 | |
| 1850 | setUnwindEdgeTo(Pred->getTerminator(), NewCleanupPadBB); |
| 1851 | |
| 1852 | |
| 1853 | auto *SwitchConstant = ConstantInt::get(SwitchType, SwitchIndex); |
| 1854 | SetDispatchValuePN->addIncoming(SwitchConstant, Pred); |
| 1855 | SwitchOnDispatch->addCase(SwitchConstant, CaseBB); |
| 1856 | SwitchIndex++; |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | static void cleanupSinglePredPHIs(Function &F) { |
| 1861 | SmallVector<PHINode *, 32> Worklist; |
| 1862 | for (auto &BB : F) { |
| 1863 | for (auto &Phi : BB.phis()) { |
| 1864 | if (Phi.getNumIncomingValues() == 1) { |
| 1865 | Worklist.push_back(&Phi); |
| 1866 | } else |
| 1867 | break; |
| 1868 | } |
| 1869 | } |
| 1870 | while (!Worklist.empty()) { |
| 1871 | auto *Phi = Worklist.back(); |
| 1872 | Worklist.pop_back(); |
| 1873 | auto *OriginalValue = Phi->getIncomingValue(0); |
| 1874 | Phi->replaceAllUsesWith(OriginalValue); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | static void rewritePHIs(BasicBlock &BB) { |
| 1879 | |
| 1880 | |
| 1881 | |
| 1882 | |
| 1883 | |
| 1884 | |
| 1885 | |
| 1886 | |
| 1887 | |
| 1888 | |
| 1889 | |
| 1890 | |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | |
| 1895 | |
| 1896 | |
| 1897 | |
| 1898 | |
| 1899 | |
| 1900 | |
| 1901 | |
| 1902 | if (auto *CleanupPad = |
| 1903 | dyn_cast_or_null<CleanupPadInst>(BB.getFirstNonPHI())) { |
| 1904 | SmallVector<BasicBlock *, 8> Preds(predecessors(&BB)); |
| 1905 | for (BasicBlock *Pred : Preds) { |
| 1906 | if (CatchSwitchInst *CS = |
| 1907 | dyn_cast<CatchSwitchInst>(Pred->getTerminator())) { |
| 1908 | |
| 1909 | |
| 1910 | assert(CS->getUnwindDest() == &BB); |
| 1911 | (void)CS; |
| 1912 | rewritePHIsForCleanupPad(&BB, CleanupPad); |
| 1913 | return; |
| 1914 | } |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | LandingPadInst *LandingPad = nullptr; |
| 1919 | PHINode *ReplPHI = nullptr; |
| 1920 | if ((LandingPad = dyn_cast_or_null<LandingPadInst>(BB.getFirstNonPHI()))) { |
| 1921 | |
| 1922 | |
| 1923 | |
| 1924 | ReplPHI = PHINode::Create(LandingPad->getType(), 1, "", LandingPad); |
| 1925 | ReplPHI->takeName(LandingPad); |
| 1926 | LandingPad->replaceAllUsesWith(ReplPHI); |
| 1927 | |
| 1928 | |
| 1929 | } |
| 1930 | |
| 1931 | SmallVector<BasicBlock *, 8> Preds(predecessors(&BB)); |
| 1932 | for (BasicBlock *Pred : Preds) { |
| 1933 | auto *IncomingBB = ehAwareSplitEdge(Pred, &BB, LandingPad, ReplPHI); |
| 1934 | IncomingBB->setName(BB.getName() + Twine(".from.") + Pred->getName()); |
| 1935 | |
| 1936 | |
| 1937 | |
| 1938 | movePHIValuesToInsertedBlock(&BB, IncomingBB, Pred, ReplPHI); |
| 1939 | } |
| 1940 | |
| 1941 | if (LandingPad) { |
| 1942 | |
| 1943 | |
| 1944 | LandingPad->eraseFromParent(); |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | static void rewritePHIs(Function &F) { |
| 1949 | SmallVector<BasicBlock *, 8> WorkList; |
| 1950 | |
| 1951 | for (BasicBlock &BB : F) |
| 1952 | if (auto *PN = dyn_cast<PHINode>(&BB.front())) |
| 1953 | if (PN->getNumIncomingValues() > 1) |
| 1954 | WorkList.push_back(&BB); |
| 1955 | |
| 1956 | for (BasicBlock *BB : WorkList) |
| 1957 | rewritePHIs(*BB); |
| 1958 | } |
| 1959 | |
| 1960 | |
| 1961 | |
| 1962 | static bool materializable(Instruction &V) { |
| 1963 | return isa<CastInst>(&V) || isa<GetElementPtrInst>(&V) || |
| 1964 | isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<SelectInst>(&V); |
| 1965 | } |
| 1966 | |
| 1967 | |
| 1968 | |
| 1969 | static bool isCoroutineStructureIntrinsic(Instruction &I) { |
| 1970 | return isa<CoroIdInst>(&I) || isa<CoroSaveInst>(&I) || |
| 1971 | isa<CoroSuspendInst>(&I); |
| 1972 | } |
| 1973 | |
| 1974 | |
| 1975 | |
| 1976 | static void rewriteMaterializableInstructions(IRBuilder<> &IRB, |
| 1977 | const SpillInfo &Spills) { |
| 1978 | for (const auto &E : Spills) { |
| 1979 | Value *Def = E.first; |
| 1980 | BasicBlock *CurrentBlock = nullptr; |
| 1981 | Instruction *CurrentMaterialization = nullptr; |
| 1982 | for (Instruction *U : E.second) { |
| 1983 | |
| 1984 | if (CurrentBlock != U->getParent()) { |
| 1985 | |
| 1986 | bool IsInCoroSuspendBlock = isa<AnyCoroSuspendInst>(U); |
| 1987 | CurrentBlock = IsInCoroSuspendBlock |
| 1988 | ? U->getParent()->getSinglePredecessor() |
| 1989 | : U->getParent(); |
| 1990 | CurrentMaterialization = cast<Instruction>(Def)->clone(); |
| 1991 | CurrentMaterialization->setName(Def->getName()); |
| 1992 | CurrentMaterialization->insertBefore( |
| 1993 | IsInCoroSuspendBlock ? CurrentBlock->getTerminator() |
| 1994 | : &*CurrentBlock->getFirstInsertionPt()); |
| 1995 | } |
| 1996 | if (auto *PN = dyn_cast<PHINode>(U)) { |
| 1997 | assert(PN->getNumIncomingValues() == 1 && |
| 1998 | "unexpected number of incoming " |
| 1999 | "values in the PHINode"); |
| 2000 | PN->replaceAllUsesWith(CurrentMaterialization); |
| 2001 | PN->eraseFromParent(); |
| 2002 | continue; |
| 2003 | } |
| 2004 | |
| 2005 | |
| 2006 | U->replaceUsesOfWith(Def, CurrentMaterialization); |
| 2007 | } |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | |
| 2012 | |
| 2013 | static BasicBlock *splitBlockIfNotFirst(Instruction *I, const Twine &Name) { |
| 2014 | auto *BB = I->getParent(); |
| 2015 | if (&BB->front() == I) { |
| 2016 | if (BB->getSinglePredecessor()) { |
| 2017 | BB->setName(Name); |
| 2018 | return BB; |
| 2019 | } |
| 2020 | } |
| 2021 | return BB->splitBasicBlock(I, Name); |
| 2022 | } |
| 2023 | |
| 2024 | |
| 2025 | |
| 2026 | static void splitAround(Instruction *I, const Twine &Name) { |
| 2027 | splitBlockIfNotFirst(I, Name); |
| 2028 | splitBlockIfNotFirst(I->getNextNode(), "After" + Name); |
| 2029 | } |
| 2030 | |
| 2031 | static bool isSuspendBlock(BasicBlock *BB) { |
| 2032 | return isa<AnyCoroSuspendInst>(BB->front()); |
| 2033 | } |
| 2034 | |
| 2035 | typedef SmallPtrSet<BasicBlock*, 8> VisitedBlocksSet; |
| 2036 | |
| 2037 | |
| 2038 | |
| 2039 | static bool isSuspendReachableFrom(BasicBlock *From, |
| 2040 | VisitedBlocksSet &VisitedOrFreeBBs) { |
| 2041 | |
| 2042 | |
| 2043 | |
| 2044 | if (!VisitedOrFreeBBs.insert(From).second) |
| 2045 | return false; |
| 2046 | |
| 2047 | |
| 2048 | if (isSuspendBlock(From)) |
| 2049 | return true; |
| 2050 | |
| 2051 | |
| 2052 | for (auto Succ : successors(From)) { |
| 2053 | if (isSuspendReachableFrom(Succ, VisitedOrFreeBBs)) |
| 2054 | return true; |
| 2055 | } |
| 2056 | |
| 2057 | return false; |
| 2058 | } |
| 2059 | |
| 2060 | |
| 2061 | |
| 2062 | static bool isLocalAlloca(CoroAllocaAllocInst *AI) { |
| 2063 | |
| 2064 | |
| 2065 | VisitedBlocksSet VisitedOrFreeBBs; |
| 2066 | for (auto User : AI->users()) { |
| 2067 | if (auto FI = dyn_cast<CoroAllocaFreeInst>(User)) |
| 2068 | VisitedOrFreeBBs.insert(FI->getParent()); |
| 2069 | } |
| 2070 | |
| 2071 | return !isSuspendReachableFrom(AI->getParent(), VisitedOrFreeBBs); |
| 2072 | } |
| 2073 | |
| 2074 | |
| 2075 | |
| 2076 | static bool willLeaveFunctionImmediatelyAfter(BasicBlock *BB, |
| 2077 | unsigned depth = 3) { |
| 2078 | |
| 2079 | |
| 2080 | if (depth == 0) return false; |
| 2081 | |
| 2082 | |
| 2083 | if (isSuspendBlock(BB)) return true; |
| 2084 | |
| 2085 | |
| 2086 | for (auto Succ : successors(BB)) { |
| 2087 | if (!willLeaveFunctionImmediatelyAfter(Succ, depth - 1)) |
| 2088 | return false; |
| 2089 | } |
| 2090 | |
| 2091 | |
| 2092 | return true; |
| 2093 | } |
| 2094 | |
| 2095 | static bool localAllocaNeedsStackSave(CoroAllocaAllocInst *AI) { |
| 2096 | |
| 2097 | |
| 2098 | |
| 2099 | for (auto U : AI->users()) { |
| 2100 | auto FI = dyn_cast<CoroAllocaFreeInst>(U); |
| 2101 | if (!FI) continue; |
| 2102 | |
| 2103 | if (!willLeaveFunctionImmediatelyAfter(FI->getParent())) |
| 2104 | return true; |
| 2105 | } |
| 2106 | |
| 2107 | |
| 2108 | return false; |
| 2109 | } |
| 2110 | |
| 2111 | |
| 2112 | |
| 2113 | static void lowerLocalAllocas(ArrayRef<CoroAllocaAllocInst*> LocalAllocas, |
| 2114 | SmallVectorImpl<Instruction*> &DeadInsts) { |
| 2115 | for (auto AI : LocalAllocas) { |
| 2116 | auto M = AI->getModule(); |
| 2117 | IRBuilder<> Builder(AI); |
| 2118 | |
| 2119 | |
| 2120 | |
| 2121 | Value *StackSave = nullptr; |
| 2122 | if (localAllocaNeedsStackSave(AI)) |
| 2123 | StackSave = Builder.CreateCall( |
| 2124 | Intrinsic::getDeclaration(M, Intrinsic::stacksave)); |
| 2125 | |
| 2126 | |
| 2127 | auto Alloca = Builder.CreateAlloca(Builder.getInt8Ty(), AI->getSize()); |
| 2128 | Alloca->setAlignment(Align(AI->getAlignment())); |
| 2129 | |
| 2130 | for (auto U : AI->users()) { |
| 2131 | |
| 2132 | if (isa<CoroAllocaGetInst>(U)) { |
| 2133 | U->replaceAllUsesWith(Alloca); |
| 2134 | |
| 2135 | |
| 2136 | |
| 2137 | |
| 2138 | } else { |
| 2139 | auto FI = cast<CoroAllocaFreeInst>(U); |
| 2140 | if (StackSave) { |
| 2141 | Builder.SetInsertPoint(FI); |
| 2142 | Builder.CreateCall( |
| 2143 | Intrinsic::getDeclaration(M, Intrinsic::stackrestore), |
| 2144 | StackSave); |
| 2145 | } |
| 2146 | } |
| 2147 | DeadInsts.push_back(cast<Instruction>(U)); |
| 2148 | } |
| 2149 | |
| 2150 | DeadInsts.push_back(AI); |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | |
| 2155 | |
| 2156 | |
| 2157 | static Instruction *lowerNonLocalAlloca(CoroAllocaAllocInst *AI, |
| 2158 | coro::Shape &Shape, |
| 2159 | SmallVectorImpl<Instruction*> &DeadInsts) { |
| 2160 | IRBuilder<> Builder(AI); |
| 2161 | auto Alloc = Shape.emitAlloc(Builder, AI->getSize(), nullptr); |
| 2162 | |
| 2163 | for (User *U : AI->users()) { |
| 2164 | if (isa<CoroAllocaGetInst>(U)) { |
| 2165 | U->replaceAllUsesWith(Alloc); |
| 2166 | } else { |
| 2167 | auto FI = cast<CoroAllocaFreeInst>(U); |
| 2168 | Builder.SetInsertPoint(FI); |
| 2169 | Shape.emitDealloc(Builder, Alloc, nullptr); |
| 2170 | } |
| 2171 | DeadInsts.push_back(cast<Instruction>(U)); |
| 2172 | } |
| 2173 | |
| 2174 | |
| 2175 | DeadInsts.push_back(AI); |
| 2176 | |
| 2177 | |
| 2178 | return cast<Instruction>(Alloc); |
| 2179 | } |
| 2180 | |
| 2181 | |
| 2182 | static Value *emitGetSwiftErrorValue(IRBuilder<> &Builder, Type *ValueTy, |
| 2183 | coro::Shape &Shape) { |
| 2184 | |
| 2185 | auto FnTy = FunctionType::get(ValueTy, {}, false); |
| 2186 | auto Fn = ConstantPointerNull::get(FnTy->getPointerTo()); |
| 2187 | |
| 2188 | auto Call = Builder.CreateCall(FnTy, Fn, {}); |
| 2189 | Shape.SwiftErrorOps.push_back(Call); |
| 2190 | |
| 2191 | return Call; |
| 2192 | } |
| 2193 | |
| 2194 | |
| 2195 | |
| 2196 | |
| 2197 | static Value *emitSetSwiftErrorValue(IRBuilder<> &Builder, Value *V, |
| 2198 | coro::Shape &Shape) { |
| 2199 | |
| 2200 | auto FnTy = FunctionType::get(V->getType()->getPointerTo(), |
| 2201 | {V->getType()}, false); |
| 2202 | auto Fn = ConstantPointerNull::get(FnTy->getPointerTo()); |
| 2203 | |
| 2204 | auto Call = Builder.CreateCall(FnTy, Fn, { V }); |
| 2205 | Shape.SwiftErrorOps.push_back(Call); |
| 2206 | |
| 2207 | return Call; |
| 2208 | } |
| 2209 | |
| 2210 | |
| 2211 | |
| 2212 | |
| 2213 | |
| 2214 | |
| 2215 | static Value *emitSetAndGetSwiftErrorValueAround(Instruction *Call, |
| 2216 | AllocaInst *Alloca, |
| 2217 | coro::Shape &Shape) { |
| 2218 | auto ValueTy = Alloca->getAllocatedType(); |
| 2219 | IRBuilder<> Builder(Call); |
| 2220 | |
| 2221 | |
| 2222 | |
| 2223 | auto ValueBeforeCall = Builder.CreateLoad(ValueTy, Alloca); |
| 2224 | auto Addr = emitSetSwiftErrorValue(Builder, ValueBeforeCall, Shape); |
| 2225 | |
| 2226 | |
| 2227 | |
| 2228 | |
| 2229 | if (isa<CallInst>(Call)) { |
| 2230 | Builder.SetInsertPoint(Call->getNextNode()); |
| 2231 | } else { |
| 2232 | auto Invoke = cast<InvokeInst>(Call); |
| 2233 | Builder.SetInsertPoint(Invoke->getNormalDest()->getFirstNonPHIOrDbg()); |
| 2234 | } |
| 2235 | |
| 2236 | |
| 2237 | auto ValueAfterCall = emitGetSwiftErrorValue(Builder, ValueTy, Shape); |
| 2238 | Builder.CreateStore(ValueAfterCall, Alloca); |
| 2239 | |
| 2240 | return Addr; |
| 2241 | } |
| 2242 | |
| 2243 | |
| 2244 | |
| 2245 | static void eliminateSwiftErrorAlloca(Function &F, AllocaInst *Alloca, |
| 2246 | coro::Shape &Shape) { |
| 2247 | for (auto UI = Alloca->use_begin(), UE = Alloca->use_end(); UI != UE; ) { |
| 2248 | |
| 2249 | |
| 2250 | auto &Use = *UI; |
| 2251 | ++UI; |
| 2252 | |
| 2253 | |
| 2254 | |
| 2255 | auto User = Use.getUser(); |
| 2256 | if (isa<LoadInst>(User) || isa<StoreInst>(User)) |
| 2257 | continue; |
| 2258 | |
| 2259 | assert(isa<CallInst>(User) || isa<InvokeInst>(User)); |
| 2260 | auto Call = cast<Instruction>(User); |
| 2261 | |
| 2262 | auto Addr = emitSetAndGetSwiftErrorValueAround(Call, Alloca, Shape); |
| 2263 | |
| 2264 | |
| 2265 | Use.set(Addr); |
| 2266 | } |
| 2267 | |
| 2268 | |
| 2269 | assert(isAllocaPromotable(Alloca)); |
| 2270 | } |
| 2271 | |
| 2272 | |
| 2273 | |
| 2274 | |
| 2275 | |
| 2276 | static void eliminateSwiftErrorArgument(Function &F, Argument &Arg, |
| 2277 | coro::Shape &Shape, |
| 2278 | SmallVectorImpl<AllocaInst*> &AllocasToPromote) { |
| 2279 | IRBuilder<> Builder(F.getEntryBlock().getFirstNonPHIOrDbg()); |
| 2280 | |
| 2281 | auto ArgTy = cast<PointerType>(Arg.getType()); |
| 2282 | auto ValueTy = ArgTy->getElementType(); |
| 2283 | |
| 2284 | |
| 2285 | |
| 2286 | |
| 2287 | auto Alloca = Builder.CreateAlloca(ValueTy, ArgTy->getAddressSpace()); |
| 2288 | Arg.replaceAllUsesWith(Alloca); |
| 2289 | |
| 2290 | |
| 2291 | auto InitialValue = Constant::getNullValue(ValueTy); |
| 2292 | Builder.CreateStore(InitialValue, Alloca); |
| 2293 | |
| 2294 | |
| 2295 | for (auto Suspend : Shape.CoroSuspends) { |
| 2296 | (void) emitSetAndGetSwiftErrorValueAround(Suspend, Alloca, Shape); |
| 2297 | } |
| 2298 | |
| 2299 | |
| 2300 | for (auto End : Shape.CoroEnds) { |
| 2301 | Builder.SetInsertPoint(End); |
| 2302 | auto FinalValue = Builder.CreateLoad(ValueTy, Alloca); |
| 2303 | (void) emitSetSwiftErrorValue(Builder, FinalValue, Shape); |
| 2304 | } |
| 2305 | |
| 2306 | |
| 2307 | AllocasToPromote.push_back(Alloca); |
| 2308 | eliminateSwiftErrorAlloca(F, Alloca, Shape); |
| 2309 | } |
| 2310 | |
| 2311 | |
| 2312 | |
| 2313 | static void eliminateSwiftError(Function &F, coro::Shape &Shape) { |
| 2314 | SmallVector<AllocaInst*, 4> AllocasToPromote; |
| 2315 | |
| 2316 | |
| 2317 | for (auto &Arg : F.args()) { |
| 2318 | if (!Arg.hasSwiftErrorAttr()) continue; |
| 2319 | |
| 2320 | eliminateSwiftErrorArgument(F, Arg, Shape, AllocasToPromote); |
| 2321 | break; |
| 2322 | } |
| 2323 | |
| 2324 | |
| 2325 | for (auto &Inst : F.getEntryBlock()) { |
| 2326 | auto Alloca = dyn_cast<AllocaInst>(&Inst); |
| 2327 | if (!Alloca || !Alloca->isSwiftError()) continue; |
| 2328 | |
| 2329 | |
| 2330 | Alloca->setSwiftError(false); |
| 2331 | |
| 2332 | AllocasToPromote.push_back(Alloca); |
| 2333 | eliminateSwiftErrorAlloca(F, Alloca, Shape); |
| 2334 | } |
| 2335 | |
| 2336 | |
| 2337 | |
| 2338 | if (!AllocasToPromote.empty()) { |
| 2339 | DominatorTree DT(F); |
| 2340 | PromoteMemToReg(AllocasToPromote, DT); |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | |
| 2345 | |
| 2346 | static void sinkSpillUsesAfterCoroBegin(Function &F, |
| 2347 | const FrameDataInfo &FrameData, |
| 2348 | CoroBeginInst *CoroBegin) { |
| 2349 | DominatorTree Dom(F); |
| 2350 | |
| 2351 | SmallSetVector<Instruction *, 32> ToMove; |
| 2352 | SmallVector<Instruction *, 32> Worklist; |
| 2353 | |
| 2354 | |
| 2355 | for (auto *Def : FrameData.getAllDefs()) { |
| 2356 | for (User *U : Def->users()) { |
| 2357 | auto Inst = cast<Instruction>(U); |
| 2358 | if (Inst->getParent() != CoroBegin->getParent() || |
| 2359 | Dom.dominates(CoroBegin, Inst)) |
| 2360 | continue; |
| 2361 | if (ToMove.insert(Inst)) |
| 2362 | Worklist.push_back(Inst); |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | while (!Worklist.empty()) { |
| 2367 | auto *Def = Worklist.pop_back_val(); |
| 2368 | for (User *U : Def->users()) { |
| 2369 | auto Inst = cast<Instruction>(U); |
| 2370 | if (Dom.dominates(CoroBegin, Inst)) |
| 2371 | continue; |
| 2372 | if (ToMove.insert(Inst)) |
| 2373 | Worklist.push_back(Inst); |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | |
| 2378 | SmallVector<Instruction *, 64> InsertionList(ToMove.begin(), ToMove.end()); |
| 2379 | llvm::sort(InsertionList, [&Dom](Instruction *A, Instruction *B) -> bool { |
| 2380 | |
| 2381 | return Dom.dominates(A, B); |
| 2382 | }); |
| 2383 | |
| 2384 | Instruction *InsertPt = CoroBegin->getNextNode(); |
| 2385 | for (Instruction *Inst : InsertionList) |
| 2386 | Inst->moveBefore(InsertPt); |
| 2387 | } |
| 2388 | |
| 2389 | |
| 2390 | |
| 2391 | |
| 2392 | |
| 2393 | static void sinkLifetimeStartMarkers(Function &F, coro::Shape &Shape, |
| 2394 | SuspendCrossingInfo &Checker) { |
| 2395 | DominatorTree DT(F); |
| 2396 | |
| 2397 | |
| 2398 | SmallPtrSet<BasicBlock *, 4> DomSet; |
| 2399 | DomSet.insert(&F.getEntryBlock()); |
| 2400 | for (auto *CSI : Shape.CoroSuspends) { |
| 2401 | BasicBlock *SuspendBlock = CSI->getParent(); |
| 2402 | assert(isSuspendBlock(SuspendBlock) && SuspendBlock->getSingleSuccessor() && |
| 2403 | "should have split coro.suspend into its own block"); |
| 2404 | DomSet.insert(SuspendBlock->getSingleSuccessor()); |
| 2405 | } |
| 2406 | |
| 2407 | for (Instruction &I : instructions(F)) { |
| 2408 | AllocaInst* AI = dyn_cast<AllocaInst>(&I); |
| 2409 | if (!AI) |
| 2410 | continue; |
| 2411 | |
| 2412 | for (BasicBlock *DomBB : DomSet) { |
| 2413 | bool Valid = true; |
| 2414 | SmallVector<Instruction *, 1> Lifetimes; |
| 2415 | |
| 2416 | auto isLifetimeStart = [](Instruction* I) { |
| 2417 | if (auto* II = dyn_cast<IntrinsicInst>(I)) |
| 2418 | return II->getIntrinsicID() == Intrinsic::lifetime_start; |
| 2419 | return false; |
| 2420 | }; |
| 2421 | |
| 2422 | auto collectLifetimeStart = [&](Instruction *U, AllocaInst *AI) { |
| 2423 | if (isLifetimeStart(U)) { |
| 2424 | Lifetimes.push_back(U); |
| 2425 | return true; |
| 2426 | } |
| 2427 | if (!U->hasOneUse() || U->stripPointerCasts() != AI) |
| 2428 | return false; |
| 2429 | if (isLifetimeStart(U->user_back())) { |
| 2430 | Lifetimes.push_back(U->user_back()); |
| 2431 | return true; |
| 2432 | } |
| 2433 | return false; |
| 2434 | }; |
| 2435 | |
| 2436 | for (User *U : AI->users()) { |
| 2437 | Instruction *UI = cast<Instruction>(U); |
| 2438 | |
| 2439 | |
| 2440 | |
| 2441 | |
| 2442 | if (!DT.dominates(DomBB, UI->getParent()) || |
| 2443 | Checker.isDefinitionAcrossSuspend(DomBB, UI)) { |
| 2444 | |
| 2445 | |
| 2446 | if (collectLifetimeStart(UI, AI)) |
| 2447 | continue; |
| 2448 | Valid = false; |
| 2449 | break; |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | |
| 2454 | if (Valid && Lifetimes.size() != 0) { |
| 2455 | |
| 2456 | auto *NewBitCast = [&](AllocaInst *AI) -> Value* { |
| 2457 | if (isa<AllocaInst>(Lifetimes[0]->getOperand(1))) |
| 2458 | return AI; |
| 2459 | auto *Int8PtrTy = Type::getInt8PtrTy(F.getContext()); |
| 2460 | return CastInst::Create(Instruction::BitCast, AI, Int8PtrTy, "", |
| 2461 | DomBB->getTerminator()); |
| 2462 | }(AI); |
| 2463 | |
| 2464 | auto *NewLifetime = Lifetimes[0]->clone(); |
| 2465 | NewLifetime->replaceUsesOfWith(NewLifetime->getOperand(1), NewBitCast); |
| 2466 | NewLifetime->insertBefore(DomBB->getTerminator()); |
| 2467 | |
| 2468 | |
| 2469 | for (Instruction *S : Lifetimes) |
| 2470 | S->eraseFromParent(); |
| 2471 | |
| 2472 | break; |
| 2473 | } |
| 2474 | } |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | static void collectFrameAllocas(Function &F, coro::Shape &Shape, |
| 2479 | const SuspendCrossingInfo &Checker, |
| 2480 | SmallVectorImpl<AllocaInfo> &Allocas) { |
| 2481 | for (Instruction &I : instructions(F)) { |
| 2482 | auto *AI = dyn_cast<AllocaInst>(&I); |
| 2483 | if (!AI) |
| 2484 | continue; |
| 2485 | |
| 2486 | |
| 2487 | if (AI == Shape.SwitchLowering.PromiseAlloca) { |
| 2488 | continue; |
| 2489 | } |
| 2490 | DominatorTree DT(F); |
| 2491 | AllocaUseVisitor Visitor{F.getParent()->getDataLayout(), DT, |
| 2492 | *Shape.CoroBegin, Checker}; |
| 2493 | Visitor.visitPtr(*AI); |
| 2494 | if (!Visitor.getShouldLiveOnFrame()) |
| 2495 | continue; |
| 2496 | Allocas.emplace_back(AI, Visitor.getAliasesCopy(), |
| 2497 | Visitor.getMayWriteBeforeCoroBegin()); |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | void coro::salvageDebugInfo( |
| 2502 | SmallDenseMap<llvm::Value *, llvm::AllocaInst *, 4> &DbgPtrAllocaCache, |
| 2503 | DbgVariableIntrinsic *DVI, bool ReuseFrameSlot) { |
| 2504 | Function *F = DVI->getFunction(); |
| 2505 | IRBuilder<> Builder(F->getContext()); |
| 2506 | auto InsertPt = F->getEntryBlock().getFirstInsertionPt(); |
| 2507 | while (isa<IntrinsicInst>(InsertPt)) |
| 2508 | ++InsertPt; |
| 2509 | Builder.SetInsertPoint(&F->getEntryBlock(), InsertPt); |
| 2510 | DIExpression *Expr = DVI->getExpression(); |
| 2511 | |
| 2512 | |
| 2513 | bool OutermostLoad = true; |
| 2514 | Value *Storage = DVI->getVariableLocationOp(0); |
| 2515 | Value *OriginalStorage = Storage; |
| 2516 | while (Storage) { |
| 2517 | if (auto *LdInst = dyn_cast<LoadInst>(Storage)) { |
| 2518 | Storage = LdInst->getOperand(0); |
| 2519 | |
| 2520 | |
| 2521 | |
| 2522 | |
| 2523 | |
| 2524 | |
| 2525 | if (!OutermostLoad) |
| 2526 | Expr = DIExpression::prepend(Expr, DIExpression::DerefBefore); |
| 2527 | OutermostLoad = false; |
| 2528 | } else if (auto *StInst = dyn_cast<StoreInst>(Storage)) { |
| 2529 | Storage = StInst->getOperand(0); |
| 2530 | } else if (auto *GEPInst = dyn_cast<GetElementPtrInst>(Storage)) { |
| 2531 | SmallVector<Value *> AdditionalValues; |
| 2532 | DIExpression *SalvagedExpr = llvm::salvageDebugInfoImpl( |
| 2533 | *GEPInst, Expr, |
| 2534 | false, 0, AdditionalValues); |
| 2535 | |
| 2536 | |
| 2537 | if (!SalvagedExpr || !AdditionalValues.empty()) |
| 2538 | break; |
| 2539 | Expr = SalvagedExpr; |
| 2540 | Storage = GEPInst->getOperand(0); |
| 2541 | } else if (auto *BCInst = dyn_cast<llvm::BitCastInst>(Storage)) |
| 2542 | Storage = BCInst->getOperand(0); |
| 2543 | else |
| 2544 | break; |
| 2545 | } |
| 2546 | if (!Storage) |
| 2547 | return; |
| 2548 | |
| 2549 | |
| 2550 | |
| 2551 | |
| 2552 | |
| 2553 | |
| 2554 | |
| 2555 | |
| 2556 | if (!ReuseFrameSlot && !EnableReuseStorageInFrame) |
| 2557 | if (auto *Arg = dyn_cast<llvm::Argument>(Storage)) { |
| 2558 | auto &Cached = DbgPtrAllocaCache[Storage]; |
| 2559 | if (!Cached) { |
| 2560 | Cached = Builder.CreateAlloca(Storage->getType(), 0, nullptr, |
| 2561 | Arg->getName() + ".debug"); |
| 2562 | Builder.CreateStore(Storage, Cached); |
| 2563 | } |
| 2564 | Storage = Cached; |
| 2565 | |
| 2566 | |
| 2567 | |
| 2568 | |
| 2569 | |
| 2570 | |
| 2571 | |
| 2572 | if (Expr && Expr->isComplex()) |
| 2573 | Expr = DIExpression::prepend(Expr, DIExpression::DerefBefore); |
| 2574 | } |
| 2575 | |
| 2576 | DVI->replaceVariableLocationOp(OriginalStorage, Storage); |
| 2577 | DVI->setExpression(Expr); |
| 2578 | |
| 2579 | if (!isa<DbgValueInst>(DVI)) { |
| 2580 | if (auto *InsertPt = dyn_cast<Instruction>(Storage)) |
| 2581 | DVI->moveAfter(InsertPt); |
| 2582 | else if (isa<Argument>(Storage)) |
| 2583 | DVI->moveAfter(F->getEntryBlock().getFirstNonPHI()); |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | void coro::buildCoroutineFrame(Function &F, Shape &Shape) { |
| 2588 | |
| 2589 | if (Shape.ABI != coro::ABI::Async || !Shape.CoroSuspends.empty()) |
| 2590 | eliminateSwiftError(F, Shape); |
| 2591 | |
| 2592 | if (Shape.ABI == coro::ABI::Switch && |
| 2593 | Shape.SwitchLowering.PromiseAlloca) { |
| 2594 | Shape.getSwitchCoroId()->clearPromise(); |
| 2595 | } |
| 2596 | |
| 2597 | |
| 2598 | |
| 2599 | |
| 2600 | for (auto *CSI : Shape.CoroSuspends) { |
| 2601 | if (auto *Save = CSI->getCoroSave()) |
| 2602 | splitAround(Save, "CoroSave"); |
| 2603 | splitAround(CSI, "CoroSuspend"); |
| 2604 | } |
| 2605 | |
| 2606 | |
| 2607 | for (AnyCoroEndInst *CE : Shape.CoroEnds) { |
| 2608 | splitAround(CE, "CoroEnd"); |
| 2609 | |
| 2610 | |
| 2611 | |
| 2612 | |
| 2613 | |
| 2614 | if (auto *AsyncEnd = dyn_cast<CoroAsyncEndInst>(CE)) { |
| 2615 | auto *MustTailCallFn = AsyncEnd->getMustTailCallFunction(); |
| 2616 | if (!MustTailCallFn) |
| 2617 | continue; |
| 2618 | IRBuilder<> Builder(AsyncEnd); |
| 2619 | SmallVector<Value *, 8> Args(AsyncEnd->args()); |
| 2620 | auto Arguments = ArrayRef<Value *>(Args).drop_front(3); |
| 2621 | auto *Call = createMustTailCall(AsyncEnd->getDebugLoc(), MustTailCallFn, |
| 2622 | Arguments, Builder); |
| 2623 | splitAround(Call, "MustTailCall.Before.CoroEnd"); |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | |
| 2628 | |
| 2629 | cleanupSinglePredPHIs(F); |
| 2630 | |
| 2631 | |
| 2632 | |
| 2633 | rewritePHIs(F); |
| 2634 | |
| 2635 | |
| 2636 | SuspendCrossingInfo Checker(F, Shape); |
| 2637 | |
| 2638 | IRBuilder<> Builder(F.getContext()); |
| 2639 | FrameDataInfo FrameData; |
| 2640 | SmallVector<CoroAllocaAllocInst*, 4> LocalAllocas; |
| 2641 | SmallVector<Instruction*, 4> DeadInstructions; |
| 2642 | |
| 2643 | { |
| 2644 | SpillInfo Spills; |
| 2645 | for (int Repeat = 0; Repeat < 4; ++Repeat) { |
| 2646 | |
| 2647 | for (Instruction &I : instructions(F)) |
| 2648 | if (materializable(I)) { |
| 2649 | for (User *U : I.users()) |
| 2650 | if (Checker.isDefinitionAcrossSuspend(I, U)) |
| 2651 | Spills[&I].push_back(cast<Instruction>(U)); |
| 2652 | |
| 2653 | |
| 2654 | SmallVector<DbgValueInst *, 16> DVIs; |
| 2655 | findDbgValues(DVIs, &I); |
| 2656 | for (auto *DVI : DVIs) |
| 2657 | if (Checker.isDefinitionAcrossSuspend(I, DVI)) |
| 2658 | Spills[&I].push_back(DVI); |
| 2659 | } |
| 2660 | |
| 2661 | if (Spills.empty()) |
| 2662 | break; |
| 2663 | |
| 2664 | |
| 2665 | |
| 2666 | LLVM_DEBUG(dumpSpills("Materializations", Spills)); |
| 2667 | rewriteMaterializableInstructions(Builder, Spills); |
| 2668 | Spills.clear(); |
| 2669 | } |
| 2670 | } |
| 2671 | |
| 2672 | sinkLifetimeStartMarkers(F, Shape, Checker); |
| 2673 | if (Shape.ABI != coro::ABI::Async || !Shape.CoroSuspends.empty()) |
| 2674 | collectFrameAllocas(F, Shape, Checker, FrameData.Allocas); |
| 2675 | LLVM_DEBUG(dumpAllocas(FrameData.Allocas)); |
| 2676 | |
| 2677 | |
| 2678 | for (Argument &A : F.args()) |
| 2679 | for (User *U : A.users()) |
| 2680 | if (Checker.isDefinitionAcrossSuspend(A, U)) |
| 2681 | FrameData.Spills[&A].push_back(cast<Instruction>(U)); |
| 2682 | |
| 2683 | for (Instruction &I : instructions(F)) { |
| 2684 | |
| 2685 | |
| 2686 | if (isCoroutineStructureIntrinsic(I) || &I == Shape.CoroBegin) |
| 2687 | continue; |
| 2688 | |
| 2689 | |
| 2690 | |
| 2691 | if (Shape.ABI == coro::ABI::Switch && |
| 2692 | Shape.SwitchLowering.PromiseAlloca == &I) |
| 2693 | continue; |
| 2694 | |
| 2695 | |
| 2696 | if (auto AI = dyn_cast<CoroAllocaAllocInst>(&I)) { |
| 2697 | |
| 2698 | if (isLocalAlloca(AI)) { |
| 2699 | LocalAllocas.push_back(AI); |
| 2700 | continue; |
| 2701 | } |
| 2702 | |
| 2703 | |
| 2704 | |
| 2705 | |
| 2706 | |
| 2707 | |
| 2708 | auto Alloc = lowerNonLocalAlloca(AI, Shape, DeadInstructions); |
| 2709 | |
| 2710 | for (User *U : Alloc->users()) { |
| 2711 | if (Checker.isDefinitionAcrossSuspend(*Alloc, U)) |
| 2712 | FrameData.Spills[Alloc].push_back(cast<Instruction>(U)); |
| 2713 | } |
| 2714 | continue; |
| 2715 | } |
| 2716 | |
| 2717 | |
| 2718 | if (isa<CoroAllocaGetInst>(I)) |
| 2719 | continue; |
| 2720 | |
| 2721 | if (isa<AllocaInst>(I)) |
| 2722 | continue; |
| 2723 | |
| 2724 | for (User *U : I.users()) |
| 2725 | if (Checker.isDefinitionAcrossSuspend(I, U)) { |
| 2726 | |
| 2727 | if (I.getType()->isTokenTy()) |
| 2728 | report_fatal_error( |
| 2729 | "token definition is separated from the use by a suspend point"); |
| 2730 | FrameData.Spills[&I].push_back(cast<Instruction>(U)); |
| 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | |
| 2735 | |
| 2736 | |
| 2737 | |
| 2738 | for (auto &Iter : FrameData.Spills) { |
| 2739 | auto *V = Iter.first; |
| 2740 | SmallVector<DbgValueInst *, 16> DVIs; |
| 2741 | findDbgValues(DVIs, V); |
| 2742 | llvm::for_each(DVIs, [&](DbgValueInst *DVI) { |
| 2743 | if (Checker.isDefinitionAcrossSuspend(*V, DVI)) |
| 2744 | FrameData.Spills[V].push_back(DVI); |
| 2745 | }); |
| 2746 | } |
| 2747 | |
| 2748 | LLVM_DEBUG(dumpSpills("Spills", FrameData.Spills)); |
| 2749 | if (Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce || |
| 2750 | Shape.ABI == coro::ABI::Async) |
| 2751 | sinkSpillUsesAfterCoroBegin(F, FrameData, Shape.CoroBegin); |
| 2752 | Shape.FrameTy = buildFrameType(F, Shape, FrameData); |
| 2753 | createFramePtr(Shape); |
| 2754 | |
| 2755 | buildFrameDebugInfo(F, Shape, FrameData); |
| 2756 | insertSpills(FrameData, Shape); |
| 2757 | lowerLocalAllocas(LocalAllocas, DeadInstructions); |
| 2758 | |
| 2759 | for (auto I : DeadInstructions) |
| 2760 | I->eraseFromParent(); |
| 2761 | } |