1 //===-- GCRootLowering.cpp - Garbage collection infrastructure ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the lowering for the gc.root mechanism.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/GCMetadata.h"
15 #include "llvm/CodeGen/GCStrategy.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/CodeGen/TargetFrameLowering.h"
22 #include "llvm/CodeGen/TargetInstrInfo.h"
23 #include "llvm/CodeGen/TargetRegisterInfo.h"
24 #include "llvm/CodeGen/TargetSubtargetInfo.h"
25 #include "llvm/IR/Dominators.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
31 
32 using namespace llvm;
33 
34 namespace {
35 
36 /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
37 /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
38 /// directed by the GCStrategy. It also performs automatic root initialization
39 /// and custom intrinsic lowering.
40 class LowerIntrinsics : public FunctionPass {
41   bool DoLowering(Function &F, GCStrategy &S);
42 
43 public:
44   static char ID;
45 
46   LowerIntrinsics();
47   StringRef getPassName() const override;
48   void getAnalysisUsage(AnalysisUsage &AU) const override;
49 
50   bool doInitialization(Module &M) override;
51   bool runOnFunction(Function &F) override;
52 };
53 
54 /// GCMachineCodeAnalysis - This is a target-independent pass over the machine
55 /// function representation to identify safe points for the garbage collector
56 /// in the machine code. It inserts labels at safe points and populates a
57 /// GCMetadata record for each function.
58 class GCMachineCodeAnalysis : public MachineFunctionPass {
59   GCFunctionInfo *FI;
60   MachineModuleInfo *MMI;
61   const TargetInstrInfo *TII;
62 
63   void FindSafePoints(MachineFunction &MF);
64   void VisitCallPoint(MachineBasicBlock::iterator CI);
65   MCSymbol *InsertLabel(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
66                         const DebugLoc &DL) const;
67 
68   void FindStackOffsets(MachineFunction &MF);
69 
70 public:
71   static char ID;
72 
73   GCMachineCodeAnalysis();
74   void getAnalysisUsage(AnalysisUsage &AU) const override;
75 
76   bool runOnMachineFunction(MachineFunction &MF) override;
77 };
78 }
79 
80 // -----------------------------------------------------------------------------
81 
82 INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering", false,
83                       false)
84 INITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
85 INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false)
86 
87 FunctionPass *llvm::createGCLoweringPass() { return new LowerIntrinsics(); }
88 
89 char LowerIntrinsics::ID = 0;
90 
91 LowerIntrinsics::LowerIntrinsics() : FunctionPass(ID) {
92   initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry());
93 }
94 
95 StringRef LowerIntrinsics::getPassName() const {
96   return "Lower Garbage Collection Instructions";
97 }
98 
99 void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
100   FunctionPass::getAnalysisUsage(AU);
101   AU.addRequired<GCModuleInfo>();
102   AU.addPreserved<DominatorTreeWrapperPass>();
103 }
104 
105 /// doInitialization - If this module uses the GC intrinsics, find them now.
106 bool LowerIntrinsics::doInitialization(Module &M) {
107   GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
108   assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?");
109   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
110     if (!I->isDeclaration() && I->hasGC())
111       MI->getFunctionInfo(*I); // Instantiate the GC strategy.
112 
113   return false;
114 }
115 
116 /// CouldBecomeSafePoint - Predicate to conservatively determine whether the
117 /// instruction could introduce a safe point.
118 static bool CouldBecomeSafePoint(Instruction *I) {
119   // The natural definition of instructions which could introduce safe points
120   // are:
121   //
122   //   - call, invoke (AfterCall, BeforeCall)
123   //   - phis (Loops)
124   //   - invoke, ret, unwind (Exit)
125   //
126   // However, instructions as seemingly inoccuous as arithmetic can become
127   // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead
128   // it is necessary to take a conservative approach.
129 
130   if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) || isa<StoreInst>(I) ||
131       isa<LoadInst>(I))
132     return false;
133 
134   // llvm.gcroot is safe because it doesn't do anything at runtime.
135   if (CallInst *CI = dyn_cast<CallInst>(I))
136     if (Function *F = CI->getCalledFunction())
137       if (Intrinsic::ID IID = F->getIntrinsicID())
138         if (IID == Intrinsic::gcroot)
139           return false;
140 
141   return true;
142 }
143 
144 static bool InsertRootInitializers(Function &F, AllocaInst **Roots,
145                                    unsigned Count) {
146   // Scroll past alloca instructions.
147   BasicBlock::iterator IP = F.getEntryBlock().begin();
148   while (isa<AllocaInst>(IP))
149     ++IP;
150 
151   // Search for initializers in the initial BB.
152   SmallPtrSet<AllocaInst *, 16> InitedRoots;
153   for (; !CouldBecomeSafePoint(&*IP); ++IP)
154     if (StoreInst *SI = dyn_cast<StoreInst>(IP))
155       if (AllocaInst *AI =
156               dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts()))
157         InitedRoots.insert(AI);
158 
159   // Add root initializers.
160   bool MadeChange = false;
161 
162   for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
163     if (!InitedRoots.count(*I)) {
164       StoreInst *SI = new StoreInst(
165           ConstantPointerNull::get(cast<PointerType>((*I)->getAllocatedType())),
166           *I);
167       SI->insertAfter(*I);
168       MadeChange = true;
169     }
170 
171   return MadeChange;
172 }
173 
174 /// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores.
175 /// Leave gcroot intrinsics; the code generator needs to see those.
176 bool LowerIntrinsics::runOnFunction(Function &F) {
177   // Quick exit for functions that do not use GC.
178   if (!F.hasGC())
179     return false;
180 
181   GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F);
182   GCStrategy &S = FI.getStrategy();
183 
184   return DoLowering(F, S);
185 }
186 
187 /// Lower barriers out of existance (if the associated GCStrategy hasn't
188 /// already done so...), and insert initializing stores to roots as a defensive
189 /// measure.  Given we're going to report all roots live at all safepoints, we
190 /// need to be able to ensure each root has been initialized by the point the
191 /// first safepoint is reached.  This really should have been done by the
192 /// frontend, but the old API made this non-obvious, so we do a potentially
193 /// redundant store just in case.
194 bool LowerIntrinsics::DoLowering(Function &F, GCStrategy &S) {
195   SmallVector<AllocaInst *, 32> Roots;
196 
197   bool MadeChange = false;
198   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
199     for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) {
200       if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) {
201         Function *F = CI->getCalledFunction();
202         switch (F->getIntrinsicID()) {
203         default: break;
204         case Intrinsic::gcwrite: {
205           // Replace a write barrier with a simple store.
206           Value *St = new StoreInst(CI->getArgOperand(0),
207                                     CI->getArgOperand(2), CI);
208           CI->replaceAllUsesWith(St);
209           CI->eraseFromParent();
210           MadeChange = true;
211           break;
212         }
213         case Intrinsic::gcread: {
214           // Replace a read barrier with a simple load.
215           Value *Ld = new LoadInst(CI->getArgOperand(1), "", CI);
216           Ld->takeName(CI);
217           CI->replaceAllUsesWith(Ld);
218           CI->eraseFromParent();
219           MadeChange = true;
220           break;
221         }
222         case Intrinsic::gcroot: {
223           // Initialize the GC root, but do not delete the intrinsic. The
224           // backend needs the intrinsic to flag the stack slot.
225           Roots.push_back(
226               cast<AllocaInst>(CI->getArgOperand(0)->stripPointerCasts()));
227           break;
228         }
229         }
230       }
231     }
232   }
233 
234   if (Roots.size())
235     MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size());
236 
237   return MadeChange;
238 }
239 
240 // -----------------------------------------------------------------------------
241 
242 char GCMachineCodeAnalysis::ID = 0;
243 char &llvm::GCMachineCodeAnalysisID = GCMachineCodeAnalysis::ID;
244 
245 INITIALIZE_PASS(GCMachineCodeAnalysis, "gc-analysis",
246                 "Analyze Machine Code For Garbage Collection", false, false)
247 
248 GCMachineCodeAnalysis::GCMachineCodeAnalysis() : MachineFunctionPass(ID) {}
249 
250 void GCMachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
251   MachineFunctionPass::getAnalysisUsage(AU);
252   AU.setPreservesAll();
253   AU.addRequired<MachineModuleInfo>();
254   AU.addRequired<GCModuleInfo>();
255 }
256 
257 MCSymbol *GCMachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
258                                              MachineBasicBlock::iterator MI,
259                                              const DebugLoc &DL) const {
260   MCSymbol *Label = MBB.getParent()->getContext().createTempSymbol();
261   BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
262   return Label;
263 }
264 
265 void GCMachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
266   // Find the return address (next instruction), too, so as to bracket the call
267   // instruction.
268   MachineBasicBlock::iterator RAI = CI;
269   ++RAI;
270 
271   if (FI->getStrategy().needsSafePoint(GC::PreCall)) {
272     MCSymbol *Label = InsertLabel(*CI->getParent(), CI, CI->getDebugLoc());
273     FI->addSafePoint(GC::PreCall, Label, CI->getDebugLoc());
274   }
275 
276   if (FI->getStrategy().needsSafePoint(GC::PostCall)) {
277     MCSymbol *Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc());
278     FI->addSafePoint(GC::PostCall, Label, CI->getDebugLoc());
279   }
280 }
281 
282 void GCMachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
283   for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end(); BBI != BBE;
284        ++BBI)
285     for (MachineBasicBlock::iterator MI = BBI->begin(), ME = BBI->end();
286          MI != ME; ++MI)
287       if (MI->isCall()) {
288         // Do not treat tail or sibling call sites as safe points.  This is
289         // legal since any arguments passed to the callee which live in the
290         // remnants of the callers frame will be owned and updated by the
291         // callee if required.
292         if (MI->isTerminator())
293           continue;
294         VisitCallPoint(MI);
295       }
296 }
297 
298 void GCMachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
299   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
300   assert(TFI && "TargetRegisterInfo not available!");
301 
302   for (GCFunctionInfo::roots_iterator RI = FI->roots_begin();
303        RI != FI->roots_end();) {
304     // If the root references a dead object, no need to keep it.
305     if (MF.getFrameInfo().isDeadObjectIndex(RI->Num)) {
306       RI = FI->removeStackRoot(RI);
307     } else {
308       unsigned FrameReg; // FIXME: surely GCRoot ought to store the
309                          // register that the offset is from?
310       RI->StackOffset = TFI->getFrameIndexReference(MF, RI->Num, FrameReg);
311       ++RI;
312     }
313   }
314 }
315 
316 bool GCMachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
317   // Quick exit for functions that do not use GC.
318   if (!MF.getFunction().hasGC())
319     return false;
320 
321   FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(MF.getFunction());
322   MMI = &getAnalysis<MachineModuleInfo>();
323   TII = MF.getSubtarget().getInstrInfo();
324 
325   // Find the size of the stack frame.  There may be no correct static frame
326   // size, we use UINT64_MAX to represent this.
327   const MachineFrameInfo &MFI = MF.getFrameInfo();
328   const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
329   const bool DynamicFrameSize = MFI.hasVarSizedObjects() ||
330     RegInfo->needsStackRealignment(MF);
331   FI->setFrameSize(DynamicFrameSize ? UINT64_MAX : MFI.getStackSize());
332 
333   // Find all safe points.
334   if (FI->getStrategy().needsSafePoints())
335     FindSafePoints(MF);
336 
337   // Find the concrete stack offsets for all roots (stack slots)
338   FindStackOffsets(MF);
339 
340   return false;
341 }
342