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