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