Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/llvm/lib/CodeGen/LowerEmuTLS.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LowerEmuTLS.cpp - Add __emutls_[vt].* 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
// This transformation is required for targets depending on libgcc style
10
// emulated thread local storage variables. For every defined TLS variable xyz,
11
// an __emutls_v.xyz is generated. If there is non-zero initialized value
12
// an __emutls_t.xyz is also generated.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "llvm/CodeGen/LowerEmuTLS.h"
17
#include "llvm/ADT/SmallVector.h"
18
#include "llvm/Analysis/GlobalsModRef.h"
19
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
20
#include "llvm/Analysis/StackSafetyAnalysis.h"
21
#include "llvm/CodeGen/Passes.h"
22
#include "llvm/CodeGen/TargetPassConfig.h"
23
#include "llvm/IR/Constants.h"
24
#include "llvm/IR/Module.h"
25
#include "llvm/InitializePasses.h"
26
#include "llvm/Pass.h"
27
#include "llvm/Target/TargetMachine.h"
28
29
using namespace llvm;
30
31
#define DEBUG_TYPE "lower-emutls"
32
33
namespace {
34
35
class LowerEmuTLS : public ModulePass {
36
public:
37
  static char ID; // Pass identification, replacement for typeid
38
0
  LowerEmuTLS() : ModulePass(ID) {
39
0
    initializeLowerEmuTLSPass(*PassRegistry::getPassRegistry());
40
0
  }
41
42
  bool runOnModule(Module &M) override;
43
};
44
}
45
46
static bool addEmuTlsVar(Module &M, const GlobalVariable *GV);
47
48
static void copyLinkageVisibility(Module &M, const GlobalVariable *from,
49
0
                                  GlobalVariable *to) {
50
0
  to->setLinkage(from->getLinkage());
51
0
  to->setVisibility(from->getVisibility());
52
0
  to->setDSOLocal(from->isDSOLocal());
53
0
  if (from->hasComdat()) {
54
0
    to->setComdat(M.getOrInsertComdat(to->getName()));
55
0
    to->getComdat()->setSelectionKind(from->getComdat()->getSelectionKind());
56
0
  }
57
0
}
58
59
0
PreservedAnalyses LowerEmuTLSPass::run(Module &M, ModuleAnalysisManager &MAM) {
60
0
  bool Changed = false;
61
0
  SmallVector<const GlobalVariable *, 8> TlsVars;
62
0
  for (const auto &G : M.globals()) {
63
0
    if (G.isThreadLocal())
64
0
      TlsVars.push_back(&G);
65
0
  }
66
0
  for (const auto *G : TlsVars)
67
0
    Changed |= addEmuTlsVar(M, G);
68
69
0
  if (!Changed)
70
0
    return PreservedAnalyses::all();
71
0
  PreservedAnalyses PA = PreservedAnalyses::all();
72
0
  PA.abandon<GlobalsAA>();
73
0
  PA.abandon<ModuleSummaryIndexAnalysis>();
74
0
  PA.abandon<StackSafetyGlobalAnalysis>();
75
0
  return PA;
76
0
}
77
78
char LowerEmuTLS::ID = 0;
79
80
INITIALIZE_PASS(LowerEmuTLS, DEBUG_TYPE,
81
                "Add __emutls_[vt]. variables for emultated TLS model", false,
82
                false)
