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"
68#include "llvm/CodeGen/MachineBasicBlock.h"
69#include "llvm/CodeGen/MachineFunction.h"
70#include "llvm/CodeGen/MachineFunctionPass.h"
71#include "llvm/CodeGen/MachineInstr.h"
72#include "llvm/CodeGen/MachineOperand.h"
73#include "llvm/CodeGen/Passes.h"
74#include "llvm/Pass.h"
75#include "llvm/Support/CommandLine.h"
76#include "llvm/Support/Debug.h"
77#include "llvm/Support/raw_ostream.h"
78#include <map>
79
80#define DEBUG_TYPE "hexagon-qfp-optimizer"
81
82using namespace llvm;
83
84cl::opt<bool>
85 DisableQFOptimizer("disable-qfp-opt", cl::init(Val: false),
86 cl::desc("Disable optimization of Qfloat operations."));
87cl::opt<bool> DisableQFOptForMul(
88 "disable-qfp-opt-mul", cl::init(Val: 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();
133 MachineFunctionPass::getAnalysisUsage(AU);
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
148FunctionPass *llvm::createHexagonQFPOptimizer() {
149 return new HexagonQFPOptimizer();
150}
151
152bool HexagonQFPOptimizer::optimizeQfp(MachineInstr *MI,
153 MachineBasicBlock *MBB) {
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(x: 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(Reg: MI->getOperand(i: 1).getReg());
173 MachineOperand &Res = MI->getOperand(i: 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(i: 1).isReg() &&
182 DefMI->getOperand(i: 1).getReg().isVirtual())
183 ReachDefDef = MRI->getVRegDef(Reg: DefMI->getOperand(i: 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(Reg: ReachDefDef->getOperand(i: 0).getReg()) ==
193 &Hexagon::HvxWRRegClass)
194 return false;
195
196 MachineOperand &SrcOp = DefMI->getOperand(i: 1);
197 MIB = BuildMI(BB&: *MBB, I: MI, MIMD: MI->getDebugLoc(), MCID: HII->get(Opcode: InstTy), DestReg: Res.getReg())
198 .addReg(RegNo: SrcOp.getReg(), Flags: {}, SubReg: 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(x: 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(i: 1).isReg())
217 DefMI1 = MRI->getVRegDef(Reg: MI->getOperand(i: 1).getReg());
218 if (MI->getOperand(i: 2).isReg())
219 DefMI2 = MRI->getVRegDef(Reg: MI->getOperand(i: 2).getReg());
220 if (!DefMI1 || !DefMI2)
221 return false;
222
223 MachineOperand &Res = MI->getOperand(i: 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(i: 1).isReg() &&
234 DefMI1->getOperand(i: 1).getReg().isVirtual())
235 Inst1 = MRI->getVRegDef(Reg: DefMI1->getOperand(i: 1).getReg());
236
237 if (DefMI2->getNumOperands() > 1 && DefMI2->getOperand(i: 1).isReg() &&
238 DefMI2->getOperand(i: 1).getReg().isVirtual())
239 Inst2 = MRI->getVRegDef(Reg: DefMI2->getOperand(i: 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(i: 0).isReg() &&
254 MRI->getRegClass(Reg: Inst1->getOperand(i: 0).getReg()) ==
255 &Hexagon::HvxWRRegClass) ||
256 (Inst2 && Inst2->getNumOperands() > 0 && Inst2->getOperand(i: 0).isReg() &&
257 MRI->getRegClass(Reg: Inst2->getOperand(i: 0).getReg()) ==
258 &Hexagon::HvxWRRegClass))
259 return false;
260
261 MachineOperand &Src1 = DefMI1->getOperand(i: 1);
262 MachineOperand &Src2 = DefMI2->getOperand(i: 1);
263
264 if (MI->getOpcode() != Hexagon::V6_vmpy_qf32_sf) {
265 auto OuterIt = QFPInstMap.find(x: MI->getOpcode());
266 if (OuterIt == QFPInstMap.end())
267 return false;
268 auto InnerIt = QFPInstMap.find(x: OuterIt->second);
269 if (InnerIt == QFPInstMap.end())
270 return false;
271 InstTy = InnerIt->second;
272 }
273
274 MIB = BuildMI(BB&: *MBB, I: MI, MIMD: MI->getDebugLoc(), MCID: HII->get(Opcode: InstTy), DestReg: Res.getReg())
275 .addReg(RegNo: Src1.getReg(), Flags: {}, SubReg: Src1.getSubReg())
276 .addReg(RegNo: Src2.getReg(), Flags: {}, SubReg: 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(Reg: Inst1->getOperand(i: 0).getReg()) ==
289 &Hexagon::HvxWRRegClass)
290 return false;
291
292 MachineOperand &Src1 = DefMI1->getOperand(i: 1);
293 MachineOperand &Src2 = MI->getOperand(i: 2);
294
295 MIB = BuildMI(BB&: *MBB, I: MI, MIMD: MI->getDebugLoc(), MCID: HII->get(Opcode: InstTy), DestReg: Res.getReg())
296 .addReg(RegNo: Src1.getReg(), Flags: {}, SubReg: Src1.getSubReg())
297 .addReg(RegNo: Src2.getReg(), Flags: {}, SubReg: 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(Reg: Inst2->getOperand(i: 0).getReg()) ==
310 &Hexagon::HvxWRRegClass)
311 return false;
312
313 MachineOperand &Src1 = MI->getOperand(i: 1);
314 MachineOperand &Src2 = DefMI2->getOperand(i: 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(BB&: *MBB, I: MI, MIMD: MI->getDebugLoc(), MCID: HII->get(Opcode: InstTy), DestReg: Res.getReg())
328 .addReg(RegNo: Src1.getReg(), Flags: {}, SubReg: Src1.getSubReg())
329 .addReg(RegNo: Src2.getReg(), Flags: {}, SubReg: Src2.getSubReg());
330 } else {
331 MIB = BuildMI(BB&: *MBB, I: MI, MIMD: MI->getDebugLoc(), MCID: HII->get(Opcode: InstTy), DestReg: Res.getReg())
332 .addReg(RegNo: Src2.getReg(), Flags: {},
333 SubReg: Src2.getSubReg()) // Notice the operands are flipped.
334 .addReg(RegNo: Src1.getReg(), Flags: {}, SubReg: 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
347 if (DisableQFOptimizer)
348 return Changed;
349
350 HST = &MF.getSubtarget<HexagonSubtarget>();
351 if (!HST->useHVXV68Ops() || !HST->usePackets() ||
352 skipFunction(F: MF.getFunction()))
353 return false;
354 HII = HST->getInstrInfo();
355 MRI = &MF.getRegInfo();
356
357 MachineFunction::iterator MBBI = MF.begin();
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;
362 MachineBasicBlock::iterator MII = MBBI->instr_begin();
363 while (MII != MBBI->instr_end()) {
364 MachineInstr *MI = &*MII;
365 ++MII; // As MI might be removed.
366 if (QFPInstMap.count(x: MI->getOpcode())) {
367 auto OpC = MI->getOpcode();
368 if (DisableQFOptForMul && HII->isQFPMul(MF: 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}
385