OSDN Git Service

Update aosp/master LLVM for rebase to r256229
[android-x86/external-llvm.git] / lib / CodeGen / LowerEmuTLS.cpp
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/Transforms/Scalar.h"
18 #include "llvm/ADT/SmallVector.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 (!TM || !TM->Options.EmulatedTLS)
67     return false;
68
69   bool Changed = false;
70   SmallVector<const GlobalVariable*, 8> TlsVars;
71   for (const auto &G : M.globals()) {
72     if (G.isThreadLocal())
73       TlsVars.append({&G});
74   }
75   for (const auto G : TlsVars)
76     Changed |= addEmuTlsVar(M, G);
77   return Changed;
78 }
79
80 bool LowerEmuTLS::addEmuTlsVar(Module &M, const GlobalVariable *GV) {
81   LLVMContext &C = M.getContext();
82   PointerType *VoidPtrType = Type::getInt8PtrTy(C);
83
84   std::string EmuTlsVarName = ("__emutls_v." + GV->getName()).str();
85   GlobalVariable *EmuTlsVar = M.getNamedGlobal(EmuTlsVarName);
86   if (EmuTlsVar)
87     return false;  // It has been added before.
88
89   const DataLayout &DL = M.getDataLayout();
90   Constant *NullPtr = ConstantPointerNull::get(VoidPtrType);
91
92   // Get non-zero initializer from GV's initializer.
93   const Constant *InitValue = nullptr;
94   if (GV->hasInitializer()) {
95     InitValue = GV->getInitializer();
96     const ConstantInt *InitIntValue = dyn_cast<ConstantInt>(InitValue);
97     // When GV's init value is all 0, omit the EmuTlsTmplVar and let
98     // the emutls library function to reset newly allocated TLS variables.
99     if (isa<ConstantAggregateZero>(InitValue) ||
100         (InitIntValue && InitIntValue->isZero()))
101       InitValue = nullptr;
102   }
103
104   // Create the __emutls_v. symbol, whose type has 4 fields:
105   //     word size;   // size of GV in bytes
106   //     word align;  // alignment of GV
107   //     void *ptr;   // initialized to 0; set at run time per thread.
108   //     void *templ; // 0 or point to __emutls_t.*
109   // sizeof(word) should be the same as sizeof(void*) on target.
110   IntegerType *WordType = DL.getIntPtrType(C);
111   PointerType *InitPtrType = InitValue ?
112       PointerType::getUnqual(InitValue->getType()) : VoidPtrType;
113   Type *ElementTypes[4] = {WordType, WordType, VoidPtrType, InitPtrType};
114   ArrayRef<Type*> ElementTypeArray(ElementTypes, 4);
115   StructType *EmuTlsVarType = StructType::create(ElementTypeArray);
116   EmuTlsVar = cast<GlobalVariable>(
117       M.getOrInsertGlobal(EmuTlsVarName, EmuTlsVarType));
118   copyLinkageVisibility(M, GV, EmuTlsVar);
119
120   // Define "__emutls_t.*" and "__emutls_v.*" only if GV is defined.
121   if (!GV->hasInitializer())
122     return true;
123
124   Type *GVType = GV->getValueType();
125   unsigned GVAlignment = GV->getAlignment();
126   if (!GVAlignment) {
127     // When LLVM IL declares a variable without alignment, use
128     // the ABI default alignment for the type.
129     GVAlignment = DL.getABITypeAlignment(GVType);
130   }
131
132   // Define "__emutls_t.*" if there is InitValue
133   GlobalVariable *EmuTlsTmplVar = nullptr;
134   if (InitValue) {
135     std::string EmuTlsTmplName = ("__emutls_t." + GV->getName()).str();
136     EmuTlsTmplVar = dyn_cast_or_null<GlobalVariable>(
137         M.getOrInsertGlobal(EmuTlsTmplName, GVType));
138     assert(EmuTlsTmplVar && "Failed to create emualted TLS initializer");
139     EmuTlsTmplVar->setConstant(true);
140     EmuTlsTmplVar->setInitializer(const_cast<Constant*>(InitValue));
141     EmuTlsTmplVar->setAlignment(GVAlignment);
142     copyLinkageVisibility(M, GV, EmuTlsTmplVar);
143   }
144
145   // Define "__emutls_v.*" with initializer and alignment.
146   Constant *ElementValues[4] = {
147       ConstantInt::get(WordType, DL.getTypeStoreSize(GVType)),
148       ConstantInt::get(WordType, GVAlignment),
149       NullPtr, EmuTlsTmplVar ? EmuTlsTmplVar : NullPtr
150   };
151   ArrayRef<Constant*> ElementValueArray(ElementValues, 4);
152   EmuTlsVar->setInitializer(
153       ConstantStruct::get(EmuTlsVarType, ElementValueArray));
154   unsigned MaxAlignment = std::max(
155       DL.getABITypeAlignment(WordType),
156       DL.getABITypeAlignment(VoidPtrType));
157   EmuTlsVar->setAlignment(MaxAlignment);
158   return true;
159 }