1 //===-- ErlangGC.cpp - Erlang/OTP GC strategy -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Erlang/OTP runtime-compatible garbage collector
11 // (e.g. defines safe points, root initialization etc.)
13 // The frametable emitter is in ErlangGCPrinter.cpp.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/CodeGen/GCs.h"
18 #include "llvm/CodeGen/GCStrategy.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetSubtargetInfo.h"
30 class ErlangGC : public GCStrategy {
31 MCSymbol *InsertLabel(MachineBasicBlock &MBB,
32 MachineBasicBlock::iterator MI,
36 bool findCustomSafePoints(GCFunctionInfo &FI, MachineFunction &MF) override;
41 static GCRegistry::Add<ErlangGC>
42 X("erlang", "erlang-compatible garbage collector");
44 void llvm::linkErlangGC() { }
46 ErlangGC::ErlangGC() {
48 NeededSafePoints = 1 << GC::PostCall;
51 CustomSafePoints = true;
54 MCSymbol *ErlangGC::InsertLabel(MachineBasicBlock &MBB,
55 MachineBasicBlock::iterator MI,
57 const TargetInstrInfo *TII = MBB.getParent()->getSubtarget().getInstrInfo();
58 MCSymbol *Label = MBB.getParent()->getContext().CreateTempSymbol();
59 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
63 bool ErlangGC::findCustomSafePoints(GCFunctionInfo &FI, MachineFunction &MF) {
64 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end(); BBI != BBE;
66 for (MachineBasicBlock::iterator MI = BBI->begin(), ME = BBI->end();
69 if (MI->getDesc().isCall()) {
71 // Do not treat tail call sites as safe points.
72 if (MI->getDesc().isTerminator())
75 /* Code copied from VisitCallPoint(...) */
76 MachineBasicBlock::iterator RAI = MI; ++RAI;
77 MCSymbol* Label = InsertLabel(*MI->getParent(), RAI, MI->getDebugLoc());
78 FI.addSafePoint(GC::PostCall, Label, MI->getDebugLoc());