LLVM 24.0.0git
HLXExpandPseudos.cpp
Go to the documentation of this file.
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"
26
27#define DEBUG_TYPE "hlx-expand-pseudos"
28
29using namespace llvm;
30
32 "disable-hlx-expand-pseudos", cl::init(false), cl::Hidden,
33 cl::desc("Disable expansion of HLX pseudo instructions"));
34
35namespace llvm {
38} // namespace llvm
39
41 unsigned Opc = MI.getOpcode();
42 return Opc == Hexagon::LX_L16b_pseudo || Opc == Hexagon::LX_S16b_pseudo;
43}
44
45namespace {
46
47struct HLXExpandPseudos : public MachineFunctionPass {
48 static char ID;
49
50 HLXExpandPseudos() : MachineFunctionPass(ID) {}
51
52 StringRef getPassName() const override { return "HLX Expand Pseudos"; }
53
54 MachineFunctionProperties getRequiredProperties() const override {
55 return MachineFunctionProperties().set(
56 MachineFunctionProperties::Property::NoVRegs);
57 }
58
59 bool runOnMachineFunction(MachineFunction &MF) override;
60
61private:
62 const HexagonInstrInfo *HII = nullptr;
63
64 bool expandPseudos(MachineInstr &MI);
65 bool expandPseudosInBundle(MachineInstr &MI);
66};
67
68} // end anonymous namespace
69
70char HLXExpandPseudos::ID = 0;
71
72INITIALIZE_PASS(HLXExpandPseudos, "hlx-expand-pseudos", "HLX Expand Pseudos",
73 false, false)
74
75bool HLXExpandPseudos::expandPseudos(MachineInstr &MI) {
76 bool IsBundled = MI.isBundled();
77 MachineBasicBlock *MBB = MI.getParent();
78 DebugLoc DL = MI.getDebugLoc();
79
80 auto ExpandLoad = [&](unsigned AssignOpc, unsigned LoadOpc) {
81 MachineOperand Op0 = MI.getOperand(0);
82 MachineOperand Op1 = MI.getOperand(1);
83 Register Reg0 = Op0.getReg();
84 MachineInstr *Start = BuildMI(*MBB, MI, DL, HII->get(LoadOpc), Reg0)
85 .add(Op1)
88 .getInstr();
90 BuildMI(*MBB, MI, DL, HII->get(AssignOpc), Reg0)
92 .getInstr();
93 if (IsBundled)
94 return true;
95 End->bundleWithPred();
96 finalizeBundle(*MBB, Start->getIterator(), MI.getIterator());
97 MBB->erase(MI);
98 return true;
99 };
100
101 auto ExpandStore = [&](unsigned AssignOpc, unsigned StoreOpc) {
102 MachineOperand Op0 = MI.getOperand(0);
103 MachineOperand Op1 = MI.getOperand(1);
104 Register Reg1 = Op1.getReg();
105 MachineInstr *Start = BuildMI(*MBB, MI, DL, HII->get(AssignOpc), Reg1)
106 .addReg(Reg1, RegState::Kill)
107 .getInstr();
108 Op1.setIsInternalRead();
109 MachineInstr *End = BuildMI(*MBB, MI, DL, HII->get(StoreOpc))
110 .add(Op0)
111 .add(Op1)
114 .getInstr();
115 if (IsBundled)
116 return true;
117 End->bundleWithPred();
118 finalizeBundle(*MBB, Start->getIterator(), MI.getIterator());
119 MBB->erase(MI);
120 return true;
121 };
122
123 switch (MI.getOpcode()) {
124 default:
125 return false;
126 case Hexagon::LX_L16b_pseudo:
127 return ExpandLoad(Hexagon::LX_assign, Hexagon::LX_L16b_tmp);
128 case Hexagon::LX_S16b_pseudo:
129 return ExpandStore(Hexagon::LX_assign, Hexagon::LX_S16b_new);
130 }
131}
132
133bool HLXExpandPseudos::expandPseudosInBundle(MachineInstr &MI) {
134 MachineBasicBlock::instr_iterator MII = MI.getIterator();
135 MachineBasicBlock::instr_iterator MIE = MI.getParent()->instr_end();
137 bool MadeChange = false;
138 for (++MII; MII != MIE && MII->isInsideBundle() && !MII->isBundle(); ++MII) {
139 if (isHLXLoadStorePseudo(*MII) && expandPseudos(*MII)) {
140 EraseList.push_back(MII);
141 MadeChange = true;
142 }
143 }
144 for (auto It : EraseList)
145 It->eraseFromBundle();
146 return MadeChange;
147}
148
149bool HLXExpandPseudos::runOnMachineFunction(MachineFunction &MF) {
150 auto &HST = MF.getSubtarget<HexagonSubtarget>();
151 if (!HST.useHLXOps() || DisableHLXExpandPseudos)
152 return false;
153
154 HII = static_cast<const HexagonInstrInfo *>(HST.getInstrInfo());
155
156 bool MadeChange = false;
157 for (MachineBasicBlock &MBB : MF) {
158 for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
159 if (MI.isBundle())
160 MadeChange |= expandPseudosInBundle(MI);
161 else if (isHLXLoadStorePseudo(MI))
162 MadeChange |= expandPseudos(MI);
163 }
164 }
165 return MadeChange;
166}
167
168FunctionPass *llvm::createHLXExpandPseudos() { return new HLXExpandPseudos(); }
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static cl::opt< bool > DisableHLXExpandPseudos("disable-hlx-expand-pseudos", cl::init(false), cl::Hidden, cl::desc("Disable expansion of HLX pseudo instructions"))
static bool isHLXLoadStorePseudo(const MachineInstr &MI)
IRTranslator LLVM IR MI
unsigned Opc
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
Instructions::iterator instr_iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & cloneMemRefs(const MachineInstr &OtherMI) const
const MachineInstrBuilder & copyImplicitOps(const MachineInstr &OtherMI) const
Copy all the implicit operands from OtherMI onto this one.
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
void setIsInternalRead(bool Val=true)
Register getReg() const
getReg - Returns the register number.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
void push_back(const T &Elt)
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
FunctionPass * createHLXExpandPseudos()
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Kill
The last use of a register.
@ InternalRead
Register reads a value that is defined inside the same instruction or bundle.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
void initializeHLXExpandPseudosPass(PassRegistry &)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320