blob: 9349797e228f6cc53360bc4ca245c896a04166cd [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),
Nicolas Geoffray7b8c2f82011-11-11 18:32:52 +0000100 CustomSafePoints(false),
Gordon Henriksenc317a602008-08-17 12:08:44 +0000101 InitRoots(true),
102 UsesMetadata(false)
Gordon Henriksen364caf02007-09-29 02:13:43 +0000103{}
104
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000105GCStrategy::~GCStrategy() {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000106 for (iterator I = begin(), E = end(); I != E; ++I)
107 delete *I;
108
109 Functions.clear();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000110}
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000111
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000112bool GCStrategy::initializeCustomLowering(Module &M) { return false; }
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000113
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000114bool GCStrategy::performCustomLowering(Function &F) {
David Greenecc54c7c2010-01-04 21:48:34 +0000115 dbgs() << "gc " << getName() << " must override performCustomLowering.\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000116 llvm_unreachable(0);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000117 return 0;
118}
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000119
Nicolas Geoffray7b8c2f82011-11-11 18:32:52 +0000120
121bool GCStrategy::findCustomSafePoints(GCFunctionInfo& FI, MachineFunction &F) {
122 dbgs() << "gc " << getName() << " must override findCustomSafePoints.\n";
123 llvm_unreachable(0);
124 return 0;
125}
126
127
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000128GCFunctionInfo *GCStrategy::insertFunctionInfo(const Function &F) {
129 GCFunctionInfo *FI = new GCFunctionInfo(F, *this);
130 Functions.push_back(FI);
131 return FI;
132}
Gordon Henriksen364caf02007-09-29 02:13:43 +0000133
134// -----------------------------------------------------------------------------
135
Owen Anderson081c34b2010-10-19 17:21:58 +0000136INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering",
137 false, false)
138INITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
139INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false)
140
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000141FunctionPass *llvm::createGCLoweringPass() {
142 return new LowerIntrinsics();
143}
144
Gordon Henriksen364caf02007-09-29 02:13:43 +0000145char LowerIntrinsics::ID = 0;
146
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000147LowerIntrinsics::LowerIntrinsics()
Owen Anderson081c34b2010-10-19 17:21:58 +0000148 : FunctionPass(ID) {
149 initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry());
150 }
Gordon Henriksen364caf02007-09-29 02:13:43 +0000151
152const char *LowerIntrinsics::getPassName() const {
153 return "Lower Garbage Collection Instructions";
154}
155
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000156void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
157 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000158 AU.addRequired<GCModuleInfo>();
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000159 AU.addPreserved<DominatorTree>();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000160}
161
162/// doInitialization - If this module uses the GC intrinsics, find them now.
Gordon Henriksen364caf02007-09-29 02:13:43 +0000163bool LowerIntrinsics::doInitialization(Module &M) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000164 // FIXME: This is rather antisocial in the context of a JIT since it performs
165 // work against the entire module. But this cannot be done at
166 // runFunction time (initializeCustomLowering likely needs to change
167 // the module).
Duncan Sands1465d612009-01-28 13:14:17 +0000168 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000169 assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?");
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000170 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000171 if (!I->isDeclaration() && I->hasGC())
172 MI->getFunctionInfo(*I); // Instantiate the GC strategy.
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000173
174 bool MadeChange = false;
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000175 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000176 if (NeedsCustomLoweringPass(**I))
177 if ((*I)->initializeCustomLowering(M))
178 MadeChange = true;
179
180 return MadeChange;
Gordon Henriksen364caf02007-09-29 02:13:43 +0000181}
182
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000183bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots,
Gordon Henriksen364caf02007-09-29 02:13:43 +0000184 unsigned Count) {
185 // Scroll past alloca instructions.
186 BasicBlock::iterator IP = F.getEntryBlock().begin();
187 while (isa<AllocaInst>(IP)) ++IP;
188
189 // Search for initializers in the initial BB.
190 SmallPtrSet<AllocaInst*,16> InitedRoots;
191 for (; !CouldBecomeSafePoint(IP); ++IP)
192 if (StoreInst *SI = dyn_cast<StoreInst>(IP))
Anton Korobeynikovb04addd2008-05-06 22:52:30 +0000193 if (AllocaInst *AI =
Anton Korobeynikov0b12ecf2008-05-07 22:54:15 +0000194 dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts()))
Gordon Henriksen364caf02007-09-29 02:13:43 +0000195 InitedRoots.insert(AI);
196
197 // Add root initializers.
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000198 bool MadeChange = false;
199
Gordon Henriksen364caf02007-09-29 02:13:43 +0000200 for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000201 if (!InitedRoots.count(*I)) {
Nicolas Geoffray8538f042010-04-15 19:53:35 +0000202 StoreInst* SI = new StoreInst(ConstantPointerNull::get(cast<PointerType>(
203 cast<PointerType>((*I)->getType())->getElementType())),
204 *I);
205 SI->insertAfter(*I);
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000206 MadeChange = true;
207 }
208
209 return MadeChange;
210}
211
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000212bool LowerIntrinsics::NeedsDefaultLoweringPass(const GCStrategy &C) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000213 // Default lowering is necessary only if read or write barriers have a default
214 // action. The default for roots is no action.
215 return !C.customWriteBarrier()
216 || !C.customReadBarrier()
217 || C.initializeRoots();
218}
219
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000220bool LowerIntrinsics::NeedsCustomLoweringPass(const GCStrategy &C) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000221 // Custom lowering is only necessary if enabled for some action.
222 return C.customWriteBarrier()
223 || C.customReadBarrier()
224 || C.customRoots();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000225}
226
227/// CouldBecomeSafePoint - Predicate to conservatively determine whether the
228/// instruction could introduce a safe point.
229bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) {
230 // The natural definition of instructions which could introduce safe points
231 // are:
232 //
233 // - call, invoke (AfterCall, BeforeCall)
234 // - phis (Loops)
235 // - invoke, ret, unwind (Exit)
236 //
237 // However, instructions as seemingly inoccuous as arithmetic can become
238 // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead
239 // it is necessary to take a conservative approach.
240
241 if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) ||
242 isa<StoreInst>(I) || isa<LoadInst>(I))
243 return false;
244
245 // llvm.gcroot is safe because it doesn't do anything at runtime.
246 if (CallInst *CI = dyn_cast<CallInst>(I))
247 if (Function *F = CI->getCalledFunction())
248 if (unsigned IID = F->getIntrinsicID())
249 if (IID == Intrinsic::gcroot)
250 return false;
251
252 return true;
253}
254
255/// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores.
256/// Leave gcroot intrinsics; the code generator needs to see those.
257bool LowerIntrinsics::runOnFunction(Function &F) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000258 // Quick exit for functions that do not use GC.
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000259 if (!F.hasGC())
260 return false;
Gordon Henriksen364caf02007-09-29 02:13:43 +0000261
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000262 GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F);
263 GCStrategy &S = FI.getStrategy();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000264
265 bool MadeChange = false;
266
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000267 if (NeedsDefaultLoweringPass(S))
268 MadeChange |= PerformDefaultLowering(F, S);
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000269
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000270 bool UseCustomLoweringPass = NeedsCustomLoweringPass(S);
271 if (UseCustomLoweringPass)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000272 MadeChange |= S.performCustomLowering(F);
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000273
274 // Custom lowering may modify the CFG, so dominators must be recomputed.
275 if (UseCustomLoweringPass) {
276 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>())
277 DT->DT->recalculate(F);
278 }
279
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000280 return MadeChange;
281}
282
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000283bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) {
284 bool LowerWr = !S.customWriteBarrier();
285 bool LowerRd = !S.customReadBarrier();
286 bool InitRoots = S.initializeRoots();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000287
Gabor Greifa3997812010-07-22 10:37:47 +0000288 SmallVector<AllocaInst*, 32> Roots;
Gordon Henriksen364caf02007-09-29 02:13:43 +0000289
290 bool MadeChange = false;
291 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
292 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) {
Gordon Henriksena6c99252007-12-22 17:27:01 +0000293 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) {
Gordon Henriksen364caf02007-09-29 02:13:43 +0000294 Function *F = CI->getCalledFunction();
Gordon Henriksena6c99252007-12-22 17:27:01 +0000295 switch (F->getIntrinsicID()) {
296 case Intrinsic::gcwrite:
297 if (LowerWr) {
298 // Replace a write barrier with a simple store.
Gabor Greifa3997812010-07-22 10:37:47 +0000299 Value *St = new StoreInst(CI->getArgOperand(0),
300 CI->getArgOperand(2), CI);
Gordon Henriksena6c99252007-12-22 17:27:01 +0000301 CI->replaceAllUsesWith(St);
302 CI->eraseFromParent();
303 }
304 break;
305 case Intrinsic::gcread:
306 if (LowerRd) {
307 // Replace a read barrier with a simple load.
Gabor Greif89c4cea2010-06-25 08:48:19 +0000308 Value *Ld = new LoadInst(CI->getArgOperand(1), "", CI);
Gordon Henriksena6c99252007-12-22 17:27:01 +0000309 Ld->takeName(CI);
310 CI->replaceAllUsesWith(Ld);
311 CI->eraseFromParent();
312 }
313 break;
314 case Intrinsic::gcroot:
315 if (InitRoots) {
316 // Initialize the GC root, but do not delete the intrinsic. The
317 // backend needs the intrinsic to flag the stack slot.
318 Roots.push_back(cast<AllocaInst>(
Gabor Greif89c4cea2010-06-25 08:48:19 +0000319 CI->getArgOperand(0)->stripPointerCasts()));
Gordon Henriksena6c99252007-12-22 17:27:01 +0000320 }
321 break;
322 default:
Gordon Henriksen364caf02007-09-29 02:13:43 +0000323 continue;
324 }
325
326 MadeChange = true;
327 }
328 }
329 }
330
331 if (Roots.size())
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000332 MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size());
Gordon Henriksen364caf02007-09-29 02:13:43 +0000333
334 return MadeChange;
335}
336
337// -----------------------------------------------------------------------------
338
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000339FunctionPass *llvm::createGCMachineCodeAnalysisPass() {
340 return new MachineCodeAnalysis();
341}
342
Gordon Henriksen364caf02007-09-29 02:13:43 +0000343char MachineCodeAnalysis::ID = 0;
344
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000345MachineCodeAnalysis::MachineCodeAnalysis()
Owen Anderson90c579d2010-08-06 18:33:48 +0000346 : MachineFunctionPass(ID) {}
Gordon Henriksen364caf02007-09-29 02:13:43 +0000347
348const char *MachineCodeAnalysis::getPassName() const {
349 return "Analyze Machine Code For Garbage Collection";
350}
351
352void MachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
353 MachineFunctionPass::getAnalysisUsage(AU);
354 AU.setPreservesAll();
355 AU.addRequired<MachineModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000356 AU.addRequired<GCModuleInfo>();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000357}
358
Chris Lattneraba9bcb2010-03-14 07:27:07 +0000359MCSymbol *MachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
360 MachineBasicBlock::iterator MI,
361 DebugLoc DL) const {
Chris Lattner77e76942010-03-17 05:41:18 +0000362 MCSymbol *Label = MBB.getParent()->getContext().CreateTempSymbol();
Chris Lattneraba9bcb2010-03-14 07:27:07 +0000363 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000364 return Label;
365}
366
367void MachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
368 // Find the return address (next instruction), too, so as to bracket the call
369 // instruction.
370 MachineBasicBlock::iterator RAI = CI;
371 ++RAI;
372
Nicolas Geoffray946e3c92010-09-24 17:27:50 +0000373 if (FI->getStrategy().needsSafePoint(GC::PreCall)) {
374 MCSymbol* Label = InsertLabel(*CI->getParent(), CI, CI->getDebugLoc());
375 FI->addSafePoint(GC::PreCall, Label, CI->getDebugLoc());
376 }
Gordon Henriksen364caf02007-09-29 02:13:43 +0000377
Nicolas Geoffray946e3c92010-09-24 17:27:50 +0000378 if (FI->getStrategy().needsSafePoint(GC::PostCall)) {
379 MCSymbol* Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc());
380 FI->addSafePoint(GC::PostCall, Label, CI->getDebugLoc());
381 }
Gordon Henriksen364caf02007-09-29 02:13:43 +0000382}
383
384void MachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
385 for (MachineFunction::iterator BBI = MF.begin(),
386 BBE = MF.end(); BBI != BBE; ++BBI)
387 for (MachineBasicBlock::iterator MI = BBI->begin(),
388 ME = BBI->end(); MI != ME; ++MI)
Chris Lattner749c6f62008-01-07 07:27:27 +0000389 if (MI->getDesc().isCall())
Dan Gohmanfd3ff032008-07-07 20:08:05 +0000390 VisitCallPoint(MI);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000391}
392
393void MachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000394 const TargetFrameLowering *TFI = TM->getFrameLowering();
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000395 assert(TFI && "TargetRegisterInfo not available!");
Gordon Henriksen364caf02007-09-29 02:13:43 +0000396
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000397 for (GCFunctionInfo::roots_iterator RI = FI->roots_begin(),
398 RE = FI->roots_end(); RI != RE; ++RI)
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000399 RI->StackOffset = TFI->getFrameIndexOffset(MF, RI->Num);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000400}
401
402bool MachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000403 // Quick exit for functions that do not use GC.
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000404 if (!MF.getFunction()->hasGC())
405 return false;
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000406
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000407 FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction());
408 if (!FI->getStrategy().needsSafePoints())
Gordon Henriksen364caf02007-09-29 02:13:43 +0000409 return false;
410
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000411 TM = &MF.getTarget();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000412 MMI = &getAnalysis<MachineModuleInfo>();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000413 TII = TM->getInstrInfo();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000414
415 // Find the size of the stack frame.
Gordon Henriksenfcbcfaa2008-08-19 17:06:35 +0000416 FI->setFrameSize(MF.getFrameInfo()->getStackSize());
Nicolas Geoffray7b8c2f82011-11-11 18:32:52 +0000417
Gordon Henriksen364caf02007-09-29 02:13:43 +0000418 // Find all safe points.
Nicolas Geoffray7b8c2f82011-11-11 18:32:52 +0000419 if (FI->getStrategy().customSafePoints()) {
420 FI->getStrategy().findCustomSafePoints(*FI, MF);
421 } else {
422 FindSafePoints(MF);
423 }
Gordon Henriksen364caf02007-09-29 02:13:43 +0000424
425 // Find the stack offsets for all roots.
426 FindStackOffsets(MF);
427
428 return false;
429}