"...lib/CodeGen/git@repo.hca.bsc.es:rferrer/llvm-epi-0.8.git" did not exist on "95531b696901d32c2cc98a988dc91d69373c70df"
Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//===-- Collector.cpp - Garbage collection infrastructure -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Gordon Henriksen and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements target- and collector-independent garbage collection
// infrastructure.
//
//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/Collector.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/Module.h"
#include "llvm/PassManager.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/Target/TargetFrameInfo.h"
#include "llvm/Target/TargetInstrInfo.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Support/Compiler.h"
using namespace llvm;
namespace {
/// This pass rewrites calls to the llvm.gcread or llvm.gcwrite intrinsics,
/// replacing them with simple loads and stores as directed by the Collector.
/// This is useful for most garbage collectors.
class VISIBILITY_HIDDEN LowerIntrinsics : public FunctionPass {
const Collector &Coll;
/// GCRootInt, GCReadInt, GCWriteInt - The function prototypes for the
/// llvm.gc* intrinsics.
Function *GCRootInt, *GCReadInt, *GCWriteInt;
static bool CouldBecomeSafePoint(Instruction *I);
static void InsertRootInitializers(Function &F,
AllocaInst **Roots, unsigned Count);
public:
static char ID;
LowerIntrinsics(const Collector &GC);
const char *getPassName() const;
bool doInitialization(Module &M);
bool runOnFunction(Function &F);
};
/// This is a target-independent pass over the machine function representation
/// to identify safe points for the garbage collector in the machine code. It
/// inserts labels at safe points and populates the GCInfo class.
class VISIBILITY_HIDDEN MachineCodeAnalysis : public MachineFunctionPass {
const Collector &Coll;
const TargetMachine &Targ;
CollectorMetadata *MD;
MachineModuleInfo *MMI;
const TargetInstrInfo *TII;
MachineFrameInfo *MFI;
void FindSafePoints(MachineFunction &MF);
void VisitCallPoint(MachineBasicBlock::iterator MI);
unsigned InsertLabel(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI) const;
void FindStackOffsets(MachineFunction &MF);
public:
static char ID;
MachineCodeAnalysis(const Collector &C, const TargetMachine &T);
const char *getPassName() const;
void getAnalysisUsage(AnalysisUsage &AU) const;
bool runOnMachineFunction(MachineFunction &MF);
};
}
// -----------------------------------------------------------------------------
const Collector *llvm::TheCollector = 0;
Collector::Collector() :
NeededSafePoints(0),
CustomReadBarriers(false),
CustomWriteBarriers(false),
CustomRoots(false),
InitRoots(true)
{}
Collector::~Collector() {}
void Collector::addLoweringPasses(FunctionPassManager &PM) const {
if (NeedsDefaultLoweringPass())
PM.add(new LowerIntrinsics(*this));
if (NeedsCustomLoweringPass())
PM.add(createCustomLoweringPass());
}
void Collector::addLoweringPasses(PassManager &PM) const {
if (NeedsDefaultLoweringPass())
PM.add(new LowerIntrinsics(*this));
if (NeedsCustomLoweringPass())
PM.add(createCustomLoweringPass());
}
void Collector::addGenericMachineCodePass(FunctionPassManager &PM,
const TargetMachine &TM,
bool Fast) const {
if (needsSafePoints())
PM.add(new MachineCodeAnalysis(*this, TM));
}
bool Collector::NeedsDefaultLoweringPass() const {
// Default lowering is necessary only if read or write barriers have a default
// action. The default for roots is no action.
return !customWriteBarrier()
|| !customReadBarrier()
|| initializeRoots();
}
bool Collector::NeedsCustomLoweringPass() const {
// Custom lowering is only necessary if enabled for some action.
return customWriteBarrier()
|| customReadBarrier()
|| customRoots();
}
Pass *Collector::createCustomLoweringPass() const {
cerr << "Collector must override createCustomLoweringPass.\n";
abort();
return 0;
}
void Collector::beginAssembly(Module &M, std::ostream &OS, AsmPrinter &AP,
const TargetAsmInfo &TAI) const {
// Default is no action.
}
void Collector::finishAssembly(Module &M, CollectorModuleMetadata &CMM,
std::ostream &OS, AsmPrinter &AP,
const TargetAsmInfo &TAI) const {
// Default is no action.
}
// -----------------------------------------------------------------------------
char LowerIntrinsics::ID = 0;
LowerIntrinsics::LowerIntrinsics(const Collector &C)
: FunctionPass((intptr_t)&ID), Coll(C),
GCRootInt(0), GCReadInt(0), GCWriteInt(0) {}
const char *LowerIntrinsics::getPassName() const {
return "Lower Garbage Collection Instructions";
}
/// doInitialization - If this module uses the GC intrinsics, find them now. If
/// not, this pass does not do anything.
bool LowerIntrinsics::doInitialization(Module &M) {
GCReadInt = M.getFunction("llvm.gcread");
GCWriteInt = M.getFunction("llvm.gcwrite");
GCRootInt = M.getFunction("llvm.gcroot");
return false;
}
void LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots,
unsigned Count) {
// Scroll past alloca instructions.
BasicBlock::iterator IP = F.getEntryBlock().begin();
while (isa<AllocaInst>(IP)) ++IP;
// Search for initializers in the initial BB.
SmallPtrSet<AllocaInst*,16> InitedRoots;
for (; !CouldBecomeSafePoint(IP); ++IP)
if (StoreInst *SI = dyn_cast<StoreInst>(IP))
if (AllocaInst *AI = dyn_cast<AllocaInst>(
IntrinsicInst::StripPointerCasts(SI->getOperand(1))))
InitedRoots.insert(AI);
// Add root initializers.
for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
if (!InitedRoots.count(*I))
new StoreInst(ConstantPointerNull::get(cast<PointerType>(
cast<PointerType>((*I)->getType())->getElementType())),
*I, IP);
}
/// CouldBecomeSafePoint - Predicate to conservatively determine whether the
/// instruction could introduce a safe point.
bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) {
// The natural definition of instructions which could introduce safe points
// are:
//
// - call, invoke (AfterCall, BeforeCall)
// - phis (Loops)
// - invoke, ret, unwind (Exit)
//
// However, instructions as seemingly inoccuous as arithmetic can become
// libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead
// it is necessary to take a conservative approach.
if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) ||
isa<StoreInst>(I) || isa<LoadInst>(I))
return false;
// llvm.gcroot is safe because it doesn't do anything at runtime.
if (CallInst *CI = dyn_cast<CallInst>(I))
if (Function *F = CI->getCalledFunction())
if (unsigned IID = F->getIntrinsicID())
if (IID == Intrinsic::gcroot)
return false;
return true;
}
/// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores.
/// Leave gcroot intrinsics; the code generator needs to see those.
bool LowerIntrinsics::runOnFunction(Function &F) {
// Quick exit for programs that do not declare the intrinsics.
if (!GCReadInt && !GCWriteInt && !GCRootInt) return false;
bool LowerWr = !Coll.customWriteBarrier();
bool LowerRd = !Coll.customReadBarrier();
bool InitRoots = Coll.initializeRoots();
SmallVector<AllocaInst*,32> Roots;
bool MadeChange = false;
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) {
if (CallInst *CI = dyn_cast<CallInst>(II++)) {
Function *F = CI->getCalledFunction();
if (F == GCWriteInt && LowerWr) {
// Replace a write barrier with a simple store.
Value *St = new StoreInst(CI->getOperand(1), CI->getOperand(3), CI);
CI->replaceAllUsesWith(St);
CI->eraseFromParent();
} else if (F == GCReadInt && LowerRd) {
// Replace a read barrier with a simple load.
Value *Ld = new LoadInst(CI->getOperand(2), "", CI);
Ld->takeName(CI);
CI->replaceAllUsesWith(Ld);
CI->eraseFromParent();
} else if (F == GCRootInt && InitRoots) {
// Initialize the GC root, but do not delete the intrinsic. The
// backend needs the intrinsic to flag the stack slot.
Roots.push_back(cast<AllocaInst>(
IntrinsicInst::StripPointerCasts(CI->getOperand(1))));
} else {
continue;
}
MadeChange = true;
}
}
}
if (Roots.size())
InsertRootInitializers(F, Roots.begin(), Roots.size());
return MadeChange;
}
// -----------------------------------------------------------------------------
char MachineCodeAnalysis::ID = 0;
MachineCodeAnalysis::MachineCodeAnalysis(const Collector &C, const TargetMachine &T)
: MachineFunctionPass(intptr_t(&ID)), Coll(C), Targ(T) {}
const char *MachineCodeAnalysis::getPassName() const {
return "Analyze Machine Code For Garbage Collection";
}
void MachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
MachineFunctionPass::getAnalysisUsage(AU);
AU.setPreservesAll();
AU.addRequired<MachineModuleInfo>();
AU.addRequired<CollectorModuleMetadata>();
}
unsigned MachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI) const {
unsigned Label = MMI->NextLabelID();
BuildMI(MBB, MI, TII->get(TargetInstrInfo::LABEL)).addImm(Label);
return Label;
}
void MachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
// Find the return address (next instruction), too, so as to bracket the call
// instruction.
MachineBasicBlock::iterator RAI = CI;
++RAI;
if (Coll.needsSafePoint(GC::PreCall))
MD->addSafePoint(GC::PreCall, InsertLabel(*CI->getParent(), CI));
if (Coll.needsSafePoint(GC::PostCall))
MD->addSafePoint(GC::PostCall, InsertLabel(*CI->getParent(), RAI));
}
void MachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
for (MachineFunction::iterator BBI = MF.begin(),
BBE = MF.end(); BBI != BBE; ++BBI)
for (MachineBasicBlock::iterator MI = BBI->begin(),
ME = BBI->end(); MI != ME; ++MI)
if (TII->isCall(MI->getOpcode()))
VisitCallPoint(*MI);
}
void MachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
uint64_t StackSize = MFI->getStackSize();
uint64_t OffsetAdjustment = MFI->getOffsetAdjustment();
uint64_t OffsetOfLocalArea = Targ.getFrameInfo()->getOffsetOfLocalArea();
for (CollectorMetadata::roots_iterator RI = MD->roots_begin(),
RE = MD->roots_end(); RI != RE; ++RI)
RI->StackOffset = MFI->getObjectOffset(RI->Num) + StackSize
- OffsetOfLocalArea + OffsetAdjustment;
}
bool MachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
if (!Coll.needsSafePoints())
return false;
MD = getAnalysis<CollectorModuleMetadata>().get(MF.getFunction());
MMI = &getAnalysis<MachineModuleInfo>();
TII = MF.getTarget().getInstrInfo();
MFI = MF.getFrameInfo();
// Find the size of the stack frame.
MD->setFrameSize(MFI->getStackSize());
// Find all safe points.
FindSafePoints(MF);
// Find the stack offsets for all roots.
FindStackOffsets(MF);
return false;
}