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