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