LLVM 24.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.
14//
15// This is split into a sparse dataflow analysis where we determine what VL is
16// demanded by each instruction first, and then afterwards try to reduce the VL
17// of each instruction if it demands less than its VL operand.
18//
19// The analysis is explained in more detail in the 2025 EuroLLVM Developers'
20// Meeting talk "Accidental Dataflow Analysis: Extending the RISC-V VL
21// Optimizer", which is available on YouTube at
22// https://www.youtube.com/watch?v=Mfb5fRSdJAc
23//
24// The slides for the talk are available at
25// https://llvm.org/devmtg/2025-04/slides/technical_talk/lau_accidental_dataflow.pdf
26//
27//===---------------------------------------------------------------------===//
28
29#include "RISCV.h"
30#include "RISCVSubtarget.h"
32#include "llvm/ADT/SetVector.h"
37
38using namespace llvm;
39
40#define DEBUG_TYPE "riscv-vl-optimizer"
41#define PASS_NAME "RISC-V VL Optimizer"
42
43namespace {
44
45/// Wrapper around MachineOperand that defaults to immediate 0.
46struct DemandedVL {
48 DemandedVL() : VL(MachineOperand::CreateImm(0)) {}
49 DemandedVL(MachineOperand VL) : VL(VL) {}
50 static DemandedVL vlmax() {
52 }
53 bool operator!=(const DemandedVL &Other) const {
54 return !VL.isIdenticalTo(Other.VL);
55 }
56
57 DemandedVL max(const MachineRegisterInfo &MRI, const DemandedVL &X) const {
58 if (RISCV::isVLKnownLE(MRI, VL, X.VL))
59 return X;
60 if (RISCV::isVLKnownLE(MRI, X.VL, VL))
61 return *this;
62 return DemandedVL::vlmax();
63 }
64};
65
66class RISCVVLOptimizerImpl {
68 const MachineDominatorTree *MDT;
69 const TargetInstrInfo *TII;
70
71public:
72 RISCVVLOptimizerImpl(const MachineDominatorTree *MDT) : MDT(MDT) {}
73
74 bool run(MachineFunction &MF);
75
76private:
77 DemandedVL getMinimumVLForUser(const MachineInstr &UserMI,
78 unsigned OpIdx) const;
79 /// Returns true if the users of \p MI have compatible EEWs and SEWs.
80 bool checkUsers(const MachineInstr &MI) const;
81 bool tryReduceVL(MachineInstr &MI, MachineOperand VL) const;
82 bool isSupportedInstr(const MachineInstr &MI) const;
83 bool isCandidate(const MachineInstr &MI) const;
84 void transfer(const MachineInstr &MI);
85
86 /// For a given instruction, records what elements of it are demanded by
87 /// downstream users.
90
91 /// \returns all vector virtual registers that \p MI uses.
92 auto virtual_vec_uses(const MachineInstr &MI) const {
93 return make_filter_range(MI.uses(), [this](const MachineOperand &MO) {
94 return MO.isReg() && MO.getReg().isVirtual() &&
95 RISCVRegisterInfo::isRVVRegClass(MRI->getRegClass(MO.getReg()));
96 });
97 }
98
99 /// \returns all vector virtual registers that \p MI defines.
100 auto virtual_vec_defs(const MachineInstr &MI) const {
101 return make_filter_range(MI.all_defs(), [this](const MachineOperand &MO) {
102 return MO.getReg().isVirtual() &&
103 RISCVRegisterInfo::isRVVRegClass(MRI->getRegClass(MO.getReg()));
104 });
105 }
106};
107
108class RISCVVLOptimizerLegacy : public MachineFunctionPass {
109public:
110 static char ID;
111
112 RISCVVLOptimizerLegacy() : MachineFunctionPass(ID) {}
113
114 bool runOnMachineFunction(MachineFunction &MF) override;
115
116 void getAnalysisUsage(AnalysisUsage &AU) const override {
117 AU.setPreservesCFG();
121 }
122
123 StringRef getPassName() const override { return PASS_NAME; }
124};
125
126/// Represents the EMUL and EEW of a MachineOperand.
127struct OperandInfo {
128 // Represent as 1,2,4,8, ... and fractional indicator. This is because
129 // EMUL can take on values that don't map to RISCVVType::VLMUL values exactly.
130 // For example, a mask operand can have an EMUL less than MF8.
131 // If nullopt, then EMUL isn't used (i.e. only a single scalar is read).
132 std::optional<std::pair<unsigned, bool>> EMUL;
133
134 unsigned Log2EEW;
135
136 OperandInfo(RISCVVType::VLMUL EMUL, unsigned Log2EEW)
137 : EMUL(RISCVVType::decodeVLMUL(EMUL)), Log2EEW(Log2EEW) {}
138
139 OperandInfo(std::pair<unsigned, bool> EMUL, unsigned Log2EEW)
140 : EMUL(EMUL), Log2EEW(Log2EEW) {}
141
142 OperandInfo(unsigned Log2EEW) : Log2EEW(Log2EEW) {}
143
144 OperandInfo() = delete;
145
146 /// Return true if the EMUL and EEW produced by \p Def is compatible with the
147 /// EMUL and EEW used by \p User.
148 static bool areCompatible(const OperandInfo &Def, const OperandInfo &User) {
149 if (Def.Log2EEW != User.Log2EEW)
150 return false;
151 if (User.EMUL && Def.EMUL != User.EMUL)
152 return false;
153 return true;
154 }
155
156 void print(raw_ostream &OS) const {
157 if (EMUL) {
158 OS << "EMUL: m";
159 if (EMUL->second)
160 OS << "f";
161 OS << EMUL->first;
162 } else
163 OS << "EMUL: none\n";
164 OS << ", EEW: " << (1 << Log2EEW);
165 }
166};
167
168} // end anonymous namespace
169
170char RISCVVLOptimizerLegacy::ID = 0;
171INITIALIZE_PASS_BEGIN(RISCVVLOptimizerLegacy, DEBUG_TYPE, PASS_NAME, false,
172 false)
174INITIALIZE_PASS_END(RISCVVLOptimizerLegacy, DEBUG_TYPE, PASS_NAME, false, false)
175
177 return new RISCVVLOptimizerLegacy();
178}
179
180[[maybe_unused]]
181static raw_ostream &operator<<(raw_ostream &OS, const OperandInfo &OI) {
182 OI.print(OS);
183 return OS;
184}
185
186[[maybe_unused]]
188 const std::optional<OperandInfo> &OI) {
189 if (OI)
190 OI->print(OS);
191 else
192 OS << "nullopt";
193 return OS;
194}
195
196/// Return EMUL = (EEW / SEW) * LMUL where EEW comes from Log2EEW and LMUL and
197/// SEW are from the TSFlags of MI.
198static std::pair<unsigned, bool>
200 RISCVVType::VLMUL MIVLMUL = RISCVII::getLMul(MI.getDesc().TSFlags);
201 auto [MILMUL, MILMULIsFractional] = RISCVVType::decodeVLMUL(MIVLMUL);
202 unsigned MILog2SEW =
203 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
204
205 // Mask instructions will have 0 as the SEW operand. But the LMUL of these
206 // instructions is calculated is as if the SEW operand was 3 (e8).
207 if (MILog2SEW == 0)
208 MILog2SEW = 3;
209
210 unsigned MISEW = 1 << MILog2SEW;
211
212 unsigned EEW = 1 << Log2EEW;
213 // Calculate (EEW/SEW)*LMUL preserving fractions less than 1. Use GCD
214 // to put fraction in simplest form.
215 unsigned Num = EEW, Denom = MISEW;
216 int GCD = MILMULIsFractional ? std::gcd(Num, Denom * MILMUL)
217 : std::gcd(Num * MILMUL, Denom);
218 Num = MILMULIsFractional ? Num / GCD : Num * MILMUL / GCD;
219 Denom = MILMULIsFractional ? Denom * MILMUL / GCD : Denom / GCD;
220 return std::make_pair(Num > Denom ? Num : Denom, Denom > Num);
221}
222
223static DemandedVL doubleVL(DemandedVL MinimumVL) {
224 if (!MinimumVL.VL.isImm())
225 return DemandedVL::vlmax();
226
227 int64_t VL = MinimumVL.VL.getImm();
228 if (!isUInt<4>(VL))
229 return DemandedVL::vlmax();
230 return MachineOperand::CreateImm(VL * 2);
231}
232
233static DemandedVL halfVL(DemandedVL MinimumVL, bool Ceil = false) {
234 if (!MinimumVL.VL.isImm())
235 return DemandedVL::vlmax();
236
237 int64_t VL = MinimumVL.VL.getImm();
238 if (!isUInt<5>(VL))
239 return DemandedVL::vlmax();
240 return MachineOperand::CreateImm((VL + Ceil) / 2);
241}
242
243static std::pair<unsigned, bool> doubleEMUL(std::pair<unsigned, bool> EMUL) {
244 auto [Num, IsFractional] = EMUL;
245 if (IsFractional)
246 return std::make_pair(Num / 2, Num > 2);
247 return std::make_pair(Num * 2, false);
248}
249
250static std::pair<unsigned, bool> halfEMUL(std::pair<unsigned, bool> EMUL) {
251 auto [Num, IsFractional] = EMUL;
252 if (IsFractional || Num == 1)
253 return std::make_pair(Num * 2, true);
254 return std::make_pair(Num / 2, false);
255}
256
257/// Dest has EEW=SEW. Source EEW=SEW/Factor (i.e. F2 => EEW/2).
258/// SEW comes from TSFlags of MI.
259static unsigned getIntegerExtensionOperandEEW(unsigned Factor,
260 const MachineInstr &MI,
261 unsigned OpIdx) {
262 unsigned MILog2SEW =
263 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
264
265 if (OpIdx == 0)
266 return MILog2SEW;
267
268 unsigned MISEW = 1 << MILog2SEW;
269 unsigned EEW = MISEW / Factor;
270 unsigned Log2EEW = Log2_32(EEW);
271
272 return Log2EEW;
273}
274
275#define VSEG_CASES(Prefix, EEW) \
276 RISCV::Prefix##SEG2E##EEW##_V: \
277 case RISCV::Prefix##SEG3E##EEW##_V: \
278 case RISCV::Prefix##SEG4E##EEW##_V: \
279 case RISCV::Prefix##SEG5E##EEW##_V: \
280 case RISCV::Prefix##SEG6E##EEW##_V: \
281 case RISCV::Prefix##SEG7E##EEW##_V: \
282 case RISCV::Prefix##SEG8E##EEW##_V
283#define VSSEG_CASES(EEW) VSEG_CASES(VS, EEW)
284#define VSSSEG_CASES(EEW) VSEG_CASES(VSS, EEW)
285#define VSUXSEG_CASES(EEW) VSEG_CASES(VSUX, I##EEW)
286#define VSOXSEG_CASES(EEW) VSEG_CASES(VSOX, I##EEW)
287
288static std::optional<unsigned> getOperandLog2EEW(const MachineInstr &MI,
289 unsigned OpIdx) {
290 const MCInstrDesc &Desc = MI.getDesc();
292 RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
293 assert(RVV && "Could not find MI in PseudoTable");
294
295 // MI has a SEW associated with it. The RVV specification defines
296 // the EEW of each operand and definition in relation to MI.SEW.
297 unsigned MILog2SEW = MI.getOperand(RISCVII::getSEWOpNum(Desc)).getImm();
298
299 const bool HasPassthru = RISCVII::isFirstDefTiedToFirstUse(Desc);
300 const bool IsTied = RISCVII::isTiedPseudo(Desc.TSFlags);
301
302 bool IsMODef =
303 OpIdx == 0 || (HasPassthru && OpIdx == MI.getNumExplicitDefs());
304
305 // All mask operands have EEW=1
306 const MCOperandInfo &Info = Desc.operands()[OpIdx];
307 if (Info.OperandType == MCOI::OPERAND_REGISTER &&
308 Info.RegClass == RISCV::VMV0RegClassID)
309 return 0;
310
311 // switch against BaseInstr to reduce number of cases that need to be
312 // considered.
313 switch (RVV->BaseInstr) {
314
315 // 6. Configuration-Setting Instructions
316 // Configuration setting instructions do not read or write vector registers
317 case RISCV::VSETIVLI:
318 case RISCV::VSETVL:
319 case RISCV::VSETVLI:
320 llvm_unreachable("Configuration setting instructions do not read or write "
321 "vector registers");
322
323 // Vector Loads and Stores
324 // Vector Unit-Stride Instructions
325 // Vector Strided Instructions
326 /// Dest EEW encoded in the instruction
327 case RISCV::VLM_V:
328 case RISCV::VSM_V:
329 return 0;
330 case RISCV::VLE8_V:
331 case RISCV::VSE8_V:
332 case RISCV::VLSE8_V:
333 case RISCV::VSSE8_V:
334 case VSSEG_CASES(8):
335 case VSSSEG_CASES(8):
336 return 3;
337 case RISCV::VLE16_V:
338 case RISCV::VSE16_V:
339 case RISCV::VLSE16_V:
340 case RISCV::VSSE16_V:
341 case VSSEG_CASES(16):
342 case VSSSEG_CASES(16):
343 return 4;
344 case RISCV::VLE32_V:
345 case RISCV::VSE32_V:
346 case RISCV::VLSE32_V:
347 case RISCV::VSSE32_V:
348 case VSSEG_CASES(32):
349 case VSSSEG_CASES(32):
350 return 5;
351 case RISCV::VLE64_V:
352 case RISCV::VSE64_V:
353 case RISCV::VLSE64_V:
354 case RISCV::VSSE64_V:
355 case VSSEG_CASES(64):
356 case VSSSEG_CASES(64):
357 return 6;
358
359 // Vector Indexed Instructions
360 // vs(o|u)xei<eew>.v
361 // Dest/Data (operand 0) EEW=SEW. Source EEW=<eew>.
362 case RISCV::VLUXEI8_V:
363 case RISCV::VLOXEI8_V:
364 case RISCV::VSUXEI8_V:
365 case RISCV::VSOXEI8_V:
366 case VSUXSEG_CASES(8):
367 case VSOXSEG_CASES(8): {
368 if (OpIdx == 0)
369 return MILog2SEW;
370 return 3;
371 }
372 case RISCV::VLUXEI16_V:
373 case RISCV::VLOXEI16_V:
374 case RISCV::VSUXEI16_V:
375 case RISCV::VSOXEI16_V:
376 case VSUXSEG_CASES(16):
377 case VSOXSEG_CASES(16): {
378 if (OpIdx == 0)
379 return MILog2SEW;
380 return 4;
381 }
382 case RISCV::VLUXEI32_V:
383 case RISCV::VLOXEI32_V:
384 case RISCV::VSUXEI32_V:
385 case RISCV::VSOXEI32_V:
386 case VSUXSEG_CASES(32):
387 case VSOXSEG_CASES(32): {
388 if (OpIdx == 0)
389 return MILog2SEW;
390 return 5;
391 }
392 case RISCV::VLUXEI64_V:
393 case RISCV::VLOXEI64_V:
394 case RISCV::VSUXEI64_V:
395 case RISCV::VSOXEI64_V:
396 case VSUXSEG_CASES(64):
397 case VSOXSEG_CASES(64): {
398 if (OpIdx == 0)
399 return MILog2SEW;
400 return 6;
401 }
402
403 // Vector Integer Arithmetic Instructions
404 // Vector Single-Width Integer Add and Subtract
405 case RISCV::VADD_VI:
406 case RISCV::VADD_VV:
407 case RISCV::VADD_VX:
408 case RISCV::VSUB_VV:
409 case RISCV::VSUB_VX:
410 case RISCV::VRSUB_VI:
411 case RISCV::VRSUB_VX:
412 // Vector Bitwise Logical Instructions
413 // Vector Single-Width Shift Instructions
414 // EEW=SEW.
415 case RISCV::VAND_VI:
416 case RISCV::VAND_VV:
417 case RISCV::VAND_VX:
418 case RISCV::VOR_VI:
419 case RISCV::VOR_VV:
420 case RISCV::VOR_VX:
421 case RISCV::VXOR_VI:
422 case RISCV::VXOR_VV:
423 case RISCV::VXOR_VX:
424 case RISCV::VSLL_VI:
425 case RISCV::VSLL_VV:
426 case RISCV::VSLL_VX:
427 case RISCV::VSRL_VI:
428 case RISCV::VSRL_VV:
429 case RISCV::VSRL_VX:
430 case RISCV::VSRA_VI:
431 case RISCV::VSRA_VV:
432 case RISCV::VSRA_VX:
433 // Vector Integer Min/Max Instructions
434 // EEW=SEW.
435 case RISCV::VMINU_VV:
436 case RISCV::VMINU_VX:
437 case RISCV::VMIN_VV:
438 case RISCV::VMIN_VX:
439 case RISCV::VMAXU_VV:
440 case RISCV::VMAXU_VX:
441 case RISCV::VMAX_VV:
442 case RISCV::VMAX_VX:
443 // Vector Single-Width Integer Multiply Instructions
444 // Source and Dest EEW=SEW.
445 case RISCV::VMUL_VV:
446 case RISCV::VMUL_VX:
447 case RISCV::VMULH_VV:
448 case RISCV::VMULH_VX:
449 case RISCV::VMULHU_VV:
450 case RISCV::VMULHU_VX:
451 case RISCV::VMULHSU_VV:
452 case RISCV::VMULHSU_VX:
453 // Vector Integer Divide Instructions
454 // EEW=SEW.
455 case RISCV::VDIVU_VV:
456 case RISCV::VDIVU_VX:
457 case RISCV::VDIV_VV:
458 case RISCV::VDIV_VX:
459 case RISCV::VREMU_VV:
460 case RISCV::VREMU_VX:
461 case RISCV::VREM_VV:
462 case RISCV::VREM_VX:
463 // Vector Single-Width Integer Multiply-Add Instructions
464 // EEW=SEW.
465 case RISCV::VMACC_VV:
466 case RISCV::VMACC_VX:
467 case RISCV::VNMSAC_VV:
468 case RISCV::VNMSAC_VX:
469 case RISCV::VMADD_VV:
470 case RISCV::VMADD_VX:
471 case RISCV::VNMSUB_VV:
472 case RISCV::VNMSUB_VX:
473 // Vector Integer Merge Instructions
474 // Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions
475 // EEW=SEW, except the mask operand has EEW=1. Mask operand is handled
476 // before this switch.
477 case RISCV::VMERGE_VIM:
478 case RISCV::VMERGE_VVM:
479 case RISCV::VMERGE_VXM:
480 case RISCV::VADC_VIM:
481 case RISCV::VADC_VVM:
482 case RISCV::VADC_VXM:
483 case RISCV::VSBC_VVM:
484 case RISCV::VSBC_VXM:
485 // Vector Integer Move Instructions
486 // Vector Fixed-Point Arithmetic Instructions
487 // Vector Single-Width Saturating Add and Subtract
488 // Vector Single-Width Averaging Add and Subtract
489 // EEW=SEW.
490 case RISCV::VMV_V_I:
491 case RISCV::VMV_V_V:
492 case RISCV::VMV_V_X:
493 case RISCV::VSADDU_VI:
494 case RISCV::VSADDU_VV:
495 case RISCV::VSADDU_VX:
496 case RISCV::VSADD_VI:
497 case RISCV::VSADD_VV:
498 case RISCV::VSADD_VX:
499 case RISCV::VSSUBU_VV:
500 case RISCV::VSSUBU_VX:
501 case RISCV::VSSUB_VV:
502 case RISCV::VSSUB_VX:
503 case RISCV::VAADDU_VV:
504 case RISCV::VAADDU_VX:
505 case RISCV::VAADD_VV:
506 case RISCV::VAADD_VX:
507 case RISCV::VASUBU_VV:
508 case RISCV::VASUBU_VX:
509 case RISCV::VASUB_VV:
510 case RISCV::VASUB_VX:
511 // Vector Single-Width Fractional Multiply with Rounding and Saturation
512 // EEW=SEW. The instruction produces 2*SEW product internally but
513 // saturates to fit into SEW bits.
514 case RISCV::VSMUL_VV:
515 case RISCV::VSMUL_VX:
516 // Vector Single-Width Scaling Shift Instructions
517 // EEW=SEW.
518 case RISCV::VSSRL_VI:
519 case RISCV::VSSRL_VV:
520 case RISCV::VSSRL_VX:
521 case RISCV::VSSRA_VI:
522 case RISCV::VSSRA_VV:
523 case RISCV::VSSRA_VX:
524 // Vector Permutation Instructions
525 // Integer Scalar Move Instructions
526 // Floating-Point Scalar Move Instructions
527 // EEW=SEW.
528 case RISCV::VMV_X_S:
529 case RISCV::VMV_S_X:
530 case RISCV::VFMV_F_S:
531 case RISCV::VFMV_S_F:
532 // Vector Slide Instructions
533 // EEW=SEW.
534 case RISCV::VSLIDEUP_VI:
535 case RISCV::VSLIDEUP_VX:
536 case RISCV::VSLIDEDOWN_VI:
537 case RISCV::VSLIDEDOWN_VX:
538 case RISCV::VSLIDE1UP_VX:
539 case RISCV::VFSLIDE1UP_VF:
540 case RISCV::VSLIDE1DOWN_VX:
541 case RISCV::VFSLIDE1DOWN_VF:
542 // Vector Register Gather Instructions
543 // EEW=SEW. For mask operand, EEW=1.
544 case RISCV::VRGATHER_VI:
545 case RISCV::VRGATHER_VV:
546 case RISCV::VRGATHER_VX:
547 // Vector Element Index Instruction
548 case RISCV::VID_V:
549 // Vector Single-Width Floating-Point Add/Subtract Instructions
550 case RISCV::VFADD_VF:
551 case RISCV::VFADD_VV:
552 case RISCV::VFSUB_VF:
553 case RISCV::VFSUB_VV:
554 case RISCV::VFRSUB_VF:
555 // Vector Single-Width Floating-Point Multiply/Divide Instructions
556 case RISCV::VFMUL_VF:
557 case RISCV::VFMUL_VV:
558 case RISCV::VFDIV_VF:
559 case RISCV::VFDIV_VV:
560 case RISCV::VFRDIV_VF:
561 // Vector Single-Width Floating-Point Fused Multiply-Add Instructions
562 case RISCV::VFMACC_VV:
563 case RISCV::VFMACC_VF:
564 case RISCV::VFNMACC_VV:
565 case RISCV::VFNMACC_VF:
566 case RISCV::VFMSAC_VV:
567 case RISCV::VFMSAC_VF:
568 case RISCV::VFNMSAC_VV:
569 case RISCV::VFNMSAC_VF:
570 case RISCV::VFMADD_VV:
571 case RISCV::VFMADD_VF:
572 case RISCV::VFNMADD_VV:
573 case RISCV::VFNMADD_VF:
574 case RISCV::VFMSUB_VV:
575 case RISCV::VFMSUB_VF:
576 case RISCV::VFNMSUB_VV:
577 case RISCV::VFNMSUB_VF:
578 // Vector Floating-Point Square-Root Instruction
579 case RISCV::VFSQRT_V:
580 // Vector Floating-Point Reciprocal Square-Root Estimate Instruction
581 case RISCV::VFRSQRT7_V:
582 // Vector Floating-Point Reciprocal Estimate Instruction
583 case RISCV::VFREC7_V:
584 // Vector Floating-Point MIN/MAX Instructions
585 case RISCV::VFMIN_VF:
586 case RISCV::VFMIN_VV:
587 case RISCV::VFMAX_VF:
588 case RISCV::VFMAX_VV:
589 // Vector Floating-Point Sign-Injection Instructions
590 case RISCV::VFSGNJ_VF:
591 case RISCV::VFSGNJ_VV:
592 case RISCV::VFSGNJN_VV:
593 case RISCV::VFSGNJN_VF:
594 case RISCV::VFSGNJX_VF:
595 case RISCV::VFSGNJX_VV:
596 // Vector Floating-Point Classify Instruction
597 case RISCV::VFCLASS_V:
598 // Vector Floating-Point Move Instruction
599 case RISCV::VFMV_V_F:
600 // Single-Width Floating-Point/Integer Type-Convert Instructions
601 case RISCV::VFCVT_XU_F_V:
602 case RISCV::VFCVT_X_F_V:
603 case RISCV::VFCVT_RTZ_XU_F_V:
604 case RISCV::VFCVT_RTZ_X_F_V:
605 case RISCV::VFCVT_F_XU_V:
606 case RISCV::VFCVT_F_X_V:
607 // Vector Floating-Point Merge Instruction
608 case RISCV::VFMERGE_VFM:
609 // Vector count population in mask vcpop.m
610 // vfirst find-first-set mask bit
611 case RISCV::VCPOP_M:
612 case RISCV::VFIRST_M:
613 // Vector Bit-manipulation Instructions (Zvbb)
614 // Vector And-Not
615 case RISCV::VANDN_VV:
616 case RISCV::VANDN_VX:
617 // Vector Reverse Bits in Elements
618 case RISCV::VBREV_V:
619 // Vector Reverse Bits in Bytes
620 case RISCV::VBREV8_V:
621 // Vector Reverse Bytes
622 case RISCV::VREV8_V:
623 // Vector Count Leading Zeros
624 case RISCV::VCLZ_V:
625 // Vector Count Trailing Zeros
626 case RISCV::VCTZ_V:
627 // Vector Population Count
628 case RISCV::VCPOP_V:
629 // Vector Rotate Left
630 case RISCV::VROL_VV:
631 case RISCV::VROL_VX:
632 // Vector Rotate Right
633 case RISCV::VROR_VI:
634 case RISCV::VROR_VV:
635 case RISCV::VROR_VX:
636 // Vector Carry-less Multiplication Instructions (Zvbc)
637 // Vector Carry-less Multiply
638 case RISCV::VCLMUL_VV:
639 case RISCV::VCLMUL_VX:
640 // Vector Carry-less Multiply Return High Half
641 case RISCV::VCLMULH_VV:
642 case RISCV::VCLMULH_VX:
643
644 // Zvabd
645 case RISCV::VABD_VV:
646 case RISCV::VABD_VX:
647 case RISCV::VABDU_VV:
648 case RISCV::VABDU_VX:
649
650 // Zvzip
651 case RISCV::VZIP_VV:
652 case RISCV::VUNZIPE_V:
653 case RISCV::VUNZIPO_V:
654 case RISCV::VPAIRE_VV:
655 case RISCV::VPAIRO_VV:
656 return MILog2SEW;
657
658 // Vector Widening Shift Left Logical (Zvbb)
659 case RISCV::VWSLL_VI:
660 case RISCV::VWSLL_VX:
661 case RISCV::VWSLL_VV:
662 // Vector Widening Integer Add/Subtract
663 // Def uses EEW=2*SEW . Operands use EEW=SEW.
664 case RISCV::VWADDU_VV:
665 case RISCV::VWADDU_VX:
666 case RISCV::VWSUBU_VV:
667 case RISCV::VWSUBU_VX:
668 case RISCV::VWADD_VV:
669 case RISCV::VWADD_VX:
670 case RISCV::VWSUB_VV:
671 case RISCV::VWSUB_VX:
672 // Vector Widening Integer Multiply Instructions
673 // Destination EEW=2*SEW. Source EEW=SEW.
674 case RISCV::VWMUL_VV:
675 case RISCV::VWMUL_VX:
676 case RISCV::VWMULSU_VV:
677 case RISCV::VWMULSU_VX:
678 case RISCV::VWMULU_VV:
679 case RISCV::VWMULU_VX:
680 // Vector Widening Integer Multiply-Add Instructions
681 // Destination EEW=2*SEW. Source EEW=SEW.
682 // A SEW-bit*SEW-bit multiply of the sources forms a 2*SEW-bit value, which
683 // is then added to the 2*SEW-bit Dest. These instructions never have a
684 // passthru operand.
685 case RISCV::VWMACCU_VV:
686 case RISCV::VWMACCU_VX:
687 case RISCV::VWMACC_VV:
688 case RISCV::VWMACC_VX:
689 case RISCV::VWMACCSU_VV:
690 case RISCV::VWMACCSU_VX:
691 case RISCV::VWMACCUS_VX:
692 // Vector Widening Floating-Point Fused Multiply-Add Instructions
693 case RISCV::VFWMACC_VF:
694 case RISCV::VFWMACC_VV:
695 case RISCV::VFWNMACC_VF:
696 case RISCV::VFWNMACC_VV:
697 case RISCV::VFWMSAC_VF:
698 case RISCV::VFWMSAC_VV:
699 case RISCV::VFWNMSAC_VF:
700 case RISCV::VFWNMSAC_VV:
701 case RISCV::VFWMACCBF16_VV:
702 case RISCV::VFWMACCBF16_VF:
703 // Vector Widening Floating-Point Add/Subtract Instructions
704 // Dest EEW=2*SEW. Source EEW=SEW.
705 case RISCV::VFWADD_VV:
706 case RISCV::VFWADD_VF:
707 case RISCV::VFWSUB_VV:
708 case RISCV::VFWSUB_VF:
709 // Vector Widening Floating-Point Multiply
710 case RISCV::VFWMUL_VF:
711 case RISCV::VFWMUL_VV:
712 // Widening Floating-Point/Integer Type-Convert Instructions
713 case RISCV::VFWCVT_XU_F_V:
714 case RISCV::VFWCVT_X_F_V:
715 case RISCV::VFWCVT_RTZ_XU_F_V:
716 case RISCV::VFWCVT_RTZ_X_F_V:
717 case RISCV::VFWCVT_F_XU_V:
718 case RISCV::VFWCVT_F_X_V:
719 case RISCV::VFWCVT_F_F_V:
720 case RISCV::VFWCVTBF16_F_F_V:
721 // Zvabd
722 case RISCV::VWABDA_VV:
723 case RISCV::VWABDA_VX:
724 case RISCV::VWABDAU_VV:
725 case RISCV::VWABDAU_VX:
726 return IsMODef ? MILog2SEW + 1 : MILog2SEW;
727
728 // Def and Op1 uses EEW=2*SEW. Op2 uses EEW=SEW.
729 case RISCV::VWADDU_WV:
730 case RISCV::VWADDU_WX:
731 case RISCV::VWSUBU_WV:
732 case RISCV::VWSUBU_WX:
733 case RISCV::VWADD_WV:
734 case RISCV::VWADD_WX:
735 case RISCV::VWSUB_WV:
736 case RISCV::VWSUB_WX:
737 // Vector Widening Floating-Point Add/Subtract Instructions
738 case RISCV::VFWADD_WF:
739 case RISCV::VFWADD_WV:
740 case RISCV::VFWSUB_WF:
741 case RISCV::VFWSUB_WV: {
742 bool IsOp1 = (HasPassthru && !IsTied) ? OpIdx == 2 : OpIdx == 1;
743 bool TwoTimes = IsMODef || IsOp1;
744 return TwoTimes ? MILog2SEW + 1 : MILog2SEW;
745 }
746
747 // Vector Integer Extension
748 case RISCV::VZEXT_VF2:
749 case RISCV::VSEXT_VF2:
750 return getIntegerExtensionOperandEEW(2, MI, OpIdx);
751 case RISCV::VZEXT_VF4:
752 case RISCV::VSEXT_VF4:
753 return getIntegerExtensionOperandEEW(4, MI, OpIdx);
754 case RISCV::VZEXT_VF8:
755 case RISCV::VSEXT_VF8:
756 return getIntegerExtensionOperandEEW(8, MI, OpIdx);
757
758 // Vector Narrowing Integer Right Shift Instructions
759 // Destination EEW=SEW, Op 1 has EEW=2*SEW. Op2 has EEW=SEW
760 case RISCV::VNSRL_WX:
761 case RISCV::VNSRL_WI:
762 case RISCV::VNSRL_WV:
763 case RISCV::VNSRA_WI:
764 case RISCV::VNSRA_WV:
765 case RISCV::VNSRA_WX:
766 // Vector Narrowing Fixed-Point Clip Instructions
767 // Destination and Op1 EEW=SEW. Op2 EEW=2*SEW.
768 case RISCV::VNCLIPU_WI:
769 case RISCV::VNCLIPU_WV:
770 case RISCV::VNCLIPU_WX:
771 case RISCV::VNCLIP_WI:
772 case RISCV::VNCLIP_WV:
773 case RISCV::VNCLIP_WX:
774 // Narrowing Floating-Point/Integer Type-Convert Instructions
775 case RISCV::VFNCVT_XU_F_W:
776 case RISCV::VFNCVT_X_F_W:
777 case RISCV::VFNCVT_RTZ_XU_F_W:
778 case RISCV::VFNCVT_RTZ_X_F_W:
779 case RISCV::VFNCVT_F_XU_W:
780 case RISCV::VFNCVT_F_X_W:
781 case RISCV::VFNCVT_F_F_W:
782 case RISCV::VFNCVT_ROD_F_F_W:
783 case RISCV::VFNCVTBF16_F_F_W: {
784 assert(!IsTied);
785 bool IsOp1 = HasPassthru ? OpIdx == 2 : OpIdx == 1;
786 bool TwoTimes = IsOp1;
787 return TwoTimes ? MILog2SEW + 1 : MILog2SEW;
788 }
789
790 // Vector Mask Instructions
791 // Vector Mask-Register Logical Instructions
792 // vmsbf.m set-before-first mask bit
793 // vmsif.m set-including-first mask bit
794 // vmsof.m set-only-first mask bit
795 // EEW=1
796 // We handle the cases when operand is a v0 mask operand above the switch,
797 // but these instructions may use non-v0 mask operands and need to be handled
798 // specifically.
799 case RISCV::VMAND_MM:
800 case RISCV::VMNAND_MM:
801 case RISCV::VMANDN_MM:
802 case RISCV::VMXOR_MM:
803 case RISCV::VMOR_MM:
804 case RISCV::VMNOR_MM:
805 case RISCV::VMORN_MM:
806 case RISCV::VMXNOR_MM:
807 case RISCV::VMSBF_M:
808 case RISCV::VMSIF_M:
809 case RISCV::VMSOF_M: {
810 return MILog2SEW;
811 }
812
813 // Vector Compress Instruction
814 // EEW=SEW, except the mask operand has EEW=1. Mask operand is not handled
815 // before this switch.
816 case RISCV::VCOMPRESS_VM:
817 return OpIdx == 3 ? 0 : MILog2SEW;
818
819 // Vector Iota Instruction
820 // EEW=SEW, except the mask operand has EEW=1. Mask operand is not handled
821 // before this switch.
822 case RISCV::VIOTA_M: {
823 if (IsMODef || OpIdx == 1)
824 return MILog2SEW;
825 return 0;
826 }
827
828 // Vector Integer Compare Instructions
829 // Dest EEW=1. Source EEW=SEW.
830 case RISCV::VMSEQ_VI:
831 case RISCV::VMSEQ_VV:
832 case RISCV::VMSEQ_VX:
833 case RISCV::VMSNE_VI:
834 case RISCV::VMSNE_VV:
835 case RISCV::VMSNE_VX:
836 case RISCV::VMSLTU_VV:
837 case RISCV::VMSLTU_VX:
838 case RISCV::VMSLT_VV:
839 case RISCV::VMSLT_VX:
840 case RISCV::VMSLEU_VV:
841 case RISCV::VMSLEU_VI:
842 case RISCV::VMSLEU_VX:
843 case RISCV::VMSLE_VV:
844 case RISCV::VMSLE_VI:
845 case RISCV::VMSLE_VX:
846 case RISCV::VMSGTU_VI:
847 case RISCV::VMSGTU_VX:
848 case RISCV::VMSGT_VI:
849 case RISCV::VMSGT_VX:
850 // Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions
851 // Dest EEW=1. Source EEW=SEW. Mask source operand handled above this switch.
852 case RISCV::VMADC_VIM:
853 case RISCV::VMADC_VVM:
854 case RISCV::VMADC_VXM:
855 case RISCV::VMSBC_VVM:
856 case RISCV::VMSBC_VXM:
857 // Dest EEW=1. Source EEW=SEW.
858 case RISCV::VMADC_VV:
859 case RISCV::VMADC_VI:
860 case RISCV::VMADC_VX:
861 case RISCV::VMSBC_VV:
862 case RISCV::VMSBC_VX:
863 // 13.13. Vector Floating-Point Compare Instructions
864 // Dest EEW=1. Source EEW=SEW
865 case RISCV::VMFEQ_VF:
866 case RISCV::VMFEQ_VV:
867 case RISCV::VMFNE_VF:
868 case RISCV::VMFNE_VV:
869 case RISCV::VMFLT_VF:
870 case RISCV::VMFLT_VV:
871 case RISCV::VMFLE_VF:
872 case RISCV::VMFLE_VV:
873 case RISCV::VMFGT_VF:
874 case RISCV::VMFGE_VF: {
875 if (IsMODef)
876 return 0;
877 return MILog2SEW;
878 }
879
880 // Vector Reduction Operations
881 // Vector Single-Width Integer Reduction Instructions
882 case RISCV::VREDAND_VS:
883 case RISCV::VREDMAX_VS:
884 case RISCV::VREDMAXU_VS:
885 case RISCV::VREDMIN_VS:
886 case RISCV::VREDMINU_VS:
887 case RISCV::VREDOR_VS:
888 case RISCV::VREDSUM_VS:
889 case RISCV::VREDXOR_VS:
890 // Vector Single-Width Floating-Point Reduction Instructions
891 case RISCV::VFREDMAX_VS:
892 case RISCV::VFREDMIN_VS:
893 case RISCV::VFREDOSUM_VS:
894 case RISCV::VFREDUSUM_VS: {
895 return MILog2SEW;
896 }
897
898 // Vector Widening Integer Reduction Instructions
899 // The Dest and VS1 read only element 0 for the vector register. Return
900 // 2*EEW for these. VS2 has EEW=SEW and EMUL=LMUL.
901 case RISCV::VWREDSUM_VS:
902 case RISCV::VWREDSUMU_VS:
903 // Vector Widening Floating-Point Reduction Instructions
904 case RISCV::VFWREDOSUM_VS:
905 case RISCV::VFWREDUSUM_VS: {
906 bool TwoTimes = IsMODef || OpIdx == 3;
907 return TwoTimes ? MILog2SEW + 1 : MILog2SEW;
908 }
909
910 // Vector Register Gather with 16-bit Index Elements Instruction
911 // Dest and source data EEW=SEW. Index vector EEW=16.
912 case RISCV::VRGATHEREI16_VV: {
913 if (OpIdx == 2)
914 return 4;
915 return MILog2SEW;
916 }
917
918 default:
919 return std::nullopt;
920 }
921}
922
923static std::optional<OperandInfo> getOperandInfo(const MachineInstr &MI,
924 unsigned OpIdx) {
926 RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
927 assert(RVV && "Could not find MI in PseudoTable");
928
929 std::optional<unsigned> Log2EEW = getOperandLog2EEW(MI, OpIdx);
930 if (!Log2EEW)
931 return std::nullopt;
932
933 switch (RVV->BaseInstr) {
934 // Vector Reduction Operations
935 // Vector Single-Width Integer Reduction Instructions
936 // Vector Widening Integer Reduction Instructions
937 // Vector Widening Floating-Point Reduction Instructions
938 // The Dest and VS1 only read element 0 of the vector register. Return just
939 // the EEW for these.
940 case RISCV::VREDAND_VS:
941 case RISCV::VREDMAX_VS:
942 case RISCV::VREDMAXU_VS:
943 case RISCV::VREDMIN_VS:
944 case RISCV::VREDMINU_VS:
945 case RISCV::VREDOR_VS:
946 case RISCV::VREDSUM_VS:
947 case RISCV::VREDXOR_VS:
948 case RISCV::VWREDSUM_VS:
949 case RISCV::VWREDSUMU_VS:
950 case RISCV::VFWREDOSUM_VS:
951 case RISCV::VFWREDUSUM_VS:
952 if (OpIdx != 2)
953 return OperandInfo(*Log2EEW);
954 break;
955
956 // Zvzip - vzip.vv interleaves two half-LMUL vectors into an LMUL result with
957 // the same SEW. The vtype LMUL describes the result, so only the two source
958 // operands have half the instruction's EMUL.
959 case RISCV::VZIP_VV: {
960 auto EMUL = getEMULEqualsEEWDivSEWTimesLMUL(*Log2EEW, MI);
961 if (OpIdx == 2 || OpIdx == 3)
962 EMUL = halfEMUL(EMUL);
963 return OperandInfo(EMUL, *Log2EEW);
964 }
965 // Zvzip - vunzipe.v / vunzipo.v split a 2*LMUL vector into LMUL even/odd
966 // elements with the same SEW. The source (and passthru tied to dest which is
967 // also LMUL sized - so only the vs2 source) has 2 * EMUL.
968 case RISCV::VUNZIPE_V:
969 case RISCV::VUNZIPO_V: {
970 auto EMUL = getEMULEqualsEEWDivSEWTimesLMUL(*Log2EEW, MI);
971 if (OpIdx == 2)
972 EMUL = doubleEMUL(EMUL);
973 return OperandInfo(EMUL, *Log2EEW);
974 }
975 };
976
977 // All others have EMUL=EEW/SEW*LMUL
978 return OperandInfo(getEMULEqualsEEWDivSEWTimesLMUL(*Log2EEW, MI), *Log2EEW);
979}
980
981static bool isTupleInsertInstr(const MachineInstr &MI);
982
983/// Return true if we can reason about demanded VLs elementwise for \p MI.
984bool RISCVVLOptimizerImpl::isSupportedInstr(const MachineInstr &MI) const {
985 if (MI.isPHI() || MI.isFullCopy() || isTupleInsertInstr(MI))
986 return true;
987
988 unsigned RVVOpc = RISCV::getRVVMCOpcode(MI.getOpcode());
989 if (!RVVOpc)
990 return false;
991
992 assert(!(MI.getNumExplicitDefs() == 0 && !MI.mayStore() &&
993 !RISCVII::elementsDependOnVL(TII->get(RVVOpc).TSFlags)) &&
994 "No defs but elements don't depend on VL?");
995
996 // TODO: Reduce vl for vmv.s.x and vfmv.s.f. Currently this introduces more vl
997 // toggles, we need to extend PRE in RISCVInsertVSETVLI first.
998 if (RVVOpc == RISCV::VMV_S_X || RVVOpc == RISCV::VFMV_S_F)
999 return false;
1000
1001 if (RISCVII::elementsDependOnVL(TII->get(RVVOpc).TSFlags))
1002 return false;
1003
1004 if (MI.mayStore())
1005 return false;
1006
1007 return true;
1008}
1009
1010/// Return true if operand \p OpIdx of \p MI is a vector operand but is used as
1011/// a scalar operand.
1012static bool isVectorOpUsedAsScalarOp(const MachineInstr &MI, unsigned OpIdx) {
1014 RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
1015
1016 if (!RVV)
1017 return false;
1018
1019 switch (RVV->BaseInstr) {
1020 // Reductions only use vs1[0] of vs1
1021 case RISCV::VREDAND_VS:
1022 case RISCV::VREDMAX_VS:
1023 case RISCV::VREDMAXU_VS:
1024 case RISCV::VREDMIN_VS:
1025 case RISCV::VREDMINU_VS:
1026 case RISCV::VREDOR_VS:
1027 case RISCV::VREDSUM_VS:
1028 case RISCV::VREDXOR_VS:
1029 case RISCV::VWREDSUM_VS:
1030 case RISCV::VWREDSUMU_VS:
1031 case RISCV::VFREDMAX_VS:
1032 case RISCV::VFREDMIN_VS:
1033 case RISCV::VFREDOSUM_VS:
1034 case RISCV::VFREDUSUM_VS:
1035 case RISCV::VFWREDOSUM_VS:
1036 case RISCV::VFWREDUSUM_VS:
1037 return OpIdx == 3;
1038 case RISCV::VMV_X_S:
1039 case RISCV::VFMV_F_S:
1040 return OpIdx == 1;
1041 default:
1042 return false;
1043 }
1044}
1045
1046bool RISCVVLOptimizerImpl::isCandidate(const MachineInstr &MI) const {
1047 const MCInstrDesc &Desc = MI.getDesc();
1048 if (!RISCVII::hasVLOp(Desc.TSFlags) || !RISCVII::hasSEWOp(Desc.TSFlags))
1049 return false;
1050
1051 if (MI.getNumExplicitDefs() != 1)
1052 return false;
1053
1054 // Some instructions have implicit defs e.g. $vxsat. If they might be read
1055 // later then we can't reduce VL.
1056 if (!MI.allImplicitDefsAreDead()) {
1057 LLVM_DEBUG(dbgs() << "Not a candidate because has non-dead implicit def\n");
1058 return false;
1059 }
1060
1061 if (MI.mayRaiseFPException()) {
1062 LLVM_DEBUG(dbgs() << "Not a candidate because may raise FP exception\n");
1063 return false;
1064 }
1065
1066 for (const MachineMemOperand *MMO : MI.memoperands()) {
1067 if (MMO->isVolatile()) {
1068 LLVM_DEBUG(dbgs() << "Not a candidate because contains volatile MMO\n");
1069 return false;
1070 }
1071 }
1072
1073 if (!isSupportedInstr(MI)) {
1074 LLVM_DEBUG(dbgs() << "Not a candidate due to unsupported instruction: "
1075 << MI);
1076 return false;
1077 }
1078
1080 TII->get(RISCV::getRVVMCOpcode(MI.getOpcode())).TSFlags) &&
1081 "Instruction shouldn't be supported if elements depend on VL");
1082
1084 MRI->getRegClass(MI.getOperand(0).getReg())->TSFlags) &&
1085 "All supported instructions produce a vector register result");
1086
1087 LLVM_DEBUG(dbgs() << "Found a candidate for VL reduction: " << MI << "\n");
1088 return true;
1089}
1090
1091/// Given a vslidedown.vx like:
1092///
1093/// %slideamt = ADDI %x, -1
1094/// %v = PseudoVSLIDEDOWN_VX %passthru, %src, %slideamt, avl=1
1095///
1096/// %v will only read the first %slideamt + 1 lanes of %src, which = %x.
1097/// This is a common case when lowering extractelement.
1098///
1099/// Note that if %x is 0, %slideamt will be all ones. In this case %src will be
1100/// completely slid down and none of its lanes will be read (since %slideamt is
1101/// greater than the largest VLMAX of 65536) so we can demand any minimum VL.
1102static std::optional<DemandedVL>
1104 const MachineRegisterInfo *MRI) {
1105 if (RISCV::getRVVMCOpcode(MI.getOpcode()) != RISCV::VSLIDEDOWN_VX)
1106 return std::nullopt;
1107 // We're looking at what lanes are used from the src operand.
1108 if (OpIdx != 2)
1109 return std::nullopt;
1110 // For now, the AVL must be 1.
1111 const MachineOperand &AVL = MI.getOperand(4);
1112 if (!AVL.isImm() || AVL.getImm() != 1)
1113 return std::nullopt;
1114 // The slide amount must be %x - 1.
1115 const MachineOperand &SlideAmt = MI.getOperand(3);
1116 if (!SlideAmt.getReg().isVirtual())
1117 return std::nullopt;
1118 MachineInstr *SlideAmtDef = MRI->getVRegDef(SlideAmt.getReg());
1119 if (!SlideAmtDef || SlideAmtDef->getOpcode() != RISCV::ADDI ||
1120 SlideAmtDef->getOperand(2).getImm() != -AVL.getImm() ||
1121 !SlideAmtDef->getOperand(1).getReg().isVirtual())
1122 return std::nullopt;
1123 return SlideAmtDef->getOperand(1);
1124}
1125
1126DemandedVL RISCVVLOptimizerImpl::getMinimumVLForUser(const MachineInstr &UserMI,
1127 unsigned OpIdx) const {
1128 const MachineOperand &UserOp = UserMI.getOperand(OpIdx);
1129 const MCInstrDesc &Desc = UserMI.getDesc();
1130
1131 if (UserMI.isPHI() || UserMI.isFullCopy() || isTupleInsertInstr(UserMI))
1132 return DemandedVLs.lookup(&UserMI);
1133
1134 if (!RISCVII::hasVLOp(Desc.TSFlags) || !RISCVII::hasSEWOp(Desc.TSFlags)) {
1135 LLVM_DEBUG(dbgs() << " Abort due to lack of VL, assume that"
1136 " use VLMAX\n");
1137 return DemandedVL::vlmax();
1138 }
1139
1140 if (auto VL = getMinimumVLForVSLIDEDOWN_VX(UserMI, OpIdx, MRI))
1141 return *VL;
1142
1143 unsigned RVVOpc = RISCV::getRVVMCOpcode(UserMI.getOpcode());
1144 bool IsVUNZIP = RVVOpc == RISCV::VUNZIPE_V || RVVOpc == RISCV::VUNZIPO_V;
1145 bool IsVZIP = RVVOpc == RISCV::VZIP_VV;
1146 if (!IsVUNZIP && RISCVII::readsPastVL(TII->get(RVVOpc).TSFlags)) {
1147 LLVM_DEBUG(dbgs() << " Abort because used by unsafe instruction\n");
1148 return DemandedVL::vlmax();
1149 }
1150
1151 unsigned VLOpNum = RISCVII::getVLOpNum(Desc);
1152 const MachineOperand &VLOp = UserMI.getOperand(VLOpNum);
1153 // Looking for an immediate or a register VL that isn't X0.
1154 assert((!VLOp.isReg() || VLOp.getReg() != RISCV::X0) &&
1155 "Did not expect X0 VL");
1156
1157 // If the user is a passthru it will read the elements past VL, so
1158 // abort if any of the elements past VL are demanded.
1159 if (UserOp.isTied()) {
1160 assert(OpIdx == UserMI.getNumExplicitDefs() &&
1162 if (!RISCV::isVLKnownLE(*MRI, DemandedVLs.lookup(&UserMI).VL, VLOp)) {
1163 LLVM_DEBUG(dbgs() << " Abort because user is passthru in "
1164 "instruction with demanded tail\n");
1165 return DemandedVL::vlmax();
1166 }
1167 }
1168
1169 // Instructions like reductions may use a vector register as a scalar
1170 // register. In this case, we should treat it as only reading the first lane.
1171 if (isVectorOpUsedAsScalarOp(UserMI, OpIdx)) {
1172 LLVM_DEBUG(dbgs() << " Used this operand as a scalar operand\n");
1173 return MachineOperand::CreateImm(1);
1174 }
1175
1176 // If we know the demanded VL of UserMI, then we can reduce the VL it
1177 // requires.
1178 DemandedVL MinimumVL = VLOp;
1179 if (RISCV::isVLKnownLE(*MRI, DemandedVLs.lookup(&UserMI).VL, VLOp))
1180 MinimumVL = DemandedVLs.lookup(&UserMI);
1181
1182 if (IsVUNZIP && OpIdx == 2)
1183 MinimumVL = doubleVL(MinimumVL);
1184 if (IsVZIP && (OpIdx == 2 || OpIdx == 3))
1185 MinimumVL = halfVL(MinimumVL, OpIdx == 2);
1186
1187 return MinimumVL;
1188}
1189
1190/// Return true if MI is an instruction used for assembling registers
1191/// for segmented store instructions, namely, RISCVISD::TUPLE_INSERT.
1192/// Currently it's lowered to INSERT_SUBREG.
1194 if (!MI.isInsertSubreg())
1195 return false;
1196
1197 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
1198 const TargetRegisterClass *DstRC = MRI.getRegClass(MI.getOperand(0).getReg());
1200 if (!RISCVRI::isVRegClass(DstRC->TSFlags))
1201 return false;
1202 unsigned NF = RISCVRI::getNF(DstRC->TSFlags);
1203 if (NF < 2)
1204 return false;
1205
1206 // Check whether INSERT_SUBREG has the correct subreg index for tuple inserts.
1207 auto VLMul = RISCVRI::getLMul(DstRC->TSFlags);
1208 unsigned SubRegIdx = MI.getOperand(3).getImm();
1209 [[maybe_unused]] auto [LMul, IsFractional] = RISCVVType::decodeVLMUL(VLMul);
1210 assert(!IsFractional && "unexpected LMUL for tuple register classes");
1211 return TRI->getSubRegIdxSize(SubRegIdx) == RISCV::RVVBitsPerBlock * LMul;
1212}
1213
1215 switch (RISCV::getRVVMCOpcode(MI.getOpcode())) {
1216 case VSSEG_CASES(8):
1217 case VSSSEG_CASES(8):
1218 case VSUXSEG_CASES(8):
1219 case VSOXSEG_CASES(8):
1220 case VSSEG_CASES(16):
1221 case VSSSEG_CASES(16):
1222 case VSUXSEG_CASES(16):
1223 case VSOXSEG_CASES(16):
1224 case VSSEG_CASES(32):
1225 case VSSSEG_CASES(32):
1226 case VSUXSEG_CASES(32):
1227 case VSOXSEG_CASES(32):
1228 case VSSEG_CASES(64):
1229 case VSSSEG_CASES(64):
1230 case VSUXSEG_CASES(64):
1231 case VSOXSEG_CASES(64):
1232 return true;
1233 default:
1234 return false;
1235 }
1236}
1237
1238bool RISCVVLOptimizerImpl::checkUsers(const MachineInstr &MI) const {
1239 if (MI.isPHI() || MI.isFullCopy() || isTupleInsertInstr(MI))
1240 return true;
1241
1242 SmallSetVector<MachineOperand *, 8> OpWorklist;
1243 SmallPtrSet<const MachineInstr *, 4> PHISeen;
1244 for (auto &UserOp : MRI->use_operands(MI.getOperand(0).getReg()))
1245 OpWorklist.insert(&UserOp);
1246
1247 while (!OpWorklist.empty()) {
1248 MachineOperand &UserOp = *OpWorklist.pop_back_val();
1249 const MachineInstr &UserMI = *UserOp.getParent();
1250 LLVM_DEBUG(dbgs() << " Checking user: " << UserMI << "\n");
1251
1252 if (UserMI.isFullCopy() && UserMI.getOperand(0).getReg().isVirtual()) {
1253 LLVM_DEBUG(dbgs() << " Peeking through uses of COPY\n");
1255 MRI->use_operands(UserMI.getOperand(0).getReg())));
1256 continue;
1257 }
1258
1259 if (isTupleInsertInstr(UserMI)) {
1260 LLVM_DEBUG(dbgs().indent(4) << "Peeking through uses of INSERT_SUBREG\n");
1261 for (MachineOperand &UseOp :
1262 MRI->use_operands(UserMI.getOperand(0).getReg())) {
1263 const MachineInstr &CandidateMI = *UseOp.getParent();
1264 // We should not propagate the VL if the user is not a segmented store
1265 // or another INSERT_SUBREG, since VL just works differently
1266 // between segmented operations (per-field) v.s. other RVV ops (on the
1267 // whole register group).
1268 if (!isTupleInsertInstr(CandidateMI) &&
1269 !isSegmentedStoreInstr(CandidateMI))
1270 return false;
1271 OpWorklist.insert(&UseOp);
1272 }
1273 continue;
1274 }
1275
1276 if (UserMI.isPHI()) {
1277 // Don't follow PHI cycles
1278 if (!PHISeen.insert(&UserMI).second)
1279 continue;
1280 LLVM_DEBUG(dbgs() << " Peeking through uses of PHI\n");
1282 MRI->use_operands(UserMI.getOperand(0).getReg())));
1283 continue;
1284 }
1285
1286 if (!RISCVII::hasSEWOp(UserMI.getDesc().TSFlags)) {
1287 LLVM_DEBUG(dbgs() << " Abort due to lack of SEW operand\n");
1288 return false;
1289 }
1290
1291 std::optional<OperandInfo> ConsumerInfo =
1292 getOperandInfo(UserMI, UserMI.getOperandNo(&UserOp));
1293 std::optional<OperandInfo> ProducerInfo = getOperandInfo(MI, 0);
1294 if (!ConsumerInfo || !ProducerInfo) {
1295 LLVM_DEBUG(dbgs() << " Abort due to unknown operand information.\n");
1296 LLVM_DEBUG(dbgs() << " ConsumerInfo is: " << ConsumerInfo << "\n");
1297 LLVM_DEBUG(dbgs() << " ProducerInfo is: " << ProducerInfo << "\n");
1298 return false;
1299 }
1300
1301 if (!OperandInfo::areCompatible(*ProducerInfo, *ConsumerInfo)) {
1302 LLVM_DEBUG(
1303 dbgs()
1304 << " Abort due to incompatible information for EMUL or EEW.\n");
1305 LLVM_DEBUG(dbgs() << " ConsumerInfo is: " << ConsumerInfo << "\n");
1306 LLVM_DEBUG(dbgs() << " ProducerInfo is: " << ProducerInfo << "\n");
1307 return false;
1308 }
1309 }
1310
1311 return true;
1312}
1313
1314bool RISCVVLOptimizerImpl::tryReduceVL(MachineInstr &MI,
1315 MachineOperand CommonVL) const {
1316 LLVM_DEBUG(dbgs() << "Trying to reduce VL for " << MI);
1317
1318 unsigned VLOpNum = RISCVII::getVLOpNum(MI.getDesc());
1319 MachineOperand &VLOp = MI.getOperand(VLOpNum);
1320
1321 assert((CommonVL.isImm() || CommonVL.getReg().isVirtual()) &&
1322 "Expected VL to be an Imm or virtual Reg");
1323
1324 // If the VL is defined by a vleff that doesn't dominate MI, try using the
1325 // vleff's AVL. It will be greater than or equal to the output VL.
1326 if (CommonVL.isReg()) {
1327 const MachineInstr *VLMI = MRI->getVRegDef(CommonVL.getReg());
1328 if (VLMI && RISCVInstrInfo::isFaultOnlyFirstLoad(*VLMI) &&
1329 !MDT->dominates(VLMI, &MI))
1330 CommonVL = VLMI->getOperand(RISCVII::getVLOpNum(VLMI->getDesc()));
1331 }
1332
1333 if (!RISCV::isVLKnownLE(*MRI, CommonVL, VLOp)) {
1334 LLVM_DEBUG(dbgs() << " Abort due to CommonVL not <= VLOp.\n");
1335 return false;
1336 }
1337
1338 if (CommonVL.isIdenticalTo(VLOp)) {
1339 LLVM_DEBUG(
1340 dbgs() << " Abort due to CommonVL == VLOp, no point in reducing.\n");
1341 return false;
1342 }
1343
1344 if (CommonVL.isImm()) {
1345 LLVM_DEBUG(dbgs() << " Reduce VL from " << VLOp << " to "
1346 << CommonVL.getImm() << " for " << MI << "\n");
1347 VLOp.ChangeToImmediate(CommonVL.getImm());
1348 return true;
1349 }
1350 MachineInstr *VLMI = MRI->getVRegDef(CommonVL.getReg());
1351 if (!VLMI)
1352 return false;
1353
1354 auto VLDominates = [this, &VLMI](const MachineInstr &MI) {
1355 return MDT->dominates(VLMI, &MI);
1356 };
1357 if (!VLDominates(MI)) {
1358 assert(MI.getNumExplicitDefs() == 1);
1359 auto Uses = MRI->use_instructions(MI.getOperand(0).getReg());
1360 auto UsesSameBB = make_filter_range(Uses, [&MI](const MachineInstr &Use) {
1361 return Use.getParent() == MI.getParent();
1362 });
1363 if (VLMI->getParent() == MI.getParent() &&
1364 all_of(UsesSameBB, VLDominates) &&
1365 RISCVInstrInfo::isSafeToMove(MI, std::next(VLMI->getIterator()))) {
1366 VLMI->getParent()->splice(std::next(VLMI->getIterator()), MI.getParent(),
1367 MI.getIterator());
1368 } else {
1369 LLVM_DEBUG(dbgs() << " Abort due to VL not dominating.\n");
1370 return false;
1371 }
1372 }
1373 LLVM_DEBUG(dbgs() << " Reduce VL from " << VLOp << " to "
1374 << printReg(CommonVL.getReg(), MRI->getTargetRegisterInfo())
1375 << " for " << MI << "\n");
1376
1377 // All our checks passed. We can reduce VL.
1378 VLOp.ChangeToRegister(CommonVL.getReg(), false);
1379 MRI->constrainRegClass(CommonVL.getReg(), &RISCV::GPRNoX0RegClass);
1380 return true;
1381}
1382
1383static bool isPhysical(const MachineOperand &MO) {
1384 return MO.isReg() && MO.getReg().isPhysical();
1385}
1386
1387/// Look through \p MI's operands and propagate what it demands to its uses.
1388void RISCVVLOptimizerImpl::transfer(const MachineInstr &MI) {
1389 if (!isSupportedInstr(MI) || !checkUsers(MI) || any_of(MI.defs(), isPhysical))
1390 DemandedVLs[&MI] = DemandedVL::vlmax();
1391
1392 for (const MachineOperand &MO : virtual_vec_uses(MI)) {
1393 const MachineInstr *Def = MRI->getVRegDef(MO.getReg());
1394 DemandedVL Prev = DemandedVLs[Def];
1395 DemandedVLs[Def] = DemandedVLs[Def].max(
1396 *MRI, getMinimumVLForUser(MI, MI.getOperandNo(&MO)));
1397 if (DemandedVLs[Def] != Prev)
1398 Worklist.insert(Def);
1399 }
1400}
1401
1402bool RISCVVLOptimizerImpl::run(MachineFunction &MF) {
1403 MRI = &MF.getRegInfo();
1404
1405 const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
1406 if (!ST.hasVInstructions())
1407 return false;
1408
1409 TII = ST.getInstrInfo();
1410
1411 assert(DemandedVLs.empty());
1412
1413 // For each instruction that defines or uses a vector, propagate the VL it
1414 // uses to its inputs.
1415 for (MachineBasicBlock *MBB : post_order(&MF)) {
1417 for (MachineInstr &MI : reverse(*MBB)) {
1418 if (MI.isDebugInstr())
1419 continue;
1420 if (virtual_vec_defs(MI).empty() && virtual_vec_uses(MI).empty())
1421 continue;
1422 Worklist.insert(&MI);
1423 }
1424 }
1425
1426 while (!Worklist.empty()) {
1427 const MachineInstr *MI = Worklist.front();
1428 Worklist.remove(MI);
1429 transfer(*MI);
1430 }
1431
1432 // Then go through and see if we can reduce the VL of any instructions to
1433 // only what's demanded.
1434 bool MadeChange = false;
1435 for (auto &[MI, VL] : DemandedVLs) {
1436 assert(MDT->isReachableFromEntry(MI->getParent()));
1437 if (!isCandidate(*MI))
1438 continue;
1439 if (!tryReduceVL(*const_cast<MachineInstr *>(MI), VL.VL))
1440 continue;
1441 MadeChange = true;
1442 }
1443
1444 DemandedVLs.clear();
1445 return MadeChange;
1446}
1447
1448bool RISCVVLOptimizerLegacy::runOnMachineFunction(MachineFunction &MF) {
1449 if (skipFunction(MF.getFunction()))
1450 return false;
1451
1452 auto *MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
1453 return RISCVVLOptimizerImpl(MDT).run(MF);
1454}
1455
1456PreservedAnalyses
1459 auto *MDT = &MFAM.getResult<MachineDominatorTreeAnalysis>(MF);
1460 bool Changed = RISCVVLOptimizerImpl(MDT).run(MF);
1461 if (!Changed)
1462 return PreservedAnalyses::all();
1463
1467 return PA;
1468}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
static bool isCandidate(const MachineInstr *MI, Register &DefedReg, Register FrameReg)
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
static DemandedVL halfVL(DemandedVL MinimumVL, bool Ceil=false)
#define VSOXSEG_CASES(EEW)
static unsigned getIntegerExtensionOperandEEW(unsigned Factor, const MachineInstr &MI, unsigned OpIdx)
Dest has EEW=SEW.
static std::pair< unsigned, bool > halfEMUL(std::pair< unsigned, bool > EMUL)
static bool isSegmentedStoreInstr(const MachineInstr &MI)
static std::optional< DemandedVL > getMinimumVLForVSLIDEDOWN_VX(const MachineInstr &MI, unsigned OpIdx, const MachineRegisterInfo *MRI)
Given a vslidedown.vx like:
static bool isVectorOpUsedAsScalarOp(const MachineInstr &MI, unsigned OpIdx)
Return true if operand OpIdx of MI is a vector operand but is used as a scalar operand.
static std::optional< OperandInfo > getOperandInfo(const MachineInstr &MI, unsigned OpIdx)
static DemandedVL doubleVL(DemandedVL MinimumVL)
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...
#define VSUXSEG_CASES(EEW)
static bool isPhysical(const MachineOperand &MO)
static std::optional< unsigned > getOperandLog2EEW(const MachineInstr &MI, unsigned OpIdx)
static std::pair< unsigned, bool > doubleEMUL(std::pair< unsigned, bool > EMUL)
#define VSSSEG_CASES(EEW)
#define VSSEG_CASES(EEW)
static bool isTupleInsertInstr(const MachineInstr &MI)
Return true if MI is an instruction used for assembling registers for segmented store instructions,...
Remove Loads Into Fake Uses
This file implements a set that has insertion order iteration characteristics.
#define LLVM_DEBUG(...)
Definition Debug.h:119
#define PASS_NAME
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:278
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
bool isReachableFromEntry(const NodeT *A) const
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Describe properties that are true of each instruction in the target description file.
This holds information about one operand of a machine instruction, indicating the register class for ...
Definition MCInstrDesc.h:88
const uint8_t TSFlags
Configurable target specific flags.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
Analysis pass which computes a MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineInstr *A, const MachineInstr *B) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
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.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
bool isFullCopy() const
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
LLVM_ABI unsigned getNumExplicitDefs() const
Returns the number of non-implicit definitions.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
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.
LLVM_ABI void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
LLVM_ABI 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.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
iterator_range< use_instr_iterator > use_instructions(Register Reg) const
const TargetRegisterInfo * getTargetRegisterInfo() const
LLVM_ABI const TargetRegisterClass * constrainRegClass(Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
iterator_range< use_iterator > use_operands(Register Reg) const
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
static bool isSafeToMove(const MachineInstr &From, const MachineBasicBlock::iterator &To)
Return true if moving From down to To won't cause any physical register reads or writes to be clobber...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
A vector that has set insertion semantics.
Definition SetVector.h:57
void insert_range(Range &&R)
Definition SetVector.h:182
bool empty() const
Determine if the SetVector is empty or not.
Definition SetVector.h:100
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
value_type pop_back_val()
Definition SetVector.h:285
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
self_iterator getIterator()
Definition ilist_node.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static bool readsPastVL(uint64_t TSFlags)
static bool isTiedPseudo(uint64_t TSFlags)
static RISCVVType::VLMUL getLMul(uint64_t TSFlags)
static unsigned getVLOpNum(const MCInstrDesc &Desc)
static bool hasVLOp(uint64_t TSFlags)
static unsigned getSEWOpNum(const MCInstrDesc &Desc)
static bool elementsDependOnVL(uint64_t TSFlags)
static bool hasSEWOp(uint64_t TSFlags)
static bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc)
static unsigned getNF(uint8_t TSFlags)
static bool isVRegClass(uint8_t TSFlags)
static RISCVVType::VLMUL getLMul(uint8_t TSFlags)
LLVM_ABI std::pair< unsigned, bool > decodeVLMUL(VLMUL VLMul)
unsigned getRVVMCOpcode(unsigned RVVPseudoOpcode)
static constexpr unsigned RVVBitsPerBlock
static constexpr int64_t VLMaxSentinel
bool isVLKnownLE(const MachineRegisterInfo &MRI, const MachineOperand &LHS, const MachineOperand &RHS)
Given two VL operands, do we know that LHS <= RHS?
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
bool empty() const
Definition BasicBlock.h:101
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
bool operator!=(uint64_t V1, const APInt &V2)
Definition APInt.h:2139
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
Op::Description Desc
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
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:326
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
Definition STLExtras.h:552
auto post_order(const T &G)
Post-order traversal of a graph.
@ Other
Any other memory.
Definition ModRef.h:68
constexpr NextUseDistance max(NextUseDistance A, NextUseDistance B)
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:368
FunctionPass * createRISCVVLOptimizerLegacyPass()
LLVM_ABI 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.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58