freebsd-nq/lib/CodeGen/GCStrategy.cpp

406 lines
14 KiB
C++
Raw Normal View History

2009-06-02 17:52:33 +00:00
//===-- GCStrategy.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 target- and collector-independent garbage collection
// infrastructure.
//
// GCMachineCodeAnalysis identifies the GC safe points in the machine code.
// Roots are identified in SelectionDAGISel.
2009-06-02 17:52:33 +00:00
//
//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/GCStrategy.h"
#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"
2009-06-02 17:52:33 +00:00
#include "llvm/Target/TargetInstrInfo.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetRegisterInfo.h"
#include "llvm/Target/TargetSubtargetInfo.h"
2009-06-02 17:52:33 +00:00
using namespace llvm;
namespace {
2009-06-02 17:52:33 +00:00
/// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
/// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
2009-06-02 17:52:33 +00:00
/// directed by the GCStrategy. It also performs automatic root initialization
/// and custom intrinsic lowering.
2009-11-04 14:58:56 +00:00
class LowerIntrinsics : public FunctionPass {
2009-06-02 17:52:33 +00:00
static bool NeedsDefaultLoweringPass(const GCStrategy &C);
static bool NeedsCustomLoweringPass(const GCStrategy &C);
static bool CouldBecomeSafePoint(Instruction *I);
bool PerformDefaultLowering(Function &F, GCStrategy &Coll);
static bool InsertRootInitializers(Function &F,
AllocaInst **Roots, unsigned Count);
2009-06-02 17:52:33 +00:00
public:
static char ID;
2009-06-02 17:52:33 +00:00
LowerIntrinsics();
const char *getPassName() const override;
void getAnalysisUsage(AnalysisUsage &AU) const override;
bool doInitialization(Module &M) override;
bool runOnFunction(Function &F) override;
2009-06-02 17:52:33 +00:00
};
/// GCMachineCodeAnalysis - This is a target-independent pass over the machine
2009-06-02 17:52:33 +00:00
/// 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 {
2009-06-02 17:52:33 +00:00
const TargetMachine *TM;
GCFunctionInfo *FI;
MachineModuleInfo *MMI;
const TargetInstrInfo *TII;
2009-06-02 17:52:33 +00:00
void FindSafePoints(MachineFunction &MF);
void VisitCallPoint(MachineBasicBlock::iterator MI);
MCSymbol *InsertLabel(MachineBasicBlock &MBB,
2010-03-16 16:51:38 +00:00
MachineBasicBlock::iterator MI,
DebugLoc DL) const;
2009-06-02 17:52:33 +00:00
void FindStackOffsets(MachineFunction &MF);
2009-06-02 17:52:33 +00:00
public:
static char ID;
GCMachineCodeAnalysis();
void getAnalysisUsage(AnalysisUsage &AU) const override;
bool runOnMachineFunction(MachineFunction &MF) override;
2009-06-02 17:52:33 +00:00
};
2009-06-02 17:52:33 +00:00
}
// -----------------------------------------------------------------------------
GCStrategy::GCStrategy() :
UseStatepoints(false),
2009-06-02 17:52:33 +00:00
NeededSafePoints(0),
CustomReadBarriers(false),
CustomWriteBarriers(false),
CustomRoots(false),
CustomSafePoints(false),
2009-06-02 17:52:33 +00:00
InitRoots(true),
UsesMetadata(false)
{}
// -----------------------------------------------------------------------------
INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering",
false, false)
INITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false)
2009-06-02 17:52:33 +00:00
FunctionPass *llvm::createGCLoweringPass() {
return new LowerIntrinsics();
}
2009-06-02 17:52:33 +00:00
char LowerIntrinsics::ID = 0;
LowerIntrinsics::LowerIntrinsics()
: FunctionPass(ID) {
initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry());
}
2009-06-02 17:52:33 +00:00
const char *LowerIntrinsics::getPassName() const {
return "Lower Garbage Collection Instructions";
}
2009-06-02 17:52:33 +00:00
void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
FunctionPass::getAnalysisUsage(AU);
AU.addRequired<GCModuleInfo>();
AU.addPreserved<DominatorTreeWrapperPass>();
2009-06-02 17:52:33 +00:00
}
/// doInitialization - If this module uses the GC intrinsics, find them now.
bool LowerIntrinsics::doInitialization(Module &M) {
// FIXME: This is rather antisocial in the context of a JIT since it performs
// work against the entire module. But this cannot be done at
// runFunction time (initializeCustomLowering likely needs to change
// the module).
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.
2009-06-02 17:52:33 +00:00
bool MadeChange = false;
for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
if (NeedsCustomLoweringPass(**I))
if ((*I)->initializeCustomLowering(M))
MadeChange = true;
2009-06-02 17:52:33 +00:00
return MadeChange;
}
bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots,
2009-06-02 17:52:33 +00:00
unsigned Count) {
// Scroll past alloca instructions.
BasicBlock::iterator IP = F.getEntryBlock().begin();
while (isa<AllocaInst>(IP)) ++IP;
2009-06-02 17:52:33 +00:00
// 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>(SI->getOperand(1)->stripPointerCasts()))
InitedRoots.insert(AI);
2009-06-02 17:52:33 +00:00
// Add root initializers.
bool MadeChange = false;
2009-06-02 17:52:33 +00:00
for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
if (!InitedRoots.count(*I)) {
2010-05-04 16:11:02 +00:00
StoreInst* SI = new StoreInst(ConstantPointerNull::get(cast<PointerType>(
cast<PointerType>((*I)->getType())->getElementType())),
*I);
SI->insertAfter(*I);
2009-06-02 17:52:33 +00:00
MadeChange = true;
}
2009-06-02 17:52:33 +00:00
return MadeChange;
}
bool LowerIntrinsics::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();
}
bool LowerIntrinsics::NeedsCustomLoweringPass(const GCStrategy &C) {
// Custom lowering is only necessary if enabled for some action.
return C.customWriteBarrier()
|| C.customReadBarrier()
|| C.customRoots();
}
/// 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:
//
2009-06-02 17:52:33 +00:00
// - call, invoke (AfterCall, BeforeCall)
// - phis (Loops)
// - invoke, ret, unwind (Exit)
//
2009-06-02 17:52:33 +00:00
// 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.
2009-06-02 17:52:33 +00:00
if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) ||
isa<StoreInst>(I) || isa<LoadInst>(I))
return false;
2009-06-02 17:52:33 +00:00
// 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;
2009-06-02 17:52:33 +00:00
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 functions that do not use GC.
if (!F.hasGC())
return false;
2009-06-02 17:52:33 +00:00
GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F);
GCStrategy &S = FI.getStrategy();
2009-06-02 17:52:33 +00:00
bool MadeChange = false;
2009-06-02 17:52:33 +00:00
if (NeedsDefaultLoweringPass(S))
MadeChange |= PerformDefaultLowering(F, S);
bool UseCustomLoweringPass = NeedsCustomLoweringPass(S);
if (UseCustomLoweringPass)
2009-06-02 17:52:33 +00:00
MadeChange |= S.performCustomLowering(F);
// Custom lowering may modify the CFG, so dominators must be recomputed.
if (UseCustomLoweringPass) {
if (DominatorTreeWrapperPass *DTWP =
getAnalysisIfAvailable<DominatorTreeWrapperPass>())
DTWP->getDomTree().recalculate(F);
}
2009-06-02 17:52:33 +00:00
return MadeChange;
}
bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) {
bool LowerWr = !S.customWriteBarrier();
bool LowerRd = !S.customReadBarrier();
bool InitRoots = S.initializeRoots();
SmallVector<AllocaInst*, 32> Roots;
2009-06-02 17:52:33 +00: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.
Value *St = new StoreInst(CI->getArgOperand(0),
CI->getArgOperand(2), CI);
2009-06-02 17:52:33 +00:00
CI->replaceAllUsesWith(St);
CI->eraseFromParent();
}
break;
case Intrinsic::gcread:
if (LowerRd) {
// Replace a read barrier with a simple load.
2010-07-13 17:19:57 +00:00
Value *Ld = new LoadInst(CI->getArgOperand(1), "", CI);
2009-06-02 17:52:33 +00:00
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.
Roots.push_back(cast<AllocaInst>(
2010-07-13 17:19:57 +00:00
CI->getArgOperand(0)->stripPointerCasts()));
2009-06-02 17:52:33 +00:00
}
break;
default:
continue;
}
2009-06-02 17:52:33 +00:00
MadeChange = true;
}
}
}
2009-06-02 17:52:33 +00:00
if (Roots.size())
MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size());
2009-06-02 17:52:33 +00:00
return MadeChange;
}
// -----------------------------------------------------------------------------
char GCMachineCodeAnalysis::ID = 0;
char &llvm::GCMachineCodeAnalysisID = GCMachineCodeAnalysis::ID;
2009-06-02 17:52:33 +00:00
INITIALIZE_PASS(GCMachineCodeAnalysis, "gc-analysis",
"Analyze Machine Code For Garbage Collection", false, false)
2009-06-02 17:52:33 +00:00
GCMachineCodeAnalysis::GCMachineCodeAnalysis()
: MachineFunctionPass(ID) {}
2009-06-02 17:52:33 +00:00
void GCMachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
2009-06-02 17:52:33 +00:00
MachineFunctionPass::getAnalysisUsage(AU);
AU.setPreservesAll();
AU.addRequired<MachineModuleInfo>();
AU.addRequired<GCModuleInfo>();
}
MCSymbol *GCMachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
DebugLoc DL) const {
2010-03-21 10:49:05 +00:00
MCSymbol *Label = MBB.getParent()->getContext().CreateTempSymbol();
2010-03-16 16:51:38 +00:00
BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
2009-06-02 17:52:33 +00:00
return Label;
}
void GCMachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
2009-06-02 17:52:33 +00:00
// 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)) {
MCSymbol* Label = InsertLabel(*CI->getParent(), CI, CI->getDebugLoc());
FI->addSafePoint(GC::PreCall, Label, CI->getDebugLoc());
}
if (FI->getStrategy().needsSafePoint(GC::PostCall)) {
MCSymbol* Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc());
FI->addSafePoint(GC::PostCall, Label, CI->getDebugLoc());
}
2009-06-02 17:52:33 +00:00
}
void GCMachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
2009-06-02 17:52:33 +00: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)
if (MI->isCall())
2009-06-02 17:52:33 +00:00
VisitCallPoint(MI);
}
void GCMachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
const TargetFrameLowering *TFI = TM->getSubtargetImpl()->getFrameLowering();
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 {
RI->StackOffset = TFI->getFrameIndexOffset(MF, RI->Num);
++RI;
}
}
2009-06-02 17:52:33 +00:00
}
bool GCMachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
2009-06-02 17:52:33 +00:00
// Quick exit for functions that do not use GC.
if (!MF.getFunction()->hasGC())
return false;
2009-06-02 17:52:33 +00:00
FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction());
if (!FI->getStrategy().needsSafePoints())
return false;
2009-06-02 17:52:33 +00:00
TM = &MF.getTarget();
MMI = &getAnalysis<MachineModuleInfo>();
TII = TM->getSubtargetImpl()->getInstrInfo();
2009-06-02 17:52:33 +00:00
// Find the size of the stack frame.
FI->setFrameSize(MF.getFrameInfo()->getStackSize());
2009-06-02 17:52:33 +00:00
// Find all safe points.
if (FI->getStrategy().customSafePoints()) {
FI->getStrategy().findCustomSafePoints(*FI, MF);
} else {
FindSafePoints(MF);
}
2009-06-02 17:52:33 +00:00
// Find the stack offsets for all roots.
FindStackOffsets(MF);
2009-06-02 17:52:33 +00:00
return false;
}