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