1//===-- XCoreLowerThreadLocal - Lower thread local variables --------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains a pass that lowers thread local variables on the
11/// XCore.
12///
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/IntrinsicsXCore.h"
22#include "llvm/IR/Module.h"
23#include "llvm/IR/ValueHandle.h"
24#include "llvm/Pass.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Transforms/Utils/BasicBlockUtils.h"
28
29#define DEBUG_TYPE "xcore-lower-thread-local"
30
31using namespace llvm;
32
33static 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(Val: 8));
37
38namespace {
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
45 XCoreLowerThreadLocal() : ModulePass(ID) {}
46
47 bool lowerGlobal(GlobalVariable *GV);
48
49 bool runOnModule(Module &M) override;
50 };
51}
52
53char XCoreLowerThreadLocal::ID = 0;
54
55INITIALIZE_PASS(XCoreLowerThreadLocal, "xcore-lower-thread-local",
56 "Lower thread local variables", false, false)
57
58ModulePass *llvm::createXCoreLowerThreadLocalPass() {
59 return new XCoreLowerThreadLocal();
60}
61
62static ArrayType *createLoweredType(Type *OriginalType) {
63 return ArrayType::get(ElementType: OriginalType, NumElements: MaxThreads);
64}
65
66static Constant *
67createLoweredInitializer(ArrayType *NewType, Constant *OriginalInitializer) {
68 SmallVector<Constant *, 8> Elements(MaxThreads);
69 for (unsigned i = 0; i != MaxThreads; ++i) {
70 Elements[i] = OriginalInitializer;
71 }
72 return ConstantArray::get(T: NewType, V: Elements);
73}
74
75
76static bool replaceConstantExprOp(ConstantExpr *CE, Pass *P) {
77 do {
78 SmallVector<WeakTrackingVH, 8> WUsers(CE->users());
79 llvm::sort(C&: WUsers);
80 WUsers.erase(CS: llvm::unique(R&: WUsers), CE: WUsers.end());
81 while (!WUsers.empty())
82 if (WeakTrackingVH WU = WUsers.pop_back_val()) {
83 if (PHINode *PN = dyn_cast<PHINode>(Val&: WU)) {
84 for (int I = 0, E = PN->getNumIncomingValues(); I < E; ++I)
85 if (PN->getIncomingValue(i: I) == CE) {
86 BasicBlock *PredBB = PN->getIncomingBlock(i: I);
87 if (PredBB->getTerminator()->getNumSuccessors() > 1)
88 PredBB = SplitEdge(From: PredBB, To: PN->getParent());
89 BasicBlock::iterator InsertPos =
90 PredBB->getTerminator()->getIterator();
91 Instruction *NewInst = CE->getAsInstruction();
92 NewInst->insertBefore(BB&: *PredBB, InsertPos);
93 PN->setOperand(i_nocapture: I, Val_nocapture: NewInst);
94 }
95 } else if (Instruction *Instr = dyn_cast<Instruction>(Val&: WU)) {
96 Instruction *NewInst = CE->getAsInstruction();
97 NewInst->insertBefore(BB&: *Instr->getParent(), InsertPos: Instr->getIterator());
98 Instr->replaceUsesOfWith(From: CE, To: NewInst);
99 } else {
100 ConstantExpr *CExpr = dyn_cast<ConstantExpr>(Val&: WU);
101 if (!CExpr || !replaceConstantExprOp(CE: CExpr, P))
102 return false;
103 }
104 }
105 } while (CE->hasNUsesOrMore(N: 1)); // We need to check because a recursive
106 // sibling may have used 'CE' when getAsInstruction was called.
107 CE->destroyConstant();
108 return true;
109}
110
111static bool rewriteNonInstructionUses(GlobalVariable *GV, Pass *P) {
112 SmallVector<WeakTrackingVH, 8> WUsers;
113 for (User *U : GV->users())
114 if (!isa<Instruction>(Val: U))
115 WUsers.push_back(Elt: WeakTrackingVH(U));
116 while (!WUsers.empty())
117 if (WeakTrackingVH WU = WUsers.pop_back_val()) {
118 ConstantExpr *CE = dyn_cast<ConstantExpr>(Val&: WU);
119 if (!CE || !replaceConstantExprOp(CE, P))
120 return false;
121 }
122 return true;
123}
124
125bool XCoreLowerThreadLocal::lowerGlobal(GlobalVariable *GV) {
126 Module *M = GV->getParent();
127 if (!GV->isThreadLocal())
128 return false;
129
130 if (!rewriteNonInstructionUses(GV, P: this))
131 return false;
132
133 // The lowered representation needs an ArrayType of the value type, which
134 // requires a known per-element stride: reject anything that can't provide
135 // one now, with a clear diagnostic, rather than emitting a malformed GEP
136 // that only fails much later (and much less clearly) in instruction
137 // selection.
138 if (!GV->getValueType()->isSized() ||
139 GV->getGlobalSize(DL: M->getDataLayout()) == 0)
140 reportFatalUsageError(reason: "Size of thread local object '" + GV->getName() +
141 "' is unknown");
142
143 // Create replacement global.
144 ArrayType *NewType = createLoweredType(OriginalType: GV->getValueType());
145 Constant *NewInitializer = nullptr;
146 if (GV->hasInitializer())
147 NewInitializer = createLoweredInitializer(NewType,
148 OriginalInitializer: GV->getInitializer());
149 GlobalVariable *NewGV =
150 new GlobalVariable(*M, NewType, GV->isConstant(), GV->getLinkage(),
151 NewInitializer, "", nullptr,
152 GlobalVariable::NotThreadLocal,
153 GV->getType()->getAddressSpace(),
154 GV->isExternallyInitialized());
155
156 // Update uses.
157 SmallVector<User *, 16> Users(GV->users());
158 for (User *U : Users) {
159 Instruction *Inst = cast<Instruction>(Val: U);
160 IRBuilder<> Builder(Inst);
161 Value *ThreadID = Builder.CreateIntrinsic(ID: Intrinsic::xcore_getid, Args: {});
162 Value *Addr = Builder.CreateInBoundsGEP(Ty: NewGV->getValueType(), Ptr: NewGV,
163 IdxList: {Builder.getInt64(C: 0), ThreadID});
164 U->replaceUsesOfWith(From: GV, To: Addr);
165 }
166
167 // Remove old global.
168 NewGV->takeName(V: GV);
169 GV->eraseFromParent();
170 return true;
171}
172
173bool XCoreLowerThreadLocal::runOnModule(Module &M) {
174 // Find thread local globals.
175 bool MadeChange = false;
176 SmallVector<GlobalVariable *, 16> ThreadLocalGlobals;
177 for (GlobalVariable &GV : M.globals())
178 if (GV.isThreadLocal())
179 ThreadLocalGlobals.push_back(Elt: &GV);
180 for (GlobalVariable *GV : ThreadLocalGlobals)
181 MadeChange |= lowerGlobal(GV);
182 return MadeChange;
183}
184