Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1 | //===-- GCStrategy.cpp - Garbage collection infrastructure -----------------===// |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 37ca83d | 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 | // |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 13 | // GCMachineCodeAnalysis identifies the GC safe points in the machine code. |
| 14 | // Roots are identified in SelectionDAGISel. |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 15 | // |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Gordon Henriksen | bcef14d | 2008-08-17 12:56:54 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/GCStrategy.h" |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/IntrinsicInst.h" |
| 26 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetFrameLowering.h" |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetInstrInfo.h" |
| 32 | #include "llvm/Target/TargetMachine.h" |
Gordon Henriksen | 72bd9df | 2008-08-19 17:06:35 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetRegisterInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetSubtargetInfo.h" |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace llvm; |
| 37 | |
| 38 | namespace { |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 39 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 40 | /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 41 | /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 42 | /// directed by the GCStrategy. It also performs automatic root initialization |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 43 | /// and custom intrinsic lowering. |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 44 | class LowerIntrinsics : public FunctionPass { |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 45 | static bool NeedsDefaultLoweringPass(const GCStrategy &C); |
| 46 | static bool NeedsCustomLoweringPass(const GCStrategy &C); |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 47 | static bool CouldBecomeSafePoint(Instruction *I); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 48 | bool PerformDefaultLowering(Function &F, GCStrategy &Coll); |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 49 | static bool InsertRootInitializers(Function &F, |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 50 | AllocaInst **Roots, unsigned Count); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 51 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 52 | public: |
| 53 | static char ID; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 54 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 55 | LowerIntrinsics(); |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 56 | const char *getPassName() const override; |
| 57 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 58 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 59 | bool doInitialization(Module &M) override; |
| 60 | bool runOnFunction(Function &F) override; |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 61 | }; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 62 | |
| 63 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 64 | /// GCMachineCodeAnalysis - This is a target-independent pass over the machine |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 65 | /// function representation to identify safe points for the garbage collector |
| 66 | /// in the machine code. It inserts labels at safe points and populates a |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 67 | /// GCMetadata record for each function. |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 68 | class GCMachineCodeAnalysis : public MachineFunctionPass { |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 69 | const TargetMachine *TM; |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 70 | GCFunctionInfo *FI; |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 71 | MachineModuleInfo *MMI; |
| 72 | const TargetInstrInfo *TII; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 73 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 74 | void FindSafePoints(MachineFunction &MF); |
| 75 | void VisitCallPoint(MachineBasicBlock::iterator MI); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 76 | MCSymbol *InsertLabel(MachineBasicBlock &MBB, |
Chris Lattner | 1065f49 | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 77 | MachineBasicBlock::iterator MI, |
| 78 | DebugLoc DL) const; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 79 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 80 | void FindStackOffsets(MachineFunction &MF); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 81 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 82 | public: |
| 83 | static char ID; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 84 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 85 | GCMachineCodeAnalysis(); |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 86 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 87 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 88 | bool runOnMachineFunction(MachineFunction &MF) override; |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 89 | }; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 90 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // ----------------------------------------------------------------------------- |
| 94 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 95 | GCStrategy::GCStrategy() : |
Philip Reames | 4ac17a3 | 2015-01-07 19:07:50 +0000 | [diff] [blame] | 96 | UseStatepoints(false), |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 97 | NeededSafePoints(0), |
| 98 | CustomReadBarriers(false), |
| 99 | CustomWriteBarriers(false), |
| 100 | CustomRoots(false), |
Nicolas Geoffray | 26c328d | 2011-11-11 18:32:52 +0000 | [diff] [blame] | 101 | CustomSafePoints(false), |
Gordon Henriksen | dbe06d3 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 102 | InitRoots(true), |
| 103 | UsesMetadata(false) |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 104 | {} |
| 105 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 106 | // ----------------------------------------------------------------------------- |
| 107 | |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 108 | INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering", |
| 109 | false, false) |
| 110 | INITIALIZE_PASS_DEPENDENCY(GCModuleInfo) |
| 111 | INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false) |
| 112 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 113 | FunctionPass *llvm::createGCLoweringPass() { |
| 114 | return new LowerIntrinsics(); |
| 115 | } |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 116 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 117 | char LowerIntrinsics::ID = 0; |
| 118 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 119 | LowerIntrinsics::LowerIntrinsics() |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 120 | : FunctionPass(ID) { |
| 121 | initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry()); |
| 122 | } |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 123 | |
| 124 | const char *LowerIntrinsics::getPassName() const { |
| 125 | return "Lower Garbage Collection Instructions"; |
| 126 | } |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 127 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 128 | void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const { |
| 129 | FunctionPass::getAnalysisUsage(AU); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 130 | AU.addRequired<GCModuleInfo>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 131 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /// doInitialization - If this module uses the GC intrinsics, find them now. |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 135 | bool LowerIntrinsics::doInitialization(Module &M) { |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 136 | // FIXME: This is rather antisocial in the context of a JIT since it performs |
| 137 | // work against the entire module. But this cannot be done at |
| 138 | // runFunction time (initializeCustomLowering likely needs to change |
| 139 | // the module). |
Duncan Sands | 5a913d6 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 140 | GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 141 | assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?"); |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 142 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 143 | if (!I->isDeclaration() && I->hasGC()) |
| 144 | MI->getFunctionInfo(*I); // Instantiate the GC strategy. |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 145 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 146 | bool MadeChange = false; |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 147 | for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I) |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 148 | if (NeedsCustomLoweringPass(**I)) |
| 149 | if ((*I)->initializeCustomLowering(M)) |
| 150 | MadeChange = true; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 151 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 152 | return MadeChange; |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 155 | bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots, |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 156 | unsigned Count) { |
| 157 | // Scroll past alloca instructions. |
| 158 | BasicBlock::iterator IP = F.getEntryBlock().begin(); |
| 159 | while (isa<AllocaInst>(IP)) ++IP; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 160 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 161 | // Search for initializers in the initial BB. |
| 162 | SmallPtrSet<AllocaInst*,16> InitedRoots; |
| 163 | for (; !CouldBecomeSafePoint(IP); ++IP) |
| 164 | if (StoreInst *SI = dyn_cast<StoreInst>(IP)) |
Anton Korobeynikov | 82c02b2 | 2008-05-06 22:52:30 +0000 | [diff] [blame] | 165 | if (AllocaInst *AI = |
Anton Korobeynikov | fc2edad | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 166 | dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts())) |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 167 | InitedRoots.insert(AI); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 168 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 169 | // Add root initializers. |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 170 | bool MadeChange = false; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 171 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 172 | for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I) |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 173 | if (!InitedRoots.count(*I)) { |
Nicolas Geoffray | 19cd1d8 | 2010-04-15 19:53:35 +0000 | [diff] [blame] | 174 | StoreInst* SI = new StoreInst(ConstantPointerNull::get(cast<PointerType>( |
| 175 | cast<PointerType>((*I)->getType())->getElementType())), |
| 176 | *I); |
| 177 | SI->insertAfter(*I); |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 178 | MadeChange = true; |
| 179 | } |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 180 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 181 | return MadeChange; |
| 182 | } |
| 183 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 184 | bool LowerIntrinsics::NeedsDefaultLoweringPass(const GCStrategy &C) { |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 185 | // Default lowering is necessary only if read or write barriers have a default |
| 186 | // action. The default for roots is no action. |
| 187 | return !C.customWriteBarrier() |
| 188 | || !C.customReadBarrier() |
| 189 | || C.initializeRoots(); |
| 190 | } |
| 191 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 192 | bool LowerIntrinsics::NeedsCustomLoweringPass(const GCStrategy &C) { |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 193 | // Custom lowering is only necessary if enabled for some action. |
| 194 | return C.customWriteBarrier() |
| 195 | || C.customReadBarrier() |
| 196 | || C.customRoots(); |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /// CouldBecomeSafePoint - Predicate to conservatively determine whether the |
| 200 | /// instruction could introduce a safe point. |
| 201 | bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) { |
| 202 | // The natural definition of instructions which could introduce safe points |
| 203 | // are: |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 204 | // |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 205 | // - call, invoke (AfterCall, BeforeCall) |
| 206 | // - phis (Loops) |
| 207 | // - invoke, ret, unwind (Exit) |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 208 | // |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 209 | // However, instructions as seemingly inoccuous as arithmetic can become |
| 210 | // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead |
| 211 | // it is necessary to take a conservative approach. |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 212 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 213 | if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) || |
| 214 | isa<StoreInst>(I) || isa<LoadInst>(I)) |
| 215 | return false; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 216 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 217 | // llvm.gcroot is safe because it doesn't do anything at runtime. |
| 218 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 219 | if (Function *F = CI->getCalledFunction()) |
| 220 | if (unsigned IID = F->getIntrinsicID()) |
| 221 | if (IID == Intrinsic::gcroot) |
| 222 | return false; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 223 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 224 | return true; |
| 225 | } |
| 226 | |
| 227 | /// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores. |
| 228 | /// Leave gcroot intrinsics; the code generator needs to see those. |
| 229 | bool LowerIntrinsics::runOnFunction(Function &F) { |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 230 | // Quick exit for functions that do not use GC. |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 231 | if (!F.hasGC()) |
| 232 | return false; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 233 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 234 | GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F); |
| 235 | GCStrategy &S = FI.getStrategy(); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 236 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 237 | bool MadeChange = false; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 238 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 239 | if (NeedsDefaultLoweringPass(S)) |
| 240 | MadeChange |= PerformDefaultLowering(F, S); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 241 | |
Cameron Zwarich | 84986b2 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 242 | bool UseCustomLoweringPass = NeedsCustomLoweringPass(S); |
| 243 | if (UseCustomLoweringPass) |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 244 | MadeChange |= S.performCustomLowering(F); |
Cameron Zwarich | 84986b2 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 245 | |
| 246 | // Custom lowering may modify the CFG, so dominators must be recomputed. |
| 247 | if (UseCustomLoweringPass) { |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 248 | if (DominatorTreeWrapperPass *DTWP = |
| 249 | getAnalysisIfAvailable<DominatorTreeWrapperPass>()) |
| 250 | DTWP->getDomTree().recalculate(F); |
Cameron Zwarich | 84986b2 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 253 | return MadeChange; |
| 254 | } |
| 255 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 256 | bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) { |
| 257 | bool LowerWr = !S.customWriteBarrier(); |
| 258 | bool LowerRd = !S.customReadBarrier(); |
| 259 | bool InitRoots = S.initializeRoots(); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 260 | |
Gabor Greif | 3e44ea1 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 261 | SmallVector<AllocaInst*, 32> Roots; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 262 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 263 | bool MadeChange = false; |
| 264 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 265 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) { |
Gordon Henriksen | 41689b5 | 2007-12-22 17:27:01 +0000 | [diff] [blame] | 266 | if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) { |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 267 | Function *F = CI->getCalledFunction(); |
Gordon Henriksen | 41689b5 | 2007-12-22 17:27:01 +0000 | [diff] [blame] | 268 | switch (F->getIntrinsicID()) { |
| 269 | case Intrinsic::gcwrite: |
| 270 | if (LowerWr) { |
| 271 | // Replace a write barrier with a simple store. |
Gabor Greif | 3e44ea1 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 272 | Value *St = new StoreInst(CI->getArgOperand(0), |
| 273 | CI->getArgOperand(2), CI); |
Gordon Henriksen | 41689b5 | 2007-12-22 17:27:01 +0000 | [diff] [blame] | 274 | CI->replaceAllUsesWith(St); |
| 275 | CI->eraseFromParent(); |
| 276 | } |
| 277 | break; |
| 278 | case Intrinsic::gcread: |
| 279 | if (LowerRd) { |
| 280 | // Replace a read barrier with a simple load. |
Gabor Greif | b5874de | 2010-06-25 08:48:19 +0000 | [diff] [blame] | 281 | Value *Ld = new LoadInst(CI->getArgOperand(1), "", CI); |
Gordon Henriksen | 41689b5 | 2007-12-22 17:27:01 +0000 | [diff] [blame] | 282 | Ld->takeName(CI); |
| 283 | CI->replaceAllUsesWith(Ld); |
| 284 | CI->eraseFromParent(); |
| 285 | } |
| 286 | break; |
| 287 | case Intrinsic::gcroot: |
| 288 | if (InitRoots) { |
| 289 | // Initialize the GC root, but do not delete the intrinsic. The |
| 290 | // backend needs the intrinsic to flag the stack slot. |
| 291 | Roots.push_back(cast<AllocaInst>( |
Gabor Greif | b5874de | 2010-06-25 08:48:19 +0000 | [diff] [blame] | 292 | CI->getArgOperand(0)->stripPointerCasts())); |
Gordon Henriksen | 41689b5 | 2007-12-22 17:27:01 +0000 | [diff] [blame] | 293 | } |
| 294 | break; |
| 295 | default: |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 296 | continue; |
| 297 | } |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 298 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 299 | MadeChange = true; |
| 300 | } |
| 301 | } |
| 302 | } |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 303 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 304 | if (Roots.size()) |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 305 | MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size()); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 306 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 307 | return MadeChange; |
| 308 | } |
| 309 | |
| 310 | // ----------------------------------------------------------------------------- |
| 311 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 312 | char GCMachineCodeAnalysis::ID = 0; |
| 313 | char &llvm::GCMachineCodeAnalysisID = GCMachineCodeAnalysis::ID; |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 314 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 315 | INITIALIZE_PASS(GCMachineCodeAnalysis, "gc-analysis", |
| 316 | "Analyze Machine Code For Garbage Collection", false, false) |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 317 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 318 | GCMachineCodeAnalysis::GCMachineCodeAnalysis() |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 319 | : MachineFunctionPass(ID) {} |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 320 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 321 | void GCMachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 322 | MachineFunctionPass::getAnalysisUsage(AU); |
| 323 | AU.setPreservesAll(); |
| 324 | AU.addRequired<MachineModuleInfo>(); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 325 | AU.addRequired<GCModuleInfo>(); |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 328 | MCSymbol *GCMachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB, |
| 329 | MachineBasicBlock::iterator MI, |
| 330 | DebugLoc DL) const { |
Chris Lattner | aed00fa | 2010-03-17 05:41:18 +0000 | [diff] [blame] | 331 | MCSymbol *Label = MBB.getParent()->getContext().CreateTempSymbol(); |
Chris Lattner | 1065f49 | 2010-03-14 07:27:07 +0000 | [diff] [blame] | 332 | BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label); |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 333 | return Label; |
| 334 | } |
| 335 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 336 | void GCMachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) { |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 337 | // Find the return address (next instruction), too, so as to bracket the call |
| 338 | // instruction. |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 339 | MachineBasicBlock::iterator RAI = CI; |
| 340 | ++RAI; |
| 341 | |
Nicolas Geoffray | cbb4218 | 2010-09-24 17:27:50 +0000 | [diff] [blame] | 342 | if (FI->getStrategy().needsSafePoint(GC::PreCall)) { |
| 343 | MCSymbol* Label = InsertLabel(*CI->getParent(), CI, CI->getDebugLoc()); |
| 344 | FI->addSafePoint(GC::PreCall, Label, CI->getDebugLoc()); |
| 345 | } |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 346 | |
Nicolas Geoffray | cbb4218 | 2010-09-24 17:27:50 +0000 | [diff] [blame] | 347 | if (FI->getStrategy().needsSafePoint(GC::PostCall)) { |
| 348 | MCSymbol* Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc()); |
| 349 | FI->addSafePoint(GC::PostCall, Label, CI->getDebugLoc()); |
| 350 | } |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 353 | void GCMachineCodeAnalysis::FindSafePoints(MachineFunction &MF) { |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 354 | for (MachineFunction::iterator BBI = MF.begin(), |
| 355 | BBE = MF.end(); BBI != BBE; ++BBI) |
| 356 | for (MachineBasicBlock::iterator MI = BBI->begin(), |
| 357 | ME = BBI->end(); MI != ME; ++MI) |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 358 | if (MI->isCall()) |
Dan Gohman | 14464bc | 2008-07-07 20:08:05 +0000 | [diff] [blame] | 359 | VisitCallPoint(MI); |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 362 | void GCMachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) { |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 363 | const TargetFrameLowering *TFI = TM->getSubtargetImpl()->getFrameLowering(); |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 364 | assert(TFI && "TargetRegisterInfo not available!"); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 365 | |
Nicolas Geoffray | 457b356 | 2012-10-26 09:15:55 +0000 | [diff] [blame] | 366 | for (GCFunctionInfo::roots_iterator RI = FI->roots_begin(); |
| 367 | RI != FI->roots_end();) { |
| 368 | // If the root references a dead object, no need to keep it. |
| 369 | if (MF.getFrameInfo()->isDeadObjectIndex(RI->Num)) { |
| 370 | RI = FI->removeStackRoot(RI); |
| 371 | } else { |
| 372 | RI->StackOffset = TFI->getFrameIndexOffset(MF, RI->Num); |
| 373 | ++RI; |
| 374 | } |
| 375 | } |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 378 | bool GCMachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) { |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 379 | // Quick exit for functions that do not use GC. |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 380 | if (!MF.getFunction()->hasGC()) |
| 381 | return false; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 382 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 383 | FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction()); |
| 384 | if (!FI->getStrategy().needsSafePoints()) |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 385 | return false; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 386 | |
Gordon Henriksen | 7843c16 | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 387 | TM = &MF.getTarget(); |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 388 | MMI = &getAnalysis<MachineModuleInfo>(); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 389 | TII = TM->getSubtargetImpl()->getInstrInfo(); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 390 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 391 | // Find the size of the stack frame. |
Gordon Henriksen | 72bd9df | 2008-08-19 17:06:35 +0000 | [diff] [blame] | 392 | FI->setFrameSize(MF.getFrameInfo()->getStackSize()); |
Nicolas Geoffray | 26c328d | 2011-11-11 18:32:52 +0000 | [diff] [blame] | 393 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 394 | // Find all safe points. |
Nicolas Geoffray | 26c328d | 2011-11-11 18:32:52 +0000 | [diff] [blame] | 395 | if (FI->getStrategy().customSafePoints()) { |
| 396 | FI->getStrategy().findCustomSafePoints(*FI, MF); |
| 397 | } else { |
| 398 | FindSafePoints(MF); |
| 399 | } |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 400 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 401 | // Find the stack offsets for all roots. |
| 402 | FindStackOffsets(MF); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 403 | |
Gordon Henriksen | 37ca83d | 2007-09-29 02:13:43 +0000 | [diff] [blame] | 404 | return false; |
| 405 | } |