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