diff options
author | Philip Reames <listmail@philipreames.com> | 2015-01-16 23:16:12 +0000 |
---|---|---|
committer | Philip Reames <listmail@philipreames.com> | 2015-01-16 23:16:12 +0000 |
commit | 36319538d0981498c9eb6ba14066049d653298a5 (patch) | |
tree | cb20cd8a5b66faeafbb545b6cefa2ccd46e641ae /llvm/lib/CodeGen | |
parent | 6bfd398022a03301f2c86d38143706a0423e7258 (diff) | |
download | bcm5719-llvm-36319538d0981498c9eb6ba14066049d653298a5.tar.gz bcm5719-llvm-36319538d0981498c9eb6ba14066049d653298a5.zip |
clang-format all the GC related files (NFC)
Nothing interesting here...
llvm-svn: 226342
Diffstat (limited to 'llvm/lib/CodeGen')
-rw-r--r-- | llvm/lib/CodeGen/AsmPrinter/ErlangGCPrinter.cpp | 31 | ||||
-rw-r--r-- | llvm/lib/CodeGen/AsmPrinter/OcamlGCPrinter.cpp | 47 | ||||
-rw-r--r-- | llvm/lib/CodeGen/ErlangGC.cpp | 15 | ||||
-rw-r--r-- | llvm/lib/CodeGen/GCMetadata.cpp | 77 | ||||
-rw-r--r-- | llvm/lib/CodeGen/GCMetadataPrinter.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/CodeGen/GCRootLowering.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/CodeGen/OcamlGC.cpp | 13 | ||||
-rw-r--r-- | llvm/lib/CodeGen/ShadowStackGC.cpp | 384 | ||||
-rw-r--r-- | llvm/lib/CodeGen/StatepointExampleGC.cpp | 6 |
9 files changed, 288 insertions, 293 deletions
diff --git a/llvm/lib/CodeGen/AsmPrinter/ErlangGCPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/ErlangGCPrinter.cpp index e293acd43a8..aa4418f6a75 100644 --- a/llvm/lib/CodeGen/AsmPrinter/ErlangGCPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/ErlangGCPrinter.cpp @@ -34,18 +34,16 @@ using namespace llvm; namespace { - class ErlangGCPrinter : public GCMetadataPrinter { - public: - void finishAssembly(Module &M, GCModuleInfo &Info, - AsmPrinter &AP) override; - }; - +class ErlangGCPrinter : public GCMetadataPrinter { +public: + void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override; +}; } static GCMetadataPrinterRegistry::Add<ErlangGCPrinter> -X("erlang", "erlang-compatible garbage collector"); + X("erlang", "erlang-compatible garbage collector"); -void llvm::linkErlangGCPrinter() { } +void llvm::linkErlangGCPrinter() {} void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) { @@ -54,13 +52,13 @@ void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info, AP.TM.getSubtargetImpl()->getDataLayout()->getPointerSize(); // Put this in a custom .note section. - AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getContext() - .getELFSection(".note.gc", ELF::SHT_PROGBITS, 0, - SectionKind::getDataRel())); + AP.OutStreamer.SwitchSection( + AP.getObjFileLowering().getContext().getELFSection( + ".note.gc", ELF::SHT_PROGBITS, 0, SectionKind::getDataRel())); // For each function... for (GCModuleInfo::FuncInfoVec::iterator FI = Info.funcinfo_begin(), - IE = Info.funcinfo_end(); + IE = Info.funcinfo_end(); FI != IE; ++FI) { GCFunctionInfo &MD = **FI; if (MD.getStrategy().getName() != getStrategy().getName()) @@ -91,7 +89,7 @@ void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info, // Emit the address of the safe point. OS.AddComment("safe point address"); MCSymbol *Label = PI->Label; - AP.EmitLabelPlusOffset(Label/*Hi*/, 0/*Offset*/, 4/*Size*/); + AP.EmitLabelPlusOffset(Label /*Hi*/, 0 /*Offset*/, 4 /*Size*/); } // Stack information never change in safe points! Only print info from the @@ -104,8 +102,9 @@ void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info, // Emit stack arity, i.e. the number of stacked arguments. unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6; - unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs ? - MD.getFunction().arg_size() - RegisteredArgs : 0; + unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs + ? MD.getFunction().arg_size() - RegisteredArgs + : 0; OS.AddComment("stack arity"); AP.EmitInt16(StackArity); @@ -116,7 +115,7 @@ void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info, // And for each live root... for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI), LE = MD.live_end(PI); - LI != LE; ++LI) { + LI != LE; ++LI) { // Emit live root's offset within the stack frame. OS.AddComment("stack index (offset / wordsize)"); AP.EmitInt16(LI->StackOffset / IntPtrSize); diff --git a/llvm/lib/CodeGen/AsmPrinter/OcamlGCPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/OcamlGCPrinter.cpp index ddb14a05736..d6822aa42a3 100644 --- a/llvm/lib/CodeGen/AsmPrinter/OcamlGCPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/OcamlGCPrinter.cpp @@ -32,20 +32,17 @@ using namespace llvm; namespace { - class OcamlGCMetadataPrinter : public GCMetadataPrinter { - public: - void beginAssembly(Module &M, GCModuleInfo &Info, - AsmPrinter &AP) override; - void finishAssembly(Module &M, GCModuleInfo &Info, - AsmPrinter &AP) override; - }; - +class OcamlGCMetadataPrinter : public GCMetadataPrinter { +public: + void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override; + void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override; +}; } static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter> -Y("ocaml", "ocaml 3.10-compatible collector"); + Y("ocaml", "ocaml 3.10-compatible collector"); -void llvm::linkOcamlGCPrinter() { } +void llvm::linkOcamlGCPrinter() {} static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) { const std::string &MId = M.getModuleIdentifier(); @@ -113,7 +110,8 @@ void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info, int NumDescriptors = 0; for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(), - IE = Info.funcinfo_end(); I != IE; ++I) { + IE = Info.funcinfo_end(); + I != IE; ++I) { GCFunctionInfo &FI = **I; if (FI.getStrategy().getName() != getStrategy().getName()) // this function is managed by some other GC @@ -123,7 +121,7 @@ void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info, } } - if (NumDescriptors >= 1<<16) { + if (NumDescriptors >= 1 << 16) { // Very rude! report_fatal_error(" Too much descriptor for ocaml GC"); } @@ -131,19 +129,22 @@ void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info, AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3); for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(), - IE = Info.funcinfo_end(); I != IE; ++I) { + IE = Info.funcinfo_end(); + I != IE; ++I) { GCFunctionInfo &FI = **I; if (FI.getStrategy().getName() != getStrategy().getName()) // this function is managed by some other GC continue; uint64_t FrameSize = FI.getFrameSize(); - if (FrameSize >= 1<<16) { + if (FrameSize >= 1 << 16) { // Very rude! report_fatal_error("Function '" + FI.getFunction().getName() + "' is too large for the ocaml GC! " - "Frame size " + Twine(FrameSize) + ">= 65536.\n" - "(" + Twine(uintptr_t(&FI)) + ")"); + "Frame size " + + Twine(FrameSize) + ">= 65536.\n" + "(" + + Twine(uintptr_t(&FI)) + ")"); } AP.OutStreamer.AddComment("live roots for " + @@ -152,11 +153,12 @@ void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info, for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) { size_t LiveCount = FI.live_size(J); - if (LiveCount >= 1<<16) { + if (LiveCount >= 1 << 16) { // Very rude! report_fatal_error("Function '" + FI.getFunction().getName() + "' is too large for the ocaml GC! " - "Live root count "+Twine(LiveCount)+" >= 65536."); + "Live root count " + + Twine(LiveCount) + " >= 65536."); } AP.OutStreamer.EmitSymbolValue(J->Label, IntPtrSize); @@ -164,12 +166,13 @@ void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info, AP.EmitInt16(LiveCount); for (GCFunctionInfo::live_iterator K = FI.live_begin(J), - KE = FI.live_end(J); K != KE; ++K) { - if (K->StackOffset >= 1<<16) { + KE = FI.live_end(J); + K != KE; ++K) { + if (K->StackOffset >= 1 << 16) { // Very rude! report_fatal_error( - "GC root stack offset is outside of fixed stack frame and out " - "of range for ocaml GC!"); + "GC root stack offset is outside of fixed stack frame and out " + "of range for ocaml GC!"); } AP.EmitInt16(K->StackOffset); } diff --git a/llvm/lib/CodeGen/ErlangGC.cpp b/llvm/lib/CodeGen/ErlangGC.cpp index ec2c2e8daf2..30af07f1214 100644 --- a/llvm/lib/CodeGen/ErlangGC.cpp +++ b/llvm/lib/CodeGen/ErlangGC.cpp @@ -27,17 +27,16 @@ using namespace llvm; namespace { - class ErlangGC : public GCStrategy { - public: - ErlangGC(); - }; - +class ErlangGC : public GCStrategy { +public: + ErlangGC(); +}; } -static GCRegistry::Add<ErlangGC> -X("erlang", "erlang-compatible garbage collector"); +static GCRegistry::Add<ErlangGC> X("erlang", + "erlang-compatible garbage collector"); -void llvm::linkErlangGC() { } +void llvm::linkErlangGC() {} ErlangGC::ErlangGC() { InitRoots = false; diff --git a/llvm/lib/CodeGen/GCMetadata.cpp b/llvm/lib/CodeGen/GCMetadata.cpp index 6f4fa11d6d9..7a61d61e7d0 100644 --- a/llvm/lib/CodeGen/GCMetadata.cpp +++ b/llvm/lib/CodeGen/GCMetadata.cpp @@ -24,22 +24,20 @@ using namespace llvm; namespace { - - class Printer : public FunctionPass { - static char ID; - raw_ostream &OS; - - public: - explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {} +class Printer : public FunctionPass { + static char ID; + raw_ostream &OS; - const char *getPassName() const override; - void getAnalysisUsage(AnalysisUsage &AU) const override; +public: + explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {} - bool runOnFunction(Function &F) override; - bool doFinalization(Module &M) override; - }; + const char *getPassName() const override; + void getAnalysisUsage(AnalysisUsage &AU) const override; + bool runOnFunction(Function &F) override; + bool doFinalization(Module &M) override; +}; } INITIALIZE_PASS(GCModuleInfo, "collector-metadata", @@ -48,7 +46,7 @@ INITIALIZE_PASS(GCModuleInfo, "collector-metadata", // ----------------------------------------------------------------------------- GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S) - : F(F), S(S), FrameSize(~0LL) {} + : F(F), S(S), FrameSize(~0LL) {} GCFunctionInfo::~GCFunctionInfo() {} @@ -56,19 +54,18 @@ GCFunctionInfo::~GCFunctionInfo() {} char GCModuleInfo::ID = 0; -GCModuleInfo::GCModuleInfo() - : ImmutablePass(ID) { +GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) { initializeGCModuleInfoPass(*PassRegistry::getPassRegistry()); } GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) { assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!"); assert(F.hasGC()); - + finfo_map_type::iterator I = FInfoMap.find(&F); if (I != FInfoMap.end()) return *I->second; - + GCStrategy *S = F.getGCStrategy(); if (!S) { std::string error = std::string("unsupported GC: ") + F.getGC(); @@ -76,7 +73,7 @@ GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) { } // Save the fact this strategy is associated with this module. Note that // these are non-owning references, the GCStrategy remains owned by the - // Context. + // Context. StrategyList.push_back(S); Functions.push_back(make_unique<GCFunctionInfo>(F, *S)); GCFunctionInfo *GFI = Functions.back().get(); @@ -98,7 +95,6 @@ FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) { return new Printer(OS); } - const char *Printer::getPassName() const { return "Print Garbage Collector Information"; } @@ -111,42 +107,49 @@ void Printer::getAnalysisUsage(AnalysisUsage &AU) const { static const char *DescKind(GC::PointKind Kind) { switch (Kind) { - case GC::Loop: return "loop"; - case GC::Return: return "return"; - case GC::PreCall: return "pre-call"; - case GC::PostCall: return "post-call"; + case GC::Loop: + return "loop"; + case GC::Return: + return "return"; + case GC::PreCall: + return "pre-call"; + case GC::PostCall: + return "post-call"; } llvm_unreachable("Invalid point kind"); } bool Printer::runOnFunction(Function &F) { - if (F.hasGC()) return false; - + if (F.hasGC()) + return false; + GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F); - + OS << "GC roots for " << FD->getFunction().getName() << ":\n"; for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(), - RE = FD->roots_end(); RI != RE; ++RI) + RE = FD->roots_end(); + RI != RE; ++RI) OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n"; - + OS << "GC safe points for " << FD->getFunction().getName() << ":\n"; - for (GCFunctionInfo::iterator PI = FD->begin(), - PE = FD->end(); PI != PE; ++PI) { - - OS << "\t" << PI->Label->getName() << ": " - << DescKind(PI->Kind) << ", live = {"; - + for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE; + ++PI) { + + OS << "\t" << PI->Label->getName() << ": " << DescKind(PI->Kind) + << ", live = {"; + for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI), - RE = FD->live_end(PI);;) { + RE = FD->live_end(PI); + ;) { OS << " " << RI->Num; if (++RI == RE) break; OS << ","; } - + OS << " }\n"; } - + return false; } diff --git a/llvm/lib/CodeGen/GCMetadataPrinter.cpp b/llvm/lib/CodeGen/GCMetadataPrinter.cpp index fdff4a78e07..bb8cfa1cc80 100644 --- a/llvm/lib/CodeGen/GCMetadataPrinter.cpp +++ b/llvm/lib/CodeGen/GCMetadataPrinter.cpp @@ -14,6 +14,6 @@ #include "llvm/CodeGen/GCMetadataPrinter.h" using namespace llvm; -GCMetadataPrinter::GCMetadataPrinter() { } +GCMetadataPrinter::GCMetadataPrinter() {} -GCMetadataPrinter::~GCMetadataPrinter() { } +GCMetadataPrinter::~GCMetadataPrinter() {} diff --git a/llvm/lib/CodeGen/GCRootLowering.cpp b/llvm/lib/CodeGen/GCRootLowering.cpp index 8ff08ece76e..7cb459874ef 100644 --- a/llvm/lib/CodeGen/GCRootLowering.cpp +++ b/llvm/lib/CodeGen/GCRootLowering.cpp @@ -116,8 +116,6 @@ static bool NeedsCustomLoweringPass(const GCStrategy &C) { return C.customWriteBarrier() || C.customReadBarrier() || C.customRoots(); } - - /// 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 @@ -139,7 +137,6 @@ bool LowerIntrinsics::doInitialization(Module &M) { return MadeChange; } - /// CouldBecomeSafePoint - Predicate to conservatively determine whether the /// instruction could introduce a safe point. static bool CouldBecomeSafePoint(Instruction *I) { @@ -199,7 +196,6 @@ static bool InsertRootInitializers(Function &F, AllocaInst **Roots, 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) { diff --git a/llvm/lib/CodeGen/OcamlGC.cpp b/llvm/lib/CodeGen/OcamlGC.cpp index c5c78529ece..164e3700909 100644 --- a/llvm/lib/CodeGen/OcamlGC.cpp +++ b/llvm/lib/CodeGen/OcamlGC.cpp @@ -20,16 +20,15 @@ using namespace llvm; namespace { - class OcamlGC : public GCStrategy { - public: - OcamlGC(); - }; +class OcamlGC : public GCStrategy { +public: + OcamlGC(); +}; } -static GCRegistry::Add<OcamlGC> -X("ocaml", "ocaml 3.10-compatible GC"); +static GCRegistry::Add<OcamlGC> X("ocaml", "ocaml 3.10-compatible GC"); -void llvm::linkOcamlGC() { } +void llvm::linkOcamlGC() {} OcamlGC::OcamlGC() { NeededSafePoints = 1 << GC::PostCall; diff --git a/llvm/lib/CodeGen/ShadowStackGC.cpp b/llvm/lib/CodeGen/ShadowStackGC.cpp index 181cb8c435c..163d0efc07a 100644 --- a/llvm/lib/CodeGen/ShadowStackGC.cpp +++ b/llvm/lib/CodeGen/ShadowStackGC.cpp @@ -39,161 +39,156 @@ using namespace llvm; namespace { - class ShadowStackGC : public GCStrategy { - /// RootChain - This is the global linked-list that contains the chain of GC - /// roots. - GlobalVariable *Head; - - /// StackEntryTy - Abstract type of a link in the shadow stack. - /// - StructType *StackEntryTy; - StructType *FrameMapTy; - - /// Roots - GC roots in the current function. Each is a pair of the - /// intrinsic call and its corresponding alloca. - std::vector<std::pair<CallInst*,AllocaInst*> > Roots; - - public: - ShadowStackGC(); - - bool initializeCustomLowering(Module &M) override; - bool performCustomLowering(Function &F) override; - - private: - bool IsNullValue(Value *V); - Constant *GetFrameMap(Function &F); - Type* GetConcreteStackEntryType(Function &F); - void CollectRoots(Function &F); - static GetElementPtrInst *CreateGEP(LLVMContext &Context, - IRBuilder<> &B, Value *BasePtr, - int Idx1, const char *Name); - static GetElementPtrInst *CreateGEP(LLVMContext &Context, - IRBuilder<> &B, Value *BasePtr, - int Idx1, int Idx2, const char *Name); - }; +class ShadowStackGC : public GCStrategy { + /// RootChain - This is the global linked-list that contains the chain of GC + /// roots. + GlobalVariable *Head; + /// StackEntryTy - Abstract type of a link in the shadow stack. + /// + StructType *StackEntryTy; + StructType *FrameMapTy; + + /// Roots - GC roots in the current function. Each is a pair of the + /// intrinsic call and its corresponding alloca. + std::vector<std::pair<CallInst *, AllocaInst *>> Roots; + +public: + ShadowStackGC(); + + bool initializeCustomLowering(Module &M) override; + bool performCustomLowering(Function &F) override; + +private: + bool IsNullValue(Value *V); + Constant *GetFrameMap(Function &F); + Type *GetConcreteStackEntryType(Function &F); + void CollectRoots(Function &F); + static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B, + Value *BasePtr, int Idx1, + const char *Name); + static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B, + Value *BasePtr, int Idx1, int Idx2, + const char *Name); +}; } static GCRegistry::Add<ShadowStackGC> -X("shadow-stack", "Very portable GC for uncooperative code generators"); + X("shadow-stack", "Very portable GC for uncooperative code generators"); namespace { - /// EscapeEnumerator - This is a little algorithm to find all escape points - /// from a function so that "finally"-style code can be inserted. In addition - /// to finding the existing return and unwind instructions, it also (if - /// necessary) transforms any call instructions into invokes and sends them to - /// a landing pad. - /// - /// It's wrapped up in a state machine using the same transform C# uses for - /// 'yield return' enumerators, This transform allows it to be non-allocating. - class EscapeEnumerator { - Function &F; - const char *CleanupBBName; - - // State. - int State; - Function::iterator StateBB, StateE; - IRBuilder<> Builder; - - public: - EscapeEnumerator(Function &F, const char *N = "cleanup") +/// EscapeEnumerator - This is a little algorithm to find all escape points +/// from a function so that "finally"-style code can be inserted. In addition +/// to finding the existing return and unwind instructions, it also (if +/// necessary) transforms any call instructions into invokes and sends them to +/// a landing pad. +/// +/// It's wrapped up in a state machine using the same transform C# uses for +/// 'yield return' enumerators, This transform allows it to be non-allocating. +class EscapeEnumerator { + Function &F; + const char *CleanupBBName; + + // State. + int State; + Function::iterator StateBB, StateE; + IRBuilder<> Builder; + +public: + EscapeEnumerator(Function &F, const char *N = "cleanup") : F(F), CleanupBBName(N), State(0), Builder(F.getContext()) {} - IRBuilder<> *Next() { - switch (State) { - default: + IRBuilder<> *Next() { + switch (State) { + default: + return nullptr; + + case 0: + StateBB = F.begin(); + StateE = F.end(); + State = 1; + + case 1: + // Find all 'return', 'resume', and 'unwind' instructions. + while (StateBB != StateE) { + BasicBlock *CurBB = StateBB++; + + // Branches and invokes do not escape, only unwind, resume, and return + // do. + TerminatorInst *TI = CurBB->getTerminator(); + if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI)) + continue; + + Builder.SetInsertPoint(TI->getParent(), TI); + return &Builder; + } + + State = 2; + + // Find all 'call' instructions. + SmallVector<Instruction *, 16> Calls; + for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) + for (BasicBlock::iterator II = BB->begin(), EE = BB->end(); II != EE; + ++II) + if (CallInst *CI = dyn_cast<CallInst>(II)) + if (!CI->getCalledFunction() || + !CI->getCalledFunction()->getIntrinsicID()) + Calls.push_back(CI); + + if (Calls.empty()) return nullptr; - case 0: - StateBB = F.begin(); - StateE = F.end(); - State = 1; - - case 1: - // Find all 'return', 'resume', and 'unwind' instructions. - while (StateBB != StateE) { - BasicBlock *CurBB = StateBB++; - - // Branches and invokes do not escape, only unwind, resume, and return - // do. - TerminatorInst *TI = CurBB->getTerminator(); - if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI)) - continue; - - Builder.SetInsertPoint(TI->getParent(), TI); - return &Builder; - } - - State = 2; - - // Find all 'call' instructions. - SmallVector<Instruction*,16> Calls; - for (Function::iterator BB = F.begin(), - E = F.end(); BB != E; ++BB) - for (BasicBlock::iterator II = BB->begin(), - EE = BB->end(); II != EE; ++II) - if (CallInst *CI = dyn_cast<CallInst>(II)) - if (!CI->getCalledFunction() || - !CI->getCalledFunction()->getIntrinsicID()) - Calls.push_back(CI); - - if (Calls.empty()) - return nullptr; - - // Create a cleanup block. - LLVMContext &C = F.getContext(); - BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F); - Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), - Type::getInt32Ty(C), nullptr); - Constant *PersFn = - F.getParent()-> - getOrInsertFunction("__gcc_personality_v0", - FunctionType::get(Type::getInt32Ty(C), true)); - LandingPadInst *LPad = LandingPadInst::Create(ExnTy, PersFn, 1, - "cleanup.lpad", - CleanupBB); - LPad->setCleanup(true); - ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB); - - // Transform the 'call' instructions into 'invoke's branching to the - // cleanup block. Go in reverse order to make prettier BB names. - SmallVector<Value*,16> Args; - for (unsigned I = Calls.size(); I != 0; ) { - CallInst *CI = cast<CallInst>(Calls[--I]); - - // Split the basic block containing the function call. - BasicBlock *CallBB = CI->getParent(); - BasicBlock *NewBB = + // Create a cleanup block. + LLVMContext &C = F.getContext(); + BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F); + Type *ExnTy = + StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C), nullptr); + Constant *PersFn = F.getParent()->getOrInsertFunction( + "__gcc_personality_v0", FunctionType::get(Type::getInt32Ty(C), true)); + LandingPadInst *LPad = + LandingPadInst::Create(ExnTy, PersFn, 1, "cleanup.lpad", CleanupBB); + LPad->setCleanup(true); + ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB); + + // Transform the 'call' instructions into 'invoke's branching to the + // cleanup block. Go in reverse order to make prettier BB names. + SmallVector<Value *, 16> Args; + for (unsigned I = Calls.size(); I != 0;) { + CallInst *CI = cast<CallInst>(Calls[--I]); + + // Split the basic block containing the function call. + BasicBlock *CallBB = CI->getParent(); + BasicBlock *NewBB = CallBB->splitBasicBlock(CI, CallBB->getName() + ".cont"); - // Remove the unconditional branch inserted at the end of CallBB. - CallBB->getInstList().pop_back(); - NewBB->getInstList().remove(CI); - - // Create a new invoke instruction. - Args.clear(); - CallSite CS(CI); - Args.append(CS.arg_begin(), CS.arg_end()); - - InvokeInst *II = InvokeInst::Create(CI->getCalledValue(), - NewBB, CleanupBB, - Args, CI->getName(), CallBB); - II->setCallingConv(CI->getCallingConv()); - II->setAttributes(CI->getAttributes()); - CI->replaceAllUsesWith(II); - delete CI; - } - - Builder.SetInsertPoint(RI->getParent(), RI); - return &Builder; + // Remove the unconditional branch inserted at the end of CallBB. + CallBB->getInstList().pop_back(); + NewBB->getInstList().remove(CI); + + // Create a new invoke instruction. + Args.clear(); + CallSite CS(CI); + Args.append(CS.arg_begin(), CS.arg_end()); + + InvokeInst *II = + InvokeInst::Create(CI->getCalledValue(), NewBB, CleanupBB, Args, + CI->getName(), CallBB); + II->setCallingConv(CI->getCallingConv()); + II->setAttributes(CI->getAttributes()); + CI->replaceAllUsesWith(II); + delete CI; } + + Builder.SetInsertPoint(RI->getParent(), RI); + return &Builder; } - }; + } +}; } // ----------------------------------------------------------------------------- -void llvm::linkShadowStackGC() { } +void llvm::linkShadowStackGC() {} ShadowStackGC::ShadowStackGC() : Head(nullptr), StackEntryTy(nullptr) { InitRoots = true; @@ -206,7 +201,7 @@ Constant *ShadowStackGC::GetFrameMap(Function &F) { // Truncate the ShadowStackDescriptor if some metadata is null. unsigned NumMeta = 0; - SmallVector<Constant*, 16> Metadata; + SmallVector<Constant *, 16> Metadata; for (unsigned I = 0; I != Roots.size(); ++I) { Constant *C = cast<Constant>(Roots[I].first->getArgOperand(1)); if (!C->isNullValue()) @@ -216,20 +211,19 @@ Constant *ShadowStackGC::GetFrameMap(Function &F) { Metadata.resize(NumMeta); Type *Int32Ty = Type::getInt32Ty(F.getContext()); - + Constant *BaseElts[] = { - ConstantInt::get(Int32Ty, Roots.size(), false), - ConstantInt::get(Int32Ty, NumMeta, false), + ConstantInt::get(Int32Ty, Roots.size(), false), + ConstantInt::get(Int32Ty, NumMeta, false), }; Constant *DescriptorElts[] = { - ConstantStruct::get(FrameMapTy, BaseElts), - ConstantArray::get(ArrayType::get(VoidPtr, NumMeta), Metadata) - }; + ConstantStruct::get(FrameMapTy, BaseElts), + ConstantArray::get(ArrayType::get(VoidPtr, NumMeta), Metadata)}; + + Type *EltTys[] = {DescriptorElts[0]->getType(), DescriptorElts[1]->getType()}; + StructType *STy = StructType::create(EltTys, "gc_map." + utostr(NumMeta)); - Type *EltTys[] = { DescriptorElts[0]->getType(),DescriptorElts[1]->getType()}; - StructType *STy = StructType::create(EltTys, "gc_map."+utostr(NumMeta)); - Constant *FrameMap = ConstantStruct::get(STy, DescriptorElts); // FIXME: Is this actually dangerous as WritingAnLLVMPass.html claims? Seems @@ -246,24 +240,23 @@ Constant *ShadowStackGC::GetFrameMap(Function &F) { // (which uses a FunctionPassManager (which segfaults (not asserts) if // provided a ModulePass))). Constant *GV = new GlobalVariable(*F.getParent(), FrameMap->getType(), true, - GlobalVariable::InternalLinkage, - FrameMap, "__gc_" + F.getName()); + GlobalVariable::InternalLinkage, FrameMap, + "__gc_" + F.getName()); Constant *GEPIndices[2] = { - ConstantInt::get(Type::getInt32Ty(F.getContext()), 0), - ConstantInt::get(Type::getInt32Ty(F.getContext()), 0) - }; + ConstantInt::get(Type::getInt32Ty(F.getContext()), 0), + ConstantInt::get(Type::getInt32Ty(F.getContext()), 0)}; return ConstantExpr::getGetElementPtr(GV, GEPIndices); } -Type* ShadowStackGC::GetConcreteStackEntryType(Function &F) { +Type *ShadowStackGC::GetConcreteStackEntryType(Function &F) { // doInitialization creates the generic version of this type. - std::vector<Type*> EltTys; + std::vector<Type *> EltTys; EltTys.push_back(StackEntryTy); for (size_t I = 0; I != Roots.size(); I++) EltTys.push_back(Roots[I].second->getAllocatedType()); - - return StructType::create(EltTys, "gc_stackentry."+F.getName().str()); + + return StructType::create(EltTys, "gc_stackentry." + F.getName().str()); } /// doInitialization - If this module uses the GC intrinsics, find them now. If @@ -274,10 +267,10 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) { // int32_t NumMeta; // Number of metadata descriptors. May be < NumRoots. // void *Meta[]; // May be absent for roots without metadata. // }; - std::vector<Type*> EltTys; + std::vector<Type *> EltTys; // 32 bits is ok up to a 32GB stack frame. :) EltTys.push_back(Type::getInt32Ty(M.getContext())); - // Specifies length of variable length array. + // Specifies length of variable length array. EltTys.push_back(Type::getInt32Ty(M.getContext())); FrameMapTy = StructType::create(EltTys, "gc_map"); PointerType *FrameMapPtrTy = PointerType::getUnqual(FrameMapTy); @@ -287,9 +280,9 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) { // FrameMap *Map; // Pointer to constant FrameMap. // void *Roots[]; // Stack roots (in-place array, so we pretend). // }; - + StackEntryTy = StructType::create(M.getContext(), "gc_stackentry"); - + EltTys.clear(); EltTys.push_back(PointerType::getUnqual(StackEntryTy)); EltTys.push_back(FrameMapPtrTy); @@ -301,10 +294,9 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) { if (!Head) { // If the root chain does not exist, insert a new one with linkonce // linkage! - Head = new GlobalVariable(M, StackEntryPtrTy, false, - GlobalValue::LinkOnceAnyLinkage, - Constant::getNullValue(StackEntryPtrTy), - "llvm_gc_root_chain"); + Head = new GlobalVariable( + M, StackEntryPtrTy, false, GlobalValue::LinkOnceAnyLinkage, + Constant::getNullValue(StackEntryPtrTy), "llvm_gc_root_chain"); } else if (Head->hasExternalLinkage() && Head->isDeclaration()) { Head->setInitializer(Constant::getNullValue(StackEntryPtrTy)); Head->setLinkage(GlobalValue::LinkOnceAnyLinkage); @@ -326,15 +318,16 @@ void ShadowStackGC::CollectRoots(Function &F) { assert(Roots.empty() && "Not cleaned up?"); - SmallVector<std::pair<CallInst*, AllocaInst*>, 16> MetaRoots; + SmallVector<std::pair<CallInst *, AllocaInst *>, 16> MetaRoots; 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++)) if (Function *F = CI->getCalledFunction()) if (F->getIntrinsicID() == Intrinsic::gcroot) { - std::pair<CallInst*, AllocaInst*> Pair = std::make_pair( - CI, cast<AllocaInst>(CI->getArgOperand(0)->stripPointerCasts())); + std::pair<CallInst *, AllocaInst *> Pair = std::make_pair( + CI, + cast<AllocaInst>(CI->getArgOperand(0)->stripPointerCasts())); if (IsNullValue(CI->getArgOperand(1))) Roots.push_back(Pair); else @@ -346,24 +339,25 @@ void ShadowStackGC::CollectRoots(Function &F) { Roots.insert(Roots.begin(), MetaRoots.begin(), MetaRoots.end()); } -GetElementPtrInst * -ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr, - int Idx, int Idx2, const char *Name) { - Value *Indices[] = { ConstantInt::get(Type::getInt32Ty(Context), 0), - ConstantInt::get(Type::getInt32Ty(Context), Idx), - ConstantInt::get(Type::getInt32Ty(Context), Idx2) }; - Value* Val = B.CreateGEP(BasePtr, Indices, Name); +GetElementPtrInst *ShadowStackGC::CreateGEP(LLVMContext &Context, + IRBuilder<> &B, Value *BasePtr, + int Idx, int Idx2, + const char *Name) { + Value *Indices[] = {ConstantInt::get(Type::getInt32Ty(Context), 0), + ConstantInt::get(Type::getInt32Ty(Context), Idx), + ConstantInt::get(Type::getInt32Ty(Context), Idx2)}; + Value *Val = B.CreateGEP(BasePtr, Indices, Name); assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant"); return dyn_cast<GetElementPtrInst>(Val); } -GetElementPtrInst * -ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr, - int Idx, const char *Name) { - Value *Indices[] = { ConstantInt::get(Type::getInt32Ty(Context), 0), - ConstantInt::get(Type::getInt32Ty(Context), Idx) }; +GetElementPtrInst *ShadowStackGC::CreateGEP(LLVMContext &Context, + IRBuilder<> &B, Value *BasePtr, + int Idx, const char *Name) { + Value *Indices[] = {ConstantInt::get(Type::getInt32Ty(Context), 0), + ConstantInt::get(Type::getInt32Ty(Context), Idx)}; Value *Val = B.CreateGEP(BasePtr, Indices, Name); assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant"); @@ -374,7 +368,7 @@ ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr, /// runOnFunction - Insert code to maintain the shadow stack. bool ShadowStackGC::performCustomLowering(Function &F) { LLVMContext &Context = F.getContext(); - + // Find calls to llvm.gcroot. CollectRoots(F); @@ -391,16 +385,17 @@ bool ShadowStackGC::performCustomLowering(Function &F) { BasicBlock::iterator IP = F.getEntryBlock().begin(); IRBuilder<> AtEntry(IP->getParent(), IP); - Instruction *StackEntry = AtEntry.CreateAlloca(ConcreteStackEntryTy, nullptr, - "gc_frame"); + Instruction *StackEntry = + AtEntry.CreateAlloca(ConcreteStackEntryTy, nullptr, "gc_frame"); - while (isa<AllocaInst>(IP)) ++IP; + while (isa<AllocaInst>(IP)) + ++IP; AtEntry.SetInsertPoint(IP->getParent(), IP); // Initialize the map pointer and load the current head of the shadow stack. - Instruction *CurrentHead = AtEntry.CreateLoad(Head, "gc_currhead"); - Instruction *EntryMapPtr = CreateGEP(Context, AtEntry, StackEntry, - 0,1,"gc_frame.map"); + Instruction *CurrentHead = AtEntry.CreateLoad(Head, "gc_currhead"); + Instruction *EntryMapPtr = + CreateGEP(Context, AtEntry, StackEntry, 0, 1, "gc_frame.map"); AtEntry.CreateStore(FrameMap, EntryMapPtr); // After all the allocas... @@ -418,14 +413,15 @@ bool ShadowStackGC::performCustomLowering(Function &F) { // really necessary (the collector would never see the intermediate state at // runtime), but it's nicer not to push the half-initialized entry onto the // shadow stack. - while (isa<StoreInst>(IP)) ++IP; + while (isa<StoreInst>(IP)) + ++IP; AtEntry.SetInsertPoint(IP->getParent(), IP); // Push the entry onto the shadow stack. - Instruction *EntryNextPtr = CreateGEP(Context, AtEntry, - StackEntry,0,0,"gc_frame.next"); - Instruction *NewHeadVal = CreateGEP(Context, AtEntry, - StackEntry, 0, "gc_newhead"); + Instruction *EntryNextPtr = + CreateGEP(Context, AtEntry, StackEntry, 0, 0, "gc_frame.next"); + Instruction *NewHeadVal = + CreateGEP(Context, AtEntry, StackEntry, 0, "gc_newhead"); AtEntry.CreateStore(CurrentHead, EntryNextPtr); AtEntry.CreateStore(NewHeadVal, Head); @@ -434,10 +430,10 @@ bool ShadowStackGC::performCustomLowering(Function &F) { while (IRBuilder<> *AtExit = EE.Next()) { // Pop the entry from the shadow stack. Don't reuse CurrentHead from // AtEntry, since that would make the value live for the entire function. - Instruction *EntryNextPtr2 = CreateGEP(Context, *AtExit, StackEntry, 0, 0, - "gc_frame.next"); + Instruction *EntryNextPtr2 = + CreateGEP(Context, *AtExit, StackEntry, 0, 0, "gc_frame.next"); Value *SavedHead = AtExit->CreateLoad(EntryNextPtr2, "gc_savedhead"); - AtExit->CreateStore(SavedHead, Head); + AtExit->CreateStore(SavedHead, Head); } // Delete the original allocas (which are no longer used) and the intrinsic diff --git a/llvm/lib/CodeGen/StatepointExampleGC.cpp b/llvm/lib/CodeGen/StatepointExampleGC.cpp index e20b270f42d..ed6d0e927f0 100644 --- a/llvm/lib/CodeGen/StatepointExampleGC.cpp +++ b/llvm/lib/CodeGen/StatepointExampleGC.cpp @@ -12,7 +12,7 @@ // suitable as a default implementation usable with any collector which can // consume the standard stackmap format generated by statepoints, uses the // default addrespace to distinguish between gc managed and non-gc managed -// pointers, and has reasonable relocation semantics. +// pointers, and has reasonable relocation semantics. // //===----------------------------------------------------------------------===// @@ -45,8 +45,8 @@ public: }; } -static GCRegistry::Add<StatepointGC> -X("statepoint-example", "an example strategy for statepoint"); +static GCRegistry::Add<StatepointGC> X("statepoint-example", + "an example strategy for statepoint"); namespace llvm { void linkStatepointExampleGC() {} |