Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 1 | //===-- GCRootLowering.cpp - Garbage collection infrastructure ------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the lowering for the gc.root mechanism. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Philip Reames | 2b45395 | 2015-01-16 20:07:33 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/GCMetadata.h" |
Chandler Carruth | 71f308a | 2015-02-13 09:09:03 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/GCStrategy.h" |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 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" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetFrameLowering.h" |
| 21 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 23 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Dominators.h" |
| 25 | #include "llvm/IR/IntrinsicInst.h" |
| 26 | #include "llvm/IR/Module.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | namespace { |
| 34 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 35 | /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or |
| 36 | /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as |
| 37 | /// directed by the GCStrategy. It also performs automatic root initialization |
| 38 | /// and custom intrinsic lowering. |
| 39 | class LowerIntrinsics : public FunctionPass { |
Philip Reames | 18945d6 | 2018-11-11 21:13:09 +0000 | [diff] [blame] | 40 | bool DoLowering(Function &F, GCStrategy &S); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 41 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 42 | public: |
| 43 | static char ID; |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 44 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 45 | LowerIntrinsics(); |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 46 | StringRef getPassName() const override; |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 47 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 48 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 49 | bool doInitialization(Module &M) override; |
| 50 | bool runOnFunction(Function &F) override; |
| 51 | }; |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 52 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 53 | /// GCMachineCodeAnalysis - This is a target-independent pass over the machine |
| 54 | /// function representation to identify safe points for the garbage collector |
| 55 | /// in the machine code. It inserts labels at safe points and populates a |
| 56 | /// GCMetadata record for each function. |
| 57 | class GCMachineCodeAnalysis : public MachineFunctionPass { |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 58 | GCFunctionInfo *FI; |
| 59 | MachineModuleInfo *MMI; |
| 60 | const TargetInstrInfo *TII; |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 61 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 62 | void FindSafePoints(MachineFunction &MF); |
Fangrui Song | cb0bab8 | 2018-07-16 18:51:40 +0000 | [diff] [blame] | 63 | void VisitCallPoint(MachineBasicBlock::iterator CI); |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 64 | MCSymbol *InsertLabel(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 65 | const DebugLoc &DL) const; |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 66 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 67 | void FindStackOffsets(MachineFunction &MF); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 68 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 69 | public: |
| 70 | static char ID; |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 71 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 72 | GCMachineCodeAnalysis(); |
| 73 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 74 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 75 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 76 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 77 | } |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 78 | |
| 79 | // ----------------------------------------------------------------------------- |
| 80 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 81 | INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering", false, |
| 82 | false) |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 83 | INITIALIZE_PASS_DEPENDENCY(GCModuleInfo) |
| 84 | INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false) |
| 85 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 86 | FunctionPass *llvm::createGCLoweringPass() { return new LowerIntrinsics(); } |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 87 | |
| 88 | char LowerIntrinsics::ID = 0; |
| 89 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 90 | LowerIntrinsics::LowerIntrinsics() : FunctionPass(ID) { |
| 91 | initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry()); |
| 92 | } |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 93 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 94 | StringRef LowerIntrinsics::getPassName() const { |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 95 | return "Lower Garbage Collection Instructions"; |
| 96 | } |
| 97 | |
| 98 | void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const { |
| 99 | FunctionPass::getAnalysisUsage(AU); |
| 100 | AU.addRequired<GCModuleInfo>(); |
| 101 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 102 | } |
| 103 | |
| 104 | /// doInitialization - If this module uses the GC intrinsics, find them now. |
| 105 | bool LowerIntrinsics::doInitialization(Module &M) { |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 106 | GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); |
| 107 | assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?"); |
| 108 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 109 | if (!I->isDeclaration() && I->hasGC()) |
| 110 | MI->getFunctionInfo(*I); // Instantiate the GC strategy. |
| 111 | |
Philip Reames | 23cf2e2 | 2015-01-28 19:28:03 +0000 | [diff] [blame] | 112 | return false; |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Philip Reames | 66c9fb0 | 2015-01-15 19:49:25 +0000 | [diff] [blame] | 115 | /// CouldBecomeSafePoint - Predicate to conservatively determine whether the |
| 116 | /// instruction could introduce a safe point. |
| 117 | static bool CouldBecomeSafePoint(Instruction *I) { |
| 118 | // The natural definition of instructions which could introduce safe points |
| 119 | // are: |
| 120 | // |
| 121 | // - call, invoke (AfterCall, BeforeCall) |
| 122 | // - phis (Loops) |
| 123 | // - invoke, ret, unwind (Exit) |
| 124 | // |
| 125 | // However, instructions as seemingly inoccuous as arithmetic can become |
| 126 | // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead |
| 127 | // it is necessary to take a conservative approach. |
| 128 | |
| 129 | if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) || isa<StoreInst>(I) || |
| 130 | isa<LoadInst>(I)) |
| 131 | return false; |
| 132 | |
| 133 | // llvm.gcroot is safe because it doesn't do anything at runtime. |
| 134 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 135 | if (Function *F = CI->getCalledFunction()) |
Pete Cooper | 9e1d335 | 2015-05-20 17:16:39 +0000 | [diff] [blame] | 136 | if (Intrinsic::ID IID = F->getIntrinsicID()) |
Philip Reames | 66c9fb0 | 2015-01-15 19:49:25 +0000 | [diff] [blame] | 137 | if (IID == Intrinsic::gcroot) |
| 138 | return false; |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
Philip Reames | 1559021 | 2018-11-12 02:26:26 +0000 | [diff] [blame] | 143 | static bool InsertRootInitializers(Function &F, ArrayRef<AllocaInst *> Roots) { |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 144 | // Scroll past alloca instructions. |
| 145 | BasicBlock::iterator IP = F.getEntryBlock().begin(); |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 146 | while (isa<AllocaInst>(IP)) |
| 147 | ++IP; |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 148 | |
| 149 | // Search for initializers in the initial BB. |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 150 | SmallPtrSet<AllocaInst *, 16> InitedRoots; |
Duncan P. N. Exon Smith | d83547a | 2015-10-09 18:44:40 +0000 | [diff] [blame] | 151 | for (; !CouldBecomeSafePoint(&*IP); ++IP) |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 152 | if (StoreInst *SI = dyn_cast<StoreInst>(IP)) |
| 153 | if (AllocaInst *AI = |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 154 | dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts())) |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 155 | InitedRoots.insert(AI); |
| 156 | |
| 157 | // Add root initializers. |
| 158 | bool MadeChange = false; |
| 159 | |
Philip Reames | 1559021 | 2018-11-12 02:26:26 +0000 | [diff] [blame] | 160 | for (AllocaInst *Root : Roots) |
| 161 | if (!InitedRoots.count(Root)) { |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 162 | StoreInst *SI = new StoreInst( |
Philip Reames | 1559021 | 2018-11-12 02:26:26 +0000 | [diff] [blame] | 163 | ConstantPointerNull::get(cast<PointerType>(Root->getAllocatedType())), |
| 164 | Root); |
| 165 | SI->insertAfter(Root); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 166 | MadeChange = true; |
| 167 | } |
| 168 | |
| 169 | return MadeChange; |
| 170 | } |
| 171 | |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 172 | /// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores. |
| 173 | /// Leave gcroot intrinsics; the code generator needs to see those. |
| 174 | bool LowerIntrinsics::runOnFunction(Function &F) { |
| 175 | // Quick exit for functions that do not use GC. |
| 176 | if (!F.hasGC()) |
| 177 | return false; |
| 178 | |
| 179 | GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F); |
| 180 | GCStrategy &S = FI.getStrategy(); |
| 181 | |
Philip Reames | 18945d6 | 2018-11-11 21:13:09 +0000 | [diff] [blame] | 182 | return DoLowering(F, S); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Philip Reames | 18945d6 | 2018-11-11 21:13:09 +0000 | [diff] [blame] | 185 | /// Lower barriers out of existance (if the associated GCStrategy hasn't |
| 186 | /// already done so...), and insert initializing stores to roots as a defensive |
| 187 | /// measure. Given we're going to report all roots live at all safepoints, we |
| 188 | /// need to be able to ensure each root has been initialized by the point the |
| 189 | /// first safepoint is reached. This really should have been done by the |
| 190 | /// frontend, but the old API made this non-obvious, so we do a potentially |
| 191 | /// redundant store just in case. |
| 192 | bool LowerIntrinsics::DoLowering(Function &F, GCStrategy &S) { |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 193 | SmallVector<AllocaInst *, 32> Roots; |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 194 | |
| 195 | bool MadeChange = false; |
Philip Reames | 1559021 | 2018-11-12 02:26:26 +0000 | [diff] [blame] | 196 | for (BasicBlock &BB : F) |
| 197 | for (BasicBlock::iterator II = BB.begin(), E = BB.end(); II != E;) { |
| 198 | IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++); |
| 199 | if (!CI) |
| 200 | continue; |
| 201 | |
| 202 | Function *F = CI->getCalledFunction(); |
| 203 | switch (F->getIntrinsicID()) { |
| 204 | default: break; |
| 205 | case Intrinsic::gcwrite: { |
| 206 | // Replace a write barrier with a simple store. |
| 207 | Value *St = new StoreInst(CI->getArgOperand(0), |
| 208 | CI->getArgOperand(2), CI); |
| 209 | CI->replaceAllUsesWith(St); |
| 210 | CI->eraseFromParent(); |
| 211 | MadeChange = true; |
| 212 | break; |
| 213 | } |
| 214 | case Intrinsic::gcread: { |
| 215 | // Replace a read barrier with a simple load. |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 216 | Value *Ld = new LoadInst(CI->getType(), CI->getArgOperand(1), "", CI); |
Philip Reames | 1559021 | 2018-11-12 02:26:26 +0000 | [diff] [blame] | 217 | Ld->takeName(CI); |
| 218 | CI->replaceAllUsesWith(Ld); |
| 219 | CI->eraseFromParent(); |
| 220 | MadeChange = true; |
| 221 | break; |
| 222 | } |
| 223 | case Intrinsic::gcroot: { |
| 224 | // Initialize the GC root, but do not delete the intrinsic. The |
| 225 | // backend needs the intrinsic to flag the stack slot. |
| 226 | Roots.push_back( |
| 227 | cast<AllocaInst>(CI->getArgOperand(0)->stripPointerCasts())); |
| 228 | break; |
| 229 | } |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 232 | |
| 233 | if (Roots.size()) |
Philip Reames | 1559021 | 2018-11-12 02:26:26 +0000 | [diff] [blame] | 234 | MadeChange |= InsertRootInitializers(F, Roots); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 235 | |
| 236 | return MadeChange; |
| 237 | } |
| 238 | |
| 239 | // ----------------------------------------------------------------------------- |
| 240 | |
| 241 | char GCMachineCodeAnalysis::ID = 0; |
| 242 | char &llvm::GCMachineCodeAnalysisID = GCMachineCodeAnalysis::ID; |
| 243 | |
| 244 | INITIALIZE_PASS(GCMachineCodeAnalysis, "gc-analysis", |
| 245 | "Analyze Machine Code For Garbage Collection", false, false) |
| 246 | |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 247 | GCMachineCodeAnalysis::GCMachineCodeAnalysis() : MachineFunctionPass(ID) {} |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 248 | |
| 249 | void GCMachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 250 | MachineFunctionPass::getAnalysisUsage(AU); |
| 251 | AU.setPreservesAll(); |
| 252 | AU.addRequired<MachineModuleInfo>(); |
| 253 | AU.addRequired<GCModuleInfo>(); |
| 254 | } |
| 255 | |
| 256 | MCSymbol *GCMachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB, |
| 257 | MachineBasicBlock::iterator MI, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 258 | const DebugLoc &DL) const { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 259 | MCSymbol *Label = MBB.getParent()->getContext().createTempSymbol(); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 260 | BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label); |
| 261 | return Label; |
| 262 | } |
| 263 | |
| 264 | void GCMachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) { |
Philip Reames | c75a0c3 | 2018-11-12 20:15:34 +0000 | [diff] [blame] | 265 | // Find the return address (next instruction), since that's what will be on |
| 266 | // the stack when the call is suspended and we need to inspect the stack. |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 267 | MachineBasicBlock::iterator RAI = CI; |
| 268 | ++RAI; |
| 269 | |
Philip Reames | e44a55d | 2018-11-12 22:03:53 +0000 | [diff] [blame] | 270 | MCSymbol *Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc()); |
| 271 | FI->addSafePoint(Label, CI->getDebugLoc()); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void GCMachineCodeAnalysis::FindSafePoints(MachineFunction &MF) { |
Philip Reames | 1559021 | 2018-11-12 02:26:26 +0000 | [diff] [blame] | 275 | for (MachineBasicBlock &MBB : MF) |
| 276 | for (MachineBasicBlock::iterator MI = MBB.begin(), ME = MBB.end(); |
Philip Reames | b871441 | 2015-01-15 19:39:17 +0000 | [diff] [blame] | 277 | MI != ME; ++MI) |
Philip Reames | 7de640a | 2015-01-16 19:33:28 +0000 | [diff] [blame] | 278 | if (MI->isCall()) { |
| 279 | // Do not treat tail or sibling call sites as safe points. This is |
| 280 | // legal since any arguments passed to the callee which live in the |
| 281 | // remnants of the callers frame will be owned and updated by the |
| 282 | // callee if required. |
| 283 | if (MI->isTerminator()) |
| 284 | continue; |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 285 | VisitCallPoint(MI); |
Philip Reames | 7de640a | 2015-01-16 19:33:28 +0000 | [diff] [blame] | 286 | } |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void GCMachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) { |
Eric Christopher | 1df0c51 | 2015-02-20 18:44:15 +0000 | [diff] [blame] | 290 | const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 291 | assert(TFI && "TargetRegisterInfo not available!"); |
| 292 | |
| 293 | for (GCFunctionInfo::roots_iterator RI = FI->roots_begin(); |
| 294 | RI != FI->roots_end();) { |
| 295 | // If the root references a dead object, no need to keep it. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 296 | if (MF.getFrameInfo().isDeadObjectIndex(RI->Num)) { |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 297 | RI = FI->removeStackRoot(RI); |
| 298 | } else { |
James Y Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 299 | unsigned FrameReg; // FIXME: surely GCRoot ought to store the |
| 300 | // register that the offset is from? |
| 301 | RI->StackOffset = TFI->getFrameIndexReference(MF, RI->Num, FrameReg); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 302 | ++RI; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | bool GCMachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) { |
| 308 | // Quick exit for functions that do not use GC. |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 309 | if (!MF.getFunction().hasGC()) |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 310 | return false; |
| 311 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 312 | FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(MF.getFunction()); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 313 | MMI = &getAnalysis<MachineModuleInfo>(); |
Eric Christopher | 1df0c51 | 2015-02-20 18:44:15 +0000 | [diff] [blame] | 314 | TII = MF.getSubtarget().getInstrInfo(); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 315 | |
Philip Reames | 2df7827 | 2015-04-02 05:00:40 +0000 | [diff] [blame] | 316 | // Find the size of the stack frame. There may be no correct static frame |
| 317 | // size, we use UINT64_MAX to represent this. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 318 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Philip Reames | 2df7827 | 2015-04-02 05:00:40 +0000 | [diff] [blame] | 319 | const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 320 | const bool DynamicFrameSize = MFI.hasVarSizedObjects() || |
Philip Reames | 2df7827 | 2015-04-02 05:00:40 +0000 | [diff] [blame] | 321 | RegInfo->needsStackRealignment(MF); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 322 | FI->setFrameSize(DynamicFrameSize ? UINT64_MAX : MFI.getStackSize()); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 323 | |
| 324 | // Find all safe points. |
Philip Reames | 2df7827 | 2015-04-02 05:00:40 +0000 | [diff] [blame] | 325 | if (FI->getStrategy().needsSafePoints()) |
| 326 | FindSafePoints(MF); |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 327 | |
Philip Reames | 2df7827 | 2015-04-02 05:00:40 +0000 | [diff] [blame] | 328 | // Find the concrete stack offsets for all roots (stack slots) |
Philip Reames | f27f373 | 2015-01-15 19:29:42 +0000 | [diff] [blame] | 329 | FindStackOffsets(MF); |
| 330 | |
| 331 | return false; |
| 332 | } |