1 //===----------- PPCVSXSwapRemoval.cpp - Remove VSX LE Swaps -------------===// 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 analyzes vector computations and removes unnecessary 11 // doubleword swaps (xxswapd instructions). This pass is performed 12 // only for little-endian VSX code generation. 13 // 14 // For this specific case, loads and stores of v4i32, v4f32, v2i64, 15 // and v2f64 vectors are inefficient. These are implemented using 16 // the lxvd2x and stxvd2x instructions, which invert the order of 17 // doublewords in a vector register. Thus code generation inserts 18 // an xxswapd after each such load, and prior to each such store. 19 // 20 // The extra xxswapd instructions reduce performance. The purpose 21 // of this pass is to reduce the number of xxswapd instructions 22 // required for correctness. 23 // 24 // The primary insight is that much code that operates on vectors 25 // does not care about the relative order of elements in a register, 26 // so long as the correct memory order is preserved. If we have a 27 // computation where all input values are provided by lxvd2x/xxswapd, 28 // all outputs are stored using xxswapd/lxvd2x, and all intermediate 29 // computations are lane-insensitive (independent of element order), 30 // then all the xxswapd instructions associated with the loads and 31 // stores may be removed without changing observable semantics. 32 // 33 // This pass uses standard equivalence class infrastructure to create 34 // maximal webs of computations fitting the above description. Each 35 // such web is then optimized by removing its unnecessary xxswapd 36 // instructions. 37 // 38 // There are some lane-sensitive operations for which we can still 39 // permit the optimization, provided we modify those operations 40 // accordingly. Such operations are identified as using "special 41 // handling" within this module. 42 // 43 //===---------------------------------------------------------------------===// 44 45 #include "PPCInstrInfo.h" 46 #include "PPC.h" 47 #include "PPCInstrBuilder.h" 48 #include "PPCTargetMachine.h" 49 #include "llvm/ADT/DenseMap.h" 50 #include "llvm/ADT/EquivalenceClasses.h" 51 #include "llvm/CodeGen/MachineFunctionPass.h" 52 #include "llvm/CodeGen/MachineInstrBuilder.h" 53 #include "llvm/CodeGen/MachineRegisterInfo.h" 54 #include "llvm/Support/Debug.h" 55 #include "llvm/Support/Format.h" 56 #include "llvm/Support/raw_ostream.h" 57 58 using namespace llvm; 59 60 #define DEBUG_TYPE "ppc-vsx-swaps" 61 62 namespace llvm { 63 void initializePPCVSXSwapRemovalPass(PassRegistry&); 64 } 65 66 namespace { 67 68 // A PPCVSXSwapEntry is created for each machine instruction that 69 // is relevant to a vector computation. 70 struct PPCVSXSwapEntry { 71 // Pointer to the instruction. 72 MachineInstr *VSEMI; 73 74 // Unique ID (position in the swap vector). 75 int VSEId; 76 77 // Attributes of this node. 78 unsigned int IsLoad : 1; 79 unsigned int IsStore : 1; 80 unsigned int IsSwap : 1; 81 unsigned int MentionsPhysVR : 1; 82 unsigned int HasImplicitSubreg : 1; 83 unsigned int IsSwappable : 1; 84 unsigned int SpecialHandling : 3; 85 unsigned int WebRejected : 1; 86 unsigned int WillRemove : 1; 87 }; 88 89 enum SHValues { 90 SH_NONE = 0, 91 SH_BUILDVEC, 92 SH_EXTRACT, 93 SH_INSERT, 94 SH_NOSWAP_LD, 95 SH_NOSWAP_ST, 96 SH_SPLAT 97 }; 98 99 struct PPCVSXSwapRemoval : public MachineFunctionPass { 100 101 static char ID; 102 const PPCInstrInfo *TII; 103 MachineFunction *MF; 104 MachineRegisterInfo *MRI; 105 106 // Swap entries are allocated in a vector for better performance. 107 std::vector<PPCVSXSwapEntry> SwapVector; 108 109 // A mapping is maintained between machine instructions and 110 // their swap entries. The key is the address of the MI. 111 DenseMap<MachineInstr*, int> SwapMap; 112 113 // Equivalence classes are used to gather webs of related computation. 114 // Swap entries are represented by their VSEId fields. 115 EquivalenceClasses<int> *EC; 116 117 PPCVSXSwapRemoval() : MachineFunctionPass(ID) { 118 initializePPCVSXSwapRemovalPass(*PassRegistry::getPassRegistry()); 119 } 120 121 private: 122 // Initialize data structures. 123 void initialize(MachineFunction &MFParm); 124 125 // Walk the machine instructions to gather vector usage information. 126 // Return true iff vector mentions are present. 127 bool gatherVectorInstructions(); 128 129 // Add an entry to the swap vector and swap map. 130 int addSwapEntry(MachineInstr *MI, PPCVSXSwapEntry &SwapEntry); 131 132 // Hunt backwards through COPY and SUBREG_TO_REG chains for a 133 // source register. VecIdx indicates the swap vector entry to 134 // mark as mentioning a physical register if the search leads 135 // to one. 136 unsigned lookThruCopyLike(unsigned SrcReg, unsigned VecIdx); 137 138 // Generate equivalence classes for related computations (webs). 139 void formWebs(); 140 141 // Analyze webs and determine those that cannot be optimized. 142 void recordUnoptimizableWebs(); 143 144 // Record which swap instructions can be safely removed. 145 void markSwapsForRemoval(); 146 147 // Remove swaps and update other instructions requiring special 148 // handling. Return true iff any changes are made. 149 bool removeSwaps(); 150 151 // Update instructions requiring special handling. 152 void handleSpecialSwappables(int EntryIdx); 153 154 // Dump a description of the entries in the swap vector. 155 void dumpSwapVector(); 156 157 // Return true iff the given register is in the given class. 158 bool isRegInClass(unsigned Reg, const TargetRegisterClass *RC) { 159 if (TargetRegisterInfo::isVirtualRegister(Reg)) 160 return RC->hasSubClassEq(MRI->getRegClass(Reg)); 161 if (RC->contains(Reg)) 162 return true; 163 return false; 164 } 165 166 // Return true iff the given register is a full vector register. 167 bool isVecReg(unsigned Reg) { 168 return (isRegInClass(Reg, &PPC::VSRCRegClass) || 169 isRegInClass(Reg, &PPC::VRRCRegClass)); 170 } 171 172 public: 173 // Main entry point for this pass. 174 bool runOnMachineFunction(MachineFunction &MF) override { 175 // If we don't have VSX on the subtarget, don't do anything. 176 const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>(); 177 if (!STI.hasVSX()) 178 return false; 179 180 bool Changed = false; 181 initialize(MF); 182 183 if (gatherVectorInstructions()) { 184 formWebs(); 185 recordUnoptimizableWebs(); 186 markSwapsForRemoval(); 187 Changed = removeSwaps(); 188 } 189 190 // FIXME: See the allocation of EC in initialize(). 191 delete EC; 192 return Changed; 193 } 194 }; 195 196 // Initialize data structures for this pass. In particular, clear the 197 // swap vector and allocate the equivalence class mapping before 198 // processing each function. 199 void PPCVSXSwapRemoval::initialize(MachineFunction &MFParm) { 200 MF = &MFParm; 201 MRI = &MF->getRegInfo(); 202 TII = static_cast<const PPCInstrInfo*>(MF->getSubtarget().getInstrInfo()); 203 204 // An initial vector size of 256 appears to work well in practice. 205 // Small/medium functions with vector content tend not to incur a 206 // reallocation at this size. Three of the vector tests in 207 // projects/test-suite reallocate, which seems like a reasonable rate. 208 const int InitialVectorSize(256); 209 SwapVector.clear(); 210 SwapVector.reserve(InitialVectorSize); 211 212 // FIXME: Currently we allocate EC each time because we don't have 213 // access to the set representation on which to call clear(). Should 214 // consider adding a clear() method to the EquivalenceClasses class. 215 EC = new EquivalenceClasses<int>; 216 } 217 218 // Create an entry in the swap vector for each instruction that mentions 219 // a full vector register, recording various characteristics of the 220 // instructions there. 221 bool PPCVSXSwapRemoval::gatherVectorInstructions() { 222 bool RelevantFunction = false; 223 224 for (MachineBasicBlock &MBB : *MF) { 225 for (MachineInstr &MI : MBB) { 226 227 bool RelevantInstr = false; 228 bool ImplicitSubreg = false; 229 230 for (const MachineOperand &MO : MI.operands()) { 231 if (!MO.isReg()) 232 continue; 233 unsigned Reg = MO.getReg(); 234 if (isVecReg(Reg)) { 235 RelevantInstr = true; 236 if (MO.getSubReg() != 0) 237 ImplicitSubreg = true; 238 break; 239 } 240 } 241 242 if (!RelevantInstr) 243 continue; 244 245 RelevantFunction = true; 246 247 // Create a SwapEntry initialized to zeros, then fill in the 248 // instruction and ID fields before pushing it to the back 249 // of the swap vector. 250 PPCVSXSwapEntry SwapEntry{}; 251 int VecIdx = addSwapEntry(&MI, SwapEntry); 252 253 if (ImplicitSubreg) 254 SwapVector[VecIdx].HasImplicitSubreg = 1; 255 256 switch(MI.getOpcode()) { 257 default: 258 // Unless noted otherwise, an instruction is considered 259 // safe for the optimization. There are a large number of 260 // such true-SIMD instructions (all vector math, logical, 261 // select, compare, etc.). 262 SwapVector[VecIdx].IsSwappable = 1; 263 break; 264 case PPC::XXPERMDI: 265 // This is a swap if it is of the form XXPERMDI t, s, s, 2. 266 // Unfortunately, MachineCSE ignores COPY and SUBREG_TO_REG, so we 267 // can also see XXPERMDI t, SUBREG_TO_REG(s), SUBREG_TO_REG(s), 2, 268 // for example. We have to look through chains of COPY and 269 // SUBREG_TO_REG to find the real source value for comparison. 270 // If the real source value is a physical register, then mark the 271 // XXPERMDI as mentioning a physical register. 272 // Any other form of XXPERMDI is lane-sensitive and unsafe 273 // for the optimization. 274 if (MI.getOperand(3).getImm() == 2) { 275 unsigned trueReg1 = lookThruCopyLike(MI.getOperand(1).getReg(), 276 VecIdx); 277 unsigned trueReg2 = lookThruCopyLike(MI.getOperand(2).getReg(), 278 VecIdx); 279 if (trueReg1 == trueReg2) 280 SwapVector[VecIdx].IsSwap = 1; 281 } 282 break; 283 case PPC::LVX: 284 // Non-permuting loads are currently unsafe. We can use special 285 // handling for this in the future. By not marking these as 286 // IsSwap, we ensure computations containing them will be rejected 287 // for now. 288 SwapVector[VecIdx].IsLoad = 1; 289 break; 290 case PPC::LXVD2X: 291 case PPC::LXVW4X: 292 // Permuting loads are marked as both load and swap, and are 293 // safe for optimization. 294 SwapVector[VecIdx].IsLoad = 1; 295 SwapVector[VecIdx].IsSwap = 1; 296 break; 297 case PPC::STVX: 298 // Non-permuting stores are currently unsafe. We can use special 299 // handling for this in the future. By not marking these as 300 // IsSwap, we ensure computations containing them will be rejected 301 // for now. 302 SwapVector[VecIdx].IsStore = 1; 303 break; 304 case PPC::STXVD2X: 305 case PPC::STXVW4X: 306 // Permuting stores are marked as both store and swap, and are 307 // safe for optimization. 308 SwapVector[VecIdx].IsStore = 1; 309 SwapVector[VecIdx].IsSwap = 1; 310 break; 311 case PPC::SUBREG_TO_REG: 312 // These are fine provided they are moving between full vector 313 // register classes. For example, the VRs are a subset of the 314 // VSRs, but each VR and each VSR is a full 128-bit register. 315 if (isVecReg(MI.getOperand(0).getReg()) && 316 isVecReg(MI.getOperand(2).getReg())) 317 SwapVector[VecIdx].IsSwappable = 1; 318 break; 319 case PPC::COPY: 320 // These are fine provided they are moving between full vector 321 // register classes. 322 if (isVecReg(MI.getOperand(0).getReg()) && 323 isVecReg(MI.getOperand(1).getReg())) 324 SwapVector[VecIdx].IsSwappable = 1; 325 break; 326 case PPC::VSPLTB: 327 case PPC::VSPLTH: 328 case PPC::VSPLTW: 329 // Splats are lane-sensitive, but we can use special handling 330 // to adjust the source lane for the splat. This is not yet 331 // implemented. When it is, we need to uncomment the following: 332 // SwapVector[VecIdx].IsSwappable = 1; 333 SwapVector[VecIdx].SpecialHandling = SHValues::SH_SPLAT; 334 break; 335 // The presence of the following lane-sensitive operations in a 336 // web will kill the optimization, at least for now. For these 337 // we do nothing, causing the optimization to fail. 338 // FIXME: Some of these could be permitted with special handling, 339 // and will be phased in as time permits. 340 // FIXME: There is no simple and maintainable way to express a set 341 // of opcodes having a common attribute in TableGen. Should this 342 // change, this is a prime candidate to use such a mechanism. 343 case PPC::INLINEASM: 344 case PPC::EXTRACT_SUBREG: 345 case PPC::INSERT_SUBREG: 346 case PPC::COPY_TO_REGCLASS: 347 case PPC::LVEBX: 348 case PPC::LVEHX: 349 case PPC::LVEWX: 350 case PPC::LVSL: 351 case PPC::LVSR: 352 case PPC::LVXL: 353 case PPC::LXVDSX: 354 case PPC::STVEBX: 355 case PPC::STVEHX: 356 case PPC::STVEWX: 357 case PPC::STVXL: 358 case PPC::STXSDX: 359 case PPC::VCIPHER: 360 case PPC::VCIPHERLAST: 361 case PPC::VMRGHB: 362 case PPC::VMRGHH: 363 case PPC::VMRGHW: 364 case PPC::VMRGLB: 365 case PPC::VMRGLH: 366 case PPC::VMRGLW: 367 case PPC::VMULESB: 368 case PPC::VMULESH: 369 case PPC::VMULESW: 370 case PPC::VMULEUB: 371 case PPC::VMULEUH: 372 case PPC::VMULEUW: 373 case PPC::VMULOSB: 374 case PPC::VMULOSH: 375 case PPC::VMULOSW: 376 case PPC::VMULOUB: 377 case PPC::VMULOUH: 378 case PPC::VMULOUW: 379 case PPC::VNCIPHER: 380 case PPC::VNCIPHERLAST: 381 case PPC::VPERM: 382 case PPC::VPERMXOR: 383 case PPC::VPKPX: 384 case PPC::VPKSHSS: 385 case PPC::VPKSHUS: 386 case PPC::VPKSWSS: 387 case PPC::VPKSWUS: 388 case PPC::VPKUHUM: 389 case PPC::VPKUHUS: 390 case PPC::VPKUWUM: 391 case PPC::VPKUWUS: 392 case PPC::VPMSUMB: 393 case PPC::VPMSUMD: 394 case PPC::VPMSUMH: 395 case PPC::VPMSUMW: 396 case PPC::VRLB: 397 case PPC::VRLD: 398 case PPC::VRLH: 399 case PPC::VRLW: 400 case PPC::VSBOX: 401 case PPC::VSHASIGMAD: 402 case PPC::VSHASIGMAW: 403 case PPC::VSL: 404 case PPC::VSLDOI: 405 case PPC::VSLO: 406 case PPC::VSR: 407 case PPC::VSRO: 408 case PPC::VSUM2SWS: 409 case PPC::VSUM4SBS: 410 case PPC::VSUM4SHS: 411 case PPC::VSUM4UBS: 412 case PPC::VSUMSWS: 413 case PPC::VUPKHPX: 414 case PPC::VUPKHSB: 415 case PPC::VUPKHSH: 416 case PPC::VUPKLPX: 417 case PPC::VUPKLSB: 418 case PPC::VUPKLSH: 419 case PPC::XXMRGHW: 420 case PPC::XXMRGLW: 421 case PPC::XXSPLTW: 422 break; 423 } 424 } 425 } 426 427 if (RelevantFunction) { 428 DEBUG(dbgs() << "Swap vector when first built\n\n"); 429 dumpSwapVector(); 430 } 431 432 return RelevantFunction; 433 } 434 435 // Add an entry to the swap vector and swap map, and make a 436 // singleton equivalence class for the entry. 437 int PPCVSXSwapRemoval::addSwapEntry(MachineInstr *MI, 438 PPCVSXSwapEntry& SwapEntry) { 439 SwapEntry.VSEMI = MI; 440 SwapEntry.VSEId = SwapVector.size(); 441 SwapVector.push_back(SwapEntry); 442 EC->insert(SwapEntry.VSEId); 443 SwapMap[MI] = SwapEntry.VSEId; 444 return SwapEntry.VSEId; 445 } 446 447 // This is used to find the "true" source register for an 448 // XXPERMDI instruction, since MachineCSE does not handle the 449 // "copy-like" operations (Copy and SubregToReg). Returns 450 // the original SrcReg unless it is the target of a copy-like 451 // operation, in which case we chain backwards through all 452 // such operations to the ultimate source register. If a 453 // physical register is encountered, we stop the search and 454 // flag the swap entry indicated by VecIdx (the original 455 // XXPERMDI) as mentioning a physical register. Similarly 456 // for implicit subregister mentions (which should never 457 // happen). 458 unsigned PPCVSXSwapRemoval::lookThruCopyLike(unsigned SrcReg, 459 unsigned VecIdx) { 460 MachineInstr *MI = MRI->getVRegDef(SrcReg); 461 if (!MI->isCopyLike()) 462 return SrcReg; 463 464 unsigned CopySrcReg, CopySrcSubreg; 465 if (MI->isCopy()) { 466 CopySrcReg = MI->getOperand(1).getReg(); 467 CopySrcSubreg = MI->getOperand(1).getSubReg(); 468 } else { 469 assert(MI->isSubregToReg() && "bad opcode for lookThruCopyLike"); 470 CopySrcReg = MI->getOperand(2).getReg(); 471 CopySrcSubreg = MI->getOperand(2).getSubReg(); 472 } 473 474 if (!TargetRegisterInfo::isVirtualRegister(CopySrcReg)) { 475 SwapVector[VecIdx].MentionsPhysVR = 1; 476 return CopySrcReg; 477 } 478 479 if (CopySrcSubreg != 0) { 480 SwapVector[VecIdx].HasImplicitSubreg = 1; 481 return CopySrcReg; 482 } 483 484 return lookThruCopyLike(CopySrcReg, VecIdx); 485 } 486 487 // Generate equivalence classes for related computations (webs) by 488 // def-use relationships of virtual registers. Mention of a physical 489 // register terminates the generation of equivalence classes as this 490 // indicates a use of a parameter, definition of a return value, use 491 // of a value returned from a call, or definition of a parameter to a 492 // call. Computations with physical register mentions are flagged 493 // as such so their containing webs will not be optimized. 494 void PPCVSXSwapRemoval::formWebs() { 495 496 DEBUG(dbgs() << "\n*** Forming webs for swap removal ***\n\n"); 497 498 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) { 499 500 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 501 502 DEBUG(dbgs() << "\n" << SwapVector[EntryIdx].VSEId << " "); 503 DEBUG(MI->dump()); 504 505 // It's sufficient to walk vector uses and join them to their unique 506 // definitions. In addition, check *all* vector register operands 507 // for physical regs. 508 for (const MachineOperand &MO : MI->operands()) { 509 if (!MO.isReg()) 510 continue; 511 512 unsigned Reg = MO.getReg(); 513 if (!isVecReg(Reg)) 514 continue; 515 516 if (!TargetRegisterInfo::isVirtualRegister(Reg)) { 517 SwapVector[EntryIdx].MentionsPhysVR = 1; 518 continue; 519 } 520 521 if (!MO.isUse()) 522 continue; 523 524 MachineInstr* DefMI = MRI->getVRegDef(Reg); 525 assert(SwapMap.find(DefMI) != SwapMap.end() && 526 "Inconsistency: def of vector reg not found in swap map!"); 527 int DefIdx = SwapMap[DefMI]; 528 (void)EC->unionSets(SwapVector[DefIdx].VSEId, 529 SwapVector[EntryIdx].VSEId); 530 531 DEBUG(dbgs() << format("Unioning %d with %d\n", SwapVector[DefIdx].VSEId, 532 SwapVector[EntryIdx].VSEId)); 533 DEBUG(dbgs() << " Def: "); 534 DEBUG(DefMI->dump()); 535 } 536 } 537 } 538 539 // Walk the swap vector entries looking for conditions that prevent their 540 // containing computations from being optimized. When such conditions are 541 // found, mark the representative of the computation's equivalence class 542 // as rejected. 543 void PPCVSXSwapRemoval::recordUnoptimizableWebs() { 544 545 DEBUG(dbgs() << "\n*** Rejecting webs for swap removal ***\n\n"); 546 547 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) { 548 int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId); 549 550 // Reject webs containing mentions of physical registers or implicit 551 // subregs, or containing operations that we don't know how to handle 552 // in a lane-permuted region. 553 if (SwapVector[EntryIdx].MentionsPhysVR || 554 SwapVector[EntryIdx].HasImplicitSubreg || 555 !(SwapVector[EntryIdx].IsSwappable || SwapVector[EntryIdx].IsSwap)) { 556 557 SwapVector[Repr].WebRejected = 1; 558 559 DEBUG(dbgs() << 560 format("Web %d rejected for physreg, subreg, or not swap[pable]\n", 561 Repr)); 562 DEBUG(dbgs() << " in " << EntryIdx << ": "); 563 DEBUG(SwapVector[EntryIdx].VSEMI->dump()); 564 DEBUG(dbgs() << "\n"); 565 } 566 567 // Reject webs than contain swapping loads that feed something other 568 // than a swap instruction. 569 else if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) { 570 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 571 unsigned DefReg = MI->getOperand(0).getReg(); 572 573 // We skip debug instructions in the analysis. (Note that debug 574 // location information is still maintained by this optimization 575 // because it remains on the LXVD2X and STXVD2X instructions after 576 // the XXPERMDIs are removed.) 577 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) { 578 int UseIdx = SwapMap[&UseMI]; 579 580 if (!SwapVector[UseIdx].IsSwap || SwapVector[UseIdx].IsLoad || 581 SwapVector[UseIdx].IsStore) { 582 583 SwapVector[Repr].WebRejected = 1; 584 585 DEBUG(dbgs() << 586 format("Web %d rejected for load not feeding swap\n", Repr)); 587 DEBUG(dbgs() << " def " << EntryIdx << ": "); 588 DEBUG(MI->dump()); 589 DEBUG(dbgs() << " use " << UseIdx << ": "); 590 DEBUG(UseMI.dump()); 591 DEBUG(dbgs() << "\n"); 592 } 593 } 594 595 // Reject webs than contain swapping stores that are fed by something 596 // other than a swap instruction. 597 } else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) { 598 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 599 unsigned UseReg = MI->getOperand(0).getReg(); 600 MachineInstr *DefMI = MRI->getVRegDef(UseReg); 601 int DefIdx = SwapMap[DefMI]; 602 603 if (!SwapVector[DefIdx].IsSwap || SwapVector[DefIdx].IsLoad || 604 SwapVector[DefIdx].IsStore) { 605 606 SwapVector[Repr].WebRejected = 1; 607 608 DEBUG(dbgs() << 609 format("Web %d rejected for store not fed by swap\n", Repr)); 610 DEBUG(dbgs() << " def " << DefIdx << ": "); 611 DEBUG(DefMI->dump()); 612 DEBUG(dbgs() << " use " << EntryIdx << ": "); 613 DEBUG(MI->dump()); 614 DEBUG(dbgs() << "\n"); 615 } 616 } 617 } 618 619 DEBUG(dbgs() << "Swap vector after web analysis:\n\n"); 620 dumpSwapVector(); 621 } 622 623 // Walk the swap vector entries looking for swaps fed by permuting loads 624 // and swaps that feed permuting stores. If the containing computation 625 // has not been marked rejected, mark each such swap for removal. 626 // (Removal is delayed in case optimization has disturbed the pattern, 627 // such that multiple loads feed the same swap, etc.) 628 void PPCVSXSwapRemoval::markSwapsForRemoval() { 629 630 DEBUG(dbgs() << "\n*** Marking swaps for removal ***\n\n"); 631 632 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) { 633 634 if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) { 635 int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId); 636 637 if (!SwapVector[Repr].WebRejected) { 638 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 639 unsigned DefReg = MI->getOperand(0).getReg(); 640 641 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) { 642 int UseIdx = SwapMap[&UseMI]; 643 SwapVector[UseIdx].WillRemove = 1; 644 645 DEBUG(dbgs() << "Marking swap fed by load for removal: "); 646 DEBUG(UseMI.dump()); 647 } 648 } 649 650 } else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) { 651 int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId); 652 653 if (!SwapVector[Repr].WebRejected) { 654 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 655 unsigned UseReg = MI->getOperand(0).getReg(); 656 MachineInstr *DefMI = MRI->getVRegDef(UseReg); 657 int DefIdx = SwapMap[DefMI]; 658 SwapVector[DefIdx].WillRemove = 1; 659 660 DEBUG(dbgs() << "Marking swap feeding store for removal: "); 661 DEBUG(DefMI->dump()); 662 } 663 664 } else if (SwapVector[EntryIdx].IsSwappable && 665 SwapVector[EntryIdx].SpecialHandling != 0) 666 handleSpecialSwappables(EntryIdx); 667 } 668 } 669 670 // The identified swap entry requires special handling to allow its 671 // containing computation to be optimized. Perform that handling 672 // here. 673 // FIXME: This code is to be phased in with subsequent patches. 674 void PPCVSXSwapRemoval::handleSpecialSwappables(int EntryIdx) { 675 } 676 677 // Walk the swap vector and replace each entry marked for removal with 678 // a copy operation. 679 bool PPCVSXSwapRemoval::removeSwaps() { 680 681 DEBUG(dbgs() << "\n*** Removing swaps ***\n\n"); 682 683 bool Changed = false; 684 685 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) { 686 if (SwapVector[EntryIdx].WillRemove) { 687 Changed = true; 688 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 689 MachineBasicBlock *MBB = MI->getParent(); 690 BuildMI(*MBB, MI, MI->getDebugLoc(), 691 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg()) 692 .addOperand(MI->getOperand(1)); 693 694 DEBUG(dbgs() << format("Replaced %d with copy: ", 695 SwapVector[EntryIdx].VSEId)); 696 DEBUG(MI->dump()); 697 698 MI->eraseFromParent(); 699 } 700 } 701 702 return Changed; 703 } 704 705 // For debug purposes, dump the contents of the swap vector. 706 void PPCVSXSwapRemoval::dumpSwapVector() { 707 708 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) { 709 710 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 711 int ID = SwapVector[EntryIdx].VSEId; 712 713 DEBUG(dbgs() << format("%6d", ID)); 714 DEBUG(dbgs() << format("%6d", EC->getLeaderValue(ID))); 715 DEBUG(dbgs() << format(" BB#%3d", MI->getParent()->getNumber())); 716 DEBUG(dbgs() << format(" %14s ", TII->getName(MI->getOpcode()))); 717 718 if (SwapVector[EntryIdx].IsLoad) 719 DEBUG(dbgs() << "load "); 720 if (SwapVector[EntryIdx].IsStore) 721 DEBUG(dbgs() << "store "); 722 if (SwapVector[EntryIdx].IsSwap) 723 DEBUG(dbgs() << "swap "); 724 if (SwapVector[EntryIdx].MentionsPhysVR) 725 DEBUG(dbgs() << "physreg "); 726 if (SwapVector[EntryIdx].HasImplicitSubreg) 727 DEBUG(dbgs() << "implsubreg "); 728 729 if (SwapVector[EntryIdx].IsSwappable) { 730 DEBUG(dbgs() << "swappable "); 731 switch(SwapVector[EntryIdx].SpecialHandling) { 732 default: 733 DEBUG(dbgs() << "special:**unknown**"); 734 break; 735 case SH_NONE: 736 break; 737 case SH_BUILDVEC: 738 DEBUG(dbgs() << "special:buildvec "); 739 break; 740 case SH_EXTRACT: 741 DEBUG(dbgs() << "special:extract "); 742 break; 743 case SH_INSERT: 744 DEBUG(dbgs() << "special:insert "); 745 break; 746 case SH_NOSWAP_LD: 747 DEBUG(dbgs() << "special:load "); 748 break; 749 case SH_NOSWAP_ST: 750 DEBUG(dbgs() << "special:store "); 751 break; 752 case SH_SPLAT: 753 DEBUG(dbgs() << "special:splat "); 754 break; 755 } 756 } 757 758 if (SwapVector[EntryIdx].WebRejected) 759 DEBUG(dbgs() << "rejected "); 760 if (SwapVector[EntryIdx].WillRemove) 761 DEBUG(dbgs() << "remove "); 762 763 DEBUG(dbgs() << "\n"); 764 765 // For no-asserts builds. 766 (void)MI; 767 (void)ID; 768 } 769 770 DEBUG(dbgs() << "\n"); 771 } 772 773 } // end default namespace 774 775 INITIALIZE_PASS_BEGIN(PPCVSXSwapRemoval, DEBUG_TYPE, 776 "PowerPC VSX Swap Removal", false, false) 777 INITIALIZE_PASS_END(PPCVSXSwapRemoval, DEBUG_TYPE, 778 "PowerPC VSX Swap Removal", false, false) 779 780 char PPCVSXSwapRemoval::ID = 0; 781 FunctionPass* 782 llvm::createPPCVSXSwapRemovalPass() { return new PPCVSXSwapRemoval(); } 783