LLVM 24.0.0git
RISCVVectorPeephole.cpp
Go to the documentation of this file.
1//===- RISCVVectorPeephole.cpp - MI Vector Pseudo Peepholes ---------------===//
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 performs various vector pseudo peephole optimisations after
10// instruction selection.
11//
12// Currently it converts vmerge.vvm to vmv.v.v
13// PseudoVMERGE_VVM %false, %false, %true, %allonesmask, %vl, %sew
14// ->
15// PseudoVMV_V_V %false, %true, %vl, %sew
16//
17// And masked pseudos to unmasked pseudos
18// PseudoVADD_V_V_MASK %passthru, %a, %b, %allonesmask, %vl, sew, policy
19// ->
20// PseudoVADD_V_V %passthru %a, %b, %vl, sew, policy
21//
22// It also converts AVLs to VLMAX where possible
23// %vl = VLENB * something
24// PseudoVADD_V_V %passthru, %a, %b, %vl, sew, policy
25// ->
26// PseudoVADD_V_V %passthru, %a, %b, -1, sew, policy
27//
28//===----------------------------------------------------------------------===//
29
30#include "RISCV.h"
31#include "RISCVSubtarget.h"
37
38using namespace llvm;
39
40#define DEBUG_TYPE "riscv-vector-peephole"
41
42namespace {
43
44class RISCVVectorPeepholeImpl {
45public:
46 bool run(MachineFunction &MF);
47
48private:
49 const TargetInstrInfo *TII;
52 const RISCVSubtarget *ST;
53 bool convertToVLMAX(MachineInstr &MI) const;
54 bool convertToWholeRegister(MachineInstr &MI) const;
55 bool convertToUnmasked(MachineInstr &MI) const;
56 bool convertAllOnesVMergeToVMv(MachineInstr &MI) const;
57 bool convertSameMaskVMergeToVMv(MachineInstr &MI);
58 bool foldUndefPassthruVMV_V_V(MachineInstr &MI);
59 bool foldVMV_V_V(MachineInstr &MI);
60 bool foldVMergeToMask(MachineInstr &MI) const;
61 bool foldVMANDToMaskedCompare(MachineInstr &MI) const;
62
63 bool hasSameEEW(const MachineInstr &User, const MachineInstr &Src) const;
64 bool isAllOnesMask(const MachineInstr *MaskDef) const;
65 std::optional<unsigned> getConstant(const MachineOperand &VL) const;
66 bool ensureDominates(ArrayRef<const MachineOperand *> Defs,
67 MachineInstr &Use) const;
69 lookThruCopies(Register Reg, bool OneUseOnly = false,
71};
72
73class RISCVVectorPeepholeLegacy : public MachineFunctionPass {
74public:
75 static char ID;
76
77 RISCVVectorPeepholeLegacy() : MachineFunctionPass(ID) {}
78
79 bool runOnMachineFunction(MachineFunction &MF) override;
80 MachineFunctionProperties getRequiredProperties() const override {
81 return MachineFunctionProperties().setIsSSA();
82 }
83
84 StringRef getPassName() const override {
85 return "RISC-V Vector Peephole Optimization";
86 }
87
88 void getAnalysisUsage(AnalysisUsage &AU) const override {
89 AU.setPreservesCFG();
92 }
93};
94
95} // namespace
96
97char RISCVVectorPeepholeLegacy::ID = 0;
98
99INITIALIZE_PASS(RISCVVectorPeepholeLegacy, DEBUG_TYPE, "RISC-V Fold Masks",
100 false, false)
101
102/// Given \p User that has an input operand with EEW=SEW, which uses the dest
103/// operand of \p Src with an unknown EEW, return true if their EEWs match.
104bool RISCVVectorPeepholeImpl::hasSameEEW(const MachineInstr &User,
105 const MachineInstr &Src) const {
106 unsigned UserLog2SEW =
107 User.getOperand(RISCVII::getSEWOpNum(User.getDesc())).getImm();
108 unsigned SrcLog2SEW =
109 Src.getOperand(RISCVII::getSEWOpNum(Src.getDesc())).getImm();
110 unsigned SrcLog2EEW = RISCV::getDestLog2EEW(
111 TII->get(RISCV::getRVVMCOpcode(Src.getOpcode())), SrcLog2SEW);
112 return SrcLog2EEW == UserLog2SEW;
113}
114
115/// Check if an operand is an immediate or a materialized ADDI $x0, imm.
116std::optional<unsigned>
117RISCVVectorPeepholeImpl::getConstant(const MachineOperand &VL) const {
118 if (VL.isImm())
119 return VL.getImm();
120
121 if (!VL.getReg().isVirtual())
122 return std::nullopt;
123 MachineInstr *Def = MRI->getVRegDef(VL.getReg());
124 if (!Def || Def->getOpcode() != RISCV::ADDI || !Def->getOperand(1).isReg() ||
125 Def->getOperand(1).getReg() != RISCV::X0)
126 return std::nullopt;
127 return Def->getOperand(2).getImm();
128}
129
130/// Convert AVLs that are known to be VLMAX to the VLMAX sentinel.
131bool RISCVVectorPeepholeImpl::convertToVLMAX(MachineInstr &MI) const {
132 if (!RISCVII::hasVLOp(MI.getDesc().TSFlags) ||
133 !RISCVII::hasSEWOp(MI.getDesc().TSFlags))
134 return false;
135
136 auto LMUL = RISCVVType::decodeVLMUL(RISCVII::getLMul(MI.getDesc().TSFlags));
137 // Fixed-point value, denominator=8
138 unsigned LMULFixed = LMUL.second ? (8 / LMUL.first) : 8 * LMUL.first;
139 unsigned Log2SEW = MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
140 // A Log2SEW of 0 is an operation on mask registers only
141 unsigned SEW = Log2SEW ? 1 << Log2SEW : 8;
142 assert(RISCVVType::isValidSEW(SEW) && "Unexpected SEW");
143 assert(8 * LMULFixed / SEW > 0);
144
145 // If the exact VLEN is known then we know VLMAX, check if the AVL == VLMAX.
146 MachineOperand &VL = MI.getOperand(RISCVII::getVLOpNum(MI.getDesc()));
147 if (auto VLen = ST->getRealVLen(), AVL = getConstant(VL);
148 VLen && AVL && (*VLen * LMULFixed) / SEW == *AVL * 8) {
150 return true;
151 }
152
153 // If an AVL is a VLENB that's possibly scaled to be equal to VLMAX, convert
154 // it to the VLMAX sentinel value.
155 if (!VL.isReg())
156 return false;
157 MachineInstr *Def = MRI->getVRegDef(VL.getReg());
158 if (!Def)
159 return false;
160
161 // Fixed-point value, denominator=8
162 uint64_t ScaleFixed = 8;
163 // Check if the VLENB was potentially scaled with slli/srli
164 if (Def->getOpcode() == RISCV::SLLI) {
165 assert(Def->getOperand(2).getImm() < 64);
166 ScaleFixed <<= Def->getOperand(2).getImm();
167 Def = MRI->getVRegDef(Def->getOperand(1).getReg());
168 } else if (Def->getOpcode() == RISCV::SRLI) {
169 assert(Def->getOperand(2).getImm() < 64);
170 ScaleFixed >>= Def->getOperand(2).getImm();
171 Def = MRI->getVRegDef(Def->getOperand(1).getReg());
172 }
173
174 if (!Def || Def->getOpcode() != RISCV::PseudoReadVLENB)
175 return false;
176
177 // AVL = (VLENB * Scale)
178 //
179 // VLMAX = (VLENB * 8 * LMUL) / SEW
180 //
181 // AVL == VLMAX
182 // -> VLENB * Scale == (VLENB * 8 * LMUL) / SEW
183 // -> Scale == (8 * LMUL) / SEW
184 if (ScaleFixed != 8 * LMULFixed / SEW)
185 return false;
186
188
189 return true;
190}
191
192bool RISCVVectorPeepholeImpl::isAllOnesMask(const MachineInstr *MaskDef) const {
193 while (MaskDef->isCopy() && MaskDef->getOperand(1).getReg().isVirtual())
194 MaskDef = MRI->getVRegDef(MaskDef->getOperand(1).getReg());
195
196 // TODO: Check that the VMSET is the expected bitwidth? The pseudo has
197 // undefined behaviour if it's the wrong bitwidth, so we could choose to
198 // assume that it's all-ones? Same applies to its VL.
199 switch (MaskDef->getOpcode()) {
200 case RISCV::PseudoVMSET_M_B1:
201 case RISCV::PseudoVMSET_M_B2:
202 case RISCV::PseudoVMSET_M_B4:
203 case RISCV::PseudoVMSET_M_B8:
204 case RISCV::PseudoVMSET_M_B16:
205 case RISCV::PseudoVMSET_M_B32:
206 case RISCV::PseudoVMSET_M_B64:
207 return true;
208 default:
209 return false;
210 }
211}
212
213/// Convert unit strided unmasked loads and stores to whole-register equivalents
214/// to avoid the dependency on $vl and $vtype.
215///
216/// %x = PseudoVLE8_V_M1 %passthru, %ptr, %vlmax, policy
217/// PseudoVSE8_V_M1 %v, %ptr, %vlmax
218///
219/// ->
220///
221/// %x = VL1RE8_V %ptr
222/// VS1R_V %v, %ptr
223bool RISCVVectorPeepholeImpl::convertToWholeRegister(MachineInstr &MI) const {
224#define CASE_WHOLE_REGISTER_LMUL_SEW(lmul, sew) \
225 case RISCV::PseudoVLE##sew##_V_M##lmul: \
226 NewOpc = RISCV::VL##lmul##RE##sew##_V; \
227 break; \
228 case RISCV::PseudoVSE##sew##_V_M##lmul: \
229 NewOpc = RISCV::VS##lmul##R_V; \
230 break;
231#define CASE_WHOLE_REGISTER_LMUL(lmul) \
232 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 8) \
233 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 16) \
234 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 32) \
235 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 64)
236
237 unsigned NewOpc;
238 switch (MI.getOpcode()) {
243 default:
244 return false;
245 }
246
247 MachineOperand &VLOp = MI.getOperand(RISCVII::getVLOpNum(MI.getDesc()));
248 if (!VLOp.isImm() || VLOp.getImm() != RISCV::VLMaxSentinel)
249 return false;
250
251 // Whole register instructions aren't pseudos so they don't have
252 // policy/SEW/AVL ops, and they don't have passthrus.
253 if (RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags))
254 MI.removeOperand(RISCVII::getVecPolicyOpNum(MI.getDesc()));
255 MI.removeOperand(RISCVII::getSEWOpNum(MI.getDesc()));
256 MI.removeOperand(RISCVII::getVLOpNum(MI.getDesc()));
257 if (RISCVII::isFirstDefTiedToFirstUse(MI.getDesc()))
258 MI.removeOperand(1);
259
260 MI.setDesc(TII->get(NewOpc));
261
262 return true;
263}
264
265static unsigned getVMV_V_VOpcodeForVMERGE_VVM(const MachineInstr &MI) {
266#define CASE_VMERGE_TO_VMV(lmul) \
267 case RISCV::PseudoVMERGE_VVM_##lmul: \
268 return RISCV::PseudoVMV_V_V_##lmul;
269 switch (MI.getOpcode()) {
270 default:
271 return 0;
272 CASE_VMERGE_TO_VMV(MF8)
273 CASE_VMERGE_TO_VMV(MF4)
274 CASE_VMERGE_TO_VMV(MF2)
275 CASE_VMERGE_TO_VMV(M1)
276 CASE_VMERGE_TO_VMV(M2)
277 CASE_VMERGE_TO_VMV(M4)
278 CASE_VMERGE_TO_VMV(M8)
279 }
280}
281
282/// Convert a PseudoVMERGE_VVM with an all ones mask to a PseudoVMV_V_V.
283///
284/// %x = PseudoVMERGE_VVM %passthru, %false, %true, %allones, sew, vl
285/// ->
286/// %x = PseudoVMV_V_V %passthru, %true, vl, sew, tu_mu
287bool RISCVVectorPeepholeImpl::convertAllOnesVMergeToVMv(
288 MachineInstr &MI) const {
289 unsigned NewOpc = getVMV_V_VOpcodeForVMERGE_VVM(MI);
290 if (!NewOpc)
291 return false;
292 if (!isAllOnesMask(MRI->getVRegDef(MI.getOperand(4).getReg())))
293 return false;
294
295 MI.setDesc(TII->get(NewOpc));
296 MI.removeOperand(2); // False operand
297 MI.removeOperand(3); // Mask operand
298 MI.addOperand(
300
301 // vmv.v.v doesn't have a mask operand, so we may be able to inflate the
302 // register class for the destination and passthru operands e.g. VRNoV0 -> VR
303 MRI->recomputeRegClass(MI.getOperand(0).getReg());
304 if (MI.getOperand(1).getReg().isValid())
305 MRI->recomputeRegClass(MI.getOperand(1).getReg());
306 return true;
307}
308
309// If \p Reg is defined by one or more COPYs of virtual registers, traverses
310// the chain and returns the root non-COPY source.
311Register RISCVVectorPeepholeImpl::lookThruCopies(
312 Register Reg, bool OneUseOnly,
313 SmallVectorImpl<MachineInstr *> *Copies) const {
314 while (MachineInstr *Def = MRI->getUniqueVRegDef(Reg)) {
315 if (!Def->isFullCopy())
316 break;
317 Register Src = Def->getOperand(1).getReg();
318 if (!Src.isVirtual())
319 break;
320 if (OneUseOnly && !MRI->hasOneNonDBGUse(Reg))
321 break;
322 if (Copies)
323 Copies->push_back(Def);
324 Reg = Src;
325 }
326 return Reg;
327}
328
329/// If a PseudoVMERGE_VVM's true operand is a masked pseudo and both have the
330/// same mask, and the masked pseudo's passthru is the same as the false
331/// operand, we can convert the PseudoVMERGE_VVM to a PseudoVMV_V_V.
332///
333/// %true = PseudoVADD_VV_M1_MASK %false, %x, %y, %mask, vl1, sew, policy
334/// %x = PseudoVMERGE_VVM %passthru, %false, %true, %mask, vl2, sew
335/// ->
336/// %true = PseudoVADD_VV_M1_MASK %false, %x, %y, %mask, vl1, sew, policy
337/// %x = PseudoVMV_V_V %passthru, %true, vl2, sew, tu_mu
338bool RISCVVectorPeepholeImpl::convertSameMaskVMergeToVMv(MachineInstr &MI) {
339 unsigned NewOpc = getVMV_V_VOpcodeForVMERGE_VVM(MI);
340 if (!NewOpc)
341 return false;
342 MachineInstr *True = MRI->getVRegDef(MI.getOperand(3).getReg());
343
344 if (!True || True->getParent() != MI.getParent())
345 return false;
346
347 auto *TrueMaskedInfo = RISCV::getMaskedPseudoInfo(True->getOpcode());
348 if (!TrueMaskedInfo || !hasSameEEW(MI, *True))
349 return false;
350
351 Register TrueMaskReg = lookThruCopies(
352 True->getOperand(TrueMaskedInfo->MaskOpIdx + True->getNumExplicitDefs())
353 .getReg());
354 Register MIMaskReg = lookThruCopies(MI.getOperand(4).getReg());
355 if (!TrueMaskReg.isVirtual() || TrueMaskReg != MIMaskReg)
356 return false;
357
358 // Masked off lanes past TrueVL will come from False, and converting to vmv
359 // will lose these lanes unless MIVL <= TrueVL.
360 // We can relax this when False == Passthru and True's tail policy is TU,
361 // because True's tail lanes will preserve its passthru (= False = Passthru).
362 const MachineOperand &MIVL = MI.getOperand(RISCVII::getVLOpNum(MI.getDesc()));
363 const MachineOperand &TrueVL =
364 True->getOperand(RISCVII::getVLOpNum(True->getDesc()));
365 Register FalseReg = MI.getOperand(2).getReg();
366 if (!RISCV::isVLKnownLE(*MRI, MIVL, TrueVL)) {
367 Register PassthruReg = MI.getOperand(1).getReg();
368 if (FalseReg.isValid() && FalseReg != PassthruReg)
369 return false;
370 if (!RISCVII::hasVecPolicyOp(True->getDesc().TSFlags))
371 return false;
372 uint64_t TruePolicy =
373 True->getOperand(RISCVII::getVecPolicyOpNum(True->getDesc())).getImm();
374 if (TruePolicy & RISCVVType::TAIL_AGNOSTIC)
375 return false;
376 }
377
378 // True's passthru needs to be equivalent to False
379 Register TruePassthruReg = True->getOperand(1).getReg();
380 if (TruePassthruReg != FalseReg) {
381 // If True's passthru is undef see if we can change it to False
382 if (TruePassthruReg.isValid() ||
383 !MRI->hasOneNonDBGUse(MI.getOperand(3).getReg()) ||
384 !ensureDominates(&MI.getOperand(2), *True))
385 return false;
386 True->getOperand(1).setReg(MI.getOperand(2).getReg());
387 // If True is masked then its passthru needs to be in VRNoV0.
388 MRI->constrainRegClass(True->getOperand(1).getReg(),
389 TII->getRegClass(True->getDesc(), 1));
390 }
391
392 // If True is mask agnostic, we need to make it mask undisturbed.
393 if (RISCVII::hasVecPolicyOp(True->getDesc().TSFlags)) {
394 MachineOperand &PolicyOp =
395 True->getOperand(RISCVII::getVecPolicyOpNum(True->getDesc()));
396 PolicyOp.setImm(PolicyOp.getImm() & ~RISCVVType::MASK_AGNOSTIC);
397 }
398
399 MI.setDesc(TII->get(NewOpc));
400 MI.removeOperand(2); // False operand
401 MI.removeOperand(3); // Mask operand
402 MI.addOperand(
404
405 // vmv.v.v doesn't have a mask operand, so we may be able to inflate the
406 // register class for the destination and passthru operands e.g. VRNoV0 -> VR
407 MRI->recomputeRegClass(MI.getOperand(0).getReg());
408 if (MI.getOperand(1).getReg().isValid())
409 MRI->recomputeRegClass(MI.getOperand(1).getReg());
410 return true;
411}
412
413bool RISCVVectorPeepholeImpl::convertToUnmasked(MachineInstr &MI) const {
414 const RISCV::RISCVMaskedPseudoInfo *I =
415 RISCV::getMaskedPseudoInfo(MI.getOpcode());
416 if (!I)
417 return false;
418
419 if (!isAllOnesMask(MRI->getVRegDef(
420 MI.getOperand(I->MaskOpIdx + MI.getNumExplicitDefs()).getReg())))
421 return false;
422
423 // There are two classes of pseudos in the table - compares and
424 // everything else. See the comment on RISCVMaskedPseudo for details.
425 const unsigned Opc = I->UnmaskedPseudo;
426 const MCInstrDesc &MCID = TII->get(Opc);
427 [[maybe_unused]] const bool HasPolicyOp =
429 const bool HasPassthru = RISCVII::isFirstDefTiedToFirstUse(MCID);
430 const MCInstrDesc &MaskedMCID = TII->get(MI.getOpcode());
433 "Unmasked pseudo has policy but masked pseudo doesn't?");
434 assert(HasPolicyOp == HasPassthru && "Unexpected pseudo structure");
435 assert(!(HasPassthru && !RISCVII::isFirstDefTiedToFirstUse(MaskedMCID)) &&
436 "Unmasked with passthru but masked with no passthru?");
437 (void)HasPolicyOp;
438
439 MI.setDesc(MCID);
440
441 // Drop the policy operand if unmasked doesn't need it.
442 if (RISCVII::hasVecPolicyOp(MaskedMCID.TSFlags) &&
444 MI.removeOperand(RISCVII::getVecPolicyOpNum(MaskedMCID));
445
446 // TODO: Increment all MaskOpIdxs in tablegen by num of explicit defs?
447 unsigned MaskOpIdx = I->MaskOpIdx + MI.getNumExplicitDefs();
448 MI.removeOperand(MaskOpIdx);
449
450 // The unmasked pseudo will no longer be constrained to the vrnov0 reg class,
451 // so try and relax it to vr.
452 MRI->recomputeRegClass(MI.getOperand(0).getReg());
453
454 // If the original masked pseudo had a passthru, relax it or remove it.
455 if (RISCVII::isFirstDefTiedToFirstUse(MaskedMCID)) {
456 unsigned PassthruOpIdx = MI.getNumExplicitDefs();
457 if (HasPassthru) {
458 if (MI.getOperand(PassthruOpIdx).getReg())
459 MRI->recomputeRegClass(MI.getOperand(PassthruOpIdx).getReg());
460 } else
461 MI.removeOperand(PassthruOpIdx);
462 }
463
464 return true;
465}
466
467/// Given A and B are in the same MBB, returns true if A comes before B.
470 assert(A->getParent() == B->getParent());
471 if (A == B)
472 return false;
473 const MachineBasicBlock *MBB = A->getParent();
474 auto MBBEnd = MBB->end();
475 if (B == MBBEnd)
476 return true;
477
479 for (; &*I != A && &*I != B; ++I)
480 ;
481
482 return &*I == A;
483}
484
485/// If a register in \p Defs doesn't dominate \p Use, try to move Use so it
486/// does. Returns false if any def doesn't dominate and we can't move Use. Each
487/// def must be in the same block as Use.
488bool RISCVVectorPeepholeImpl::ensureDominates(
489 ArrayRef<const MachineOperand *> Defs, MachineInstr &Use) const {
490 MachineInstr *Dest = &Use;
491
492 for (const MachineOperand *MO : Defs) {
493 assert(MO->getParent()->getParent() == Use.getParent());
494 if (!MO->isReg() || !MO->getReg().isValid())
495 continue;
496 if (MO->getReg().isPhysical())
497 return false;
498
499 MachineInstr *Def = MRI->getVRegDef(MO->getReg());
500 if (!Def)
501 continue;
502
503 if (Def->getParent() == Dest->getParent() &&
504 !strictlyDominates(Def, *Dest)) {
505 if (!RISCVInstrInfo::isSafeToMove(*Dest, *Def->getNextNode()))
506 return false;
507 Dest = Def->getNextNode();
508 }
509 }
510
511 if (Dest != &Use)
512 Use.moveBefore(Dest);
513
514 return true;
515}
516
517/// If a PseudoVMV_V_V's passthru is undef then we can replace it with its input
518bool RISCVVectorPeepholeImpl::foldUndefPassthruVMV_V_V(MachineInstr &MI) {
519 if (RISCV::getRVVMCOpcode(MI.getOpcode()) != RISCV::VMV_V_V)
520 return false;
521 if (MI.getOperand(1).getReg().isValid())
522 return false;
523
524 // If the input was a pseudo with a policy operand, we can give it a tail
525 // agnostic policy if MI's undef tail subsumes the input's.
526 MachineInstr *Src = MRI->getVRegDef(MI.getOperand(2).getReg());
527 if (Src && !Src->hasUnmodeledSideEffects() &&
528 MRI->hasOneNonDBGUse(MI.getOperand(2).getReg()) &&
529 RISCVII::hasVLOp(Src->getDesc().TSFlags) &&
530 RISCVII::hasVecPolicyOp(Src->getDesc().TSFlags) && hasSameEEW(MI, *Src)) {
531 const MachineOperand &MIVL = MI.getOperand(3);
532 const MachineOperand &SrcVL =
533 Src->getOperand(RISCVII::getVLOpNum(Src->getDesc()));
534
535 MachineOperand &SrcPolicy =
536 Src->getOperand(RISCVII::getVecPolicyOpNum(Src->getDesc()));
537
538 if (RISCV::isVLKnownLE(*MRI, MIVL, SrcVL))
539 SrcPolicy.setImm(SrcPolicy.getImm() | RISCVVType::TAIL_AGNOSTIC);
540 }
541
542 MRI->constrainRegClass(MI.getOperand(2).getReg(),
543 MRI->getRegClass(MI.getOperand(0).getReg()));
544 MRI->replaceRegWith(MI.getOperand(0).getReg(), MI.getOperand(2).getReg());
545 MI.eraseFromParent();
546 return true;
547}
548
549/// If a PseudoVMV_V_V is the only non-debug user of its input, fold its
550/// passthru and VL into it.
551///
552/// %x = PseudoVADD_V_V_M1 %passthru, %a, %b, %vl1, sew, policy
553/// %y = PseudoVMV_V_V_M1 %passthru, %x, %vl2, sew, policy
554/// (where %vl1 <= %vl2)
555///
556/// ->
557///
558/// %y = PseudoVADD_V_V_M1 %passthru, %a, %b, vl1, sew, policy
559bool RISCVVectorPeepholeImpl::foldVMV_V_V(MachineInstr &MI) {
560 if (RISCV::getRVVMCOpcode(MI.getOpcode()) != RISCV::VMV_V_V)
561 return false;
562
563 MachineOperand &Passthru = MI.getOperand(1);
564
565 if (!MRI->hasOneNonDBGUse(MI.getOperand(2).getReg()))
566 return false;
567
568 MachineInstr *Src = MRI->getVRegDef(MI.getOperand(2).getReg());
569 if (!Src || Src->hasUnmodeledSideEffects() ||
570 Src->getParent() != MI.getParent() ||
571 !RISCVII::isFirstDefTiedToFirstUse(Src->getDesc()) ||
572 !RISCVII::hasVLOp(Src->getDesc().TSFlags))
573 return false;
574
575 // Src's dest needs to have the same EEW as MI's input.
576 if (!hasSameEEW(MI, *Src))
577 return false;
578
579 std::optional<std::pair<unsigned, unsigned>> NeedsCommute;
580
581 // Src needs to have the same passthru as VMV_V_V
582 MachineOperand &SrcPassthru = Src->getOperand(Src->getNumExplicitDefs());
583 if (SrcPassthru.getReg().isValid() &&
584 SrcPassthru.getReg() != Passthru.getReg()) {
585 // If Src's passthru != Passthru, check if it uses Passthru in another
586 // operand and try to commute it.
587 int OtherIdx = Src->findRegisterUseOperandIdx(Passthru.getReg(), TRI);
588 if (OtherIdx == -1)
589 return false;
590 unsigned OpIdx1 = OtherIdx;
591 unsigned OpIdx2 = Src->getNumExplicitDefs();
592 if (!TII->findCommutedOpIndices(*Src, OpIdx1, OpIdx2))
593 return false;
594 NeedsCommute = {OpIdx1, OpIdx2};
595 }
596
597 // Src VL will have already been reduced if legal by RISCVVLOptimizer,
598 // so we don't need to handle a smaller source VL here. However, the
599 // user's VL may be larger
600 MachineOperand &SrcVL = Src->getOperand(RISCVII::getVLOpNum(Src->getDesc()));
601 if (!RISCV::isVLKnownLE(*MRI, SrcVL, MI.getOperand(3)))
602 return false;
603
604 // If the new passthru doesn't dominate Src, try to move Src so it does.
605 if (!ensureDominates(&Passthru, *Src))
606 return false;
607
608 if (NeedsCommute) {
609 auto [OpIdx1, OpIdx2] = *NeedsCommute;
610 [[maybe_unused]] bool Commuted =
611 TII->commuteInstruction(*Src, /*NewMI=*/false, OpIdx1, OpIdx2);
612 assert(Commuted && "Failed to commute Src?");
613 }
614
615 if (SrcPassthru.getReg() != Passthru.getReg()) {
616 SrcPassthru.setReg(Passthru.getReg());
617 // If Src is masked then its passthru needs to be in VRNoV0.
618 if (Passthru.getReg().isValid())
620 Passthru.getReg(),
621 TII->getRegClass(Src->getDesc(), SrcPassthru.getOperandNo()));
622 }
623
624 if (RISCVII::hasVecPolicyOp(Src->getDesc().TSFlags)) {
625 // If MI was tail agnostic and the VL didn't increase, preserve it.
627 if ((MI.getOperand(5).getImm() & RISCVVType::TAIL_AGNOSTIC) &&
628 RISCV::isVLKnownLE(*MRI, MI.getOperand(3), SrcVL))
630 Src->getOperand(RISCVII::getVecPolicyOpNum(Src->getDesc())).setImm(Policy);
631 }
632
633 MRI->constrainRegClass(Src->getOperand(0).getReg(),
634 MRI->getRegClass(MI.getOperand(0).getReg()));
635 MRI->replaceRegWith(MI.getOperand(0).getReg(), Src->getOperand(0).getReg());
636 MI.eraseFromParent();
637
638 return true;
639}
640
641/// Try to fold away VMERGE_VVM instructions into their operands:
642///
643/// %true = PseudoVADD_VV ...
644/// %x = PseudoVMERGE_VVM_M1 %false, %false, %true, %mask
645/// ->
646/// %x = PseudoVADD_VV_M1_MASK %false, ..., %mask
647///
648/// We can only fold if vmerge's passthru operand, vmerge's false operand and
649/// %true's passthru operand (if it has one) are the same. This is because we
650/// have to consolidate them into one passthru operand in the result.
651///
652/// If %true is masked, then we can use its mask instead of vmerge's if vmerge's
653/// mask is all ones.
654///
655/// The resulting VL is the minimum of the two VLs.
656///
657/// The resulting policy is the effective policy the vmerge would have had,
658/// i.e. whether or not it's passthru operand was implicit-def.
659bool RISCVVectorPeepholeImpl::foldVMergeToMask(MachineInstr &MI) const {
660 if (RISCV::getRVVMCOpcode(MI.getOpcode()) != RISCV::VMERGE_VVM)
661 return false;
662
663 // Collect chain of COPYs on True's result for later cleanup.
664 SmallVector<MachineInstr *, 4> TrueCopies;
665 Register PassthruReg = lookThruCopies(MI.getOperand(1).getReg());
666 const MachineOperand &FalseOp = MI.getOperand(2);
667 Register FalseReg = lookThruCopies(FalseOp.getReg());
668 Register TrueReg = lookThruCopies(MI.getOperand(3).getReg(),
669 /*OneUseOnly=*/true, &TrueCopies);
670 if (!TrueReg.isVirtual() || !MRI->hasOneNonDBGUse(TrueReg))
671 return false;
672 MachineInstr *TrueDef = MRI->getVRegDef(TrueReg);
673 if (!TrueDef)
674 return false;
675 MachineInstr &True = *TrueDef;
676 if (True.getParent() != MI.getParent())
677 return false;
678 const MachineOperand &MaskOp = MI.getOperand(4);
679 MachineInstr *Mask = MRI->getUniqueVRegDef(MaskOp.getReg());
680 assert(Mask);
681
682 const RISCV::RISCVMaskedPseudoInfo *Info =
683 RISCV::lookupMaskedIntrinsicByUnmasked(True.getOpcode());
684 if (!Info)
685 return false;
686
687 // If the EEW of True is different from vmerge's SEW, then we can't fold.
688 if (!hasSameEEW(MI, True))
689 return false;
690
691 // We require that either passthru and false are the same, or that passthru
692 // is undefined.
693 if (PassthruReg && !(PassthruReg.isVirtual() && PassthruReg == FalseReg))
694 return false;
695
696 std::optional<std::pair<unsigned, unsigned>> NeedsCommute;
697
698 // If True has a passthru operand then it needs to be the same as vmerge's
699 // False, since False will be used for the result's passthru operand.
700 Register TruePassthru;
702 TruePassthru =
703 lookThruCopies(True.getOperand(True.getNumExplicitDefs()).getReg());
704 if (TruePassthru && !(TruePassthru.isVirtual() && TruePassthru == FalseReg)) {
705 // If True's passthru != False, check if it uses False in another operand
706 // and try to commute it.
707 int OtherIdx = True.findRegisterUseOperandIdx(FalseReg, TRI);
708 if (OtherIdx == -1)
709 return false;
710 unsigned OpIdx1 = OtherIdx;
711 unsigned OpIdx2 = True.getNumExplicitDefs();
712 if (!TII->findCommutedOpIndices(True, OpIdx1, OpIdx2))
713 return false;
714 NeedsCommute = {OpIdx1, OpIdx2};
715 }
716
717 // Make sure it doesn't raise any observable fp exceptions, since changing the
718 // active elements will affect how fflags is set.
719 if (True.hasUnmodeledSideEffects() || True.mayRaiseFPException())
720 return false;
721
722 const MachineOperand &VMergeVL =
723 MI.getOperand(RISCVII::getVLOpNum(MI.getDesc()));
724 const MachineOperand &TrueVL =
725 True.getOperand(RISCVII::getVLOpNum(True.getDesc()));
726
727 MachineOperand MinVL = MachineOperand::CreateImm(0);
728 if (RISCV::isVLKnownLE(*MRI, TrueVL, VMergeVL))
729 MinVL = TrueVL;
730 else if (RISCV::isVLKnownLE(*MRI, VMergeVL, TrueVL))
731 MinVL = VMergeVL;
732 else if (!TruePassthru && !True.mayLoadOrStore())
733 // If True's passthru is undef, we can use vmerge's vl.
734 MinVL = VMergeVL;
735 else
736 return false;
737
738 unsigned RVVOpc = RISCV::getRVVMCOpcode(True.getOpcode());
739 unsigned RVVTSFlags = TII->get(RVVOpc).TSFlags;
740 if (RISCVII::elementsDependOnVL(RVVTSFlags) && !TrueVL.isIdenticalTo(MinVL))
741 return false;
742 if (RISCVII::elementsDependOnMask(RVVTSFlags) && !isAllOnesMask(Mask))
743 return false;
744
745 // Use a tumu policy, relaxing it to tail agnostic provided that the passthru
746 // operand is undefined.
747 //
748 // However, if the VL became smaller than what the vmerge had originally, then
749 // elements past VL that were previously in the vmerge's body will have moved
750 // to the tail. In that case we always need to use tail undisturbed to
751 // preserve them.
753 if (!PassthruReg && RISCV::isVLKnownLE(*MRI, VMergeVL, MinVL))
755
756 assert(RISCVII::hasVecPolicyOp(True.getDesc().TSFlags) &&
757 "Foldable unmasked pseudo should have a policy op already");
758
759 // Make sure Mask, False and MinVL dominate True and its copies, otherwise
760 // move down True so it does.
761 if (!ensureDominates({&MaskOp, &FalseOp, &MinVL}, True))
762 return false;
763
764 if (NeedsCommute) {
765 auto [OpIdx1, OpIdx2] = *NeedsCommute;
766 [[maybe_unused]] bool Commuted =
767 TII->commuteInstruction(True, /*NewMI=*/false, OpIdx1, OpIdx2);
768 assert(Commuted && "Failed to commute True?");
769 Info = RISCV::lookupMaskedIntrinsicByUnmasked(True.getOpcode());
770 }
771
772 True.setDesc(TII->get(Info->MaskedPseudo));
773
774 // Insert the mask operand.
775 // TODO: Increment MaskOpIdx by number of explicit defs?
776 True.insert(True.operands_begin() + Info->MaskOpIdx +
777 True.getNumExplicitDefs(),
778 MachineOperand::CreateReg(MaskOp.getReg(), false));
779
780 // Update the passthru, AVL and policy.
781 MachineOperand &TruePassthruOp = True.getOperand(True.getNumExplicitDefs());
782 TruePassthruOp.setReg(FalseReg);
783 TruePassthruOp.setIsUndef(FalseOp.isUndef());
784 True.removeOperand(RISCVII::getVLOpNum(True.getDesc()));
785 True.insert(True.operands_begin() + RISCVII::getVLOpNum(True.getDesc()),
786 MinVL);
787 True.getOperand(RISCVII::getVecPolicyOpNum(True.getDesc())).setImm(Policy);
788
789 MRI->replaceRegWith(True.getOperand(0).getReg(), MI.getOperand(0).getReg());
790 // Now that True is masked, constrain its operands from vr -> vrnov0.
791 for (MachineOperand &MO : True.explicit_operands()) {
792 if (!MO.isReg() || !MO.getReg().isVirtual())
793 continue;
795 MO.getReg(), True.getRegClassConstraint(MO.getOperandNo(), TII, TRI));
796 }
797 MI.eraseFromParent();
798
799 // Cleanup all the COPYs on True's value. We have to manually do this because
800 // sometimes sinking True causes these COPY to be invalid (use before define).
801 for (MachineInstr *TrueCopy : TrueCopies)
802 TrueCopy->eraseFromParent();
803
804 return true;
805}
806
807/// Fold a mask-register AND of a mask comparison into a mask-undisturbed
808/// masked comparison, saving an instruction:
809///
810/// %cmp1 = PseudoVMSLT_VV_M1 %a, %b, %vl, %sew
811/// %cmp2 = PseudoVMSLT_VV_M1 %c, %d, %vl, %sew
812/// %and = PseudoVMAND_MM %cmp1, %cmp2, %vl, 0
813/// ->
814/// %cmp1 = PseudoVMSLT_VV_M1 %a, %b, %vl, %sew
815/// %and = PseudoVMSLT_VV_M1_MASK %cmp1, %c, %d, %cmp1, %vl, %sew, mu
816///
817/// This works because for a mask-undisturbed masked compare whose passthru is
818/// the same register as its mask %m, the result is %m[i] ? (c cmp d)[i] :
819/// %m[i], which is exactly %m[i] & (c cmp d)[i], i.e. vmand(%m, vmscmp(c, d)).
820///
821/// Since vmand is commutative it's enough for either operand to be a foldable
822/// comparison; the other operand becomes both the mask and the passthru.
823bool RISCVVectorPeepholeImpl::foldVMANDToMaskedCompare(MachineInstr &MI) const {
824 if (RISCV::getRVVMCOpcode(MI.getOpcode()) != RISCV::VMAND_MM)
825 return false;
826
827 // The masked comparison we create needs its mask (and passthru) in v0, which
828 // the original vmand did not require. If the vmand's result has more than one
829 // use then it is an interior mask value rather than a final result feeding
830 // v0, and introducing the v0 requirement tends to add vmv1r.v moves. Only
831 // fold results with a single non-debug use, where the value coalesces onto
832 // v0 for free.
833 if (!MRI->hasOneNonDBGUse(MI.getOperand(0).getReg()))
834 return false;
835
836 // Try each operand as the comparison to be masked; the other becomes the
837 // mask/passthru.
838 for (unsigned CmpIdx : {1, 2}) {
839 unsigned MaskIdx = CmpIdx == 1 ? 2 : 1;
840
841 // The comparison must have a single non-debug use so that folding it into
842 // MI doesn't leave an extra unmasked comparison behind.
843 SmallVector<MachineInstr *, 4> CmpCopies;
844 Register CmpReg = lookThruCopies(MI.getOperand(CmpIdx).getReg(),
845 /*OneUseOnly=*/true, &CmpCopies);
846 if (!CmpReg.isVirtual() || !MRI->hasOneNonDBGUse(CmpReg))
847 continue;
848 MachineInstr &Cmp = *MRI->getUniqueVRegDef(CmpReg);
849 if (Cmp.getParent() != MI.getParent())
850 continue;
851 if (!RISCVInstrInfo::isRVVCompare(Cmp))
852 continue;
853
854 // Find the masked pseudo corresponding to the unmasked comparison.
855 const RISCV::RISCVMaskedPseudoInfo *Info =
856 RISCV::lookupMaskedIntrinsicByUnmasked(Cmp.getOpcode());
857 if (!Info)
858 continue;
859
860 // The EEW of the comparison's dest must match vmand's SEW.
861 if (!hasSameEEW(MI, Cmp))
862 continue;
863
864 // Masking restricts the comparison to the mask's active elements, so any FP
865 // exceptions raised on inactive elements would be lost.
866 if (Cmp.hasUnmodeledSideEffects() || Cmp.mayRaiseFPException())
867 continue;
868
869 // All active elements of vmand must also be active in the comparison. If
870 // the comparison's VL were smaller, elements in between the two VLs would
871 // become tail elements of the masked comparison and could not be preserved
872 // from the mask because mask results are always tail agnostic.
873 const MachineOperand &CmpVL =
874 Cmp.getOperand(RISCVII::getVLOpNum(Cmp.getDesc()));
875 const MachineOperand &MIVL =
876 MI.getOperand(RISCVII::getVLOpNum(MI.getDesc()));
877 if (!RISCV::isVLKnownLE(*MRI, MIVL, CmpVL))
878 continue;
879
880 const MachineOperand &MaskOp = MI.getOperand(MaskIdx);
881 Register MaskReg = MaskOp.getReg();
882
883 unsigned MaskedOpc = Info->MaskedPseudo;
884 const MCInstrDesc &MaskedDesc = TII->get(MaskedOpc);
885 unsigned SEW = Cmp.getOperand(RISCVII::getSEWOpNum(Cmp.getDesc())).getImm();
886
887 // Only fold if the masked comparison's dest can live in v0. Its mask
888 // operand must be v0, and we reuse the mask as the passthru, so if the dest
889 // can also be v0 the whole thing coalesces onto v0 and we save the vmand
890 // for free. For LMUL >= 2 the dest is earlyclobbered into vrnov0, which
891 // would force extra vmv1r.v moves for the mask and result and make this a
892 // regression, so bail out in that case. This check must happen before we
893 // mutate any instructions below.
894 if (!TII->getRegClass(MaskedDesc, 0)->contains(RISCV::V0))
895 continue;
896
897 // Make sure the mask and VL dominate the comparison, sinking it if needed.
898 if (!ensureDominates({&MaskOp, &MIVL}, Cmp))
899 continue;
900
901 // The masked comparison's mask operand lives in the VMV0 (v0) class, and
902 // its passthru operand shares the dest's class. Copy the vmand mask into
903 // both; the coalescer collapses these back onto v0, matching the
904 // two-instruction ideal.
905 Register MaskV0Reg = MRI->createVirtualRegister(&RISCV::VMV0RegClass);
906 BuildMI(*MI.getParent(), Cmp, Cmp.getDebugLoc(),
907 TII->get(TargetOpcode::COPY), MaskV0Reg)
908 .addReg(MaskReg);
909 // The passthru shares the dest's class, which the V0 check above restricts
910 // to LMUL <= 1, so it is always a single vector register.
911 Register PassthruReg = MRI->createVirtualRegister(&RISCV::VRRegClass);
912 BuildMI(*MI.getParent(), Cmp, Cmp.getDebugLoc(),
913 TII->get(TargetOpcode::COPY), PassthruReg)
914 .addReg(MaskReg);
915
916 // Build the masked comparison. Its dest reuses vmand's dest; the passthru
917 // (tied to the dest) and mask are both the other vmand operand. Preserve
918 // the source comparison's MI flags (e.g. nofpexcept), which still hold
919 // since the masked comparison operates on a subset of the original active
920 // elements.
921 Register DestReg = MI.getOperand(0).getReg();
922 MachineInstr *Masked =
923 BuildMI(*MI.getParent(), Cmp, MIMetadata(Cmp), MaskedDesc, DestReg)
924 .addReg(PassthruReg)
925 .add(Cmp.getOperand(1))
926 .add(Cmp.getOperand(2))
927 .addReg(MaskV0Reg)
928 .add(MIVL)
929 .addImm(SEW)
930 // The result is a mask register, whose tail is always agnostic, so
931 // we only need mask-undisturbed (MASK_AGNOSTIC clear) to preserve
932 // the inactive elements from the mask/passthru.
934 .setMIFlags(Cmp.getFlags());
935
936 // Now that the comparison is masked, constrain its operands to the masked
937 // pseudo's register classes (e.g. vr -> vrnov0 for LMUL >= 2).
938 for (MachineOperand &MO : Masked->explicit_operands()) {
939 if (!MO.isReg() || !MO.getReg().isVirtual())
940 continue;
941 if (const TargetRegisterClass *RC =
942 Masked->getRegClassConstraint(MO.getOperandNo(), TII, TRI))
943 MRI->constrainRegClass(MO.getReg(), RC);
944 }
945 MI.eraseFromParent();
946 Cmp.eraseFromParent();
947 for (MachineInstr *CmpCopy : CmpCopies)
948 CmpCopy->eraseFromParent();
949
950 return true;
951 }
952
953 return false;
954}
955
956bool RISCVVectorPeepholeImpl::run(MachineFunction &MF) {
957 // Skip if the vector extension is not enabled.
958 ST = &MF.getSubtarget<RISCVSubtarget>();
959 if (!ST->hasVInstructions())
960 return false;
961
962 TII = ST->getInstrInfo();
963 MRI = &MF.getRegInfo();
965
966 bool Changed = false;
967
968 for (MachineBasicBlock &MBB : MF) {
969 for (MachineInstr &MI : make_early_inc_range(MBB))
970 Changed |= foldVMergeToMask(MI);
971
972 for (MachineInstr &MI : make_early_inc_range(MBB))
973 Changed |= foldVMANDToMaskedCompare(MI);
974
975 for (MachineInstr &MI : make_early_inc_range(MBB)) {
976 Changed |= convertToVLMAX(MI);
977 Changed |= convertToUnmasked(MI);
978 Changed |= convertToWholeRegister(MI);
979 Changed |= convertAllOnesVMergeToVMv(MI);
980 Changed |= convertSameMaskVMergeToVMv(MI);
981 if (foldUndefPassthruVMV_V_V(MI)) {
982 Changed |= true;
983 continue; // MI is erased
984 }
985 Changed |= foldVMV_V_V(MI);
986 }
987 }
988
989 return Changed;
990}
991
992bool RISCVVectorPeepholeLegacy::runOnMachineFunction(MachineFunction &MF) {
993 if (skipFunction(MF.getFunction()))
994 return false;
995 return RISCVVectorPeepholeImpl().run(MF);
996}
997
998PreservedAnalyses
1001 MFPropsModifier _(*this, MF);
1002 bool Changed = RISCVVectorPeepholeImpl().run(MF);
1003 if (!Changed)
1004 return PreservedAnalyses::all();
1005
1009 return PA;
1010}
1011
1013 return new RISCVVectorPeepholeLegacy();
1014}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
uint64_t uint64_t
MachineBasicBlock & MBB
static uint64_t getConstant(const Value *IndexValue)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define DEBUG_TYPE
const HexagonInstrInfo * TII
#define _
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
MachineRegisterInfo & MRI
static bool strictlyDominates(MachineBasicBlock::const_iterator A, MachineBasicBlock::const_iterator B)
Given A and B are in the same MBB, returns true if A comes before B.
#define CASE_WHOLE_REGISTER_LMUL(lmul)
SI Lower i1 Copies
Represent the analysis usage information of a pass.
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:280
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
An RAII based helper class to modify MachineFunctionProperties when running pass.
MachineInstrBundleIterator< const MachineInstr > const_iterator
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.
Properties which a MachineFunction may have at a given point in time.
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.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isCopy() const
const MachineBasicBlock * getParent() const
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.
void setImm(int64_t immVal)
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
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.
static MachineOperand CreateImm(int64_t Val)
void setIsUndef(bool Val=true)
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 ...
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI bool hasOneNonDBGUse(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug use of the specified register.
LLVM_ABI bool recomputeRegClass(Register Reg)
recomputeRegClass - Try to find a legal super-class of Reg's register class that still satisfies the ...
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 ...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
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...
LLVM_ABI void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
LLVM_ABI LLVM_READONLY MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
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...
bool hasVInstructions() const
std::optional< unsigned > getRealVLen() const
const RISCVInstrInfo * getInstrInfo() const override
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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...
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
Value * getOperand(unsigned i) const
Definition User.h:207
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
static unsigned getVecPolicyOpNum(const MCInstrDesc &Desc)
static RISCVVType::VLMUL getLMul(uint64_t TSFlags)
static unsigned getVLOpNum(const MCInstrDesc &Desc)
static bool hasVLOp(uint64_t TSFlags)
static bool elementsDependOnMask(uint64_t TSFlags)
static bool hasVecPolicyOp(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)
LLVM_ABI std::pair< unsigned, bool > decodeVLMUL(VLMUL VLMul)
static bool isValidSEW(unsigned SEW)
unsigned getRVVMCOpcode(unsigned RVVPseudoOpcode)
unsigned getDestLog2EEW(const MCInstrDesc &Desc, unsigned Log2SEW)
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.
FunctionPass * createRISCVVectorPeepholeLegacyPass()
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
ArrayRef(const T &OneElt) -> ArrayRef< T >
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58