LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GCStrategy.cpp
Go to the documentation of this file.
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 
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Support/Debug.h"
35 
36 using namespace llvm;
37 
38 namespace {
39 
40  /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
41  /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
42  /// directed by the GCStrategy. It also performs automatic root initialization
43  /// and custom intrinsic lowering.
44  class LowerIntrinsics : public FunctionPass {
45  static bool NeedsDefaultLoweringPass(const GCStrategy &C);
46  static bool NeedsCustomLoweringPass(const GCStrategy &C);
47  static bool CouldBecomeSafePoint(Instruction *I);
48  bool PerformDefaultLowering(Function &F, GCStrategy &Coll);
49  static bool InsertRootInitializers(Function &F,
50  AllocaInst **Roots, unsigned Count);
51 
52  public:
53  static char ID;
54 
55  LowerIntrinsics();
56  const char *getPassName() const;
57  void getAnalysisUsage(AnalysisUsage &AU) const;
58 
59  bool doInitialization(Module &M);
60  bool runOnFunction(Function &F);
61  };
62 
63 
64  /// GCMachineCodeAnalysis - This is a target-independent pass over the machine
65  /// function representation to identify safe points for the garbage collector
66  /// in the machine code. It inserts labels at safe points and populates a
67  /// GCMetadata record for each function.
68  class GCMachineCodeAnalysis : public MachineFunctionPass {
69  const TargetMachine *TM;
70  GCFunctionInfo *FI;
71  MachineModuleInfo *MMI;
72  const TargetInstrInfo *TII;
73 
74  void FindSafePoints(MachineFunction &MF);
75  void VisitCallPoint(MachineBasicBlock::iterator MI);
76  MCSymbol *InsertLabel(MachineBasicBlock &MBB,
78  DebugLoc DL) const;
79 
80  void FindStackOffsets(MachineFunction &MF);
81 
82  public:
83  static char ID;
84 
85  GCMachineCodeAnalysis();
86  void getAnalysisUsage(AnalysisUsage &AU) const;
87 
88  bool runOnMachineFunction(MachineFunction &MF);
89  };
90 
91 }
92 
93 // -----------------------------------------------------------------------------
94 
96  NeededSafePoints(0),
97  CustomReadBarriers(false),
98  CustomWriteBarriers(false),
99  CustomRoots(false),
100  CustomSafePoints(false),
101  InitRoots(true),
102  UsesMetadata(false)
103 {}
104 
106  for (iterator I = begin(), E = end(); I != E; ++I)
107  delete *I;
108 
109  Functions.clear();
110 }
111 
113 
115  dbgs() << "gc " << getName() << " must override performCustomLowering.\n";
116  llvm_unreachable("must override performCustomLowering");
117 }
118 
119 
121  dbgs() << "gc " << getName() << " must override findCustomSafePoints.\n";
122  llvm_unreachable(0);
123 }
124 
125 
127  GCFunctionInfo *FI = new GCFunctionInfo(F, *this);
128  Functions.push_back(FI);
129  return FI;
130 }
131 
132 // -----------------------------------------------------------------------------
133 
134 INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering",
135  false, false)
137 INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false)
138 
140  return new LowerIntrinsics();
141 }
142 
143 char LowerIntrinsics::ID = 0;
144 
145 LowerIntrinsics::LowerIntrinsics()
146  : FunctionPass(ID) {
147  initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry());
148  }
149 
150 const char *LowerIntrinsics::getPassName() const {
151  return "Lower Garbage Collection Instructions";
152 }
153 
154 void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
155  FunctionPass::getAnalysisUsage(AU);
158 }
159 
160 /// doInitialization - If this module uses the GC intrinsics, find them now.
161 bool LowerIntrinsics::doInitialization(Module &M) {
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).
166  GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
167  assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?");
168  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
169  if (!I->isDeclaration() && I->hasGC())
170  MI->getFunctionInfo(*I); // Instantiate the GC strategy.
171 
172  bool MadeChange = false;
173  for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
174  if (NeedsCustomLoweringPass(**I))
175  if ((*I)->initializeCustomLowering(M))
176  MadeChange = true;
177 
178  return MadeChange;
179 }
180 
181 bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots,
182  unsigned Count) {
183  // Scroll past alloca instructions.
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))
191  if (AllocaInst *AI =
192  dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts()))
193  InitedRoots.insert(AI);
194 
195  // Add root initializers.
196  bool MadeChange = false;
197 
198  for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
199  if (!InitedRoots.count(*I)) {
200  StoreInst* SI = new StoreInst(ConstantPointerNull::get(cast<PointerType>(
201  cast<PointerType>((*I)->getType())->getElementType())),
202  *I);
203  SI->insertAfter(*I);
204  MadeChange = true;
205  }
206 
207  return MadeChange;
208 }
209 
210 bool LowerIntrinsics::NeedsDefaultLoweringPass(const GCStrategy &C) {
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 
218 bool LowerIntrinsics::NeedsCustomLoweringPass(const GCStrategy &C) {
219  // Custom lowering is only necessary if enabled for some action.
220  return C.customWriteBarrier()
221  || C.customReadBarrier()
222  || C.customRoots();
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) {
256  // Quick exit for functions that do not use GC.
257  if (!F.hasGC())
258  return false;
259 
260  GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F);
261  GCStrategy &S = FI.getStrategy();
262 
263  bool MadeChange = false;
264 
265  if (NeedsDefaultLoweringPass(S))
266  MadeChange |= PerformDefaultLowering(F, S);
267 
268  bool UseCustomLoweringPass = NeedsCustomLoweringPass(S);
269  if (UseCustomLoweringPass)
270  MadeChange |= S.performCustomLowering(F);
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 
278  return MadeChange;
279 }
280 
281 bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) {
282  bool LowerWr = !S.customWriteBarrier();
283  bool LowerRd = !S.customReadBarrier();
284  bool InitRoots = S.initializeRoots();
285 
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;) {
291  if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) {
292  Function *F = CI->getCalledFunction();
293  switch (F->getIntrinsicID()) {
294  case Intrinsic::gcwrite:
295  if (LowerWr) {
296  // Replace a write barrier with a simple store.
297  Value *St = new StoreInst(CI->getArgOperand(0),
298  CI->getArgOperand(2), CI);
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.
306  Value *Ld = new LoadInst(CI->getArgOperand(1), "", CI);
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>(
317  CI->getArgOperand(0)->stripPointerCasts()));
318  }
319  break;
320  default:
321  continue;
322  }
323 
324  MadeChange = true;
325  }
326  }
327  }
328 
329  if (Roots.size())
330  MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size());
331 
332  return MadeChange;
333 }
334 
335 // -----------------------------------------------------------------------------
336 
339 
340 INITIALIZE_PASS(GCMachineCodeAnalysis, "gc-analysis",
341  "Analyze Machine Code For Garbage Collection", false, false)
342 
343 GCMachineCodeAnalysis::GCMachineCodeAnalysis()
344  : MachineFunctionPass(ID) {}
345 
346 void GCMachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
347  MachineFunctionPass::getAnalysisUsage(AU);
348  AU.setPreservesAll();
351 }
352 
353 MCSymbol *GCMachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
355  DebugLoc DL) const {
356  MCSymbol *Label = MBB.getParent()->getContext().CreateTempSymbol();
357  BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
358  return Label;
359 }
360 
361 void GCMachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
362  // Find the return address (next instruction), too, so as to bracket the call
363  // instruction.
365  ++RAI;
366 
368  MCSymbol* Label = InsertLabel(*CI->getParent(), CI, CI->getDebugLoc());
369  FI->addSafePoint(GC::PreCall, Label, CI->getDebugLoc());
370  }
371 
373  MCSymbol* Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc());
374  FI->addSafePoint(GC::PostCall, Label, CI->getDebugLoc());
375  }
376 }
377 
378 void GCMachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
379  for (MachineFunction::iterator BBI = MF.begin(),
380  BBE = MF.end(); BBI != BBE; ++BBI)
381  for (MachineBasicBlock::iterator MI = BBI->begin(),
382  ME = BBI->end(); MI != ME; ++MI)
383  if (MI->isCall())
384  VisitCallPoint(MI);
385 }
386 
387 void GCMachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
388  const TargetFrameLowering *TFI = TM->getFrameLowering();
389  assert(TFI && "TargetRegisterInfo not available!");
390 
392  RI != FI->roots_end();) {
393  // If the root references a dead object, no need to keep it.
394  if (MF.getFrameInfo()->isDeadObjectIndex(RI->Num)) {
395  RI = FI->removeStackRoot(RI);
396  } else {
397  RI->StackOffset = TFI->getFrameIndexOffset(MF, RI->Num);
398  ++RI;
399  }
400  }
401 }
402 
403 bool GCMachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
404  // Quick exit for functions that do not use GC.
405  if (!MF.getFunction()->hasGC())
406  return false;
407 
408  FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction());
409  if (!FI->getStrategy().needsSafePoints())
410  return false;
411 
412  TM = &MF.getTarget();
413  MMI = &getAnalysis<MachineModuleInfo>();
414  TII = TM->getInstrInfo();
415 
416  // Find the size of the stack frame.
418 
419  // Find all safe points.
420  if (FI->getStrategy().customSafePoints()) {
421  FI->getStrategy().findCustomSafePoints(*FI, MF);
422  } else {
423  FindSafePoints(MF);
424  }
425 
426  // Find the stack offsets for all roots.
427  FindStackOffsets(MF);
428 
429  return false;
430 }
const MachineFunction * getParent() const
AnalysisUsage & addPreserved()
virtual bool performCustomLowering(Function &F)
Definition: GCStrategy.cpp:114
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
iterator end()
Definition: Function.h:397
virtual bool findCustomSafePoints(GCFunctionInfo &FI, MachineFunction &MF)
Definition: GCStrategy.cpp:120
bool insert(PtrType Ptr)
Definition: SmallPtrSet.h:253
F(f)
const Function * getFunction() const
virtual ~GCStrategy()
Definition: GCStrategy.cpp:105
iterator begin()
Definition: BasicBlock.h:193
uint64_t getStackSize() const
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:167
INITIALIZE_PASS_BEGIN(LowerIntrinsics,"gc-lowering","GC Lowering", false, false) FunctionPass *llvm
Definition: GCStrategy.cpp:134
MCSymbol * CreateTempSymbol()
Definition: MCContext.cpp:165
const HexagonInstrInfo * TII
FunctionPass * createGCLoweringPass()
#define llvm_unreachable(msg)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:172
iterator end() const
Definition: GCMetadata.h:191
GCFunctionInfo & getFunctionInfo(const Function &F)
Definition: GCMetadata.cpp:90
void initializeLowerIntrinsicsPass(PassRegistry &)
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
#define false
Definition: ConvertUTF.c:64
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
Definition: SmallPtrSet.h:264
list_type::const_iterator iterator
Definition: GCMetadata.h:176
const std::string & getName() const
Definition: GCStrategy.h:85
INITIALIZE_PASS(GCMachineCodeAnalysis,"gc-analysis","Analyze Machine Code For Garbage Collection", false, false) GCMachineCodeAnalysis
Definition: GCStrategy.cpp:340
iterator begin()
Definition: GCStrategy.h:134
void replaceAllUsesWith(Value *V)
Definition: Value.cpp:303
MCContext & getContext() const
void takeName(Value *V)
Definition: Value.cpp:239
iterator begin()
Definition: Function.h:395
bool customReadBarrier() const
Definition: GCStrategy.h:111
bundle_iterator< MachineInstr, instr_iterator > iterator
#define true
Definition: ConvertUTF.c:65
roots_iterator roots_end()
Definition: GCMetadata.h:151
unsigned getIntrinsicID() const LLVM_READONLY
Definition: Function.cpp:371
bool initializeRoots() const
initializeRoots - If set, gcroot intrinsics should initialize their
Definition: GCStrategy.h:126
Instr is the return address of a call.
Definition: GCMetadata.h:54
roots_iterator removeStackRoot(roots_iterator position)
removeStackRoot - Removes a root.
Definition: GCMetadata.h:126
GCStrategy & getStrategy()
Definition: GCMetadata.h:116
list_type::iterator iterator
Definition: GCStrategy.h:58
bool needsSafePoint(GC::PointKind Kind) const
needsSafePoint(Kind) - True if the given kind of safe point is
Definition: GCStrategy.h:99
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
bool needsSafePoints() const
needsSafePoitns - True if safe points of any kind are required. By
Definition: GCStrategy.h:93
roots_iterator roots_begin()
Definition: GCMetadata.h:150
bool isDeadObjectIndex(int ObjectIdx) const
bool customRoots() const
Definition: GCStrategy.h:116
virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const
iterator end()
Definition: GCStrategy.h:135
MachineFrameInfo * getFrameInfo()
const BasicBlock & getEntryBlock() const
Definition: Function.h:380
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
bool hasGC() const
Definition: Function.cpp:310
iterator begin() const
Definition: GCMetadata.h:190
char & GCMachineCodeAnalysisID
Definition: GCStrategy.cpp:338
iterator end()
Definition: Module.h:533
void insertAfter(Instruction *InsertPos)
Definition: Instruction.cpp:84
bool customWriteBarrier() const
Definition: GCStrategy.h:106
#define I(x, y, z)
Definition: MD5.cpp:54
iterator begin()
Definition: Module.h:531
const TargetMachine & getTarget() const
Instr is a call instruction.
Definition: GCMetadata.h:53
GCFunctionInfo * insertFunctionInfo(const Function &F)
Definition: GCStrategy.cpp:126
void setFrameSize(uint64_t S)
Definition: GCMetadata.h:140
bool customSafePoints() const
Definition: GCStrategy.h:121
LLVM Value Representation.
Definition: Value.h:66
BasicBlockListType::iterator iterator
void addSafePoint(GC::PointKind Kind, MCSymbol *Label, DebugLoc DL)
Definition: GCMetadata.h:133
std::vector< GCRoot >::iterator roots_iterator
Definition: GCMetadata.h:86
virtual bool initializeCustomLowering(Module &F)
Definition: GCStrategy.cpp:112