blob: 766c6ee542a94cf0e7fe3980d82140d21bfcff24 [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"
Cameron Zwarich80f6a502011-01-08 17:01:52 +000022#include "llvm/Analysis/Dominators.h"
Gordon Henriksen364caf02007-09-29 02:13:43 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000027#include "llvm/Target/TargetFrameLowering.h"
Gordon Henriksen364caf02007-09-29 02:13:43 +000028#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetMachine.h"
Gordon Henriksenfcbcfaa2008-08-19 17:06:35 +000030#include "llvm/Target/TargetRegisterInfo.h"
David Greenecc54c7c2010-01-04 21:48:34 +000031#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000032#include "llvm/Support/ErrorHandling.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000033#include "llvm/Support/raw_ostream.h"
Gordon Henriksen364caf02007-09-29 02:13:43 +000034
35using namespace llvm;
36
37namespace {
38
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000039 /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
40 /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
Gordon Henriksen5eca0752008-08-17 18:44:35 +000041 /// directed by the GCStrategy. It also performs automatic root initialization
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000042 /// and custom intrinsic lowering.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000043 class LowerIntrinsics : public FunctionPass {
Gordon Henriksen5eca0752008-08-17 18:44:35 +000044 static bool NeedsDefaultLoweringPass(const GCStrategy &C);
45 static bool NeedsCustomLoweringPass(const GCStrategy &C);
Gordon Henriksen364caf02007-09-29 02:13:43 +000046 static bool CouldBecomeSafePoint(Instruction *I);
Gordon Henriksen5eca0752008-08-17 18:44:35 +000047 bool PerformDefaultLowering(Function &F, GCStrategy &Coll);
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000048 static bool InsertRootInitializers(Function &F,
Gordon Henriksen364caf02007-09-29 02:13:43 +000049 AllocaInst **Roots, unsigned Count);
50
51 public:
52 static char ID;
53
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000054 LowerIntrinsics();
Gordon Henriksen364caf02007-09-29 02:13:43 +000055 const char *getPassName() const;
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000056 void getAnalysisUsage(AnalysisUsage &AU) const;
Gordon Henriksen364caf02007-09-29 02:13:43 +000057
58 bool doInitialization(Module &M);
59 bool runOnFunction(Function &F);
60 };
61
62
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000063 /// MachineCodeAnalysis - This is a target-independent pass over the machine
64 /// function representation to identify safe points for the garbage collector
65 /// in the machine code. It inserts labels at safe points and populates a
Gordon Henriksen5eca0752008-08-17 18:44:35 +000066 /// GCMetadata record for each function.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000067 class MachineCodeAnalysis : public MachineFunctionPass {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000068 const TargetMachine *TM;
Gordon Henriksen5eca0752008-08-17 18:44:35 +000069 GCFunctionInfo *FI;
Gordon Henriksen364caf02007-09-29 02:13:43 +000070 MachineModuleInfo *MMI;
71 const TargetInstrInfo *TII;
Gordon Henriksen364caf02007-09-29 02:13:43 +000072
73 void FindSafePoints(MachineFunction &MF);
74 void VisitCallPoint(MachineBasicBlock::iterator MI);
Chris Lattneraba9bcb2010-03-14 07:27:07 +000075 MCSymbol *InsertLabel(MachineBasicBlock &MBB,
76 MachineBasicBlock::iterator MI,
77 DebugLoc DL) const;
Gordon Henriksen364caf02007-09-29 02:13:43 +000078
79 void FindStackOffsets(MachineFunction &MF);
80
81 public:
82 static char ID;
83
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000084 MachineCodeAnalysis();
Gordon Henriksen364caf02007-09-29 02:13:43 +000085 const char *getPassName() const;
86 void getAnalysisUsage(AnalysisUsage &AU) const;
87
88 bool runOnMachineFunction(MachineFunction &MF);
89 };
90
91}
92
93// -----------------------------------------------------------------------------
94
Gordon Henriksen5eca0752008-08-17 18:44:35 +000095GCStrategy::GCStrategy() :
Gordon Henriksen364caf02007-09-29 02:13:43 +000096 NeededSafePoints(0),
97 CustomReadBarriers(false),
98 CustomWriteBarriers(false),
99 CustomRoots(false),
Gordon Henriksenc317a602008-08-17 12:08:44 +0000100 InitRoots(true),
101 UsesMetadata(false)
Gordon Henriksen364caf02007-09-29 02:13:43 +0000102{}
103
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000104GCStrategy::~GCStrategy() {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000105 for (iterator I = begin(), E = end(); I != E; ++I)
106 delete *I;
107
108 Functions.clear();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000109}
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000110
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000111bool GCStrategy::initializeCustomLowering(Module &M) { return false; }
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000112
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000113bool GCStrategy::performCustomLowering(Function &F) {
David Greenecc54c7c2010-01-04 21:48:34 +0000114 dbgs() << "gc " << getName() << " must override performCustomLowering.\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000115 llvm_unreachable(0);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000116 return 0;
117}
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000118
119GCFunctionInfo *GCStrategy::insertFunctionInfo(const Function &F) {
120 GCFunctionInfo *FI = new GCFunctionInfo(F, *this);
121 Functions.push_back(FI);
122 return FI;
123}
Gordon Henriksen364caf02007-09-29 02:13:43 +0000124
125// -----------------------------------------------------------------------------
126
Owen Anderson081c34b2010-10-19 17:21:58 +0000127INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering",
128 false, false)
129INITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
130INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false)
131
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000132FunctionPass *llvm::createGCLoweringPass() {
133 return new LowerIntrinsics();
134}
135
Gordon Henriksen364caf02007-09-29 02:13:43 +0000136char LowerIntrinsics::ID = 0;
137
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000138LowerIntrinsics::LowerIntrinsics()
Owen Anderson081c34b2010-10-19 17:21:58 +0000139 : FunctionPass(ID) {
140 initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry());
141 }
Gordon Henriksen364caf02007-09-29 02:13:43 +0000142
143const char *LowerIntrinsics::getPassName() const {
144 return "Lower Garbage Collection Instructions";
145}
146
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000147void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
148 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000149 AU.addRequired<GCModuleInfo>();
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000150 AU.addPreserved<DominatorTree>();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000151}
152
153/// doInitialization - If this module uses the GC intrinsics, find them now.
Gordon Henriksen364caf02007-09-29 02:13:43 +0000154bool LowerIntrinsics::doInitialization(Module &M) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000155 // FIXME: This is rather antisocial in the context of a JIT since it performs
156 // work against the entire module. But this cannot be done at
157 // runFunction time (initializeCustomLowering likely needs to change
158 // the module).
Duncan Sands1465d612009-01-28 13:14:17 +0000159 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000160 assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?");
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000161 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000162 if (!I->isDeclaration() && I->hasGC())
163 MI->getFunctionInfo(*I); // Instantiate the GC strategy.
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000164
165 bool MadeChange = false;
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000166 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000167 if (NeedsCustomLoweringPass(**I))
168 if ((*I)->initializeCustomLowering(M))
169 MadeChange = true;
170
171 return MadeChange;
Gordon Henriksen364caf02007-09-29 02:13:43 +0000172}
173
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000174bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots,
Gordon Henriksen364caf02007-09-29 02:13:43 +0000175 unsigned Count) {
176 // Scroll past alloca instructions.
177 BasicBlock::iterator IP = F.getEntryBlock().begin();
178 while (isa<AllocaInst>(IP)) ++IP;
179
180 // Search for initializers in the initial BB.
181 SmallPtrSet<AllocaInst*,16> InitedRoots;
182 for (; !CouldBecomeSafePoint(IP); ++IP)
183 if (StoreInst *SI = dyn_cast<StoreInst>(IP))
Anton Korobeynikovb04addd2008-05-06 22:52:30 +0000184 if (AllocaInst *AI =
Anton Korobeynikov0b12ecf2008-05-07 22:54:15 +0000185 dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts()))
Gordon Henriksen364caf02007-09-29 02:13:43 +0000186 InitedRoots.insert(AI);
187
188 // Add root initializers.
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000189 bool MadeChange = false;
190
Gordon Henriksen364caf02007-09-29 02:13:43 +0000191 for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000192 if (!InitedRoots.count(*I)) {
Nicolas Geoffray8538f042010-04-15 19:53:35 +0000193 StoreInst* SI = new StoreInst(ConstantPointerNull::get(cast<PointerType>(
194 cast<PointerType>((*I)->getType())->getElementType())),
195 *I);
196 SI->insertAfter(*I);
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000197 MadeChange = true;
198 }
199
200 return MadeChange;
201}
202
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000203bool LowerIntrinsics::NeedsDefaultLoweringPass(const GCStrategy &C) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000204 // Default lowering is necessary only if read or write barriers have a default
205 // action. The default for roots is no action.
206 return !C.customWriteBarrier()
207 || !C.customReadBarrier()
208 || C.initializeRoots();
209}
210
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000211bool LowerIntrinsics::NeedsCustomLoweringPass(const GCStrategy &C) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000212 // Custom lowering is only necessary if enabled for some action.
213 return C.customWriteBarrier()
214 || C.customReadBarrier()
215 || C.customRoots();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000216}
217
218/// CouldBecomeSafePoint - Predicate to conservatively determine whether the
219/// instruction could introduce a safe point.
220bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) {
221 // The natural definition of instructions which could introduce safe points
222 // are:
223 //
224 // - call, invoke (AfterCall, BeforeCall)
225 // - phis (Loops)
226 // - invoke, ret, unwind (Exit)
227 //
228 // However, instructions as seemingly inoccuous as arithmetic can become
229 // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead
230 // it is necessary to take a conservative approach.
231
232 if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) ||
233 isa<StoreInst>(I) || isa<LoadInst>(I))
234 return false;
235
236 // llvm.gcroot is safe because it doesn't do anything at runtime.
237 if (CallInst *CI = dyn_cast<CallInst>(I))
238 if (Function *F = CI->getCalledFunction())
239 if (unsigned IID = F->getIntrinsicID())
240 if (IID == Intrinsic::gcroot)
241 return false;
242
243 return true;
244}
245
246/// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores.
247/// Leave gcroot intrinsics; the code generator needs to see those.
248bool LowerIntrinsics::runOnFunction(Function &F) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000249 // Quick exit for functions that do not use GC.
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000250 if (!F.hasGC())
251 return false;
Gordon Henriksen364caf02007-09-29 02:13:43 +0000252
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000253 GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F);
254 GCStrategy &S = FI.getStrategy();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000255
256 bool MadeChange = false;
257
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000258 if (NeedsDefaultLoweringPass(S))
259 MadeChange |= PerformDefaultLowering(F, S);
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000260
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000261 bool UseCustomLoweringPass = NeedsCustomLoweringPass(S);
262 if (UseCustomLoweringPass)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000263 MadeChange |= S.performCustomLowering(F);
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000264
265 // Custom lowering may modify the CFG, so dominators must be recomputed.
266 if (UseCustomLoweringPass) {
267 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>())
268 DT->DT->recalculate(F);
269 }
270
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000271 return MadeChange;
272}
273
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000274bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) {
275 bool LowerWr = !S.customWriteBarrier();
276 bool LowerRd = !S.customReadBarrier();
277 bool InitRoots = S.initializeRoots();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000278
Gabor Greifa3997812010-07-22 10:37:47 +0000279 SmallVector<AllocaInst*, 32> Roots;
Gordon Henriksen364caf02007-09-29 02:13:43 +0000280
281 bool MadeChange = false;
282 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
283 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) {
Gordon Henriksena6c99252007-12-22 17:27:01 +0000284 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) {
Gordon Henriksen364caf02007-09-29 02:13:43 +0000285 Function *F = CI->getCalledFunction();
Gordon Henriksena6c99252007-12-22 17:27:01 +0000286 switch (F->getIntrinsicID()) {
287 case Intrinsic::gcwrite:
288 if (LowerWr) {
289 // Replace a write barrier with a simple store.
Gabor Greifa3997812010-07-22 10:37:47 +0000290 Value *St = new StoreInst(CI->getArgOperand(0),
291 CI->getArgOperand(2), CI);
Gordon Henriksena6c99252007-12-22 17:27:01 +0000292 CI->replaceAllUsesWith(St);
293 CI->eraseFromParent();
294 }
295 break;
296 case Intrinsic::gcread:
297 if (LowerRd) {
298 // Replace a read barrier with a simple load.
Gabor Greif89c4cea2010-06-25 08:48:19 +0000299 Value *Ld = new LoadInst(CI->getArgOperand(1), "", CI);
Gordon Henriksena6c99252007-12-22 17:27:01 +0000300 Ld->takeName(CI);
301 CI->replaceAllUsesWith(Ld);
302 CI->eraseFromParent();
303 }
304 break;
305 case Intrinsic::gcroot:
306 if (InitRoots) {
307 // Initialize the GC root, but do not delete the intrinsic. The
308 // backend needs the intrinsic to flag the stack slot.
309 Roots.push_back(cast<AllocaInst>(
Gabor Greif89c4cea2010-06-25 08:48:19 +0000310 CI->getArgOperand(0)->stripPointerCasts()));
Gordon Henriksena6c99252007-12-22 17:27:01 +0000311 }
312 break;
313 default:
Gordon Henriksen364caf02007-09-29 02:13:43 +0000314 continue;
315 }
316
317 MadeChange = true;
318 }
319 }
320 }
321
322 if (Roots.size())
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000323 MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size());
Gordon Henriksen364caf02007-09-29 02:13:43 +0000324
325 return MadeChange;
326}
327
328// -----------------------------------------------------------------------------
329
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000330FunctionPass *llvm::createGCMachineCodeAnalysisPass() {
331 return new MachineCodeAnalysis();
332}
333
Gordon Henriksen364caf02007-09-29 02:13:43 +0000334char MachineCodeAnalysis::ID = 0;
335
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000336MachineCodeAnalysis::MachineCodeAnalysis()
Owen Anderson90c579d2010-08-06 18:33:48 +0000337 : MachineFunctionPass(ID) {}
Gordon Henriksen364caf02007-09-29 02:13:43 +0000338
339const char *MachineCodeAnalysis::getPassName() const {
340 return "Analyze Machine Code For Garbage Collection";
341}
342
343void MachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
344 MachineFunctionPass::getAnalysisUsage(AU);
345 AU.setPreservesAll();
346 AU.addRequired<MachineModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000347 AU.addRequired<GCModuleInfo>();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000348}
349
Chris Lattneraba9bcb2010-03-14 07:27:07 +0000350MCSymbol *MachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
351 MachineBasicBlock::iterator MI,
352 DebugLoc DL) const {
Chris Lattner77e76942010-03-17 05:41:18 +0000353 MCSymbol *Label = MBB.getParent()->getContext().CreateTempSymbol();
Chris Lattneraba9bcb2010-03-14 07:27:07 +0000354 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000355 return Label;
356}
357
358void MachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
359 // Find the return address (next instruction), too, so as to bracket the call
360 // instruction.
361 MachineBasicBlock::iterator RAI = CI;
362 ++RAI;
363
Nicolas Geoffray946e3c92010-09-24 17:27:50 +0000364 if (FI->getStrategy().needsSafePoint(GC::PreCall)) {
365 MCSymbol* Label = InsertLabel(*CI->getParent(), CI, CI->getDebugLoc());
366 FI->addSafePoint(GC::PreCall, Label, CI->getDebugLoc());
367 }
Gordon Henriksen364caf02007-09-29 02:13:43 +0000368
Nicolas Geoffray946e3c92010-09-24 17:27:50 +0000369 if (FI->getStrategy().needsSafePoint(GC::PostCall)) {
370 MCSymbol* Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc());
371 FI->addSafePoint(GC::PostCall, Label, CI->getDebugLoc());
372 }
Gordon Henriksen364caf02007-09-29 02:13:43 +0000373}
374
375void MachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
376 for (MachineFunction::iterator BBI = MF.begin(),
377 BBE = MF.end(); BBI != BBE; ++BBI)
378 for (MachineBasicBlock::iterator MI = BBI->begin(),
379 ME = BBI->end(); MI != ME; ++MI)
Chris Lattner749c6f62008-01-07 07:27:27 +0000380 if (MI->getDesc().isCall())
Dan Gohmanfd3ff032008-07-07 20:08:05 +0000381 VisitCallPoint(MI);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000382}
383
384void MachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000385 const TargetFrameLowering *TFI = TM->getFrameLowering();
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000386 assert(TFI && "TargetRegisterInfo not available!");
Gordon Henriksen364caf02007-09-29 02:13:43 +0000387
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000388 for (GCFunctionInfo::roots_iterator RI = FI->roots_begin(),
389 RE = FI->roots_end(); RI != RE; ++RI)
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000390 RI->StackOffset = TFI->getFrameIndexOffset(MF, RI->Num);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000391}
392
393bool MachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000394 // Quick exit for functions that do not use GC.
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000395 if (!MF.getFunction()->hasGC())
396 return false;
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000397
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000398 FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction());
399 if (!FI->getStrategy().needsSafePoints())
Gordon Henriksen364caf02007-09-29 02:13:43 +0000400 return false;
401
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000402 TM = &MF.getTarget();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000403 MMI = &getAnalysis<MachineModuleInfo>();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000404 TII = TM->getInstrInfo();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000405
406 // Find the size of the stack frame.
Gordon Henriksenfcbcfaa2008-08-19 17:06:35 +0000407 FI->setFrameSize(MF.getFrameInfo()->getStackSize());
Gordon Henriksen364caf02007-09-29 02:13:43 +0000408
409 // Find all safe points.
410 FindSafePoints(MF);
411
412 // Find the stack offsets for all roots.
413 FindStackOffsets(MF);
414
415 return false;
416}