XCoreLowerThreadLocal.cpp revision 296417
1226584Sdim//===-- XCoreLowerThreadLocal - Lower thread local variables --------------===//
2226584Sdim//
3226584Sdim//                     The LLVM Compiler Infrastructure
4226584Sdim//
5226584Sdim// This file is distributed under the University of Illinois Open Source
6226584Sdim// License. See LICENSE.TXT for details.
7226584Sdim//
8226584Sdim//===----------------------------------------------------------------------===//
9226584Sdim///
10226584Sdim/// \file
11226584Sdim/// \brief This file contains a pass that lowers thread local variables on the
12226584Sdim///        XCore.
13226584Sdim///
14226584Sdim//===----------------------------------------------------------------------===//
15226584Sdim
16226584Sdim#include "XCore.h"
17226584Sdim#include "llvm/IR/Constants.h"
18249423Sdim#include "llvm/IR/DerivedTypes.h"
19226584Sdim#include "llvm/IR/GlobalVariable.h"
20226584Sdim#include "llvm/IR/IRBuilder.h"
21249423Sdim#include "llvm/IR/Intrinsics.h"
22234353Sdim#include "llvm/IR/Module.h"
23249423Sdim#include "llvm/IR/NoFolder.h"
24226584Sdim#include "llvm/IR/ValueHandle.h"
25249423Sdim#include "llvm/Pass.h"
26226584Sdim#include "llvm/Support/CommandLine.h"
27234353Sdim#include "llvm/Transforms/Utils/BasicBlockUtils.h"
28226584Sdim
29226584Sdim#define DEBUG_TYPE "xcore-lower-thread-local"
30226584Sdim
31234353Sdimusing namespace llvm;
32263508Sdim
33243830Sdimstatic cl::opt<unsigned> MaxThreads(
34234353Sdim  "xcore-max-threads", cl::Optional,
35234982Sdim  cl::desc("Maximum number of threads (for emulation thread-local storage)"),
36263508Sdim  cl::Hidden, cl::value_desc("number"), cl::init(8));
37263508Sdim
38263508Sdimnamespace {
39263508Sdim  /// Lowers thread local variables on the XCore. Each thread local variable is
40263508Sdim  /// expanded to an array of n elements indexed by the thread ID where n is the
41263508Sdim  /// fixed number hardware threads supported by the device.
42251662Sdim  struct XCoreLowerThreadLocal : public ModulePass {
43243830Sdim    static char ID;
44243830Sdim
45263508Sdim    XCoreLowerThreadLocal() : ModulePass(ID) {
46251662Sdim      initializeXCoreLowerThreadLocalPass(*PassRegistry::getPassRegistry());
47251662Sdim    }
48251662Sdim
49234982Sdim    bool lowerGlobal(GlobalVariable *GV);
50234982Sdim
51234982Sdim    bool runOnModule(Module &M) override;
52234982Sdim  };
53243830Sdim}
54243830Sdim
55243830Sdimchar XCoreLowerThreadLocal::ID = 0;
56243830Sdim
57263508SdimINITIALIZE_PASS(XCoreLowerThreadLocal, "xcore-lower-thread-local",
58263508Sdim                "Lower thread local variables", false, false)
59263508Sdim
60263508SdimModulePass *llvm::createXCoreLowerThreadLocalPass() {
61226584Sdim  return new XCoreLowerThreadLocal();
62226584Sdim}
63226584Sdim
64226584Sdimstatic ArrayType *createLoweredType(Type *OriginalType) {
65226584Sdim  return ArrayType::get(OriginalType, MaxThreads);
66226584Sdim}
67226584Sdim
68251662Sdimstatic Constant *
69251662SdimcreateLoweredInitializer(ArrayType *NewType, Constant *OriginalInitializer) {
70243830Sdim  SmallVector<Constant *, 8> Elements(MaxThreads);
71263508Sdim  for (unsigned i = 0; i != MaxThreads; ++i) {
72263508Sdim    Elements[i] = OriginalInitializer;
73226584Sdim  }
74226584Sdim  return ConstantArray::get(NewType, Elements);
75226584Sdim}
76226584Sdim
77226584Sdimstatic Instruction *
78226584SdimcreateReplacementInstr(ConstantExpr *CE, Instruction *Instr) {
79226584Sdim  IRBuilder<true,NoFolder> Builder(Instr);
80226584Sdim  unsigned OpCode = CE->getOpcode();
81226584Sdim  switch (OpCode) {
82226584Sdim    case Instruction::GetElementPtr: {
83226584Sdim      SmallVector<Value *,4> CEOpVec(CE->op_begin(), CE->op_end());
84226584Sdim      ArrayRef<Value *> CEOps(CEOpVec);
85226584Sdim      return dyn_cast<Instruction>(Builder.CreateInBoundsGEP(
86226584Sdim          cast<GEPOperator>(CE)->getSourceElementType(), CEOps[0],
87226584Sdim          CEOps.slice(1)));
88226584Sdim    }
89226584Sdim    case Instruction::Add:
90226584Sdim    case Instruction::Sub:
91226584Sdim    case Instruction::Mul:
92226584Sdim    case Instruction::UDiv:
93226584Sdim    case Instruction::SDiv:
94226584Sdim    case Instruction::FDiv:
95226584Sdim    case Instruction::URem:
96226584Sdim    case Instruction::SRem:
97226584Sdim    case Instruction::FRem:
98226584Sdim    case Instruction::Shl:
99226584Sdim    case Instruction::LShr:
100226584Sdim    case Instruction::AShr:
101226584Sdim    case Instruction::And:
102226584Sdim    case Instruction::Or:
103226584Sdim    case Instruction::Xor:
104226584Sdim      return dyn_cast<Instruction>(
105226584Sdim                  Builder.CreateBinOp((Instruction::BinaryOps)OpCode,
106226584Sdim                                      CE->getOperand(0), CE->getOperand(1),
107226584Sdim                                      CE->getName()));
108226584Sdim    case Instruction::Trunc:
109226584Sdim    case Instruction::ZExt:
110226584Sdim    case Instruction::SExt:
111226584Sdim    case Instruction::FPToUI:
112226584Sdim    case Instruction::FPToSI:
113226584Sdim    case Instruction::UIToFP:
114226584Sdim    case Instruction::SIToFP:
115226584Sdim    case Instruction::FPTrunc:
116226584Sdim    case Instruction::FPExt:
117226584Sdim    case Instruction::PtrToInt:
118226584Sdim    case Instruction::IntToPtr:
119226584Sdim    case Instruction::BitCast:
120226584Sdim      return dyn_cast<Instruction>(
121226584Sdim                  Builder.CreateCast((Instruction::CastOps)OpCode,
122226584Sdim                                     CE->getOperand(0), CE->getType(),
123226584Sdim                                     CE->getName()));
124243830Sdim    default:
125243830Sdim      llvm_unreachable("Unhandled constant expression!\n");
126243830Sdim  }
127243830Sdim}
128226584Sdim
129226584Sdimstatic bool replaceConstantExprOp(ConstantExpr *CE, Pass *P) {
130226584Sdim  do {
131226584Sdim    SmallVector<WeakVH,8> WUsers(CE->user_begin(), CE->user_end());
132226584Sdim    std::sort(WUsers.begin(), WUsers.end());
133226584Sdim    WUsers.erase(std::unique(WUsers.begin(), WUsers.end()), WUsers.end());
134226584Sdim    while (!WUsers.empty())
135226584Sdim      if (WeakVH WU = WUsers.pop_back_val()) {
136226584Sdim        if (PHINode *PN = dyn_cast<PHINode>(WU)) {
137226584Sdim          for (int I = 0, E = PN->getNumIncomingValues(); I < E; ++I)
138226584Sdim            if (PN->getIncomingValue(I) == CE) {
139243830Sdim              BasicBlock *PredBB = PN->getIncomingBlock(I);
140243830Sdim              if (PredBB->getTerminator()->getNumSuccessors() > 1)
141243830Sdim                PredBB = SplitEdge(PredBB, PN->getParent());
142243830Sdim              Instruction *InsertPos = PredBB->getTerminator();
143243830Sdim              Instruction *NewInst = createReplacementInstr(CE, InsertPos);
144243830Sdim              PN->setOperand(I, NewInst);
145243830Sdim            }
146243830Sdim        } else if (Instruction *Instr = dyn_cast<Instruction>(WU)) {
147234353Sdim          Instruction *NewInst = createReplacementInstr(CE, Instr);
148226584Sdim          Instr->replaceUsesOfWith(CE, NewInst);
149226584Sdim        } else {
150226584Sdim          ConstantExpr *CExpr = dyn_cast<ConstantExpr>(WU);
151226584Sdim          if (!CExpr || !replaceConstantExprOp(CExpr, P))
152226584Sdim            return false;
153226584Sdim        }
154226584Sdim      }
155226584Sdim  } while (CE->hasNUsesOrMore(1)); // We need to check because a recursive
156226584Sdim  // sibling may have used 'CE' when createReplacementInstr was called.
157234353Sdim  CE->destroyConstant();
158234353Sdim  return true;
159226584Sdim}
160226584Sdim
161226584Sdimstatic bool rewriteNonInstructionUses(GlobalVariable *GV, Pass *P) {
162226584Sdim  SmallVector<WeakVH,8> WUsers;
163226584Sdim  for (User *U : GV->users())
164226584Sdim    if (!isa<Instruction>(U))
165226584Sdim      WUsers.push_back(WeakVH(U));
166226584Sdim  while (!WUsers.empty())
167226584Sdim    if (WeakVH WU = WUsers.pop_back_val()) {
168226584Sdim      ConstantExpr *CE = dyn_cast<ConstantExpr>(WU);
169226584Sdim      if (!CE || !replaceConstantExprOp(CE, P))
170226584Sdim        return false;
171226584Sdim    }
172226584Sdim  return true;
173226584Sdim}
174226584Sdim
175226584Sdimstatic bool isZeroLengthArray(Type *Ty) {
176226584Sdim  ArrayType *AT = dyn_cast<ArrayType>(Ty);
177226584Sdim  return AT && (AT->getNumElements() == 0);
178226584Sdim}
179226584Sdim
180226584Sdimbool XCoreLowerThreadLocal::lowerGlobal(GlobalVariable *GV) {
181226584Sdim  Module *M = GV->getParent();
182243830Sdim  LLVMContext &Ctx = M->getContext();
183243830Sdim  if (!GV->isThreadLocal())
184243830Sdim    return false;
185243830Sdim
186226584Sdim  // Skip globals that we can't lower and leave it for the backend to error.
187226584Sdim  if (!rewriteNonInstructionUses(GV, this) ||
188226584Sdim      !GV->getType()->isSized() || isZeroLengthArray(GV->getType()))
189226584Sdim    return false;
190226584Sdim
191226584Sdim  // Create replacement global.
192226584Sdim  ArrayType *NewType = createLoweredType(GV->getType()->getElementType());
193226584Sdim  Constant *NewInitializer = nullptr;
194226584Sdim  if (GV->hasInitializer())
195226584Sdim    NewInitializer = createLoweredInitializer(NewType,
196226584Sdim                                              GV->getInitializer());
197226584Sdim  GlobalVariable *NewGV =
198226584Sdim    new GlobalVariable(*M, NewType, GV->isConstant(), GV->getLinkage(),
199226584Sdim                       NewInitializer, "", nullptr,
200226584Sdim                       GlobalVariable::NotThreadLocal,
201226584Sdim                       GV->getType()->getAddressSpace(),
202243830Sdim                       GV->isExternallyInitialized());
203263508Sdim
204263508Sdim  // Update uses.
205243830Sdim  SmallVector<User *, 16> Users(GV->user_begin(), GV->user_end());
206226584Sdim  for (unsigned I = 0, E = Users.size(); I != E; ++I) {
207226584Sdim    User *U = Users[I];
208226584Sdim    Instruction *Inst = cast<Instruction>(U);
209226584Sdim    IRBuilder<> Builder(Inst);
210226584Sdim    Function *GetID = Intrinsic::getDeclaration(GV->getParent(),
211226584Sdim                                                Intrinsic::xcore_getid);
212226584Sdim    Value *ThreadID = Builder.CreateCall(GetID, {});
213226584Sdim    SmallVector<Value *, 2> Indices;
214226584Sdim    Indices.push_back(Constant::getNullValue(Type::getInt64Ty(Ctx)));
215226584Sdim    Indices.push_back(ThreadID);
216226584Sdim    Value *Addr =
217226584Sdim        Builder.CreateInBoundsGEP(NewGV->getValueType(), NewGV, Indices);
218226584Sdim    U->replaceUsesOfWith(GV, Addr);
219226584Sdim  }
220226584Sdim
221226584Sdim  // Remove old global.
222226584Sdim  NewGV->takeName(GV);
223226584Sdim  GV->eraseFromParent();
224263508Sdim  return true;
225263508Sdim}
226251662Sdim
227263508Sdimbool XCoreLowerThreadLocal::runOnModule(Module &M) {
228251662Sdim  // Find thread local globals.
229251662Sdim  bool MadeChange = false;
230234353Sdim  SmallVector<GlobalVariable *, 16> ThreadLocalGlobals;
231234353Sdim  for (GlobalVariable &GV : M.globals())
232234982Sdim    if (GV.isThreadLocal())
233263508Sdim      ThreadLocalGlobals.push_back(&GV);
234234982Sdim  for (unsigned I = 0, E = ThreadLocalGlobals.size(); I != E; ++I) {
235263508Sdim    MadeChange |= lowerGlobal(ThreadLocalGlobals[I]);
236249423Sdim  }
237249423Sdim  return MadeChange;
238249423Sdim}
239249423Sdim