109467b48Spatrick //===----------- PPCVSXSwapRemoval.cpp - Remove VSX LE Swaps -------------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===---------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This pass analyzes vector computations and removes unnecessary
1009467b48Spatrick // doubleword swaps (xxswapd instructions). This pass is performed
1109467b48Spatrick // only for little-endian VSX code generation.
1209467b48Spatrick //
1309467b48Spatrick // For this specific case, loads and stores of v4i32, v4f32, v2i64,
1409467b48Spatrick // and v2f64 vectors are inefficient. These are implemented using
1509467b48Spatrick // the lxvd2x and stxvd2x instructions, which invert the order of
1609467b48Spatrick // doublewords in a vector register. Thus code generation inserts
1709467b48Spatrick // an xxswapd after each such load, and prior to each such store.
1809467b48Spatrick //
1909467b48Spatrick // The extra xxswapd instructions reduce performance. The purpose
2009467b48Spatrick // of this pass is to reduce the number of xxswapd instructions
2109467b48Spatrick // required for correctness.
2209467b48Spatrick //
2309467b48Spatrick // The primary insight is that much code that operates on vectors
2409467b48Spatrick // does not care about the relative order of elements in a register,
2509467b48Spatrick // so long as the correct memory order is preserved. If we have a
2609467b48Spatrick // computation where all input values are provided by lxvd2x/xxswapd,
2709467b48Spatrick // all outputs are stored using xxswapd/lxvd2x, and all intermediate
2809467b48Spatrick // computations are lane-insensitive (independent of element order),
2909467b48Spatrick // then all the xxswapd instructions associated with the loads and
3009467b48Spatrick // stores may be removed without changing observable semantics.
3109467b48Spatrick //
3209467b48Spatrick // This pass uses standard equivalence class infrastructure to create
3309467b48Spatrick // maximal webs of computations fitting the above description. Each
3409467b48Spatrick // such web is then optimized by removing its unnecessary xxswapd
3509467b48Spatrick // instructions.
3609467b48Spatrick //
3709467b48Spatrick // There are some lane-sensitive operations for which we can still
3809467b48Spatrick // permit the optimization, provided we modify those operations
3909467b48Spatrick // accordingly. Such operations are identified as using "special
4009467b48Spatrick // handling" within this module.
4109467b48Spatrick //
4209467b48Spatrick //===---------------------------------------------------------------------===//
4309467b48Spatrick
4409467b48Spatrick #include "PPC.h"
4509467b48Spatrick #include "PPCInstrBuilder.h"
4609467b48Spatrick #include "PPCInstrInfo.h"
4709467b48Spatrick #include "PPCTargetMachine.h"
4809467b48Spatrick #include "llvm/ADT/DenseMap.h"
4909467b48Spatrick #include "llvm/ADT/EquivalenceClasses.h"
5009467b48Spatrick #include "llvm/CodeGen/MachineFunctionPass.h"
5109467b48Spatrick #include "llvm/CodeGen/MachineInstrBuilder.h"
5209467b48Spatrick #include "llvm/CodeGen/MachineRegisterInfo.h"
5309467b48Spatrick #include "llvm/Config/llvm-config.h"
5409467b48Spatrick #include "llvm/Support/Debug.h"
5509467b48Spatrick #include "llvm/Support/Format.h"
5609467b48Spatrick #include "llvm/Support/raw_ostream.h"
5709467b48Spatrick
5809467b48Spatrick using namespace llvm;
5909467b48Spatrick
6009467b48Spatrick #define DEBUG_TYPE "ppc-vsx-swaps"
6109467b48Spatrick
6209467b48Spatrick namespace {
6309467b48Spatrick
6409467b48Spatrick // A PPCVSXSwapEntry is created for each machine instruction that
6509467b48Spatrick // is relevant to a vector computation.
6609467b48Spatrick struct PPCVSXSwapEntry {
6709467b48Spatrick // Pointer to the instruction.
6809467b48Spatrick MachineInstr *VSEMI;
6909467b48Spatrick
7009467b48Spatrick // Unique ID (position in the swap vector).
7109467b48Spatrick int VSEId;
7209467b48Spatrick
7309467b48Spatrick // Attributes of this node.
7409467b48Spatrick unsigned int IsLoad : 1;
7509467b48Spatrick unsigned int IsStore : 1;
7609467b48Spatrick unsigned int IsSwap : 1;
7709467b48Spatrick unsigned int MentionsPhysVR : 1;
7809467b48Spatrick unsigned int IsSwappable : 1;
7909467b48Spatrick unsigned int MentionsPartialVR : 1;
8009467b48Spatrick unsigned int SpecialHandling : 3;
8109467b48Spatrick unsigned int WebRejected : 1;
8209467b48Spatrick unsigned int WillRemove : 1;
8309467b48Spatrick };
8409467b48Spatrick
8509467b48Spatrick enum SHValues {
8609467b48Spatrick SH_NONE = 0,
8709467b48Spatrick SH_EXTRACT,
8809467b48Spatrick SH_INSERT,
8909467b48Spatrick SH_NOSWAP_LD,
9009467b48Spatrick SH_NOSWAP_ST,
9109467b48Spatrick SH_SPLAT,
9209467b48Spatrick SH_XXPERMDI,
9309467b48Spatrick SH_COPYWIDEN
9409467b48Spatrick };
9509467b48Spatrick
9609467b48Spatrick struct PPCVSXSwapRemoval : public MachineFunctionPass {
9709467b48Spatrick
9809467b48Spatrick static char ID;
9909467b48Spatrick const PPCInstrInfo *TII;
10009467b48Spatrick MachineFunction *MF;
10109467b48Spatrick MachineRegisterInfo *MRI;
10209467b48Spatrick
10309467b48Spatrick // Swap entries are allocated in a vector for better performance.
10409467b48Spatrick std::vector<PPCVSXSwapEntry> SwapVector;
10509467b48Spatrick
10609467b48Spatrick // A mapping is maintained between machine instructions and
10709467b48Spatrick // their swap entries. The key is the address of the MI.
10809467b48Spatrick DenseMap<MachineInstr*, int> SwapMap;
10909467b48Spatrick
11009467b48Spatrick // Equivalence classes are used to gather webs of related computation.
11109467b48Spatrick // Swap entries are represented by their VSEId fields.
11209467b48Spatrick EquivalenceClasses<int> *EC;
11309467b48Spatrick
PPCVSXSwapRemoval__anona10f4e210111::PPCVSXSwapRemoval11409467b48Spatrick PPCVSXSwapRemoval() : MachineFunctionPass(ID) {
11509467b48Spatrick initializePPCVSXSwapRemovalPass(*PassRegistry::getPassRegistry());
11609467b48Spatrick }
11709467b48Spatrick
11809467b48Spatrick private:
11909467b48Spatrick // Initialize data structures.
12009467b48Spatrick void initialize(MachineFunction &MFParm);
12109467b48Spatrick
12209467b48Spatrick // Walk the machine instructions to gather vector usage information.
12309467b48Spatrick // Return true iff vector mentions are present.
12409467b48Spatrick bool gatherVectorInstructions();
12509467b48Spatrick
12609467b48Spatrick // Add an entry to the swap vector and swap map.
12709467b48Spatrick int addSwapEntry(MachineInstr *MI, PPCVSXSwapEntry &SwapEntry);
12809467b48Spatrick
12909467b48Spatrick // Hunt backwards through COPY and SUBREG_TO_REG chains for a
13009467b48Spatrick // source register. VecIdx indicates the swap vector entry to
13109467b48Spatrick // mark as mentioning a physical register if the search leads
13209467b48Spatrick // to one.
13309467b48Spatrick unsigned lookThruCopyLike(unsigned SrcReg, unsigned VecIdx);
13409467b48Spatrick
13509467b48Spatrick // Generate equivalence classes for related computations (webs).
13609467b48Spatrick void formWebs();
13709467b48Spatrick
13809467b48Spatrick // Analyze webs and determine those that cannot be optimized.
13909467b48Spatrick void recordUnoptimizableWebs();
14009467b48Spatrick
14109467b48Spatrick // Record which swap instructions can be safely removed.
14209467b48Spatrick void markSwapsForRemoval();
14309467b48Spatrick
14409467b48Spatrick // Remove swaps and update other instructions requiring special
14509467b48Spatrick // handling. Return true iff any changes are made.
14609467b48Spatrick bool removeSwaps();
14709467b48Spatrick
14809467b48Spatrick // Insert a swap instruction from SrcReg to DstReg at the given
14909467b48Spatrick // InsertPoint.
15009467b48Spatrick void insertSwap(MachineInstr *MI, MachineBasicBlock::iterator InsertPoint,
15109467b48Spatrick unsigned DstReg, unsigned SrcReg);
15209467b48Spatrick
15309467b48Spatrick // Update instructions requiring special handling.
15409467b48Spatrick void handleSpecialSwappables(int EntryIdx);
15509467b48Spatrick
15609467b48Spatrick // Dump a description of the entries in the swap vector.
15709467b48Spatrick void dumpSwapVector();
15809467b48Spatrick
15909467b48Spatrick // Return true iff the given register is in the given class.
isRegInClass__anona10f4e210111::PPCVSXSwapRemoval16009467b48Spatrick bool isRegInClass(unsigned Reg, const TargetRegisterClass *RC) {
16109467b48Spatrick if (Register::isVirtualRegister(Reg))
16209467b48Spatrick return RC->hasSubClassEq(MRI->getRegClass(Reg));
16309467b48Spatrick return RC->contains(Reg);
16409467b48Spatrick }
16509467b48Spatrick
16609467b48Spatrick // Return true iff the given register is a full vector register.
isVecReg__anona10f4e210111::PPCVSXSwapRemoval16709467b48Spatrick bool isVecReg(unsigned Reg) {
16809467b48Spatrick return (isRegInClass(Reg, &PPC::VSRCRegClass) ||
16909467b48Spatrick isRegInClass(Reg, &PPC::VRRCRegClass));
17009467b48Spatrick }
17109467b48Spatrick
17209467b48Spatrick // Return true iff the given register is a partial vector register.
isScalarVecReg__anona10f4e210111::PPCVSXSwapRemoval17309467b48Spatrick bool isScalarVecReg(unsigned Reg) {
17409467b48Spatrick return (isRegInClass(Reg, &PPC::VSFRCRegClass) ||
17509467b48Spatrick isRegInClass(Reg, &PPC::VSSRCRegClass));
17609467b48Spatrick }
17709467b48Spatrick
17809467b48Spatrick // Return true iff the given register mentions all or part of a
17909467b48Spatrick // vector register. Also sets Partial to true if the mention
18009467b48Spatrick // is for just the floating-point register overlap of the register.
isAnyVecReg__anona10f4e210111::PPCVSXSwapRemoval18109467b48Spatrick bool isAnyVecReg(unsigned Reg, bool &Partial) {
18209467b48Spatrick if (isScalarVecReg(Reg))
18309467b48Spatrick Partial = true;
18409467b48Spatrick return isScalarVecReg(Reg) || isVecReg(Reg);
18509467b48Spatrick }
18609467b48Spatrick
18709467b48Spatrick public:
18809467b48Spatrick // Main entry point for this pass.
runOnMachineFunction__anona10f4e210111::PPCVSXSwapRemoval18909467b48Spatrick bool runOnMachineFunction(MachineFunction &MF) override {
19009467b48Spatrick if (skipFunction(MF.getFunction()))
19109467b48Spatrick return false;
19209467b48Spatrick
19309467b48Spatrick // If we don't have VSX on the subtarget, don't do anything.
19409467b48Spatrick // Also, on Power 9 the load and store ops preserve element order and so
19509467b48Spatrick // the swaps are not required.
19609467b48Spatrick const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
19709467b48Spatrick if (!STI.hasVSX() || !STI.needsSwapsForVSXMemOps())
19809467b48Spatrick return false;
19909467b48Spatrick
20009467b48Spatrick bool Changed = false;
20109467b48Spatrick initialize(MF);
20209467b48Spatrick
20309467b48Spatrick if (gatherVectorInstructions()) {
20409467b48Spatrick formWebs();
20509467b48Spatrick recordUnoptimizableWebs();
20609467b48Spatrick markSwapsForRemoval();
20709467b48Spatrick Changed = removeSwaps();
20809467b48Spatrick }
20909467b48Spatrick
21009467b48Spatrick // FIXME: See the allocation of EC in initialize().
21109467b48Spatrick delete EC;
21209467b48Spatrick return Changed;
21309467b48Spatrick }
21409467b48Spatrick };
21509467b48Spatrick
21609467b48Spatrick // Initialize data structures for this pass. In particular, clear the
21709467b48Spatrick // swap vector and allocate the equivalence class mapping before
21809467b48Spatrick // processing each function.
initialize(MachineFunction & MFParm)21909467b48Spatrick void PPCVSXSwapRemoval::initialize(MachineFunction &MFParm) {
22009467b48Spatrick MF = &MFParm;
22109467b48Spatrick MRI = &MF->getRegInfo();
22209467b48Spatrick TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo();
22309467b48Spatrick
22409467b48Spatrick // An initial vector size of 256 appears to work well in practice.
22509467b48Spatrick // Small/medium functions with vector content tend not to incur a
22609467b48Spatrick // reallocation at this size. Three of the vector tests in
22709467b48Spatrick // projects/test-suite reallocate, which seems like a reasonable rate.
22809467b48Spatrick const int InitialVectorSize(256);
22909467b48Spatrick SwapVector.clear();
23009467b48Spatrick SwapVector.reserve(InitialVectorSize);
23109467b48Spatrick
23209467b48Spatrick // FIXME: Currently we allocate EC each time because we don't have
23309467b48Spatrick // access to the set representation on which to call clear(). Should
23409467b48Spatrick // consider adding a clear() method to the EquivalenceClasses class.
23509467b48Spatrick EC = new EquivalenceClasses<int>;
23609467b48Spatrick }
23709467b48Spatrick
23809467b48Spatrick // Create an entry in the swap vector for each instruction that mentions
23909467b48Spatrick // a full vector register, recording various characteristics of the
24009467b48Spatrick // instructions there.
gatherVectorInstructions()24109467b48Spatrick bool PPCVSXSwapRemoval::gatherVectorInstructions() {
24209467b48Spatrick bool RelevantFunction = false;
24309467b48Spatrick
24409467b48Spatrick for (MachineBasicBlock &MBB : *MF) {
24509467b48Spatrick for (MachineInstr &MI : MBB) {
24609467b48Spatrick
24709467b48Spatrick if (MI.isDebugInstr())
24809467b48Spatrick continue;
24909467b48Spatrick
25009467b48Spatrick bool RelevantInstr = false;
25109467b48Spatrick bool Partial = false;
25209467b48Spatrick
25309467b48Spatrick for (const MachineOperand &MO : MI.operands()) {
25409467b48Spatrick if (!MO.isReg())
25509467b48Spatrick continue;
25609467b48Spatrick Register Reg = MO.getReg();
25773471bf0Spatrick // All operands need to be checked because there are instructions that
25873471bf0Spatrick // operate on a partial register and produce a full register (such as
25973471bf0Spatrick // XXPERMDIs).
26073471bf0Spatrick if (isAnyVecReg(Reg, Partial))
26109467b48Spatrick RelevantInstr = true;
26209467b48Spatrick }
26309467b48Spatrick
26409467b48Spatrick if (!RelevantInstr)
26509467b48Spatrick continue;
26609467b48Spatrick
26709467b48Spatrick RelevantFunction = true;
26809467b48Spatrick
26909467b48Spatrick // Create a SwapEntry initialized to zeros, then fill in the
27009467b48Spatrick // instruction and ID fields before pushing it to the back
27109467b48Spatrick // of the swap vector.
27209467b48Spatrick PPCVSXSwapEntry SwapEntry{};
27309467b48Spatrick int VecIdx = addSwapEntry(&MI, SwapEntry);
27409467b48Spatrick
27509467b48Spatrick switch(MI.getOpcode()) {
27609467b48Spatrick default:
27709467b48Spatrick // Unless noted otherwise, an instruction is considered
27809467b48Spatrick // safe for the optimization. There are a large number of
27909467b48Spatrick // such true-SIMD instructions (all vector math, logical,
28009467b48Spatrick // select, compare, etc.). However, if the instruction
28109467b48Spatrick // mentions a partial vector register and does not have
28209467b48Spatrick // special handling defined, it is not swappable.
28309467b48Spatrick if (Partial)
28409467b48Spatrick SwapVector[VecIdx].MentionsPartialVR = 1;
28509467b48Spatrick else
28609467b48Spatrick SwapVector[VecIdx].IsSwappable = 1;
28709467b48Spatrick break;
28809467b48Spatrick case PPC::XXPERMDI: {
28909467b48Spatrick // This is a swap if it is of the form XXPERMDI t, s, s, 2.
29009467b48Spatrick // Unfortunately, MachineCSE ignores COPY and SUBREG_TO_REG, so we
29109467b48Spatrick // can also see XXPERMDI t, SUBREG_TO_REG(s), SUBREG_TO_REG(s), 2,
29209467b48Spatrick // for example. We have to look through chains of COPY and
29309467b48Spatrick // SUBREG_TO_REG to find the real source value for comparison.
29409467b48Spatrick // If the real source value is a physical register, then mark the
29509467b48Spatrick // XXPERMDI as mentioning a physical register.
29609467b48Spatrick int immed = MI.getOperand(3).getImm();
29709467b48Spatrick if (immed == 2) {
29809467b48Spatrick unsigned trueReg1 = lookThruCopyLike(MI.getOperand(1).getReg(),
29909467b48Spatrick VecIdx);
30009467b48Spatrick unsigned trueReg2 = lookThruCopyLike(MI.getOperand(2).getReg(),
30109467b48Spatrick VecIdx);
30209467b48Spatrick if (trueReg1 == trueReg2)
30309467b48Spatrick SwapVector[VecIdx].IsSwap = 1;
30409467b48Spatrick else {
30509467b48Spatrick // We can still handle these if the two registers are not
30609467b48Spatrick // identical, by adjusting the form of the XXPERMDI.
30709467b48Spatrick SwapVector[VecIdx].IsSwappable = 1;
30809467b48Spatrick SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
30909467b48Spatrick }
31009467b48Spatrick // This is a doubleword splat if it is of the form
31109467b48Spatrick // XXPERMDI t, s, s, 0 or XXPERMDI t, s, s, 3. As above we
31209467b48Spatrick // must look through chains of copy-likes to find the source
31309467b48Spatrick // register. We turn off the marking for mention of a physical
31409467b48Spatrick // register, because splatting it is safe; the optimization
31509467b48Spatrick // will not swap the value in the physical register. Whether
31609467b48Spatrick // or not the two input registers are identical, we can handle
31709467b48Spatrick // these by adjusting the form of the XXPERMDI.
31809467b48Spatrick } else if (immed == 0 || immed == 3) {
31909467b48Spatrick
32009467b48Spatrick SwapVector[VecIdx].IsSwappable = 1;
32109467b48Spatrick SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
32209467b48Spatrick
32309467b48Spatrick unsigned trueReg1 = lookThruCopyLike(MI.getOperand(1).getReg(),
32409467b48Spatrick VecIdx);
32509467b48Spatrick unsigned trueReg2 = lookThruCopyLike(MI.getOperand(2).getReg(),
32609467b48Spatrick VecIdx);
32709467b48Spatrick if (trueReg1 == trueReg2)
32809467b48Spatrick SwapVector[VecIdx].MentionsPhysVR = 0;
32909467b48Spatrick
33009467b48Spatrick } else {
33109467b48Spatrick // We can still handle these by adjusting the form of the XXPERMDI.
33209467b48Spatrick SwapVector[VecIdx].IsSwappable = 1;
33309467b48Spatrick SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
33409467b48Spatrick }
33509467b48Spatrick break;
33609467b48Spatrick }
33709467b48Spatrick case PPC::LVX:
33809467b48Spatrick // Non-permuting loads are currently unsafe. We can use special
33909467b48Spatrick // handling for this in the future. By not marking these as
34009467b48Spatrick // IsSwap, we ensure computations containing them will be rejected
34109467b48Spatrick // for now.
34209467b48Spatrick SwapVector[VecIdx].IsLoad = 1;
34309467b48Spatrick break;
34409467b48Spatrick case PPC::LXVD2X:
34509467b48Spatrick case PPC::LXVW4X:
34609467b48Spatrick // Permuting loads are marked as both load and swap, and are
34709467b48Spatrick // safe for optimization.
34809467b48Spatrick SwapVector[VecIdx].IsLoad = 1;
34909467b48Spatrick SwapVector[VecIdx].IsSwap = 1;
35009467b48Spatrick break;
35109467b48Spatrick case PPC::LXSDX:
35209467b48Spatrick case PPC::LXSSPX:
35309467b48Spatrick case PPC::XFLOADf64:
35409467b48Spatrick case PPC::XFLOADf32:
35509467b48Spatrick // A load of a floating-point value into the high-order half of
35609467b48Spatrick // a vector register is safe, provided that we introduce a swap
35709467b48Spatrick // following the load, which will be done by the SUBREG_TO_REG
35809467b48Spatrick // support. So just mark these as safe.
35909467b48Spatrick SwapVector[VecIdx].IsLoad = 1;
36009467b48Spatrick SwapVector[VecIdx].IsSwappable = 1;
36109467b48Spatrick break;
36209467b48Spatrick case PPC::STVX:
36309467b48Spatrick // Non-permuting stores are currently unsafe. We can use special
36409467b48Spatrick // handling for this in the future. By not marking these as
36509467b48Spatrick // IsSwap, we ensure computations containing them will be rejected
36609467b48Spatrick // for now.
36709467b48Spatrick SwapVector[VecIdx].IsStore = 1;
36809467b48Spatrick break;
36909467b48Spatrick case PPC::STXVD2X:
37009467b48Spatrick case PPC::STXVW4X:
37109467b48Spatrick // Permuting stores are marked as both store and swap, and are
37209467b48Spatrick // safe for optimization.
37309467b48Spatrick SwapVector[VecIdx].IsStore = 1;
37409467b48Spatrick SwapVector[VecIdx].IsSwap = 1;
37509467b48Spatrick break;
37609467b48Spatrick case PPC::COPY:
37709467b48Spatrick // These are fine provided they are moving between full vector
37809467b48Spatrick // register classes.
37909467b48Spatrick if (isVecReg(MI.getOperand(0).getReg()) &&
38009467b48Spatrick isVecReg(MI.getOperand(1).getReg()))
38109467b48Spatrick SwapVector[VecIdx].IsSwappable = 1;
38209467b48Spatrick // If we have a copy from one scalar floating-point register
38309467b48Spatrick // to another, we can accept this even if it is a physical
38409467b48Spatrick // register. The only way this gets involved is if it feeds
38509467b48Spatrick // a SUBREG_TO_REG, which is handled by introducing a swap.
38609467b48Spatrick else if (isScalarVecReg(MI.getOperand(0).getReg()) &&
38709467b48Spatrick isScalarVecReg(MI.getOperand(1).getReg()))
38809467b48Spatrick SwapVector[VecIdx].IsSwappable = 1;
38909467b48Spatrick break;
39009467b48Spatrick case PPC::SUBREG_TO_REG: {
39109467b48Spatrick // These are fine provided they are moving between full vector
39209467b48Spatrick // register classes. If they are moving from a scalar
39309467b48Spatrick // floating-point class to a vector class, we can handle those
39409467b48Spatrick // as well, provided we introduce a swap. It is generally the
39509467b48Spatrick // case that we will introduce fewer swaps than we remove, but
39609467b48Spatrick // (FIXME) a cost model could be used. However, introduced
39709467b48Spatrick // swaps could potentially be CSEd, so this is not trivial.
39809467b48Spatrick if (isVecReg(MI.getOperand(0).getReg()) &&
39909467b48Spatrick isVecReg(MI.getOperand(2).getReg()))
40009467b48Spatrick SwapVector[VecIdx].IsSwappable = 1;
40109467b48Spatrick else if (isVecReg(MI.getOperand(0).getReg()) &&
40209467b48Spatrick isScalarVecReg(MI.getOperand(2).getReg())) {
40309467b48Spatrick SwapVector[VecIdx].IsSwappable = 1;
40409467b48Spatrick SwapVector[VecIdx].SpecialHandling = SHValues::SH_COPYWIDEN;
40509467b48Spatrick }
40609467b48Spatrick break;
40709467b48Spatrick }
40809467b48Spatrick case PPC::VSPLTB:
40909467b48Spatrick case PPC::VSPLTH:
41009467b48Spatrick case PPC::VSPLTW:
41109467b48Spatrick case PPC::XXSPLTW:
41209467b48Spatrick // Splats are lane-sensitive, but we can use special handling
41309467b48Spatrick // to adjust the source lane for the splat.
41409467b48Spatrick SwapVector[VecIdx].IsSwappable = 1;
41509467b48Spatrick SwapVector[VecIdx].SpecialHandling = SHValues::SH_SPLAT;
41609467b48Spatrick break;
41709467b48Spatrick // The presence of the following lane-sensitive operations in a
41809467b48Spatrick // web will kill the optimization, at least for now. For these
41909467b48Spatrick // we do nothing, causing the optimization to fail.
42009467b48Spatrick // FIXME: Some of these could be permitted with special handling,
42109467b48Spatrick // and will be phased in as time permits.
42209467b48Spatrick // FIXME: There is no simple and maintainable way to express a set
42309467b48Spatrick // of opcodes having a common attribute in TableGen. Should this
42409467b48Spatrick // change, this is a prime candidate to use such a mechanism.
42509467b48Spatrick case PPC::INLINEASM:
42609467b48Spatrick case PPC::INLINEASM_BR:
42709467b48Spatrick case PPC::EXTRACT_SUBREG:
42809467b48Spatrick case PPC::INSERT_SUBREG:
42909467b48Spatrick case PPC::COPY_TO_REGCLASS:
43009467b48Spatrick case PPC::LVEBX:
43109467b48Spatrick case PPC::LVEHX:
43209467b48Spatrick case PPC::LVEWX:
43309467b48Spatrick case PPC::LVSL:
43409467b48Spatrick case PPC::LVSR:
43509467b48Spatrick case PPC::LVXL:
43609467b48Spatrick case PPC::STVEBX:
43709467b48Spatrick case PPC::STVEHX:
43809467b48Spatrick case PPC::STVEWX:
43909467b48Spatrick case PPC::STVXL:
44009467b48Spatrick // We can handle STXSDX and STXSSPX similarly to LXSDX and LXSSPX,
44109467b48Spatrick // by adding special handling for narrowing copies as well as
44209467b48Spatrick // widening ones. However, I've experimented with this, and in
44309467b48Spatrick // practice we currently do not appear to use STXSDX fed by
44409467b48Spatrick // a narrowing copy from a full vector register. Since I can't
44509467b48Spatrick // generate any useful test cases, I've left this alone for now.
44609467b48Spatrick case PPC::STXSDX:
44709467b48Spatrick case PPC::STXSSPX:
44809467b48Spatrick case PPC::VCIPHER:
44909467b48Spatrick case PPC::VCIPHERLAST:
45009467b48Spatrick case PPC::VMRGHB:
45109467b48Spatrick case PPC::VMRGHH:
45209467b48Spatrick case PPC::VMRGHW:
45309467b48Spatrick case PPC::VMRGLB:
45409467b48Spatrick case PPC::VMRGLH:
45509467b48Spatrick case PPC::VMRGLW:
45609467b48Spatrick case PPC::VMULESB:
45709467b48Spatrick case PPC::VMULESH:
45809467b48Spatrick case PPC::VMULESW:
45909467b48Spatrick case PPC::VMULEUB:
46009467b48Spatrick case PPC::VMULEUH:
46109467b48Spatrick case PPC::VMULEUW:
46209467b48Spatrick case PPC::VMULOSB:
46309467b48Spatrick case PPC::VMULOSH:
46409467b48Spatrick case PPC::VMULOSW:
46509467b48Spatrick case PPC::VMULOUB:
46609467b48Spatrick case PPC::VMULOUH:
46709467b48Spatrick case PPC::VMULOUW:
46809467b48Spatrick case PPC::VNCIPHER:
46909467b48Spatrick case PPC::VNCIPHERLAST:
47009467b48Spatrick case PPC::VPERM:
47109467b48Spatrick case PPC::VPERMXOR:
47209467b48Spatrick case PPC::VPKPX:
47309467b48Spatrick case PPC::VPKSHSS:
47409467b48Spatrick case PPC::VPKSHUS:
47509467b48Spatrick case PPC::VPKSDSS:
47609467b48Spatrick case PPC::VPKSDUS:
47709467b48Spatrick case PPC::VPKSWSS:
47809467b48Spatrick case PPC::VPKSWUS:
47909467b48Spatrick case PPC::VPKUDUM:
48009467b48Spatrick case PPC::VPKUDUS:
48109467b48Spatrick case PPC::VPKUHUM:
48209467b48Spatrick case PPC::VPKUHUS:
48309467b48Spatrick case PPC::VPKUWUM:
48409467b48Spatrick case PPC::VPKUWUS:
48509467b48Spatrick case PPC::VPMSUMB:
48609467b48Spatrick case PPC::VPMSUMD:
48709467b48Spatrick case PPC::VPMSUMH:
48809467b48Spatrick case PPC::VPMSUMW:
48909467b48Spatrick case PPC::VRLB:
49009467b48Spatrick case PPC::VRLD:
49109467b48Spatrick case PPC::VRLH:
49209467b48Spatrick case PPC::VRLW:
49309467b48Spatrick case PPC::VSBOX:
49409467b48Spatrick case PPC::VSHASIGMAD:
49509467b48Spatrick case PPC::VSHASIGMAW:
49609467b48Spatrick case PPC::VSL:
49709467b48Spatrick case PPC::VSLDOI:
49809467b48Spatrick case PPC::VSLO:
49909467b48Spatrick case PPC::VSR:
50009467b48Spatrick case PPC::VSRO:
50109467b48Spatrick case PPC::VSUM2SWS:
50209467b48Spatrick case PPC::VSUM4SBS:
50309467b48Spatrick case PPC::VSUM4SHS:
50409467b48Spatrick case PPC::VSUM4UBS:
50509467b48Spatrick case PPC::VSUMSWS:
50609467b48Spatrick case PPC::VUPKHPX:
50709467b48Spatrick case PPC::VUPKHSB:
50809467b48Spatrick case PPC::VUPKHSH:
50909467b48Spatrick case PPC::VUPKHSW:
51009467b48Spatrick case PPC::VUPKLPX:
51109467b48Spatrick case PPC::VUPKLSB:
51209467b48Spatrick case PPC::VUPKLSH:
51309467b48Spatrick case PPC::VUPKLSW:
51409467b48Spatrick case PPC::XXMRGHW:
51509467b48Spatrick case PPC::XXMRGLW:
51609467b48Spatrick // XXSLDWI could be replaced by a general permute with one of three
51709467b48Spatrick // permute control vectors (for shift values 1, 2, 3). However,
51809467b48Spatrick // VPERM has a more restrictive register class.
51909467b48Spatrick case PPC::XXSLDWI:
52009467b48Spatrick case PPC::XSCVDPSPN:
52109467b48Spatrick case PPC::XSCVSPDPN:
522*d415bd75Srobert case PPC::MTVSCR:
523*d415bd75Srobert case PPC::MFVSCR:
52409467b48Spatrick break;
52509467b48Spatrick }
52609467b48Spatrick }
52709467b48Spatrick }
52809467b48Spatrick
52909467b48Spatrick if (RelevantFunction) {
53009467b48Spatrick LLVM_DEBUG(dbgs() << "Swap vector when first built\n\n");
53109467b48Spatrick LLVM_DEBUG(dumpSwapVector());
53209467b48Spatrick }
53309467b48Spatrick
53409467b48Spatrick return RelevantFunction;
53509467b48Spatrick }
53609467b48Spatrick
53709467b48Spatrick // Add an entry to the swap vector and swap map, and make a
53809467b48Spatrick // singleton equivalence class for the entry.
addSwapEntry(MachineInstr * MI,PPCVSXSwapEntry & SwapEntry)53909467b48Spatrick int PPCVSXSwapRemoval::addSwapEntry(MachineInstr *MI,
54009467b48Spatrick PPCVSXSwapEntry& SwapEntry) {
54109467b48Spatrick SwapEntry.VSEMI = MI;
54209467b48Spatrick SwapEntry.VSEId = SwapVector.size();
54309467b48Spatrick SwapVector.push_back(SwapEntry);
54409467b48Spatrick EC->insert(SwapEntry.VSEId);
54509467b48Spatrick SwapMap[MI] = SwapEntry.VSEId;
54609467b48Spatrick return SwapEntry.VSEId;
54709467b48Spatrick }
54809467b48Spatrick
54909467b48Spatrick // This is used to find the "true" source register for an
55009467b48Spatrick // XXPERMDI instruction, since MachineCSE does not handle the
55109467b48Spatrick // "copy-like" operations (Copy and SubregToReg). Returns
55209467b48Spatrick // the original SrcReg unless it is the target of a copy-like
55309467b48Spatrick // operation, in which case we chain backwards through all
55409467b48Spatrick // such operations to the ultimate source register. If a
55509467b48Spatrick // physical register is encountered, we stop the search and
55609467b48Spatrick // flag the swap entry indicated by VecIdx (the original
55709467b48Spatrick // XXPERMDI) as mentioning a physical register.
lookThruCopyLike(unsigned SrcReg,unsigned VecIdx)55809467b48Spatrick unsigned PPCVSXSwapRemoval::lookThruCopyLike(unsigned SrcReg,
55909467b48Spatrick unsigned VecIdx) {
56009467b48Spatrick MachineInstr *MI = MRI->getVRegDef(SrcReg);
56109467b48Spatrick if (!MI->isCopyLike())
56209467b48Spatrick return SrcReg;
56309467b48Spatrick
56409467b48Spatrick unsigned CopySrcReg;
56509467b48Spatrick if (MI->isCopy())
56609467b48Spatrick CopySrcReg = MI->getOperand(1).getReg();
56709467b48Spatrick else {
56809467b48Spatrick assert(MI->isSubregToReg() && "bad opcode for lookThruCopyLike");
56909467b48Spatrick CopySrcReg = MI->getOperand(2).getReg();
57009467b48Spatrick }
57109467b48Spatrick
57209467b48Spatrick if (!Register::isVirtualRegister(CopySrcReg)) {
57309467b48Spatrick if (!isScalarVecReg(CopySrcReg))
57409467b48Spatrick SwapVector[VecIdx].MentionsPhysVR = 1;
57509467b48Spatrick return CopySrcReg;
57609467b48Spatrick }
57709467b48Spatrick
57809467b48Spatrick return lookThruCopyLike(CopySrcReg, VecIdx);
57909467b48Spatrick }
58009467b48Spatrick
58109467b48Spatrick // Generate equivalence classes for related computations (webs) by
58209467b48Spatrick // def-use relationships of virtual registers. Mention of a physical
58309467b48Spatrick // register terminates the generation of equivalence classes as this
58409467b48Spatrick // indicates a use of a parameter, definition of a return value, use
58509467b48Spatrick // of a value returned from a call, or definition of a parameter to a
58609467b48Spatrick // call. Computations with physical register mentions are flagged
58709467b48Spatrick // as such so their containing webs will not be optimized.
formWebs()58809467b48Spatrick void PPCVSXSwapRemoval::formWebs() {
58909467b48Spatrick
59009467b48Spatrick LLVM_DEBUG(dbgs() << "\n*** Forming webs for swap removal ***\n\n");
59109467b48Spatrick
59209467b48Spatrick for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
59309467b48Spatrick
59409467b48Spatrick MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
59509467b48Spatrick
59609467b48Spatrick LLVM_DEBUG(dbgs() << "\n" << SwapVector[EntryIdx].VSEId << " ");
59709467b48Spatrick LLVM_DEBUG(MI->dump());
59809467b48Spatrick
59909467b48Spatrick // It's sufficient to walk vector uses and join them to their unique
60009467b48Spatrick // definitions. In addition, check full vector register operands
60109467b48Spatrick // for physical regs. We exclude partial-vector register operands
60209467b48Spatrick // because we can handle them if copied to a full vector.
60309467b48Spatrick for (const MachineOperand &MO : MI->operands()) {
60409467b48Spatrick if (!MO.isReg())
60509467b48Spatrick continue;
60609467b48Spatrick
60709467b48Spatrick Register Reg = MO.getReg();
60809467b48Spatrick if (!isVecReg(Reg) && !isScalarVecReg(Reg))
60909467b48Spatrick continue;
61009467b48Spatrick
611*d415bd75Srobert if (!Reg.isVirtual()) {
61209467b48Spatrick if (!(MI->isCopy() && isScalarVecReg(Reg)))
61309467b48Spatrick SwapVector[EntryIdx].MentionsPhysVR = 1;
61409467b48Spatrick continue;
61509467b48Spatrick }
61609467b48Spatrick
61709467b48Spatrick if (!MO.isUse())
61809467b48Spatrick continue;
61909467b48Spatrick
62009467b48Spatrick MachineInstr* DefMI = MRI->getVRegDef(Reg);
62109467b48Spatrick assert(SwapMap.find(DefMI) != SwapMap.end() &&
62209467b48Spatrick "Inconsistency: def of vector reg not found in swap map!");
62309467b48Spatrick int DefIdx = SwapMap[DefMI];
62409467b48Spatrick (void)EC->unionSets(SwapVector[DefIdx].VSEId,
62509467b48Spatrick SwapVector[EntryIdx].VSEId);
62609467b48Spatrick
62709467b48Spatrick LLVM_DEBUG(dbgs() << format("Unioning %d with %d\n",
62809467b48Spatrick SwapVector[DefIdx].VSEId,
62909467b48Spatrick SwapVector[EntryIdx].VSEId));
63009467b48Spatrick LLVM_DEBUG(dbgs() << " Def: ");
63109467b48Spatrick LLVM_DEBUG(DefMI->dump());
63209467b48Spatrick }
63309467b48Spatrick }
63409467b48Spatrick }
63509467b48Spatrick
63609467b48Spatrick // Walk the swap vector entries looking for conditions that prevent their
63709467b48Spatrick // containing computations from being optimized. When such conditions are
63809467b48Spatrick // found, mark the representative of the computation's equivalence class
63909467b48Spatrick // as rejected.
recordUnoptimizableWebs()64009467b48Spatrick void PPCVSXSwapRemoval::recordUnoptimizableWebs() {
64109467b48Spatrick
64209467b48Spatrick LLVM_DEBUG(dbgs() << "\n*** Rejecting webs for swap removal ***\n\n");
64309467b48Spatrick
64409467b48Spatrick for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
64509467b48Spatrick int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
64609467b48Spatrick
64709467b48Spatrick // If representative is already rejected, don't waste further time.
64809467b48Spatrick if (SwapVector[Repr].WebRejected)
64909467b48Spatrick continue;
65009467b48Spatrick
65109467b48Spatrick // Reject webs containing mentions of physical or partial registers, or
65209467b48Spatrick // containing operations that we don't know how to handle in a lane-
65309467b48Spatrick // permuted region.
65409467b48Spatrick if (SwapVector[EntryIdx].MentionsPhysVR ||
65509467b48Spatrick SwapVector[EntryIdx].MentionsPartialVR ||
65609467b48Spatrick !(SwapVector[EntryIdx].IsSwappable || SwapVector[EntryIdx].IsSwap)) {
65709467b48Spatrick
65809467b48Spatrick SwapVector[Repr].WebRejected = 1;
65909467b48Spatrick
66009467b48Spatrick LLVM_DEBUG(
66109467b48Spatrick dbgs() << format("Web %d rejected for physreg, partial reg, or not "
66209467b48Spatrick "swap[pable]\n",
66309467b48Spatrick Repr));
66409467b48Spatrick LLVM_DEBUG(dbgs() << " in " << EntryIdx << ": ");
66509467b48Spatrick LLVM_DEBUG(SwapVector[EntryIdx].VSEMI->dump());
66609467b48Spatrick LLVM_DEBUG(dbgs() << "\n");
66709467b48Spatrick }
66809467b48Spatrick
66909467b48Spatrick // Reject webs than contain swapping loads that feed something other
67009467b48Spatrick // than a swap instruction.
67109467b48Spatrick else if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) {
67209467b48Spatrick MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
67309467b48Spatrick Register DefReg = MI->getOperand(0).getReg();
67409467b48Spatrick
67509467b48Spatrick // We skip debug instructions in the analysis. (Note that debug
67609467b48Spatrick // location information is still maintained by this optimization
67709467b48Spatrick // because it remains on the LXVD2X and STXVD2X instructions after
67809467b48Spatrick // the XXPERMDIs are removed.)
67909467b48Spatrick for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) {
68009467b48Spatrick int UseIdx = SwapMap[&UseMI];
68109467b48Spatrick
68209467b48Spatrick if (!SwapVector[UseIdx].IsSwap || SwapVector[UseIdx].IsLoad ||
68309467b48Spatrick SwapVector[UseIdx].IsStore) {
68409467b48Spatrick
68509467b48Spatrick SwapVector[Repr].WebRejected = 1;
68609467b48Spatrick
68709467b48Spatrick LLVM_DEBUG(dbgs() << format(
68809467b48Spatrick "Web %d rejected for load not feeding swap\n", Repr));
68909467b48Spatrick LLVM_DEBUG(dbgs() << " def " << EntryIdx << ": ");
69009467b48Spatrick LLVM_DEBUG(MI->dump());
69109467b48Spatrick LLVM_DEBUG(dbgs() << " use " << UseIdx << ": ");
69209467b48Spatrick LLVM_DEBUG(UseMI.dump());
69309467b48Spatrick LLVM_DEBUG(dbgs() << "\n");
69409467b48Spatrick }
69573471bf0Spatrick
69673471bf0Spatrick // It is possible that the load feeds a swap and that swap feeds a
69773471bf0Spatrick // store. In such a case, the code is actually trying to store a swapped
69873471bf0Spatrick // vector. We must reject such webs.
69973471bf0Spatrick if (SwapVector[UseIdx].IsSwap && !SwapVector[UseIdx].IsLoad &&
70073471bf0Spatrick !SwapVector[UseIdx].IsStore) {
70173471bf0Spatrick Register SwapDefReg = UseMI.getOperand(0).getReg();
70273471bf0Spatrick for (MachineInstr &UseOfUseMI :
70373471bf0Spatrick MRI->use_nodbg_instructions(SwapDefReg)) {
70473471bf0Spatrick int UseOfUseIdx = SwapMap[&UseOfUseMI];
70573471bf0Spatrick if (SwapVector[UseOfUseIdx].IsStore) {
70673471bf0Spatrick SwapVector[Repr].WebRejected = 1;
70773471bf0Spatrick LLVM_DEBUG(
70873471bf0Spatrick dbgs() << format(
70973471bf0Spatrick "Web %d rejected for load/swap feeding a store\n", Repr));
71073471bf0Spatrick LLVM_DEBUG(dbgs() << " def " << EntryIdx << ": ");
71173471bf0Spatrick LLVM_DEBUG(MI->dump());
71273471bf0Spatrick LLVM_DEBUG(dbgs() << " use " << UseIdx << ": ");
71373471bf0Spatrick LLVM_DEBUG(UseMI.dump());
71473471bf0Spatrick LLVM_DEBUG(dbgs() << "\n");
71573471bf0Spatrick }
71673471bf0Spatrick }
71773471bf0Spatrick }
71809467b48Spatrick }
71909467b48Spatrick
72009467b48Spatrick // Reject webs that contain swapping stores that are fed by something
72109467b48Spatrick // other than a swap instruction.
72209467b48Spatrick } else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) {
72309467b48Spatrick MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
72409467b48Spatrick Register UseReg = MI->getOperand(0).getReg();
72509467b48Spatrick MachineInstr *DefMI = MRI->getVRegDef(UseReg);
72609467b48Spatrick Register DefReg = DefMI->getOperand(0).getReg();
72709467b48Spatrick int DefIdx = SwapMap[DefMI];
72809467b48Spatrick
72909467b48Spatrick if (!SwapVector[DefIdx].IsSwap || SwapVector[DefIdx].IsLoad ||
73009467b48Spatrick SwapVector[DefIdx].IsStore) {
73109467b48Spatrick
73209467b48Spatrick SwapVector[Repr].WebRejected = 1;
73309467b48Spatrick
73409467b48Spatrick LLVM_DEBUG(dbgs() << format(
73509467b48Spatrick "Web %d rejected for store not fed by swap\n", Repr));
73609467b48Spatrick LLVM_DEBUG(dbgs() << " def " << DefIdx << ": ");
73709467b48Spatrick LLVM_DEBUG(DefMI->dump());
73809467b48Spatrick LLVM_DEBUG(dbgs() << " use " << EntryIdx << ": ");
73909467b48Spatrick LLVM_DEBUG(MI->dump());
74009467b48Spatrick LLVM_DEBUG(dbgs() << "\n");
74109467b48Spatrick }
74209467b48Spatrick
74309467b48Spatrick // Ensure all uses of the register defined by DefMI feed store
74409467b48Spatrick // instructions
74509467b48Spatrick for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) {
74609467b48Spatrick int UseIdx = SwapMap[&UseMI];
74709467b48Spatrick
74809467b48Spatrick if (SwapVector[UseIdx].VSEMI->getOpcode() != MI->getOpcode()) {
74909467b48Spatrick SwapVector[Repr].WebRejected = 1;
75009467b48Spatrick
75109467b48Spatrick LLVM_DEBUG(
75209467b48Spatrick dbgs() << format(
75309467b48Spatrick "Web %d rejected for swap not feeding only stores\n", Repr));
75409467b48Spatrick LLVM_DEBUG(dbgs() << " def "
75509467b48Spatrick << " : ");
75609467b48Spatrick LLVM_DEBUG(DefMI->dump());
75709467b48Spatrick LLVM_DEBUG(dbgs() << " use " << UseIdx << ": ");
75809467b48Spatrick LLVM_DEBUG(SwapVector[UseIdx].VSEMI->dump());
75909467b48Spatrick LLVM_DEBUG(dbgs() << "\n");
76009467b48Spatrick }
76109467b48Spatrick }
76209467b48Spatrick }
76309467b48Spatrick }
76409467b48Spatrick
76509467b48Spatrick LLVM_DEBUG(dbgs() << "Swap vector after web analysis:\n\n");
76609467b48Spatrick LLVM_DEBUG(dumpSwapVector());
76709467b48Spatrick }
76809467b48Spatrick
76909467b48Spatrick // Walk the swap vector entries looking for swaps fed by permuting loads
77009467b48Spatrick // and swaps that feed permuting stores. If the containing computation
77109467b48Spatrick // has not been marked rejected, mark each such swap for removal.
77209467b48Spatrick // (Removal is delayed in case optimization has disturbed the pattern,
77309467b48Spatrick // such that multiple loads feed the same swap, etc.)
markSwapsForRemoval()77409467b48Spatrick void PPCVSXSwapRemoval::markSwapsForRemoval() {
77509467b48Spatrick
77609467b48Spatrick LLVM_DEBUG(dbgs() << "\n*** Marking swaps for removal ***\n\n");
77709467b48Spatrick
77809467b48Spatrick for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
77909467b48Spatrick
78009467b48Spatrick if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) {
78109467b48Spatrick int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
78209467b48Spatrick
78309467b48Spatrick if (!SwapVector[Repr].WebRejected) {
78409467b48Spatrick MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
78509467b48Spatrick Register DefReg = MI->getOperand(0).getReg();
78609467b48Spatrick
78709467b48Spatrick for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DefReg)) {
78809467b48Spatrick int UseIdx = SwapMap[&UseMI];
78909467b48Spatrick SwapVector[UseIdx].WillRemove = 1;
79009467b48Spatrick
79109467b48Spatrick LLVM_DEBUG(dbgs() << "Marking swap fed by load for removal: ");
79209467b48Spatrick LLVM_DEBUG(UseMI.dump());
79309467b48Spatrick }
79409467b48Spatrick }
79509467b48Spatrick
79609467b48Spatrick } else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) {
79709467b48Spatrick int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
79809467b48Spatrick
79909467b48Spatrick if (!SwapVector[Repr].WebRejected) {
80009467b48Spatrick MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
80109467b48Spatrick Register UseReg = MI->getOperand(0).getReg();
80209467b48Spatrick MachineInstr *DefMI = MRI->getVRegDef(UseReg);
80309467b48Spatrick int DefIdx = SwapMap[DefMI];
80409467b48Spatrick SwapVector[DefIdx].WillRemove = 1;
80509467b48Spatrick
80609467b48Spatrick LLVM_DEBUG(dbgs() << "Marking swap feeding store for removal: ");
80709467b48Spatrick LLVM_DEBUG(DefMI->dump());
80809467b48Spatrick }
80909467b48Spatrick
81009467b48Spatrick } else if (SwapVector[EntryIdx].IsSwappable &&
81109467b48Spatrick SwapVector[EntryIdx].SpecialHandling != 0) {
81209467b48Spatrick int Repr = EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
81309467b48Spatrick
81409467b48Spatrick if (!SwapVector[Repr].WebRejected)
81509467b48Spatrick handleSpecialSwappables(EntryIdx);
81609467b48Spatrick }
81709467b48Spatrick }
81809467b48Spatrick }
81909467b48Spatrick
82009467b48Spatrick // Create an xxswapd instruction and insert it prior to the given point.
82109467b48Spatrick // MI is used to determine basic block and debug loc information.
82209467b48Spatrick // FIXME: When inserting a swap, we should check whether SrcReg is
82309467b48Spatrick // defined by another swap: SrcReg = XXPERMDI Reg, Reg, 2; If so,
82409467b48Spatrick // then instead we should generate a copy from Reg to DstReg.
insertSwap(MachineInstr * MI,MachineBasicBlock::iterator InsertPoint,unsigned DstReg,unsigned SrcReg)82509467b48Spatrick void PPCVSXSwapRemoval::insertSwap(MachineInstr *MI,
82609467b48Spatrick MachineBasicBlock::iterator InsertPoint,
82709467b48Spatrick unsigned DstReg, unsigned SrcReg) {
82809467b48Spatrick BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(),
82909467b48Spatrick TII->get(PPC::XXPERMDI), DstReg)
83009467b48Spatrick .addReg(SrcReg)
83109467b48Spatrick .addReg(SrcReg)
83209467b48Spatrick .addImm(2);
83309467b48Spatrick }
83409467b48Spatrick
83509467b48Spatrick // The identified swap entry requires special handling to allow its
83609467b48Spatrick // containing computation to be optimized. Perform that handling
83709467b48Spatrick // here.
83809467b48Spatrick // FIXME: Additional opportunities will be phased in with subsequent
83909467b48Spatrick // patches.
handleSpecialSwappables(int EntryIdx)84009467b48Spatrick void PPCVSXSwapRemoval::handleSpecialSwappables(int EntryIdx) {
84109467b48Spatrick switch (SwapVector[EntryIdx].SpecialHandling) {
84209467b48Spatrick
84309467b48Spatrick default:
84409467b48Spatrick llvm_unreachable("Unexpected special handling type");
84509467b48Spatrick
84609467b48Spatrick // For splats based on an index into a vector, add N/2 modulo N
84709467b48Spatrick // to the index, where N is the number of vector elements.
84809467b48Spatrick case SHValues::SH_SPLAT: {
84909467b48Spatrick MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
85009467b48Spatrick unsigned NElts;
85109467b48Spatrick
85209467b48Spatrick LLVM_DEBUG(dbgs() << "Changing splat: ");
85309467b48Spatrick LLVM_DEBUG(MI->dump());
85409467b48Spatrick
85509467b48Spatrick switch (MI->getOpcode()) {
85609467b48Spatrick default:
85709467b48Spatrick llvm_unreachable("Unexpected splat opcode");
85809467b48Spatrick case PPC::VSPLTB: NElts = 16; break;
85909467b48Spatrick case PPC::VSPLTH: NElts = 8; break;
86009467b48Spatrick case PPC::VSPLTW:
86109467b48Spatrick case PPC::XXSPLTW: NElts = 4; break;
86209467b48Spatrick }
86309467b48Spatrick
86409467b48Spatrick unsigned EltNo;
86509467b48Spatrick if (MI->getOpcode() == PPC::XXSPLTW)
86609467b48Spatrick EltNo = MI->getOperand(2).getImm();
86709467b48Spatrick else
86809467b48Spatrick EltNo = MI->getOperand(1).getImm();
86909467b48Spatrick
87009467b48Spatrick EltNo = (EltNo + NElts / 2) % NElts;
87109467b48Spatrick if (MI->getOpcode() == PPC::XXSPLTW)
87209467b48Spatrick MI->getOperand(2).setImm(EltNo);
87309467b48Spatrick else
87409467b48Spatrick MI->getOperand(1).setImm(EltNo);
87509467b48Spatrick
87609467b48Spatrick LLVM_DEBUG(dbgs() << " Into: ");
87709467b48Spatrick LLVM_DEBUG(MI->dump());
87809467b48Spatrick break;
87909467b48Spatrick }
88009467b48Spatrick
88109467b48Spatrick // For an XXPERMDI that isn't handled otherwise, we need to
88209467b48Spatrick // reverse the order of the operands. If the selector operand
88309467b48Spatrick // has a value of 0 or 3, we need to change it to 3 or 0,
88409467b48Spatrick // respectively. Otherwise we should leave it alone. (This
88509467b48Spatrick // is equivalent to reversing the two bits of the selector
88609467b48Spatrick // operand and complementing the result.)
88709467b48Spatrick case SHValues::SH_XXPERMDI: {
88809467b48Spatrick MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
88909467b48Spatrick
89009467b48Spatrick LLVM_DEBUG(dbgs() << "Changing XXPERMDI: ");
89109467b48Spatrick LLVM_DEBUG(MI->dump());
89209467b48Spatrick
89309467b48Spatrick unsigned Selector = MI->getOperand(3).getImm();
89409467b48Spatrick if (Selector == 0 || Selector == 3)
89509467b48Spatrick Selector = 3 - Selector;
89609467b48Spatrick MI->getOperand(3).setImm(Selector);
89709467b48Spatrick
89809467b48Spatrick Register Reg1 = MI->getOperand(1).getReg();
89909467b48Spatrick Register Reg2 = MI->getOperand(2).getReg();
90009467b48Spatrick MI->getOperand(1).setReg(Reg2);
90109467b48Spatrick MI->getOperand(2).setReg(Reg1);
90209467b48Spatrick
90309467b48Spatrick // We also need to swap kill flag associated with the register.
90409467b48Spatrick bool IsKill1 = MI->getOperand(1).isKill();
90509467b48Spatrick bool IsKill2 = MI->getOperand(2).isKill();
90609467b48Spatrick MI->getOperand(1).setIsKill(IsKill2);
90709467b48Spatrick MI->getOperand(2).setIsKill(IsKill1);
90809467b48Spatrick
90909467b48Spatrick LLVM_DEBUG(dbgs() << " Into: ");
91009467b48Spatrick LLVM_DEBUG(MI->dump());
91109467b48Spatrick break;
91209467b48Spatrick }
91309467b48Spatrick
91409467b48Spatrick // For a copy from a scalar floating-point register to a vector
91509467b48Spatrick // register, removing swaps will leave the copied value in the
91609467b48Spatrick // wrong lane. Insert a swap following the copy to fix this.
91709467b48Spatrick case SHValues::SH_COPYWIDEN: {
91809467b48Spatrick MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
91909467b48Spatrick
92009467b48Spatrick LLVM_DEBUG(dbgs() << "Changing SUBREG_TO_REG: ");
92109467b48Spatrick LLVM_DEBUG(MI->dump());
92209467b48Spatrick
92309467b48Spatrick Register DstReg = MI->getOperand(0).getReg();
92409467b48Spatrick const TargetRegisterClass *DstRC = MRI->getRegClass(DstReg);
92509467b48Spatrick Register NewVReg = MRI->createVirtualRegister(DstRC);
92609467b48Spatrick
92709467b48Spatrick MI->getOperand(0).setReg(NewVReg);
92809467b48Spatrick LLVM_DEBUG(dbgs() << " Into: ");
92909467b48Spatrick LLVM_DEBUG(MI->dump());
93009467b48Spatrick
93109467b48Spatrick auto InsertPoint = ++MachineBasicBlock::iterator(MI);
93209467b48Spatrick
93309467b48Spatrick // Note that an XXPERMDI requires a VSRC, so if the SUBREG_TO_REG
93409467b48Spatrick // is copying to a VRRC, we need to be careful to avoid a register
93509467b48Spatrick // assignment problem. In this case we must copy from VRRC to VSRC
93609467b48Spatrick // prior to the swap, and from VSRC to VRRC following the swap.
93709467b48Spatrick // Coalescing will usually remove all this mess.
93809467b48Spatrick if (DstRC == &PPC::VRRCRegClass) {
93909467b48Spatrick Register VSRCTmp1 = MRI->createVirtualRegister(&PPC::VSRCRegClass);
94009467b48Spatrick Register VSRCTmp2 = MRI->createVirtualRegister(&PPC::VSRCRegClass);
94109467b48Spatrick
94209467b48Spatrick BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(),
94309467b48Spatrick TII->get(PPC::COPY), VSRCTmp1)
94409467b48Spatrick .addReg(NewVReg);
94509467b48Spatrick LLVM_DEBUG(std::prev(InsertPoint)->dump());
94609467b48Spatrick
94709467b48Spatrick insertSwap(MI, InsertPoint, VSRCTmp2, VSRCTmp1);
94809467b48Spatrick LLVM_DEBUG(std::prev(InsertPoint)->dump());
94909467b48Spatrick
95009467b48Spatrick BuildMI(*MI->getParent(), InsertPoint, MI->getDebugLoc(),
95109467b48Spatrick TII->get(PPC::COPY), DstReg)
95209467b48Spatrick .addReg(VSRCTmp2);
95309467b48Spatrick LLVM_DEBUG(std::prev(InsertPoint)->dump());
95409467b48Spatrick
95509467b48Spatrick } else {
95609467b48Spatrick insertSwap(MI, InsertPoint, DstReg, NewVReg);
95709467b48Spatrick LLVM_DEBUG(std::prev(InsertPoint)->dump());
95809467b48Spatrick }
95909467b48Spatrick break;
96009467b48Spatrick }
96109467b48Spatrick }
96209467b48Spatrick }
96309467b48Spatrick
96409467b48Spatrick // Walk the swap vector and replace each entry marked for removal with
96509467b48Spatrick // a copy operation.
removeSwaps()96609467b48Spatrick bool PPCVSXSwapRemoval::removeSwaps() {
96709467b48Spatrick
96809467b48Spatrick LLVM_DEBUG(dbgs() << "\n*** Removing swaps ***\n\n");
96909467b48Spatrick
97009467b48Spatrick bool Changed = false;
97109467b48Spatrick
97209467b48Spatrick for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
97309467b48Spatrick if (SwapVector[EntryIdx].WillRemove) {
97409467b48Spatrick Changed = true;
97509467b48Spatrick MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
97609467b48Spatrick MachineBasicBlock *MBB = MI->getParent();
97709467b48Spatrick BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(TargetOpcode::COPY),
97809467b48Spatrick MI->getOperand(0).getReg())
97909467b48Spatrick .add(MI->getOperand(1));
98009467b48Spatrick
98109467b48Spatrick LLVM_DEBUG(dbgs() << format("Replaced %d with copy: ",
98209467b48Spatrick SwapVector[EntryIdx].VSEId));
98309467b48Spatrick LLVM_DEBUG(MI->dump());
98409467b48Spatrick
98509467b48Spatrick MI->eraseFromParent();
98609467b48Spatrick }
98709467b48Spatrick }
98809467b48Spatrick
98909467b48Spatrick return Changed;
99009467b48Spatrick }
99109467b48Spatrick
99209467b48Spatrick #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
99309467b48Spatrick // For debug purposes, dump the contents of the swap vector.
dumpSwapVector()99409467b48Spatrick LLVM_DUMP_METHOD void PPCVSXSwapRemoval::dumpSwapVector() {
99509467b48Spatrick
99609467b48Spatrick for (unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
99709467b48Spatrick
99809467b48Spatrick MachineInstr *MI = SwapVector[EntryIdx].VSEMI;
99909467b48Spatrick int ID = SwapVector[EntryIdx].VSEId;
100009467b48Spatrick
100109467b48Spatrick dbgs() << format("%6d", ID);
100209467b48Spatrick dbgs() << format("%6d", EC->getLeaderValue(ID));
100309467b48Spatrick dbgs() << format(" %bb.%3d", MI->getParent()->getNumber());
100409467b48Spatrick dbgs() << format(" %14s ", TII->getName(MI->getOpcode()).str().c_str());
100509467b48Spatrick
100609467b48Spatrick if (SwapVector[EntryIdx].IsLoad)
100709467b48Spatrick dbgs() << "load ";
100809467b48Spatrick if (SwapVector[EntryIdx].IsStore)
100909467b48Spatrick dbgs() << "store ";
101009467b48Spatrick if (SwapVector[EntryIdx].IsSwap)
101109467b48Spatrick dbgs() << "swap ";
101209467b48Spatrick if (SwapVector[EntryIdx].MentionsPhysVR)
101309467b48Spatrick dbgs() << "physreg ";
101409467b48Spatrick if (SwapVector[EntryIdx].MentionsPartialVR)
101509467b48Spatrick dbgs() << "partialreg ";
101609467b48Spatrick
101709467b48Spatrick if (SwapVector[EntryIdx].IsSwappable) {
101809467b48Spatrick dbgs() << "swappable ";
101909467b48Spatrick switch(SwapVector[EntryIdx].SpecialHandling) {
102009467b48Spatrick default:
102109467b48Spatrick dbgs() << "special:**unknown**";
102209467b48Spatrick break;
102309467b48Spatrick case SH_NONE:
102409467b48Spatrick break;
102509467b48Spatrick case SH_EXTRACT:
102609467b48Spatrick dbgs() << "special:extract ";
102709467b48Spatrick break;
102809467b48Spatrick case SH_INSERT:
102909467b48Spatrick dbgs() << "special:insert ";
103009467b48Spatrick break;
103109467b48Spatrick case SH_NOSWAP_LD:
103209467b48Spatrick dbgs() << "special:load ";
103309467b48Spatrick break;
103409467b48Spatrick case SH_NOSWAP_ST:
103509467b48Spatrick dbgs() << "special:store ";
103609467b48Spatrick break;
103709467b48Spatrick case SH_SPLAT:
103809467b48Spatrick dbgs() << "special:splat ";
103909467b48Spatrick break;
104009467b48Spatrick case SH_XXPERMDI:
104109467b48Spatrick dbgs() << "special:xxpermdi ";
104209467b48Spatrick break;
104309467b48Spatrick case SH_COPYWIDEN:
104409467b48Spatrick dbgs() << "special:copywiden ";
104509467b48Spatrick break;
104609467b48Spatrick }
104709467b48Spatrick }
104809467b48Spatrick
104909467b48Spatrick if (SwapVector[EntryIdx].WebRejected)
105009467b48Spatrick dbgs() << "rejected ";
105109467b48Spatrick if (SwapVector[EntryIdx].WillRemove)
105209467b48Spatrick dbgs() << "remove ";
105309467b48Spatrick
105409467b48Spatrick dbgs() << "\n";
105509467b48Spatrick
105609467b48Spatrick // For no-asserts builds.
105709467b48Spatrick (void)MI;
105809467b48Spatrick (void)ID;
105909467b48Spatrick }
106009467b48Spatrick
106109467b48Spatrick dbgs() << "\n";
106209467b48Spatrick }
106309467b48Spatrick #endif
106409467b48Spatrick
106509467b48Spatrick } // end default namespace
106609467b48Spatrick
106709467b48Spatrick INITIALIZE_PASS_BEGIN(PPCVSXSwapRemoval, DEBUG_TYPE,
106809467b48Spatrick "PowerPC VSX Swap Removal", false, false)
106909467b48Spatrick INITIALIZE_PASS_END(PPCVSXSwapRemoval, DEBUG_TYPE,
107009467b48Spatrick "PowerPC VSX Swap Removal", false, false)
107109467b48Spatrick
107209467b48Spatrick char PPCVSXSwapRemoval::ID = 0;
107309467b48Spatrick FunctionPass*
createPPCVSXSwapRemovalPass()107409467b48Spatrick llvm::createPPCVSXSwapRemovalPass() { return new PPCVSXSwapRemoval(); }
1075