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