1 //===--- Scalarizer.cpp - Scalarize vector operations ---------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This pass converts vector operations into scalar operations, in order 11 // to expose optimization opportunities on the individual scalar operations. 12 // It is mainly intended for targets that do not have vector units, but it 13 // may also be useful for revectorizing code to different vector widths. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/IR/IRBuilder.h" 19 #include "llvm/IR/InstVisitor.h" 20 #include "llvm/Pass.h" 21 #include "llvm/Support/CommandLine.h" 22 #include "llvm/Transforms/Scalar.h" 23 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 24 25 using namespace llvm; 26 27 #define DEBUG_TYPE "scalarizer" 28 29 namespace { 30 // Used to store the scattered form of a vector. 31 typedef SmallVector<Value *, 8> ValueVector; 32 33 // Used to map a vector Value to its scattered form. We use std::map 34 // because we want iterators to persist across insertion and because the 35 // values are relatively large. 36 typedef std::map<Value *, ValueVector> ScatterMap; 37 38 // Lists Instructions that have been replaced with scalar implementations, 39 // along with a pointer to their scattered forms. 40 typedef SmallVector<std::pair<Instruction *, ValueVector *>, 16> GatherList; 41 42 // Provides a very limited vector-like interface for lazily accessing one 43 // component of a scattered vector or vector pointer. 44 class Scatterer { 45 public: 46 Scatterer() {} 47 48 // Scatter V into Size components. If new instructions are needed, 49 // insert them before BBI in BB. If Cache is nonnull, use it to cache 50 // the results. 51 Scatterer(BasicBlock *bb, BasicBlock::iterator bbi, Value *v, 52 ValueVector *cachePtr = nullptr); 53 54 // Return component I, creating a new Value for it if necessary. 55 Value *operator[](unsigned I); 56 57 // Return the number of components. 58 unsigned size() const { return Size; } 59 60 private: 61 BasicBlock *BB; 62 BasicBlock::iterator BBI; 63 Value *V; 64 ValueVector *CachePtr; 65 PointerType *PtrTy; 66 ValueVector Tmp; 67 unsigned Size; 68 }; 69 70 // FCmpSpliiter(FCI)(Builder, X, Y, Name) uses Builder to create an FCmp 71 // called Name that compares X and Y in the same way as FCI. 72 struct FCmpSplitter { 73 FCmpSplitter(FCmpInst &fci) : FCI(fci) {} 74 Value *operator()(IRBuilder<> &Builder, Value *Op0, Value *Op1, 75 const Twine &Name) const { 76 return Builder.CreateFCmp(FCI.getPredicate(), Op0, Op1, Name); 77 } 78 FCmpInst &FCI; 79 }; 80 81 // ICmpSpliiter(ICI)(Builder, X, Y, Name) uses Builder to create an ICmp 82 // called Name that compares X and Y in the same way as ICI. 83 struct ICmpSplitter { 84 ICmpSplitter(ICmpInst &ici) : ICI(ici) {} 85 Value *operator()(IRBuilder<> &Builder, Value *Op0, Value *Op1, 86 const Twine &Name) const { 87 return Builder.CreateICmp(ICI.getPredicate(), Op0, Op1, Name); 88 } 89 ICmpInst &ICI; 90 }; 91 92 // BinarySpliiter(BO)(Builder, X, Y, Name) uses Builder to create 93 // a binary operator like BO called Name with operands X and Y. 94 struct BinarySplitter { 95 BinarySplitter(BinaryOperator &bo) : BO(bo) {} 96 Value *operator()(IRBuilder<> &Builder, Value *Op0, Value *Op1, 97 const Twine &Name) const { 98 return Builder.CreateBinOp(BO.getOpcode(), Op0, Op1, Name); 99 } 100 BinaryOperator &BO; 101 }; 102 103 // Information about a load or store that we're scalarizing. 104 struct VectorLayout { 105 VectorLayout() : VecTy(nullptr), ElemTy(nullptr), VecAlign(0), ElemSize(0) {} 106 107 // Return the alignment of element I. 108 uint64_t getElemAlign(unsigned I) { 109 return MinAlign(VecAlign, I * ElemSize); 110 } 111 112 // The type of the vector. 113 VectorType *VecTy; 114 115 // The type of each element. 116 Type *ElemTy; 117 118 // The alignment of the vector. 119 uint64_t VecAlign; 120 121 // The size of each element. 122 uint64_t ElemSize; 123 }; 124 125 class Scalarizer : public FunctionPass, 126 public InstVisitor<Scalarizer, bool> { 127 public: 128 static char ID; 129 130 Scalarizer() : 131 FunctionPass(ID) { 132 initializeScalarizerPass(*PassRegistry::getPassRegistry()); 133 } 134 135 bool doInitialization(Module &M) override; 136 bool runOnFunction(Function &F) override; 137 138 // InstVisitor methods. They return true if the instruction was scalarized, 139 // false if nothing changed. 140 bool visitInstruction(Instruction &) { return false; } 141 bool visitSelectInst(SelectInst &SI); 142 bool visitICmpInst(ICmpInst &); 143 bool visitFCmpInst(FCmpInst &); 144 bool visitBinaryOperator(BinaryOperator &); 145 bool visitGetElementPtrInst(GetElementPtrInst &); 146 bool visitCastInst(CastInst &); 147 bool visitBitCastInst(BitCastInst &); 148 bool visitShuffleVectorInst(ShuffleVectorInst &); 149 bool visitPHINode(PHINode &); 150 bool visitLoadInst(LoadInst &); 151 bool visitStoreInst(StoreInst &); 152 153 static void registerOptions() { 154 // This is disabled by default because having separate loads and stores 155 // makes it more likely that the -combiner-alias-analysis limits will be 156 // reached. 157 OptionRegistry::registerOption<bool, Scalarizer, 158 &Scalarizer::ScalarizeLoadStore>( 159 "scalarize-load-store", 160 "Allow the scalarizer pass to scalarize loads and store", false); 161 } 162 163 private: 164 Scatterer scatter(Instruction *, Value *); 165 void gather(Instruction *, const ValueVector &); 166 bool canTransferMetadata(unsigned Kind); 167 void transferMetadata(Instruction *, const ValueVector &); 168 bool getVectorLayout(Type *, unsigned, VectorLayout &); 169 bool finish(); 170 171 template<typename T> bool splitBinary(Instruction &, const T &); 172 173 ScatterMap Scattered; 174 GatherList Gathered; 175 unsigned ParallelLoopAccessMDKind; 176 const DataLayout *DL; 177 bool ScalarizeLoadStore; 178 }; 179 180 char Scalarizer::ID = 0; 181 } // end anonymous namespace 182 183 INITIALIZE_PASS_WITH_OPTIONS(Scalarizer, "scalarizer", 184 "Scalarize vector operations", false, false) 185 186 Scatterer::Scatterer(BasicBlock *bb, BasicBlock::iterator bbi, Value *v, 187 ValueVector *cachePtr) 188 : BB(bb), BBI(bbi), V(v), CachePtr(cachePtr) { 189 Type *Ty = V->getType(); 190 PtrTy = dyn_cast<PointerType>(Ty); 191 if (PtrTy) 192 Ty = PtrTy->getElementType(); 193 Size = Ty->getVectorNumElements(); 194 if (!CachePtr) 195 Tmp.resize(Size, nullptr); 196 else if (CachePtr->empty()) 197 CachePtr->resize(Size, nullptr); 198 else 199 assert(Size == CachePtr->size() && "Inconsistent vector sizes"); 200 } 201 202 // Return component I, creating a new Value for it if necessary. 203 Value *Scatterer::operator[](unsigned I) { 204 ValueVector &CV = (CachePtr ? *CachePtr : Tmp); 205 // Try to reuse a previous value. 206 if (CV[I]) 207 return CV[I]; 208 IRBuilder<> Builder(BB, BBI); 209 if (PtrTy) { 210 if (!CV[0]) { 211 Type *Ty = 212 PointerType::get(PtrTy->getElementType()->getVectorElementType(), 213 PtrTy->getAddressSpace()); 214 CV[0] = Builder.CreateBitCast(V, Ty, V->getName() + ".i0"); 215 } 216 if (I != 0) 217 CV[I] = Builder.CreateConstGEP1_32(CV[0], I, 218 V->getName() + ".i" + Twine(I)); 219 } else { 220 // Search through a chain of InsertElementInsts looking for element I. 221 // Record other elements in the cache. The new V is still suitable 222 // for all uncached indices. 223 for (;;) { 224 InsertElementInst *Insert = dyn_cast<InsertElementInst>(V); 225 if (!Insert) 226 break; 227 ConstantInt *Idx = dyn_cast<ConstantInt>(Insert->getOperand(2)); 228 if (!Idx) 229 break; 230 unsigned J = Idx->getZExtValue(); 231 CV[J] = Insert->getOperand(1); 232 V = Insert->getOperand(0); 233 if (I == J) 234 return CV[J]; 235 } 236 CV[I] = Builder.CreateExtractElement(V, Builder.getInt32(I), 237 V->getName() + ".i" + Twine(I)); 238 } 239 return CV[I]; 240 } 241 242 bool Scalarizer::doInitialization(Module &M) { 243 ParallelLoopAccessMDKind = 244 M.getContext().getMDKindID("llvm.mem.parallel_loop_access"); 245 ScalarizeLoadStore = 246 M.getContext().getOption<bool, Scalarizer, &Scalarizer::ScalarizeLoadStore>(); 247 return false; 248 } 249 250 bool Scalarizer::runOnFunction(Function &F) { 251 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); 252 DL = DLP ? &DLP->getDataLayout() : nullptr; 253 for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) { 254 BasicBlock *BB = BBI; 255 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) { 256 Instruction *I = II; 257 bool Done = visit(I); 258 ++II; 259 if (Done && I->getType()->isVoidTy()) 260 I->eraseFromParent(); 261 } 262 } 263 return finish(); 264 } 265 266 // Return a scattered form of V that can be accessed by Point. V must be a 267 // vector or a pointer to a vector. 268 Scatterer Scalarizer::scatter(Instruction *Point, Value *V) { 269 if (Argument *VArg = dyn_cast<Argument>(V)) { 270 // Put the scattered form of arguments in the entry block, 271 // so that it can be used everywhere. 272 Function *F = VArg->getParent(); 273 BasicBlock *BB = &F->getEntryBlock(); 274 return Scatterer(BB, BB->begin(), V, &Scattered[V]); 275 } 276 if (Instruction *VOp = dyn_cast<Instruction>(V)) { 277 // Put the scattered form of an instruction directly after the 278 // instruction. 279 BasicBlock *BB = VOp->getParent(); 280 return Scatterer(BB, std::next(BasicBlock::iterator(VOp)), 281 V, &Scattered[V]); 282 } 283 // In the fallback case, just put the scattered before Point and 284 // keep the result local to Point. 285 return Scatterer(Point->getParent(), Point, V); 286 } 287 288 // Replace Op with the gathered form of the components in CV. Defer the 289 // deletion of Op and creation of the gathered form to the end of the pass, 290 // so that we can avoid creating the gathered form if all uses of Op are 291 // replaced with uses of CV. 292 void Scalarizer::gather(Instruction *Op, const ValueVector &CV) { 293 // Since we're not deleting Op yet, stub out its operands, so that it 294 // doesn't make anything live unnecessarily. 295 for (unsigned I = 0, E = Op->getNumOperands(); I != E; ++I) 296 Op->setOperand(I, UndefValue::get(Op->getOperand(I)->getType())); 297 298 transferMetadata(Op, CV); 299 300 // If we already have a scattered form of Op (created from ExtractElements 301 // of Op itself), replace them with the new form. 302 ValueVector &SV = Scattered[Op]; 303 if (!SV.empty()) { 304 for (unsigned I = 0, E = SV.size(); I != E; ++I) { 305 Instruction *Old = cast<Instruction>(SV[I]); 306 CV[I]->takeName(Old); 307 Old->replaceAllUsesWith(CV[I]); 308 Old->eraseFromParent(); 309 } 310 } 311 SV = CV; 312 Gathered.push_back(GatherList::value_type(Op, &SV)); 313 } 314 315 // Return true if it is safe to transfer the given metadata tag from 316 // vector to scalar instructions. 317 bool Scalarizer::canTransferMetadata(unsigned Tag) { 318 return (Tag == LLVMContext::MD_tbaa 319 || Tag == LLVMContext::MD_fpmath 320 || Tag == LLVMContext::MD_tbaa_struct 321 || Tag == LLVMContext::MD_invariant_load 322 || Tag == LLVMContext::MD_alias_scope 323 || Tag == LLVMContext::MD_noalias 324 || Tag == ParallelLoopAccessMDKind); 325 } 326 327 // Transfer metadata from Op to the instructions in CV if it is known 328 // to be safe to do so. 329 void Scalarizer::transferMetadata(Instruction *Op, const ValueVector &CV) { 330 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 331 Op->getAllMetadataOtherThanDebugLoc(MDs); 332 for (unsigned I = 0, E = CV.size(); I != E; ++I) { 333 if (Instruction *New = dyn_cast<Instruction>(CV[I])) { 334 for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator 335 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) 336 if (canTransferMetadata(MI->first)) 337 New->setMetadata(MI->first, MI->second); 338 New->setDebugLoc(Op->getDebugLoc()); 339 } 340 } 341 } 342 343 // Try to fill in Layout from Ty, returning true on success. Alignment is 344 // the alignment of the vector, or 0 if the ABI default should be used. 345 bool Scalarizer::getVectorLayout(Type *Ty, unsigned Alignment, 346 VectorLayout &Layout) { 347 if (!DL) 348 return false; 349 350 // Make sure we're dealing with a vector. 351 Layout.VecTy = dyn_cast<VectorType>(Ty); 352 if (!Layout.VecTy) 353 return false; 354 355 // Check that we're dealing with full-byte elements. 356 Layout.ElemTy = Layout.VecTy->getElementType(); 357 if (DL->getTypeSizeInBits(Layout.ElemTy) != 358 DL->getTypeStoreSizeInBits(Layout.ElemTy)) 359 return false; 360 361 if (Alignment) 362 Layout.VecAlign = Alignment; 363 else 364 Layout.VecAlign = DL->getABITypeAlignment(Layout.VecTy); 365 Layout.ElemSize = DL->getTypeStoreSize(Layout.ElemTy); 366 return true; 367 } 368 369 // Scalarize two-operand instruction I, using Split(Builder, X, Y, Name) 370 // to create an instruction like I with operands X and Y and name Name. 371 template<typename Splitter> 372 bool Scalarizer::splitBinary(Instruction &I, const Splitter &Split) { 373 VectorType *VT = dyn_cast<VectorType>(I.getType()); 374 if (!VT) 375 return false; 376 377 unsigned NumElems = VT->getNumElements(); 378 IRBuilder<> Builder(I.getParent(), &I); 379 Scatterer Op0 = scatter(&I, I.getOperand(0)); 380 Scatterer Op1 = scatter(&I, I.getOperand(1)); 381 assert(Op0.size() == NumElems && "Mismatched binary operation"); 382 assert(Op1.size() == NumElems && "Mismatched binary operation"); 383 ValueVector Res; 384 Res.resize(NumElems); 385 for (unsigned Elem = 0; Elem < NumElems; ++Elem) 386 Res[Elem] = Split(Builder, Op0[Elem], Op1[Elem], 387 I.getName() + ".i" + Twine(Elem)); 388 gather(&I, Res); 389 return true; 390 } 391 392 bool Scalarizer::visitSelectInst(SelectInst &SI) { 393 VectorType *VT = dyn_cast<VectorType>(SI.getType()); 394 if (!VT) 395 return false; 396 397 unsigned NumElems = VT->getNumElements(); 398 IRBuilder<> Builder(SI.getParent(), &SI); 399 Scatterer Op1 = scatter(&SI, SI.getOperand(1)); 400 Scatterer Op2 = scatter(&SI, SI.getOperand(2)); 401 assert(Op1.size() == NumElems && "Mismatched select"); 402 assert(Op2.size() == NumElems && "Mismatched select"); 403 ValueVector Res; 404 Res.resize(NumElems); 405 406 if (SI.getOperand(0)->getType()->isVectorTy()) { 407 Scatterer Op0 = scatter(&SI, SI.getOperand(0)); 408 assert(Op0.size() == NumElems && "Mismatched select"); 409 for (unsigned I = 0; I < NumElems; ++I) 410 Res[I] = Builder.CreateSelect(Op0[I], Op1[I], Op2[I], 411 SI.getName() + ".i" + Twine(I)); 412 } else { 413 Value *Op0 = SI.getOperand(0); 414 for (unsigned I = 0; I < NumElems; ++I) 415 Res[I] = Builder.CreateSelect(Op0, Op1[I], Op2[I], 416 SI.getName() + ".i" + Twine(I)); 417 } 418 gather(&SI, Res); 419 return true; 420 } 421 422 bool Scalarizer::visitICmpInst(ICmpInst &ICI) { 423 return splitBinary(ICI, ICmpSplitter(ICI)); 424 } 425 426 bool Scalarizer::visitFCmpInst(FCmpInst &FCI) { 427 return splitBinary(FCI, FCmpSplitter(FCI)); 428 } 429 430 bool Scalarizer::visitBinaryOperator(BinaryOperator &BO) { 431 return splitBinary(BO, BinarySplitter(BO)); 432 } 433 434 bool Scalarizer::visitGetElementPtrInst(GetElementPtrInst &GEPI) { 435 VectorType *VT = dyn_cast<VectorType>(GEPI.getType()); 436 if (!VT) 437 return false; 438 439 IRBuilder<> Builder(GEPI.getParent(), &GEPI); 440 unsigned NumElems = VT->getNumElements(); 441 unsigned NumIndices = GEPI.getNumIndices(); 442 443 Scatterer Base = scatter(&GEPI, GEPI.getOperand(0)); 444 445 SmallVector<Scatterer, 8> Ops; 446 Ops.resize(NumIndices); 447 for (unsigned I = 0; I < NumIndices; ++I) 448 Ops[I] = scatter(&GEPI, GEPI.getOperand(I + 1)); 449 450 ValueVector Res; 451 Res.resize(NumElems); 452 for (unsigned I = 0; I < NumElems; ++I) { 453 SmallVector<Value *, 8> Indices; 454 Indices.resize(NumIndices); 455 for (unsigned J = 0; J < NumIndices; ++J) 456 Indices[J] = Ops[J][I]; 457 Res[I] = Builder.CreateGEP(Base[I], Indices, 458 GEPI.getName() + ".i" + Twine(I)); 459 if (GEPI.isInBounds()) 460 if (GetElementPtrInst *NewGEPI = dyn_cast<GetElementPtrInst>(Res[I])) 461 NewGEPI->setIsInBounds(); 462 } 463 gather(&GEPI, Res); 464 return true; 465 } 466 467 bool Scalarizer::visitCastInst(CastInst &CI) { 468 VectorType *VT = dyn_cast<VectorType>(CI.getDestTy()); 469 if (!VT) 470 return false; 471 472 unsigned NumElems = VT->getNumElements(); 473 IRBuilder<> Builder(CI.getParent(), &CI); 474 Scatterer Op0 = scatter(&CI, CI.getOperand(0)); 475 assert(Op0.size() == NumElems && "Mismatched cast"); 476 ValueVector Res; 477 Res.resize(NumElems); 478 for (unsigned I = 0; I < NumElems; ++I) 479 Res[I] = Builder.CreateCast(CI.getOpcode(), Op0[I], VT->getElementType(), 480 CI.getName() + ".i" + Twine(I)); 481 gather(&CI, Res); 482 return true; 483 } 484 485 bool Scalarizer::visitBitCastInst(BitCastInst &BCI) { 486 VectorType *DstVT = dyn_cast<VectorType>(BCI.getDestTy()); 487 VectorType *SrcVT = dyn_cast<VectorType>(BCI.getSrcTy()); 488 if (!DstVT || !SrcVT) 489 return false; 490 491 unsigned DstNumElems = DstVT->getNumElements(); 492 unsigned SrcNumElems = SrcVT->getNumElements(); 493 IRBuilder<> Builder(BCI.getParent(), &BCI); 494 Scatterer Op0 = scatter(&BCI, BCI.getOperand(0)); 495 ValueVector Res; 496 Res.resize(DstNumElems); 497 498 if (DstNumElems == SrcNumElems) { 499 for (unsigned I = 0; I < DstNumElems; ++I) 500 Res[I] = Builder.CreateBitCast(Op0[I], DstVT->getElementType(), 501 BCI.getName() + ".i" + Twine(I)); 502 } else if (DstNumElems > SrcNumElems) { 503 // <M x t1> -> <N*M x t2>. Convert each t1 to <N x t2> and copy the 504 // individual elements to the destination. 505 unsigned FanOut = DstNumElems / SrcNumElems; 506 Type *MidTy = VectorType::get(DstVT->getElementType(), FanOut); 507 unsigned ResI = 0; 508 for (unsigned Op0I = 0; Op0I < SrcNumElems; ++Op0I) { 509 Value *V = Op0[Op0I]; 510 Instruction *VI; 511 // Look through any existing bitcasts before converting to <N x t2>. 512 // In the best case, the resulting conversion might be a no-op. 513 while ((VI = dyn_cast<Instruction>(V)) && 514 VI->getOpcode() == Instruction::BitCast) 515 V = VI->getOperand(0); 516 V = Builder.CreateBitCast(V, MidTy, V->getName() + ".cast"); 517 Scatterer Mid = scatter(&BCI, V); 518 for (unsigned MidI = 0; MidI < FanOut; ++MidI) 519 Res[ResI++] = Mid[MidI]; 520 } 521 } else { 522 // <N*M x t1> -> <M x t2>. Convert each group of <N x t1> into a t2. 523 unsigned FanIn = SrcNumElems / DstNumElems; 524 Type *MidTy = VectorType::get(SrcVT->getElementType(), FanIn); 525 unsigned Op0I = 0; 526 for (unsigned ResI = 0; ResI < DstNumElems; ++ResI) { 527 Value *V = UndefValue::get(MidTy); 528 for (unsigned MidI = 0; MidI < FanIn; ++MidI) 529 V = Builder.CreateInsertElement(V, Op0[Op0I++], Builder.getInt32(MidI), 530 BCI.getName() + ".i" + Twine(ResI) 531 + ".upto" + Twine(MidI)); 532 Res[ResI] = Builder.CreateBitCast(V, DstVT->getElementType(), 533 BCI.getName() + ".i" + Twine(ResI)); 534 } 535 } 536 gather(&BCI, Res); 537 return true; 538 } 539 540 bool Scalarizer::visitShuffleVectorInst(ShuffleVectorInst &SVI) { 541 VectorType *VT = dyn_cast<VectorType>(SVI.getType()); 542 if (!VT) 543 return false; 544 545 unsigned NumElems = VT->getNumElements(); 546 Scatterer Op0 = scatter(&SVI, SVI.getOperand(0)); 547 Scatterer Op1 = scatter(&SVI, SVI.getOperand(1)); 548 ValueVector Res; 549 Res.resize(NumElems); 550 551 for (unsigned I = 0; I < NumElems; ++I) { 552 int Selector = SVI.getMaskValue(I); 553 if (Selector < 0) 554 Res[I] = UndefValue::get(VT->getElementType()); 555 else if (unsigned(Selector) < Op0.size()) 556 Res[I] = Op0[Selector]; 557 else 558 Res[I] = Op1[Selector - Op0.size()]; 559 } 560 gather(&SVI, Res); 561 return true; 562 } 563 564 bool Scalarizer::visitPHINode(PHINode &PHI) { 565 VectorType *VT = dyn_cast<VectorType>(PHI.getType()); 566 if (!VT) 567 return false; 568 569 unsigned NumElems = VT->getNumElements(); 570 IRBuilder<> Builder(PHI.getParent(), &PHI); 571 ValueVector Res; 572 Res.resize(NumElems); 573 574 unsigned NumOps = PHI.getNumOperands(); 575 for (unsigned I = 0; I < NumElems; ++I) 576 Res[I] = Builder.CreatePHI(VT->getElementType(), NumOps, 577 PHI.getName() + ".i" + Twine(I)); 578 579 for (unsigned I = 0; I < NumOps; ++I) { 580 Scatterer Op = scatter(&PHI, PHI.getIncomingValue(I)); 581 BasicBlock *IncomingBlock = PHI.getIncomingBlock(I); 582 for (unsigned J = 0; J < NumElems; ++J) 583 cast<PHINode>(Res[J])->addIncoming(Op[J], IncomingBlock); 584 } 585 gather(&PHI, Res); 586 return true; 587 } 588 589 bool Scalarizer::visitLoadInst(LoadInst &LI) { 590 if (!ScalarizeLoadStore) 591 return false; 592 if (!LI.isSimple()) 593 return false; 594 595 VectorLayout Layout; 596 if (!getVectorLayout(LI.getType(), LI.getAlignment(), Layout)) 597 return false; 598 599 unsigned NumElems = Layout.VecTy->getNumElements(); 600 IRBuilder<> Builder(LI.getParent(), &LI); 601 Scatterer Ptr = scatter(&LI, LI.getPointerOperand()); 602 ValueVector Res; 603 Res.resize(NumElems); 604 605 for (unsigned I = 0; I < NumElems; ++I) 606 Res[I] = Builder.CreateAlignedLoad(Ptr[I], Layout.getElemAlign(I), 607 LI.getName() + ".i" + Twine(I)); 608 gather(&LI, Res); 609 return true; 610 } 611 612 bool Scalarizer::visitStoreInst(StoreInst &SI) { 613 if (!ScalarizeLoadStore) 614 return false; 615 if (!SI.isSimple()) 616 return false; 617 618 VectorLayout Layout; 619 Value *FullValue = SI.getValueOperand(); 620 if (!getVectorLayout(FullValue->getType(), SI.getAlignment(), Layout)) 621 return false; 622 623 unsigned NumElems = Layout.VecTy->getNumElements(); 624 IRBuilder<> Builder(SI.getParent(), &SI); 625 Scatterer Ptr = scatter(&SI, SI.getPointerOperand()); 626 Scatterer Val = scatter(&SI, FullValue); 627 628 ValueVector Stores; 629 Stores.resize(NumElems); 630 for (unsigned I = 0; I < NumElems; ++I) { 631 unsigned Align = Layout.getElemAlign(I); 632 Stores[I] = Builder.CreateAlignedStore(Val[I], Ptr[I], Align); 633 } 634 transferMetadata(&SI, Stores); 635 return true; 636 } 637 638 // Delete the instructions that we scalarized. If a full vector result 639 // is still needed, recreate it using InsertElements. 640 bool Scalarizer::finish() { 641 if (Gathered.empty()) 642 return false; 643 for (GatherList::iterator GMI = Gathered.begin(), GME = Gathered.end(); 644 GMI != GME; ++GMI) { 645 Instruction *Op = GMI->first; 646 ValueVector &CV = *GMI->second; 647 if (!Op->use_empty()) { 648 // The value is still needed, so recreate it using a series of 649 // InsertElements. 650 Type *Ty = Op->getType(); 651 Value *Res = UndefValue::get(Ty); 652 BasicBlock *BB = Op->getParent(); 653 unsigned Count = Ty->getVectorNumElements(); 654 IRBuilder<> Builder(BB, Op); 655 if (isa<PHINode>(Op)) 656 Builder.SetInsertPoint(BB, BB->getFirstInsertionPt()); 657 for (unsigned I = 0; I < Count; ++I) 658 Res = Builder.CreateInsertElement(Res, CV[I], Builder.getInt32(I), 659 Op->getName() + ".upto" + Twine(I)); 660 Res->takeName(Op); 661 Op->replaceAllUsesWith(Res); 662 } 663 Op->eraseFromParent(); 664 } 665 Gathered.clear(); 666 Scattered.clear(); 667 return true; 668 } 669 670 FunctionPass *llvm::createScalarizerPass() { 671 return new Scalarizer(); 672 } 673