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