blob: b851c074c218f68d7ab53561713bb8ec310c6e5e [file] [log] [blame]
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001//===-- GCStrategy.cpp - Garbage collection infrastructure -----------------===//
Gordon Henriksen364caf02007-09-29 02:13:43 +00002//
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.
Gordon Henriksen364caf02007-09-29 02:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements target- and collector-independent garbage collection
11// infrastructure.
12//
Gordon Henriksen5eca0752008-08-17 18:44:35 +000013// MachineCodeAnalysis identifies the GC safe points in the machine code. Roots
14// are identified in SelectionDAGISel.
15//
Gordon Henriksen364caf02007-09-29 02:13:43 +000016//===----------------------------------------------------------------------===//
17
Gordon Henriksen5a29c9e2008-08-17 12:56:54 +000018#include "llvm/CodeGen/GCStrategy.h"
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000019#include "llvm/CodeGen/Passes.h"
Gordon Henriksen364caf02007-09-29 02:13:43 +000020#include "llvm/IntrinsicInst.h"
21#include "llvm/Module.h"
Gordon Henriksen364caf02007-09-29 02:13:43 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Gordon Henriksen364caf02007-09-29 02:13:43 +000026#include "llvm/Target/TargetFrameInfo.h"
27#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
Gordon Henriksenfcbcfaa2008-08-19 17:06:35 +000029#include "llvm/Target/TargetRegisterInfo.h"
Gordon Henriksen364caf02007-09-29 02:13:43 +000030#include "llvm/Support/Compiler.h"
31
32using namespace llvm;
33
34namespace {
35
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000036 /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
37 /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
Gordon Henriksen5eca0752008-08-17 18:44:35 +000038 /// directed by the GCStrategy. It also performs automatic root initialization
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000039 /// and custom intrinsic lowering.
Gordon Henriksen364caf02007-09-29 02:13:43 +000040 class VISIBILITY_HIDDEN LowerIntrinsics : public FunctionPass {
Gordon Henriksen5eca0752008-08-17 18:44:35 +000041 static bool NeedsDefaultLoweringPass(const GCStrategy &C);
42 static bool NeedsCustomLoweringPass(const GCStrategy &C);
Gordon Henriksen364caf02007-09-29 02:13:43 +000043 static bool CouldBecomeSafePoint(Instruction *I);
Gordon Henriksen5eca0752008-08-17 18:44:35 +000044 bool PerformDefaultLowering(Function &F, GCStrategy &Coll);
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000045 static bool InsertRootInitializers(Function &F,
Gordon Henriksen364caf02007-09-29 02:13:43 +000046 AllocaInst **Roots, unsigned Count);
47
48 public:
49 static char ID;
50
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000051 LowerIntrinsics();
Gordon Henriksen364caf02007-09-29 02:13:43 +000052 const char *getPassName() const;
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000053 void getAnalysisUsage(AnalysisUsage &AU) const;
Gordon Henriksen364caf02007-09-29 02:13:43 +000054
55 bool doInitialization(Module &M);
56 bool runOnFunction(Function &F);
57 };
58
59
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000060 /// MachineCodeAnalysis - This is a target-independent pass over the machine
61 /// function representation to identify safe points for the garbage collector
62 /// in the machine code. It inserts labels at safe points and populates a
Gordon Henriksen5eca0752008-08-17 18:44:35 +000063 /// GCMetadata record for each function.
Gordon Henriksen364caf02007-09-29 02:13:43 +000064 class VISIBILITY_HIDDEN MachineCodeAnalysis : public MachineFunctionPass {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000065 const TargetMachine *TM;
Gordon Henriksen5eca0752008-08-17 18:44:35 +000066 GCFunctionInfo *FI;
Gordon Henriksen364caf02007-09-29 02:13:43 +000067 MachineModuleInfo *MMI;
68 const TargetInstrInfo *TII;
Gordon Henriksen364caf02007-09-29 02:13:43 +000069
70 void FindSafePoints(MachineFunction &MF);
71 void VisitCallPoint(MachineBasicBlock::iterator MI);
72 unsigned InsertLabel(MachineBasicBlock &MBB,
73 MachineBasicBlock::iterator MI) const;
74
75 void FindStackOffsets(MachineFunction &MF);
76
77 public:
78 static char ID;
79
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000080 MachineCodeAnalysis();
Gordon Henriksen364caf02007-09-29 02:13:43 +000081 const char *getPassName() const;
82 void getAnalysisUsage(AnalysisUsage &AU) const;
83
84 bool runOnMachineFunction(MachineFunction &MF);
85 };
86
87}
88
89// -----------------------------------------------------------------------------
90
Gordon Henriksen5eca0752008-08-17 18:44:35 +000091GCStrategy::GCStrategy() :
Gordon Henriksen364caf02007-09-29 02:13:43 +000092 NeededSafePoints(0),
93 CustomReadBarriers(false),
94 CustomWriteBarriers(false),
95 CustomRoots(false),
Gordon Henriksenc317a602008-08-17 12:08:44 +000096 InitRoots(true),
97 UsesMetadata(false)
Gordon Henriksen364caf02007-09-29 02:13:43 +000098{}
99
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000100GCStrategy::~GCStrategy() {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000101 for (iterator I = begin(), E = end(); I != E; ++I)
102 delete *I;
103
104 Functions.clear();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000105}
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000106
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000107bool GCStrategy::initializeCustomLowering(Module &M) { return false; }
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000108
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000109bool GCStrategy::performCustomLowering(Function &F) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000110 cerr << "gc " << getName() << " must override performCustomLowering.\n";
Gordon Henriksen364caf02007-09-29 02:13:43 +0000111 abort();
112 return 0;
113}
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000114
115GCFunctionInfo *GCStrategy::insertFunctionInfo(const Function &F) {
116 GCFunctionInfo *FI = new GCFunctionInfo(F, *this);
117 Functions.push_back(FI);
118 return FI;
119}
Gordon Henriksen364caf02007-09-29 02:13:43 +0000120
121// -----------------------------------------------------------------------------
122
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000123FunctionPass *llvm::createGCLoweringPass() {
124 return new LowerIntrinsics();
125}
126
Gordon Henriksen364caf02007-09-29 02:13:43 +0000127char LowerIntrinsics::ID = 0;
128
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000129LowerIntrinsics::LowerIntrinsics()
Dan Gohmanae73dc12008-09-04 17:05:41 +0000130 : FunctionPass(&ID) {}
Gordon Henriksen364caf02007-09-29 02:13:43 +0000131
132const char *LowerIntrinsics::getPassName() const {
133 return "Lower Garbage Collection Instructions";
134}
135
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000136void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
137 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000138 AU.addRequired<GCModuleInfo>();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000139}
140
141/// doInitialization - If this module uses the GC intrinsics, find them now.
Gordon Henriksen364caf02007-09-29 02:13:43 +0000142bool LowerIntrinsics::doInitialization(Module &M) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000143 // FIXME: This is rather antisocial in the context of a JIT since it performs
144 // work against the entire module. But this cannot be done at
145 // runFunction time (initializeCustomLowering likely needs to change
146 // the module).
Duncan Sands1465d612009-01-28 13:14:17 +0000147 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000148 assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?");
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000149 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000150 if (!I->isDeclaration() && I->hasGC())
151 MI->getFunctionInfo(*I); // Instantiate the GC strategy.
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000152
153 bool MadeChange = false;
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000154 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000155 if (NeedsCustomLoweringPass(**I))
156 if ((*I)->initializeCustomLowering(M))
157 MadeChange = true;
158
159 return MadeChange;
Gordon Henriksen364caf02007-09-29 02:13:43 +0000160}
161
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000162bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots,
Gordon Henriksen364caf02007-09-29 02:13:43 +0000163 unsigned Count) {
164 // Scroll past alloca instructions.
165 BasicBlock::iterator IP = F.getEntryBlock().begin();
166 while (isa<AllocaInst>(IP)) ++IP;
167
168 // Search for initializers in the initial BB.
169 SmallPtrSet<AllocaInst*,16> InitedRoots;
170 for (; !CouldBecomeSafePoint(IP); ++IP)
171 if (StoreInst *SI = dyn_cast<StoreInst>(IP))
Anton Korobeynikovb04addd2008-05-06 22:52:30 +0000172 if (AllocaInst *AI =
Anton Korobeynikov0b12ecf2008-05-07 22:54:15 +0000173 dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts()))
Gordon Henriksen364caf02007-09-29 02:13:43 +0000174 InitedRoots.insert(AI);
175
176 // Add root initializers.
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000177 bool MadeChange = false;
178
Gordon Henriksen364caf02007-09-29 02:13:43 +0000179 for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000180 if (!InitedRoots.count(*I)) {
Gordon Henriksen364caf02007-09-29 02:13:43 +0000181 new StoreInst(ConstantPointerNull::get(cast<PointerType>(
182 cast<PointerType>((*I)->getType())->getElementType())),
183 *I, IP);
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000184 MadeChange = true;
185 }
186
187 return MadeChange;
188}
189
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000190bool LowerIntrinsics::NeedsDefaultLoweringPass(const GCStrategy &C) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000191 // Default lowering is necessary only if read or write barriers have a default
192 // action. The default for roots is no action.
193 return !C.customWriteBarrier()
194 || !C.customReadBarrier()
195 || C.initializeRoots();
196}
197
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000198bool LowerIntrinsics::NeedsCustomLoweringPass(const GCStrategy &C) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000199 // Custom lowering is only necessary if enabled for some action.
200 return C.customWriteBarrier()
201 || C.customReadBarrier()
202 || C.customRoots();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000203}
204
205/// CouldBecomeSafePoint - Predicate to conservatively determine whether the
206/// instruction could introduce a safe point.
207bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) {
208 // The natural definition of instructions which could introduce safe points
209 // are:
210 //
211 // - call, invoke (AfterCall, BeforeCall)
212 // - phis (Loops)
213 // - invoke, ret, unwind (Exit)
214 //
215 // However, instructions as seemingly inoccuous as arithmetic can become
216 // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead
217 // it is necessary to take a conservative approach.
218
219 if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) ||
220 isa<StoreInst>(I) || isa<LoadInst>(I))
221 return false;
222
223 // llvm.gcroot is safe because it doesn't do anything at runtime.
224 if (CallInst *CI = dyn_cast<CallInst>(I))
225 if (Function *F = CI->getCalledFunction())
226 if (unsigned IID = F->getIntrinsicID())
227 if (IID == Intrinsic::gcroot)
228 return false;
229
230 return true;
231}
232
233/// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores.
234/// Leave gcroot intrinsics; the code generator needs to see those.
235bool LowerIntrinsics::runOnFunction(Function &F) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000236 // Quick exit for functions that do not use GC.
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000237 if (!F.hasGC())
238 return false;
Gordon Henriksen364caf02007-09-29 02:13:43 +0000239
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000240 GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F);
241 GCStrategy &S = FI.getStrategy();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000242
243 bool MadeChange = false;
244
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000245 if (NeedsDefaultLoweringPass(S))
246 MadeChange |= PerformDefaultLowering(F, S);
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000247
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000248 if (NeedsCustomLoweringPass(S))
249 MadeChange |= S.performCustomLowering(F);
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000250
251 return MadeChange;
252}
253
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000254bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) {
255 bool LowerWr = !S.customWriteBarrier();
256 bool LowerRd = !S.customReadBarrier();
257 bool InitRoots = S.initializeRoots();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000258
259 SmallVector<AllocaInst*,32> Roots;
260
261 bool MadeChange = false;
262 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
263 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) {
Gordon Henriksena6c99252007-12-22 17:27:01 +0000264 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) {
Gordon Henriksen364caf02007-09-29 02:13:43 +0000265 Function *F = CI->getCalledFunction();
Gordon Henriksena6c99252007-12-22 17:27:01 +0000266 switch (F->getIntrinsicID()) {
267 case Intrinsic::gcwrite:
268 if (LowerWr) {
269 // Replace a write barrier with a simple store.
270 Value *St = new StoreInst(CI->getOperand(1), CI->getOperand(3), CI);
271 CI->replaceAllUsesWith(St);
272 CI->eraseFromParent();
273 }
274 break;
275 case Intrinsic::gcread:
276 if (LowerRd) {
277 // Replace a read barrier with a simple load.
278 Value *Ld = new LoadInst(CI->getOperand(2), "", CI);
279 Ld->takeName(CI);
280 CI->replaceAllUsesWith(Ld);
281 CI->eraseFromParent();
282 }
283 break;
284 case Intrinsic::gcroot:
285 if (InitRoots) {
286 // Initialize the GC root, but do not delete the intrinsic. The
287 // backend needs the intrinsic to flag the stack slot.
288 Roots.push_back(cast<AllocaInst>(
Anton Korobeynikov0b12ecf2008-05-07 22:54:15 +0000289 CI->getOperand(1)->stripPointerCasts()));
Gordon Henriksena6c99252007-12-22 17:27:01 +0000290 }
291 break;
292 default:
Gordon Henriksen364caf02007-09-29 02:13:43 +0000293 continue;
294 }
295
296 MadeChange = true;
297 }
298 }
299 }
300
301 if (Roots.size())
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000302 MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size());
Gordon Henriksen364caf02007-09-29 02:13:43 +0000303
304 return MadeChange;
305}
306
307// -----------------------------------------------------------------------------
308
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000309FunctionPass *llvm::createGCMachineCodeAnalysisPass() {
310 return new MachineCodeAnalysis();
311}
312
Gordon Henriksen364caf02007-09-29 02:13:43 +0000313char MachineCodeAnalysis::ID = 0;
314
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000315MachineCodeAnalysis::MachineCodeAnalysis()
316 : MachineFunctionPass(intptr_t(&ID)) {}
Gordon Henriksen364caf02007-09-29 02:13:43 +0000317
318const char *MachineCodeAnalysis::getPassName() const {
319 return "Analyze Machine Code For Garbage Collection";
320}
321
322void MachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
323 MachineFunctionPass::getAnalysisUsage(AU);
324 AU.setPreservesAll();
325 AU.addRequired<MachineModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000326 AU.addRequired<GCModuleInfo>();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000327}
328
329unsigned MachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
330 MachineBasicBlock::iterator MI) const {
331 unsigned Label = MMI->NextLabelID();
Bill Wendlingd62e06c2009-02-03 02:29:34 +0000332 BuildMI(MBB, MI, MI->getDebugLoc(),
333 TII->get(TargetInstrInfo::GC_LABEL)).addImm(Label);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000334 return Label;
335}
336
337void MachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
338 // Find the return address (next instruction), too, so as to bracket the call
339 // instruction.
340 MachineBasicBlock::iterator RAI = CI;
341 ++RAI;
342
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000343 if (FI->getStrategy().needsSafePoint(GC::PreCall))
344 FI->addSafePoint(GC::PreCall, InsertLabel(*CI->getParent(), CI));
Gordon Henriksen364caf02007-09-29 02:13:43 +0000345
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000346 if (FI->getStrategy().needsSafePoint(GC::PostCall))
347 FI->addSafePoint(GC::PostCall, InsertLabel(*CI->getParent(), RAI));
Gordon Henriksen364caf02007-09-29 02:13:43 +0000348}
349
350void MachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
351 for (MachineFunction::iterator BBI = MF.begin(),
352 BBE = MF.end(); BBI != BBE; ++BBI)
353 for (MachineBasicBlock::iterator MI = BBI->begin(),
354 ME = BBI->end(); MI != ME; ++MI)
Chris Lattner749c6f62008-01-07 07:27:27 +0000355 if (MI->getDesc().isCall())
Dan Gohmanfd3ff032008-07-07 20:08:05 +0000356 VisitCallPoint(MI);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000357}
358
359void MachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
Gordon Henriksenfcbcfaa2008-08-19 17:06:35 +0000360 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
361 assert(TRI && "TargetRegisterInfo not available!");
Gordon Henriksen364caf02007-09-29 02:13:43 +0000362
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000363 for (GCFunctionInfo::roots_iterator RI = FI->roots_begin(),
364 RE = FI->roots_end(); RI != RE; ++RI)
Gordon Henriksenfcbcfaa2008-08-19 17:06:35 +0000365 RI->StackOffset = TRI->getFrameIndexOffset(MF, RI->Num);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000366}
367
368bool MachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000369 // Quick exit for functions that do not use GC.
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000370 if (!MF.getFunction()->hasGC())
371 return false;
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000372
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000373 FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction());
374 if (!FI->getStrategy().needsSafePoints())
Gordon Henriksen364caf02007-09-29 02:13:43 +0000375 return false;
376
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000377 TM = &MF.getTarget();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000378 MMI = &getAnalysis<MachineModuleInfo>();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000379 TII = TM->getInstrInfo();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000380
381 // Find the size of the stack frame.
Gordon Henriksenfcbcfaa2008-08-19 17:06:35 +0000382 FI->setFrameSize(MF.getFrameInfo()->getStackSize());
Gordon Henriksen364caf02007-09-29 02:13:43 +0000383
384 // Find all safe points.
385 FindSafePoints(MF);
386
387 // Find the stack offsets for all roots.
388 FindStackOffsets(MF);
389
390 return false;
391}