blob: 89749862d24eaf238002f54a71166c58e2aa4e71 [file] [log] [blame]
Chris Lattner08005df2004-05-23 21:19:22 +00001//===-- LowerGC.cpp - Provide GC support for targets that don't -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner08005df2004-05-23 21:19:22 +00007//
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 Lattnerf52988a2004-05-23 21:27:29 +000018// Fergus Henderson, ISMM, 2002
Chris Lattner08005df2004-05-23 21:19:22 +000019//
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 Lattnera4f0b3a2006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
David Greene52eec542007-08-01 03:43:44 +000030#include "llvm/ADT/SmallVector.h"
Chris Lattner08005df2004-05-23 21:19:22 +000031using namespace llvm;
32
33namespace {
Chris Lattnerf4b54612006-06-28 22:08:15 +000034 class VISIBILITY_HIDDEN LowerGC : public FunctionPass {
Chris Lattner08005df2004-05-23 21:19:22 +000035 /// 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 Lattner92141962007-01-07 06:58:05 +000041 Constant *GCRead, *GCWrite;
Chris Lattner08005df2004-05-23 21:19:22 +000042
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 Lewyckyecd94c82007-05-06 13:37:16 +000051 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000052 LowerGC() : FunctionPass((intptr_t)&ID),
53 GCRootInt(0), GCReadInt(0), GCWriteInt(0),
Chris Lattner08005df2004-05-23 21:19:22 +000054 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 Patel19974732007-05-03 01:11:54 +000062 char LowerGC::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000063 RegisterPass<LowerGC>
Chris Lattner08005df2004-05-23 21:19:22 +000064 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.
69FunctionPass *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.
75const 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 Lamb43ad6b32007-12-17 01:12:55 +000089 ST.push_back(PointerType::getUnqual(RootListH)); // Prev pointer
Reid Spencerc5b206b2006-12-31 05:48:39 +000090 ST.push_back(Type::Int32Ty); // NumElements in array
Chris Lattner08005df2004-05-23 21:19:22 +000091 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.
104bool LowerGC::doInitialization(Module &M) {
Reid Spencer688b0492007-02-05 21:19:13 +0000105 GCRootInt = M.getFunction("llvm.gcroot");
106 GCReadInt = M.getFunction("llvm.gcread");
107 GCWriteInt = M.getFunction("llvm.gcwrite");
Chris Lattner08005df2004-05-23 21:19:22 +0000108 if (!GCRootInt && !GCReadInt && !GCWriteInt) return false;
109
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000110 PointerType *VoidPtr = PointerType::getUnqual(Type::Int8Ty);
111 PointerType *VoidPtrPtr = PointerType::getUnqual(VoidPtr);
Chris Lattner08005df2004-05-23 21:19:22 +0000112
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 Cohen66c5fd62005-10-23 04:37:20 +0000116 GCRead = M.getOrInsertFunction("llvm_gc_read", VoidPtr, VoidPtr, VoidPtrPtr,
117 (Type *)0);
Chris Lattner08005df2004-05-23 21:19:22 +0000118 if (GCWriteInt) // Make: void %llvm_gc_write(sbyte*, sbyte**)
119 GCWrite = M.getOrInsertFunction("llvm_gc_write", Type::VoidTy,
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000120 VoidPtr, VoidPtr, VoidPtrPtr, (Type *)0);
Chris Lattner08005df2004-05-23 21:19:22 +0000121
122 // If the program has GC roots, get or create the global root list.
123 if (GCRootInt) {
124 const StructType *RootListTy = getRootRecordType(0);
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000125 const Type *PRLTy = PointerType::getUnqual(RootListTy);
Chris Lattner08005df2004-05-23 21:19:22 +0000126 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 Brukmanfd939082005-04-21 23:48:37 +0000133 RootChain = new GlobalVariable(PRLTy, false,
Chris Lattner2f486862004-10-27 03:55:24 +0000134 GlobalValue::LinkOnceLinkage,
135 Constant::getNullValue(PRLTy),
Chris Lattner08005df2004-05-23 21:19:22 +0000136 "llvm_gc_root_chain", &M);
Reid Spencer5cbf9852007-01-30 20:08:39 +0000137 } else if (RootChain->hasExternalLinkage() && RootChain->isDeclaration()) {
Chris Lattner08005df2004-05-23 21:19:22 +0000138 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 Spencer3da59db2006-11-27 01:05:10 +0000146/// not have the specified type, insert a cast. Note that this only uses BitCast
147/// because the types involved are all pointers.
Chris Lattner08005df2004-05-23 21:19:22 +0000148static void Coerce(Instruction *I, unsigned OpNum, Type *Ty) {
149 if (I->getOperand(OpNum)->getType() != Ty) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000150 if (Constant *C = dyn_cast<Constant>(I->getOperand(OpNum)))
Reid Spencer3da59db2006-11-27 01:05:10 +0000151 I->setOperand(OpNum, ConstantExpr::getBitCast(C, Ty));
Chris Lattner08005df2004-05-23 21:19:22 +0000152 else {
Reid Spencer7b06bd52006-12-13 00:50:17 +0000153 CastInst *CI = new BitCastInst(I->getOperand(OpNum), Ty, "", I);
Reid Spencer17e6e442004-10-18 14:38:48 +0000154 I->setOperand(OpNum, CI);
Chris Lattner08005df2004-05-23 21:19:22 +0000155 }
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 Brukmanfd939082005-04-21 23:48:37 +0000161/// inline them. Finally, build the data structures for
Chris Lattner08005df2004-05-23 21:19:22 +0000162bool LowerGC::runOnFunction(Function &F) {
163 // Quick exit for programs that are not using GC mechanisms.
164 if (!GCRootInt && !GCReadInt && !GCWriteInt) return false;
165
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000166 PointerType *VoidPtr = PointerType::getUnqual(Type::Int8Ty);
167 PointerType *VoidPtrPtr = PointerType::getUnqual(VoidPtr);
Chris Lattner08005df2004-05-23 21:19:22 +0000168
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 Sandsa3355ff2007-12-03 20:06:50 +0000180 !CI->getCalledFunction()->isIntrinsic())
Chris Lattner08005df2004-05-23 21:19:22 +0000181 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 Lattner93c95872004-07-22 05:51:13 +0000192 Coerce(CI, 2, VoidPtr);
193 Coerce(CI, 3, VoidPtrPtr);
Chris Lattner08005df2004-05-23 21:19:22 +0000194 } else {
Chris Lattner93c95872004-07-22 05:51:13 +0000195 Coerce(CI, 1, VoidPtr);
196 Coerce(CI, 2, VoidPtrPtr);
Chris Lattner08005df2004-05-23 21:19:22 +0000197 if (CI->getType() == VoidPtr) {
198 CI->setOperand(0, GCRead);
199 } else {
200 // Create a whole new call to replace the old one.
David Greene52eec542007-08-01 03:43:44 +0000201
202 // It sure would be nice to pass op_begin()+1,
203 // op_begin()+2 but it runs into trouble with
David Greene32fe3de2007-08-07 16:52:03 +0000204 // CallInst::init's &*iterator, which requires a
David Greene52eec542007-08-01 03:43:44 +0000205 // 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 Greene242be6e2007-08-06 15:09:17 +0000208 Value* Args[] = {
209 CI->getOperand(1),
210 CI->getOperand(2)
211 };
212 CallInst *NC = new CallInst(GCRead, Args, Args + 2,
Chris Lattner08005df2004-05-23 21:19:22 +0000213 CI->getName(), CI);
Reid Spencer3da59db2006-11-27 01:05:10 +0000214 // 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 Lattner08005df2004-05-23 21:19:22 +0000217 CI->replaceAllUsesWith(NV);
218 BB->getInstList().erase(CI);
219 CI = NC;
220 }
221 }
222
Chris Lattner08005df2004-05-23 21:19:22 +0000223 MadeChange = true;
224 }
225 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000226
Chris Lattner08005df2004-05-23 21:19:22 +0000227 // 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 Spencerc5b206b2006-12-31 05:48:39 +0000242 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
243 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattner08005df2004-05-23 21:19:22 +0000244
David Greeneb8f74792007-09-04 15:46:09 +0000245 Value *Idx[2] = { Zero, Zero };
246
Chris Lattner08005df2004-05-23 21:19:22 +0000247 // Get a pointer to the prev pointer.
David Greeneb8f74792007-09-04 15:46:09 +0000248 Value *PrevPtrPtr = new GetElementPtrInst(AI, Idx, Idx + 2,
249 "prevptrptr", IP);
Chris Lattner08005df2004-05-23 21:19:22 +0000250
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 Greeneb8f74792007-09-04 15:46:09 +0000257 Idx[1] = One;
258 Value *NumEltsPtr = new GetElementPtrInst(AI, Idx, Idx + 2,
259 "numeltsptr", IP);
Reid Spencerc5b206b2006-12-31 05:48:39 +0000260 new StoreInst(ConstantInt::get(Type::Int32Ty, GCRoots.size()), NumEltsPtr,IP);
Chris Lattner08005df2004-05-23 21:19:22 +0000261
Chris Lattnerfbbe92f2007-01-31 20:08:52 +0000262 Value* Par[4];
263 Par[0] = Zero;
Reid Spencerc5b206b2006-12-31 05:48:39 +0000264 Par[1] = ConstantInt::get(Type::Int32Ty, 2);
Chris Lattner08005df2004-05-23 21:19:22 +0000265
266 const PointerType *PtrLocTy =
267 cast<PointerType>(GCRootInt->getFunctionType()->getParamType(0));
268 Constant *Null = ConstantPointerNull::get(PtrLocTy);
269
Evan Cheng4e9c4732007-09-01 02:00:51 +0000270 // Initialize all of the gcroot records now.
Chris Lattner08005df2004-05-23 21:19:22 +0000271 for (unsigned i = 0, e = GCRoots.size(); i != e; ++i) {
272 // Initialize the meta-data pointer.
Reid Spencerc5b206b2006-12-31 05:48:39 +0000273 Par[2] = ConstantInt::get(Type::Int32Ty, i);
Chris Lattner08005df2004-05-23 21:19:22 +0000274 Par[3] = One;
David Greeneb8f74792007-09-04 15:46:09 +0000275 Value *MetaDataPtr = new GetElementPtrInst(AI, Par, Par + 4,
276 "MetaDataPtr", IP);
Reid Spencer48dc46a2004-07-18 00:29:57 +0000277 assert(isa<Constant>(GCRoots[i]->getOperand(2)) && "Must be a constant");
Chris Lattner08005df2004-05-23 21:19:22 +0000278 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 Greeneb8f74792007-09-04 15:46:09 +0000282 Value *RootPtrPtr = new GetElementPtrInst(AI, Par, Par + 4,
283 "RootEntPtr", IP);
Chris Lattner08005df2004-05-23 21:19:22 +0000284 new StoreInst(Null, RootPtrPtr, IP);
Misha Brukmanfd939082005-04-21 23:48:37 +0000285
Chris Lattner08005df2004-05-23 21:19:22 +0000286 // Each occurrance of the llvm.gcroot intrinsic now turns into an
Chris Lattner36597a52007-09-12 17:53:10 +0000287 // initialization of the slot with the address.
Chris Lattner08005df2004-05-23 21:19:22 +0000288 new StoreInst(GCRoots[i]->getOperand(1), RootPtrPtr, GCRoots[i]);
Chris Lattner08005df2004-05-23 21:19:22 +0000289 }
290
291 // Now that the record is all initialized, store the pointer into the global
292 // pointer.
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000293 Value *C = new BitCastInst(AI, PointerType::getUnqual(MainRootRecordType), "", IP);
Chris Lattner08005df2004-05-23 21:19:22 +0000294 new StoreInst(C, RootChain, IP);
295
Evan Cheng4e9c4732007-09-01 02:00:51 +0000296 // 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 Lattner08005df2004-05-23 21:19:22 +0000300 // 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 Brukmanfd939082005-04-21 23:48:37 +0000323
Chris Lattner08005df2004-05-23 21:19:22 +0000324 // 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 Brukmanfd939082005-04-21 23:48:37 +0000336
Chris Lattner08005df2004-05-23 21:19:22 +0000337 // Create a new invoke instruction.
Chris Lattner93e985f2007-02-13 02:10:56 +0000338 std::vector<Value*> Args(CI->op_begin()+1, CI->op_end());
339
Chris Lattner08005df2004-05-23 21:19:22 +0000340 Value *II = new InvokeInst(CI->getCalledValue(), NewBB, Cleanup,
David Greenef1355a52007-08-27 19:04:21 +0000341 Args.begin(), Args.end(), CI->getName(), CBB);
Duncan Sandsdc024672007-11-27 13:23:08 +0000342 cast<InvokeInst>(II)->setCallingConv(CI->getCallingConv());
343 cast<InvokeInst>(II)->setParamAttrs(CI->getParamAttrs());
Chris Lattner08005df2004-05-23 21:19:22 +0000344 CI->replaceAllUsesWith(II);
345 delete CI;
346 }
347 }
348
349 return true;
350}