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