LLVM API Documentation
00001 //===--- Scalarizer.cpp - Scalarize vector operations ---------------------===// 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 // This pass converts vector operations into scalar operations, in order 00011 // to expose optimization opportunities on the individual scalar operations. 00012 // It is mainly intended for targets that do not have vector units, but it 00013 // may also be useful for revectorizing code to different vector widths. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #include "llvm/ADT/STLExtras.h" 00018 #include "llvm/IR/IRBuilder.h" 00019 #include "llvm/IR/InstVisitor.h" 00020 #include "llvm/Pass.h" 00021 #include "llvm/Support/CommandLine.h" 00022 #include "llvm/Transforms/Scalar.h" 00023 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 00024 00025 using namespace llvm; 00026 00027 #define DEBUG_TYPE "scalarizer" 00028 00029 namespace { 00030 // Used to store the scattered form of a vector. 00031 typedef SmallVector<Value *, 8> ValueVector; 00032 00033 // Used to map a vector Value to its scattered form. We use std::map 00034 // because we want iterators to persist across insertion and because the 00035 // values are relatively large. 00036 typedef std::map<Value *, ValueVector> ScatterMap; 00037 00038 // Lists Instructions that have been replaced with scalar implementations, 00039 // along with a pointer to their scattered forms. 00040 typedef SmallVector<std::pair<Instruction *, ValueVector *>, 16> GatherList; 00041 00042 // Provides a very limited vector-like interface for lazily accessing one 00043 // component of a scattered vector or vector pointer. 00044 class Scatterer { 00045 public: 00046 Scatterer() {} 00047 00048 // Scatter V into Size components. If new instructions are needed, 00049 // insert them before BBI in BB. If Cache is nonnull, use it to cache 00050 // the results. 00051 Scatterer(BasicBlock *bb, BasicBlock::iterator bbi, Value *v, 00052 ValueVector *cachePtr = nullptr); 00053 00054 // Return component I, creating a new Value for it if necessary. 00055 Value *operator[](unsigned I); 00056 00057 // Return the number of components. 00058 unsigned size() const { return Size; } 00059 00060 private: 00061 BasicBlock *BB; 00062 BasicBlock::iterator BBI; 00063 Value *V; 00064 ValueVector *CachePtr; 00065 PointerType *PtrTy; 00066 ValueVector Tmp; 00067 unsigned Size; 00068 }; 00069 00070 // FCmpSpliiter(FCI)(Builder, X, Y, Name) uses Builder to create an FCmp 00071 // called Name that compares X and Y in the same way as FCI. 00072 struct FCmpSplitter { 00073 FCmpSplitter(FCmpInst &fci) : FCI(fci) {} 00074 Value *operator()(IRBuilder<> &Builder, Value *Op0, Value *Op1, 00075 const Twine &Name) const { 00076 return Builder.CreateFCmp(FCI.getPredicate(), Op0, Op1, Name); 00077 } 00078 FCmpInst &FCI; 00079 }; 00080 00081 // ICmpSpliiter(ICI)(Builder, X, Y, Name) uses Builder to create an ICmp 00082 // called Name that compares X and Y in the same way as ICI. 00083 struct ICmpSplitter { 00084 ICmpSplitter(ICmpInst &ici) : ICI(ici) {} 00085 Value *operator()(IRBuilder<> &Builder, Value *Op0, Value *Op1, 00086 const Twine &Name) const { 00087 return Builder.CreateICmp(ICI.getPredicate(), Op0, Op1, Name); 00088 } 00089 ICmpInst &ICI; 00090 }; 00091 00092 // BinarySpliiter(BO)(Builder, X, Y, Name) uses Builder to create 00093 // a binary operator like BO called Name with operands X and Y. 00094 struct BinarySplitter { 00095 BinarySplitter(BinaryOperator &bo) : BO(bo) {} 00096 Value *operator()(IRBuilder<> &Builder, Value *Op0, Value *Op1, 00097 const Twine &Name) const { 00098 return Builder.CreateBinOp(BO.getOpcode(), Op0, Op1, Name); 00099 } 00100 BinaryOperator &BO; 00101 }; 00102 00103 // Information about a load or store that we're scalarizing. 00104 struct VectorLayout { 00105 VectorLayout() : VecTy(nullptr), ElemTy(nullptr), VecAlign(0), ElemSize(0) {} 00106 00107 // Return the alignment of element I. 00108 uint64_t getElemAlign(unsigned I) { 00109 return MinAlign(VecAlign, I * ElemSize); 00110 } 00111 00112 // The type of the vector. 00113 VectorType *VecTy; 00114 00115 // The type of each element. 00116 Type *ElemTy; 00117 00118 // The alignment of the vector. 00119 uint64_t VecAlign; 00120 00121 // The size of each element. 00122 uint64_t ElemSize; 00123 }; 00124 00125 class Scalarizer : public FunctionPass, 00126 public InstVisitor<Scalarizer, bool> { 00127 public: 00128 static char ID; 00129 00130 Scalarizer() : 00131 FunctionPass(ID) { 00132 initializeScalarizerPass(*PassRegistry::getPassRegistry()); 00133 } 00134 00135 bool doInitialization(Module &M) override; 00136 bool runOnFunction(Function &F) override; 00137 00138 // InstVisitor methods. They return true if the instruction was scalarized, 00139 // false if nothing changed. 00140 bool visitInstruction(Instruction &) { return false; } 00141 bool visitSelectInst(SelectInst &SI); 00142 bool visitICmpInst(ICmpInst &); 00143 bool visitFCmpInst(FCmpInst &); 00144 bool visitBinaryOperator(BinaryOperator &); 00145 bool visitGetElementPtrInst(GetElementPtrInst &); 00146 bool visitCastInst(CastInst &); 00147 bool visitBitCastInst(BitCastInst &); 00148 bool visitShuffleVectorInst(ShuffleVectorInst &); 00149 bool visitPHINode(PHINode &); 00150 bool visitLoadInst(LoadInst &); 00151 bool visitStoreInst(StoreInst &); 00152 00153 private: 00154 Scatterer scatter(Instruction *, Value *); 00155 void gather(Instruction *, const ValueVector &); 00156 bool canTransferMetadata(unsigned Kind); 00157 void transferMetadata(Instruction *, const ValueVector &); 00158 bool getVectorLayout(Type *, unsigned, VectorLayout &); 00159 bool finish(); 00160 00161 template<typename T> bool splitBinary(Instruction &, const T &); 00162 00163 ScatterMap Scattered; 00164 GatherList Gathered; 00165 unsigned ParallelLoopAccessMDKind; 00166 const DataLayout *DL; 00167 }; 00168 00169 char Scalarizer::ID = 0; 00170 } // end anonymous namespace 00171 00172 // This is disabled by default because having separate loads and stores makes 00173 // it more likely that the -combiner-alias-analysis limits will be reached. 00174 static cl::opt<bool> ScalarizeLoadStore 00175 ("scalarize-load-store", cl::Hidden, cl::init(false), 00176 cl::desc("Allow the scalarizer pass to scalarize loads and store")); 00177 00178 INITIALIZE_PASS(Scalarizer, "scalarizer", "Scalarize vector operations", 00179 false, false) 00180 00181 Scatterer::Scatterer(BasicBlock *bb, BasicBlock::iterator bbi, Value *v, 00182 ValueVector *cachePtr) 00183 : BB(bb), BBI(bbi), V(v), CachePtr(cachePtr) { 00184 Type *Ty = V->getType(); 00185 PtrTy = dyn_cast<PointerType>(Ty); 00186 if (PtrTy) 00187 Ty = PtrTy->getElementType(); 00188 Size = Ty->getVectorNumElements(); 00189 if (!CachePtr) 00190 Tmp.resize(Size, nullptr); 00191 else if (CachePtr->empty()) 00192 CachePtr->resize(Size, nullptr); 00193 else 00194 assert(Size == CachePtr->size() && "Inconsistent vector sizes"); 00195 } 00196 00197 // Return component I, creating a new Value for it if necessary. 00198 Value *Scatterer::operator[](unsigned I) { 00199 ValueVector &CV = (CachePtr ? *CachePtr : Tmp); 00200 // Try to reuse a previous value. 00201 if (CV[I]) 00202 return CV[I]; 00203 IRBuilder<> Builder(BB, BBI); 00204 if (PtrTy) { 00205 if (!CV[0]) { 00206 Type *Ty = 00207 PointerType::get(PtrTy->getElementType()->getVectorElementType(), 00208 PtrTy->getAddressSpace()); 00209 CV[0] = Builder.CreateBitCast(V, Ty, V->getName() + ".i0"); 00210 } 00211 if (I != 0) 00212 CV[I] = Builder.CreateConstGEP1_32(CV[0], I, 00213 V->getName() + ".i" + Twine(I)); 00214 } else { 00215 // Search through a chain of InsertElementInsts looking for element I. 00216 // Record other elements in the cache. The new V is still suitable 00217 // for all uncached indices. 00218 for (;;) { 00219 InsertElementInst *Insert = dyn_cast<InsertElementInst>(V); 00220 if (!Insert) 00221 break; 00222 ConstantInt *Idx = dyn_cast<ConstantInt>(Insert->getOperand(2)); 00223 if (!Idx) 00224 break; 00225 unsigned J = Idx->getZExtValue(); 00226 CV[J] = Insert->getOperand(1); 00227 V = Insert->getOperand(0); 00228 if (I == J) 00229 return CV[J]; 00230 } 00231 CV[I] = Builder.CreateExtractElement(V, Builder.getInt32(I), 00232 V->getName() + ".i" + Twine(I)); 00233 } 00234 return CV[I]; 00235 } 00236 00237 bool Scalarizer::doInitialization(Module &M) { 00238 ParallelLoopAccessMDKind = 00239 M.getContext().getMDKindID("llvm.mem.parallel_loop_access"); 00240 return false; 00241 } 00242 00243 bool Scalarizer::runOnFunction(Function &F) { 00244 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); 00245 DL = DLP ? &DLP->getDataLayout() : nullptr; 00246 for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) { 00247 BasicBlock *BB = BBI; 00248 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) { 00249 Instruction *I = II; 00250 bool Done = visit(I); 00251 ++II; 00252 if (Done && I->getType()->isVoidTy()) 00253 I->eraseFromParent(); 00254 } 00255 } 00256 return finish(); 00257 } 00258 00259 // Return a scattered form of V that can be accessed by Point. V must be a 00260 // vector or a pointer to a vector. 00261 Scatterer Scalarizer::scatter(Instruction *Point, Value *V) { 00262 if (Argument *VArg = dyn_cast<Argument>(V)) { 00263 // Put the scattered form of arguments in the entry block, 00264 // so that it can be used everywhere. 00265 Function *F = VArg->getParent(); 00266 BasicBlock *BB = &F->getEntryBlock(); 00267 return Scatterer(BB, BB->begin(), V, &Scattered[V]); 00268 } 00269 if (Instruction *VOp = dyn_cast<Instruction>(V)) { 00270 // Put the scattered form of an instruction directly after the 00271 // instruction. 00272 BasicBlock *BB = VOp->getParent(); 00273 return Scatterer(BB, std::next(BasicBlock::iterator(VOp)), 00274 V, &Scattered[V]); 00275 } 00276 // In the fallback case, just put the scattered before Point and 00277 // keep the result local to Point. 00278 return Scatterer(Point->getParent(), Point, V); 00279 } 00280 00281 // Replace Op with the gathered form of the components in CV. Defer the 00282 // deletion of Op and creation of the gathered form to the end of the pass, 00283 // so that we can avoid creating the gathered form if all uses of Op are 00284 // replaced with uses of CV. 00285 void Scalarizer::gather(Instruction *Op, const ValueVector &CV) { 00286 // Since we're not deleting Op yet, stub out its operands, so that it 00287 // doesn't make anything live unnecessarily. 00288 for (unsigned I = 0, E = Op->getNumOperands(); I != E; ++I) 00289 Op->setOperand(I, UndefValue::get(Op->getOperand(I)->getType())); 00290 00291 transferMetadata(Op, CV); 00292 00293 // If we already have a scattered form of Op (created from ExtractElements 00294 // of Op itself), replace them with the new form. 00295 ValueVector &SV = Scattered[Op]; 00296 if (!SV.empty()) { 00297 for (unsigned I = 0, E = SV.size(); I != E; ++I) { 00298 Instruction *Old = cast<Instruction>(SV[I]); 00299 CV[I]->takeName(Old); 00300 Old->replaceAllUsesWith(CV[I]); 00301 Old->eraseFromParent(); 00302 } 00303 } 00304 SV = CV; 00305 Gathered.push_back(GatherList::value_type(Op, &SV)); 00306 } 00307 00308 // Return true if it is safe to transfer the given metadata tag from 00309 // vector to scalar instructions. 00310 bool Scalarizer::canTransferMetadata(unsigned Tag) { 00311 return (Tag == LLVMContext::MD_tbaa 00312 || Tag == LLVMContext::MD_fpmath 00313 || Tag == LLVMContext::MD_tbaa_struct 00314 || Tag == LLVMContext::MD_invariant_load 00315 || Tag == LLVMContext::MD_alias_scope 00316 || Tag == LLVMContext::MD_noalias 00317 || Tag == ParallelLoopAccessMDKind); 00318 } 00319 00320 // Transfer metadata from Op to the instructions in CV if it is known 00321 // to be safe to do so. 00322 void Scalarizer::transferMetadata(Instruction *Op, const ValueVector &CV) { 00323 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 00324 Op->getAllMetadataOtherThanDebugLoc(MDs); 00325 for (unsigned I = 0, E = CV.size(); I != E; ++I) { 00326 if (Instruction *New = dyn_cast<Instruction>(CV[I])) { 00327 for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator 00328 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) 00329 if (canTransferMetadata(MI->first)) 00330 New->setMetadata(MI->first, MI->second); 00331 New->setDebugLoc(Op->getDebugLoc()); 00332 } 00333 } 00334 } 00335 00336 // Try to fill in Layout from Ty, returning true on success. Alignment is 00337 // the alignment of the vector, or 0 if the ABI default should be used. 00338 bool Scalarizer::getVectorLayout(Type *Ty, unsigned Alignment, 00339 VectorLayout &Layout) { 00340 if (!DL) 00341 return false; 00342 00343 // Make sure we're dealing with a vector. 00344 Layout.VecTy = dyn_cast<VectorType>(Ty); 00345 if (!Layout.VecTy) 00346 return false; 00347 00348 // Check that we're dealing with full-byte elements. 00349 Layout.ElemTy = Layout.VecTy->getElementType(); 00350 if (DL->getTypeSizeInBits(Layout.ElemTy) != 00351 DL->getTypeStoreSizeInBits(Layout.ElemTy)) 00352 return false; 00353 00354 if (Alignment) 00355 Layout.VecAlign = Alignment; 00356 else 00357 Layout.VecAlign = DL->getABITypeAlignment(Layout.VecTy); 00358 Layout.ElemSize = DL->getTypeStoreSize(Layout.ElemTy); 00359 return true; 00360 } 00361 00362 // Scalarize two-operand instruction I, using Split(Builder, X, Y, Name) 00363 // to create an instruction like I with operands X and Y and name Name. 00364 template<typename Splitter> 00365 bool Scalarizer::splitBinary(Instruction &I, const Splitter &Split) { 00366 VectorType *VT = dyn_cast<VectorType>(I.getType()); 00367 if (!VT) 00368 return false; 00369 00370 unsigned NumElems = VT->getNumElements(); 00371 IRBuilder<> Builder(I.getParent(), &I); 00372 Scatterer Op0 = scatter(&I, I.getOperand(0)); 00373 Scatterer Op1 = scatter(&I, I.getOperand(1)); 00374 assert(Op0.size() == NumElems && "Mismatched binary operation"); 00375 assert(Op1.size() == NumElems && "Mismatched binary operation"); 00376 ValueVector Res; 00377 Res.resize(NumElems); 00378 for (unsigned Elem = 0; Elem < NumElems; ++Elem) 00379 Res[Elem] = Split(Builder, Op0[Elem], Op1[Elem], 00380 I.getName() + ".i" + Twine(Elem)); 00381 gather(&I, Res); 00382 return true; 00383 } 00384 00385 bool Scalarizer::visitSelectInst(SelectInst &SI) { 00386 VectorType *VT = dyn_cast<VectorType>(SI.getType()); 00387 if (!VT) 00388 return false; 00389 00390 unsigned NumElems = VT->getNumElements(); 00391 IRBuilder<> Builder(SI.getParent(), &SI); 00392 Scatterer Op1 = scatter(&SI, SI.getOperand(1)); 00393 Scatterer Op2 = scatter(&SI, SI.getOperand(2)); 00394 assert(Op1.size() == NumElems && "Mismatched select"); 00395 assert(Op2.size() == NumElems && "Mismatched select"); 00396 ValueVector Res; 00397 Res.resize(NumElems); 00398 00399 if (SI.getOperand(0)->getType()->isVectorTy()) { 00400 Scatterer Op0 = scatter(&SI, SI.getOperand(0)); 00401 assert(Op0.size() == NumElems && "Mismatched select"); 00402 for (unsigned I = 0; I < NumElems; ++I) 00403 Res[I] = Builder.CreateSelect(Op0[I], Op1[I], Op2[I], 00404 SI.getName() + ".i" + Twine(I)); 00405 } else { 00406 Value *Op0 = SI.getOperand(0); 00407 for (unsigned I = 0; I < NumElems; ++I) 00408 Res[I] = Builder.CreateSelect(Op0, Op1[I], Op2[I], 00409 SI.getName() + ".i" + Twine(I)); 00410 } 00411 gather(&SI, Res); 00412 return true; 00413 } 00414 00415 bool Scalarizer::visitICmpInst(ICmpInst &ICI) { 00416 return splitBinary(ICI, ICmpSplitter(ICI)); 00417 } 00418 00419 bool Scalarizer::visitFCmpInst(FCmpInst &FCI) { 00420 return splitBinary(FCI, FCmpSplitter(FCI)); 00421 } 00422 00423 bool Scalarizer::visitBinaryOperator(BinaryOperator &BO) { 00424 return splitBinary(BO, BinarySplitter(BO)); 00425 } 00426 00427 bool Scalarizer::visitGetElementPtrInst(GetElementPtrInst &GEPI) { 00428 VectorType *VT = dyn_cast<VectorType>(GEPI.getType()); 00429 if (!VT) 00430 return false; 00431 00432 IRBuilder<> Builder(GEPI.getParent(), &GEPI); 00433 unsigned NumElems = VT->getNumElements(); 00434 unsigned NumIndices = GEPI.getNumIndices(); 00435 00436 Scatterer Base = scatter(&GEPI, GEPI.getOperand(0)); 00437 00438 SmallVector<Scatterer, 8> Ops; 00439 Ops.resize(NumIndices); 00440 for (unsigned I = 0; I < NumIndices; ++I) 00441 Ops[I] = scatter(&GEPI, GEPI.getOperand(I + 1)); 00442 00443 ValueVector Res; 00444 Res.resize(NumElems); 00445 for (unsigned I = 0; I < NumElems; ++I) { 00446 SmallVector<Value *, 8> Indices; 00447 Indices.resize(NumIndices); 00448 for (unsigned J = 0; J < NumIndices; ++J) 00449 Indices[J] = Ops[J][I]; 00450 Res[I] = Builder.CreateGEP(Base[I], Indices, 00451 GEPI.getName() + ".i" + Twine(I)); 00452 if (GEPI.isInBounds()) 00453 if (GetElementPtrInst *NewGEPI = dyn_cast<GetElementPtrInst>(Res[I])) 00454 NewGEPI->setIsInBounds(); 00455 } 00456 gather(&GEPI, Res); 00457 return true; 00458 } 00459 00460 bool Scalarizer::visitCastInst(CastInst &CI) { 00461 VectorType *VT = dyn_cast<VectorType>(CI.getDestTy()); 00462 if (!VT) 00463 return false; 00464 00465 unsigned NumElems = VT->getNumElements(); 00466 IRBuilder<> Builder(CI.getParent(), &CI); 00467 Scatterer Op0 = scatter(&CI, CI.getOperand(0)); 00468 assert(Op0.size() == NumElems && "Mismatched cast"); 00469 ValueVector Res; 00470 Res.resize(NumElems); 00471 for (unsigned I = 0; I < NumElems; ++I) 00472 Res[I] = Builder.CreateCast(CI.getOpcode(), Op0[I], VT->getElementType(), 00473 CI.getName() + ".i" + Twine(I)); 00474 gather(&CI, Res); 00475 return true; 00476 } 00477 00478 bool Scalarizer::visitBitCastInst(BitCastInst &BCI) { 00479 VectorType *DstVT = dyn_cast<VectorType>(BCI.getDestTy()); 00480 VectorType *SrcVT = dyn_cast<VectorType>(BCI.getSrcTy()); 00481 if (!DstVT || !SrcVT) 00482 return false; 00483 00484 unsigned DstNumElems = DstVT->getNumElements(); 00485 unsigned SrcNumElems = SrcVT->getNumElements(); 00486 IRBuilder<> Builder(BCI.getParent(), &BCI); 00487 Scatterer Op0 = scatter(&BCI, BCI.getOperand(0)); 00488 ValueVector Res; 00489 Res.resize(DstNumElems); 00490 00491 if (DstNumElems == SrcNumElems) { 00492 for (unsigned I = 0; I < DstNumElems; ++I) 00493 Res[I] = Builder.CreateBitCast(Op0[I], DstVT->getElementType(), 00494 BCI.getName() + ".i" + Twine(I)); 00495 } else if (DstNumElems > SrcNumElems) { 00496 // <M x t1> -> <N*M x t2>. Convert each t1 to <N x t2> and copy the 00497 // individual elements to the destination. 00498 unsigned FanOut = DstNumElems / SrcNumElems; 00499 Type *MidTy = VectorType::get(DstVT->getElementType(), FanOut); 00500 unsigned ResI = 0; 00501 for (unsigned Op0I = 0; Op0I < SrcNumElems; ++Op0I) { 00502 Value *V = Op0[Op0I]; 00503 Instruction *VI; 00504 // Look through any existing bitcasts before converting to <N x t2>. 00505 // In the best case, the resulting conversion might be a no-op. 00506 while ((VI = dyn_cast<Instruction>(V)) && 00507 VI->getOpcode() == Instruction::BitCast) 00508 V = VI->getOperand(0); 00509 V = Builder.CreateBitCast(V, MidTy, V->getName() + ".cast"); 00510 Scatterer Mid = scatter(&BCI, V); 00511 for (unsigned MidI = 0; MidI < FanOut; ++MidI) 00512 Res[ResI++] = Mid[MidI]; 00513 } 00514 } else { 00515 // <N*M x t1> -> <M x t2>. Convert each group of <N x t1> into a t2. 00516 unsigned FanIn = SrcNumElems / DstNumElems; 00517 Type *MidTy = VectorType::get(SrcVT->getElementType(), FanIn); 00518 unsigned Op0I = 0; 00519 for (unsigned ResI = 0; ResI < DstNumElems; ++ResI) { 00520 Value *V = UndefValue::get(MidTy); 00521 for (unsigned MidI = 0; MidI < FanIn; ++MidI) 00522 V = Builder.CreateInsertElement(V, Op0[Op0I++], Builder.getInt32(MidI), 00523 BCI.getName() + ".i" + Twine(ResI) 00524 + ".upto" + Twine(MidI)); 00525 Res[ResI] = Builder.CreateBitCast(V, DstVT->getElementType(), 00526 BCI.getName() + ".i" + Twine(ResI)); 00527 } 00528 } 00529 gather(&BCI, Res); 00530 return true; 00531 } 00532 00533 bool Scalarizer::visitShuffleVectorInst(ShuffleVectorInst &SVI) { 00534 VectorType *VT = dyn_cast<VectorType>(SVI.getType()); 00535 if (!VT) 00536 return false; 00537 00538 unsigned NumElems = VT->getNumElements(); 00539 Scatterer Op0 = scatter(&SVI, SVI.getOperand(0)); 00540 Scatterer Op1 = scatter(&SVI, SVI.getOperand(1)); 00541 ValueVector Res; 00542 Res.resize(NumElems); 00543 00544 for (unsigned I = 0; I < NumElems; ++I) { 00545 int Selector = SVI.getMaskValue(I); 00546 if (Selector < 0) 00547 Res[I] = UndefValue::get(VT->getElementType()); 00548 else if (unsigned(Selector) < Op0.size()) 00549 Res[I] = Op0[Selector]; 00550 else 00551 Res[I] = Op1[Selector - Op0.size()]; 00552 } 00553 gather(&SVI, Res); 00554 return true; 00555 } 00556 00557 bool Scalarizer::visitPHINode(PHINode &PHI) { 00558 VectorType *VT = dyn_cast<VectorType>(PHI.getType()); 00559 if (!VT) 00560 return false; 00561 00562 unsigned NumElems = VT->getNumElements(); 00563 IRBuilder<> Builder(PHI.getParent(), &PHI); 00564 ValueVector Res; 00565 Res.resize(NumElems); 00566 00567 unsigned NumOps = PHI.getNumOperands(); 00568 for (unsigned I = 0; I < NumElems; ++I) 00569 Res[I] = Builder.CreatePHI(VT->getElementType(), NumOps, 00570 PHI.getName() + ".i" + Twine(I)); 00571 00572 for (unsigned I = 0; I < NumOps; ++I) { 00573 Scatterer Op = scatter(&PHI, PHI.getIncomingValue(I)); 00574 BasicBlock *IncomingBlock = PHI.getIncomingBlock(I); 00575 for (unsigned J = 0; J < NumElems; ++J) 00576 cast<PHINode>(Res[J])->addIncoming(Op[J], IncomingBlock); 00577 } 00578 gather(&PHI, Res); 00579 return true; 00580 } 00581 00582 bool Scalarizer::visitLoadInst(LoadInst &LI) { 00583 if (!ScalarizeLoadStore) 00584 return false; 00585 if (!LI.isSimple()) 00586 return false; 00587 00588 VectorLayout Layout; 00589 if (!getVectorLayout(LI.getType(), LI.getAlignment(), Layout)) 00590 return false; 00591 00592 unsigned NumElems = Layout.VecTy->getNumElements(); 00593 IRBuilder<> Builder(LI.getParent(), &LI); 00594 Scatterer Ptr = scatter(&LI, LI.getPointerOperand()); 00595 ValueVector Res; 00596 Res.resize(NumElems); 00597 00598 for (unsigned I = 0; I < NumElems; ++I) 00599 Res[I] = Builder.CreateAlignedLoad(Ptr[I], Layout.getElemAlign(I), 00600 LI.getName() + ".i" + Twine(I)); 00601 gather(&LI, Res); 00602 return true; 00603 } 00604 00605 bool Scalarizer::visitStoreInst(StoreInst &SI) { 00606 if (!ScalarizeLoadStore) 00607 return false; 00608 if (!SI.isSimple()) 00609 return false; 00610 00611 VectorLayout Layout; 00612 Value *FullValue = SI.getValueOperand(); 00613 if (!getVectorLayout(FullValue->getType(), SI.getAlignment(), Layout)) 00614 return false; 00615 00616 unsigned NumElems = Layout.VecTy->getNumElements(); 00617 IRBuilder<> Builder(SI.getParent(), &SI); 00618 Scatterer Ptr = scatter(&SI, SI.getPointerOperand()); 00619 Scatterer Val = scatter(&SI, FullValue); 00620 00621 ValueVector Stores; 00622 Stores.resize(NumElems); 00623 for (unsigned I = 0; I < NumElems; ++I) { 00624 unsigned Align = Layout.getElemAlign(I); 00625 Stores[I] = Builder.CreateAlignedStore(Val[I], Ptr[I], Align); 00626 } 00627 transferMetadata(&SI, Stores); 00628 return true; 00629 } 00630 00631 // Delete the instructions that we scalarized. If a full vector result 00632 // is still needed, recreate it using InsertElements. 00633 bool Scalarizer::finish() { 00634 if (Gathered.empty()) 00635 return false; 00636 for (GatherList::iterator GMI = Gathered.begin(), GME = Gathered.end(); 00637 GMI != GME; ++GMI) { 00638 Instruction *Op = GMI->first; 00639 ValueVector &CV = *GMI->second; 00640 if (!Op->use_empty()) { 00641 // The value is still needed, so recreate it using a series of 00642 // InsertElements. 00643 Type *Ty = Op->getType(); 00644 Value *Res = UndefValue::get(Ty); 00645 BasicBlock *BB = Op->getParent(); 00646 unsigned Count = Ty->getVectorNumElements(); 00647 IRBuilder<> Builder(BB, Op); 00648 if (isa<PHINode>(Op)) 00649 Builder.SetInsertPoint(BB, BB->getFirstInsertionPt()); 00650 for (unsigned I = 0; I < Count; ++I) 00651 Res = Builder.CreateInsertElement(Res, CV[I], Builder.getInt32(I), 00652 Op->getName() + ".upto" + Twine(I)); 00653 Res->takeName(Op); 00654 Op->replaceAllUsesWith(Res); 00655 } 00656 Op->eraseFromParent(); 00657 } 00658 Gathered.clear(); 00659 Scattered.clear(); 00660 return true; 00661 } 00662 00663 FunctionPass *llvm::createScalarizerPass() { 00664 return new Scalarizer(); 00665 }