1fe723b9aSBill Schmidt //===----------- PPCVSXSwapRemoval.cpp - Remove VSX LE Swaps -------------===//
2fe723b9aSBill Schmidt //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe723b9aSBill Schmidt //
7fe723b9aSBill Schmidt //===---------------------------------------------------------------------===//
8fe723b9aSBill Schmidt //
9fe723b9aSBill Schmidt // This pass analyzes vector computations and removes unnecessary
10fe723b9aSBill Schmidt // doubleword swaps (xxswapd instructions). This pass is performed
11fe723b9aSBill Schmidt // only for little-endian VSX code generation.
12fe723b9aSBill Schmidt //
13fe723b9aSBill Schmidt // For this specific case, loads and stores of v4i32, v4f32, v2i64,
14fe723b9aSBill Schmidt // and v2f64 vectors are inefficient. These are implemented using
15fe723b9aSBill Schmidt // the lxvd2x and stxvd2x instructions, which invert the order of
16fe723b9aSBill Schmidt // doublewords in a vector register. Thus code generation inserts
17fe723b9aSBill Schmidt // an xxswapd after each such load, and prior to each such store.
18fe723b9aSBill Schmidt //
19fe723b9aSBill Schmidt // The extra xxswapd instructions reduce performance. The purpose
20fe723b9aSBill Schmidt // of this pass is to reduce the number of xxswapd instructions
21fe723b9aSBill Schmidt // required for correctness.
22fe723b9aSBill Schmidt //
23fe723b9aSBill Schmidt // The primary insight is that much code that operates on vectors
24fe723b9aSBill Schmidt // does not care about the relative order of elements in a register,
25fe723b9aSBill Schmidt // so long as the correct memory order is preserved. If we have a
26fe723b9aSBill Schmidt // computation where all input values are provided by lxvd2x/xxswapd,
27fe723b9aSBill Schmidt // all outputs are stored using xxswapd/lxvd2x, and all intermediate
28fe723b9aSBill Schmidt // computations are lane-insensitive (independent of element order),
29fe723b9aSBill Schmidt // then all the xxswapd instructions associated with the loads and
30fe723b9aSBill Schmidt // stores may be removed without changing observable semantics.
31fe723b9aSBill Schmidt //
32fe723b9aSBill Schmidt // This pass uses standard equivalence class infrastructure to create
33fe723b9aSBill Schmidt // maximal webs of computations fitting the above description. Each
34fe723b9aSBill Schmidt // such web is then optimized by removing its unnecessary xxswapd
35fe723b9aSBill Schmidt // instructions.
36fe723b9aSBill Schmidt //
37fe723b9aSBill Schmidt // There are some lane-sensitive operations for which we can still
38fe723b9aSBill Schmidt // permit the optimization, provided we modify those operations
39fe723b9aSBill Schmidt // accordingly. Such operations are identified as using "special
40fe723b9aSBill Schmidt // handling" within this module.
41fe723b9aSBill Schmidt //
42fe723b9aSBill Schmidt //===---------------------------------------------------------------------===//
43fe723b9aSBill Schmidt
44fe723b9aSBill Schmidt #include "PPC.h"
45fe723b9aSBill Schmidt #include "PPCInstrBuilder.h"
466bda14b3SChandler Carruth #include "PPCInstrInfo.h"
47fe723b9aSBill Schmidt #include "PPCTargetMachine.h"
48fe723b9aSBill Schmidt #include "llvm/ADT/DenseMap.h"
49fe723b9aSBill Schmidt #include "llvm/ADT/EquivalenceClasses.h"
50fe723b9aSBill Schmidt #include "llvm/CodeGen/MachineFunctionPass.h"
51fe723b9aSBill Schmidt #include "llvm/CodeGen/MachineInstrBuilder.h"
52fe723b9aSBill Schmidt #include "llvm/CodeGen/MachineRegisterInfo.h"
53432a3883SNico Weber #include "llvm/Config/llvm-config.h"
54fe723b9aSBill Schmidt #include "llvm/Support/Debug.h"
55fe723b9aSBill Schmidt #include "llvm/Support/Format.h"
56fe723b9aSBill Schmidt #include "llvm/Support/raw_ostream.h"
57fe723b9aSBill Schmidt
58fe723b9aSBill Schmidt using namespace llvm;
59fe723b9aSBill Schmidt
60fe723b9aSBill Schmidt #define DEBUG_TYPE "ppc-vsx-swaps"
61fe723b9aSBill Schmidt
62fe723b9aSBill Schmidt namespace {
63fe723b9aSBill Schmidt
64fe723b9aSBill Schmidt // A PPCVSXSwapEntry is created for each machine instruction that
65fe723b9aSBill Schmidt // is relevant to a vector computation.
66fe723b9aSBill Schmidt struct PPCVSXSwapEntry {
67fe723b9aSBill Schmidt // Pointer to the instruction.
68fe723b9aSBill Schmidt MachineInstr *VSEMI;
69fe723b9aSBill Schmidt
70fe723b9aSBill Schmidt // Unique ID (position in the swap vector).
71fe723b9aSBill Schmidt int VSEId;
72fe723b9aSBill Schmidt
73fe723b9aSBill Schmidt // Attributes of this node.
74fe723b9aSBill Schmidt unsigned int IsLoad : 1;
75fe723b9aSBill Schmidt unsigned int IsStore : 1;
76fe723b9aSBill Schmidt unsigned int IsSwap : 1;
77fe723b9aSBill Schmidt unsigned int MentionsPhysVR : 1;
78fe723b9aSBill Schmidt unsigned int IsSwappable : 1;
7915deb803SBill Schmidt unsigned int MentionsPartialVR : 1;
80fe723b9aSBill Schmidt unsigned int SpecialHandling : 3;
81fe723b9aSBill Schmidt unsigned int WebRejected : 1;
82fe723b9aSBill Schmidt unsigned int WillRemove : 1;
83fe723b9aSBill Schmidt };
84fe723b9aSBill Schmidt
85fe723b9aSBill Schmidt enum SHValues {
86fe723b9aSBill Schmidt SH_NONE = 0,
87fe723b9aSBill Schmidt SH_EXTRACT,
88fe723b9aSBill Schmidt SH_INSERT,
89fe723b9aSBill Schmidt SH_NOSWAP_LD,
90fe723b9aSBill Schmidt SH_NOSWAP_ST,
9115deb803SBill Schmidt SH_SPLAT,
9215deb803SBill Schmidt SH_XXPERMDI,
932be8054bSBill Schmidt SH_COPYWIDEN
94fe723b9aSBill Schmidt };
95fe723b9aSBill Schmidt
96fe723b9aSBill Schmidt struct PPCVSXSwapRemoval : public MachineFunctionPass {
97fe723b9aSBill Schmidt
98fe723b9aSBill Schmidt static char ID;
99fe723b9aSBill Schmidt const PPCInstrInfo *TII;
100fe723b9aSBill Schmidt MachineFunction *MF;
101fe723b9aSBill Schmidt MachineRegisterInfo *MRI;
102fe723b9aSBill Schmidt
103fe723b9aSBill Schmidt // Swap entries are allocated in a vector for better performance.
104fe723b9aSBill Schmidt std::vector<PPCVSXSwapEntry> SwapVector;
105fe723b9aSBill Schmidt
106fe723b9aSBill Schmidt // A mapping is maintained between machine instructions and
107fe723b9aSBill Schmidt // their swap entries. The key is the address of the MI.
108fe723b9aSBill Schmidt DenseMap<MachineInstr*, int> SwapMap;
109fe723b9aSBill Schmidt
110fe723b9aSBill Schmidt // Equivalence classes are used to gather webs of related computation.
111fe723b9aSBill Schmidt // Swap entries are represented by their VSEId fields.
112fe723b9aSBill Schmidt EquivalenceClasses<int> *EC;
113fe723b9aSBill Schmidt
PPCVSXSwapRemoval__anonf5e3183a0111::PPCVSXSwapRemoval114fe723b9aSBill Schmidt PPCVSXSwapRemoval() : MachineFunctionPass(ID) {
115fe723b9aSBill Schmidt initializePPCVSXSwapRemovalPass(*PassRegistry::getPassRegistry());
116fe723b9aSBill Schmidt }
117fe723b9aSBill Schmidt
118fe723b9aSBill Schmidt private:
119fe723b9aSBill Schmidt // Initialize data structures.
120fe723b9aSBill Schmidt void initialize(MachineFunction &MFParm);
121fe723b9aSBill Schmidt
122fe723b9aSBill Schmidt // Walk the machine instructions to gather vector usage information.
123fe723b9aSBill Schmidt // Return true iff vector mentions are present.
124fe723b9aSBill Schmidt bool gatherVectorInstructions();
125fe723b9aSBill Schmidt
126fe723b9aSBill Schmidt // Add an entry to the swap vector and swap map.
127fe723b9aSBill Schmidt int addSwapEntry(MachineInstr *MI, PPCVSXSwapEntry &SwapEntry);
128fe723b9aSBill Schmidt
129fe723b9aSBill Schmidt // Hunt backwards through COPY and SUBREG_TO_REG chains for a
130fe723b9aSBill Schmidt // source register. VecIdx indicates the swap vector entry to
131fe723b9aSBill Schmidt // mark as mentioning a physical register if the search leads
132fe723b9aSBill Schmidt // to one.
133fe723b9aSBill Schmidt unsigned lookThruCopyLike(unsigned SrcReg, unsigned VecIdx);
134fe723b9aSBill Schmidt
135fe723b9aSBill Schmidt // Generate equivalence classes for related computations (webs).
136fe723b9aSBill Schmidt void formWebs();
137fe723b9aSBill Schmidt
138fe723b9aSBill Schmidt // Analyze webs and determine those that cannot be optimized.
139fe723b9aSBill Schmidt void recordUnoptimizableWebs();
140fe723b9aSBill Schmidt
141fe723b9aSBill Schmidt // Record which swap instructions can be safely removed.
142fe723b9aSBill Schmidt void markSwapsForRemoval();
143fe723b9aSBill Schmidt
144fe723b9aSBill Schmidt // Remove swaps and update other instructions requiring special
145fe723b9aSBill Schmidt // handling. Return true iff any changes are made.
146fe723b9aSBill Schmidt bool removeSwaps();
147fe723b9aSBill Schmidt
1482be8054bSBill Schmidt // Insert a swap instruction from SrcReg to DstReg at the given
1492be8054bSBill Schmidt // InsertPoint.
1502be8054bSBill Schmidt void insertSwap(MachineInstr *MI, MachineBasicBlock::iterator InsertPoint,
1512be8054bSBill Schmidt unsigned DstReg, unsigned SrcReg);
1522be8054bSBill Schmidt
153fe723b9aSBill Schmidt // Update instructions requiring special handling.
154fe723b9aSBill Schmidt void handleSpecialSwappables(int EntryIdx);
155fe723b9aSBill Schmidt
156fe723b9aSBill Schmidt // Dump a description of the entries in the swap vector.
157fe723b9aSBill Schmidt void dumpSwapVector();
158fe723b9aSBill Schmidt
159fe723b9aSBill Schmidt // Return true iff the given register is in the given class.
isRegInClass__anonf5e3183a0111::PPCVSXSwapRemoval160fe723b9aSBill Schmidt bool isRegInClass(unsigned Reg, const TargetRegisterClass *RC) {
1612bea69bfSDaniel Sanders if (Register::isVirtualRegister(Reg))
162fe723b9aSBill Schmidt return RC->hasSubClassEq(MRI->getRegClass(Reg));
163175a7cbfSAlexander Kornienko return RC->contains(Reg);
164fe723b9aSBill Schmidt }
165fe723b9aSBill Schmidt
166fe723b9aSBill Schmidt // Return true iff the given register is a full vector register.
isVecReg__anonf5e3183a0111::PPCVSXSwapRemoval167fe723b9aSBill Schmidt bool isVecReg(unsigned Reg) {
168fe723b9aSBill Schmidt return (isRegInClass(Reg, &PPC::VSRCRegClass) ||
169fe723b9aSBill Schmidt isRegInClass(Reg, &PPC::VRRCRegClass));
170fe723b9aSBill Schmidt }
171fe723b9aSBill Schmidt
17215deb803SBill Schmidt // Return true iff the given register is a partial vector register.
isScalarVecReg__anonf5e3183a0111::PPCVSXSwapRemoval17315deb803SBill Schmidt bool isScalarVecReg(unsigned Reg) {
17415deb803SBill Schmidt return (isRegInClass(Reg, &PPC::VSFRCRegClass) ||
17515deb803SBill Schmidt isRegInClass(Reg, &PPC::VSSRCRegClass));
17615deb803SBill Schmidt }
17715deb803SBill Schmidt
17815deb803SBill Schmidt // Return true iff the given register mentions all or part of a
17915deb803SBill Schmidt // vector register. Also sets Partial to true if the mention
18015deb803SBill Schmidt // is for just the floating-point register overlap of the register.
isAnyVecReg__anonf5e3183a0111::PPCVSXSwapRemoval18115deb803SBill Schmidt bool isAnyVecReg(unsigned Reg, bool &Partial) {
18215deb803SBill Schmidt if (isScalarVecReg(Reg))
18315deb803SBill Schmidt Partial = true;
18415deb803SBill Schmidt return isScalarVecReg(Reg) || isVecReg(Reg);
18515deb803SBill Schmidt }
18615deb803SBill Schmidt
187fe723b9aSBill Schmidt public:
188fe723b9aSBill Schmidt // Main entry point for this pass.
runOnMachineFunction__anonf5e3183a0111::PPCVSXSwapRemoval189fe723b9aSBill Schmidt bool runOnMachineFunction(MachineFunction &MF) override {
190f1caa283SMatthias Braun if (skipFunction(MF.getFunction()))
191289bd5f6SAndrew Kaylor return false;
192289bd5f6SAndrew Kaylor
193fe723b9aSBill Schmidt // If we don't have VSX on the subtarget, don't do anything.
1943cd1a036SSean Fertile // Also, on Power 9 the load and store ops preserve element order and so
1953cd1a036SSean Fertile // the swaps are not required.
196fe723b9aSBill Schmidt const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
1973cd1a036SSean Fertile if (!STI.hasVSX() || !STI.needsSwapsForVSXMemOps())
198fe723b9aSBill Schmidt return false;
199fe723b9aSBill Schmidt
200fe723b9aSBill Schmidt bool Changed = false;
201fe723b9aSBill Schmidt initialize(MF);
202fe723b9aSBill Schmidt
203fe723b9aSBill Schmidt if (gatherVectorInstructions()) {
204fe723b9aSBill Schmidt formWebs();
205fe723b9aSBill Schmidt recordUnoptimizableWebs();
206fe723b9aSBill Schmidt markSwapsForRemoval();
207fe723b9aSBill Schmidt Changed = removeSwaps();
208fe723b9aSBill Schmidt }
209fe723b9aSBill Schmidt
210fe723b9aSBill Schmidt // FIXME: See the allocation of EC in initialize().
211fe723b9aSBill Schmidt delete EC;
212fe723b9aSBill Schmidt return Changed;
213fe723b9aSBill Schmidt }
214fe723b9aSBill Schmidt };
215fe723b9aSBill Schmidt
216fe723b9aSBill Schmidt // Initialize data structures for this pass. In particular, clear the
217fe723b9aSBill Schmidt // swap vector and allocate the equivalence class mapping before
218fe723b9aSBill Schmidt // processing each function.
initialize(MachineFunction & MFParm)219fe723b9aSBill Schmidt void PPCVSXSwapRemoval::initialize(MachineFunction &MFParm) {
220fe723b9aSBill Schmidt MF = &MFParm;
221fe723b9aSBill Schmidt MRI = &MF->getRegInfo();
2228ed7cec1SBill Schmidt TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo();
223fe723b9aSBill Schmidt
224fe723b9aSBill Schmidt // An initial vector size of 256 appears to work well in practice.
225fe723b9aSBill Schmidt // Small/medium functions with vector content tend not to incur a
226fe723b9aSBill Schmidt // reallocation at this size. Three of the vector tests in
227fe723b9aSBill Schmidt // projects/test-suite reallocate, which seems like a reasonable rate.
228fe723b9aSBill Schmidt const int InitialVectorSize(256);
229fe723b9aSBill Schmidt SwapVector.clear();
230fe723b9aSBill Schmidt SwapVector.reserve(InitialVectorSize);
231fe723b9aSBill Schmidt
232fe723b9aSBill Schmidt // FIXME: Currently we allocate EC each time because we don't have
233fe723b9aSBill Schmidt // access to the set representation on which to call clear(). Should
234fe723b9aSBill Schmidt // consider adding a clear() method to the EquivalenceClasses class.
235fe723b9aSBill Schmidt EC = new EquivalenceClasses<int>;
236fe723b9aSBill Schmidt }
237fe723b9aSBill Schmidt
238fe723b9aSBill Schmidt // Create an entry in the swap vector for each instruction that mentions
239fe723b9aSBill Schmidt // a full vector register, recording various characteristics of the
240fe723b9aSBill Schmidt // instructions there.
gatherVectorInstructions()241fe723b9aSBill Schmidt bool PPCVSXSwapRemoval::gatherVectorInstructions() {
242fe723b9aSBill Schmidt bool RelevantFunction = false;
243fe723b9aSBill Schmidt
244fe723b9aSBill Schmidt for (MachineBasicBlock &MBB : *MF) {
245fe723b9aSBill Schmidt for (MachineInstr &MI : MBB) {
246fe723b9aSBill Schmidt
247801bf7ebSShiva Chen if (MI.isDebugInstr())
24832fd189dSBill Schmidt continue;
24932fd189dSBill Schmidt
250fe723b9aSBill Schmidt bool RelevantInstr = false;
25115deb803SBill Schmidt bool Partial = false;
252fe723b9aSBill Schmidt
253fe723b9aSBill Schmidt for (const MachineOperand &MO : MI.operands()) {
254fe723b9aSBill Schmidt if (!MO.isReg())
255fe723b9aSBill Schmidt continue;
2560c476111SDaniel Sanders Register Reg = MO.getReg();
2577486de1bSNemanja Ivanovic // All operands need to be checked because there are instructions that
2587486de1bSNemanja Ivanovic // operate on a partial register and produce a full register (such as
2597486de1bSNemanja Ivanovic // XXPERMDIs).
2607486de1bSNemanja Ivanovic if (isAnyVecReg(Reg, Partial))
261fe723b9aSBill Schmidt RelevantInstr = true;
262fe723b9aSBill Schmidt }
263fe723b9aSBill Schmidt
264fe723b9aSBill Schmidt if (!RelevantInstr)
265fe723b9aSBill Schmidt continue;
266fe723b9aSBill Schmidt
267fe723b9aSBill Schmidt RelevantFunction = true;
268fe723b9aSBill Schmidt
269fe723b9aSBill Schmidt // Create a SwapEntry initialized to zeros, then fill in the
270fe723b9aSBill Schmidt // instruction and ID fields before pushing it to the back
271fe723b9aSBill Schmidt // of the swap vector.
272fe723b9aSBill Schmidt PPCVSXSwapEntry SwapEntry{};
273fe723b9aSBill Schmidt int VecIdx = addSwapEntry(&MI, SwapEntry);
274fe723b9aSBill Schmidt
275fe723b9aSBill Schmidt switch(MI.getOpcode()) {
276fe723b9aSBill Schmidt default:
277fe723b9aSBill Schmidt // Unless noted otherwise, an instruction is considered
278fe723b9aSBill Schmidt // safe for the optimization. There are a large number of
279fe723b9aSBill Schmidt // such true-SIMD instructions (all vector math, logical,
28015deb803SBill Schmidt // select, compare, etc.). However, if the instruction
28115deb803SBill Schmidt // mentions a partial vector register and does not have
28215deb803SBill Schmidt // special handling defined, it is not swappable.
28315deb803SBill Schmidt if (Partial)
28415deb803SBill Schmidt SwapVector[VecIdx].MentionsPartialVR = 1;
28515deb803SBill Schmidt else
286fe723b9aSBill Schmidt SwapVector[VecIdx].IsSwappable = 1;
287fe723b9aSBill Schmidt break;
2887c691feeSBill Schmidt case PPC::XXPERMDI: {
289fe723b9aSBill Schmidt // This is a swap if it is of the form XXPERMDI t, s, s, 2.
290fe723b9aSBill Schmidt // Unfortunately, MachineCSE ignores COPY and SUBREG_TO_REG, so we
291fe723b9aSBill Schmidt // can also see XXPERMDI t, SUBREG_TO_REG(s), SUBREG_TO_REG(s), 2,
292fe723b9aSBill Schmidt // for example. We have to look through chains of COPY and
293fe723b9aSBill Schmidt // SUBREG_TO_REG to find the real source value for comparison.
294fe723b9aSBill Schmidt // If the real source value is a physical register, then mark the
295fe723b9aSBill Schmidt // XXPERMDI as mentioning a physical register.
2967c691feeSBill Schmidt int immed = MI.getOperand(3).getImm();
2977c691feeSBill Schmidt if (immed == 2) {
298fe723b9aSBill Schmidt unsigned trueReg1 = lookThruCopyLike(MI.getOperand(1).getReg(),
299fe723b9aSBill Schmidt VecIdx);
300fe723b9aSBill Schmidt unsigned trueReg2 = lookThruCopyLike(MI.getOperand(2).getReg(),
301fe723b9aSBill Schmidt VecIdx);
302fe723b9aSBill Schmidt if (trueReg1 == trueReg2)
303fe723b9aSBill Schmidt SwapVector[VecIdx].IsSwap = 1;
30415deb803SBill Schmidt else {
30515deb803SBill Schmidt // We can still handle these if the two registers are not
30615deb803SBill Schmidt // identical, by adjusting the form of the XXPERMDI.
30715deb803SBill Schmidt SwapVector[VecIdx].IsSwappable = 1;
30815deb803SBill Schmidt SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
309fe723b9aSBill Schmidt }
3107c691feeSBill Schmidt // This is a doubleword splat if it is of the form
3117c691feeSBill Schmidt // XXPERMDI t, s, s, 0 or XXPERMDI t, s, s, 3. As above we
3127c691feeSBill Schmidt // must look through chains of copy-likes to find the source
3137c691feeSBill Schmidt // register. We turn off the marking for mention of a physical
3147c691feeSBill Schmidt // register, because splatting it is safe; the optimization
31515deb803SBill Schmidt // will not swap the value in the physical register. Whether
31615deb803SBill Schmidt // or not the two input registers are identical, we can handle
31715deb803SBill Schmidt // these by adjusting the form of the XXPERMDI.
31815deb803SBill Schmidt } else if (immed == 0 || immed == 3) {
31915deb803SBill Schmidt
32015deb803SBill Schmidt SwapVector[VecIdx].IsSwappable = 1;
32115deb803SBill Schmidt SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
32215deb803SBill Schmidt
3237c691feeSBill Schmidt unsigned trueReg1 = lookThruCopyLike(MI.getOperand(1).getReg(),
3247c691feeSBill Schmidt VecIdx);
3257c691feeSBill Schmidt unsigned trueReg2 = lookThruCopyLike(MI.getOperand(2).getReg(),
3267c691feeSBill Schmidt VecIdx);
32715deb803SBill Schmidt if (trueReg1 == trueReg2)
3287c691feeSBill Schmidt SwapVector[VecIdx].MentionsPhysVR = 0;
32915deb803SBill Schmidt
33015deb803SBill Schmidt } else {
33115deb803SBill Schmidt // We can still handle these by adjusting the form of the XXPERMDI.
33215deb803SBill Schmidt SwapVector[VecIdx].IsSwappable = 1;
33315deb803SBill Schmidt SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
3347c691feeSBill Schmidt }
335fe723b9aSBill Schmidt break;
3367c691feeSBill Schmidt }
337fe723b9aSBill Schmidt case PPC::LVX:
338fe723b9aSBill Schmidt // Non-permuting loads are currently unsafe. We can use special
339fe723b9aSBill Schmidt // handling for this in the future. By not marking these as
340fe723b9aSBill Schmidt // IsSwap, we ensure computations containing them will be rejected
341fe723b9aSBill Schmidt // for now.
342fe723b9aSBill Schmidt SwapVector[VecIdx].IsLoad = 1;
343fe723b9aSBill Schmidt break;
344fe723b9aSBill Schmidt case PPC::LXVD2X:
345fe723b9aSBill Schmidt case PPC::LXVW4X:
346fe723b9aSBill Schmidt // Permuting loads are marked as both load and swap, and are
347fe723b9aSBill Schmidt // safe for optimization.
348fe723b9aSBill Schmidt SwapVector[VecIdx].IsLoad = 1;
349fe723b9aSBill Schmidt SwapVector[VecIdx].IsSwap = 1;
350fe723b9aSBill Schmidt break;
3512be8054bSBill Schmidt case PPC::LXSDX:
3522be8054bSBill Schmidt case PPC::LXSSPX:
353438bf4a6STony Jiang case PPC::XFLOADf64:
354438bf4a6STony Jiang case PPC::XFLOADf32:
3552be8054bSBill Schmidt // A load of a floating-point value into the high-order half of
3562be8054bSBill Schmidt // a vector register is safe, provided that we introduce a swap
3572be8054bSBill Schmidt // following the load, which will be done by the SUBREG_TO_REG
3582be8054bSBill Schmidt // support. So just mark these as safe.
3592be8054bSBill Schmidt SwapVector[VecIdx].IsLoad = 1;
3602be8054bSBill Schmidt SwapVector[VecIdx].IsSwappable = 1;
3612be8054bSBill Schmidt break;
362fe723b9aSBill Schmidt case PPC::STVX:
363fe723b9aSBill Schmidt // Non-permuting stores are currently unsafe. We can use special
364fe723b9aSBill Schmidt // handling for this in the future. By not marking these as
365fe723b9aSBill Schmidt // IsSwap, we ensure computations containing them will be rejected
366fe723b9aSBill Schmidt // for now.
367fe723b9aSBill Schmidt SwapVector[VecIdx].IsStore = 1;
368fe723b9aSBill Schmidt break;
369fe723b9aSBill Schmidt case PPC::STXVD2X:
370fe723b9aSBill Schmidt case PPC::STXVW4X:
371fe723b9aSBill Schmidt // Permuting stores are marked as both store and swap, and are
372fe723b9aSBill Schmidt // safe for optimization.
373fe723b9aSBill Schmidt SwapVector[VecIdx].IsStore = 1;
374fe723b9aSBill Schmidt SwapVector[VecIdx].IsSwap = 1;
375fe723b9aSBill Schmidt break;
376fe723b9aSBill Schmidt case PPC::COPY:
377fe723b9aSBill Schmidt // These are fine provided they are moving between full vector
378fe723b9aSBill Schmidt // register classes.
379fe723b9aSBill Schmidt if (isVecReg(MI.getOperand(0).getReg()) &&
380fe723b9aSBill Schmidt isVecReg(MI.getOperand(1).getReg()))
381fe723b9aSBill Schmidt SwapVector[VecIdx].IsSwappable = 1;
38215deb803SBill Schmidt // If we have a copy from one scalar floating-point register
38315deb803SBill Schmidt // to another, we can accept this even if it is a physical
38415deb803SBill Schmidt // register. The only way this gets involved is if it feeds
38515deb803SBill Schmidt // a SUBREG_TO_REG, which is handled by introducing a swap.
38615deb803SBill Schmidt else if (isScalarVecReg(MI.getOperand(0).getReg()) &&
38715deb803SBill Schmidt isScalarVecReg(MI.getOperand(1).getReg()))
38815deb803SBill Schmidt SwapVector[VecIdx].IsSwappable = 1;
389fe723b9aSBill Schmidt break;
39015deb803SBill Schmidt case PPC::SUBREG_TO_REG: {
39115deb803SBill Schmidt // These are fine provided they are moving between full vector
39215deb803SBill Schmidt // register classes. If they are moving from a scalar
39315deb803SBill Schmidt // floating-point class to a vector class, we can handle those
39415deb803SBill Schmidt // as well, provided we introduce a swap. It is generally the
39515deb803SBill Schmidt // case that we will introduce fewer swaps than we remove, but
39615deb803SBill Schmidt // (FIXME) a cost model could be used. However, introduced
39715deb803SBill Schmidt // swaps could potentially be CSEd, so this is not trivial.
39815deb803SBill Schmidt if (isVecReg(MI.getOperand(0).getReg()) &&
39915deb803SBill Schmidt isVecReg(MI.getOperand(2).getReg()))
40015deb803SBill Schmidt SwapVector[VecIdx].IsSwappable = 1;
40115deb803SBill Schmidt else if (isVecReg(MI.getOperand(0).getReg()) &&
40215deb803SBill Schmidt isScalarVecReg(MI.getOperand(2).getReg())) {
40315deb803SBill Schmidt SwapVector[VecIdx].IsSwappable = 1;
4042be8054bSBill Schmidt SwapVector[VecIdx].SpecialHandling = SHValues::SH_COPYWIDEN;
40515deb803SBill Schmidt }
40615deb803SBill Schmidt break;
40715deb803SBill Schmidt }
408fe723b9aSBill Schmidt case PPC::VSPLTB:
409fe723b9aSBill Schmidt case PPC::VSPLTH:
410fe723b9aSBill Schmidt case PPC::VSPLTW:
4111a2b2f03SNemanja Ivanovic case PPC::XXSPLTW:
412fe723b9aSBill Schmidt // Splats are lane-sensitive, but we can use special handling
4131a2b2f03SNemanja Ivanovic // to adjust the source lane for the splat.
4145fe2e25fSBill Schmidt SwapVector[VecIdx].IsSwappable = 1;
415fe723b9aSBill Schmidt SwapVector[VecIdx].SpecialHandling = SHValues::SH_SPLAT;
416fe723b9aSBill Schmidt break;
417fe723b9aSBill Schmidt // The presence of the following lane-sensitive operations in a
418fe723b9aSBill Schmidt // web will kill the optimization, at least for now. For these
419fe723b9aSBill Schmidt // we do nothing, causing the optimization to fail.
420fe723b9aSBill Schmidt // FIXME: Some of these could be permitted with special handling,
421fe723b9aSBill Schmidt // and will be phased in as time permits.
422fe723b9aSBill Schmidt // FIXME: There is no simple and maintainable way to express a set
423fe723b9aSBill Schmidt // of opcodes having a common attribute in TableGen. Should this
424fe723b9aSBill Schmidt // change, this is a prime candidate to use such a mechanism.
425fe723b9aSBill Schmidt case PPC::INLINEASM:
4267fcad2f1SNick Desaulniers case PPC::INLINEASM_BR:
427fe723b9aSBill Schmidt case PPC::EXTRACT_SUBREG:
428fe723b9aSBill Schmidt case PPC::INSERT_SUBREG:
429fe723b9aSBill Schmidt case PPC::COPY_TO_REGCLASS:
430fe723b9aSBill Schmidt case PPC::LVEBX:
431fe723b9aSBill Schmidt case PPC::LVEHX:
432fe723b9aSBill Schmidt case PPC::LVEWX:
433fe723b9aSBill Schmidt case PPC::LVSL:
434fe723b9aSBill Schmidt case PPC::LVSR:
435fe723b9aSBill Schmidt case PPC::LVXL:
436fe723b9aSBill Schmidt case PPC::STVEBX:
437fe723b9aSBill Schmidt case PPC::STVEHX:
438fe723b9aSBill Schmidt case PPC::STVEWX:
439fe723b9aSBill Schmidt case PPC::STVXL:
4402be8054bSBill Schmidt // We can handle STXSDX and STXSSPX similarly to LXSDX and LXSSPX,
4412be8054bSBill Schmidt // by adding special handling for narrowing copies as well as
4422be8054bSBill Schmidt // widening ones. However, I've experimented with this, and in
4432be8054bSBill Schmidt // practice we currently do not appear to use STXSDX fed by
4442be8054bSBill Schmidt // a narrowing copy from a full vector register. Since I can't
4452be8054bSBill Schmidt // generate any useful test cases, I've left this alone for now.
446fe723b9aSBill Schmidt case PPC::STXSDX:
4472be8054bSBill Schmidt case PPC::STXSSPX:
448fe723b9aSBill Schmidt case PPC::VCIPHER:
449fe723b9aSBill Schmidt case PPC::VCIPHERLAST:
450fe723b9aSBill Schmidt case PPC::VMRGHB:
451fe723b9aSBill Schmidt case PPC::VMRGHH:
452fe723b9aSBill Schmidt case PPC::VMRGHW:
453fe723b9aSBill Schmidt case PPC::VMRGLB:
454fe723b9aSBill Schmidt case PPC::VMRGLH:
455fe723b9aSBill Schmidt case PPC::VMRGLW:
456fe723b9aSBill Schmidt case PPC::VMULESB:
457fe723b9aSBill Schmidt case PPC::VMULESH:
458fe723b9aSBill Schmidt case PPC::VMULESW:
459fe723b9aSBill Schmidt case PPC::VMULEUB:
460fe723b9aSBill Schmidt case PPC::VMULEUH:
461fe723b9aSBill Schmidt case PPC::VMULEUW:
462fe723b9aSBill Schmidt case PPC::VMULOSB:
463fe723b9aSBill Schmidt case PPC::VMULOSH:
464fe723b9aSBill Schmidt case PPC::VMULOSW:
465fe723b9aSBill Schmidt case PPC::VMULOUB:
466fe723b9aSBill Schmidt case PPC::VMULOUH:
467fe723b9aSBill Schmidt case PPC::VMULOUW:
468fe723b9aSBill Schmidt case PPC::VNCIPHER:
469fe723b9aSBill Schmidt case PPC::VNCIPHERLAST:
470fe723b9aSBill Schmidt case PPC::VPERM:
471fe723b9aSBill Schmidt case PPC::VPERMXOR:
472fe723b9aSBill Schmidt case PPC::VPKPX:
473fe723b9aSBill Schmidt case PPC::VPKSHSS:
474fe723b9aSBill Schmidt case PPC::VPKSHUS:
4755ed84cdbSBill Schmidt case PPC::VPKSDSS:
4765ed84cdbSBill Schmidt case PPC::VPKSDUS:
477fe723b9aSBill Schmidt case PPC::VPKSWSS:
478fe723b9aSBill Schmidt case PPC::VPKSWUS:
4795ed84cdbSBill Schmidt case PPC::VPKUDUM:
4805ed84cdbSBill Schmidt case PPC::VPKUDUS:
481fe723b9aSBill Schmidt case PPC::VPKUHUM:
482fe723b9aSBill Schmidt case PPC::VPKUHUS:
483fe723b9aSBill Schmidt case PPC::VPKUWUM:
484fe723b9aSBill Schmidt case PPC::VPKUWUS:
485fe723b9aSBill Schmidt case PPC::VPMSUMB:
486fe723b9aSBill Schmidt case PPC::VPMSUMD:
487fe723b9aSBill Schmidt case PPC::VPMSUMH:
488fe723b9aSBill Schmidt case PPC::VPMSUMW:
489fe723b9aSBill Schmidt case PPC::VRLB:
490fe723b9aSBill Schmidt case PPC::VRLD:
491fe723b9aSBill Schmidt case PPC::VRLH:
492fe723b9aSBill Schmidt case PPC::VRLW:
493fe723b9aSBill Schmidt case PPC::VSBOX:
494fe723b9aSBill Schmidt case PPC::VSHASIGMAD:
495fe723b9aSBill Schmidt case PPC::VSHASIGMAW:
496fe723b9aSBill Schmidt case PPC::VSL:
497fe723b9aSBill Schmidt case PPC::VSLDOI:
498fe723b9aSBill Schmidt case PPC::VSLO:
499fe723b9aSBill Schmidt case PPC::VSR:
500fe723b9aSBill Schmidt case PPC::VSRO:
501fe723b9aSBill Schmidt case PPC::VSUM2SWS:
502fe723b9aSBill Schmidt case PPC::VSUM4SBS:
503fe723b9aSBill Schmidt case PPC::VSUM4SHS:
504fe723b9aSBill Schmidt case PPC::VSUM4UBS:
505fe723b9aSBill Schmidt case PPC::VSUMSWS:
506fe723b9aSBill Schmidt case PPC::VUPKHPX:
507fe723b9aSBill Schmidt case PPC::VUPKHSB:
508fe723b9aSBill Schmidt case PPC::VUPKHSH:
5095ed84cdbSBill Schmidt case PPC::VUPKHSW:
510fe723b9aSBill Schmidt case PPC::VUPKLPX:
511fe723b9aSBill Schmidt case PPC::VUPKLSB:
512fe723b9aSBill Schmidt case PPC::VUPKLSH:
5135ed84cdbSBill Schmidt case PPC::VUPKLSW:
514fe723b9aSBill Schmidt case PPC::XXMRGHW:
515fe723b9aSBill Schmidt case PPC::XXMRGLW:
51615deb803SBill Schmidt // XXSLDWI could be replaced by a general permute with one of three
51715deb803SBill Schmidt // permute control vectors (for shift values 1, 2, 3). However,
51815deb803SBill Schmidt // VPERM has a more restrictive register class.
51915deb803SBill Schmidt case PPC::XXSLDWI:
52077e34f15SNemanja Ivanovic case PPC::XSCVDPSPN:
52177e34f15SNemanja Ivanovic case PPC::XSCVSPDPN:
522*deb76552SQuinn Pham case PPC::MTVSCR:
523*deb76552SQuinn Pham case PPC::MFVSCR:
524fe723b9aSBill Schmidt break;
525fe723b9aSBill Schmidt }
526fe723b9aSBill Schmidt }
527fe723b9aSBill Schmidt }
528fe723b9aSBill Schmidt
529fe723b9aSBill Schmidt if (RelevantFunction) {
530d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Swap vector when first built\n\n");
531d34e60caSNicola Zaghen LLVM_DEBUG(dumpSwapVector());
532fe723b9aSBill Schmidt }
533fe723b9aSBill Schmidt
534fe723b9aSBill Schmidt return RelevantFunction;
535fe723b9aSBill Schmidt }
536fe723b9aSBill Schmidt
537fe723b9aSBill Schmidt // Add an entry to the swap vector and swap map, and make a
538fe723b9aSBill Schmidt // singleton equivalence class for the entry.
addSwapEntry(MachineInstr * MI,PPCVSXSwapEntry & SwapEntry)539fe723b9aSBill Schmidt int PPCVSXSwapRemoval::addSwapEntry(MachineInstr *MI,
540fe723b9aSBill Schmidt PPCVSXSwapEntry& SwapEntry) {
541fe723b9aSBill Schmidt SwapEntry.VSEMI = MI;
542fe723b9aSBill Schmidt SwapEntry.VSEId = SwapVector.size();
543fe723b9aSBill Schmidt SwapVector.push_back(SwapEntry);
544fe723b9aSBill Schmidt EC->insert(SwapEntry.VSEId);
545fe723b9aSBill Schmidt SwapMap[MI] = SwapEntry.VSEId;
546fe723b9aSBill Schmidt return SwapEntry.VSEId;
547fe723b9aSBill Schmidt }
548fe723b9aSBill Schmidt
549fe723b9aSBill Schmidt // This is used to find the "true" source register for an
550fe723b9aSBill Schmidt // XXPERMDI instruction, since MachineCSE does not handle the
551fe723b9aSBill Schmidt // "copy-like" operations (Copy and SubregToReg). Returns
552fe723b9aSBill Schmidt // the original SrcReg unless it is the target of a copy-like
553fe723b9aSBill Schmidt // operation, in which case we chain backwards through all
554fe723b9aSBill Schmidt // such operations to the ultimate source register. If a
555fe723b9aSBill Schmidt // physical register is encountered, we stop the search and
556fe723b9aSBill Schmidt // flag the swap entry indicated by VecIdx (the original
557a1c30053SBill Schmidt // XXPERMDI) as mentioning a physical register.
lookThruCopyLike(unsigned SrcReg,unsigned VecIdx)558fe723b9aSBill Schmidt unsigned PPCVSXSwapRemoval::lookThruCopyLike(unsigned SrcReg,
559fe723b9aSBill Schmidt unsigned VecIdx) {
560fe723b9aSBill Schmidt MachineInstr *MI = MRI->getVRegDef(SrcReg);
561fe723b9aSBill Schmidt if (!MI->isCopyLike())
562fe723b9aSBill Schmidt return SrcReg;
563fe723b9aSBill Schmidt
564a1c30053SBill Schmidt unsigned CopySrcReg;
565a1c30053SBill Schmidt if (MI->isCopy())
566fe723b9aSBill Schmidt CopySrcReg = MI->getOperand(1).getReg();
567a1c30053SBill Schmidt else {
568fe723b9aSBill Schmidt assert(MI->isSubregToReg() && "bad opcode for lookThruCopyLike");
569fe723b9aSBill Schmidt CopySrcReg = MI->getOperand(2).getReg();
570fe723b9aSBill Schmidt }
571fe723b9aSBill Schmidt
5722bea69bfSDaniel Sanders if (!Register::isVirtualRegister(CopySrcReg)) {
5732be8054bSBill Schmidt if (!isScalarVecReg(CopySrcReg))
574fe723b9aSBill Schmidt SwapVector[VecIdx].MentionsPhysVR = 1;
575fe723b9aSBill Schmidt return CopySrcReg;
576fe723b9aSBill Schmidt }
577fe723b9aSBill Schmidt
578fe723b9aSBill Schmidt return lookThruCopyLike(CopySrcReg, VecIdx);
579fe723b9aSBill Schmidt }
580fe723b9aSBill Schmidt
581fe723b9aSBill Schmidt // Generate equivalence classes for related computations (webs) by
582fe723b9aSBill Schmidt // def-use relationships of virtual registers. Mention of a physical
583fe723b9aSBill Schmidt // register terminates the generation of equivalence classes as this
584fe723b9aSBill Schmidt // indicates a use of a parameter, definition of a return value, use
585fe723b9aSBill Schmidt // of a value returned from a call, or definition of a parameter to a
586fe723b9aSBill Schmidt // call. Computations with physical register mentions are flagged
587fe723b9aSBill Schmidt // as such so their containing webs will not be optimized.
formWebs()588fe723b9aSBill Schmidt void PPCVSXSwapRemoval::formWebs() {
589fe723b9aSBill Schmidt
590d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n*** Forming webs for swap removal ***\n\n");
591fe723b9aSBill Schmidt
592fe723b9aSBill Schmidt for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
593fe723b9aSBill Schmidt
594fe723b9aSBill Schmidt MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
595fe723b9aSBill Schmidt
596d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n" << SwapVector[EntryIdx].VSEId << " ");
597d34e60caSNicola Zaghen LLVM_DEBUG(MI->dump());
598fe723b9aSBill Schmidt
599fe723b9aSBill Schmidt // It's sufficient to walk vector uses and join them to their unique
60015deb803SBill Schmidt // definitions. In addition, check full vector register operands
60115deb803SBill Schmidt // for physical regs. We exclude partial-vector register operands
60215deb803SBill Schmidt // because we can handle them if copied to a full vector.
603fe723b9aSBill Schmidt for (const MachineOperand &MO : MI->operands()) {
604fe723b9aSBill Schmidt if (!MO.isReg())
605fe723b9aSBill Schmidt continue;
606fe723b9aSBill Schmidt
6070c476111SDaniel Sanders Register Reg = MO.getReg();
60815deb803SBill Schmidt if (!isVecReg(Reg) && !isScalarVecReg(Reg))
609fe723b9aSBill Schmidt continue;
610fe723b9aSBill Schmidt
6112bea69bfSDaniel Sanders if (!Register::isVirtualRegister(Reg)) {
61215deb803SBill Schmidt if (!(MI->isCopy() && isScalarVecReg(Reg)))
613fe723b9aSBill Schmidt SwapVector[EntryIdx].MentionsPhysVR = 1;
614fe723b9aSBill Schmidt continue;
615fe723b9aSBill Schmidt }
616fe723b9aSBill Schmidt
617fe723b9aSBill Schmidt if (!MO.isUse())
618fe723b9aSBill Schmidt continue;
619fe723b9aSBill Schmidt
620fe723b9aSBill Schmidt MachineInstr* DefMI = MRI->getVRegDef(Reg);
621fe723b9aSBill Schmidt assert(SwapMap.find(DefMI) != SwapMap.end() &&
622fe723b9aSBill Schmidt "Inconsistency: def of vector reg not found in swap map!");
623fe723b9aSBill Schmidt int DefIdx = SwapMap[DefMI];
624fe723b9aSBill Schmidt (void)EC->unionSets(SwapVector[DefIdx].VSEId,
625fe723b9aSBill Schmidt SwapVector[EntryIdx].VSEId);
626fe723b9aSBill Schmidt
627d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << format("Unioning %d with %d\n",
628d34e60caSNicola Zaghen SwapVector[DefIdx].VSEId,
629fe723b9aSBill Schmidt SwapVector[EntryIdx].VSEId));
630d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Def: ");
631d34e60caSNicola Zaghen LLVM_DEBUG(DefMI->dump());
632fe723b9aSBill Schmidt }
633fe723b9aSBill Schmidt }
634fe723b9aSBill Schmidt }
635fe723b9aSBill Schmidt
636fe723b9aSBill Schmidt // Walk the swap vector entries looking for conditions that prevent their
637fe723b9aSBill Schmidt // containing computations from being optimized. When such conditions are
638fe723b9aSBill Schmidt // found, mark the representative of the computation's equivalence class
639fe723b9aSBill Schmidt // as rejected.
recordUnoptimizableWebs()640fe723b9aSBill Schmidt void PPCVSXSwapRemoval::recordUnoptimizableWebs() {
641fe723b9aSBill Schmidt
642d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n*** Rejecting webs for swap removal ***\n\n");
643fe723b9aSBill Schmidt
644fe723b9aSBill Schmidt for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
645fe723b9aSBill Schmidt int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
646fe723b9aSBill Schmidt
64715deb803SBill Schmidt // If representative is already rejected, don't waste further time.
64815deb803SBill Schmidt if (SwapVector[Repr].WebRejected)
64915deb803SBill Schmidt continue;
65015deb803SBill Schmidt
65115deb803SBill Schmidt // Reject webs containing mentions of physical or partial registers, or
65215deb803SBill Schmidt // containing operations that we don't know how to handle in a lane-
65315deb803SBill Schmidt // permuted region.
654fe723b9aSBill Schmidt if (SwapVector[EntryIdx].MentionsPhysVR ||
65515deb803SBill Schmidt SwapVector[EntryIdx].MentionsPartialVR ||
656fe723b9aSBill Schmidt !(SwapVector[EntryIdx].IsSwappable || SwapVector[EntryIdx].IsSwap)) {
657fe723b9aSBill Schmidt
658fe723b9aSBill Schmidt SwapVector[Repr].WebRejected = 1;
659fe723b9aSBill Schmidt
660d34e60caSNicola Zaghen LLVM_DEBUG(
661d34e60caSNicola Zaghen dbgs() << format("Web %d rejected for physreg, partial reg, or not "
662d34e60caSNicola Zaghen "swap[pable]\n",
663d34e60caSNicola Zaghen Repr));
664d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " in " << EntryIdx << ": ");
665d34e60caSNicola Zaghen LLVM_DEBUG(SwapVector[EntryIdx].VSEMI->dump());
666d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n");
667fe723b9aSBill Schmidt }
668fe723b9aSBill Schmidt
669fe723b9aSBill Schmidt // Reject webs than contain swapping loads that feed something other
670fe723b9aSBill Schmidt // than a swap instruction.
671fe723b9aSBill Schmidt else if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) {
672fe723b9aSBill Schmidt MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
6730c476111SDaniel Sanders Register DefReg = MI->getOperand(0).getReg();
674fe723b9aSBill Schmidt
675fe723b9aSBill Schmidt // We skip debug instructions in the analysis. (Note that debug
676fe723b9aSBill Schmidt // location information is still maintained by this optimization
677fe723b9aSBill Schmidt // because it remains on the LXVD2X and STXVD2X instructions after
678fe723b9aSBill Schmidt // the XXPERMDIs are removed.)
679fe723b9aSBill Schmidt for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) {
680fe723b9aSBill Schmidt int UseIdx = SwapMap[&UseMI];
681fe723b9aSBill Schmidt
682fe723b9aSBill Schmidt if (!SwapVector[UseIdx].IsSwap || SwapVector[UseIdx].IsLoad ||
683fe723b9aSBill Schmidt SwapVector[UseIdx].IsStore) {
684fe723b9aSBill Schmidt
685fe723b9aSBill Schmidt SwapVector[Repr].WebRejected = 1;
686fe723b9aSBill Schmidt
687d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << format(
688d34e60caSNicola Zaghen "Web %d rejected for load not feeding swap\n", Repr));
689d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " def " << EntryIdx << ": ");
690d34e60caSNicola Zaghen LLVM_DEBUG(MI->dump());
691d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " use " << UseIdx << ": ");
692d34e60caSNicola Zaghen LLVM_DEBUG(UseMI.dump());
693d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n");
694fe723b9aSBill Schmidt }
69514d726acSNemanja Ivanovic
69614d726acSNemanja Ivanovic // It is possible that the load feeds a swap and that swap feeds a
69714d726acSNemanja Ivanovic // store. In such a case, the code is actually trying to store a swapped
69814d726acSNemanja Ivanovic // vector. We must reject such webs.
69914d726acSNemanja Ivanovic if (SwapVector[UseIdx].IsSwap && !SwapVector[UseIdx].IsLoad &&
70014d726acSNemanja Ivanovic !SwapVector[UseIdx].IsStore) {
70114d726acSNemanja Ivanovic Register SwapDefReg = UseMI.getOperand(0).getReg();
70214d726acSNemanja Ivanovic for (MachineInstr &UseOfUseMI :
70314d726acSNemanja Ivanovic MRI->use_nodbg_instructions(SwapDefReg)) {
70414d726acSNemanja Ivanovic int UseOfUseIdx = SwapMap[&UseOfUseMI];
70514d726acSNemanja Ivanovic if (SwapVector[UseOfUseIdx].IsStore) {
70614d726acSNemanja Ivanovic SwapVector[Repr].WebRejected = 1;
70714d726acSNemanja Ivanovic LLVM_DEBUG(
70814d726acSNemanja Ivanovic dbgs() << format(
70914d726acSNemanja Ivanovic "Web %d rejected for load/swap feeding a store\n", Repr));
71014d726acSNemanja Ivanovic LLVM_DEBUG(dbgs() << " def " << EntryIdx << ": ");
71114d726acSNemanja Ivanovic LLVM_DEBUG(MI->dump());
71214d726acSNemanja Ivanovic LLVM_DEBUG(dbgs() << " use " << UseIdx << ": ");
71314d726acSNemanja Ivanovic LLVM_DEBUG(UseMI.dump());
71414d726acSNemanja Ivanovic LLVM_DEBUG(dbgs() << "\n");
71514d726acSNemanja Ivanovic }
71614d726acSNemanja Ivanovic }
71714d726acSNemanja Ivanovic }
718fe723b9aSBill Schmidt }
719fe723b9aSBill Schmidt
72015deb803SBill Schmidt // Reject webs that contain swapping stores that are fed by something
721fe723b9aSBill Schmidt // other than a swap instruction.
722fe723b9aSBill Schmidt } else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) {
723fe723b9aSBill Schmidt MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
7240c476111SDaniel Sanders Register UseReg = MI->getOperand(0).getReg();
725fe723b9aSBill Schmidt MachineInstr *DefMI = MRI->getVRegDef(UseReg);
7260c476111SDaniel Sanders Register DefReg = DefMI->getOperand(0).getReg();
727fe723b9aSBill Schmidt int DefIdx = SwapMap[DefMI];
728fe723b9aSBill Schmidt
729fe723b9aSBill Schmidt if (!SwapVector[DefIdx].IsSwap || SwapVector[DefIdx].IsLoad ||
730fe723b9aSBill Schmidt SwapVector[DefIdx].IsStore) {
731fe723b9aSBill Schmidt
732fe723b9aSBill Schmidt SwapVector[Repr].WebRejected = 1;
733fe723b9aSBill Schmidt
734d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << format(
735d34e60caSNicola Zaghen "Web %d rejected for store not fed by swap\n", Repr));
736d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " def " << DefIdx << ": ");
737d34e60caSNicola Zaghen LLVM_DEBUG(DefMI->dump());
738d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " use " << EntryIdx << ": ");
739d34e60caSNicola Zaghen LLVM_DEBUG(MI->dump());
740d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n");
741fe723b9aSBill Schmidt }
742f9d0a405SKit Barton
743f9d0a405SKit Barton // Ensure all uses of the register defined by DefMI feed store
744f9d0a405SKit Barton // instructions
745f9d0a405SKit Barton for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) {
746f9d0a405SKit Barton int UseIdx = SwapMap[&UseMI];
747f9d0a405SKit Barton
748f9d0a405SKit Barton if (SwapVector[UseIdx].VSEMI->getOpcode() != MI->getOpcode()) {
749f9d0a405SKit Barton SwapVector[Repr].WebRejected = 1;
750f9d0a405SKit Barton
751d34e60caSNicola Zaghen LLVM_DEBUG(
752d34e60caSNicola Zaghen dbgs() << format(
753d34e60caSNicola Zaghen "Web %d rejected for swap not feeding only stores\n", Repr));
754d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " def "
755d34e60caSNicola Zaghen << " : ");
756d34e60caSNicola Zaghen LLVM_DEBUG(DefMI->dump());
757d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " use " << UseIdx << ": ");
758d34e60caSNicola Zaghen LLVM_DEBUG(SwapVector[UseIdx].VSEMI->dump());
759d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n");
760f9d0a405SKit Barton }
761f9d0a405SKit Barton }
762fe723b9aSBill Schmidt }
763fe723b9aSBill Schmidt }
764fe723b9aSBill Schmidt
765d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Swap vector after web analysis:\n\n");
766d34e60caSNicola Zaghen LLVM_DEBUG(dumpSwapVector());
767fe723b9aSBill Schmidt }
768fe723b9aSBill Schmidt
769fe723b9aSBill Schmidt // Walk the swap vector entries looking for swaps fed by permuting loads
770fe723b9aSBill Schmidt // and swaps that feed permuting stores. If the containing computation
771fe723b9aSBill Schmidt // has not been marked rejected, mark each such swap for removal.
772fe723b9aSBill Schmidt // (Removal is delayed in case optimization has disturbed the pattern,
773fe723b9aSBill Schmidt // such that multiple loads feed the same swap, etc.)
markSwapsForRemoval()774fe723b9aSBill Schmidt void PPCVSXSwapRemoval::markSwapsForRemoval() {
775fe723b9aSBill Schmidt
776d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n*** Marking swaps for removal ***\n\n");
777fe723b9aSBill Schmidt
778fe723b9aSBill Schmidt for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
779fe723b9aSBill Schmidt
780fe723b9aSBill Schmidt if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) {
781fe723b9aSBill Schmidt int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
782fe723b9aSBill Schmidt
783fe723b9aSBill Schmidt if (!SwapVector[Repr].WebRejected) {
784fe723b9aSBill Schmidt MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
7850c476111SDaniel Sanders Register DefReg = MI->getOperand(0).getReg();
786fe723b9aSBill Schmidt
787fe723b9aSBill Schmidt for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) {
788fe723b9aSBill Schmidt int UseIdx = SwapMap[&UseMI];
789fe723b9aSBill Schmidt SwapVector[UseIdx].WillRemove = 1;
790fe723b9aSBill Schmidt
791d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Marking swap fed by load for removal: ");
792d34e60caSNicola Zaghen LLVM_DEBUG(UseMI.dump());
793fe723b9aSBill Schmidt }
794fe723b9aSBill Schmidt }
795fe723b9aSBill Schmidt
796fe723b9aSBill Schmidt } else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) {
797fe723b9aSBill Schmidt int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
798fe723b9aSBill Schmidt
799fe723b9aSBill Schmidt if (!SwapVector[Repr].WebRejected) {
800fe723b9aSBill Schmidt MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
8010c476111SDaniel Sanders Register UseReg = MI->getOperand(0).getReg();
802fe723b9aSBill Schmidt MachineInstr *DefMI = MRI->getVRegDef(UseReg);
803fe723b9aSBill Schmidt int DefIdx = SwapMap[DefMI];
804fe723b9aSBill Schmidt SwapVector[DefIdx].WillRemove = 1;
805fe723b9aSBill Schmidt
806d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Marking swap feeding store for removal: ");
807d34e60caSNicola Zaghen LLVM_DEBUG(DefMI->dump());
808fe723b9aSBill Schmidt }
809fe723b9aSBill Schmidt
810fe723b9aSBill Schmidt } else if (SwapVector[EntryIdx].IsSwappable &&
8115fe2e25fSBill Schmidt SwapVector[EntryIdx].SpecialHandling != 0) {
8125fe2e25fSBill Schmidt int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
8135fe2e25fSBill Schmidt
8145fe2e25fSBill Schmidt if (!SwapVector[Repr].WebRejected)
815fe723b9aSBill Schmidt handleSpecialSwappables(EntryIdx);
816fe723b9aSBill Schmidt }
817fe723b9aSBill Schmidt }
8185fe2e25fSBill Schmidt }
819fe723b9aSBill Schmidt
8202be8054bSBill Schmidt // Create an xxswapd instruction and insert it prior to the given point.
8212be8054bSBill Schmidt // MI is used to determine basic block and debug loc information.
8222be8054bSBill Schmidt // FIXME: When inserting a swap, we should check whether SrcReg is
8232be8054bSBill Schmidt // defined by another swap: SrcReg = XXPERMDI Reg, Reg, 2; If so,
8242be8054bSBill Schmidt // then instead we should generate a copy from Reg to DstReg.
insertSwap(MachineInstr * MI,MachineBasicBlock::iterator InsertPoint,unsigned DstReg,unsigned SrcReg)8252be8054bSBill Schmidt void PPCVSXSwapRemoval::insertSwap(MachineInstr *MI,
8262be8054bSBill Schmidt MachineBasicBlock::iterator InsertPoint,
8272be8054bSBill Schmidt unsigned DstReg, unsigned SrcReg) {
8282be8054bSBill Schmidt BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(),
8292be8054bSBill Schmidt TII->get(PPC::XXPERMDI), DstReg)
8302be8054bSBill Schmidt .addReg(SrcReg)
8312be8054bSBill Schmidt .addReg(SrcReg)
8322be8054bSBill Schmidt .addImm(2);
8332be8054bSBill Schmidt }
8342be8054bSBill Schmidt
835fe723b9aSBill Schmidt // The identified swap entry requires special handling to allow its
836fe723b9aSBill Schmidt // containing computation to be optimized. Perform that handling
837fe723b9aSBill Schmidt // here.
83815deb803SBill Schmidt // FIXME: Additional opportunities will be phased in with subsequent
83915deb803SBill Schmidt // patches.
handleSpecialSwappables(int EntryIdx)840fe723b9aSBill Schmidt void PPCVSXSwapRemoval::handleSpecialSwappables(int EntryIdx) {
8415fe2e25fSBill Schmidt switch (SwapVector[EntryIdx].SpecialHandling) {
8425fe2e25fSBill Schmidt
8435fe2e25fSBill Schmidt default:
8448ceb323bSBenjamin Kramer llvm_unreachable("Unexpected special handling type");
8455fe2e25fSBill Schmidt
8465fe2e25fSBill Schmidt // For splats based on an index into a vector, add N/2 modulo N
8475fe2e25fSBill Schmidt // to the index, where N is the number of vector elements.
8485fe2e25fSBill Schmidt case SHValues::SH_SPLAT: {
8495fe2e25fSBill Schmidt MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
8505fe2e25fSBill Schmidt unsigned NElts;
8515fe2e25fSBill Schmidt
852d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Changing splat: ");
853d34e60caSNicola Zaghen LLVM_DEBUG(MI->dump());
8545fe2e25fSBill Schmidt
8555fe2e25fSBill Schmidt switch (MI->getOpcode()) {
8565fe2e25fSBill Schmidt default:
8578ceb323bSBenjamin Kramer llvm_unreachable("Unexpected splat opcode");
8585fe2e25fSBill Schmidt case PPC::VSPLTB: NElts = 16; break;
8595fe2e25fSBill Schmidt case PPC::VSPLTH: NElts = 8; break;
8601a2b2f03SNemanja Ivanovic case PPC::VSPLTW:
8611a2b2f03SNemanja Ivanovic case PPC::XXSPLTW: NElts = 4; break;
8625fe2e25fSBill Schmidt }
8635fe2e25fSBill Schmidt
8641a2b2f03SNemanja Ivanovic unsigned EltNo;
8651a2b2f03SNemanja Ivanovic if (MI->getOpcode() == PPC::XXSPLTW)
8661a2b2f03SNemanja Ivanovic EltNo = MI->getOperand(2).getImm();
8671a2b2f03SNemanja Ivanovic else
8681a2b2f03SNemanja Ivanovic EltNo = MI->getOperand(1).getImm();
8691a2b2f03SNemanja Ivanovic
8705fe2e25fSBill Schmidt EltNo = (EltNo + NElts / 2) % NElts;
8711a2b2f03SNemanja Ivanovic if (MI->getOpcode() == PPC::XXSPLTW)
8721a2b2f03SNemanja Ivanovic MI->getOperand(2).setImm(EltNo);
8731a2b2f03SNemanja Ivanovic else
8745fe2e25fSBill Schmidt MI->getOperand(1).setImm(EltNo);
8755fe2e25fSBill Schmidt
876d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Into: ");
877d34e60caSNicola Zaghen LLVM_DEBUG(MI->dump());
8785fe2e25fSBill Schmidt break;
8795fe2e25fSBill Schmidt }
8805fe2e25fSBill Schmidt
88115deb803SBill Schmidt // For an XXPERMDI that isn't handled otherwise, we need to
88215deb803SBill Schmidt // reverse the order of the operands. If the selector operand
88315deb803SBill Schmidt // has a value of 0 or 3, we need to change it to 3 or 0,
88415deb803SBill Schmidt // respectively. Otherwise we should leave it alone. (This
88515deb803SBill Schmidt // is equivalent to reversing the two bits of the selector
88615deb803SBill Schmidt // operand and complementing the result.)
88715deb803SBill Schmidt case SHValues::SH_XXPERMDI: {
88815deb803SBill Schmidt MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
88915deb803SBill Schmidt
890d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Changing XXPERMDI: ");
891d34e60caSNicola Zaghen LLVM_DEBUG(MI->dump());
89215deb803SBill Schmidt
89315deb803SBill Schmidt unsigned Selector = MI->getOperand(3).getImm();
89415deb803SBill Schmidt if (Selector == 0 || Selector == 3)
89515deb803SBill Schmidt Selector = 3 - Selector;
89615deb803SBill Schmidt MI->getOperand(3).setImm(Selector);
89715deb803SBill Schmidt
8980c476111SDaniel Sanders Register Reg1 = MI->getOperand(1).getReg();
8990c476111SDaniel Sanders Register Reg2 = MI->getOperand(2).getReg();
90015deb803SBill Schmidt MI->getOperand(1).setReg(Reg2);
90115deb803SBill Schmidt MI->getOperand(2).setReg(Reg1);
90215deb803SBill Schmidt
9039bffc94cSHiroshi Inoue // We also need to swap kill flag associated with the register.
9049bffc94cSHiroshi Inoue bool IsKill1 = MI->getOperand(1).isKill();
9059bffc94cSHiroshi Inoue bool IsKill2 = MI->getOperand(2).isKill();
9069bffc94cSHiroshi Inoue MI->getOperand(1).setIsKill(IsKill2);
9079bffc94cSHiroshi Inoue MI->getOperand(2).setIsKill(IsKill1);
9089bffc94cSHiroshi Inoue
909d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Into: ");
910d34e60caSNicola Zaghen LLVM_DEBUG(MI->dump());
91115deb803SBill Schmidt break;
91215deb803SBill Schmidt }
91315deb803SBill Schmidt
91415deb803SBill Schmidt // For a copy from a scalar floating-point register to a vector
91515deb803SBill Schmidt // register, removing swaps will leave the copied value in the
91615deb803SBill Schmidt // wrong lane. Insert a swap following the copy to fix this.
9172be8054bSBill Schmidt case SHValues::SH_COPYWIDEN: {
91815deb803SBill Schmidt MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
91915deb803SBill Schmidt
920d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Changing SUBREG_TO_REG: ");
921d34e60caSNicola Zaghen LLVM_DEBUG(MI->dump());
92215deb803SBill Schmidt
9230c476111SDaniel Sanders Register DstReg = MI->getOperand(0).getReg();
92415deb803SBill Schmidt const TargetRegisterClass *DstRC = MRI->getRegClass(DstReg);
9250c476111SDaniel Sanders Register NewVReg = MRI->createVirtualRegister(DstRC);
92615deb803SBill Schmidt
92715deb803SBill Schmidt MI->getOperand(0).setReg(NewVReg);
928d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Into: ");
929d34e60caSNicola Zaghen LLVM_DEBUG(MI->dump());
93015deb803SBill Schmidt
931a3da4488SDuncan P. N. Exon Smith auto InsertPoint = ++MachineBasicBlock::iterator(MI);
93215deb803SBill Schmidt
93315deb803SBill Schmidt // Note that an XXPERMDI requires a VSRC, so if the SUBREG_TO_REG
93415deb803SBill Schmidt // is copying to a VRRC, we need to be careful to avoid a register
93515deb803SBill Schmidt // assignment problem. In this case we must copy from VRRC to VSRC
93615deb803SBill Schmidt // prior to the swap, and from VSRC to VRRC following the swap.
93715deb803SBill Schmidt // Coalescing will usually remove all this mess.
93815deb803SBill Schmidt if (DstRC == &PPC::VRRCRegClass) {
9390c476111SDaniel Sanders Register VSRCTmp1 = MRI->createVirtualRegister(&PPC::VSRCRegClass);
9400c476111SDaniel Sanders Register VSRCTmp2 = MRI->createVirtualRegister(&PPC::VSRCRegClass);
94115deb803SBill Schmidt
94215deb803SBill Schmidt BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(),
94315deb803SBill Schmidt TII->get(PPC::COPY), VSRCTmp1)
94415deb803SBill Schmidt .addReg(NewVReg);
945d34e60caSNicola Zaghen LLVM_DEBUG(std::prev(InsertPoint)->dump());
94615deb803SBill Schmidt
9472be8054bSBill Schmidt insertSwap(MI, InsertPoint, VSRCTmp2, VSRCTmp1);
948d34e60caSNicola Zaghen LLVM_DEBUG(std::prev(InsertPoint)->dump());
94915deb803SBill Schmidt
95015deb803SBill Schmidt BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(),
95115deb803SBill Schmidt TII->get(PPC::COPY), DstReg)
95215deb803SBill Schmidt .addReg(VSRCTmp2);
953d34e60caSNicola Zaghen LLVM_DEBUG(std::prev(InsertPoint)->dump());
95415deb803SBill Schmidt
95515deb803SBill Schmidt } else {
9562be8054bSBill Schmidt insertSwap(MI, InsertPoint, DstReg, NewVReg);
957d34e60caSNicola Zaghen LLVM_DEBUG(std::prev(InsertPoint)->dump());
95815deb803SBill Schmidt }
95915deb803SBill Schmidt break;
96015deb803SBill Schmidt }
9615fe2e25fSBill Schmidt }
962fe723b9aSBill Schmidt }
963fe723b9aSBill Schmidt
964fe723b9aSBill Schmidt // Walk the swap vector and replace each entry marked for removal with
965fe723b9aSBill Schmidt // a copy operation.
removeSwaps()966fe723b9aSBill Schmidt bool PPCVSXSwapRemoval::removeSwaps() {
967fe723b9aSBill Schmidt
968d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n*** Removing swaps ***\n\n");
969fe723b9aSBill Schmidt
970fe723b9aSBill Schmidt bool Changed = false;
971fe723b9aSBill Schmidt
972fe723b9aSBill Schmidt for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
973fe723b9aSBill Schmidt if (SwapVector[EntryIdx].WillRemove) {
974fe723b9aSBill Schmidt Changed = true;
975fe723b9aSBill Schmidt MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
976fe723b9aSBill Schmidt MachineBasicBlock *MBB = MI->getParent();
977116bbab4SDiana Picus BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(TargetOpcode::COPY),
978116bbab4SDiana Picus MI->getOperand(0).getReg())
979116bbab4SDiana Picus .add(MI->getOperand(1));
980fe723b9aSBill Schmidt
981d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << format("Replaced %d with copy: ",
982fe723b9aSBill Schmidt SwapVector[EntryIdx].VSEId));
983d34e60caSNicola Zaghen LLVM_DEBUG(MI->dump());
984fe723b9aSBill Schmidt
985fe723b9aSBill Schmidt MI->eraseFromParent();
986fe723b9aSBill Schmidt }
987fe723b9aSBill Schmidt }
988fe723b9aSBill Schmidt
989fe723b9aSBill Schmidt return Changed;
990fe723b9aSBill Schmidt }
991fe723b9aSBill Schmidt
992615eb470SAaron Ballman #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
993fe723b9aSBill Schmidt // For debug purposes, dump the contents of the swap vector.
dumpSwapVector()9948c209aa8SMatthias Braun LLVM_DUMP_METHOD void PPCVSXSwapRemoval::dumpSwapVector() {
995fe723b9aSBill Schmidt
996fe723b9aSBill Schmidt for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
997fe723b9aSBill Schmidt
998fe723b9aSBill Schmidt MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
999fe723b9aSBill Schmidt int ID = SwapVector[EntryIdx].VSEId;
1000fe723b9aSBill Schmidt
10018c209aa8SMatthias Braun dbgs() << format("%6d", ID);
10028c209aa8SMatthias Braun dbgs() << format("%6d", EC->getLeaderValue(ID));
100325528d6dSFrancis Visoiu Mistrih dbgs() << format(" %bb.%3d", MI->getParent()->getNumber());
10048c209aa8SMatthias Braun dbgs() << format(" %14s ", TII->getName(MI->getOpcode()).str().c_str());
1005fe723b9aSBill Schmidt
1006fe723b9aSBill Schmidt if (SwapVector[EntryIdx].IsLoad)
10078c209aa8SMatthias Braun dbgs() << "load ";
1008fe723b9aSBill Schmidt if (SwapVector[EntryIdx].IsStore)
10098c209aa8SMatthias Braun dbgs() << "store ";
1010fe723b9aSBill Schmidt if (SwapVector[EntryIdx].IsSwap)
10118c209aa8SMatthias Braun dbgs() << "swap ";
1012fe723b9aSBill Schmidt if (SwapVector[EntryIdx].MentionsPhysVR)
10138c209aa8SMatthias Braun dbgs() << "physreg ";
101415deb803SBill Schmidt if (SwapVector[EntryIdx].MentionsPartialVR)
10158c209aa8SMatthias Braun dbgs() << "partialreg ";
1016fe723b9aSBill Schmidt
1017fe723b9aSBill Schmidt if (SwapVector[EntryIdx].IsSwappable) {
10188c209aa8SMatthias Braun dbgs() << "swappable ";
1019fe723b9aSBill Schmidt switch(SwapVector[EntryIdx].SpecialHandling) {
1020fe723b9aSBill Schmidt default:
10218c209aa8SMatthias Braun dbgs() << "special:**unknown**";
1022fe723b9aSBill Schmidt break;
1023fe723b9aSBill Schmidt case SH_NONE:
1024fe723b9aSBill Schmidt break;
1025fe723b9aSBill Schmidt case SH_EXTRACT:
10268c209aa8SMatthias Braun dbgs() << "special:extract ";
1027fe723b9aSBill Schmidt break;
1028fe723b9aSBill Schmidt case SH_INSERT:
10298c209aa8SMatthias Braun dbgs() << "special:insert ";
1030fe723b9aSBill Schmidt break;
1031fe723b9aSBill Schmidt case SH_NOSWAP_LD:
10328c209aa8SMatthias Braun dbgs() << "special:load ";
1033fe723b9aSBill Schmidt break;
1034fe723b9aSBill Schmidt case SH_NOSWAP_ST:
10358c209aa8SMatthias Braun dbgs() << "special:store ";
1036fe723b9aSBill Schmidt break;
1037fe723b9aSBill Schmidt case SH_SPLAT:
10388c209aa8SMatthias Braun dbgs() << "special:splat ";
1039fe723b9aSBill Schmidt break;
104015deb803SBill Schmidt case SH_XXPERMDI:
10418c209aa8SMatthias Braun dbgs() << "special:xxpermdi ";
104215deb803SBill Schmidt break;
10432be8054bSBill Schmidt case SH_COPYWIDEN:
10448c209aa8SMatthias Braun dbgs() << "special:copywiden ";
104515deb803SBill Schmidt break;
1046fe723b9aSBill Schmidt }
1047fe723b9aSBill Schmidt }
1048fe723b9aSBill Schmidt
1049fe723b9aSBill Schmidt if (SwapVector[EntryIdx].WebRejected)
10508c209aa8SMatthias Braun dbgs() << "rejected ";
1051fe723b9aSBill Schmidt if (SwapVector[EntryIdx].WillRemove)
10528c209aa8SMatthias Braun dbgs() << "remove ";
1053fe723b9aSBill Schmidt
10548c209aa8SMatthias Braun dbgs() << "\n";
1055e71db85bSBill Schmidt
1056e71db85bSBill Schmidt // For no-asserts builds.
1057e71db85bSBill Schmidt (void)MI;
1058e71db85bSBill Schmidt (void)ID;
1059fe723b9aSBill Schmidt }
1060fe723b9aSBill Schmidt
10618c209aa8SMatthias Braun dbgs() << "\n";
1062fe723b9aSBill Schmidt }
10638c209aa8SMatthias Braun #endif
1064fe723b9aSBill Schmidt
1065f00654e3SAlexander Kornienko } // end default namespace
1066fe723b9aSBill Schmidt
1067fe723b9aSBill Schmidt INITIALIZE_PASS_BEGIN(PPCVSXSwapRemoval, DEBUG_TYPE,
1068fe723b9aSBill Schmidt "PowerPC VSX Swap Removal", false, false)
1069fe723b9aSBill Schmidt INITIALIZE_PASS_END(PPCVSXSwapRemoval, DEBUG_TYPE,
1070fe723b9aSBill Schmidt "PowerPC VSX Swap Removal", false, false)
1071fe723b9aSBill Schmidt
1072fe723b9aSBill Schmidt char PPCVSXSwapRemoval::ID = 0;
1073fe723b9aSBill Schmidt FunctionPass*
createPPCVSXSwapRemovalPass()1074fe723b9aSBill Schmidt llvm::createPPCVSXSwapRemovalPass() { return new PPCVSXSwapRemoval(); }
1075