| OLD | NEW |
| 1 //===- ExpandTls.cpp - Convert TLS variables to a concrete layout----------===// | 1 //===- ExpandTls.cpp - Convert TLS variables to a concrete layout----------===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This pass expands out uses of thread-local (TLS) variables into | 10 // This pass expands out uses of thread-local (TLS) variables into |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // | 22 // |
| 23 //===----------------------------------------------------------------------===// | 23 //===----------------------------------------------------------------------===// |
| 24 | 24 |
| 25 #include <vector> | 25 #include <vector> |
| 26 | 26 |
| 27 #include "llvm/Pass.h" | 27 #include "llvm/Pass.h" |
| 28 #include "llvm/IR/Constants.h" | 28 #include "llvm/IR/Constants.h" |
| 29 #include "llvm/IR/DataLayout.h" | 29 #include "llvm/IR/DataLayout.h" |
| 30 #include "llvm/IR/DerivedTypes.h" | 30 #include "llvm/IR/DerivedTypes.h" |
| 31 #include "llvm/IR/Instructions.h" | 31 #include "llvm/IR/Instructions.h" |
| 32 #include "llvm/IR/Intrinsics.h" |
| 32 #include "llvm/IR/Module.h" | 33 #include "llvm/IR/Module.h" |
| 33 #include "llvm/Support/raw_ostream.h" | 34 #include "llvm/Support/raw_ostream.h" |
| 34 #include "llvm/Transforms/NaCl.h" | 35 #include "llvm/Transforms/NaCl.h" |
| 35 | 36 |
| 36 using namespace llvm; | 37 using namespace llvm; |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 struct VarInfo { | 40 struct VarInfo { |
| 40 GlobalVariable *TlsVar; | 41 GlobalVariable *TlsVar; |
| 41 bool IsBss; // Whether variable is in zero-intialized part of template | 42 bool IsBss; // Whether variable is in zero-intialized part of template |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 ConstantInt::get(M.getContext(), APInt(32, State.Alignment))); | 227 ConstantInt::get(M.getContext(), APInt(32, State.Alignment))); |
| 227 setGlobalVariableValue(M, AlignmentSymbol, AlignmentVar); | 228 setGlobalVariableValue(M, AlignmentSymbol, AlignmentVar); |
| 228 AlignmentVar->setName(AlignmentSymbol); | 229 AlignmentVar->setName(AlignmentSymbol); |
| 229 | 230 |
| 230 return TemplatePtrType; | 231 return TemplatePtrType; |
| 231 } | 232 } |
| 232 | 233 |
| 233 static void rewriteTlsVars(Module &M, std::vector<VarInfo> *TlsVars, | 234 static void rewriteTlsVars(Module &M, std::vector<VarInfo> *TlsVars, |
| 234 PointerType *TemplatePtrType) { | 235 PointerType *TemplatePtrType) { |
| 235 // Set up the intrinsic that reads the thread pointer. | 236 // Set up the intrinsic that reads the thread pointer. |
| 236 Type *i8 = Type::getInt8Ty(M.getContext()); | 237 Function *ReadTpFunc = Intrinsic::getDeclaration(&M, Intrinsic::nacl_read_tp); |
| 237 FunctionType *ReadTpType = FunctionType::get(PointerType::get(i8, 0), | |
| 238 /*isVarArg=*/false); | |
| 239 AttrBuilder B; | |
| 240 B.addAttribute(Attribute::ReadOnly); | |
| 241 B.addAttribute(Attribute::NoUnwind); | |
| 242 AttributeSet ReadTpAttrs = AttributeSet::get( | |
| 243 M.getContext(), AttributeSet::FunctionIndex, B); | |
| 244 Constant *ReadTpFunc = M.getOrInsertTargetIntrinsic("llvm.nacl.read.tp", | |
| 245 ReadTpType, | |
| 246 ReadTpAttrs); | |
| 247 | 238 |
| 248 for (std::vector<VarInfo>::iterator VarInfo = TlsVars->begin(); | 239 for (std::vector<VarInfo>::iterator VarInfo = TlsVars->begin(); |
| 249 VarInfo != TlsVars->end(); | 240 VarInfo != TlsVars->end(); |
| 250 ++VarInfo) { | 241 ++VarInfo) { |
| 251 GlobalVariable *Var = VarInfo->TlsVar; | 242 GlobalVariable *Var = VarInfo->TlsVar; |
| 252 while (!Var->use_empty()) { | 243 while (!Var->use_empty()) { |
| 253 Use *U = &Var->use_begin().getUse(); | 244 Use *U = &Var->use_begin().getUse(); |
| 254 Instruction *InsertPt = PhiSafeInsertPt(U); | 245 Instruction *InsertPt = PhiSafeInsertPt(U); |
| 255 Value *RawThreadPtr = CallInst::Create(ReadTpFunc, "tls_raw", InsertPt); | 246 Value *RawThreadPtr = CallInst::Create(ReadTpFunc, "tls_raw", InsertPt); |
| 256 Value *TypedThreadPtr = new BitCastInst(RawThreadPtr, TemplatePtrType, | 247 Value *TypedThreadPtr = new BitCastInst(RawThreadPtr, TemplatePtrType, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 rewriteTlsVars(M, &TlsVars, TemplatePtrType); | 325 rewriteTlsVars(M, &TlsVars, TemplatePtrType); |
| 335 | 326 |
| 336 defineTlsLayoutIntrinsics(M); | 327 defineTlsLayoutIntrinsics(M); |
| 337 | 328 |
| 338 return true; | 329 return true; |
| 339 } | 330 } |
| 340 | 331 |
| 341 ModulePass *llvm::createExpandTlsPass() { | 332 ModulePass *llvm::createExpandTlsPass() { |
| 342 return new ExpandTls(); | 333 return new ExpandTls(); |
| 343 } | 334 } |
| OLD | NEW |