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