X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FCodeGen%2FGCStrategy.cpp;h=1fdff6bbf106295941606b19bb3adfc4002a7786;hb=953f25078959d2517c88bf903a8270dc11f1403f;hp=629a2e13dee4c73a59fdc7d5c2231d93cfbcfb0c;hpb=5a29c9eed157af51a8d338b5a225b146881819e8;p=oota-llvm.git diff --git a/lib/CodeGen/GCStrategy.cpp b/lib/CodeGen/GCStrategy.cpp index 629a2e13dee..1fdff6bbf10 100644 --- a/lib/CodeGen/GCStrategy.cpp +++ b/lib/CodeGen/GCStrategy.cpp @@ -1,4 +1,4 @@ -//===-- Collector.cpp - Garbage collection infrastructure -----------------===// +//===-- GCStrategy.cpp - Garbage collection infrastructure -----------------===// // // The LLVM Compiler Infrastructure // @@ -10,129 +10,142 @@ // 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. +// //===----------------------------------------------------------------------===// #include "llvm/CodeGen/GCStrategy.h" -#include "llvm/CodeGen/Passes.h" -#include "llvm/IntrinsicInst.h" -#include "llvm/Module.h" #include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineInstrBuilder.h" #include "llvm/CodeGen/MachineModuleInfo.h" -#include "llvm/Target/TargetFrameInfo.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/Support/Compiler.h" +#include "llvm/Target/TargetRegisterInfo.h" using namespace llvm; namespace { - + /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or - /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as - /// directed by the Collector. It also performs automatic root initialization + /// 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 VISIBILITY_HIDDEN LowerIntrinsics : public FunctionPass { - static bool NeedsDefaultLoweringPass(const Collector &C); - static bool NeedsCustomLoweringPass(const Collector &C); + class LowerIntrinsics : public FunctionPass { + static bool NeedsDefaultLoweringPass(const GCStrategy &C); + static bool NeedsCustomLoweringPass(const GCStrategy &C); static bool CouldBecomeSafePoint(Instruction *I); - bool PerformDefaultLowering(Function &F, Collector &Coll); + bool PerformDefaultLowering(Function &F, GCStrategy &Coll); static bool InsertRootInitializers(Function &F, AllocaInst **Roots, unsigned Count); - + public: static char ID; - + LowerIntrinsics(); - const char *getPassName() const; - void getAnalysisUsage(AnalysisUsage &AU) const; - - bool doInitialization(Module &M); - bool runOnFunction(Function &F); + const char *getPassName() const override; + void getAnalysisUsage(AnalysisUsage &AU) const override; + + bool doInitialization(Module &M) override; + bool runOnFunction(Function &F) override; }; - - - /// MachineCodeAnalysis - This is a target-independent pass over the machine + + + /// 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 - /// CollectorMetadata record for each function. - class VISIBILITY_HIDDEN MachineCodeAnalysis : public MachineFunctionPass { + /// GCMetadata record for each function. + class GCMachineCodeAnalysis : public MachineFunctionPass { const TargetMachine *TM; - CollectorMetadata *MD; + GCFunctionInfo *FI; MachineModuleInfo *MMI; const TargetInstrInfo *TII; - MachineFrameInfo *MFI; - + void FindSafePoints(MachineFunction &MF); void VisitCallPoint(MachineBasicBlock::iterator MI); - unsigned InsertLabel(MachineBasicBlock &MBB, - MachineBasicBlock::iterator MI) const; - + MCSymbol *InsertLabel(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MI, + DebugLoc DL) const; + void FindStackOffsets(MachineFunction &MF); - + public: static char ID; - - MachineCodeAnalysis(); - const char *getPassName() const; - void getAnalysisUsage(AnalysisUsage &AU) const; - - bool runOnMachineFunction(MachineFunction &MF); + + GCMachineCodeAnalysis(); + void getAnalysisUsage(AnalysisUsage &AU) const override; + + bool runOnMachineFunction(MachineFunction &MF) override; }; - + } // ----------------------------------------------------------------------------- -Collector::Collector() : +GCStrategy::GCStrategy() : NeededSafePoints(0), CustomReadBarriers(false), CustomWriteBarriers(false), CustomRoots(false), + CustomSafePoints(false), InitRoots(true), UsesMetadata(false) {} -Collector::~Collector() { - for (iterator I = begin(), E = end(); I != E; ++I) - delete *I; - - Functions.clear(); +bool GCStrategy::initializeCustomLowering(Module &M) { return false; } + +bool GCStrategy::performCustomLowering(Function &F) { + dbgs() << "gc " << getName() << " must override performCustomLowering.\n"; + llvm_unreachable("must override performCustomLowering"); } - -bool Collector::initializeCustomLowering(Module &M) { return false; } - -bool Collector::performCustomLowering(Function &F) { - cerr << "gc " << getName() << " must override performCustomLowering.\n"; - abort(); - return 0; + + +bool GCStrategy::findCustomSafePoints(GCFunctionInfo& FI, MachineFunction &F) { + dbgs() << "gc " << getName() << " must override findCustomSafePoints.\n"; + llvm_unreachable(nullptr); +} + + +GCFunctionInfo *GCStrategy::insertFunctionInfo(const Function &F) { + Functions.push_back(make_unique(F, *this)); + return Functions.back().get(); } - -CollectorMetadata *Collector::insertFunctionMetadata(const Function &F) { - CollectorMetadata *CM = new CollectorMetadata(F, *this); - Functions.push_back(CM); - return CM; -} // ----------------------------------------------------------------------------- +INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering", + false, false) +INITIALIZE_PASS_DEPENDENCY(GCModuleInfo) +INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false) + FunctionPass *llvm::createGCLoweringPass() { return new LowerIntrinsics(); } - + char LowerIntrinsics::ID = 0; LowerIntrinsics::LowerIntrinsics() - : FunctionPass((intptr_t)&ID) {} + : FunctionPass(ID) { + initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry()); + } const char *LowerIntrinsics::getPassName() const { return "Lower Garbage Collection Instructions"; } - + void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const { FunctionPass::getAnalysisUsage(AU); - AU.addRequired(); + AU.addRequired(); + AU.addPreserved(); } /// doInitialization - If this module uses the GC intrinsics, find them now. @@ -141,28 +154,27 @@ bool LowerIntrinsics::doInitialization(Module &M) { // work against the entire module. But this cannot be done at // runFunction time (initializeCustomLowering likely needs to change // the module). - CollectorModuleMetadata *CMM = getAnalysisToUpdate(); - assert(CMM && "LowerIntrinsics didn't require CollectorModuleMetadata!?"); + GCModuleInfo *MI = getAnalysisIfAvailable(); + assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?"); for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) - if (I->hasCollector()) - CMM->get(*I); // Instantiate the Collector. - + if (!I->isDeclaration() && I->hasGC()) + MI->getFunctionInfo(*I); // Instantiate the GC strategy. + bool MadeChange = false; - for (CollectorModuleMetadata::iterator I = CMM->begin(), - E = CMM->end(); I != E; ++I) + for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I) if (NeedsCustomLoweringPass(**I)) if ((*I)->initializeCustomLowering(M)) MadeChange = true; - + return MadeChange; } -bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots, +bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots, unsigned Count) { // Scroll past alloca instructions. BasicBlock::iterator IP = F.getEntryBlock().begin(); while (isa(IP)) ++IP; - + // Search for initializers in the initial BB. SmallPtrSet InitedRoots; for (; !CouldBecomeSafePoint(IP); ++IP) @@ -170,22 +182,23 @@ bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots, if (AllocaInst *AI = dyn_cast(SI->getOperand(1)->stripPointerCasts())) InitedRoots.insert(AI); - + // Add root initializers. bool MadeChange = false; - + for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I) if (!InitedRoots.count(*I)) { - new StoreInst(ConstantPointerNull::get(cast( - cast((*I)->getType())->getElementType())), - *I, IP); + StoreInst* SI = new StoreInst(ConstantPointerNull::get(cast( + cast((*I)->getType())->getElementType())), + *I); + SI->insertAfter(*I); MadeChange = true; } - + return MadeChange; } -bool LowerIntrinsics::NeedsDefaultLoweringPass(const Collector &C) { +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() @@ -193,7 +206,7 @@ bool LowerIntrinsics::NeedsDefaultLoweringPass(const Collector &C) { || C.initializeRoots(); } -bool LowerIntrinsics::NeedsCustomLoweringPass(const Collector &C) { +bool LowerIntrinsics::NeedsCustomLoweringPass(const GCStrategy &C) { // Custom lowering is only necessary if enabled for some action. return C.customWriteBarrier() || C.customReadBarrier() @@ -205,26 +218,26 @@ bool LowerIntrinsics::NeedsCustomLoweringPass(const Collector &C) { 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(I) || isa(I) || isa(I) || isa(I)) return false; - + // llvm.gcroot is safe because it doesn't do anything at runtime. if (CallInst *CI = dyn_cast(I)) if (Function *F = CI->getCalledFunction()) if (unsigned IID = F->getIntrinsicID()) if (IID == Intrinsic::gcroot) return false; - + return true; } @@ -232,29 +245,38 @@ bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) { /// 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.hasCollector()) return false; - - CollectorMetadata &MD = getAnalysis().get(F); - Collector &Coll = MD.getCollector(); - + if (!F.hasGC()) + return false; + + GCFunctionInfo &FI = getAnalysis().getFunctionInfo(F); + GCStrategy &S = FI.getStrategy(); + bool MadeChange = false; - - if (NeedsDefaultLoweringPass(Coll)) - MadeChange |= PerformDefaultLowering(F, Coll); - - if (NeedsCustomLoweringPass(Coll)) - MadeChange |= Coll.performCustomLowering(F); - + + if (NeedsDefaultLoweringPass(S)) + MadeChange |= PerformDefaultLowering(F, S); + + bool UseCustomLoweringPass = NeedsCustomLoweringPass(S); + if (UseCustomLoweringPass) + MadeChange |= S.performCustomLowering(F); + + // Custom lowering may modify the CFG, so dominators must be recomputed. + if (UseCustomLoweringPass) { + if (DominatorTreeWrapperPass *DTWP = + getAnalysisIfAvailable()) + DTWP->getDomTree().recalculate(F); + } + return MadeChange; } -bool LowerIntrinsics::PerformDefaultLowering(Function &F, Collector &Coll) { - bool LowerWr = !Coll.customWriteBarrier(); - bool LowerRd = !Coll.customReadBarrier(); - bool InitRoots = Coll.initializeRoots(); - - SmallVector Roots; - +bool LowerIntrinsics::PerformDefaultLowering(Function &F, GCStrategy &S) { + bool LowerWr = !S.customWriteBarrier(); + bool LowerRd = !S.customReadBarrier(); + bool InitRoots = S.initializeRoots(); + + SmallVector 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;) { @@ -264,7 +286,8 @@ bool LowerIntrinsics::PerformDefaultLowering(Function &F, Collector &Coll) { case Intrinsic::gcwrite: if (LowerWr) { // Replace a write barrier with a simple store. - Value *St = new StoreInst(CI->getOperand(1), CI->getOperand(3), CI); + Value *St = new StoreInst(CI->getArgOperand(0), + CI->getArgOperand(2), CI); CI->replaceAllUsesWith(St); CI->eraseFromParent(); } @@ -272,7 +295,7 @@ bool LowerIntrinsics::PerformDefaultLowering(Function &F, Collector &Coll) { case Intrinsic::gcread: if (LowerRd) { // Replace a read barrier with a simple load. - Value *Ld = new LoadInst(CI->getOperand(2), "", CI); + Value *Ld = new LoadInst(CI->getArgOperand(1), "", CI); Ld->takeName(CI); CI->replaceAllUsesWith(Ld); CI->eraseFromParent(); @@ -283,107 +306,117 @@ bool LowerIntrinsics::PerformDefaultLowering(Function &F, Collector &Coll) { // Initialize the GC root, but do not delete the intrinsic. The // backend needs the intrinsic to flag the stack slot. Roots.push_back(cast( - CI->getOperand(1)->stripPointerCasts())); + CI->getArgOperand(0)->stripPointerCasts())); } break; default: continue; } - + MadeChange = true; } } } - + if (Roots.size()) MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size()); - + return MadeChange; } // ----------------------------------------------------------------------------- -FunctionPass *llvm::createGCMachineCodeAnalysisPass() { - return new MachineCodeAnalysis(); -} - -char MachineCodeAnalysis::ID = 0; +char GCMachineCodeAnalysis::ID = 0; +char &llvm::GCMachineCodeAnalysisID = GCMachineCodeAnalysis::ID; -MachineCodeAnalysis::MachineCodeAnalysis() - : MachineFunctionPass(intptr_t(&ID)) {} +INITIALIZE_PASS(GCMachineCodeAnalysis, "gc-analysis", + "Analyze Machine Code For Garbage Collection", false, false) -const char *MachineCodeAnalysis::getPassName() const { - return "Analyze Machine Code For Garbage Collection"; -} +GCMachineCodeAnalysis::GCMachineCodeAnalysis() + : MachineFunctionPass(ID) {} -void MachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { +void GCMachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { MachineFunctionPass::getAnalysisUsage(AU); AU.setPreservesAll(); AU.addRequired(); - AU.addRequired(); + AU.addRequired(); } -unsigned MachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB, - MachineBasicBlock::iterator MI) const { - unsigned Label = MMI->NextLabelID(); - BuildMI(MBB, MI, TII->get(TargetInstrInfo::GC_LABEL)).addImm(Label); +MCSymbol *GCMachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MI, + DebugLoc DL) const { + MCSymbol *Label = MBB.getParent()->getContext().CreateTempSymbol(); + BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label); return Label; } -void MachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) { +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 (MD->getCollector().needsSafePoint(GC::PreCall)) - MD->addSafePoint(GC::PreCall, InsertLabel(*CI->getParent(), CI)); - - if (MD->getCollector().needsSafePoint(GC::PostCall)) - MD->addSafePoint(GC::PostCall, InsertLabel(*CI->getParent(), RAI)); + 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()); + } } -void MachineCodeAnalysis::FindSafePoints(MachineFunction &MF) { +void GCMachineCodeAnalysis::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 (MI->getDesc().isCall()) + if (MI->isCall()) VisitCallPoint(MI); } -void MachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) { - uint64_t StackSize = MFI->getStackSize(); - uint64_t OffsetAdjustment = MFI->getOffsetAdjustment(); - uint64_t OffsetOfLocalArea = TM->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; +void GCMachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) { + const TargetFrameLowering *TFI = TM->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; + } + } } -bool MachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) { +bool GCMachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) { // Quick exit for functions that do not use GC. - if (!MF.getFunction()->hasCollector()) return false; - - MD = &getAnalysis().get(*MF.getFunction()); - if (!MD->getCollector().needsSafePoints()) + if (!MF.getFunction()->hasGC()) return false; - + + FI = &getAnalysis().getFunctionInfo(*MF.getFunction()); + if (!FI->getStrategy().needsSafePoints()) + return false; + TM = &MF.getTarget(); MMI = &getAnalysis(); TII = TM->getInstrInfo(); - MFI = MF.getFrameInfo(); - + // Find the size of the stack frame. - MD->setFrameSize(MFI->getStackSize()); - + FI->setFrameSize(MF.getFrameInfo()->getStackSize()); + // Find all safe points. - FindSafePoints(MF); - + if (FI->getStrategy().customSafePoints()) { + FI->getStrategy().findCustomSafePoints(*FI, MF); + } else { + FindSafePoints(MF); + } + // Find the stack offsets for all roots. FindStackOffsets(MF); - + return false; }