83
84
0
ModulePass *llvm::createLowerEmuTLSPass() { return new LowerEmuTLS(); }
85
86
0
bool LowerEmuTLS::runOnModule(Module &M) {
87
0
  if (skipModule(M))
88
0
    return false;
89
90
0
  auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
91
0
  if (!TPC)
92
0
    return false;
93
94
0
  auto &TM = TPC->getTM<TargetMachine>();
95
0
  if (!TM.useEmulatedTLS())
96
0
    return false;
97
98
0
  bool Changed = false;
99
0
  SmallVector<const GlobalVariable*, 8> TlsVars;
100
0
  for (const auto &G : M.globals()) {
101
0
    if (G.isThreadLocal())
102
0
      TlsVars.append({&G});
103
0
  }
104
0
  for (const auto *const G : TlsVars)
105
0
    Changed |= addEmuTlsVar(M, G);
106
0
  return Changed;
107
0
}
108
109
0
bool addEmuTlsVar(Module &M, const GlobalVariable *GV) {
110
0
  LLVMContext &C = M.getContext();
111
0
  PointerType *VoidPtrType = PointerType::getUnqual(C);
112
113
0
  std::string EmuTlsVarName = ("__emutls_v." + GV->getName()).str();
114
0
  GlobalVariable *EmuTlsVar = M.getNamedGlobal(EmuTlsVarName);
115
0
  if (EmuTlsVar)
116
0
    return false;  // It has been added before.
117
118
0
  const DataLayout &DL = M.getDataLayout();
119
0
  Constant *NullPtr = ConstantPointerNull::get(VoidPtrType);
120
121
  // Get non-zero initializer from GV's initializer.
122
0
  const Constant *InitValue = nullptr;
123
0
  if (GV->hasInitializer()) {
124
0
    InitValue = GV->getInitializer();
125
0
    const ConstantInt *InitIntValue = dyn_cast<ConstantInt>(InitValue);
126
    // When GV's init value is all 0, omit the EmuTlsTmplVar and let
127
    // the emutls library function to reset newly allocated TLS variables.
128
0
    if (isa<ConstantAggregateZero>(InitValue) ||
129
0
        (InitIntValue && InitIntValue->isZero()))
130
0
      InitValue = nullptr;
131
0
  }
132
133
  // Create the __emutls_v. symbol, whose type has 4 fields:
134
  //     word size;   // size of GV in bytes
135
  //     word align;  // alignment of GV
136
  //     void *ptr;   // initialized to 0; set at run time per thread.
137
  //     void *templ; // 0 or point to __emutls_t.*
138
  // sizeof(word) should be the same as sizeof(void*) on target.
139
0
  IntegerType *WordType = DL.getIntPtrType(C);
140
0
  PointerType *InitPtrType = PointerType::getUnqual(C);
141
0
  Type *ElementTypes[4] = {WordType, WordType, VoidPtrType, InitPtrType};
142
0
  ArrayRef<Type*> ElementTypeArray(ElementTypes, 4);
143
0
  StructType *EmuTlsVarType = StructType::create(ElementTypeArray);
144
0
  EmuTlsVar = cast<GlobalVariable>(
145
0
      M.getOrInsertGlobal(EmuTlsVarName, EmuTlsVarType));
146
0
  copyLinkageVisibility(M, GV, EmuTlsVar);
147
148
  // Define "__emutls_t.*" and "__emutls_v.*" only if GV is defined.
149
0
  if (!GV->hasInitializer())
150
0
    return true;
151
152
0
  Type *GVType = GV->getValueType();
153
0
  Align GVAlignment = DL.getValueOrABITypeAlignment(GV->getAlign(), GVType);
154
155
  // Define "__emutls_t.*" if there is InitValue
156
0
  GlobalVariable *EmuTlsTmplVar = nullptr;
157
0
  if (InitValue) {
158
0
    std::string EmuTlsTmplName = ("__emutls_t." + GV->getName()).str();
159
0
    EmuTlsTmplVar = dyn_cast_or_null<GlobalVariable>(
160
0
        M.getOrInsertGlobal(EmuTlsTmplName, GVType));
161
0
    assert(EmuTlsTmplVar && "Failed to create emualted TLS initializer");
162
0
    EmuTlsTmplVar->setConstant(true);
163
0
    EmuTlsTmplVar->setInitializer(const_cast<Constant*>(InitValue));
164
0
    EmuTlsTmplVar->setAlignment(GVAlignment);
165
0
    copyLinkageVisibility(M, GV, EmuTlsTmplVar);
166
0
  }
167
168
  // Define "__emutls_v.*" with initializer and alignment.
169
0
  Constant *ElementValues[4] = {
170
0
      ConstantInt::get(WordType, DL.getTypeStoreSize(GVType)),
171
0
      ConstantInt::get(WordType, GVAlignment.value()), NullPtr,
172
0
      EmuTlsTmplVar ? EmuTlsTmplVar : NullPtr};
173
0
  ArrayRef<Constant*> ElementValueArray(ElementValues, 4);
174
0
  EmuTlsVar->setInitializer(
175
0
      ConstantStruct::get(EmuTlsVarType, ElementValueArray));
176
0
  Align MaxAlignment =
177
0
      std::max(DL.getABITypeAlign(WordType), DL.getABITypeAlign(VoidPtrType));
178
0
  EmuTlsVar->setAlignment(MaxAlignment);
179
0
  return true;
180
0
}