LLVM: lib/Target/XCore/XCoreLowerThreadLocal.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

21#include "llvm/IR/IntrinsicsXCore.h"

27

28#define DEBUG_TYPE "xcore-lower-thread-local"

29

30using namespace llvm;

31

34 cl::desc("Maximum number of threads (for emulation thread-local storage)"),

36

37namespace {

38

39

40

41 struct XCoreLowerThreadLocal : public ModulePass {

42 static char ID;

43

45

46 bool lowerGlobal(GlobalVariable *GV);

47

48 bool runOnModule(Module &M) override;

49 };

50}

51

52char XCoreLowerThreadLocal::ID = 0;

53

55 "Lower thread local variables", false, false)

56

58 return new XCoreLowerThreadLocal();

59}

60

64

68 for (unsigned i = 0; i != MaxThreads; ++i) {

69 Elements[i] = OriginalInitializer;

70 }

72}

73

74

76 do {

80 while (!WUsers.empty())

83 for (int I = 0, E = PN->getNumIncomingValues(); I < E; ++I)

84 if (PN->getIncomingValue(I) == CE) {

85 BasicBlock *PredBB = PN->getIncomingBlock(I);

90 Instruction *NewInst = CE->getAsInstruction();

92 PN->setOperand(I, NewInst);

93 }

95 Instruction *NewInst = CE->getAsInstruction();

96 NewInst->insertBefore(*Instr->getParent(), Instr->getIterator());

97 Instr->replaceUsesOfWith(CE, NewInst);

98 } else {

101 return false;

102 }

103 }

104 } while (CE->hasNUsesOrMore(1));

105

106 CE->destroyConstant();

107 return true;

108}

109

115 while (!WUsers.empty())

119 return false;

120 }

121 return true;

122}

123

126 return AT && (AT->getNumElements() == 0);

127}

128

129bool XCoreLowerThreadLocal::lowerGlobal(GlobalVariable *GV) {

132 return false;

133

134

137 return false;

138

139

141 Constant *NewInitializer = nullptr;

145 GlobalVariable *NewGV =

147 NewInitializer, "", nullptr,

148 GlobalVariable::NotThreadLocal,

151

152

154 for (User *U : Users) {

157 Value *ThreadID = Builder.CreateIntrinsic(Intrinsic::xcore_getid, {});

158 Value *Addr = Builder.CreateInBoundsGEP(NewGV->getValueType(), NewGV,

159 {Builder.getInt64(0), ThreadID});

160 U->replaceUsesOfWith(GV, Addr);

161 }

162

163

166 return true;

167}

168

169bool XCoreLowerThreadLocal::runOnModule(Module &M) {

170

171 bool MadeChange = false;

173 for (GlobalVariable &GV : M.globals())

175 ThreadLocalGlobals.push_back(&GV);

176 for (GlobalVariable *GV : ThreadLocalGlobals)

177 MadeChange |= lowerGlobal(GV);

178 return MadeChange;

179}

static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")

This file contains the declarations for the subclasses of Constant, which represent the different fla...

Module.h This file contains the declarations for the Module class.

iv Induction Variable Users

Machine Check Debug Module

#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)

static bool replaceConstantExprOp(ConstantExpr *CE, Pass *P)

Definition XCoreLowerThreadLocal.cpp:75

static bool isZeroLengthArray(Type *Ty)

Definition XCoreLowerThreadLocal.cpp:124

static Constant * createLoweredInitializer(ArrayType *NewType, Constant *OriginalInitializer)

Definition XCoreLowerThreadLocal.cpp:66

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))

static bool rewriteNonInstructionUses(GlobalVariable *GV, Pass *P)

Definition XCoreLowerThreadLocal.cpp:110

static ArrayType * createLoweredType(Type *OriginalType)

Definition XCoreLowerThreadLocal.cpp:61

static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)

This static method is the primary way to construct an ArrayType.

LLVM Basic Block Representation.

const Function * getParent() const

Return the enclosing method, or null if none.

InstListType::iterator iterator

Instruction iterators...

const Instruction * getTerminator() const LLVM_READONLY

Returns the terminator instruction if the block is well formed or null if the block is not well forme...

static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)

A constant value that is initialized with an expression using other constant values.

This is an important base class in LLVM.

bool isThreadLocal() const

If the value is "Thread Local", its value isn't shared by the threads.

LinkageTypes getLinkage() const

Module * getParent()

Get the module that this global value is contained inside of...

PointerType * getType() const

Global values are always pointers.

Type * getValueType() const

const Constant * getInitializer() const

getInitializer - Return the initializer for this global variable.

bool isExternallyInitialized() const

bool hasInitializer() const

Definitions have initializers, declarations don't.

bool isConstant() const

If the value is a global constant, its value is immutable throughout the runtime execution of the pro...

LLVM_ABI void eraseFromParent()

eraseFromParent - This method unlinks 'this' from the containing module and deletes it.

LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY

Return the number of successors that this instruction has.

LLVM_ABI void insertBefore(InstListType::iterator InsertPos)

Insert an unlinked instruction into a basic block immediately before the specified position.

ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...

Pass interface - Implemented by all 'passes'.

unsigned getAddressSpace() const

Return the address space of the Pointer type.

iterator erase(const_iterator CI)

void push_back(const T &Elt)

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

The instances of the Type class are immutable: once they are created, they are never changed.

bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const

Return true if it makes sense to take the size of this type.

iterator_range< user_iterator > users()

LLVM_ABI void takeName(Value *V)

Transfer the name from V to this value.

Value handle that is nullable, but tries to track the Value.

self_iterator getIterator()

unsigned ID

LLVM IR allows to use arbitrary numbers as calling convention identifiers.

initializer< Ty > init(const Ty &Val)

friend class Instruction

Iterator for Instructions in a `BasicBlock.

This is an optimization pass for GlobalISel generic memory operations.

FunctionAddr VTableAddr Value

decltype(auto) dyn_cast(const From &Val)

dyn_cast - Return the argument parameter cast to the specified type.

auto unique(Range &&R, Predicate P)

void sort(IteratorTy Start, IteratorTy End)

class LLVM_GSL_OWNER SmallVector

Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...

bool isa(const From &Val)

isa - Return true if the parameter to the template is an instance of one of the template type argu...

IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >

ModulePass * createXCoreLowerThreadLocalPass()

decltype(auto) cast(const From &Val)

cast - Return the argument parameter cast to the specified type.

LLVM_ABI BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")

Split the edge connecting the specified blocks, and return the newly created basic block between From...