XCoreLowerThreadLocal.cpp 8.4 KB

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