blob: 7d34efd4af3e0fca9cf3f84b02b4b03647fd54d9 [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
Bill Schmidtfe723b92015-04-27 19:57:34 +000045#include "PPC.h"
46#include "PPCInstrBuilder.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000047#include "PPCInstrInfo.h"
Bill Schmidtfe723b92015-04-27 19:57:34 +000048#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;
Bill Schmidt15deb802015-07-13 22:58:19 +000083 unsigned int MentionsPartialVR : 1;
Bill Schmidtfe723b92015-04-27 19:57:34 +000084 unsigned int SpecialHandling : 3;
85 unsigned int WebRejected : 1;
86 unsigned int WillRemove : 1;
87};
88
89enum SHValues {
90 SH_NONE = 0,
Bill Schmidtfe723b92015-04-27 19:57:34 +000091 SH_EXTRACT,
92 SH_INSERT,
93 SH_NOSWAP_LD,
94 SH_NOSWAP_ST,
Bill Schmidt15deb802015-07-13 22:58:19 +000095 SH_SPLAT,
96 SH_XXPERMDI,
Bill Schmidt2be80542015-07-21 21:40:17 +000097 SH_COPYWIDEN
Bill Schmidtfe723b92015-04-27 19:57:34 +000098};
99
100struct 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
122private:
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
Bill Schmidt2be80542015-07-21 21:40:17 +0000152 // 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
Bill Schmidtfe723b92015-04-27 19:57:34 +0000157 // 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));
Alexander Kornienko175a7cb2015-12-28 13:38:42 +0000167 return RC->contains(Reg);
Bill Schmidtfe723b92015-04-27 19:57:34 +0000168 }
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
Bill Schmidt15deb802015-07-13 22:58:19 +0000176 // 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
Bill Schmidtfe723b92015-04-27 19:57:34 +0000191public:
192 // Main entry point for this pass.
193 bool runOnMachineFunction(MachineFunction &MF) override {
Andrew Kaylor289bd5f2016-04-27 19:39:32 +0000194 if (skipFunction(*MF.getFunction()))
195 return false;
196
Bill Schmidtfe723b92015-04-27 19:57:34 +0000197 // If we don't have VSX on the subtarget, don't do anything.
Sean Fertile3cd1a032017-07-05 18:37:10 +0000198 // Also, on Power 9 the load and store ops preserve element order and so
199 // the swaps are not required.
Bill Schmidtfe723b92015-04-27 19:57:34 +0000200 const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
Sean Fertile3cd1a032017-07-05 18:37:10 +0000201 if (!STI.hasVSX() || !STI.needsSwapsForVSXMemOps())
Bill Schmidtfe723b92015-04-27 19:57:34 +0000202 return false;
203
204 bool Changed = false;
205 initialize(MF);
206
207 if (gatherVectorInstructions()) {
208 formWebs();
209 recordUnoptimizableWebs();
210 markSwapsForRemoval();
211 Changed = removeSwaps();
212 }
213
214 // FIXME: See the allocation of EC in initialize().
215 delete EC;
216 return Changed;
217 }
218};
219
220// Initialize data structures for this pass. In particular, clear the
221// swap vector and allocate the equivalence class mapping before
222// processing each function.
223void PPCVSXSwapRemoval::initialize(MachineFunction &MFParm) {
224 MF = &MFParm;
225 MRI = &MF->getRegInfo();
Bill Schmidt8ed7cec2015-11-02 22:43:57 +0000226 TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo();
Bill Schmidtfe723b92015-04-27 19:57:34 +0000227
228 // An initial vector size of 256 appears to work well in practice.
229 // Small/medium functions with vector content tend not to incur a
230 // reallocation at this size. Three of the vector tests in
231 // projects/test-suite reallocate, which seems like a reasonable rate.
232 const int InitialVectorSize(256);
233 SwapVector.clear();
234 SwapVector.reserve(InitialVectorSize);
235
236 // FIXME: Currently we allocate EC each time because we don't have
237 // access to the set representation on which to call clear(). Should
238 // consider adding a clear() method to the EquivalenceClasses class.
239 EC = new EquivalenceClasses<int>;
240}
241
242// Create an entry in the swap vector for each instruction that mentions
243// a full vector register, recording various characteristics of the
244// instructions there.
245bool PPCVSXSwapRemoval::gatherVectorInstructions() {
246 bool RelevantFunction = false;
247
248 for (MachineBasicBlock &MBB : *MF) {
249 for (MachineInstr &MI : MBB) {
250
Bill Schmidt32fd1892015-08-24 19:27:27 +0000251 if (MI.isDebugValue())
252 continue;
253
Bill Schmidtfe723b92015-04-27 19:57:34 +0000254 bool RelevantInstr = false;
Bill Schmidt15deb802015-07-13 22:58:19 +0000255 bool Partial = false;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000256
257 for (const MachineOperand &MO : MI.operands()) {
258 if (!MO.isReg())
259 continue;
260 unsigned Reg = MO.getReg();
Bill Schmidt15deb802015-07-13 22:58:19 +0000261 if (isAnyVecReg(Reg, Partial)) {
Bill Schmidtfe723b92015-04-27 19:57:34 +0000262 RelevantInstr = true;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000263 break;
264 }
265 }
266
267 if (!RelevantInstr)
268 continue;
269
270 RelevantFunction = true;
271
272 // Create a SwapEntry initialized to zeros, then fill in the
273 // instruction and ID fields before pushing it to the back
274 // of the swap vector.
275 PPCVSXSwapEntry SwapEntry{};
276 int VecIdx = addSwapEntry(&MI, SwapEntry);
277
Bill Schmidtfe723b92015-04-27 19:57:34 +0000278 switch(MI.getOpcode()) {
279 default:
280 // Unless noted otherwise, an instruction is considered
281 // safe for the optimization. There are a large number of
282 // such true-SIMD instructions (all vector math, logical,
Bill Schmidt15deb802015-07-13 22:58:19 +0000283 // select, compare, etc.). However, if the instruction
284 // mentions a partial vector register and does not have
285 // special handling defined, it is not swappable.
286 if (Partial)
287 SwapVector[VecIdx].MentionsPartialVR = 1;
288 else
289 SwapVector[VecIdx].IsSwappable = 1;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000290 break;
Bill Schmidt7c691fe2015-07-02 17:03:06 +0000291 case PPC::XXPERMDI: {
Bill Schmidtfe723b92015-04-27 19:57:34 +0000292 // This is a swap if it is of the form XXPERMDI t, s, s, 2.
293 // Unfortunately, MachineCSE ignores COPY and SUBREG_TO_REG, so we
294 // can also see XXPERMDI t, SUBREG_TO_REG(s), SUBREG_TO_REG(s), 2,
295 // for example. We have to look through chains of COPY and
296 // SUBREG_TO_REG to find the real source value for comparison.
297 // If the real source value is a physical register, then mark the
298 // XXPERMDI as mentioning a physical register.
Bill Schmidt7c691fe2015-07-02 17:03:06 +0000299 int immed = MI.getOperand(3).getImm();
300 if (immed == 2) {
Bill Schmidtfe723b92015-04-27 19:57:34 +0000301 unsigned trueReg1 = lookThruCopyLike(MI.getOperand(1).getReg(),
302 VecIdx);
303 unsigned trueReg2 = lookThruCopyLike(MI.getOperand(2).getReg(),
304 VecIdx);
305 if (trueReg1 == trueReg2)
306 SwapVector[VecIdx].IsSwap = 1;
Bill Schmidt15deb802015-07-13 22:58:19 +0000307 else {
308 // We can still handle these if the two registers are not
309 // identical, by adjusting the form of the XXPERMDI.
310 SwapVector[VecIdx].IsSwappable = 1;
311 SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
312 }
Bill Schmidt7c691fe2015-07-02 17:03:06 +0000313 // This is a doubleword splat if it is of the form
314 // XXPERMDI t, s, s, 0 or XXPERMDI t, s, s, 3. As above we
315 // must look through chains of copy-likes to find the source
316 // register. We turn off the marking for mention of a physical
317 // register, because splatting it is safe; the optimization
Bill Schmidt15deb802015-07-13 22:58:19 +0000318 // will not swap the value in the physical register. Whether
319 // or not the two input registers are identical, we can handle
320 // these by adjusting the form of the XXPERMDI.
321 } else if (immed == 0 || immed == 3) {
322
323 SwapVector[VecIdx].IsSwappable = 1;
324 SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
325
Bill Schmidt7c691fe2015-07-02 17:03:06 +0000326 unsigned trueReg1 = lookThruCopyLike(MI.getOperand(1).getReg(),
327 VecIdx);
328 unsigned trueReg2 = lookThruCopyLike(MI.getOperand(2).getReg(),
329 VecIdx);
Bill Schmidt15deb802015-07-13 22:58:19 +0000330 if (trueReg1 == trueReg2)
Bill Schmidt7c691fe2015-07-02 17:03:06 +0000331 SwapVector[VecIdx].MentionsPhysVR = 0;
Bill Schmidt15deb802015-07-13 22:58:19 +0000332
333 } else {
334 // We can still handle these by adjusting the form of the XXPERMDI.
335 SwapVector[VecIdx].IsSwappable = 1;
336 SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
Bill Schmidt7c691fe2015-07-02 17:03:06 +0000337 }
Bill Schmidtfe723b92015-04-27 19:57:34 +0000338 break;
Bill Schmidt7c691fe2015-07-02 17:03:06 +0000339 }
Bill Schmidtfe723b92015-04-27 19:57:34 +0000340 case PPC::LVX:
341 // Non-permuting loads are currently unsafe. We can use special
342 // handling for this in the future. By not marking these as
343 // IsSwap, we ensure computations containing them will be rejected
344 // for now.
345 SwapVector[VecIdx].IsLoad = 1;
346 break;
347 case PPC::LXVD2X:
348 case PPC::LXVW4X:
349 // Permuting loads are marked as both load and swap, and are
350 // safe for optimization.
351 SwapVector[VecIdx].IsLoad = 1;
352 SwapVector[VecIdx].IsSwap = 1;
353 break;
Bill Schmidt2be80542015-07-21 21:40:17 +0000354 case PPC::LXSDX:
355 case PPC::LXSSPX:
356 // A load of a floating-point value into the high-order half of
357 // a vector register is safe, provided that we introduce a swap
358 // following the load, which will be done by the SUBREG_TO_REG
359 // support. So just mark these as safe.
360 SwapVector[VecIdx].IsLoad = 1;
361 SwapVector[VecIdx].IsSwappable = 1;
362 break;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000363 case PPC::STVX:
364 // Non-permuting stores are currently unsafe. We can use special
365 // handling for this in the future. By not marking these as
366 // IsSwap, we ensure computations containing them will be rejected
367 // for now.
368 SwapVector[VecIdx].IsStore = 1;
369 break;
370 case PPC::STXVD2X:
371 case PPC::STXVW4X:
372 // Permuting stores are marked as both store and swap, and are
373 // safe for optimization.
374 SwapVector[VecIdx].IsStore = 1;
375 SwapVector[VecIdx].IsSwap = 1;
376 break;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000377 case PPC::COPY:
378 // These are fine provided they are moving between full vector
379 // register classes.
380 if (isVecReg(MI.getOperand(0).getReg()) &&
381 isVecReg(MI.getOperand(1).getReg()))
382 SwapVector[VecIdx].IsSwappable = 1;
Bill Schmidt15deb802015-07-13 22:58:19 +0000383 // If we have a copy from one scalar floating-point register
384 // to another, we can accept this even if it is a physical
385 // register. The only way this gets involved is if it feeds
386 // a SUBREG_TO_REG, which is handled by introducing a swap.
387 else if (isScalarVecReg(MI.getOperand(0).getReg()) &&
388 isScalarVecReg(MI.getOperand(1).getReg()))
389 SwapVector[VecIdx].IsSwappable = 1;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000390 break;
Bill Schmidt15deb802015-07-13 22:58:19 +0000391 case PPC::SUBREG_TO_REG: {
392 // These are fine provided they are moving between full vector
393 // register classes. If they are moving from a scalar
394 // floating-point class to a vector class, we can handle those
395 // as well, provided we introduce a swap. It is generally the
396 // case that we will introduce fewer swaps than we remove, but
397 // (FIXME) a cost model could be used. However, introduced
398 // swaps could potentially be CSEd, so this is not trivial.
399 if (isVecReg(MI.getOperand(0).getReg()) &&
400 isVecReg(MI.getOperand(2).getReg()))
401 SwapVector[VecIdx].IsSwappable = 1;
402 else if (isVecReg(MI.getOperand(0).getReg()) &&
403 isScalarVecReg(MI.getOperand(2).getReg())) {
404 SwapVector[VecIdx].IsSwappable = 1;
Bill Schmidt2be80542015-07-21 21:40:17 +0000405 SwapVector[VecIdx].SpecialHandling = SHValues::SH_COPYWIDEN;
Bill Schmidt15deb802015-07-13 22:58:19 +0000406 }
407 break;
408 }
Bill Schmidtfe723b92015-04-27 19:57:34 +0000409 case PPC::VSPLTB:
410 case PPC::VSPLTH:
411 case PPC::VSPLTW:
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +0000412 case PPC::XXSPLTW:
Bill Schmidtfe723b92015-04-27 19:57:34 +0000413 // Splats are lane-sensitive, but we can use special handling
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +0000414 // to adjust the source lane for the splat.
Bill Schmidt5fe2e252015-05-06 15:40:46 +0000415 SwapVector[VecIdx].IsSwappable = 1;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000416 SwapVector[VecIdx].SpecialHandling = SHValues::SH_SPLAT;
417 break;
418 // The presence of the following lane-sensitive operations in a
419 // web will kill the optimization, at least for now. For these
420 // we do nothing, causing the optimization to fail.
421 // FIXME: Some of these could be permitted with special handling,
422 // and will be phased in as time permits.
423 // FIXME: There is no simple and maintainable way to express a set
424 // of opcodes having a common attribute in TableGen. Should this
425 // change, this is a prime candidate to use such a mechanism.
426 case PPC::INLINEASM:
427 case PPC::EXTRACT_SUBREG:
428 case PPC::INSERT_SUBREG:
429 case PPC::COPY_TO_REGCLASS:
430 case PPC::LVEBX:
431 case PPC::LVEHX:
432 case PPC::LVEWX:
433 case PPC::LVSL:
434 case PPC::LVSR:
435 case PPC::LVXL:
Bill Schmidtfe723b92015-04-27 19:57:34 +0000436 case PPC::STVEBX:
437 case PPC::STVEHX:
438 case PPC::STVEWX:
439 case PPC::STVXL:
Bill Schmidt2be80542015-07-21 21:40:17 +0000440 // We can handle STXSDX and STXSSPX similarly to LXSDX and LXSSPX,
441 // by adding special handling for narrowing copies as well as
442 // widening ones. However, I've experimented with this, and in
443 // practice we currently do not appear to use STXSDX fed by
444 // a narrowing copy from a full vector register. Since I can't
445 // generate any useful test cases, I've left this alone for now.
Bill Schmidtfe723b92015-04-27 19:57:34 +0000446 case PPC::STXSDX:
Bill Schmidt2be80542015-07-21 21:40:17 +0000447 case PPC::STXSSPX:
Bill Schmidtfe723b92015-04-27 19:57:34 +0000448 case PPC::VCIPHER:
449 case PPC::VCIPHERLAST:
450 case PPC::VMRGHB:
451 case PPC::VMRGHH:
452 case PPC::VMRGHW:
453 case PPC::VMRGLB:
454 case PPC::VMRGLH:
455 case PPC::VMRGLW:
456 case PPC::VMULESB:
457 case PPC::VMULESH:
458 case PPC::VMULESW:
459 case PPC::VMULEUB:
460 case PPC::VMULEUH:
461 case PPC::VMULEUW:
462 case PPC::VMULOSB:
463 case PPC::VMULOSH:
464 case PPC::VMULOSW:
465 case PPC::VMULOUB:
466 case PPC::VMULOUH:
467 case PPC::VMULOUW:
468 case PPC::VNCIPHER:
469 case PPC::VNCIPHERLAST:
470 case PPC::VPERM:
471 case PPC::VPERMXOR:
472 case PPC::VPKPX:
473 case PPC::VPKSHSS:
474 case PPC::VPKSHUS:
Bill Schmidt5ed84cd2015-05-16 01:02:12 +0000475 case PPC::VPKSDSS:
476 case PPC::VPKSDUS:
Bill Schmidtfe723b92015-04-27 19:57:34 +0000477 case PPC::VPKSWSS:
478 case PPC::VPKSWUS:
Bill Schmidt5ed84cd2015-05-16 01:02:12 +0000479 case PPC::VPKUDUM:
480 case PPC::VPKUDUS:
Bill Schmidtfe723b92015-04-27 19:57:34 +0000481 case PPC::VPKUHUM:
482 case PPC::VPKUHUS:
483 case PPC::VPKUWUM:
484 case PPC::VPKUWUS:
485 case PPC::VPMSUMB:
486 case PPC::VPMSUMD:
487 case PPC::VPMSUMH:
488 case PPC::VPMSUMW:
489 case PPC::VRLB:
490 case PPC::VRLD:
491 case PPC::VRLH:
492 case PPC::VRLW:
493 case PPC::VSBOX:
494 case PPC::VSHASIGMAD:
495 case PPC::VSHASIGMAW:
496 case PPC::VSL:
497 case PPC::VSLDOI:
498 case PPC::VSLO:
499 case PPC::VSR:
500 case PPC::VSRO:
501 case PPC::VSUM2SWS:
502 case PPC::VSUM4SBS:
503 case PPC::VSUM4SHS:
504 case PPC::VSUM4UBS:
505 case PPC::VSUMSWS:
506 case PPC::VUPKHPX:
507 case PPC::VUPKHSB:
508 case PPC::VUPKHSH:
Bill Schmidt5ed84cd2015-05-16 01:02:12 +0000509 case PPC::VUPKHSW:
Bill Schmidtfe723b92015-04-27 19:57:34 +0000510 case PPC::VUPKLPX:
511 case PPC::VUPKLSB:
512 case PPC::VUPKLSH:
Bill Schmidt5ed84cd2015-05-16 01:02:12 +0000513 case PPC::VUPKLSW:
Bill Schmidtfe723b92015-04-27 19:57:34 +0000514 case PPC::XXMRGHW:
515 case PPC::XXMRGLW:
Bill Schmidt15deb802015-07-13 22:58:19 +0000516 // XXSLDWI could be replaced by a general permute with one of three
517 // permute control vectors (for shift values 1, 2, 3). However,
518 // VPERM has a more restrictive register class.
519 case PPC::XXSLDWI:
Bill Schmidtfe723b92015-04-27 19:57:34 +0000520 break;
521 }
522 }
523 }
524
525 if (RelevantFunction) {
526 DEBUG(dbgs() << "Swap vector when first built\n\n");
Matthias Braun8c209aa2017-01-28 02:02:38 +0000527 DEBUG(dumpSwapVector());
Bill Schmidtfe723b92015-04-27 19:57:34 +0000528 }
529
530 return RelevantFunction;
531}
532
533// Add an entry to the swap vector and swap map, and make a
534// singleton equivalence class for the entry.
535int PPCVSXSwapRemoval::addSwapEntry(MachineInstr *MI,
536 PPCVSXSwapEntry& SwapEntry) {
537 SwapEntry.VSEMI = MI;
538 SwapEntry.VSEId = SwapVector.size();
539 SwapVector.push_back(SwapEntry);
540 EC->insert(SwapEntry.VSEId);
541 SwapMap[MI] = SwapEntry.VSEId;
542 return SwapEntry.VSEId;
543}
544
545// This is used to find the "true" source register for an
546// XXPERMDI instruction, since MachineCSE does not handle the
547// "copy-like" operations (Copy and SubregToReg). Returns
548// the original SrcReg unless it is the target of a copy-like
549// operation, in which case we chain backwards through all
550// such operations to the ultimate source register. If a
551// physical register is encountered, we stop the search and
552// flag the swap entry indicated by VecIdx (the original
Bill Schmidta1c30052015-07-02 19:01:22 +0000553// XXPERMDI) as mentioning a physical register.
Bill Schmidtfe723b92015-04-27 19:57:34 +0000554unsigned PPCVSXSwapRemoval::lookThruCopyLike(unsigned SrcReg,
555 unsigned VecIdx) {
556 MachineInstr *MI = MRI->getVRegDef(SrcReg);
557 if (!MI->isCopyLike())
558 return SrcReg;
559
Bill Schmidta1c30052015-07-02 19:01:22 +0000560 unsigned CopySrcReg;
561 if (MI->isCopy())
Bill Schmidtfe723b92015-04-27 19:57:34 +0000562 CopySrcReg = MI->getOperand(1).getReg();
Bill Schmidta1c30052015-07-02 19:01:22 +0000563 else {
Bill Schmidtfe723b92015-04-27 19:57:34 +0000564 assert(MI->isSubregToReg() && "bad opcode for lookThruCopyLike");
565 CopySrcReg = MI->getOperand(2).getReg();
Bill Schmidtfe723b92015-04-27 19:57:34 +0000566 }
567
568 if (!TargetRegisterInfo::isVirtualRegister(CopySrcReg)) {
Bill Schmidt2be80542015-07-21 21:40:17 +0000569 if (!isScalarVecReg(CopySrcReg))
570 SwapVector[VecIdx].MentionsPhysVR = 1;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000571 return CopySrcReg;
572 }
573
Bill Schmidtfe723b92015-04-27 19:57:34 +0000574 return lookThruCopyLike(CopySrcReg, VecIdx);
575}
576
577// Generate equivalence classes for related computations (webs) by
578// def-use relationships of virtual registers. Mention of a physical
579// register terminates the generation of equivalence classes as this
580// indicates a use of a parameter, definition of a return value, use
581// of a value returned from a call, or definition of a parameter to a
582// call. Computations with physical register mentions are flagged
583// as such so their containing webs will not be optimized.
584void PPCVSXSwapRemoval::formWebs() {
585
586 DEBUG(dbgs() << "\n*** Forming webs for swap removal ***\n\n");
587
588 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
589
590 MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
591
592 DEBUG(dbgs() << "\n" << SwapVector[EntryIdx].VSEId << " ");
593 DEBUG(MI->dump());
594
595 // It's sufficient to walk vector uses and join them to their unique
Bill Schmidt15deb802015-07-13 22:58:19 +0000596 // definitions. In addition, check full vector register operands
597 // for physical regs. We exclude partial-vector register operands
598 // because we can handle them if copied to a full vector.
Bill Schmidtfe723b92015-04-27 19:57:34 +0000599 for (const MachineOperand &MO : MI->operands()) {
600 if (!MO.isReg())
601 continue;
602
603 unsigned Reg = MO.getReg();
Bill Schmidt15deb802015-07-13 22:58:19 +0000604 if (!isVecReg(Reg) && !isScalarVecReg(Reg))
Bill Schmidtfe723b92015-04-27 19:57:34 +0000605 continue;
606
607 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
Bill Schmidt15deb802015-07-13 22:58:19 +0000608 if (!(MI->isCopy() && isScalarVecReg(Reg)))
609 SwapVector[EntryIdx].MentionsPhysVR = 1;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000610 continue;
611 }
612
613 if (!MO.isUse())
614 continue;
615
616 MachineInstr* DefMI = MRI->getVRegDef(Reg);
617 assert(SwapMap.find(DefMI) != SwapMap.end() &&
618 "Inconsistency: def of vector reg not found in swap map!");
619 int DefIdx = SwapMap[DefMI];
620 (void)EC->unionSets(SwapVector[DefIdx].VSEId,
621 SwapVector[EntryIdx].VSEId);
622
623 DEBUG(dbgs() << format("Unioning %d with %d\n", SwapVector[DefIdx].VSEId,
624 SwapVector[EntryIdx].VSEId));
625 DEBUG(dbgs() << " Def: ");
626 DEBUG(DefMI->dump());
627 }
628 }
629}
630
631// Walk the swap vector entries looking for conditions that prevent their
632// containing computations from being optimized. When such conditions are
633// found, mark the representative of the computation's equivalence class
634// as rejected.
635void PPCVSXSwapRemoval::recordUnoptimizableWebs() {
636
637 DEBUG(dbgs() << "\n*** Rejecting webs for swap removal ***\n\n");
638
639 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
640 int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
641
Bill Schmidt15deb802015-07-13 22:58:19 +0000642 // If representative is already rejected, don't waste further time.
643 if (SwapVector[Repr].WebRejected)
644 continue;
645
646 // Reject webs containing mentions of physical or partial registers, or
647 // containing operations that we don't know how to handle in a lane-
648 // permuted region.
Bill Schmidtfe723b92015-04-27 19:57:34 +0000649 if (SwapVector[EntryIdx].MentionsPhysVR ||
Bill Schmidt15deb802015-07-13 22:58:19 +0000650 SwapVector[EntryIdx].MentionsPartialVR ||
Bill Schmidtfe723b92015-04-27 19:57:34 +0000651 !(SwapVector[EntryIdx].IsSwappable || SwapVector[EntryIdx].IsSwap)) {
652
653 SwapVector[Repr].WebRejected = 1;
654
655 DEBUG(dbgs() <<
Bill Schmidt2be80542015-07-21 21:40:17 +0000656 format("Web %d rejected for physreg, partial reg, or not "
657 "swap[pable]\n", Repr));
Bill Schmidtfe723b92015-04-27 19:57:34 +0000658 DEBUG(dbgs() << " in " << EntryIdx << ": ");
659 DEBUG(SwapVector[EntryIdx].VSEMI->dump());
660 DEBUG(dbgs() << "\n");
661 }
662
663 // Reject webs than contain swapping loads that feed something other
664 // than a swap instruction.
665 else if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) {
666 MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
667 unsigned DefReg = MI->getOperand(0).getReg();
668
669 // We skip debug instructions in the analysis. (Note that debug
670 // location information is still maintained by this optimization
671 // because it remains on the LXVD2X and STXVD2X instructions after
672 // the XXPERMDIs are removed.)
673 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) {
674 int UseIdx = SwapMap[&UseMI];
675
676 if (!SwapVector[UseIdx].IsSwap || SwapVector[UseIdx].IsLoad ||
677 SwapVector[UseIdx].IsStore) {
678
679 SwapVector[Repr].WebRejected = 1;
680
681 DEBUG(dbgs() <<
682 format("Web %d rejected for load not feeding swap\n", Repr));
683 DEBUG(dbgs() << " def " << EntryIdx << ": ");
684 DEBUG(MI->dump());
685 DEBUG(dbgs() << " use " << UseIdx << ": ");
686 DEBUG(UseMI.dump());
687 DEBUG(dbgs() << "\n");
688 }
689 }
690
Bill Schmidt15deb802015-07-13 22:58:19 +0000691 // Reject webs that contain swapping stores that are fed by something
Bill Schmidtfe723b92015-04-27 19:57:34 +0000692 // other than a swap instruction.
693 } else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) {
694 MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
695 unsigned UseReg = MI->getOperand(0).getReg();
696 MachineInstr *DefMI = MRI->getVRegDef(UseReg);
Kit Bartonf9d0a402016-07-06 18:03:52 +0000697 unsigned DefReg = DefMI->getOperand(0).getReg();
Bill Schmidtfe723b92015-04-27 19:57:34 +0000698 int DefIdx = SwapMap[DefMI];
699
700 if (!SwapVector[DefIdx].IsSwap || SwapVector[DefIdx].IsLoad ||
701 SwapVector[DefIdx].IsStore) {
702
703 SwapVector[Repr].WebRejected = 1;
704
705 DEBUG(dbgs() <<
706 format("Web %d rejected for store not fed by swap\n", Repr));
707 DEBUG(dbgs() << " def " << DefIdx << ": ");
708 DEBUG(DefMI->dump());
709 DEBUG(dbgs() << " use " << EntryIdx << ": ");
710 DEBUG(MI->dump());
711 DEBUG(dbgs() << "\n");
712 }
Kit Bartonf9d0a402016-07-06 18:03:52 +0000713
714 // Ensure all uses of the register defined by DefMI feed store
715 // instructions
716 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) {
717 int UseIdx = SwapMap[&UseMI];
718
719 if (SwapVector[UseIdx].VSEMI->getOpcode() != MI->getOpcode()) {
720 SwapVector[Repr].WebRejected = 1;
721
722 DEBUG(dbgs() <<
723 format("Web %d rejected for swap not feeding only stores\n",
724 Repr));
725 DEBUG(dbgs() << " def " << " : ");
726 DEBUG(DefMI->dump());
727 DEBUG(dbgs() << " use " << UseIdx << ": ");
728 DEBUG(SwapVector[UseIdx].VSEMI->dump());
729 DEBUG(dbgs() << "\n");
730 }
731 }
Bill Schmidtfe723b92015-04-27 19:57:34 +0000732 }
733 }
734
735 DEBUG(dbgs() << "Swap vector after web analysis:\n\n");
Matthias Braun8c209aa2017-01-28 02:02:38 +0000736 DEBUG(dumpSwapVector());
Bill Schmidtfe723b92015-04-27 19:57:34 +0000737}
738
739// Walk the swap vector entries looking for swaps fed by permuting loads
740// and swaps that feed permuting stores. If the containing computation
741// has not been marked rejected, mark each such swap for removal.
742// (Removal is delayed in case optimization has disturbed the pattern,
743// such that multiple loads feed the same swap, etc.)
744void PPCVSXSwapRemoval::markSwapsForRemoval() {
745
746 DEBUG(dbgs() << "\n*** Marking swaps for removal ***\n\n");
747
748 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
749
750 if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) {
751 int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
752
753 if (!SwapVector[Repr].WebRejected) {
754 MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
755 unsigned DefReg = MI->getOperand(0).getReg();
756
757 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) {
758 int UseIdx = SwapMap[&UseMI];
759 SwapVector[UseIdx].WillRemove = 1;
760
761 DEBUG(dbgs() << "Marking swap fed by load for removal: ");
762 DEBUG(UseMI.dump());
763 }
764 }
765
766 } else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) {
767 int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
768
769 if (!SwapVector[Repr].WebRejected) {
770 MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
771 unsigned UseReg = MI->getOperand(0).getReg();
772 MachineInstr *DefMI = MRI->getVRegDef(UseReg);
773 int DefIdx = SwapMap[DefMI];
774 SwapVector[DefIdx].WillRemove = 1;
775
776 DEBUG(dbgs() << "Marking swap feeding store for removal: ");
777 DEBUG(DefMI->dump());
778 }
779
780 } else if (SwapVector[EntryIdx].IsSwappable &&
Bill Schmidt5fe2e252015-05-06 15:40:46 +0000781 SwapVector[EntryIdx].SpecialHandling != 0) {
782 int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
783
784 if (!SwapVector[Repr].WebRejected)
785 handleSpecialSwappables(EntryIdx);
786 }
Bill Schmidtfe723b92015-04-27 19:57:34 +0000787 }
788}
789
Bill Schmidt2be80542015-07-21 21:40:17 +0000790// Create an xxswapd instruction and insert it prior to the given point.
791// MI is used to determine basic block and debug loc information.
792// FIXME: When inserting a swap, we should check whether SrcReg is
793// defined by another swap: SrcReg = XXPERMDI Reg, Reg, 2; If so,
794// then instead we should generate a copy from Reg to DstReg.
795void PPCVSXSwapRemoval::insertSwap(MachineInstr *MI,
796 MachineBasicBlock::iterator InsertPoint,
797 unsigned DstReg, unsigned SrcReg) {
798 BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(),
799 TII->get(PPC::XXPERMDI), DstReg)
800 .addReg(SrcReg)
801 .addReg(SrcReg)
802 .addImm(2);
803}
804
Bill Schmidtfe723b92015-04-27 19:57:34 +0000805// The identified swap entry requires special handling to allow its
806// containing computation to be optimized. Perform that handling
807// here.
Bill Schmidt15deb802015-07-13 22:58:19 +0000808// FIXME: Additional opportunities will be phased in with subsequent
809// patches.
Bill Schmidtfe723b92015-04-27 19:57:34 +0000810void PPCVSXSwapRemoval::handleSpecialSwappables(int EntryIdx) {
Bill Schmidt5fe2e252015-05-06 15:40:46 +0000811 switch (SwapVector[EntryIdx].SpecialHandling) {
812
813 default:
Benjamin Kramer8ceb3232015-10-25 22:28:27 +0000814 llvm_unreachable("Unexpected special handling type");
Bill Schmidt5fe2e252015-05-06 15:40:46 +0000815
816 // For splats based on an index into a vector, add N/2 modulo N
817 // to the index, where N is the number of vector elements.
818 case SHValues::SH_SPLAT: {
819 MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
820 unsigned NElts;
821
822 DEBUG(dbgs() << "Changing splat: ");
823 DEBUG(MI->dump());
824
825 switch (MI->getOpcode()) {
826 default:
Benjamin Kramer8ceb3232015-10-25 22:28:27 +0000827 llvm_unreachable("Unexpected splat opcode");
Bill Schmidt5fe2e252015-05-06 15:40:46 +0000828 case PPC::VSPLTB: NElts = 16; break;
829 case PPC::VSPLTH: NElts = 8; break;
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +0000830 case PPC::VSPLTW:
831 case PPC::XXSPLTW: NElts = 4; break;
Bill Schmidt5fe2e252015-05-06 15:40:46 +0000832 }
833
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +0000834 unsigned EltNo;
835 if (MI->getOpcode() == PPC::XXSPLTW)
836 EltNo = MI->getOperand(2).getImm();
837 else
838 EltNo = MI->getOperand(1).getImm();
839
Bill Schmidt5fe2e252015-05-06 15:40:46 +0000840 EltNo = (EltNo + NElts / 2) % NElts;
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +0000841 if (MI->getOpcode() == PPC::XXSPLTW)
842 MI->getOperand(2).setImm(EltNo);
843 else
844 MI->getOperand(1).setImm(EltNo);
Bill Schmidt5fe2e252015-05-06 15:40:46 +0000845
846 DEBUG(dbgs() << " Into: ");
847 DEBUG(MI->dump());
848 break;
849 }
850
Bill Schmidt15deb802015-07-13 22:58:19 +0000851 // For an XXPERMDI that isn't handled otherwise, we need to
852 // reverse the order of the operands. If the selector operand
853 // has a value of 0 or 3, we need to change it to 3 or 0,
854 // respectively. Otherwise we should leave it alone. (This
855 // is equivalent to reversing the two bits of the selector
856 // operand and complementing the result.)
857 case SHValues::SH_XXPERMDI: {
858 MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
859
860 DEBUG(dbgs() << "Changing XXPERMDI: ");
861 DEBUG(MI->dump());
862
863 unsigned Selector = MI->getOperand(3).getImm();
864 if (Selector == 0 || Selector == 3)
865 Selector = 3 - Selector;
866 MI->getOperand(3).setImm(Selector);
867
868 unsigned Reg1 = MI->getOperand(1).getReg();
869 unsigned Reg2 = MI->getOperand(2).getReg();
870 MI->getOperand(1).setReg(Reg2);
871 MI->getOperand(2).setReg(Reg1);
872
873 DEBUG(dbgs() << " Into: ");
874 DEBUG(MI->dump());
875 break;
876 }
877
878 // For a copy from a scalar floating-point register to a vector
879 // register, removing swaps will leave the copied value in the
880 // wrong lane. Insert a swap following the copy to fix this.
Bill Schmidt2be80542015-07-21 21:40:17 +0000881 case SHValues::SH_COPYWIDEN: {
Bill Schmidt15deb802015-07-13 22:58:19 +0000882 MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
883
884 DEBUG(dbgs() << "Changing SUBREG_TO_REG: ");
885 DEBUG(MI->dump());
886
887 unsigned DstReg = MI->getOperand(0).getReg();
888 const TargetRegisterClass *DstRC = MRI->getRegClass(DstReg);
889 unsigned NewVReg = MRI->createVirtualRegister(DstRC);
890
891 MI->getOperand(0).setReg(NewVReg);
892 DEBUG(dbgs() << " Into: ");
893 DEBUG(MI->dump());
894
Duncan P. N. Exon Smitha3da4482015-10-08 22:20:37 +0000895 auto InsertPoint = ++MachineBasicBlock::iterator(MI);
Bill Schmidt15deb802015-07-13 22:58:19 +0000896
897 // Note that an XXPERMDI requires a VSRC, so if the SUBREG_TO_REG
898 // is copying to a VRRC, we need to be careful to avoid a register
899 // assignment problem. In this case we must copy from VRRC to VSRC
900 // prior to the swap, and from VSRC to VRRC following the swap.
901 // Coalescing will usually remove all this mess.
Bill Schmidt15deb802015-07-13 22:58:19 +0000902 if (DstRC == &PPC::VRRCRegClass) {
903 unsigned VSRCTmp1 = MRI->createVirtualRegister(&PPC::VSRCRegClass);
904 unsigned VSRCTmp2 = MRI->createVirtualRegister(&PPC::VSRCRegClass);
905
906 BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(),
907 TII->get(PPC::COPY), VSRCTmp1)
908 .addReg(NewVReg);
Duncan P. N. Exon Smitha3da4482015-10-08 22:20:37 +0000909 DEBUG(std::prev(InsertPoint)->dump());
Bill Schmidt15deb802015-07-13 22:58:19 +0000910
Bill Schmidt2be80542015-07-21 21:40:17 +0000911 insertSwap(MI, InsertPoint, VSRCTmp2, VSRCTmp1);
Duncan P. N. Exon Smitha3da4482015-10-08 22:20:37 +0000912 DEBUG(std::prev(InsertPoint)->dump());
Bill Schmidt15deb802015-07-13 22:58:19 +0000913
914 BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(),
915 TII->get(PPC::COPY), DstReg)
916 .addReg(VSRCTmp2);
Duncan P. N. Exon Smitha3da4482015-10-08 22:20:37 +0000917 DEBUG(std::prev(InsertPoint)->dump());
Bill Schmidt15deb802015-07-13 22:58:19 +0000918
919 } else {
Bill Schmidt2be80542015-07-21 21:40:17 +0000920 insertSwap(MI, InsertPoint, DstReg, NewVReg);
Duncan P. N. Exon Smitha3da4482015-10-08 22:20:37 +0000921 DEBUG(std::prev(InsertPoint)->dump());
Bill Schmidt15deb802015-07-13 22:58:19 +0000922 }
923 break;
924 }
Bill Schmidt5fe2e252015-05-06 15:40:46 +0000925 }
Bill Schmidtfe723b92015-04-27 19:57:34 +0000926}
927
928// Walk the swap vector and replace each entry marked for removal with
929// a copy operation.
930bool PPCVSXSwapRemoval::removeSwaps() {
931
932 DEBUG(dbgs() << "\n*** Removing swaps ***\n\n");
933
934 bool Changed = false;
935
936 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
937 if (SwapVector[EntryIdx].WillRemove) {
938 Changed = true;
939 MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
940 MachineBasicBlock *MBB = MI->getParent();
Diana Picus116bbab2017-01-13 09:58:52 +0000941 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(TargetOpcode::COPY),
942 MI->getOperand(0).getReg())
943 .add(MI->getOperand(1));
Bill Schmidtfe723b92015-04-27 19:57:34 +0000944
945 DEBUG(dbgs() << format("Replaced %d with copy: ",
946 SwapVector[EntryIdx].VSEId));
947 DEBUG(MI->dump());
948
949 MI->eraseFromParent();
950 }
951 }
952
953 return Changed;
954}
955
Aaron Ballman615eb472017-10-15 14:32:27 +0000956#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Bill Schmidtfe723b92015-04-27 19:57:34 +0000957// For debug purposes, dump the contents of the swap vector.
Matthias Braun8c209aa2017-01-28 02:02:38 +0000958LLVM_DUMP_METHOD void PPCVSXSwapRemoval::dumpSwapVector() {
Bill Schmidtfe723b92015-04-27 19:57:34 +0000959
960 for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
961
962 MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
963 int ID = SwapVector[EntryIdx].VSEId;
964
Matthias Braun8c209aa2017-01-28 02:02:38 +0000965 dbgs() << format("%6d", ID);
966 dbgs() << format("%6d", EC->getLeaderValue(ID));
967 dbgs() << format(" BB#%3d", MI->getParent()->getNumber());
968 dbgs() << format(" %14s ", TII->getName(MI->getOpcode()).str().c_str());
Bill Schmidtfe723b92015-04-27 19:57:34 +0000969
970 if (SwapVector[EntryIdx].IsLoad)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000971 dbgs() << "load ";
Bill Schmidtfe723b92015-04-27 19:57:34 +0000972 if (SwapVector[EntryIdx].IsStore)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000973 dbgs() << "store ";
Bill Schmidtfe723b92015-04-27 19:57:34 +0000974 if (SwapVector[EntryIdx].IsSwap)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000975 dbgs() << "swap ";
Bill Schmidtfe723b92015-04-27 19:57:34 +0000976 if (SwapVector[EntryIdx].MentionsPhysVR)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000977 dbgs() << "physreg ";
Bill Schmidt15deb802015-07-13 22:58:19 +0000978 if (SwapVector[EntryIdx].MentionsPartialVR)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000979 dbgs() << "partialreg ";
Bill Schmidtfe723b92015-04-27 19:57:34 +0000980
981 if (SwapVector[EntryIdx].IsSwappable) {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000982 dbgs() << "swappable ";
Bill Schmidtfe723b92015-04-27 19:57:34 +0000983 switch(SwapVector[EntryIdx].SpecialHandling) {
984 default:
Matthias Braun8c209aa2017-01-28 02:02:38 +0000985 dbgs() << "special:**unknown**";
Bill Schmidtfe723b92015-04-27 19:57:34 +0000986 break;
987 case SH_NONE:
988 break;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000989 case SH_EXTRACT:
Matthias Braun8c209aa2017-01-28 02:02:38 +0000990 dbgs() << "special:extract ";
Bill Schmidtfe723b92015-04-27 19:57:34 +0000991 break;
992 case SH_INSERT:
Matthias Braun8c209aa2017-01-28 02:02:38 +0000993 dbgs() << "special:insert ";
Bill Schmidtfe723b92015-04-27 19:57:34 +0000994 break;
995 case SH_NOSWAP_LD:
Matthias Braun8c209aa2017-01-28 02:02:38 +0000996 dbgs() << "special:load ";
Bill Schmidtfe723b92015-04-27 19:57:34 +0000997 break;
998 case SH_NOSWAP_ST:
Matthias Braun8c209aa2017-01-28 02:02:38 +0000999 dbgs() << "special:store ";
Bill Schmidtfe723b92015-04-27 19:57:34 +00001000 break;
1001 case SH_SPLAT:
Matthias Braun8c209aa2017-01-28 02:02:38 +00001002 dbgs() << "special:splat ";
Bill Schmidtfe723b92015-04-27 19:57:34 +00001003 break;
Bill Schmidt15deb802015-07-13 22:58:19 +00001004 case SH_XXPERMDI:
Matthias Braun8c209aa2017-01-28 02:02:38 +00001005 dbgs() << "special:xxpermdi ";
Bill Schmidt15deb802015-07-13 22:58:19 +00001006 break;
Bill Schmidt2be80542015-07-21 21:40:17 +00001007 case SH_COPYWIDEN:
Matthias Braun8c209aa2017-01-28 02:02:38 +00001008 dbgs() << "special:copywiden ";
Bill Schmidt15deb802015-07-13 22:58:19 +00001009 break;
Bill Schmidtfe723b92015-04-27 19:57:34 +00001010 }
1011 }
1012
1013 if (SwapVector[EntryIdx].WebRejected)
Matthias Braun8c209aa2017-01-28 02:02:38 +00001014 dbgs() << "rejected ";
Bill Schmidtfe723b92015-04-27 19:57:34 +00001015 if (SwapVector[EntryIdx].WillRemove)
Matthias Braun8c209aa2017-01-28 02:02:38 +00001016 dbgs() << "remove ";
Bill Schmidtfe723b92015-04-27 19:57:34 +00001017
Matthias Braun8c209aa2017-01-28 02:02:38 +00001018 dbgs() << "\n";
Bill Schmidte71db852015-04-27 20:22:35 +00001019
1020 // For no-asserts builds.
1021 (void)MI;
1022 (void)ID;
Bill Schmidtfe723b92015-04-27 19:57:34 +00001023 }
1024
Matthias Braun8c209aa2017-01-28 02:02:38 +00001025 dbgs() << "\n";
Bill Schmidtfe723b92015-04-27 19:57:34 +00001026}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001027#endif
Bill Schmidtfe723b92015-04-27 19:57:34 +00001028
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001029} // end default namespace
Bill Schmidtfe723b92015-04-27 19:57:34 +00001030
1031INITIALIZE_PASS_BEGIN(PPCVSXSwapRemoval, DEBUG_TYPE,
1032 "PowerPC VSX Swap Removal", false, false)
1033INITIALIZE_PASS_END(PPCVSXSwapRemoval, DEBUG_TYPE,
1034 "PowerPC VSX Swap Removal", false, false)
1035
1036char PPCVSXSwapRemoval::ID = 0;
1037FunctionPass*
1038llvm::createPPCVSXSwapRemovalPass() { return new PPCVSXSwapRemoval(); }