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