llvm-project/llvm/lib/Target/XCore/XCoreLowerThreadLocal.cpp

235 lines
8.1 KiB
C++
Raw Normal View History

//===-- 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
/// This file contains a pass that lowers thread local variables on the
/// XCore.
///
//===----------------------------------------------------------------------===//
#include "XCore.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/NoFolder.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#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);
bool runOnModule(Module &M) override;
};
}
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);
}
static Instruction *
createReplacementInstr(ConstantExpr *CE, Instruction *Instr) {
IRBuilder<NoFolder> Builder(Instr);
unsigned OpCode = CE->getOpcode();
switch (OpCode) {
case Instruction::GetElementPtr: {
SmallVector<Value *,4> CEOpVec(CE->op_begin(), CE->op_end());
ArrayRef<Value *> CEOps(CEOpVec);
return dyn_cast<Instruction>(Builder.CreateInBoundsGEP(
cast<GEPOperator>(CE)->getSourceElementType(), CEOps[0],
CEOps.slice(1)));
}
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:
llvm_unreachable("Unhandled constant expression!\n");
}
}
static bool replaceConstantExprOp(ConstantExpr *CE, Pass *P) {
do {
SmallVector<WeakTrackingVH, 8> WUsers(CE->user_begin(), CE->user_end());
llvm::sort(WUsers);
WUsers.erase(std::unique(WUsers.begin(), WUsers.end()), WUsers.end());
while (!WUsers.empty())
if (WeakTrackingVH WU = WUsers.pop_back_val()) {
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)
PredBB = SplitEdge(PredBB, PN->getParent());
Instruction *InsertPos = PredBB->getTerminator();
Instruction *NewInst = createReplacementInstr(CE, InsertPos);
PN->setOperand(I, NewInst);
}
} else if (Instruction *Instr = dyn_cast<Instruction>(WU)) {
Instruction *NewInst = createReplacementInstr(CE, Instr);
Instr->replaceUsesOfWith(CE, NewInst);
} else {
ConstantExpr *CExpr = dyn_cast<ConstantExpr>(WU);
if (!CExpr || !replaceConstantExprOp(CExpr, P))
return false;
}
}
} while (CE->hasNUsesOrMore(1)); // We need to check because a recursive
// sibling may have used 'CE' when createReplacementInstr was called.
CE->destroyConstant();
return true;
}
static bool rewriteNonInstructionUses(GlobalVariable *GV, Pass *P) {
SmallVector<WeakTrackingVH, 8> WUsers;
[C++11] Add range based accessors for the Use-Def chain of a Value. This requires a number of steps. 1) Move value_use_iterator into the Value class as an implementation detail 2) Change it to actually be a *Use* iterator rather than a *User* iterator. 3) Add an adaptor which is a User iterator that always looks through the Use to the User. 4) Wrap these in Value::use_iterator and Value::user_iterator typedefs. 5) Add the range adaptors as Value::uses() and Value::users(). 6) Update *all* of the callers to correctly distinguish between whether they wanted a use_iterator (and to explicitly dig out the User when needed), or a user_iterator which makes the Use itself totally opaque. Because #6 requires churning essentially everything that walked the Use-Def chains, I went ahead and added all of the range adaptors and switched them to range-based loops where appropriate. Also because the renaming requires at least churning every line of code, it didn't make any sense to split these up into multiple commits -- all of which would touch all of the same lies of code. The result is still not quite optimal. The Value::use_iterator is a nice regular iterator, but Value::user_iterator is an iterator over User*s rather than over the User objects themselves. As a consequence, it fits a bit awkwardly into the range-based world and it has the weird extra-dereferencing 'operator->' that so many of our iterators have. I think this could be fixed by providing something which transforms a range of T&s into a range of T*s, but that *can* be separated into another patch, and it isn't yet 100% clear whether this is the right move. However, this change gets us most of the benefit and cleans up a substantial amount of code around Use and User. =] llvm-svn: 203364
2014-03-09 11:16:01 +08:00
for (User *U : GV->users())
if (!isa<Instruction>(U))
WUsers.push_back(WeakTrackingVH(U));
while (!WUsers.empty())
if (WeakTrackingVH WU = WUsers.pop_back_val()) {
ConstantExpr *CE = dyn_cast<ConstantExpr>(WU);
if (!CE || !replaceConstantExprOp(CE, P))
return false;
}
return true;
}
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.
if (!rewriteNonInstructionUses(GV, this) ||
!GV->getType()->isSized() || isZeroLengthArray(GV->getType()))
return false;
// Create replacement global.
ArrayType *NewType = createLoweredType(GV->getValueType());
Constant *NewInitializer = nullptr;
if (GV->hasInitializer())
NewInitializer = createLoweredInitializer(NewType,
GV->getInitializer());
GlobalVariable *NewGV =
new GlobalVariable(*M, NewType, GV->isConstant(), GV->getLinkage(),
NewInitializer, "", nullptr,
GlobalVariable::NotThreadLocal,
GV->getType()->getAddressSpace(),
GV->isExternallyInitialized());
// Update uses.
[C++11] Add range based accessors for the Use-Def chain of a Value. This requires a number of steps. 1) Move value_use_iterator into the Value class as an implementation detail 2) Change it to actually be a *Use* iterator rather than a *User* iterator. 3) Add an adaptor which is a User iterator that always looks through the Use to the User. 4) Wrap these in Value::use_iterator and Value::user_iterator typedefs. 5) Add the range adaptors as Value::uses() and Value::users(). 6) Update *all* of the callers to correctly distinguish between whether they wanted a use_iterator (and to explicitly dig out the User when needed), or a user_iterator which makes the Use itself totally opaque. Because #6 requires churning essentially everything that walked the Use-Def chains, I went ahead and added all of the range adaptors and switched them to range-based loops where appropriate. Also because the renaming requires at least churning every line of code, it didn't make any sense to split these up into multiple commits -- all of which would touch all of the same lies of code. The result is still not quite optimal. The Value::use_iterator is a nice regular iterator, but Value::user_iterator is an iterator over User*s rather than over the User objects themselves. As a consequence, it fits a bit awkwardly into the range-based world and it has the weird extra-dereferencing 'operator->' that so many of our iterators have. I think this could be fixed by providing something which transforms a range of T&s into a range of T*s, but that *can* be separated into another patch, and it isn't yet 100% clear whether this is the right move. However, this change gets us most of the benefit and cleans up a substantial amount of code around Use and User. =] llvm-svn: 203364
2014-03-09 11:16:01 +08:00
SmallVector<User *, 16> Users(GV->user_begin(), GV->user_end());
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);
Value *ThreadID = Builder.CreateCall(GetID, {});
Value *Addr = Builder.CreateInBoundsGEP(NewGV->getValueType(), NewGV,
{Builder.getInt64(0), ThreadID});
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;
for (GlobalVariable &GV : M.globals())
if (GV.isThreadLocal())
ThreadLocalGlobals.push_back(&GV);
for (unsigned I = 0, E = ThreadLocalGlobals.size(); I != E; ++I) {
MadeChange |= lowerGlobal(ThreadLocalGlobals[I]);
}
return MadeChange;
}