blob: b31a0f2d0bc05735c1c545fbccb87279331bf3b0 [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//
Andrew Trick1dd8c852012-02-08 21:23:13 +000013// GCMachineCodeAnalysis identifies the GC safe points in the machine code.
14// Roots are identified in SelectionDAGISel.
Gordon Henriksen5eca0752008-08-17 18:44:35 +000015//
Gordon Henriksen364caf02007-09-29 02:13:43 +000016//===----------------------------------------------------------------------===//
17
Gordon Henriksen5a29c9e2008-08-17 12:56:54 +000018#include "llvm/CodeGen/GCStrategy.h"
Gordon Henriksen364caf02007-09-29 02:13:43 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/CodeGen/Passes.h"
Stephen Hines36b56882014-04-23 16:57:46 -070024#include "llvm/IR/Dominators.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000025#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000030#include "llvm/Target/TargetFrameLowering.h"
Gordon Henriksen364caf02007-09-29 02:13:43 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Gordon Henriksenfcbcfaa2008-08-19 17:06:35 +000033#include "llvm/Target/TargetRegisterInfo.h"
Gordon Henriksen364caf02007-09-29 02:13:43 +000034
35using namespace llvm;
36
37namespace {
Andrew Trick1df91b02012-02-08 21:22:43 +000038
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000039 /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
Andrew Trick1df91b02012-02-08 21:22:43 +000040 /// 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);
Andrew Trick1df91b02012-02-08 21:22:43 +000050
Gordon Henriksen364caf02007-09-29 02:13:43 +000051 public:
52 static char ID;
Andrew Trick1df91b02012-02-08 21:22:43 +000053
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000054 LowerIntrinsics();
Stephen Hines36b56882014-04-23 16:57:46 -070055 const char *getPassName() const override;
56 void getAnalysisUsage(AnalysisUsage &AU) const override;
Andrew Trick1df91b02012-02-08 21:22:43 +000057
Stephen Hines36b56882014-04-23 16:57:46 -070058 bool doInitialization(Module &M) override;
59 bool runOnFunction(Function &F) override;
Gordon Henriksen364caf02007-09-29 02:13:43 +000060 };
Andrew Trick1df91b02012-02-08 21:22:43 +000061
62
Andrew Trick1dd8c852012-02-08 21:23:13 +000063 /// GCMachineCodeAnalysis - This is a target-independent pass over the machine
Gordon Henriksenad93c4f2007-12-11 00:30:17 +000064 /// 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.
Andrew Trick1dd8c852012-02-08 21:23:13 +000067 class GCMachineCodeAnalysis : 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;
Andrew Trick1df91b02012-02-08 21:22:43 +000072
Gordon Henriksen364caf02007-09-29 02:13:43 +000073 void FindSafePoints(MachineFunction &MF);
74 void VisitCallPoint(MachineBasicBlock::iterator MI);
Andrew Trick1df91b02012-02-08 21:22:43 +000075 MCSymbol *InsertLabel(MachineBasicBlock &MBB,
Chris Lattneraba9bcb2010-03-14 07:27:07 +000076 MachineBasicBlock::iterator MI,
77 DebugLoc DL) const;
Andrew Trick1df91b02012-02-08 21:22:43 +000078
Gordon Henriksen364caf02007-09-29 02:13:43 +000079 void FindStackOffsets(MachineFunction &MF);
Andrew Trick1df91b02012-02-08 21:22:43 +000080
Gordon Henriksen364caf02007-09-29 02:13:43 +000081 public:
82 static char ID;
Andrew Trick1df91b02012-02-08 21:22:43 +000083
Andrew Trick1dd8c852012-02-08 21:23:13 +000084 GCMachineCodeAnalysis();
Stephen Hines36b56882014-04-23 16:57:46 -070085 void getAnalysisUsage(AnalysisUsage &AU) const override;
Andrew Trick1df91b02012-02-08 21:22:43 +000086
Stephen Hines36b56882014-04-23 16:57:46 -070087 bool runOnMachineFunction(MachineFunction &MF) override;
Gordon Henriksen364caf02007-09-29 02:13:43 +000088 };
Andrew Trick1df91b02012-02-08 21:22:43 +000089
Gordon Henriksen364caf02007-09-29 02:13:43 +000090}
91
92// -----------------------------------------------------------------------------
93
Gordon Henriksen5eca0752008-08-17 18:44:35 +000094GCStrategy::GCStrategy() :
Gordon Henriksen364caf02007-09-29 02:13:43 +000095 NeededSafePoints(0),
96 CustomReadBarriers(false),
97 CustomWriteBarriers(false),
98 CustomRoots(false),
Nicolas Geoffray7b8c2f82011-11-11 18:32:52 +000099 CustomSafePoints(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;
Andrew Trick1df91b02012-02-08 21:22:43 +0000107
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000108 Functions.clear();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000109}
Andrew Trick1df91b02012-02-08 21:22:43 +0000110
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000111bool GCStrategy::initializeCustomLowering(Module &M) { return false; }
Andrew Trick1df91b02012-02-08 21:22:43 +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";
Ahmed Charlesb0934ab2012-02-19 11:37:01 +0000115 llvm_unreachable("must override performCustomLowering");
Gordon Henriksen364caf02007-09-29 02:13:43 +0000116}
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000117
Nicolas Geoffray7b8c2f82011-11-11 18:32:52 +0000118
119bool GCStrategy::findCustomSafePoints(GCFunctionInfo& FI, MachineFunction &F) {
120 dbgs() << "gc " << getName() << " must override findCustomSafePoints.\n";
121 llvm_unreachable(0);
Nicolas Geoffray7b8c2f82011-11-11 18:32:52 +0000122}
123
124
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000125GCFunctionInfo *GCStrategy::insertFunctionInfo(const Function &F) {
126 GCFunctionInfo *FI = new GCFunctionInfo(F, *this);
127 Functions.push_back(FI);
128 return FI;
129}
Gordon Henriksen364caf02007-09-29 02:13:43 +0000130
131// -----------------------------------------------------------------------------
132
Owen Anderson081c34b2010-10-19 17:21:58 +0000133INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering",
134 false, false)
135INITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
136INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false)
137
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000138FunctionPass *llvm::createGCLoweringPass() {
139 return new LowerIntrinsics();
140}
Andrew Trick1df91b02012-02-08 21:22:43 +0000141
Gordon Henriksen364caf02007-09-29 02:13:43 +0000142char LowerIntrinsics::ID = 0;
143
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000144LowerIntrinsics::LowerIntrinsics()
Owen Anderson081c34b2010-10-19 17:21:58 +0000145 : FunctionPass(ID) {
146 initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry());
147 }
Gordon Henriksen364caf02007-09-29 02:13:43 +0000148
149const char *LowerIntrinsics::getPassName() const {
150 return "Lower Garbage Collection Instructions";
151}
Andrew Trick1df91b02012-02-08 21:22:43 +0000152
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000153void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
154 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000155 AU.addRequired<GCModuleInfo>();
Stephen Hines36b56882014-04-23 16:57:46 -0700156 AU.addPreserved<DominatorTreeWrapperPass>();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000157}
158
159/// doInitialization - If this module uses the GC intrinsics, find them now.
Gordon Henriksen364caf02007-09-29 02:13:43 +0000160bool LowerIntrinsics::doInitialization(Module &M) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000161 // FIXME: This is rather antisocial in the context of a JIT since it performs
162 // work against the entire module. But this cannot be done at
163 // runFunction time (initializeCustomLowering likely needs to change
164 // the module).
Duncan Sands1465d612009-01-28 13:14:17 +0000165 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000166 assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?");
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000167 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000168 if (!I->isDeclaration() && I->hasGC())
169 MI->getFunctionInfo(*I); // Instantiate the GC strategy.
Andrew Trick1df91b02012-02-08 21:22:43 +0000170
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000171 bool MadeChange = false;
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000172 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000173 if (NeedsCustomLoweringPass(**I))
174 if ((*I)->initializeCustomLowering(M))
175 MadeChange = true;
Andrew Trick1df91b02012-02-08 21:22:43 +0000176
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000177 return MadeChange;
Gordon Henriksen364caf02007-09-29 02:13:43 +0000178}
179
Andrew Trick1df91b02012-02-08 21:22:43 +0000180bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots,
Gordon Henriksen364caf02007-09-29 02:13:43 +0000181 unsigned Count) {
182 // Scroll past alloca instructions.
183 BasicBlock::iterator IP = F.getEntryBlock().begin();
184 while (isa<AllocaInst>(IP)) ++IP;
Andrew Trick1df91b02012-02-08 21:22:43 +0000185
Gordon Henriksen364caf02007-09-29 02:13:43 +0000186 // Search for initializers in the initial BB.
187 SmallPtrSet<AllocaInst*,16> InitedRoots;
188 for (; !CouldBecomeSafePoint(IP); ++IP)
189 if (StoreInst *SI = dyn_cast<StoreInst>(IP))
Anton Korobeynikovb04addd2008-05-06 22:52:30 +0000190 if (AllocaInst *AI =
Anton Korobeynikov0b12ecf2008-05-07 22:54:15 +0000191 dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts()))
Gordon Henriksen364caf02007-09-29 02:13:43 +0000192 InitedRoots.insert(AI);
Andrew Trick1df91b02012-02-08 21:22:43 +0000193
Gordon Henriksen364caf02007-09-29 02:13:43 +0000194 // Add root initializers.
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000195 bool MadeChange = false;
Andrew Trick1df91b02012-02-08 21:22:43 +0000196
Gordon Henriksen364caf02007-09-29 02:13:43 +0000197 for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000198 if (!InitedRoots.count(*I)) {
Nicolas Geoffray8538f042010-04-15 19:53:35 +0000199 StoreInst* SI = new StoreInst(ConstantPointerNull::get(cast<PointerType>(
200 cast<PointerType>((*I)->getType())->getElementType())),
201 *I);
202 SI->insertAfter(*I);
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000203 MadeChange = true;
204 }
Andrew Trick1df91b02012-02-08 21:22:43 +0000205
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000206 return MadeChange;
207}
208
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000209bool LowerIntrinsics::NeedsDefaultLoweringPass(const GCStrategy &C) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000210 // Default lowering is necessary only if read or write barriers have a default
211 // action. The default for roots is no action.
212 return !C.customWriteBarrier()
213 || !C.customReadBarrier()
214 || C.initializeRoots();
215}
216
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000217bool LowerIntrinsics::NeedsCustomLoweringPass(const GCStrategy &C) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000218 // Custom lowering is only necessary if enabled for some action.
219 return C.customWriteBarrier()
220 || C.customReadBarrier()
221 || C.customRoots();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000222}
223
224/// CouldBecomeSafePoint - Predicate to conservatively determine whether the
225/// instruction could introduce a safe point.
226bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) {
227 // The natural definition of instructions which could introduce safe points
228 // are:
Andrew Trick1df91b02012-02-08 21:22:43 +0000229 //
Gordon Henriksen364caf02007-09-29 02:13:43 +0000230 // - call, invoke (AfterCall, BeforeCall)
231 // - phis (Loops)
232 // - invoke, ret, unwind (Exit)
Andrew Trick1df91b02012-02-08 21:22:43 +0000233 //
Gordon Henriksen364caf02007-09-29 02:13:43 +0000234 // However, instructions as seemingly inoccuous as arithmetic can become
235 // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead
236 // it is necessary to take a conservative approach.
Andrew Trick1df91b02012-02-08 21:22:43 +0000237
Gordon Henriksen364caf02007-09-29 02:13:43 +0000238 if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) ||
239 isa<StoreInst>(I) || isa<LoadInst>(I))
240 return false;
Andrew Trick1df91b02012-02-08 21:22:43 +0000241
Gordon Henriksen364caf02007-09-29 02:13:43 +0000242 // llvm.gcroot is safe because it doesn't do anything at runtime.
243 if (CallInst *CI = dyn_cast<CallInst>(I))
244 if (Function *F = CI->getCalledFunction())
245 if (unsigned IID = F->getIntrinsicID())
246 if (IID == Intrinsic::gcroot)
247 return false;
Andrew Trick1df91b02012-02-08 21:22:43 +0000248
Gordon Henriksen364caf02007-09-29 02:13:43 +0000249 return true;
250}
251
252/// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores.
253/// Leave gcroot intrinsics; the code generator needs to see those.
254bool LowerIntrinsics::runOnFunction(Function &F) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000255 // Quick exit for functions that do not use GC.
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000256 if (!F.hasGC())
257 return false;
Andrew Trick1df91b02012-02-08 21:22:43 +0000258
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000259 GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F);
260 GCStrategy &S = FI.getStrategy();
Andrew Trick1df91b02012-02-08 21:22:43 +0000261
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000262 bool MadeChange = false;
Andrew Trick1df91b02012-02-08 21:22:43 +0000263
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000264 if (NeedsDefaultLoweringPass(S))
265 MadeChange |= PerformDefaultLowering(F, S);
Andrew Trick1df91b02012-02-08 21:22:43 +0000266
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000267 bool UseCustomLoweringPass = NeedsCustomLoweringPass(S);
268 if (UseCustomLoweringPass)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000269 MadeChange |= S.performCustomLowering(F);
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000270
271 // Custom lowering may modify the CFG, so dominators must be recomputed.
272 if (UseCustomLoweringPass) {
Stephen Hines36b56882014-04-23 16:57:46 -0700273 if (DominatorTreeWrapperPass *DTWP =
274 getAnalysisIfAvailable<DominatorTreeWrapperPass>())
275 DTWP->getDomTree().recalculate(F);
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000276 }
277
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000278 return MadeChange;
279}
280
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000281bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) {
282 bool LowerWr = !S.customWriteBarrier();
283 bool LowerRd = !S.customReadBarrier();
284 bool InitRoots = S.initializeRoots();
Andrew Trick1df91b02012-02-08 21:22:43 +0000285
Gabor Greifa3997812010-07-22 10:37:47 +0000286 SmallVector<AllocaInst*, 32> Roots;
Andrew Trick1df91b02012-02-08 21:22:43 +0000287
Gordon Henriksen364caf02007-09-29 02:13:43 +0000288 bool MadeChange = false;
289 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
290 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) {
Gordon Henriksena6c99252007-12-22 17:27:01 +0000291 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) {
Gordon Henriksen364caf02007-09-29 02:13:43 +0000292 Function *F = CI->getCalledFunction();
Gordon Henriksena6c99252007-12-22 17:27:01 +0000293 switch (F->getIntrinsicID()) {
294 case Intrinsic::gcwrite:
295 if (LowerWr) {
296 // Replace a write barrier with a simple store.
Gabor Greifa3997812010-07-22 10:37:47 +0000297 Value *St = new StoreInst(CI->getArgOperand(0),
298 CI->getArgOperand(2), CI);
Gordon Henriksena6c99252007-12-22 17:27:01 +0000299 CI->replaceAllUsesWith(St);
300 CI->eraseFromParent();
301 }
302 break;
303 case Intrinsic::gcread:
304 if (LowerRd) {
305 // Replace a read barrier with a simple load.
Gabor Greif89c4cea2010-06-25 08:48:19 +0000306 Value *Ld = new LoadInst(CI->getArgOperand(1), "", CI);
Gordon Henriksena6c99252007-12-22 17:27:01 +0000307 Ld->takeName(CI);
308 CI->replaceAllUsesWith(Ld);
309 CI->eraseFromParent();
310 }
311 break;
312 case Intrinsic::gcroot:
313 if (InitRoots) {
314 // Initialize the GC root, but do not delete the intrinsic. The
315 // backend needs the intrinsic to flag the stack slot.
316 Roots.push_back(cast<AllocaInst>(
Gabor Greif89c4cea2010-06-25 08:48:19 +0000317 CI->getArgOperand(0)->stripPointerCasts()));
Gordon Henriksena6c99252007-12-22 17:27:01 +0000318 }
319 break;
320 default:
Gordon Henriksen364caf02007-09-29 02:13:43 +0000321 continue;
322 }
Andrew Trick1df91b02012-02-08 21:22:43 +0000323
Gordon Henriksen364caf02007-09-29 02:13:43 +0000324 MadeChange = true;
325 }
326 }
327 }
Andrew Trick1df91b02012-02-08 21:22:43 +0000328
Gordon Henriksen364caf02007-09-29 02:13:43 +0000329 if (Roots.size())
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000330 MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size());
Andrew Trick1df91b02012-02-08 21:22:43 +0000331
Gordon Henriksen364caf02007-09-29 02:13:43 +0000332 return MadeChange;
333}
334
335// -----------------------------------------------------------------------------
336
Andrew Trick1dd8c852012-02-08 21:23:13 +0000337char GCMachineCodeAnalysis::ID = 0;
338char &llvm::GCMachineCodeAnalysisID = GCMachineCodeAnalysis::ID;
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000339
Andrew Trick1dd8c852012-02-08 21:23:13 +0000340INITIALIZE_PASS(GCMachineCodeAnalysis, "gc-analysis",
341 "Analyze Machine Code For Garbage Collection", false, false)
Gordon Henriksen364caf02007-09-29 02:13:43 +0000342
Andrew Trick1dd8c852012-02-08 21:23:13 +0000343GCMachineCodeAnalysis::GCMachineCodeAnalysis()
Owen Anderson90c579d2010-08-06 18:33:48 +0000344 : MachineFunctionPass(ID) {}
Gordon Henriksen364caf02007-09-29 02:13:43 +0000345
Andrew Trick1dd8c852012-02-08 21:23:13 +0000346void GCMachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksen364caf02007-09-29 02:13:43 +0000347 MachineFunctionPass::getAnalysisUsage(AU);
348 AU.setPreservesAll();
349 AU.addRequired<MachineModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000350 AU.addRequired<GCModuleInfo>();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000351}
352
Andrew Trick1dd8c852012-02-08 21:23:13 +0000353MCSymbol *GCMachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
354 MachineBasicBlock::iterator MI,
355 DebugLoc DL) const {
Chris Lattner77e76942010-03-17 05:41:18 +0000356 MCSymbol *Label = MBB.getParent()->getContext().CreateTempSymbol();
Chris Lattneraba9bcb2010-03-14 07:27:07 +0000357 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000358 return Label;
359}
360
Andrew Trick1dd8c852012-02-08 21:23:13 +0000361void GCMachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
Gordon Henriksen364caf02007-09-29 02:13:43 +0000362 // Find the return address (next instruction), too, so as to bracket the call
363 // instruction.
Andrew Trick1df91b02012-02-08 21:22:43 +0000364 MachineBasicBlock::iterator RAI = CI;
365 ++RAI;
366
Nicolas Geoffray946e3c92010-09-24 17:27:50 +0000367 if (FI->getStrategy().needsSafePoint(GC::PreCall)) {
368 MCSymbol* Label = InsertLabel(*CI->getParent(), CI, CI->getDebugLoc());
369 FI->addSafePoint(GC::PreCall, Label, CI->getDebugLoc());
370 }
Andrew Trick1df91b02012-02-08 21:22:43 +0000371
Nicolas Geoffray946e3c92010-09-24 17:27:50 +0000372 if (FI->getStrategy().needsSafePoint(GC::PostCall)) {
373 MCSymbol* Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc());
374 FI->addSafePoint(GC::PostCall, Label, CI->getDebugLoc());
375 }
Gordon Henriksen364caf02007-09-29 02:13:43 +0000376}
377
Andrew Trick1dd8c852012-02-08 21:23:13 +0000378void GCMachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
Gordon Henriksen364caf02007-09-29 02:13:43 +0000379 for (MachineFunction::iterator BBI = MF.begin(),
380 BBE = MF.end(); BBI != BBE; ++BBI)
381 for (MachineBasicBlock::iterator MI = BBI->begin(),
382 ME = BBI->end(); MI != ME; ++MI)
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000383 if (MI->isCall())
Dan Gohmanfd3ff032008-07-07 20:08:05 +0000384 VisitCallPoint(MI);
Gordon Henriksen364caf02007-09-29 02:13:43 +0000385}
386
Andrew Trick1dd8c852012-02-08 21:23:13 +0000387void GCMachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000388 const TargetFrameLowering *TFI = TM->getFrameLowering();
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000389 assert(TFI && "TargetRegisterInfo not available!");
Andrew Trick1df91b02012-02-08 21:22:43 +0000390
Nicolas Geoffray0bd10f22012-10-26 09:15:55 +0000391 for (GCFunctionInfo::roots_iterator RI = FI->roots_begin();
392 RI != FI->roots_end();) {
393 // If the root references a dead object, no need to keep it.
394 if (MF.getFrameInfo()->isDeadObjectIndex(RI->Num)) {
395 RI = FI->removeStackRoot(RI);
396 } else {
397 RI->StackOffset = TFI->getFrameIndexOffset(MF, RI->Num);
398 ++RI;
399 }
400 }
Gordon Henriksen364caf02007-09-29 02:13:43 +0000401}
402
Andrew Trick1dd8c852012-02-08 21:23:13 +0000403bool GCMachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000404 // Quick exit for functions that do not use GC.
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000405 if (!MF.getFunction()->hasGC())
406 return false;
Andrew Trick1df91b02012-02-08 21:22:43 +0000407
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000408 FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction());
409 if (!FI->getStrategy().needsSafePoints())
Gordon Henriksen364caf02007-09-29 02:13:43 +0000410 return false;
Andrew Trick1df91b02012-02-08 21:22:43 +0000411
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000412 TM = &MF.getTarget();
Gordon Henriksen364caf02007-09-29 02:13:43 +0000413 MMI = &getAnalysis<MachineModuleInfo>();
Gordon Henriksenad93c4f2007-12-11 00:30:17 +0000414 TII = TM->getInstrInfo();
Andrew Trick1df91b02012-02-08 21:22:43 +0000415
Gordon Henriksen364caf02007-09-29 02:13:43 +0000416 // Find the size of the stack frame.
Gordon Henriksenfcbcfaa2008-08-19 17:06:35 +0000417 FI->setFrameSize(MF.getFrameInfo()->getStackSize());
Nicolas Geoffray7b8c2f82011-11-11 18:32:52 +0000418
Gordon Henriksen364caf02007-09-29 02:13:43 +0000419 // Find all safe points.
Nicolas Geoffray7b8c2f82011-11-11 18:32:52 +0000420 if (FI->getStrategy().customSafePoints()) {
421 FI->getStrategy().findCustomSafePoints(*FI, MF);
422 } else {
423 FindSafePoints(MF);
424 }
Andrew Trick1df91b02012-02-08 21:22:43 +0000425
Gordon Henriksen364caf02007-09-29 02:13:43 +0000426 // Find the stack offsets for all roots.
427 FindStackOffsets(MF);
Andrew Trick1df91b02012-02-08 21:22:43 +0000428
Gordon Henriksen364caf02007-09-29 02:13:43 +0000429 return false;
430}