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/IRBuilder.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/NoFolder.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
28 
29 #define DEBUG_TYPE "xcore-lower-thread-local"
30 
31 using namespace llvm;
32 
33 static cl::opt<unsigned> MaxThreads(
34   "xcore-max-threads", cl::Optional,
35   cl::desc("Maximum number of threads (for emulation thread-local storage)"),
36   cl::Hidden, cl::value_desc("number"), cl::init(8));
37 
38 namespace {
39   /// Lowers thread local variables on the XCore. Each thread local variable is
40   /// expanded to an array of n elements indexed by the thread ID where n is the
41   /// fixed number hardware threads supported by the device.
42   struct XCoreLowerThreadLocal : public ModulePass {
43     static char ID;
44 
XCoreLowerThreadLocal__anonf1c0e3850111::XCoreLowerThreadLocal45     XCoreLowerThreadLocal() : ModulePass(ID) {
46       initializeXCoreLowerThreadLocalPass(*PassRegistry::getPassRegistry());
47     }
48 
49     bool lowerGlobal(GlobalVariable *GV);
50 
51     bool runOnModule(Module &M) override;
52   };
53 }
54 
55 char XCoreLowerThreadLocal::ID = 0;
56 
57 INITIALIZE_PASS(XCoreLowerThreadLocal, "xcore-lower-thread-local",
58                 "Lower thread local variables", false, false)
59 
createXCoreLowerThreadLocalPass()60 ModulePass *llvm::createXCoreLowerThreadLocalPass() {
61   return new XCoreLowerThreadLocal();
62 }
63 
createLoweredType(Type * OriginalType)64 static ArrayType *createLoweredType(Type *OriginalType) {
65   return ArrayType::get(OriginalType, MaxThreads);
66 }
67 
68 static Constant *
createLoweredInitializer(ArrayType * NewType,Constant * OriginalInitializer)69 createLoweredInitializer(ArrayType *NewType, Constant *OriginalInitializer) {
70   SmallVector<Constant *, 8> Elements(MaxThreads);
71   for (unsigned i = 0; i != MaxThreads; ++i) {
72     Elements[i] = OriginalInitializer;
73   }
74   return ConstantArray::get(NewType, Elements);
75 }
76 
77 static Instruction *
createReplacementInstr(ConstantExpr * CE,Instruction * Instr)78 createReplacementInstr(ConstantExpr *CE, Instruction *Instr) {
79   IRBuilder<true,NoFolder> Builder(Instr);
80   unsigned OpCode = CE->getOpcode();
81   switch (OpCode) {
82     case Instruction::GetElementPtr: {
83       SmallVector<Value *,4> CEOpVec(CE->op_begin(), CE->op_end());
84       ArrayRef<Value *> CEOps(CEOpVec);
85       return dyn_cast<Instruction>(Builder.CreateInBoundsGEP(
86           cast<GEPOperator>(CE)->getSourceElementType(), CEOps[0],
87           CEOps.slice(1)));
88     }
89     case Instruction::Add:
90     case Instruction::Sub:
91     case Instruction::Mul:
92     case Instruction::UDiv:
93     case Instruction::SDiv:
94     case Instruction::FDiv:
95     case Instruction::URem:
96     case Instruction::SRem:
97     case Instruction::FRem:
98     case Instruction::Shl:
99     case Instruction::LShr:
100     case Instruction::AShr:
101     case Instruction::And:
102     case Instruction::Or:
103     case Instruction::Xor:
104       return dyn_cast<Instruction>(
105                   Builder.CreateBinOp((Instruction::BinaryOps)OpCode,
106                                       CE->getOperand(0), CE->getOperand(1),
107                                       CE->getName()));
108     case Instruction::Trunc:
109     case Instruction::ZExt:
110     case Instruction::SExt:
111     case Instruction::FPToUI:
112     case Instruction::FPToSI:
113     case Instruction::UIToFP:
114     case Instruction::SIToFP:
115     case Instruction::FPTrunc:
116     case Instruction::FPExt:
117     case Instruction::PtrToInt:
118     case Instruction::IntToPtr:
119     case Instruction::BitCast:
120       return dyn_cast<Instruction>(
121                   Builder.CreateCast((Instruction::CastOps)OpCode,
122                                      CE->getOperand(0), CE->getType(),
123                                      CE->getName()));
124     default:
125       llvm_unreachable("Unhandled constant expression!\n");
126   }
127 }
128 
replaceConstantExprOp(ConstantExpr * CE,Pass * P)129 static bool replaceConstantExprOp(ConstantExpr *CE, Pass *P) {
130   do {
131     SmallVector<WeakVH,8> WUsers(CE->user_begin(), CE->user_end());
132     std::sort(WUsers.begin(), WUsers.end());
133     WUsers.erase(std::unique(WUsers.begin(), WUsers.end()), WUsers.end());
134     while (!WUsers.empty())
135       if (WeakVH WU = WUsers.pop_back_val()) {
136         if (PHINode *PN = dyn_cast<PHINode>(WU)) {
137           for (int I = 0, E = PN->getNumIncomingValues(); I < E; ++I)
138             if (PN->getIncomingValue(I) == CE) {
139               BasicBlock *PredBB = PN->getIncomingBlock(I);
140               if (PredBB->getTerminator()->getNumSuccessors() > 1)
141                 PredBB = SplitEdge(PredBB, PN->getParent());
142               Instruction *InsertPos = PredBB->getTerminator();
143               Instruction *NewInst = createReplacementInstr(CE, InsertPos);
144               PN->setOperand(I, NewInst);
145             }
146         } else if (Instruction *Instr = dyn_cast<Instruction>(WU)) {
147           Instruction *NewInst = createReplacementInstr(CE, Instr);
148           Instr->replaceUsesOfWith(CE, NewInst);
149         } else {
150           ConstantExpr *CExpr = dyn_cast<ConstantExpr>(WU);
151           if (!CExpr || !replaceConstantExprOp(CExpr, P))
152             return false;
153         }
154       }
155   } while (CE->hasNUsesOrMore(1)); // We need to check because a recursive
156   // sibling may have used 'CE' when createReplacementInstr was called.
157   CE->destroyConstant();
158   return true;
159 }
160 
rewriteNonInstructionUses(GlobalVariable * GV,Pass * P)161 static bool rewriteNonInstructionUses(GlobalVariable *GV, Pass *P) {
162   SmallVector<WeakVH,8> WUsers;
163   for (User *U : GV->users())
164     if (!isa<Instruction>(U))
165       WUsers.push_back(WeakVH(U));
166   while (!WUsers.empty())
167     if (WeakVH WU = WUsers.pop_back_val()) {
168       ConstantExpr *CE = dyn_cast<ConstantExpr>(WU);
169       if (!CE || !replaceConstantExprOp(CE, P))
170         return false;
171     }
172   return true;
173 }
174 
isZeroLengthArray(Type * Ty)175 static bool isZeroLengthArray(Type *Ty) {
176   ArrayType *AT = dyn_cast<ArrayType>(Ty);
177   return AT && (AT->getNumElements() == 0);
178 }
179 
lowerGlobal(GlobalVariable * GV)180 bool XCoreLowerThreadLocal::lowerGlobal(GlobalVariable *GV) {
181   Module *M = GV->getParent();
182   LLVMContext &Ctx = M->getContext();
183   if (!GV->isThreadLocal())
184     return false;
185 
186   // Skip globals that we can't lower and leave it for the backend to error.
187   if (!rewriteNonInstructionUses(GV, this) ||
188       !GV->getType()->isSized() || isZeroLengthArray(GV->getType()))
189     return false;
190 
191   // Create replacement global.
192   ArrayType *NewType = createLoweredType(GV->getType()->getElementType());
193   Constant *NewInitializer = nullptr;
194   if (GV->hasInitializer())
195     NewInitializer = createLoweredInitializer(NewType,
196                                               GV->getInitializer());
197   GlobalVariable *NewGV =
198     new GlobalVariable(*M, NewType, GV->isConstant(), GV->getLinkage(),
199                        NewInitializer, "", nullptr,
200                        GlobalVariable::NotThreadLocal,
201                        GV->getType()->getAddressSpace(),
202                        GV->isExternallyInitialized());
203 
204   // Update uses.
205   SmallVector<User *, 16> Users(GV->user_begin(), GV->user_end());
206   for (unsigned I = 0, E = Users.size(); I != E; ++I) {
207     User *U = Users[I];
208     Instruction *Inst = cast<Instruction>(U);
209     IRBuilder<> Builder(Inst);
210     Function *GetID = Intrinsic::getDeclaration(GV->getParent(),
211                                                 Intrinsic::xcore_getid);
212     Value *ThreadID = Builder.CreateCall(GetID, {});
213     SmallVector<Value *, 2> Indices;
214     Indices.push_back(Constant::getNullValue(Type::getInt64Ty(Ctx)));
215     Indices.push_back(ThreadID);
216     Value *Addr =
217         Builder.CreateInBoundsGEP(NewGV->getValueType(), NewGV, Indices);
218     U->replaceUsesOfWith(GV, Addr);
219   }
220 
221   // Remove old global.
222   NewGV->takeName(GV);
223   GV->eraseFromParent();
224   return true;
225 }
226 
runOnModule(Module & M)227 bool XCoreLowerThreadLocal::runOnModule(Module &M) {
228   // Find thread local globals.
229   bool MadeChange = false;
230   SmallVector<GlobalVariable *, 16> ThreadLocalGlobals;
231   for (GlobalVariable &GV : M.globals())
232     if (GV.isThreadLocal())
233       ThreadLocalGlobals.push_back(&GV);
234   for (unsigned I = 0, E = ThreadLocalGlobals.size(); I != E; ++I) {
235     MadeChange |= lowerGlobal(ThreadLocalGlobals[I]);
236   }
237   return MadeChange;
238 }
239