Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 1 | //===-- Collector.cpp - Garbage collection infrastructure -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements target- and collector-independent garbage collection |
| 11 | // infrastructure. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/CodeGen/Collector.h" |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Passes.h" |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 17 | #include "llvm/IntrinsicInst.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/PassManager.h" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetFrameInfo.h" |
| 26 | #include "llvm/Target/TargetInstrInfo.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Support/Compiler.h" |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 34 | /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or |
| 35 | /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as |
| 36 | /// directed by the Collector. It also performs automatic root initialization |
| 37 | /// and custom intrinsic lowering. |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 38 | class VISIBILITY_HIDDEN LowerIntrinsics : public FunctionPass { |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 39 | static bool NeedsDefaultLoweringPass(const Collector &C); |
| 40 | static bool NeedsCustomLoweringPass(const Collector &C); |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 41 | static bool CouldBecomeSafePoint(Instruction *I); |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 42 | bool PerformDefaultLowering(Function &F, Collector &Coll); |
| 43 | static bool InsertRootInitializers(Function &F, |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 44 | AllocaInst **Roots, unsigned Count); |
| 45 | |
| 46 | public: |
| 47 | static char ID; |
| 48 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 49 | LowerIntrinsics(); |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 50 | const char *getPassName() const; |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 51 | void getAnalysisUsage(AnalysisUsage &AU) const; |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 52 | |
| 53 | bool doInitialization(Module &M); |
| 54 | bool runOnFunction(Function &F); |
| 55 | }; |
| 56 | |
| 57 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 58 | /// MachineCodeAnalysis - This is a target-independent pass over the machine |
| 59 | /// function representation to identify safe points for the garbage collector |
| 60 | /// in the machine code. It inserts labels at safe points and populates a |
| 61 | /// CollectorMetadata record for each function. |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 62 | class VISIBILITY_HIDDEN MachineCodeAnalysis : public MachineFunctionPass { |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 63 | const TargetMachine *TM; |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 64 | CollectorMetadata *MD; |
| 65 | MachineModuleInfo *MMI; |
| 66 | const TargetInstrInfo *TII; |
| 67 | MachineFrameInfo *MFI; |
| 68 | |
| 69 | void FindSafePoints(MachineFunction &MF); |
| 70 | void VisitCallPoint(MachineBasicBlock::iterator MI); |
| 71 | unsigned InsertLabel(MachineBasicBlock &MBB, |
| 72 | MachineBasicBlock::iterator MI) const; |
| 73 | |
| 74 | void FindStackOffsets(MachineFunction &MF); |
| 75 | |
| 76 | public: |
| 77 | static char ID; |
| 78 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 79 | MachineCodeAnalysis(); |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 80 | const char *getPassName() const; |
| 81 | void getAnalysisUsage(AnalysisUsage &AU) const; |
| 82 | |
| 83 | bool runOnMachineFunction(MachineFunction &MF); |
| 84 | }; |
| 85 | |
| 86 | } |
| 87 | |
| 88 | // ----------------------------------------------------------------------------- |
| 89 | |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 90 | Collector::Collector() : |
| 91 | NeededSafePoints(0), |
| 92 | CustomReadBarriers(false), |
| 93 | CustomWriteBarriers(false), |
| 94 | CustomRoots(false), |
| 95 | InitRoots(true) |
| 96 | {} |
| 97 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 98 | Collector::~Collector() { |
| 99 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 100 | delete *I; |
| 101 | |
| 102 | Functions.clear(); |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 103 | } |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 104 | |
| 105 | bool Collector::initializeCustomLowering(Module &M) { return false; } |
| 106 | |
| 107 | bool Collector::performCustomLowering(Function &F) { |
| 108 | cerr << "gc " << getName() << " must override performCustomLowering.\n"; |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 109 | abort(); |
| 110 | return 0; |
| 111 | } |
| 112 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 113 | void Collector::beginAssembly(std::ostream &OS, AsmPrinter &AP, |
| 114 | const TargetAsmInfo &TAI) { |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 115 | // Default is no action. |
| 116 | } |
| 117 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 118 | void Collector::finishAssembly(std::ostream &OS, AsmPrinter &AP, |
| 119 | const TargetAsmInfo &TAI) { |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 120 | // Default is no action. |
| 121 | } |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 122 | |
| 123 | CollectorMetadata *Collector::insertFunctionMetadata(const Function &F) { |
| 124 | CollectorMetadata *CM = new CollectorMetadata(F, *this); |
| 125 | Functions.push_back(CM); |
| 126 | return CM; |
| 127 | } |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 128 | |
| 129 | // ----------------------------------------------------------------------------- |
| 130 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 131 | FunctionPass *llvm::createGCLoweringPass() { |
| 132 | return new LowerIntrinsics(); |
| 133 | } |
| 134 | |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 135 | char LowerIntrinsics::ID = 0; |
| 136 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 137 | LowerIntrinsics::LowerIntrinsics() |
Gordon Henriksen | a6c9925 | 2007-12-22 17:27:01 +0000 | [diff] [blame] | 138 | : FunctionPass((intptr_t)&ID) {} |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 139 | |
| 140 | const char *LowerIntrinsics::getPassName() const { |
| 141 | return "Lower Garbage Collection Instructions"; |
| 142 | } |
| 143 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 144 | void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const { |
| 145 | FunctionPass::getAnalysisUsage(AU); |
| 146 | AU.addRequired<CollectorModuleMetadata>(); |
| 147 | } |
| 148 | |
| 149 | /// doInitialization - If this module uses the GC intrinsics, find them now. |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 150 | bool LowerIntrinsics::doInitialization(Module &M) { |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 151 | // FIXME: This is rather antisocial in the context of a JIT since it performs |
| 152 | // work against the entire module. But this cannot be done at |
| 153 | // runFunction time (initializeCustomLowering likely needs to change |
| 154 | // the module). |
| 155 | CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>(); |
| 156 | assert(CMM && "LowerIntrinsics didn't require CollectorModuleMetadata!?"); |
| 157 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 158 | if (I->hasCollector()) |
| 159 | CMM->get(*I); // Instantiate the Collector. |
| 160 | |
| 161 | bool MadeChange = false; |
| 162 | for (CollectorModuleMetadata::iterator I = CMM->begin(), |
| 163 | E = CMM->end(); I != E; ++I) |
| 164 | if (NeedsCustomLoweringPass(**I)) |
| 165 | if ((*I)->initializeCustomLowering(M)) |
| 166 | MadeChange = true; |
| 167 | |
| 168 | return MadeChange; |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 171 | bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots, |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 172 | unsigned Count) { |
| 173 | // Scroll past alloca instructions. |
| 174 | BasicBlock::iterator IP = F.getEntryBlock().begin(); |
| 175 | while (isa<AllocaInst>(IP)) ++IP; |
| 176 | |
| 177 | // Search for initializers in the initial BB. |
| 178 | SmallPtrSet<AllocaInst*,16> InitedRoots; |
| 179 | for (; !CouldBecomeSafePoint(IP); ++IP) |
| 180 | if (StoreInst *SI = dyn_cast<StoreInst>(IP)) |
Anton Korobeynikov | b04addd | 2008-05-06 22:52:30 +0000 | [diff] [blame] | 181 | if (AllocaInst *AI = |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 182 | dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts())) |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 183 | InitedRoots.insert(AI); |
| 184 | |
| 185 | // Add root initializers. |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 186 | bool MadeChange = false; |
| 187 | |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 188 | for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I) |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 189 | if (!InitedRoots.count(*I)) { |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 190 | new StoreInst(ConstantPointerNull::get(cast<PointerType>( |
| 191 | cast<PointerType>((*I)->getType())->getElementType())), |
| 192 | *I, IP); |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 193 | MadeChange = true; |
| 194 | } |
| 195 | |
| 196 | return MadeChange; |
| 197 | } |
| 198 | |
| 199 | bool LowerIntrinsics::NeedsDefaultLoweringPass(const Collector &C) { |
| 200 | // Default lowering is necessary only if read or write barriers have a default |
| 201 | // action. The default for roots is no action. |
| 202 | return !C.customWriteBarrier() |
| 203 | || !C.customReadBarrier() |
| 204 | || C.initializeRoots(); |
| 205 | } |
| 206 | |
| 207 | bool LowerIntrinsics::NeedsCustomLoweringPass(const Collector &C) { |
| 208 | // Custom lowering is only necessary if enabled for some action. |
| 209 | return C.customWriteBarrier() |
| 210 | || C.customReadBarrier() |
| 211 | || C.customRoots(); |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /// CouldBecomeSafePoint - Predicate to conservatively determine whether the |
| 215 | /// instruction could introduce a safe point. |
| 216 | bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) { |
| 217 | // The natural definition of instructions which could introduce safe points |
| 218 | // are: |
| 219 | // |
| 220 | // - call, invoke (AfterCall, BeforeCall) |
| 221 | // - phis (Loops) |
| 222 | // - invoke, ret, unwind (Exit) |
| 223 | // |
| 224 | // However, instructions as seemingly inoccuous as arithmetic can become |
| 225 | // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead |
| 226 | // it is necessary to take a conservative approach. |
| 227 | |
| 228 | if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) || |
| 229 | isa<StoreInst>(I) || isa<LoadInst>(I)) |
| 230 | return false; |
| 231 | |
| 232 | // llvm.gcroot is safe because it doesn't do anything at runtime. |
| 233 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 234 | if (Function *F = CI->getCalledFunction()) |
| 235 | if (unsigned IID = F->getIntrinsicID()) |
| 236 | if (IID == Intrinsic::gcroot) |
| 237 | return false; |
| 238 | |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | /// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores. |
| 243 | /// Leave gcroot intrinsics; the code generator needs to see those. |
| 244 | bool LowerIntrinsics::runOnFunction(Function &F) { |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 245 | // Quick exit for functions that do not use GC. |
| 246 | if (!F.hasCollector()) return false; |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 247 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 248 | CollectorMetadata &MD = getAnalysis<CollectorModuleMetadata>().get(F); |
| 249 | Collector &Coll = MD.getCollector(); |
| 250 | |
| 251 | bool MadeChange = false; |
| 252 | |
| 253 | if (NeedsDefaultLoweringPass(Coll)) |
| 254 | MadeChange |= PerformDefaultLowering(F, Coll); |
| 255 | |
| 256 | if (NeedsCustomLoweringPass(Coll)) |
| 257 | MadeChange |= Coll.performCustomLowering(F); |
| 258 | |
| 259 | return MadeChange; |
| 260 | } |
| 261 | |
| 262 | bool LowerIntrinsics::PerformDefaultLowering(Function &F, Collector &Coll) { |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 263 | bool LowerWr = !Coll.customWriteBarrier(); |
| 264 | bool LowerRd = !Coll.customReadBarrier(); |
| 265 | bool InitRoots = Coll.initializeRoots(); |
| 266 | |
| 267 | SmallVector<AllocaInst*,32> Roots; |
| 268 | |
| 269 | bool MadeChange = false; |
| 270 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 271 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) { |
Gordon Henriksen | a6c9925 | 2007-12-22 17:27:01 +0000 | [diff] [blame] | 272 | if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) { |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 273 | Function *F = CI->getCalledFunction(); |
Gordon Henriksen | a6c9925 | 2007-12-22 17:27:01 +0000 | [diff] [blame] | 274 | switch (F->getIntrinsicID()) { |
| 275 | case Intrinsic::gcwrite: |
| 276 | if (LowerWr) { |
| 277 | // Replace a write barrier with a simple store. |
| 278 | Value *St = new StoreInst(CI->getOperand(1), CI->getOperand(3), CI); |
| 279 | CI->replaceAllUsesWith(St); |
| 280 | CI->eraseFromParent(); |
| 281 | } |
| 282 | break; |
| 283 | case Intrinsic::gcread: |
| 284 | if (LowerRd) { |
| 285 | // Replace a read barrier with a simple load. |
| 286 | Value *Ld = new LoadInst(CI->getOperand(2), "", CI); |
| 287 | Ld->takeName(CI); |
| 288 | CI->replaceAllUsesWith(Ld); |
| 289 | CI->eraseFromParent(); |
| 290 | } |
| 291 | break; |
| 292 | case Intrinsic::gcroot: |
| 293 | if (InitRoots) { |
| 294 | // Initialize the GC root, but do not delete the intrinsic. The |
| 295 | // backend needs the intrinsic to flag the stack slot. |
| 296 | Roots.push_back(cast<AllocaInst>( |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 297 | CI->getOperand(1)->stripPointerCasts())); |
Gordon Henriksen | a6c9925 | 2007-12-22 17:27:01 +0000 | [diff] [blame] | 298 | } |
| 299 | break; |
| 300 | default: |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 301 | continue; |
| 302 | } |
| 303 | |
| 304 | MadeChange = true; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if (Roots.size()) |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 310 | MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size()); |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 311 | |
| 312 | return MadeChange; |
| 313 | } |
| 314 | |
| 315 | // ----------------------------------------------------------------------------- |
| 316 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 317 | FunctionPass *llvm::createGCMachineCodeAnalysisPass() { |
| 318 | return new MachineCodeAnalysis(); |
| 319 | } |
| 320 | |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 321 | char MachineCodeAnalysis::ID = 0; |
| 322 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 323 | MachineCodeAnalysis::MachineCodeAnalysis() |
| 324 | : MachineFunctionPass(intptr_t(&ID)) {} |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 325 | |
| 326 | const char *MachineCodeAnalysis::getPassName() const { |
| 327 | return "Analyze Machine Code For Garbage Collection"; |
| 328 | } |
| 329 | |
| 330 | void MachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 331 | MachineFunctionPass::getAnalysisUsage(AU); |
| 332 | AU.setPreservesAll(); |
| 333 | AU.addRequired<MachineModuleInfo>(); |
| 334 | AU.addRequired<CollectorModuleMetadata>(); |
| 335 | } |
| 336 | |
| 337 | unsigned MachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB, |
| 338 | MachineBasicBlock::iterator MI) const { |
| 339 | unsigned Label = MMI->NextLabelID(); |
Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 340 | BuildMI(MBB, MI, TII->get(TargetInstrInfo::GC_LABEL)).addImm(Label); |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 341 | return Label; |
| 342 | } |
| 343 | |
| 344 | void MachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) { |
| 345 | // Find the return address (next instruction), too, so as to bracket the call |
| 346 | // instruction. |
| 347 | MachineBasicBlock::iterator RAI = CI; |
| 348 | ++RAI; |
| 349 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 350 | if (MD->getCollector().needsSafePoint(GC::PreCall)) |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 351 | MD->addSafePoint(GC::PreCall, InsertLabel(*CI->getParent(), CI)); |
| 352 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 353 | if (MD->getCollector().needsSafePoint(GC::PostCall)) |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 354 | MD->addSafePoint(GC::PostCall, InsertLabel(*CI->getParent(), RAI)); |
| 355 | } |
| 356 | |
| 357 | void MachineCodeAnalysis::FindSafePoints(MachineFunction &MF) { |
| 358 | for (MachineFunction::iterator BBI = MF.begin(), |
| 359 | BBE = MF.end(); BBI != BBE; ++BBI) |
| 360 | for (MachineBasicBlock::iterator MI = BBI->begin(), |
| 361 | ME = BBI->end(); MI != ME; ++MI) |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 362 | if (MI->getDesc().isCall()) |
Dan Gohman | fd3ff03 | 2008-07-07 20:08:05 +0000 | [diff] [blame] | 363 | VisitCallPoint(MI); |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void MachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) { |
| 367 | uint64_t StackSize = MFI->getStackSize(); |
| 368 | uint64_t OffsetAdjustment = MFI->getOffsetAdjustment(); |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 369 | uint64_t OffsetOfLocalArea = TM->getFrameInfo()->getOffsetOfLocalArea(); |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 370 | |
| 371 | for (CollectorMetadata::roots_iterator RI = MD->roots_begin(), |
| 372 | RE = MD->roots_end(); RI != RE; ++RI) |
| 373 | RI->StackOffset = MFI->getObjectOffset(RI->Num) + StackSize |
| 374 | - OffsetOfLocalArea + OffsetAdjustment; |
| 375 | } |
| 376 | |
| 377 | bool MachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) { |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 378 | // Quick exit for functions that do not use GC. |
| 379 | if (!MF.getFunction()->hasCollector()) return false; |
| 380 | |
| 381 | MD = &getAnalysis<CollectorModuleMetadata>().get(*MF.getFunction()); |
| 382 | if (!MD->getCollector().needsSafePoints()) |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 383 | return false; |
| 384 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 385 | TM = &MF.getTarget(); |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 386 | MMI = &getAnalysis<MachineModuleInfo>(); |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 387 | TII = TM->getInstrInfo(); |
Gordon Henriksen | 364caf0 | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 388 | MFI = MF.getFrameInfo(); |
| 389 | |
| 390 | // Find the size of the stack frame. |
| 391 | MD->setFrameSize(MFI->getStackSize()); |
| 392 | |
| 393 | // Find all safe points. |
| 394 | FindSafePoints(MF); |
| 395 | |
| 396 | // Find the stack offsets for all roots. |
| 397 | FindStackOffsets(MF); |
| 398 | |
| 399 | return false; |
| 400 | } |