1283625Sdim//===-- ShadowStackGCLowering.cpp - Custom lowering for shadow-stack gc ---===//
2283625Sdim//
3283625Sdim//                     The LLVM Compiler Infrastructure
4283625Sdim//
5283625Sdim// This file is distributed under the University of Illinois Open Source
6283625Sdim// License. See LICENSE.TXT for details.
7283625Sdim//
8283625Sdim//===----------------------------------------------------------------------===//
9283625Sdim//
10283625Sdim// This file contains the custom lowering code required by the shadow-stack GC
11283625Sdim// strategy.
12283625Sdim//
13283625Sdim//===----------------------------------------------------------------------===//
14283625Sdim
15283625Sdim#include "llvm/CodeGen/Passes.h"
16283625Sdim#include "llvm/ADT/StringExtras.h"
17283625Sdim#include "llvm/CodeGen/GCStrategy.h"
18283625Sdim#include "llvm/IR/CallSite.h"
19283625Sdim#include "llvm/IR/IRBuilder.h"
20283625Sdim#include "llvm/IR/IntrinsicInst.h"
21283625Sdim#include "llvm/IR/Module.h"
22283625Sdim
23283625Sdimusing namespace llvm;
24283625Sdim
25283625Sdim#define DEBUG_TYPE "shadowstackgclowering"
26283625Sdim
27283625Sdimnamespace {
28283625Sdim
29283625Sdimclass ShadowStackGCLowering : public FunctionPass {
30283625Sdim  /// RootChain - This is the global linked-list that contains the chain of GC
31283625Sdim  /// roots.
32283625Sdim  GlobalVariable *Head;
33283625Sdim
34283625Sdim  /// StackEntryTy - Abstract type of a link in the shadow stack.
35283625Sdim  ///
36283625Sdim  StructType *StackEntryTy;
37283625Sdim  StructType *FrameMapTy;
38283625Sdim
39283625Sdim  /// Roots - GC roots in the current function. Each is a pair of the
40283625Sdim  /// intrinsic call and its corresponding alloca.
41283625Sdim  std::vector<std::pair<CallInst *, AllocaInst *>> Roots;
42283625Sdim
43283625Sdimpublic:
44283625Sdim  static char ID;
45283625Sdim  ShadowStackGCLowering();
46283625Sdim
47283625Sdim  bool doInitialization(Module &M) override;
48283625Sdim  bool runOnFunction(Function &F) override;
49283625Sdim
50283625Sdimprivate:
51283625Sdim  bool IsNullValue(Value *V);
52283625Sdim  Constant *GetFrameMap(Function &F);
53283625Sdim  Type *GetConcreteStackEntryType(Function &F);
54283625Sdim  void CollectRoots(Function &F);
55283625Sdim  static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B,
56283625Sdim                                      Type *Ty, Value *BasePtr, int Idx1,
57283625Sdim                                      const char *Name);
58283625Sdim  static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B,
59283625Sdim                                      Type *Ty, Value *BasePtr, int Idx1, int Idx2,
60283625Sdim                                      const char *Name);
61283625Sdim};
62285181Sdim}
63283625Sdim
64283625SdimINITIALIZE_PASS_BEGIN(ShadowStackGCLowering, "shadow-stack-gc-lowering",
65283625Sdim                      "Shadow Stack GC Lowering", false, false)
66283625SdimINITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
67283625SdimINITIALIZE_PASS_END(ShadowStackGCLowering, "shadow-stack-gc-lowering",
68283625Sdim                    "Shadow Stack GC Lowering", false, false)
69283625Sdim
70283625SdimFunctionPass *llvm::createShadowStackGCLoweringPass() { return new ShadowStackGCLowering(); }
71283625Sdim
72283625Sdimchar ShadowStackGCLowering::ID = 0;
73283625Sdim
74283625SdimShadowStackGCLowering::ShadowStackGCLowering()
75283625Sdim  : FunctionPass(ID), Head(nullptr), StackEntryTy(nullptr),
76283625Sdim    FrameMapTy(nullptr) {
77283625Sdim  initializeShadowStackGCLoweringPass(*PassRegistry::getPassRegistry());
78283625Sdim}
79283625Sdim
80283625Sdimnamespace {
81283625Sdim/// EscapeEnumerator - This is a little algorithm to find all escape points
82283625Sdim/// from a function so that "finally"-style code can be inserted. In addition
83283625Sdim/// to finding the existing return and unwind instructions, it also (if
84283625Sdim/// necessary) transforms any call instructions into invokes and sends them to
85283625Sdim/// a landing pad.
86283625Sdim///
87283625Sdim/// It's wrapped up in a state machine using the same transform C# uses for
88283625Sdim/// 'yield return' enumerators, This transform allows it to be non-allocating.
89283625Sdimclass EscapeEnumerator {
90283625Sdim  Function &F;
91283625Sdim  const char *CleanupBBName;
92283625Sdim
93283625Sdim  // State.
94283625Sdim  int State;
95283625Sdim  Function::iterator StateBB, StateE;
96283625Sdim  IRBuilder<> Builder;
97283625Sdim
98283625Sdimpublic:
99283625Sdim  EscapeEnumerator(Function &F, const char *N = "cleanup")
100283625Sdim      : F(F), CleanupBBName(N), State(0), Builder(F.getContext()) {}
101283625Sdim
102283625Sdim  IRBuilder<> *Next() {
103283625Sdim    switch (State) {
104283625Sdim    default:
105283625Sdim      return nullptr;
106283625Sdim
107283625Sdim    case 0:
108283625Sdim      StateBB = F.begin();
109283625Sdim      StateE = F.end();
110283625Sdim      State = 1;
111283625Sdim
112283625Sdim    case 1:
113283625Sdim      // Find all 'return', 'resume', and 'unwind' instructions.
114283625Sdim      while (StateBB != StateE) {
115296417Sdim        BasicBlock *CurBB = &*StateBB++;
116283625Sdim
117283625Sdim        // Branches and invokes do not escape, only unwind, resume, and return
118283625Sdim        // do.
119283625Sdim        TerminatorInst *TI = CurBB->getTerminator();
120283625Sdim        if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI))
121283625Sdim          continue;
122283625Sdim
123296417Sdim        Builder.SetInsertPoint(TI);
124283625Sdim        return &Builder;
125283625Sdim      }
126283625Sdim
127283625Sdim      State = 2;
128283625Sdim
129283625Sdim      // Find all 'call' instructions.
130283625Sdim      SmallVector<Instruction *, 16> Calls;
131283625Sdim      for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
132283625Sdim        for (BasicBlock::iterator II = BB->begin(), EE = BB->end(); II != EE;
133283625Sdim             ++II)
134283625Sdim          if (CallInst *CI = dyn_cast<CallInst>(II))
135283625Sdim            if (!CI->getCalledFunction() ||
136283625Sdim                !CI->getCalledFunction()->getIntrinsicID())
137283625Sdim              Calls.push_back(CI);
138283625Sdim
139283625Sdim      if (Calls.empty())
140283625Sdim        return nullptr;
141283625Sdim
142283625Sdim      // Create a cleanup block.
143283625Sdim      LLVMContext &C = F.getContext();
144283625Sdim      BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
145283625Sdim      Type *ExnTy =
146283625Sdim          StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C), nullptr);
147284734Sdim      if (!F.hasPersonalityFn()) {
148284734Sdim        Constant *PersFn = F.getParent()->getOrInsertFunction(
149284734Sdim            "__gcc_personality_v0",
150284734Sdim            FunctionType::get(Type::getInt32Ty(C), true));
151284734Sdim        F.setPersonalityFn(PersFn);
152284734Sdim      }
153283625Sdim      LandingPadInst *LPad =
154284734Sdim          LandingPadInst::Create(ExnTy, 1, "cleanup.lpad", CleanupBB);
155283625Sdim      LPad->setCleanup(true);
156283625Sdim      ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
157283625Sdim
158283625Sdim      // Transform the 'call' instructions into 'invoke's branching to the
159283625Sdim      // cleanup block. Go in reverse order to make prettier BB names.
160283625Sdim      SmallVector<Value *, 16> Args;
161283625Sdim      for (unsigned I = Calls.size(); I != 0;) {
162283625Sdim        CallInst *CI = cast<CallInst>(Calls[--I]);
163283625Sdim
164283625Sdim        // Split the basic block containing the function call.
165283625Sdim        BasicBlock *CallBB = CI->getParent();
166296417Sdim        BasicBlock *NewBB = CallBB->splitBasicBlock(
167296417Sdim            CI->getIterator(), CallBB->getName() + ".cont");
168283625Sdim
169283625Sdim        // Remove the unconditional branch inserted at the end of CallBB.
170283625Sdim        CallBB->getInstList().pop_back();
171283625Sdim        NewBB->getInstList().remove(CI);
172283625Sdim
173283625Sdim        // Create a new invoke instruction.
174283625Sdim        Args.clear();
175283625Sdim        CallSite CS(CI);
176283625Sdim        Args.append(CS.arg_begin(), CS.arg_end());
177283625Sdim
178283625Sdim        InvokeInst *II =
179283625Sdim            InvokeInst::Create(CI->getCalledValue(), NewBB, CleanupBB, Args,
180283625Sdim                               CI->getName(), CallBB);
181283625Sdim        II->setCallingConv(CI->getCallingConv());
182283625Sdim        II->setAttributes(CI->getAttributes());
183283625Sdim        CI->replaceAllUsesWith(II);
184283625Sdim        delete CI;
185283625Sdim      }
186283625Sdim
187296417Sdim      Builder.SetInsertPoint(RI);
188283625Sdim      return &Builder;
189283625Sdim    }
190283625Sdim  }
191283625Sdim};
192285181Sdim}
193283625Sdim
194283625Sdim
195283625SdimConstant *ShadowStackGCLowering::GetFrameMap(Function &F) {
196283625Sdim  // doInitialization creates the abstract type of this value.
197283625Sdim  Type *VoidPtr = Type::getInt8PtrTy(F.getContext());
198283625Sdim
199283625Sdim  // Truncate the ShadowStackDescriptor if some metadata is null.
200283625Sdim  unsigned NumMeta = 0;
201283625Sdim  SmallVector<Constant *, 16> Metadata;
202283625Sdim  for (unsigned I = 0; I != Roots.size(); ++I) {
203283625Sdim    Constant *C = cast<Constant>(Roots[I].first->getArgOperand(1));
204283625Sdim    if (!C->isNullValue())
205283625Sdim      NumMeta = I + 1;
206283625Sdim    Metadata.push_back(ConstantExpr::getBitCast(C, VoidPtr));
207283625Sdim  }
208283625Sdim  Metadata.resize(NumMeta);
209283625Sdim
210283625Sdim  Type *Int32Ty = Type::getInt32Ty(F.getContext());
211283625Sdim
212283625Sdim  Constant *BaseElts[] = {
213283625Sdim      ConstantInt::get(Int32Ty, Roots.size(), false),
214283625Sdim      ConstantInt::get(Int32Ty, NumMeta, false),
215283625Sdim  };
216283625Sdim
217283625Sdim  Constant *DescriptorElts[] = {
218283625Sdim      ConstantStruct::get(FrameMapTy, BaseElts),
219283625Sdim      ConstantArray::get(ArrayType::get(VoidPtr, NumMeta), Metadata)};
220283625Sdim
221283625Sdim  Type *EltTys[] = {DescriptorElts[0]->getType(), DescriptorElts[1]->getType()};
222283625Sdim  StructType *STy = StructType::create(EltTys, "gc_map." + utostr(NumMeta));
223283625Sdim
224283625Sdim  Constant *FrameMap = ConstantStruct::get(STy, DescriptorElts);
225283625Sdim
226283625Sdim  // FIXME: Is this actually dangerous as WritingAnLLVMPass.html claims? Seems
227283625Sdim  //        that, short of multithreaded LLVM, it should be safe; all that is
228283625Sdim  //        necessary is that a simple Module::iterator loop not be invalidated.
229283625Sdim  //        Appending to the GlobalVariable list is safe in that sense.
230283625Sdim  //
231283625Sdim  //        All of the output passes emit globals last. The ExecutionEngine
232283625Sdim  //        explicitly supports adding globals to the module after
233283625Sdim  //        initialization.
234283625Sdim  //
235283625Sdim  //        Still, if it isn't deemed acceptable, then this transformation needs
236283625Sdim  //        to be a ModulePass (which means it cannot be in the 'llc' pipeline
237283625Sdim  //        (which uses a FunctionPassManager (which segfaults (not asserts) if
238283625Sdim  //        provided a ModulePass))).
239283625Sdim  Constant *GV = new GlobalVariable(*F.getParent(), FrameMap->getType(), true,
240283625Sdim                                    GlobalVariable::InternalLinkage, FrameMap,
241283625Sdim                                    "__gc_" + F.getName());
242283625Sdim
243283625Sdim  Constant *GEPIndices[2] = {
244283625Sdim      ConstantInt::get(Type::getInt32Ty(F.getContext()), 0),
245283625Sdim      ConstantInt::get(Type::getInt32Ty(F.getContext()), 0)};
246283625Sdim  return ConstantExpr::getGetElementPtr(FrameMap->getType(), GV, GEPIndices);
247283625Sdim}
248283625Sdim
249283625SdimType *ShadowStackGCLowering::GetConcreteStackEntryType(Function &F) {
250283625Sdim  // doInitialization creates the generic version of this type.
251283625Sdim  std::vector<Type *> EltTys;
252283625Sdim  EltTys.push_back(StackEntryTy);
253283625Sdim  for (size_t I = 0; I != Roots.size(); I++)
254283625Sdim    EltTys.push_back(Roots[I].second->getAllocatedType());
255283625Sdim
256283625Sdim  return StructType::create(EltTys, ("gc_stackentry." + F.getName()).str());
257283625Sdim}
258283625Sdim
259283625Sdim/// doInitialization - If this module uses the GC intrinsics, find them now. If
260283625Sdim/// not, exit fast.
261283625Sdimbool ShadowStackGCLowering::doInitialization(Module &M) {
262283625Sdim  bool Active = false;
263283625Sdim  for (Function &F : M) {
264283625Sdim    if (F.hasGC() && F.getGC() == std::string("shadow-stack")) {
265283625Sdim      Active = true;
266283625Sdim      break;
267283625Sdim    }
268283625Sdim  }
269283625Sdim  if (!Active)
270283625Sdim    return false;
271283625Sdim
272283625Sdim  // struct FrameMap {
273283625Sdim  //   int32_t NumRoots; // Number of roots in stack frame.
274283625Sdim  //   int32_t NumMeta;  // Number of metadata descriptors. May be < NumRoots.
275283625Sdim  //   void *Meta[];     // May be absent for roots without metadata.
276283625Sdim  // };
277283625Sdim  std::vector<Type *> EltTys;
278283625Sdim  // 32 bits is ok up to a 32GB stack frame. :)
279283625Sdim  EltTys.push_back(Type::getInt32Ty(M.getContext()));
280283625Sdim  // Specifies length of variable length array.
281283625Sdim  EltTys.push_back(Type::getInt32Ty(M.getContext()));
282283625Sdim  FrameMapTy = StructType::create(EltTys, "gc_map");
283283625Sdim  PointerType *FrameMapPtrTy = PointerType::getUnqual(FrameMapTy);
284283625Sdim
285283625Sdim  // struct StackEntry {
286283625Sdim  //   ShadowStackEntry *Next; // Caller's stack entry.
287283625Sdim  //   FrameMap *Map;          // Pointer to constant FrameMap.
288283625Sdim  //   void *Roots[];          // Stack roots (in-place array, so we pretend).
289283625Sdim  // };
290283625Sdim
291283625Sdim  StackEntryTy = StructType::create(M.getContext(), "gc_stackentry");
292283625Sdim
293283625Sdim  EltTys.clear();
294283625Sdim  EltTys.push_back(PointerType::getUnqual(StackEntryTy));
295283625Sdim  EltTys.push_back(FrameMapPtrTy);
296283625Sdim  StackEntryTy->setBody(EltTys);
297283625Sdim  PointerType *StackEntryPtrTy = PointerType::getUnqual(StackEntryTy);
298283625Sdim
299283625Sdim  // Get the root chain if it already exists.
300283625Sdim  Head = M.getGlobalVariable("llvm_gc_root_chain");
301283625Sdim  if (!Head) {
302283625Sdim    // If the root chain does not exist, insert a new one with linkonce
303283625Sdim    // linkage!
304283625Sdim    Head = new GlobalVariable(
305283625Sdim        M, StackEntryPtrTy, false, GlobalValue::LinkOnceAnyLinkage,
306283625Sdim        Constant::getNullValue(StackEntryPtrTy), "llvm_gc_root_chain");
307283625Sdim  } else if (Head->hasExternalLinkage() && Head->isDeclaration()) {
308283625Sdim    Head->setInitializer(Constant::getNullValue(StackEntryPtrTy));
309283625Sdim    Head->setLinkage(GlobalValue::LinkOnceAnyLinkage);
310283625Sdim  }
311283625Sdim
312283625Sdim  return true;
313283625Sdim}
314283625Sdim
315283625Sdimbool ShadowStackGCLowering::IsNullValue(Value *V) {
316283625Sdim  if (Constant *C = dyn_cast<Constant>(V))
317283625Sdim    return C->isNullValue();
318283625Sdim  return false;
319283625Sdim}
320283625Sdim
321283625Sdimvoid ShadowStackGCLowering::CollectRoots(Function &F) {
322283625Sdim  // FIXME: Account for original alignment. Could fragment the root array.
323283625Sdim  //   Approach 1: Null initialize empty slots at runtime. Yuck.
324283625Sdim  //   Approach 2: Emit a map of the array instead of just a count.
325283625Sdim
326283625Sdim  assert(Roots.empty() && "Not cleaned up?");
327283625Sdim
328283625Sdim  SmallVector<std::pair<CallInst *, AllocaInst *>, 16> MetaRoots;
329283625Sdim
330283625Sdim  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
331283625Sdim    for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
332283625Sdim      if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++))
333283625Sdim        if (Function *F = CI->getCalledFunction())
334283625Sdim          if (F->getIntrinsicID() == Intrinsic::gcroot) {
335283625Sdim            std::pair<CallInst *, AllocaInst *> Pair = std::make_pair(
336283625Sdim                CI,
337283625Sdim                cast<AllocaInst>(CI->getArgOperand(0)->stripPointerCasts()));
338283625Sdim            if (IsNullValue(CI->getArgOperand(1)))
339283625Sdim              Roots.push_back(Pair);
340283625Sdim            else
341283625Sdim              MetaRoots.push_back(Pair);
342283625Sdim          }
343283625Sdim
344283625Sdim  // Number roots with metadata (usually empty) at the beginning, so that the
345283625Sdim  // FrameMap::Meta array can be elided.
346283625Sdim  Roots.insert(Roots.begin(), MetaRoots.begin(), MetaRoots.end());
347283625Sdim}
348283625Sdim
349283625SdimGetElementPtrInst *ShadowStackGCLowering::CreateGEP(LLVMContext &Context,
350283625Sdim                                                    IRBuilder<> &B, Type *Ty,
351283625Sdim                                                    Value *BasePtr, int Idx,
352283625Sdim                                                    int Idx2,
353283625Sdim                                                    const char *Name) {
354283625Sdim  Value *Indices[] = {ConstantInt::get(Type::getInt32Ty(Context), 0),
355283625Sdim                      ConstantInt::get(Type::getInt32Ty(Context), Idx),
356283625Sdim                      ConstantInt::get(Type::getInt32Ty(Context), Idx2)};
357283625Sdim  Value *Val = B.CreateGEP(Ty, BasePtr, Indices, Name);
358283625Sdim
359283625Sdim  assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant");
360283625Sdim
361283625Sdim  return dyn_cast<GetElementPtrInst>(Val);
362283625Sdim}
363283625Sdim
364283625SdimGetElementPtrInst *ShadowStackGCLowering::CreateGEP(LLVMContext &Context,
365283625Sdim                                            IRBuilder<> &B, Type *Ty, Value *BasePtr,
366283625Sdim                                            int Idx, const char *Name) {
367283625Sdim  Value *Indices[] = {ConstantInt::get(Type::getInt32Ty(Context), 0),
368283625Sdim                      ConstantInt::get(Type::getInt32Ty(Context), Idx)};
369283625Sdim  Value *Val = B.CreateGEP(Ty, BasePtr, Indices, Name);
370283625Sdim
371283625Sdim  assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant");
372283625Sdim
373283625Sdim  return dyn_cast<GetElementPtrInst>(Val);
374283625Sdim}
375283625Sdim
376283625Sdim/// runOnFunction - Insert code to maintain the shadow stack.
377283625Sdimbool ShadowStackGCLowering::runOnFunction(Function &F) {
378283625Sdim  // Quick exit for functions that do not use the shadow stack GC.
379283625Sdim  if (!F.hasGC() ||
380283625Sdim      F.getGC() != std::string("shadow-stack"))
381283625Sdim    return false;
382283625Sdim
383283625Sdim  LLVMContext &Context = F.getContext();
384283625Sdim
385283625Sdim  // Find calls to llvm.gcroot.
386283625Sdim  CollectRoots(F);
387283625Sdim
388283625Sdim  // If there are no roots in this function, then there is no need to add a
389283625Sdim  // stack map entry for it.
390283625Sdim  if (Roots.empty())
391283625Sdim    return false;
392283625Sdim
393283625Sdim  // Build the constant map and figure the type of the shadow stack entry.
394283625Sdim  Value *FrameMap = GetFrameMap(F);
395283625Sdim  Type *ConcreteStackEntryTy = GetConcreteStackEntryType(F);
396283625Sdim
397283625Sdim  // Build the shadow stack entry at the very start of the function.
398283625Sdim  BasicBlock::iterator IP = F.getEntryBlock().begin();
399283625Sdim  IRBuilder<> AtEntry(IP->getParent(), IP);
400283625Sdim
401283625Sdim  Instruction *StackEntry =
402283625Sdim      AtEntry.CreateAlloca(ConcreteStackEntryTy, nullptr, "gc_frame");
403283625Sdim
404283625Sdim  while (isa<AllocaInst>(IP))
405283625Sdim    ++IP;
406283625Sdim  AtEntry.SetInsertPoint(IP->getParent(), IP);
407283625Sdim
408283625Sdim  // Initialize the map pointer and load the current head of the shadow stack.
409283625Sdim  Instruction *CurrentHead = AtEntry.CreateLoad(Head, "gc_currhead");
410283625Sdim  Instruction *EntryMapPtr = CreateGEP(Context, AtEntry, ConcreteStackEntryTy,
411283625Sdim                                       StackEntry, 0, 1, "gc_frame.map");
412283625Sdim  AtEntry.CreateStore(FrameMap, EntryMapPtr);
413283625Sdim
414283625Sdim  // After all the allocas...
415283625Sdim  for (unsigned I = 0, E = Roots.size(); I != E; ++I) {
416283625Sdim    // For each root, find the corresponding slot in the aggregate...
417283625Sdim    Value *SlotPtr = CreateGEP(Context, AtEntry, ConcreteStackEntryTy,
418283625Sdim                               StackEntry, 1 + I, "gc_root");
419283625Sdim
420283625Sdim    // And use it in lieu of the alloca.
421283625Sdim    AllocaInst *OriginalAlloca = Roots[I].second;
422283625Sdim    SlotPtr->takeName(OriginalAlloca);
423283625Sdim    OriginalAlloca->replaceAllUsesWith(SlotPtr);
424283625Sdim  }
425283625Sdim
426283625Sdim  // Move past the original stores inserted by GCStrategy::InitRoots. This isn't
427283625Sdim  // really necessary (the collector would never see the intermediate state at
428283625Sdim  // runtime), but it's nicer not to push the half-initialized entry onto the
429283625Sdim  // shadow stack.
430283625Sdim  while (isa<StoreInst>(IP))
431283625Sdim    ++IP;
432283625Sdim  AtEntry.SetInsertPoint(IP->getParent(), IP);
433283625Sdim
434283625Sdim  // Push the entry onto the shadow stack.
435283625Sdim  Instruction *EntryNextPtr = CreateGEP(Context, AtEntry, ConcreteStackEntryTy,
436283625Sdim                                        StackEntry, 0, 0, "gc_frame.next");
437283625Sdim  Instruction *NewHeadVal = CreateGEP(Context, AtEntry, ConcreteStackEntryTy,
438283625Sdim                                      StackEntry, 0, "gc_newhead");
439283625Sdim  AtEntry.CreateStore(CurrentHead, EntryNextPtr);
440283625Sdim  AtEntry.CreateStore(NewHeadVal, Head);
441283625Sdim
442283625Sdim  // For each instruction that escapes...
443283625Sdim  EscapeEnumerator EE(F, "gc_cleanup");
444283625Sdim  while (IRBuilder<> *AtExit = EE.Next()) {
445283625Sdim    // Pop the entry from the shadow stack. Don't reuse CurrentHead from
446283625Sdim    // AtEntry, since that would make the value live for the entire function.
447283625Sdim    Instruction *EntryNextPtr2 =
448283625Sdim        CreateGEP(Context, *AtExit, ConcreteStackEntryTy, StackEntry, 0, 0,
449283625Sdim                  "gc_frame.next");
450283625Sdim    Value *SavedHead = AtExit->CreateLoad(EntryNextPtr2, "gc_savedhead");
451283625Sdim    AtExit->CreateStore(SavedHead, Head);
452283625Sdim  }
453283625Sdim
454283625Sdim  // Delete the original allocas (which are no longer used) and the intrinsic
455283625Sdim  // calls (which are no longer valid). Doing this last avoids invalidating
456283625Sdim  // iterators.
457283625Sdim  for (unsigned I = 0, E = Roots.size(); I != E; ++I) {
458283625Sdim    Roots[I].first->eraseFromParent();
459283625Sdim    Roots[I].second->eraseFromParent();
460283625Sdim  }
461283625Sdim
462283625Sdim  Roots.clear();
463283625Sdim  return true;
464283625Sdim}
465