#include "llvm/ADT/SmallVector.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Target/TargetMachine.h"
using namespace llvm;
#define DEBUG_TYPE "loweremutls"
namespace {
class LowerEmuTLS : public ModulePass {
public:
static char ID; LowerEmuTLS() : ModulePass(ID) {
initializeLowerEmuTLSPass(*PassRegistry::getPassRegistry());
}
bool runOnModule(Module &M) override;
private:
bool addEmuTlsVar(Module &M, const GlobalVariable *GV);
static void copyLinkageVisibility(Module &M,
const GlobalVariable *from,
GlobalVariable *to) {
to->setLinkage(from->getLinkage());
to->setVisibility(from->getVisibility());
to->setDSOLocal(from->isDSOLocal());
if (from->hasComdat()) {
to->setComdat(M.getOrInsertComdat(to->getName()));
to->getComdat()->setSelectionKind(from->getComdat()->getSelectionKind());
}
}
};
}
char LowerEmuTLS::ID = 0;
INITIALIZE_PASS(LowerEmuTLS, DEBUG_TYPE,
"Add __emutls_[vt]. variables for emultated TLS model", false,
false)
ModulePass *llvm::createLowerEmuTLSPass() { return new LowerEmuTLS(); }
bool LowerEmuTLS::runOnModule(Module &M) {
if (skipModule(M))
return false;
auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
if (!TPC)
return false;
auto &TM = TPC->getTM<TargetMachine>();
if (!TM.useEmulatedTLS())
return false;
bool Changed = false;
SmallVector<const GlobalVariable*, 8> TlsVars;
for (const auto &G : M.globals()) {
if (G.isThreadLocal())
TlsVars.append({&G});
}
for (const auto *const G : TlsVars)
Changed |= addEmuTlsVar(M, G);
return Changed;
}
bool LowerEmuTLS::addEmuTlsVar(Module &M, const GlobalVariable *GV) {
LLVMContext &C = M.getContext();
PointerType *VoidPtrType = Type::getInt8PtrTy(C);
std::string EmuTlsVarName = ("__emutls_v." + GV->getName()).str();
GlobalVariable *EmuTlsVar = M.getNamedGlobal(EmuTlsVarName);
if (EmuTlsVar)
return false;
const DataLayout &DL = M.getDataLayout();
Constant *NullPtr = ConstantPointerNull::get(VoidPtrType);
const Constant *InitValue = nullptr;
if (GV->hasInitializer()) {
InitValue = GV->getInitializer();
const ConstantInt *InitIntValue = dyn_cast<ConstantInt>(InitValue);
if (isa<ConstantAggregateZero>(InitValue) ||
(InitIntValue && InitIntValue->isZero()))
InitValue = nullptr;
}
IntegerType *WordType = DL.getIntPtrType(C);
PointerType *InitPtrType = InitValue ?
PointerType::getUnqual(InitValue->getType()) : VoidPtrType;
Type *ElementTypes[4] = {WordType, WordType, VoidPtrType, InitPtrType};
ArrayRef<Type*> ElementTypeArray(ElementTypes, 4);
StructType *EmuTlsVarType = StructType::create(ElementTypeArray);
EmuTlsVar = cast<GlobalVariable>(
M.getOrInsertGlobal(EmuTlsVarName, EmuTlsVarType));
copyLinkageVisibility(M, GV, EmuTlsVar);
if (!GV->hasInitializer())
return true;
Type *GVType = GV->getValueType();
Align GVAlignment = DL.getValueOrABITypeAlignment(GV->getAlign(), GVType);
GlobalVariable *EmuTlsTmplVar = nullptr;
if (InitValue) {
std::string EmuTlsTmplName = ("__emutls_t." + GV->getName()).str();
EmuTlsTmplVar = dyn_cast_or_null<GlobalVariable>(
M.getOrInsertGlobal(EmuTlsTmplName, GVType));
assert(EmuTlsTmplVar && "Failed to create emualted TLS initializer");
EmuTlsTmplVar->setConstant(true);
EmuTlsTmplVar->setInitializer(const_cast<Constant*>(InitValue));
EmuTlsTmplVar->setAlignment(GVAlignment);
copyLinkageVisibility(M, GV, EmuTlsTmplVar);
}
Constant *ElementValues[4] = {
ConstantInt::get(WordType, DL.getTypeStoreSize(GVType)),
ConstantInt::get(WordType, GVAlignment.value()), NullPtr,
EmuTlsTmplVar ? EmuTlsTmplVar : NullPtr};
ArrayRef<Constant*> ElementValueArray(ElementValues, 4);
EmuTlsVar->setInitializer(
ConstantStruct::get(EmuTlsVarType, ElementValueArray));
Align MaxAlignment =
std::max(DL.getABITypeAlign(WordType), DL.getABITypeAlign(VoidPtrType));
EmuTlsVar->setAlignment(MaxAlignment);
return true;
}