LLVM API Documentation
00001 //===-- GCStrategy.cpp - Garbage collection infrastructure -----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements target- and collector-independent garbage collection 00011 // infrastructure. 00012 // 00013 // MachineCodeAnalysis identifies the GC safe points in the machine code. Roots 00014 // are identified in SelectionDAGISel. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #include "llvm/CodeGen/GCStrategy.h" 00019 #include "llvm/CodeGen/Passes.h" 00020 #include "llvm/IntrinsicInst.h" 00021 #include "llvm/Module.h" 00022 #include "llvm/CodeGen/MachineFrameInfo.h" 00023 #include "llvm/CodeGen/MachineFunctionPass.h" 00024 #include "llvm/CodeGen/MachineInstrBuilder.h" 00025 #include "llvm/CodeGen/MachineModuleInfo.h" 00026 #include "llvm/Target/TargetFrameInfo.h" 00027 #include "llvm/Target/TargetInstrInfo.h" 00028 #include "llvm/Target/TargetMachine.h" 00029 #include "llvm/Target/TargetRegisterInfo.h" 00030 #include "llvm/Support/Compiler.h" 00031 #include "llvm/Support/ErrorHandling.h" 00032 00033 using namespace llvm; 00034 00035 namespace { 00036 00037 /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or 00038 /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as 00039 /// directed by the GCStrategy. It also performs automatic root initialization 00040 /// and custom intrinsic lowering. 00041 class VISIBILITY_HIDDEN LowerIntrinsics : public FunctionPass { 00042 static bool NeedsDefaultLoweringPass(const GCStrategy &C); 00043 static bool NeedsCustomLoweringPass(const GCStrategy &C); 00044 static bool CouldBecomeSafePoint(Instruction *I); 00045 bool PerformDefaultLowering(Function &F, GCStrategy &Coll); 00046 static bool InsertRootInitializers(Function &F, 00047 AllocaInst **Roots, unsigned Count); 00048 00049 public: 00050 static char ID; 00051 00052 LowerIntrinsics(); 00053 const char *getPassName() const; 00054 void getAnalysisUsage(AnalysisUsage &AU) const; 00055 00056 bool doInitialization(Module &M); 00057 bool runOnFunction(Function &F); 00058 }; 00059 00060 00061 /// MachineCodeAnalysis - This is a target-independent pass over the machine 00062 /// function representation to identify safe points for the garbage collector 00063 /// in the machine code. It inserts labels at safe points and populates a 00064 /// GCMetadata record for each function. 00065 class VISIBILITY_HIDDEN MachineCodeAnalysis : public MachineFunctionPass { 00066 const TargetMachine *TM; 00067 GCFunctionInfo *FI; 00068 MachineModuleInfo *MMI; 00069 const TargetInstrInfo *TII; 00070 00071 void FindSafePoints(MachineFunction &MF); 00072 void VisitCallPoint(MachineBasicBlock::iterator MI); 00073 unsigned InsertLabel(MachineBasicBlock &MBB, 00074 MachineBasicBlock::iterator MI) const; 00075 00076 void FindStackOffsets(MachineFunction &MF); 00077 00078 public: 00079 static char ID; 00080 00081 MachineCodeAnalysis(); 00082 const char *getPassName() const; 00083 void getAnalysisUsage(AnalysisUsage &AU) const; 00084 00085 bool runOnMachineFunction(MachineFunction &MF); 00086 }; 00087 00088 } 00089 00090 // ----------------------------------------------------------------------------- 00091 00092 GCStrategy::GCStrategy() : 00093 NeededSafePoints(0), 00094 CustomReadBarriers(false), 00095 CustomWriteBarriers(false), 00096 CustomRoots(false), 00097 InitRoots(true), 00098 UsesMetadata(false) 00099 {} 00100 00101 GCStrategy::~GCStrategy() { 00102 for (iterator I = begin(), E = end(); I != E; ++I) 00103 delete *I; 00104 00105 Functions.clear(); 00106 } 00107 00108 bool GCStrategy::initializeCustomLowering(Module &M) { return false; } 00109 00110 bool GCStrategy::performCustomLowering(Function &F) { 00111 cerr << "gc " << getName() << " must override performCustomLowering.\n"; 00112 llvm_unreachable(0); 00113 return 0; 00114 } 00115 00116 GCFunctionInfo *GCStrategy::insertFunctionInfo(const Function &F) { 00117 GCFunctionInfo *FI = new GCFunctionInfo(F, *this); 00118 Functions.push_back(FI); 00119 return FI; 00120 } 00121 00122 // ----------------------------------------------------------------------------- 00123 00124 FunctionPass *llvm::createGCLoweringPass() { 00125 return new LowerIntrinsics(); 00126 } 00127 00128 char LowerIntrinsics::ID = 0; 00129 00130 LowerIntrinsics::LowerIntrinsics() 00131 : FunctionPass(&ID) {} 00132 00133 const char *LowerIntrinsics::getPassName() const { 00134 return "Lower Garbage Collection Instructions"; 00135 } 00136 00137 void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const { 00138 FunctionPass::getAnalysisUsage(AU); 00139 AU.addRequired<GCModuleInfo>(); 00140 } 00141 00142 /// doInitialization - If this module uses the GC intrinsics, find them now. 00143 bool LowerIntrinsics::doInitialization(Module &M) { 00144 // FIXME: This is rather antisocial in the context of a JIT since it performs 00145 // work against the entire module. But this cannot be done at 00146 // runFunction time (initializeCustomLowering likely needs to change 00147 // the module). 00148 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); 00149 assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?"); 00150 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 00151 if (!I->isDeclaration() && I->hasGC()) 00152 MI->getFunctionInfo(*I); // Instantiate the GC strategy. 00153 00154 bool MadeChange = false; 00155 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I) 00156 if (NeedsCustomLoweringPass(**I)) 00157 if ((*I)->initializeCustomLowering(M)) 00158 MadeChange = true; 00159 00160 return MadeChange; 00161 } 00162 00163 bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots, 00164 unsigned Count) { 00165 // Scroll past alloca instructions. 00166 BasicBlock::iterator IP = F.getEntryBlock().begin(); 00167 while (isa<AllocaInst>(IP)) ++IP; 00168 00169 // Search for initializers in the initial BB. 00170 SmallPtrSet<AllocaInst*,16> InitedRoots; 00171 for (; !CouldBecomeSafePoint(IP); ++IP) 00172 if (StoreInst *SI = dyn_cast<StoreInst>(IP)) 00173 if (AllocaInst *AI = 00174 dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts())) 00175 InitedRoots.insert(AI); 00176 00177 // Add root initializers. 00178 bool MadeChange = false; 00179 00180 for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I) 00181 if (!InitedRoots.count(*I)) { 00182 new StoreInst(ConstantPointerNull::get(cast<PointerType>( 00183 cast<PointerType>((*I)->getType())->getElementType())), 00184 *I, IP); 00185 MadeChange = true; 00186 } 00187 00188 return MadeChange; 00189 } 00190 00191 bool LowerIntrinsics::NeedsDefaultLoweringPass(const GCStrategy &C) { 00192 // Default lowering is necessary only if read or write barriers have a default 00193 // action. The default for roots is no action. 00194 return !C.customWriteBarrier() 00195 || !C.customReadBarrier() 00196 || C.initializeRoots(); 00197 } 00198 00199 bool LowerIntrinsics::NeedsCustomLoweringPass(const GCStrategy &C) { 00200 // Custom lowering is only necessary if enabled for some action. 00201 return C.customWriteBarrier() 00202 || C.customReadBarrier() 00203 || C.customRoots(); 00204 } 00205 00206 /// CouldBecomeSafePoint - Predicate to conservatively determine whether the 00207 /// instruction could introduce a safe point. 00208 bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) { 00209 // The natural definition of instructions which could introduce safe points 00210 // are: 00211 // 00212 // - call, invoke (AfterCall, BeforeCall) 00213 // - phis (Loops) 00214 // - invoke, ret, unwind (Exit) 00215 // 00216 // However, instructions as seemingly inoccuous as arithmetic can become 00217 // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead 00218 // it is necessary to take a conservative approach. 00219 00220 if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) || 00221 isa<StoreInst>(I) || isa<LoadInst>(I)) 00222 return false; 00223 00224 // llvm.gcroot is safe because it doesn't do anything at runtime. 00225 if (CallInst *CI = dyn_cast<CallInst>(I)) 00226 if (Function *F = CI->getCalledFunction()) 00227 if (unsigned IID = F->getIntrinsicID()) 00228 if (IID == Intrinsic::gcroot) 00229 return false; 00230 00231 return true; 00232 } 00233 00234 /// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores. 00235 /// Leave gcroot intrinsics; the code generator needs to see those. 00236 bool LowerIntrinsics::runOnFunction(Function &F) { 00237 // Quick exit for functions that do not use GC. 00238 if (!F.hasGC()) 00239 return false; 00240 00241 GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F); 00242 GCStrategy &S = FI.getStrategy(); 00243 00244 bool MadeChange = false; 00245 00246 if (NeedsDefaultLoweringPass(S)) 00247 MadeChange |= PerformDefaultLowering(F, S); 00248 00249 if (NeedsCustomLoweringPass(S)) 00250 MadeChange |= S.performCustomLowering(F); 00251 00252 return MadeChange; 00253 } 00254 00255 bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) { 00256 bool LowerWr = !S.customWriteBarrier(); 00257 bool LowerRd = !S.customReadBarrier(); 00258 bool InitRoots = S.initializeRoots(); 00259 00260 SmallVector<AllocaInst*,32> Roots; 00261 00262 bool MadeChange = false; 00263 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { 00264 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) { 00265 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) { 00266 Function *F = CI->getCalledFunction(); 00267 switch (F->getIntrinsicID()) { 00268 case Intrinsic::gcwrite: 00269 if (LowerWr) { 00270 // Replace a write barrier with a simple store. 00271 Value *St = new StoreInst(CI->getOperand(1), CI->getOperand(3), CI); 00272 CI->replaceAllUsesWith(St); 00273 CI->eraseFromParent(); 00274 } 00275 break; 00276 case Intrinsic::gcread: 00277 if (LowerRd) { 00278 // Replace a read barrier with a simple load. 00279 Value *Ld = new LoadInst(CI->getOperand(2), "", CI); 00280 Ld->takeName(CI); 00281 CI->replaceAllUsesWith(Ld); 00282 CI->eraseFromParent(); 00283 } 00284 break; 00285 case Intrinsic::gcroot: 00286 if (InitRoots) { 00287 // Initialize the GC root, but do not delete the intrinsic. The 00288 // backend needs the intrinsic to flag the stack slot. 00289 Roots.push_back(cast<AllocaInst>( 00290 CI->getOperand(1)->stripPointerCasts())); 00291 } 00292 break; 00293 default: 00294 continue; 00295 } 00296 00297 MadeChange = true; 00298 } 00299 } 00300 } 00301 00302 if (Roots.size()) 00303 MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size()); 00304 00305 return MadeChange; 00306 } 00307 00308 // ----------------------------------------------------------------------------- 00309 00310 FunctionPass *llvm::createGCMachineCodeAnalysisPass() { 00311 return new MachineCodeAnalysis(); 00312 } 00313 00314 char MachineCodeAnalysis::ID = 0; 00315 00316 MachineCodeAnalysis::MachineCodeAnalysis() 00317 : MachineFunctionPass(&ID) {} 00318 00319 const char *MachineCodeAnalysis::getPassName() const { 00320 return "Analyze Machine Code For Garbage Collection"; 00321 } 00322 00323 void MachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 00324 MachineFunctionPass::getAnalysisUsage(AU); 00325 AU.setPreservesAll(); 00326 AU.addRequired<MachineModuleInfo>(); 00327 AU.addRequired<GCModuleInfo>(); 00328 } 00329 00330 unsigned MachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB, 00331 MachineBasicBlock::iterator MI) const { 00332 unsigned Label = MMI->NextLabelID(); 00333 // N.B. we assume that MI is *not* equal to the "end()" iterator. 00334 BuildMI(MBB, MI, MI->getDebugLoc(), 00335 TII->get(TargetInstrInfo::GC_LABEL)).addImm(Label); 00336 return Label; 00337 } 00338 00339 void MachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) { 00340 // Find the return address (next instruction), too, so as to bracket the call 00341 // instruction. 00342 MachineBasicBlock::iterator RAI = CI; 00343 ++RAI; 00344 00345 if (FI->getStrategy().needsSafePoint(GC::PreCall)) 00346 FI->addSafePoint(GC::PreCall, InsertLabel(*CI->getParent(), CI)); 00347 00348 if (FI->getStrategy().needsSafePoint(GC::PostCall)) 00349 FI->addSafePoint(GC::PostCall, InsertLabel(*CI->getParent(), RAI)); 00350 } 00351 00352 void MachineCodeAnalysis::FindSafePoints(MachineFunction &MF) { 00353 for (MachineFunction::iterator BBI = MF.begin(), 00354 BBE = MF.end(); BBI != BBE; ++BBI) 00355 for (MachineBasicBlock::iterator MI = BBI->begin(), 00356 ME = BBI->end(); MI != ME; ++MI) 00357 if (MI->getDesc().isCall()) 00358 VisitCallPoint(MI); 00359 } 00360 00361 void MachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) { 00362 const TargetRegisterInfo *TRI = TM->getRegisterInfo(); 00363 assert(TRI && "TargetRegisterInfo not available!"); 00364 00365 for (GCFunctionInfo::roots_iterator RI = FI->roots_begin(), 00366 RE = FI->roots_end(); RI != RE; ++RI) 00367 RI->StackOffset = TRI->getFrameIndexOffset(MF, RI->Num); 00368 } 00369 00370 bool MachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) { 00371 // Quick exit for functions that do not use GC. 00372 if (!MF.getFunction()->hasGC()) 00373 return false; 00374 00375 FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction()); 00376 if (!FI->getStrategy().needsSafePoints()) 00377 return false; 00378 00379 TM = &MF.getTarget(); 00380 MMI = &getAnalysis<MachineModuleInfo>(); 00381 TII = TM->getInstrInfo(); 00382 00383 // Find the size of the stack frame. 00384 FI->setFrameSize(MF.getFrameInfo()->getStackSize()); 00385 00386 // Find all safe points. 00387 FindSafePoints(MF); 00388 00389 // Find the stack offsets for all roots. 00390 FindStackOffsets(MF); 00391 00392 return false; 00393 }