LLVM 20.0.0git
RISCVVLOptimizer.cpp
Go to the documentation of this file.
1//===-------------- RISCVVLOptimizer.cpp - VL Optimizer -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===---------------------------------------------------------------------===//
8//
9// This pass reduces the VL where possible at the MI level, before VSETVLI
10// instructions are inserted.
11//
12// The purpose of this optimization is to make the VL argument, for instructions
13// that have a VL argument, as small as possible. This is implemented by
14// visiting each instruction in reverse order and checking that if it has a VL
15// argument, whether the VL can be reduced.
16//
17//===---------------------------------------------------------------------===//
18
19#include "RISCV.h"
20#include "RISCVSubtarget.h"
21#include "llvm/ADT/SetVector.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "riscv-vl-optimizer"
29#define PASS_NAME "RISC-V VL Optimizer"
30
31namespace {
32
33class RISCVVLOptimizer : public MachineFunctionPass {
35 const MachineDominatorTree *MDT;
36
37public:
38 static char ID;
39
40 RISCVVLOptimizer() : MachineFunctionPass(ID) {}
41
42 bool runOnMachineFunction(MachineFunction &MF) override;
43
44 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.setPreservesCFG();
48 }
49
50 StringRef getPassName() const override { return PASS_NAME; }
51
52private:
53 bool checkUsers(const MachineOperand *&CommonVL, MachineInstr &MI);
54 bool tryReduceVL(MachineInstr &MI);
55 bool isCandidate(const MachineInstr &MI) const;
56};
57
58} // end anonymous namespace
59
60char RISCVVLOptimizer::ID = 0;
61INITIALIZE_PASS_BEGIN(RISCVVLOptimizer, DEBUG_TYPE, PASS_NAME, false, false)
64
66 return new RISCVVLOptimizer();
67}
68
69/// Return true if R is a physical or virtual vector register, false otherwise.
71 if (R.isPhysical())
72 return RISCV::VRRegClass.contains(R);
73 const TargetRegisterClass *RC = MRI->getRegClass(R);
74 return RISCVRI::isVRegClass(RC->TSFlags);
75}
76
77/// Represents the EMUL and EEW of a MachineOperand.
79 enum class State {
80 Unknown,
81 Known,
82 } S;
83
84 // Represent as 1,2,4,8, ... and fractional indicator. This is because
85 // EMUL can take on values that don't map to RISCVII::VLMUL values exactly.
86 // For example, a mask operand can have an EMUL less than MF8.
87 std::optional<std::pair<unsigned, bool>> EMUL;
88
89 unsigned Log2EEW;
90
92 : S(State::Known), EMUL(RISCVVType::decodeVLMUL(EMUL)), Log2EEW(Log2EEW) {
93 }
94
95 OperandInfo(std::pair<unsigned, bool> EMUL, unsigned Log2EEW)
97
99
100 bool isUnknown() const { return S == State::Unknown; }
101 bool isKnown() const { return S == State::Known; }
102
103 static bool EMULAndEEWAreEqual(const OperandInfo &A, const OperandInfo &B) {
104 assert(A.isKnown() && B.isKnown() && "Both operands must be known");
105
106 return A.Log2EEW == B.Log2EEW && A.EMUL->first == B.EMUL->first &&
107 A.EMUL->second == B.EMUL->second;
108 }
109
110 void print(raw_ostream &OS) const {
111 if (isUnknown()) {
112 OS << "Unknown";
113 return;
114 }
115 assert(EMUL && "Expected EMUL to have value");
116 OS << "EMUL: m";
117 if (EMUL->second)
118 OS << "f";
119 OS << EMUL->first;
120 OS << ", EEW: " << (1 << Log2EEW);
121 }
122};
123
126 OI.print(OS);
127 return OS;
128}
129
130namespace llvm {
131namespace RISCVVType {
132/// Return the RISCVII::VLMUL that is two times VLMul.
133/// Precondition: VLMul is not LMUL_RESERVED or LMUL_8.
135 switch (VLMul) {
149 default:
150 llvm_unreachable("Could not multiply VLMul by 2");
151 }
152}
153
154/// Return EMUL = (EEW / SEW) * LMUL where EEW comes from Log2EEW and LMUL and
155/// SEW are from the TSFlags of MI.
156static std::pair<unsigned, bool>
158 RISCVII::VLMUL MIVLMUL = RISCVII::getLMul(MI.getDesc().TSFlags);
159 auto [MILMUL, MILMULIsFractional] = RISCVVType::decodeVLMUL(MIVLMUL);
160 unsigned MILog2SEW =
161 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
162
163 // Mask instructions will have 0 as the SEW operand. But the LMUL of these
164 // instructions is calculated is as if the SEW operand was 3 (e8).
165 if (MILog2SEW == 0)
166 MILog2SEW = 3;
167
168 unsigned MISEW = 1 << MILog2SEW;
169
170 unsigned EEW = 1 << Log2EEW;
171 // Calculate (EEW/SEW)*LMUL preserving fractions less than 1. Use GCD
172 // to put fraction in simplest form.
173 unsigned Num = EEW, Denom = MISEW;
174 int GCD = MILMULIsFractional ? std::gcd(Num, Denom * MILMUL)
175 : std::gcd(Num * MILMUL, Denom);
176 Num = MILMULIsFractional ? Num / GCD : Num * MILMUL / GCD;
177 Denom = MILMULIsFractional ? Denom * MILMUL / GCD : Denom / GCD;
178 return std::make_pair(Num > Denom ? Num : Denom, Denom > Num);
179}
180} // end namespace RISCVVType
181} // end namespace llvm
182
183/// Dest has EEW=SEW and EMUL=LMUL. Source EEW=SEW/Factor (i.e. F2 => EEW/2).
184/// Source has EMUL=(EEW/SEW)*LMUL. LMUL and SEW comes from TSFlags of MI.
186 const MachineInstr &MI,
187 const MachineOperand &MO) {
188 RISCVII::VLMUL MIVLMul = RISCVII::getLMul(MI.getDesc().TSFlags);
189 unsigned MILog2SEW =
190 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
191
192 if (MO.getOperandNo() == 0)
193 return OperandInfo(MIVLMul, MILog2SEW);
194
195 unsigned MISEW = 1 << MILog2SEW;
196 unsigned EEW = MISEW / Factor;
197 unsigned Log2EEW = Log2_32(EEW);
198
200 Log2EEW);
201}
202
203/// Check whether MO is a mask operand of MI.
204static bool isMaskOperand(const MachineInstr &MI, const MachineOperand &MO,
205 const MachineRegisterInfo *MRI) {
206
207 if (!MO.isReg() || !isVectorRegClass(MO.getReg(), MRI))
208 return false;
209
210 const MCInstrDesc &Desc = MI.getDesc();
211 return Desc.operands()[MO.getOperandNo()].RegClass == RISCV::VMV0RegClassID;
212}
213
214/// Return the OperandInfo for MO.
216 const MachineRegisterInfo *MRI) {
217 const MachineInstr &MI = *MO.getParent();
219 RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
220 assert(RVV && "Could not find MI in PseudoTable");
221
222 // MI has a VLMUL and SEW associated with it. The RVV specification defines
223 // the LMUL and SEW of each operand and definition in relation to MI.VLMUL and
224 // MI.SEW.
225 RISCVII::VLMUL MIVLMul = RISCVII::getLMul(MI.getDesc().TSFlags);
226 unsigned MILog2SEW =
227 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
228
229 const bool HasPassthru = RISCVII::isFirstDefTiedToFirstUse(MI.getDesc());
230
231 // We bail out early for instructions that have passthru with non NoRegister,
232 // which means they are using TU policy. We are not interested in these
233 // since they must preserve the entire register content.
234 if (HasPassthru && MO.getOperandNo() == MI.getNumExplicitDefs() &&
235 (MO.getReg() != RISCV::NoRegister))
236 return {};
237
238 bool IsMODef = MO.getOperandNo() == 0;
239
240 // All mask operands have EEW=1, EMUL=(EEW/SEW)*LMUL
241 if (isMaskOperand(MI, MO, MRI))
243
244 // switch against BaseInstr to reduce number of cases that need to be
245 // considered.
246 switch (RVV->BaseInstr) {
247
248 // 6. Configuration-Setting Instructions
249 // Configuration setting instructions do not read or write vector registers
250 case RISCV::VSETIVLI:
251 case RISCV::VSETVL:
252 case RISCV::VSETVLI:
253 llvm_unreachable("Configuration setting instructions do not read or write "
254 "vector registers");
255
256 // Vector Loads and Stores
257 // Vector Unit-Stride Instructions
258 // Vector Strided Instructions
259 /// Dest EEW encoded in the instruction and EMUL=(EEW/SEW)*LMUL
260 case RISCV::VSE8_V:
261 case RISCV::VSSE8_V:
263 case RISCV::VSE16_V:
264 case RISCV::VSSE16_V:
266 case RISCV::VSE32_V:
267 case RISCV::VSSE32_V:
269 case RISCV::VSE64_V:
270 case RISCV::VSSE64_V:
272
273 // Vector Indexed Instructions
274 // vs(o|u)xei<eew>.v
275 // Dest/Data (operand 0) EEW=SEW, EMUL=LMUL. Source EEW=<eew> and
276 // EMUL=(EEW/SEW)*LMUL.
277 case RISCV::VLUXEI8_V:
278 case RISCV::VLOXEI8_V:
279 case RISCV::VSUXEI8_V:
280 case RISCV::VSOXEI8_V: {
281 if (MO.getOperandNo() == 0)
282 return OperandInfo(MIVLMul, MILog2SEW);
284 }
285 case RISCV::VLUXEI16_V:
286 case RISCV::VLOXEI16_V:
287 case RISCV::VSUXEI16_V:
288 case RISCV::VSOXEI16_V: {
289 if (MO.getOperandNo() == 0)
290 return OperandInfo(MIVLMul, MILog2SEW);
292 }
293 case RISCV::VLUXEI32_V:
294 case RISCV::VLOXEI32_V:
295 case RISCV::VSUXEI32_V:
296 case RISCV::VSOXEI32_V: {
297 if (MO.getOperandNo() == 0)
298 return OperandInfo(MIVLMul, MILog2SEW);
300 }
301 case RISCV::VLUXEI64_V:
302 case RISCV::VLOXEI64_V:
303 case RISCV::VSUXEI64_V:
304 case RISCV::VSOXEI64_V: {
305 if (MO.getOperandNo() == 0)
306 return OperandInfo(MIVLMul, MILog2SEW);
308 }
309
310 // Vector Integer Arithmetic Instructions
311 // Vector Single-Width Integer Add and Subtract
312 case RISCV::VADD_VI:
313 case RISCV::VADD_VV:
314 case RISCV::VADD_VX:
315 case RISCV::VSUB_VV:
316 case RISCV::VSUB_VX:
317 case RISCV::VRSUB_VI:
318 case RISCV::VRSUB_VX:
319 // Vector Bitwise Logical Instructions
320 // Vector Single-Width Shift Instructions
321 // EEW=SEW. EMUL=LMUL.
322 case RISCV::VAND_VI:
323 case RISCV::VAND_VV:
324 case RISCV::VAND_VX:
325 case RISCV::VOR_VI:
326 case RISCV::VOR_VV:
327 case RISCV::VOR_VX:
328 case RISCV::VXOR_VI:
329 case RISCV::VXOR_VV:
330 case RISCV::VXOR_VX:
331 case RISCV::VSLL_VI:
332 case RISCV::VSLL_VV:
333 case RISCV::VSLL_VX:
334 case RISCV::VSRL_VI:
335 case RISCV::VSRL_VV:
336 case RISCV::VSRL_VX:
337 case RISCV::VSRA_VI:
338 case RISCV::VSRA_VV:
339 case RISCV::VSRA_VX:
340 // Vector Integer Min/Max Instructions
341 // EEW=SEW. EMUL=LMUL.
342 case RISCV::VMINU_VV:
343 case RISCV::VMINU_VX:
344 case RISCV::VMIN_VV:
345 case RISCV::VMIN_VX:
346 case RISCV::VMAXU_VV:
347 case RISCV::VMAXU_VX:
348 case RISCV::VMAX_VV:
349 case RISCV::VMAX_VX:
350 // Vector Single-Width Integer Multiply Instructions
351 // Source and Dest EEW=SEW and EMUL=LMUL.
352 case RISCV::VMUL_VV:
353 case RISCV::VMUL_VX:
354 case RISCV::VMULH_VV:
355 case RISCV::VMULH_VX:
356 case RISCV::VMULHU_VV:
357 case RISCV::VMULHU_VX:
358 case RISCV::VMULHSU_VV:
359 case RISCV::VMULHSU_VX:
360 // Vector Integer Divide Instructions
361 // EEW=SEW. EMUL=LMUL.
362 case RISCV::VDIVU_VV:
363 case RISCV::VDIVU_VX:
364 case RISCV::VDIV_VV:
365 case RISCV::VDIV_VX:
366 case RISCV::VREMU_VV:
367 case RISCV::VREMU_VX:
368 case RISCV::VREM_VV:
369 case RISCV::VREM_VX:
370 // Vector Single-Width Integer Multiply-Add Instructions
371 // EEW=SEW. EMUL=LMUL.
372 case RISCV::VMACC_VV:
373 case RISCV::VMACC_VX:
374 case RISCV::VNMSAC_VV:
375 case RISCV::VNMSAC_VX:
376 case RISCV::VMADD_VV:
377 case RISCV::VMADD_VX:
378 case RISCV::VNMSUB_VV:
379 case RISCV::VNMSUB_VX:
380 // Vector Integer Merge Instructions
381 // Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions
382 // EEW=SEW and EMUL=LMUL, except the mask operand has EEW=1 and EMUL=
383 // (EEW/SEW)*LMUL. Mask operand is handled before this switch.
384 case RISCV::VMERGE_VIM:
385 case RISCV::VMERGE_VVM:
386 case RISCV::VMERGE_VXM:
387 case RISCV::VADC_VIM:
388 case RISCV::VADC_VVM:
389 case RISCV::VADC_VXM:
390 case RISCV::VSBC_VVM:
391 case RISCV::VSBC_VXM:
392 // Vector Integer Move Instructions
393 // Vector Fixed-Point Arithmetic Instructions
394 // Vector Single-Width Saturating Add and Subtract
395 // Vector Single-Width Averaging Add and Subtract
396 // EEW=SEW. EMUL=LMUL.
397 case RISCV::VMV_V_I:
398 case RISCV::VMV_V_V:
399 case RISCV::VMV_V_X:
400 case RISCV::VSADDU_VI:
401 case RISCV::VSADDU_VV:
402 case RISCV::VSADDU_VX:
403 case RISCV::VSADD_VI:
404 case RISCV::VSADD_VV:
405 case RISCV::VSADD_VX:
406 case RISCV::VSSUBU_VV:
407 case RISCV::VSSUBU_VX:
408 case RISCV::VSSUB_VV:
409 case RISCV::VSSUB_VX:
410 case RISCV::VAADDU_VV:
411 case RISCV::VAADDU_VX:
412 case RISCV::VAADD_VV:
413 case RISCV::VAADD_VX:
414 case RISCV::VASUBU_VV:
415 case RISCV::VASUBU_VX:
416 case RISCV::VASUB_VV:
417 case RISCV::VASUB_VX:
418 // Vector Single-Width Scaling Shift Instructions
419 // EEW=SEW. EMUL=LMUL.
420 case RISCV::VSSRL_VI:
421 case RISCV::VSSRL_VV:
422 case RISCV::VSSRL_VX:
423 case RISCV::VSSRA_VI:
424 case RISCV::VSSRA_VV:
425 case RISCV::VSSRA_VX:
426 // Vector Permutation Instructions
427 // Integer Scalar Move Instructions
428 // Floating-Point Scalar Move Instructions
429 // EMUL=LMUL. EEW=SEW.
430 case RISCV::VMV_X_S:
431 case RISCV::VMV_S_X:
432 case RISCV::VFMV_F_S:
433 case RISCV::VFMV_S_F:
434 // Vector Slide Instructions
435 // EMUL=LMUL. EEW=SEW.
436 case RISCV::VSLIDEUP_VI:
437 case RISCV::VSLIDEUP_VX:
438 case RISCV::VSLIDEDOWN_VI:
439 case RISCV::VSLIDEDOWN_VX:
440 case RISCV::VSLIDE1UP_VX:
441 case RISCV::VFSLIDE1UP_VF:
442 case RISCV::VSLIDE1DOWN_VX:
443 case RISCV::VFSLIDE1DOWN_VF:
444 // Vector Register Gather Instructions
445 // EMUL=LMUL. EEW=SEW. For mask operand, EMUL=1 and EEW=1.
446 case RISCV::VRGATHER_VI:
447 case RISCV::VRGATHER_VV:
448 case RISCV::VRGATHER_VX:
449 // Vector Compress Instruction
450 // EMUL=LMUL. EEW=SEW.
451 case RISCV::VCOMPRESS_VM:
452 // Vector Element Index Instruction
453 case RISCV::VID_V:
454 return OperandInfo(MIVLMul, MILog2SEW);
455
456 // Vector Widening Integer Add/Subtract
457 // Def uses EEW=2*SEW and EMUL=2*LMUL. Operands use EEW=SEW and EMUL=LMUL.
458 case RISCV::VWADDU_VV:
459 case RISCV::VWADDU_VX:
460 case RISCV::VWSUBU_VV:
461 case RISCV::VWSUBU_VX:
462 case RISCV::VWADD_VV:
463 case RISCV::VWADD_VX:
464 case RISCV::VWSUB_VV:
465 case RISCV::VWSUB_VX:
466 case RISCV::VWSLL_VI:
467 // Vector Widening Integer Multiply Instructions
468 // Source and Destination EMUL=LMUL. Destination EEW=2*SEW. Source EEW=SEW.
469 case RISCV::VWMUL_VV:
470 case RISCV::VWMUL_VX:
471 case RISCV::VWMULSU_VV:
472 case RISCV::VWMULSU_VX:
473 case RISCV::VWMULU_VV:
474 case RISCV::VWMULU_VX:
475 // Vector Widening Integer Multiply-Add Instructions
476 // Destination EEW=2*SEW and EMUL=2*LMUL. Source EEW=SEW and EMUL=LMUL.
477 // A SEW-bit*SEW-bit multiply of the sources forms a 2*SEW-bit value, which
478 // is then added to the 2*SEW-bit Dest. These instructions never have a
479 // passthru operand.
480 case RISCV::VWMACCU_VV:
481 case RISCV::VWMACCU_VX:
482 case RISCV::VWMACC_VV:
483 case RISCV::VWMACC_VX:
484 case RISCV::VWMACCSU_VV:
485 case RISCV::VWMACCSU_VX:
486 case RISCV::VWMACCUS_VX: {
487 unsigned Log2EEW = IsMODef ? MILog2SEW + 1 : MILog2SEW;
488 RISCVII::VLMUL EMUL =
489 IsMODef ? RISCVVType::twoTimesVLMUL(MIVLMul) : MIVLMul;
490 return OperandInfo(EMUL, Log2EEW);
491 }
492
493 // Def and Op1 uses EEW=2*SEW and EMUL=2*LMUL. Op2 uses EEW=SEW and EMUL=LMUL
494 case RISCV::VWADDU_WV:
495 case RISCV::VWADDU_WX:
496 case RISCV::VWSUBU_WV:
497 case RISCV::VWSUBU_WX:
498 case RISCV::VWADD_WV:
499 case RISCV::VWADD_WX:
500 case RISCV::VWSUB_WV:
501 case RISCV::VWSUB_WX: {
502 bool IsOp1 = HasPassthru ? MO.getOperandNo() == 2 : MO.getOperandNo() == 1;
503 bool TwoTimes = IsMODef || IsOp1;
504 unsigned Log2EEW = TwoTimes ? MILog2SEW + 1 : MILog2SEW;
505 RISCVII::VLMUL EMUL =
506 TwoTimes ? RISCVVType::twoTimesVLMUL(MIVLMul) : MIVLMul;
507 return OperandInfo(EMUL, Log2EEW);
508 }
509
510 // Vector Integer Extension
511 case RISCV::VZEXT_VF2:
512 case RISCV::VSEXT_VF2:
513 return getIntegerExtensionOperandInfo(2, MI, MO);
514 case RISCV::VZEXT_VF4:
515 case RISCV::VSEXT_VF4:
516 return getIntegerExtensionOperandInfo(4, MI, MO);
517 case RISCV::VZEXT_VF8:
518 case RISCV::VSEXT_VF8:
519 return getIntegerExtensionOperandInfo(8, MI, MO);
520
521 // Vector Narrowing Integer Right Shift Instructions
522 // Destination EEW=SEW and EMUL=LMUL, Op 1 has EEW=2*SEW EMUL=2*LMUL. Op2 has
523 // EEW=SEW EMUL=LMUL.
524 case RISCV::VNSRL_WX:
525 case RISCV::VNSRL_WI:
526 case RISCV::VNSRL_WV:
527 case RISCV::VNSRA_WI:
528 case RISCV::VNSRA_WV:
529 case RISCV::VNSRA_WX:
530 // Vector Narrowing Fixed-Point Clip Instructions
531 // Destination and Op1 EEW=SEW and EMUL=LMUL. Op2 EEW=2*SEW and EMUL=2*LMUL
532 case RISCV::VNCLIPU_WI:
533 case RISCV::VNCLIPU_WV:
534 case RISCV::VNCLIPU_WX:
535 case RISCV::VNCLIP_WI:
536 case RISCV::VNCLIP_WV:
537 case RISCV::VNCLIP_WX: {
538 bool IsOp1 = HasPassthru ? MO.getOperandNo() == 2 : MO.getOperandNo() == 1;
539 bool TwoTimes = IsOp1;
540 unsigned Log2EEW = TwoTimes ? MILog2SEW + 1 : MILog2SEW;
541 RISCVII::VLMUL EMUL =
542 TwoTimes ? RISCVVType::twoTimesVLMUL(MIVLMul) : MIVLMul;
543 return OperandInfo(EMUL, Log2EEW);
544 }
545
546 // Vector Mask Instructions
547 // Vector Mask-Register Logical Instructions
548 // vmsbf.m set-before-first mask bit
549 // vmsif.m set-including-first mask bit
550 // vmsof.m set-only-first mask bit
551 // EEW=1 and EMUL=(EEW/SEW)*LMUL
552 // We handle the cases when operand is a v0 mask operand above the switch,
553 // but these instructions may use non-v0 mask operands and need to be handled
554 // specifically.
555 case RISCV::VMAND_MM:
556 case RISCV::VMNAND_MM:
557 case RISCV::VMANDN_MM:
558 case RISCV::VMXOR_MM:
559 case RISCV::VMOR_MM:
560 case RISCV::VMNOR_MM:
561 case RISCV::VMORN_MM:
562 case RISCV::VMXNOR_MM:
563 case RISCV::VMSBF_M:
564 case RISCV::VMSIF_M:
565 case RISCV::VMSOF_M: {
567 }
568
569 // Vector Iota Instruction
570 // EEW=SEW and EMUL=LMUL, except the mask operand has EEW=1 and EMUL=
571 // (EEW/SEW)*LMUL. Mask operand is not handled before this switch.
572 case RISCV::VIOTA_M: {
573 if (IsMODef || MO.getOperandNo() == 1)
574 return OperandInfo(MIVLMul, MILog2SEW);
576 }
577
578 // Vector Integer Compare Instructions
579 // Dest EEW=1 and EMUL=(EEW/SEW)*LMUL. Source EEW=SEW and EMUL=LMUL.
580 case RISCV::VMSEQ_VI:
581 case RISCV::VMSEQ_VV:
582 case RISCV::VMSEQ_VX:
583 case RISCV::VMSNE_VI:
584 case RISCV::VMSNE_VV:
585 case RISCV::VMSNE_VX:
586 case RISCV::VMSLTU_VV:
587 case RISCV::VMSLTU_VX:
588 case RISCV::VMSLT_VV:
589 case RISCV::VMSLT_VX:
590 case RISCV::VMSLEU_VV:
591 case RISCV::VMSLEU_VI:
592 case RISCV::VMSLEU_VX:
593 case RISCV::VMSLE_VV:
594 case RISCV::VMSLE_VI:
595 case RISCV::VMSLE_VX:
596 case RISCV::VMSGTU_VI:
597 case RISCV::VMSGTU_VX:
598 case RISCV::VMSGT_VI:
599 case RISCV::VMSGT_VX:
600 // Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions
601 // Dest EEW=1 and EMUL=(EEW/SEW)*LMUL. Source EEW=SEW and EMUL=LMUL. Mask
602 // source operand handled above this switch.
603 case RISCV::VMADC_VIM:
604 case RISCV::VMADC_VVM:
605 case RISCV::VMADC_VXM:
606 case RISCV::VMSBC_VVM:
607 case RISCV::VMSBC_VXM:
608 // Dest EEW=1 and EMUL=(EEW/SEW)*LMUL. Source EEW=SEW and EMUL=LMUL.
609 case RISCV::VMADC_VV:
610 case RISCV::VMADC_VI:
611 case RISCV::VMADC_VX:
612 case RISCV::VMSBC_VV:
613 case RISCV::VMSBC_VX: {
614 if (IsMODef)
616 return OperandInfo(MIVLMul, MILog2SEW);
617 }
618
619 default:
620 return {};
621 }
622}
623
624/// Return true if this optimization should consider MI for VL reduction. This
625/// white-list approach simplifies this optimization for instructions that may
626/// have more complex semantics with relation to how it uses VL.
627static bool isSupportedInstr(const MachineInstr &MI) {
629 RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
630
631 if (!RVV)
632 return false;
633
634 switch (RVV->BaseInstr) {
635 // Vector Single-Width Integer Add and Subtract
636 case RISCV::VADD_VI:
637 case RISCV::VADD_VV:
638 case RISCV::VADD_VX:
639 case RISCV::VSUB_VV:
640 case RISCV::VSUB_VX:
641 case RISCV::VRSUB_VI:
642 case RISCV::VRSUB_VX:
643 // Vector Bitwise Logical Instructions
644 // Vector Single-Width Shift Instructions
645 case RISCV::VAND_VI:
646 case RISCV::VAND_VV:
647 case RISCV::VAND_VX:
648 case RISCV::VOR_VI:
649 case RISCV::VOR_VV:
650 case RISCV::VOR_VX:
651 case RISCV::VXOR_VI:
652 case RISCV::VXOR_VV:
653 case RISCV::VXOR_VX:
654 case RISCV::VSLL_VI:
655 case RISCV::VSLL_VV:
656 case RISCV::VSLL_VX:
657 case RISCV::VSRL_VI:
658 case RISCV::VSRL_VV:
659 case RISCV::VSRL_VX:
660 case RISCV::VSRA_VI:
661 case RISCV::VSRA_VV:
662 case RISCV::VSRA_VX:
663 // Vector Widening Integer Add/Subtract
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 case RISCV::VWADDU_WV:
673 case RISCV::VWADDU_WX:
674 case RISCV::VWSUBU_WV:
675 case RISCV::VWSUBU_WX:
676 case RISCV::VWADD_WV:
677 case RISCV::VWADD_WX:
678 case RISCV::VWSUB_WV:
679 case RISCV::VWSUB_WX:
680 // Vector Integer Extension
681 case RISCV::VZEXT_VF2:
682 case RISCV::VSEXT_VF2:
683 case RISCV::VZEXT_VF4:
684 case RISCV::VSEXT_VF4:
685 case RISCV::VZEXT_VF8:
686 case RISCV::VSEXT_VF8:
687 // Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions
688 // FIXME: Add support
689 case RISCV::VMADC_VV:
690 case RISCV::VMADC_VI:
691 case RISCV::VMADC_VX:
692 case RISCV::VMSBC_VV:
693 case RISCV::VMSBC_VX:
694 // Vector Narrowing Integer Right Shift Instructions
695 case RISCV::VNSRL_WX:
696 case RISCV::VNSRL_WI:
697 case RISCV::VNSRL_WV:
698 case RISCV::VNSRA_WI:
699 case RISCV::VNSRA_WV:
700 case RISCV::VNSRA_WX:
701 // Vector Integer Compare Instructions
702 case RISCV::VMSEQ_VI:
703 case RISCV::VMSEQ_VV:
704 case RISCV::VMSEQ_VX:
705 case RISCV::VMSNE_VI:
706 case RISCV::VMSNE_VV:
707 case RISCV::VMSNE_VX:
708 case RISCV::VMSLTU_VV:
709 case RISCV::VMSLTU_VX:
710 case RISCV::VMSLT_VV:
711 case RISCV::VMSLT_VX:
712 case RISCV::VMSLEU_VV:
713 case RISCV::VMSLEU_VI:
714 case RISCV::VMSLEU_VX:
715 case RISCV::VMSLE_VV:
716 case RISCV::VMSLE_VI:
717 case RISCV::VMSLE_VX:
718 case RISCV::VMSGTU_VI:
719 case RISCV::VMSGTU_VX:
720 case RISCV::VMSGT_VI:
721 case RISCV::VMSGT_VX:
722 // Vector Integer Min/Max Instructions
723 case RISCV::VMINU_VV:
724 case RISCV::VMINU_VX:
725 case RISCV::VMIN_VV:
726 case RISCV::VMIN_VX:
727 case RISCV::VMAXU_VV:
728 case RISCV::VMAXU_VX:
729 case RISCV::VMAX_VV:
730 case RISCV::VMAX_VX:
731 // Vector Single-Width Integer Multiply Instructions
732 case RISCV::VMUL_VV:
733 case RISCV::VMUL_VX:
734 case RISCV::VMULH_VV:
735 case RISCV::VMULH_VX:
736 case RISCV::VMULHU_VV:
737 case RISCV::VMULHU_VX:
738 case RISCV::VMULHSU_VV:
739 case RISCV::VMULHSU_VX:
740 // Vector Integer Divide Instructions
741 case RISCV::VDIVU_VV:
742 case RISCV::VDIVU_VX:
743 case RISCV::VDIV_VV:
744 case RISCV::VDIV_VX:
745 case RISCV::VREMU_VV:
746 case RISCV::VREMU_VX:
747 case RISCV::VREM_VV:
748 case RISCV::VREM_VX:
749 // Vector Widening Integer Multiply Instructions
750 case RISCV::VWMUL_VV:
751 case RISCV::VWMUL_VX:
752 case RISCV::VWMULSU_VV:
753 case RISCV::VWMULSU_VX:
754 case RISCV::VWMULU_VV:
755 case RISCV::VWMULU_VX:
756 // Vector Single-Width Integer Multiply-Add Instructions
757 case RISCV::VMACC_VV:
758 case RISCV::VMACC_VX:
759 case RISCV::VNMSAC_VV:
760 case RISCV::VNMSAC_VX:
761 case RISCV::VMADD_VV:
762 case RISCV::VMADD_VX:
763 case RISCV::VNMSUB_VV:
764 case RISCV::VNMSUB_VX:
765 // Vector Widening Integer Multiply-Add Instructions
766 case RISCV::VWMACCU_VV:
767 case RISCV::VWMACCU_VX:
768 case RISCV::VWMACC_VV:
769 case RISCV::VWMACC_VX:
770 case RISCV::VWMACCSU_VV:
771 case RISCV::VWMACCSU_VX:
772 case RISCV::VWMACCUS_VX:
773 // Vector Integer Merge Instructions
774 // FIXME: Add support
775 // Vector Integer Move Instructions
776 // FIXME: Add support
777 case RISCV::VMV_V_I:
778 case RISCV::VMV_V_X:
779 case RISCV::VMV_V_V:
780
781 // Vector Crypto
782 case RISCV::VWSLL_VI:
783
784 // Vector Mask Instructions
785 // Vector Mask-Register Logical Instructions
786 // vmsbf.m set-before-first mask bit
787 // vmsif.m set-including-first mask bit
788 // vmsof.m set-only-first mask bit
789 // Vector Iota Instruction
790 // Vector Element Index Instruction
791 case RISCV::VMAND_MM:
792 case RISCV::VMNAND_MM:
793 case RISCV::VMANDN_MM:
794 case RISCV::VMXOR_MM:
795 case RISCV::VMOR_MM:
796 case RISCV::VMNOR_MM:
797 case RISCV::VMORN_MM:
798 case RISCV::VMXNOR_MM:
799 case RISCV::VMSBF_M:
800 case RISCV::VMSIF_M:
801 case RISCV::VMSOF_M:
802 case RISCV::VIOTA_M:
803 case RISCV::VID_V:
804 return true;
805 }
806
807 return false;
808}
809
810/// Return true if MO is a vector operand but is used as a scalar operand.
812 MachineInstr *MI = MO.getParent();
814 RISCVVPseudosTable::getPseudoInfo(MI->getOpcode());
815
816 if (!RVV)
817 return false;
818
819 switch (RVV->BaseInstr) {
820 // Reductions only use vs1[0] of vs1
821 case RISCV::VREDAND_VS:
822 case RISCV::VREDMAX_VS:
823 case RISCV::VREDMAXU_VS:
824 case RISCV::VREDMIN_VS:
825 case RISCV::VREDMINU_VS:
826 case RISCV::VREDOR_VS:
827 case RISCV::VREDSUM_VS:
828 case RISCV::VREDXOR_VS:
829 case RISCV::VWREDSUM_VS:
830 case RISCV::VWREDSUMU_VS:
831 case RISCV::VFREDMAX_VS:
832 case RISCV::VFREDMIN_VS:
833 case RISCV::VFREDOSUM_VS:
834 case RISCV::VFREDUSUM_VS:
835 case RISCV::VFWREDOSUM_VS:
836 case RISCV::VFWREDUSUM_VS:
837 return MO.getOperandNo() == 3;
838 default:
839 return false;
840 }
841}
842
843/// Return true if MI may read elements past VL.
844static bool mayReadPastVL(const MachineInstr &MI) {
846 RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
847 if (!RVV)
848 return true;
849
850 switch (RVV->BaseInstr) {
851 // vslidedown instructions may read elements past VL. They are handled
852 // according to current tail policy.
853 case RISCV::VSLIDEDOWN_VI:
854 case RISCV::VSLIDEDOWN_VX:
855 case RISCV::VSLIDE1DOWN_VX:
856 case RISCV::VFSLIDE1DOWN_VF:
857
858 // vrgather instructions may read the source vector at any index < VLMAX,
859 // regardless of VL.
860 case RISCV::VRGATHER_VI:
861 case RISCV::VRGATHER_VV:
862 case RISCV::VRGATHER_VX:
863 case RISCV::VRGATHEREI16_VV:
864 return true;
865
866 default:
867 return false;
868 }
869}
870
871bool RISCVVLOptimizer::isCandidate(const MachineInstr &MI) const {
872 const MCInstrDesc &Desc = MI.getDesc();
873 if (!RISCVII::hasVLOp(Desc.TSFlags) || !RISCVII::hasSEWOp(Desc.TSFlags))
874 return false;
875 if (MI.getNumDefs() != 1)
876 return false;
877
878 // If we're not using VLMAX, then we need to be careful whether we are using
879 // TA/TU when there is a non-undef Passthru. But when we are using VLMAX, it
880 // does not matter whether we are using TA/TU with a non-undef Passthru, since
881 // there are no tail elements to be preserved.
882 unsigned VLOpNum = RISCVII::getVLOpNum(Desc);
883 const MachineOperand &VLOp = MI.getOperand(VLOpNum);
884 if (VLOp.isReg() || VLOp.getImm() != RISCV::VLMaxSentinel) {
885 // If MI has a non-undef passthru, we will not try to optimize it since
886 // that requires us to preserve tail elements according to TA/TU.
887 // Otherwise, The MI has an undef Passthru, so it doesn't matter whether we
888 // are using TA/TU.
889 bool HasPassthru = RISCVII::isFirstDefTiedToFirstUse(Desc);
890 unsigned PassthruOpIdx = MI.getNumExplicitDefs();
891 if (HasPassthru &&
892 MI.getOperand(PassthruOpIdx).getReg() != RISCV::NoRegister) {
894 dbgs() << " Not a candidate because it uses non-undef passthru"
895 " with non-VLMAX VL\n");
896 return false;
897 }
898 }
899
900 // If the VL is 1, then there is no need to reduce it. This is an
901 // optimization, not needed to preserve correctness.
902 if (VLOp.isImm() && VLOp.getImm() == 1) {
903 LLVM_DEBUG(dbgs() << " Not a candidate because VL is already 1\n");
904 return false;
905 }
906
907 // Some instructions that produce vectors have semantics that make it more
908 // difficult to determine whether the VL can be reduced. For example, some
909 // instructions, such as reductions, may write lanes past VL to a scalar
910 // register. Other instructions, such as some loads or stores, may write
911 // lower lanes using data from higher lanes. There may be other complex
912 // semantics not mentioned here that make it hard to determine whether
913 // the VL can be optimized. As a result, a white-list of supported
914 // instructions is used. Over time, more instructions can be supported
915 // upon careful examination of their semantics under the logic in this
916 // optimization.
917 // TODO: Use a better approach than a white-list, such as adding
918 // properties to instructions using something like TSFlags.
919 if (!isSupportedInstr(MI)) {
920 LLVM_DEBUG(dbgs() << "Not a candidate due to unsupported instruction\n");
921 return false;
922 }
923
924 LLVM_DEBUG(dbgs() << "Found a candidate for VL reduction: " << MI << "\n");
925 return true;
926}
927
928bool RISCVVLOptimizer::checkUsers(const MachineOperand *&CommonVL,
929 MachineInstr &MI) {
930 // FIXME: Avoid visiting each user for each time we visit something on the
931 // worklist, combined with an extra visit from the outer loop. Restructure
932 // along lines of an instcombine style worklist which integrates the outer
933 // pass.
934 bool CanReduceVL = true;
935 for (auto &UserOp : MRI->use_operands(MI.getOperand(0).getReg())) {
936 const MachineInstr &UserMI = *UserOp.getParent();
937 LLVM_DEBUG(dbgs() << " Checking user: " << UserMI << "\n");
938
939 // Instructions like reductions may use a vector register as a scalar
940 // register. In this case, we should treat it like a scalar register which
941 // does not impact the decision on whether to optimize VL.
942 // TODO: Treat it like a scalar register instead of bailing out.
943 if (isVectorOpUsedAsScalarOp(UserOp)) {
944 CanReduceVL = false;
945 break;
946 }
947
948 if (mayReadPastVL(UserMI)) {
949 LLVM_DEBUG(dbgs() << " Abort because used by unsafe instruction\n");
950 CanReduceVL = false;
951 break;
952 }
953
954 // Tied operands might pass through.
955 if (UserOp.isTied()) {
956 LLVM_DEBUG(dbgs() << " Abort because user used as tied operand\n");
957 CanReduceVL = false;
958 break;
959 }
960
961 const MCInstrDesc &Desc = UserMI.getDesc();
962 if (!RISCVII::hasVLOp(Desc.TSFlags) || !RISCVII::hasSEWOp(Desc.TSFlags)) {
963 LLVM_DEBUG(dbgs() << " Abort due to lack of VL or SEW, assume that"
964 " use VLMAX\n");
965 CanReduceVL = false;
966 break;
967 }
968
969 unsigned VLOpNum = RISCVII::getVLOpNum(Desc);
970 const MachineOperand &VLOp = UserMI.getOperand(VLOpNum);
971
972 // Looking for an immediate or a register VL that isn't X0.
973 assert((!VLOp.isReg() || VLOp.getReg() != RISCV::X0) &&
974 "Did not expect X0 VL");
975
976 // Use the largest VL among all the users. If we cannot determine this
977 // statically, then we cannot optimize the VL.
978 if (!CommonVL || RISCV::isVLKnownLE(*CommonVL, VLOp)) {
979 CommonVL = &VLOp;
980 LLVM_DEBUG(dbgs() << " User VL is: " << VLOp << "\n");
981 } else if (!RISCV::isVLKnownLE(VLOp, *CommonVL)) {
982 LLVM_DEBUG(dbgs() << " Abort because cannot determine a common VL\n");
983 CanReduceVL = false;
984 break;
985 }
986
987 // The SEW and LMUL of destination and source registers need to match.
988 OperandInfo ConsumerInfo = getOperandInfo(UserOp, MRI);
989 OperandInfo ProducerInfo = getOperandInfo(MI.getOperand(0), MRI);
990 if (ConsumerInfo.isUnknown() || ProducerInfo.isUnknown() ||
991 !OperandInfo::EMULAndEEWAreEqual(ConsumerInfo, ProducerInfo)) {
992 LLVM_DEBUG(dbgs() << " Abort due to incompatible or unknown "
993 "information for EMUL or EEW.\n");
994 LLVM_DEBUG(dbgs() << " ConsumerInfo is: " << ConsumerInfo << "\n");
995 LLVM_DEBUG(dbgs() << " ProducerInfo is: " << ProducerInfo << "\n");
996 CanReduceVL = false;
997 break;
998 }
999 }
1000 return CanReduceVL;
1001}
1002
1003bool RISCVVLOptimizer::tryReduceVL(MachineInstr &OrigMI) {
1005 Worklist.insert(&OrigMI);
1006
1007 bool MadeChange = false;
1008 while (!Worklist.empty()) {
1009 MachineInstr &MI = *Worklist.pop_back_val();
1010 LLVM_DEBUG(dbgs() << "Trying to reduce VL for " << MI << "\n");
1011
1012 const MachineOperand *CommonVL = nullptr;
1013 bool CanReduceVL = true;
1014 if (isVectorRegClass(MI.getOperand(0).getReg(), MRI))
1015 CanReduceVL = checkUsers(CommonVL, MI);
1016
1017 if (!CanReduceVL || !CommonVL)
1018 continue;
1019
1020 assert((CommonVL->isImm() || CommonVL->getReg().isVirtual()) &&
1021 "Expected VL to be an Imm or virtual Reg");
1022
1023 unsigned VLOpNum = RISCVII::getVLOpNum(MI.getDesc());
1024 MachineOperand &VLOp = MI.getOperand(VLOpNum);
1025
1026 if (!RISCV::isVLKnownLE(*CommonVL, VLOp)) {
1027 LLVM_DEBUG(dbgs() << " Abort due to CommonVL not <= VLOp.\n");
1028 continue;
1029 }
1030
1031 if (CommonVL->isImm()) {
1032 LLVM_DEBUG(dbgs() << " Reduce VL from " << VLOp << " to "
1033 << CommonVL->getImm() << " for " << MI << "\n");
1034 VLOp.ChangeToImmediate(CommonVL->getImm());
1035 } else {
1036 const MachineInstr *VLMI = MRI->getVRegDef(CommonVL->getReg());
1037 if (!MDT->dominates(VLMI, &MI))
1038 continue;
1039 LLVM_DEBUG(
1040 dbgs() << " Reduce VL from " << VLOp << " to "
1041 << printReg(CommonVL->getReg(), MRI->getTargetRegisterInfo())
1042 << " for " << MI << "\n");
1043
1044 // All our checks passed. We can reduce VL.
1045 VLOp.ChangeToRegister(CommonVL->getReg(), false);
1046 }
1047
1048 MadeChange = true;
1049
1050 // Now add all inputs to this instruction to the worklist.
1051 for (auto &Op : MI.operands()) {
1052 if (!Op.isReg() || !Op.isUse() || !Op.getReg().isVirtual())
1053 continue;
1054
1055 if (!isVectorRegClass(Op.getReg(), MRI))
1056 continue;
1057
1058 MachineInstr *DefMI = MRI->getVRegDef(Op.getReg());
1059
1060 if (!isCandidate(*DefMI))
1061 continue;
1062
1063 Worklist.insert(DefMI);
1064 }
1065 }
1066
1067 return MadeChange;
1068}
1069
1070bool RISCVVLOptimizer::runOnMachineFunction(MachineFunction &MF) {
1071 if (skipFunction(MF.getFunction()))
1072 return false;
1073
1074 MRI = &MF.getRegInfo();
1075 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
1076
1078 if (!ST.hasVInstructions())
1079 return false;
1080
1081 bool MadeChange = false;
1082 for (MachineBasicBlock &MBB : MF) {
1083 // Visit instructions in reverse order.
1084 for (auto &MI : make_range(MBB.rbegin(), MBB.rend())) {
1085 if (!isCandidate(MI))
1086 continue;
1087
1088 MadeChange |= tryReduceVL(MI);
1089 }
1090 }
1091
1092 return MadeChange;
1093}
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
MachineBasicBlock & MBB
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:282
#define LLVM_DEBUG(...)
Definition: Debug.h:106
IRTranslator LLVM IR MI
static bool isCandidate(const MachineInstr *MI, Register &DefedReg, Register FrameReg)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
static bool mayReadPastVL(const MachineInstr &MI)
Return true if MI may read elements past VL.
static OperandInfo getOperandInfo(const MachineOperand &MO, const MachineRegisterInfo *MRI)
Return the OperandInfo for MO.
static LLVM_ATTRIBUTE_UNUSED raw_ostream & operator<<(raw_ostream &OS, const OperandInfo &OI)
static bool isVectorOpUsedAsScalarOp(MachineOperand &MO)
Return true if MO is a vector operand but is used as a scalar operand.
static bool isVectorRegClass(Register R, const MachineRegisterInfo *MRI)
Return true if R is a physical or virtual vector register, false otherwise.
static bool isSupportedInstr(const MachineInstr &MI)
Return true if this optimization should consider MI for VL reduction.
#define PASS_NAME
#define DEBUG_TYPE
static OperandInfo getIntegerExtensionOperandInfo(unsigned Factor, const MachineInstr &MI, const MachineOperand &MO)
Dest has EEW=SEW and EMUL=LMUL.
static bool isMaskOperand(const MachineInstr &MI, const MachineOperand &MO, const MachineRegisterInfo *MRI)
Check whether MO is a mask operand of MI.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file implements a set that has insertion order iteration characteristics.
#define PASS_NAME
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
This class represents an Operation in the Expression.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
reverse_iterator rend()
reverse_iterator rbegin()
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:69
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:347
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:572
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:585
MachineOperand class - Representation of each machine instruction operand.
unsigned getOperandNo() const
Returns the index of this operand in the instruction that it belongs to.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
void ChangeToRegister(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:91
A vector that has set insertion semantics.
Definition: SetVector.h:57
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:93
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
value_type pop_back_val()
Definition: SetVector.h:285
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
const uint8_t TSFlags
Configurable target specific flags.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
static unsigned getVLOpNum(const MCInstrDesc &Desc)
static VLMUL getLMul(uint64_t TSFlags)
static bool hasVLOp(uint64_t TSFlags)
static unsigned getSEWOpNum(const MCInstrDesc &Desc)
static bool hasSEWOp(uint64_t TSFlags)
static bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc)
static bool isVRegClass(uint64_t TSFlags)
static std::pair< unsigned, bool > getEMULEqualsEEWDivSEWTimesLMUL(unsigned Log2EEW, const MachineInstr &MI)
Return EMUL = (EEW / SEW) * LMUL where EEW comes from Log2EEW and LMUL and SEW are from the TSFlags o...
std::pair< unsigned, bool > decodeVLMUL(RISCVII::VLMUL VLMUL)
static RISCVII::VLMUL twoTimesVLMUL(RISCVII::VLMUL VLMul)
Return the RISCVII::VLMUL that is two times VLMul.
bool isVLKnownLE(const MachineOperand &LHS, const MachineOperand &RHS)
Given two VL operands, do we know that LHS <= RHS?
static constexpr int64_t VLMaxSentinel
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
FunctionPass * createRISCVVLOptimizerPass()
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:340
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
Represents the EMUL and EEW of a MachineOperand.
OperandInfo(std::pair< unsigned, bool > EMUL, unsigned Log2EEW)
bool isKnown() const
void print(raw_ostream &OS) const
static bool EMULAndEEWAreEqual(const OperandInfo &A, const OperandInfo &B)
OperandInfo(RISCVII::VLMUL EMUL, unsigned Log2EEW)
bool isUnknown() const
std::optional< std::pair< unsigned, bool > > EMUL
enum OperandInfo::State S
Description of the encoding of one expression Op.