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 IsSwappable : 1; 83 unsigned int MentionsPartialVR : 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_EXTRACT, 92 SH_INSERT, 93 SH_NOSWAP_LD, 94 SH_NOSWAP_ST, 95 SH_SPLAT, 96 SH_XXPERMDI, 97 SH_COPYWIDEN 98 }; 99 100 struct PPCVSXSwapRemoval : public MachineFunctionPass { 101 102 static char ID; 103 const PPCInstrInfo *TII; 104 MachineFunction *MF; 105 MachineRegisterInfo *MRI; 106 107 // Swap entries are allocated in a vector for better performance. 108 std::vector<PPCVSXSwapEntry> SwapVector; 109 110 // A mapping is maintained between machine instructions and 111 // their swap entries. The key is the address of the MI. 112 DenseMap<MachineInstr*, int> SwapMap; 113 114 // Equivalence classes are used to gather webs of related computation. 115 // Swap entries are represented by their VSEId fields. 116 EquivalenceClasses<int> *EC; 117 118 PPCVSXSwapRemoval() : MachineFunctionPass(ID) { 119 initializePPCVSXSwapRemovalPass(*PassRegistry::getPassRegistry()); 120 } 121 122 private: 123 // Initialize data structures. 124 void initialize(MachineFunction &MFParm); 125 126 // Walk the machine instructions to gather vector usage information. 127 // Return true iff vector mentions are present. 128 bool gatherVectorInstructions(); 129 130 // Add an entry to the swap vector and swap map. 131 int addSwapEntry(MachineInstr *MI, PPCVSXSwapEntry &SwapEntry); 132 133 // Hunt backwards through COPY and SUBREG_TO_REG chains for a 134 // source register. VecIdx indicates the swap vector entry to 135 // mark as mentioning a physical register if the search leads 136 // to one. 137 unsigned lookThruCopyLike(unsigned SrcReg, unsigned VecIdx); 138 139 // Generate equivalence classes for related computations (webs). 140 void formWebs(); 141 142 // Analyze webs and determine those that cannot be optimized. 143 void recordUnoptimizableWebs(); 144 145 // Record which swap instructions can be safely removed. 146 void markSwapsForRemoval(); 147 148 // Remove swaps and update other instructions requiring special 149 // handling. Return true iff any changes are made. 150 bool removeSwaps(); 151 152 // Insert a swap instruction from SrcReg to DstReg at the given 153 // InsertPoint. 154 void insertSwap(MachineInstr *MI, MachineBasicBlock::iterator InsertPoint, 155 unsigned DstReg, unsigned SrcReg); 156 157 // Update instructions requiring special handling. 158 void handleSpecialSwappables(int EntryIdx); 159 160 // Dump a description of the entries in the swap vector. 161 void dumpSwapVector(); 162 163 // Return true iff the given register is in the given class. 164 bool isRegInClass(unsigned Reg, const TargetRegisterClass *RC) { 165 if (TargetRegisterInfo::isVirtualRegister(Reg)) 166 return RC->hasSubClassEq(MRI->getRegClass(Reg)); 167 return RC->contains(Reg); 168 } 169 170 // Return true iff the given register is a full vector register. 171 bool isVecReg(unsigned Reg) { 172 return (isRegInClass(Reg, &PPC::VSRCRegClass) || 173 isRegInClass(Reg, &PPC::VRRCRegClass)); 174 } 175 176 // Return true iff the given register is a partial vector register. 177 bool isScalarVecReg(unsigned Reg) { 178 return (isRegInClass(Reg, &PPC::VSFRCRegClass) || 179 isRegInClass(Reg, &PPC::VSSRCRegClass)); 180 } 181 182 // Return true iff the given register mentions all or part of a 183 // vector register. Also sets Partial to true if the mention 184 // is for just the floating-point register overlap of the register. 185 bool isAnyVecReg(unsigned Reg, bool &Partial) { 186 if (isScalarVecReg(Reg)) 187 Partial = true; 188 return isScalarVecReg(Reg) || isVecReg(Reg); 189 } 190 191 public: 192 // Main entry point for this pass. 193 bool runOnMachineFunction(MachineFunction &MF) override { 194 if (skipFunction(*MF.getFunction())) 195 return false; 196 197 // If we don't have VSX on the subtarget, don't do anything. 198 const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>(); 199 if (!STI.hasVSX()) 200 return false; 201 202 bool Changed = false; 203 initialize(MF); 204 205 if (gatherVectorInstructions()) { 206 formWebs(); 207 recordUnoptimizableWebs(); 208 markSwapsForRemoval(); 209 Changed = removeSwaps(); 210 } 211 212 // FIXME: See the allocation of EC in initialize(). 213 delete EC; 214 return Changed; 215 } 216 }; 217 218 // Initialize data structures for this pass. In particular, clear the 219 // swap vector and allocate the equivalence class mapping before 220 // processing each function. 221 void PPCVSXSwapRemoval::initialize(MachineFunction &MFParm) { 222 MF = &MFParm; 223 MRI = &MF->getRegInfo(); 224 TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo(); 225 226 // An initial vector size of 256 appears to work well in practice. 227 // Small/medium functions with vector content tend not to incur a 228 // reallocation at this size. Three of the vector tests in 229 // projects/test-suite reallocate, which seems like a reasonable rate. 230 const int InitialVectorSize(256); 231 SwapVector.clear(); 232 SwapVector.reserve(InitialVectorSize); 233 234 // FIXME: Currently we allocate EC each time because we don't have 235 // access to the set representation on which to call clear(). Should 236 // consider adding a clear() method to the EquivalenceClasses class. 237 EC = new EquivalenceClasses<int>; 238 } 239 240 // Create an entry in the swap vector for each instruction that mentions 241 // a full vector register, recording various characteristics of the 242 // instructions there. 243 bool PPCVSXSwapRemoval::gatherVectorInstructions() { 244 bool RelevantFunction = false; 245 246 for (MachineBasicBlock &MBB : *MF) { 247 for (MachineInstr &MI : MBB) { 248 249 if (MI.isDebugValue()) 250 continue; 251 252 bool RelevantInstr = false; 253 bool Partial = false; 254 255 for (const MachineOperand &MO : MI.operands()) { 256 if (!MO.isReg()) 257 continue; 258 unsigned Reg = MO.getReg(); 259 if (isAnyVecReg(Reg, Partial)) { 260 RelevantInstr = true; 261 break; 262 } 263 } 264 265 if (!RelevantInstr) 266 continue; 267 268 RelevantFunction = true; 269 270 // Create a SwapEntry initialized to zeros, then fill in the 271 // instruction and ID fields before pushing it to the back 272 // of the swap vector. 273 PPCVSXSwapEntry SwapEntry{}; 274 int VecIdx = addSwapEntry(&MI, SwapEntry); 275 276 switch(MI.getOpcode()) { 277 default: 278 // Unless noted otherwise, an instruction is considered 279 // safe for the optimization. There are a large number of 280 // such true-SIMD instructions (all vector math, logical, 281 // select, compare, etc.). However, if the instruction 282 // mentions a partial vector register and does not have 283 // special handling defined, it is not swappable. 284 if (Partial) 285 SwapVector[VecIdx].MentionsPartialVR = 1; 286 else 287 SwapVector[VecIdx].IsSwappable = 1; 288 break; 289 case PPC::XXPERMDI: { 290 // This is a swap if it is of the form XXPERMDI t, s, s, 2. 291 // Unfortunately, MachineCSE ignores COPY and SUBREG_TO_REG, so we 292 // can also see XXPERMDI t, SUBREG_TO_REG(s), SUBREG_TO_REG(s), 2, 293 // for example. We have to look through chains of COPY and 294 // SUBREG_TO_REG to find the real source value for comparison. 295 // If the real source value is a physical register, then mark the 296 // XXPERMDI as mentioning a physical register. 297 int immed = MI.getOperand(3).getImm(); 298 if (immed == 2) { 299 unsigned trueReg1 = lookThruCopyLike(MI.getOperand(1).getReg(), 300 VecIdx); 301 unsigned trueReg2 = lookThruCopyLike(MI.getOperand(2).getReg(), 302 VecIdx); 303 if (trueReg1 == trueReg2) 304 SwapVector[VecIdx].IsSwap = 1; 305 else { 306 // We can still handle these if the two registers are not 307 // identical, by adjusting the form of the XXPERMDI. 308 SwapVector[VecIdx].IsSwappable = 1; 309 SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI; 310 } 311 // This is a doubleword splat if it is of the form 312 // XXPERMDI t, s, s, 0 or XXPERMDI t, s, s, 3. As above we 313 // must look through chains of copy-likes to find the source 314 // register. We turn off the marking for mention of a physical 315 // register, because splatting it is safe; the optimization 316 // will not swap the value in the physical register. Whether 317 // or not the two input registers are identical, we can handle 318 // these by adjusting the form of the XXPERMDI. 319 } else if (immed == 0 || immed == 3) { 320 321 SwapVector[VecIdx].IsSwappable = 1; 322 SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI; 323 324 unsigned trueReg1 = lookThruCopyLike(MI.getOperand(1).getReg(), 325 VecIdx); 326 unsigned trueReg2 = lookThruCopyLike(MI.getOperand(2).getReg(), 327 VecIdx); 328 if (trueReg1 == trueReg2) 329 SwapVector[VecIdx].MentionsPhysVR = 0; 330 331 } else { 332 // We can still handle these by adjusting the form of the XXPERMDI. 333 SwapVector[VecIdx].IsSwappable = 1; 334 SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI; 335 } 336 break; 337 } 338 case PPC::LVX: 339 // Non-permuting loads are currently unsafe. We can use special 340 // handling for this in the future. By not marking these as 341 // IsSwap, we ensure computations containing them will be rejected 342 // for now. 343 SwapVector[VecIdx].IsLoad = 1; 344 break; 345 case PPC::LXVD2X: 346 case PPC::LXVW4X: 347 // Permuting loads are marked as both load and swap, and are 348 // safe for optimization. 349 SwapVector[VecIdx].IsLoad = 1; 350 SwapVector[VecIdx].IsSwap = 1; 351 break; 352 case PPC::LXSDX: 353 case PPC::LXSSPX: 354 // A load of a floating-point value into the high-order half of 355 // a vector register is safe, provided that we introduce a swap 356 // following the load, which will be done by the SUBREG_TO_REG 357 // support. So just mark these as safe. 358 SwapVector[VecIdx].IsLoad = 1; 359 SwapVector[VecIdx].IsSwappable = 1; 360 break; 361 case PPC::STVX: 362 // Non-permuting stores are currently unsafe. We can use special 363 // handling for this in the future. By not marking these as 364 // IsSwap, we ensure computations containing them will be rejected 365 // for now. 366 SwapVector[VecIdx].IsStore = 1; 367 break; 368 case PPC::STXVD2X: 369 case PPC::STXVW4X: 370 // Permuting stores are marked as both store and swap, and are 371 // safe for optimization. 372 SwapVector[VecIdx].IsStore = 1; 373 SwapVector[VecIdx].IsSwap = 1; 374 break; 375 case PPC::COPY: 376 // These are fine provided they are moving between full vector 377 // register classes. 378 if (isVecReg(MI.getOperand(0).getReg()) && 379 isVecReg(MI.getOperand(1).getReg())) 380 SwapVector[VecIdx].IsSwappable = 1; 381 // If we have a copy from one scalar floating-point register 382 // to another, we can accept this even if it is a physical 383 // register. The only way this gets involved is if it feeds 384 // a SUBREG_TO_REG, which is handled by introducing a swap. 385 else if (isScalarVecReg(MI.getOperand(0).getReg()) && 386 isScalarVecReg(MI.getOperand(1).getReg())) 387 SwapVector[VecIdx].IsSwappable = 1; 388 break; 389 case PPC::SUBREG_TO_REG: { 390 // These are fine provided they are moving between full vector 391 // register classes. If they are moving from a scalar 392 // floating-point class to a vector class, we can handle those 393 // as well, provided we introduce a swap. It is generally the 394 // case that we will introduce fewer swaps than we remove, but 395 // (FIXME) a cost model could be used. However, introduced 396 // swaps could potentially be CSEd, so this is not trivial. 397 if (isVecReg(MI.getOperand(0).getReg()) && 398 isVecReg(MI.getOperand(2).getReg())) 399 SwapVector[VecIdx].IsSwappable = 1; 400 else if (isVecReg(MI.getOperand(0).getReg()) && 401 isScalarVecReg(MI.getOperand(2).getReg())) { 402 SwapVector[VecIdx].IsSwappable = 1; 403 SwapVector[VecIdx].SpecialHandling = SHValues::SH_COPYWIDEN; 404 } 405 break; 406 } 407 case PPC::VSPLTB: 408 case PPC::VSPLTH: 409 case PPC::VSPLTW: 410 // Splats are lane-sensitive, but we can use special handling 411 // to adjust the source lane for the splat. This is not yet 412 // implemented. When it is, we need to uncomment the following: 413 SwapVector[VecIdx].IsSwappable = 1; 414 SwapVector[VecIdx].SpecialHandling = SHValues::SH_SPLAT; 415 break; 416 // The presence of the following lane-sensitive operations in a 417 // web will kill the optimization, at least for now. For these 418 // we do nothing, causing the optimization to fail. 419 // FIXME: Some of these could be permitted with special handling, 420 // and will be phased in as time permits. 421 // FIXME: There is no simple and maintainable way to express a set 422 // of opcodes having a common attribute in TableGen. Should this 423 // change, this is a prime candidate to use such a mechanism. 424 case PPC::INLINEASM: 425 case PPC::EXTRACT_SUBREG: 426 case PPC::INSERT_SUBREG: 427 case PPC::COPY_TO_REGCLASS: 428 case PPC::LVEBX: 429 case PPC::LVEHX: 430 case PPC::LVEWX: 431 case PPC::LVSL: 432 case PPC::LVSR: 433 case PPC::LVXL: 434 case PPC::STVEBX: 435 case PPC::STVEHX: 436 case PPC::STVEWX: 437 case PPC::STVXL: 438 // We can handle STXSDX and STXSSPX similarly to LXSDX and LXSSPX, 439 // by adding special handling for narrowing copies as well as 440 // widening ones. However, I've experimented with this, and in 441 // practice we currently do not appear to use STXSDX fed by 442 // a narrowing copy from a full vector register. Since I can't 443 // generate any useful test cases, I've left this alone for now. 444 case PPC::STXSDX: 445 case PPC::STXSSPX: 446 case PPC::VCIPHER: 447 case PPC::VCIPHERLAST: 448 case PPC::VMRGHB: 449 case PPC::VMRGHH: 450 case PPC::VMRGHW: 451 case PPC::VMRGLB: 452 case PPC::VMRGLH: 453 case PPC::VMRGLW: 454 case PPC::VMULESB: 455 case PPC::VMULESH: 456 case PPC::VMULESW: 457 case PPC::VMULEUB: 458 case PPC::VMULEUH: 459 case PPC::VMULEUW: 460 case PPC::VMULOSB: 461 case PPC::VMULOSH: 462 case PPC::VMULOSW: 463 case PPC::VMULOUB: 464 case PPC::VMULOUH: 465 case PPC::VMULOUW: 466 case PPC::VNCIPHER: 467 case PPC::VNCIPHERLAST: 468 case PPC::VPERM: 469 case PPC::VPERMXOR: 470 case PPC::VPKPX: 471 case PPC::VPKSHSS: 472 case PPC::VPKSHUS: 473 case PPC::VPKSDSS: 474 case PPC::VPKSDUS: 475 case PPC::VPKSWSS: 476 case PPC::VPKSWUS: 477 case PPC::VPKUDUM: 478 case PPC::VPKUDUS: 479 case PPC::VPKUHUM: 480 case PPC::VPKUHUS: 481 case PPC::VPKUWUM: 482 case PPC::VPKUWUS: 483 case PPC::VPMSUMB: 484 case PPC::VPMSUMD: 485 case PPC::VPMSUMH: 486 case PPC::VPMSUMW: 487 case PPC::VRLB: 488 case PPC::VRLD: 489 case PPC::VRLH: 490 case PPC::VRLW: 491 case PPC::VSBOX: 492 case PPC::VSHASIGMAD: 493 case PPC::VSHASIGMAW: 494 case PPC::VSL: 495 case PPC::VSLDOI: 496 case PPC::VSLO: 497 case PPC::VSR: 498 case PPC::VSRO: 499 case PPC::VSUM2SWS: 500 case PPC::VSUM4SBS: 501 case PPC::VSUM4SHS: 502 case PPC::VSUM4UBS: 503 case PPC::VSUMSWS: 504 case PPC::VUPKHPX: 505 case PPC::VUPKHSB: 506 case PPC::VUPKHSH: 507 case PPC::VUPKHSW: 508 case PPC::VUPKLPX: 509 case PPC::VUPKLSB: 510 case PPC::VUPKLSH: 511 case PPC::VUPKLSW: 512 case PPC::XXMRGHW: 513 case PPC::XXMRGLW: 514 // XXSLDWI could be replaced by a general permute with one of three 515 // permute control vectors (for shift values 1, 2, 3). However, 516 // VPERM has a more restrictive register class. 517 case PPC::XXSLDWI: 518 case PPC::XXSPLTW: 519 break; 520 } 521 } 522 } 523 524 if (RelevantFunction) { 525 DEBUG(dbgs() << "Swap vector when first built\n\n"); 526 dumpSwapVector(); 527 } 528 529 return RelevantFunction; 530 } 531 532 // Add an entry to the swap vector and swap map, and make a 533 // singleton equivalence class for the entry. 534 int PPCVSXSwapRemoval::addSwapEntry(MachineInstr *MI, 535 PPCVSXSwapEntry& SwapEntry) { 536 SwapEntry.VSEMI = MI; 537 SwapEntry.VSEId = SwapVector.size(); 538 SwapVector.push_back(SwapEntry); 539 EC->insert(SwapEntry.VSEId); 540 SwapMap[MI] = SwapEntry.VSEId; 541 return SwapEntry.VSEId; 542 } 543 544 // This is used to find the "true" source register for an 545 // XXPERMDI instruction, since MachineCSE does not handle the 546 // "copy-like" operations (Copy and SubregToReg). Returns 547 // the original SrcReg unless it is the target of a copy-like 548 // operation, in which case we chain backwards through all 549 // such operations to the ultimate source register. If a 550 // physical register is encountered, we stop the search and 551 // flag the swap entry indicated by VecIdx (the original 552 // XXPERMDI) as mentioning a physical register. 553 unsigned PPCVSXSwapRemoval::lookThruCopyLike(unsigned SrcReg, 554 unsigned VecIdx) { 555 MachineInstr *MI = MRI->getVRegDef(SrcReg); 556 if (!MI->isCopyLike()) 557 return SrcReg; 558 559 unsigned CopySrcReg; 560 if (MI->isCopy()) 561 CopySrcReg = MI->getOperand(1).getReg(); 562 else { 563 assert(MI->isSubregToReg() && "bad opcode for lookThruCopyLike"); 564 CopySrcReg = MI->getOperand(2).getReg(); 565 } 566 567 if (!TargetRegisterInfo::isVirtualRegister(CopySrcReg)) { 568 if (!isScalarVecReg(CopySrcReg)) 569 SwapVector[VecIdx].MentionsPhysVR = 1; 570 return CopySrcReg; 571 } 572 573 return lookThruCopyLike(CopySrcReg, VecIdx); 574 } 575 576 // Generate equivalence classes for related computations (webs) by 577 // def-use relationships of virtual registers. Mention of a physical 578 // register terminates the generation of equivalence classes as this 579 // indicates a use of a parameter, definition of a return value, use 580 // of a value returned from a call, or definition of a parameter to a 581 // call. Computations with physical register mentions are flagged 582 // as such so their containing webs will not be optimized. 583 void PPCVSXSwapRemoval::formWebs() { 584 585 DEBUG(dbgs() << "\n*** Forming webs for swap removal ***\n\n"); 586 587 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) { 588 589 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 590 591 DEBUG(dbgs() << "\n" << SwapVector[EntryIdx].VSEId << " "); 592 DEBUG(MI->dump()); 593 594 // It's sufficient to walk vector uses and join them to their unique 595 // definitions. In addition, check full vector register operands 596 // for physical regs. We exclude partial-vector register operands 597 // because we can handle them if copied to a full vector. 598 for (const MachineOperand &MO : MI->operands()) { 599 if (!MO.isReg()) 600 continue; 601 602 unsigned Reg = MO.getReg(); 603 if (!isVecReg(Reg) && !isScalarVecReg(Reg)) 604 continue; 605 606 if (!TargetRegisterInfo::isVirtualRegister(Reg)) { 607 if (!(MI->isCopy() && isScalarVecReg(Reg))) 608 SwapVector[EntryIdx].MentionsPhysVR = 1; 609 continue; 610 } 611 612 if (!MO.isUse()) 613 continue; 614 615 MachineInstr* DefMI = MRI->getVRegDef(Reg); 616 assert(SwapMap.find(DefMI) != SwapMap.end() && 617 "Inconsistency: def of vector reg not found in swap map!"); 618 int DefIdx = SwapMap[DefMI]; 619 (void)EC->unionSets(SwapVector[DefIdx].VSEId, 620 SwapVector[EntryIdx].VSEId); 621 622 DEBUG(dbgs() << format("Unioning %d with %d\n", SwapVector[DefIdx].VSEId, 623 SwapVector[EntryIdx].VSEId)); 624 DEBUG(dbgs() << " Def: "); 625 DEBUG(DefMI->dump()); 626 } 627 } 628 } 629 630 // Walk the swap vector entries looking for conditions that prevent their 631 // containing computations from being optimized. When such conditions are 632 // found, mark the representative of the computation's equivalence class 633 // as rejected. 634 void PPCVSXSwapRemoval::recordUnoptimizableWebs() { 635 636 DEBUG(dbgs() << "\n*** Rejecting webs for swap removal ***\n\n"); 637 638 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) { 639 int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId); 640 641 // If representative is already rejected, don't waste further time. 642 if (SwapVector[Repr].WebRejected) 643 continue; 644 645 // Reject webs containing mentions of physical or partial registers, or 646 // containing operations that we don't know how to handle in a lane- 647 // permuted region. 648 if (SwapVector[EntryIdx].MentionsPhysVR || 649 SwapVector[EntryIdx].MentionsPartialVR || 650 !(SwapVector[EntryIdx].IsSwappable || SwapVector[EntryIdx].IsSwap)) { 651 652 SwapVector[Repr].WebRejected = 1; 653 654 DEBUG(dbgs() << 655 format("Web %d rejected for physreg, partial reg, or not " 656 "swap[pable]\n", Repr)); 657 DEBUG(dbgs() << " in " << EntryIdx << ": "); 658 DEBUG(SwapVector[EntryIdx].VSEMI->dump()); 659 DEBUG(dbgs() << "\n"); 660 } 661 662 // Reject webs than contain swapping loads that feed something other 663 // than a swap instruction. 664 else if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) { 665 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 666 unsigned DefReg = MI->getOperand(0).getReg(); 667 668 // We skip debug instructions in the analysis. (Note that debug 669 // location information is still maintained by this optimization 670 // because it remains on the LXVD2X and STXVD2X instructions after 671 // the XXPERMDIs are removed.) 672 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) { 673 int UseIdx = SwapMap[&UseMI]; 674 675 if (!SwapVector[UseIdx].IsSwap || SwapVector[UseIdx].IsLoad || 676 SwapVector[UseIdx].IsStore) { 677 678 SwapVector[Repr].WebRejected = 1; 679 680 DEBUG(dbgs() << 681 format("Web %d rejected for load not feeding swap\n", Repr)); 682 DEBUG(dbgs() << " def " << EntryIdx << ": "); 683 DEBUG(MI->dump()); 684 DEBUG(dbgs() << " use " << UseIdx << ": "); 685 DEBUG(UseMI.dump()); 686 DEBUG(dbgs() << "\n"); 687 } 688 } 689 690 // Reject webs that contain swapping stores that are fed by something 691 // other than a swap instruction. 692 } else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) { 693 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 694 unsigned UseReg = MI->getOperand(0).getReg(); 695 MachineInstr *DefMI = MRI->getVRegDef(UseReg); 696 int DefIdx = SwapMap[DefMI]; 697 698 if (!SwapVector[DefIdx].IsSwap || SwapVector[DefIdx].IsLoad || 699 SwapVector[DefIdx].IsStore) { 700 701 SwapVector[Repr].WebRejected = 1; 702 703 DEBUG(dbgs() << 704 format("Web %d rejected for store not fed by swap\n", Repr)); 705 DEBUG(dbgs() << " def " << DefIdx << ": "); 706 DEBUG(DefMI->dump()); 707 DEBUG(dbgs() << " use " << EntryIdx << ": "); 708 DEBUG(MI->dump()); 709 DEBUG(dbgs() << "\n"); 710 } 711 } 712 } 713 714 DEBUG(dbgs() << "Swap vector after web analysis:\n\n"); 715 dumpSwapVector(); 716 } 717 718 // Walk the swap vector entries looking for swaps fed by permuting loads 719 // and swaps that feed permuting stores. If the containing computation 720 // has not been marked rejected, mark each such swap for removal. 721 // (Removal is delayed in case optimization has disturbed the pattern, 722 // such that multiple loads feed the same swap, etc.) 723 void PPCVSXSwapRemoval::markSwapsForRemoval() { 724 725 DEBUG(dbgs() << "\n*** Marking swaps for removal ***\n\n"); 726 727 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) { 728 729 if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) { 730 int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId); 731 732 if (!SwapVector[Repr].WebRejected) { 733 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 734 unsigned DefReg = MI->getOperand(0).getReg(); 735 736 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) { 737 int UseIdx = SwapMap[&UseMI]; 738 SwapVector[UseIdx].WillRemove = 1; 739 740 DEBUG(dbgs() << "Marking swap fed by load for removal: "); 741 DEBUG(UseMI.dump()); 742 } 743 } 744 745 } else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) { 746 int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId); 747 748 if (!SwapVector[Repr].WebRejected) { 749 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 750 unsigned UseReg = MI->getOperand(0).getReg(); 751 MachineInstr *DefMI = MRI->getVRegDef(UseReg); 752 int DefIdx = SwapMap[DefMI]; 753 SwapVector[DefIdx].WillRemove = 1; 754 755 DEBUG(dbgs() << "Marking swap feeding store for removal: "); 756 DEBUG(DefMI->dump()); 757 } 758 759 } else if (SwapVector[EntryIdx].IsSwappable && 760 SwapVector[EntryIdx].SpecialHandling != 0) { 761 int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId); 762 763 if (!SwapVector[Repr].WebRejected) 764 handleSpecialSwappables(EntryIdx); 765 } 766 } 767 } 768 769 // Create an xxswapd instruction and insert it prior to the given point. 770 // MI is used to determine basic block and debug loc information. 771 // FIXME: When inserting a swap, we should check whether SrcReg is 772 // defined by another swap: SrcReg = XXPERMDI Reg, Reg, 2; If so, 773 // then instead we should generate a copy from Reg to DstReg. 774 void PPCVSXSwapRemoval::insertSwap(MachineInstr *MI, 775 MachineBasicBlock::iterator InsertPoint, 776 unsigned DstReg, unsigned SrcReg) { 777 BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(), 778 TII->get(PPC::XXPERMDI), DstReg) 779 .addReg(SrcReg) 780 .addReg(SrcReg) 781 .addImm(2); 782 } 783 784 // The identified swap entry requires special handling to allow its 785 // containing computation to be optimized. Perform that handling 786 // here. 787 // FIXME: Additional opportunities will be phased in with subsequent 788 // patches. 789 void PPCVSXSwapRemoval::handleSpecialSwappables(int EntryIdx) { 790 switch (SwapVector[EntryIdx].SpecialHandling) { 791 792 default: 793 llvm_unreachable("Unexpected special handling type"); 794 795 // For splats based on an index into a vector, add N/2 modulo N 796 // to the index, where N is the number of vector elements. 797 case SHValues::SH_SPLAT: { 798 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 799 unsigned NElts; 800 801 DEBUG(dbgs() << "Changing splat: "); 802 DEBUG(MI->dump()); 803 804 switch (MI->getOpcode()) { 805 default: 806 llvm_unreachable("Unexpected splat opcode"); 807 case PPC::VSPLTB: NElts = 16; break; 808 case PPC::VSPLTH: NElts = 8; break; 809 case PPC::VSPLTW: NElts = 4; break; 810 } 811 812 unsigned EltNo = MI->getOperand(1).getImm(); 813 EltNo = (EltNo + NElts / 2) % NElts; 814 MI->getOperand(1).setImm(EltNo); 815 816 DEBUG(dbgs() << " Into: "); 817 DEBUG(MI->dump()); 818 break; 819 } 820 821 // For an XXPERMDI that isn't handled otherwise, we need to 822 // reverse the order of the operands. If the selector operand 823 // has a value of 0 or 3, we need to change it to 3 or 0, 824 // respectively. Otherwise we should leave it alone. (This 825 // is equivalent to reversing the two bits of the selector 826 // operand and complementing the result.) 827 case SHValues::SH_XXPERMDI: { 828 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 829 830 DEBUG(dbgs() << "Changing XXPERMDI: "); 831 DEBUG(MI->dump()); 832 833 unsigned Selector = MI->getOperand(3).getImm(); 834 if (Selector == 0 || Selector == 3) 835 Selector = 3 - Selector; 836 MI->getOperand(3).setImm(Selector); 837 838 unsigned Reg1 = MI->getOperand(1).getReg(); 839 unsigned Reg2 = MI->getOperand(2).getReg(); 840 MI->getOperand(1).setReg(Reg2); 841 MI->getOperand(2).setReg(Reg1); 842 843 DEBUG(dbgs() << " Into: "); 844 DEBUG(MI->dump()); 845 break; 846 } 847 848 // For a copy from a scalar floating-point register to a vector 849 // register, removing swaps will leave the copied value in the 850 // wrong lane. Insert a swap following the copy to fix this. 851 case SHValues::SH_COPYWIDEN: { 852 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 853 854 DEBUG(dbgs() << "Changing SUBREG_TO_REG: "); 855 DEBUG(MI->dump()); 856 857 unsigned DstReg = MI->getOperand(0).getReg(); 858 const TargetRegisterClass *DstRC = MRI->getRegClass(DstReg); 859 unsigned NewVReg = MRI->createVirtualRegister(DstRC); 860 861 MI->getOperand(0).setReg(NewVReg); 862 DEBUG(dbgs() << " Into: "); 863 DEBUG(MI->dump()); 864 865 auto InsertPoint = ++MachineBasicBlock::iterator(MI); 866 867 // Note that an XXPERMDI requires a VSRC, so if the SUBREG_TO_REG 868 // is copying to a VRRC, we need to be careful to avoid a register 869 // assignment problem. In this case we must copy from VRRC to VSRC 870 // prior to the swap, and from VSRC to VRRC following the swap. 871 // Coalescing will usually remove all this mess. 872 if (DstRC == &PPC::VRRCRegClass) { 873 unsigned VSRCTmp1 = MRI->createVirtualRegister(&PPC::VSRCRegClass); 874 unsigned VSRCTmp2 = MRI->createVirtualRegister(&PPC::VSRCRegClass); 875 876 BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(), 877 TII->get(PPC::COPY), VSRCTmp1) 878 .addReg(NewVReg); 879 DEBUG(std::prev(InsertPoint)->dump()); 880 881 insertSwap(MI, InsertPoint, VSRCTmp2, VSRCTmp1); 882 DEBUG(std::prev(InsertPoint)->dump()); 883 884 BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(), 885 TII->get(PPC::COPY), DstReg) 886 .addReg(VSRCTmp2); 887 DEBUG(std::prev(InsertPoint)->dump()); 888 889 } else { 890 insertSwap(MI, InsertPoint, DstReg, NewVReg); 891 DEBUG(std::prev(InsertPoint)->dump()); 892 } 893 break; 894 } 895 } 896 } 897 898 // Walk the swap vector and replace each entry marked for removal with 899 // a copy operation. 900 bool PPCVSXSwapRemoval::removeSwaps() { 901 902 DEBUG(dbgs() << "\n*** Removing swaps ***\n\n"); 903 904 bool Changed = false; 905 906 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) { 907 if (SwapVector[EntryIdx].WillRemove) { 908 Changed = true; 909 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 910 MachineBasicBlock *MBB = MI->getParent(); 911 BuildMI(*MBB, MI, MI->getDebugLoc(), 912 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg()) 913 .addOperand(MI->getOperand(1)); 914 915 DEBUG(dbgs() << format("Replaced %d with copy: ", 916 SwapVector[EntryIdx].VSEId)); 917 DEBUG(MI->dump()); 918 919 MI->eraseFromParent(); 920 } 921 } 922 923 return Changed; 924 } 925 926 // For debug purposes, dump the contents of the swap vector. 927 void PPCVSXSwapRemoval::dumpSwapVector() { 928 929 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) { 930 931 MachineInstr *MI = SwapVector[EntryIdx].VSEMI; 932 int ID = SwapVector[EntryIdx].VSEId; 933 934 DEBUG(dbgs() << format("%6d", ID)); 935 DEBUG(dbgs() << format("%6d", EC->getLeaderValue(ID))); 936 DEBUG(dbgs() << format(" BB#%3d", MI->getParent()->getNumber())); 937 DEBUG(dbgs() << format(" %14s ", TII->getName(MI->getOpcode()))); 938 939 if (SwapVector[EntryIdx].IsLoad) 940 DEBUG(dbgs() << "load "); 941 if (SwapVector[EntryIdx].IsStore) 942 DEBUG(dbgs() << "store "); 943 if (SwapVector[EntryIdx].IsSwap) 944 DEBUG(dbgs() << "swap "); 945 if (SwapVector[EntryIdx].MentionsPhysVR) 946 DEBUG(dbgs() << "physreg "); 947 if (SwapVector[EntryIdx].MentionsPartialVR) 948 DEBUG(dbgs() << "partialreg "); 949 950 if (SwapVector[EntryIdx].IsSwappable) { 951 DEBUG(dbgs() << "swappable "); 952 switch(SwapVector[EntryIdx].SpecialHandling) { 953 default: 954 DEBUG(dbgs() << "special:**unknown**"); 955 break; 956 case SH_NONE: 957 break; 958 case SH_EXTRACT: 959 DEBUG(dbgs() << "special:extract "); 960 break; 961 case SH_INSERT: 962 DEBUG(dbgs() << "special:insert "); 963 break; 964 case SH_NOSWAP_LD: 965 DEBUG(dbgs() << "special:load "); 966 break; 967 case SH_NOSWAP_ST: 968 DEBUG(dbgs() << "special:store "); 969 break; 970 case SH_SPLAT: 971 DEBUG(dbgs() << "special:splat "); 972 break; 973 case SH_XXPERMDI: 974 DEBUG(dbgs() << "special:xxpermdi "); 975 break; 976 case SH_COPYWIDEN: 977 DEBUG(dbgs() << "special:copywiden "); 978 break; 979 } 980 } 981 982 if (SwapVector[EntryIdx].WebRejected) 983 DEBUG(dbgs() << "rejected "); 984 if (SwapVector[EntryIdx].WillRemove) 985 DEBUG(dbgs() << "remove "); 986 987 DEBUG(dbgs() << "\n"); 988 989 // For no-asserts builds. 990 (void)MI; 991 (void)ID; 992 } 993 994 DEBUG(dbgs() << "\n"); 995 } 996 997 } // end default namespace 998 999 INITIALIZE_PASS_BEGIN(PPCVSXSwapRemoval, DEBUG_TYPE, 1000 "PowerPC VSX Swap Removal", false, false) 1001 INITIALIZE_PASS_END(PPCVSXSwapRemoval, DEBUG_TYPE, 1002 "PowerPC VSX Swap Removal", false, false) 1003 1004 char PPCVSXSwapRemoval::ID = 0; 1005 FunctionPass* 1006 llvm::createPPCVSXSwapRemovalPass() { return new PPCVSXSwapRemoval(); } 1007