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