2015-01-16 03:29:42 +08:00
|
|
|
//===-- GCRootLowering.cpp - Garbage collection infrastructure ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the lowering for the gc.root mechanism.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-01-17 04:07:33 +08:00
|
|
|
#include "llvm/CodeGen/GCMetadata.h"
|
2015-02-13 17:09:03 +08:00
|
|
|
#include "llvm/CodeGen/GCStrategy.h"
|
2015-01-16 03:29:42 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetFrameLowering.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-01-16 03:39:17 +08:00
|
|
|
/// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
|
|
|
|
/// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
|
|
|
|
/// directed by the GCStrategy. It also performs automatic root initialization
|
|
|
|
/// and custom intrinsic lowering.
|
|
|
|
class LowerIntrinsics : public FunctionPass {
|
|
|
|
bool PerformDefaultLowering(Function &F, GCStrategy &Coll);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
LowerIntrinsics();
|
|
|
|
const char *getPassName() const override;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
|
|
|
|
bool doInitialization(Module &M) override;
|
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// GCMachineCodeAnalysis - 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 a
|
|
|
|
/// GCMetadata record for each function.
|
|
|
|
class GCMachineCodeAnalysis : public MachineFunctionPass {
|
|
|
|
GCFunctionInfo *FI;
|
|
|
|
MachineModuleInfo *MMI;
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
|
|
|
|
void FindSafePoints(MachineFunction &MF);
|
|
|
|
void VisitCallPoint(MachineBasicBlock::iterator MI);
|
|
|
|
MCSymbol *InsertLabel(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
|
|
|
|
DebugLoc DL) const;
|
|
|
|
|
|
|
|
void FindStackOffsets(MachineFunction &MF);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
GCMachineCodeAnalysis();
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2015-01-16 03:29:42 +08:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2015-01-16 03:39:17 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering", false,
|
|
|
|
false)
|
2015-01-16 03:29:42 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
|
|
|
|
INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false)
|
|
|
|
|
2015-01-16 03:39:17 +08:00
|
|
|
FunctionPass *llvm::createGCLoweringPass() { return new LowerIntrinsics(); }
|
2015-01-16 03:29:42 +08:00
|
|
|
|
|
|
|
char LowerIntrinsics::ID = 0;
|
|
|
|
|
2015-01-16 03:39:17 +08:00
|
|
|
LowerIntrinsics::LowerIntrinsics() : FunctionPass(ID) {
|
|
|
|
initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2015-01-16 03:29:42 +08:00
|
|
|
|
|
|
|
const char *LowerIntrinsics::getPassName() const {
|
|
|
|
return "Lower Garbage Collection Instructions";
|
|
|
|
}
|
|
|
|
|
|
|
|
void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
FunctionPass::getAnalysisUsage(AU);
|
|
|
|
AU.addRequired<GCModuleInfo>();
|
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
|
|
|
}
|
|
|
|
|
2015-01-16 03:49:25 +08:00
|
|
|
static bool NeedsDefaultLoweringPass(const GCStrategy &C) {
|
|
|
|
// Default lowering is necessary only if read or write barriers have a default
|
|
|
|
// action. The default for roots is no action.
|
|
|
|
return !C.customWriteBarrier() || !C.customReadBarrier() ||
|
|
|
|
C.initializeRoots();
|
|
|
|
}
|
|
|
|
|
2015-01-16 03:29:42 +08:00
|
|
|
/// doInitialization - If this module uses the GC intrinsics, find them now.
|
|
|
|
bool LowerIntrinsics::doInitialization(Module &M) {
|
|
|
|
GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
|
|
|
|
assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?");
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
|
|
if (!I->isDeclaration() && I->hasGC())
|
|
|
|
MI->getFunctionInfo(*I); // Instantiate the GC strategy.
|
|
|
|
|
2015-01-29 03:28:03 +08:00
|
|
|
return false;
|
2015-01-16 03:29:42 +08:00
|
|
|
}
|
|
|
|
|
2015-01-16 03:49:25 +08:00
|
|
|
/// CouldBecomeSafePoint - Predicate to conservatively determine whether the
|
|
|
|
/// instruction could introduce a safe point.
|
|
|
|
static bool 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())
|
2015-05-21 01:16:39 +08:00
|
|
|
if (Intrinsic::ID IID = F->getIntrinsicID())
|
2015-01-16 03:49:25 +08:00
|
|
|
if (IID == Intrinsic::gcroot)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool InsertRootInitializers(Function &F, AllocaInst **Roots,
|
|
|
|
unsigned Count) {
|
2015-01-16 03:29:42 +08:00
|
|
|
// Scroll past alloca instructions.
|
|
|
|
BasicBlock::iterator IP = F.getEntryBlock().begin();
|
2015-01-16 03:39:17 +08:00
|
|
|
while (isa<AllocaInst>(IP))
|
|
|
|
++IP;
|
2015-01-16 03:29:42 +08:00
|
|
|
|
|
|
|
// Search for initializers in the initial BB.
|
2015-01-16 03:39:17 +08:00
|
|
|
SmallPtrSet<AllocaInst *, 16> InitedRoots;
|
2015-10-10 02:44:40 +08:00
|
|
|
for (; !CouldBecomeSafePoint(&*IP); ++IP)
|
2015-01-16 03:29:42 +08:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(IP))
|
|
|
|
if (AllocaInst *AI =
|
2015-01-16 03:39:17 +08:00
|
|
|
dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts()))
|
2015-01-16 03:29:42 +08:00
|
|
|
InitedRoots.insert(AI);
|
|
|
|
|
|
|
|
// Add root initializers.
|
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
|
|
|
|
if (!InitedRoots.count(*I)) {
|
2015-01-16 03:39:17 +08:00
|
|
|
StoreInst *SI = new StoreInst(
|
|
|
|
ConstantPointerNull::get(cast<PointerType>(
|
|
|
|
cast<PointerType>((*I)->getType())->getElementType())),
|
|
|
|
*I);
|
2015-01-16 03:29:42 +08:00
|
|
|
SI->insertAfter(*I);
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 functions that do not use GC.
|
|
|
|
if (!F.hasGC())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F);
|
|
|
|
GCStrategy &S = FI.getStrategy();
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
if (NeedsDefaultLoweringPass(S))
|
|
|
|
MadeChange |= PerformDefaultLowering(F, S);
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) {
|
|
|
|
bool LowerWr = !S.customWriteBarrier();
|
|
|
|
bool LowerRd = !S.customReadBarrier();
|
|
|
|
bool InitRoots = S.initializeRoots();
|
|
|
|
|
2015-01-16 03:39:17 +08:00
|
|
|
SmallVector<AllocaInst *, 32> Roots;
|
2015-01-16 03:29:42 +08:00
|
|
|
|
|
|
|
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 (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) {
|
|
|
|
Function *F = CI->getCalledFunction();
|
|
|
|
switch (F->getIntrinsicID()) {
|
|
|
|
case Intrinsic::gcwrite:
|
|
|
|
if (LowerWr) {
|
|
|
|
// Replace a write barrier with a simple store.
|
2015-01-16 03:39:17 +08:00
|
|
|
Value *St =
|
|
|
|
new StoreInst(CI->getArgOperand(0), CI->getArgOperand(2), CI);
|
2015-01-16 03:29:42 +08:00
|
|
|
CI->replaceAllUsesWith(St);
|
|
|
|
CI->eraseFromParent();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Intrinsic::gcread:
|
|
|
|
if (LowerRd) {
|
|
|
|
// Replace a read barrier with a simple load.
|
|
|
|
Value *Ld = new LoadInst(CI->getArgOperand(1), "", CI);
|
|
|
|
Ld->takeName(CI);
|
|
|
|
CI->replaceAllUsesWith(Ld);
|
|
|
|
CI->eraseFromParent();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Intrinsic::gcroot:
|
|
|
|
if (InitRoots) {
|
|
|
|
// Initialize the GC root, but do not delete the intrinsic. The
|
|
|
|
// backend needs the intrinsic to flag the stack slot.
|
2015-01-16 03:39:17 +08:00
|
|
|
Roots.push_back(
|
|
|
|
cast<AllocaInst>(CI->getArgOperand(0)->stripPointerCasts()));
|
2015-01-16 03:29:42 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Roots.size())
|
|
|
|
MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size());
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
char GCMachineCodeAnalysis::ID = 0;
|
|
|
|
char &llvm::GCMachineCodeAnalysisID = GCMachineCodeAnalysis::ID;
|
|
|
|
|
|
|
|
INITIALIZE_PASS(GCMachineCodeAnalysis, "gc-analysis",
|
|
|
|
"Analyze Machine Code For Garbage Collection", false, false)
|
|
|
|
|
2015-01-16 03:39:17 +08:00
|
|
|
GCMachineCodeAnalysis::GCMachineCodeAnalysis() : MachineFunctionPass(ID) {}
|
2015-01-16 03:29:42 +08:00
|
|
|
|
|
|
|
void GCMachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<MachineModuleInfo>();
|
|
|
|
AU.addRequired<GCModuleInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
MCSymbol *GCMachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
DebugLoc DL) const {
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *Label = MBB.getParent()->getContext().createTempSymbol();
|
2015-01-16 03:29:42 +08:00
|
|
|
BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
|
|
|
|
return Label;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GCMachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
|
|
|
|
// Find the return address (next instruction), too, so as to bracket the call
|
|
|
|
// instruction.
|
|
|
|
MachineBasicBlock::iterator RAI = CI;
|
|
|
|
++RAI;
|
|
|
|
|
|
|
|
if (FI->getStrategy().needsSafePoint(GC::PreCall)) {
|
2015-01-16 03:39:17 +08:00
|
|
|
MCSymbol *Label = InsertLabel(*CI->getParent(), CI, CI->getDebugLoc());
|
2015-01-16 03:29:42 +08:00
|
|
|
FI->addSafePoint(GC::PreCall, Label, CI->getDebugLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FI->getStrategy().needsSafePoint(GC::PostCall)) {
|
2015-01-16 03:39:17 +08:00
|
|
|
MCSymbol *Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc());
|
2015-01-16 03:29:42 +08:00
|
|
|
FI->addSafePoint(GC::PostCall, Label, CI->getDebugLoc());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GCMachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
|
2015-01-16 03:39:17 +08:00
|
|
|
for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end(); BBI != BBE;
|
|
|
|
++BBI)
|
|
|
|
for (MachineBasicBlock::iterator MI = BBI->begin(), ME = BBI->end();
|
|
|
|
MI != ME; ++MI)
|
2015-01-17 03:33:28 +08:00
|
|
|
if (MI->isCall()) {
|
|
|
|
// Do not treat tail or sibling call sites as safe points. This is
|
|
|
|
// legal since any arguments passed to the callee which live in the
|
|
|
|
// remnants of the callers frame will be owned and updated by the
|
|
|
|
// callee if required.
|
|
|
|
if (MI->isTerminator())
|
|
|
|
continue;
|
2015-01-16 03:29:42 +08:00
|
|
|
VisitCallPoint(MI);
|
2015-01-17 03:33:28 +08:00
|
|
|
}
|
2015-01-16 03:29:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GCMachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
|
2015-02-21 02:44:15 +08:00
|
|
|
const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
|
2015-01-16 03:29:42 +08:00
|
|
|
assert(TFI && "TargetRegisterInfo not available!");
|
|
|
|
|
|
|
|
for (GCFunctionInfo::roots_iterator RI = FI->roots_begin();
|
|
|
|
RI != FI->roots_end();) {
|
|
|
|
// If the root references a dead object, no need to keep it.
|
|
|
|
if (MF.getFrameInfo()->isDeadObjectIndex(RI->Num)) {
|
|
|
|
RI = FI->removeStackRoot(RI);
|
|
|
|
} else {
|
2015-08-15 10:32:35 +08:00
|
|
|
unsigned FrameReg; // FIXME: surely GCRoot ought to store the
|
|
|
|
// register that the offset is from?
|
|
|
|
RI->StackOffset = TFI->getFrameIndexReference(MF, RI->Num, FrameReg);
|
2015-01-16 03:29:42 +08:00
|
|
|
++RI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GCMachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
// Quick exit for functions that do not use GC.
|
|
|
|
if (!MF.getFunction()->hasGC())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction());
|
|
|
|
MMI = &getAnalysis<MachineModuleInfo>();
|
2015-02-21 02:44:15 +08:00
|
|
|
TII = MF.getSubtarget().getInstrInfo();
|
2015-01-16 03:29:42 +08:00
|
|
|
|
2015-04-02 13:00:40 +08:00
|
|
|
// Find the size of the stack frame. There may be no correct static frame
|
|
|
|
// size, we use UINT64_MAX to represent this.
|
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
|
|
|
|
const bool DynamicFrameSize = MFI->hasVarSizedObjects() ||
|
|
|
|
RegInfo->needsStackRealignment(MF);
|
|
|
|
FI->setFrameSize(DynamicFrameSize ? UINT64_MAX : MFI->getStackSize());
|
2015-01-16 03:29:42 +08:00
|
|
|
|
|
|
|
// Find all safe points.
|
2015-04-02 13:00:40 +08:00
|
|
|
if (FI->getStrategy().needsSafePoints())
|
|
|
|
FindSafePoints(MF);
|
2015-01-16 03:29:42 +08:00
|
|
|
|
2015-04-02 13:00:40 +08:00
|
|
|
// Find the concrete stack offsets for all roots (stack slots)
|
2015-01-16 03:29:42 +08:00
|
|
|
FindStackOffsets(MF);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|