1 //===-- GCRootLowering.cpp - Garbage collection infrastructure ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the lowering for the gc.root mechanism.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/GCStrategy.h"
15 #include "llvm/CodeGen/MachineFrameInfo.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "llvm/CodeGen/MachineModuleInfo.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/IR/Dominators.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetFrameLowering.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetRegisterInfo.h"
30 #include "llvm/Target/TargetSubtargetInfo.h"
36 /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
37 /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
38 /// directed by the GCStrategy. It also performs automatic root initialization
39 /// and custom intrinsic lowering.
40 class LowerIntrinsics : public FunctionPass {
41 static bool NeedsDefaultLoweringPass(const GCStrategy &C);
42 static bool NeedsCustomLoweringPass(const GCStrategy &C);
43 static bool CouldBecomeSafePoint(Instruction *I);
44 bool PerformDefaultLowering(Function &F, GCStrategy &Coll);
45 static bool InsertRootInitializers(Function &F,
46 AllocaInst **Roots, unsigned Count);
52 const char *getPassName() const override;
53 void getAnalysisUsage(AnalysisUsage &AU) const override;
55 bool doInitialization(Module &M) override;
56 bool runOnFunction(Function &F) override;
60 /// GCMachineCodeAnalysis - This is a target-independent pass over the machine
61 /// function representation to identify safe points for the garbage collector
62 /// in the machine code. It inserts labels at safe points and populates a
63 /// GCMetadata record for each function.
64 class GCMachineCodeAnalysis : public MachineFunctionPass {
65 const TargetMachine *TM;
67 MachineModuleInfo *MMI;
68 const TargetInstrInfo *TII;
70 void FindSafePoints(MachineFunction &MF);
71 void VisitCallPoint(MachineBasicBlock::iterator MI);
72 MCSymbol *InsertLabel(MachineBasicBlock &MBB,
73 MachineBasicBlock::iterator MI,
76 void FindStackOffsets(MachineFunction &MF);
81 GCMachineCodeAnalysis();
82 void getAnalysisUsage(AnalysisUsage &AU) const override;
84 bool runOnMachineFunction(MachineFunction &MF) override;
89 // -----------------------------------------------------------------------------
91 INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering",
93 INITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
94 INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false)
96 FunctionPass *llvm::createGCLoweringPass() {
97 return new LowerIntrinsics();
100 char LowerIntrinsics::ID = 0;
102 LowerIntrinsics::LowerIntrinsics()
104 initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry());
107 const char *LowerIntrinsics::getPassName() const {
108 return "Lower Garbage Collection Instructions";
111 void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
112 FunctionPass::getAnalysisUsage(AU);
113 AU.addRequired<GCModuleInfo>();
114 AU.addPreserved<DominatorTreeWrapperPass>();
117 /// doInitialization - If this module uses the GC intrinsics, find them now.
118 bool LowerIntrinsics::doInitialization(Module &M) {
119 // FIXME: This is rather antisocial in the context of a JIT since it performs
120 // work against the entire module. But this cannot be done at
121 // runFunction time (initializeCustomLowering likely needs to change
123 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
124 assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?");
125 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
126 if (!I->isDeclaration() && I->hasGC())
127 MI->getFunctionInfo(*I); // Instantiate the GC strategy.
129 bool MadeChange = false;
130 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
131 if (NeedsCustomLoweringPass(**I))
132 if ((*I)->initializeCustomLowering(M))
138 bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots,
140 // Scroll past alloca instructions.
141 BasicBlock::iterator IP = F.getEntryBlock().begin();
142 while (isa<AllocaInst>(IP)) ++IP;
144 // Search for initializers in the initial BB.
145 SmallPtrSet<AllocaInst*,16> InitedRoots;
146 for (; !CouldBecomeSafePoint(IP); ++IP)
147 if (StoreInst *SI = dyn_cast<StoreInst>(IP))
149 dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts()))
150 InitedRoots.insert(AI);
152 // Add root initializers.
153 bool MadeChange = false;
155 for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
156 if (!InitedRoots.count(*I)) {
157 StoreInst* SI = new StoreInst(ConstantPointerNull::get(cast<PointerType>(
158 cast<PointerType>((*I)->getType())->getElementType())),
167 bool LowerIntrinsics::NeedsDefaultLoweringPass(const GCStrategy &C) {
168 // Default lowering is necessary only if read or write barriers have a default
169 // action. The default for roots is no action.
170 return !C.customWriteBarrier()
171 || !C.customReadBarrier()
172 || C.initializeRoots();
175 bool LowerIntrinsics::NeedsCustomLoweringPass(const GCStrategy &C) {
176 // Custom lowering is only necessary if enabled for some action.
177 return C.customWriteBarrier()
178 || C.customReadBarrier()
182 /// CouldBecomeSafePoint - Predicate to conservatively determine whether the
183 /// instruction could introduce a safe point.
184 bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) {
185 // The natural definition of instructions which could introduce safe points
188 // - call, invoke (AfterCall, BeforeCall)
190 // - invoke, ret, unwind (Exit)
192 // However, instructions as seemingly inoccuous as arithmetic can become
193 // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead
194 // it is necessary to take a conservative approach.
196 if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) ||
197 isa<StoreInst>(I) || isa<LoadInst>(I))
200 // llvm.gcroot is safe because it doesn't do anything at runtime.
201 if (CallInst *CI = dyn_cast<CallInst>(I))
202 if (Function *F = CI->getCalledFunction())
203 if (unsigned IID = F->getIntrinsicID())
204 if (IID == Intrinsic::gcroot)
210 /// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores.
211 /// Leave gcroot intrinsics; the code generator needs to see those.
212 bool LowerIntrinsics::runOnFunction(Function &F) {
213 // Quick exit for functions that do not use GC.
217 GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F);
218 GCStrategy &S = FI.getStrategy();
220 bool MadeChange = false;
222 if (NeedsDefaultLoweringPass(S))
223 MadeChange |= PerformDefaultLowering(F, S);
225 bool UseCustomLoweringPass = NeedsCustomLoweringPass(S);
226 if (UseCustomLoweringPass)
227 MadeChange |= S.performCustomLowering(F);
229 // Custom lowering may modify the CFG, so dominators must be recomputed.
230 if (UseCustomLoweringPass) {
231 if (DominatorTreeWrapperPass *DTWP =
232 getAnalysisIfAvailable<DominatorTreeWrapperPass>())
233 DTWP->getDomTree().recalculate(F);
239 bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) {
240 bool LowerWr = !S.customWriteBarrier();
241 bool LowerRd = !S.customReadBarrier();
242 bool InitRoots = S.initializeRoots();
244 SmallVector<AllocaInst*, 32> Roots;
246 bool MadeChange = false;
247 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
248 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) {
249 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) {
250 Function *F = CI->getCalledFunction();
251 switch (F->getIntrinsicID()) {
252 case Intrinsic::gcwrite:
254 // Replace a write barrier with a simple store.
255 Value *St = new StoreInst(CI->getArgOperand(0),
256 CI->getArgOperand(2), CI);
257 CI->replaceAllUsesWith(St);
258 CI->eraseFromParent();
261 case Intrinsic::gcread:
263 // Replace a read barrier with a simple load.
264 Value *Ld = new LoadInst(CI->getArgOperand(1), "", CI);
266 CI->replaceAllUsesWith(Ld);
267 CI->eraseFromParent();
270 case Intrinsic::gcroot:
272 // Initialize the GC root, but do not delete the intrinsic. The
273 // backend needs the intrinsic to flag the stack slot.
274 Roots.push_back(cast<AllocaInst>(
275 CI->getArgOperand(0)->stripPointerCasts()));
288 MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size());
293 // -----------------------------------------------------------------------------
295 char GCMachineCodeAnalysis::ID = 0;
296 char &llvm::GCMachineCodeAnalysisID = GCMachineCodeAnalysis::ID;
298 INITIALIZE_PASS(GCMachineCodeAnalysis, "gc-analysis",
299 "Analyze Machine Code For Garbage Collection", false, false)
301 GCMachineCodeAnalysis::GCMachineCodeAnalysis()
302 : MachineFunctionPass(ID) {}
304 void GCMachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
305 MachineFunctionPass::getAnalysisUsage(AU);
306 AU.setPreservesAll();
307 AU.addRequired<MachineModuleInfo>();
308 AU.addRequired<GCModuleInfo>();
311 MCSymbol *GCMachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
312 MachineBasicBlock::iterator MI,
314 MCSymbol *Label = MBB.getParent()->getContext().CreateTempSymbol();
315 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
319 void GCMachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
320 // Find the return address (next instruction), too, so as to bracket the call
322 MachineBasicBlock::iterator RAI = CI;
325 if (FI->getStrategy().needsSafePoint(GC::PreCall)) {
326 MCSymbol* Label = InsertLabel(*CI->getParent(), CI, CI->getDebugLoc());
327 FI->addSafePoint(GC::PreCall, Label, CI->getDebugLoc());
330 if (FI->getStrategy().needsSafePoint(GC::PostCall)) {
331 MCSymbol* Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc());
332 FI->addSafePoint(GC::PostCall, Label, CI->getDebugLoc());
336 void GCMachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
337 for (MachineFunction::iterator BBI = MF.begin(),
338 BBE = MF.end(); BBI != BBE; ++BBI)
339 for (MachineBasicBlock::iterator MI = BBI->begin(),
340 ME = BBI->end(); MI != ME; ++MI)
345 void GCMachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
346 const TargetFrameLowering *TFI = TM->getSubtargetImpl()->getFrameLowering();
347 assert(TFI && "TargetRegisterInfo not available!");
349 for (GCFunctionInfo::roots_iterator RI = FI->roots_begin();
350 RI != FI->roots_end();) {
351 // If the root references a dead object, no need to keep it.
352 if (MF.getFrameInfo()->isDeadObjectIndex(RI->Num)) {
353 RI = FI->removeStackRoot(RI);
355 RI->StackOffset = TFI->getFrameIndexOffset(MF, RI->Num);
361 bool GCMachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
362 // Quick exit for functions that do not use GC.
363 if (!MF.getFunction()->hasGC())
366 FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction());
367 if (!FI->getStrategy().needsSafePoints())
370 TM = &MF.getTarget();
371 MMI = &getAnalysis<MachineModuleInfo>();
372 TII = TM->getSubtargetImpl()->getInstrInfo();
374 // Find the size of the stack frame.
375 FI->setFrameSize(MF.getFrameInfo()->getStackSize());
377 // Find all safe points.
378 if (FI->getStrategy().customSafePoints()) {
379 FI->getStrategy().findCustomSafePoints(*FI, MF);
384 // Find the stack offsets for all roots.
385 FindStackOffsets(MF);