| 1 | //===- HLXExpandPseudos.cpp - Expand HLX load/store pseudos ---------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass expands the HLX load/store pseudo instructions into their final |
| 10 | // two-instruction bundled form. It runs after packetization so the resulting |
| 11 | // bundles are not seen by the post-RA scheduler. |
| 12 | // |
| 13 | // LX_L16b_pseudo -> { Ld.tmp = mem(Rs); Ld = Ld } (LX_L16b_tmp; LX_assign) |
| 14 | // LX_S16b_pseudo -> { Lx = Lx; mem(Rs) = Lx.new } (LX_assign; LX_S16b_new) |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "Hexagon.h" |
| 19 | #include "HexagonInstrInfo.h" |
| 20 | #include "HexagonSubtarget.h" |
| 21 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 23 | #include "llvm/CodeGen/MachineInstrBundle.h" |
| 24 | #include "llvm/InitializePasses.h" |
| 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | |
| 28 | #define DEBUG_TYPE "hlx-expand-pseudos" |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | static cl::opt<bool> DisableHLXExpandPseudos( |
| 33 | "disable-hlx-expand-pseudos" , cl::init(Val: false), cl::Hidden, |
| 34 | cl::desc("Disable expansion of HLX pseudo instructions" )); |
| 35 | |
| 36 | namespace llvm { |
| 37 | FunctionPass *createHLXExpandPseudos(); |
| 38 | void initializeHLXExpandPseudosPass(PassRegistry &); |
| 39 | } // namespace llvm |
| 40 | |
| 41 | static bool isHLXLoadStorePseudo(const MachineInstr &MI) { |
| 42 | unsigned Opc = MI.getOpcode(); |
| 43 | return Opc == Hexagon::LX_L16b_pseudo || Opc == Hexagon::LX_S16b_pseudo; |
| 44 | } |
| 45 | |
| 46 | namespace { |
| 47 | |
| 48 | struct HLXExpandPseudos : public MachineFunctionPass { |
| 49 | static char ID; |
| 50 | |
| 51 | HLXExpandPseudos() : MachineFunctionPass(ID) {} |
| 52 | |
| 53 | StringRef getPassName() const override { return "HLX Expand Pseudos" ; } |
| 54 | |
| 55 | MachineFunctionProperties getRequiredProperties() const override { |
| 56 | return MachineFunctionProperties().set( |
| 57 | MachineFunctionProperties::Property::NoVRegs); |
| 58 | } |
| 59 | |
| 60 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 61 | |
| 62 | private: |
| 63 | const HexagonInstrInfo *HII = nullptr; |
| 64 | |
| 65 | bool expandPseudos(MachineInstr &MI); |
| 66 | bool expandPseudosInBundle(MachineInstr &MI); |
| 67 | }; |
| 68 | |
| 69 | } // end anonymous namespace |
| 70 | |
| 71 | char HLXExpandPseudos::ID = 0; |
| 72 | |
| 73 | INITIALIZE_PASS(HLXExpandPseudos, "hlx-expand-pseudos" , "HLX Expand Pseudos" , |
| 74 | false, false) |
| 75 | |
| 76 | bool HLXExpandPseudos::expandPseudos(MachineInstr &MI) { |
| 77 | bool IsBundled = MI.isBundled(); |
| 78 | MachineBasicBlock *MBB = MI.getParent(); |
| 79 | DebugLoc DL = MI.getDebugLoc(); |
| 80 | |
| 81 | auto ExpandLoad = [&](unsigned AssignOpc, unsigned LoadOpc) { |
| 82 | MachineOperand Op0 = MI.getOperand(i: 0); |
| 83 | MachineOperand Op1 = MI.getOperand(i: 1); |
| 84 | Register Reg0 = Op0.getReg(); |
| 85 | MachineInstr *Start = BuildMI(BB&: *MBB, I&: MI, MIMD: DL, MCID: HII->get(Opcode: LoadOpc), DestReg: Reg0) |
| 86 | .add(MO: Op1) |
| 87 | .cloneMemRefs(OtherMI: MI) |
| 88 | .copyImplicitOps(OtherMI: MI) |
| 89 | .getInstr(); |
| 90 | MachineInstr *End = |
| 91 | BuildMI(BB&: *MBB, I&: MI, MIMD: DL, MCID: HII->get(Opcode: AssignOpc), DestReg: Reg0) |
| 92 | .addReg(RegNo: Reg0, Flags: RegState::Kill | RegState::InternalRead) |
| 93 | .getInstr(); |
| 94 | if (IsBundled) |
| 95 | return true; |
| 96 | End->bundleWithPred(); |
| 97 | finalizeBundle(MBB&: *MBB, FirstMI: Start->getIterator(), LastMI: MI.getIterator()); |
| 98 | MBB->erase(I: MI); |
| 99 | return true; |
| 100 | }; |
| 101 | |
| 102 | auto ExpandStore = [&](unsigned AssignOpc, unsigned StoreOpc) { |
| 103 | MachineOperand Op0 = MI.getOperand(i: 0); |
| 104 | MachineOperand Op1 = MI.getOperand(i: 1); |
| 105 | Register Reg1 = Op1.getReg(); |
| 106 | MachineInstr *Start = BuildMI(BB&: *MBB, I&: MI, MIMD: DL, MCID: HII->get(Opcode: AssignOpc), DestReg: Reg1) |
| 107 | .addReg(RegNo: Reg1, Flags: RegState::Kill) |
| 108 | .getInstr(); |
| 109 | Op1.setIsInternalRead(); |
| 110 | MachineInstr *End = BuildMI(BB&: *MBB, I&: MI, MIMD: DL, MCID: HII->get(Opcode: StoreOpc)) |
| 111 | .add(MO: Op0) |
| 112 | .add(MO: Op1) |
| 113 | .cloneMemRefs(OtherMI: MI) |
| 114 | .copyImplicitOps(OtherMI: MI) |
| 115 | .getInstr(); |
| 116 | if (IsBundled) |
| 117 | return true; |
| 118 | End->bundleWithPred(); |
| 119 | finalizeBundle(MBB&: *MBB, FirstMI: Start->getIterator(), LastMI: MI.getIterator()); |
| 120 | MBB->erase(I: MI); |
| 121 | return true; |
| 122 | }; |
| 123 | |
| 124 | switch (MI.getOpcode()) { |
| 125 | default: |
| 126 | return false; |
| 127 | case Hexagon::LX_L16b_pseudo: |
| 128 | return ExpandLoad(Hexagon::LX_assign, Hexagon::LX_L16b_tmp); |
| 129 | case Hexagon::LX_S16b_pseudo: |
| 130 | return ExpandStore(Hexagon::LX_assign, Hexagon::LX_S16b_new); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | bool HLXExpandPseudos::expandPseudosInBundle(MachineInstr &MI) { |
| 135 | MachineBasicBlock::instr_iterator MII = MI.getIterator(); |
| 136 | MachineBasicBlock::instr_iterator MIE = MI.getParent()->instr_end(); |
| 137 | SmallVector<MachineBasicBlock::instr_iterator, 4> EraseList; |
| 138 | bool MadeChange = false; |
| 139 | for (++MII; MII != MIE && MII->isInsideBundle() && !MII->isBundle(); ++MII) { |
| 140 | if (isHLXLoadStorePseudo(MI: *MII) && expandPseudos(MI&: *MII)) { |
| 141 | EraseList.push_back(Elt: MII); |
| 142 | MadeChange = true; |
| 143 | } |
| 144 | } |
| 145 | for (auto It : EraseList) |
| 146 | It->eraseFromBundle(); |
| 147 | return MadeChange; |
| 148 | } |
| 149 | |
| 150 | bool HLXExpandPseudos::runOnMachineFunction(MachineFunction &MF) { |
| 151 | auto &HST = MF.getSubtarget<HexagonSubtarget>(); |
| 152 | if (!HST.useHLXOps() || DisableHLXExpandPseudos) |
| 153 | return false; |
| 154 | |
| 155 | HII = static_cast<const HexagonInstrInfo *>(HST.getInstrInfo()); |
| 156 | |
| 157 | bool MadeChange = false; |
| 158 | for (MachineBasicBlock &MBB : MF) { |
| 159 | for (MachineInstr &MI : llvm::make_early_inc_range(Range&: MBB)) { |
| 160 | if (MI.isBundle()) |
| 161 | MadeChange |= expandPseudosInBundle(MI); |
| 162 | else if (isHLXLoadStorePseudo(MI)) |
| 163 | MadeChange |= expandPseudos(MI); |
| 164 | } |
| 165 | } |
| 166 | return MadeChange; |
| 167 | } |
| 168 | |
| 169 | FunctionPass *llvm::createHLXExpandPseudos() { return new HLXExpandPseudos(); } |
| 170 | |