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