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