diff options
Diffstat (limited to 'lib/Target/WebAssembly/WebAssemblyLowerGlobalDtors.cpp')
| -rw-r--r-- | lib/Target/WebAssembly/WebAssemblyLowerGlobalDtors.cpp | 84 |
1 files changed, 40 insertions, 44 deletions
diff --git a/lib/Target/WebAssembly/WebAssemblyLowerGlobalDtors.cpp b/lib/Target/WebAssembly/WebAssemblyLowerGlobalDtors.cpp index ee708d637b256..84c877cb8d028 100644 --- a/lib/Target/WebAssembly/WebAssemblyLowerGlobalDtors.cpp +++ b/lib/Target/WebAssembly/WebAssemblyLowerGlobalDtors.cpp @@ -18,15 +18,15 @@ //===----------------------------------------------------------------------===// #include "WebAssembly.h" +#include "llvm/ADT/MapVector.h" #include "llvm/IR/Constants.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/Module.h" -#include "llvm/Transforms/Utils/ModuleUtils.h" #include "llvm/Pass.h" -#include "llvm/ADT/MapVector.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/ModuleUtils.h" using namespace llvm; #define DEBUG_TYPE "wasm-lower-global-dtors" @@ -59,6 +59,8 @@ ModulePass *llvm::createWebAssemblyLowerGlobalDtors() { } bool LowerGlobalDtors::runOnModule(Module &M) { + LLVM_DEBUG(dbgs() << "********** Lower Global Destructors **********\n"); + GlobalVariable *GV = M.getGlobalVariable("llvm.global_dtors"); if (!GV) return false; @@ -77,18 +79,20 @@ bool LowerGlobalDtors::runOnModule(Module &M) { // Collect the contents of @llvm.global_dtors, collated by priority and // associated symbol. - std::map<uint16_t, MapVector<Constant *, std::vector<Constant *> > > DtorFuncs; + std::map<uint16_t, MapVector<Constant *, std::vector<Constant *>>> DtorFuncs; for (Value *O : InitList->operands()) { ConstantStruct *CS = dyn_cast<ConstantStruct>(O); - if (!CS) continue; // Malformed. + if (!CS) + continue; // Malformed. ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0)); - if (!Priority) continue; // Malformed. + if (!Priority) + continue; // Malformed. uint16_t PriorityValue = Priority->getLimitedValue(UINT16_MAX); Constant *DtorFunc = CS->getOperand(1); if (DtorFunc->isNullValue()) - break; // Found a null terminator, skip the rest. + break; // Found a null terminator, skip the rest. Constant *Associated = CS->getOperand(2); Associated = cast<Constant>(Associated->stripPointerCastsNoFollowAliases()); @@ -101,31 +105,23 @@ bool LowerGlobalDtors::runOnModule(Module &M) { // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d); LLVMContext &C = M.getContext(); PointerType *VoidStar = Type::getInt8PtrTy(C); - Type *AtExitFuncArgs[] = { VoidStar }; - FunctionType *AtExitFuncTy = FunctionType::get( - Type::getVoidTy(C), - AtExitFuncArgs, - /*isVarArg=*/false); - - Type *AtExitArgs[] = { - PointerType::get(AtExitFuncTy, 0), - VoidStar, - VoidStar - }; - FunctionType *AtExitTy = FunctionType::get( - Type::getInt32Ty(C), - AtExitArgs, - /*isVarArg=*/false); + Type *AtExitFuncArgs[] = {VoidStar}; + FunctionType *AtExitFuncTy = + FunctionType::get(Type::getVoidTy(C), AtExitFuncArgs, + /*isVarArg=*/false); + + Type *AtExitArgs[] = {PointerType::get(AtExitFuncTy, 0), VoidStar, VoidStar}; + FunctionType *AtExitTy = FunctionType::get(Type::getInt32Ty(C), AtExitArgs, + /*isVarArg=*/false); Constant *AtExit = M.getOrInsertFunction("__cxa_atexit", AtExitTy); // Declare __dso_local. Constant *DsoHandle = M.getNamedValue("__dso_handle"); if (!DsoHandle) { Type *DsoHandleTy = Type::getInt8Ty(C); - GlobalVariable *Handle = - new GlobalVariable(M, DsoHandleTy, /*isConstant=*/true, - GlobalVariable::ExternalWeakLinkage, - nullptr, "__dso_handle"); + GlobalVariable *Handle = new GlobalVariable( + M, DsoHandleTy, /*isConstant=*/true, + GlobalVariable::ExternalWeakLinkage, nullptr, "__dso_handle"); Handle->setVisibility(GlobalVariable::HiddenVisibility); DsoHandle = Handle; } @@ -139,13 +135,13 @@ bool LowerGlobalDtors::runOnModule(Module &M) { Constant *Associated = AssociatedAndMore.first; Function *CallDtors = Function::Create( - AtExitFuncTy, Function::PrivateLinkage, - "call_dtors" + - (Priority != UINT16_MAX ? - (Twine(".") + Twine(Priority)) : Twine()) + - (!Associated->isNullValue() ? - (Twine(".") + Associated->getName()) : Twine()), - &M); + AtExitFuncTy, Function::PrivateLinkage, + "call_dtors" + + (Priority != UINT16_MAX ? (Twine(".") + Twine(Priority)) + : Twine()) + + (!Associated->isNullValue() ? (Twine(".") + Associated->getName()) + : Twine()), + &M); BasicBlock *BB = BasicBlock::Create(C, "body", CallDtors); for (auto Dtor : AssociatedAndMore.second) @@ -155,29 +151,29 @@ bool LowerGlobalDtors::runOnModule(Module &M) { FunctionType *VoidVoid = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); Function *RegisterCallDtors = Function::Create( - VoidVoid, Function::PrivateLinkage, - "register_call_dtors" + - (Priority != UINT16_MAX ? - (Twine(".") + Twine(Priority)) : Twine()) + - (!Associated->isNullValue() ? - (Twine(".") + Associated->getName()) : Twine()), - &M); + VoidVoid, Function::PrivateLinkage, + "register_call_dtors" + + (Priority != UINT16_MAX ? (Twine(".") + Twine(Priority)) + : Twine()) + + (!Associated->isNullValue() ? (Twine(".") + Associated->getName()) + : Twine()), + &M); BasicBlock *EntryBB = BasicBlock::Create(C, "entry", RegisterCallDtors); BasicBlock *FailBB = BasicBlock::Create(C, "fail", RegisterCallDtors); BasicBlock *RetBB = BasicBlock::Create(C, "return", RegisterCallDtors); Value *Null = ConstantPointerNull::get(VoidStar); - Value *Args[] = { CallDtors, Null, DsoHandle }; + Value *Args[] = {CallDtors, Null, DsoHandle}; Value *Res = CallInst::Create(AtExit, Args, "call", EntryBB); Value *Cmp = new ICmpInst(*EntryBB, ICmpInst::ICMP_NE, Res, Constant::getNullValue(Res->getType())); BranchInst::Create(FailBB, RetBB, Cmp, EntryBB); // If `__cxa_atexit` hits out-of-memory, trap, so that we don't misbehave. - // This should be very rare, because if the process is running out of memory - // before main has even started, something is wrong. - CallInst::Create(Intrinsic::getDeclaration(&M, Intrinsic::trap), - "", FailBB); + // This should be very rare, because if the process is running out of + // memory before main has even started, something is wrong. + CallInst::Create(Intrinsic::getDeclaration(&M, Intrinsic::trap), "", + FailBB); new UnreachableInst(C, FailBB); ReturnInst::Create(C, RetBB); |
