2013-05-05 01:01:55 +08:00
|
|
|
//===-- XCoreLowerThreadLocal - Lower thread local variables --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This file contains a pass that lowers thread local variables on the
|
2013-05-05 01:01:55 +08:00
|
|
|
/// XCore.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "XCore.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2013-05-05 01:01:55 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-03-04 20:05:47 +08:00
|
|
|
#include "llvm/IR/NoFolder.h"
|
2014-03-04 19:17:44 +08:00
|
|
|
#include "llvm/IR/ValueHandle.h"
|
2013-05-05 01:01:55 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2013-10-11 18:26:48 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2013-05-05 01:01:55 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "xcore-lower-thread-local"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<unsigned> MaxThreads(
|
|
|
|
"xcore-max-threads", cl::Optional,
|
|
|
|
cl::desc("Maximum number of threads (for emulation thread-local storage)"),
|
|
|
|
cl::Hidden, cl::value_desc("number"), cl::init(8));
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// Lowers thread local variables on the XCore. Each thread local variable is
|
|
|
|
/// expanded to an array of n elements indexed by the thread ID where n is the
|
|
|
|
/// fixed number hardware threads supported by the device.
|
|
|
|
struct XCoreLowerThreadLocal : public ModulePass {
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
XCoreLowerThreadLocal() : ModulePass(ID) {
|
|
|
|
initializeXCoreLowerThreadLocalPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lowerGlobal(GlobalVariable *GV);
|
|
|
|
|
2014-04-29 15:57:00 +08:00
|
|
|
bool runOnModule(Module &M) override;
|
2013-05-05 01:01:55 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2013-05-05 01:01:55 +08:00
|
|
|
|
|
|
|
char XCoreLowerThreadLocal::ID = 0;
|
|
|
|
|
|
|
|
INITIALIZE_PASS(XCoreLowerThreadLocal, "xcore-lower-thread-local",
|
|
|
|
"Lower thread local variables", false, false)
|
|
|
|
|
|
|
|
ModulePass *llvm::createXCoreLowerThreadLocalPass() {
|
|
|
|
return new XCoreLowerThreadLocal();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArrayType *createLoweredType(Type *OriginalType) {
|
|
|
|
return ArrayType::get(OriginalType, MaxThreads);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Constant *
|
|
|
|
createLoweredInitializer(ArrayType *NewType, Constant *OriginalInitializer) {
|
|
|
|
SmallVector<Constant *, 8> Elements(MaxThreads);
|
|
|
|
for (unsigned i = 0; i != MaxThreads; ++i) {
|
|
|
|
Elements[i] = OriginalInitializer;
|
|
|
|
}
|
|
|
|
return ConstantArray::get(NewType, Elements);
|
|
|
|
}
|
|
|
|
|
2013-09-09 18:42:11 +08:00
|
|
|
static Instruction *
|
|
|
|
createReplacementInstr(ConstantExpr *CE, Instruction *Instr) {
|
2016-03-14 05:05:13 +08:00
|
|
|
IRBuilder<NoFolder> Builder(Instr);
|
2013-09-09 18:42:11 +08:00
|
|
|
unsigned OpCode = CE->getOpcode();
|
|
|
|
switch (OpCode) {
|
|
|
|
case Instruction::GetElementPtr: {
|
|
|
|
SmallVector<Value *,4> CEOpVec(CE->op_begin(), CE->op_end());
|
|
|
|
ArrayRef<Value *> CEOps(CEOpVec);
|
2015-04-04 05:33:42 +08:00
|
|
|
return dyn_cast<Instruction>(Builder.CreateInBoundsGEP(
|
|
|
|
cast<GEPOperator>(CE)->getSourceElementType(), CEOps[0],
|
|
|
|
CEOps.slice(1)));
|
2013-09-09 18:42:11 +08:00
|
|
|
}
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::Mul:
|
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::FDiv:
|
|
|
|
case Instruction::URem:
|
|
|
|
case Instruction::SRem:
|
|
|
|
case Instruction::FRem:
|
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::AShr:
|
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
|
|
|
return dyn_cast<Instruction>(
|
|
|
|
Builder.CreateBinOp((Instruction::BinaryOps)OpCode,
|
|
|
|
CE->getOperand(0), CE->getOperand(1),
|
|
|
|
CE->getName()));
|
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::PtrToInt:
|
|
|
|
case Instruction::IntToPtr:
|
|
|
|
case Instruction::BitCast:
|
|
|
|
return dyn_cast<Instruction>(
|
|
|
|
Builder.CreateCast((Instruction::CastOps)OpCode,
|
|
|
|
CE->getOperand(0), CE->getType(),
|
|
|
|
CE->getName()));
|
|
|
|
default:
|
2013-09-09 22:17:30 +08:00
|
|
|
llvm_unreachable("Unhandled constant expression!\n");
|
2013-09-09 18:42:11 +08:00
|
|
|
}
|
|
|
|
}
|
2013-05-05 01:01:55 +08:00
|
|
|
|
2013-10-11 18:26:48 +08:00
|
|
|
static bool replaceConstantExprOp(ConstantExpr *CE, Pass *P) {
|
2013-09-09 18:42:11 +08:00
|
|
|
do {
|
2017-05-02 01:07:49 +08:00
|
|
|
SmallVector<WeakTrackingVH, 8> WUsers(CE->user_begin(), CE->user_end());
|
2018-03-27 02:08:26 +08:00
|
|
|
llvm::sort(WUsers.begin(), WUsers.end());
|
2013-10-11 18:26:48 +08:00
|
|
|
WUsers.erase(std::unique(WUsers.begin(), WUsers.end()), WUsers.end());
|
2013-09-09 18:42:11 +08:00
|
|
|
while (!WUsers.empty())
|
2017-05-02 01:07:49 +08:00
|
|
|
if (WeakTrackingVH WU = WUsers.pop_back_val()) {
|
2013-10-11 18:26:48 +08:00
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(WU)) {
|
|
|
|
for (int I = 0, E = PN->getNumIncomingValues(); I < E; ++I)
|
|
|
|
if (PN->getIncomingValue(I) == CE) {
|
|
|
|
BasicBlock *PredBB = PN->getIncomingBlock(I);
|
|
|
|
if (PredBB->getTerminator()->getNumSuccessors() > 1)
|
2015-01-19 20:36:53 +08:00
|
|
|
PredBB = SplitEdge(PredBB, PN->getParent());
|
2013-10-11 18:26:48 +08:00
|
|
|
Instruction *InsertPos = PredBB->getTerminator();
|
|
|
|
Instruction *NewInst = createReplacementInstr(CE, InsertPos);
|
|
|
|
PN->setOperand(I, NewInst);
|
|
|
|
}
|
|
|
|
} else if (Instruction *Instr = dyn_cast<Instruction>(WU)) {
|
2013-09-09 18:42:11 +08:00
|
|
|
Instruction *NewInst = createReplacementInstr(CE, Instr);
|
|
|
|
Instr->replaceUsesOfWith(CE, NewInst);
|
|
|
|
} else {
|
|
|
|
ConstantExpr *CExpr = dyn_cast<ConstantExpr>(WU);
|
2013-10-11 18:26:48 +08:00
|
|
|
if (!CExpr || !replaceConstantExprOp(CExpr, P))
|
2013-09-09 18:42:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-01-25 01:20:08 +08:00
|
|
|
} while (CE->hasNUsesOrMore(1)); // We need to check because a recursive
|
|
|
|
// sibling may have used 'CE' when createReplacementInstr was called.
|
2013-09-09 18:42:11 +08:00
|
|
|
CE->destroyConstant();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-11 18:26:48 +08:00
|
|
|
static bool rewriteNonInstructionUses(GlobalVariable *GV, Pass *P) {
|
2017-05-02 01:07:49 +08:00
|
|
|
SmallVector<WeakTrackingVH, 8> WUsers;
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : GV->users())
|
|
|
|
if (!isa<Instruction>(U))
|
2017-05-02 01:07:49 +08:00
|
|
|
WUsers.push_back(WeakTrackingVH(U));
|
2013-09-09 18:42:11 +08:00
|
|
|
while (!WUsers.empty())
|
2017-05-02 01:07:49 +08:00
|
|
|
if (WeakTrackingVH WU = WUsers.pop_back_val()) {
|
2013-09-09 18:42:11 +08:00
|
|
|
ConstantExpr *CE = dyn_cast<ConstantExpr>(WU);
|
2013-10-11 18:26:48 +08:00
|
|
|
if (!CE || !replaceConstantExprOp(CE, P))
|
2013-09-09 18:42:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2013-05-05 01:01:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isZeroLengthArray(Type *Ty) {
|
|
|
|
ArrayType *AT = dyn_cast<ArrayType>(Ty);
|
|
|
|
return AT && (AT->getNumElements() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XCoreLowerThreadLocal::lowerGlobal(GlobalVariable *GV) {
|
|
|
|
Module *M = GV->getParent();
|
|
|
|
if (!GV->isThreadLocal())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Skip globals that we can't lower and leave it for the backend to error.
|
2013-10-11 18:26:48 +08:00
|
|
|
if (!rewriteNonInstructionUses(GV, this) ||
|
2013-05-05 01:01:55 +08:00
|
|
|
!GV->getType()->isSized() || isZeroLengthArray(GV->getType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Create replacement global.
|
2016-01-17 04:30:46 +08:00
|
|
|
ArrayType *NewType = createLoweredType(GV->getValueType());
|
2014-04-25 13:30:21 +08:00
|
|
|
Constant *NewInitializer = nullptr;
|
2013-09-09 18:42:11 +08:00
|
|
|
if (GV->hasInitializer())
|
|
|
|
NewInitializer = createLoweredInitializer(NewType,
|
|
|
|
GV->getInitializer());
|
2013-05-05 01:01:55 +08:00
|
|
|
GlobalVariable *NewGV =
|
|
|
|
new GlobalVariable(*M, NewType, GV->isConstant(), GV->getLinkage(),
|
2014-04-25 13:30:21 +08:00
|
|
|
NewInitializer, "", nullptr,
|
|
|
|
GlobalVariable::NotThreadLocal,
|
2013-05-05 01:01:55 +08:00
|
|
|
GV->getType()->getAddressSpace(),
|
|
|
|
GV->isExternallyInitialized());
|
|
|
|
|
|
|
|
// Update uses.
|
2014-03-09 11:16:01 +08:00
|
|
|
SmallVector<User *, 16> Users(GV->user_begin(), GV->user_end());
|
2013-05-05 01:01:55 +08:00
|
|
|
for (unsigned I = 0, E = Users.size(); I != E; ++I) {
|
|
|
|
User *U = Users[I];
|
|
|
|
Instruction *Inst = cast<Instruction>(U);
|
|
|
|
IRBuilder<> Builder(Inst);
|
|
|
|
Function *GetID = Intrinsic::getDeclaration(GV->getParent(),
|
|
|
|
Intrinsic::xcore_getid);
|
2015-05-19 06:13:54 +08:00
|
|
|
Value *ThreadID = Builder.CreateCall(GetID, {});
|
2016-07-02 19:41:39 +08:00
|
|
|
Value *Addr = Builder.CreateInBoundsGEP(NewGV->getValueType(), NewGV,
|
|
|
|
{Builder.getInt64(0), ThreadID});
|
2013-05-05 01:01:55 +08:00
|
|
|
U->replaceUsesOfWith(GV, Addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove old global.
|
|
|
|
NewGV->takeName(GV);
|
|
|
|
GV->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XCoreLowerThreadLocal::runOnModule(Module &M) {
|
|
|
|
// Find thread local globals.
|
|
|
|
bool MadeChange = false;
|
|
|
|
SmallVector<GlobalVariable *, 16> ThreadLocalGlobals;
|
2015-10-20 09:07:42 +08:00
|
|
|
for (GlobalVariable &GV : M.globals())
|
|
|
|
if (GV.isThreadLocal())
|
|
|
|
ThreadLocalGlobals.push_back(&GV);
|
2013-05-05 01:01:55 +08:00
|
|
|
for (unsigned I = 0, E = ThreadLocalGlobals.size(); I != E; ++I) {
|
|
|
|
MadeChange |= lowerGlobal(ThreadLocalGlobals[I]);
|
|
|
|
}
|
|
|
|
return MadeChange;
|
|
|
|
}
|