blob: ed2e3d8cf6e8f72426ec774d9981723c7e4508d1 [file] [log] [blame]
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +00001//===-- ErlangGC.cpp - Erlang/OTP GC strategy -------------------*- C++ -*-===//
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 Erlang/OTP runtime-compatible garbage collector
11// (e.g. defines safe points, root initialization etc.)
12//
13// The frametable emitter is in ErlangGCPrinter.cpp.
14//
15//===----------------------------------------------------------------------===//
16
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"
Eric Christopherd9134482014-08-04 21:25:23 +000024#include "llvm/Target/TargetSubtargetInfo.h"
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000025
26using namespace llvm;
27
28namespace {
29
30 class ErlangGC : public GCStrategy {
31 MCSymbol *InsertLabel(MachineBasicBlock &MBB,
32 MachineBasicBlock::iterator MI,
33 DebugLoc DL) const;
34 public:
35 ErlangGC();
Craig Topper4584cd52014-03-07 09:26:03 +000036 bool findCustomSafePoints(GCFunctionInfo &FI, MachineFunction &MF) override;
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000037 };
38
39}
40
41static GCRegistry::Add<ErlangGC>
42X("erlang", "erlang-compatible garbage collector");
43
44void llvm::linkErlangGC() { }
45
46ErlangGC::ErlangGC() {
47 InitRoots = false;
48 NeededSafePoints = 1 << GC::PostCall;
49 UsesMetadata = true;
50 CustomRoots = false;
51 CustomSafePoints = true;
52}
53
54MCSymbol *ErlangGC::InsertLabel(MachineBasicBlock &MBB,
55 MachineBasicBlock::iterator MI,
56 DebugLoc DL) const {
Eric Christopherd9134482014-08-04 21:25:23 +000057 const TargetInstrInfo *TII =
58 MBB.getParent()->getTarget().getSubtargetImpl()->getInstrInfo();
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000059 MCSymbol *Label = MBB.getParent()->getContext().CreateTempSymbol();
60 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
61 return Label;
62}
63
64bool ErlangGC::findCustomSafePoints(GCFunctionInfo &FI, MachineFunction &MF) {
65 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end(); BBI != BBE;
66 ++BBI)
67 for (MachineBasicBlock::iterator MI = BBI->begin(), ME = BBI->end();
68 MI != ME; ++MI)
69
70 if (MI->getDesc().isCall()) {
71
72 // Do not treat tail call sites as safe points.
73 if (MI->getDesc().isTerminator())
74 continue;
75
76 /* Code copied from VisitCallPoint(...) */
77 MachineBasicBlock::iterator RAI = MI; ++RAI;
78 MCSymbol* Label = InsertLabel(*MI->getParent(), RAI, MI->getDebugLoc());
79 FI.addSafePoint(GC::PostCall, Label, MI->getDebugLoc());
80 }
81
82 return false;
83}