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