]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/lib/Target/XCore/XCoreLowerThreadLocal.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / lib / Target / XCore / XCoreLowerThreadLocal.cpp
1 //===-- XCoreLowerThreadLocal - Lower thread local 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 /// \file
11 /// \brief This file contains a pass that lowers thread local variables on the
12 ///        XCore.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #include "XCore.h"
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"
25
26 #define DEBUG_TYPE "xcore-lower-thread-local"
27
28 using namespace llvm;
29
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));
34
35 namespace {
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 {
40     static char ID;
41
42     XCoreLowerThreadLocal() : ModulePass(ID) {
43       initializeXCoreLowerThreadLocalPass(*PassRegistry::getPassRegistry());
44     }
45
46     bool lowerGlobal(GlobalVariable *GV);
47
48     bool runOnModule(Module &M);
49   };
50 }
51
52 char XCoreLowerThreadLocal::ID = 0;
53
54 INITIALIZE_PASS(XCoreLowerThreadLocal, "xcore-lower-thread-local",
55                 "Lower thread local variables", false, false)
56
57 ModulePass *llvm::createXCoreLowerThreadLocalPass() {
58   return new XCoreLowerThreadLocal();
59 }
60
61 static ArrayType *createLoweredType(Type *OriginalType) {
62   return ArrayType::get(OriginalType, MaxThreads);
63 }
64
65 static Constant *
66 createLoweredInitializer(ArrayType *NewType, Constant *OriginalInitializer) {
67   SmallVector<Constant *, 8> Elements(MaxThreads);
68   for (unsigned i = 0; i != MaxThreads; ++i) {
69     Elements[i] = OriginalInitializer;
70   }
71   return ConstantArray::get(NewType, Elements);
72 }
73
74 static bool hasNonInstructionUse(GlobalVariable *GV) {
75   for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E;
76        ++UI)
77     if (!isa<Instruction>(*UI))
78       return true;
79
80   return false;
81 }
82
83 static bool isZeroLengthArray(Type *Ty) {
84   ArrayType *AT = dyn_cast<ArrayType>(Ty);
85   return AT && (AT->getNumElements() == 0);
86 }
87
88 bool XCoreLowerThreadLocal::lowerGlobal(GlobalVariable *GV) {
89   Module *M = GV->getParent();
90   LLVMContext &Ctx = M->getContext();
91   if (!GV->isThreadLocal())
92     return false;
93
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()))
97     return false;
98
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());
108
109   // Update uses.
110   SmallVector<User *, 16> Users(GV->use_begin(), GV->use_end());
111   for (unsigned I = 0, E = Users.size(); I != E; ++I) {
112     User *U = Users[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);
123   }
124
125   // Remove old global.
126   NewGV->takeName(GV);
127   GV->eraseFromParent();
128   return true;
129 }
130
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();
136        GVI != E; ++GVI) {
137     GlobalVariable *GV = GVI;
138     if (GV->isThreadLocal())
139       ThreadLocalGlobals.push_back(GV);
140   }
141   for (unsigned I = 0, E = ThreadLocalGlobals.size(); I != E; ++I) {
142     MadeChange |= lowerGlobal(ThreadLocalGlobals[I]);
143   }
144   return MadeChange;
145 }