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