LowerEmuTLS.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //===- LowerEmuTLS.cpp - Add __emutls_[vt].* variables --------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This transformation is required for targets depending on libgcc style
  10. // emulated thread local storage variables. For every defined TLS variable xyz,
  11. // an __emutls_v.xyz is generated. If there is non-zero initialized value
  12. // an __emutls_t.xyz is also generated.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/CodeGen/Passes.h"
  17. #include "llvm/CodeGen/TargetPassConfig.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/LLVMContext.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/InitializePasses.h"
  22. #include "llvm/Pass.h"
  23. #include "llvm/Target/TargetMachine.h"
  24. using namespace llvm;
  25. #define DEBUG_TYPE "loweremutls"
  26. namespace {
  27. class LowerEmuTLS : public ModulePass {
  28. public:
  29. static char ID; // Pass identification, replacement for typeid
  30. LowerEmuTLS() : ModulePass(ID) {
  31. initializeLowerEmuTLSPass(*PassRegistry::getPassRegistry());
  32. }
  33. bool runOnModule(Module &M) override;
  34. private:
  35. bool addEmuTlsVar(Module &M, const GlobalVariable *GV);
  36. static void copyLinkageVisibility(Module &M,
  37. const GlobalVariable *from,
  38. GlobalVariable *to) {
  39. to->setLinkage(from->getLinkage());
  40. to->setVisibility(from->getVisibility());
  41. to->setDSOLocal(from->isDSOLocal());
  42. if (from->hasComdat()) {
  43. to->setComdat(M.getOrInsertComdat(to->getName()));
  44. to->getComdat()->setSelectionKind(from->getComdat()->getSelectionKind());
  45. }
  46. }
  47. };
  48. }
  49. char LowerEmuTLS::ID = 0;
  50. INITIALIZE_PASS(LowerEmuTLS, DEBUG_TYPE,
  51. "Add __emutls_[vt]. variables for emultated TLS model", false,
  52. false)
  53. ModulePass *llvm::createLowerEmuTLSPass() { return new LowerEmuTLS(); }
  54. bool LowerEmuTLS::runOnModule(Module &M) {
  55. if (skipModule(M))
  56. return false;
  57. auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
  58. if (!TPC)
  59. return false;
  60. auto &TM = TPC->getTM<TargetMachine>();
  61. if (!TM.useEmulatedTLS())
  62. return false;
  63. bool Changed = false;
  64. SmallVector<const GlobalVariable*, 8> TlsVars;
  65. for (const auto &G : M.globals()) {
  66. if (G.isThreadLocal())
  67. TlsVars.append({&G});
  68. }
  69. for (const auto G : TlsVars)
  70. Changed |= addEmuTlsVar(M, G);
  71. return Changed;
  72. }
  73. bool LowerEmuTLS::addEmuTlsVar(Module &M, const GlobalVariable *GV) {
  74. LLVMContext &C = M.getContext();
  75. PointerType *VoidPtrType = Type::getInt8PtrTy(C);
  76. std::string EmuTlsVarName = ("__emutls_v." + GV->getName()).str();
  77. GlobalVariable *EmuTlsVar = M.getNamedGlobal(EmuTlsVarName);
  78. if (EmuTlsVar)
  79. return false; // It has been added before.
  80. const DataLayout &DL = M.getDataLayout();
  81. Constant *NullPtr = ConstantPointerNull::get(VoidPtrType);
  82. // Get non-zero initializer from GV's initializer.
  83. const Constant *InitValue = nullptr;
  84. if (GV->hasInitializer()) {
  85. InitValue = GV->getInitializer();
  86. const ConstantInt *InitIntValue = dyn_cast<ConstantInt>(InitValue);
  87. // When GV's init value is all 0, omit the EmuTlsTmplVar and let
  88. // the emutls library function to reset newly allocated TLS variables.
  89. if (isa<ConstantAggregateZero>(InitValue) ||
  90. (InitIntValue && InitIntValue->isZero()))
  91. InitValue = nullptr;
  92. }
  93. // Create the __emutls_v. symbol, whose type has 4 fields:
  94. // word size; // size of GV in bytes
  95. // word align; // alignment of GV
  96. // void *ptr; // initialized to 0; set at run time per thread.
  97. // void *templ; // 0 or point to __emutls_t.*
  98. // sizeof(word) should be the same as sizeof(void*) on target.
  99. IntegerType *WordType = DL.getIntPtrType(C);
  100. PointerType *InitPtrType = InitValue ?
  101. PointerType::getUnqual(InitValue->getType()) : VoidPtrType;
  102. Type *ElementTypes[4] = {WordType, WordType, VoidPtrType, InitPtrType};
  103. ArrayRef<Type*> ElementTypeArray(ElementTypes, 4);
  104. StructType *EmuTlsVarType = StructType::create(ElementTypeArray);
  105. EmuTlsVar = cast<GlobalVariable>(
  106. M.getOrInsertGlobal(EmuTlsVarName, EmuTlsVarType));
  107. copyLinkageVisibility(M, GV, EmuTlsVar);
  108. // Define "__emutls_t.*" and "__emutls_v.*" only if GV is defined.
  109. if (!GV->hasInitializer())
  110. return true;
  111. Type *GVType = GV->getValueType();
  112. Align GVAlignment = DL.getValueOrABITypeAlignment(GV->getAlign(), GVType);
  113. // Define "__emutls_t.*" if there is InitValue
  114. GlobalVariable *EmuTlsTmplVar = nullptr;
  115. if (InitValue) {
  116. std::string EmuTlsTmplName = ("__emutls_t." + GV->getName()).str();
  117. EmuTlsTmplVar = dyn_cast_or_null<GlobalVariable>(
  118. M.getOrInsertGlobal(EmuTlsTmplName, GVType));
  119. assert(EmuTlsTmplVar && "Failed to create emualted TLS initializer");
  120. EmuTlsTmplVar->setConstant(true);
  121. EmuTlsTmplVar->setInitializer(const_cast<Constant*>(InitValue));
  122. EmuTlsTmplVar->setAlignment(GVAlignment);
  123. copyLinkageVisibility(M, GV, EmuTlsTmplVar);
  124. }
  125. // Define "__emutls_v.*" with initializer and alignment.
  126. Constant *ElementValues[4] = {
  127. ConstantInt::get(WordType, DL.getTypeStoreSize(GVType)),
  128. ConstantInt::get(WordType, GVAlignment.value()), NullPtr,
  129. EmuTlsTmplVar ? EmuTlsTmplVar : NullPtr};
  130. ArrayRef<Constant*> ElementValueArray(ElementValues, 4);
  131. EmuTlsVar->setInitializer(
  132. ConstantStruct::get(EmuTlsVarType, ElementValueArray));
  133. Align MaxAlignment =
  134. std::max(DL.getABITypeAlign(WordType), DL.getABITypeAlign(VoidPtrType));
  135. EmuTlsVar->setAlignment(MaxAlignment);
  136. return true;
  137. }