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