Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 1 | //===-- ShadowStackCollector.cpp - GC support for uncooperative targets ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements lowering for the llvm.gc* intrinsics for targets that do |
| 11 | // not natively support them (which includes the C backend). Note that the code |
| 12 | // generated is not quite as efficient as collectors which generate stack maps |
| 13 | // to identify roots. |
| 14 | // |
| 15 | // This pass implements the code transformation described in this paper: |
| 16 | // "Accurate Garbage Collection in an Uncooperative Environment" |
| 17 | // Fergus Henderson, ISMM, 2002 |
| 18 | // |
| 19 | // In runtime/GC/SemiSpace.cpp is a prototype runtime which is compatible with |
| 20 | // this collector. |
| 21 | // |
| 22 | // In order to support this particular transformation, all stack roots are |
| 23 | // coallocated in the stack. This allows a fully target-independent stack map |
| 24 | // while introducing only minor runtime overhead. |
| 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #define DEBUG_TYPE "shadowstackgc" |
| 29 | #include "llvm/CodeGen/Collectors.h" |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
| 31 | #include "llvm/CodeGen/Collector.h" |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 32 | #include "llvm/IntrinsicInst.h" |
| 33 | #include "llvm/Module.h" |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 34 | #include "llvm/Support/IRBuilder.h" |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace llvm; |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | class VISIBILITY_HIDDEN ShadowStackCollector : public Collector { |
| 41 | /// RootChain - This is the global linked-list that contains the chain of GC |
| 42 | /// roots. |
| 43 | GlobalVariable *Head; |
| 44 | |
| 45 | /// StackEntryTy - Abstract type of a link in the shadow stack. |
| 46 | /// |
| 47 | const StructType *StackEntryTy; |
| 48 | |
| 49 | /// Roots - GC roots in the current function. Each is a pair of the |
| 50 | /// intrinsic call and its corresponding alloca. |
| 51 | std::vector<std::pair<CallInst*,AllocaInst*> > Roots; |
| 52 | |
| 53 | public: |
| 54 | ShadowStackCollector(); |
| 55 | |
| 56 | bool initializeCustomLowering(Module &M); |
| 57 | bool performCustomLowering(Function &F); |
| 58 | |
| 59 | private: |
| 60 | bool IsNullValue(Value *V); |
| 61 | Constant *GetFrameMap(Function &F); |
| 62 | const Type* GetConcreteStackEntryType(Function &F); |
| 63 | void CollectRoots(Function &F); |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 64 | static GetElementPtrInst *CreateGEP(IRBuilder &B, Value *BasePtr, |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 65 | int Idx1, const char *Name); |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 66 | static GetElementPtrInst *CreateGEP(IRBuilder &B, Value *BasePtr, |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 67 | int Idx1, int Idx2, const char *Name); |
| 68 | }; |
| 69 | |
| 70 | CollectorRegistry::Add<ShadowStackCollector> |
| 71 | Y("shadow-stack", |
| 72 | "Very portable collector for uncooperative code generators"); |
| 73 | |
| 74 | /// EscapeEnumerator - This is a little algorithm to find all escape points |
| 75 | /// from a function so that "finally"-style code can be inserted. In addition |
| 76 | /// to finding the existing return and unwind instructions, it also (if |
| 77 | /// necessary) transforms any call instructions into invokes and sends them to |
| 78 | /// a landing pad. |
| 79 | /// |
| 80 | /// It's wrapped up in a state machine using the same transform C# uses for |
| 81 | /// 'yield return' enumerators, This transform allows it to be non-allocating. |
| 82 | class VISIBILITY_HIDDEN EscapeEnumerator { |
| 83 | Function &F; |
| 84 | const char *CleanupBBName; |
| 85 | |
| 86 | // State. |
| 87 | int State; |
| 88 | Function::iterator StateBB, StateE; |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 89 | IRBuilder Builder; |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 90 | |
| 91 | public: |
| 92 | EscapeEnumerator(Function &F, const char *N = "cleanup") |
| 93 | : F(F), CleanupBBName(N), State(0) {} |
| 94 | |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 95 | IRBuilder *Next() { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 96 | switch (State) { |
| 97 | default: |
| 98 | return 0; |
| 99 | |
| 100 | case 0: |
| 101 | StateBB = F.begin(); |
| 102 | StateE = F.end(); |
| 103 | State = 1; |
| 104 | |
| 105 | case 1: |
| 106 | // Find all 'return' and 'unwind' instructions. |
| 107 | while (StateBB != StateE) { |
| 108 | BasicBlock *CurBB = StateBB++; |
| 109 | |
| 110 | // Branches and invokes do not escape, only unwind and return do. |
| 111 | TerminatorInst *TI = CurBB->getTerminator(); |
| 112 | if (!isa<UnwindInst>(TI) && !isa<ReturnInst>(TI)) |
| 113 | continue; |
| 114 | |
| 115 | Builder.SetInsertPoint(TI->getParent(), TI); |
| 116 | return &Builder; |
| 117 | } |
| 118 | |
| 119 | State = 2; |
| 120 | |
| 121 | // Find all 'call' instructions. |
| 122 | SmallVector<Instruction*,16> Calls; |
| 123 | for (Function::iterator BB = F.begin(), |
| 124 | E = F.end(); BB != E; ++BB) |
| 125 | for (BasicBlock::iterator II = BB->begin(), |
| 126 | EE = BB->end(); II != EE; ++II) |
| 127 | if (CallInst *CI = dyn_cast<CallInst>(II)) |
| 128 | if (!CI->getCalledFunction() || |
| 129 | !CI->getCalledFunction()->getIntrinsicID()) |
| 130 | Calls.push_back(CI); |
| 131 | |
| 132 | if (Calls.empty()) |
| 133 | return 0; |
| 134 | |
| 135 | // Create a cleanup block. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 136 | BasicBlock *CleanupBB = BasicBlock::Create(CleanupBBName, &F); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 137 | UnwindInst *UI = new UnwindInst(CleanupBB); |
| 138 | |
| 139 | // Transform the 'call' instructions into 'invoke's branching to the |
| 140 | // cleanup block. Go in reverse order to make prettier BB names. |
| 141 | SmallVector<Value*,16> Args; |
| 142 | for (unsigned I = Calls.size(); I != 0; ) { |
| 143 | CallInst *CI = cast<CallInst>(Calls[--I]); |
| 144 | |
| 145 | // Split the basic block containing the function call. |
| 146 | BasicBlock *CallBB = CI->getParent(); |
| 147 | BasicBlock *NewBB = |
| 148 | CallBB->splitBasicBlock(CI, CallBB->getName() + ".cont"); |
| 149 | |
| 150 | // Remove the unconditional branch inserted at the end of CallBB. |
| 151 | CallBB->getInstList().pop_back(); |
| 152 | NewBB->getInstList().remove(CI); |
| 153 | |
| 154 | // Create a new invoke instruction. |
| 155 | Args.clear(); |
| 156 | Args.append(CI->op_begin() + 1, CI->op_end()); |
| 157 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 158 | InvokeInst *II = InvokeInst::Create(CI->getOperand(0), |
| 159 | NewBB, CleanupBB, |
| 160 | Args.begin(), Args.end(), |
| 161 | CI->getName(), CallBB); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 162 | II->setCallingConv(CI->getCallingConv()); |
| 163 | II->setParamAttrs(CI->getParamAttrs()); |
| 164 | CI->replaceAllUsesWith(II); |
| 165 | delete CI; |
| 166 | } |
| 167 | |
| 168 | Builder.SetInsertPoint(UI->getParent(), UI); |
| 169 | return &Builder; |
| 170 | } |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | } |
| 175 | |
| 176 | // ----------------------------------------------------------------------------- |
| 177 | |
| 178 | Collector *llvm::createShadowStackCollector() { |
| 179 | return new ShadowStackCollector(); |
| 180 | } |
| 181 | |
| 182 | ShadowStackCollector::ShadowStackCollector() : Head(0), StackEntryTy(0) { |
| 183 | InitRoots = true; |
| 184 | CustomRoots = true; |
| 185 | } |
| 186 | |
| 187 | Constant *ShadowStackCollector::GetFrameMap(Function &F) { |
| 188 | // doInitialization creates the abstract type of this value. |
| 189 | |
| 190 | Type *VoidPtr = PointerType::getUnqual(Type::Int8Ty); |
| 191 | |
| 192 | // Truncate the ShadowStackDescriptor if some metadata is null. |
| 193 | unsigned NumMeta = 0; |
| 194 | SmallVector<Constant*,16> Metadata; |
| 195 | for (unsigned I = 0; I != Roots.size(); ++I) { |
| 196 | Constant *C = cast<Constant>(Roots[I].first->getOperand(2)); |
| 197 | if (!C->isNullValue()) |
| 198 | NumMeta = I + 1; |
| 199 | Metadata.push_back(ConstantExpr::getBitCast(C, VoidPtr)); |
| 200 | } |
| 201 | |
| 202 | Constant *BaseElts[] = { |
| 203 | ConstantInt::get(Type::Int32Ty, Roots.size(), false), |
| 204 | ConstantInt::get(Type::Int32Ty, NumMeta, false), |
| 205 | }; |
| 206 | |
| 207 | Constant *DescriptorElts[] = { |
| 208 | ConstantStruct::get(BaseElts, 2), |
| 209 | ConstantArray::get(ArrayType::get(VoidPtr, NumMeta), |
| 210 | Metadata.begin(), NumMeta) |
| 211 | }; |
| 212 | |
| 213 | Constant *FrameMap = ConstantStruct::get(DescriptorElts, 2); |
| 214 | |
| 215 | std::string TypeName("gc_map."); |
| 216 | TypeName += utostr(NumMeta); |
| 217 | F.getParent()->addTypeName(TypeName, FrameMap->getType()); |
| 218 | |
| 219 | // FIXME: Is this actually dangerous as WritingAnLLVMPass.html claims? Seems |
| 220 | // that, short of multithreaded LLVM, it should be safe; all that is |
| 221 | // necessary is that a simple Module::iterator loop not be invalidated. |
| 222 | // Appending to the GlobalVariable list is safe in that sense. |
| 223 | // |
| 224 | // All of the output passes emit globals last. The ExecutionEngine |
| 225 | // explicitly supports adding globals to the module after |
| 226 | // initialization. |
| 227 | // |
| 228 | // Still, if it isn't deemed acceptable, then this transformation needs |
| 229 | // to be a ModulePass (which means it cannot be in the 'llc' pipeline |
| 230 | // (which uses a FunctionPassManager (which segfaults (not asserts) if |
| 231 | // provided a ModulePass))). |
| 232 | Constant *GV = new GlobalVariable(FrameMap->getType(), true, |
| 233 | GlobalVariable::InternalLinkage, |
| 234 | FrameMap, "__gc_" + F.getName(), |
| 235 | F.getParent()); |
| 236 | |
| 237 | Constant *GEPIndices[2] = { ConstantInt::get(Type::Int32Ty, 0), |
| 238 | ConstantInt::get(Type::Int32Ty, 0) }; |
| 239 | return ConstantExpr::getGetElementPtr(GV, GEPIndices, 2); |
| 240 | } |
| 241 | |
| 242 | const Type* ShadowStackCollector::GetConcreteStackEntryType(Function &F) { |
| 243 | // doInitialization creates the generic version of this type. |
| 244 | std::vector<const Type*> EltTys; |
| 245 | EltTys.push_back(StackEntryTy); |
| 246 | for (size_t I = 0; I != Roots.size(); I++) |
| 247 | EltTys.push_back(Roots[I].second->getAllocatedType()); |
| 248 | Type *Ty = StructType::get(EltTys); |
| 249 | |
| 250 | std::string TypeName("gc_stackentry."); |
| 251 | TypeName += F.getName(); |
| 252 | F.getParent()->addTypeName(TypeName, Ty); |
| 253 | |
| 254 | return Ty; |
| 255 | } |
| 256 | |
| 257 | /// doInitialization - If this module uses the GC intrinsics, find them now. If |
| 258 | /// not, exit fast. |
| 259 | bool ShadowStackCollector::initializeCustomLowering(Module &M) { |
| 260 | // struct FrameMap { |
| 261 | // int32_t NumRoots; // Number of roots in stack frame. |
| 262 | // int32_t NumMeta; // Number of metadata descriptors. May be < NumRoots. |
| 263 | // void *Meta[]; // May be absent for roots without metadata. |
| 264 | // }; |
| 265 | std::vector<const Type*> EltTys; |
| 266 | EltTys.push_back(Type::Int32Ty); // 32 bits is ok up to a 32GB stack frame. :) |
| 267 | EltTys.push_back(Type::Int32Ty); // Specifies length of variable length array. |
| 268 | StructType *FrameMapTy = StructType::get(EltTys); |
| 269 | M.addTypeName("gc_map", FrameMapTy); |
| 270 | PointerType *FrameMapPtrTy = PointerType::getUnqual(FrameMapTy); |
| 271 | |
| 272 | // struct StackEntry { |
| 273 | // ShadowStackEntry *Next; // Caller's stack entry. |
| 274 | // FrameMap *Map; // Pointer to constant FrameMap. |
| 275 | // void *Roots[]; // Stack roots (in-place array, so we pretend). |
| 276 | // }; |
| 277 | OpaqueType *RecursiveTy = OpaqueType::get(); |
| 278 | |
| 279 | EltTys.clear(); |
| 280 | EltTys.push_back(PointerType::getUnqual(RecursiveTy)); |
| 281 | EltTys.push_back(FrameMapPtrTy); |
| 282 | PATypeHolder LinkTyH = StructType::get(EltTys); |
| 283 | |
| 284 | RecursiveTy->refineAbstractTypeTo(LinkTyH.get()); |
| 285 | StackEntryTy = cast<StructType>(LinkTyH.get()); |
| 286 | const PointerType *StackEntryPtrTy = PointerType::getUnqual(StackEntryTy); |
| 287 | M.addTypeName("gc_stackentry", LinkTyH.get()); // FIXME: Is this safe from |
| 288 | // a FunctionPass? |
| 289 | |
| 290 | // Get the root chain if it already exists. |
| 291 | Head = M.getGlobalVariable("llvm_gc_root_chain"); |
| 292 | if (!Head) { |
| 293 | // If the root chain does not exist, insert a new one with linkonce |
| 294 | // linkage! |
| 295 | Head = new GlobalVariable(StackEntryPtrTy, false, |
| 296 | GlobalValue::LinkOnceLinkage, |
| 297 | Constant::getNullValue(StackEntryPtrTy), |
| 298 | "llvm_gc_root_chain", &M); |
| 299 | } else if (Head->hasExternalLinkage() && Head->isDeclaration()) { |
| 300 | Head->setInitializer(Constant::getNullValue(StackEntryPtrTy)); |
| 301 | Head->setLinkage(GlobalValue::LinkOnceLinkage); |
| 302 | } |
| 303 | |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | bool ShadowStackCollector::IsNullValue(Value *V) { |
| 308 | if (Constant *C = dyn_cast<Constant>(V)) |
| 309 | return C->isNullValue(); |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | void ShadowStackCollector::CollectRoots(Function &F) { |
| 314 | // FIXME: Account for original alignment. Could fragment the root array. |
| 315 | // Approach 1: Null initialize empty slots at runtime. Yuck. |
| 316 | // Approach 2: Emit a map of the array instead of just a count. |
| 317 | |
| 318 | assert(Roots.empty() && "Not cleaned up?"); |
| 319 | |
| 320 | SmallVector<std::pair<CallInst*,AllocaInst*>,16> MetaRoots; |
| 321 | |
| 322 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 323 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) |
| 324 | if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) |
| 325 | if (Function *F = CI->getCalledFunction()) |
| 326 | if (F->getIntrinsicID() == Intrinsic::gcroot) { |
| 327 | std::pair<CallInst*,AllocaInst*> Pair = std::make_pair( |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 328 | CI, cast<AllocaInst>(CI->getOperand(1)->stripPointerCasts())); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 329 | if (IsNullValue(CI->getOperand(2))) |
| 330 | Roots.push_back(Pair); |
| 331 | else |
| 332 | MetaRoots.push_back(Pair); |
| 333 | } |
| 334 | |
| 335 | // Number roots with metadata (usually empty) at the beginning, so that the |
| 336 | // FrameMap::Meta array can be elided. |
| 337 | Roots.insert(Roots.begin(), MetaRoots.begin(), MetaRoots.end()); |
| 338 | } |
| 339 | |
| 340 | GetElementPtrInst * |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 341 | ShadowStackCollector::CreateGEP(IRBuilder &B, Value *BasePtr, |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 342 | int Idx, int Idx2, const char *Name) { |
| 343 | Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0), |
| 344 | ConstantInt::get(Type::Int32Ty, Idx), |
| 345 | ConstantInt::get(Type::Int32Ty, Idx2) }; |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 346 | Value* Val = B.CreateGEP(BasePtr, Indices, Indices + 3, Name); |
| 347 | |
| 348 | assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant"); |
| 349 | |
| 350 | return dyn_cast<GetElementPtrInst>(Val); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | GetElementPtrInst * |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 354 | ShadowStackCollector::CreateGEP(IRBuilder &B, Value *BasePtr, |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 355 | int Idx, const char *Name) { |
| 356 | Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0), |
| 357 | ConstantInt::get(Type::Int32Ty, Idx) }; |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 358 | Value *Val = B.CreateGEP(BasePtr, Indices, Indices + 2, Name); |
| 359 | |
| 360 | assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant"); |
| 361 | |
| 362 | return dyn_cast<GetElementPtrInst>(Val); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /// runOnFunction - Insert code to maintain the shadow stack. |
| 366 | bool ShadowStackCollector::performCustomLowering(Function &F) { |
| 367 | // Find calls to llvm.gcroot. |
| 368 | CollectRoots(F); |
| 369 | |
| 370 | // If there are no roots in this function, then there is no need to add a |
| 371 | // stack map entry for it. |
| 372 | if (Roots.empty()) |
| 373 | return false; |
| 374 | |
| 375 | // Build the constant map and figure the type of the shadow stack entry. |
| 376 | Value *FrameMap = GetFrameMap(F); |
| 377 | const Type *ConcreteStackEntryTy = GetConcreteStackEntryType(F); |
| 378 | |
| 379 | // Build the shadow stack entry at the very start of the function. |
| 380 | BasicBlock::iterator IP = F.getEntryBlock().begin(); |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 381 | IRBuilder AtEntry(IP->getParent(), IP); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 382 | |
| 383 | Instruction *StackEntry = AtEntry.CreateAlloca(ConcreteStackEntryTy, 0, |
| 384 | "gc_frame"); |
| 385 | |
| 386 | while (isa<AllocaInst>(IP)) ++IP; |
| 387 | AtEntry.SetInsertPoint(IP->getParent(), IP); |
| 388 | |
| 389 | // Initialize the map pointer and load the current head of the shadow stack. |
| 390 | Instruction *CurrentHead = AtEntry.CreateLoad(Head, "gc_currhead"); |
| 391 | Instruction *EntryMapPtr = CreateGEP(AtEntry, StackEntry,0,1,"gc_frame.map"); |
| 392 | AtEntry.CreateStore(FrameMap, EntryMapPtr); |
| 393 | |
| 394 | // After all the allocas... |
| 395 | for (unsigned I = 0, E = Roots.size(); I != E; ++I) { |
| 396 | // For each root, find the corresponding slot in the aggregate... |
| 397 | Value *SlotPtr = CreateGEP(AtEntry, StackEntry, 1 + I, "gc_root"); |
| 398 | |
| 399 | // And use it in lieu of the alloca. |
| 400 | AllocaInst *OriginalAlloca = Roots[I].second; |
| 401 | SlotPtr->takeName(OriginalAlloca); |
| 402 | OriginalAlloca->replaceAllUsesWith(SlotPtr); |
| 403 | } |
| 404 | |
| 405 | // Move past the original stores inserted by Collector::InitRoots. This isn't |
| 406 | // really necessary (the collector would never see the intermediate state), |
| 407 | // but it's nicer not to push the half-initialized entry onto the stack. |
| 408 | while (isa<StoreInst>(IP)) ++IP; |
| 409 | AtEntry.SetInsertPoint(IP->getParent(), IP); |
| 410 | |
| 411 | // Push the entry onto the shadow stack. |
| 412 | Instruction *EntryNextPtr = CreateGEP(AtEntry,StackEntry,0,0,"gc_frame.next"); |
| 413 | Instruction *NewHeadVal = CreateGEP(AtEntry,StackEntry, 0, "gc_newhead"); |
| 414 | AtEntry.CreateStore(CurrentHead, EntryNextPtr); |
| 415 | AtEntry.CreateStore(NewHeadVal, Head); |
| 416 | |
| 417 | // For each instruction that escapes... |
| 418 | EscapeEnumerator EE(F, "gc_cleanup"); |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 419 | while (IRBuilder *AtExit = EE.Next()) { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 420 | // Pop the entry from the shadow stack. Don't reuse CurrentHead from |
| 421 | // AtEntry, since that would make the value live for the entire function. |
| 422 | Instruction *EntryNextPtr2 = CreateGEP(*AtExit, StackEntry, 0, 0, |
| 423 | "gc_frame.next"); |
| 424 | Value *SavedHead = AtExit->CreateLoad(EntryNextPtr2, "gc_savedhead"); |
| 425 | AtExit->CreateStore(SavedHead, Head); |
| 426 | } |
| 427 | |
| 428 | // Delete the original allocas (which are no longer used) and the intrinsic |
| 429 | // calls (which are no longer valid). Doing this last avoids invalidating |
| 430 | // iterators. |
| 431 | for (unsigned I = 0, E = Roots.size(); I != E; ++I) { |
| 432 | Roots[I].first->eraseFromParent(); |
| 433 | Roots[I].second->eraseFromParent(); |
| 434 | } |
| 435 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 436 | Roots.clear(); |
| 437 | return true; |
| 438 | } |