Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 1 | //===-- LowerGC.cpp - Provide GC support for targets that don't -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 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 as efficient as it would be for targets that natively |
| 13 | // support the GC intrinsics, but it is useful for getting new targets |
| 14 | // up-and-running quickly. |
| 15 | // |
| 16 | // This pass implements the code transformation described in this paper: |
| 17 | // "Accurate Garbage Collection in an Uncooperative Environment" |
Chris Lattner | f52988a | 2004-05-23 21:27:29 +0000 | [diff] [blame] | 18 | // Fergus Henderson, ISMM, 2002 |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #define DEBUG_TYPE "lowergc" |
| 23 | #include "llvm/Transforms/Scalar.h" |
| 24 | #include "llvm/Constants.h" |
| 25 | #include "llvm/DerivedTypes.h" |
| 26 | #include "llvm/Instructions.h" |
| 27 | #include "llvm/Module.h" |
| 28 | #include "llvm/Pass.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Compiler.h" |
David Greene | 52eec54 | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
| 33 | namespace { |
Chris Lattner | f4b5461 | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 34 | class VISIBILITY_HIDDEN LowerGC : public FunctionPass { |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 35 | /// GCRootInt, GCReadInt, GCWriteInt - The function prototypes for the |
| 36 | /// llvm.gcread/llvm.gcwrite/llvm.gcroot intrinsics. |
| 37 | Function *GCRootInt, *GCReadInt, *GCWriteInt; |
| 38 | |
| 39 | /// GCRead/GCWrite - These are the functions provided by the garbage |
| 40 | /// collector for read/write barriers. |
Chris Lattner | 9214196 | 2007-01-07 06:58:05 +0000 | [diff] [blame] | 41 | Constant *GCRead, *GCWrite; |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 42 | |
| 43 | /// RootChain - This is the global linked-list that contains the chain of GC |
| 44 | /// roots. |
| 45 | GlobalVariable *RootChain; |
| 46 | |
| 47 | /// MainRootRecordType - This is the type for a function root entry if it |
| 48 | /// had zero roots. |
| 49 | const Type *MainRootRecordType; |
| 50 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 51 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 52 | LowerGC() : FunctionPass((intptr_t)&ID), |
| 53 | GCRootInt(0), GCReadInt(0), GCWriteInt(0), |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 54 | GCRead(0), GCWrite(0), RootChain(0), MainRootRecordType(0) {} |
| 55 | virtual bool doInitialization(Module &M); |
| 56 | virtual bool runOnFunction(Function &F); |
| 57 | |
| 58 | private: |
| 59 | const StructType *getRootRecordType(unsigned NumRoots); |
| 60 | }; |
| 61 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 62 | char LowerGC::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 63 | RegisterPass<LowerGC> |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 64 | X("lowergc", "Lower GC intrinsics, for GCless code generators"); |
| 65 | } |
| 66 | |
| 67 | /// createLowerGCPass - This function returns an instance of the "lowergc" |
| 68 | /// pass, which lowers garbage collection intrinsics to normal LLVM code. |
| 69 | FunctionPass *llvm::createLowerGCPass() { |
| 70 | return new LowerGC(); |
| 71 | } |
| 72 | |
| 73 | /// getRootRecordType - This function creates and returns the type for a root |
| 74 | /// record containing 'NumRoots' roots. |
| 75 | const StructType *LowerGC::getRootRecordType(unsigned NumRoots) { |
| 76 | // Build a struct that is a type used for meta-data/root pairs. |
| 77 | std::vector<const Type *> ST; |
| 78 | ST.push_back(GCRootInt->getFunctionType()->getParamType(0)); |
| 79 | ST.push_back(GCRootInt->getFunctionType()->getParamType(1)); |
| 80 | StructType *PairTy = StructType::get(ST); |
| 81 | |
| 82 | // Build the array of pairs. |
| 83 | ArrayType *PairArrTy = ArrayType::get(PairTy, NumRoots); |
| 84 | |
| 85 | // Now build the recursive list type. |
| 86 | PATypeHolder RootListH = |
| 87 | MainRootRecordType ? (Type*)MainRootRecordType : (Type*)OpaqueType::get(); |
| 88 | ST.clear(); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 89 | ST.push_back(PointerType::getUnqual(RootListH)); // Prev pointer |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 90 | ST.push_back(Type::Int32Ty); // NumElements in array |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 91 | ST.push_back(PairArrTy); // The pairs |
| 92 | StructType *RootList = StructType::get(ST); |
| 93 | if (MainRootRecordType) |
| 94 | return RootList; |
| 95 | |
| 96 | assert(NumRoots == 0 && "The main struct type should have zero entries!"); |
| 97 | cast<OpaqueType>((Type*)RootListH.get())->refineAbstractTypeTo(RootList); |
| 98 | MainRootRecordType = RootListH; |
| 99 | return cast<StructType>(RootListH.get()); |
| 100 | } |
| 101 | |
| 102 | /// doInitialization - If this module uses the GC intrinsics, find them now. If |
| 103 | /// not, this pass does not do anything. |
| 104 | bool LowerGC::doInitialization(Module &M) { |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 105 | GCRootInt = M.getFunction("llvm.gcroot"); |
| 106 | GCReadInt = M.getFunction("llvm.gcread"); |
| 107 | GCWriteInt = M.getFunction("llvm.gcwrite"); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 108 | if (!GCRootInt && !GCReadInt && !GCWriteInt) return false; |
| 109 | |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 110 | PointerType *VoidPtr = PointerType::getUnqual(Type::Int8Ty); |
| 111 | PointerType *VoidPtrPtr = PointerType::getUnqual(VoidPtr); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 112 | |
| 113 | // If the program is using read/write barriers, find the implementations of |
| 114 | // them from the GC runtime library. |
| 115 | if (GCReadInt) // Make: sbyte* %llvm_gc_read(sbyte**) |
Jeff Cohen | 66c5fd6 | 2005-10-23 04:37:20 +0000 | [diff] [blame] | 116 | GCRead = M.getOrInsertFunction("llvm_gc_read", VoidPtr, VoidPtr, VoidPtrPtr, |
| 117 | (Type *)0); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 118 | if (GCWriteInt) // Make: void %llvm_gc_write(sbyte*, sbyte**) |
| 119 | GCWrite = M.getOrInsertFunction("llvm_gc_write", Type::VoidTy, |
Jeff Cohen | 66c5fd6 | 2005-10-23 04:37:20 +0000 | [diff] [blame] | 120 | VoidPtr, VoidPtr, VoidPtrPtr, (Type *)0); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 121 | |
| 122 | // If the program has GC roots, get or create the global root list. |
| 123 | if (GCRootInt) { |
| 124 | const StructType *RootListTy = getRootRecordType(0); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 125 | const Type *PRLTy = PointerType::getUnqual(RootListTy); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 126 | M.addTypeName("llvm_gc_root_ty", RootListTy); |
| 127 | |
| 128 | // Get the root chain if it already exists. |
| 129 | RootChain = M.getGlobalVariable("llvm_gc_root_chain", PRLTy); |
| 130 | if (RootChain == 0) { |
| 131 | // If the root chain does not exist, insert a new one with linkonce |
| 132 | // linkage! |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 133 | RootChain = new GlobalVariable(PRLTy, false, |
Chris Lattner | 2f48686 | 2004-10-27 03:55:24 +0000 | [diff] [blame] | 134 | GlobalValue::LinkOnceLinkage, |
| 135 | Constant::getNullValue(PRLTy), |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 136 | "llvm_gc_root_chain", &M); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 137 | } else if (RootChain->hasExternalLinkage() && RootChain->isDeclaration()) { |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 138 | RootChain->setInitializer(Constant::getNullValue(PRLTy)); |
| 139 | RootChain->setLinkage(GlobalValue::LinkOnceLinkage); |
| 140 | } |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | /// Coerce - If the specified operand number of the specified instruction does |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 146 | /// not have the specified type, insert a cast. Note that this only uses BitCast |
| 147 | /// because the types involved are all pointers. |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 148 | static void Coerce(Instruction *I, unsigned OpNum, Type *Ty) { |
| 149 | if (I->getOperand(OpNum)->getType() != Ty) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 150 | if (Constant *C = dyn_cast<Constant>(I->getOperand(OpNum))) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 151 | I->setOperand(OpNum, ConstantExpr::getBitCast(C, Ty)); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 152 | else { |
Reid Spencer | 7b06bd5 | 2006-12-13 00:50:17 +0000 | [diff] [blame] | 153 | CastInst *CI = new BitCastInst(I->getOperand(OpNum), Ty, "", I); |
Reid Spencer | 17e6e44 | 2004-10-18 14:38:48 +0000 | [diff] [blame] | 154 | I->setOperand(OpNum, CI); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /// runOnFunction - If the program is using GC intrinsics, replace any |
| 160 | /// read/write intrinsics with the appropriate read/write barrier calls, then |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 161 | /// inline them. Finally, build the data structures for |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 162 | bool LowerGC::runOnFunction(Function &F) { |
| 163 | // Quick exit for programs that are not using GC mechanisms. |
| 164 | if (!GCRootInt && !GCReadInt && !GCWriteInt) return false; |
| 165 | |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 166 | PointerType *VoidPtr = PointerType::getUnqual(Type::Int8Ty); |
| 167 | PointerType *VoidPtrPtr = PointerType::getUnqual(VoidPtr); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 168 | |
| 169 | // If there are read/write barriers in the program, perform a quick pass over |
| 170 | // the function eliminating them. While we are at it, remember where we see |
| 171 | // calls to llvm.gcroot. |
| 172 | std::vector<CallInst*> GCRoots; |
| 173 | std::vector<CallInst*> NormalCalls; |
| 174 | |
| 175 | bool MadeChange = false; |
| 176 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 177 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) |
| 178 | if (CallInst *CI = dyn_cast<CallInst>(II++)) { |
| 179 | if (!CI->getCalledFunction() || |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 180 | !CI->getCalledFunction()->isIntrinsic()) |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 181 | NormalCalls.push_back(CI); // Remember all normal function calls. |
| 182 | |
| 183 | if (Function *F = CI->getCalledFunction()) |
| 184 | if (F == GCRootInt) |
| 185 | GCRoots.push_back(CI); |
| 186 | else if (F == GCReadInt || F == GCWriteInt) { |
| 187 | if (F == GCWriteInt) { |
| 188 | // Change a llvm.gcwrite call to call llvm_gc_write instead. |
| 189 | CI->setOperand(0, GCWrite); |
| 190 | // Insert casts of the operands as needed. |
| 191 | Coerce(CI, 1, VoidPtr); |
Chris Lattner | 93c9587 | 2004-07-22 05:51:13 +0000 | [diff] [blame] | 192 | Coerce(CI, 2, VoidPtr); |
| 193 | Coerce(CI, 3, VoidPtrPtr); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 194 | } else { |
Chris Lattner | 93c9587 | 2004-07-22 05:51:13 +0000 | [diff] [blame] | 195 | Coerce(CI, 1, VoidPtr); |
| 196 | Coerce(CI, 2, VoidPtrPtr); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 197 | if (CI->getType() == VoidPtr) { |
| 198 | CI->setOperand(0, GCRead); |
| 199 | } else { |
| 200 | // Create a whole new call to replace the old one. |
David Greene | 52eec54 | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 201 | |
| 202 | // It sure would be nice to pass op_begin()+1, |
| 203 | // op_begin()+2 but it runs into trouble with |
David Greene | 32fe3de | 2007-08-07 16:52:03 +0000 | [diff] [blame] | 204 | // CallInst::init's &*iterator, which requires a |
David Greene | 52eec54 | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 205 | // conversion from Use* to Value*. The conversion |
| 206 | // from Use to Value * is not useful because the |
| 207 | // memory for Value * won't be contiguous. |
David Greene | 242be6e | 2007-08-06 15:09:17 +0000 | [diff] [blame] | 208 | Value* Args[] = { |
| 209 | CI->getOperand(1), |
| 210 | CI->getOperand(2) |
| 211 | }; |
| 212 | CallInst *NC = new CallInst(GCRead, Args, Args + 2, |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 213 | CI->getName(), CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 214 | // These functions only deal with ptr type results so BitCast |
| 215 | // is the correct kind of cast (no-op cast). |
| 216 | Value *NV = new BitCastInst(NC, CI->getType(), "", CI); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 217 | CI->replaceAllUsesWith(NV); |
| 218 | BB->getInstList().erase(CI); |
| 219 | CI = NC; |
| 220 | } |
| 221 | } |
| 222 | |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 223 | MadeChange = true; |
| 224 | } |
| 225 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 227 | // If there are no GC roots in this function, then there is no need to create |
| 228 | // a GC list record for it. |
| 229 | if (GCRoots.empty()) return MadeChange; |
| 230 | |
| 231 | // Okay, there are GC roots in this function. On entry to the function, add a |
| 232 | // record to the llvm_gc_root_chain, and remove it on exit. |
| 233 | |
| 234 | // Create the alloca, and zero it out. |
| 235 | const StructType *RootListTy = getRootRecordType(GCRoots.size()); |
| 236 | AllocaInst *AI = new AllocaInst(RootListTy, 0, "gcroots", F.begin()->begin()); |
| 237 | |
| 238 | // Insert the memset call after all of the allocas in the function. |
| 239 | BasicBlock::iterator IP = AI; |
| 240 | while (isa<AllocaInst>(IP)) ++IP; |
| 241 | |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 242 | Constant *Zero = ConstantInt::get(Type::Int32Ty, 0); |
| 243 | Constant *One = ConstantInt::get(Type::Int32Ty, 1); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 244 | |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 245 | Value *Idx[2] = { Zero, Zero }; |
| 246 | |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 247 | // Get a pointer to the prev pointer. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 248 | Value *PrevPtrPtr = new GetElementPtrInst(AI, Idx, Idx + 2, |
| 249 | "prevptrptr", IP); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 250 | |
| 251 | // Load the previous pointer. |
| 252 | Value *PrevPtr = new LoadInst(RootChain, "prevptr", IP); |
| 253 | // Store the previous pointer into the prevptrptr |
| 254 | new StoreInst(PrevPtr, PrevPtrPtr, IP); |
| 255 | |
| 256 | // Set the number of elements in this record. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 257 | Idx[1] = One; |
| 258 | Value *NumEltsPtr = new GetElementPtrInst(AI, Idx, Idx + 2, |
| 259 | "numeltsptr", IP); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 260 | new StoreInst(ConstantInt::get(Type::Int32Ty, GCRoots.size()), NumEltsPtr,IP); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 261 | |
Chris Lattner | fbbe92f | 2007-01-31 20:08:52 +0000 | [diff] [blame] | 262 | Value* Par[4]; |
| 263 | Par[0] = Zero; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 264 | Par[1] = ConstantInt::get(Type::Int32Ty, 2); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 265 | |
| 266 | const PointerType *PtrLocTy = |
| 267 | cast<PointerType>(GCRootInt->getFunctionType()->getParamType(0)); |
| 268 | Constant *Null = ConstantPointerNull::get(PtrLocTy); |
| 269 | |
Evan Cheng | 4e9c473 | 2007-09-01 02:00:51 +0000 | [diff] [blame] | 270 | // Initialize all of the gcroot records now. |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 271 | for (unsigned i = 0, e = GCRoots.size(); i != e; ++i) { |
| 272 | // Initialize the meta-data pointer. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 273 | Par[2] = ConstantInt::get(Type::Int32Ty, i); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 274 | Par[3] = One; |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 275 | Value *MetaDataPtr = new GetElementPtrInst(AI, Par, Par + 4, |
| 276 | "MetaDataPtr", IP); |
Reid Spencer | 48dc46a | 2004-07-18 00:29:57 +0000 | [diff] [blame] | 277 | assert(isa<Constant>(GCRoots[i]->getOperand(2)) && "Must be a constant"); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 278 | new StoreInst(GCRoots[i]->getOperand(2), MetaDataPtr, IP); |
| 279 | |
| 280 | // Initialize the root pointer to null on entry to the function. |
| 281 | Par[3] = Zero; |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 282 | Value *RootPtrPtr = new GetElementPtrInst(AI, Par, Par + 4, |
| 283 | "RootEntPtr", IP); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 284 | new StoreInst(Null, RootPtrPtr, IP); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 286 | // Each occurrance of the llvm.gcroot intrinsic now turns into an |
Chris Lattner | 36597a5 | 2007-09-12 17:53:10 +0000 | [diff] [blame] | 287 | // initialization of the slot with the address. |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 288 | new StoreInst(GCRoots[i]->getOperand(1), RootPtrPtr, GCRoots[i]); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // Now that the record is all initialized, store the pointer into the global |
| 292 | // pointer. |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 293 | Value *C = new BitCastInst(AI, PointerType::getUnqual(MainRootRecordType), "", IP); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 294 | new StoreInst(C, RootChain, IP); |
| 295 | |
Evan Cheng | 4e9c473 | 2007-09-01 02:00:51 +0000 | [diff] [blame] | 296 | // Eliminate all the gcroot records now. |
| 297 | for (unsigned i = 0, e = GCRoots.size(); i != e; ++i) |
| 298 | GCRoots[i]->getParent()->getInstList().erase(GCRoots[i]); |
| 299 | |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 300 | // On exit from the function we have to remove the entry from the GC root |
| 301 | // chain. Doing this is straight-forward for return and unwind instructions: |
| 302 | // just insert the appropriate copy. |
| 303 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 304 | if (isa<UnwindInst>(BB->getTerminator()) || |
| 305 | isa<ReturnInst>(BB->getTerminator())) { |
| 306 | // We could reuse the PrevPtr loaded on entry to the function, but this |
| 307 | // would make the value live for the whole function, which is probably a |
| 308 | // bad idea. Just reload the value out of our stack entry. |
| 309 | PrevPtr = new LoadInst(PrevPtrPtr, "prevptr", BB->getTerminator()); |
| 310 | new StoreInst(PrevPtr, RootChain, BB->getTerminator()); |
| 311 | } |
| 312 | |
| 313 | // If an exception is thrown from a callee we have to make sure to |
| 314 | // unconditionally take the record off the stack. For this reason, we turn |
| 315 | // all call instructions into invoke whose cleanup pops the entry off the |
| 316 | // stack. We only insert one cleanup block, which is shared by all invokes. |
| 317 | if (!NormalCalls.empty()) { |
| 318 | // Create the shared cleanup block. |
| 319 | BasicBlock *Cleanup = new BasicBlock("gc_cleanup", &F); |
| 320 | UnwindInst *UI = new UnwindInst(Cleanup); |
| 321 | PrevPtr = new LoadInst(PrevPtrPtr, "prevptr", UI); |
| 322 | new StoreInst(PrevPtr, RootChain, UI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 324 | // Loop over all of the function calls, turning them into invokes. |
| 325 | while (!NormalCalls.empty()) { |
| 326 | CallInst *CI = NormalCalls.back(); |
| 327 | BasicBlock *CBB = CI->getParent(); |
| 328 | NormalCalls.pop_back(); |
| 329 | |
| 330 | // Split the basic block containing the function call. |
| 331 | BasicBlock *NewBB = CBB->splitBasicBlock(CI, CBB->getName()+".cont"); |
| 332 | |
| 333 | // Remove the unconditional branch inserted at the end of the CBB. |
| 334 | CBB->getInstList().pop_back(); |
| 335 | NewBB->getInstList().remove(CI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 337 | // Create a new invoke instruction. |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 338 | std::vector<Value*> Args(CI->op_begin()+1, CI->op_end()); |
| 339 | |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 340 | Value *II = new InvokeInst(CI->getCalledValue(), NewBB, Cleanup, |
David Greene | f1355a5 | 2007-08-27 19:04:21 +0000 | [diff] [blame] | 341 | Args.begin(), Args.end(), CI->getName(), CBB); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 342 | cast<InvokeInst>(II)->setCallingConv(CI->getCallingConv()); |
| 343 | cast<InvokeInst>(II)->setParamAttrs(CI->getParamAttrs()); |
Chris Lattner | 08005df | 2004-05-23 21:19:22 +0000 | [diff] [blame] | 344 | CI->replaceAllUsesWith(II); |
| 345 | delete CI; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return true; |
| 350 | } |