1 //===-- XCoreLowerThreadLocal - Lower thread local variables --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file contains a pass that lowers thread local variables on the
14 //===----------------------------------------------------------------------===//
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/GlobalVariable.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/CommandLine.h"
26 #define DEBUG_TYPE "xcore-lower-thread-local"
30 static cl::opt<unsigned> MaxThreads(
31 "xcore-max-threads", cl::Optional,
32 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
33 cl::Hidden, cl::value_desc("number"), cl::init(8));
36 /// Lowers thread local variables on the XCore. Each thread local variable is
37 /// expanded to an array of n elements indexed by the thread ID where n is the
38 /// fixed number hardware threads supported by the device.
39 struct XCoreLowerThreadLocal : public ModulePass {
42 XCoreLowerThreadLocal() : ModulePass(ID) {
43 initializeXCoreLowerThreadLocalPass(*PassRegistry::getPassRegistry());
46 bool lowerGlobal(GlobalVariable *GV);
48 bool runOnModule(Module &M);
52 char XCoreLowerThreadLocal::ID = 0;
54 INITIALIZE_PASS(XCoreLowerThreadLocal, "xcore-lower-thread-local",
55 "Lower thread local variables", false, false)
57 ModulePass *llvm::createXCoreLowerThreadLocalPass() {
58 return new XCoreLowerThreadLocal();
61 static ArrayType *createLoweredType(Type *OriginalType) {
62 return ArrayType::get(OriginalType, MaxThreads);
66 createLoweredInitializer(ArrayType *NewType, Constant *OriginalInitializer) {
67 SmallVector<Constant *, 8> Elements(MaxThreads);
68 for (unsigned i = 0; i != MaxThreads; ++i) {
69 Elements[i] = OriginalInitializer;
71 return ConstantArray::get(NewType, Elements);
74 static bool hasNonInstructionUse(GlobalVariable *GV) {
75 for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E;
77 if (!isa<Instruction>(*UI))
83 static bool isZeroLengthArray(Type *Ty) {
84 ArrayType *AT = dyn_cast<ArrayType>(Ty);
85 return AT && (AT->getNumElements() == 0);
88 bool XCoreLowerThreadLocal::lowerGlobal(GlobalVariable *GV) {
89 Module *M = GV->getParent();
90 LLVMContext &Ctx = M->getContext();
91 if (!GV->isThreadLocal())
94 // Skip globals that we can't lower and leave it for the backend to error.
95 if (hasNonInstructionUse(GV) ||
96 !GV->getType()->isSized() || isZeroLengthArray(GV->getType()))
99 // Create replacement global.
100 ArrayType *NewType = createLoweredType(GV->getType()->getElementType());
101 Constant *NewInitializer = createLoweredInitializer(NewType,
102 GV->getInitializer());
103 GlobalVariable *NewGV =
104 new GlobalVariable(*M, NewType, GV->isConstant(), GV->getLinkage(),
105 NewInitializer, "", 0, GlobalVariable::NotThreadLocal,
106 GV->getType()->getAddressSpace(),
107 GV->isExternallyInitialized());
110 SmallVector<User *, 16> Users(GV->use_begin(), GV->use_end());
111 for (unsigned I = 0, E = Users.size(); I != E; ++I) {
113 Instruction *Inst = cast<Instruction>(U);
114 IRBuilder<> Builder(Inst);
115 Function *GetID = Intrinsic::getDeclaration(GV->getParent(),
116 Intrinsic::xcore_getid);
117 Value *ThreadID = Builder.CreateCall(GetID);
118 SmallVector<Value *, 2> Indices;
119 Indices.push_back(Constant::getNullValue(Type::getInt64Ty(Ctx)));
120 Indices.push_back(ThreadID);
121 Value *Addr = Builder.CreateInBoundsGEP(NewGV, Indices);
122 U->replaceUsesOfWith(GV, Addr);
125 // Remove old global.
127 GV->eraseFromParent();
131 bool XCoreLowerThreadLocal::runOnModule(Module &M) {
132 // Find thread local globals.
133 bool MadeChange = false;
134 SmallVector<GlobalVariable *, 16> ThreadLocalGlobals;
135 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
137 GlobalVariable *GV = GVI;
138 if (GV->isThreadLocal())
139 ThreadLocalGlobals.push_back(GV);
141 for (unsigned I = 0, E = ThreadLocalGlobals.size(); I != E; ++I) {
142 MadeChange |= lowerGlobal(ThreadLocalGlobals[I]);