LLVM 24.0.0git
HexagonQFPOptimizer.cpp
Go to the documentation of this file.
1//===----- HexagonQFPOptimizer.cpp - Qualcomm-FP to IEEE-FP conversions
2// optimizer ------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// Basic infrastructure for optimizing intermediate conversion instructions
11// generated while performing vector floating point operations.
12// Currently run at the starting of the code generation for Hexagon, cleans
13// up redundant conversion instructions and replaces the uses of conversion
14// with appropriate machine operand. Liveness is preserved after this pass.
15//
16// @note: The redundant conversion instructions are not eliminated in this pass.
17// In this pass, we are only trying to replace the uses of conversion
18// instructions with its appropriate QFP instruction. We are leaving the job to
19// Dead instruction Elimination pass to remove redundant conversion
20// instructions.
21//
22// Brief overview of working of this QFP optimizer.
23// This version of Hexagon QFP optimizer basically iterates over each
24// instruction, checks whether if it belongs to hexagon floating point HVX
25// arithmetic instruction category(Add, Sub, Mul). And then it finds the unique
26// definition for the machine operands corresponding to the instruction.
27//
28// Example:
29// MachineInstruction *MI be the HVX vadd instruction
30// MI -> $v0 = V6_vadd_sf $v1, $v2
31// MachineOperand *DefMI1 = MRI->getVRegDef(MI->getOperand(1).getReg());
32// MachineOperand *DefMI2 = MRI->getVRegDef(MI->getOperand(2).getReg());
33//
34// In the above example, DefMI1 and DefMI2 gives the unique definitions
35// corresponding to the operands($v1 and &v2 respectively) of instruction MI.
36//
37// If both of the definitions are not conversion instructions(V6_vconv_sf_qf32,
38// V6_vconv_hf_qf16), then it will skip optimizing the current instruction and
39// iterates over next instruction.
40//
41// If one the definitions is conversion instruction then our pass will replace
42// the arithmetic instruction with its corresponding mix variant.
43// In the above example, if $v1 is conversion instruction
44// DefMI1 -> $v1 = V6_vconv_sf_qf32 $v3
45// After Transformation:
46// MI -> $v0 = V6_vadd_qf32_mix $v3, $v2 ($v1 is replaced with $v3)
47//
48// If both the definitions are conversion instructions then the instruction will
49// be replaced with its qf variant
50// In the above example, if $v1 and $v2 are conversion instructions
51// DefMI1 -> $v1 = V6_vconv_sf_qf32 $v3
52// DefMI2 -> $v2 = V6_vconv_sf_qf32 $v4
53// After Transformation:
54// MI -> $v0 = V6_vadd_qf32 $v3, $v4 ($v1 is replaced with $v3, $v2 is replaced
55// with $v4)
56//
57// Currently, in this pass, we are not handling the case when the definitions
58// are PHI inst.
59//
60//===----------------------------------------------------------------------===//
61
62#define HEXAGON_QFP_OPTIMIZER "QFP optimizer pass"
63
64#include "Hexagon.h"
65#include "HexagonInstrInfo.h"
66#include "HexagonSubtarget.h"
67#include "llvm/ADT/StringRef.h"
73#include "llvm/CodeGen/Passes.h"
74#include "llvm/Pass.h"
76#include "llvm/Support/Debug.h"
78#include <map>
79
80#define DEBUG_TYPE "hexagon-qfp-optimizer"
81
82using namespace llvm;
83
85 DisableQFOptimizer("disable-qfp-opt", cl::init(false),
86 cl::desc("Disable optimization of Qfloat operations."));
88 "disable-qfp-opt-mul", cl::init(true),
89 cl::desc("Disable optimization of Qfloat operations for multiply."));
90
91namespace {
92const std::map<unsigned short, unsigned short> QFPInstMap{
93 {Hexagon::V6_vadd_hf, Hexagon::V6_vadd_qf16_mix},
94 {Hexagon::V6_vadd_qf16_mix, Hexagon::V6_vadd_qf16},
95 {Hexagon::V6_vadd_sf, Hexagon::V6_vadd_qf32_mix},
96 {Hexagon::V6_vadd_qf32_mix, Hexagon::V6_vadd_qf32},
97 {Hexagon::V6_vsub_hf, Hexagon::V6_vsub_qf16_mix},
98 {Hexagon::V6_vsub_qf16_mix, Hexagon::V6_vsub_qf16},
99 {Hexagon::V6_vsub_sf, Hexagon::V6_vsub_qf32_mix},
100 {Hexagon::V6_vsub_qf32_mix, Hexagon::V6_vsub_qf32},
101 {Hexagon::V6_vmpy_qf16_hf, Hexagon::V6_vmpy_qf16_mix_hf},
102 {Hexagon::V6_vmpy_qf16_mix_hf, Hexagon::V6_vmpy_qf16},
103 {Hexagon::V6_vmpy_qf32_hf, Hexagon::V6_vmpy_qf32_mix_hf},
104 {Hexagon::V6_vmpy_qf32_mix_hf, Hexagon::V6_vmpy_qf32_qf16},
105 {Hexagon::V6_vmpy_qf32_sf, Hexagon::V6_vmpy_qf32},
106 {Hexagon::V6_vilog2_sf, Hexagon::V6_vilog2_qf32},
107 {Hexagon::V6_vilog2_hf, Hexagon::V6_vilog2_qf16},
108 {Hexagon::V6_vabs_qf32_sf, Hexagon::V6_vabs_qf32_qf32},
109 {Hexagon::V6_vabs_qf16_hf, Hexagon::V6_vabs_qf16_qf16},
110 {Hexagon::V6_vneg_qf32_sf, Hexagon::V6_vneg_qf32_qf32},
111 {Hexagon::V6_vneg_qf16_hf, Hexagon::V6_vneg_qf16_qf16}};
112} // namespace
113
114namespace {
115struct HexagonQFPOptimizer : public MachineFunctionPass {
116public:
117 static char ID;
118
119 HexagonQFPOptimizer() : MachineFunctionPass(ID) {}
120
121 bool runOnMachineFunction(MachineFunction &MF) override;
122
123 bool optimizeQfp(MachineInstr *MI, MachineBasicBlock *MBB);
124
125 bool optimizeQfpTwoOp(MachineInstr *MI, MachineBasicBlock *MBB);
126
127 bool optimizeQfpOneOp(MachineInstr *MI, MachineBasicBlock *MBB);
128
129 StringRef getPassName() const override { return HEXAGON_QFP_OPTIMIZER; }
130
131 void getAnalysisUsage(AnalysisUsage &AU) const override {
132 AU.setPreservesCFG();
134 }
135
136private:
137 const HexagonSubtarget *HST = nullptr;
138 const HexagonInstrInfo *HII = nullptr;
139 const MachineRegisterInfo *MRI = nullptr;
140};
141
142char HexagonQFPOptimizer::ID = 0;
143} // namespace
144
145INITIALIZE_PASS(HexagonQFPOptimizer, "hexagon-qfp-optimizer",
146 HEXAGON_QFP_OPTIMIZER, false, false)
147
149 return new HexagonQFPOptimizer();
150}
151
152bool HexagonQFPOptimizer::optimizeQfp(MachineInstr *MI,
154
155 if (MI->getNumOperands() == 2)
156 return optimizeQfpOneOp(MI, MBB);
157 else if (MI->getNumOperands() == 3)
158 return optimizeQfpTwoOp(MI, MBB);
159 else
160 return false;
161}
162
163bool HexagonQFPOptimizer::optimizeQfpOneOp(MachineInstr *MI,
164 MachineBasicBlock *MBB) {
165
166 auto It = QFPInstMap.find(MI->getOpcode());
167 if (It == QFPInstMap.end())
168 return false;
169
170 unsigned short InstTy = It->second;
171 // Get the reachind defs of MI
172 MachineInstr *DefMI = MRI->getVRegDef(MI->getOperand(1).getReg());
173 MachineOperand &Res = MI->getOperand(0);
174 if (!Res.isReg())
175 return false;
176
177 LLVM_DEBUG(dbgs() << "\n[Reaching Defs of operands]: "; DefMI->dump());
178 MachineInstr *ReachDefDef = nullptr;
179
180 // Get the reaching def of the reaching def to check for W reg def
181 if (DefMI->getNumOperands() > 1 && DefMI->getOperand(1).isReg() &&
183 ReachDefDef = MRI->getVRegDef(DefMI->getOperand(1).getReg());
184 unsigned ReachDefOp = DefMI->getOpcode();
185 MachineInstrBuilder MIB;
186
187 // Check if the reaching def is a conversion
188 if (ReachDefOp == Hexagon::V6_vconv_sf_qf32 ||
189 ReachDefOp == Hexagon::V6_vconv_hf_qf16) {
190
191 // Return if the reaching def of reaching def is W type
192 if (ReachDefDef && MRI->getRegClass(ReachDefDef->getOperand(0).getReg()) ==
193 &Hexagon::HvxWRRegClass)
194 return false;
195
196 MachineOperand &SrcOp = DefMI->getOperand(1);
197 MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), HII->get(InstTy), Res.getReg())
198 .addReg(SrcOp.getReg(), {}, SrcOp.getSubReg());
199 LLVM_DEBUG(dbgs() << "\n[Inserting]: "; MIB.getInstr()->dump());
200 return true;
201 }
202 return false;
203}
204
205bool HexagonQFPOptimizer::optimizeQfpTwoOp(MachineInstr *MI,
206 MachineBasicBlock *MBB) {
207
208 auto It = QFPInstMap.find(MI->getOpcode());
209 if (It == QFPInstMap.end())
210 return false;
211 unsigned short InstTy = It->second;
212 // Get the reaching defs of MI, DefMI1 and DefMI2
213 MachineInstr *DefMI1 = nullptr;
214 MachineInstr *DefMI2 = nullptr;
215
216 if (MI->getOperand(1).isReg())
217 DefMI1 = MRI->getVRegDef(MI->getOperand(1).getReg());
218 if (MI->getOperand(2).isReg())
219 DefMI2 = MRI->getVRegDef(MI->getOperand(2).getReg());
220 if (!DefMI1 || !DefMI2)
221 return false;
222
223 MachineOperand &Res = MI->getOperand(0);
224 if (!Res.isReg())
225 return false;
226
227 MachineInstr *Inst1 = nullptr;
228 MachineInstr *Inst2 = nullptr;
229 LLVM_DEBUG(dbgs() << "\n[Reaching Defs of operands]: "; DefMI1->dump();
230 DefMI2->dump());
231
232 // Get the reaching defs of DefMI
233 if (DefMI1->getNumOperands() > 1 && DefMI1->getOperand(1).isReg() &&
234 DefMI1->getOperand(1).getReg().isVirtual())
235 Inst1 = MRI->getVRegDef(DefMI1->getOperand(1).getReg());
236
237 if (DefMI2->getNumOperands() > 1 && DefMI2->getOperand(1).isReg() &&
238 DefMI2->getOperand(1).getReg().isVirtual())
239 Inst2 = MRI->getVRegDef(DefMI2->getOperand(1).getReg());
240
241 unsigned Def1OP = DefMI1->getOpcode();
242 unsigned Def2OP = DefMI2->getOpcode();
243
244 MachineInstrBuilder MIB;
245
246 // Check if the both the reaching defs of MI are qf to sf/hf conversions
247 if ((Def1OP == Hexagon::V6_vconv_sf_qf32 &&
248 Def2OP == Hexagon::V6_vconv_sf_qf32) ||
249 (Def1OP == Hexagon::V6_vconv_hf_qf16 &&
250 Def2OP == Hexagon::V6_vconv_hf_qf16)) {
251
252 // If the reaching defs of DefMI are W register type, we return
253 if ((Inst1 && Inst1->getNumOperands() > 0 && Inst1->getOperand(0).isReg() &&
254 MRI->getRegClass(Inst1->getOperand(0).getReg()) ==
255 &Hexagon::HvxWRRegClass) ||
256 (Inst2 && Inst2->getNumOperands() > 0 && Inst2->getOperand(0).isReg() &&
257 MRI->getRegClass(Inst2->getOperand(0).getReg()) ==
258 &Hexagon::HvxWRRegClass))
259 return false;
260
261 MachineOperand &Src1 = DefMI1->getOperand(1);
262 MachineOperand &Src2 = DefMI2->getOperand(1);
263
264 if (MI->getOpcode() != Hexagon::V6_vmpy_qf32_sf) {
265 auto OuterIt = QFPInstMap.find(MI->getOpcode());
266 if (OuterIt == QFPInstMap.end())
267 return false;
268 auto InnerIt = QFPInstMap.find(OuterIt->second);
269 if (InnerIt == QFPInstMap.end())
270 return false;
271 InstTy = InnerIt->second;
272 }
273
274 MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), HII->get(InstTy), Res.getReg())
275 .addReg(Src1.getReg(), {}, Src1.getSubReg())
276 .addReg(Src2.getReg(), {}, Src2.getSubReg());
277 LLVM_DEBUG(dbgs() << "\n[Inserting]: "; MIB.getInstr()->dump());
278 return true;
279
280 // Check if left operand's reaching def is a conversion to sf/hf
281 } else if (((Def1OP == Hexagon::V6_vconv_sf_qf32 &&
282 Def2OP != Hexagon::V6_vconv_sf_qf32) ||
283 (Def1OP == Hexagon::V6_vconv_hf_qf16 &&
284 Def2OP != Hexagon::V6_vconv_hf_qf16)) &&
285 !DefMI2->isPHI() &&
286 (MI->getOpcode() != Hexagon::V6_vmpy_qf32_sf)) {
287
288 if (Inst1 && MRI->getRegClass(Inst1->getOperand(0).getReg()) ==
289 &Hexagon::HvxWRRegClass)
290 return false;
291
292 MachineOperand &Src1 = DefMI1->getOperand(1);
293 MachineOperand &Src2 = MI->getOperand(2);
294
295 MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), HII->get(InstTy), Res.getReg())
296 .addReg(Src1.getReg(), {}, Src1.getSubReg())
297 .addReg(Src2.getReg(), {}, Src2.getSubReg());
298 LLVM_DEBUG(dbgs() << "\n[Inserting]: "; MIB.getInstr()->dump());
299 return true;
300
301 // Check if right operand's reaching def is a conversion to sf/hf
302 } else if (((Def1OP != Hexagon::V6_vconv_sf_qf32 &&
303 Def2OP == Hexagon::V6_vconv_sf_qf32) ||
304 (Def1OP != Hexagon::V6_vconv_hf_qf16 &&
305 Def2OP == Hexagon::V6_vconv_hf_qf16)) &&
306 !DefMI1->isPHI() &&
307 (MI->getOpcode() != Hexagon::V6_vmpy_qf32_sf)) {
308 // The second operand of original instruction is converted.
309 if (Inst2 && MRI->getRegClass(Inst2->getOperand(0).getReg()) ==
310 &Hexagon::HvxWRRegClass)
311 return false;
312
313 MachineOperand &Src1 = MI->getOperand(1);
314 MachineOperand &Src2 = DefMI2->getOperand(1);
315
316 if (InstTy == Hexagon::V6_vsub_qf16_mix ||
317 InstTy == Hexagon::V6_vsub_qf32_mix) {
318 if (!HST->useHVXV81Ops())
319 // vsub_(hf|sf)_mix insts are only avlbl on hvx81+
320 return false;
321 // vsub is not commutative w.r.t. operands -> treat it as a special case
322 // to choose the correct mix instruction.
323 if (Def2OP == Hexagon::V6_vconv_sf_qf32)
324 InstTy = Hexagon::V6_vsub_sf_mix;
325 else if (Def2OP == Hexagon::V6_vconv_hf_qf16)
326 InstTy = Hexagon::V6_vsub_hf_mix;
327 MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), HII->get(InstTy), Res.getReg())
328 .addReg(Src1.getReg(), {}, Src1.getSubReg())
329 .addReg(Src2.getReg(), {}, Src2.getSubReg());
330 } else {
331 MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), HII->get(InstTy), Res.getReg())
332 .addReg(Src2.getReg(), {},
333 Src2.getSubReg()) // Notice the operands are flipped.
334 .addReg(Src1.getReg(), {}, Src1.getSubReg());
335 }
336 LLVM_DEBUG(dbgs() << "\n[Inserting]: "; MIB.getInstr()->dump());
337 return true;
338 }
339
340 return false;
341}
342
343bool HexagonQFPOptimizer::runOnMachineFunction(MachineFunction &MF) {
344
345 bool Changed = false;
346
348 return Changed;
349
350 HST = &MF.getSubtarget<HexagonSubtarget>();
351 if (!HST->useHVXV68Ops() || !HST->usePackets() ||
352 skipFunction(MF.getFunction()))
353 return false;
354 HII = HST->getInstrInfo();
355 MRI = &MF.getRegInfo();
356
358 LLVM_DEBUG(dbgs() << "\n=== Running QFPOptimzer Pass for : " << MF.getName()
359 << " Optimize intermediate conversions ===\n");
360 while (MBBI != MF.end()) {
361 MachineBasicBlock *MBB = &*MBBI;
363 while (MII != MBBI->instr_end()) {
364 MachineInstr *MI = &*MII;
365 ++MII; // As MI might be removed.
366 if (QFPInstMap.count(MI->getOpcode())) {
367 auto OpC = MI->getOpcode();
368 if (DisableQFOptForMul && HII->isQFPMul(MI))
369 continue;
370 if (OpC != Hexagon::V6_vconv_sf_qf32 &&
371 OpC != Hexagon::V6_vconv_hf_qf16) {
372 LLVM_DEBUG(dbgs() << "\n###Analyzing for removal: "; MI->dump());
373 if (optimizeQfp(MI, MBB)) {
374 MI->eraseFromParent();
375 LLVM_DEBUG(dbgs() << "\t....Removing....");
376 Changed = true;
377 }
378 }
379 }
380 }
381 ++MBBI;
382 }
383 return Changed;
384}
MachineInstrBuilder MachineInstrBuilder & DefMI
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
cl::opt< bool > DisableQFOptimizer("disable-qfp-opt", cl::init(false), cl::desc("Disable optimization of Qfloat operations."))
cl::opt< bool > DisableQFOptForMul("disable-qfp-opt-mul", cl::init(true), cl::desc("Disable optimization of Qfloat operations for multiply."))
#define HEXAGON_QFP_OPTIMIZER
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
MachineRegisterInfo & MRI
#define LLVM_DEBUG(...)
Definition Debug.h:119
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool isQFPMul(const MachineInstr *MF) const
const HexagonInstrInfo * getInstrInfo() const override
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
unsigned getNumOperands() const
Retuns the total number of operands.
LLVM_ABI void dump() const
const MachineOperand & getOperand(unsigned i) const
unsigned getSubReg() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createHexagonQFPOptimizer()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209