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