LLVM API Documentation
00001 //===-- XCoreLowerThreadLocal - Lower thread local variables --------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 /// 00010 /// \file 00011 /// \brief This file contains a pass that lowers thread local variables on the 00012 /// XCore. 00013 /// 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "XCore.h" 00017 #include "llvm/IR/Constants.h" 00018 #include "llvm/IR/DerivedTypes.h" 00019 #include "llvm/IR/GlobalVariable.h" 00020 #include "llvm/IR/IRBuilder.h" 00021 #include "llvm/IR/Intrinsics.h" 00022 #include "llvm/IR/Module.h" 00023 #include "llvm/IR/NoFolder.h" 00024 #include "llvm/IR/ValueHandle.h" 00025 #include "llvm/Pass.h" 00026 #include "llvm/Support/CommandLine.h" 00027 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 00028 00029 #define DEBUG_TYPE "xcore-lower-thread-local" 00030 00031 using namespace llvm; 00032 00033 static cl::opt<unsigned> MaxThreads( 00034 "xcore-max-threads", cl::Optional, 00035 cl::desc("Maximum number of threads (for emulation thread-local storage)"), 00036 cl::Hidden, cl::value_desc("number"), cl::init(8)); 00037 00038 namespace { 00039 /// Lowers thread local variables on the XCore. Each thread local variable is 00040 /// expanded to an array of n elements indexed by the thread ID where n is the 00041 /// fixed number hardware threads supported by the device. 00042 struct XCoreLowerThreadLocal : public ModulePass { 00043 static char ID; 00044 00045 XCoreLowerThreadLocal() : ModulePass(ID) { 00046 initializeXCoreLowerThreadLocalPass(*PassRegistry::getPassRegistry()); 00047 } 00048 00049 bool lowerGlobal(GlobalVariable *GV); 00050 00051 bool runOnModule(Module &M) override; 00052 }; 00053 } 00054 00055 char XCoreLowerThreadLocal::ID = 0; 00056 00057 INITIALIZE_PASS(XCoreLowerThreadLocal, "xcore-lower-thread-local", 00058 "Lower thread local variables", false, false) 00059 00060 ModulePass *llvm::createXCoreLowerThreadLocalPass() { 00061 return new XCoreLowerThreadLocal(); 00062 } 00063 00064 static ArrayType *createLoweredType(Type *OriginalType) { 00065 return ArrayType::get(OriginalType, MaxThreads); 00066 } 00067 00068 static Constant * 00069 createLoweredInitializer(ArrayType *NewType, Constant *OriginalInitializer) { 00070 SmallVector<Constant *, 8> Elements(MaxThreads); 00071 for (unsigned i = 0; i != MaxThreads; ++i) { 00072 Elements[i] = OriginalInitializer; 00073 } 00074 return ConstantArray::get(NewType, Elements); 00075 } 00076 00077 static Instruction * 00078 createReplacementInstr(ConstantExpr *CE, Instruction *Instr) { 00079 IRBuilder<true,NoFolder> Builder(Instr); 00080 unsigned OpCode = CE->getOpcode(); 00081 switch (OpCode) { 00082 case Instruction::GetElementPtr: { 00083 SmallVector<Value *,4> CEOpVec(CE->op_begin(), CE->op_end()); 00084 ArrayRef<Value *> CEOps(CEOpVec); 00085 return dyn_cast<Instruction>(Builder.CreateInBoundsGEP(CEOps[0], 00086 CEOps.slice(1))); 00087 } 00088 case Instruction::Add: 00089 case Instruction::Sub: 00090 case Instruction::Mul: 00091 case Instruction::UDiv: 00092 case Instruction::SDiv: 00093 case Instruction::FDiv: 00094 case Instruction::URem: 00095 case Instruction::SRem: 00096 case Instruction::FRem: 00097 case Instruction::Shl: 00098 case Instruction::LShr: 00099 case Instruction::AShr: 00100 case Instruction::And: 00101 case Instruction::Or: 00102 case Instruction::Xor: 00103 return dyn_cast<Instruction>( 00104 Builder.CreateBinOp((Instruction::BinaryOps)OpCode, 00105 CE->getOperand(0), CE->getOperand(1), 00106 CE->getName())); 00107 case Instruction::Trunc: 00108 case Instruction::ZExt: 00109 case Instruction::SExt: 00110 case Instruction::FPToUI: 00111 case Instruction::FPToSI: 00112 case Instruction::UIToFP: 00113 case Instruction::SIToFP: 00114 case Instruction::FPTrunc: 00115 case Instruction::FPExt: 00116 case Instruction::PtrToInt: 00117 case Instruction::IntToPtr: 00118 case Instruction::BitCast: 00119 return dyn_cast<Instruction>( 00120 Builder.CreateCast((Instruction::CastOps)OpCode, 00121 CE->getOperand(0), CE->getType(), 00122 CE->getName())); 00123 default: 00124 llvm_unreachable("Unhandled constant expression!\n"); 00125 } 00126 } 00127 00128 static bool replaceConstantExprOp(ConstantExpr *CE, Pass *P) { 00129 do { 00130 SmallVector<WeakVH,8> WUsers(CE->user_begin(), CE->user_end()); 00131 std::sort(WUsers.begin(), WUsers.end()); 00132 WUsers.erase(std::unique(WUsers.begin(), WUsers.end()), WUsers.end()); 00133 while (!WUsers.empty()) 00134 if (WeakVH WU = WUsers.pop_back_val()) { 00135 if (PHINode *PN = dyn_cast<PHINode>(WU)) { 00136 for (int I = 0, E = PN->getNumIncomingValues(); I < E; ++I) 00137 if (PN->getIncomingValue(I) == CE) { 00138 BasicBlock *PredBB = PN->getIncomingBlock(I); 00139 if (PredBB->getTerminator()->getNumSuccessors() > 1) 00140 PredBB = SplitEdge(PredBB, PN->getParent(), P); 00141 Instruction *InsertPos = PredBB->getTerminator(); 00142 Instruction *NewInst = createReplacementInstr(CE, InsertPos); 00143 PN->setOperand(I, NewInst); 00144 } 00145 } else if (Instruction *Instr = dyn_cast<Instruction>(WU)) { 00146 Instruction *NewInst = createReplacementInstr(CE, Instr); 00147 Instr->replaceUsesOfWith(CE, NewInst); 00148 } else { 00149 ConstantExpr *CExpr = dyn_cast<ConstantExpr>(WU); 00150 if (!CExpr || !replaceConstantExprOp(CExpr, P)) 00151 return false; 00152 } 00153 } 00154 } while (CE->hasNUsesOrMore(1)); // We need to check because a recursive 00155 // sibling may have used 'CE' when createReplacementInstr was called. 00156 CE->destroyConstant(); 00157 return true; 00158 } 00159 00160 static bool rewriteNonInstructionUses(GlobalVariable *GV, Pass *P) { 00161 SmallVector<WeakVH,8> WUsers; 00162 for (User *U : GV->users()) 00163 if (!isa<Instruction>(U)) 00164 WUsers.push_back(WeakVH(U)); 00165 while (!WUsers.empty()) 00166 if (WeakVH WU = WUsers.pop_back_val()) { 00167 ConstantExpr *CE = dyn_cast<ConstantExpr>(WU); 00168 if (!CE || !replaceConstantExprOp(CE, P)) 00169 return false; 00170 } 00171 return true; 00172 } 00173 00174 static bool isZeroLengthArray(Type *Ty) { 00175 ArrayType *AT = dyn_cast<ArrayType>(Ty); 00176 return AT && (AT->getNumElements() == 0); 00177 } 00178 00179 bool XCoreLowerThreadLocal::lowerGlobal(GlobalVariable *GV) { 00180 Module *M = GV->getParent(); 00181 LLVMContext &Ctx = M->getContext(); 00182 if (!GV->isThreadLocal()) 00183 return false; 00184 00185 // Skip globals that we can't lower and leave it for the backend to error. 00186 if (!rewriteNonInstructionUses(GV, this) || 00187 !GV->getType()->isSized() || isZeroLengthArray(GV->getType())) 00188 return false; 00189 00190 // Create replacement global. 00191 ArrayType *NewType = createLoweredType(GV->getType()->getElementType()); 00192 Constant *NewInitializer = nullptr; 00193 if (GV->hasInitializer()) 00194 NewInitializer = createLoweredInitializer(NewType, 00195 GV->getInitializer()); 00196 GlobalVariable *NewGV = 00197 new GlobalVariable(*M, NewType, GV->isConstant(), GV->getLinkage(), 00198 NewInitializer, "", nullptr, 00199 GlobalVariable::NotThreadLocal, 00200 GV->getType()->getAddressSpace(), 00201 GV->isExternallyInitialized()); 00202 00203 // Update uses. 00204 SmallVector<User *, 16> Users(GV->user_begin(), GV->user_end()); 00205 for (unsigned I = 0, E = Users.size(); I != E; ++I) { 00206 User *U = Users[I]; 00207 Instruction *Inst = cast<Instruction>(U); 00208 IRBuilder<> Builder(Inst); 00209 Function *GetID = Intrinsic::getDeclaration(GV->getParent(), 00210 Intrinsic::xcore_getid); 00211 Value *ThreadID = Builder.CreateCall(GetID); 00212 SmallVector<Value *, 2> Indices; 00213 Indices.push_back(Constant::getNullValue(Type::getInt64Ty(Ctx))); 00214 Indices.push_back(ThreadID); 00215 Value *Addr = Builder.CreateInBoundsGEP(NewGV, Indices); 00216 U->replaceUsesOfWith(GV, Addr); 00217 } 00218 00219 // Remove old global. 00220 NewGV->takeName(GV); 00221 GV->eraseFromParent(); 00222 return true; 00223 } 00224 00225 bool XCoreLowerThreadLocal::runOnModule(Module &M) { 00226 // Find thread local globals. 00227 bool MadeChange = false; 00228 SmallVector<GlobalVariable *, 16> ThreadLocalGlobals; 00229 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); 00230 GVI != E; ++GVI) { 00231 GlobalVariable *GV = GVI; 00232 if (GV->isThreadLocal()) 00233 ThreadLocalGlobals.push_back(GV); 00234 } 00235 for (unsigned I = 0, E = ThreadLocalGlobals.size(); I != E; ++I) { 00236 MadeChange |= lowerGlobal(ThreadLocalGlobals[I]); 00237 } 00238 return MadeChange; 00239 }