LLVM 23.0.0git
RISCVInstrInfo.cpp
Go to the documentation of this file.
1//===-- RISCVInstrInfo.cpp - RISC-V Instruction Information -----*- C++ -*-===//
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 file contains the RISC-V implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RISCVInstrInfo.h"
16#include "RISCV.h"
18#include "RISCVSubtarget.h"
19#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Statistic.h"
33#include "llvm/IR/Module.h"
34#include "llvm/MC/MCDwarf.h"
38
39using namespace llvm;
40
41#define GEN_CHECK_COMPRESS_INSTR
42#include "RISCVGenCompressInstEmitter.inc"
43
44#define GET_INSTRINFO_CTOR_DTOR
45#include "RISCVGenInstrInfo.inc"
46
47#define DEBUG_TYPE "riscv-instr-info"
48STATISTIC(NumVRegSpilled,
49 "Number of registers within vector register groups spilled");
50STATISTIC(NumVRegReloaded,
51 "Number of registers within vector register groups reloaded");
52
54 "riscv-prefer-whole-register-move", cl::init(false), cl::Hidden,
55 cl::desc("Prefer whole register move for vector registers."));
56
58 "riscv-force-machine-combiner-strategy", cl::Hidden,
59 cl::desc("Force machine combiner to use a specific strategy for machine "
60 "trace metrics evaluation."),
63 "Local strategy."),
65 "MinInstrCount strategy.")));
66
68 "riscv-outliner-regsave", cl::init(true), cl::Hidden,
69 cl::desc("Enable RegSave strategy in machine outliner (save X5 to a "
70 "temporary register when X5 is live across outlined calls)."));
71
73
74using namespace RISCV;
75
76#define GET_RISCVVPseudosTable_IMPL
77#include "RISCVGenSearchableTables.inc"
78
79} // namespace llvm::RISCVVPseudosTable
80
81namespace llvm::RISCV {
82
83#define GET_RISCVMaskedPseudosTable_IMPL
84#include "RISCVGenSearchableTables.inc"
85
86} // end namespace llvm::RISCV
87
89 : RISCVGenInstrInfo(STI, RegInfo, RISCV::ADJCALLSTACKDOWN,
90 RISCV::ADJCALLSTACKUP),
91 RegInfo(STI.getHwMode()), STI(STI) {}
92
93#define GET_INSTRINFO_HELPERS
94#include "RISCVGenInstrInfo.inc"
95
97 if (STI.hasStdExtZca())
98 return MCInstBuilder(RISCV::C_NOP);
99 return MCInstBuilder(RISCV::ADDI)
100 .addReg(RISCV::X0)
101 .addReg(RISCV::X0)
102 .addImm(0);
103}
104
106 int &FrameIndex) const {
107 TypeSize Dummy = TypeSize::getZero();
108 return isLoadFromStackSlot(MI, FrameIndex, Dummy);
109}
110
111static std::optional<unsigned> getLMULForRVVWholeLoadStore(unsigned Opcode) {
112 switch (Opcode) {
113 default:
114 return std::nullopt;
115 case RISCV::VS1R_V:
116 case RISCV::VL1RE8_V:
117 case RISCV::VL1RE16_V:
118 case RISCV::VL1RE32_V:
119 case RISCV::VL1RE64_V:
120 return 1;
121 case RISCV::VS2R_V:
122 case RISCV::VL2RE8_V:
123 case RISCV::VL2RE16_V:
124 case RISCV::VL2RE32_V:
125 case RISCV::VL2RE64_V:
126 return 2;
127 case RISCV::VS4R_V:
128 case RISCV::VL4RE8_V:
129 case RISCV::VL4RE16_V:
130 case RISCV::VL4RE32_V:
131 case RISCV::VL4RE64_V:
132 return 4;
133 case RISCV::VS8R_V:
134 case RISCV::VL8RE8_V:
135 case RISCV::VL8RE16_V:
136 case RISCV::VL8RE32_V:
137 case RISCV::VL8RE64_V:
138 return 8;
139 }
140}
141
143 int &FrameIndex,
144 TypeSize &MemBytes) const {
145 switch (MI.getOpcode()) {
146 default:
147 return 0;
148 case RISCV::LB:
149 case RISCV::LBU:
150 MemBytes = TypeSize::getFixed(1);
151 break;
152 case RISCV::LH:
153 case RISCV::LH_INX:
154 case RISCV::LHU:
155 case RISCV::FLH:
156 MemBytes = TypeSize::getFixed(2);
157 break;
158 case RISCV::LW:
159 case RISCV::LW_INX:
160 case RISCV::FLW:
161 case RISCV::LWU:
162 MemBytes = TypeSize::getFixed(4);
163 break;
164 case RISCV::LD:
165 case RISCV::LD_RV32:
166 case RISCV::FLD:
167 MemBytes = TypeSize::getFixed(8);
168 break;
169 case RISCV::VL1RE8_V:
170 case RISCV::VL2RE8_V:
171 case RISCV::VL4RE8_V:
172 case RISCV::VL8RE8_V:
173 if (!MI.getOperand(1).isFI())
174 return Register();
175 FrameIndex = MI.getOperand(1).getIndex();
176 unsigned LMUL = *getLMULForRVVWholeLoadStore(MI.getOpcode());
178 return MI.getOperand(0).getReg();
179 }
180
181 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
182 MI.getOperand(2).getImm() == 0) {
183 FrameIndex = MI.getOperand(1).getIndex();
184 return MI.getOperand(0).getReg();
185 }
186
187 return 0;
188}
189
191 int &FrameIndex) const {
192 TypeSize Dummy = TypeSize::getZero();
193 return isStoreToStackSlot(MI, FrameIndex, Dummy);
194}
195
197 int &FrameIndex,
198 TypeSize &MemBytes) const {
199 switch (MI.getOpcode()) {
200 default:
201 return 0;
202 case RISCV::SB:
203 MemBytes = TypeSize::getFixed(1);
204 break;
205 case RISCV::SH:
206 case RISCV::SH_INX:
207 case RISCV::FSH:
208 MemBytes = TypeSize::getFixed(2);
209 break;
210 case RISCV::SW:
211 case RISCV::SW_INX:
212 case RISCV::FSW:
213 MemBytes = TypeSize::getFixed(4);
214 break;
215 case RISCV::SD:
216 case RISCV::SD_RV32:
217 case RISCV::FSD:
218 MemBytes = TypeSize::getFixed(8);
219 break;
220 case RISCV::VS1R_V:
221 case RISCV::VS2R_V:
222 case RISCV::VS4R_V:
223 case RISCV::VS8R_V:
224 if (!MI.getOperand(1).isFI())
225 return Register();
226 FrameIndex = MI.getOperand(1).getIndex();
227 unsigned LMUL = *getLMULForRVVWholeLoadStore(MI.getOpcode());
229 return MI.getOperand(0).getReg();
230 }
231
232 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
233 MI.getOperand(2).getImm() == 0) {
234 FrameIndex = MI.getOperand(1).getIndex();
235 return MI.getOperand(0).getReg();
236 }
237
238 return 0;
239}
240
242 const MachineInstr &MI) const {
243 switch (RISCV::getRVVMCOpcode(MI.getOpcode())) {
244 case RISCV::VMV_V_X:
245 case RISCV::VFMV_V_F:
246 case RISCV::VMV_V_I:
247 case RISCV::VMV_S_X:
248 case RISCV::VFMV_S_F:
249 case RISCV::VID_V:
250 return MI.getOperand(1).isUndef();
251 default:
253 }
254}
255
256static bool forwardCopyWillClobberTuple(unsigned DstReg, unsigned SrcReg,
257 unsigned NumRegs) {
258 return DstReg > SrcReg && (DstReg - SrcReg) < NumRegs;
259}
260
262 const MachineBasicBlock &MBB,
265 RISCVVType::VLMUL LMul) {
267 return false;
268
269 assert(MBBI->getOpcode() == TargetOpcode::COPY &&
270 "Unexpected COPY instruction.");
271 Register SrcReg = MBBI->getOperand(1).getReg();
273
274 bool FoundDef = false;
275 bool FirstVSetVLI = false;
276 unsigned FirstSEW = 0;
277 while (MBBI != MBB.begin()) {
278 --MBBI;
279 if (MBBI->isMetaInstruction())
280 continue;
281
282 if (RISCVInstrInfo::isVectorConfigInstr(*MBBI)) {
283 // There is a vsetvli between COPY and source define instruction.
284 // vy = def_vop ... (producing instruction)
285 // ...
286 // vsetvli
287 // ...
288 // vx = COPY vy
289 if (!FoundDef) {
290 if (!FirstVSetVLI) {
291 FirstVSetVLI = true;
292 unsigned FirstVType = MBBI->getOperand(2).getImm();
293 RISCVVType::VLMUL FirstLMul = RISCVVType::getVLMUL(FirstVType);
294 FirstSEW = RISCVVType::getSEW(FirstVType);
295 // The first encountered vsetvli must have the same lmul as the
296 // register class of COPY.
297 if (FirstLMul != LMul)
298 return false;
299 }
300 // Only permit `vsetvli x0, x0, vtype` between COPY and the source
301 // define instruction.
302 if (!RISCVInstrInfo::isVLPreservingConfig(*MBBI))
303 return false;
304 continue;
305 }
306
307 // MBBI is the first vsetvli before the producing instruction.
308 unsigned VType = MBBI->getOperand(2).getImm();
309 // If there is a vsetvli between COPY and the producing instruction.
310 if (FirstVSetVLI) {
311 // If SEW is different, return false.
312 if (RISCVVType::getSEW(VType) != FirstSEW)
313 return false;
314 }
315
316 // If the vsetvli is tail undisturbed, keep the whole register move.
317 if (!RISCVVType::isTailAgnostic(VType))
318 return false;
319
320 // The checking is conservative. We only have register classes for
321 // LMUL = 1/2/4/8. We should be able to convert vmv1r.v to vmv.v.v
322 // for fractional LMUL operations. However, we could not use the vsetvli
323 // lmul for widening operations. The result of widening operation is
324 // 2 x LMUL.
325 return LMul == RISCVVType::getVLMUL(VType);
326 } else if (MBBI->isInlineAsm() || MBBI->isCall()) {
327 return false;
328 } else if (MBBI->getNumDefs()) {
329 // Check all the instructions which will change VL.
330 // For example, vleff has implicit def VL.
331 if (MBBI->modifiesRegister(RISCV::VL, /*TRI=*/nullptr))
332 return false;
333
334 // Only converting whole register copies to vmv.v.v when the defining
335 // value appears in the explicit operands.
336 for (const MachineOperand &MO : MBBI->explicit_operands()) {
337 if (!MO.isReg() || !MO.isDef())
338 continue;
339 if (!FoundDef && TRI->regsOverlap(MO.getReg(), SrcReg)) {
340 // We only permit the source of COPY has the same LMUL as the defined
341 // operand.
342 // There are cases we need to keep the whole register copy if the LMUL
343 // is different.
344 // For example,
345 // $x0 = PseudoVSETIVLI 4, 73 // vsetivli zero, 4, e16,m2,ta,m
346 // $v28m4 = PseudoVWADD_VV_M2 $v26m2, $v8m2
347 // # The COPY may be created by vlmul_trunc intrinsic.
348 // $v26m2 = COPY renamable $v28m2, implicit killed $v28m4
349 //
350 // After widening, the valid value will be 4 x e32 elements. If we
351 // convert the COPY to vmv.v.v, it will only copy 4 x e16 elements.
352 // FIXME: The COPY of subregister of Zvlsseg register will not be able
353 // to convert to vmv.v.[v|i] under the constraint.
354 if (MO.getReg() != SrcReg)
355 return false;
356
357 // In widening reduction instructions with LMUL_1 input vector case,
358 // only checking the LMUL is insufficient due to reduction result is
359 // always LMUL_1.
360 // For example,
361 // $x11 = PseudoVSETIVLI 1, 64 // vsetivli a1, 1, e8, m1, ta, mu
362 // $v8m1 = PseudoVWREDSUM_VS_M1 $v26, $v27
363 // $v26 = COPY killed renamable $v8
364 // After widening, The valid value will be 1 x e16 elements. If we
365 // convert the COPY to vmv.v.v, it will only copy 1 x e8 elements.
366 uint64_t TSFlags = MBBI->getDesc().TSFlags;
368 return false;
369
370 // If the producing instruction does not depend on vsetvli, do not
371 // convert COPY to vmv.v.v. For example, VL1R_V or PseudoVRELOAD.
372 if (!RISCVII::hasSEWOp(TSFlags) || !RISCVII::hasVLOp(TSFlags))
373 return false;
374
375 // Found the definition.
376 FoundDef = true;
377 DefMBBI = MBBI;
378 break;
379 }
380 }
381 }
382 }
383
384 return false;
385}
386
389 const DebugLoc &DL, MCRegister DstReg, MCRegister SrcReg, bool KillSrc,
390 const TargetRegisterClass *RegClass) const {
391 const RISCVRegisterInfo *TRI = STI.getRegisterInfo();
393 unsigned NF = RISCVRI::getNF(RegClass->TSFlags);
394
395 uint16_t SrcEncoding = TRI->getEncodingValue(SrcReg);
396 uint16_t DstEncoding = TRI->getEncodingValue(DstReg);
397 auto [LMulVal, Fractional] = RISCVVType::decodeVLMUL(LMul);
398 assert(!Fractional && "It is impossible be fractional lmul here.");
399 unsigned NumRegs = NF * LMulVal;
400 bool ReversedCopy =
401 forwardCopyWillClobberTuple(DstEncoding, SrcEncoding, NumRegs);
402 if (ReversedCopy) {
403 // If the src and dest overlap when copying a tuple, we need to copy the
404 // registers in reverse.
405 SrcEncoding += NumRegs - 1;
406 DstEncoding += NumRegs - 1;
407 }
408
409 unsigned I = 0;
410 auto GetCopyInfo = [&](uint16_t SrcEncoding, uint16_t DstEncoding)
411 -> std::tuple<RISCVVType::VLMUL, const TargetRegisterClass &, unsigned,
412 unsigned, unsigned> {
413 if (ReversedCopy) {
414 // For reversed copying, if there are enough aligned registers(8/4/2), we
415 // can do a larger copy(LMUL8/4/2).
416 // Besides, we have already known that DstEncoding is larger than
417 // SrcEncoding in forwardCopyWillClobberTuple, so the difference between
418 // DstEncoding and SrcEncoding should be >= LMUL value we try to use to
419 // avoid clobbering.
420 uint16_t Diff = DstEncoding - SrcEncoding;
421 if (I + 8 <= NumRegs && Diff >= 8 && SrcEncoding % 8 == 7 &&
422 DstEncoding % 8 == 7)
423 return {RISCVVType::LMUL_8, RISCV::VRM8RegClass, RISCV::VMV8R_V,
424 RISCV::PseudoVMV_V_V_M8, RISCV::PseudoVMV_V_I_M8};
425 if (I + 4 <= NumRegs && Diff >= 4 && SrcEncoding % 4 == 3 &&
426 DstEncoding % 4 == 3)
427 return {RISCVVType::LMUL_4, RISCV::VRM4RegClass, RISCV::VMV4R_V,
428 RISCV::PseudoVMV_V_V_M4, RISCV::PseudoVMV_V_I_M4};
429 if (I + 2 <= NumRegs && Diff >= 2 && SrcEncoding % 2 == 1 &&
430 DstEncoding % 2 == 1)
431 return {RISCVVType::LMUL_2, RISCV::VRM2RegClass, RISCV::VMV2R_V,
432 RISCV::PseudoVMV_V_V_M2, RISCV::PseudoVMV_V_I_M2};
433 // Or we should do LMUL1 copying.
434 return {RISCVVType::LMUL_1, RISCV::VRRegClass, RISCV::VMV1R_V,
435 RISCV::PseudoVMV_V_V_M1, RISCV::PseudoVMV_V_I_M1};
436 }
437
438 // For forward copying, if source register encoding and destination register
439 // encoding are aligned to 8/4/2, we can do a LMUL8/4/2 copying.
440 if (I + 8 <= NumRegs && SrcEncoding % 8 == 0 && DstEncoding % 8 == 0)
441 return {RISCVVType::LMUL_8, RISCV::VRM8RegClass, RISCV::VMV8R_V,
442 RISCV::PseudoVMV_V_V_M8, RISCV::PseudoVMV_V_I_M8};
443 if (I + 4 <= NumRegs && SrcEncoding % 4 == 0 && DstEncoding % 4 == 0)
444 return {RISCVVType::LMUL_4, RISCV::VRM4RegClass, RISCV::VMV4R_V,
445 RISCV::PseudoVMV_V_V_M4, RISCV::PseudoVMV_V_I_M4};
446 if (I + 2 <= NumRegs && SrcEncoding % 2 == 0 && DstEncoding % 2 == 0)
447 return {RISCVVType::LMUL_2, RISCV::VRM2RegClass, RISCV::VMV2R_V,
448 RISCV::PseudoVMV_V_V_M2, RISCV::PseudoVMV_V_I_M2};
449 // Or we should do LMUL1 copying.
450 return {RISCVVType::LMUL_1, RISCV::VRRegClass, RISCV::VMV1R_V,
451 RISCV::PseudoVMV_V_V_M1, RISCV::PseudoVMV_V_I_M1};
452 };
453
454 while (I != NumRegs) {
455 // For non-segment copying, we only do this once as the registers are always
456 // aligned.
457 // For segment copying, we may do this several times. If the registers are
458 // aligned to larger LMUL, we can eliminate some copyings.
459 auto [LMulCopied, RegClass, Opc, VVOpc, VIOpc] =
460 GetCopyInfo(SrcEncoding, DstEncoding);
461 auto [NumCopied, _] = RISCVVType::decodeVLMUL(LMulCopied);
462
464 if (LMul == LMulCopied &&
465 isConvertibleToVMV_V_V(STI, MBB, MBBI, DefMBBI, LMul)) {
466 Opc = VVOpc;
467 if (DefMBBI->getOpcode() == VIOpc)
468 Opc = VIOpc;
469 }
470
471 // Emit actual copying.
472 // For reversed copying, the encoding should be decreased.
473 MCRegister ActualSrcReg = TRI->findVRegWithEncoding(
474 RegClass, ReversedCopy ? (SrcEncoding - NumCopied + 1) : SrcEncoding);
475 MCRegister ActualDstReg = TRI->findVRegWithEncoding(
476 RegClass, ReversedCopy ? (DstEncoding - NumCopied + 1) : DstEncoding);
477
478 auto MIB = BuildMI(MBB, MBBI, DL, get(Opc), ActualDstReg);
479 bool UseVMV_V_I = RISCV::getRVVMCOpcode(Opc) == RISCV::VMV_V_I;
480 bool UseVMV = UseVMV_V_I || RISCV::getRVVMCOpcode(Opc) == RISCV::VMV_V_V;
481 if (UseVMV)
482 MIB.addReg(ActualDstReg, RegState::Undef);
483 if (UseVMV_V_I)
484 MIB = MIB.add(DefMBBI->getOperand(2));
485 else
486 MIB = MIB.addReg(ActualSrcReg, getKillRegState(KillSrc));
487 if (UseVMV) {
488 const MCInstrDesc &Desc = DefMBBI->getDesc();
489 MIB.add(DefMBBI->getOperand(RISCVII::getVLOpNum(Desc))); // AVL
490 unsigned Log2SEW =
491 DefMBBI->getOperand(RISCVII::getSEWOpNum(Desc)).getImm();
492 MIB.addImm(Log2SEW ? Log2SEW : 3); // SEW
493 MIB.addImm(0); // tu, mu
494 MIB.addReg(RISCV::VL, RegState::Implicit);
495 MIB.addReg(RISCV::VTYPE, RegState::Implicit);
496 }
497 // Add an implicit read of the original source to silence the verifier
498 // in the cases where some of the smaller VRs we're copying from might be
499 // undef, caused by the fact that the original, larger source VR might not
500 // be fully initialized at the time this COPY happens.
501 MIB.addReg(SrcReg, RegState::Implicit);
502
503 // If we are copying reversely, we should decrease the encoding.
504 SrcEncoding += (ReversedCopy ? -NumCopied : NumCopied);
505 DstEncoding += (ReversedCopy ? -NumCopied : NumCopied);
506 I += NumCopied;
507 }
508}
509
512 const DebugLoc &DL, Register DstReg,
513 Register SrcReg, bool KillSrc,
514 bool RenamableDest, bool RenamableSrc) const {
515 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
516 RegState KillFlag = getKillRegState(KillSrc);
517
518 if (RISCV::GPRRegClass.contains(DstReg, SrcReg)) {
519 BuildMI(MBB, MBBI, DL, get(RISCV::ADDI), DstReg)
520 .addReg(SrcReg, KillFlag | getRenamableRegState(RenamableSrc))
521 .addImm(0);
522 return;
523 }
524
525 if (RISCV::GPRF16RegClass.contains(DstReg, SrcReg)) {
526 BuildMI(MBB, MBBI, DL, get(RISCV::PseudoMV_FPR16INX), DstReg)
527 .addReg(SrcReg, KillFlag | getRenamableRegState(RenamableSrc));
528 return;
529 }
530
531 if (RISCV::GPRF32RegClass.contains(DstReg, SrcReg)) {
532 BuildMI(MBB, MBBI, DL, get(RISCV::PseudoMV_FPR32INX), DstReg)
533 .addReg(SrcReg, KillFlag | getRenamableRegState(RenamableSrc));
534 return;
535 }
536
537 if (RISCV::GPRPairRegClass.contains(DstReg, SrcReg)) {
538 if (STI.isRV32()) {
539 if (STI.hasStdExtZdinx()) {
540 // On RV32_Zdinx, FMV.D will move a pair of registers to another pair of
541 // registers, in one instruction.
542 BuildMI(MBB, MBBI, DL, get(RISCV::FSGNJ_D_IN32X), DstReg)
543 .addReg(SrcReg, getRenamableRegState(RenamableSrc))
544 .addReg(SrcReg, KillFlag | getRenamableRegState(RenamableSrc));
545 return;
546 }
547
548 if (STI.hasStdExtP()) {
549 // On RV32P, `padd.dw` is a GPR Pair Add
550 BuildMI(MBB, MBBI, DL, get(RISCV::PADD_DW), DstReg)
551 .addReg(SrcReg, KillFlag | getRenamableRegState(RenamableSrc))
552 .addReg(RISCV::X0_Pair);
553 return;
554 }
555 }
556
557 MCRegister EvenReg = TRI->getSubReg(SrcReg, RISCV::sub_gpr_even);
558 MCRegister OddReg = TRI->getSubReg(SrcReg, RISCV::sub_gpr_odd);
559 // We need to correct the odd register of X0_Pair.
560 if (OddReg == RISCV::DUMMY_REG_PAIR_WITH_X0)
561 OddReg = RISCV::X0;
562 assert(DstReg != RISCV::X0_Pair && "Cannot write to X0_Pair");
563
564 // Emit an ADDI for both parts of GPRPair.
565 BuildMI(MBB, MBBI, DL, get(RISCV::ADDI),
566 TRI->getSubReg(DstReg, RISCV::sub_gpr_even))
567 .addReg(EvenReg, KillFlag)
568 .addImm(0);
569 BuildMI(MBB, MBBI, DL, get(RISCV::ADDI),
570 TRI->getSubReg(DstReg, RISCV::sub_gpr_odd))
571 .addReg(OddReg, KillFlag)
572 .addImm(0);
573 return;
574 }
575
576 // Handle copy from csr
577 if (RISCV::VCSRRegClass.contains(SrcReg) &&
578 RISCV::GPRRegClass.contains(DstReg)) {
579 BuildMI(MBB, MBBI, DL, get(RISCV::CSRRS), DstReg)
580 .addImm(RISCVSysReg::lookupSysRegByName(TRI->getName(SrcReg))->Encoding)
581 .addReg(RISCV::X0);
582 return;
583 }
584
585 if (RISCV::FPR16RegClass.contains(DstReg, SrcReg)) {
586 unsigned Opc;
587 if (STI.hasStdExtZfh()) {
588 Opc = RISCV::FSGNJ_H;
589 } else {
590 assert(STI.hasStdExtF() &&
591 (STI.hasStdExtZfhmin() || STI.hasStdExtZfbfmin()) &&
592 "Unexpected extensions");
593 // Zfhmin/Zfbfmin doesn't have FSGNJ_H, replace FSGNJ_H with FSGNJ_S.
594 DstReg = TRI->getMatchingSuperReg(DstReg, RISCV::sub_16,
595 &RISCV::FPR32RegClass);
596 SrcReg = TRI->getMatchingSuperReg(SrcReg, RISCV::sub_16,
597 &RISCV::FPR32RegClass);
598 Opc = RISCV::FSGNJ_S;
599 }
600 BuildMI(MBB, MBBI, DL, get(Opc), DstReg)
601 .addReg(SrcReg, KillFlag)
602 .addReg(SrcReg, KillFlag);
603 return;
604 }
605
606 if (RISCV::FPR32RegClass.contains(DstReg, SrcReg)) {
607 BuildMI(MBB, MBBI, DL, get(RISCV::FSGNJ_S), DstReg)
608 .addReg(SrcReg, KillFlag)
609 .addReg(SrcReg, KillFlag);
610 return;
611 }
612
613 if (RISCV::FPR64RegClass.contains(DstReg, SrcReg)) {
614 BuildMI(MBB, MBBI, DL, get(RISCV::FSGNJ_D), DstReg)
615 .addReg(SrcReg, KillFlag)
616 .addReg(SrcReg, KillFlag);
617 return;
618 }
619
620 if (RISCV::FPR32RegClass.contains(DstReg) &&
621 RISCV::GPRRegClass.contains(SrcReg)) {
622 BuildMI(MBB, MBBI, DL, get(RISCV::FMV_W_X), DstReg)
623 .addReg(SrcReg, KillFlag);
624 return;
625 }
626
627 if (RISCV::GPRRegClass.contains(DstReg) &&
628 RISCV::FPR32RegClass.contains(SrcReg)) {
629 BuildMI(MBB, MBBI, DL, get(RISCV::FMV_X_W), DstReg)
630 .addReg(SrcReg, KillFlag);
631 return;
632 }
633
634 if (RISCV::FPR64RegClass.contains(DstReg) &&
635 RISCV::GPRRegClass.contains(SrcReg)) {
636 assert(STI.getXLen() == 64 && "Unexpected GPR size");
637 BuildMI(MBB, MBBI, DL, get(RISCV::FMV_D_X), DstReg)
638 .addReg(SrcReg, KillFlag);
639 return;
640 }
641
642 if (RISCV::GPRRegClass.contains(DstReg) &&
643 RISCV::FPR64RegClass.contains(SrcReg)) {
644 assert(STI.getXLen() == 64 && "Unexpected GPR size");
645 BuildMI(MBB, MBBI, DL, get(RISCV::FMV_X_D), DstReg)
646 .addReg(SrcReg, KillFlag);
647 return;
648 }
649
650 // VR->VR copies.
651 const TargetRegisterClass *RegClass =
652 TRI->getCommonMinimalPhysRegClass(SrcReg, DstReg);
653 if (RISCVRegisterInfo::isRVVRegClass(RegClass)) {
654 copyPhysRegVector(MBB, MBBI, DL, DstReg, SrcReg, KillSrc, RegClass);
655 return;
656 }
657
658 llvm_unreachable("Impossible reg-to-reg copy");
659}
660
663 Register SrcReg, bool IsKill, int FI,
664 const TargetRegisterClass *RC,
665 Register VReg,
666 MachineInstr::MIFlag Flags) const {
667 MachineFunction *MF = MBB.getParent();
668 MachineFrameInfo &MFI = MF->getFrameInfo();
669 Align Alignment = MFI.getObjectAlign(FI);
670
671 unsigned Opcode;
672 if (RISCV::GPRRegClass.hasSubClassEq(RC)) {
673 Opcode = RegInfo.getRegSizeInBits(RISCV::GPRRegClass) == 32 ? RISCV::SW
674 : RISCV::SD;
675 } else if (RISCV::GPRF16RegClass.hasSubClassEq(RC)) {
676 Opcode = RISCV::SH_INX;
677 } else if (RISCV::GPRF32RegClass.hasSubClassEq(RC)) {
678 Opcode = RISCV::SW_INX;
679 } else if (RISCV::GPRPairRegClass.hasSubClassEq(RC)) {
680 if (!STI.is64Bit() && STI.hasStdExtZilsd() &&
681 Alignment >= STI.getZilsdAlign()) {
682 Opcode = RISCV::SD_RV32;
683 } else {
684 Opcode = RISCV::PseudoRV32ZdinxSD;
685 }
686 } else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) {
687 Opcode = RISCV::FSH;
688 } else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) {
689 Opcode = RISCV::FSW;
690 } else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) {
691 Opcode = RISCV::FSD;
692 } else if (RISCV::VRRegClass.hasSubClassEq(RC)) {
693 Opcode = RISCV::VS1R_V;
694 } else if (RISCV::VRM2RegClass.hasSubClassEq(RC)) {
695 Opcode = RISCV::VS2R_V;
696 } else if (RISCV::VRM4RegClass.hasSubClassEq(RC)) {
697 Opcode = RISCV::VS4R_V;
698 } else if (RISCV::VRM8RegClass.hasSubClassEq(RC)) {
699 Opcode = RISCV::VS8R_V;
700 } else if (RISCV::VRN2M1RegClass.hasSubClassEq(RC))
701 Opcode = RISCV::PseudoVSPILL2_M1;
702 else if (RISCV::VRN2M2RegClass.hasSubClassEq(RC))
703 Opcode = RISCV::PseudoVSPILL2_M2;
704 else if (RISCV::VRN2M4RegClass.hasSubClassEq(RC))
705 Opcode = RISCV::PseudoVSPILL2_M4;
706 else if (RISCV::VRN3M1RegClass.hasSubClassEq(RC))
707 Opcode = RISCV::PseudoVSPILL3_M1;
708 else if (RISCV::VRN3M2RegClass.hasSubClassEq(RC))
709 Opcode = RISCV::PseudoVSPILL3_M2;
710 else if (RISCV::VRN4M1RegClass.hasSubClassEq(RC))
711 Opcode = RISCV::PseudoVSPILL4_M1;
712 else if (RISCV::VRN4M2RegClass.hasSubClassEq(RC))
713 Opcode = RISCV::PseudoVSPILL4_M2;
714 else if (RISCV::VRN5M1RegClass.hasSubClassEq(RC))
715 Opcode = RISCV::PseudoVSPILL5_M1;
716 else if (RISCV::VRN6M1RegClass.hasSubClassEq(RC))
717 Opcode = RISCV::PseudoVSPILL6_M1;
718 else if (RISCV::VRN7M1RegClass.hasSubClassEq(RC))
719 Opcode = RISCV::PseudoVSPILL7_M1;
720 else if (RISCV::VRN8M1RegClass.hasSubClassEq(RC))
721 Opcode = RISCV::PseudoVSPILL8_M1;
722 else
723 llvm_unreachable("Can't store this register to stack slot");
724
728 TypeSize::getScalable(MFI.getObjectSize(FI)), Alignment);
729
731 BuildMI(MBB, I, DebugLoc(), get(Opcode))
732 .addReg(SrcReg, getKillRegState(IsKill))
733 .addFrameIndex(FI)
734 .addMemOperand(MMO)
735 .setMIFlag(Flags);
736 NumVRegSpilled += RegInfo.getRegSizeInBits(*RC) / RISCV::RVVBitsPerBlock;
737 } else {
740 MFI.getObjectSize(FI), Alignment);
741
742 BuildMI(MBB, I, DebugLoc(), get(Opcode))
743 .addReg(SrcReg, getKillRegState(IsKill))
744 .addFrameIndex(FI)
745 .addImm(0)
746 .addMemOperand(MMO)
747 .setMIFlag(Flags);
748 }
749}
750
753 Register DstReg, int FI,
754 const TargetRegisterClass *RC,
755 Register VReg, unsigned SubReg,
756 MachineInstr::MIFlag Flags) const {
757 MachineFunction *MF = MBB.getParent();
758 MachineFrameInfo &MFI = MF->getFrameInfo();
759 Align Alignment = MFI.getObjectAlign(FI);
760 DebugLoc DL =
761 Flags & MachineInstr::FrameDestroy ? MBB.findDebugLoc(I) : DebugLoc();
762
763 unsigned Opcode;
764 if (RISCV::GPRRegClass.hasSubClassEq(RC)) {
765 Opcode = RegInfo.getRegSizeInBits(RISCV::GPRRegClass) == 32 ? RISCV::LW
766 : RISCV::LD;
767 } else if (RISCV::GPRF16RegClass.hasSubClassEq(RC)) {
768 Opcode = RISCV::LH_INX;
769 } else if (RISCV::GPRF32RegClass.hasSubClassEq(RC)) {
770 Opcode = RISCV::LW_INX;
771 } else if (RISCV::GPRPairRegClass.hasSubClassEq(RC)) {
772 if (!STI.is64Bit() && STI.hasStdExtZilsd() &&
773 Alignment >= STI.getZilsdAlign()) {
774 Opcode = RISCV::LD_RV32;
775 } else {
776 Opcode = RISCV::PseudoRV32ZdinxLD;
777 }
778 } else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) {
779 Opcode = RISCV::FLH;
780 } else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) {
781 Opcode = RISCV::FLW;
782 } else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) {
783 Opcode = RISCV::FLD;
784 } else if (RISCV::VRRegClass.hasSubClassEq(RC)) {
785 Opcode = RISCV::VL1RE8_V;
786 } else if (RISCV::VRM2RegClass.hasSubClassEq(RC)) {
787 Opcode = RISCV::VL2RE8_V;
788 } else if (RISCV::VRM4RegClass.hasSubClassEq(RC)) {
789 Opcode = RISCV::VL4RE8_V;
790 } else if (RISCV::VRM8RegClass.hasSubClassEq(RC)) {
791 Opcode = RISCV::VL8RE8_V;
792 } else if (RISCV::VRN2M1RegClass.hasSubClassEq(RC))
793 Opcode = RISCV::PseudoVRELOAD2_M1;
794 else if (RISCV::VRN2M2RegClass.hasSubClassEq(RC))
795 Opcode = RISCV::PseudoVRELOAD2_M2;
796 else if (RISCV::VRN2M4RegClass.hasSubClassEq(RC))
797 Opcode = RISCV::PseudoVRELOAD2_M4;
798 else if (RISCV::VRN3M1RegClass.hasSubClassEq(RC))
799 Opcode = RISCV::PseudoVRELOAD3_M1;
800 else if (RISCV::VRN3M2RegClass.hasSubClassEq(RC))
801 Opcode = RISCV::PseudoVRELOAD3_M2;
802 else if (RISCV::VRN4M1RegClass.hasSubClassEq(RC))
803 Opcode = RISCV::PseudoVRELOAD4_M1;
804 else if (RISCV::VRN4M2RegClass.hasSubClassEq(RC))
805 Opcode = RISCV::PseudoVRELOAD4_M2;
806 else if (RISCV::VRN5M1RegClass.hasSubClassEq(RC))
807 Opcode = RISCV::PseudoVRELOAD5_M1;
808 else if (RISCV::VRN6M1RegClass.hasSubClassEq(RC))
809 Opcode = RISCV::PseudoVRELOAD6_M1;
810 else if (RISCV::VRN7M1RegClass.hasSubClassEq(RC))
811 Opcode = RISCV::PseudoVRELOAD7_M1;
812 else if (RISCV::VRN8M1RegClass.hasSubClassEq(RC))
813 Opcode = RISCV::PseudoVRELOAD8_M1;
814 else
815 llvm_unreachable("Can't load this register from stack slot");
816
820 TypeSize::getScalable(MFI.getObjectSize(FI)), Alignment);
821
823 BuildMI(MBB, I, DL, get(Opcode), DstReg)
824 .addFrameIndex(FI)
825 .addMemOperand(MMO)
826 .setMIFlag(Flags);
827 NumVRegReloaded += RegInfo.getRegSizeInBits(*RC) / RISCV::RVVBitsPerBlock;
828 } else {
831 MFI.getObjectSize(FI), Alignment);
832
833 BuildMI(MBB, I, DL, get(Opcode), DstReg)
834 .addFrameIndex(FI)
835 .addImm(0)
836 .addMemOperand(MMO)
837 .setMIFlag(Flags);
838 }
839}
840std::optional<unsigned> getFoldedOpcode(MachineFunction &MF, MachineInstr &MI,
842 const RISCVSubtarget &ST) {
843
844 // The below optimizations narrow the load so they are only valid for little
845 // endian.
846 // TODO: Support big endian by adding an offset into the frame object?
847 if (MF.getDataLayout().isBigEndian())
848 return std::nullopt;
849
850 // Fold load from stack followed by sext.b/sext.h/sext.w/zext.b/zext.h/zext.w.
851 if (Ops.size() != 1 || Ops[0] != 1)
852 return std::nullopt;
853
854 switch (MI.getOpcode()) {
855 default:
856 if (RISCVInstrInfo::isSEXT_W(MI))
857 return RISCV::LW;
858 if (RISCVInstrInfo::isZEXT_W(MI))
859 return RISCV::LWU;
860 if (RISCVInstrInfo::isZEXT_B(MI))
861 return RISCV::LBU;
862 break;
863 case RISCV::SEXT_H:
864 return RISCV::LH;
865 case RISCV::SEXT_B:
866 return RISCV::LB;
867 case RISCV::ZEXT_H_RV32:
868 case RISCV::ZEXT_H_RV64:
869 return RISCV::LHU;
870 }
871
872 switch (RISCV::getRVVMCOpcode(MI.getOpcode())) {
873 default:
874 return std::nullopt;
875 case RISCV::VMV_X_S: {
876 unsigned Log2SEW =
877 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
878 if (ST.getXLen() < (1U << Log2SEW))
879 return std::nullopt;
880 switch (Log2SEW) {
881 case 3:
882 return RISCV::LB;
883 case 4:
884 return RISCV::LH;
885 case 5:
886 return RISCV::LW;
887 case 6:
888 return RISCV::LD;
889 default:
890 llvm_unreachable("Unexpected SEW");
891 }
892 }
893 case RISCV::VFMV_F_S: {
894 unsigned Log2SEW =
895 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
896 switch (Log2SEW) {
897 case 4:
898 return RISCV::FLH;
899 case 5:
900 return RISCV::FLW;
901 case 6:
902 return RISCV::FLD;
903 default:
904 llvm_unreachable("Unexpected SEW");
905 }
906 }
907 }
908}
909
910// This is the version used during InlineSpiller::spillAroundUses
913 ArrayRef<unsigned> Ops, int FrameIndex,
914 MachineInstr *&CopyMI, LiveIntervals *LIS,
915 VirtRegMap *VRM) const {
917 std::optional<unsigned> LoadOpc = getFoldedOpcode(MF, MI, Ops, STI);
918 if (!LoadOpc)
919 return nullptr;
920 Register DstReg = MI.getOperand(0).getReg();
921 return BuildMI(*MI.getParent(), InsertPt, MI.getDebugLoc(), get(*LoadOpc),
922 DstReg)
923 .addFrameIndex(FrameIndex)
924 .addImm(0);
925}
926
927static unsigned getLoadPredicatedOpcode(unsigned Opcode) {
928 switch (Opcode) {
929 case RISCV::LB:
930 return RISCV::PseudoCCLB;
931 case RISCV::LBU:
932 return RISCV::PseudoCCLBU;
933 case RISCV::LH:
934 return RISCV::PseudoCCLH;
935 case RISCV::LHU:
936 return RISCV::PseudoCCLHU;
937 case RISCV::LW:
938 return RISCV::PseudoCCLW;
939 case RISCV::LWU:
940 return RISCV::PseudoCCLWU;
941 case RISCV::LD:
942 return RISCV::PseudoCCLD;
943 case RISCV::QC_E_LB:
944 return RISCV::PseudoCCQC_E_LB;
945 case RISCV::QC_E_LBU:
946 return RISCV::PseudoCCQC_E_LBU;
947 case RISCV::QC_E_LH:
948 return RISCV::PseudoCCQC_E_LH;
949 case RISCV::QC_E_LHU:
950 return RISCV::PseudoCCQC_E_LHU;
951 case RISCV::QC_E_LW:
952 return RISCV::PseudoCCQC_E_LW;
953 default:
954 return 0;
955 }
956}
957
960 MachineInstr &LoadMI, MachineInstr *&CopyMI, LiveIntervals *LIS,
961 VirtRegMap *VRM) const {
963 // For now, only handle RISCV::PseudoCCMOVGPR.
964 if (MI.getOpcode() != RISCV::PseudoCCMOVGPR)
965 return nullptr;
966
967 unsigned PredOpc = getLoadPredicatedOpcode(LoadMI.getOpcode());
968
969 if (!STI.hasShortForwardBranchILoad() || !PredOpc)
970 return nullptr;
971
973 if (Ops.size() != 1 || (Ops[0] != 1 && Ops[0] != 2))
974 return nullptr;
975
976 bool Invert = Ops[0] == 2;
977 const MachineOperand &FalseReg = MI.getOperand(!Invert ? 2 : 1);
978 Register DestReg = MI.getOperand(0).getReg();
979 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
980 if (!MRI.constrainRegClass(DestReg, PreviousClass))
981 return nullptr;
982
983 // Create a new predicated version of DefMI.
984 MachineInstrBuilder NewMI = BuildMI(*MI.getParent(), InsertPt,
985 MI.getDebugLoc(), get(PredOpc), DestReg);
986
987 // Copy the false register.
988 NewMI.add(FalseReg);
989
990 // Copy all the DefMI operands.
991 const MCInstrDesc &DefDesc = LoadMI.getDesc();
992 for (unsigned i = 1, e = DefDesc.getNumOperands(); i != e; ++i)
993 NewMI.add(LoadMI.getOperand(i));
994
995 // Add branch opcode, inverting if necessary.
996 unsigned BCC = MI.getOperand(MI.getNumExplicitOperands() - 3).getImm();
997 if (!Invert)
999 NewMI.addImm(BCC);
1000
1001 // Copy condition portion
1002 NewMI.add({MI.getOperand(MI.getNumExplicitOperands() - 2),
1003 MI.getOperand(MI.getNumExplicitOperands() - 1)});
1004 NewMI.cloneMemRefs(LoadMI);
1005 return NewMI;
1006}
1007
1010 const DebugLoc &DL, Register DstReg, uint64_t Val,
1011 MachineInstr::MIFlag Flag, bool DstRenamable,
1012 bool DstIsDead) const {
1013 Register SrcReg = RISCV::X0;
1014
1015 // For RV32, allow a sign or unsigned 32 bit value.
1016 if (!STI.is64Bit() && !isInt<32>(Val)) {
1017 // If have a uimm32 it will still fit in a register so we can allow it.
1018 if (!isUInt<32>(Val))
1019 report_fatal_error("Should only materialize 32-bit constants for RV32");
1020
1021 // Sign extend for generateInstSeq.
1022 Val = SignExtend64<32>(Val);
1023 }
1024
1026 assert(!Seq.empty());
1027
1028 bool SrcRenamable = false;
1029 unsigned Num = 0;
1030
1031 for (const RISCVMatInt::Inst &Inst : Seq) {
1032 bool LastItem = ++Num == Seq.size();
1033 RegState DstRegState = getDeadRegState(DstIsDead && LastItem) |
1034 getRenamableRegState(DstRenamable);
1035 RegState SrcRegState = getKillRegState(SrcReg != RISCV::X0) |
1036 getRenamableRegState(SrcRenamable);
1037 switch (Inst.getOpndKind()) {
1038 case RISCVMatInt::Imm:
1039 BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
1040 .addReg(DstReg, RegState::Define | DstRegState)
1041 .addImm(Inst.getImm())
1042 .setMIFlag(Flag);
1043 break;
1044 case RISCVMatInt::RegX0:
1045 BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
1046 .addReg(DstReg, RegState::Define | DstRegState)
1047 .addReg(SrcReg, SrcRegState)
1048 .addReg(RISCV::X0)
1049 .setMIFlag(Flag);
1050 break;
1052 BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
1053 .addReg(DstReg, RegState::Define | DstRegState)
1054 .addReg(SrcReg, SrcRegState)
1055 .addReg(SrcReg, SrcRegState)
1056 .setMIFlag(Flag);
1057 break;
1059 BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
1060 .addReg(DstReg, RegState::Define | DstRegState)
1061 .addReg(SrcReg, SrcRegState)
1062 .addImm(Inst.getImm())
1063 .setMIFlag(Flag);
1064 break;
1065 }
1066
1067 // Only the first instruction has X0 as its source.
1068 SrcReg = DstReg;
1069 SrcRenamable = DstRenamable;
1070 }
1071}
1072
1074 switch (Opc) {
1075 default:
1076 return RISCVCC::COND_INVALID;
1077 case RISCV::BEQ:
1078 case RISCV::BEQI:
1079 case RISCV::CV_BEQIMM:
1080 case RISCV::QC_BEQI:
1081 case RISCV::QC_E_BEQI:
1082 case RISCV::NDS_BBC:
1083 case RISCV::NDS_BEQC:
1084 return RISCVCC::COND_EQ;
1085 case RISCV::BNE:
1086 case RISCV::BNEI:
1087 case RISCV::QC_BNEI:
1088 case RISCV::QC_E_BNEI:
1089 case RISCV::CV_BNEIMM:
1090 case RISCV::NDS_BBS:
1091 case RISCV::NDS_BNEC:
1092 return RISCVCC::COND_NE;
1093 case RISCV::BLT:
1094 case RISCV::QC_BLTI:
1095 case RISCV::QC_E_BLTI:
1096 return RISCVCC::COND_LT;
1097 case RISCV::BGE:
1098 case RISCV::QC_BGEI:
1099 case RISCV::QC_E_BGEI:
1100 return RISCVCC::COND_GE;
1101 case RISCV::BLTU:
1102 case RISCV::QC_BLTUI:
1103 case RISCV::QC_E_BLTUI:
1104 return RISCVCC::COND_LTU;
1105 case RISCV::BGEU:
1106 case RISCV::QC_BGEUI:
1107 case RISCV::QC_E_BGEUI:
1108 return RISCVCC::COND_GEU;
1109 }
1110}
1111
1113 int64_t C1) {
1114 switch (CC) {
1115 default:
1116 llvm_unreachable("Unexpected CC");
1117 case RISCVCC::COND_EQ:
1118 return C0 == C1;
1119 case RISCVCC::COND_NE:
1120 return C0 != C1;
1121 case RISCVCC::COND_LT:
1122 return C0 < C1;
1123 case RISCVCC::COND_GE:
1124 return C0 >= C1;
1125 case RISCVCC::COND_LTU:
1126 return (uint64_t)C0 < (uint64_t)C1;
1127 case RISCVCC::COND_GEU:
1128 return (uint64_t)C0 >= (uint64_t)C1;
1129 }
1130}
1131
1132// The contents of values added to Cond are not examined outside of
1133// RISCVInstrInfo, giving us flexibility in what to push to it. For RISCV, we
1134// push BranchOpcode, Reg1, Reg2.
1137 // Block ends with fall-through condbranch.
1138 assert(LastInst.getDesc().isConditionalBranch() &&
1139 "Unknown conditional branch");
1140 Target = LastInst.getOperand(2).getMBB();
1141 Cond.push_back(MachineOperand::CreateImm(LastInst.getOpcode()));
1142 Cond.push_back(LastInst.getOperand(0));
1143 Cond.push_back(LastInst.getOperand(1));
1144}
1145
1146static unsigned getInverseXqcicmOpcode(unsigned Opcode) {
1147 switch (Opcode) {
1148 default:
1149 llvm_unreachable("Unexpected Opcode");
1150 case RISCV::QC_MVEQ:
1151 return RISCV::QC_MVNE;
1152 case RISCV::QC_MVNE:
1153 return RISCV::QC_MVEQ;
1154 case RISCV::QC_MVLT:
1155 return RISCV::QC_MVGE;
1156 case RISCV::QC_MVGE:
1157 return RISCV::QC_MVLT;
1158 case RISCV::QC_MVLTU:
1159 return RISCV::QC_MVGEU;
1160 case RISCV::QC_MVGEU:
1161 return RISCV::QC_MVLTU;
1162 case RISCV::QC_MVEQI:
1163 return RISCV::QC_MVNEI;
1164 case RISCV::QC_MVNEI:
1165 return RISCV::QC_MVEQI;
1166 case RISCV::QC_MVLTI:
1167 return RISCV::QC_MVGEI;
1168 case RISCV::QC_MVGEI:
1169 return RISCV::QC_MVLTI;
1170 case RISCV::QC_MVLTUI:
1171 return RISCV::QC_MVGEUI;
1172 case RISCV::QC_MVGEUI:
1173 return RISCV::QC_MVLTUI;
1174 }
1175}
1176
1177unsigned RISCVCC::getBrCond(RISCVCC::CondCode CC, unsigned SelectOpc) {
1178 switch (SelectOpc) {
1179 default:
1180 switch (CC) {
1181 default:
1182 llvm_unreachable("Unexpected condition code!");
1183 case RISCVCC::COND_EQ:
1184 return RISCV::BEQ;
1185 case RISCVCC::COND_NE:
1186 return RISCV::BNE;
1187 case RISCVCC::COND_LT:
1188 return RISCV::BLT;
1189 case RISCVCC::COND_GE:
1190 return RISCV::BGE;
1191 case RISCVCC::COND_LTU:
1192 return RISCV::BLTU;
1193 case RISCVCC::COND_GEU:
1194 return RISCV::BGEU;
1195 }
1196 break;
1197 case RISCV::Select_GPR_Using_CC_Imm5_Zibi:
1198 switch (CC) {
1199 default:
1200 llvm_unreachable("Unexpected condition code!");
1201 case RISCVCC::COND_EQ:
1202 return RISCV::BEQI;
1203 case RISCVCC::COND_NE:
1204 return RISCV::BNEI;
1205 }
1206 break;
1207 case RISCV::Select_GPR_Using_CC_SImm5_CV:
1208 switch (CC) {
1209 default:
1210 llvm_unreachable("Unexpected condition code!");
1211 case RISCVCC::COND_EQ:
1212 return RISCV::CV_BEQIMM;
1213 case RISCVCC::COND_NE:
1214 return RISCV::CV_BNEIMM;
1215 }
1216 break;
1217 case RISCV::Select_GPRNoX0_Using_CC_SImm5NonZero_QC:
1218 switch (CC) {
1219 default:
1220 llvm_unreachable("Unexpected condition code!");
1221 case RISCVCC::COND_EQ:
1222 return RISCV::QC_BEQI;
1223 case RISCVCC::COND_NE:
1224 return RISCV::QC_BNEI;
1225 case RISCVCC::COND_LT:
1226 return RISCV::QC_BLTI;
1227 case RISCVCC::COND_GE:
1228 return RISCV::QC_BGEI;
1229 }
1230 break;
1231 case RISCV::Select_GPRNoX0_Using_CC_UImm5NonZero_QC:
1232 switch (CC) {
1233 default:
1234 llvm_unreachable("Unexpected condition code!");
1235 case RISCVCC::COND_LTU:
1236 return RISCV::QC_BLTUI;
1237 case RISCVCC::COND_GEU:
1238 return RISCV::QC_BGEUI;
1239 }
1240 break;
1241 case RISCV::Select_GPRNoX0_Using_CC_SImm16NonZero_QC:
1242 switch (CC) {
1243 default:
1244 llvm_unreachable("Unexpected condition code!");
1245 case RISCVCC::COND_EQ:
1246 return RISCV::QC_E_BEQI;
1247 case RISCVCC::COND_NE:
1248 return RISCV::QC_E_BNEI;
1249 case RISCVCC::COND_LT:
1250 return RISCV::QC_E_BLTI;
1251 case RISCVCC::COND_GE:
1252 return RISCV::QC_E_BGEI;
1253 }
1254 break;
1255 case RISCV::Select_GPRNoX0_Using_CC_UImm16NonZero_QC:
1256 switch (CC) {
1257 default:
1258 llvm_unreachable("Unexpected condition code!");
1259 case RISCVCC::COND_LTU:
1260 return RISCV::QC_E_BLTUI;
1261 case RISCVCC::COND_GEU:
1262 return RISCV::QC_E_BGEUI;
1263 }
1264 break;
1265 case RISCV::Select_GPR_Using_CC_UImmLog2XLen_NDS:
1266 switch (CC) {
1267 default:
1268 llvm_unreachable("Unexpected condition code!");
1269 case RISCVCC::COND_EQ:
1270 return RISCV::NDS_BBC;
1271 case RISCVCC::COND_NE:
1272 return RISCV::NDS_BBS;
1273 }
1274 break;
1275 case RISCV::Select_GPR_Using_CC_UImm7_NDS:
1276 switch (CC) {
1277 default:
1278 llvm_unreachable("Unexpected condition code!");
1279 case RISCVCC::COND_EQ:
1280 return RISCV::NDS_BEQC;
1281 case RISCVCC::COND_NE:
1282 return RISCV::NDS_BNEC;
1283 }
1284 break;
1285 }
1286}
1287
1289 switch (CC) {
1290 default:
1291 llvm_unreachable("Unrecognized conditional branch");
1292 case RISCVCC::COND_EQ:
1293 return RISCVCC::COND_NE;
1294 case RISCVCC::COND_NE:
1295 return RISCVCC::COND_EQ;
1296 case RISCVCC::COND_LT:
1297 return RISCVCC::COND_GE;
1298 case RISCVCC::COND_GE:
1299 return RISCVCC::COND_LT;
1300 case RISCVCC::COND_LTU:
1301 return RISCVCC::COND_GEU;
1302 case RISCVCC::COND_GEU:
1303 return RISCVCC::COND_LTU;
1304 }
1305}
1306
1307// Return inverse branch
1308unsigned RISCVCC::getInverseBranchOpcode(unsigned BCC) {
1309 switch (BCC) {
1310 default:
1311 llvm_unreachable("Unexpected branch opcode!");
1312 case RISCV::BEQ:
1313 return RISCV::BNE;
1314 case RISCV::BEQI:
1315 return RISCV::BNEI;
1316 case RISCV::BNE:
1317 return RISCV::BEQ;
1318 case RISCV::BNEI:
1319 return RISCV::BEQI;
1320 case RISCV::BLT:
1321 return RISCV::BGE;
1322 case RISCV::BGE:
1323 return RISCV::BLT;
1324 case RISCV::BLTU:
1325 return RISCV::BGEU;
1326 case RISCV::BGEU:
1327 return RISCV::BLTU;
1328 case RISCV::CV_BEQIMM:
1329 return RISCV::CV_BNEIMM;
1330 case RISCV::CV_BNEIMM:
1331 return RISCV::CV_BEQIMM;
1332 case RISCV::QC_BEQI:
1333 return RISCV::QC_BNEI;
1334 case RISCV::QC_BNEI:
1335 return RISCV::QC_BEQI;
1336 case RISCV::QC_BLTI:
1337 return RISCV::QC_BGEI;
1338 case RISCV::QC_BGEI:
1339 return RISCV::QC_BLTI;
1340 case RISCV::QC_BLTUI:
1341 return RISCV::QC_BGEUI;
1342 case RISCV::QC_BGEUI:
1343 return RISCV::QC_BLTUI;
1344 case RISCV::QC_E_BEQI:
1345 return RISCV::QC_E_BNEI;
1346 case RISCV::QC_E_BNEI:
1347 return RISCV::QC_E_BEQI;
1348 case RISCV::QC_E_BLTI:
1349 return RISCV::QC_E_BGEI;
1350 case RISCV::QC_E_BGEI:
1351 return RISCV::QC_E_BLTI;
1352 case RISCV::QC_E_BLTUI:
1353 return RISCV::QC_E_BGEUI;
1354 case RISCV::QC_E_BGEUI:
1355 return RISCV::QC_E_BLTUI;
1356 case RISCV::NDS_BBC:
1357 return RISCV::NDS_BBS;
1358 case RISCV::NDS_BBS:
1359 return RISCV::NDS_BBC;
1360 case RISCV::NDS_BEQC:
1361 return RISCV::NDS_BNEC;
1362 case RISCV::NDS_BNEC:
1363 return RISCV::NDS_BEQC;
1364 }
1365}
1366
1369 MachineBasicBlock *&FBB,
1371 bool AllowModify) const {
1372 TBB = FBB = nullptr;
1373 Cond.clear();
1374
1375 // If the block has no terminators, it just falls into the block after it.
1376 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
1377 if (I == MBB.end() || !isUnpredicatedTerminator(*I))
1378 return false;
1379
1380 // Count the number of terminators and find the first unconditional or
1381 // indirect branch.
1382 MachineBasicBlock::iterator FirstUncondOrIndirectBr = MBB.end();
1383 int NumTerminators = 0;
1384 for (auto J = I.getReverse(); J != MBB.rend() && isUnpredicatedTerminator(*J);
1385 J++) {
1386 NumTerminators++;
1387 if (J->getDesc().isUnconditionalBranch() ||
1388 J->getDesc().isIndirectBranch()) {
1389 FirstUncondOrIndirectBr = J.getReverse();
1390 }
1391 }
1392
1393 // If AllowModify is true, we can erase any terminators after
1394 // FirstUncondOrIndirectBR.
1395 if (AllowModify && FirstUncondOrIndirectBr != MBB.end()) {
1396 while (std::next(FirstUncondOrIndirectBr) != MBB.end()) {
1397 std::next(FirstUncondOrIndirectBr)->eraseFromParent();
1398 NumTerminators--;
1399 }
1400 I = FirstUncondOrIndirectBr;
1401 }
1402
1403 // We can't handle blocks that end in an indirect branch.
1404 if (I->getDesc().isIndirectBranch())
1405 return true;
1406
1407 // We can't handle Generic branch opcodes from Global ISel.
1408 if (I->isPreISelOpcode())
1409 return true;
1410
1411 // We can't handle blocks with more than 2 terminators.
1412 if (NumTerminators > 2)
1413 return true;
1414
1415 // Handle a single unconditional branch.
1416 if (NumTerminators == 1 && I->getDesc().isUnconditionalBranch()) {
1418 return false;
1419 }
1420
1421 // Handle a single conditional branch.
1422 if (NumTerminators == 1 && I->getDesc().isConditionalBranch()) {
1424 return false;
1425 }
1426
1427 // Handle a conditional branch followed by an unconditional branch.
1428 if (NumTerminators == 2 && std::prev(I)->getDesc().isConditionalBranch() &&
1429 I->getDesc().isUnconditionalBranch()) {
1430 parseCondBranch(*std::prev(I), TBB, Cond);
1431 FBB = getBranchDestBlock(*I);
1432 return false;
1433 }
1434
1435 // Otherwise, we can't handle this.
1436 return true;
1437}
1438
1440 int *BytesRemoved) const {
1441 if (BytesRemoved)
1442 *BytesRemoved = 0;
1443 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
1444 if (I == MBB.end())
1445 return 0;
1446
1447 if (!I->getDesc().isUnconditionalBranch() &&
1448 !I->getDesc().isConditionalBranch())
1449 return 0;
1450
1451 // Remove the branch.
1452 if (BytesRemoved)
1453 *BytesRemoved += getInstSizeInBytes(*I);
1454 I->eraseFromParent();
1455
1456 I = MBB.end();
1457
1458 if (I == MBB.begin())
1459 return 1;
1460 --I;
1461 if (!I->getDesc().isConditionalBranch())
1462 return 1;
1463
1464 // Remove the branch.
1465 if (BytesRemoved)
1466 *BytesRemoved += getInstSizeInBytes(*I);
1467 I->eraseFromParent();
1468 return 2;
1469}
1470
1471// Inserts a branch into the end of the specific MachineBasicBlock, returning
1472// the number of instructions inserted.
1475 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
1476 if (BytesAdded)
1477 *BytesAdded = 0;
1478
1479 // Shouldn't be a fall through.
1480 assert(TBB && "insertBranch must not be told to insert a fallthrough");
1481 assert((Cond.size() == 3 || Cond.size() == 0) &&
1482 "RISC-V branch conditions have two components!");
1483
1484 // Unconditional branch.
1485 if (Cond.empty()) {
1486 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(TBB);
1487 if (BytesAdded)
1488 *BytesAdded += getInstSizeInBytes(MI);
1489 return 1;
1490 }
1491
1492 // Either a one or two-way conditional branch.
1493 MachineInstr &CondMI = *BuildMI(&MBB, DL, get(Cond[0].getImm()))
1494 .add(Cond[1])
1495 .add(Cond[2])
1496 .addMBB(TBB);
1497 if (BytesAdded)
1498 *BytesAdded += getInstSizeInBytes(CondMI);
1499
1500 // One-way conditional branch.
1501 if (!FBB)
1502 return 1;
1503
1504 // Two-way conditional branch.
1505 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(FBB);
1506 if (BytesAdded)
1507 *BytesAdded += getInstSizeInBytes(MI);
1508 return 2;
1509}
1510
1512 MachineBasicBlock &DestBB,
1513 MachineBasicBlock &RestoreBB,
1514 const DebugLoc &DL, int64_t BrOffset,
1515 RegScavenger *RS) const {
1516 assert(RS && "RegScavenger required for long branching");
1517 assert(MBB.empty() &&
1518 "new block should be inserted for expanding unconditional branch");
1519 assert(MBB.pred_size() == 1);
1520 assert(RestoreBB.empty() &&
1521 "restore block should be inserted for restoring clobbered registers");
1522
1523 MachineFunction *MF = MBB.getParent();
1524 MachineRegisterInfo &MRI = MF->getRegInfo();
1527
1528 if (!isInt<32>(BrOffset))
1530 "Branch offsets outside of the signed 32-bit range not supported");
1531
1532 // FIXME: A virtual register must be used initially, as the register
1533 // scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch
1534 // uses the same workaround).
1535 Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRJALRRegClass);
1536 auto II = MBB.end();
1537 // We may also update the jump target to RestoreBB later.
1538 MachineInstr &MI = *BuildMI(MBB, II, DL, get(RISCV::PseudoJump))
1539 .addReg(ScratchReg, RegState::Define | RegState::Dead)
1540 .addMBB(&DestBB, RISCVII::MO_CALL);
1541
1542 RS->enterBasicBlockEnd(MBB);
1543 // When cf-protection-branch is enabled, we must use t2 (x7) for software
1544 // guarded branches to hold the landing pad label.
1545 bool HasCFBranch =
1546 MF->getInfo<RISCVMachineFunctionInfo>()->hasCFProtectionBranch();
1547 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
1548 if (HasCFBranch)
1549 RC = &RISCV::GPRX7RegClass;
1550 Register TmpGPR =
1551 RS->scavengeRegisterBackwards(*RC, MI.getIterator(),
1552 /*RestoreAfter=*/false, /*SpAdj=*/0,
1553 /*AllowSpill=*/false);
1554 if (TmpGPR.isValid())
1555 RS->setRegUsed(TmpGPR);
1556 else {
1557 // The case when there is no scavenged register needs special handling.
1558
1559 // Pick s11(or s1 for rve) because it doesn't make a difference.
1560 TmpGPR = STI.hasStdExtE() ? RISCV::X9 : RISCV::X27;
1561 // Force t2 if cf-protection-branch is enabled
1562 if (HasCFBranch)
1563 TmpGPR = RISCV::X7;
1564
1565 int FrameIndex = RVFI->getBranchRelaxationScratchFrameIndex();
1566 if (FrameIndex == -1)
1567 report_fatal_error("underestimated function size");
1568
1569 storeRegToStackSlot(MBB, MI, TmpGPR, /*IsKill=*/true, FrameIndex,
1570 &RISCV::GPRRegClass, Register());
1571 TRI->eliminateFrameIndex(std::prev(MI.getIterator()),
1572 /*SpAdj=*/0, /*FIOperandNum=*/1);
1573
1574 MI.getOperand(1).setMBB(&RestoreBB);
1575
1576 loadRegFromStackSlot(RestoreBB, RestoreBB.end(), TmpGPR, FrameIndex,
1577 &RISCV::GPRRegClass, Register());
1578 TRI->eliminateFrameIndex(RestoreBB.back(),
1579 /*SpAdj=*/0, /*FIOperandNum=*/1);
1580 }
1581
1582 MRI.replaceRegWith(ScratchReg, TmpGPR);
1583 MRI.clearVirtRegs();
1584}
1585
1588 assert((Cond.size() == 3) && "Invalid branch condition!");
1589
1591
1592 return false;
1593}
1594
1595// Return true if the instruction is a load immediate instruction (i.e.
1596// (ADDI x0, imm) or (BSETI x0, imm)).
1597static bool isLoadImm(const MachineInstr *MI, int64_t &Imm) {
1598 if (MI->getOpcode() == RISCV::ADDI && MI->getOperand(1).isReg() &&
1599 MI->getOperand(1).getReg() == RISCV::X0) {
1600 Imm = MI->getOperand(2).getImm();
1601 return true;
1602 }
1603 // BSETI can be used to create power of 2 constants. Only 2048 is currently
1604 // interesting because it is 1 more than the maximum ADDI constant.
1605 if (MI->getOpcode() == RISCV::BSETI && MI->getOperand(1).isReg() &&
1606 MI->getOperand(1).getReg() == RISCV::X0 &&
1607 MI->getOperand(2).getImm() == 11) {
1608 Imm = 2048;
1609 return true;
1610 }
1611 return false;
1612}
1613
1615 const MachineOperand &Op, int64_t &Imm) {
1616 // Either a load from immediate instruction or X0.
1617 if (!Op.isReg())
1618 return false;
1619
1620 Register Reg = Op.getReg();
1621 if (Reg == RISCV::X0) {
1622 Imm = 0;
1623 return true;
1624 }
1625 return Reg.isVirtual() && isLoadImm(MRI.getVRegDef(Reg), Imm);
1626}
1627
1629 bool IsSigned = false;
1630 bool IsEquality = false;
1631 switch (MI.getOpcode()) {
1632 default:
1633 return false;
1634 case RISCV::BEQ:
1635 case RISCV::BNE:
1636 IsEquality = true;
1637 break;
1638 case RISCV::BGE:
1639 case RISCV::BLT:
1640 IsSigned = true;
1641 break;
1642 case RISCV::BGEU:
1643 case RISCV::BLTU:
1644 break;
1645 }
1646
1647 MachineBasicBlock *MBB = MI.getParent();
1648 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1649
1650 const MachineOperand &LHS = MI.getOperand(0);
1651 const MachineOperand &RHS = MI.getOperand(1);
1652 MachineBasicBlock *TBB = MI.getOperand(2).getMBB();
1653
1654 RISCVCC::CondCode CC = getCondFromBranchOpc(MI.getOpcode());
1656
1657 // Canonicalize conditional branches which can be constant folded into
1658 // beqz or bnez. We can't modify the CFG here.
1659 int64_t C0, C1;
1660 if (isFromLoadImm(MRI, LHS, C0) && isFromLoadImm(MRI, RHS, C1)) {
1661 unsigned NewOpc = evaluateCondBranch(CC, C0, C1) ? RISCV::BEQ : RISCV::BNE;
1662 // Build the new branch and remove the old one.
1663 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
1664 .addReg(RISCV::X0)
1665 .addReg(RISCV::X0)
1666 .addMBB(TBB);
1667 MI.eraseFromParent();
1668 return true;
1669 }
1670
1671 if (IsEquality)
1672 return false;
1673
1674 // For two constants C0 and C1 from
1675 // ```
1676 // li Y, C0
1677 // li Z, C1
1678 // ```
1679 // 1. if C1 = C0 + 1
1680 // we can turn:
1681 // (a) blt Y, X -> bge X, Z
1682 // (b) bge Y, X -> blt X, Z
1683 //
1684 // 2. if C1 = C0 - 1
1685 // we can turn:
1686 // (a) blt X, Y -> bge Z, X
1687 // (b) bge X, Y -> blt Z, X
1688 //
1689 // To make sure this optimization is really beneficial, we only
1690 // optimize for cases where Y had only one use (i.e. only used by the branch).
1691 // Try to find the register for constant Z; return
1692 // invalid register otherwise.
1693 auto searchConst = [&](int64_t C1) -> Register {
1695 auto DefC1 = std::find_if(++II, E, [&](const MachineInstr &I) -> bool {
1696 int64_t Imm;
1697 return isLoadImm(&I, Imm) && Imm == C1 &&
1698 I.getOperand(0).getReg().isVirtual();
1699 });
1700 if (DefC1 != E)
1701 return DefC1->getOperand(0).getReg();
1702
1703 return Register();
1704 };
1705
1706 unsigned NewOpc = RISCVCC::getBrCond(getInverseBranchCondition(CC));
1707
1708 // Might be case 1.
1709 // Don't change 0 to 1 since we can use x0.
1710 // For unsigned cases changing -1U to 0 would be incorrect.
1711 // The incorrect case for signed would be INT_MAX, but isFromLoadImm can't
1712 // return that.
1713 if (isFromLoadImm(MRI, LHS, C0) && C0 != 0 && LHS.getReg().isVirtual() &&
1714 MRI.hasOneUse(LHS.getReg()) && (IsSigned || C0 != -1)) {
1715 assert((isInt<12>(C0) || C0 == 2048) && "Unexpected immediate");
1716 if (Register RegZ = searchConst(C0 + 1)) {
1717 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
1718 .add(RHS)
1719 .addReg(RegZ)
1720 .addMBB(TBB);
1721 // We might extend the live range of Z, clear its kill flag to
1722 // account for this.
1723 MRI.clearKillFlags(RegZ);
1724 MI.eraseFromParent();
1725 return true;
1726 }
1727 }
1728
1729 // Might be case 2.
1730 // For signed cases we don't want to change 0 since we can use x0.
1731 // For unsigned cases changing 0 to -1U would be incorrect.
1732 // The incorrect case for signed would be INT_MIN, but isFromLoadImm can't
1733 // return that.
1734 if (isFromLoadImm(MRI, RHS, C0) && C0 != 0 && RHS.getReg().isVirtual() &&
1735 MRI.hasOneUse(RHS.getReg())) {
1736 assert((isInt<12>(C0) || C0 == 2048) && "Unexpected immediate");
1737 if (Register RegZ = searchConst(C0 - 1)) {
1738 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
1739 .addReg(RegZ)
1740 .add(LHS)
1741 .addMBB(TBB);
1742 // We might extend the live range of Z, clear its kill flag to
1743 // account for this.
1744 MRI.clearKillFlags(RegZ);
1745 MI.eraseFromParent();
1746 return true;
1747 }
1748 }
1749
1750 return false;
1751}
1752
1755 assert(MI.getDesc().isBranch() && "Unexpected opcode!");
1756 // The branch target is always the last operand.
1757 int NumOp = MI.getNumExplicitOperands();
1758 return MI.getOperand(NumOp - 1).getMBB();
1759}
1760
1762 int64_t BrOffset) const {
1763 unsigned XLen = STI.getXLen();
1764 // Ideally we could determine the supported branch offset from the
1765 // RISCVII::FormMask, but this can't be used for Pseudo instructions like
1766 // PseudoBR.
1767 switch (BranchOp) {
1768 default:
1769 llvm_unreachable("Unexpected opcode!");
1770 case RISCV::NDS_BBC:
1771 case RISCV::NDS_BBS:
1772 case RISCV::NDS_BEQC:
1773 case RISCV::NDS_BNEC:
1774 return isInt<11>(BrOffset);
1775 case RISCV::BEQ:
1776 case RISCV::BNE:
1777 case RISCV::BLT:
1778 case RISCV::BGE:
1779 case RISCV::BLTU:
1780 case RISCV::BGEU:
1781 case RISCV::BEQI:
1782 case RISCV::BNEI:
1783 case RISCV::CV_BEQIMM:
1784 case RISCV::CV_BNEIMM:
1785 case RISCV::QC_BEQI:
1786 case RISCV::QC_BNEI:
1787 case RISCV::QC_BGEI:
1788 case RISCV::QC_BLTI:
1789 case RISCV::QC_BLTUI:
1790 case RISCV::QC_BGEUI:
1791 case RISCV::QC_E_BEQI:
1792 case RISCV::QC_E_BNEI:
1793 case RISCV::QC_E_BGEI:
1794 case RISCV::QC_E_BLTI:
1795 case RISCV::QC_E_BLTUI:
1796 case RISCV::QC_E_BGEUI:
1797 return isInt<13>(BrOffset);
1798 case RISCV::JAL:
1799 case RISCV::PseudoBR:
1800 return isInt<21>(BrOffset);
1801 case RISCV::PseudoJump:
1802 return isInt<32>(SignExtend64(BrOffset + 0x800, XLen));
1803 }
1804}
1805
1806// If the operation has a predicated pseudo instruction, return the pseudo
1807// instruction opcode. Otherwise, return RISCV::INSTRUCTION_LIST_END.
1808// TODO: Support more operations.
1809unsigned getPredicatedOpcode(unsigned Opcode) {
1810 // clang-format off
1811 switch (Opcode) {
1812 case RISCV::ADD: return RISCV::PseudoCCADD;
1813 case RISCV::SUB: return RISCV::PseudoCCSUB;
1814 case RISCV::SLL: return RISCV::PseudoCCSLL;
1815 case RISCV::SRL: return RISCV::PseudoCCSRL;
1816 case RISCV::SRA: return RISCV::PseudoCCSRA;
1817 case RISCV::AND: return RISCV::PseudoCCAND;
1818 case RISCV::OR: return RISCV::PseudoCCOR;
1819 case RISCV::XOR: return RISCV::PseudoCCXOR;
1820 case RISCV::MAX: return RISCV::PseudoCCMAX;
1821 case RISCV::MAXU: return RISCV::PseudoCCMAXU;
1822 case RISCV::MIN: return RISCV::PseudoCCMIN;
1823 case RISCV::MINU: return RISCV::PseudoCCMINU;
1824 case RISCV::MUL: return RISCV::PseudoCCMUL;
1825 case RISCV::LUI: return RISCV::PseudoCCLUI;
1826 case RISCV::QC_LI: return RISCV::PseudoCCQC_LI;
1827 case RISCV::QC_E_LI: return RISCV::PseudoCCQC_E_LI;
1828
1829 case RISCV::ADDI: return RISCV::PseudoCCADDI;
1830 case RISCV::SLLI: return RISCV::PseudoCCSLLI;
1831 case RISCV::SRLI: return RISCV::PseudoCCSRLI;
1832 case RISCV::SRAI: return RISCV::PseudoCCSRAI;
1833 case RISCV::ANDI: return RISCV::PseudoCCANDI;
1834 case RISCV::ORI: return RISCV::PseudoCCORI;
1835 case RISCV::XORI: return RISCV::PseudoCCXORI;
1836
1837 case RISCV::ADDW: return RISCV::PseudoCCADDW;
1838 case RISCV::SUBW: return RISCV::PseudoCCSUBW;
1839 case RISCV::SLLW: return RISCV::PseudoCCSLLW;
1840 case RISCV::SRLW: return RISCV::PseudoCCSRLW;
1841 case RISCV::SRAW: return RISCV::PseudoCCSRAW;
1842
1843 case RISCV::ADDIW: return RISCV::PseudoCCADDIW;
1844 case RISCV::SLLIW: return RISCV::PseudoCCSLLIW;
1845 case RISCV::SRLIW: return RISCV::PseudoCCSRLIW;
1846 case RISCV::SRAIW: return RISCV::PseudoCCSRAIW;
1847
1848 case RISCV::ANDN: return RISCV::PseudoCCANDN;
1849 case RISCV::ORN: return RISCV::PseudoCCORN;
1850 case RISCV::XNOR: return RISCV::PseudoCCXNOR;
1851
1852 case RISCV::NDS_BFOS: return RISCV::PseudoCCNDS_BFOS;
1853 case RISCV::NDS_BFOZ: return RISCV::PseudoCCNDS_BFOZ;
1854 }
1855 // clang-format on
1856
1857 return RISCV::INSTRUCTION_LIST_END;
1858}
1859
1860/// Identify instructions that can be folded into a CCMOV instruction, and
1861/// return the defining instruction.
1863 const MachineRegisterInfo &MRI,
1864 const TargetInstrInfo *TII,
1865 const RISCVSubtarget &STI) {
1866 if (!Reg.isVirtual())
1867 return nullptr;
1868 if (!MRI.hasOneNonDBGUse(Reg))
1869 return nullptr;
1870 MachineInstr *MI = MRI.getVRegDef(Reg);
1871 if (!MI)
1872 return nullptr;
1873
1874 if (!STI.hasShortForwardBranchIMinMax() &&
1875 (MI->getOpcode() == RISCV::MAX || MI->getOpcode() == RISCV::MIN ||
1876 MI->getOpcode() == RISCV::MINU || MI->getOpcode() == RISCV::MAXU))
1877 return nullptr;
1878
1879 if (!STI.hasShortForwardBranchIMul() && MI->getOpcode() == RISCV::MUL)
1880 return nullptr;
1881
1882 // Check if MI can be predicated and folded into the CCMOV.
1883 if (getPredicatedOpcode(MI->getOpcode()) == RISCV::INSTRUCTION_LIST_END)
1884 return nullptr;
1885 // Don't predicate li idiom.
1886 if (MI->getOpcode() == RISCV::ADDI && MI->getOperand(1).isReg() &&
1887 MI->getOperand(1).getReg() == RISCV::X0)
1888 return nullptr;
1889 // Check if MI has any other defs or physreg uses.
1890 for (const MachineOperand &MO : llvm::drop_begin(MI->operands())) {
1891 // Reject frame index operands, PEI can't handle the predicated pseudos.
1892 if (MO.isFI() || MO.isCPI() || MO.isJTI())
1893 return nullptr;
1894 if (!MO.isReg())
1895 continue;
1896 // MI can't have any tied operands, that would conflict with predication.
1897 if (MO.isTied())
1898 return nullptr;
1899 if (MO.isDef())
1900 return nullptr;
1901 // Allow constant physregs.
1902 if (MO.getReg().isPhysical() && !MRI.isConstantPhysReg(MO.getReg()))
1903 return nullptr;
1904 }
1905 bool DontMoveAcrossStores = true;
1906 if (!MI->isSafeToMove(DontMoveAcrossStores))
1907 return nullptr;
1908 return MI;
1909}
1910
1914 bool PreferFalse) const {
1915 assert(MI.getOpcode() == RISCV::PseudoCCMOVGPR &&
1916 "Unknown select instruction");
1917 if (!STI.hasShortForwardBranchIALU())
1918 return nullptr;
1919
1920 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1922 canFoldAsPredicatedOp(MI.getOperand(2).getReg(), MRI, this, STI);
1923 bool Invert = !DefMI;
1924 if (!DefMI)
1925 DefMI = canFoldAsPredicatedOp(MI.getOperand(1).getReg(), MRI, this, STI);
1926 if (!DefMI)
1927 return nullptr;
1928
1929 // Find new register class to use.
1930 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
1931 Register DestReg = MI.getOperand(0).getReg();
1932 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1933 if (!MRI.constrainRegClass(DestReg, PreviousClass))
1934 return nullptr;
1935
1936 unsigned PredOpc = getPredicatedOpcode(DefMI->getOpcode());
1937 assert(PredOpc != RISCV::INSTRUCTION_LIST_END && "Unexpected opcode!");
1938
1939 // Create a new predicated version of DefMI.
1940 MachineInstrBuilder NewMI =
1941 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(PredOpc), DestReg);
1942
1943 // Copy the false register.
1944 NewMI.add(FalseReg);
1945
1946 // Copy all the DefMI operands.
1947 const MCInstrDesc &DefDesc = DefMI->getDesc();
1948 for (unsigned i = 1, e = DefDesc.getNumOperands(); i != e; ++i)
1949 NewMI.add(DefMI->getOperand(i));
1950
1951 // Add branch opcode, inverting if necessary.
1952 unsigned BCCOpcode = MI.getOperand(MI.getNumExplicitOperands() - 3).getImm();
1953 if (Invert)
1954 BCCOpcode = RISCVCC::getInverseBranchOpcode(BCCOpcode);
1955 NewMI.addImm(BCCOpcode);
1956
1957 // Copy the condition portion.
1958 NewMI.add(MI.getOperand(MI.getNumExplicitOperands() - 2));
1959 NewMI.add(MI.getOperand(MI.getNumExplicitOperands() - 1));
1960
1961 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1962 SeenMIs.insert(NewMI);
1963 SeenMIs.erase(DefMI);
1964
1965 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1966 // DefMI would be invalid when transferred inside the loop. Checking for a
1967 // loop is expensive, but at least remove kill flags if they are in different
1968 // BBs.
1969 if (DefMI->getParent() != MI.getParent())
1970 NewMI->clearKillInfo();
1971
1972 // The caller will erase MI, but not DefMI.
1973 DefMI->eraseFromParent();
1974 return NewMI;
1975}
1976
1978 if (MI.isMetaInstruction())
1979 return 0;
1980
1981 unsigned Opcode = MI.getOpcode();
1982
1983 if (Opcode == TargetOpcode::INLINEASM ||
1984 Opcode == TargetOpcode::INLINEASM_BR) {
1985 const MachineFunction &MF = *MI.getParent()->getParent();
1986 return getInlineAsmLength(MI.getOperand(0).getSymbolName(),
1987 MF.getTarget().getMCAsmInfo());
1988 }
1989
1990 if (requiresNTLHint(MI)) {
1991 if (STI.hasStdExtZca()) {
1992 if (isCompressibleInst(MI, STI))
1993 return 4; // c.ntl.all + c.load/c.store
1994 return 6; // c.ntl.all + load/store
1995 }
1996 return 8; // ntl.all + load/store
1997 }
1998
1999 if (Opcode == TargetOpcode::BUNDLE)
2000 return getInstBundleSize(MI);
2001
2002 if (MI.getParent() && MI.getParent()->getParent()) {
2003 if (isCompressibleInst(MI, STI))
2004 return 2;
2005 }
2006
2007 switch (Opcode) {
2008 case RISCV::PseudoMV_FPR16INX:
2009 case RISCV::PseudoMV_FPR32INX:
2010 // MV is always compressible to either c.mv or c.li rd, 0.
2011 return STI.hasStdExtZca() ? 2 : 4;
2012 // Below cases are for short forward branch pseudos
2013 case RISCV::PseudoCCMOVGPRNoX0:
2014 return get(MI.getOperand(MI.getNumExplicitOperands() - 3).getImm())
2015 .getSize() +
2016 2;
2017 case RISCV::PseudoCCMOVGPR:
2018 case RISCV::PseudoCCADD:
2019 case RISCV::PseudoCCSUB:
2020 case RISCV::PseudoCCSLL:
2021 case RISCV::PseudoCCSRL:
2022 case RISCV::PseudoCCSRA:
2023 case RISCV::PseudoCCAND:
2024 case RISCV::PseudoCCOR:
2025 case RISCV::PseudoCCXOR:
2026 case RISCV::PseudoCCADDI:
2027 case RISCV::PseudoCCANDI:
2028 case RISCV::PseudoCCORI:
2029 case RISCV::PseudoCCXORI:
2030 case RISCV::PseudoCCLUI:
2031 case RISCV::PseudoCCSLLI:
2032 case RISCV::PseudoCCSRLI:
2033 case RISCV::PseudoCCSRAI:
2034 case RISCV::PseudoCCADDW:
2035 case RISCV::PseudoCCSUBW:
2036 case RISCV::PseudoCCSLLW:
2037 case RISCV::PseudoCCSRLW:
2038 case RISCV::PseudoCCSRAW:
2039 case RISCV::PseudoCCADDIW:
2040 case RISCV::PseudoCCSLLIW:
2041 case RISCV::PseudoCCSRLIW:
2042 case RISCV::PseudoCCSRAIW:
2043 case RISCV::PseudoCCANDN:
2044 case RISCV::PseudoCCORN:
2045 case RISCV::PseudoCCXNOR:
2046 case RISCV::PseudoCCMAX:
2047 case RISCV::PseudoCCMIN:
2048 case RISCV::PseudoCCMAXU:
2049 case RISCV::PseudoCCMINU:
2050 case RISCV::PseudoCCMUL:
2051 case RISCV::PseudoCCLB:
2052 case RISCV::PseudoCCLH:
2053 case RISCV::PseudoCCLW:
2054 case RISCV::PseudoCCLHU:
2055 case RISCV::PseudoCCLBU:
2056 case RISCV::PseudoCCLWU:
2057 case RISCV::PseudoCCLD:
2058 case RISCV::PseudoCCQC_LI:
2059 return get(MI.getOperand(MI.getNumExplicitOperands() - 3).getImm())
2060 .getSize() +
2061 4;
2062 case RISCV::PseudoCCQC_E_LI:
2063 case RISCV::PseudoCCQC_E_LB:
2064 case RISCV::PseudoCCQC_E_LH:
2065 case RISCV::PseudoCCQC_E_LW:
2066 case RISCV::PseudoCCQC_E_LHU:
2067 case RISCV::PseudoCCQC_E_LBU:
2068 return get(MI.getOperand(MI.getNumExplicitOperands() - 3).getImm())
2069 .getSize() +
2070 6;
2071 case TargetOpcode::STACKMAP:
2072 // The upper bound for a stackmap intrinsic is the full length of its shadow
2074 case TargetOpcode::PATCHPOINT:
2075 // The size of the patchpoint intrinsic is the number of bytes requested
2077 case TargetOpcode::STATEPOINT: {
2078 // The size of the statepoint intrinsic is the number of bytes requested
2079 unsigned NumBytes = StatepointOpers(&MI).getNumPatchBytes();
2080 // No patch bytes means at most a PseudoCall is emitted
2081 return std::max(NumBytes, 8U);
2082 }
2083 case TargetOpcode::PATCHABLE_FUNCTION_ENTER:
2084 case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
2085 case TargetOpcode::PATCHABLE_TAIL_CALL: {
2086 const MachineFunction &MF = *MI.getParent()->getParent();
2087 const Function &F = MF.getFunction();
2088 if (Opcode == TargetOpcode::PATCHABLE_FUNCTION_ENTER &&
2089 F.hasFnAttribute("patchable-function-entry")) {
2090 unsigned Num =
2091 F.getFnAttributeAsParsedInteger("patchable-function-entry");
2092 // Number of C.NOP or NOP
2093 return (STI.hasStdExtZca() ? 2 : 4) * Num;
2094 }
2095 // XRay uses C.JAL + 21 or 33 C.NOP for each sled in RV32 and RV64,
2096 // respectively.
2097 return STI.is64Bit() ? 68 : 44;
2098 }
2099 default:
2100 return get(Opcode).getSize();
2101 }
2102}
2103
2105 const unsigned Opcode = MI.getOpcode();
2106 switch (Opcode) {
2107 default:
2108 break;
2109 case RISCV::FSGNJ_D:
2110 case RISCV::FSGNJ_S:
2111 case RISCV::FSGNJ_H:
2112 case RISCV::FSGNJ_D_INX:
2113 case RISCV::FSGNJ_D_IN32X:
2114 case RISCV::FSGNJ_S_INX:
2115 case RISCV::FSGNJ_H_INX:
2116 // The canonical floating-point move is fsgnj rd, rs, rs.
2117 return MI.getOperand(1).isReg() && MI.getOperand(2).isReg() &&
2118 MI.getOperand(1).getReg() == MI.getOperand(2).getReg();
2119 case RISCV::ADDI:
2120 case RISCV::ORI:
2121 case RISCV::XORI:
2122 return (MI.getOperand(1).isReg() &&
2123 MI.getOperand(1).getReg() == RISCV::X0) ||
2124 (MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0);
2125 }
2126 return MI.isAsCheapAsAMove();
2127}
2128
2129std::optional<DestSourcePair>
2131 if (MI.isMoveReg())
2132 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
2133 switch (MI.getOpcode()) {
2134 default:
2135 break;
2136 case RISCV::ADD:
2137 case RISCV::OR:
2138 case RISCV::XOR:
2139 if (MI.getOperand(1).isReg() && MI.getOperand(1).getReg() == RISCV::X0 &&
2140 MI.getOperand(2).isReg())
2141 return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
2142 if (MI.getOperand(2).isReg() && MI.getOperand(2).getReg() == RISCV::X0 &&
2143 MI.getOperand(1).isReg())
2144 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
2145 break;
2146 case RISCV::ADDI:
2147 // Operand 1 can be a frameindex but callers expect registers
2148 if (MI.getOperand(1).isReg() && MI.getOperand(2).isImm() &&
2149 MI.getOperand(2).getImm() == 0)
2150 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
2151 break;
2152 case RISCV::SUB:
2153 if (MI.getOperand(2).isReg() && MI.getOperand(2).getReg() == RISCV::X0 &&
2154 MI.getOperand(1).isReg())
2155 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
2156 break;
2157 case RISCV::SH1ADD:
2158 case RISCV::SH1ADD_UW:
2159 case RISCV::SH2ADD:
2160 case RISCV::SH2ADD_UW:
2161 case RISCV::SH3ADD:
2162 case RISCV::SH3ADD_UW:
2163 if (MI.getOperand(1).isReg() && MI.getOperand(1).getReg() == RISCV::X0 &&
2164 MI.getOperand(2).isReg())
2165 return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
2166 break;
2167 case RISCV::FSGNJ_D:
2168 case RISCV::FSGNJ_S:
2169 case RISCV::FSGNJ_H:
2170 case RISCV::FSGNJ_D_INX:
2171 case RISCV::FSGNJ_D_IN32X:
2172 case RISCV::FSGNJ_S_INX:
2173 case RISCV::FSGNJ_H_INX:
2174 // The canonical floating-point move is fsgnj rd, rs, rs.
2175 if (MI.getOperand(1).isReg() && MI.getOperand(2).isReg() &&
2176 MI.getOperand(1).getReg() == MI.getOperand(2).getReg())
2177 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
2178 break;
2179 }
2180 return std::nullopt;
2181}
2182
2184 if (ForceMachineCombinerStrategy.getNumOccurrences() == 0) {
2185 // The option is unused. Choose Local strategy only for in-order cores. When
2186 // scheduling model is unspecified, use MinInstrCount strategy as more
2187 // generic one.
2188 const auto &SchedModel = STI.getSchedModel();
2189 return (!SchedModel.hasInstrSchedModel() || SchedModel.isOutOfOrder())
2192 }
2193 // The strategy was forced by the option.
2195}
2196
2198 MachineInstr &Root, unsigned &Pattern,
2199 SmallVectorImpl<MachineInstr *> &InsInstrs) const {
2200 int16_t FrmOpIdx =
2201 RISCV::getNamedOperandIdx(Root.getOpcode(), RISCV::OpName::frm);
2202 if (FrmOpIdx < 0) {
2203 assert(all_of(InsInstrs,
2204 [](MachineInstr *MI) {
2205 return RISCV::getNamedOperandIdx(MI->getOpcode(),
2206 RISCV::OpName::frm) < 0;
2207 }) &&
2208 "New instructions require FRM whereas the old one does not have it");
2209 return;
2210 }
2211
2212 const MachineOperand &FRM = Root.getOperand(FrmOpIdx);
2213 MachineFunction &MF = *Root.getMF();
2214
2215 for (auto *NewMI : InsInstrs) {
2216 // We'd already added the FRM operand.
2217 if (static_cast<unsigned>(RISCV::getNamedOperandIdx(
2218 NewMI->getOpcode(), RISCV::OpName::frm)) != NewMI->getNumOperands())
2219 continue;
2220 MachineInstrBuilder MIB(MF, NewMI);
2221 MIB.add(FRM);
2222 if (FRM.getImm() == RISCVFPRndMode::DYN)
2223 MIB.addUse(RISCV::FRM, RegState::Implicit);
2224 }
2225}
2226
2227static bool isFADD(unsigned Opc) {
2228 switch (Opc) {
2229 default:
2230 return false;
2231 case RISCV::FADD_H:
2232 case RISCV::FADD_S:
2233 case RISCV::FADD_D:
2234 return true;
2235 }
2236}
2237
2238static bool isFSUB(unsigned Opc) {
2239 switch (Opc) {
2240 default:
2241 return false;
2242 case RISCV::FSUB_H:
2243 case RISCV::FSUB_S:
2244 case RISCV::FSUB_D:
2245 return true;
2246 }
2247}
2248
2249static bool isFMUL(unsigned Opc) {
2250 switch (Opc) {
2251 default:
2252 return false;
2253 case RISCV::FMUL_H:
2254 case RISCV::FMUL_S:
2255 case RISCV::FMUL_D:
2256 return true;
2257 }
2258}
2259
2260bool RISCVInstrInfo::isVectorAssociativeAndCommutative(const MachineInstr &Inst,
2261 bool Invert) const {
2262#define OPCODE_LMUL_CASE(OPC) \
2263 case RISCV::OPC##_M1: \
2264 case RISCV::OPC##_M2: \
2265 case RISCV::OPC##_M4: \
2266 case RISCV::OPC##_M8: \
2267 case RISCV::OPC##_MF2: \
2268 case RISCV::OPC##_MF4: \
2269 case RISCV::OPC##_MF8
2270
2271#define OPCODE_LMUL_MASK_CASE(OPC) \
2272 case RISCV::OPC##_M1_MASK: \
2273 case RISCV::OPC##_M2_MASK: \
2274 case RISCV::OPC##_M4_MASK: \
2275 case RISCV::OPC##_M8_MASK: \
2276 case RISCV::OPC##_MF2_MASK: \
2277 case RISCV::OPC##_MF4_MASK: \
2278 case RISCV::OPC##_MF8_MASK
2279
2280 unsigned Opcode = Inst.getOpcode();
2281 if (Invert) {
2282 if (auto InvOpcode = getInverseOpcode(Opcode))
2283 Opcode = *InvOpcode;
2284 else
2285 return false;
2286 }
2287
2288 // clang-format off
2289 switch (Opcode) {
2290 default:
2291 return false;
2292 OPCODE_LMUL_CASE(PseudoVADD_VV):
2293 OPCODE_LMUL_MASK_CASE(PseudoVADD_VV):
2294 OPCODE_LMUL_CASE(PseudoVMUL_VV):
2295 OPCODE_LMUL_MASK_CASE(PseudoVMUL_VV):
2296 return true;
2297 }
2298 // clang-format on
2299
2300#undef OPCODE_LMUL_MASK_CASE
2301#undef OPCODE_LMUL_CASE
2302}
2303
2304bool RISCVInstrInfo::areRVVInstsReassociable(const MachineInstr &Root,
2305 const MachineInstr &Prev) const {
2306 if (!areOpcodesEqualOrInverse(Root.getOpcode(), Prev.getOpcode()))
2307 return false;
2308
2309 assert(Root.getMF() == Prev.getMF());
2310 const MachineRegisterInfo *MRI = &Root.getMF()->getRegInfo();
2311 const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
2312
2313 // Make sure vtype operands are also the same.
2314 const MCInstrDesc &Desc = get(Root.getOpcode());
2315 const uint64_t TSFlags = Desc.TSFlags;
2316
2317 auto checkImmOperand = [&](unsigned OpIdx) {
2318 return Root.getOperand(OpIdx).getImm() == Prev.getOperand(OpIdx).getImm();
2319 };
2320
2321 auto checkRegOperand = [&](unsigned OpIdx) {
2322 return Root.getOperand(OpIdx).getReg() == Prev.getOperand(OpIdx).getReg();
2323 };
2324
2325 // PassThru
2326 // TODO: Potentially we can loosen the condition to consider Root to be
2327 // associable with Prev if Root has NoReg as passthru. In which case we
2328 // also need to loosen the condition on vector policies between these.
2329 if (!checkRegOperand(1))
2330 return false;
2331
2332 // SEW
2333 if (RISCVII::hasSEWOp(TSFlags) &&
2334 !checkImmOperand(RISCVII::getSEWOpNum(Desc)))
2335 return false;
2336
2337 // Mask
2338 if (RISCVII::usesMaskPolicy(TSFlags)) {
2339 const MachineBasicBlock *MBB = Root.getParent();
2342 Register MI1VReg;
2343
2344 bool SeenMI2 = false;
2345 for (auto End = MBB->rend(), It = It1; It != End; ++It) {
2346 if (It == It2) {
2347 SeenMI2 = true;
2348 if (!MI1VReg.isValid())
2349 // There is no V0 def between Root and Prev; they're sharing the
2350 // same V0.
2351 break;
2352 }
2353
2354 if (It->modifiesRegister(RISCV::V0, TRI)) {
2355 Register SrcReg = It->getOperand(1).getReg();
2356 // If it's not VReg it'll be more difficult to track its defs, so
2357 // bailing out here just to be safe.
2358 if (!SrcReg.isVirtual())
2359 return false;
2360
2361 if (!MI1VReg.isValid()) {
2362 // This is the V0 def for Root.
2363 MI1VReg = SrcReg;
2364 continue;
2365 }
2366
2367 // Some random mask updates.
2368 if (!SeenMI2)
2369 continue;
2370
2371 // This is the V0 def for Prev; check if it's the same as that of
2372 // Root.
2373 if (MI1VReg != SrcReg)
2374 return false;
2375 else
2376 break;
2377 }
2378 }
2379
2380 // If we haven't encountered Prev, it's likely that this function was
2381 // called in a wrong way (e.g. Root is before Prev).
2382 assert(SeenMI2 && "Prev is expected to appear before Root");
2383 }
2384
2385 // Tail / Mask policies
2386 if (RISCVII::hasVecPolicyOp(TSFlags) &&
2387 !checkImmOperand(RISCVII::getVecPolicyOpNum(Desc)))
2388 return false;
2389
2390 // VL
2391 if (RISCVII::hasVLOp(TSFlags)) {
2392 unsigned OpIdx = RISCVII::getVLOpNum(Desc);
2393 const MachineOperand &Op1 = Root.getOperand(OpIdx);
2394 const MachineOperand &Op2 = Prev.getOperand(OpIdx);
2395 if (Op1.getType() != Op2.getType())
2396 return false;
2397 switch (Op1.getType()) {
2399 if (Op1.getReg() != Op2.getReg())
2400 return false;
2401 break;
2403 if (Op1.getImm() != Op2.getImm())
2404 return false;
2405 break;
2406 default:
2407 llvm_unreachable("Unrecognized VL operand type");
2408 }
2409 }
2410
2411 // Rounding modes
2412 if (int Idx = RISCVII::getFRMOpNum(Desc); Idx >= 0 && !checkImmOperand(Idx))
2413 return false;
2414 if (int Idx = RISCVII::getVXRMOpNum(Desc); Idx >= 0 && !checkImmOperand(Idx))
2415 return false;
2416
2417 return true;
2418}
2419
2420// Most of our RVV pseudos have passthru operand, so the real operands
2421// start from index = 2.
2422bool RISCVInstrInfo::hasReassociableVectorSibling(const MachineInstr &Inst,
2423 bool &Commuted) const {
2424 const MachineBasicBlock *MBB = Inst.getParent();
2425 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
2427 "Expect the present of passthrough operand.");
2428 MachineInstr *MI1 = MRI.getUniqueVRegDef(Inst.getOperand(2).getReg());
2429 MachineInstr *MI2 = MRI.getUniqueVRegDef(Inst.getOperand(3).getReg());
2430
2431 // If only one operand has the same or inverse opcode and it's the second
2432 // source operand, the operands must be commuted.
2433 Commuted = !areRVVInstsReassociable(Inst, *MI1) &&
2434 areRVVInstsReassociable(Inst, *MI2);
2435 if (Commuted)
2436 std::swap(MI1, MI2);
2437
2438 return areRVVInstsReassociable(Inst, *MI1) &&
2439 (isVectorAssociativeAndCommutative(*MI1) ||
2440 isVectorAssociativeAndCommutative(*MI1, /* Invert */ true)) &&
2442 MRI.hasOneNonDBGUse(MI1->getOperand(0).getReg());
2443}
2444
2446 const MachineInstr &Inst, const MachineBasicBlock *MBB) const {
2447 if (!isVectorAssociativeAndCommutative(Inst) &&
2448 !isVectorAssociativeAndCommutative(Inst, /*Invert=*/true))
2450
2451 const MachineOperand &Op1 = Inst.getOperand(2);
2452 const MachineOperand &Op2 = Inst.getOperand(3);
2453 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
2454
2455 // We need virtual register definitions for the operands that we will
2456 // reassociate.
2457 MachineInstr *MI1 = nullptr;
2458 MachineInstr *MI2 = nullptr;
2459 if (Op1.isReg() && Op1.getReg().isVirtual())
2460 MI1 = MRI.getUniqueVRegDef(Op1.getReg());
2461 if (Op2.isReg() && Op2.getReg().isVirtual())
2462 MI2 = MRI.getUniqueVRegDef(Op2.getReg());
2463
2464 // And at least one operand must be defined in MBB.
2465 return MI1 && MI2 && (MI1->getParent() == MBB || MI2->getParent() == MBB);
2466}
2467
2469 const MachineInstr &Root, unsigned Pattern,
2470 std::array<unsigned, 5> &OperandIndices) const {
2472 if (RISCV::getRVVMCOpcode(Root.getOpcode())) {
2473 // Skip the passthrough operand, so increment all indices by one.
2474 for (unsigned I = 0; I < 5; ++I)
2475 ++OperandIndices[I];
2476 }
2477}
2478
2480 bool &Commuted) const {
2481 if (isVectorAssociativeAndCommutative(Inst) ||
2482 isVectorAssociativeAndCommutative(Inst, /*Invert=*/true))
2483 return hasReassociableVectorSibling(Inst, Commuted);
2484
2485 if (!TargetInstrInfo::hasReassociableSibling(Inst, Commuted))
2486 return false;
2487
2488 const MachineRegisterInfo &MRI = Inst.getMF()->getRegInfo();
2489 unsigned OperandIdx = Commuted ? 2 : 1;
2490 const MachineInstr &Sibling =
2491 *MRI.getVRegDef(Inst.getOperand(OperandIdx).getReg());
2492
2493 int16_t InstFrmOpIdx =
2494 RISCV::getNamedOperandIdx(Inst.getOpcode(), RISCV::OpName::frm);
2495 int16_t SiblingFrmOpIdx =
2496 RISCV::getNamedOperandIdx(Sibling.getOpcode(), RISCV::OpName::frm);
2497
2498 return (InstFrmOpIdx < 0 && SiblingFrmOpIdx < 0) ||
2499 RISCV::hasEqualFRM(Inst, Sibling);
2500}
2501
2503 bool Invert) const {
2504 if (isVectorAssociativeAndCommutative(Inst, Invert))
2505 return true;
2506
2507 unsigned Opc = Inst.getOpcode();
2508 if (Invert) {
2509 auto InverseOpcode = getInverseOpcode(Opc);
2510 if (!InverseOpcode)
2511 return false;
2512 Opc = *InverseOpcode;
2513 }
2514
2515 if (isFADD(Opc) || isFMUL(Opc))
2518
2519 switch (Opc) {
2520 default:
2521 return false;
2522 case RISCV::ADD:
2523 case RISCV::ADDW:
2524 case RISCV::AND:
2525 case RISCV::OR:
2526 case RISCV::XOR:
2527 // From RISC-V ISA spec, if both the high and low bits of the same product
2528 // are required, then the recommended code sequence is:
2529 //
2530 // MULH[[S]U] rdh, rs1, rs2
2531 // MUL rdl, rs1, rs2
2532 // (source register specifiers must be in same order and rdh cannot be the
2533 // same as rs1 or rs2)
2534 //
2535 // Microarchitectures can then fuse these into a single multiply operation
2536 // instead of performing two separate multiplies.
2537 // MachineCombiner may reassociate MUL operands and lose the fusion
2538 // opportunity.
2539 case RISCV::MUL:
2540 case RISCV::MULW:
2541 case RISCV::MIN:
2542 case RISCV::MINU:
2543 case RISCV::MAX:
2544 case RISCV::MAXU:
2545 case RISCV::FMIN_H:
2546 case RISCV::FMIN_S:
2547 case RISCV::FMIN_D:
2548 case RISCV::FMAX_H:
2549 case RISCV::FMAX_S:
2550 case RISCV::FMAX_D:
2551 return true;
2552 }
2553
2554 return false;
2555}
2556
2557std::optional<unsigned>
2558RISCVInstrInfo::getInverseOpcode(unsigned Opcode) const {
2559#define RVV_OPC_LMUL_CASE(OPC, INV) \
2560 case RISCV::OPC##_M1: \
2561 return RISCV::INV##_M1; \
2562 case RISCV::OPC##_M2: \
2563 return RISCV::INV##_M2; \
2564 case RISCV::OPC##_M4: \
2565 return RISCV::INV##_M4; \
2566 case RISCV::OPC##_M8: \
2567 return RISCV::INV##_M8; \
2568 case RISCV::OPC##_MF2: \
2569 return RISCV::INV##_MF2; \
2570 case RISCV::OPC##_MF4: \
2571 return RISCV::INV##_MF4; \
2572 case RISCV::OPC##_MF8: \
2573 return RISCV::INV##_MF8
2574
2575#define RVV_OPC_LMUL_MASK_CASE(OPC, INV) \
2576 case RISCV::OPC##_M1_MASK: \
2577 return RISCV::INV##_M1_MASK; \
2578 case RISCV::OPC##_M2_MASK: \
2579 return RISCV::INV##_M2_MASK; \
2580 case RISCV::OPC##_M4_MASK: \
2581 return RISCV::INV##_M4_MASK; \
2582 case RISCV::OPC##_M8_MASK: \
2583 return RISCV::INV##_M8_MASK; \
2584 case RISCV::OPC##_MF2_MASK: \
2585 return RISCV::INV##_MF2_MASK; \
2586 case RISCV::OPC##_MF4_MASK: \
2587 return RISCV::INV##_MF4_MASK; \
2588 case RISCV::OPC##_MF8_MASK: \
2589 return RISCV::INV##_MF8_MASK
2590
2591 switch (Opcode) {
2592 default:
2593 return std::nullopt;
2594 case RISCV::FADD_H:
2595 return RISCV::FSUB_H;
2596 case RISCV::FADD_S:
2597 return RISCV::FSUB_S;
2598 case RISCV::FADD_D:
2599 return RISCV::FSUB_D;
2600 case RISCV::FSUB_H:
2601 return RISCV::FADD_H;
2602 case RISCV::FSUB_S:
2603 return RISCV::FADD_S;
2604 case RISCV::FSUB_D:
2605 return RISCV::FADD_D;
2606 case RISCV::ADD:
2607 return RISCV::SUB;
2608 case RISCV::SUB:
2609 return RISCV::ADD;
2610 case RISCV::ADDW:
2611 return RISCV::SUBW;
2612 case RISCV::SUBW:
2613 return RISCV::ADDW;
2614 // clang-format off
2615 RVV_OPC_LMUL_CASE(PseudoVADD_VV, PseudoVSUB_VV);
2616 RVV_OPC_LMUL_MASK_CASE(PseudoVADD_VV, PseudoVSUB_VV);
2617 RVV_OPC_LMUL_CASE(PseudoVSUB_VV, PseudoVADD_VV);
2618 RVV_OPC_LMUL_MASK_CASE(PseudoVSUB_VV, PseudoVADD_VV);
2619 // clang-format on
2620 }
2621
2622#undef RVV_OPC_LMUL_MASK_CASE
2623#undef RVV_OPC_LMUL_CASE
2624}
2625
2627 const MachineOperand &MO,
2628 bool DoRegPressureReduce) {
2629 if (!MO.isReg() || !MO.getReg().isVirtual())
2630 return false;
2631 const MachineRegisterInfo &MRI = Root.getMF()->getRegInfo();
2632 MachineInstr *MI = MRI.getVRegDef(MO.getReg());
2633 if (!MI || !isFMUL(MI->getOpcode()))
2634 return false;
2635
2638 return false;
2639
2640 // Try combining even if fmul has more than one use as it eliminates
2641 // dependency between fadd(fsub) and fmul. However, it can extend liveranges
2642 // for fmul operands, so reject the transformation in register pressure
2643 // reduction mode.
2644 if (DoRegPressureReduce && !MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()))
2645 return false;
2646
2647 // Do not combine instructions from different basic blocks.
2648 if (Root.getParent() != MI->getParent())
2649 return false;
2650 return RISCV::hasEqualFRM(Root, *MI);
2651}
2652
2654 SmallVectorImpl<unsigned> &Patterns,
2655 bool DoRegPressureReduce) {
2656 unsigned Opc = Root.getOpcode();
2657 bool IsFAdd = isFADD(Opc);
2658 if (!IsFAdd && !isFSUB(Opc))
2659 return false;
2660 bool Added = false;
2661 if (canCombineFPFusedMultiply(Root, Root.getOperand(1),
2662 DoRegPressureReduce)) {
2665 Added = true;
2666 }
2667 if (canCombineFPFusedMultiply(Root, Root.getOperand(2),
2668 DoRegPressureReduce)) {
2671 Added = true;
2672 }
2673 return Added;
2674}
2675
2676static bool getFPPatterns(MachineInstr &Root,
2677 SmallVectorImpl<unsigned> &Patterns,
2678 bool DoRegPressureReduce) {
2679 return getFPFusedMultiplyPatterns(Root, Patterns, DoRegPressureReduce);
2680}
2681
2682/// Utility routine that checks if \param MO is defined by an
2683/// \param CombineOpc instruction in the basic block \param MBB
2685 const MachineOperand &MO,
2686 unsigned CombineOpc) {
2687 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2688 const MachineInstr *MI = nullptr;
2689
2690 if (MO.isReg() && MO.getReg().isVirtual())
2691 MI = MRI.getUniqueVRegDef(MO.getReg());
2692 // And it needs to be in the trace (otherwise, it won't have a depth).
2693 if (!MI || MI->getParent() != &MBB || MI->getOpcode() != CombineOpc)
2694 return nullptr;
2695 // Must only used by the user we combine with.
2696 if (!MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()))
2697 return nullptr;
2698
2699 return MI;
2700}
2701
2702/// Utility routine that checks if \param MO is defined by a SLLI in \param
2703/// MBB that can be combined by splitting across 2 SHXADD instructions. The
2704/// first SHXADD shift amount is given by \param OuterShiftAmt.
2706 const MachineOperand &MO,
2707 unsigned OuterShiftAmt) {
2708 const MachineInstr *ShiftMI = canCombine(MBB, MO, RISCV::SLLI);
2709 if (!ShiftMI)
2710 return false;
2711
2712 unsigned InnerShiftAmt = ShiftMI->getOperand(2).getImm();
2713 if (InnerShiftAmt < OuterShiftAmt || (InnerShiftAmt - OuterShiftAmt) > 3)
2714 return false;
2715
2716 return true;
2717}
2718
2719// Returns the shift amount from a SHXADD instruction. Returns 0 if the
2720// instruction is not a SHXADD.
2721static unsigned getSHXADDShiftAmount(unsigned Opc) {
2722 switch (Opc) {
2723 default:
2724 return 0;
2725 case RISCV::SH1ADD:
2726 return 1;
2727 case RISCV::SH2ADD:
2728 return 2;
2729 case RISCV::SH3ADD:
2730 return 3;
2731 }
2732}
2733
2734// Returns the shift amount from a SHXADD.UW instruction. Returns 0 if the
2735// instruction is not a SHXADD.UW.
2736static unsigned getSHXADDUWShiftAmount(unsigned Opc) {
2737 switch (Opc) {
2738 default:
2739 return 0;
2740 case RISCV::SH1ADD_UW:
2741 return 1;
2742 case RISCV::SH2ADD_UW:
2743 return 2;
2744 case RISCV::SH3ADD_UW:
2745 return 3;
2746 }
2747}
2748
2749// Look for opportunities to combine (sh3add Z, (add X, (slli Y, 5))) into
2750// (sh3add (sh2add Y, Z), X).
2751static bool getSHXADDPatterns(const MachineInstr &Root,
2752 SmallVectorImpl<unsigned> &Patterns) {
2753 unsigned ShiftAmt = getSHXADDShiftAmount(Root.getOpcode());
2754 if (!ShiftAmt)
2755 return false;
2756
2757 const MachineBasicBlock &MBB = *Root.getParent();
2758
2759 const MachineInstr *AddMI = canCombine(MBB, Root.getOperand(2), RISCV::ADD);
2760 if (!AddMI)
2761 return false;
2762
2763 bool Found = false;
2764 if (canCombineShiftIntoShXAdd(MBB, AddMI->getOperand(1), ShiftAmt)) {
2766 Found = true;
2767 }
2768 if (canCombineShiftIntoShXAdd(MBB, AddMI->getOperand(2), ShiftAmt)) {
2770 Found = true;
2771 }
2772
2773 return Found;
2774}
2775
2787
2789 MachineInstr &Root, SmallVectorImpl<unsigned> &Patterns,
2790 bool DoRegPressureReduce) const {
2791
2792 if (getFPPatterns(Root, Patterns, DoRegPressureReduce))
2793 return true;
2794
2795 if (getSHXADDPatterns(Root, Patterns))
2796 return true;
2797
2798 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns,
2799 DoRegPressureReduce);
2800}
2801
2802static unsigned getFPFusedMultiplyOpcode(unsigned RootOpc, unsigned Pattern) {
2803 switch (RootOpc) {
2804 default:
2805 llvm_unreachable("Unexpected opcode");
2806 case RISCV::FADD_H:
2807 return RISCV::FMADD_H;
2808 case RISCV::FADD_S:
2809 return RISCV::FMADD_S;
2810 case RISCV::FADD_D:
2811 return RISCV::FMADD_D;
2812 case RISCV::FSUB_H:
2813 return Pattern == RISCVMachineCombinerPattern::FMSUB ? RISCV::FMSUB_H
2814 : RISCV::FNMSUB_H;
2815 case RISCV::FSUB_S:
2816 return Pattern == RISCVMachineCombinerPattern::FMSUB ? RISCV::FMSUB_S
2817 : RISCV::FNMSUB_S;
2818 case RISCV::FSUB_D:
2819 return Pattern == RISCVMachineCombinerPattern::FMSUB ? RISCV::FMSUB_D
2820 : RISCV::FNMSUB_D;
2821 }
2822}
2823
2824static unsigned getAddendOperandIdx(unsigned Pattern) {
2825 switch (Pattern) {
2826 default:
2827 llvm_unreachable("Unexpected pattern");
2830 return 2;
2833 return 1;
2834 }
2835}
2836
2838 unsigned Pattern,
2841 MachineFunction *MF = Root.getMF();
2842 MachineRegisterInfo &MRI = MF->getRegInfo();
2844
2845 MachineOperand &Mul1 = Prev.getOperand(1);
2846 MachineOperand &Mul2 = Prev.getOperand(2);
2847 MachineOperand &Dst = Root.getOperand(0);
2849
2850 Register DstReg = Dst.getReg();
2851 unsigned FusedOpc = getFPFusedMultiplyOpcode(Root.getOpcode(), Pattern);
2852 uint32_t IntersectedFlags = Root.getFlags() & Prev.getFlags();
2853 DebugLoc MergedLoc =
2855
2856 bool Mul1IsKill = Mul1.isKill();
2857 bool Mul2IsKill = Mul2.isKill();
2858 bool AddendIsKill = Addend.isKill();
2859
2860 // We need to clear kill flags since we may be extending the live range past
2861 // a kill. If the mul had kill flags, we can preserve those since we know
2862 // where the previous range stopped.
2863 MRI.clearKillFlags(Mul1.getReg());
2864 MRI.clearKillFlags(Mul2.getReg());
2865
2867 BuildMI(*MF, MergedLoc, TII->get(FusedOpc), DstReg)
2868 .addReg(Mul1.getReg(), getKillRegState(Mul1IsKill))
2869 .addReg(Mul2.getReg(), getKillRegState(Mul2IsKill))
2870 .addReg(Addend.getReg(), getKillRegState(AddendIsKill))
2871 .setMIFlags(IntersectedFlags);
2872
2873 InsInstrs.push_back(MIB);
2874 if (MRI.hasOneNonDBGUse(Prev.getOperand(0).getReg()))
2875 DelInstrs.push_back(&Prev);
2876 DelInstrs.push_back(&Root);
2877}
2878
2879// Combine patterns like (sh3add Z, (add X, (slli Y, 5))) to
2880// (sh3add (sh2add Y, Z), X) if the shift amount can be split across two
2881// shXadd instructions. The outer shXadd keeps its original opcode.
2882static void
2883genShXAddAddShift(MachineInstr &Root, unsigned AddOpIdx,
2886 DenseMap<Register, unsigned> &InstrIdxForVirtReg) {
2887 MachineFunction *MF = Root.getMF();
2888 MachineRegisterInfo &MRI = MF->getRegInfo();
2890
2891 unsigned OuterShiftAmt = getSHXADDShiftAmount(Root.getOpcode());
2892 assert(OuterShiftAmt != 0 && "Unexpected opcode");
2893
2894 MachineInstr *AddMI = MRI.getUniqueVRegDef(Root.getOperand(2).getReg());
2895 MachineInstr *ShiftMI =
2896 MRI.getUniqueVRegDef(AddMI->getOperand(AddOpIdx).getReg());
2897
2898 unsigned InnerShiftAmt = ShiftMI->getOperand(2).getImm();
2899 assert(InnerShiftAmt >= OuterShiftAmt && "Unexpected shift amount");
2900
2901 unsigned InnerOpc;
2902 switch (InnerShiftAmt - OuterShiftAmt) {
2903 default:
2904 llvm_unreachable("Unexpected shift amount");
2905 case 0:
2906 InnerOpc = RISCV::ADD;
2907 break;
2908 case 1:
2909 InnerOpc = RISCV::SH1ADD;
2910 break;
2911 case 2:
2912 InnerOpc = RISCV::SH2ADD;
2913 break;
2914 case 3:
2915 InnerOpc = RISCV::SH3ADD;
2916 break;
2917 }
2918
2919 const MachineOperand &X = AddMI->getOperand(3 - AddOpIdx);
2920 const MachineOperand &Y = ShiftMI->getOperand(1);
2921 const MachineOperand &Z = Root.getOperand(1);
2922
2923 Register NewVR = MRI.createVirtualRegister(&RISCV::GPRRegClass);
2924
2925 auto MIB1 = BuildMI(*MF, MIMetadata(Root), TII->get(InnerOpc), NewVR)
2926 .addReg(Y.getReg(), getKillRegState(Y.isKill()))
2927 .addReg(Z.getReg(), getKillRegState(Z.isKill()));
2928 auto MIB2 = BuildMI(*MF, MIMetadata(Root), TII->get(Root.getOpcode()),
2929 Root.getOperand(0).getReg())
2930 .addReg(NewVR, RegState::Kill)
2931 .addReg(X.getReg(), getKillRegState(X.isKill()));
2932
2933 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
2934 InsInstrs.push_back(MIB1);
2935 InsInstrs.push_back(MIB2);
2936 DelInstrs.push_back(ShiftMI);
2937 DelInstrs.push_back(AddMI);
2938 DelInstrs.push_back(&Root);
2939}
2940
2942 MachineInstr &Root, unsigned Pattern,
2945 DenseMap<Register, unsigned> &InstrIdxForVirtReg) const {
2946 MachineRegisterInfo &MRI = Root.getMF()->getRegInfo();
2947 switch (Pattern) {
2948 default:
2950 DelInstrs, InstrIdxForVirtReg);
2951 return;
2954 MachineInstr &Prev = *MRI.getVRegDef(Root.getOperand(1).getReg());
2955 combineFPFusedMultiply(Root, Prev, Pattern, InsInstrs, DelInstrs);
2956 return;
2957 }
2960 MachineInstr &Prev = *MRI.getVRegDef(Root.getOperand(2).getReg());
2961 combineFPFusedMultiply(Root, Prev, Pattern, InsInstrs, DelInstrs);
2962 return;
2963 }
2965 genShXAddAddShift(Root, 1, InsInstrs, DelInstrs, InstrIdxForVirtReg);
2966 return;
2968 genShXAddAddShift(Root, 2, InsInstrs, DelInstrs, InstrIdxForVirtReg);
2969 return;
2970 }
2971}
2972
2974 StringRef &ErrInfo) const {
2975 MCInstrDesc const &Desc = MI.getDesc();
2976
2977 for (const auto &[Index, Operand] : enumerate(Desc.operands())) {
2978 const MachineOperand &MO = MI.getOperand(Index);
2979 unsigned OpType = Operand.OperandType;
2980 switch (OpType) {
2981 default:
2982 if (OpType >= RISCVOp::OPERAND_FIRST_RISCV_IMM &&
2984 if (!MO.isImm()) {
2985 ErrInfo = "Expected an immediate operand.";
2986 return false;
2987 }
2988 int64_t Imm = MO.getImm();
2989 bool Ok;
2990 switch (OpType) {
2991 default:
2992 llvm_unreachable("Unexpected operand type");
2993
2994#define CASE_OPERAND_UIMM(NUM) \
2995 case RISCVOp::OPERAND_UIMM##NUM: \
2996 Ok = isUInt<NUM>(Imm); \
2997 break;
2998#define CASE_OPERAND_UIMM_LSB_ZEROS(BITS, SUFFIX) \
2999 case RISCVOp::OPERAND_UIMM##BITS##_LSB##SUFFIX: { \
3000 constexpr size_t NumZeros = sizeof(#SUFFIX) - 1; \
3001 Ok = isShiftedUInt<BITS - NumZeros, NumZeros>(Imm); \
3002 break; \
3003 }
3004#define CASE_OPERAND_SIMM(NUM) \
3005 case RISCVOp::OPERAND_SIMM##NUM: \
3006 Ok = isInt<NUM>(Imm); \
3007 break;
3008 // clang-format off
3032 // clang-format on
3034 Ok = isUInt<5>(Imm) && (Imm != 0);
3035 break;
3037 Ok = isUInt<5>(Imm) && (Imm > 3);
3038 break;
3040 Ok = Imm >= 1 && Imm <= 32;
3041 break;
3043 Ok = Imm >= 1 && Imm <= 64;
3044 break;
3046 Ok = isUInt<8>(Imm) && Imm >= 32;
3047 break;
3049 Ok = isShiftedInt<6, 4>(Imm) && (Imm != 0);
3050 break;
3052 Ok = isShiftedUInt<8, 2>(Imm) && (Imm != 0);
3053 break;
3055 Ok = isUInt<16>(Imm) && (Imm != 0);
3056 break;
3058 Ok = Imm == 3;
3059 break;
3061 Ok = Imm == 4;
3062 break;
3064 Ok = (isUInt<5>(Imm) && Imm != 0) || Imm == -1;
3065 break;
3066 // clang-format off
3073 // clang-format on
3075 Ok = Imm >= -15 && Imm <= 16;
3076 break;
3078 Ok = isInt<5>(Imm) && (Imm != 0);
3079 break;
3081 Ok = Imm != 0 && isInt<6>(Imm);
3082 break;
3084 Ok = isUInt<10>(Imm) && RISCVVType::isValidVType(Imm);
3085 break;
3087 Ok = isUInt<11>(Imm) && RISCVVType::isValidVType(Imm);
3088 break;
3090 Ok = isShiftedInt<7, 5>(Imm);
3091 break;
3093 Ok = isInt<16>(Imm) && (Imm != 0);
3094 break;
3096 Ok = isInt<20>(Imm);
3097 break;
3099 Ok = STI.is64Bit() ? isUInt<6>(Imm) : isUInt<5>(Imm);
3100 break;
3102 Ok = STI.is64Bit() ? isUInt<6>(Imm) : isUInt<5>(Imm);
3103 Ok = Ok && Imm != 0;
3104 break;
3106 Ok = (isUInt<5>(Imm) && Imm != 0) || (Imm >= 0xfffe0 && Imm <= 0xfffff);
3107 break;
3109 Ok = Imm >= 0 && Imm <= 10;
3110 break;
3112 Ok = Imm >= 0 && Imm <= 7;
3113 break;
3115 Ok = Imm >= 1 && Imm <= 10;
3116 break;
3118 Ok = Imm >= 2 && Imm <= 14;
3119 break;
3121 Ok = Imm >= RISCVZC::RA && Imm <= RISCVZC::RA_S0_S11;
3122 break;
3124 Ok = Imm >= RISCVZC::RA_S0 && Imm <= RISCVZC::RA_S0_S11;
3125 break;
3127 Ok = Imm >= 0 && Imm <= 48 && Imm % 16 == 0;
3128 break;
3131 break;
3133 Ok = Imm == RISCVFPRndMode::RTZ;
3134 break;
3136 Ok = Imm >= 0 && Imm < RISCVCC::COND_INVALID;
3137 break;
3139 Ok = isValidAtomicOrdering(Imm);
3140 break;
3143 Imm;
3144 break;
3146 Ok = (isUInt<5>(Imm) && RISCVVType::isValidSEW(1 << Imm));
3147 break;
3149 Ok = Imm == 0;
3150 break;
3153 if (RISCVII::usesVXRM(Desc.TSFlags))
3154 Ok = isUInt<2>(Imm);
3155 else
3157 break;
3160 break;
3162 Ok = Imm == 1 || Imm == 2 || Imm == 4;
3163 break;
3164 }
3165 if (!Ok) {
3166 ErrInfo = "Invalid immediate";
3167 return false;
3168 }
3169 }
3170 break;
3172 // TODO: We could be stricter about what non-register operands are
3173 // allowed.
3174 if (MO.isReg()) {
3175 ErrInfo = "Expected a non-register operand.";
3176 return false;
3177 }
3178 if (MO.isImm() && !isInt<12>(MO.getImm())) {
3179 ErrInfo = "Invalid immediate";
3180 return false;
3181 }
3182 break;
3185 // TODO: We could be stricter about what non-register operands are
3186 // allowed.
3187 if (MO.isReg()) {
3188 ErrInfo = "Expected a non-register operand.";
3189 return false;
3190 }
3191 if (MO.isImm() && !isUInt<20>(MO.getImm())) {
3192 ErrInfo = "Invalid immediate";
3193 return false;
3194 }
3195 break;
3197 // TODO: We could be stricter about what non-register operands are
3198 // allowed.
3199 if (MO.isReg()) {
3200 ErrInfo = "Expected a non-register operand.";
3201 return false;
3202 }
3203 if (MO.isImm() && !isInt<32>(MO.getImm())) {
3204 ErrInfo = "Invalid immediate";
3205 return false;
3206 }
3207 break;
3209 if (MO.isImm()) {
3210 int64_t Imm = MO.getImm();
3211 // VLMAX is represented as -1.
3212 if (!isUInt<5>(Imm) && Imm != -1) {
3213 ErrInfo = "Invalid immediate";
3214 return false;
3215 }
3216 } else if (!MO.isReg()) {
3217 ErrInfo = "Expected a register or immediate operand.";
3218 return false;
3219 }
3220 break;
3222 if (!MO.isReg() && !MO.isImm()) {
3223 ErrInfo = "Expected a register or immediate operand.";
3224 return false;
3225 }
3226 break;
3227 }
3228 }
3229
3230 const uint64_t TSFlags = Desc.TSFlags;
3231 if (RISCVII::hasVLOp(TSFlags)) {
3232 const MachineOperand &Op = MI.getOperand(RISCVII::getVLOpNum(Desc));
3233 if (!Op.isImm() && !Op.isReg()) {
3234 ErrInfo = "Invalid operand type for VL operand";
3235 return false;
3236 }
3237 if (Op.isReg() && Op.getReg().isValid()) {
3238 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
3239 auto *RC = MRI.getRegClass(Op.getReg());
3240 if (!RISCV::GPRNoX0RegClass.hasSubClassEq(RC)) {
3241 ErrInfo = "Invalid register class for VL operand";
3242 return false;
3243 }
3244 }
3245 if (!RISCVII::hasSEWOp(TSFlags)) {
3246 ErrInfo = "VL operand w/o SEW operand?";
3247 return false;
3248 }
3249 }
3250 if (RISCVII::hasSEWOp(TSFlags)) {
3251 unsigned OpIdx = RISCVII::getSEWOpNum(Desc);
3252 if (!MI.getOperand(OpIdx).isImm()) {
3253 ErrInfo = "SEW value expected to be an immediate";
3254 return false;
3255 }
3256 uint64_t Log2SEW = MI.getOperand(OpIdx).getImm();
3257 if (Log2SEW > 31) {
3258 ErrInfo = "Unexpected SEW value";
3259 return false;
3260 }
3261 unsigned SEW = Log2SEW ? 1 << Log2SEW : 8;
3262 if (!RISCVVType::isValidSEW(SEW)) {
3263 ErrInfo = "Unexpected SEW value";
3264 return false;
3265 }
3266 }
3267 if (RISCVII::hasVecPolicyOp(TSFlags)) {
3269 if (!MI.getOperand(OpIdx).isImm()) {
3270 ErrInfo = "Policy operand expected to be an immediate";
3271 return false;
3272 }
3273 uint64_t Policy = MI.getOperand(OpIdx).getImm();
3275 ErrInfo = "Invalid Policy Value";
3276 return false;
3277 }
3278 if (!RISCVII::hasVLOp(TSFlags)) {
3279 ErrInfo = "policy operand w/o VL operand?";
3280 return false;
3281 }
3282
3283 // VecPolicy operands can only exist on instructions with passthru/merge
3284 // arguments. Note that not all arguments with passthru have vec policy
3285 // operands- some instructions have implicit policies.
3286 unsigned UseOpIdx;
3287 if (!MI.isRegTiedToUseOperand(0, &UseOpIdx)) {
3288 ErrInfo = "policy operand w/o tied operand?";
3289 return false;
3290 }
3291 }
3292
3293 if (int Idx = RISCVII::getFRMOpNum(Desc);
3294 Idx >= 0 && MI.getOperand(Idx).getImm() == RISCVFPRndMode::DYN &&
3295 !MI.readsRegister(RISCV::FRM, /*TRI=*/nullptr)) {
3296 ErrInfo = "dynamic rounding mode should read FRM";
3297 return false;
3298 }
3299
3300 return true;
3301}
3302
3304 const MachineInstr &AddrI,
3305 ExtAddrMode &AM) const {
3306 switch (MemI.getOpcode()) {
3307 default:
3308 return false;
3309 case RISCV::LB:
3310 case RISCV::LBU:
3311 case RISCV::LH:
3312 case RISCV::LH_INX:
3313 case RISCV::LHU:
3314 case RISCV::LW:
3315 case RISCV::LW_INX:
3316 case RISCV::LWU:
3317 case RISCV::LD:
3318 case RISCV::LD_RV32:
3319 case RISCV::FLH:
3320 case RISCV::FLW:
3321 case RISCV::FLD:
3322 case RISCV::SB:
3323 case RISCV::SH:
3324 case RISCV::SH_INX:
3325 case RISCV::SW:
3326 case RISCV::SW_INX:
3327 case RISCV::SD:
3328 case RISCV::SD_RV32:
3329 case RISCV::FSH:
3330 case RISCV::FSW:
3331 case RISCV::FSD:
3332 break;
3333 }
3334
3335 if (MemI.getOperand(0).getReg() == Reg)
3336 return false;
3337
3338 if (AddrI.getOpcode() != RISCV::ADDI || !AddrI.getOperand(1).isReg() ||
3339 !AddrI.getOperand(2).isImm())
3340 return false;
3341
3342 int64_t OldOffset = MemI.getOperand(2).getImm();
3343 int64_t Disp = AddrI.getOperand(2).getImm();
3344 int64_t NewOffset = OldOffset + Disp;
3345 if (!STI.is64Bit())
3346 NewOffset = SignExtend64<32>(NewOffset);
3347
3348 if (!isInt<12>(NewOffset))
3349 return false;
3350
3351 AM.BaseReg = AddrI.getOperand(1).getReg();
3352 AM.ScaledReg = 0;
3353 AM.Scale = 0;
3354 AM.Displacement = NewOffset;
3356 return true;
3357}
3358
3360 const ExtAddrMode &AM) const {
3361
3362 const DebugLoc &DL = MemI.getDebugLoc();
3363 MachineBasicBlock &MBB = *MemI.getParent();
3364
3365 assert(AM.ScaledReg == 0 && AM.Scale == 0 &&
3366 "Addressing mode not supported for folding");
3367
3368 return BuildMI(MBB, MemI, DL, get(MemI.getOpcode()))
3369 .addReg(MemI.getOperand(0).getReg(), getDefRegState(MemI.mayLoad()))
3370 .addReg(AM.BaseReg)
3371 .addImm(AM.Displacement)
3372 .setMemRefs(MemI.memoperands())
3373 .setMIFlags(MemI.getFlags());
3374}
3375
3376// TODO: At the moment, MIPS introduced paring of instructions operating with
3377// word or double word. This should be extended with more instructions when more
3378// vendors support load/store pairing.
3380 switch (Opc) {
3381 default:
3382 return false;
3383 case RISCV::SW:
3384 case RISCV::SD:
3385 case RISCV::LD:
3386 case RISCV::LW:
3387 return true;
3388 }
3389}
3390
3392 const TargetRegisterInfo *TRI) {
3393 // If this is a volatile load/store, don't mess with it.
3394 if (LdSt.hasOrderedMemoryRef() || LdSt.getNumExplicitOperands() != 3)
3395 return false;
3396
3397 if (LdSt.getOperand(1).isFI())
3398 return true;
3399
3400 assert(LdSt.getOperand(1).isReg() && "Expected a reg operand.");
3401 // Can't cluster if the instruction modifies the base register
3402 // or it is update form. e.g. ld x5,8(x5)
3403 if (LdSt.modifiesRegister(LdSt.getOperand(1).getReg(), TRI))
3404 return false;
3405
3406 if (!LdSt.getOperand(2).isImm())
3407 return false;
3408
3409 return true;
3410}
3411
3414 int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width,
3415 const TargetRegisterInfo *TRI) const {
3416 if (!LdSt.mayLoadOrStore())
3417 return false;
3418
3419 // Conservatively, only handle scalar loads/stores for now.
3420 switch (LdSt.getOpcode()) {
3421 case RISCV::LB:
3422 case RISCV::LBU:
3423 case RISCV::SB:
3424 case RISCV::LH:
3425 case RISCV::LH_INX:
3426 case RISCV::LHU:
3427 case RISCV::FLH:
3428 case RISCV::SH:
3429 case RISCV::SH_INX:
3430 case RISCV::FSH:
3431 case RISCV::LW:
3432 case RISCV::LW_INX:
3433 case RISCV::LWU:
3434 case RISCV::FLW:
3435 case RISCV::SW:
3436 case RISCV::SW_INX:
3437 case RISCV::FSW:
3438 case RISCV::LD:
3439 case RISCV::LD_RV32:
3440 case RISCV::FLD:
3441 case RISCV::SD:
3442 case RISCV::SD_RV32:
3443 case RISCV::FSD:
3444 break;
3445 default:
3446 return false;
3447 }
3448 const MachineOperand *BaseOp;
3449 OffsetIsScalable = false;
3450 if (!getMemOperandWithOffsetWidth(LdSt, BaseOp, Offset, Width, TRI))
3451 return false;
3452 BaseOps.push_back(BaseOp);
3453 return true;
3454}
3455
3456// TODO: This was copied from SIInstrInfo. Could it be lifted to a common
3457// helper?
3460 const MachineInstr &MI2,
3462 // Only examine the first "base" operand of each instruction, on the
3463 // assumption that it represents the real base address of the memory access.
3464 // Other operands are typically offsets or indices from this base address.
3465 if (BaseOps1.front()->isIdenticalTo(*BaseOps2.front()))
3466 return true;
3467
3468 if (!MI1.hasOneMemOperand() || !MI2.hasOneMemOperand())
3469 return false;
3470
3471 auto MO1 = *MI1.memoperands_begin();
3472 auto MO2 = *MI2.memoperands_begin();
3473 if (MO1->getAddrSpace() != MO2->getAddrSpace())
3474 return false;
3475
3476 auto Base1 = MO1->getValue();
3477 auto Base2 = MO2->getValue();
3478 if (!Base1 || !Base2)
3479 return false;
3480 Base1 = getUnderlyingObject(Base1);
3481 Base2 = getUnderlyingObject(Base2);
3482
3483 if (isa<UndefValue>(Base1) || isa<UndefValue>(Base2))
3484 return false;
3485
3486 return Base1 == Base2;
3487}
3488
3490 ArrayRef<const MachineOperand *> BaseOps1, int64_t Offset1,
3491 bool OffsetIsScalable1, ArrayRef<const MachineOperand *> BaseOps2,
3492 int64_t Offset2, bool OffsetIsScalable2, unsigned ClusterSize,
3493 unsigned NumBytes) const {
3494 // If the mem ops (to be clustered) do not have the same base ptr, then they
3495 // should not be clustered
3496 if (!BaseOps1.empty() && !BaseOps2.empty()) {
3497 const MachineInstr &FirstLdSt = *BaseOps1.front()->getParent();
3498 const MachineInstr &SecondLdSt = *BaseOps2.front()->getParent();
3499 if (!memOpsHaveSameBasePtr(FirstLdSt, BaseOps1, SecondLdSt, BaseOps2))
3500 return false;
3501 } else if (!BaseOps1.empty() || !BaseOps2.empty()) {
3502 // If only one base op is empty, they do not have the same base ptr
3503 return false;
3504 }
3505
3506 unsigned CacheLineSize =
3507 BaseOps1.front()->getParent()->getMF()->getSubtarget().getCacheLineSize();
3508 // Assume a cache line size of 64 bytes if no size is set in RISCVSubtarget.
3510 // Cluster if the memory operations are on the same or a neighbouring cache
3511 // line, but limit the maximum ClusterSize to avoid creating too much
3512 // additional register pressure.
3513 return ClusterSize <= 4 && std::abs(Offset1 - Offset2) < CacheLineSize;
3514}
3515
3516// Set BaseReg (the base register operand), Offset (the byte offset being
3517// accessed) and the access Width of the passed instruction that reads/writes
3518// memory. Returns false if the instruction does not read/write memory or the
3519// BaseReg/Offset/Width can't be determined. Is not guaranteed to always
3520// recognise base operands and offsets in all cases.
3521// TODO: Add an IsScalable bool ref argument (like the equivalent AArch64
3522// function) and set it as appropriate.
3524 const MachineInstr &LdSt, const MachineOperand *&BaseReg, int64_t &Offset,
3525 LocationSize &Width, const TargetRegisterInfo *TRI) const {
3526 if (!LdSt.mayLoadOrStore())
3527 return false;
3528
3529 // Here we assume the standard RISC-V ISA, which uses a base+offset
3530 // addressing mode. You'll need to relax these conditions to support custom
3531 // load/store instructions.
3532 if (LdSt.getNumExplicitOperands() != 3)
3533 return false;
3534 if ((!LdSt.getOperand(1).isReg() && !LdSt.getOperand(1).isFI()) ||
3535 !LdSt.getOperand(2).isImm())
3536 return false;
3537
3538 if (!LdSt.hasOneMemOperand())
3539 return false;
3540
3541 Width = (*LdSt.memoperands_begin())->getSize();
3542 BaseReg = &LdSt.getOperand(1);
3543 Offset = LdSt.getOperand(2).getImm();
3544 return true;
3545}
3546
3548 const MachineInstr &MIa, const MachineInstr &MIb) const {
3549 assert(MIa.mayLoadOrStore() && "MIa must be a load or store.");
3550 assert(MIb.mayLoadOrStore() && "MIb must be a load or store.");
3551
3554 return false;
3555
3556 // Retrieve the base register, offset from the base register and width. Width
3557 // is the size of memory that is being loaded/stored (e.g. 1, 2, 4). If
3558 // base registers are identical, and the offset of a lower memory access +
3559 // the width doesn't overlap the offset of a higher memory access,
3560 // then the memory accesses are different.
3561 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
3562 const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr;
3563 int64_t OffsetA = 0, OffsetB = 0;
3565 WidthB = LocationSize::precise(0);
3566 if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, WidthA, TRI) &&
3567 getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, WidthB, TRI)) {
3568 if (BaseOpA->isIdenticalTo(*BaseOpB)) {
3569 int LowOffset = std::min(OffsetA, OffsetB);
3570 int HighOffset = std::max(OffsetA, OffsetB);
3571 LocationSize LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
3572 if (LowWidth.hasValue() &&
3573 LowOffset + (int)LowWidth.getValue() <= HighOffset)
3574 return true;
3575 }
3576 }
3577 return false;
3578}
3579
3580std::pair<unsigned, unsigned>
3582 const unsigned Mask = RISCVII::MO_DIRECT_FLAG_MASK;
3583 return std::make_pair(TF & Mask, TF & ~Mask);
3584}
3585
3588 using namespace RISCVII;
3589 static const std::pair<unsigned, const char *> TargetFlags[] = {
3590 {MO_CALL, "riscv-call"},
3591 {MO_LO, "riscv-lo"},
3592 {MO_HI, "riscv-hi"},
3593 {MO_PCREL_LO, "riscv-pcrel-lo"},
3594 {MO_PCREL_HI, "riscv-pcrel-hi"},
3595 {MO_GOT_HI, "riscv-got-hi"},
3596 {MO_TPREL_LO, "riscv-tprel-lo"},
3597 {MO_TPREL_HI, "riscv-tprel-hi"},
3598 {MO_TPREL_ADD, "riscv-tprel-add"},
3599 {MO_TLS_GOT_HI, "riscv-tls-got-hi"},
3600 {MO_TLS_GD_HI, "riscv-tls-gd-hi"},
3601 {MO_TLSDESC_HI, "riscv-tlsdesc-hi"},
3602 {MO_TLSDESC_LOAD_LO, "riscv-tlsdesc-load-lo"},
3603 {MO_TLSDESC_ADD_LO, "riscv-tlsdesc-add-lo"},
3604 {MO_TLSDESC_CALL, "riscv-tlsdesc-call"}};
3605 return ArrayRef(TargetFlags);
3606}
3608 MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
3609 const Function &F = MF.getFunction();
3610
3611 // Can F be deduplicated by the linker? If it can, don't outline from it.
3612 if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
3613 return false;
3614
3615 // Don't outline from functions with section markings; the program could
3616 // expect that all the code is in the named section.
3617 if (F.hasSection())
3618 return false;
3619
3620 // It's safe to outline from MF.
3621 return true;
3622}
3623
3625 unsigned &Flags) const {
3626 // More accurate safety checking is done in getOutliningCandidateInfo.
3628}
3629
3630// Enum values indicating how an outlined call should be constructed.
3636
3641
3643 const MachineFunction *MF = MBB.getParent();
3644 const Function &F = MF->getFunction();
3645 return F.getFnAttribute("fentry-call").getValueAsBool() ||
3646 F.hasFnAttribute("patchable-function-entry");
3647}
3648
3650 MCRegister RegNo) {
3651 return MI.readsRegister(RegNo, TRI) ||
3652 MI.getDesc().hasImplicitUseOfPhysReg(RegNo);
3653}
3654
3656 const TargetRegisterInfo *TRI, MCRegister RegNo) {
3657 return MI.modifiesRegister(RegNo, TRI) ||
3658 MI.getDesc().hasImplicitDefOfPhysReg(RegNo);
3659}
3660
3662 if (!MBB.back().isReturn())
3663 return true;
3665 return true;
3666
3667 // If the candidate reads the pre-set register
3668 // that can be used for expanding PseudoTAIL instruction,
3669 // then we cannot insert tail call.
3670 const TargetSubtargetInfo &STI = MBB.getParent()->getSubtarget();
3671 MCRegister TailExpandUseRegNo =
3673 for (const MachineInstr &MI : MBB) {
3674 if (isMIReadsReg(MI, STI.getRegisterInfo(), TailExpandUseRegNo))
3675 return true;
3676 if (isMIModifiesReg(MI, STI.getRegisterInfo(), TailExpandUseRegNo))
3677 break;
3678 }
3679 return false;
3680}
3681
3683 const TargetRegisterInfo &TRI) {
3684 // Candidate registers for saving X5: t1-t6
3685 static const MCPhysReg TempRegs[] = {
3686 RISCV::X6, // t1
3687 RISCV::X7, // t2
3688 RISCV::X28, // t3
3689 RISCV::X29, // t4
3690 RISCV::X30, // t5
3691 RISCV::X31 // t6
3692 };
3693
3694 const MachineFunction *MF = C.getMF();
3695 const MachineRegisterInfo &MRI = MF->getRegInfo();
3696
3697 for (MCPhysReg Reg : TempRegs) {
3698 if (MRI.isReserved(Reg))
3699 continue;
3700
3701 if (C.isAvailableAcrossAndOutOfSeq(Reg, TRI) &&
3702 C.isAvailableInsideSeq(Reg, TRI)) {
3703 return Reg;
3704 }
3705 }
3706
3707 return Register();
3708}
3709
3711 // If the expansion register for tail calls is live across the candidate
3712 // outlined call site, we cannot outline that candidate as the expansion
3713 // would clobber the register.
3714 MCRegister TailExpandUseReg =
3715 RISCVII::getTailExpandUseRegNo(STI.getFeatureBits());
3716 if (C.back().isReturn() &&
3717 !C.isAvailableAcrossAndOutOfSeq(TailExpandUseReg, RegInfo)) {
3718 LLVM_DEBUG(dbgs() << "MBB:\n" << *C.getMBB());
3719 LLVM_DEBUG(dbgs() << "Cannot be outlined between: " << C.front() << "and "
3720 << C.back());
3721 LLVM_DEBUG(dbgs() << "Because the tail-call register is live across "
3722 "the proposed outlined function call\n");
3723 return true;
3724 }
3725
3726 // If last instruction is return then we can rely on
3727 // the verification already performed in the getOutliningTypeImpl.
3728 if (C.back().isReturn()) {
3729 assert(!cannotInsertTailCall(*C.getMBB()) &&
3730 "The candidate who uses return instruction must be outlined "
3731 "using tail call");
3732 return false;
3733 }
3734
3735 // Filter out candidates where the X5 register (t0) can't be used to setup
3736 // the function call.
3737 if (!C.isAvailableInsideSeq(RISCV::X5, RegInfo))
3738 return true;
3739
3740 // If X5 is available in the region, use X5 directly (MachineOutlinerDefault).
3741 if (C.isAvailableAcrossAndOutOfSeq(RISCV::X5, RegInfo))
3742 return false;
3743
3744 // Otherwise, try to save X5 into t1-t6 (MachineOutlinerRegSave).
3746 return false;
3747
3748 return true;
3749}
3750
3751std::optional<std::unique_ptr<outliner::OutlinedFunction>>
3753 const MachineModuleInfo &MMI,
3754 std::vector<outliner::Candidate> &RepeatedSequenceLocs,
3755 unsigned MinRepeats) const {
3756
3757 // Analyze each candidate and erase the ones that are not viable.
3758 llvm::erase_if(RepeatedSequenceLocs, [this](auto Candidate) {
3759 return analyzeCandidate(Candidate);
3760 });
3761
3762 // If the sequence doesn't have enough candidates left, then we're done.
3763 if (RepeatedSequenceLocs.size() < MinRepeats)
3764 return std::nullopt;
3765
3766 // Each RepeatedSequenceLoc is identical.
3767 outliner::Candidate &Candidate = RepeatedSequenceLocs[0];
3768 unsigned InstrSizeCExt =
3769 Candidate.getMF()->getSubtarget<RISCVSubtarget>().hasStdExtZca() ? 2 : 4;
3770 unsigned CallOverhead = 0, FrameOverhead = 0;
3771
3772 // Count the number of CFI instructions in the candidate, if present.
3773 unsigned CFICount = 0;
3774 for (auto &I : Candidate) {
3775 if (I.isCFIInstruction())
3776 CFICount++;
3777 }
3778
3779 // Ensure CFI coverage matches: comparing the number of CFIs in the candidate
3780 // with the total number of CFIs in the parent function for each candidate.
3781 // Outlining only a subset of a function’s CFIs would split the unwind state
3782 // across two code regions and lead to incorrect address offsets between the
3783 // outlined body and the remaining code. To preserve correct unwind info, we
3784 // only outline when all CFIs in the function can be outlined together.
3785 for (outliner::Candidate &C : RepeatedSequenceLocs) {
3786 std::vector<MCCFIInstruction> CFIInstructions =
3787 C.getMF()->getFrameInstructions();
3788
3789 if (CFICount > 0 && CFICount != CFIInstructions.size())
3790 return std::nullopt;
3791 }
3792
3794 if (Candidate.back().isReturn()) {
3796 // tail call = auipc + jalr in the worst case without linker relaxation.
3797 // FIXME: This code suggests the JALR can be compressed - how?
3798 CallOverhead = 4 + InstrSizeCExt;
3799 // Using tail call we move ret instruction from caller to callee.
3800 FrameOverhead = 0;
3801 } else {
3802 // call t0, function = 8 bytes.
3803 CallOverhead = 8;
3804 // jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled.
3805 FrameOverhead = InstrSizeCExt;
3806 }
3807
3808 // If we have CFI instructions, we can only outline if the outlined section
3809 // can be a tail call.
3810 if (MOCI != MachineOutlinerTailCall && CFICount > 0)
3811 return std::nullopt;
3812
3814 // Set per-candidate overhead based on X5 availability
3815 for (auto &C : RepeatedSequenceLocs) {
3816
3817 if (C.isAvailableAcrossAndOutOfSeq(RISCV::X5, RegInfo)) {
3818 // X5 is available, just need the call
3819 unsigned CandCallOverhead = 8;
3820 C.setCallInfo(MachineOutlinerDefault, CandCallOverhead);
3821 } else {
3822 // X5 unavailable, need save + call + restore
3823 // Save (2-4) + Call (8) + Restore (2-4)
3824 unsigned CandCallOverhead = InstrSizeCExt + 8 + InstrSizeCExt;
3825 C.setCallInfo(MachineOutlinerRegSave, CandCallOverhead);
3826 }
3827 }
3828 } else {
3829 for (auto &C : RepeatedSequenceLocs)
3830 C.setCallInfo(MOCI, CallOverhead);
3831 }
3832
3833 unsigned SequenceSize = 0;
3834 for (auto &MI : Candidate)
3835 SequenceSize += getInstSizeInBytes(MI);
3836
3837 return std::make_unique<outliner::OutlinedFunction>(
3838 RepeatedSequenceLocs, SequenceSize, FrameOverhead, MOCI);
3839}
3840
3844 unsigned Flags) const {
3845 MachineInstr &MI = *MBBI;
3846 MachineBasicBlock *MBB = MI.getParent();
3847 const TargetRegisterInfo *TRI =
3848 MBB->getParent()->getSubtarget().getRegisterInfo();
3849 const auto &F = MI.getMF()->getFunction();
3850
3851 // We can only outline CFI instructions if we will tail call the outlined
3852 // function, or fix up the CFI offsets. Currently, CFI instructions are
3853 // outlined only if in a tail call.
3854 if (MI.isCFIInstruction())
3856
3857 if (cannotInsertTailCall(*MBB) &&
3858 (MI.isReturn() || isMIModifiesReg(MI, TRI, RISCV::X5)))
3860
3861 // Make sure the operands don't reference something unsafe.
3862 for (const auto &MO : MI.operands()) {
3863
3864 // pcrel-hi and pcrel-lo can't put in separate sections, filter that out
3865 // if any possible.
3866 if (MO.getTargetFlags() == RISCVII::MO_PCREL_LO &&
3867 (MI.getMF()->getTarget().getFunctionSections() || F.hasComdat() ||
3868 F.hasSection() || F.getSectionPrefix()))
3870 }
3871
3872 if (isLPAD(MI))
3874
3876}
3877
3880 const outliner::OutlinedFunction &OF) const {
3881
3882 if (OF.FrameConstructionID == MachineOutlinerTailCall)
3883 return;
3884
3885 MBB.addLiveIn(RISCV::X5);
3886
3887 // Add in a return instruction to the end of the outlined frame.
3888 MBB.insert(MBB.end(), BuildMI(MF, DebugLoc(), get(RISCV::JALR))
3889 .addReg(RISCV::X0, RegState::Define)
3890 .addReg(RISCV::X5)
3891 .addImm(0));
3892}
3893
3897
3898 if (C.CallConstructionID == MachineOutlinerTailCall) {
3899 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(RISCV::PseudoTAIL))
3900 .addGlobalAddress(M.getNamedValue(MF.getName()),
3901 /*Offset=*/0, RISCVII::MO_CALL));
3902 return It;
3903 }
3904
3905 if (C.CallConstructionID == MachineOutlinerRegSave) {
3906 Register SaveReg = findRegisterToSaveX5To(C, RegInfo);
3907 assert(SaveReg && "Cannot find an available register to save/restore X5.");
3908
3909 // Save: ADDI SaveReg, X5, 0 (equivalent to MV SaveReg, X5)
3910 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(RISCV::ADDI), SaveReg)
3911 .addReg(RISCV::X5)
3912 .addImm(0));
3913 It++;
3914
3915 // Call: PseudoCALLReg X5
3916 It = MBB.insert(
3917 It, BuildMI(MF, DebugLoc(), get(RISCV::PseudoCALLReg), RISCV::X5)
3918 .addGlobalAddress(M.getNamedValue(MF.getName()), 0,
3920 MachineBasicBlock::iterator CallPt = It;
3921 It++;
3922
3923 // Restore: ADDI X5, SaveReg, 0 (equivalent to MV X5, SaveReg)
3924 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(RISCV::ADDI), RISCV::X5)
3925 .addReg(SaveReg)
3926 .addImm(0));
3927
3928 return CallPt;
3929 }
3930
3931 // Add in a call instruction to the outlined function at the given location.
3932 It = MBB.insert(It,
3933 BuildMI(MF, DebugLoc(), get(RISCV::PseudoCALLReg), RISCV::X5)
3934 .addGlobalAddress(M.getNamedValue(MF.getName()), 0,
3936 return It;
3937}
3938
3939std::optional<RegImmPair> RISCVInstrInfo::isAddImmediate(const MachineInstr &MI,
3940 Register Reg) const {
3941 // TODO: Handle cases where Reg is a super- or sub-register of the
3942 // destination register.
3943 const MachineOperand &Op0 = MI.getOperand(0);
3944 if (!Op0.isReg() || Reg != Op0.getReg())
3945 return std::nullopt;
3946
3947 // Don't consider ADDIW as a candidate because the caller may not be aware
3948 // of its sign extension behaviour.
3949 if (MI.getOpcode() == RISCV::ADDI && MI.getOperand(1).isReg() &&
3950 MI.getOperand(2).isImm())
3951 return RegImmPair{MI.getOperand(1).getReg(), MI.getOperand(2).getImm()};
3952
3953 return std::nullopt;
3954}
3955
3956// MIR printer helper function to annotate Operands with a comment.
3958 const MachineInstr &MI, const MachineOperand &Op, unsigned OpIdx,
3959 const TargetRegisterInfo *TRI) const {
3960 // Print a generic comment for this operand if there is one.
3961 std::string GenericComment =
3963 if (!GenericComment.empty())
3964 return GenericComment;
3965
3966 const MCInstrDesc &Desc = MI.getDesc();
3967 if (OpIdx >= Desc.getNumOperands())
3968 return std::string();
3969
3970 std::string Comment;
3971 raw_string_ostream OS(Comment);
3972
3973 const MCOperandInfo &OpInfo = Desc.operands()[OpIdx];
3974
3975 // Print the full VType operand of vsetvli/vsetivli instructions, and the SEW
3976 // operand of vector codegen pseudos.
3977 switch (OpInfo.OperandType) {
3980 unsigned Imm = Op.getImm();
3981 RISCVVType::printVType(Imm, OS);
3982 break;
3983 }
3985 unsigned Imm = Op.getImm();
3987 break;
3988 }
3990 unsigned Imm = Op.getImm();
3991 OS << "w" << Imm;
3992 break;
3993 }
3996 unsigned Log2SEW = Op.getImm();
3997 unsigned SEW = Log2SEW ? 1 << Log2SEW : 8;
3998 assert(RISCVVType::isValidSEW(SEW) && "Unexpected SEW");
3999 OS << "e" << SEW;
4000 break;
4001 }
4003 unsigned Policy = Op.getImm();
4005 "Invalid Policy Value");
4006 OS << (Policy & RISCVVType::TAIL_AGNOSTIC ? "ta" : "tu") << ", "
4007 << (Policy & RISCVVType::MASK_AGNOSTIC ? "ma" : "mu");
4008 break;
4009 }
4011 if (Op.isImm() && Op.getImm() == -1)
4012 OS << "vl=VLMAX";
4013 else
4014 OS << "vl";
4015 break;
4017 if (RISCVII::usesVXRM(Desc.TSFlags)) {
4019 auto VXRM = static_cast<RISCVVXRndMode::RoundingMode>(Op.getImm());
4020 OS << "vxrm=" << RISCVVXRndMode::roundingModeToString(VXRM);
4021 } else {
4023 auto FRM = static_cast<RISCVFPRndMode::RoundingMode>(Op.getImm());
4024 OS << "frm=" << RISCVFPRndMode::roundingModeToString(FRM);
4025 }
4026 break;
4027 }
4028
4029 return Comment;
4030}
4031
4032// clang-format off
4033#define CASE_RVV_OPCODE_UNMASK_LMUL(OP, LMUL) \
4034 RISCV::Pseudo##OP##_##LMUL
4035
4036#define CASE_RVV_OPCODE_MASK_LMUL(OP, LMUL) \
4037 RISCV::Pseudo##OP##_##LMUL##_MASK
4038
4039#define CASE_RVV_OPCODE_LMUL(OP, LMUL) \
4040 CASE_RVV_OPCODE_UNMASK_LMUL(OP, LMUL): \
4041 case CASE_RVV_OPCODE_MASK_LMUL(OP, LMUL)
4042
4043#define CASE_RVV_OPCODE_UNMASK_WIDEN(OP) \
4044 CASE_RVV_OPCODE_UNMASK_LMUL(OP, MF8): \
4045 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, MF4): \
4046 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, MF2): \
4047 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, M1): \
4048 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, M2): \
4049 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, M4)
4050
4051#define CASE_RVV_OPCODE_UNMASK(OP) \
4052 CASE_RVV_OPCODE_UNMASK_WIDEN(OP): \
4053 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, M8)
4054
4055#define CASE_RVV_OPCODE_MASK_WIDEN(OP) \
4056 CASE_RVV_OPCODE_MASK_LMUL(OP, MF8): \
4057 case CASE_RVV_OPCODE_MASK_LMUL(OP, MF4): \
4058 case CASE_RVV_OPCODE_MASK_LMUL(OP, MF2): \
4059 case CASE_RVV_OPCODE_MASK_LMUL(OP, M1): \
4060 case CASE_RVV_OPCODE_MASK_LMUL(OP, M2): \
4061 case CASE_RVV_OPCODE_MASK_LMUL(OP, M4)
4062
4063#define CASE_RVV_OPCODE_MASK(OP) \
4064 CASE_RVV_OPCODE_MASK_WIDEN(OP): \
4065 case CASE_RVV_OPCODE_MASK_LMUL(OP, M8)
4066
4067#define CASE_RVV_OPCODE_WIDEN(OP) \
4068 CASE_RVV_OPCODE_UNMASK_WIDEN(OP): \
4069 case CASE_RVV_OPCODE_MASK_WIDEN(OP)
4070
4071#define CASE_RVV_OPCODE(OP) \
4072 CASE_RVV_OPCODE_UNMASK(OP): \
4073 case CASE_RVV_OPCODE_MASK(OP)
4074// clang-format on
4075
4076// clang-format off
4077#define CASE_VMA_OPCODE_COMMON(OP, TYPE, LMUL) \
4078 RISCV::PseudoV##OP##_##TYPE##_##LMUL
4079
4080#define CASE_VMA_OPCODE_LMULS(OP, TYPE) \
4081 CASE_VMA_OPCODE_COMMON(OP, TYPE, MF8): \
4082 case CASE_VMA_OPCODE_COMMON(OP, TYPE, MF4): \
4083 case CASE_VMA_OPCODE_COMMON(OP, TYPE, MF2): \
4084 case CASE_VMA_OPCODE_COMMON(OP, TYPE, M1): \
4085 case CASE_VMA_OPCODE_COMMON(OP, TYPE, M2): \
4086 case CASE_VMA_OPCODE_COMMON(OP, TYPE, M4): \
4087 case CASE_VMA_OPCODE_COMMON(OP, TYPE, M8)
4088
4089// VFMA instructions are SEW specific.
4090#define CASE_VFMA_OPCODE_COMMON(OP, TYPE, LMUL, SEW) \
4091 RISCV::PseudoV##OP##_##TYPE##_##LMUL##_##SEW
4092
4093#define CASE_VFMA_OPCODE_LMULS_M1(OP, TYPE, SEW) \
4094 CASE_VFMA_OPCODE_COMMON(OP, TYPE, M1, SEW): \
4095 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M2, SEW): \
4096 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M4, SEW): \
4097 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M8, SEW)
4098
4099#define CASE_VFMA_OPCODE_LMULS_MF2(OP, TYPE, SEW) \
4100 CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF2, SEW): \
4101 case CASE_VFMA_OPCODE_LMULS_M1(OP, TYPE, SEW)
4102
4103#define CASE_VFMA_OPCODE_LMULS_MF4(OP, TYPE, SEW) \
4104 CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF4, SEW): \
4105 case CASE_VFMA_OPCODE_LMULS_MF2(OP, TYPE, SEW)
4106
4107#define CASE_VFMA_OPCODE_VV(OP) \
4108 CASE_VFMA_OPCODE_LMULS_MF4(OP, VV, E16): \
4109 case CASE_VFMA_OPCODE_LMULS_MF4(OP##_ALT, VV, E16): \
4110 case CASE_VFMA_OPCODE_LMULS_MF2(OP, VV, E32): \
4111 case CASE_VFMA_OPCODE_LMULS_M1(OP, VV, E64)
4112
4113#define CASE_VFMA_SPLATS(OP) \
4114 CASE_VFMA_OPCODE_LMULS_MF4(OP, VFPR16, E16): \
4115 case CASE_VFMA_OPCODE_LMULS_MF4(OP##_ALT, VFPR16, E16): \
4116 case CASE_VFMA_OPCODE_LMULS_MF2(OP, VFPR32, E32): \
4117 case CASE_VFMA_OPCODE_LMULS_M1(OP, VFPR64, E64)
4118// clang-format on
4119
4121 unsigned &SrcOpIdx1,
4122 unsigned &SrcOpIdx2) const {
4123 const MCInstrDesc &Desc = MI.getDesc();
4124 if (!Desc.isCommutable())
4125 return false;
4126
4127 switch (MI.getOpcode()) {
4128 case RISCV::TH_MVEQZ:
4129 case RISCV::TH_MVNEZ:
4130 // We can't commute operands if operand 2 (i.e., rs1 in
4131 // mveqz/mvnez rd,rs1,rs2) is the zero-register (as it is
4132 // not valid as the in/out-operand 1).
4133 if (MI.getOperand(2).getReg() == RISCV::X0)
4134 return false;
4135 // Operands 1 and 2 are commutable, if we switch the opcode.
4136 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 1, 2);
4137 case RISCV::QC_SELECTIEQ:
4138 case RISCV::QC_SELECTINE:
4139 case RISCV::QC_SELECTIIEQ:
4140 case RISCV::QC_SELECTIINE:
4141 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 1, 2);
4142 case RISCV::QC_MVEQ:
4143 case RISCV::QC_MVNE:
4144 case RISCV::QC_MVLT:
4145 case RISCV::QC_MVGE:
4146 case RISCV::QC_MVLTU:
4147 case RISCV::QC_MVGEU:
4148 case RISCV::QC_MVEQI:
4149 case RISCV::QC_MVNEI:
4150 case RISCV::QC_MVLTI:
4151 case RISCV::QC_MVGEI:
4152 case RISCV::QC_MVLTUI:
4153 case RISCV::QC_MVGEUI:
4154 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 1, 4);
4155 case RISCV::TH_MULA:
4156 case RISCV::TH_MULAW:
4157 case RISCV::TH_MULAH:
4158 case RISCV::TH_MULS:
4159 case RISCV::TH_MULSW:
4160 case RISCV::TH_MULSH:
4161 // Operands 2 and 3 are commutable.
4162 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3);
4163 case RISCV::PseudoCCMOVGPRNoX0:
4164 case RISCV::PseudoCCMOVGPR:
4165 // Operands 1 and 2 are commutable.
4166 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 1, 2);
4167 case CASE_RVV_OPCODE(VADD_VV):
4168 case CASE_RVV_OPCODE(VAND_VV):
4169 case CASE_RVV_OPCODE(VOR_VV):
4170 case CASE_RVV_OPCODE(VXOR_VV):
4171 case CASE_RVV_OPCODE_MASK(VMSEQ_VV):
4172 case CASE_RVV_OPCODE_MASK(VMSNE_VV):
4173 case CASE_RVV_OPCODE(VMIN_VV):
4174 case CASE_RVV_OPCODE(VMINU_VV):
4175 case CASE_RVV_OPCODE(VMAX_VV):
4176 case CASE_RVV_OPCODE(VMAXU_VV):
4177 case CASE_RVV_OPCODE(VMUL_VV):
4178 case CASE_RVV_OPCODE(VMULH_VV):
4179 case CASE_RVV_OPCODE(VMULHU_VV):
4180 case CASE_RVV_OPCODE_WIDEN(VWADD_VV):
4181 case CASE_RVV_OPCODE_WIDEN(VWADDU_VV):
4182 case CASE_RVV_OPCODE_WIDEN(VWMUL_VV):
4183 case CASE_RVV_OPCODE_WIDEN(VWMULU_VV):
4184 case CASE_RVV_OPCODE_WIDEN(VWMACC_VV):
4185 case CASE_RVV_OPCODE_WIDEN(VWMACCU_VV):
4186 case CASE_RVV_OPCODE(VABD_VV):
4187 case CASE_RVV_OPCODE(VABDU_VV):
4188 case CASE_RVV_OPCODE_WIDEN(VWABDA_VV):
4189 case CASE_RVV_OPCODE_WIDEN(VWABDAU_VV):
4190 case CASE_RVV_OPCODE_UNMASK(VADC_VVM):
4191 case CASE_RVV_OPCODE(VSADD_VV):
4192 case CASE_RVV_OPCODE(VSADDU_VV):
4193 case CASE_RVV_OPCODE(VAADD_VV):
4194 case CASE_RVV_OPCODE(VAADDU_VV):
4195 case CASE_RVV_OPCODE(VSMUL_VV):
4196 case CASE_RVV_OPCODE_LMUL(VDOT4A_VV, MF2):
4197 case CASE_RVV_OPCODE_LMUL(VDOT4A_VV, M1):
4198 case CASE_RVV_OPCODE_LMUL(VDOT4A_VV, M2):
4199 case CASE_RVV_OPCODE_LMUL(VDOT4A_VV, M4):
4200 case CASE_RVV_OPCODE_LMUL(VDOT4A_VV, M8):
4201 case CASE_RVV_OPCODE_LMUL(VDOT4AU_VV, MF2):
4202 case CASE_RVV_OPCODE_LMUL(VDOT4AU_VV, M1):
4203 case CASE_RVV_OPCODE_LMUL(VDOT4AU_VV, M2):
4204 case CASE_RVV_OPCODE_LMUL(VDOT4AU_VV, M4):
4205 case CASE_RVV_OPCODE_LMUL(VDOT4AU_VV, M8):
4206 // Operands 2 and 3 are commutable.
4207 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3);
4208 case CASE_VFMA_SPLATS(FMADD):
4209 case CASE_VFMA_SPLATS(FMSUB):
4210 case CASE_VFMA_SPLATS(FMACC):
4211 case CASE_VFMA_SPLATS(FMSAC):
4214 case CASE_VFMA_SPLATS(FNMACC):
4215 case CASE_VFMA_SPLATS(FNMSAC):
4216 case CASE_VFMA_OPCODE_VV(FMACC):
4217 case CASE_VFMA_OPCODE_VV(FMSAC):
4218 case CASE_VFMA_OPCODE_VV(FNMACC):
4219 case CASE_VFMA_OPCODE_VV(FNMSAC):
4220 case CASE_VMA_OPCODE_LMULS(MADD, VX):
4221 case CASE_VMA_OPCODE_LMULS(NMSUB, VX):
4222 case CASE_VMA_OPCODE_LMULS(MACC, VX):
4223 case CASE_VMA_OPCODE_LMULS(NMSAC, VX):
4224 case CASE_VMA_OPCODE_LMULS(MACC, VV):
4225 case CASE_VMA_OPCODE_LMULS(NMSAC, VV): {
4226 // If the tail policy is undisturbed we can't commute.
4227 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags));
4228 if ((MI.getOperand(RISCVII::getVecPolicyOpNum(MI.getDesc())).getImm() &
4229 1) == 0)
4230 return false;
4231
4232 // For these instructions we can only swap operand 1 and operand 3 by
4233 // changing the opcode.
4234 unsigned CommutableOpIdx1 = 1;
4235 unsigned CommutableOpIdx2 = 3;
4236 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
4237 CommutableOpIdx2))
4238 return false;
4239 return true;
4240 }
4241 case CASE_VFMA_OPCODE_VV(FMADD):
4245 case CASE_VMA_OPCODE_LMULS(MADD, VV):
4246 case CASE_VMA_OPCODE_LMULS(NMSUB, VV): {
4247 // If the tail policy is undisturbed we can't commute.
4248 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags));
4249 if ((MI.getOperand(RISCVII::getVecPolicyOpNum(MI.getDesc())).getImm() &
4250 1) == 0)
4251 return false;
4252
4253 // For these instructions we have more freedom. We can commute with the
4254 // other multiplicand or with the addend/subtrahend/minuend.
4255
4256 // Any fixed operand must be from source 1, 2 or 3.
4257 if (SrcOpIdx1 != CommuteAnyOperandIndex && SrcOpIdx1 > 3)
4258 return false;
4259 if (SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx2 > 3)
4260 return false;
4261
4262 // It both ops are fixed one must be the tied source.
4263 if (SrcOpIdx1 != CommuteAnyOperandIndex &&
4264 SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx1 != 1 && SrcOpIdx2 != 1)
4265 return false;
4266
4267 // Look for two different register operands assumed to be commutable
4268 // regardless of the FMA opcode. The FMA opcode is adjusted later if
4269 // needed.
4270 if (SrcOpIdx1 == CommuteAnyOperandIndex ||
4271 SrcOpIdx2 == CommuteAnyOperandIndex) {
4272 // At least one of operands to be commuted is not specified and
4273 // this method is free to choose appropriate commutable operands.
4274 unsigned CommutableOpIdx1 = SrcOpIdx1;
4275 if (SrcOpIdx1 == SrcOpIdx2) {
4276 // Both of operands are not fixed. Set one of commutable
4277 // operands to the tied source.
4278 CommutableOpIdx1 = 1;
4279 } else if (SrcOpIdx1 == CommuteAnyOperandIndex) {
4280 // Only one of the operands is not fixed.
4281 CommutableOpIdx1 = SrcOpIdx2;
4282 }
4283
4284 // CommutableOpIdx1 is well defined now. Let's choose another commutable
4285 // operand and assign its index to CommutableOpIdx2.
4286 unsigned CommutableOpIdx2;
4287 if (CommutableOpIdx1 != 1) {
4288 // If we haven't already used the tied source, we must use it now.
4289 CommutableOpIdx2 = 1;
4290 } else {
4291 Register Op1Reg = MI.getOperand(CommutableOpIdx1).getReg();
4292
4293 // The commuted operands should have different registers.
4294 // Otherwise, the commute transformation does not change anything and
4295 // is useless. We use this as a hint to make our decision.
4296 if (Op1Reg != MI.getOperand(2).getReg())
4297 CommutableOpIdx2 = 2;
4298 else
4299 CommutableOpIdx2 = 3;
4300 }
4301
4302 // Assign the found pair of commutable indices to SrcOpIdx1 and
4303 // SrcOpIdx2 to return those values.
4304 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
4305 CommutableOpIdx2))
4306 return false;
4307 }
4308
4309 return true;
4310 }
4311 }
4312
4313 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
4314}
4315
4316// clang-format off
4317#define CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, LMUL) \
4318 case RISCV::PseudoV##OLDOP##_##TYPE##_##LMUL: \
4319 Opc = RISCV::PseudoV##NEWOP##_##TYPE##_##LMUL; \
4320 break;
4321
4322#define CASE_VMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, TYPE) \
4323 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF8) \
4324 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF4) \
4325 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF2) \
4326 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M1) \
4327 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M2) \
4328 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M4) \
4329 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M8)
4330
4331// VFMA depends on SEW.
4332#define CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, LMUL, SEW) \
4333 case RISCV::PseudoV##OLDOP##_##TYPE##_##LMUL##_##SEW: \
4334 Opc = RISCV::PseudoV##NEWOP##_##TYPE##_##LMUL##_##SEW; \
4335 break;
4336
4337#define CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, TYPE, SEW) \
4338 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M1, SEW) \
4339 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M2, SEW) \
4340 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M4, SEW) \
4341 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M8, SEW)
4342
4343#define CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, TYPE, SEW) \
4344 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF2, SEW) \
4345 CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, TYPE, SEW)
4346
4347#define CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP, NEWOP, TYPE, SEW) \
4348 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF4, SEW) \
4349 CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, TYPE, SEW)
4350
4351#define CASE_VFMA_CHANGE_OPCODE_VV(OLDOP, NEWOP) \
4352 CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP, NEWOP, VV, E16) \
4353 CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP##_ALT, NEWOP##_ALT, VV, E16) \
4354 CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, VV, E32) \
4355 CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, VV, E64)
4356
4357#define CASE_VFMA_CHANGE_OPCODE_SPLATS(OLDOP, NEWOP) \
4358 CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP, NEWOP, VFPR16, E16) \
4359 CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP##_ALT, NEWOP##_ALT, VFPR16, E16) \
4360 CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, VFPR32, E32) \
4361 CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, VFPR64, E64)
4362// clang-format on
4363
4365 bool NewMI,
4366 unsigned OpIdx1,
4367 unsigned OpIdx2) const {
4368 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & {
4369 if (NewMI)
4370 return *MI.getParent()->getParent()->CloneMachineInstr(&MI);
4371 return MI;
4372 };
4373
4374 switch (MI.getOpcode()) {
4375 case RISCV::TH_MVEQZ:
4376 case RISCV::TH_MVNEZ: {
4377 auto &WorkingMI = cloneIfNew(MI);
4378 WorkingMI.setDesc(get(MI.getOpcode() == RISCV::TH_MVEQZ ? RISCV::TH_MVNEZ
4379 : RISCV::TH_MVEQZ));
4380 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, false, OpIdx1,
4381 OpIdx2);
4382 }
4383 case RISCV::QC_SELECTIEQ:
4384 case RISCV::QC_SELECTINE:
4385 case RISCV::QC_SELECTIIEQ:
4386 case RISCV::QC_SELECTIINE:
4387 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
4388 case RISCV::QC_MVEQ:
4389 case RISCV::QC_MVNE:
4390 case RISCV::QC_MVLT:
4391 case RISCV::QC_MVGE:
4392 case RISCV::QC_MVLTU:
4393 case RISCV::QC_MVGEU:
4394 case RISCV::QC_MVEQI:
4395 case RISCV::QC_MVNEI:
4396 case RISCV::QC_MVLTI:
4397 case RISCV::QC_MVGEI:
4398 case RISCV::QC_MVLTUI:
4399 case RISCV::QC_MVGEUI: {
4400 auto &WorkingMI = cloneIfNew(MI);
4401 WorkingMI.setDesc(get(getInverseXqcicmOpcode(MI.getOpcode())));
4402 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, false, OpIdx1,
4403 OpIdx2);
4404 }
4405 case RISCV::PseudoCCMOVGPRNoX0:
4406 case RISCV::PseudoCCMOVGPR: {
4407 // CCMOV can be commuted by inverting the condition.
4408 unsigned BCC = MI.getOperand(MI.getNumExplicitOperands() - 3).getImm();
4410 auto &WorkingMI = cloneIfNew(MI);
4411 WorkingMI.getOperand(MI.getNumExplicitOperands() - 3).setImm(BCC);
4412 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI*/ false,
4413 OpIdx1, OpIdx2);
4414 }
4415 case CASE_VFMA_SPLATS(FMACC):
4416 case CASE_VFMA_SPLATS(FMADD):
4417 case CASE_VFMA_SPLATS(FMSAC):
4418 case CASE_VFMA_SPLATS(FMSUB):
4419 case CASE_VFMA_SPLATS(FNMACC):
4421 case CASE_VFMA_SPLATS(FNMSAC):
4423 case CASE_VFMA_OPCODE_VV(FMACC):
4424 case CASE_VFMA_OPCODE_VV(FMSAC):
4425 case CASE_VFMA_OPCODE_VV(FNMACC):
4426 case CASE_VFMA_OPCODE_VV(FNMSAC):
4427 case CASE_VMA_OPCODE_LMULS(MADD, VX):
4428 case CASE_VMA_OPCODE_LMULS(NMSUB, VX):
4429 case CASE_VMA_OPCODE_LMULS(MACC, VX):
4430 case CASE_VMA_OPCODE_LMULS(NMSAC, VX):
4431 case CASE_VMA_OPCODE_LMULS(MACC, VV):
4432 case CASE_VMA_OPCODE_LMULS(NMSAC, VV): {
4433 // It only make sense to toggle these between clobbering the
4434 // addend/subtrahend/minuend one of the multiplicands.
4435 assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index");
4436 assert((OpIdx1 == 3 || OpIdx2 == 3) && "Unexpected opcode index");
4437 unsigned Opc;
4438 switch (MI.getOpcode()) {
4439 default:
4440 llvm_unreachable("Unexpected opcode");
4441 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMACC, FMADD)
4442 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMADD, FMACC)
4449 CASE_VFMA_CHANGE_OPCODE_VV(FMACC, FMADD)
4453 CASE_VMA_CHANGE_OPCODE_LMULS(MACC, MADD, VX)
4454 CASE_VMA_CHANGE_OPCODE_LMULS(MADD, MACC, VX)
4455 CASE_VMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, VX)
4456 CASE_VMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VX)
4457 CASE_VMA_CHANGE_OPCODE_LMULS(MACC, MADD, VV)
4458 CASE_VMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, VV)
4459 }
4460
4461 auto &WorkingMI = cloneIfNew(MI);
4462 WorkingMI.setDesc(get(Opc));
4463 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
4464 OpIdx1, OpIdx2);
4465 }
4466 case CASE_VFMA_OPCODE_VV(FMADD):
4470 case CASE_VMA_OPCODE_LMULS(MADD, VV):
4471 case CASE_VMA_OPCODE_LMULS(NMSUB, VV): {
4472 assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index");
4473 // If one of the operands, is the addend we need to change opcode.
4474 // Otherwise we're just swapping 2 of the multiplicands.
4475 if (OpIdx1 == 3 || OpIdx2 == 3) {
4476 unsigned Opc;
4477 switch (MI.getOpcode()) {
4478 default:
4479 llvm_unreachable("Unexpected opcode");
4480 CASE_VFMA_CHANGE_OPCODE_VV(FMADD, FMACC)
4484 CASE_VMA_CHANGE_OPCODE_LMULS(MADD, MACC, VV)
4485 CASE_VMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VV)
4486 }
4487
4488 auto &WorkingMI = cloneIfNew(MI);
4489 WorkingMI.setDesc(get(Opc));
4490 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
4491 OpIdx1, OpIdx2);
4492 }
4493 // Let the default code handle it.
4494 break;
4495 }
4496 }
4497
4498 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
4499}
4500
4501#undef CASE_VMA_CHANGE_OPCODE_COMMON
4502#undef CASE_VMA_CHANGE_OPCODE_LMULS
4503#undef CASE_VFMA_CHANGE_OPCODE_COMMON
4504#undef CASE_VFMA_CHANGE_OPCODE_LMULS_M1
4505#undef CASE_VFMA_CHANGE_OPCODE_LMULS_MF2
4506#undef CASE_VFMA_CHANGE_OPCODE_LMULS_MF4
4507#undef CASE_VFMA_CHANGE_OPCODE_VV
4508#undef CASE_VFMA_CHANGE_OPCODE_SPLATS
4509
4510#undef CASE_RVV_OPCODE_UNMASK_LMUL
4511#undef CASE_RVV_OPCODE_MASK_LMUL
4512#undef CASE_RVV_OPCODE_LMUL
4513#undef CASE_RVV_OPCODE_UNMASK_WIDEN
4514#undef CASE_RVV_OPCODE_UNMASK
4515#undef CASE_RVV_OPCODE_MASK_WIDEN
4516#undef CASE_RVV_OPCODE_MASK
4517#undef CASE_RVV_OPCODE_WIDEN
4518#undef CASE_RVV_OPCODE
4519
4520#undef CASE_VMA_OPCODE_COMMON
4521#undef CASE_VMA_OPCODE_LMULS
4522#undef CASE_VFMA_OPCODE_COMMON
4523#undef CASE_VFMA_OPCODE_LMULS_M1
4524#undef CASE_VFMA_OPCODE_LMULS_MF2
4525#undef CASE_VFMA_OPCODE_LMULS_MF4
4526#undef CASE_VFMA_OPCODE_VV
4527#undef CASE_VFMA_SPLATS
4528
4530 switch (MI.getOpcode()) {
4531 default:
4532 break;
4533 case RISCV::ADD:
4534 case RISCV::OR:
4535 case RISCV::XOR:
4536 // Normalize (so we hit the next if clause).
4537 // add/[x]or rd, zero, rs => add/[x]or rd, rs, zero
4538 if (MI.getOperand(1).getReg() == RISCV::X0)
4539 commuteInstruction(MI);
4540 // add/[x]or rd, rs, zero => addi rd, rs, 0
4541 if (MI.getOperand(2).getReg() == RISCV::X0) {
4542 MI.getOperand(2).ChangeToImmediate(0);
4543 MI.setDesc(get(RISCV::ADDI));
4544 return true;
4545 }
4546 // xor rd, rs, rs => addi rd, zero, 0
4547 if (MI.getOpcode() == RISCV::XOR &&
4548 MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
4549 MI.getOperand(1).setReg(RISCV::X0);
4550 MI.getOperand(2).ChangeToImmediate(0);
4551 MI.setDesc(get(RISCV::ADDI));
4552 return true;
4553 }
4554 break;
4555 case RISCV::ORI:
4556 case RISCV::XORI:
4557 // [x]ori rd, zero, N => addi rd, zero, N
4558 if (MI.getOperand(1).getReg() == RISCV::X0) {
4559 MI.setDesc(get(RISCV::ADDI));
4560 return true;
4561 }
4562 break;
4563 case RISCV::SUB:
4564 // sub rd, rs, zero => addi rd, rs, 0
4565 if (MI.getOperand(2).getReg() == RISCV::X0) {
4566 MI.getOperand(2).ChangeToImmediate(0);
4567 MI.setDesc(get(RISCV::ADDI));
4568 return true;
4569 }
4570 break;
4571 case RISCV::SUBW:
4572 // subw rd, rs, zero => addiw rd, rs, 0
4573 if (MI.getOperand(2).getReg() == RISCV::X0) {
4574 MI.getOperand(2).ChangeToImmediate(0);
4575 MI.setDesc(get(RISCV::ADDIW));
4576 return true;
4577 }
4578 break;
4579 case RISCV::ADDW:
4580 // Normalize (so we hit the next if clause).
4581 // addw rd, zero, rs => addw rd, rs, zero
4582 if (MI.getOperand(1).getReg() == RISCV::X0)
4583 commuteInstruction(MI);
4584 // addw rd, rs, zero => addiw rd, rs, 0
4585 if (MI.getOperand(2).getReg() == RISCV::X0) {
4586 MI.getOperand(2).ChangeToImmediate(0);
4587 MI.setDesc(get(RISCV::ADDIW));
4588 return true;
4589 }
4590 break;
4591 case RISCV::SH1ADD:
4592 case RISCV::SH1ADD_UW:
4593 case RISCV::SH2ADD:
4594 case RISCV::SH2ADD_UW:
4595 case RISCV::SH3ADD:
4596 case RISCV::SH3ADD_UW:
4597 // shNadd[.uw] rd, zero, rs => addi rd, rs, 0
4598 if (MI.getOperand(1).getReg() == RISCV::X0) {
4599 MI.removeOperand(1);
4600 MI.addOperand(MachineOperand::CreateImm(0));
4601 MI.setDesc(get(RISCV::ADDI));
4602 return true;
4603 }
4604 // shNadd[.uw] rd, rs, zero => slli[.uw] rd, rs, N
4605 if (MI.getOperand(2).getReg() == RISCV::X0) {
4606 MI.removeOperand(2);
4607 unsigned Opc = MI.getOpcode();
4608 if (Opc == RISCV::SH1ADD_UW || Opc == RISCV::SH2ADD_UW ||
4609 Opc == RISCV::SH3ADD_UW) {
4611 MI.setDesc(get(RISCV::SLLI_UW));
4612 return true;
4613 }
4615 MI.setDesc(get(RISCV::SLLI));
4616 return true;
4617 }
4618 break;
4619 case RISCV::AND:
4620 case RISCV::MUL:
4621 case RISCV::MULH:
4622 case RISCV::MULHSU:
4623 case RISCV::MULHU:
4624 case RISCV::MULW:
4625 // and rd, zero, rs => addi rd, zero, 0
4626 // mul* rd, zero, rs => addi rd, zero, 0
4627 // and rd, rs, zero => addi rd, zero, 0
4628 // mul* rd, rs, zero => addi rd, zero, 0
4629 if (MI.getOperand(1).getReg() == RISCV::X0 ||
4630 MI.getOperand(2).getReg() == RISCV::X0) {
4631 MI.getOperand(1).setReg(RISCV::X0);
4632 MI.getOperand(2).ChangeToImmediate(0);
4633 MI.setDesc(get(RISCV::ADDI));
4634 return true;
4635 }
4636 break;
4637 case RISCV::ANDI:
4638 // andi rd, zero, C => addi rd, zero, 0
4639 if (MI.getOperand(1).getReg() == RISCV::X0) {
4640 MI.getOperand(2).setImm(0);
4641 MI.setDesc(get(RISCV::ADDI));
4642 return true;
4643 }
4644 break;
4645 case RISCV::SLL:
4646 case RISCV::SRL:
4647 case RISCV::SRA:
4648 // shift rd, zero, rs => addi rd, zero, 0
4649 if (MI.getOperand(1).getReg() == RISCV::X0) {
4650 MI.getOperand(2).ChangeToImmediate(0);
4651 MI.setDesc(get(RISCV::ADDI));
4652 return true;
4653 }
4654 // shift rd, rs, zero => addi rd, rs, 0
4655 if (MI.getOperand(2).getReg() == RISCV::X0) {
4656 MI.getOperand(2).ChangeToImmediate(0);
4657 MI.setDesc(get(RISCV::ADDI));
4658 return true;
4659 }
4660 break;
4661 case RISCV::SLLW:
4662 case RISCV::SRLW:
4663 case RISCV::SRAW:
4664 // shiftw rd, zero, rs => addi rd, zero, 0
4665 if (MI.getOperand(1).getReg() == RISCV::X0) {
4666 MI.getOperand(2).ChangeToImmediate(0);
4667 MI.setDesc(get(RISCV::ADDI));
4668 return true;
4669 }
4670 break;
4671 case RISCV::SLLI:
4672 case RISCV::SRLI:
4673 case RISCV::SRAI:
4674 case RISCV::SLLIW:
4675 case RISCV::SRLIW:
4676 case RISCV::SRAIW:
4677 case RISCV::SLLI_UW:
4678 // shiftimm rd, zero, N => addi rd, zero, 0
4679 if (MI.getOperand(1).getReg() == RISCV::X0) {
4680 MI.getOperand(2).setImm(0);
4681 MI.setDesc(get(RISCV::ADDI));
4682 return true;
4683 }
4684 break;
4685 case RISCV::SLTU:
4686 case RISCV::ADD_UW:
4687 // sltu rd, zero, zero => addi rd, zero, 0
4688 // add.uw rd, zero, zero => addi rd, zero, 0
4689 if (MI.getOperand(1).getReg() == RISCV::X0 &&
4690 MI.getOperand(2).getReg() == RISCV::X0) {
4691 MI.getOperand(2).ChangeToImmediate(0);
4692 MI.setDesc(get(RISCV::ADDI));
4693 return true;
4694 }
4695 // add.uw rd, zero, rs => addi rd, rs, 0
4696 if (MI.getOpcode() == RISCV::ADD_UW &&
4697 MI.getOperand(1).getReg() == RISCV::X0) {
4698 MI.removeOperand(1);
4699 MI.addOperand(MachineOperand::CreateImm(0));
4700 MI.setDesc(get(RISCV::ADDI));
4701 }
4702 break;
4703 case RISCV::SLTIU:
4704 // sltiu rd, zero, NZC => addi rd, zero, 1
4705 // sltiu rd, zero, 0 => addi rd, zero, 0
4706 if (MI.getOperand(1).getReg() == RISCV::X0) {
4707 MI.getOperand(2).setImm(MI.getOperand(2).getImm() != 0);
4708 MI.setDesc(get(RISCV::ADDI));
4709 return true;
4710 }
4711 break;
4712 case RISCV::SEXT_H:
4713 case RISCV::SEXT_B:
4714 case RISCV::ZEXT_H_RV32:
4715 case RISCV::ZEXT_H_RV64:
4716 // sext.[hb] rd, zero => addi rd, zero, 0
4717 // zext.h rd, zero => addi rd, zero, 0
4718 if (MI.getOperand(1).getReg() == RISCV::X0) {
4719 MI.addOperand(MachineOperand::CreateImm(0));
4720 MI.setDesc(get(RISCV::ADDI));
4721 return true;
4722 }
4723 break;
4724 case RISCV::MIN:
4725 case RISCV::MINU:
4726 case RISCV::MAX:
4727 case RISCV::MAXU:
4728 // min|max rd, rs, rs => addi rd, rs, 0
4729 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
4730 MI.getOperand(2).ChangeToImmediate(0);
4731 MI.setDesc(get(RISCV::ADDI));
4732 return true;
4733 }
4734 break;
4735 case RISCV::BEQ:
4736 case RISCV::BNE:
4737 // b{eq,ne} zero, rs, imm => b{eq,ne} rs, zero, imm
4738 if (MI.getOperand(0).getReg() == RISCV::X0) {
4739 MachineOperand MO0 = MI.getOperand(0);
4740 MI.removeOperand(0);
4741 MI.insert(MI.operands_begin() + 1, {MO0});
4742 }
4743 break;
4744 case RISCV::BLTU:
4745 // bltu zero, rs, imm => bne rs, zero, imm
4746 if (MI.getOperand(0).getReg() == RISCV::X0) {
4747 MachineOperand MO0 = MI.getOperand(0);
4748 MI.removeOperand(0);
4749 MI.insert(MI.operands_begin() + 1, {MO0});
4750 MI.setDesc(get(RISCV::BNE));
4751 }
4752 break;
4753 case RISCV::BGEU:
4754 // bgeu zero, rs, imm => beq rs, zero, imm
4755 if (MI.getOperand(0).getReg() == RISCV::X0) {
4756 MachineOperand MO0 = MI.getOperand(0);
4757 MI.removeOperand(0);
4758 MI.insert(MI.operands_begin() + 1, {MO0});
4759 MI.setDesc(get(RISCV::BEQ));
4760 }
4761 break;
4762 }
4763 return false;
4764}
4765
4766// clang-format off
4767#define CASE_WIDEOP_OPCODE_COMMON(OP, LMUL) \
4768 RISCV::PseudoV##OP##_##LMUL##_TIED
4769
4770#define CASE_WIDEOP_OPCODE_LMULS(OP) \
4771 CASE_WIDEOP_OPCODE_COMMON(OP, MF8): \
4772 case CASE_WIDEOP_OPCODE_COMMON(OP, MF4): \
4773 case CASE_WIDEOP_OPCODE_COMMON(OP, MF2): \
4774 case CASE_WIDEOP_OPCODE_COMMON(OP, M1): \
4775 case CASE_WIDEOP_OPCODE_COMMON(OP, M2): \
4776 case CASE_WIDEOP_OPCODE_COMMON(OP, M4)
4777
4778#define CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, LMUL) \
4779 case RISCV::PseudoV##OP##_##LMUL##_TIED: \
4780 NewOpc = RISCV::PseudoV##OP##_##LMUL; \
4781 break;
4782
4783#define CASE_WIDEOP_CHANGE_OPCODE_LMULS(OP) \
4784 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF8) \
4785 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF4) \
4786 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF2) \
4787 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M1) \
4788 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M2) \
4789 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4)
4790
4791// FP Widening Ops may by SEW aware. Create SEW aware cases for these cases.
4792#define CASE_FP_WIDEOP_OPCODE_COMMON(OP, LMUL, SEW) \
4793 RISCV::PseudoV##OP##_##LMUL##_##SEW##_TIED
4794
4795#define CASE_FP_WIDEOP_OPCODE_LMULS(OP) \
4796 CASE_FP_WIDEOP_OPCODE_COMMON(OP, MF4, E16): \
4797 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, MF2, E16): \
4798 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, MF2, E32): \
4799 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M1, E16): \
4800 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M1, E32): \
4801 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M2, E16): \
4802 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M2, E32): \
4803 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M4, E16): \
4804 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M4, E32) \
4805
4806#define CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, LMUL, SEW) \
4807 case RISCV::PseudoV##OP##_##LMUL##_##SEW##_TIED: \
4808 NewOpc = RISCV::PseudoV##OP##_##LMUL##_##SEW; \
4809 break;
4810
4811#define CASE_FP_WIDEOP_CHANGE_OPCODE_LMULS(OP) \
4812 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF4, E16) \
4813 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF2, E16) \
4814 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF2, E32) \
4815 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M1, E16) \
4816 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M1, E32) \
4817 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M2, E16) \
4818 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M2, E32) \
4819 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4, E16) \
4820 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4, E32) \
4821
4822#define CASE_FP_WIDEOP_OPCODE_LMULS_ALT(OP) \
4823 CASE_FP_WIDEOP_OPCODE_COMMON(OP, MF4, E16): \
4824 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, MF2, E16): \
4825 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M1, E16): \
4826 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M2, E16): \
4827 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M4, E16)
4828
4829#define CASE_FP_WIDEOP_CHANGE_OPCODE_LMULS_ALT(OP) \
4830 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF4, E16) \
4831 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF2, E16) \
4832 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M1, E16) \
4833 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M2, E16) \
4834 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4, E16)
4835// clang-format on
4836
4838 LiveVariables *LV,
4839 LiveIntervals *LIS) const {
4841 switch (MI.getOpcode()) {
4842 default:
4843 return nullptr;
4844 case CASE_FP_WIDEOP_OPCODE_LMULS_ALT(FWADD_ALT_WV):
4845 case CASE_FP_WIDEOP_OPCODE_LMULS_ALT(FWSUB_ALT_WV):
4846 case CASE_FP_WIDEOP_OPCODE_LMULS(FWADD_WV):
4847 case CASE_FP_WIDEOP_OPCODE_LMULS(FWSUB_WV): {
4848 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags) &&
4849 MI.getNumExplicitOperands() == 7 &&
4850 "Expect 7 explicit operands rd, rs2, rs1, rm, vl, sew, policy");
4851 // If the tail policy is undisturbed we can't convert.
4852 if ((MI.getOperand(RISCVII::getVecPolicyOpNum(MI.getDesc())).getImm() &
4853 1) == 0)
4854 return nullptr;
4855 // clang-format off
4856 unsigned NewOpc;
4857 switch (MI.getOpcode()) {
4858 default:
4859 llvm_unreachable("Unexpected opcode");
4864 }
4865 // clang-format on
4866
4867 MachineBasicBlock &MBB = *MI.getParent();
4868 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
4869 .add(MI.getOperand(0))
4870 .addReg(MI.getOperand(0).getReg(), RegState::Undef)
4871 .add(MI.getOperand(1))
4872 .add(MI.getOperand(2))
4873 .add(MI.getOperand(3))
4874 .add(MI.getOperand(4))
4875 .add(MI.getOperand(5))
4876 .add(MI.getOperand(6));
4877 break;
4878 }
4879 case CASE_WIDEOP_OPCODE_LMULS(WADD_WV):
4880 case CASE_WIDEOP_OPCODE_LMULS(WADDU_WV):
4881 case CASE_WIDEOP_OPCODE_LMULS(WSUB_WV):
4882 case CASE_WIDEOP_OPCODE_LMULS(WSUBU_WV): {
4883 // If the tail policy is undisturbed we can't convert.
4884 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags) &&
4885 MI.getNumExplicitOperands() == 6);
4886 if ((MI.getOperand(RISCVII::getVecPolicyOpNum(MI.getDesc())).getImm() &
4887 1) == 0)
4888 return nullptr;
4889
4890 // clang-format off
4891 unsigned NewOpc;
4892 switch (MI.getOpcode()) {
4893 default:
4894 llvm_unreachable("Unexpected opcode");
4899 }
4900 // clang-format on
4901
4902 MachineBasicBlock &MBB = *MI.getParent();
4903 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
4904 .add(MI.getOperand(0))
4905 .addReg(MI.getOperand(0).getReg(), RegState::Undef)
4906 .add(MI.getOperand(1))
4907 .add(MI.getOperand(2))
4908 .add(MI.getOperand(3))
4909 .add(MI.getOperand(4))
4910 .add(MI.getOperand(5));
4911 break;
4912 }
4913 }
4914 MIB.copyImplicitOps(MI);
4915
4916 if (LV) {
4917 unsigned NumOps = MI.getNumOperands();
4918 for (unsigned I = 1; I < NumOps; ++I) {
4919 MachineOperand &Op = MI.getOperand(I);
4920 if (Op.isReg() && Op.isKill())
4921 LV->replaceKillInstruction(Op.getReg(), MI, *MIB);
4922 }
4923 }
4924
4925 if (LIS) {
4926 SlotIndex Idx = LIS->ReplaceMachineInstrInMaps(MI, *MIB);
4927
4928 if (MI.getOperand(0).isEarlyClobber()) {
4929 // Use operand 1 was tied to early-clobber def operand 0, so its live
4930 // interval could have ended at an early-clobber slot. Now they are not
4931 // tied we need to update it to the normal register slot.
4932 LiveInterval &LI = LIS->getInterval(MI.getOperand(1).getReg());
4934 if (S->end == Idx.getRegSlot(true))
4935 S->end = Idx.getRegSlot();
4936 }
4937 }
4938
4939 return MIB;
4940}
4941
4942#undef CASE_WIDEOP_OPCODE_COMMON
4943#undef CASE_WIDEOP_OPCODE_LMULS
4944#undef CASE_WIDEOP_CHANGE_OPCODE_COMMON
4945#undef CASE_WIDEOP_CHANGE_OPCODE_LMULS
4946#undef CASE_FP_WIDEOP_OPCODE_COMMON
4947#undef CASE_FP_WIDEOP_OPCODE_LMULS
4948#undef CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON
4949#undef CASE_FP_WIDEOP_CHANGE_OPCODE_LMULS
4950
4953 Register DestReg, uint32_t Amount,
4954 MachineInstr::MIFlag Flag) const {
4955 MachineRegisterInfo &MRI = MF.getRegInfo();
4956 if (llvm::has_single_bit<uint32_t>(Amount)) {
4957 uint32_t ShiftAmount = Log2_32(Amount);
4958 if (ShiftAmount == 0)
4959 return;
4960 BuildMI(MBB, II, DL, get(RISCV::SLLI), DestReg)
4961 .addReg(DestReg, RegState::Kill)
4962 .addImm(ShiftAmount)
4963 .setMIFlag(Flag);
4964 } else if (int ShXAmount, ShiftAmount;
4965 STI.hasShlAdd(3) &&
4966 (ShXAmount = isShifted359(Amount, ShiftAmount)) != 0) {
4967 // We can use Zba SHXADD+SLLI instructions for multiply in some cases.
4968 unsigned Opc;
4969 switch (ShXAmount) {
4970 case 1:
4971 Opc = RISCV::SH1ADD;
4972 break;
4973 case 2:
4974 Opc = RISCV::SH2ADD;
4975 break;
4976 case 3:
4977 Opc = RISCV::SH3ADD;
4978 break;
4979 default:
4980 llvm_unreachable("unexpected result of isShifted359");
4981 }
4982 if (ShiftAmount)
4983 BuildMI(MBB, II, DL, get(RISCV::SLLI), DestReg)
4984 .addReg(DestReg, RegState::Kill)
4985 .addImm(ShiftAmount)
4986 .setMIFlag(Flag);
4987 BuildMI(MBB, II, DL, get(Opc), DestReg)
4988 .addReg(DestReg, RegState::Kill)
4989 .addReg(DestReg)
4990 .setMIFlag(Flag);
4991 } else if (llvm::has_single_bit<uint32_t>(Amount - 1)) {
4992 Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass);
4993 uint32_t ShiftAmount = Log2_32(Amount - 1);
4994 BuildMI(MBB, II, DL, get(RISCV::SLLI), ScaledRegister)
4995 .addReg(DestReg)
4996 .addImm(ShiftAmount)
4997 .setMIFlag(Flag);
4998 BuildMI(MBB, II, DL, get(RISCV::ADD), DestReg)
4999 .addReg(ScaledRegister, RegState::Kill)
5000 .addReg(DestReg, RegState::Kill)
5001 .setMIFlag(Flag);
5002 } else if (llvm::has_single_bit<uint32_t>(Amount + 1)) {
5003 Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass);
5004 uint32_t ShiftAmount = Log2_32(Amount + 1);
5005 BuildMI(MBB, II, DL, get(RISCV::SLLI), ScaledRegister)
5006 .addReg(DestReg)
5007 .addImm(ShiftAmount)
5008 .setMIFlag(Flag);
5009 BuildMI(MBB, II, DL, get(RISCV::SUB), DestReg)
5010 .addReg(ScaledRegister, RegState::Kill)
5011 .addReg(DestReg, RegState::Kill)
5012 .setMIFlag(Flag);
5013 } else if (STI.hasStdExtZmmul()) {
5014 Register N = MRI.createVirtualRegister(&RISCV::GPRRegClass);
5015 movImm(MBB, II, DL, N, Amount, Flag);
5016 BuildMI(MBB, II, DL, get(RISCV::MUL), DestReg)
5017 .addReg(DestReg, RegState::Kill)
5019 .setMIFlag(Flag);
5020 } else {
5021 Register Acc;
5022 uint32_t PrevShiftAmount = 0;
5023 for (uint32_t ShiftAmount = 0; Amount >> ShiftAmount; ShiftAmount++) {
5024 if (Amount & (1U << ShiftAmount)) {
5025 if (ShiftAmount)
5026 BuildMI(MBB, II, DL, get(RISCV::SLLI), DestReg)
5027 .addReg(DestReg, RegState::Kill)
5028 .addImm(ShiftAmount - PrevShiftAmount)
5029 .setMIFlag(Flag);
5030 if (Amount >> (ShiftAmount + 1)) {
5031 // If we don't have an accmulator yet, create it and copy DestReg.
5032 if (!Acc) {
5033 Acc = MRI.createVirtualRegister(&RISCV::GPRRegClass);
5034 BuildMI(MBB, II, DL, get(TargetOpcode::COPY), Acc)
5035 .addReg(DestReg)
5036 .setMIFlag(Flag);
5037 } else {
5038 BuildMI(MBB, II, DL, get(RISCV::ADD), Acc)
5039 .addReg(Acc, RegState::Kill)
5040 .addReg(DestReg)
5041 .setMIFlag(Flag);
5042 }
5043 }
5044 PrevShiftAmount = ShiftAmount;
5045 }
5046 }
5047 assert(Acc && "Expected valid accumulator");
5048 BuildMI(MBB, II, DL, get(RISCV::ADD), DestReg)
5049 .addReg(DestReg, RegState::Kill)
5050 .addReg(Acc, RegState::Kill)
5051 .setMIFlag(Flag);
5052 }
5053}
5054
5057 static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] =
5058 {{MONontemporalBit0, "riscv-nontemporal-domain-bit-0"},
5059 {MONontemporalBit1, "riscv-nontemporal-domain-bit-1"}};
5060 return ArrayRef(TargetFlags);
5061}
5062
5064 return OptLevel >= CodeGenOptLevel::Aggressive
5065 ? STI.getTailDupAggressiveThreshold()
5066 : 2;
5067}
5068
5070 // RVV lacks any support for immediate addressing for stack addresses, so be
5071 // conservative.
5072 unsigned Opcode = MI.getOpcode();
5073 if (!RISCVVPseudosTable::getPseudoInfo(Opcode) &&
5075 return false;
5076 return true;
5077}
5078
5079/// Return true if \p MI is a copy that will be lowered to one or more vmvNr.vs.
5081 const MachineInstr &MI) {
5082 return MI.isCopy() && MI.getOperand(0).getReg().isPhysical() &&
5084 TRI->getMinimalPhysRegClass(MI.getOperand(0).getReg()));
5085}
5086
5087std::optional<std::pair<unsigned, unsigned>>
5089 switch (Opcode) {
5090 default:
5091 return std::nullopt;
5092 case RISCV::PseudoVSPILL2_M1:
5093 case RISCV::PseudoVRELOAD2_M1:
5094 return std::make_pair(2u, 1u);
5095 case RISCV::PseudoVSPILL2_M2:
5096 case RISCV::PseudoVRELOAD2_M2:
5097 return std::make_pair(2u, 2u);
5098 case RISCV::PseudoVSPILL2_M4:
5099 case RISCV::PseudoVRELOAD2_M4:
5100 return std::make_pair(2u, 4u);
5101 case RISCV::PseudoVSPILL3_M1:
5102 case RISCV::PseudoVRELOAD3_M1:
5103 return std::make_pair(3u, 1u);
5104 case RISCV::PseudoVSPILL3_M2:
5105 case RISCV::PseudoVRELOAD3_M2:
5106 return std::make_pair(3u, 2u);
5107 case RISCV::PseudoVSPILL4_M1:
5108 case RISCV::PseudoVRELOAD4_M1:
5109 return std::make_pair(4u, 1u);
5110 case RISCV::PseudoVSPILL4_M2:
5111 case RISCV::PseudoVRELOAD4_M2:
5112 return std::make_pair(4u, 2u);
5113 case RISCV::PseudoVSPILL5_M1:
5114 case RISCV::PseudoVRELOAD5_M1:
5115 return std::make_pair(5u, 1u);
5116 case RISCV::PseudoVSPILL6_M1:
5117 case RISCV::PseudoVRELOAD6_M1:
5118 return std::make_pair(6u, 1u);
5119 case RISCV::PseudoVSPILL7_M1:
5120 case RISCV::PseudoVRELOAD7_M1:
5121 return std::make_pair(7u, 1u);
5122 case RISCV::PseudoVSPILL8_M1:
5123 case RISCV::PseudoVRELOAD8_M1:
5124 return std::make_pair(8u, 1u);
5125 }
5126}
5127
5128bool RISCV::hasEqualFRM(const MachineInstr &MI1, const MachineInstr &MI2) {
5129 int16_t MI1FrmOpIdx =
5130 RISCV::getNamedOperandIdx(MI1.getOpcode(), RISCV::OpName::frm);
5131 int16_t MI2FrmOpIdx =
5132 RISCV::getNamedOperandIdx(MI2.getOpcode(), RISCV::OpName::frm);
5133 if (MI1FrmOpIdx < 0 || MI2FrmOpIdx < 0)
5134 return false;
5135 MachineOperand FrmOp1 = MI1.getOperand(MI1FrmOpIdx);
5136 MachineOperand FrmOp2 = MI2.getOperand(MI2FrmOpIdx);
5137 return FrmOp1.getImm() == FrmOp2.getImm();
5138}
5139
5140std::optional<unsigned>
5141RISCV::getVectorLowDemandedScalarBits(unsigned Opcode, unsigned Log2SEW) {
5142 switch (Opcode) {
5143 default:
5144 return std::nullopt;
5145
5146 // 11.6. Vector Single-Width Shift Instructions
5147 case RISCV::VSLL_VX:
5148 case RISCV::VSRL_VX:
5149 case RISCV::VSRA_VX:
5150 // 12.4. Vector Single-Width Scaling Shift Instructions
5151 case RISCV::VSSRL_VX:
5152 case RISCV::VSSRA_VX:
5153 // Zvbb
5154 case RISCV::VROL_VX:
5155 case RISCV::VROR_VX:
5156 // Only the low lg2(SEW) bits of the shift-amount value are used.
5157 return Log2SEW;
5158
5159 // 11.7 Vector Narrowing Integer Right Shift Instructions
5160 case RISCV::VNSRL_WX:
5161 case RISCV::VNSRA_WX:
5162 // 12.5. Vector Narrowing Fixed-Point Clip Instructions
5163 case RISCV::VNCLIPU_WX:
5164 case RISCV::VNCLIP_WX:
5165 // Zvbb
5166 case RISCV::VWSLL_VX:
5167 // Only the low lg2(2*SEW) bits of the shift-amount value are used.
5168 return Log2SEW + 1;
5169
5170 // 11.1. Vector Single-Width Integer Add and Subtract
5171 case RISCV::VADD_VX:
5172 case RISCV::VSUB_VX:
5173 case RISCV::VRSUB_VX:
5174 // 11.2. Vector Widening Integer Add/Subtract
5175 case RISCV::VWADDU_VX:
5176 case RISCV::VWSUBU_VX:
5177 case RISCV::VWADD_VX:
5178 case RISCV::VWSUB_VX:
5179 case RISCV::VWADDU_WX:
5180 case RISCV::VWSUBU_WX:
5181 case RISCV::VWADD_WX:
5182 case RISCV::VWSUB_WX:
5183 // 11.4. Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions
5184 case RISCV::VADC_VXM:
5185 case RISCV::VADC_VIM:
5186 case RISCV::VMADC_VXM:
5187 case RISCV::VMADC_VIM:
5188 case RISCV::VMADC_VX:
5189 case RISCV::VSBC_VXM:
5190 case RISCV::VMSBC_VXM:
5191 case RISCV::VMSBC_VX:
5192 // 11.5 Vector Bitwise Logical Instructions
5193 case RISCV::VAND_VX:
5194 case RISCV::VOR_VX:
5195 case RISCV::VXOR_VX:
5196 // 11.8. Vector Integer Compare Instructions
5197 case RISCV::VMSEQ_VX:
5198 case RISCV::VMSNE_VX:
5199 case RISCV::VMSLTU_VX:
5200 case RISCV::VMSLT_VX:
5201 case RISCV::VMSLEU_VX:
5202 case RISCV::VMSLE_VX:
5203 case RISCV::VMSGTU_VX:
5204 case RISCV::VMSGT_VX:
5205 // 11.9. Vector Integer Min/Max Instructions
5206 case RISCV::VMINU_VX:
5207 case RISCV::VMIN_VX:
5208 case RISCV::VMAXU_VX:
5209 case RISCV::VMAX_VX:
5210 // 11.10. Vector Single-Width Integer Multiply Instructions
5211 case RISCV::VMUL_VX:
5212 case RISCV::VMULH_VX:
5213 case RISCV::VMULHU_VX:
5214 case RISCV::VMULHSU_VX:
5215 // 11.11. Vector Integer Divide Instructions
5216 case RISCV::VDIVU_VX:
5217 case RISCV::VDIV_VX:
5218 case RISCV::VREMU_VX:
5219 case RISCV::VREM_VX:
5220 // 11.12. Vector Widening Integer Multiply Instructions
5221 case RISCV::VWMUL_VX:
5222 case RISCV::VWMULU_VX:
5223 case RISCV::VWMULSU_VX:
5224 // 11.13. Vector Single-Width Integer Multiply-Add Instructions
5225 case RISCV::VMACC_VX:
5226 case RISCV::VNMSAC_VX:
5227 case RISCV::VMADD_VX:
5228 case RISCV::VNMSUB_VX:
5229 // 11.14. Vector Widening Integer Multiply-Add Instructions
5230 case RISCV::VWMACCU_VX:
5231 case RISCV::VWMACC_VX:
5232 case RISCV::VWMACCSU_VX:
5233 case RISCV::VWMACCUS_VX:
5234 // 11.15. Vector Integer Merge Instructions
5235 case RISCV::VMERGE_VXM:
5236 // 11.16. Vector Integer Move Instructions
5237 case RISCV::VMV_V_X:
5238 // 12.1. Vector Single-Width Saturating Add and Subtract
5239 case RISCV::VSADDU_VX:
5240 case RISCV::VSADD_VX:
5241 case RISCV::VSSUBU_VX:
5242 case RISCV::VSSUB_VX:
5243 // 12.2. Vector Single-Width Averaging Add and Subtract
5244 case RISCV::VAADDU_VX:
5245 case RISCV::VAADD_VX:
5246 case RISCV::VASUBU_VX:
5247 case RISCV::VASUB_VX:
5248 // 12.3. Vector Single-Width Fractional Multiply with Rounding and Saturation
5249 case RISCV::VSMUL_VX:
5250 // 16.1. Integer Scalar Move Instructions
5251 case RISCV::VMV_S_X:
5252 // Zvbb
5253 case RISCV::VANDN_VX:
5254 return 1U << Log2SEW;
5255 }
5256}
5257
5258unsigned RISCV::getRVVMCOpcode(unsigned RVVPseudoOpcode) {
5260 RISCVVPseudosTable::getPseudoInfo(RVVPseudoOpcode);
5261 if (!RVV)
5262 return 0;
5263 return RVV->BaseInstr;
5264}
5265
5266unsigned RISCV::getDestLog2EEW(const MCInstrDesc &Desc, unsigned Log2SEW) {
5267 unsigned DestEEW =
5269 // EEW = 1
5270 if (DestEEW == 0)
5271 return 0;
5272 // EEW = SEW * n
5273 unsigned Scaled = Log2SEW + (DestEEW - 1);
5274 assert(Scaled >= 3 && Scaled <= 6);
5275 return Scaled;
5276}
5277
5278static std::optional<int64_t> getEffectiveImm(const MachineOperand &MO) {
5279 assert(MO.isImm() || MO.getReg().isVirtual());
5280 if (MO.isImm())
5281 return MO.getImm();
5282 const MachineInstr *Def =
5283 MO.getParent()->getMF()->getRegInfo().getVRegDef(MO.getReg());
5284 int64_t Imm;
5285 if (isLoadImm(Def, Imm))
5286 return Imm;
5287 return std::nullopt;
5288}
5289
5290/// Given two VL operands, do we know that LHS <= RHS? Must be used in SSA form.
5292 assert((LHS.isImm() || LHS.getParent()->getMF()->getRegInfo().isSSA()) &&
5293 (RHS.isImm() || RHS.getParent()->getMF()->getRegInfo().isSSA()));
5294 if (LHS.isReg() && RHS.isReg() && LHS.getReg().isVirtual() &&
5295 LHS.getReg() == RHS.getReg())
5296 return true;
5297 if (RHS.isImm() && RHS.getImm() == RISCV::VLMaxSentinel)
5298 return true;
5299 if (LHS.isImm() && LHS.getImm() == 0)
5300 return true;
5301 if (LHS.isImm() && LHS.getImm() == RISCV::VLMaxSentinel)
5302 return false;
5303 std::optional<int64_t> LHSImm = getEffectiveImm(LHS),
5304 RHSImm = getEffectiveImm(RHS);
5305 if (!LHSImm || !RHSImm)
5306 return false;
5307 return LHSImm <= RHSImm;
5308}
5309
5310namespace {
5311class RISCVPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
5312 const MachineInstr *LHS;
5313 const MachineInstr *RHS;
5315
5316public:
5317 RISCVPipelinerLoopInfo(const MachineInstr *LHS, const MachineInstr *RHS,
5319 : LHS(LHS), RHS(RHS), Cond(Cond.begin(), Cond.end()) {}
5320
5321 bool shouldIgnoreForPipelining(const MachineInstr *MI) const override {
5322 // Make the instructions for loop control be placed in stage 0.
5323 // The predecessors of LHS/RHS are considered by the caller.
5324 if (LHS && MI == LHS)
5325 return true;
5326 if (RHS && MI == RHS)
5327 return true;
5328 return false;
5329 }
5330
5331 std::optional<bool> createTripCountGreaterCondition(
5332 int TC, MachineBasicBlock &MBB,
5333 SmallVectorImpl<MachineOperand> &CondParam) override {
5334 // A branch instruction will be inserted as "if (Cond) goto epilogue".
5335 // Cond is normalized for such use.
5336 // The predecessors of the branch are assumed to have already been inserted.
5337 CondParam = Cond;
5338 return {};
5339 }
5340
5341 void setPreheader(MachineBasicBlock *NewPreheader) override {}
5342
5343 void adjustTripCount(int TripCountAdjust) override {}
5344};
5345} // namespace
5346
5347std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo>
5349 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
5351 if (analyzeBranch(*LoopBB, TBB, FBB, Cond, /*AllowModify=*/false))
5352 return nullptr;
5353
5354 // Infinite loops are not supported
5355 if (TBB == LoopBB && FBB == LoopBB)
5356 return nullptr;
5357
5358 // Must be conditional branch
5359 if (FBB == nullptr)
5360 return nullptr;
5361
5362 assert((TBB == LoopBB || FBB == LoopBB) &&
5363 "The Loop must be a single-basic-block loop");
5364
5365 // Normalization for createTripCountGreaterCondition()
5366 if (TBB == LoopBB)
5368
5369 const MachineRegisterInfo &MRI = LoopBB->getParent()->getRegInfo();
5370 auto FindRegDef = [&MRI](MachineOperand &Op) -> const MachineInstr * {
5371 if (!Op.isReg())
5372 return nullptr;
5373 Register Reg = Op.getReg();
5374 if (!Reg.isVirtual())
5375 return nullptr;
5376 return MRI.getVRegDef(Reg);
5377 };
5378
5379 const MachineInstr *LHS = FindRegDef(Cond[1]);
5380 const MachineInstr *RHS = FindRegDef(Cond[2]);
5381 if (LHS && LHS->isPHI())
5382 return nullptr;
5383 if (RHS && RHS->isPHI())
5384 return nullptr;
5385
5386 return std::make_unique<RISCVPipelinerLoopInfo>(LHS, RHS, Cond);
5387}
5388
5389// FIXME: We should remove this if we have a default generic scheduling model.
5391 unsigned RVVMCOpcode = RISCV::getRVVMCOpcode(Opc);
5392 Opc = RVVMCOpcode ? RVVMCOpcode : Opc;
5393 switch (Opc) {
5394 default:
5395 return false;
5396 // Integer div/rem.
5397 case RISCV::DIV:
5398 case RISCV::DIVW:
5399 case RISCV::DIVU:
5400 case RISCV::DIVUW:
5401 case RISCV::REM:
5402 case RISCV::REMW:
5403 case RISCV::REMU:
5404 case RISCV::REMUW:
5405 // Floating-point div/sqrt.
5406 case RISCV::FDIV_H:
5407 case RISCV::FDIV_S:
5408 case RISCV::FDIV_D:
5409 case RISCV::FDIV_H_INX:
5410 case RISCV::FDIV_S_INX:
5411 case RISCV::FDIV_D_INX:
5412 case RISCV::FDIV_D_IN32X:
5413 case RISCV::FSQRT_H:
5414 case RISCV::FSQRT_S:
5415 case RISCV::FSQRT_D:
5416 case RISCV::FSQRT_H_INX:
5417 case RISCV::FSQRT_S_INX:
5418 case RISCV::FSQRT_D_INX:
5419 case RISCV::FSQRT_D_IN32X:
5420 // Vector integer div/rem
5421 case RISCV::VDIV_VV:
5422 case RISCV::VDIV_VX:
5423 case RISCV::VDIVU_VV:
5424 case RISCV::VDIVU_VX:
5425 case RISCV::VREM_VV:
5426 case RISCV::VREM_VX:
5427 case RISCV::VREMU_VV:
5428 case RISCV::VREMU_VX:
5429 // Vector floating-point div/sqrt.
5430 case RISCV::VFDIV_VV:
5431 case RISCV::VFDIV_VF:
5432 case RISCV::VFRDIV_VF:
5433 case RISCV::VFSQRT_V:
5434 case RISCV::VFRSQRT7_V:
5435 return true;
5436 }
5437}
5438
5439bool RISCVInstrInfo::isVRegCopy(const MachineInstr *MI, unsigned LMul) const {
5440 if (MI->getOpcode() != TargetOpcode::COPY)
5441 return false;
5442 const MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
5444
5445 Register DstReg = MI->getOperand(0).getReg();
5446 const TargetRegisterClass *RC = DstReg.isVirtual()
5447 ? MRI.getRegClass(DstReg)
5448 : TRI->getMinimalPhysRegClass(DstReg);
5449
5451 return false;
5452
5453 if (!LMul)
5454 return true;
5455
5456 // TODO: Perhaps we could distinguish segment register classes (e.g. VRN3M2)
5457 // in the future.
5458 auto [RCLMul, RCFractional] =
5460 return (!RCFractional && LMul == RCLMul) || (RCFractional && LMul == 1);
5461}
5462
5464 if (MI.memoperands_empty())
5465 return false;
5466
5467 MachineMemOperand *MMO = *(MI.memoperands_begin());
5468 if (!MMO->isNonTemporal())
5469 return false;
5470
5471 return true;
5472}
5473
5475 const MachineBasicBlock::iterator &To) {
5476 assert(To == From.getParent()->end() || From.getParent() == To->getParent());
5477 SmallVector<Register> PhysUses, PhysDefs;
5478 for (const MachineOperand &MO : From.all_uses())
5479 if (MO.getReg().isPhysical())
5480 PhysUses.push_back(MO.getReg());
5481 for (const MachineOperand &MO : From.all_defs())
5482 if (MO.getReg().isPhysical())
5483 PhysDefs.push_back(MO.getReg());
5484 bool SawStore = false;
5485 for (auto II = std::next(From.getIterator()); II != To; II++) {
5486 for (Register PhysReg : PhysUses)
5487 if (II->definesRegister(PhysReg, nullptr))
5488 return false;
5489 for (Register PhysReg : PhysDefs)
5490 if (II->definesRegister(PhysReg, nullptr) ||
5491 II->readsRegister(PhysReg, nullptr))
5492 return false;
5493 if (II->mayStore()) {
5494 SawStore = true;
5495 break;
5496 }
5497 }
5498 return From.isSafeToMove(SawStore);
5499}
MachineInstrBuilder MachineInstrBuilder & DefMI
static bool forwardCopyWillClobberTuple(unsigned DestReg, unsigned SrcReg, unsigned NumRegs)
static void parseCondBranch(MachineInstr *LastInst, MachineBasicBlock *&Target, SmallVectorImpl< MachineOperand > &Cond)
@ MachineOutlinerTailCall
Emit a save, restore, call, and return.
@ MachineOutlinerRegSave
Emit a call and tail-call.
@ MachineOutlinerDefault
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
SmallVector< int16_t, MAX_SRC_OPERANDS_NUM > OperandIndices
@ Scaled
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
basic Basic Alias true
#define X(NUM, ENUM, NAME)
Definition ELF.h:853
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
const HexagonInstrInfo * TII
#define _
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
This file provides utility analysis objects describing memory locations.
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
static bool cannotInsertTailCall(const MachineBasicBlock &MBB)
#define CASE_VFMA_CHANGE_OPCODE_SPLATS(OLDOP, NEWOP)
#define CASE_FP_WIDEOP_CHANGE_OPCODE_LMULS_ALT(OP)
#define CASE_FP_WIDEOP_OPCODE_LMULS(OP)
#define CASE_OPERAND_SIMM(NUM)
static std::optional< unsigned > getLMULForRVVWholeLoadStore(unsigned Opcode)
#define CASE_VFMA_CHANGE_OPCODE_VV(OLDOP, NEWOP)
static unsigned getFPFusedMultiplyOpcode(unsigned RootOpc, unsigned Pattern)
std::optional< unsigned > getFoldedOpcode(MachineFunction &MF, MachineInstr &MI, ArrayRef< unsigned > Ops, const RISCVSubtarget &ST)
#define RVV_OPC_LMUL_CASE(OPC, INV)
#define CASE_FP_WIDEOP_CHANGE_OPCODE_LMULS(OP)
static void combineFPFusedMultiply(MachineInstr &Root, MachineInstr &Prev, unsigned Pattern, SmallVectorImpl< MachineInstr * > &InsInstrs, SmallVectorImpl< MachineInstr * > &DelInstrs)
static unsigned getAddendOperandIdx(unsigned Pattern)
#define CASE_RVV_OPCODE_UNMASK(OP)
#define CASE_WIDEOP_CHANGE_OPCODE_LMULS(OP)
static cl::opt< bool > PreferWholeRegisterMove("riscv-prefer-whole-register-move", cl::init(false), cl::Hidden, cl::desc("Prefer whole register move for vector registers."))
#define CASE_VFMA_SPLATS(OP)
unsigned getPredicatedOpcode(unsigned Opcode)
#define CASE_FP_WIDEOP_OPCODE_LMULS_ALT(OP)
#define CASE_WIDEOP_OPCODE_LMULS(OP)
static bool isMIReadsReg(const MachineInstr &MI, const TargetRegisterInfo *TRI, MCRegister RegNo)
#define OPCODE_LMUL_MASK_CASE(OPC)
#define CASE_OPERAND_UIMM_LSB_ZEROS(BITS, SUFFIX)
static bool isFSUB(unsigned Opc)
#define CASE_VMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, TYPE)
#define CASE_RVV_OPCODE(OP)
static std::optional< int64_t > getEffectiveImm(const MachineOperand &MO)
#define CASE_VFMA_OPCODE_VV(OP)
static cl::opt< bool > OutlinerEnableRegSave("riscv-outliner-regsave", cl::init(true), cl::Hidden, cl::desc("Enable RegSave strategy in machine outliner (save X5 to a " "temporary register when X5 is live across outlined calls)."))
MachineOutlinerConstructionID
#define CASE_RVV_OPCODE_WIDEN(OP)
static unsigned getLoadPredicatedOpcode(unsigned Opcode)
static unsigned getSHXADDUWShiftAmount(unsigned Opc)
#define CASE_VMA_OPCODE_LMULS(OP, TYPE)
static bool isConvertibleToVMV_V_V(const RISCVSubtarget &STI, const MachineBasicBlock &MBB, MachineBasicBlock::const_iterator MBBI, MachineBasicBlock::const_iterator &DefMBBI, RISCVVType::VLMUL LMul)
static bool isFMUL(unsigned Opc)
static unsigned getInverseXqcicmOpcode(unsigned Opcode)
static bool getFPPatterns(MachineInstr &Root, SmallVectorImpl< unsigned > &Patterns, bool DoRegPressureReduce)
#define OPCODE_LMUL_CASE(OPC)
#define CASE_OPERAND_UIMM(NUM)
static Register findRegisterToSaveX5To(outliner::Candidate &C, const TargetRegisterInfo &TRI)
static bool canCombineShiftIntoShXAdd(const MachineBasicBlock &MBB, const MachineOperand &MO, unsigned OuterShiftAmt)
Utility routine that checks if.
static bool isCandidatePatchable(const MachineBasicBlock &MBB)
static bool isFADD(unsigned Opc)
static void genShXAddAddShift(MachineInstr &Root, unsigned AddOpIdx, SmallVectorImpl< MachineInstr * > &InsInstrs, SmallVectorImpl< MachineInstr * > &DelInstrs, DenseMap< Register, unsigned > &InstrIdxForVirtReg)
static bool isLoadImm(const MachineInstr *MI, int64_t &Imm)
static bool isMIModifiesReg(const MachineInstr &MI, const TargetRegisterInfo *TRI, MCRegister RegNo)
#define CASE_RVV_OPCODE_LMUL(OP, LMUL)
static bool canCombineFPFusedMultiply(const MachineInstr &Root, const MachineOperand &MO, bool DoRegPressureReduce)
static bool getSHXADDPatterns(const MachineInstr &Root, SmallVectorImpl< unsigned > &Patterns)
static bool getFPFusedMultiplyPatterns(MachineInstr &Root, SmallVectorImpl< unsigned > &Patterns, bool DoRegPressureReduce)
static cl::opt< MachineTraceStrategy > ForceMachineCombinerStrategy("riscv-force-machine-combiner-strategy", cl::Hidden, cl::desc("Force machine combiner to use a specific strategy for machine " "trace metrics evaluation."), cl::init(MachineTraceStrategy::TS_NumStrategies), cl::values(clEnumValN(MachineTraceStrategy::TS_Local, "local", "Local strategy."), clEnumValN(MachineTraceStrategy::TS_MinInstrCount, "min-instr", "MinInstrCount strategy.")))
static unsigned getSHXADDShiftAmount(unsigned Opc)
#define CASE_RVV_OPCODE_MASK(OP)
#define RVV_OPC_LMUL_MASK_CASE(OPC, INV)
static MachineInstr * canFoldAsPredicatedOp(Register Reg, const MachineRegisterInfo &MRI, const TargetInstrInfo *TII, const RISCVSubtarget &STI)
Identify instructions that can be folded into a CCMOV instruction, and return the defining instructio...
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
This file declares the machine register scavenger class.
static bool memOpsHaveSameBasePtr(const MachineInstr &MI1, ArrayRef< const MachineOperand * > BaseOps1, const MachineInstr &MI2, ArrayRef< const MachineOperand * > BaseOps2)
This file contains some templates that are useful if you are working with the STL at all.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:483
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static bool canCombine(MachineBasicBlock &MBB, MachineOperand &MO, unsigned CombineOpc=0)
static cl::opt< unsigned > CacheLineSize("cache-line-size", cl::init(0), cl::Hidden, cl::desc("Use this to override the target cache line size when " "specified by the user."))
Value * RHS
Value * LHS
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
const T & front() const
Get the first element.
Definition ArrayRef.h:144
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
static LLVM_ABI DILocation * getMergedLocation(DILocation *LocA, DILocation *LocB)
Attempts to merge LocA and LocB into a single location; see DebugLoc::getMergedLocation for more deta...
bool isBigEndian() const
Definition DataLayout.h:218
A debug info location.
Definition DebugLoc.h:123
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:239
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:711
LiveInterval - This class represents the liveness of a register, or stack slot.
LiveInterval & getInterval(Register Reg)
SlotIndex ReplaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI)
const Segment * getSegmentContaining(SlotIndex Idx) const
Return the segment that contains the specified index, or null if there is none.
LLVM_ABI void replaceKillInstruction(Register Reg, MachineInstr &OldMI, MachineInstr &NewMI)
replaceKillInstruction - Update register kill info by replacing a kill instruction with a new one.
bool hasValue() const
static LocationSize precise(uint64_t Value)
TypeSize getValue() const
MCInstBuilder & addReg(MCRegister Reg)
Add a new register operand.
MCInstBuilder & addImm(int64_t Val)
Add a new integer immediate operand.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Describe properties that are true of each instruction in the target description file.
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
bool isConditionalBranch() const
Return true if this is a branch which may fall through to the next instruction or may transfer contro...
This holds information about one operand of a machine instruction, indicating the register class for ...
Definition MCInstrDesc.h:86
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
const FeatureBitset & getFeatureBits() const
Set of metadata that should be preserved when using BuildMI().
MachineInstrBundleIterator< const MachineInstr > const_iterator
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
MachineInstrBundleIterator< const MachineInstr, true > const_reverse_iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setStackID(int ObjectIdx, uint8_t ID)
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & setMemRefs(ArrayRef< MachineMemOperand * > MMOs) const
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & cloneMemRefs(const MachineInstr &OtherMI) const
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
const MachineInstrBuilder & copyImplicitOps(const MachineInstr &OtherMI) const
Copy all the implicit operands from OtherMI onto this one.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
reverse_iterator getReverse() const
Get a reverse iterator to the same node.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isReturn(QueryType Type=AnyInBundle) const
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
const MachineBasicBlock * getParent() const
filtered_mop_range all_defs()
Returns an iterator range over all operands that are (explicit or implicit) register defs.
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
LLVM_ABI bool isSafeToMove(bool &SawStore) const
Return true if it is safe to move this instruction.
LLVM_ABI unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
bool modifiesRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr modifies (fully define or partially define) the specified register.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
LLVM_ABI bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore,...
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
LLVM_ABI bool hasOrderedMemoryRef() const
Return true if this instruction may have an ordered or volatile memory reference, or if the informati...
LLVM_ABI const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
ArrayRef< MachineMemOperand * > memoperands() const
Access to memory operands of the instruction.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
filtered_mop_range all_uses()
Returns an iterator range over all operands that are (explicit or implicit) register uses.
const MachineOperand & getOperand(unsigned i) const
uint32_t getFlags() const
Return the MI flags bitvector.
LLVM_ABI void clearKillInfo()
Clears kill flags on all operands.
A description of a memory reference used in the backend.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
This class contains meta information specific to a module.
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static MachineOperand CreateImm(int64_t Val)
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Register getReg() const
getReg - Returns the register number.
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
@ MO_Immediate
Immediate operand.
@ MO_Register
Register operand.
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.
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI void clearKillFlags(Register Reg) const
clearKillFlags - Iterate over all the uses of the given register and clear the kill flag from the Mac...
LLVM_ABI MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool hasOneUse(Register RegNo) const
hasOneUse - Return true if there is exactly one instruction using the specified register.
LLVM_ABI void clearVirtRegs()
clearVirtRegs - Remove all virtual registers (after physreg assignment).
const TargetRegisterInfo * getTargetRegisterInfo() const
LLVM_ABI bool isConstantPhysReg(MCRegister PhysReg) const
Returns true if PhysReg is unallocatable and constant throughout the function.
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 MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
MI-level patchpoint operands.
Definition StackMaps.h:77
uint32_t getNumPatchBytes() const
Return the number of patchable bytes the given patchpoint should emit.
Definition StackMaps.h:105
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool IsKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
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...
MachineInstr * convertToThreeAddress(MachineInstr &MI, LiveVariables *LV, LiveIntervals *LIS) const override
Register isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex) const override
std::optional< std::unique_ptr< outliner::OutlinedFunction > > getOutliningCandidateInfo(const MachineModuleInfo &MMI, std::vector< outliner::Candidate > &RepeatedSequenceLocs, unsigned MinRepeats) const override
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
void genAlternativeCodeSequence(MachineInstr &Root, unsigned Pattern, SmallVectorImpl< MachineInstr * > &InsInstrs, SmallVectorImpl< MachineInstr * > &DelInstrs, DenseMap< Register, unsigned > &InstrIdxForVirtReg) const override
void movImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, Register DstReg, uint64_t Val, MachineInstr::MIFlag Flag=MachineInstr::NoFlags, bool DstRenamable=false, bool DstIsDead=false) const
MachineInstr * emitLdStWithAddr(MachineInstr &MemI, const ExtAddrMode &AM) const override
void mulImm(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator II, const DebugLoc &DL, Register DestReg, uint32_t Amt, MachineInstr::MIFlag Flag) const
Generate code to multiply the value in DestReg by Amt - handles all the common optimizations for this...
static bool isPairableLdStInstOpc(unsigned Opc)
Return true if pairing the given load or store may be paired with another.
RISCVInstrInfo(const RISCVSubtarget &STI)
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DstReg, int FrameIndex, const TargetRegisterClass *RC, Register VReg, unsigned SubReg=0, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
bool isFunctionSafeToOutlineFrom(MachineFunction &MF, bool OutlineFromLinkOnceODRs) const override
std::unique_ptr< TargetInstrInfo::PipelinerLoopInfo > analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const override
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &dl, int *BytesAdded=nullptr) const override
bool hasReassociableSibling(const MachineInstr &Inst, bool &Commuted) const override
static bool isLdStSafeToPair(const MachineInstr &LdSt, const TargetRegisterInfo *TRI)
void copyPhysRegVector(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, MCRegister DstReg, MCRegister SrcReg, bool KillSrc, const TargetRegisterClass *RegClass) const
bool isReMaterializableImpl(const MachineInstr &MI) const override
MachineInstr * optimizeSelect(MachineInstr &MI, SmallPtrSetImpl< MachineInstr * > &SeenMIs, bool) const override
bool isVRegCopy(const MachineInstr *MI, unsigned LMul=0) const
Return true if MI is a COPY to a vector register of a specific LMul, or any kind of vector registers ...
bool canFoldIntoAddrMode(const MachineInstr &MemI, Register Reg, const MachineInstr &AddrI, ExtAddrMode &AM) const override
void insertIndirectBranch(MachineBasicBlock &MBB, MachineBasicBlock &NewDestBB, MachineBasicBlock &RestoreBB, const DebugLoc &DL, int64_t BrOffset, RegScavenger *RS) const override
bool isAsCheapAsAMove(const MachineInstr &MI) const override
bool verifyInstruction(const MachineInstr &MI, StringRef &ErrInfo) const override
bool getMemOperandWithOffsetWidth(const MachineInstr &LdSt, const MachineOperand *&BaseOp, int64_t &Offset, LocationSize &Width, const TargetRegisterInfo *TRI) const
unsigned getTailDuplicateSize(CodeGenOptLevel OptLevel) const override
void getReassociateOperandIndices(const MachineInstr &Root, unsigned Pattern, std::array< unsigned, 5 > &OperandIndices) const override
const RISCVSubtarget & STI
Register isStoreToStackSlot(const MachineInstr &MI, int &FrameIndex) const override
std::optional< unsigned > getInverseOpcode(unsigned Opcode) const override
bool simplifyInstruction(MachineInstr &MI) const override
ArrayRef< std::pair< unsigned, const char * > > getSerializableDirectMachineOperandTargetFlags() const override
outliner::InstrType getOutliningTypeImpl(const MachineModuleInfo &MMI, MachineBasicBlock::iterator &MBBI, unsigned Flags) const override
MachineTraceStrategy getMachineCombinerTraceStrategy() const override
unsigned getInstSizeInBytes(const MachineInstr &MI) const override
std::optional< RegImmPair > isAddImmediate(const MachineInstr &MI, Register Reg) const override
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
ArrayRef< std::pair< MachineMemOperand::Flags, const char * > > getSerializableMachineMemOperandTargetFlags() const override
MCInst getNop() const override
bool analyzeCandidate(outliner::Candidate &C) const
bool isMBBSafeToOutlineFrom(MachineBasicBlock &MBB, unsigned &Flags) const override
bool getMemOperandsWithOffsetWidth(const MachineInstr &MI, SmallVectorImpl< const MachineOperand * > &BaseOps, int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width, const TargetRegisterInfo *TRI) const override
void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF, const outliner::OutlinedFunction &OF) const override
bool requiresNTLHint(const MachineInstr &MI) const
Return true if the instruction requires an NTL hint to be emitted.
void finalizeInsInstrs(MachineInstr &Root, unsigned &Pattern, SmallVectorImpl< MachineInstr * > &InsInstrs) const override
std::pair< unsigned, unsigned > decomposeMachineOperandsTargetFlags(unsigned TF) const override
MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const override
bool hasReassociableOperands(const MachineInstr &Inst, const MachineBasicBlock *MBB) const override
MachineBasicBlock * getBranchDestBlock(const MachineInstr &MI) const override
std::string createMIROperandComment(const MachineInstr &MI, const MachineOperand &Op, unsigned OpIdx, const TargetRegisterInfo *TRI) const override
bool shouldOutlineFromFunctionByDefault(MachineFunction &MF) const override
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, Register DstReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx1, unsigned &SrcOpIdx2) const override
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
MachineBasicBlock::iterator insertOutlinedCall(Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It, MachineFunction &MF, outliner::Candidate &C) const override
MachineInstr * foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI, ArrayRef< unsigned > Ops, int FrameIndex, MachineInstr *&CopyMI, LiveIntervals *LIS=nullptr, VirtRegMap *VRM=nullptr) const override
bool isBranchOffsetInRange(unsigned BranchOpc, int64_t BrOffset) const override
static RISCVCC::CondCode getCondFromBranchOpc(unsigned Opc)
bool isAssociativeAndCommutative(const MachineInstr &Inst, bool Invert) const override
CombinerObjective getCombinerObjective(unsigned Pattern) const override
bool isHighLatencyDef(int Opc) const override
static bool evaluateCondBranch(RISCVCC::CondCode CC, int64_t C0, int64_t C1)
Return the result of the evaluation of C0 CC C1, where CC is a RISCVCC::CondCode.
bool getMachineCombinerPatterns(MachineInstr &Root, SmallVectorImpl< unsigned > &Patterns, bool DoRegPressureReduce) const override
bool optimizeCondBranch(MachineInstr &MI) const override
std::optional< DestSourcePair > isCopyInstrImpl(const MachineInstr &MI) const override
static bool isFromLoadImm(const MachineRegisterInfo &MRI, const MachineOperand &Op, int64_t &Imm)
Return true if the operand is a load immediate instruction and sets Imm to the immediate value.
bool shouldClusterMemOps(ArrayRef< const MachineOperand * > BaseOps1, int64_t Offset1, bool OffsetIsScalable1, ArrayRef< const MachineOperand * > BaseOps2, int64_t Offset2, bool OffsetIsScalable2, unsigned ClusterSize, unsigned NumBytes) const override
bool areMemAccessesTriviallyDisjoint(const MachineInstr &MIa, const MachineInstr &MIb) const override
RISCVMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private RISCV-...
const RISCVRegisterInfo * getRegisterInfo() const override
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
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
MI-level stackmap operands.
Definition StackMaps.h:36
uint32_t getNumPatchBytes() const
Return the number of patchable bytes the given stackmap should emit.
Definition StackMaps.h:51
MI-level Statepoint operands.
Definition StackMaps.h:159
uint32_t getNumPatchBytes() const
Return the number of patchable bytes the given statepoint should emit.
Definition StackMaps.h:208
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Object returned by analyzeLoopForPipelining.
TargetInstrInfo - Interface to description of machine instruction set.
virtual bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx1, unsigned &SrcOpIdx2) const
Returns true iff the routine could find two commutable operands in the given machine instruction.
virtual bool hasReassociableOperands(const MachineInstr &Inst, const MachineBasicBlock *MBB) const
Return true when \P Inst has reassociable operands in the same \P MBB.
virtual void genAlternativeCodeSequence(MachineInstr &Root, unsigned Pattern, SmallVectorImpl< MachineInstr * > &InsInstrs, SmallVectorImpl< MachineInstr * > &DelInstrs, DenseMap< Register, unsigned > &InstIdxForVirtReg) const
When getMachineCombinerPatterns() finds patterns, this function generates the instructions that could...
virtual bool getMachineCombinerPatterns(MachineInstr &Root, SmallVectorImpl< unsigned > &Patterns, bool DoRegPressureReduce) const
Return true when there is potentially a faster code sequence for an instruction chain ending in Root.
virtual bool isReMaterializableImpl(const MachineInstr &MI) const
For instructions with opcodes for which the M_REMATERIALIZABLE flag is set, this hook lets the target...
virtual bool isMBBSafeToOutlineFrom(MachineBasicBlock &MBB, unsigned &Flags) const
Optional target hook that returns true if MBB is safe to outline from, and returns any target-specifi...
virtual void getReassociateOperandIndices(const MachineInstr &Root, unsigned Pattern, std::array< unsigned, 5 > &OperandIndices) const
The returned array encodes the operand index for each parameter because the operands may be commuted;...
virtual CombinerObjective getCombinerObjective(unsigned Pattern) const
Return the objective of a combiner pattern.
virtual MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const
This method commutes the operands of the given machine instruction MI.
virtual bool hasReassociableSibling(const MachineInstr &Inst, bool &Commuted) const
Return true when \P Inst has reassociable sibling.
virtual std::string createMIROperandComment(const MachineInstr &MI, const MachineOperand &Op, unsigned OpIdx, const TargetRegisterInfo *TRI) const
const MCAsmInfo & getMCAsmInfo() const
Return target specific asm information.
const uint8_t TSFlags
Configurable target specific flags.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
Target - Wrapper for Target specific information.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
static constexpr TypeSize getZero()
Definition TypeSize.h:349
static constexpr TypeSize getScalable(ScalarTy MinimumSize)
Definition TypeSize.h:346
self_iterator getIterator()
Definition ilist_node.h:123
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
CondCode getInverseBranchCondition(CondCode)
unsigned getInverseBranchOpcode(unsigned BCC)
unsigned getBrCond(CondCode CC, unsigned SelectOpc=0)
static bool isValidRoundingMode(unsigned Mode)
static StringRef roundingModeToString(RoundingMode RndMode)
static unsigned getVecPolicyOpNum(const MCInstrDesc &Desc)
static bool usesMaskPolicy(uint64_t TSFlags)
static bool hasRoundModeOp(uint64_t TSFlags)
static unsigned getVLOpNum(const MCInstrDesc &Desc)
static bool hasVLOp(uint64_t TSFlags)
static MCRegister getTailExpandUseRegNo(const FeatureBitset &FeatureBits)
static int getFRMOpNum(const MCInstrDesc &Desc)
static int getVXRMOpNum(const MCInstrDesc &Desc)
static bool hasVecPolicyOp(uint64_t TSFlags)
static bool usesVXRM(uint64_t TSFlags)
static bool isRVVWideningReduction(uint64_t TSFlags)
static unsigned getSEWOpNum(const MCInstrDesc &Desc)
static bool hasSEWOp(uint64_t TSFlags)
static bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc)
InstSeq generateInstSeq(int64_t Val, const MCSubtargetInfo &STI)
SmallVector< Inst, 8 > InstSeq
Definition RISCVMatInt.h:43
@ OPERAND_UIMMLOG2XLEN_NONZERO
@ OPERAND_UIMM10_LSB00_NONZERO
@ OPERAND_SIMM10_LSB0000_NONZERO
static unsigned getNF(uint8_t TSFlags)
static RISCVVType::VLMUL getLMul(uint8_t TSFlags)
static bool isTailAgnostic(unsigned VType)
LLVM_ABI void printXSfmmVType(unsigned VType, raw_ostream &OS)
LLVM_ABI std::pair< unsigned, bool > decodeVLMUL(VLMUL VLMul)
static bool isValidSEW(unsigned SEW)
static bool isValidVType(unsigned VType)
LLVM_ABI void printVType(unsigned VType, raw_ostream &OS)
static bool isValidXSfmmVType(unsigned VTypeI)
static unsigned getSEW(unsigned VType)
static VLMUL getVLMUL(unsigned VType)
static bool isValidRoundingMode(unsigned Mode)
static StringRef roundingModeToString(RoundingMode RndMode)
bool hasEqualFRM(const MachineInstr &MI1, const MachineInstr &MI2)
bool isVLKnownLE(const MachineOperand &LHS, const MachineOperand &RHS)
Given two VL operands, do we know that LHS <= RHS?
unsigned getRVVMCOpcode(unsigned RVVPseudoOpcode)
unsigned getDestLog2EEW(const MCInstrDesc &Desc, unsigned Log2SEW)
std::optional< unsigned > getVectorLowDemandedScalarBits(unsigned Opcode, unsigned Log2SEW)
std::optional< std::pair< unsigned, unsigned > > isRVVSpillForZvlsseg(unsigned Opcode)
static constexpr unsigned RVVBitsPerBlock
bool isRVVSpill(const MachineInstr &MI)
static constexpr unsigned RVVBytesPerBlock
static constexpr int64_t VLMaxSentinel
bool isVectorCopy(const TargetRegisterInfo *TRI, const MachineInstr &MI)
Return true if MI is a copy that will be lowered to one or more vmvNr.vs.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
InstrType
Represents how an instruction should be mapped by the outliner.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
@ Offset
Definition DWP.cpp:557
@ SHXADD_ADD_SLLI_OP2
@ SHXADD_ADD_SLLI_OP1
MachineTraceStrategy
Strategies for selecting traces.
@ TS_MinInstrCount
Select the trace through a block that has the fewest instructions.
@ TS_Local
Select the trace that contains only the current basic block.
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:1738
static const MachineMemOperand::Flags MONontemporalBit1
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
RegState
Flags to represent properties of register accesses.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Dead
Unused definition.
@ Kill
The last use of a register.
@ Undef
Value of the register doesn't matter.
@ Define
Register definition.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2553
bool isValidAtomicOrdering(Int I)
constexpr RegState getKillRegState(bool B)
static const MachineMemOperand::Flags MONontemporalBit0
constexpr RegState getDeadRegState(bool B)
Op::Description Desc
unsigned M1(unsigned Val)
Definition VE.h:377
constexpr bool has_single_bit(T Value) noexcept
Definition bit.h:149
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:331
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
constexpr RegState getRenamableRegState(bool B)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr RegState getDefRegState(bool B)
CombinerObjective
The combiner's goal may differ based on which pattern it is attempting to optimize.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
int isShifted359(T Value, int &Shift)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition MathExtras.h:182
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2191
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
Definition MathExtras.h:198
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:876
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Used to describe addressing mode similar to ExtAddrMode in CodeGenPrepare.
This represents a simple continuous liveness interval for a value.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
static bool isRVVRegClass(const TargetRegisterClass *RC)
Used to describe a register and immediate addition.
An individual sequence of instructions to be replaced with a call to an outlined function.
MachineFunction * getMF() const
The information necessary to create an outlined function for some class of candidate.