LowerEmuTLS.cpp 5.8 KB

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