blob: 05c36fcb00d4c99cae475a9eaf2719f79ed43324 [file] [log] [blame]
Gordon Henriksend930f912008-08-17 18:44:35 +00001//===-- GCStrategy.cpp - Garbage collection infrastructure -----------------===//
Gordon Henriksen37ca83d2007-09-29 02:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Henriksen37ca83d2007-09-29 02:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements target- and collector-independent garbage collection
11// infrastructure.
12//
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000013// GCMachineCodeAnalysis identifies the GC safe points in the machine code.
14// Roots are identified in SelectionDAGISel.
Gordon Henriksend930f912008-08-17 18:44:35 +000015//
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000016//===----------------------------------------------------------------------===//
17
Gordon Henriksenbcef14d2008-08-17 12:56:54 +000018#include "llvm/CodeGen/GCStrategy.h"
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/CodeGen/Passes.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000024#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-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 Korobeynikov2f931282011-01-10 12:39:04 +000030#include "llvm/Target/TargetFrameLowering.h"
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Gordon Henriksen72bd9df2008-08-19 17:06:35 +000033#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000034#include "llvm/Target/TargetSubtargetInfo.h"
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000035
36using namespace llvm;
37
38namespace {
Andrew Trick9e761992012-02-08 21:22:43 +000039
Gordon Henriksen7843c162007-12-11 00:30:17 +000040 /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
Andrew Trick9e761992012-02-08 21:22:43 +000041 /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
Gordon Henriksend930f912008-08-17 18:44:35 +000042 /// directed by the GCStrategy. It also performs automatic root initialization
Gordon Henriksen7843c162007-12-11 00:30:17 +000043 /// and custom intrinsic lowering.
Nick Lewycky02d5f772009-10-25 06:33:48 +000044 class LowerIntrinsics : public FunctionPass {
Gordon Henriksend930f912008-08-17 18:44:35 +000045 static bool NeedsDefaultLoweringPass(const GCStrategy &C);
46 static bool NeedsCustomLoweringPass(const GCStrategy &C);
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000047 static bool CouldBecomeSafePoint(Instruction *I);
Gordon Henriksend930f912008-08-17 18:44:35 +000048 bool PerformDefaultLowering(Function &F, GCStrategy &Coll);
Gordon Henriksen7843c162007-12-11 00:30:17 +000049 static bool InsertRootInitializers(Function &F,
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000050 AllocaInst **Roots, unsigned Count);
Andrew Trick9e761992012-02-08 21:22:43 +000051
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000052 public:
53 static char ID;
Andrew Trick9e761992012-02-08 21:22:43 +000054
Gordon Henriksen7843c162007-12-11 00:30:17 +000055 LowerIntrinsics();
Craig Topper4584cd52014-03-07 09:26:03 +000056 const char *getPassName() const override;
57 void getAnalysisUsage(AnalysisUsage &AU) const override;
Andrew Trick9e761992012-02-08 21:22:43 +000058
Craig Topper4584cd52014-03-07 09:26:03 +000059 bool doInitialization(Module &M) override;
60 bool runOnFunction(Function &F) override;
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000061 };
Andrew Trick9e761992012-02-08 21:22:43 +000062
63
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000064 /// GCMachineCodeAnalysis - This is a target-independent pass over the machine
Gordon Henriksen7843c162007-12-11 00:30:17 +000065 /// function representation to identify safe points for the garbage collector
66 /// in the machine code. It inserts labels at safe points and populates a
Gordon Henriksend930f912008-08-17 18:44:35 +000067 /// GCMetadata record for each function.
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000068 class GCMachineCodeAnalysis : public MachineFunctionPass {
Gordon Henriksen7843c162007-12-11 00:30:17 +000069 const TargetMachine *TM;
Gordon Henriksend930f912008-08-17 18:44:35 +000070 GCFunctionInfo *FI;
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000071 MachineModuleInfo *MMI;
72 const TargetInstrInfo *TII;
Andrew Trick9e761992012-02-08 21:22:43 +000073
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000074 void FindSafePoints(MachineFunction &MF);
75 void VisitCallPoint(MachineBasicBlock::iterator MI);
Andrew Trick9e761992012-02-08 21:22:43 +000076 MCSymbol *InsertLabel(MachineBasicBlock &MBB,
Chris Lattner1065f492010-03-14 07:27:07 +000077 MachineBasicBlock::iterator MI,
78 DebugLoc DL) const;
Andrew Trick9e761992012-02-08 21:22:43 +000079
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000080 void FindStackOffsets(MachineFunction &MF);
Andrew Trick9e761992012-02-08 21:22:43 +000081
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000082 public:
83 static char ID;
Andrew Trick9e761992012-02-08 21:22:43 +000084
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000085 GCMachineCodeAnalysis();
Craig Topper4584cd52014-03-07 09:26:03 +000086 void getAnalysisUsage(AnalysisUsage &AU) const override;
Andrew Trick9e761992012-02-08 21:22:43 +000087
Craig Topper4584cd52014-03-07 09:26:03 +000088 bool runOnMachineFunction(MachineFunction &MF) override;
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000089 };
Andrew Trick9e761992012-02-08 21:22:43 +000090
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000091}
92
93// -----------------------------------------------------------------------------
94
Gordon Henriksend930f912008-08-17 18:44:35 +000095GCStrategy::GCStrategy() :
Philip Reames4ac17a32015-01-07 19:07:50 +000096 UseStatepoints(false),
Gordon Henriksen37ca83d2007-09-29 02:13:43 +000097 NeededSafePoints(0),
98 CustomReadBarriers(false),
99 CustomWriteBarriers(false),
100 CustomRoots(false),
Nicolas Geoffray26c328d2011-11-11 18:32:52 +0000101 CustomSafePoints(false),
Gordon Henriksendbe06d32008-08-17 12:08:44 +0000102 InitRoots(true),
103 UsesMetadata(false)
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000104{}
105
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000106// -----------------------------------------------------------------------------
107
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000108INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering",
109 false, false)
110INITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
111INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false)
112
Gordon Henriksen7843c162007-12-11 00:30:17 +0000113FunctionPass *llvm::createGCLoweringPass() {
114 return new LowerIntrinsics();
115}
Andrew Trick9e761992012-02-08 21:22:43 +0000116
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000117char LowerIntrinsics::ID = 0;
118
Gordon Henriksen7843c162007-12-11 00:30:17 +0000119LowerIntrinsics::LowerIntrinsics()
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000120 : FunctionPass(ID) {
121 initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry());
122 }
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000123
124const char *LowerIntrinsics::getPassName() const {
125 return "Lower Garbage Collection Instructions";
126}
Andrew Trick9e761992012-02-08 21:22:43 +0000127
Gordon Henriksen7843c162007-12-11 00:30:17 +0000128void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
129 FunctionPass::getAnalysisUsage(AU);
Gordon Henriksend930f912008-08-17 18:44:35 +0000130 AU.addRequired<GCModuleInfo>();
Chandler Carruth73523022014-01-13 13:07:17 +0000131 AU.addPreserved<DominatorTreeWrapperPass>();
Gordon Henriksen7843c162007-12-11 00:30:17 +0000132}
133
134/// doInitialization - If this module uses the GC intrinsics, find them now.
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000135bool LowerIntrinsics::doInitialization(Module &M) {
Gordon Henriksen7843c162007-12-11 00:30:17 +0000136 // FIXME: This is rather antisocial in the context of a JIT since it performs
137 // work against the entire module. But this cannot be done at
138 // runFunction time (initializeCustomLowering likely needs to change
139 // the module).
Duncan Sands5a913d62009-01-28 13:14:17 +0000140 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000141 assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?");
Gordon Henriksen7843c162007-12-11 00:30:17 +0000142 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Gordon Henriksend930f912008-08-17 18:44:35 +0000143 if (!I->isDeclaration() && I->hasGC())
144 MI->getFunctionInfo(*I); // Instantiate the GC strategy.
Andrew Trick9e761992012-02-08 21:22:43 +0000145
Gordon Henriksen7843c162007-12-11 00:30:17 +0000146 bool MadeChange = false;
Gordon Henriksend930f912008-08-17 18:44:35 +0000147 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
Gordon Henriksen7843c162007-12-11 00:30:17 +0000148 if (NeedsCustomLoweringPass(**I))
149 if ((*I)->initializeCustomLowering(M))
150 MadeChange = true;
Andrew Trick9e761992012-02-08 21:22:43 +0000151
Gordon Henriksen7843c162007-12-11 00:30:17 +0000152 return MadeChange;
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000153}
154
Andrew Trick9e761992012-02-08 21:22:43 +0000155bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots,
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000156 unsigned Count) {
157 // Scroll past alloca instructions.
158 BasicBlock::iterator IP = F.getEntryBlock().begin();
159 while (isa<AllocaInst>(IP)) ++IP;
Andrew Trick9e761992012-02-08 21:22:43 +0000160
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000161 // Search for initializers in the initial BB.
162 SmallPtrSet<AllocaInst*,16> InitedRoots;
163 for (; !CouldBecomeSafePoint(IP); ++IP)
164 if (StoreInst *SI = dyn_cast<StoreInst>(IP))
Anton Korobeynikov82c02b22008-05-06 22:52:30 +0000165 if (AllocaInst *AI =
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000166 dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts()))
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000167 InitedRoots.insert(AI);
Andrew Trick9e761992012-02-08 21:22:43 +0000168
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000169 // Add root initializers.
Gordon Henriksen7843c162007-12-11 00:30:17 +0000170 bool MadeChange = false;
Andrew Trick9e761992012-02-08 21:22:43 +0000171
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000172 for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
Gordon Henriksen7843c162007-12-11 00:30:17 +0000173 if (!InitedRoots.count(*I)) {
Nicolas Geoffray19cd1d82010-04-15 19:53:35 +0000174 StoreInst* SI = new StoreInst(ConstantPointerNull::get(cast<PointerType>(
175 cast<PointerType>((*I)->getType())->getElementType())),
176 *I);
177 SI->insertAfter(*I);
Gordon Henriksen7843c162007-12-11 00:30:17 +0000178 MadeChange = true;
179 }
Andrew Trick9e761992012-02-08 21:22:43 +0000180
Gordon Henriksen7843c162007-12-11 00:30:17 +0000181 return MadeChange;
182}
183
Gordon Henriksend930f912008-08-17 18:44:35 +0000184bool LowerIntrinsics::NeedsDefaultLoweringPass(const GCStrategy &C) {
Gordon Henriksen7843c162007-12-11 00:30:17 +0000185 // Default lowering is necessary only if read or write barriers have a default
186 // action. The default for roots is no action.
187 return !C.customWriteBarrier()
188 || !C.customReadBarrier()
189 || C.initializeRoots();
190}
191
Gordon Henriksend930f912008-08-17 18:44:35 +0000192bool LowerIntrinsics::NeedsCustomLoweringPass(const GCStrategy &C) {
Gordon Henriksen7843c162007-12-11 00:30:17 +0000193 // Custom lowering is only necessary if enabled for some action.
194 return C.customWriteBarrier()
195 || C.customReadBarrier()
196 || C.customRoots();
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000197}
198
199/// CouldBecomeSafePoint - Predicate to conservatively determine whether the
200/// instruction could introduce a safe point.
201bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) {
202 // The natural definition of instructions which could introduce safe points
203 // are:
Andrew Trick9e761992012-02-08 21:22:43 +0000204 //
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000205 // - call, invoke (AfterCall, BeforeCall)
206 // - phis (Loops)
207 // - invoke, ret, unwind (Exit)
Andrew Trick9e761992012-02-08 21:22:43 +0000208 //
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000209 // However, instructions as seemingly inoccuous as arithmetic can become
210 // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead
211 // it is necessary to take a conservative approach.
Andrew Trick9e761992012-02-08 21:22:43 +0000212
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000213 if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) ||
214 isa<StoreInst>(I) || isa<LoadInst>(I))
215 return false;
Andrew Trick9e761992012-02-08 21:22:43 +0000216
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000217 // llvm.gcroot is safe because it doesn't do anything at runtime.
218 if (CallInst *CI = dyn_cast<CallInst>(I))
219 if (Function *F = CI->getCalledFunction())
220 if (unsigned IID = F->getIntrinsicID())
221 if (IID == Intrinsic::gcroot)
222 return false;
Andrew Trick9e761992012-02-08 21:22:43 +0000223
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000224 return true;
225}
226
227/// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores.
228/// Leave gcroot intrinsics; the code generator needs to see those.
229bool LowerIntrinsics::runOnFunction(Function &F) {
Gordon Henriksen7843c162007-12-11 00:30:17 +0000230 // Quick exit for functions that do not use GC.
Gordon Henriksend930f912008-08-17 18:44:35 +0000231 if (!F.hasGC())
232 return false;
Andrew Trick9e761992012-02-08 21:22:43 +0000233
Gordon Henriksend930f912008-08-17 18:44:35 +0000234 GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F);
235 GCStrategy &S = FI.getStrategy();
Andrew Trick9e761992012-02-08 21:22:43 +0000236
Gordon Henriksen7843c162007-12-11 00:30:17 +0000237 bool MadeChange = false;
Andrew Trick9e761992012-02-08 21:22:43 +0000238
Gordon Henriksend930f912008-08-17 18:44:35 +0000239 if (NeedsDefaultLoweringPass(S))
240 MadeChange |= PerformDefaultLowering(F, S);
Andrew Trick9e761992012-02-08 21:22:43 +0000241
Cameron Zwarich84986b22011-01-08 17:01:52 +0000242 bool UseCustomLoweringPass = NeedsCustomLoweringPass(S);
243 if (UseCustomLoweringPass)
Gordon Henriksend930f912008-08-17 18:44:35 +0000244 MadeChange |= S.performCustomLowering(F);
Cameron Zwarich84986b22011-01-08 17:01:52 +0000245
246 // Custom lowering may modify the CFG, so dominators must be recomputed.
247 if (UseCustomLoweringPass) {
Chandler Carruth73523022014-01-13 13:07:17 +0000248 if (DominatorTreeWrapperPass *DTWP =
249 getAnalysisIfAvailable<DominatorTreeWrapperPass>())
250 DTWP->getDomTree().recalculate(F);
Cameron Zwarich84986b22011-01-08 17:01:52 +0000251 }
252
Gordon Henriksen7843c162007-12-11 00:30:17 +0000253 return MadeChange;
254}
255
Gordon Henriksend930f912008-08-17 18:44:35 +0000256bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) {
257 bool LowerWr = !S.customWriteBarrier();
258 bool LowerRd = !S.customReadBarrier();
259 bool InitRoots = S.initializeRoots();
Andrew Trick9e761992012-02-08 21:22:43 +0000260
Gabor Greif3e44ea12010-07-22 10:37:47 +0000261 SmallVector<AllocaInst*, 32> Roots;
Andrew Trick9e761992012-02-08 21:22:43 +0000262
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000263 bool MadeChange = false;
264 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
265 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) {
Gordon Henriksen41689b52007-12-22 17:27:01 +0000266 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) {
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000267 Function *F = CI->getCalledFunction();
Gordon Henriksen41689b52007-12-22 17:27:01 +0000268 switch (F->getIntrinsicID()) {
269 case Intrinsic::gcwrite:
270 if (LowerWr) {
271 // Replace a write barrier with a simple store.
Gabor Greif3e44ea12010-07-22 10:37:47 +0000272 Value *St = new StoreInst(CI->getArgOperand(0),
273 CI->getArgOperand(2), CI);
Gordon Henriksen41689b52007-12-22 17:27:01 +0000274 CI->replaceAllUsesWith(St);
275 CI->eraseFromParent();
276 }
277 break;
278 case Intrinsic::gcread:
279 if (LowerRd) {
280 // Replace a read barrier with a simple load.
Gabor Greifb5874de2010-06-25 08:48:19 +0000281 Value *Ld = new LoadInst(CI->getArgOperand(1), "", CI);
Gordon Henriksen41689b52007-12-22 17:27:01 +0000282 Ld->takeName(CI);
283 CI->replaceAllUsesWith(Ld);
284 CI->eraseFromParent();
285 }
286 break;
287 case Intrinsic::gcroot:
288 if (InitRoots) {
289 // Initialize the GC root, but do not delete the intrinsic. The
290 // backend needs the intrinsic to flag the stack slot.
291 Roots.push_back(cast<AllocaInst>(
Gabor Greifb5874de2010-06-25 08:48:19 +0000292 CI->getArgOperand(0)->stripPointerCasts()));
Gordon Henriksen41689b52007-12-22 17:27:01 +0000293 }
294 break;
295 default:
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000296 continue;
297 }
Andrew Trick9e761992012-02-08 21:22:43 +0000298
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000299 MadeChange = true;
300 }
301 }
302 }
Andrew Trick9e761992012-02-08 21:22:43 +0000303
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000304 if (Roots.size())
Gordon Henriksen7843c162007-12-11 00:30:17 +0000305 MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size());
Andrew Trick9e761992012-02-08 21:22:43 +0000306
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000307 return MadeChange;
308}
309
310// -----------------------------------------------------------------------------
311
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000312char GCMachineCodeAnalysis::ID = 0;
313char &llvm::GCMachineCodeAnalysisID = GCMachineCodeAnalysis::ID;
Gordon Henriksen7843c162007-12-11 00:30:17 +0000314
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000315INITIALIZE_PASS(GCMachineCodeAnalysis, "gc-analysis",
316 "Analyze Machine Code For Garbage Collection", false, false)
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000317
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000318GCMachineCodeAnalysis::GCMachineCodeAnalysis()
Owen Andersona7aed182010-08-06 18:33:48 +0000319 : MachineFunctionPass(ID) {}
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000320
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000321void GCMachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000322 MachineFunctionPass::getAnalysisUsage(AU);
323 AU.setPreservesAll();
324 AU.addRequired<MachineModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000325 AU.addRequired<GCModuleInfo>();
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000326}
327
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000328MCSymbol *GCMachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
329 MachineBasicBlock::iterator MI,
330 DebugLoc DL) const {
Chris Lattneraed00fa2010-03-17 05:41:18 +0000331 MCSymbol *Label = MBB.getParent()->getContext().CreateTempSymbol();
Chris Lattner1065f492010-03-14 07:27:07 +0000332 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000333 return Label;
334}
335
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000336void GCMachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000337 // Find the return address (next instruction), too, so as to bracket the call
338 // instruction.
Andrew Trick9e761992012-02-08 21:22:43 +0000339 MachineBasicBlock::iterator RAI = CI;
340 ++RAI;
341
Nicolas Geoffraycbb42182010-09-24 17:27:50 +0000342 if (FI->getStrategy().needsSafePoint(GC::PreCall)) {
343 MCSymbol* Label = InsertLabel(*CI->getParent(), CI, CI->getDebugLoc());
344 FI->addSafePoint(GC::PreCall, Label, CI->getDebugLoc());
345 }
Andrew Trick9e761992012-02-08 21:22:43 +0000346
Nicolas Geoffraycbb42182010-09-24 17:27:50 +0000347 if (FI->getStrategy().needsSafePoint(GC::PostCall)) {
348 MCSymbol* Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc());
349 FI->addSafePoint(GC::PostCall, Label, CI->getDebugLoc());
350 }
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000351}
352
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000353void GCMachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000354 for (MachineFunction::iterator BBI = MF.begin(),
355 BBE = MF.end(); BBI != BBE; ++BBI)
356 for (MachineBasicBlock::iterator MI = BBI->begin(),
357 ME = BBI->end(); MI != ME; ++MI)
Evan Cheng7f8e5632011-12-07 07:15:52 +0000358 if (MI->isCall())
Dan Gohman14464bc2008-07-07 20:08:05 +0000359 VisitCallPoint(MI);
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000360}
361
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000362void GCMachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
Eric Christopherd9134482014-08-04 21:25:23 +0000363 const TargetFrameLowering *TFI = TM->getSubtargetImpl()->getFrameLowering();
Anton Korobeynikov46877782010-11-20 15:59:32 +0000364 assert(TFI && "TargetRegisterInfo not available!");
Andrew Trick9e761992012-02-08 21:22:43 +0000365
Nicolas Geoffray457b3562012-10-26 09:15:55 +0000366 for (GCFunctionInfo::roots_iterator RI = FI->roots_begin();
367 RI != FI->roots_end();) {
368 // If the root references a dead object, no need to keep it.
369 if (MF.getFrameInfo()->isDeadObjectIndex(RI->Num)) {
370 RI = FI->removeStackRoot(RI);
371 } else {
372 RI->StackOffset = TFI->getFrameIndexOffset(MF, RI->Num);
373 ++RI;
374 }
375 }
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000376}
377
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000378bool GCMachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
Gordon Henriksen7843c162007-12-11 00:30:17 +0000379 // Quick exit for functions that do not use GC.
Gordon Henriksend930f912008-08-17 18:44:35 +0000380 if (!MF.getFunction()->hasGC())
381 return false;
Andrew Trick9e761992012-02-08 21:22:43 +0000382
Gordon Henriksend930f912008-08-17 18:44:35 +0000383 FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction());
384 if (!FI->getStrategy().needsSafePoints())
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000385 return false;
Andrew Trick9e761992012-02-08 21:22:43 +0000386
Gordon Henriksen7843c162007-12-11 00:30:17 +0000387 TM = &MF.getTarget();
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000388 MMI = &getAnalysis<MachineModuleInfo>();
Eric Christopherd9134482014-08-04 21:25:23 +0000389 TII = TM->getSubtargetImpl()->getInstrInfo();
Andrew Trick9e761992012-02-08 21:22:43 +0000390
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000391 // Find the size of the stack frame.
Gordon Henriksen72bd9df2008-08-19 17:06:35 +0000392 FI->setFrameSize(MF.getFrameInfo()->getStackSize());
Nicolas Geoffray26c328d2011-11-11 18:32:52 +0000393
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000394 // Find all safe points.
Nicolas Geoffray26c328d2011-11-11 18:32:52 +0000395 if (FI->getStrategy().customSafePoints()) {
396 FI->getStrategy().findCustomSafePoints(*FI, MF);
397 } else {
398 FindSafePoints(MF);
399 }
Andrew Trick9e761992012-02-08 21:22:43 +0000400
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000401 // Find the stack offsets for all roots.
402 FindStackOffsets(MF);
Andrew Trick9e761992012-02-08 21:22:43 +0000403
Gordon Henriksen37ca83d2007-09-29 02:13:43 +0000404 return false;
405}