blob: 2e03807ed2434930b20128ca2353f3b99368d7f5 [file] [log] [blame]
Chris Lattner099c8cf2004-05-23 21:19:22 +00001//===-- LowerGC.cpp - Provide GC support for targets that don't -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements lowering for the llvm.gc* intrinsics for targets that do
11// not natively support them (which includes the C backend). Note that the code
12// generated is not 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 Lattner99173872004-05-23 21:27:29 +000018// Fergus Henderson, ISMM, 2002
Chris Lattner099c8cf2004-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"
29#include "llvm/Transforms/Utils/Cloning.h"
30using namespace llvm;
31
32namespace {
33 class LowerGC : public FunctionPass {
34 /// GCRootInt, GCReadInt, GCWriteInt - The function prototypes for the
35 /// llvm.gcread/llvm.gcwrite/llvm.gcroot intrinsics.
36 Function *GCRootInt, *GCReadInt, *GCWriteInt;
37
38 /// GCRead/GCWrite - These are the functions provided by the garbage
39 /// collector for read/write barriers.
40 Function *GCRead, *GCWrite;
41
42 /// RootChain - This is the global linked-list that contains the chain of GC
43 /// roots.
44 GlobalVariable *RootChain;
45
46 /// MainRootRecordType - This is the type for a function root entry if it
47 /// had zero roots.
48 const Type *MainRootRecordType;
49 public:
50 LowerGC() : GCRootInt(0), GCReadInt(0), GCWriteInt(0),
51 GCRead(0), GCWrite(0), RootChain(0), MainRootRecordType(0) {}
52 virtual bool doInitialization(Module &M);
53 virtual bool runOnFunction(Function &F);
54
55 private:
56 const StructType *getRootRecordType(unsigned NumRoots);
57 };
58
59 RegisterOpt<LowerGC>
60 X("lowergc", "Lower GC intrinsics, for GCless code generators");
61}
62
63/// createLowerGCPass - This function returns an instance of the "lowergc"
64/// pass, which lowers garbage collection intrinsics to normal LLVM code.
65FunctionPass *llvm::createLowerGCPass() {
66 return new LowerGC();
67}
68
69/// getRootRecordType - This function creates and returns the type for a root
70/// record containing 'NumRoots' roots.
71const StructType *LowerGC::getRootRecordType(unsigned NumRoots) {
72 // Build a struct that is a type used for meta-data/root pairs.
73 std::vector<const Type *> ST;
74 ST.push_back(GCRootInt->getFunctionType()->getParamType(0));
75 ST.push_back(GCRootInt->getFunctionType()->getParamType(1));
76 StructType *PairTy = StructType::get(ST);
77
78 // Build the array of pairs.
79 ArrayType *PairArrTy = ArrayType::get(PairTy, NumRoots);
80
81 // Now build the recursive list type.
82 PATypeHolder RootListH =
83 MainRootRecordType ? (Type*)MainRootRecordType : (Type*)OpaqueType::get();
84 ST.clear();
85 ST.push_back(PointerType::get(RootListH)); // Prev pointer
86 ST.push_back(Type::UIntTy); // NumElements in array
87 ST.push_back(PairArrTy); // The pairs
88 StructType *RootList = StructType::get(ST);
89 if (MainRootRecordType)
90 return RootList;
91
92 assert(NumRoots == 0 && "The main struct type should have zero entries!");
93 cast<OpaqueType>((Type*)RootListH.get())->refineAbstractTypeTo(RootList);
94 MainRootRecordType = RootListH;
95 return cast<StructType>(RootListH.get());
96}
97
98/// doInitialization - If this module uses the GC intrinsics, find them now. If
99/// not, this pass does not do anything.
100bool LowerGC::doInitialization(Module &M) {
101 GCRootInt = M.getNamedFunction("llvm.gcroot");
102 GCReadInt = M.getNamedFunction("llvm.gcread");
103 GCWriteInt = M.getNamedFunction("llvm.gcwrite");
104 if (!GCRootInt && !GCReadInt && !GCWriteInt) return false;
105
106 PointerType *VoidPtr = PointerType::get(Type::SByteTy);
107 PointerType *VoidPtrPtr = PointerType::get(VoidPtr);
108
109 // If the program is using read/write barriers, find the implementations of
110 // them from the GC runtime library.
111 if (GCReadInt) // Make: sbyte* %llvm_gc_read(sbyte**)
Chris Lattner51f7c9e2004-07-22 05:51:13 +0000112 GCRead = M.getOrInsertFunction("llvm_gc_read", VoidPtr, VoidPtr, VoidPtrPtr, 0);
Chris Lattner099c8cf2004-05-23 21:19:22 +0000113 if (GCWriteInt) // Make: void %llvm_gc_write(sbyte*, sbyte**)
114 GCWrite = M.getOrInsertFunction("llvm_gc_write", Type::VoidTy,
Chris Lattner51f7c9e2004-07-22 05:51:13 +0000115 VoidPtr, VoidPtr, VoidPtrPtr, 0);
Chris Lattner099c8cf2004-05-23 21:19:22 +0000116
117 // If the program has GC roots, get or create the global root list.
118 if (GCRootInt) {
119 const StructType *RootListTy = getRootRecordType(0);
120 const Type *PRLTy = PointerType::get(RootListTy);
121 M.addTypeName("llvm_gc_root_ty", RootListTy);
122
123 // Get the root chain if it already exists.
124 RootChain = M.getGlobalVariable("llvm_gc_root_chain", PRLTy);
125 if (RootChain == 0) {
126 // If the root chain does not exist, insert a new one with linkonce
127 // linkage!
128 RootChain = new GlobalVariable(PRLTy, false, GlobalValue::LinkOnceLinkage,
129 Constant::getNullValue(RootListTy),
130 "llvm_gc_root_chain", &M);
131 } else if (RootChain->hasExternalLinkage() && RootChain->isExternal()) {
132 RootChain->setInitializer(Constant::getNullValue(PRLTy));
133 RootChain->setLinkage(GlobalValue::LinkOnceLinkage);
134 }
135 }
136 return true;
137}
138
139/// Coerce - If the specified operand number of the specified instruction does
140/// not have the specified type, insert a cast.
141static void Coerce(Instruction *I, unsigned OpNum, Type *Ty) {
142 if (I->getOperand(OpNum)->getType() != Ty) {
Reid Spencerc44cb6b2004-07-18 08:34:19 +0000143 if (Constant *C = dyn_cast<Constant>(I->getOperand(OpNum)))
Chris Lattner099c8cf2004-05-23 21:19:22 +0000144 I->setOperand(OpNum, ConstantExpr::getCast(C, Ty));
145 else {
146 CastInst *C = new CastInst(I->getOperand(OpNum), Ty, "", I);
147 I->setOperand(OpNum, C);
148 }
149 }
150}
151
152/// runOnFunction - If the program is using GC intrinsics, replace any
153/// read/write intrinsics with the appropriate read/write barrier calls, then
154/// inline them. Finally, build the data structures for
155bool LowerGC::runOnFunction(Function &F) {
156 // Quick exit for programs that are not using GC mechanisms.
157 if (!GCRootInt && !GCReadInt && !GCWriteInt) return false;
158
159 PointerType *VoidPtr = PointerType::get(Type::SByteTy);
160 PointerType *VoidPtrPtr = PointerType::get(VoidPtr);
161
162 // If there are read/write barriers in the program, perform a quick pass over
163 // the function eliminating them. While we are at it, remember where we see
164 // calls to llvm.gcroot.
165 std::vector<CallInst*> GCRoots;
166 std::vector<CallInst*> NormalCalls;
167
168 bool MadeChange = false;
169 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
170 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
171 if (CallInst *CI = dyn_cast<CallInst>(II++)) {
172 if (!CI->getCalledFunction() ||
173 !CI->getCalledFunction()->getIntrinsicID())
174 NormalCalls.push_back(CI); // Remember all normal function calls.
175
176 if (Function *F = CI->getCalledFunction())
177 if (F == GCRootInt)
178 GCRoots.push_back(CI);
179 else if (F == GCReadInt || F == GCWriteInt) {
180 if (F == GCWriteInt) {
181 // Change a llvm.gcwrite call to call llvm_gc_write instead.
182 CI->setOperand(0, GCWrite);
183 // Insert casts of the operands as needed.
184 Coerce(CI, 1, VoidPtr);
Chris Lattner51f7c9e2004-07-22 05:51:13 +0000185 Coerce(CI, 2, VoidPtr);
186 Coerce(CI, 3, VoidPtrPtr);
Chris Lattner099c8cf2004-05-23 21:19:22 +0000187 } else {
Chris Lattner51f7c9e2004-07-22 05:51:13 +0000188 Coerce(CI, 1, VoidPtr);
189 Coerce(CI, 2, VoidPtrPtr);
Chris Lattner099c8cf2004-05-23 21:19:22 +0000190 if (CI->getType() == VoidPtr) {
191 CI->setOperand(0, GCRead);
192 } else {
193 // Create a whole new call to replace the old one.
Chris Lattner51f7c9e2004-07-22 05:51:13 +0000194 CallInst *NC = new CallInst(GCRead, CI->getOperand(1),
195 CI->getOperand(2),
Chris Lattner099c8cf2004-05-23 21:19:22 +0000196 CI->getName(), CI);
197 Value *NV = new CastInst(NC, CI->getType(), "", CI);
198 CI->replaceAllUsesWith(NV);
199 BB->getInstList().erase(CI);
200 CI = NC;
201 }
202 }
203
204 // Now that we made the replacement, inline expand the call if
205 // possible, otherwise things will be too horribly expensive.
206 InlineFunction(CI);
207 MadeChange = true;
208 }
209 }
210
211 // If there are no GC roots in this function, then there is no need to create
212 // a GC list record for it.
213 if (GCRoots.empty()) return MadeChange;
214
215 // Okay, there are GC roots in this function. On entry to the function, add a
216 // record to the llvm_gc_root_chain, and remove it on exit.
217
218 // Create the alloca, and zero it out.
219 const StructType *RootListTy = getRootRecordType(GCRoots.size());
220 AllocaInst *AI = new AllocaInst(RootListTy, 0, "gcroots", F.begin()->begin());
221
222 // Insert the memset call after all of the allocas in the function.
223 BasicBlock::iterator IP = AI;
224 while (isa<AllocaInst>(IP)) ++IP;
225
226 Constant *Zero = ConstantUInt::get(Type::UIntTy, 0);
227 Constant *One = ConstantUInt::get(Type::UIntTy, 1);
228
229 // Get a pointer to the prev pointer.
230 std::vector<Value*> Par;
231 Par.push_back(Zero);
232 Par.push_back(Zero);
233 Value *PrevPtrPtr = new GetElementPtrInst(AI, Par, "prevptrptr", IP);
234
235 // Load the previous pointer.
236 Value *PrevPtr = new LoadInst(RootChain, "prevptr", IP);
237 // Store the previous pointer into the prevptrptr
238 new StoreInst(PrevPtr, PrevPtrPtr, IP);
239
240 // Set the number of elements in this record.
241 Par[1] = ConstantUInt::get(Type::UIntTy, 1);
242 Value *NumEltsPtr = new GetElementPtrInst(AI, Par, "numeltsptr", IP);
243 new StoreInst(ConstantUInt::get(Type::UIntTy, GCRoots.size()), NumEltsPtr,IP);
244
245 Par[1] = ConstantUInt::get(Type::UIntTy, 2);
246 Par.resize(4);
247
248 const PointerType *PtrLocTy =
249 cast<PointerType>(GCRootInt->getFunctionType()->getParamType(0));
250 Constant *Null = ConstantPointerNull::get(PtrLocTy);
251
252 // Initialize all of the gcroot records now, and eliminate them as we go.
253 for (unsigned i = 0, e = GCRoots.size(); i != e; ++i) {
254 // Initialize the meta-data pointer.
255 Par[2] = ConstantUInt::get(Type::UIntTy, i);
256 Par[3] = One;
257 Value *MetaDataPtr = new GetElementPtrInst(AI, Par, "MetaDataPtr", IP);
Reid Spencer9e855c62004-07-18 00:29:57 +0000258 assert(isa<Constant>(GCRoots[i]->getOperand(2)) && "Must be a constant");
Chris Lattner099c8cf2004-05-23 21:19:22 +0000259 new StoreInst(GCRoots[i]->getOperand(2), MetaDataPtr, IP);
260
261 // Initialize the root pointer to null on entry to the function.
262 Par[3] = Zero;
263 Value *RootPtrPtr = new GetElementPtrInst(AI, Par, "RootEntPtr", IP);
264 new StoreInst(Null, RootPtrPtr, IP);
265
266 // Each occurrance of the llvm.gcroot intrinsic now turns into an
267 // initialization of the slot with the address and a zeroing out of the
268 // address specified.
269 new StoreInst(Constant::getNullValue(PtrLocTy->getElementType()),
270 GCRoots[i]->getOperand(1), GCRoots[i]);
271 new StoreInst(GCRoots[i]->getOperand(1), RootPtrPtr, GCRoots[i]);
272 GCRoots[i]->getParent()->getInstList().erase(GCRoots[i]);
273 }
274
275 // Now that the record is all initialized, store the pointer into the global
276 // pointer.
277 Value *C = new CastInst(AI, PointerType::get(MainRootRecordType), "", IP);
278 new StoreInst(C, RootChain, IP);
279
280 // On exit from the function we have to remove the entry from the GC root
281 // chain. Doing this is straight-forward for return and unwind instructions:
282 // just insert the appropriate copy.
283 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
284 if (isa<UnwindInst>(BB->getTerminator()) ||
285 isa<ReturnInst>(BB->getTerminator())) {
286 // We could reuse the PrevPtr loaded on entry to the function, but this
287 // would make the value live for the whole function, which is probably a
288 // bad idea. Just reload the value out of our stack entry.
289 PrevPtr = new LoadInst(PrevPtrPtr, "prevptr", BB->getTerminator());
290 new StoreInst(PrevPtr, RootChain, BB->getTerminator());
291 }
292
293 // If an exception is thrown from a callee we have to make sure to
294 // unconditionally take the record off the stack. For this reason, we turn
295 // all call instructions into invoke whose cleanup pops the entry off the
296 // stack. We only insert one cleanup block, which is shared by all invokes.
297 if (!NormalCalls.empty()) {
298 // Create the shared cleanup block.
299 BasicBlock *Cleanup = new BasicBlock("gc_cleanup", &F);
300 UnwindInst *UI = new UnwindInst(Cleanup);
301 PrevPtr = new LoadInst(PrevPtrPtr, "prevptr", UI);
302 new StoreInst(PrevPtr, RootChain, UI);
303
304 // Loop over all of the function calls, turning them into invokes.
305 while (!NormalCalls.empty()) {
306 CallInst *CI = NormalCalls.back();
307 BasicBlock *CBB = CI->getParent();
308 NormalCalls.pop_back();
309
310 // Split the basic block containing the function call.
311 BasicBlock *NewBB = CBB->splitBasicBlock(CI, CBB->getName()+".cont");
312
313 // Remove the unconditional branch inserted at the end of the CBB.
314 CBB->getInstList().pop_back();
315 NewBB->getInstList().remove(CI);
316
317 // Create a new invoke instruction.
318 Value *II = new InvokeInst(CI->getCalledValue(), NewBB, Cleanup,
319 std::vector<Value*>(CI->op_begin()+1,
320 CI->op_end()),
321 CI->getName(), CBB);
322 CI->replaceAllUsesWith(II);
323 delete CI;
324 }
325 }
326
327 return true;
328}