LowerEmuTLS.cpp 5.7 KB

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