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