LLVM 20.0.0git
RISCVVLOptimizer.cpp
Go to the documentation of this file.
1//===-------------- RISCVVLOptimizer.cpp - VL Optimizer -------------------===//
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 reduces the VL where possible at the MI level, before VSETVLI
10// instructions are inserted.
11//
12// The purpose of this optimization is to make the VL argument, for instructions
13// that have a VL argument, as small as possible. This is implemented by
14// visiting each instruction in reverse order and checking that if it has a VL
15// argument, whether the VL can be reduced.
16//
17//===---------------------------------------------------------------------===//
18
19#include "RISCV.h"
20#include "RISCVSubtarget.h"
21#include "llvm/ADT/SetVector.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "riscv-vl-optimizer"
29#define PASS_NAME "RISC-V VL Optimizer"
30
31namespace {
32
33class RISCVVLOptimizer : public MachineFunctionPass {
35 const MachineDominatorTree *MDT;
36
37public:
38 static char ID;
39
40 RISCVVLOptimizer() : MachineFunctionPass(ID) {}
41
42 bool runOnMachineFunction(MachineFunction &MF) override;
43
44 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.setPreservesCFG();
48 }
49
50 StringRef getPassName() const override { return PASS_NAME; }
51
52private:
53 std::optional<MachineOperand> getMinimumVLForUser(MachineOperand &UserOp);
54 /// Returns the largest common VL MachineOperand that may be used to optimize
55 /// MI. Returns std::nullopt if it failed to find a suitable VL.
56 std::optional<MachineOperand> checkUsers(MachineInstr &MI);
57 bool tryReduceVL(MachineInstr &MI);
58 bool isCandidate(const MachineInstr &MI) const;
59};
60
61} // end anonymous namespace
62
63char RISCVVLOptimizer::ID = 0;
64INITIALIZE_PASS_BEGIN(RISCVVLOptimizer, DEBUG_TYPE, PASS_NAME, false, false)
67
69 return new RISCVVLOptimizer();
70}
71
72/// Return true if R is a physical or virtual vector register, false otherwise.
74 if (R.isPhysical())
75 return RISCV::VRRegClass.contains(R);
76 const TargetRegisterClass *RC = MRI->getRegClass(R);
77 return RISCVRI::isVRegClass(RC->TSFlags);
78}
79
80/// Represents the EMUL and EEW of a MachineOperand.
82 // Represent as 1,2,4,8, ... and fractional indicator. This is because
83 // EMUL can take on values that don't map to RISCVII::VLMUL values exactly.
84 // For example, a mask operand can have an EMUL less than MF8.
85 std::optional<std::pair<unsigned, bool>> EMUL;
86
87 unsigned Log2EEW;
88
90 : EMUL(RISCVVType::decodeVLMUL(EMUL)), Log2EEW(Log2EEW) {}
91
92 OperandInfo(std::pair<unsigned, bool> EMUL, unsigned Log2EEW)
93 : EMUL(EMUL), Log2EEW(Log2EEW) {}
94
96
97 OperandInfo() = delete;
98
99 static bool EMULAndEEWAreEqual(const OperandInfo &A, const OperandInfo &B) {
100 return A.Log2EEW == B.Log2EEW && A.EMUL->first == B.EMUL->first &&
101 A.EMUL->second == B.EMUL->second;
102 }
103
104 static bool EEWAreEqual(const OperandInfo &A, const OperandInfo &B) {
105 return A.Log2EEW == B.Log2EEW;
106 }
107
108 void print(raw_ostream &OS) const {
109 if (EMUL) {
110 OS << "EMUL: m";
111 if (EMUL->second)
112 OS << "f";
113 OS << EMUL->first;
114 } else
115 OS << "EMUL: unknown\n";
116 OS << ", EEW: " << (1 << Log2EEW);
117 }
118};
119
122 OI.print(OS);
123 return OS;
124}
125
128 const std::optional<OperandInfo> &OI) {
129 if (OI)
130 OI->print(OS);
131 else
132 OS << "nullopt";
133 return OS;
134}
135
136namespace llvm {
137namespace RISCVVType {
138/// Return EMUL = (EEW / SEW) * LMUL where EEW comes from Log2EEW and LMUL and
139/// SEW are from the TSFlags of MI.
140static std::pair<unsigned, bool>
142 RISCVII::VLMUL MIVLMUL = RISCVII::getLMul(MI.getDesc().TSFlags);
143 auto [MILMUL, MILMULIsFractional] = RISCVVType::decodeVLMUL(MIVLMUL);
144 unsigned MILog2SEW =
145 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
146
147 // Mask instructions will have 0 as the SEW operand. But the LMUL of these
148 // instructions is calculated is as if the SEW operand was 3 (e8).
149 if (MILog2SEW == 0)
150 MILog2SEW = 3;
151
152 unsigned MISEW = 1 << MILog2SEW;
153
154 unsigned EEW = 1 << Log2EEW;
155 // Calculate (EEW/SEW)*LMUL preserving fractions less than 1. Use GCD
156 // to put fraction in simplest form.
157 unsigned Num = EEW, Denom = MISEW;
158 int GCD = MILMULIsFractional ? std::gcd(Num, Denom * MILMUL)
159 : std::gcd(Num * MILMUL, Denom);
160 Num = MILMULIsFractional ? Num / GCD : Num * MILMUL / GCD;
161 Denom = MILMULIsFractional ? Denom * MILMUL / GCD : Denom / GCD;
162 return std::make_pair(Num > Denom ? Num : Denom, Denom > Num);
163}
164} // end namespace RISCVVType
165} // end namespace llvm
166
167/// Dest has EEW=SEW. Source EEW=SEW/Factor (i.e. F2 => EEW/2).
168/// SEW comes from TSFlags of MI.
169static unsigned getIntegerExtensionOperandEEW(unsigned Factor,
170 const MachineInstr &MI,
171 const MachineOperand &MO) {
172 unsigned MILog2SEW =
173 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
174
175 if (MO.getOperandNo() == 0)
176 return MILog2SEW;
177
178 unsigned MISEW = 1 << MILog2SEW;
179 unsigned EEW = MISEW / Factor;
180 unsigned Log2EEW = Log2_32(EEW);
181
182 return Log2EEW;
183}
184
185/// Check whether MO is a mask operand of MI.
186static bool isMaskOperand(const MachineInstr &MI, const MachineOperand &MO,
187 const MachineRegisterInfo *MRI) {
188
189 if (!MO.isReg() || !isVectorRegClass(MO.getReg(), MRI))
190 return false;
191
192 const MCInstrDesc &Desc = MI.getDesc();
193 return Desc.operands()[MO.getOperandNo()].RegClass == RISCV::VMV0RegClassID;
194}
195
196static std::optional<unsigned>
198 const MachineInstr &MI = *MO.getParent();
200 RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
201 assert(RVV && "Could not find MI in PseudoTable");
202
203 // MI has a SEW associated with it. The RVV specification defines
204 // the EEW of each operand and definition in relation to MI.SEW.
205 unsigned MILog2SEW =
206 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
207
208 const bool HasPassthru = RISCVII::isFirstDefTiedToFirstUse(MI.getDesc());
209
210 // We bail out early for instructions that have passthru with non NoRegister,
211 // which means they are using TU policy. We are not interested in these
212 // since they must preserve the entire register content.
213 if (HasPassthru && MO.getOperandNo() == MI.getNumExplicitDefs() &&
214 (MO.getReg() != RISCV::NoRegister))
215 return std::nullopt;
216
217 bool IsMODef = MO.getOperandNo() == 0;
218
219 // All mask operands have EEW=1
220 if (isMaskOperand(MI, MO, MRI))
221 return 0;
222
223 // switch against BaseInstr to reduce number of cases that need to be
224 // considered.
225 switch (RVV->BaseInstr) {
226
227 // 6. Configuration-Setting Instructions
228 // Configuration setting instructions do not read or write vector registers
229 case RISCV::VSETIVLI:
230 case RISCV::VSETVL:
231 case RISCV::VSETVLI:
232 llvm_unreachable("Configuration setting instructions do not read or write "
233 "vector registers");
234
235 // Vector Loads and Stores
236 // Vector Unit-Stride Instructions
237 // Vector Strided Instructions
238 /// Dest EEW encoded in the instruction
239 case RISCV::VLM_V:
240 case RISCV::VSM_V:
241 return 0;
242 case RISCV::VLE8_V:
243 case RISCV::VSE8_V:
244 case RISCV::VLSE8_V:
245 case RISCV::VSSE8_V:
246 return 3;
247 case RISCV::VLE16_V:
248 case RISCV::VSE16_V:
249 case RISCV::VLSE16_V:
250 case RISCV::VSSE16_V:
251 return 4;
252 case RISCV::VLE32_V:
253 case RISCV::VSE32_V:
254 case RISCV::VLSE32_V:
255 case RISCV::VSSE32_V:
256 return 5;
257 case RISCV::VLE64_V:
258 case RISCV::VSE64_V:
259 case RISCV::VLSE64_V:
260 case RISCV::VSSE64_V:
261 return 6;
262
263 // Vector Indexed Instructions
264 // vs(o|u)xei<eew>.v
265 // Dest/Data (operand 0) EEW=SEW. Source EEW=<eew>.
266 case RISCV::VLUXEI8_V:
267 case RISCV::VLOXEI8_V:
268 case RISCV::VSUXEI8_V:
269 case RISCV::VSOXEI8_V: {
270 if (MO.getOperandNo() == 0)
271 return MILog2SEW;
272 return 3;
273 }
274 case RISCV::VLUXEI16_V:
275 case RISCV::VLOXEI16_V:
276 case RISCV::VSUXEI16_V:
277 case RISCV::VSOXEI16_V: {
278 if (MO.getOperandNo() == 0)
279 return MILog2SEW;
280 return 4;
281 }
282 case RISCV::VLUXEI32_V:
283 case RISCV::VLOXEI32_V:
284 case RISCV::VSUXEI32_V:
285 case RISCV::VSOXEI32_V: {
286 if (MO.getOperandNo() == 0)
287 return MILog2SEW;
288 return 5;
289 }
290 case RISCV::VLUXEI64_V:
291 case RISCV::VLOXEI64_V:
292 case RISCV::VSUXEI64_V:
293 case RISCV::VSOXEI64_V: {
294 if (MO.getOperandNo() == 0)
295 return MILog2SEW;
296 return 6;
297 }
298
299 // Vector Integer Arithmetic Instructions
300 // Vector Single-Width Integer Add and Subtract
301 case RISCV::VADD_VI:
302 case RISCV::VADD_VV:
303 case RISCV::VADD_VX:
304 case RISCV::VSUB_VV:
305 case RISCV::VSUB_VX:
306 case RISCV::VRSUB_VI:
307 case RISCV::VRSUB_VX:
308 // Vector Bitwise Logical Instructions
309 // Vector Single-Width Shift Instructions
310 // EEW=SEW.
311 case RISCV::VAND_VI:
312 case RISCV::VAND_VV:
313 case RISCV::VAND_VX:
314 case RISCV::VOR_VI:
315 case RISCV::VOR_VV:
316 case RISCV::VOR_VX:
317 case RISCV::VXOR_VI:
318 case RISCV::VXOR_VV:
319 case RISCV::VXOR_VX:
320 case RISCV::VSLL_VI:
321 case RISCV::VSLL_VV:
322 case RISCV::VSLL_VX:
323 case RISCV::VSRL_VI:
324 case RISCV::VSRL_VV:
325 case RISCV::VSRL_VX:
326 case RISCV::VSRA_VI:
327 case RISCV::VSRA_VV:
328 case RISCV::VSRA_VX:
329 // Vector Integer Min/Max Instructions
330 // EEW=SEW.
331 case RISCV::VMINU_VV:
332 case RISCV::VMINU_VX:
333 case RISCV::VMIN_VV:
334 case RISCV::VMIN_VX:
335 case RISCV::VMAXU_VV:
336 case RISCV::VMAXU_VX:
337 case RISCV::VMAX_VV:
338 case RISCV::VMAX_VX:
339 // Vector Single-Width Integer Multiply Instructions
340 // Source and Dest EEW=SEW.
341 case RISCV::VMUL_VV:
342 case RISCV::VMUL_VX:
343 case RISCV::VMULH_VV:
344 case RISCV::VMULH_VX:
345 case RISCV::VMULHU_VV:
346 case RISCV::VMULHU_VX:
347 case RISCV::VMULHSU_VV:
348 case RISCV::VMULHSU_VX:
349 // Vector Integer Divide Instructions
350 // EEW=SEW.
351 case RISCV::VDIVU_VV:
352 case RISCV::VDIVU_VX:
353 case RISCV::VDIV_VV:
354 case RISCV::VDIV_VX:
355 case RISCV::VREMU_VV:
356 case RISCV::VREMU_VX:
357 case RISCV::VREM_VV:
358 case RISCV::VREM_VX:
359 // Vector Single-Width Integer Multiply-Add Instructions
360 // EEW=SEW.
361 case RISCV::VMACC_VV:
362 case RISCV::VMACC_VX:
363 case RISCV::VNMSAC_VV:
364 case RISCV::VNMSAC_VX:
365 case RISCV::VMADD_VV:
366 case RISCV::VMADD_VX:
367 case RISCV::VNMSUB_VV:
368 case RISCV::VNMSUB_VX:
369 // Vector Integer Merge Instructions
370 // Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions
371 // EEW=SEW, except the mask operand has EEW=1. Mask operand is handled
372 // before this switch.
373 case RISCV::VMERGE_VIM:
374 case RISCV::VMERGE_VVM:
375 case RISCV::VMERGE_VXM:
376 case RISCV::VADC_VIM:
377 case RISCV::VADC_VVM:
378 case RISCV::VADC_VXM:
379 case RISCV::VSBC_VVM:
380 case RISCV::VSBC_VXM:
381 // Vector Integer Move Instructions
382 // Vector Fixed-Point Arithmetic Instructions
383 // Vector Single-Width Saturating Add and Subtract
384 // Vector Single-Width Averaging Add and Subtract
385 // EEW=SEW.
386 case RISCV::VMV_V_I:
387 case RISCV::VMV_V_V:
388 case RISCV::VMV_V_X:
389 case RISCV::VSADDU_VI:
390 case RISCV::VSADDU_VV:
391 case RISCV::VSADDU_VX:
392 case RISCV::VSADD_VI:
393 case RISCV::VSADD_VV:
394 case RISCV::VSADD_VX:
395 case RISCV::VSSUBU_VV:
396 case RISCV::VSSUBU_VX:
397 case RISCV::VSSUB_VV:
398 case RISCV::VSSUB_VX:
399 case RISCV::VAADDU_VV:
400 case RISCV::VAADDU_VX:
401 case RISCV::VAADD_VV:
402 case RISCV::VAADD_VX:
403 case RISCV::VASUBU_VV:
404 case RISCV::VASUBU_VX:
405 case RISCV::VASUB_VV:
406 case RISCV::VASUB_VX:
407 // Vector Single-Width Fractional Multiply with Rounding and Saturation
408 // EEW=SEW. The instruction produces 2*SEW product internally but
409 // saturates to fit into SEW bits.
410 case RISCV::VSMUL_VV:
411 case RISCV::VSMUL_VX:
412 // Vector Single-Width Scaling Shift Instructions
413 // EEW=SEW.
414 case RISCV::VSSRL_VI:
415 case RISCV::VSSRL_VV:
416 case RISCV::VSSRL_VX:
417 case RISCV::VSSRA_VI:
418 case RISCV::VSSRA_VV:
419 case RISCV::VSSRA_VX:
420 // Vector Permutation Instructions
421 // Integer Scalar Move Instructions
422 // Floating-Point Scalar Move Instructions
423 // EEW=SEW.
424 case RISCV::VMV_X_S:
425 case RISCV::VMV_S_X:
426 case RISCV::VFMV_F_S:
427 case RISCV::VFMV_S_F:
428 // Vector Slide Instructions
429 // EEW=SEW.
430 case RISCV::VSLIDEUP_VI:
431 case RISCV::VSLIDEUP_VX:
432 case RISCV::VSLIDEDOWN_VI:
433 case RISCV::VSLIDEDOWN_VX:
434 case RISCV::VSLIDE1UP_VX:
435 case RISCV::VFSLIDE1UP_VF:
436 case RISCV::VSLIDE1DOWN_VX:
437 case RISCV::VFSLIDE1DOWN_VF:
438 // Vector Register Gather Instructions
439 // EEW=SEW. For mask operand, EEW=1.
440 case RISCV::VRGATHER_VI:
441 case RISCV::VRGATHER_VV:
442 case RISCV::VRGATHER_VX:
443 // Vector Compress Instruction
444 // EEW=SEW.
445 case RISCV::VCOMPRESS_VM:
446 // Vector Element Index Instruction
447 case RISCV::VID_V:
448 // Vector Single-Width Floating-Point Add/Subtract Instructions
449 case RISCV::VFADD_VF:
450 case RISCV::VFADD_VV:
451 case RISCV::VFSUB_VF:
452 case RISCV::VFSUB_VV:
453 case RISCV::VFRSUB_VF:
454 // Vector Single-Width Floating-Point Multiply/Divide Instructions
455 case RISCV::VFMUL_VF:
456 case RISCV::VFMUL_VV:
457 case RISCV::VFDIV_VF:
458 case RISCV::VFDIV_VV:
459 case RISCV::VFRDIV_VF:
460 // Vector Floating-Point Square-Root Instruction
461 case RISCV::VFSQRT_V:
462 // Vector Floating-Point Reciprocal Square-Root Estimate Instruction
463 case RISCV::VFRSQRT7_V:
464 // Vector Floating-Point Reciprocal Estimate Instruction
465 case RISCV::VFREC7_V:
466 // Vector Floating-Point MIN/MAX Instructions
467 case RISCV::VFMIN_VF:
468 case RISCV::VFMIN_VV:
469 case RISCV::VFMAX_VF:
470 case RISCV::VFMAX_VV:
471 // Vector Floating-Point Sign-Injection Instructions
472 case RISCV::VFSGNJ_VF:
473 case RISCV::VFSGNJ_VV:
474 case RISCV::VFSGNJN_VV:
475 case RISCV::VFSGNJN_VF:
476 case RISCV::VFSGNJX_VF:
477 case RISCV::VFSGNJX_VV:
478 // Vector Floating-Point Classify Instruction
479 case RISCV::VFCLASS_V:
480 // Vector Floating-Point Move Instruction
481 case RISCV::VFMV_V_F:
482 // Single-Width Floating-Point/Integer Type-Convert Instructions
483 case RISCV::VFCVT_XU_F_V:
484 case RISCV::VFCVT_X_F_V:
485 case RISCV::VFCVT_RTZ_XU_F_V:
486 case RISCV::VFCVT_RTZ_X_F_V:
487 case RISCV::VFCVT_F_XU_V:
488 case RISCV::VFCVT_F_X_V:
489 // Vector Floating-Point Merge Instruction
490 case RISCV::VFMERGE_VFM:
491 // Vector count population in mask vcpop.m
492 // vfirst find-first-set mask bit
493 case RISCV::VCPOP_M:
494 case RISCV::VFIRST_M:
495 return MILog2SEW;
496
497 // Vector Widening Integer Add/Subtract
498 // Def uses EEW=2*SEW . Operands use EEW=SEW.
499 case RISCV::VWADDU_VV:
500 case RISCV::VWADDU_VX:
501 case RISCV::VWSUBU_VV:
502 case RISCV::VWSUBU_VX:
503 case RISCV::VWADD_VV:
504 case RISCV::VWADD_VX:
505 case RISCV::VWSUB_VV:
506 case RISCV::VWSUB_VX:
507 case RISCV::VWSLL_VI:
508 // Vector Widening Integer Multiply Instructions
509 // Destination EEW=2*SEW. Source EEW=SEW.
510 case RISCV::VWMUL_VV:
511 case RISCV::VWMUL_VX:
512 case RISCV::VWMULSU_VV:
513 case RISCV::VWMULSU_VX:
514 case RISCV::VWMULU_VV:
515 case RISCV::VWMULU_VX:
516 // Vector Widening Integer Multiply-Add Instructions
517 // Destination EEW=2*SEW. Source EEW=SEW.
518 // A SEW-bit*SEW-bit multiply of the sources forms a 2*SEW-bit value, which
519 // is then added to the 2*SEW-bit Dest. These instructions never have a
520 // passthru operand.
521 case RISCV::VWMACCU_VV:
522 case RISCV::VWMACCU_VX:
523 case RISCV::VWMACC_VV:
524 case RISCV::VWMACC_VX:
525 case RISCV::VWMACCSU_VV:
526 case RISCV::VWMACCSU_VX:
527 case RISCV::VWMACCUS_VX:
528 // Vector Widening Floating-Point Fused Multiply-Add Instructions
529 case RISCV::VFWMACC_VF:
530 case RISCV::VFWMACC_VV:
531 case RISCV::VFWNMACC_VF:
532 case RISCV::VFWNMACC_VV:
533 case RISCV::VFWMSAC_VF:
534 case RISCV::VFWMSAC_VV:
535 case RISCV::VFWNMSAC_VF:
536 case RISCV::VFWNMSAC_VV:
537 // Vector Widening Floating-Point Add/Subtract Instructions
538 // Dest EEW=2*SEW. Source EEW=SEW.
539 case RISCV::VFWADD_VV:
540 case RISCV::VFWADD_VF:
541 case RISCV::VFWSUB_VV:
542 case RISCV::VFWSUB_VF:
543 // Vector Widening Floating-Point Multiply
544 case RISCV::VFWMUL_VF:
545 case RISCV::VFWMUL_VV:
546 // Widening Floating-Point/Integer Type-Convert Instructions
547 case RISCV::VFWCVT_XU_F_V:
548 case RISCV::VFWCVT_X_F_V:
549 case RISCV::VFWCVT_RTZ_XU_F_V:
550 case RISCV::VFWCVT_RTZ_X_F_V:
551 case RISCV::VFWCVT_F_XU_V:
552 case RISCV::VFWCVT_F_X_V:
553 case RISCV::VFWCVT_F_F_V:
554 case RISCV::VFWCVTBF16_F_F_V:
555 return IsMODef ? MILog2SEW + 1 : MILog2SEW;
556
557 // Def and Op1 uses EEW=2*SEW. Op2 uses EEW=SEW.
558 case RISCV::VWADDU_WV:
559 case RISCV::VWADDU_WX:
560 case RISCV::VWSUBU_WV:
561 case RISCV::VWSUBU_WX:
562 case RISCV::VWADD_WV:
563 case RISCV::VWADD_WX:
564 case RISCV::VWSUB_WV:
565 case RISCV::VWSUB_WX:
566 // Vector Widening Floating-Point Add/Subtract Instructions
567 case RISCV::VFWADD_WF:
568 case RISCV::VFWADD_WV:
569 case RISCV::VFWSUB_WF:
570 case RISCV::VFWSUB_WV: {
571 bool IsOp1 = HasPassthru ? MO.getOperandNo() == 2 : MO.getOperandNo() == 1;
572 bool TwoTimes = IsMODef || IsOp1;
573 return TwoTimes ? MILog2SEW + 1 : MILog2SEW;
574 }
575
576 // Vector Integer Extension
577 case RISCV::VZEXT_VF2:
578 case RISCV::VSEXT_VF2:
579 return getIntegerExtensionOperandEEW(2, MI, MO);
580 case RISCV::VZEXT_VF4:
581 case RISCV::VSEXT_VF4:
582 return getIntegerExtensionOperandEEW(4, MI, MO);
583 case RISCV::VZEXT_VF8:
584 case RISCV::VSEXT_VF8:
585 return getIntegerExtensionOperandEEW(8, MI, MO);
586
587 // Vector Narrowing Integer Right Shift Instructions
588 // Destination EEW=SEW, Op 1 has EEW=2*SEW. Op2 has EEW=SEW
589 case RISCV::VNSRL_WX:
590 case RISCV::VNSRL_WI:
591 case RISCV::VNSRL_WV:
592 case RISCV::VNSRA_WI:
593 case RISCV::VNSRA_WV:
594 case RISCV::VNSRA_WX:
595 // Vector Narrowing Fixed-Point Clip Instructions
596 // Destination and Op1 EEW=SEW. Op2 EEW=2*SEW.
597 case RISCV::VNCLIPU_WI:
598 case RISCV::VNCLIPU_WV:
599 case RISCV::VNCLIPU_WX:
600 case RISCV::VNCLIP_WI:
601 case RISCV::VNCLIP_WV:
602 case RISCV::VNCLIP_WX:
603 // Narrowing Floating-Point/Integer Type-Convert Instructions
604 case RISCV::VFNCVT_XU_F_W:
605 case RISCV::VFNCVT_X_F_W:
606 case RISCV::VFNCVT_RTZ_XU_F_W:
607 case RISCV::VFNCVT_RTZ_X_F_W:
608 case RISCV::VFNCVT_F_XU_W:
609 case RISCV::VFNCVT_F_X_W:
610 case RISCV::VFNCVT_F_F_W:
611 case RISCV::VFNCVT_ROD_F_F_W:
612 case RISCV::VFNCVTBF16_F_F_W: {
613 bool IsOp1 = HasPassthru ? MO.getOperandNo() == 2 : MO.getOperandNo() == 1;
614 bool TwoTimes = IsOp1;
615 return TwoTimes ? MILog2SEW + 1 : MILog2SEW;
616 }
617
618 // Vector Mask Instructions
619 // Vector Mask-Register Logical Instructions
620 // vmsbf.m set-before-first mask bit
621 // vmsif.m set-including-first mask bit
622 // vmsof.m set-only-first mask bit
623 // EEW=1
624 // We handle the cases when operand is a v0 mask operand above the switch,
625 // but these instructions may use non-v0 mask operands and need to be handled
626 // specifically.
627 case RISCV::VMAND_MM:
628 case RISCV::VMNAND_MM:
629 case RISCV::VMANDN_MM:
630 case RISCV::VMXOR_MM:
631 case RISCV::VMOR_MM:
632 case RISCV::VMNOR_MM:
633 case RISCV::VMORN_MM:
634 case RISCV::VMXNOR_MM:
635 case RISCV::VMSBF_M:
636 case RISCV::VMSIF_M:
637 case RISCV::VMSOF_M: {
638 return MILog2SEW;
639 }
640
641 // Vector Iota Instruction
642 // EEW=SEW, except the mask operand has EEW=1. Mask operand is not handled
643 // before this switch.
644 case RISCV::VIOTA_M: {
645 if (IsMODef || MO.getOperandNo() == 1)
646 return MILog2SEW;
647 return 0;
648 }
649
650 // Vector Integer Compare Instructions
651 // Dest EEW=1. Source EEW=SEW.
652 case RISCV::VMSEQ_VI:
653 case RISCV::VMSEQ_VV:
654 case RISCV::VMSEQ_VX:
655 case RISCV::VMSNE_VI:
656 case RISCV::VMSNE_VV:
657 case RISCV::VMSNE_VX:
658 case RISCV::VMSLTU_VV:
659 case RISCV::VMSLTU_VX:
660 case RISCV::VMSLT_VV:
661 case RISCV::VMSLT_VX:
662 case RISCV::VMSLEU_VV:
663 case RISCV::VMSLEU_VI:
664 case RISCV::VMSLEU_VX:
665 case RISCV::VMSLE_VV:
666 case RISCV::VMSLE_VI:
667 case RISCV::VMSLE_VX:
668 case RISCV::VMSGTU_VI:
669 case RISCV::VMSGTU_VX:
670 case RISCV::VMSGT_VI:
671 case RISCV::VMSGT_VX:
672 // Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions
673 // Dest EEW=1. Source EEW=SEW. Mask source operand handled above this switch.
674 case RISCV::VMADC_VIM:
675 case RISCV::VMADC_VVM:
676 case RISCV::VMADC_VXM:
677 case RISCV::VMSBC_VVM:
678 case RISCV::VMSBC_VXM:
679 // Dest EEW=1. Source EEW=SEW.
680 case RISCV::VMADC_VV:
681 case RISCV::VMADC_VI:
682 case RISCV::VMADC_VX:
683 case RISCV::VMSBC_VV:
684 case RISCV::VMSBC_VX:
685 // 13.13. Vector Floating-Point Compare Instructions
686 // Dest EEW=1. Source EEW=SEW
687 case RISCV::VMFEQ_VF:
688 case RISCV::VMFEQ_VV:
689 case RISCV::VMFNE_VF:
690 case RISCV::VMFNE_VV:
691 case RISCV::VMFLT_VF:
692 case RISCV::VMFLT_VV:
693 case RISCV::VMFLE_VF:
694 case RISCV::VMFLE_VV:
695 case RISCV::VMFGT_VF:
696 case RISCV::VMFGE_VF: {
697 if (IsMODef)
698 return 0;
699 return MILog2SEW;
700 }
701
702 // Vector Reduction Operations
703 // Vector Single-Width Integer Reduction Instructions
704 case RISCV::VREDAND_VS:
705 case RISCV::VREDMAX_VS:
706 case RISCV::VREDMAXU_VS:
707 case RISCV::VREDMIN_VS:
708 case RISCV::VREDMINU_VS:
709 case RISCV::VREDOR_VS:
710 case RISCV::VREDSUM_VS:
711 case RISCV::VREDXOR_VS:
712 // Vector Single-Width Floating-Point Reduction Instructions
713 case RISCV::VFREDMAX_VS:
714 case RISCV::VFREDMIN_VS:
715 case RISCV::VFREDOSUM_VS:
716 case RISCV::VFREDUSUM_VS: {
717 return MILog2SEW;
718 }
719
720 // Vector Widening Integer Reduction Instructions
721 // The Dest and VS1 read only element 0 for the vector register. Return
722 // 2*EEW for these. VS2 has EEW=SEW and EMUL=LMUL.
723 case RISCV::VWREDSUM_VS:
724 case RISCV::VWREDSUMU_VS:
725 // Vector Widening Floating-Point Reduction Instructions
726 case RISCV::VFWREDOSUM_VS:
727 case RISCV::VFWREDUSUM_VS: {
728 bool TwoTimes = IsMODef || MO.getOperandNo() == 3;
729 return TwoTimes ? MILog2SEW + 1 : MILog2SEW;
730 }
731
732 default:
733 return std::nullopt;
734 }
735}
736
737static std::optional<OperandInfo>
739 const MachineInstr &MI = *MO.getParent();
741 RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
742 assert(RVV && "Could not find MI in PseudoTable");
743
744 std::optional<unsigned> Log2EEW = getOperandLog2EEW(MO, MRI);
745 if (!Log2EEW)
746 return std::nullopt;
747
748 switch (RVV->BaseInstr) {
749 // Vector Reduction Operations
750 // Vector Single-Width Integer Reduction Instructions
751 // Vector Widening Integer Reduction Instructions
752 // Vector Widening Floating-Point Reduction Instructions
753 // The Dest and VS1 only read element 0 of the vector register. Return just
754 // the EEW for these.
755 case RISCV::VREDAND_VS:
756 case RISCV::VREDMAX_VS:
757 case RISCV::VREDMAXU_VS:
758 case RISCV::VREDMIN_VS:
759 case RISCV::VREDMINU_VS:
760 case RISCV::VREDOR_VS:
761 case RISCV::VREDSUM_VS:
762 case RISCV::VREDXOR_VS:
763 case RISCV::VWREDSUM_VS:
764 case RISCV::VWREDSUMU_VS:
765 case RISCV::VFWREDOSUM_VS:
766 case RISCV::VFWREDUSUM_VS:
767 if (MO.getOperandNo() != 2)
768 return OperandInfo(*Log2EEW);
769 break;
770 };
771
772 // All others have EMUL=EEW/SEW*LMUL
774 *Log2EEW);
775}
776
777/// Return true if this optimization should consider MI for VL reduction. This
778/// white-list approach simplifies this optimization for instructions that may
779/// have more complex semantics with relation to how it uses VL.
780static bool isSupportedInstr(const MachineInstr &MI) {
782 RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
783
784 if (!RVV)
785 return false;
786
787 switch (RVV->BaseInstr) {
788 // Vector Unit-Stride Instructions
789 // Vector Strided Instructions
790 case RISCV::VLM_V:
791 case RISCV::VLE8_V:
792 case RISCV::VLSE8_V:
793 case RISCV::VLE16_V:
794 case RISCV::VLSE16_V:
795 case RISCV::VLE32_V:
796 case RISCV::VLSE32_V:
797 case RISCV::VLE64_V:
798 case RISCV::VLSE64_V:
799 // Vector Indexed Instructions
800 case RISCV::VLUXEI8_V:
801 case RISCV::VLOXEI8_V:
802 case RISCV::VLUXEI16_V:
803 case RISCV::VLOXEI16_V:
804 case RISCV::VLUXEI32_V:
805 case RISCV::VLOXEI32_V:
806 case RISCV::VLUXEI64_V:
807 case RISCV::VLOXEI64_V: {
808 for (const MachineMemOperand *MMO : MI.memoperands())
809 if (MMO->isVolatile())
810 return false;
811 return true;
812 }
813
814 // Vector Single-Width Integer Add and Subtract
815 case RISCV::VADD_VI:
816 case RISCV::VADD_VV:
817 case RISCV::VADD_VX:
818 case RISCV::VSUB_VV:
819 case RISCV::VSUB_VX:
820 case RISCV::VRSUB_VI:
821 case RISCV::VRSUB_VX:
822 // Vector Bitwise Logical Instructions
823 // Vector Single-Width Shift Instructions
824 case RISCV::VAND_VI:
825 case RISCV::VAND_VV:
826 case RISCV::VAND_VX:
827 case RISCV::VOR_VI:
828 case RISCV::VOR_VV:
829 case RISCV::VOR_VX:
830 case RISCV::VXOR_VI:
831 case RISCV::VXOR_VV:
832 case RISCV::VXOR_VX:
833 case RISCV::VSLL_VI:
834 case RISCV::VSLL_VV:
835 case RISCV::VSLL_VX:
836 case RISCV::VSRL_VI:
837 case RISCV::VSRL_VV:
838 case RISCV::VSRL_VX:
839 case RISCV::VSRA_VI:
840 case RISCV::VSRA_VV:
841 case RISCV::VSRA_VX:
842 // Vector Widening Integer Add/Subtract
843 case RISCV::VWADDU_VV:
844 case RISCV::VWADDU_VX:
845 case RISCV::VWSUBU_VV:
846 case RISCV::VWSUBU_VX:
847 case RISCV::VWADD_VV:
848 case RISCV::VWADD_VX:
849 case RISCV::VWSUB_VV:
850 case RISCV::VWSUB_VX:
851 case RISCV::VWADDU_WV:
852 case RISCV::VWADDU_WX:
853 case RISCV::VWSUBU_WV:
854 case RISCV::VWSUBU_WX:
855 case RISCV::VWADD_WV:
856 case RISCV::VWADD_WX:
857 case RISCV::VWSUB_WV:
858 case RISCV::VWSUB_WX:
859 // Vector Integer Extension
860 case RISCV::VZEXT_VF2:
861 case RISCV::VSEXT_VF2:
862 case RISCV::VZEXT_VF4:
863 case RISCV::VSEXT_VF4:
864 case RISCV::VZEXT_VF8:
865 case RISCV::VSEXT_VF8:
866 // Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions
867 // FIXME: Add support
868 case RISCV::VMADC_VV:
869 case RISCV::VMADC_VI:
870 case RISCV::VMADC_VX:
871 case RISCV::VMSBC_VV:
872 case RISCV::VMSBC_VX:
873 // Vector Narrowing Integer Right Shift Instructions
874 case RISCV::VNSRL_WX:
875 case RISCV::VNSRL_WI:
876 case RISCV::VNSRL_WV:
877 case RISCV::VNSRA_WI:
878 case RISCV::VNSRA_WV:
879 case RISCV::VNSRA_WX:
880 // Vector Integer Compare Instructions
881 case RISCV::VMSEQ_VI:
882 case RISCV::VMSEQ_VV:
883 case RISCV::VMSEQ_VX:
884 case RISCV::VMSNE_VI:
885 case RISCV::VMSNE_VV:
886 case RISCV::VMSNE_VX:
887 case RISCV::VMSLTU_VV:
888 case RISCV::VMSLTU_VX:
889 case RISCV::VMSLT_VV:
890 case RISCV::VMSLT_VX:
891 case RISCV::VMSLEU_VV:
892 case RISCV::VMSLEU_VI:
893 case RISCV::VMSLEU_VX:
894 case RISCV::VMSLE_VV:
895 case RISCV::VMSLE_VI:
896 case RISCV::VMSLE_VX:
897 case RISCV::VMSGTU_VI:
898 case RISCV::VMSGTU_VX:
899 case RISCV::VMSGT_VI:
900 case RISCV::VMSGT_VX:
901 // Vector Integer Min/Max Instructions
902 case RISCV::VMINU_VV:
903 case RISCV::VMINU_VX:
904 case RISCV::VMIN_VV:
905 case RISCV::VMIN_VX:
906 case RISCV::VMAXU_VV:
907 case RISCV::VMAXU_VX:
908 case RISCV::VMAX_VV:
909 case RISCV::VMAX_VX:
910 // Vector Single-Width Integer Multiply Instructions
911 case RISCV::VMUL_VV:
912 case RISCV::VMUL_VX:
913 case RISCV::VMULH_VV:
914 case RISCV::VMULH_VX:
915 case RISCV::VMULHU_VV:
916 case RISCV::VMULHU_VX:
917 case RISCV::VMULHSU_VV:
918 case RISCV::VMULHSU_VX:
919 // Vector Integer Divide Instructions
920 case RISCV::VDIVU_VV:
921 case RISCV::VDIVU_VX:
922 case RISCV::VDIV_VV:
923 case RISCV::VDIV_VX:
924 case RISCV::VREMU_VV:
925 case RISCV::VREMU_VX:
926 case RISCV::VREM_VV:
927 case RISCV::VREM_VX:
928 // Vector Widening Integer Multiply Instructions
929 case RISCV::VWMUL_VV:
930 case RISCV::VWMUL_VX:
931 case RISCV::VWMULSU_VV:
932 case RISCV::VWMULSU_VX:
933 case RISCV::VWMULU_VV:
934 case RISCV::VWMULU_VX:
935 // Vector Single-Width Integer Multiply-Add Instructions
936 case RISCV::VMACC_VV:
937 case RISCV::VMACC_VX:
938 case RISCV::VNMSAC_VV:
939 case RISCV::VNMSAC_VX:
940 case RISCV::VMADD_VV:
941 case RISCV::VMADD_VX:
942 case RISCV::VNMSUB_VV:
943 case RISCV::VNMSUB_VX:
944 // Vector Integer Merge Instructions
945 case RISCV::VMERGE_VIM:
946 case RISCV::VMERGE_VVM:
947 case RISCV::VMERGE_VXM:
948 // Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions
949 case RISCV::VADC_VIM:
950 case RISCV::VADC_VVM:
951 case RISCV::VADC_VXM:
952 // Vector Widening Integer Multiply-Add Instructions
953 case RISCV::VWMACCU_VV:
954 case RISCV::VWMACCU_VX:
955 case RISCV::VWMACC_VV:
956 case RISCV::VWMACC_VX:
957 case RISCV::VWMACCSU_VV:
958 case RISCV::VWMACCSU_VX:
959 case RISCV::VWMACCUS_VX:
960 // Vector Integer Merge Instructions
961 // FIXME: Add support
962 // Vector Integer Move Instructions
963 // FIXME: Add support
964 case RISCV::VMV_V_I:
965 case RISCV::VMV_V_X:
966 case RISCV::VMV_V_V:
967 // Vector Single-Width Averaging Add and Subtract
968 case RISCV::VAADDU_VV:
969 case RISCV::VAADDU_VX:
970 case RISCV::VAADD_VV:
971 case RISCV::VAADD_VX:
972 case RISCV::VASUBU_VV:
973 case RISCV::VASUBU_VX:
974 case RISCV::VASUB_VV:
975 case RISCV::VASUB_VX:
976
977 // Vector Crypto
978 case RISCV::VWSLL_VI:
979
980 // Vector Mask Instructions
981 // Vector Mask-Register Logical Instructions
982 // vmsbf.m set-before-first mask bit
983 // vmsif.m set-including-first mask bit
984 // vmsof.m set-only-first mask bit
985 // Vector Iota Instruction
986 // Vector Element Index Instruction
987 case RISCV::VMAND_MM:
988 case RISCV::VMNAND_MM:
989 case RISCV::VMANDN_MM:
990 case RISCV::VMXOR_MM:
991 case RISCV::VMOR_MM:
992 case RISCV::VMNOR_MM:
993 case RISCV::VMORN_MM:
994 case RISCV::VMXNOR_MM:
995 case RISCV::VMSBF_M:
996 case RISCV::VMSIF_M:
997 case RISCV::VMSOF_M:
998 case RISCV::VIOTA_M:
999 case RISCV::VID_V:
1000 // Vector Single-Width Floating-Point Add/Subtract Instructions
1001 case RISCV::VFADD_VF:
1002 case RISCV::VFADD_VV:
1003 case RISCV::VFSUB_VF:
1004 case RISCV::VFSUB_VV:
1005 case RISCV::VFRSUB_VF:
1006 // Vector Widening Floating-Point Add/Subtract Instructions
1007 case RISCV::VFWADD_VV:
1008 case RISCV::VFWADD_VF:
1009 case RISCV::VFWSUB_VV:
1010 case RISCV::VFWSUB_VF:
1011 case RISCV::VFWADD_WF:
1012 case RISCV::VFWADD_WV:
1013 case RISCV::VFWSUB_WF:
1014 case RISCV::VFWSUB_WV:
1015 // Vector Single-Width Floating-Point Multiply/Divide Instructions
1016 case RISCV::VFMUL_VF:
1017 case RISCV::VFMUL_VV:
1018 case RISCV::VFDIV_VF:
1019 case RISCV::VFDIV_VV:
1020 case RISCV::VFRDIV_VF:
1021 // Vector Widening Floating-Point Multiply
1022 case RISCV::VFWMUL_VF:
1023 case RISCV::VFWMUL_VV:
1024 // Vector Floating-Point Compare Instructions
1025 case RISCV::VMFEQ_VF:
1026 case RISCV::VMFEQ_VV:
1027 case RISCV::VMFNE_VF:
1028 case RISCV::VMFNE_VV:
1029 case RISCV::VMFLT_VF:
1030 case RISCV::VMFLT_VV:
1031 case RISCV::VMFLE_VF:
1032 case RISCV::VMFLE_VV:
1033 case RISCV::VMFGT_VF:
1034 case RISCV::VMFGE_VF:
1035 // Single-Width Floating-Point/Integer Type-Convert Instructions
1036 case RISCV::VFCVT_XU_F_V:
1037 case RISCV::VFCVT_X_F_V:
1038 case RISCV::VFCVT_RTZ_XU_F_V:
1039 case RISCV::VFCVT_RTZ_X_F_V:
1040 case RISCV::VFCVT_F_XU_V:
1041 case RISCV::VFCVT_F_X_V:
1042 // Widening Floating-Point/Integer Type-Convert Instructions
1043 case RISCV::VFWCVT_XU_F_V:
1044 case RISCV::VFWCVT_X_F_V:
1045 case RISCV::VFWCVT_RTZ_XU_F_V:
1046 case RISCV::VFWCVT_RTZ_X_F_V:
1047 case RISCV::VFWCVT_F_XU_V:
1048 case RISCV::VFWCVT_F_X_V:
1049 case RISCV::VFWCVT_F_F_V:
1050 case RISCV::VFWCVTBF16_F_F_V:
1051 // Narrowing Floating-Point/Integer Type-Convert Instructions
1052 case RISCV::VFNCVT_XU_F_W:
1053 case RISCV::VFNCVT_X_F_W:
1054 case RISCV::VFNCVT_RTZ_XU_F_W:
1055 case RISCV::VFNCVT_RTZ_X_F_W:
1056 case RISCV::VFNCVT_F_XU_W:
1057 case RISCV::VFNCVT_F_X_W:
1058 case RISCV::VFNCVT_F_F_W:
1059 case RISCV::VFNCVT_ROD_F_F_W:
1060 case RISCV::VFNCVTBF16_F_F_W:
1061 return true;
1062 }
1063
1064 return false;
1065}
1066
1067/// Return true if MO is a vector operand but is used as a scalar operand.
1069 MachineInstr *MI = MO.getParent();
1071 RISCVVPseudosTable::getPseudoInfo(MI->getOpcode());
1072
1073 if (!RVV)
1074 return false;
1075
1076 switch (RVV->BaseInstr) {
1077 // Reductions only use vs1[0] of vs1
1078 case RISCV::VREDAND_VS:
1079 case RISCV::VREDMAX_VS:
1080 case RISCV::VREDMAXU_VS:
1081 case RISCV::VREDMIN_VS:
1082 case RISCV::VREDMINU_VS:
1083 case RISCV::VREDOR_VS:
1084 case RISCV::VREDSUM_VS:
1085 case RISCV::VREDXOR_VS:
1086 case RISCV::VWREDSUM_VS:
1087 case RISCV::VWREDSUMU_VS:
1088 case RISCV::VFREDMAX_VS:
1089 case RISCV::VFREDMIN_VS:
1090 case RISCV::VFREDOSUM_VS:
1091 case RISCV::VFREDUSUM_VS:
1092 case RISCV::VFWREDOSUM_VS:
1093 case RISCV::VFWREDUSUM_VS:
1094 return MO.getOperandNo() == 3;
1095 case RISCV::VMV_X_S:
1096 case RISCV::VFMV_F_S:
1097 return MO.getOperandNo() == 1;
1098 default:
1099 return false;
1100 }
1101}
1102
1103/// Return true if MI may read elements past VL.
1104static bool mayReadPastVL(const MachineInstr &MI) {
1106 RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
1107 if (!RVV)
1108 return true;
1109
1110 switch (RVV->BaseInstr) {
1111 // vslidedown instructions may read elements past VL. They are handled
1112 // according to current tail policy.
1113 case RISCV::VSLIDEDOWN_VI:
1114 case RISCV::VSLIDEDOWN_VX:
1115 case RISCV::VSLIDE1DOWN_VX:
1116 case RISCV::VFSLIDE1DOWN_VF:
1117
1118 // vrgather instructions may read the source vector at any index < VLMAX,
1119 // regardless of VL.
1120 case RISCV::VRGATHER_VI:
1121 case RISCV::VRGATHER_VV:
1122 case RISCV::VRGATHER_VX:
1123 case RISCV::VRGATHEREI16_VV:
1124 return true;
1125
1126 default:
1127 return false;
1128 }
1129}
1130
1131bool RISCVVLOptimizer::isCandidate(const MachineInstr &MI) const {
1132 const MCInstrDesc &Desc = MI.getDesc();
1133 if (!RISCVII::hasVLOp(Desc.TSFlags) || !RISCVII::hasSEWOp(Desc.TSFlags))
1134 return false;
1135 if (MI.getNumDefs() != 1)
1136 return false;
1137
1138 // If we're not using VLMAX, then we need to be careful whether we are using
1139 // TA/TU when there is a non-undef Passthru. But when we are using VLMAX, it
1140 // does not matter whether we are using TA/TU with a non-undef Passthru, since
1141 // there are no tail elements to be preserved.
1142 unsigned VLOpNum = RISCVII::getVLOpNum(Desc);
1143 const MachineOperand &VLOp = MI.getOperand(VLOpNum);
1144 if (VLOp.isReg() || VLOp.getImm() != RISCV::VLMaxSentinel) {
1145 // If MI has a non-undef passthru, we will not try to optimize it since
1146 // that requires us to preserve tail elements according to TA/TU.
1147 // Otherwise, The MI has an undef Passthru, so it doesn't matter whether we
1148 // are using TA/TU.
1149 bool HasPassthru = RISCVII::isFirstDefTiedToFirstUse(Desc);
1150 unsigned PassthruOpIdx = MI.getNumExplicitDefs();
1151 if (HasPassthru &&
1152 MI.getOperand(PassthruOpIdx).getReg() != RISCV::NoRegister) {
1153 LLVM_DEBUG(
1154 dbgs() << " Not a candidate because it uses non-undef passthru"
1155 " with non-VLMAX VL\n");
1156 return false;
1157 }
1158 }
1159
1160 // If the VL is 1, then there is no need to reduce it. This is an
1161 // optimization, not needed to preserve correctness.
1162 if (VLOp.isImm() && VLOp.getImm() == 1) {
1163 LLVM_DEBUG(dbgs() << " Not a candidate because VL is already 1\n");
1164 return false;
1165 }
1166
1167 if (MI.mayRaiseFPException()) {
1168 LLVM_DEBUG(dbgs() << "Not a candidate because may raise FP exception\n");
1169 return false;
1170 }
1171
1172 // Some instructions that produce vectors have semantics that make it more
1173 // difficult to determine whether the VL can be reduced. For example, some
1174 // instructions, such as reductions, may write lanes past VL to a scalar
1175 // register. Other instructions, such as some loads or stores, may write
1176 // lower lanes using data from higher lanes. There may be other complex
1177 // semantics not mentioned here that make it hard to determine whether
1178 // the VL can be optimized. As a result, a white-list of supported
1179 // instructions is used. Over time, more instructions can be supported
1180 // upon careful examination of their semantics under the logic in this
1181 // optimization.
1182 // TODO: Use a better approach than a white-list, such as adding
1183 // properties to instructions using something like TSFlags.
1184 if (!isSupportedInstr(MI)) {
1185 LLVM_DEBUG(dbgs() << "Not a candidate due to unsupported instruction\n");
1186 return false;
1187 }
1188
1189 LLVM_DEBUG(dbgs() << "Found a candidate for VL reduction: " << MI << "\n");
1190 return true;
1191}
1192
1193std::optional<MachineOperand>
1194RISCVVLOptimizer::getMinimumVLForUser(MachineOperand &UserOp) {
1195 const MachineInstr &UserMI = *UserOp.getParent();
1196 const MCInstrDesc &Desc = UserMI.getDesc();
1197
1198 if (!RISCVII::hasVLOp(Desc.TSFlags) || !RISCVII::hasSEWOp(Desc.TSFlags)) {
1199 LLVM_DEBUG(dbgs() << " Abort due to lack of VL, assume that"
1200 " use VLMAX\n");
1201 return std::nullopt;
1202 }
1203
1204 // Instructions like reductions may use a vector register as a scalar
1205 // register. In this case, we should treat it as only reading the first lane.
1206 if (isVectorOpUsedAsScalarOp(UserOp)) {
1207 [[maybe_unused]] Register R = UserOp.getReg();
1208 [[maybe_unused]] const TargetRegisterClass *RC = MRI->getRegClass(R);
1209 assert(RISCV::VRRegClass.hasSubClassEq(RC) &&
1210 "Expect LMUL 1 register class for vector as scalar operands!");
1211 LLVM_DEBUG(dbgs() << " Used this operand as a scalar operand\n");
1212
1213 return MachineOperand::CreateImm(1);
1214 }
1215
1216 unsigned VLOpNum = RISCVII::getVLOpNum(Desc);
1217 const MachineOperand &VLOp = UserMI.getOperand(VLOpNum);
1218 // Looking for an immediate or a register VL that isn't X0.
1219 assert((!VLOp.isReg() || VLOp.getReg() != RISCV::X0) &&
1220 "Did not expect X0 VL");
1221 return VLOp;
1222}
1223
1224std::optional<MachineOperand> RISCVVLOptimizer::checkUsers(MachineInstr &MI) {
1225 // FIXME: Avoid visiting each user for each time we visit something on the
1226 // worklist, combined with an extra visit from the outer loop. Restructure
1227 // along lines of an instcombine style worklist which integrates the outer
1228 // pass.
1229 std::optional<MachineOperand> CommonVL;
1230 for (auto &UserOp : MRI->use_operands(MI.getOperand(0).getReg())) {
1231 const MachineInstr &UserMI = *UserOp.getParent();
1232 LLVM_DEBUG(dbgs() << " Checking user: " << UserMI << "\n");
1233 if (mayReadPastVL(UserMI)) {
1234 LLVM_DEBUG(dbgs() << " Abort because used by unsafe instruction\n");
1235 return std::nullopt;
1236 }
1237
1238 // Tied operands might pass through.
1239 if (UserOp.isTied()) {
1240 LLVM_DEBUG(dbgs() << " Abort because user used as tied operand\n");
1241 return std::nullopt;
1242 }
1243
1244 auto VLOp = getMinimumVLForUser(UserOp);
1245 if (!VLOp)
1246 return std::nullopt;
1247
1248 // Use the largest VL among all the users. If we cannot determine this
1249 // statically, then we cannot optimize the VL.
1250 if (!CommonVL || RISCV::isVLKnownLE(*CommonVL, *VLOp)) {
1251 CommonVL = *VLOp;
1252 LLVM_DEBUG(dbgs() << " User VL is: " << VLOp << "\n");
1253 } else if (!RISCV::isVLKnownLE(*VLOp, *CommonVL)) {
1254 LLVM_DEBUG(dbgs() << " Abort because cannot determine a common VL\n");
1255 return std::nullopt;
1256 }
1257
1258 if (!RISCVII::hasSEWOp(UserMI.getDesc().TSFlags)) {
1259 LLVM_DEBUG(dbgs() << " Abort due to lack of SEW operand\n");
1260 return std::nullopt;
1261 }
1262
1263 std::optional<OperandInfo> ConsumerInfo = getOperandInfo(UserOp, MRI);
1264 std::optional<OperandInfo> ProducerInfo =
1265 getOperandInfo(MI.getOperand(0), MRI);
1266 if (!ConsumerInfo || !ProducerInfo) {
1267 LLVM_DEBUG(dbgs() << " Abort due to unknown operand information.\n");
1268 LLVM_DEBUG(dbgs() << " ConsumerInfo is: " << ConsumerInfo << "\n");
1269 LLVM_DEBUG(dbgs() << " ProducerInfo is: " << ProducerInfo << "\n");
1270 return std::nullopt;
1271 }
1272
1273 // If the operand is used as a scalar operand, then the EEW must be
1274 // compatible. Otherwise, the EMUL *and* EEW must be compatible.
1275 bool IsVectorOpUsedAsScalarOp = isVectorOpUsedAsScalarOp(UserOp);
1276 if ((IsVectorOpUsedAsScalarOp &&
1277 !OperandInfo::EEWAreEqual(*ConsumerInfo, *ProducerInfo)) ||
1278 (!IsVectorOpUsedAsScalarOp &&
1279 !OperandInfo::EMULAndEEWAreEqual(*ConsumerInfo, *ProducerInfo))) {
1280 LLVM_DEBUG(
1281 dbgs()
1282 << " Abort due to incompatible information for EMUL or EEW.\n");
1283 LLVM_DEBUG(dbgs() << " ConsumerInfo is: " << ConsumerInfo << "\n");
1284 LLVM_DEBUG(dbgs() << " ProducerInfo is: " << ProducerInfo << "\n");
1285 return std::nullopt;
1286 }
1287 }
1288
1289 return CommonVL;
1290}
1291
1292bool RISCVVLOptimizer::tryReduceVL(MachineInstr &OrigMI) {
1294 Worklist.insert(&OrigMI);
1295
1296 bool MadeChange = false;
1297 while (!Worklist.empty()) {
1298 MachineInstr &MI = *Worklist.pop_back_val();
1299 LLVM_DEBUG(dbgs() << "Trying to reduce VL for " << MI << "\n");
1300
1301 if (!isVectorRegClass(MI.getOperand(0).getReg(), MRI))
1302 continue;
1303
1304 auto CommonVL = checkUsers(MI);
1305 if (!CommonVL)
1306 continue;
1307
1308 assert((CommonVL->isImm() || CommonVL->getReg().isVirtual()) &&
1309 "Expected VL to be an Imm or virtual Reg");
1310
1311 unsigned VLOpNum = RISCVII::getVLOpNum(MI.getDesc());
1312 MachineOperand &VLOp = MI.getOperand(VLOpNum);
1313
1314 if (!RISCV::isVLKnownLE(*CommonVL, VLOp)) {
1315 LLVM_DEBUG(dbgs() << " Abort due to CommonVL not <= VLOp.\n");
1316 continue;
1317 }
1318
1319 if (CommonVL->isImm()) {
1320 LLVM_DEBUG(dbgs() << " Reduce VL from " << VLOp << " to "
1321 << CommonVL->getImm() << " for " << MI << "\n");
1322 VLOp.ChangeToImmediate(CommonVL->getImm());
1323 } else {
1324 const MachineInstr *VLMI = MRI->getVRegDef(CommonVL->getReg());
1325 if (!MDT->dominates(VLMI, &MI))
1326 continue;
1327 LLVM_DEBUG(
1328 dbgs() << " Reduce VL from " << VLOp << " to "
1329 << printReg(CommonVL->getReg(), MRI->getTargetRegisterInfo())
1330 << " for " << MI << "\n");
1331
1332 // All our checks passed. We can reduce VL.
1333 VLOp.ChangeToRegister(CommonVL->getReg(), false);
1334 }
1335
1336 MadeChange = true;
1337
1338 // Now add all inputs to this instruction to the worklist.
1339 for (auto &Op : MI.operands()) {
1340 if (!Op.isReg() || !Op.isUse() || !Op.getReg().isVirtual())
1341 continue;
1342
1343 if (!isVectorRegClass(Op.getReg(), MRI))
1344 continue;
1345
1346 MachineInstr *DefMI = MRI->getVRegDef(Op.getReg());
1347
1348 if (!isCandidate(*DefMI))
1349 continue;
1350
1351 Worklist.insert(DefMI);
1352 }
1353 }
1354
1355 return MadeChange;
1356}
1357
1358bool RISCVVLOptimizer::runOnMachineFunction(MachineFunction &MF) {
1359 if (skipFunction(MF.getFunction()))
1360 return false;
1361
1362 MRI = &MF.getRegInfo();
1363 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
1364
1366 if (!ST.hasVInstructions())
1367 return false;
1368
1369 bool MadeChange = false;
1370 for (MachineBasicBlock &MBB : MF) {
1371 // Visit instructions in reverse order.
1372 for (auto &MI : make_range(MBB.rbegin(), MBB.rend())) {
1373 if (!isCandidate(MI))
1374 continue;
1375
1376 MadeChange |= tryReduceVL(MI);
1377 }
1378 }
1379
1380 return MadeChange;
1381}
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
MachineBasicBlock & MBB
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:282
#define LLVM_DEBUG(...)
Definition: Debug.h:106
IRTranslator LLVM IR MI
static bool isCandidate(const MachineInstr *MI, Register &DefedReg, Register FrameReg)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
static bool mayReadPastVL(const MachineInstr &MI)
Return true if MI may read elements past VL.
static LLVM_ATTRIBUTE_UNUSED raw_ostream & operator<<(raw_ostream &OS, const OperandInfo &OI)
static unsigned getIntegerExtensionOperandEEW(unsigned Factor, const MachineInstr &MI, const MachineOperand &MO)
Dest has EEW=SEW.
static bool isVectorOpUsedAsScalarOp(MachineOperand &MO)
Return true if MO is a vector operand but is used as a scalar operand.
static std::optional< unsigned > getOperandLog2EEW(const MachineOperand &MO, const MachineRegisterInfo *MRI)
static bool isVectorRegClass(Register R, const MachineRegisterInfo *MRI)
Return true if R is a physical or virtual vector register, false otherwise.
static bool isSupportedInstr(const MachineInstr &MI)
Return true if this optimization should consider MI for VL reduction.
#define PASS_NAME
#define DEBUG_TYPE
static bool isMaskOperand(const MachineInstr &MI, const MachineOperand &MO, const MachineRegisterInfo *MRI)
Check whether MO is a mask operand of MI.
static std::optional< OperandInfo > getOperandInfo(const MachineOperand &MO, const MachineRegisterInfo *MRI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file implements a set that has insertion order iteration characteristics.
#define PASS_NAME
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
This class represents an Operation in the Expression.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
reverse_iterator rend()
reverse_iterator rbegin()
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:69
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:572
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:585
A description of a memory reference used in the backend.
MachineOperand class - Representation of each machine instruction operand.
unsigned getOperandNo() const
Returns the index of this operand in the instruction that it belongs to.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
void ChangeToRegister(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static MachineOperand CreateImm(int64_t Val)
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
A vector that has set insertion semantics.
Definition: SetVector.h:57
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:93
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
value_type pop_back_val()
Definition: SetVector.h:285
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
const uint8_t TSFlags
Configurable target specific flags.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
static unsigned getVLOpNum(const MCInstrDesc &Desc)
static VLMUL getLMul(uint64_t TSFlags)
static bool hasVLOp(uint64_t TSFlags)
static unsigned getSEWOpNum(const MCInstrDesc &Desc)
static bool hasSEWOp(uint64_t TSFlags)
static bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc)
static bool isVRegClass(uint64_t TSFlags)
static std::pair< unsigned, bool > getEMULEqualsEEWDivSEWTimesLMUL(unsigned Log2EEW, const MachineInstr &MI)
Return EMUL = (EEW / SEW) * LMUL where EEW comes from Log2EEW and LMUL and SEW are from the TSFlags o...
std::pair< unsigned, bool > decodeVLMUL(RISCVII::VLMUL VLMUL)
bool isVLKnownLE(const MachineOperand &LHS, const MachineOperand &RHS)
Given two VL operands, do we know that LHS <= RHS?
static constexpr int64_t VLMaxSentinel
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
FunctionPass * createRISCVVLOptimizerPass()
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:340
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
Represents the EMUL and EEW of a MachineOperand.
static bool EEWAreEqual(const OperandInfo &A, const OperandInfo &B)
OperandInfo(std::pair< unsigned, bool > EMUL, unsigned Log2EEW)
OperandInfo(unsigned Log2EEW)
void print(raw_ostream &OS) const
static bool EMULAndEEWAreEqual(const OperandInfo &A, const OperandInfo &B)
OperandInfo()=delete
OperandInfo(RISCVII::VLMUL EMUL, unsigned Log2EEW)
std::optional< std::pair< unsigned, bool > > EMUL
Description of the encoding of one expression Op.