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